In [ ]:
%autoreload 2
%load_ext autoreload
In [ ]:
import pylab
from pylab import *
from collections import OrderedDict
import pickle
%matplotlib inline
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# Make sure that caffe is on the python path:
caffe_root = '../../' # this file is expected to be in {caffe_root}/notebooks
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
In [ ]:
from localdefs import *
from plotting import *
from caffe_misc import *
from jby_misc import *
In [ ]:
# Define paths
load_dir = '/home/jyosinsk/results/140311_234854_afadfd3_priv_netbase_upgraded/'
model_def_file = load_dir + 'deploy_1.prototxt'
pretrained_model = load_dir + 'caffe_imagenet_train_iter_450000'
#dataset_file = '/home/jyosinsk/s/caffe/data/ilsvrc12/data/whole_train/files.txt'
#dataset_dir = '/home/jyosinsk/imagenet2012/train/'
dataset_file = '/home/jyosinsk/s/caffe/data/ilsvrc12/data/whole_valid/files.txt'
dataset_dir = '/home/jyosinsk/imagenet2012/val/'
In [ ]:
# Load labels
with open(caffe_root + '/data/ilsvrc12/synset_words.txt') as ff:
labels = [line.strip() for line in ff.readlines()]
def get_image_info(dataset_file):
with open(dataset_file) as ff:
lines = [line.strip().split() for line in ff.readlines()]
image_info = {}
for line in lines:
filename = line[0]
label = int(line[1])
dict_for_label = image_info.setdefault(label,{'classname':labels[label], 'files':[]})
dict_for_label['files'].append(filename)
#filenames = [line[0] for line in lines]
#classlabels = [int(line[1]) for line in lines]
#image_info = [(ff,ll) for (ff,ll) in zip(filenames, classlabels)]
return image_info
image_info = get_image_info(dataset_file)
In [ ]:
print 'First image in first few classes:'
for ii in range(5):
print ' %d: %s' % (ii, image_info[ii]['classname'])
print ' ', image_info[ii]['files'][0]
In [ ]:
# Load mean
imagenet_mean = np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy')
imagenet_mean = imagenet_mean[:, 14:14+227, 14:14+227]
print imagenet_mean.shape
print imagenet_mean.min(), imagenet_mean.max()
showimage(imagenet_mean[::-1]/255, c01=True) # Color order is BGR, reverse to RGB before plotting
In [ ]:
# Load network
net = caffe.Classifier(model_def_file, pretrained_model,
#mean=inmean,
#channel_swap=(2,1,0),
#raw_scale=255.0,
#image_dims=(256, 256),
)
net.set_phase_test()
net.set_mode_cpu()
In [ ]:
def load_image(filename):
image = caffe.io.load_image(filename) # produces an image in [0,1]
return image
def resize_and_scale(image, size = (227,227), scale = 255):
im = caffe.io.resize_image(image, (227,227))
im = norm01(im) * scale
return im
In [ ]:
#image_filename = caffe_root + 'examples/images/cat.jpg'
#image_filename = caffe_root + 'examples/images/lion.jpg'
class_id = 291
image_index = 0
filename = dataset_dir + image_info[class_id]['files'][image_index]
image_large = load_image(filename)
image = resize_and_scale(image_large)
figsize(12,5)
print 'Filename:', filename
print 'Loaded image %d from class %d (%s)' % (image_index, class_id, image_info[class_id]['classname'])
print 'Image has shape %s and values in [%g,%g]' % (image.shape, image.min(), image.max())
showimagesc(image)
In [ ]:
# image is: (227,227,3), RGB order, [0,255]
tmp = image.transpose((2,0,1))[newaxis, ::-1, : :] # (1,3,227,227), BGR order [0,255]
data_blob = tmp - imagenet_mean # 0-centered
print 'data_blob has shape %s and values in [%g,%g]' % (data_blob.shape, data_blob.min(), data_blob.max())
In [ ]:
# Show network
shownet(net)
In [ ]:
# Forward prop
out = net.forward_all(data = data_blob)
In [ ]:
figsize(12,5)
plot(out['prob'].flatten())
iimax = out['prob'].argmax()
plot(iimax, out['prob'].flatten()[iimax], 'ro')
_=title('Max at idx %d (%s)' % (iimax, labels[iimax]))
In [ ]:
# Show activations throughout the network
shownet(net)
In [ ]:
blob_name = 'conv5'
blob = net.blobs[blob_name].data
print blob_name, 'has shape', blob.shape
figsize(12,12)
imshow(tile_images(blob[0], padval = 1))
In [ ]:
In [ ]:
%connect_info