Skip to content

This repository is created for submission of Assignment 2 of the course ES 335 - Machine Learning, at the Indian Institute of Technology, Gandhinagar.

Notifications You must be signed in to change notification settings

HarshilShah1804/ES335_Assignment2

Repository files navigation

Assignment 2

Total marks: 10 (This assignment total to 20, we will overall scale by a factor of 0.5)

Task 1 : Ascending the Gradient Descent [6 marks]

Use the below dataset for Task 1:

np.random.seed(45)
num_samples = 40
    
# Generate data
x1 = np.random.uniform(-1, 1, num_samples)
f_x = 3*x1 + 4
eps = np.random.randn(num_samples)
y = f_x + eps
  1. Use torch.autograd to find the true gradient on the above dataset using linear regression (in the form $\theta_1x + \theta_0$) for any given values of $(\theta_0,\theta_1)$. [1 mark]

  2. Using the same $(\theta_0,\theta_1)$ as above, calculate the stochastic gradient for all points in the dataset. Then, find the average of all those gradients and show that the stochastic gradient is a good estimate of the true gradient. [1 mark]

  3. Implement full-batch, mini-batch and stochastic gradient descent. Calculate the average number of iterations required for each method to get sufficiently close to the optimal solution, where "sufficiently close" means within a distance of $\epsilon$ (or $\epsilon$-neighborhood) from the minimum value of the loss function. Visualize the convergence process for 15 epochs. Choose $\epsilon = 0.001$ for convergence criteria. Which optimization process takes a larger number of epochs to converge, and why? Show the contour plots for different epochs (or show an animation/GIF) for visualisation of optimisation process. Also, make a plot for Loss v/s epochs for all the methods. [2 marks]

  4. Explore the article here on gradient descent with momentum. Implement gradient descent with momentum for the dataset. Visualize the convergence process for 15 steps. Compare the average number of steps taken with gradient descent (for variants full batch and stochastic) with momentum to that of vanilla gradient descent to converge to an $\epsilon$-neighborhood for both dataset. Choose $\epsilon = 0.001$. Write down your observations. Show the contour plots for different epochs for momentum implementation. Specifically, show all the vectors: gradient, current value of theta, momentum, etc. [2 marks]

Task 2 : Reconstructing using Random Fourier Features (RFF) [3 marks]

Begin by exploring the instructor's notebook that introduces the application of Random Fourier Features (RFF) for image reconstruction.

  1. Image Reconstruction - Choose any image you like. Use Random Fourier Features (RFF) and Linear Regression to learn the mapping from the image coordinates $(X, Y)$ to the pixel colors $(R, G, B)$. Here, $(X, Y)$ represents the coordinates of the pixels, and $(R, G, B)$ represents the color values at those coordinates. Display both the original image and the reconstructed image. Also, calculate and report the Root Mean Squared Error (MSE) and Peak Signal-to-Noise Ratio (PSNR) between the original and reconstructed images. [1.5 Mark]

  2. Audio Reconstruction - Pick a 5-second audio sample of your liking. Use Random Fourier Features (RFF) and Linear Regression to learn the mapping from time $(t)$ to amplitude $(A)$, where $t$ is the time point, and $A$ is the audio amplitude at that time. Play the reconstructed audio and the original audio to demonstrate reconstruction. Calculate the Root Mean Squared Error (RMSE) and Signal-to-Noise Ratio (SNR) to evaluate the reconstruction. [1.5 Mark]

Note : Please notice that generally PSNR is used for images while SNR is used for audio signals.

Task 3 : Super-Resolution using Random Fourier Features (RFF) [4 Marks]

Begin by exploring the instructor's notebook that introduces the application of Random Fourier Features (RFF) for image reconstruction. Demonstrate the following applications using the cropped image from the notebook:

  1. Super-resolution: Perform superresolution on the image shown in notebook to enhance its resolution by factor 2. Show a qualitative comparison of original and reconstructed image. (i.e display original image and the image you created side by side) [3 Marks]

  2. The above only helps us with a qualitative comparison. Let us now do a quantitative comparison. Compute the below given metrics: [1 Marks]

    • RMSE on predicted v/s ground truth high resolution image
    • Peak SNR on predicted v/s ground truth high resolution image

Follow the below steps to execute this task:

  • Start with a 400x400 high-resolution image (the original image).
  • Resize it to a 200x200 image (this will be your input image). You can use OpenCV's resize function or another library for this. Here’s a tutorial to help: OpenCV Resize Image Tutorial.
  • Use Random Fourier Features (RFF) along with linear regression to learn a function based on the 200x200 image.
  • Use the learned function to predict the pixel values for the 400x400 image. This means predicting the values for the pixels that fall between the existing pixels of the 200x200 image.
  • Compare your predicted 400x400 image with the original 400x400 high-resolution image.
  • Calculate the Root Mean Squared Error (RMSE) and Peak Signal-to-Noise Ratio (PSNR) between the predicted image and the original high-resolution image.

Task 4 : Image Reconstruction using Matrix Factorisation [4 Marks]

Use the instructor's notebook on matrix factorisation, and solve the following questions. Here, ground truth pixel values are missing for particular regions within the image- you don't have access to them.

  1. Pick an image of your liking and reconstruct it using matrix factorization. Choose a suitable value for the rank $r$. Run Gradient Descent until it converges, plot the reconstructed image alongside the original image. Calculate and report the RMSE and PSNR metrics. [1 Marks]

  2. Consider a case where 900 pixels (30x30) are randomly missing from an image. Reconstruct the image using matrix factorization, plot the reconstructed image, and calculate the RMSE and PSNR metrics. Next, reconstruct the same image with the missing pixels using Random Fourier Features (RFF) and Linear Regression. Compute the RMSE and PSNR for both methods, and compare the results to see which performs better.[3 Marks]

Task 5 : Image Compression using Matrix Factorisation [3 Marks]

You have an image patch of size (50x50) that you want to compress using matrix factorization. To do this, you'll split the patch $[N\times N]$ into two smaller matrices of size $[N\times r]$ and $[r\times N]$ using matrix factorization. Compute the compressed patch by multiplying these two matrices and compare the reconstructed image patch with the original patch. Compute the Root Mean Squared Error (RMSE) and Peak Signal-to-Noise Ratio (PSNR) between the original and reconstructed image patches.

  • Test different values for the low-rank $r = [5, 10, 25, 50]$.
  • Use Gradient Descent to learn the compressed matrices.
  • Display the reconstructed image patches, keeping the original pixel values outside the patch unchanged, and use your compressed matrix for the patch to show how well the reconstruction works.
  • Compute the RMSE and PSNR for each value of $r$.

Here is a reference set of patches that you can choose. You can chose an image of your liking and create patches. You can choose the image shown below as well. [3 Marks]

Image 1 Image 2 Image 3


  • Present your results in a Jupyter Notebook or an MD file. If you choose to use an MD file, also include your code as a separate .py or .ipynb file.
  • The assignment is worth 20 marks but will be scaled down to 10 marks.
  • Carefully read the questions and make sure you answer all parts, as missing some could result in losing marks.
  • Ensure your code is readable and submitted before the deadline.
  • For any questions, use the #assignments channel on Slack.
  • Be efficient by reusing code from previous tasks wherever possible to solve new problems. In a few question, some task can be used as a base to solve the next task.

About

This repository is created for submission of Assignment 2 of the course ES 335 - Machine Learning, at the Indian Institute of Technology, Gandhinagar.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •