Unable to reshape blob

213 views
Skip to first unread message

Varshaneya V

unread,
Nov 19, 2016, 3:59:47 AM11/19/16
to Caffe Users
I wish to extract features from all the frames from a video in sumMe dataset. The frames of the video are generated using MATLAB and stored in .mat file which I am loading in python. I am trying to reshape blob size for the frame to fit in. Here is the code that I have written:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import caffe
import scipy.io as sio
import sys

vidName = sys.argv[1]
directory = '../framesSumMe/'

net = caffe.Net('deploy3.prototxt','bvlc_alexnet.caffemodel',caffe.TEST)

data = sio.loadmat(directory + vidName +'Frames.mat')

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.load('python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1))
transformer.set_transpose('data', (2,0,1))
transformer.set_channel_swap('data', (2,1,0))
transformer.set_raw_scale('data', 255.0)

net.blobs['data'].reshape(1,3,int(data['width']),int(data['height']))

features = dict()
temp = np.zeros((data['frCount'][0][0],4096));

features['frCount']=data['frCount'][0][0]

for i in range(0,data['frCount'][0][0]):
#load the image in the data layer
    im = data['allFrames'][:,:,:,i]
    net.blobs['data'].data[...] = transformer.preprocess('data', im)
    out = net.forward_all(data=np.asarray([transformer.preprocess('data', im)]))
    temp[0:4095][i] = net.blobs['fc6'].data.copy()

features['allFeatures']=temp

sio.savemat(directory + vidName +'Features',features)

When I run the code with the name of the .mat file as runtime argument I get an error saying "ValueError: could not broadcast input array from shape (3,227,227) into shape (1,3,320,240) "  for the line net.blobs['data'].data[...] = transformer.preprocess('data', im). I do not get any error when I use the default value of 227 for height and width of the blob.

Kindly point out where is the mistake.

Grayson Adkins

unread,
Nov 21, 2016, 3:46:10 PM11/21/16
to Caffe Users
For whatever reason, the values of data['width'] and data['height'] are 320 and 240, respectively. You are trying to reshape a (3,227,227) array into a (1,3,320,240) array, however, when reshaping, the number of elements must not change. Thus, you get an error. 

If you just want to convert your 3D array into a 4D array (i.e. into caffe format: (N x C x H x W), try just adding a new axis for a batch size of one, as follows:

input = net.blobs['data']
input = input[np.newaxis, ...]

Hope this helps.

Varshaneya V

unread,
Nov 25, 2016, 1:43:16 AM11/25/16
to Caffe Users
Sorry that did not work. It threw up an error "TypeError: 'Blob' object has no attribute '__getitem__" in the line "net = net[np.newaxis, ...]". 

Could you suggest methods to change the number of elements in the blob itself because my video frames are NOT of the dimension 227 X 227. if I consider 227X227 then I will lose out on some part of the image and that will not be fed into the neural network.
Reply all
Reply to author
Forward
0 new messages