Homework0 Questions Template
Homework0 Questions Template
Homework 0 Questions
Instructions
• 5 questions.
• This assignment is fixed length, and the pages have been assigned for you in
Gradescope. As a result, please do NOT add any new pages. We will provide
ample room for you to answer the questions. If you really wish for more space,
please add a page at the end of the document.
• We do NOT expect you to fill up each page with your answer. Some answers
will only be a few sentences long, and that is okay.
• When you are finished, compile this document to a PDF and submit it directly to
Gradescope.
For each of the following, complete the task and check the box to mark it as done.
Set up an editing environment (VSCode), get it to use your python virtual environ-
ment, and know how to debug within it by setting breakpoints.
1
Homework 0 Questions CSCI 1430
Questions
Q1: Please find and read the course collaboration policy on the course website and
mark whether each of the following scenarios violates the policy.
LaTeX: To fill in boxes, replace ‘\square’ with ‘\blacksquare’ for your answer.
(a) Another CSCI1430 student looking at your code to help you debug, after you have
spent time trying to tackle the bug or have come to TA office hours/Ed.
Acceptable
Violation
(b) Using the result images from another student’s code for your write up because
your code is broken.
Acceptable
Violation
(c) Googling third party sites to clarify concepts for written and code assignments,
with proper citation.
Acceptable
Violation
(d) A student who has previously taken the course and is not currently a TA sharing
code with you to help you get through a bug.
Acceptable
Violation
2
Homework 0 Questions CSCI 1430
Q2.1: Computer vision is all around us, sometimes in surprising ways. If you could
have any computer vision related superpower, with no limitations, what would it be?
How would you use it? [2-3 sentences]
Q2.2: All students enter the course with different backgrounds in socially responsi-
ble computing. Please first review our ethics primer that introduces basic concepts to
everyone.
Please list at least three values that are at stake if you use your superpower as intended (or
if your archenemy misused your superpower). As a utilitarian, how would you evaluate
your new superpower? As a Kantian deontologist? To what extent do you agree with
each perspective and why? In evaluating your superpower, did you find any limitations
to these two ethical frameworks? [6-7 sentences]
3
Homework 0 Questions CSCI 1430
We wish to set all pixels that have a value of 50 or less to 0, to remove some of the lower-
intensity haze around the bright lights. However, our code only works on single-channel
grayscale images.
from skimage import io
A = io.imread(’grizzlypeakg.png’)
(height,width) = A.shape
for i in range(height):
for j in range(width):
if A[i,j] <= 50 :
A[i,j] = 0
How could we convert it to handle color images using another for loop? Please include
your code.
Image: grizzlypeak.jpg (also in images folder) is a color version of the same image.
4
Homework 0 Questions CSCI 1430
Q3.2: Next, imagine we wanted to process 1000 images in the same way, but we
weren’t sure if our program was fast enough in execution time to cope with that many
images.
(a) Please time your code execution for a single image! We learned how to time execution
in the Python tutorial.
However, let’s not assume that the time taken for one image ×1000 will equal 1000
image computations, as single short tasks on multitasking computers often take variable
time. Instead, compute the time on a smaller number, say, 10 images, and compute the
average time for a single image.
When measuring the time, you can ignore file loading. To do so, you should either write
your code in a way such that you make copies of the loaded image inside the loop or in a
way such that you load the image in a loop, but only time the modification. You can also
assume that all images are of equal resolution to our initial image—we only need to use
grizzlypeak.jpg for this question.
(b) You might find the code would be too slow to handle 1000 images—why is it slow?
Let’s speed it up. Using what we learned in our Python tutorial, write code for a sped-up
version.
(c) Time the execution of your faster code for a single image—use an appropriate
number of images to get a reliable estimate. How much faster is the new version, as a
multiplicative factor? (eg., 2×, 5×.)
(b)
# Your code goes here
5
Homework 0 Questions CSCI 1430
6
Homework 0 Questions CSCI 1430
Q4: We wish to darken an image by editing the values in its matrix. But, when trying
to visualize the result, we see some “errors”.
I = io.imread(’gigi.jpg’).astype(np.float32)
I = I - 50
plt.imshow( I )
plt.show()
What is incorrect with this approach? How can it be fixed while maintaining the same
intended operation? Please include your code as well as the original image and result
image.
7
Homework 0 Questions CSCI 1430
8
Homework 0 Questions CSCI 1430
Q5.1: Debugging—Control Flow Next, please show us that you can use the debugger
within VSCode—we’d like to make sure everyone is set up and can use it correctly.
Imagine our task is to create a crop of an image that starts at the center of the image
and extends to the lower right corner of the image. If all goes well, we should only see
content from the lower right region of the original image.
origImage = io.imread(’gigi.jpg’)
(height, width, channels) = origImage.shape
startCropX = width % 2
startCropY = height % 2
croppedImage = origImage[startCropY:, startCropX:]
plt.imshow(croppedImage)
plt.show()
Create a new python file in the same directory as the image, and copy in the above code
block. Then, open the file in VSCode, and execute the code within a debugging session
by pressing F5 (or ‘Run → Start Debugging’). At the prompt, we wish to ‘Debug the
currently active Python file’.
The output is not currently what we want, so let’s stop execution and then identify the
bug in this program:
1. First, set a breakpoint at line 7 and then re-execute the code within a debugging
session.
2. Inspect the ‘startCropX’ variable either by looking at the left-hand Variables panel,
or by mouse hovering over the variable in the text editor. What should it be?
3. Execute line 7 of code by ‘stepping over’ the current line (F10, or ‘Run → Step
Over). We should now be about to execute line 8.
At this point, you might have an idea of how to fix the code. But, before stopping
execution and editing the file, let’s test out our hypothesis in the ‘Debug Console’ during
debugging.
3. Assign the right value to ‘startCropX’ within the Debug Console. Notice how the
value updated in the Variables panel.
9
Homework 0 Questions CSCI 1430
5. From this point, execute the rest of the code by Continuing beyond our current
paused position in the code. Press F5 to Continue (or ‘Run → Continue’).
A5.1: (a) Re-execute the debugger, and capture a screenshot showing your use of the
Debug Console and inspection of a variable. Please include it below.
(b) Below in writing, please describe what caused the bug. In addition to your written
response, please also write the corrected lines as your answer below.
# Put the correct code here.
10
Homework 0 Questions CSCI 1430
11
Homework 0 Questions CSCI 1430
Q5.2: Debugging—Exceptions This program should print out the maximum value in
the matrix obtained by multiplying a random non-square matrix with its transpose.
Here, we’re using some numpy functions that may be new to us, but they each have
self-explanatory names.
import numpy as np
from numpy import random as r
mat_1 = r.rand(200,150)
mat_2 = mat_1
np.transpose(mat_2)
mat_3 = np.matmul(mat_1, mat_2)
mat_max = np.max(mat_3)
Run the code in a debugging session, note the exception, and inspect the variables. Form
a hypothesis for the error, and use the Debug Console to test that it prevents the exception.
Capture a screenshot of your session showing us the issue and your fix.
Hint: Remember rules about matrix multiplication. What should the dimensions of each
matrix be? Use the debugger to notice how the shapes of the images do or do not change.
(b) Describe what caused the bug. You can write the corrected lines as your answer
below.
# Put the correct code here.
12
Homework 0 Questions CSCI 1430
13