In [ ]:
import graphlab
In [21]:
image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')
In [ ]:
graphlab.canvas.set_target('ipynb')
In [ ]:
image_train['image'].show()
In [15]:
image_train['label'].sketch_summary()
Out[15]:
In [ ]:
raw_pixel_model = graphlab.logistic_classifier.create(image_train,target='label',
features=['image_array'])
In [ ]:
image_test[0:3]['image'].show()
In [ ]:
image_test[0:3]['label']
In [ ]:
raw_pixel_model.predict(image_test[0:3])
The model makes wrong predictions for all three images.
In [ ]:
raw_pixel_model.evaluate(image_test)
The accuracy of this model is poor, getting only about 46% accuracy.
We only have 2005 data points, so it is not possible to train a deep neural network effectively with so little data. Instead, we will use transfer learning: using deep features trained on the full ImageNet dataset, we will train a simple model on this small dataset.
In [ ]:
len(image_train)
The two lines below allow us to compute deep features. This computation takes a little while, so we have already computed them and saved the results as a column in the data you loaded.
(Note that if you would like to compute such deep features and have a GPU on your machine, you should use the GPU enabled GraphLab Create, which will be significantly faster for this task.)
In [ ]:
#deep_learning_model = graphlab.load_model('http://s3.amazonaws.com/GraphLab-Datasets/deeplearning/imagenet_model_iter45')
#image_train['deep_features'] = deep_learning_model.extract_features(image_train)
As we can see, the column deep_features already contains the pre-computed deep features for this data.
In [ ]:
image_train.head()
In [16]:
deep_features_model = graphlab.logistic_classifier.create(image_train,
features=['deep_features'],
target='label')
In [46]:
cat_model = graphlab.nearest_neighbors.create(image_train[image_train['label']=='cat'],
features=['deep_features'],
target='label')
dog_model = graphlab.nearest_neighbors.create(image_train[image_train['label']=='dog'],
features=['deep_features'],
target='label')
automobile_model = graphlab.nearest_neighbors.create(image_train[image_train['label']=='automobile'],
features=['deep_features'],
target='label')
bird_model = graphlab.nearest_neighbors.create(image_train[image_train['label']=='bird'],
features=['deep_features'],
target='label')
In [56]:
cat_model.query(image_test[0:1])['distance'].mean()
image_train[image_train['label']=='cat'][181:182]['image'].show()
In [44]:
dog_model.query(image_test[0:1])
image_train[image_train['label']=='dog'][159:160]['image'].show()
dog_model.query(image_test[0:1])['distance'].mean()
Out[44]:
In [17]:
image_test[0:3]['image'].show()
In [48]:
image_test_dog=image_test[image_test['label']=='dog']
dog_cat_neighbors = cat_model.query(image_test_dog, k=1)
dog_dog_neighbors = dog_model.query(image_test_dog, k=1)
dog_automobile_neighbors = automobile_model.query(image_test_dog, k=1)
dog_bird_neighbors = bird_model.query(image_test_dog, k=1)
In [18]:
deep_features_model.predict(image_test[0:3])
Out[18]:
In [55]:
dog_distances = graphlab.SFrame({
'dog-cat': dog_cat_neighbors['distance'],
'dog-dog': dog_dog_neighbors['distance'],
'dog-automobile': dog_automobile_neighbors['distance'],
'dog-bird': dog_bird_neighbors['distance']
})
dog_distances
def is_dog_correct(row):
d = row['dog-dog']
if d<=row['dog-cat'] and d<=row['dog-automobile'] and d<=row['dog-bird']:
return 1
else:
return 0
dog_distances.num_rows
Out[55]:
The classifier with deep features gets all of these images right!
In [19]:
deep_features_model.evaluate(image_test)
Out[19]: