A slight problem in visualizing layers in classification.inpy

225 views
Skip to first unread message

Hossein Hasanpour

unread,
May 7, 2016, 6:26:30 AM5/7/16
to Caffe Users
I'm trying to get the visualization for all layers both for weights and their filters 
For this instead of repeating:
# the parameters are a list of [weights, biases]
filters = net.params['conv1'][0].data
vis_square(filters.transpose(0, 2, 3, 1))

and changing layer name, I tried using a loop like this : 
 for layer_name, param in net.params.iteritems():
   
# the parameters are a list of [weights, biases]
    filters
= net.params[layer_name][0].data
    vis_square
(filters.transpose(0, 2, 3, 1))

now it works fine for the first layer, but gives this error for and stops continuing with the rest: 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-cf7d5999a45c> in <module>()
      2     # the parameters are a list of [weights, biases]
      3     filters = net.params[layer_name][0].data
----> 4     vis_square(filters.transpose(0, 2, 3, 1))

ValueError: axes don't match array


Now what is wrong here? 
by the way what is 
filters.transpose(0, 2, 3, 1)
is doing? I'd be grateful if anyone could give me a hand on this (clearly I dont have python background)

Hossein Hasanpour

unread,
May 9, 2016, 1:56:15 PM5/9/16
to Caffe Users
Any help is greatly appreciated :(

Jan

unread,
May 10, 2016, 4:17:05 AM5/10/16
to Caffe Users
I don't really know how vis_square works, but I can tell about the filters variable. It is a 4D array of shape [n_out, n_in, filter_height, filter_width], which contains all filters of the convolutional layer. The transpose function just transposes the axes of this array, e.g. after that call filters.transpose(0, 2, 3, 1) the filters dimensions are [n_out, filter_height, filter_width, n_in].

Jan

Hossein Hasanpour

unread,
May 10, 2016, 4:26:38 AM5/10/16
to Caffe Users
Thanks alot dear Jan, 
By any chance do you have any script for visualizing layers in python? 
and as a side question, why do we even need to visualize wieghts ? When I visualize wieghts I get color feature maps. whats the use for that? 
I get that visualizing the feature maps, shows the effect of filters, like as if a gabor filter was applied on the input image. but over all I dont get the idea how would these visualization would benefit us . 
again Thank you a great deal for all the helps you have so far given us

Jan

unread,
May 10, 2016, 5:35:41 AM5/10/16
to Caffe Users
Well, I usually just use imshow() and maybe some subplot commands (matplotlib).

Yeah, visualizing the filters usually just shows _that_ the filters have a gabor-like nature, and combining filters from several consecutive layers cleverly sometimes reveals some more refined structure which is apparently extracted/recognized by this combination of filters. There are some approaches trying to understand what features are extracted by which layer/filters and how high-level these features are. E.g. you can look at http://arxiv.org/abs/1311.2901. But in the end, what counts is how well the network performs. And measuring that is not always as easy as building a test set and be done with the final accuracy on that...

Jan

Sai Krishna

unread,
Aug 23, 2016, 3:07:47 PM8/23/16
to Caffe Users
Hi Jan,

What if the channels of the weight filter are not equal to 3. imshow() doesn't work in that case. Do we any other solution in that case?

thank you!
Reply all
Reply to author
Forward
0 new messages