|
COS 429 - Computer Vision
|
Fall 2019
|
Assignment 0: Setup
Nothing to turn in -- but getting this done early will make your life happier later in the course.
September 19th is a good target due date.
1. Installing Python through Miniconda
The first thing that you need to do is set up the Python environment. In this class, we will
be using the conda package management system. If you currently have Python installed, but not through
conda, we highly recommend you still go through this tutorial, since conda makes environment and package
management very simple. If you already have Anaconda or Miniconda installed with Python 3.7, skip ahead to step b).
1.a) Installing Miniconda
The installation guide for Miniconda for each operating system can be found here. Make sure you install Miniconda, rather than Anaconda. There's no problem if you install Anaconda instead, but that comes with far more tools than this course will need, making installation more cumbersome.
Follow the regular installation instructions for you operating system (on Mac, I found that downloading the bash and following instructions made life easier). Once that is done, test the build by following these instructions.
If a list of installed packages are printed out, congratulations! Now you have a fresh installation of python 3.7 on your machine.
1.b) Creating a virtual environment for this class
To keep dependencies and packages consistent between all members of the class, we are going to create a virtual environment.
Think of an environment as a separate installation of python. Packages installed in an environment stay localized to it, so by creating an environment purely for this class, we reduce the risk of having packages from other classes interfering with our dependencies.
You can read up on virtual environments here, but the TL;DR for creating an environment with the name cos429 is as follows:.
- Open up terminal (if you're on MacOS) or the Anaconda Prompt.
- To create an environment, type conda create --name cos429
- To activate the environment, type conda activate cos429
- To de-activate the environment, type conda deactivate
You'll only create the environment once, but any time you want to do work for this class, from writing code to installing new packages, always activate the environment first. De-activate the environment when you're not doing cos429 work.
1.c) Installing necessary packages
Luckily, installing packages using conda management system is a breeze. For now, you're going to install some packages that will definitely be needed in the class, but if additional packages will be needed later on, the TAs will also provide the proper command to be typed into terminal/anaconda prompt.
Make sure that you have your cos429 environment activated (conda activate cos429), and then type the following lines one at a time:
- conda install -c anaconda numpy
- numpy allows for matrix computations.
- conda install -c anaconda opencv
- OpenCV is an image processing library that, in conjunction with numpy, lets us work with image files.
- conda install -c anaconda pillow
- pillow, or PIL, contains the Image module which gives us image processing tools. Will be useful later with Pytorch.
- conda install -c pytorch pytorch
- pytorch, or torch is a deep learning library, which we will explore in the latter part of the course.
- conda install -c pytorch torchvision
- torchvision gives us easy tools for vision applications in pytorch.
- conda install -c conda-forge matplotlib
- matplotlib gives us tools to plot and visualize data
Let's check to make sure everything installed properly. Open a python shell (type python in terminal), and try the following:
import numpy
import cv2
from PIL import Image
import torch
import torchvision
import matplotlib.pyplot as plt
If none of these gave you errors, then move on to the next step.
As an aside, Miniconda does not come with Jupyter. If you also want to have Jupyter Notebook functionality (heavily HEAVILY recommended, read about Jupyter here), use the command conda install jupyter.
1.d) Version Numbers
Both Python and certain packages are in constant flux. To minimize the confusion that stem from version mismatch, make sure to have the following versions installed:
Python Version 3.7
OpenCV Version 3.4.2
PIL Version 6.1.0
Pytorch Version 1.2.0
Torchvision Version 0.4.0
To check the version number of Python, open a shell. The top line should include the version number.
To check the version number of a package, first import the package (e.g. import torch), and then check the __version__ property (e.g. torch.__version__).
If you are having difficulties with the setup, or have a version mismatch that you are unable to resolve, please contact the TAs through Piazza, or come to office hours.
2. Getting familiar with Python
Note: This assignment, and all subsequent ones, assume basic familiarity with Python. If you have no experience with the language, there are many guides online to help you get started.
To familiarize yourself with how OpenCV and Numpy interact with each other in Python, go through the following exercises in a python shell or a Jupyter Notebook. Remember to first import the packages first (import cv2, import numpy as np) before attempting these.
Work through the following tasks using an image of your choice:
- Read an image into a variable.
Hint #1: A basic cv2 tutorial can be found here .
Hint #2: If your image isn't in the same directory as where you launched your shell, make sure you also provide directory path in the image name.
Hint #3: Note that the color channel order is BLUE, GREEN, RED, rather than the standard RGB. The image is read in as a numpy matrix.
- Display the image.
Hint: Look in above tutorial.
- Convert the image to grayscale.
Hint: This can be done with cv2.cvtColor
- Check the height and width of the image.
Hint #1: Since the image is a numpy matrix, we can use the .shape property of a matrix. The first value gives rows (which is HEIGHT or Y), second value gives columns (which is WIDTH or X).
- Extract the middle 100 pixels from the greyscale image.
Hint #1: Extracting a part of a matrix is done by
matrix2 = matrix1[row_min:row_max,col_min:col_max]
Note that the first dimension is row (or y), and second dimension is column (or x).
The indices are exclusive on the upper bound: array[low:high] returns the set [low, high).
Just a ":" will grab all values in that dimension.
Python uses 0 indexing.
Hint #2: Since the middle 100 pixels is also an image, you can display these pixels as well using OpenCV to make sure you did it properly.
- Write a pair of nested "for" loops to set a grid of every 10th pixel
horizontally and every 20th pixel vertically to 0.
- Flip the image vertically. Then show the original and the flipped image side-by-side.
Hint #1: cv2.flip
Hint #2: Create a new numpy matrix with twice the number of columns, and dtype np.uint8. Put the original image on the left side, and the flipped image on the right side.
- Write the combined image back to a new file.
Hint #1: cv2.imwrite
If you get stuck on any of these, ask for help on piazza.
3. Getting familiar with Pytorch
Modern computer vision relies heavily on neural networks. Although we won't cover deep learning until later in the course, many of you may want to base your final project on a deep learning method. Therefore, we provide a brief PyTorch example in a Jupyter Notebook for those that want to get started early. Run through the code and work through the exercise at the end.
Last update
16-Sep-2019 16:29:18