In [1]:
import graphlab
In [48]:
image_train = graphlab.SFrame('image_train_data/')
In [49]:
image_train.head()
Out[49]:
In [50]:
image_test = graphlab.SFrame('image_test_data/')
In [57]:
image_test[9:10]
Out[57]:
In [4]:
knn_model = graphlab.nearest_neighbors.create(image_train, features=['deep_features'], label='id')
In [5]:
graphlab.canvas.set_target('ipynb')
cat = image_train[18:19]
cat['image'].show()
In [6]:
knn_model.query(cat)
Out[6]:
In [7]:
def get_images_from_ids(query_result):
return image_train.filter_by(query_result['reference_label'], 'id')
In [8]:
cat_neighbors = get_images_from_ids(knn_model.query(cat))
In [9]:
cat_neighbors['image'].show()
In [10]:
car = image_train[8:9]
In [11]:
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 [16]:
show_neighbors(1222)
In [17]:
show_neighbors(2000)
In [18]:
image_train['label'].sketch_summary()
Out[18]:
In [47]:
image_train_cat = image_train[image_train['label'] == 'cat']
len(image_train_cat)
image_train_cat.head()
Out[47]:
In [20]:
image_train_dog = image_train[image_train['label'] == 'dog']
len(image_train_dog)
image_train_dog.head()
Out[20]:
In [21]:
image_train_auto = image_train[image_train['label'] == 'automobile']
len(image_train_auto)
image_train_auto.head()
Out[21]:
In [22]:
image_train_bird = image_train[image_train['label'] == 'bird']
len(image_train_bird)
image_train_bird.head()
Out[22]:
In [23]:
knn_model_cat = graphlab.nearest_neighbors.create(image_train_cat, features=['deep_features'], label='id')
In [58]:
knn_model_dog = graphlab.nearest_neighbors.create(image_train_dog, features=['deep_features'], label='id')
In [25]:
knn_model_auto = graphlab.nearest_neighbors.create(image_train_auto, features=['deep_features'], label='id')
In [26]:
knn_model_bird = graphlab.nearest_neighbors.create(image_train_bird, features=['deep_features'], label='id')
In [27]:
image_test = graphlab.SFrame('image_test_data/')
In [28]:
image_test[0:1]
image_test[0:1]['image'].show()
In [29]:
knn_model_cat.query(image_test[0:1])
Out[29]:
In [44]:
image_train_cat.filter_by(knn_model_cat.query(image_test[0:1])['reference_label'], 'id')['image'].show()
In [61]:
knn_model_dog.query(image_test[0:1])
Out[61]:
In [46]:
get_images_from_ids(knn_model_dog.query(image_test[0:1]))['image'].show()
In [31]:
type(knn_model_cat.query(image_test[0:1])[0:5]['distance'])
Out[31]:
In [60]:
knn_model_cat.query(image_test[0:1])[0:5]['distance'].mean()
Out[60]:
In [59]:
knn_model_dog.query(image_test[0:1])[0:5]['distance'].mean()
Out[59]:
In [34]:
image_test_cat = image_test[image_test['label'] == 'cat']
image_test_cat.head()
Out[34]:
In [35]:
image_test_dog = image_test[image_test['label'] == 'dog']
image_test_dog.head()
Out[35]:
In [36]:
image_test_auto = image_test[image_test['label'] == 'automobile']
image_test_auto.head()
Out[36]:
In [37]:
image_test_bird = image_test[image_test['label'] == 'bird']
image_test_bird.head()
Out[37]:
In [64]:
dog_cat_neighbors = knn_model_cat.query(image_test_dog, k=1)
dog_dog_neighbors = knn_model_dog.query(image_test_dog, k=1)
dog_bird_neighbors = knn_model_bird.query(image_test_dog, k=1)
dog_auto_neighbors = knn_model_auto.query(image_test_dog, k=1)
In [66]:
dog_distances = graphlab.SFrame({'dog-dog': dog_dog_neighbors['distance'],
'dog-cat': dog_cat_neighbors['distance'],
'dog-bird': dog_bird_neighbors['distance'],
'dog-auto': dog_auto_neighbors['distance']})
dog_distances.head()
Out[66]:
In [69]:
def is_dog_correct(row):
if row['dog-auto'] < row['dog-dog']:
return 0
if row['dog-bird'] < row['dog-dog']:
return 0
if row['dog-cat'] < row['dog-dog']:
return 0
return 1
In [70]:
dog_distances.apply(is_dog_correct).sum()
Out[70]:
In [71]:
len(dog_distances)
Out[71]: