In [1]:
import graphlab

Load dataset with photos and their deep features:


In [2]:
photos = graphlab.SFrame('photos_deep_features.gl')


[INFO] GraphLab Create v1.8.3 started. Logging: /tmp/graphlab_server_1456485720.log

Show a few first rows of dataset:


In [3]:
photos


Out[3]:
path image deep_features
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0,
0.553484618664, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0,
0.572288155556, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.21896219254, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.391624748707, 0.0,
0.187315821648, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.0834910273552, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.655602097511, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
0.00101053714752, ...
/data/yelp-photo-explorer
/photos_las_vegas_foo ...
Height: 256 Width: 256 [0.0, 0.0, 0.0, 0.0, 0.0,
2.86969923973, 0.0, 0.0, ...
[16185 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.

Train a nearest neighbors model on deep features of photos


In [4]:
nn_model = graphlab.nearest_neighbors.create(photos, features=['deep_features'], label='path')

Print information about the model:


In [5]:
nn_model


Out[5]:
Class                         : NearestNeighborsModel

Attributes
----------
Method                        : brute_force
Number of distance components : 1
Number of examples            : 16185
Number of feature columns     : 1
Number of unpacked features   : 4096
Total training time (seconds) : 2.4992

Show 12 nearest photos for first 10 photos in the dataset:


In [6]:
graphlab.canvas.set_target('ipynb')
n = 12 # number of nearest neighbors
for i in xrange(10): 
    query = nn_model.query(photos[i:i+1],k=n)
    nearest_neighbors = photos.filter_by(query['reference_label'], 'path')
    nearest_neighbors['image'].show()


Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 10.041ms     |
| Done         |         | 100         | 540.271ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.773ms      |
| Done         |         | 100         | 591.642ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.832ms      |
| Done         |         | 100         | 598.776ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.544ms      |
| Done         |         | 100         | 592.492ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 7.217ms      |
| Done         |         | 100         | 598.679ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.804ms      |
| Done         |         | 100         | 605.177ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.969ms      |
| Done         |         | 100         | 635.982ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.664ms      |
| Done         |         | 100         | 607.747ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 11.205ms     |
| Done         |         | 100         | 639.821ms    |
+--------------+---------+-------------+--------------+
Starting pairwise querying.
+--------------+---------+-------------+--------------+
| Query points | # Pairs | % Complete. | Elapsed Time |
+--------------+---------+-------------+--------------+
| 0            | 1       | 0.00617856  | 8.803ms      |
| Done         |         | 100         | 579.85ms     |
+--------------+---------+-------------+--------------+

This notebook is inspired by one of the notebooks in the following Coursera course: https://www.coursera.org/learn/ml-foundations