In [1]:
# import
import graphlab as gl
In [2]:
# setting defaults
gl.canvas.set_target('ipynb')
In [3]:
image_train = gl.SFrame('data/image_train_data/')
image_test = gl.SFrame('data/image_test_data/')
In [4]:
image_train['image'].show()
In [5]:
raw_pixel_model = gl.logistic_classifier.create(image_train,
target='label',
features=['image_array'])
In [6]:
image_test['image'][:6].show()
In [7]:
image_test[:6]['label']
Out[7]:
In [8]:
raw_pixel_model.predict(image_test[:6])
Out[8]:
In [9]:
raw_pixel_model.evaluate(image_test)
Out[9]:
In [10]:
len(image_train)
Out[10]:
In [11]:
#deep_learning_model = gl.load_model('http://s3.amazonaws.com/GraphLab-Datasets/deeplearning/imagenet_model_iter45')
#deep_learning_model.save('imagenet_model')
#image_train['deep_features']= deep_learning_model.extract_features(image_train)
In [12]:
image_train.head(5)
Out[12]:
In [13]:
deep_feature_model = gl.logistic_classifier.create(image_train,
features = ['deep_features'],
target = 'label')
In [15]:
image_test[:6]['image'].show()
In [16]:
deep_feature_model.predict(image_test[:6])
Out[16]:
In [17]:
deep_feature_model.evaluate(image_test)
Out[17]:
In [21]:
knn_model = gl.nearest_neighbors.create(image_train,
features=['deep_features'],
label='id')
In [23]:
cat = image_train[18:19]
cat['image'].show()
In [24]:
knn_model.query(cat)
Out[24]:
In [25]:
def get_image(query_result):
return image_train.filter_by(query_result['reference_label'], 'id')
In [27]:
cat_neighbour = get_image(knn_model.query(cat))
In [29]:
cat_neighbour['image'].show()
In [30]:
car = image_train[8:9]
car['image'].show()
In [31]:
get_image(knn_model.query(car))['image'].show()
In [32]:
# A function to find NN images
show_nn = lambda inp: get_image(knn_model.query( image_train[inp:inp+1]))['image'].show()
In [33]:
show_nn(8)
In [34]:
show_nn(12)
In [36]:
image_train['label'].sketch_summary()
Out[36]:
In [44]:
# filter the data into {‘dog’,’cat’,’automobile’,bird’}
animal_type=['dog','cat','automobile','bird']
#dog = image_train.filter_by(image_train['label'], 'dog')
dog = image_train[image_train['label']=='dog']
cat = image_train[image_train['label']=='cat']
automobile = image_train[image_train['label']=='automobile']
bird = image_train[image_train['label']=='bird']
In [95]:
print(len(dog), len(bird), len(automobile), len(cat))
In [114]:
# creating models for each category
dog_model = gl.nearest_neighbors.create(dog,
features=['deep_features'],
label='label')
cat_model = gl.nearest_neighbors.create(cat,
features=['deep_features'],
label='label')
automobile_model = gl.nearest_neighbors.create(automobile,
features=['deep_features'],
label='label')
bird_model = gl.nearest_neighbors.create(bird,
features=['deep_features'],
label='label')
cat_model_id = gl.nearest_neighbors.create(cat,
features=['deep_features'],
label='id')
dog_model_id = gl.nearest_neighbors.create(dog,
features=['deep_features'],
label='id')
In [109]:
img = image_test[0:1]
img['image'].show()
In [48]:
cat_nn = cat_model.query(img)
In [113]:
get_image(cat_model_id.query(img, k=1))['image'].show()
In [115]:
get_image(dog_model_id.query(img, k=1))['image'].show()
In [49]:
cat_model.query(img)
Out[49]:
In [53]:
dog_nn = dog_model.query(img)
In [54]:
dog_model.query(img)
Out[54]:
In [55]:
cat_distance_mean = cat_nn['distance'].mean()
cat_distance_mean
Out[55]:
In [56]:
dog_distance_mean = dog_nn['distance'].mean()
dog_distance_mean
Out[56]:
In [57]:
image_test_cat = image_test[image_test['label']=='cat']
image_test_dog = image_test[image_test['label']=='dog']
image_test_bird = image_test[image_test['label']=='bird']
image_test_automobile = image_test[image_test['label']=='automobile']
In [58]:
dog_cat_neighbors = cat_model.query(image_test_dog, k=1)
dog_cat_neighbors
Out[58]:
In [59]:
dog_dog_neighbors = dog_model.query(image_test_dog, k=1)
dog_bird_neighbors = bird_model.query(image_test_dog, k=1)
dog_auto_neighbors = automobile_model.query(image_test_dog, k=1)
In [69]:
dog_distances = gl.SFrame({'dog-automobile': dog_auto_neighbors['distance'],
'dog-bird': dog_bird_neighbors['distance'],
'dog-cat':dog_cat_neighbors['distance'],
'dog-dog':dog_dog_neighbors['distance']})
In [70]:
dog_distances.head()
Out[70]:
In [88]:
def is_dog_correct(row):
da = row['dog-dog'] < row['dog-automobile']
dc = row['dog-dog'] < row['dog-cat']
db = row['dog-dog'] < row['dog-bird']
return 1 if ( da and dc and db ) else 0
In [89]:
is_dog_correct(dog_distances[11])
Out[89]:
In [90]:
dog_distances[11]
Out[90]:
In [91]:
sum_correct_predic = dog_distances.apply(is_dog_correct).sum()
In [92]:
sum_correct_predic
Out[92]:
In [117]:
dog_model.evaluate(img)
In [ ]: