The objective of this assignment is to learn about simple data curation practices, and familiarize you with some of the data we’ll be reusing later.
This notebook uses the notMNIST dataset to be used with python experiments. This dataset is designed to look like the classic MNIST dataset, while looking a little more like real data: it’s a harder task, and the data is a lot less ‘clean’ than MNIST.
In [2]:
First, we’ll download the dataset to our local machine. The data consists of characters rendered in a variety of fonts on a 28x28 image. The labels are limited to ‘A’ through ‘J’ (10 classes). The training set has about 500k and the testset 19000 labelled examples. Given these sizes, it should be possible to train models quickly on any machine.
In [3]:
Found and verified notMNIST_large.tar.gz
Found and verified notMNIST_small.tar.gz
Extract the dataset from the compressed .tar.gz file.
This should give you a set of directories, labelled A through J.
Let’s take a peek at some of the data to make sure it looks sensible. Each exemplar should be an image of a character A through J rendered in a different font. Display a sample of the images that we just downloaded. Hint: you can use the package IPython.display.
In [5]:
Now let’s load the data in a more manageable format. Since, depending on your computer setup you might not be able to fit it all in memory, we’ll load each class into a separate dataset, store them on disk and curate them independently. Later we’ll merge them into a single dataset of manageable size.
We’ll convert the entire dataset into a 3D array (image index, x, y) of floating point values, normalized to have approximately zero mean and standard deviation ~0.5 to make training easier down the road.
A few images might not be readable, we’ll just skip them.
Let’s verify that the data still looks good. Displaying a sample of the labels and images from the ndarray. Hint: you can use matplotlib.pyplot.
In [7]:
notMNIST_small/F.pickle
Problem 3
———
Another check: we expect the data to be balanced across classes. Verify that.
According to this link:
http://machinelearningmastery.com/tactics-to-combat-imbalanced-classes-in-your-machine-learning-dataset/
The classes have to be balanced, meaning that they should be represented equally. Let’s check the number of images by class.
In [8]:
train datasets:
A has 52909 images.
B has 52911 images.
C has 52912 images.
D has 52911 images.
E has 52912 images.
F has 52912 images.
G has 52912 images.
H has 52912 images.
I has 52912 images.
J has 52911 images.
test datasets:
A has 1872 images.
B has 1873 images.
C has 1873 images.
D has 1873 images.
E has 1873 images.
F has 1872 images.
G has 1872 images.
H has 1872 images.
I has 1872 images.
J has 1872 images.
Merge and prune the training data as needed. Depending on your computer setup, you might not be able to fit it all in memory, and you can tune train_size as needed. The labels will be stored into a separate array of integers 0 through 9.
Also create a validation dataset for hyperparameter tuning.
Next, we’ll randomize the data. It’s important to have the labels well shuffled for the training and test distributions to match.
In [10]:
Problem 4
———
Convince yourself that the data is still good after shuffling!
In [11]:
train dataset:
test dataset:
valid dataset:
Finally, let’s save the data for later reuse:
In [12]:
In [13]:
Compressed pickle size: 690800441
Problem 5
———
By construction, this dataset might contain a lot of overlapping samples, including training data that’s also contained in the validation and test set! Overlap between training and test can skew the results if you expect to use your model in an environment where there is never an overlap, but are actually ok if you expect to see training samples recur when you use it.
Measure how much overlap there is between training, validation and test samples.
Optional questions:
- What about near duplicates between datasets? (images that are almost identical)
- Create a sanitized validation and test set, and compare your accuracy on those in subsequent assignments.
—
In [14]:
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-14-cf541de072ef> in <module>()
2 for train_data in train_dataset:
3 for test_data in test_dataset:
----> 4 if (train_data == test_data).all():
5 dups += 1
6 print('Overlap count: ', dups)
/usr/lib/python2.7/dist-packages/numpy/core/_methods.pyc in _all(a, axis, dtype, out, keepdims)
35 def _all(a, axis=None, dtype=None, out=None, keepdims=False):
36 return um.logical_and.reduce(a, axis=axis, dtype=dtype, out=out,
---> 37 keepdims=keepdims)
38
39 def _count_reduce_items(arr, axis):
KeyboardInterrupt:
Problem 6
———
Let’s get an idea of what an off-the-shelf classifier can give you on this data. It’s always good to check that there is something to learn, and that it’s a problem that is not so trivial that a canned solution solves it.
Train a simple model on this data using 50, 100, 1000 and 5000 training samples. Hint: you can use the LogisticRegression model from sklearn.linear_model.
Optional question: train an off-the-shelf model on all the data!