Invalid image dimension while trying to visualize filters and features

1,262 views
Skip to first unread message

Rohit

unread,
Jan 20, 2016, 4:29:55 AM1/20/16
to Caffe Users
Hi
while I am trying to visualize my features and  filters, i am getting  errors:
    
File "netviz.py", line 53, in <module>
    vis_square(filters.transpose(0, 2, 3, 1))
  File "netviz.py", line 47, in vis_square
    plt.imshow(data)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2892, in imshow
    imlim=imlim, resample=resample, url=url, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7300, in imshow
    im.set_data(X)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 429, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data



This is my python code and my image size is 288*288

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.cm as cm
import cv2 
plt.rcParams['figure.figsize'] = (6.0, 4.0)
import sys 
import caffe
MODEL_FILE = '/usr/share/caffe-master/models/bvlc_reference_caffenet/emotiondeploy.prototxt'
PRETRAINED = '/usr/share/caffe-master/models/bvlc_reference_caffenet/mydepthmodels/caffenet_train_iter_800.caffemodel'
IMAGE_FILE1 = '/home/iimtech7/depthdataset/train/neutral/neutral2.jpeg'
IMAGE_FILE2 = '/home/iimtech7/depthdataset/train/smile/smile2.jpeg'
blob = caffe.proto.caffe_pb2.BlobProto()
data = open( '/usr/share/caffe-master/examples/imagenet/mean.binaryproto' , 'rb' ).read()
blob.ParseFromString(data)
arr = np.array( caffe.io.blobproto_to_array(blob) )
out = arr[0]
net = caffe.Classifier(MODEL_FILE,PRETRAINED,mean=out,channel_swap=(2,1,0),raw_scale=255,image_dims=(288,288))
net2 = caffe.Classifier(MODEL_FILE,PRETRAINED,mean=out,channel_swap=(2,1,0),raw_scale=255,image_dims=(288,288))

input_image1 = caffe.io.load_image(IMAGE_FILE1)
input_image2 = caffe.io.load_image(IMAGE_FILE2)
# Display the test images
plt.subplot(1,4,1).imshow(input_image1),plt.title('neutral')
plt.subplot(1,4,2).imshow(input_image2),plt.title('side')
plt.show()
#prediction1 = net.predict([input_image1]) 
#prediction2 = net2.predict([input_image2])
#width=0.1
#plt.bar(np.arange(2),prediction1[0],width,color='blue',label='neutral')
#plt.bar(np.arange(2)+width,prediction2[0],width,color='green',label='side')
#plt.legend()
#plt.ylabel('Class Probability')
#plt.show()
def vis_square(data, padsize=1, padval=0):
    data -= data.min()
    data /= data.max()
    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))
    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.imshow(data)
    plt.show()
    

plt.rcParams['figure.figsize'] = (50.0, 40.0)
filters = net.params['conv4'][0].data
vis_square(filters.transpose(0, 2, 3, 1)) 

feat = net.blobs['conv1'].data[0,:96]
plt.subplot(1,2,1),plt.title('neutral')
vis_square(feat, padval=1)
net.blobs['conv1'].data.shape

Jan C Peters

unread,
Jan 25, 2016, 7:17:14 AM1/25/16
to Caffe Users
Well, as the error says: the dimensions (or "shape" in numpy speak) of "data" is one that imshow cannot understand. So try to "print data.shape" just before the imshow and see what shape it actually has. imshow can only handle [H, W, 3] or [H, W].

Jan
Reply all
Reply to author
Forward
0 new messages