In [1]:
import numpy as np
import sys
sys.path.append('../src/')
import settings
sys.path.append(settings.CAFFE_PYTHON_PATH)

from dataset import CUB_200_2011
from parts import *

import caffe

%matplotlib inline
import matplotlib.pylab as plt

import cv2

from sklearn import svm
from sklearn import metrics
import utils

Visualize how part bounding box works


In [11]:
cub = CUB_200_2011(settings.CUB_ROOT)
cub_parts = cub.get_parts()
bbox = cub.get_bbox()
body_alpha = 0.6

bad_body_id_list = [1,7,55,65,89,106,139,140,147,245,266,272,282,318,327,333,362,379,380,383,385,397,402,403,405,423,956,987,1180,1183,1206,1212,1220,1226,1229,1232,1234,1245,1248,1249,1251,1256,1258,1264,1266,1269,1271,1273,1274,1276,1281,1284,1286,1301,1303,1304,1305,1307,1312,1319,1322,1325,1331,1332,1333,1342,1776,1797,2217,2230,2258,2480,2487,2506,2830,2842,2844,2856,2863,2867,2868,2874,2889,2900,2904,2907,2909,2912,2928,2929,2940,2945,2959,2994,3010,3013,3020,3024,3040,3043,3044,3045,3046,3051,3054,3055,3062,3064,3067,3733,3773,4373,4887,4895,4966,4985,4986,4989,4992,4996,4998,5007,5013,5032,5078,5183,5223,5226,5872,5877,5878,5894,5904,5910,5912,6096,6109,6112,6127,6131,6132,6137,6138,6144,6156,6173,6189,6194,6195,6266,6273,6301,6312,6589,7908,7950,7997,7998,8842,8866,8872,8873,9439,9465,9545,10437,10456,10458,10978,10983,10985,10998]

def vis_id(my_image_id):
    img = caffe.io.load_image(cub.get_image_info(my_image_id))
    parts = cub_parts.for_image(my_image_id)
    
    fig = plt.figure(figsize=(15, 10))
    ax = fig.add_subplot(131)
    
    head_parts = parts.filter_by_name(Parts.HEAD_PART_NAMES)
    body_parts = parts.filter_by_name(Parts.BODY_PART_NAMES)
    
    img = head_parts.draw_rect(img)
    img = body_parts.draw_rect(img, alpha=body_alpha, color=50)
    img = utils.draw_bbox(img, bbox[my_image_id - 1])
    
    ax.imshow(img)
    ax.set_xlim((0, img.shape[1]))
    ax.set_ylim((img.shape[0], 0))
    parts.draw_part(ax)

    img = caffe.io.load_image(cub.get_image_info(my_image_id))
    ax = fig.add_subplot(132)
    ax.set_axis_off()
    ax.imshow(body_parts.get_rect(img, alpha=body_alpha))
    
    ax = fig.add_subplot(133)
    ax.set_axis_off()
    ax.imshow(head_parts.get_rect(img))
    
for my_image_id in bad_body_id_list[100:102]:
    vis_id(my_image_id)



In [3]:
my_image_id = 100
img = caffe.io.load_image(cub.get_image_info(my_image_id))
parts = cub_parts.for_image(my_image_id)
bbox = cub.get_bbox()
img_bbox = bbox[my_image_id - 1]

H, W = img.shape[0], img.shape[1]
bx, by, bw, bh = int(img_bbox[0]), int(img_bbox[1]), int(img_bbox[2]), int(img_bbox[3])

# transfer
size = 256
# img = img[by:by+bh, bx:bx+bw]
# img = cv2.resize(img, (size, size))

# parts.norm_for_bbox(bx, by)
# parts.norm_for_size(bw, bh, size)

# -------

part = parts.filter_by_name(['beak'])

Px = part[0].x
Py = part[0].y

print 'H: %d, W: %d' % (H, W)
print 'bx: %d, by: %d, bh: %d, bw: %d' % (bx, by, bh, bw)
print 'Px: %d, Py: %d' % (Px, Py)


fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(121)
filtered_parts = parts.filter_by_name(Parts.HEAD_PART_NAMES)
img = filtered_parts.draw_rect(img)

# cv2.rectangle(img, (bx, by), (bx+bw, by+bh), (255, 125, 25))

ax.imshow(img)
ax.set_xlim((0, img.shape[1]))
ax.set_ylim((img.shape[0], 0))
part.draw_part(ax)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

img = caffe.io.load_image(cub.get_image_info(my_image_id))
ax = fig.add_subplot(122)
ax.set_axis_off()
ax.imshow(filtered_parts.get_rect(img))


H: 340, W: 500
bx: 13, by: 58, bh: 243, bw: 473
Px: 172, Py: 153
Out[3]:
<matplotlib.image.AxesImage at 0xa49ae50>

Classify with the help of head part


In [13]:
from dataset import CUB_200_2011, CUB_200_2011_Parts_Head
from storage import datastore
from deep_extractor import CNN_Features_CAFFE_REFERENCE

cub = CUB_200_2011(settings.CUB_ROOT)
cub_part_head = CUB_200_2011_Parts_Head(settings.CUB_ROOT)

features_storage_r = datastore(settings.storage('ccrft'))
feature_extractor_r = CNN_Features_CAFFE_REFERENCE(features_storage_r, make_net=False)

features_storage_c = datastore(settings.storage('cccft'))
feature_extractor_c = CNN_Features_CAFFE_REFERENCE(features_storage_c, make_net=False)

features_storage_p_h = datastore(settings.storage('ccpheadft-100000'))
feature_extractor_p_h = CNN_Features_CAFFE_REFERENCE(features_storage_p_h, make_net=False)

Xtrain_r, ytrain_r, Xtest_r, ytest_r = cub.get_train_test(feature_extractor_r.extract_one)
Xtrain_c, ytrain_c, Xtest_c, ytest_c = cub.get_train_test(feature_extractor_c.extract_one)
Xtrain_p_h, ytrain_p_h, Xtest_p_h, ytest_p_h = cub_part_head.get_train_test(feature_extractor_p_h.extract_one)

In [14]:
Xtrain = np.concatenate((Xtrain_r, Xtrain_c, Xtrain_p_h), axis=1)
Xtest = np.concatenate((Xtest_r, Xtest_c, Xtest_p_h), axis=1)

In [15]:
model = svm.LinearSVC(C=0.001)
model.fit(Xtrain, ytrain_r)
predictions = model.predict(Xtest)

print metrics.accuracy_score(ytest_r, predictions)
print utils.mean_accuracy(ytest_r, predictions)


0.769244045564
0.771764

In [ ]: