# Load the data. Make sure you have the right path to the imagenet model file.
%matplotlib inline
from decaf.scripts import imagenet
from decaf.util import smalldata
from decaf.util import visualize
from matplotlib import pyplot
import numpy as np
import pylab
# We will use a larger figure size since many figures are fairly big.
pylab.rcParams['figure.figsize'] = (10.0, 10.0)
data_root='/Users/jiayq/Research/models/'
net = imagenet.DecafNet(data_root+'imagenet.decafnet.epoch90', data_root+'imagenet.decafnet.meta')
lena = smalldata.lena()
# Run a classification pass to create all the intermediate features
scores = net.classify(lena, center_only=True)
print 'blobs:', net._net.blobs.keys()
blobs: ['fc6_cudanet_out', 'fc6dropout_cudanet_out', 'probs_cudanet_out', 'pool2_cudanet_out', 'pool5_cudanet_out', 'fc6_neuron_cudanet_out', 'rnorm2_cudanet_out', 'conv1_neuron_cudanet_out', 'fc7_neuron_cudanet_out', '_decaf_fc6_flatten_out', 'conv1_cudanet_out', 'conv4_neuron_cudanet_out', 'rnorm1_cudanet_out', 'conv5_neuron_cudanet_out', 'data', 'fc7dropout_cudanet_out', 'conv2_cudanet_out', 'conv5_cudanet_out', 'fc8_cudanet_out', 'fc7_cudanet_out', 'pool1_cudanet_out', 'conv3_cudanet_out', 'conv3_neuron_cudanet_out', 'conv2_neuron_cudanet_out', 'conv4_cudanet_out']
# Show the input image
image = net.feature('data')[0,::-1]
image -= image.min()
image /= image.max()
_ = visualize.show_single(image)
# Show the first layer filters
filters = net._net.layers['conv1'].param()[0].data()
_ = visualize.show_multiple(filters.T)
pyplot.title('First layer Filters')
<matplotlib.text.Text at 0x113443550>
# show the first layer output. There are 96 channels,
# but we will just show 32 channels and their corresponding filters.
feat = net.feature('conv1_cudanet_out')[0,::-1, :, ::3]
filters = net._net.layers['conv1'].param()[0].data()
_ = visualize.show_multiple(filters.T[::3])
pyplot.title('Filters')
pyplot.figure()
_ = visualize.show_channels(feat)
pyplot.title('Output')
<matplotlib.text.Text at 0x1142db110>
# show what the second layer filters look like.
# It is a bit complex, since there are 128 filters,
# and each filter is 5*5*48. We show the channels separately,
# and show only the first 48 filters, one row per filter.
filters = net._net.layers['conv2'].param()[0].data()
# make the right filter shape
filters = filters.T.reshape(128, 5, 5, 48)
filters = filters.swapaxes(2,3).swapaxes(1,2).reshape(128*48, 5, 5)
_ = visualize.show_multiple(filters[:48*48], ncols=48)
pyplot.title('Second layer filters, each filter is shown as a row of channels.')
<matplotlib.text.Text at 0x1142ef7d0>
# show what the second layer output look like.
# There are 256 channels, but we are going to only show the first 36 filters.
feat = net.feature('conv2_cudanet_out')[0, ::-1, :]
_ = visualize.show_channels(feat[:, :, :36])
# show what the third layer output look like.
# There are 384 channels
feat = net.feature('conv3_cudanet_out')[0, ::-1, :]
print feat.shape
_ = visualize.show_channels(feat)
(13, 13, 384)
# show what the fourth layer output look like.
# There are 384 channels
feat = net.feature('conv4_cudanet_out')[0, ::-1, :]
print feat.shape
_ = visualize.show_channels(feat)
(13, 13, 384)
# show what the fifth layer output look like.
# There are 256 channels.
feat = net.feature('conv5_cudanet_out')[0, ::-1, :]
print feat.shape
_ = visualize.show_channels(feat)
(13, 13, 256)
# show what the fifth layer output look like after ReLU and pooling.
# There are 256 channels. This is the last layer of
# convolutional neural networks. After this layer, all computations
# are fully connected.
feat = net.feature('pool5_cudanet_out')[0, ::-1, :]
print feat.shape
_ = visualize.show_channels(feat, bg_func=np.max)
(6, 6, 256)
# show the feature of the first fully connected layers.
feat = net.feature('fc6_cudanet_out')[0]
print feat.shape
pyplot.plot(feat)
pyplot.title('feature')
pyplot.figure()
_ = pyplot.hist(feat, bins=100)
pyplot.title('histogram')
(4096,)
<matplotlib.text.Text at 0x11347a6d0>
# show the feature of the second fully connected layers.
feat = net.feature('fc7_cudanet_out')[0]
print feat.shape
pyplot.plot(feat)
pyplot.figure()
_ = pyplot.hist(feat, bins=100)
(4096,)
# show the final prediction probability
feat = net.feature('probs_cudanet_out')[0]
print feat.shape
pyplot.plot(feat)
(1000,)
[<matplotlib.lines.Line2D at 0x1148d3d10>]
# Now, print the top 5 predictions.
print net.top_k_prediction(scores, 5)[1]
['sombrero', 'cowboy hat', 'hand blower', 'bonnet', 'shower cap']