vlfeat / matconvnet

MatConvNet: CNNs for MATLAB
Other
1.4k stars 753 forks source link

How to show CIFAR filters in the first layer correctly in a pdf file? #936

Open anselmoferreira opened 7 years ago

anselmoferreira commented 7 years ago

I usually save the first layer filters in a pdf file and this works perfectly well for the MNIST network.

I can do almost the same for the CIFAR network in matconvnet using the vlfeat library. The code is below:

function save_filters()
net=load('net-epoch-142.mat');
net.layers=net.net.layers(1:12);
fig=figure(2) ; clf ; colormap jet;
set(gca,'XtickLabel',[],'YtickLabel',[]);
vl_imarraysc(squeeze(net.net.layers{1}.weights{1}),'spacing',2)
colorbar;
axis off;
title('filters in the first layer (CIFAR network)') ;
print(fig,'Filters-CIFAR','-dpdf', '-r2200')

The result of such operation is the following image:

Pdf Image with filters

As you can see, most of colors on the left can't be found in the colorbar (or I am wrong?), for example, I can't find gray and purple in the colorbar in the right.

Is there a better way to show CIFAR first layer filters in a pdf file using colorbars?

Is it also possible to convert these filters to gray and use gray colormap?

omair-kg commented 7 years ago

i think run colormap jet after vl_imarraysc just in case it is setting the colormap to something else

anselmoferreira commented 7 years ago

Thanks @omair-kg for the help. However, the problem continues even if I set colormap before vl_imarraysc.

lenck commented 7 years ago

Aren't your filters RGB? Meaning with 3 channels? In that case the colorbar is ignored...

anselmoferreira commented 7 years ago

Hi @lenck, good point. Is it possible to convert the filters to grayscale and then use a grayscale colormap? thanks.

lenck commented 7 years ago

Sure, as simple as taking average over the third dimension... mean(filters, 3)...

anselmoferreira commented 7 years ago

Thanks again @lenck ,

The size of filters in the first layer are

>> size(squeeze(net.layers{1}.weights{1}))

ans =

     5     5     3    32

If I do the following:

vl_imarraysc(mean(squeeze(net.layers{1}.weights{1}),3),'spacing',2);

The following error returns to me:

Error using vl_imarraysc (line 59)
A has more than three dimensions and the third one is not equal to three.

Error in salva_filtros (line 10)
    vl_imarraysc(mean(squeeze(net.layers{1}.weights{1}),3),'spacing',2);

What I did wrong?

lenck commented 7 years ago

You just need to squeeze it and it should work then :) Or you can update the vl_imarraysc from my fork... Hopefully we will push this to main vlfeat repo soon...