In [1]:
import graphlab
In [2]:
image_train = graphlab.SFrame('image_train_data/')
image_test = graphlab.SFrame('image_test_data/')
In [4]:
image_train['label'].sketch_summary()
Out[4]:
In [5]:
dogs = image_train[image_train['label'] == 'dog']
automobiles = image_train[image_train['label'] == 'automobile']
cats = image_train[image_train['label'] == 'cat']
birds = image_train[image_train['label'] == 'bird']
In [22]:
cat_image = image_test[0:1]
cat_image['image'].show()
In [28]:
cat_model = graphlab.nearest_neighbors.create(cats,
features=['deep_features'],
label='id')
car_model = graphlab.nearest_neighbors.create(automobiles,
features=['deep_features'],
label='id')
dog_model = graphlab.nearest_neighbors.create(dogs,
features=['deep_features'],
label='id')
bird_model = graphlab.nearest_neighbors.create(birds,
features=['deep_features'],
label='id')
In [9]:
cat_model.query(cat_image)
Out[9]:
In [44]:
cat_model.query(cat_image)
cats[cats['id']==16289]['image'].show()
In [50]:
print dog_model.query(cat_image)
dogs[dogs['id'] == 16976]['image'].show()
In [36]:
cat_model.query(cat_image)[0:5]['distance'].mean()
Out[36]:
In [37]:
dog_model.query(cat_image)[0:5]['distance'].mean()
Out[37]:
In [38]:
cat_model.query(cat_image)
Out[38]:
In [40]:
cats[cats['id']==16289]['image'].show()
In [53]:
image_test_dog = image_test[image_test['label'] == 'dog']
image_test_automobiles = image_test[image_test['label'] == 'automobile']
image_test_cat = image_test[image_test['label'] == 'cat']
image_test_bird = image_test[image_test['label'] == 'bird']
In [54]:
dog_cat_neighbors = cat_model.query(image_test_dog, k=1)
In [59]:
dog_distances = graphlab.SFrame({'dog-dog': dog_model.query(image_test_dog, k=1)['distance'],
'dog-cat': cat_model.query(image_test_dog, k=1)['distance'],
'dog-car': car_model.query(image_test_dog, k=1)['distance'],
'dog-bird': bird_model.query(image_test_dog, k=1)['distance']})
In [60]:
dog_distances
Out[60]:
In [62]:
def is_dog_correct(row):
if row['dog-dog'] < row['dog-cat'] and row['dog-dog'] < row['dog-car'] and row['dog-dog'] < row['dog-bird']:
return 1
else:
return 0
In [67]:
is_dog_correct(dog_distances[5:6])
Out[67]:
In [69]:
dog_distances['classified_correct'] = dog_distances.apply(is_dog_correct)
In [75]:
dog_distances['classified_correct'].sum()/float(len(image_test_dog))
Out[75]:
In [ ]: