Reshape layer in python

3,220 views
Skip to first unread message

Bartosz Ludwiczuk

unread,
Jan 13, 2015, 3:34:27 AM1/13/15
to caffe...@googlegroups.com
Hi,
I would like to Reshape all net structure, where shape depend on the input image shape. I saw, that #594 introduce possibility of doing that. But I do knot know how I should do it.

I have fully-convolutional model like in example. And I would like to reshape input net to image dimension, then propagate information about reshaping and finally get prediction. I would like to it in python.
I thought that I need to do:
<code>
blob.reshape(1,3,width,height)#reshape input data
net.reshape()#propagate information about reshaping
net.predict(data)
</code>
But I do not know how to get input blob 'data' and if this idea of reshaping is correct.

Could anybody help me with resolving that problem? 

Evan Shelhamer

unread,
Jan 13, 2015, 8:50:48 PM1/13/15
to Bartosz Ludwiczuk, caffe...@googlegroups.com
But I do not know how to get input blob 'data' and if this idea of reshaping is correct.

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

where 'data' is the name of the input blob and the args to reshape are number, channel, height, and width. Then call `net.reshape()`. Set the input data and call forward to process data with the new shape. 

Evan Shelhamer

--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/caffe-users/a774ab88-848c-40c8-8e7d-126289b54e17%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jonathan L Long

unread,
Jan 13, 2015, 9:31:38 PM1/13/15
to Evan Shelhamer, Bartosz Ludwiczuk, caffe...@googlegroups.com
By the way, if you're going to do a forward pass immediately, calling `net.reshape()` is superfluous; just call reshape on the input blob, put your data in it, and call forward. (Calling `net.reshape` can be useful in special situations, e.g., if you want to run a backward pass without a corresponding forward for some reason, or you want to compute all blob sizes without having to wait for a forward pass.)

JLL

Message has been deleted

Pan.H.BestSonny

unread,
Jan 13, 2015, 9:49:49 PM1/13/15
to caffe...@googlegroups.com
I am using python wrapper and my solution is to add a new function under the Pynet to change the input blob and iterately update the Net
Below is this function,hope it can help you

  void set_input_dim(int num,int channel,int height,int width) {
       
     for (int i = 0; i < net_->input_blobs().size(); ++ i)
        net_->input_blobs()[i]->Reshape(num, channel, height, width);
     for (int i = 0; i < net_->layers().size(); ++i)
net_->layers()[i]->Reshape(net_->bottom_vecs()[i], &net_->top_vecs()[i]);
    }

在 2015年1月13日星期二 UTC+8下午4:34:27,Bartosz Ludwiczuk写道:
Message has been deleted

Bartosz Ludwiczuk

unread,
Jan 14, 2015, 3:49:19 AM1/14/15
to caffe...@googlegroups.com, shel...@eecs.berkeley.edu, melg...@gmail.com

Thanks for all answers. Now is it working correctly. 
@Jonathan L Long  Thanks for pointing out the functionality of "net.reshape" function. Now it is much more clear for me:)

Yasser Souri

unread,
Feb 3, 2015, 8:28:44 AM2/3/15
to caffe...@googlegroups.com, melg...@gmail.com, shel...@eecs.berkeley.edu
This way we also have to resize the mean file, let's say if using the CaffeNet. Can you suggest any workaround for that?

Can we just switch to pixel wise mean and still use the `bvlc_reference_caffenet.caffemodel`?

Bartosz Ludwiczuk

unread,
Feb 3, 2015, 10:46:00 AM2/3/15
to caffe...@googlegroups.com, melg...@gmail.com, shel...@eecs.berkeley.edu
For Nets, which I was using "Reshaping", was learn using mean pixel (exactly, they were fine-tuned from Caffe Reference Model). 
If you want to use mean pixel only in prediction step, this could provide different accuracy compared to original Net.
So, if you would like to reshape input dimension, you could:
  • resize mean file and use Reference model
  • train using mean pixel and the predict using that mean pixel

Yasser Souri

unread,
Feb 3, 2015, 11:30:02 AM2/3/15
to caffe...@googlegroups.com, melg...@gmail.com, shel...@eecs.berkeley.edu
Ok, thank you. But few notes:

 - how to resize the mean file? can you share a code snippet?
 - How much do you think training with mean image and testing with mean pixel will degrade the performance?

Yasser Souri

unread,
Feb 3, 2015, 11:31:00 AM2/3/15
to caffe...@googlegroups.com, melg...@gmail.com, shel...@eecs.berkeley.edu
Also Caffe's reference model or CaffeNet is trained using mean image I think.


On Tuesday, February 3, 2015 at 7:16:00 PM UTC+3:30, Bartosz Ludwiczuk wrote:

Evan Shelhamer

unread,
Feb 3, 2015, 12:46:55 PM2/3/15
to Yasser Souri, caffe...@googlegroups.com, melg...@gmail.com
The mean pixel approach works fine. While both training and testing with a mean pixel is best, switching from mean image training to mean pixel testing can work if the mean image is mostly spatially uniform to start -- as the ILSVRC mean is.

At one point we checked CaffeNet with a mean pixel and I don't remember a significant change. We'll likely even change the reference model to have a mean pixel for simplicity when there's time.

For what it's worth I've used a mean pixel in classification and segmentation experiments on several data sets without trouble. The exception could be if you have aligned images with some systematic variation of features with position.
--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.

Yasser Souri

unread,
Feb 3, 2015, 2:36:30 PM2/3/15
to caffe...@googlegroups.com, yasse...@gmail.com, melg...@gmail.com, shel...@eecs.berkeley.edu
Can you please comment on fine-tuning a mean image trained model (like the provided CaffeNet) with another dataset, while changing the transformation settings in the data layer to use a pixel-wise mean. Is this approach going to work?

Jon

unread,
Nov 21, 2015, 4:34:59 AM11/21/15
to Caffe Users, melg...@gmail.com, shel...@eecs.berkeley.edu
If I change the shape for all images in different shapes during feature extraction, will it be costly for reshaping?
Reply all
Reply to author
Forward
0 new messages