In [1]:
import graphlab
We will use a popular benchmark dataset in computer vision called CIFAR-10.
(We've reduced the data to just 4 categories = {'cat','bird','automobile','dog'}.)
This dataset is already split into a training set and test set. In this simple retrieval example, there is no notion of "testing", so we will only use the training data.
In [2]:
image_train = graphlab.SFrame('image_train_data/')
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 [3]:
#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)
In [4]:
image_train.head()
Out[4]:
In [5]:
knn_model = graphlab.nearest_neighbors.create(image_train,features=['deep_features'],
label='id')
In [6]:
graphlab.canvas.set_target('ipynb')
cat = image_train[18:19]
cat['image'].show()
In [7]:
knn_model.query(cat)
Out[7]:
We are going to create a simple function to view the nearest neighbors to save typing:
In [8]:
def get_images_from_ids(query_result):
return image_train.filter_by(query_result['reference_label'],'id')
In [9]:
cat_neighbors = get_images_from_ids(knn_model.query(cat))
In [10]:
cat_neighbors['image'].show()
In [11]:
car = image_train[8:9]
car['image'].show()
In [12]:
get_images_from_ids(knn_model.query(car))['image'].show()
In [13]:
show_neighbors = lambda i: get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()
In [14]:
show_neighbors(8)
In [15]:
show_neighbors(26)
In [17]:
image_train['label'].sketch_summary()
Out[17]:
In [19]:
show_neighbors(0)
In [20]:
cat_images = image_train[image_train['label']=='cat']
In [23]:
dog_images = image_train[image_train['label']=='dog']
In [24]:
car_images = image_train[image_train['label']=='automobile']
In [27]:
bird_images = image_train[image_train['label']=='bird']
In [32]:
bird_images.head()
Out[32]:
In [34]:
cat_model = graphlab.nearest_neighbors.create(cat_images,features=['deep_features'], label='id')
In [47]:
dog_model = graphlab.nearest_neighbors.create(dog_images,features=['deep_features'], label='id')
In [48]:
car_model = graphlab.nearest_neighbors.create(car_images,features=['deep_features'], label='id')
In [49]:
bird_model = graphlab.nearest_neighbors.create(bird_images,features=['deep_features'], label='id')
In [38]:
image_test = graphlab.SFrame('image_test_data/')
In [55]:
cat_model.query(image_test[0:1])[0:5]['distance'].mean()
Out[55]:
In [56]:
dog_model.query(image_test[0:1])[0:5]['distance'].mean()
Out[56]:
In [52]:
dog_images.filter_by(16976, 'id').show()
In [57]:
dog_test_images = image_test.filter_by('dog', 'label')
In [59]:
dog_cat = cat_model.query(dog_test_images, k=1)
In [61]:
dog_dog = dog_model.query(dog_test_images, k=1)
In [62]:
dog_car = car_model.query(dog_test_images, k=1)
In [63]:
dog_bird = bird_model.query(dog_test_images, k=1)
In [64]:
dog_distances = graphlab.SFrame({'dog':dog_dog['distance'], 'cat':dog_cat['distance'], 'car':dog_car['distance'], 'bird':dog_bird['distance']})
In [65]:
dog_distances.head()
Out[65]:
In [75]:
dog_distances.apply(lambda row: row['dog'] == min(row.values())).sum()
Out[75]:
In [69]:
min(dog_distances[0].values())
Out[69]:
In [ ]: