In [1]:
import graphlab
graphlab.canvas.set_target('ipynb')

Deep Learning Basics

Today we'll walk through building a deep learning network for image recognition CIFAR-10 dataset. The CIFAR data-set represents real-world data that is already formatted and labeled, so we can focus on building our network today instead of cleaning the data.

We're going to walk through 4 steps today:

  • Loading the Data
  • Training our Model
  • Evaluating the results of the model
  • Look at ways to improveing the performance of our model

We've download a subset of the CIFAR-10 data set for you. We load the data into an SFame, which is a powerful and scalable data structure that is used by many of the models in GraphLab Create.


In [2]:
image_train = graphlab.SFrame('https://static.turi.com/datasets/coursera/deep_learning/image_train_data')
image_test = graphlab.SFrame('https://static.turi.com/datasets/coursera/deep_learning/image_test_data')


[INFO] This commercial license of GraphLab Create is assigned to engr@turi.com.

[INFO] Start server at: ipc:///tmp/graphlab_server-98991 - Server binary: /Users/piotrteterwak/.virtualenvs/gl-1.4/lib/python2.7/site-packages/graphlab/unity_server - Server log: /tmp/graphlab_server_1442964667.log
[INFO] GraphLab Server Version: 1.6
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_train_data/dir_archive.ini to /var/tmp/graphlab-piotrteterwak/98991/51c6a729-39ce-497e-a6e4-32b47158b5be.ini
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_train_data/objects.bin to /var/tmp/graphlab-piotrteterwak/98991/99e89616-5a60-4315-82f5-a198f20ee633.bin
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_train_data/m_8b0619884fada761.frame_idx to /var/tmp/graphlab-piotrteterwak/98991/bb1e963e-9ee5-4a5e-a477-682948e3ea88.frame_idx
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_train_data/m_8b0619884fada761.sidx to /var/tmp/graphlab-piotrteterwak/98991/8b59f77c-29da-4ff9-b925-94e8615e73f5.sidx
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_test_data/dir_archive.ini to /var/tmp/graphlab-piotrteterwak/98991/4034beac-c3da-4b74-9400-a532da514a1e.ini
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_test_data/objects.bin to /var/tmp/graphlab-piotrteterwak/98991/ac7cfe1b-5671-4d87-9f40-9e9130c126a8.bin
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_test_data/m_686d0b1bd5620cb1.frame_idx to /var/tmp/graphlab-piotrteterwak/98991/6b43b4c2-9a6e-45f7-99c2-0a4a449178cf.frame_idx
PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_test_data/m_686d0b1bd5620cb1.sidx to /var/tmp/graphlab-piotrteterwak/98991/8184f530-f346-452d-a09b-6dbe8d3f1454.sidx

Exploring the data


In [3]:
image_train


PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_train_data/m_8b0619884fada761.0000 to /var/tmp/graphlab-piotrteterwak/98991/a25f18f8-7a29-4ee4-9559-c13719f94172.0000
Out[3]:
id image label deep_features image_array
24 Height: 32 Width: 32 bird [0.242871761322,
1.09545373917, 0.0, ...
[73.0, 77.0, 58.0, 71.0,
68.0, 50.0, 77.0, 69.0, ...
33 Height: 32 Width: 32 cat [0.525087952614, 0.0,
0.0, 0.0, 0.0, 0.0, ...
[7.0, 5.0, 8.0, 7.0, 5.0,
8.0, 5.0, 4.0, 6.0, 7.0, ...
36 Height: 32 Width: 32 cat [0.566015958786, 0.0,
0.0, 0.0, 0.0, 0.0, ...
[169.0, 122.0, 65.0,
131.0, 108.0, 75.0, ...
70 Height: 32 Width: 32 dog [1.12979578972, 0.0, 0.0,
0.778194487095, 0.0, ...
[154.0, 179.0, 152.0,
159.0, 183.0, 157.0, ...
90 Height: 32 Width: 32 bird [1.71786928177, 0.0, 0.0,
0.0, 0.0, 0.0, ...
[216.0, 195.0, 180.0,
201.0, 178.0, 160.0, ...
97 Height: 32 Width: 32 automobile [1.57818555832, 0.0, 0.0,
0.0, 0.0, 0.0, ...
[33.0, 44.0, 27.0, 29.0,
44.0, 31.0, 32.0, 45.0, ...
107 Height: 32 Width: 32 dog [0.0, 0.0,
0.220677852631, 0.0, ...
[97.0, 51.0, 31.0, 104.0,
58.0, 38.0, 107.0, 61.0, ...
121 Height: 32 Width: 32 bird [0.0, 0.23753464222, 0.0,
0.0, 0.0, 0.0, ...
[93.0, 96.0, 88.0, 102.0,
106.0, 97.0, 117.0, ...
136 Height: 32 Width: 32 automobile [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 7.5737862587, 0.0, ...
[35.0, 59.0, 53.0, 36.0,
56.0, 56.0, 42.0, 62.0, ...
138 Height: 32 Width: 32 bird [0.658935725689, 0.0,
0.0, 0.0, 0.0, 0.0, ...
[205.0, 193.0, 195.0,
200.0, 187.0, 193.0, ...
[2005 rows x 5 columns]
Note: Only the head of the SFrame is printed.
You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.


In [4]:
graphlab.image_analysis.resize(image_train['image'], 128, 128, 3).show()



We now use the neuralnet_classifier provided by GraphLab Create to create a neural network for our data set. The create method picks a default network architecture for you based on the dataset.


In [5]:
raw_pixel_model = graphlab.logistic_classifier.create(image_train, features=['image_array'], target='label')


PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 1902
PROGRESS: Number of classes           : 4
PROGRESS: Number of feature columns   : 1
PROGRESS: Number of unpacked features : 3072
PROGRESS: Number of coefficients    : 9219
PROGRESS: Starting L-BFGS
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 6        | 0.000010  | 3.915201     | 0.305994          | 0.310680            |
PROGRESS: | 2         | 8        | 1.000000  | 5.005213     | 0.380652          | 0.310680            |
PROGRESS: | 3         | 9        | 1.000000  | 5.642424     | 0.404837          | 0.388350            |
PROGRESS: | 4         | 10       | 1.000000  | 6.282546     | 0.413249          | 0.368932            |
PROGRESS: | 5         | 11       | 1.000000  | 6.935667     | 0.420610          | 0.398058            |
PROGRESS: | 6         | 12       | 1.000000  | 7.559418     | 0.364879          | 0.291262            |
PROGRESS: | 10        | 17       | 1.000000  | 10.457879    | 0.497371          | 0.436893            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.


In order to ensure that the deep learning model is acutally learning how to recognize the data, instead of memorizing features, we want to validate it with a dataset it hasn't seen before.

Making a single prediction


In [6]:
graphlab.image_analysis.resize(image_test[0:1]['image'], 128, 128, 3).show()


PROGRESS: Downloading https://static.turi.com/datasets/coursera/deep_learning/image_test_data/m_686d0b1bd5620cb1.0000 to /var/tmp/graphlab-piotrteterwak/98991/729e4e3f-ce1f-4319-a2fd-b57f4a6eea08.0000

In [7]:
image_test[0:1]['label']


Out[7]:
dtype: str
Rows: 1
['cat']

In [8]:
raw_pixel_model.predict(image_test[0:1])


Out[8]:
dtype: str
Rows: 1
['dog']

Evaluating the model


In [31]:
raw_pixel_model.evaluate(image_test)


Out[31]:
{'accuracy': 0.46225, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |     dog      |       cat       |  149  |
 |  automobile  |       dog       |  190  |
 |     bird     |       dog       |  259  |
 |  automobile  |       cat       |   75  |
 |  automobile  |    automobile   |  602  |
 |     bird     |    automobile   |  106  |
 |     dog      |    automobile   |   93  |
 |     cat      |       dog       |  380  |
 |     dog      |       dog       |  485  |
 |     cat      |       cat       |  217  |
 +--------------+-----------------+-------+
 [16 rows x 3 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}


Our network does well on the data, but we'd like to do better. How can we improve it?


In [10]:
# if you wanted to do this on your own
# step 1: deep_learning_model = graphlab.load_model('imagenet_model/')
# step 2: image_train['deep_features'] = deep_learning_model.extract_features(image_train)

In [11]:
deep_model = graphlab.neuralnet_classifier.create(image_train, features=['image'], target='label')


PROGRESS: Computing mean image...
PROGRESS: Done computing mean image.
PROGRESS: Creating neuralnet using cpu
PROGRESS: Training with batch size = 100
PROGRESS: +-----------+----------+--------------+-------------------+---------------------+-----------------+
PROGRESS: | Iteration | Examples | Elapsed Time | Training-accuracy | Validation-accuracy | Examples/second |
PROGRESS: +-----------+----------+--------------+-------------------+---------------------+-----------------+
PROGRESS: | 1         | 2000     | 1.567609     | 0.305000          | 0.378641            | 1275.828613     |
PROGRESS: | 2         | 2000     | 2.797574     | 0.385500          | 0.504854            | 1626.391235     |
PROGRESS: | 3         | 2000     | 3.969070     | 0.447500          | 0.592233            | 1707.442139     |
PROGRESS: | 4         | 2000     | 5.235676     | 0.505500          | 0.553398            | 1579.210205     |
PROGRESS: | 5         | 2000     | 6.448314     | 0.549000          | 0.592233            | 1649.498657     |
PROGRESS: | 6         | 2000     | 7.728839     | 0.567000          | 0.601942            | 1562.195190     |
PROGRESS: | 7         | 2000     | 9.101758     | 0.574500          | 0.631068            | 1456.916870     |
PROGRESS: | 8         | 2000     | 10.345062    | 0.599000          | 0.660194            | 1608.810547     |
PROGRESS: | 9         | 2000     | 11.642864    | 0.623000          | 0.621359            | 1541.266968     |
PROGRESS: | 10        | 2000     | 12.812695    | 0.630000          | 0.650485            | 1709.868774     |

Evaluating the Improved Model


In [17]:
graphlab.image_analysis.resize(image_test[0:1]['image'], 128, 128, 3).show()



In [18]:
image_test[0:1]['label']


Out[18]:
dtype: str
Rows: 1
['cat']

In [19]:
deep_model.predict(image_test[0:1])


Out[19]:
dtype: str
Rows: 1
['cat']

In [32]:
deep_model.evaluate(image_test)


Out[32]:
{'accuracy': 0.597000002861023, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |     bird     |       bird      |  511  |
 |     cat      |       bird      |  104  |
 |     dog      |       bird      |  134  |
 |  automobile  |       bird      |   25  |
 |     bird     |       cat       |  186  |
 |     cat      |       cat       |  521  |
 |     dog      |       cat       |  320  |
 |  automobile  |       cat       |   56  |
 |     bird     |       dog       |  164  |
 |     cat      |       dog       |  244  |
 +--------------+-----------------+-------+
 [16 rows x 3 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}

Improving the model further with Deep Features


In [21]:
deep_features_model = graphlab.logistic_classifier.create(image_train, features=['deep_features'], target='label')


PROGRESS: WARNING: Detected extremely low variance for feature(s) 'deep_features' because all entries are nearly the same.
Proceeding with model training using all features. If the model does not provide results of adequate quality, exclude the above mentioned feature(s) from the input dataset.
PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 1903
PROGRESS: Number of classes           : 4
PROGRESS: Number of feature columns   : 1
PROGRESS: Number of unpacked features : 4096
PROGRESS: Number of coefficients    : 12291
PROGRESS: Starting L-BFGS
PROGRESS: --------------------------------------------------------
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | Iteration | Passes   | Step size | Elapsed Time | Training-accuracy | Validation-accuracy |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: | 1         | 5        | 0.000131  | 4.282864     | 0.743037          | 0.715686            |
PROGRESS: | 2         | 9        | 0.250000  | 7.641871     | 0.768786          | 0.735294            |
PROGRESS: | 3         | 10       | 0.250000  | 8.691117     | 0.771939          | 0.735294            |
PROGRESS: | 4         | 11       | 0.250000  | 9.755411     | 0.778245          | 0.754902            |
PROGRESS: | 5         | 12       | 0.250000  | 10.764233    | 0.784025          | 0.745098            |
PROGRESS: | 6         | 13       | 0.250000  | 11.726264    | 0.793484          | 0.774510            |
PROGRESS: | 7         | 14       | 0.250000  | 12.695556    | 0.818707          | 0.803922            |
PROGRESS: | 8         | 15       | 0.250000  | 13.687111    | 0.843405          | 0.784314            |
PROGRESS: | 9         | 16       | 0.250000  | 14.631095    | 0.856017          | 0.803922            |
PROGRESS: | 10        | 17       | 0.250000  | 15.605435    | 0.865476          | 0.803922            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+
PROGRESS: Creating a validation set from 5 percent of training data. This may take a while.
          You can set ``validation_set=None`` to disable validation tracking.

Evaluating the deep model


In [33]:
deep_features_model.evaluate(image_test)


Out[33]:
{'accuracy': 0.784, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |  automobile  |       cat       |   14  |
 |     dog      |       cat       |  232  |
 |  automobile  |       dog       |   5   |
 |     cat      |       bird      |   69  |
 |     bird     |       dog       |   58  |
 |     dog      |       bird      |   43  |
 |     cat      |    automobile   |   38  |
 |     bird     |       cat       |  133  |
 |     dog      |    automobile   |   21  |
 |     dog      |       dog       |  704  |
 +--------------+-----------------+-------+
 [16 rows x 3 columns]
 Note: Only the head of the SFrame is printed.
 You can use print_rows(num_rows=m, num_columns=n) to print more rows and columns.}


Finding Similar Images


In [23]:
nearest_neighbors_model = graphlab.nearest_neighbors.create(image_train, features=['deep_features'],
                                                            label='id')


PROGRESS: Starting brute force nearest neighbors model training.

In [24]:
def get_nearest_neighbors(image):
    ans = nearest_neighbors_model.query(image)
    return image_train.filter_by(ans['reference_label'],'id')

Similar Images: Cat Photo


In [26]:
cat = image_train[18:19]
graphlab.image_analysis.resize(cat['image'],128,128,3).show()



In [28]:
graphlab.image_analysis.resize(get_nearest_neighbors(cat)['image'],128,128,3).show()


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 27.056ms     |
PROGRESS: | Done         |         | 100         | 350.123ms    |
PROGRESS: +--------------+---------+-------------+--------------+

Similar Images: Car Photo


In [29]:
car = image_train[8:9]
graphlab.image_analysis.resize(car['image'], 128, 128, 3).show()



In [30]:
graphlab.image_analysis.resize(get_nearest_neighbors(car)['image'],256,256,3).show()


PROGRESS: Starting pairwise querying.
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | Query points | # Pairs | % Complete. | Elapsed Time |
PROGRESS: +--------------+---------+-------------+--------------+
PROGRESS: | 0            | 1       | 0.0498753   | 13.922ms     |
PROGRESS: | Done         |         | 100         | 395.811ms    |
PROGRESS: +--------------+---------+-------------+--------------+

In [ ]: