In [1]:
 
import os
os.chdir('/home/renwh/gaze/renwh/caffe_with_cudnnv3/')
import sys
sys.path.insert(0,'./python')
import caffe
from pylab import *
%matplotlib inline
caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.SGDSolver('examples/mymodel/00/lenet_solver.prototxt')
In [2]:
 
# each output is (batch size, feature dim, spatial dim)
[(k, v.data.shape) for k, v in solver.net.blobs.items()]
Out[2]:
[('data', (1000, 1, 36, 60)),
 ('label', (1000, 6)),
 ('gaze', (1000, 3)),
 ('headpose', (1000, 3)),
 ('conv1', (1000, 20, 32, 56)),
 ('pool1', (1000, 20, 16, 28)),
 ('conv2', (1000, 50, 12, 24)),
 ('pool2', (1000, 50, 6, 12)),
 ('ip1', (1000, 500)),
 ('cat', (1000, 503)),
 ('ip2', (1000, 3)),
 ('loss', ())]
In [3]:
 
# just print the weight sizes (not biases)
[(k, v[0].data.shape) for k, v in solver.net.params.items()]
Out[3]:
[('conv1', (20, 1, 5, 5)),
 ('conv2', (50, 20, 5, 5)),
 ('ip1', (500, 3600)),
 ('ip2', (3, 503))]
In [4]:
 
solver.net.forward()  # train net
solver.test_nets[0].forward()  # test net (there can be more than one)
Out[4]:
{'loss': array(0.44639846682548523, dtype=float32)}
In [5]:
 
# we use a little trick to tile the first eight images
imshow(solver.net.blobs['data'].data[:8, 0].transpose(1, 0, 2).reshape(36, 8*60), cmap='gray')
print solver.net.blobs['label'].data[:8]
[[ -2.44513273e-01   5.20949736e-02  -9.68245506e-01  -5.07045567e-01
   -1.12138920e-01  -2.90884897e-02]
 [ -7.41908699e-02   2.27922529e-01  -9.70848620e-01  -1.28387764e-01
    1.65355857e-02   1.06296828e-03]
 [ -1.74087971e-01   3.04691344e-02  -9.84258592e-01  -9.52000245e-02
   -3.14195365e-01  -1.50917871e-02]
 [ -2.49744281e-02   1.77879885e-01  -9.83735263e-01  -7.38587156e-02
   -1.21144764e-02  -4.47588827e-04]
 [ -1.61419377e-01   5.79187945e-02  -9.85184848e-01  -1.06810793e-01
    1.42905980e-01   7.65229668e-03]
 [ -1.52415037e-01   2.09456533e-01  -9.65866268e-01  -5.29863574e-02
   -1.14266567e-01  -3.03129526e-03]
 [ -1.76816806e-02   6.62708879e-02  -9.97644961e-01  -6.35477304e-02
   -2.95568883e-01  -9.46362782e-03]
 [  1.79661021e-01   2.34958977e-01  -9.55257118e-01  -8.40480402e-02
    1.60711512e-01   6.77234307e-03]]
In [6]:
 
solver.step(1)
In [7]:
 
imshow(solver.net.params['conv1'][0].diff[:, 0].reshape(4, 5, 5, 5)
       .transpose(0, 2, 1, 3).reshape(4*5, 5*5), cmap='gray')
Out[7]:
<matplotlib.image.AxesImage at 0x7f3cc4050d10>
 
Show the conv1 weights pics.
Then, I will train the model, and log some information.

Show the conv1 weights pics.

Then, I will train the model, and log some information.

In [8]:
%%time
niter = 500
test_interval = 25
# losses will also be stored in the log
train_loss = zeros(niter)
#test_acc = zeros(int(np.ceil(niter / test_interval)))
output = zeros((niter, 8, 3))
# the main solver loop
for it in range(niter):
    solver.step(1)  # SGD by Caffe
    
    # store the train loss
    train_loss[it] = solver.net.blobs['loss'].data
    
    # store the output on the first test batch
    # (start the forward pass at conv1 to avoid loading new data)
    solver.test_nets[0].forward(start='conv1')
    output[it] = solver.test_nets[0].blobs['ip2'].data[:8]
    if it % test_interval == 0:
        print 'Iteration', it
    # run a full test every so often
    # (Caffe can also do this for us and write to a log, but we show here
    #  how to do it directly in Python, where more complicated things are easier.)
Iteration 0
Iteration 25
Iteration 50
Iteration 75
Iteration 100
Iteration 125
Iteration 150
Iteration 175
Iteration 200
Iteration 225
Iteration 250
Iteration 275
Iteration 300
Iteration 325
Iteration 350
Iteration 375
Iteration 400
Iteration 425
Iteration 450
Iteration 475
CPU times: user 31.8 s, sys: 4.57 s, total: 36.4 s
Wall time: 36.4 s
In [9]:
 
_, ax1 = subplots()
ax1.plot(arange(niter), train_loss)
ax1.set_xlabel('iteration')
ax1.set_ylabel('train loss')
Out[9]:
<matplotlib.text.Text at 0x7f3cbf1d3990>
 
**show you the train loss curve.

**show you the train loss curve.

In [10]:
 
figsize=(20, 10)
imshow(solver.test_nets[0].blobs['data'].data[:8, 0].transpose(1, 0, 2).reshape(36, 8*60), cmap='gray')
    
# print the label and train result
for i in range(20):
    print solver.test_nets[0].blobs['label'].data[i,:3] ,'label<->ip2', solver.test_nets[0].blobs['ip2'].data[i]
    
[-0.05740207  0.26108676 -0.96360713] label<->ip2 [-0.10854036  0.16547096 -0.9392308 ]
[-0.05108735  0.33534154 -0.94071043] label<->ip2 [-0.26814818  0.17025429 -0.95560873]
[ 0.14314307  0.08807883 -0.98577493] label<->ip2 [-0.01435356  0.1202116  -0.95390987]
[ 0.14494585  0.10631151 -0.9837116 ] label<->ip2 [-0.13950546  0.11444768 -0.94234967]
[ 0.07297696  0.09071066 -0.99319983] label<->ip2 [-0.07475061  0.10443642 -0.96073043]
[-0.22788107  0.33246592 -0.91517025] label<->ip2 [-0.3046324   0.18025452 -0.96343148]
[ 0.11161654  0.03402296 -0.99316877] label<->ip2 [-0.07809033  0.10121632 -0.96737516]
[ 0.16556081  0.14849275 -0.97495615] label<->ip2 [  3.90633941e-04   1.17082566e-01  -9.40931201e-01]
[ 0.22707051  0.11209575 -0.96740556] label<->ip2 [ 0.06739761  0.05849258 -0.94882739]
[-0.26985002  0.27037239 -0.92416435] label<->ip2 [-0.25276926  0.13409963 -0.93681848]
[ 0.186772    0.03520598 -0.98177224] label<->ip2 [ 0.08484142  0.07407014 -0.94333595]
[ 0.25852728 -0.02351192 -0.96571779] label<->ip2 [-0.08786423  0.11980689 -0.90472221]
[ 0.04857407  0.20633523 -0.97727495] label<->ip2 [-0.11045387  0.20200166 -0.93761557]
[ 0.22813955  0.2115647  -0.95036453] label<->ip2 [ 0.0059359   0.14121914 -0.94515216]
[ 0.22677779  0.18486373 -0.95624119] label<->ip2 [-0.02454845  0.09789832 -0.94061673]
[ 0.16038546  0.30930048 -0.93734181] label<->ip2 [ 0.03613424  0.21273294 -0.94080299]
[-0.25437474  0.04642556 -0.96599078] label<->ip2 [-0.3044751   0.07149749 -0.94814098]
[-0.01042224  0.20404638 -0.97890574] label<->ip2 [-0.12749863  0.14481348 -0.96326244]
[-0.268558    0.2635223  -0.92651635] label<->ip2 [-0.23386669  0.1769792  -0.9629159 ]
[-0.22500102  0.28130114 -0.93286884] label<->ip2 [-0.27789089  0.12139966 -0.94430178]
In [11]:
 
imshow(solver.net.params['conv1'][0].diff[:, 0].reshape(4, 5, 5, 5)
       .transpose(0, 2, 1, 3).reshape(4*5, 5*5), cmap='gray')
Out[11]:
<matplotlib.image.AxesImage at 0x7f3cbefbe4d0>
In [12]:
 
figure(figsize=(10, 5))
imshow(solver.net.params['conv2'][0].diff[:, 0].reshape(5, 10, 5, 5)
       .transpose(0, 2, 1, 3).reshape(5*5, 10*5), cmap='gray')
Out[12]:
<matplotlib.image.AxesImage at 0x7f3cbef5e410>
In [13]:
 
figure(figsize=(20, 10))
imshow(solver.test_nets[0].blobs['conv1'].data[:8, :].reshape(8,20,32,56)
           .transpose(0,2,1,3).reshape(8*32, 20*56), cmap='gray')
Out[13]:
<matplotlib.image.AxesImage at 0x7f3cbee93250>
In [14]:
 
figure(figsize=(50, 25))
imshow(solver.test_nets[0].blobs['conv2'].data[:8, :].reshape(40, 10, 12, 24)
       .transpose(0,2,1,3).reshape(40*12, 10*24), cmap='gray')
Out[14]:
<matplotlib.image.AxesImage at 0x7f3cbecb7910>
In [ ]: