In [1]:
import graphlab
In [2]:
image_train = graphlab.SFrame('image_train_data/')
In [24]:
image_test = graphlab.SFrame('image_test_data/')
In [6]:
image_train['label'].sketch_summary()
Out[6]:
In [21]:
targets = ['bird', 'dog', 'cat', 'automobile']
In [22]:
targets_train_data = {}
for x in targets:
targets_train_data[x] = image_train[image_train['label'] == x]
print len(targets_train_data[x])
In [23]:
targets_model = {}
for x in targets:
targets_model[x] = graphlab.nearest_neighbors.create(targets_train_data[x], features=['deep_features'], label='id')
In [67]:
graphlab.canvas.set_target('browser')
In [57]:
def show_image(id):
image_train.filter_by(id, 'id')['image'].show()
In [30]:
targets_model['cat'].query(image_test[0:1])
Out[30]:
In [55]:
sum([row['distance'] for row in targets_model['cat'].query(image_test[0:1], k=5)]) / 5.0
Out[55]:
In [68]:
show_image(16289)
In [31]:
targets_model['dog'].query(image_test[0:1])
Out[31]:
In [56]:
sum([row['distance'] for row in targets_model['dog'].query(image_test[0:1], k=5)]) / 5.0
Out[56]:
In [69]:
show_image(16976)
In [33]:
targets_test_data = {}
for x in targets:
targets_test_data[x] = image_test[image_test['label'] == x]
print len(targets_test_data[x])
In [35]:
dog_neighbors = {}
for x in targets:
dog_neighbors[x] = targets_model[x].query(targets_test_data['dog'], k=1)
In [39]:
dog_dist_gen_dict = {}
for x in targets:
dog_dist_gen_dict['dog-' + x] = dog_neighbors[x]['distance']
In [40]:
dogSF = graphlab.SFrame(dog_dist_gen_dict)
In [47]:
def is_dog_correct(row):
return min(row.values()) == row['dog-dog']
In [49]:
dogSF.apply(is_dog_correct).sum()
Out[49]:
In [ ]:
In [50]:
cat_neighbors = {}
for x in targets:
cat_neighbors[x] = targets_model[x].query(targets_test_data['cat'], k=1)
In [51]:
cat_dist_gen_dict = {}
for x in targets:
cat_dist_gen_dict['cat-' + x] = cat_neighbors[x]['distance']
In [52]:
catSF = graphlab.SFrame(cat_dist_gen_dict)
In [53]:
def is_cat_correct(row):
return min(row.values()) == row['cat-cat']
In [54]:
catSF.apply(is_cat_correct).sum()
Out[54]: