In [1]:
import graphlab
In [2]:
image_train = graphlab.SFrame('image_train_data_2/')
In [3]:
image_train.head()
Out[3]:
In [4]:
knn_model = graphlab.nearest_neighbors.create(image_train,
features=['deep_features'],
label='id')
In [5]:
cat = image_train[18:19]
In [6]:
graphlab.canvas.set_target('ipynb')
In [7]:
cat['image'].show()
In [8]:
knn_model.query(cat)
Out[8]:
In [10]:
def get_images_from_ids(query_result):
return image_train.filter_by(query_result['reference_label'],
'id')
In [11]:
cat_neighbors = get_images_from_ids(knn_model.query(cat))
In [12]:
cat_neighbors['image'].show()
In [13]:
car = image_train[8:9]
In [14]:
car['image'].show()
In [15]:
get_images_from_ids(knn_model.query(car))['image'].show()
In [16]:
show_neighbors = lambda i: get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()
In [17]:
show_neighbors(8)
In [18]:
show_neighbors(26)
In [19]:
show_neighbors(122)
In [20]:
show_neighbors(1222)
In [21]:
show_neighbors(2000)
In [23]:
image_train['label'].sketch_summary()
Out[23]:
In [30]:
dog_sframe = image_train[image_train['label'] == 'dog']
In [31]:
dog_sframe.show()
In [32]:
len(dog_sframe)
Out[32]:
In [33]:
cat_sframe = image_train[image_train['label'] == 'cat']
In [34]:
bird_sframe = image_train[image_train['label'] == 'bird']
In [35]:
automobile_sframe = image_train[image_train['label'] == 'automobile']
In [38]:
len(cat_sframe), len(bird_sframe), len(automobile_sframe), len(dog_sframe)
Out[38]:
In [39]:
cat_sframe.show()
In [40]:
dog_sframe.show()
In [41]:
bird_sframe.show()
In [42]:
automobile_sframe.show()
In [43]:
dog_model = graphlab.nearest_neighbors.create(dog_sframe,
features=['deep_features'],
label='id')
In [44]:
cat_model = graphlab.nearest_neighbors.create(cat_sframe,
features=['deep_features'],
label='id')
In [45]:
bird_model = graphlab.nearest_neighbors.create(bird_sframe,
features=['deep_features'],
label='id')
In [46]:
automobile_model = graphlab.nearest_neighbors.create(automobile_sframe,
features=['deep_features'],
label='id')
In [47]:
image_test = graphlab.SFrame('image_test_data_2/')
In [48]:
image_test[0:1]['image'].show()
In [49]:
catQ = image_test[0:1]
In [50]:
catQ
Out[50]:
In [51]:
cat_model.query(catQ)
Out[51]:
In [54]:
get_images_from_ids(cat_model.query(catQ))['image'].show()
In [58]:
image_test['id'==16289]['image'].show()
In [59]:
graphlab.canvas.set_target('ipynb')
In [62]:
get_images_from_ids(dog_model.query(catQ))['image'].show()
In [63]:
dog_model.query(catQ)
Out[63]:
In [67]:
dog_model.query(catQ)['distance'].mean()
Out[67]:
In [68]:
cat_model.query(catQ)['distance'].mean()
Out[68]:
In [69]:
dog_test = image_test[image_test['label'] == 'dog']
In [70]:
cat_test = image_test[image_test['label'] == 'cat']
In [71]:
bird_test = image_test[image_test['label'] == 'bird']
In [72]:
automobile_test = image_test[image_test['label'] == 'automobile']
In [73]:
dog_cat_neighbors = cat_model.query(dog_test, k=1)
In [74]:
dog_cat_neighbors.head()
Out[74]:
In [75]:
dog_bird_neighbors = bird_model.query(dog_test, k=1)
In [76]:
dog_dog_neighbors = dog_model.query(dog_test, k=1)
In [77]:
dog_automobile_neighbors = automobile_model.query(dog_test, k=1)
In [78]:
new_sframe = graphlab.SFrame({'dog-automobile': dog_automobile_neighbors['distance'],
'dog-bird': dog_bird_neighbors['distance'],
'dog-cat': dog_cat_neighbors['distance'],
'dog-dog': dog_dog_neighbors['distance']})
In [79]:
new_sframe.head()
Out[79]:
In [80]:
dog_distance= new_sframe
In [83]:
dog_distance[0:1]
Out[83]:
In [85]:
def is_dog_correct(row):
if row['dog-dog'] < row['dog-automobile'] and row['dog-dog'] < row['dog-bird'] and row['dog-dog'] < row['dog-cat']:
return 1
else:
return 0
In [86]:
dog_distance.apply(is_dog_correct).sum()
Out[86]:
In [87]:
len(dog_test)
Out[87]:
In [ ]: