HW2: CIFAR2: Convolutional Neural Networks

Due Friday, 10/11/19 at 11:59pm

Through the eons, humans have interrogated the universe's many fundamental questions. How did we come to be? What comprises a "good" life? From whence can a sentient being derive meaning?

Of these many questions, one stands alone in its ability to evade consesus and vex the brightest thinkers.

Of course, it is....Which are better: Cats or Dogs?

We will not be able to answer such an intractable question in this short assignment. However, we can work towards our goal in earnest by tackling a simpler subproblem. We will learn to tell them apart.

In this assignment, you will be building a Convolutional Neural Network (CNN) with pooling layers using the CIFAR dataset. Please read this handout in its entirety before beginning the assignment.

Getting the stencil

You can find the files located here or on the "Files" column under the Assignments page. The files are compressed into a ZIP file, and to unzip the ZIP file, you can just double click on the ZIP file. There should be the files: assignment.py, convolution.py, preprocess.py, README, and CIFAR_data_compressed folder. You can find the conceptual questions located here or the "Conceptual Questions" column in the Assignments page.

Logistics

Work on this assignment off of the stencil code provided, but do not change the stencil except where specified. Changing the stencil will result in incompatiblity with the autograder and result in a low grade. You shouldn't change any method signatures or add any trainable parameters to init that we don't give you (other instance variables are fine).

This assignment should take longer than the previous assignment. If completed correctly, the model should train and test within 10 minutes on a department machine. While you will mainly be using TensorFlow functions, the second part of the assignment requires you to write your own convolution function, which is very computationally expensive. To counter this, we only require that you print the accuracy across the test set after finishing all training. On a local machine, training using TensorFlow functions may take about 5 minutes, and testing using your own convolution function may take upwards of 10 minutes. On a department machine, training should take about 3 minutes and testing using your own convolution should take about 2 minutes.

This assignment requires the TensorFlow, NumPy, and Matplotlib packages. You can install them using pip or run the assignment in a virtual environment.

To run the virtual environment on a department machine, you can run:

source /course/cs1470/tf-2.0/bin/activate
            

You can also check out the Python virtual environment guide to set up TensorFlow 2.0 on your local machine.

CIFAR2, not CIFAR10

Your task is a binary classification problem. While the CIFAR10 dataset has 10 possible classes (airplane, automobile, bird, cat, deer, frog, horse, ship, and truck), you will build a CNN to take in an image and correctly predict its class to either be a cat or dog, hence CIFAR2. We limit this assignment to a binary classification problem so that you can train the model in a reasonable amount of time.

The assignment has 3 parts.

Our stencil provides a model class with several methods and hyperparameters you need to use for your network. You will also fill out a function that performs the convolution operator. You will also answer a questions related to the assignment and class material as part of this assignment.

Part 1: The Model

Roadmap

You will notice that the structure of the Model class is very similar to the Model class defined in your first assignment. We strongly suggest that you first complete the Intro to TensorFlow Lab before starting this assignment. The lab includes many explanations about the way a Model class is structured, what variables are, and how things work in TensorFlow. If you come into hours with questions about TensorFlow related material that is covered in the lab, we will direct you to the lab.

Below is a brief outline of some things you should do. We expect you to fill in some of the missing gaps (review lecture slides to understand the pipeline) as this is your second assignment.

Step 1. Preprocess the data

Step 2. Create your model

Step 4. Train and test

Mandatory Hyperparameters

You can train with any batch size but you are limited to training for at most 25 epochs (I know, the title of this section is a bit misleading). However, your model must train using TensorFlow functions and test using your own convolution function within 10 minutes on a department machine. We will be timing this when autograding. Again, the parameters we suggest are training for 10 epochs using a batch size of 64.

Reading in the Data

The CIFAR files are pickled objects. We have provided you with a function unpickle(filename). You should not edit it. Note: You should normalize the pixel values so that they range from 0 to 1 (This can easily be done by dividing each pixel value by 255) to avoid any numerical overflow issues.

Data format

The testing and training data files to be read in are in the following format:

train: A pickled object of 50,000 train images and labels. This includes images and labels of all 10 classes. After unpickling the file, the dictionary will have the following elements:

test: A pickled object of 10,000 test images and labels. This includes images and labels of all 10 classes. Unpickling the file gives a dictionary with the same key values as above.

We've already done the job of unpickling the file and have extracted the unprocessed inputs and labels in the get_data function.

To get only the images and labels of classes 3 and 5 (representing dog and cat), you will want to loop over the data and only add it to your result array of inputs and labels if they belong to those classes.

Visualizing Results

Part 2: Conv2d

Before starting this part of the assignment, you should ensure that you have an accuracy of at least 70% on the test set using only TensorFlow functions for the problem of classifying dogs and cats.

As a new addition to this assignment, you will be implementing your very own convolution function! Deep Learning == TensorFlow tutorial no more!

For the sake of simple math calculations (less is more, no?), we'll require that our conv2d function only works with a stride of 1 (for both width and height). This is because the calculation for padding size changes as a result of the stride, which would be way more complex and unreasonable for a second assignment.

Do NOT change the parameters of the function we have provided. Even though the conv2d function takes in a strides argument, you should ALWAYS pass in [1, 1, 1, 1]. Leaving in strides as an argument was a conscious design choice - if you wanted to eventually make the function work for other kinds of strides in your own time, this would allow you to easily change it.

Roadmap

Testing out your own conv2d:

Autograder

Your model must complete training within 10 minutes AND/or under 25 epochs on a department machines.

Our autograder will import your model and your preprocessing functions. We will feed the result of your get_data function called on a path to our data and pass the result to your train method in order to return a fully trained model. After this, we will feed in your trained model, alongside the TA pre-processed data, to our custom test function. This will just batch the testing data using YOUR batch size and run it through your model's call function. However, we will test that your model can test with any batch size, meaning that you should not harcode self.batch_size in your call function. The logits which are returned will then be fed through an accuracy function. When testing your own convolution function, we will only test on inputs with even inputs and filters dimensions for SAME padding that are not affected when the floor function is applied to the calculation of padding. This is because you might result in different output dimensions that TensorFlow's convolution function when using SAME padding on odd inputs. In order to ensure you don't lose points, you need to make sure that you... A) correctly return training inputs and labels from get_data, B ) ensure that your model's call function returns logits from the inputs specified, and that it does not break on different batch sizes when testing, C) make sure your own convolution function works, and D) no part of your code relies on any packages outside of TensorFlow, NumPy, MatplotLib, or the Python standard library.

Part 3: Conceptual Questions

Fill out conceptual questions and submit in either PDF or .txt format. Submitting a scan of written work is also fine as long as it is readable. Please copy over the questions and write well thought out answers to the questions.

Grading

Code: You will be primarily graded on functionality. Your model should have an accuracy that is at least greater than 70% on the testing data.

Conceptual: You will be primarily graded on correctness (when applicable), thoughtfulness, and clarity.

You will not receive credit if you use the tf.keras, tf.layers, and tf.slim libraries. You can use tf.keras for your optimizer but do NOT use Keras layers!

CS2470 Students

1. Please complete the CS2470-only conceptual questions in addition to the coding assignment and the CS1470 conceptual questions. Note: Questions about 2470 will only be answered on Piazza, or by TAs marked with an asterisk (*) on the calendar.

2. You must receive an accuracy of at least 70% within 10 epochs of training your model. This means that you must choose an architecture/play around with hyperparameters to reach an accuracy that is of 70% in a shorter amount of time.

Handing In

You should submit the assignment using this Google Form. You must be logged in with your Brown account. Your assignment.py, preprocess.py, and convolution.py files should be Python files, while the written up conceptual questions should be either of PDF or txt format. The README can be any format.

If you receive an alert that your file has to be renamed, but you know it is actually correctly submitted with the formats from above, do not worry. Everything is fine!! :)

Cats vs. Dogs? from the creators of this assignment

"I am currently neither, and I have been both in the past" - Daniel Ritchie

"Definitely a dog person. In fact I’m allergic to cats" - David Oyeka

"I’m a dog. But I am a cat person" - Zach Horvitz

"I'm a big time doggo" - Brian Oppenheim

"I feel like I'm neither dog or cat. I'm a bull" - Amy Pu (I'm actually a huge dog person)