[Solved] ECE324-Assignment 1 Python Review; NumPy, Matplotlib, Image Representation

$25

File Name: ECE324_Assignment_1_Python_Review__NumPy__Matplotlib__Image_Representation.zip
File Size: 697.08 KB

SKU: [Solved] ECE324-Assignment 1 Python Review; NumPy, Matplotlib, Image Representation Category: Tag:
5/5 - (1 vote)

1 Coding & NumPy Exercise

The purpose of this section is to get you re-used to the basics of Python, and the use of helpful Python libraries. In the first part of the assignment, you will be manipulating arrays using NumPy input functions, computing with arrays using for-loops, and then doing the same thing using the built-in NumPy functions. You will need the files matrix.csv and vector.npy which can be found where you downloaded this assignment.

Write a Python program in the file part1.py to accomplish the following tasks:

  1. Load the csv file into a NumPy array variable called matrix, using the numpy.loadtxt function.
  2. Load the npy file into a NumPy array variable called vector, using the numpy.load function.
  3. Perform matrix multiplication: output = matrixvector using for loops to iterate through the column and rows. Do not use any built-in NumPy functions. Save output variable into a CSV file called csv using numpy.savetxt.
  4. Perform matrix multiplication:output_2 = matrix vector by using the built in NumPy function dot. Save output_2 variable into a NumPy Array (.npy) file called output_dot.npy using numpy.save.
  5. As a way to test for consistency, make sure that the outputs match by computing the difference between output and output_2 and saving it into a CSV file called csv.

Answer the following question: If the two files you compared above are the same, does it prove that your code is correct? Explain your answer.

2 Callable Objects

A useful programming concept that is used extensively in this course is a callable object. A callable object is any object that can be called like a function. In Python, any object whose class has a __call__ method will be callable. For example, we can define an AddConst class that is initialized with a value val. When the object of the AddConst class is called with input, it will return the sum of val and input:

class AddConst(object):def __init__(self, val): self.val = valdef __call__(self, input):return self.val + inputfoo = AddConst(4) foo(3) # Output: 7

You can think of the syntax foo(3) as a short form for foo.__call__(3).

In this second part of the assignment, you will implement several callable classes to emulate a layer in a neural network. Each class will implement a function that is parameterized by the objects initialization parameters. Figure 7 illustrates this diagram.

Figure 7: A Callable Class initialized with parameters; it is used to call on an input to produce and output. In the AddConst class example, the initialization parameter is val and the input (when called) is the scalar value 4 with the scalar output 7

Create a Python program part2.py to accomplish the following tasks. Your implementation should be able to handle both Python scalars (int/float) or NumPy arrays (of arbitrary dimensions) as inputs:

  1. Create a callable object class ElementwiseMultiply that is initialized with weight, a numpy array (with 1-dimension). When called on input of the same shape as weight, the object will output an elementwise product of input and weight. For example, the 1st element in the output will be the product of the first element of input and the first element of weight.
  2. Create a callable object class AddBias that is initialized with bias, a scalar number. When called on input, the object will output the sum of input and bias. Note that input can be a numpy array, so the same bias value is added to all elements of input.
  3. Create a callable object class LeakyRelu that is initialized with alpha, a scalar value. When called on input, which may be a NumPy array, the object will output:

(1)

Hint: You can use NumPy functions to help you implement this class without using any for-loops. Refer to the numpy.where function.

  1. Create a callable object class Compose that is initialized with layers, which is a list of callable objects (like those described above) that take in one parameter when called. The object Compose will compute the first layer function on the input parameter, and then the second and so on, producing an output that is a composition of object calls in layers. The order of the computations is in the order given in layers.
  2. To test your code from above, run py using either command line (python part2_test.py) or through PyCharm (right click on the part2_test.py script in the project tree and click Run part2 test. What is the output in the terminal?

3 Image Processing

A picture or image can be represented as a NumPy array of pixels, with dimensions H W C, where H is the height, W is the width, and C is the number of colour channels.

Figure 8 illustrates the coordinate system. The origin is at the top left corner, and the first dimension indicates the Y (row) direction, while the second dimension indicates the X (column) dimension. Typically we will use an image with channels that give the the Red, Green, and Blue level of each pixel, which is referred to with the short form RGB. The value for each channel ranges from 0 (darkest) to 255 (lightest). However, when loading an image through Matplotlib, this range will be scaled to be from 0 (darkest) to 1 (brightest) instead, and will be a real number, rather than an integer.

You will write Python code to load an image and perform several manipulations to the image and visualize their effects. Youll need to get the file pink_link.png from the same place you downloaded this assignment. Save the output images in the same directory as the Python file that you will be implementing.

3.1 Python Program Specification

Implement a Python program in part3.py to accomplish the following tasks:

  1. Load the image png into the variable img, using the pyplot.imread function from matplotlib. You can assume that the image file is located in the same directory as the Python part3.py file.
  2. Visualize the image by using the imshow function. To make the plot appear on the screen until you dismiss it, also use the pyplot.show function.

Figure 8: The image coordinate system

  1. Modify the image by adding a constant value of 0.25 to each pixel in the img and store the result in the variable img_add. Note that, since the range for the pixels needs to be between [0,1], you will also need to implement a clip function on the image, where any value in the image that is outside of the desired range to the closest endpoint. You can use the numpy function clip to do this. Save the resulting image in a PNG file called img_add.png in the same directory as the part3.py Python file, using the function pyplot.imsave.
  2. From the original image, create three images that separate out the three colour channels (red, green and blue), saving each as png,img_chan_1.png,img_chan_2.png. Hint: First create an array initialized with zeros, then copy over the specific channels 2D content from img.
  3. Convert the original image to a grayscale image. In a grayscale image, the pixel value of the 3 channels will be the same for a particular X,Y The equation for the pixel value [1] is given by:

p = 0.299R + 0.587G + 0.114B (2)

Where the R,G,B are the values for each of the corresponding channels. We will do this by creating an array called img_gray with the same shape as img. Save the output image in a file called img_gray.png.

  1. Crop the image to be the top half of the image. Save the output image in a file called png.
  2. Flip the original image vertically that is flip it about a horizontal line that is half-way down the image. Save the flipped image to the file png.

3.2 Qualitative Questions

Answer the following qualitative questions in assign1.pdf:

  1. How does the image png differ from the original image? What would happen if we had subtracted 0.25 from the original image instead of adding?
  2. Describe your programming experience in a few paragraphs. This can include the courses you have taken here at UofT, but if you have more experience, describe that as well.
  3. Describe your experience with Assignment 1: how clear were the installation instructions and questions? How can we make it more helpful?

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] ECE324-Assignment 1 Python Review; NumPy, Matplotlib, Image Representation[Solved] ECE324-Assignment 1 Python Review; NumPy, Matplotlib, Image Representation
$25