#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib inline
import sys,os,caffe
################################################################################
caffe_root = '/home/zlf/my_software/my_caffe_configure/caffe-master/'
sys.path.insert(0, caffe_root + 'python')
os.chdir(caffe_root)
if not os.path.isfile(caffe_root + 'examples/ScatterParamsOld/cifar10_quick_iter_10000.caffemodel'):
print "caffemodel is not exist..."
else:
print "Ready to Go ..."
caffe.set_mode_gpu()
net = caffe.Net(caffe_root + 'examples/ScatterParamsOld/deploy.protxt',
caffe_root + 'examples/ScatterParamsOld/cifar10_quick_iter_10000.caffemodel',
caffe.TEST)
print(net.blobs['data'].data.shape)
im = caffe.io.load_image('/home/zlf/Documents/LC20816B_A11k1_1k2_1p1_1p2_1.tif')
print(im.shape)
plt.imshow(im)
plt.axis('off')
################################################################################
def convert_mean(binMean,npyMean):
blob = caffe.proto.caffe_pb2.BlobProto()
bin_mean = open(binMean, 'rb' ).read()
blob.ParseFromString(bin_mean)
arr = np.array( caffe.io.blobproto_to_array(blob) )
npy_mean = arr[0]
np.save(npyMean, npy_mean )
binMean=caffe_root+'examples/cifar10/mean.binaryproto'
npyMean=caffe_root+'examples/cifar10/mean.npy'
convert_mean(binMean,npyMean)
#print(np.load(npyMean))
print("np.load(npyMean).shape")
print(np.load(npyMean).shape)
################################################################################
#import pdb
#pdb.set_trace()
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(npyMean).mean(1).mean(1))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
net.blobs['data'].data[...] = transformer.preprocess('data',im)
inputData=net.blobs['data'].data
################################################################################
plt.figure()
plt.subplot(1,2,1),plt.title("origin")
plt.imshow(im)
plt.axis('off')
plt.subplot(1,2,2),plt.title("subtract mean")
plt.imshow(transformer.deprocess('data', inputData[0]))
plt.axis('off')
################################################################################
net.forward()
for layer_name, blob in net.blobs.iteritems():
print layer_name + '\t' + str(blob.data.shape)
print("\n\n")
for layer_name, param in net.params.iteritems():
print layer_name + '\t' + str(param[0].data.shape), str(param[1].data.shape)
################################################################################
def show_data(data, padsize=1, padval=0):
data -= data.min()
data /= data.max()
# force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
# tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
plt.figure()
plt.imshow(data,cmap='gray')
plt.axis('off')
plt.rcParams['figure.figsize'] = (8, 8)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
################################################################################
show_data(net.blobs['conv1'].data[0])
print ("net.blobs['conv1'].data.shape:" +str(net.blobs['conv1'].data.shape) )
#show_data(net.params['conv1'][0].data.reshape(32*3,5,5))
#print ("net.params['conv1'][0].data.shape:" +str(net.params['conv1'][0].data.shape) )
show_data(net.blobs['pool1'].data[0])
print ("net.blobs['pool1'].data.shape:" +str(net.blobs['pool1'].data.shape) )
show_data(net.blobs['conv2'].data[0],padval=0.5)
print ("net.blobs['conv2'].data.shape:" +str(net.blobs['conv2'].data.shape) )
#show_data(net.params['conv2'][0].data.reshape(32*2,5,5))
#print ("net.params['conv2'][0].data.shape" +str(net.params['conv2'][0].data.shape) )
show_data(net.blobs['conv3'].data[0],padval=0.5)
print ("net.blobs['conv3'].data.shape:" +str(net.blobs['conv3'].data.shape) )
#show_data(net.params['conv3'][0].data.reshape(64*32,5,5)[:1024])
#print ("net.params['conv3'][0].data.shape:" +str(net.params['conv3'][0].data.shape) )
#show_data(net.blobs['pool3'].data[0],padval=0.2)
#print ("net.blobs['pool3'].data.shape:" +str(net.blobs['pool3'].data.shape) )
feat = net.blobs['prob'].data[0]
print feat
plt.figure()
plt.plot(feat.flat)