Setup


In [1]:
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_freeze')
import caffe

In [2]:
from localdefs import *
from plotting import *
from caffe_misc import *
from jby_misc import *

In [3]:
# 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/'

Load labels, mean, and trained network


In [6]:
# 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 [7]:
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]


First image in first few classes:
  0: n01440764 tench, Tinca tinca
    ILSVRC2012_val_00000293.JPEG
  1: n01443537 goldfish, Carassius auratus
    ILSVRC2012_val_00000236.JPEG
  2: n01484850 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias
    ILSVRC2012_val_00002338.JPEG
  3: n01491361 tiger shark, Galeocerdo cuvieri
    ILSVRC2012_val_00002922.JPEG
  4: n01494475 hammerhead, hammerhead shark
    ILSVRC2012_val_00001676.JPEG

In [8]:
# 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


(3, 227, 227)
94.1421813965 130.189407349

In [9]:
# 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()

Load an image


In [10]:
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 [16]:
#image_filename = caffe_root + 'examples/images/cat.jpg'
#image_filename = caffe_root + 'examples/images/lion.jpg'

class_id = 291
image_index = 2

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)


Filename: /home/jyosinsk/imagenet2012/val/ILSVRC2012_val_00003788.JPEG
Loaded image 2 from class 291 (n02129165 lion, king of beasts, Panthera leo)
Image has shape (227, 227, 3) and values in [0,255]

In [17]:
# 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())


data_blob has shape (1, 3, 227, 227) and values in [-119.446,156.741]

Forward prop and plot


In [18]:
# Show network
shownet(net)


                                         acts                           act diffs
                                             params                         param diffs
data  (1, 3, 227, 227)                   (-118.791, 158.043)            (0, 0)
   P: conv1 (96, 3, 11, 11)                  (-0.432212, 0.389596)          (0, 0)
            (1, 1, 1, 96)                    (-0.860493, 0.224677)          (0, 0)
conv1 (1, 96, 55, 55)                    (0, 1938.01)                   (0, 0)
pool1 (1, 96, 27, 27)                    (0, 1938.01)                   (0, 0)
norm1 (1, 96, 27, 27)                    (0, 138.622)                   (0, 0)
   P: conv2 (256, 48, 5, 5)                  (-0.247664, 0.438688)          (0, 0)
            (1, 1, 1, 256)                   (0.966555, 1.0324)             (0, 0)
conv2 (1, 256, 27, 27)                   (0, 287.632)                   (0, 0)
pool2 (1, 256, 13, 13)                   (0, 287.632)                   (0, 0)
norm2 (1, 256, 13, 13)                   (0, 133.638)                   (0, 0)
   P: conv3 (384, 256, 3, 3)                 (-0.202087, 0.474761)          (0, 0)
            (1, 1, 1, 384)                   (-0.104274, 0.0958545)         (0, 0)
conv3 (1, 384, 13, 13)                   (0, 214.344)                   (0, 0)
   P: conv4 (384, 192, 3, 3)                 (-0.157355, 0.359945)          (0, 0)
            (1, 1, 1, 384)                   (0.762975, 1.23653)            (0, 0)
conv4 (1, 384, 13, 13)                   (0, 179.195)                   (0, 0)
   P: conv5 (256, 192, 3, 3)                 (-0.154038, 0.303211)          (0, 0)
            (1, 1, 1, 256)                   (0.477638, 1.73028)            (0, 0)
conv5 (1, 256, 13, 13)                   (0, 199.394)                   (0, 0)
pool5 (1, 256, 6, 6)                     (0, 199.394)                   (0, 0)
   P: fc6   (1, 1, 4096, 9216)               (-0.0355359, 0.0473105)        (0, 0)
            (1, 1, 1, 4096)                  (0.910832, 1.0562)             (0, 0)
fc6   (1, 4096, 1, 1)                    (0, 40.3146)                   (0, 0)
   P: fc7   (1, 1, 4096, 4096)               (-0.0306103, 0.0487324)        (0, 0)
            (1, 1, 1, 4096)                  (0.736774, 1.22635)            (0, 0)
fc7   (1, 4096, 1, 1)                    (0, 13.7827)                   (0, 0)
   P: fc8   (1, 1, 1000, 4096)               (-0.045499, 0.0789288)         (0, 0)
            (1, 1, 1, 1000)                  (-0.372501, 0.388838)          (0, 0)
fc8   (1, 1000, 1, 1)                    (-6.45795, 14.5328)            (0, 0)
prob  (1, 1000, 1, 1)                    (5.70257e-10, 0.745109)        (0, 0)

In [19]:
# Forward prop
out = net.forward_all(data = data_blob)

In [20]:
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 [21]:
# Show activations throughout the network
shownet(net)


                                         acts                           act diffs
                                             params                         param diffs
data  (1, 3, 227, 227)                   (-119.446, 156.741)            (0, 0)
   P: conv1 (96, 3, 11, 11)                  (-0.432212, 0.389596)          (0, 0)
            (1, 1, 1, 96)                    (-0.860493, 0.224677)          (0, 0)
conv1 (1, 96, 55, 55)                    (0, 2754.65)                   (0, 0)
pool1 (1, 96, 27, 27)                    (0, 2754.65)                   (0, 0)
norm1 (1, 96, 27, 27)                    (0, 138.561)                   (0, 0)
   P: conv2 (256, 48, 5, 5)                  (-0.247664, 0.438688)          (0, 0)
            (1, 1, 1, 256)                   (0.966555, 1.0324)             (0, 0)
conv2 (1, 256, 27, 27)                   (0, 242.275)                   (0, 0)
pool2 (1, 256, 13, 13)                   (0, 242.275)                   (0, 0)
norm2 (1, 256, 13, 13)                   (0, 133.657)                   (0, 0)
   P: conv3 (384, 256, 3, 3)                 (-0.202087, 0.474761)          (0, 0)
            (1, 1, 1, 384)                   (-0.104274, 0.0958545)         (0, 0)
conv3 (1, 384, 13, 13)                   (0, 231.299)                   (0, 0)
   P: conv4 (384, 192, 3, 3)                 (-0.157355, 0.359945)          (0, 0)
            (1, 1, 1, 384)                   (0.762975, 1.23653)            (0, 0)
conv4 (1, 384, 13, 13)                   (0, 176.88)                    (0, 0)
   P: conv5 (256, 192, 3, 3)                 (-0.154038, 0.303211)          (0, 0)
            (1, 1, 1, 256)                   (0.477638, 1.73028)            (0, 0)
conv5 (1, 256, 13, 13)                   (0, 93.0522)                   (0, 0)
pool5 (1, 256, 6, 6)                     (0, 93.0522)                   (0, 0)
   P: fc6   (1, 1, 4096, 9216)               (-0.0355359, 0.0473105)        (0, 0)
            (1, 1, 1, 4096)                  (0.910832, 1.0562)             (0, 0)
fc6   (1, 4096, 1, 1)                    (0, 26.9144)                   (0, 0)
   P: fc7   (1, 1, 4096, 4096)               (-0.0306103, 0.0487324)        (0, 0)
            (1, 1, 1, 4096)                  (0.736774, 1.22635)            (0, 0)
fc7   (1, 4096, 1, 1)                    (0, 9.66949)                   (0, 0)
   P: fc8   (1, 1, 1000, 4096)               (-0.045499, 0.0789288)         (0, 0)
            (1, 1, 1, 1000)                  (-0.372501, 0.388838)          (0, 0)
fc8   (1, 1000, 1, 1)                    (-6.28917, 15.3348)            (0, 0)
prob  (1, 1000, 1, 1)                    (3.85316e-10, 0.948392)        (0, 0)

In [22]:
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))


conv5 has shape (1, 256, 13, 13)
Out[22]:
<matplotlib.image.AxesImage at 0x46c0110>

In [ ]: