In [1]:
import graphlab

Load train and test images


In [2]:
image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')


[INFO] This non-commercial license of GraphLab Create is assigned to akshay.narayan@u.nus.edu and will expire on September 26, 2016. For commercial licensing options, visit https://dato.com/buy/.

[INFO] Start server at: ipc:///tmp/graphlab_server-7036 - Server binary: /usr/local/lib/python2.7/dist-packages/graphlab/unity_server - Server log: /tmp/graphlab_server_1449375968.log
[INFO] GraphLab Server Version: 1.6.1

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

In [4]:
image_train['image'].show()



In [5]:
image_train.show()


Train a classifier on the raw image pixels


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


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.

PROGRESS: Logistic regression:
PROGRESS: --------------------------------------------------------
PROGRESS: Number of examples          : 1908
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.000006  | 3.002678     | 0.261530          | 0.175258            |
PROGRESS: | 2         | 9        | 5.000000  | 4.100743     | 0.408281          | 0.360825            |
PROGRESS: | 3         | 10       | 5.000000  | 4.590109     | 0.419287          | 0.360825            |
PROGRESS: | 4         | 12       | 1.000000  | 5.418787     | 0.445493          | 0.443299            |
PROGRESS: | 5         | 13       | 1.000000  | 5.881215     | 0.459644          | 0.453608            |
PROGRESS: | 6         | 14       | 1.000000  | 6.361428     | 0.384172          | 0.329897            |
PROGRESS: | 10        | 18       | 1.000000  | 8.157102     | 0.508910          | 0.463918            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+

Making prediction with the simple raw pixel model


In [7]:
image_test[0:3]['image'].show()



In [8]:
image_test[0:3]['label']


Out[8]:
dtype: str
Rows: 3
['cat', 'automobile', 'cat']

In [9]:
raw_pixel_model.predict(image_test[0:3])


Out[9]:
dtype: str
Rows: 3
['bird', 'cat', 'bird']

Evaluating raw pixel model on test data


In [10]:
raw_pixel_model.evaluate(image_test)


Out[10]:
{'accuracy': 0.467, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |     cat      |       cat       |  418  |
 |     bird     |    automobile   |  113  |
 |  automobile  |    automobile   |  594  |
 |  automobile  |       cat       |  218  |
 |     cat      |       bird      |  198  |
 |     dog      |    automobile   |   90  |
 |     dog      |       dog       |  394  |
 |     cat      |       dog       |  249  |
 |     bird     |       cat       |  279  |
 |     cat      |    automobile   |  135  |
 +--------------+-----------------+-------+
 [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 with deep features


In [11]:
len(image_train)


Out[11]:
2005

In [12]:
len(image_test)


Out[12]:
4000

In [15]:
deep_learning_model = graphlab.load_model(imagenet_model)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-9f01ebb38aec> in <module>()
----> 1 deep_learning_model = graphlab.load_model(imagenet_model)

NameError: name 'imagenet_model' is not defined

In [16]:
image_train.head()


Out[16]:
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, ...
[10 rows x 5 columns]

Training classifier using deep features


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


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.

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          : 1908
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  | 2.392598     | 0.710168          | 0.701031            |
PROGRESS: | 2         | 9        | 0.250000  | 4.446716     | 0.769392          | 0.793814            |
PROGRESS: | 3         | 10       | 0.250000  | 5.107348     | 0.770440          | 0.783505            |
PROGRESS: | 4         | 11       | 0.250000  | 5.796446     | 0.780922          | 0.783505            |
PROGRESS: | 5         | 12       | 0.250000  | 6.473750     | 0.791929          | 0.804124            |
PROGRESS: | 6         | 13       | 0.250000  | 7.120380     | 0.809748          | 0.824742            |
PROGRESS: | 7         | 14       | 0.250000  | 7.774480     | 0.844864          | 0.845361            |
PROGRESS: | 8         | 15       | 0.250000  | 8.428039     | 0.849057          | 0.855670            |
PROGRESS: | 9         | 16       | 0.250000  | 9.071685     | 0.864256          | 0.865979            |
PROGRESS: | 10        | 17       | 0.250000  | 9.720529     | 0.879979          | 0.855670            |
PROGRESS: +-----------+----------+-----------+--------------+-------------------+---------------------+

Making prediction using deep feature model


In [18]:
deep_features_model.predict(image_test[0:3])


Out[18]:
dtype: str
Rows: 3
['cat', 'automobile', 'cat']

In [19]:
deep_features_model.evaluate(image_test)


Out[19]:
{'accuracy': 0.79, 'confusion_matrix': Columns:
 	target_label	str
 	predicted_label	str
 	count	int
 
 Rows: 16
 
 Data:
 +--------------+-----------------+-------+
 | target_label | predicted_label | count |
 +--------------+-----------------+-------+
 |     dog      |    automobile   |   15  |
 |     bird     |       bird      |  815  |
 |  automobile  |       bird      |   26  |
 |     dog      |       bird      |   51  |
 |     cat      |    automobile   |   33  |
 |     cat      |       cat       |  670  |
 |     bird     |       cat       |  111  |
 |     cat      |       dog       |  215  |
 |     dog      |       dog       |  724  |
 |  automobile  |    automobile   |  951  |
 +--------------+-----------------+-------+
 [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.}

In [ ]: