caffe icon indicating copy to clipboard operation
caffe copied to clipboard

The CNN seems to not learn anything at all. Getting all zeroes as output.

Open aalok1993 opened this issue 11 years ago • 6 comments

Hi,

I am using caffe-future for Fully Convolutional Networks. I am trying to work on a regression problem where the input to the FCN is an 256 X 256 image and the output that the FCN is supposed to produce is also an 256 X 256 image (for running the FCN, I am using the PR of @kashefy).

Initially while training, I was getting a lots of Nan's and Inf's in my weight parameters. I tried to modify the learning rates and this problem went away. (a small query: what various parameters should I try to modify to solve this issue) After that the issue I am facing is that, when I take an image and pass it forward, most of the blob values are coming out to be zeros and the final image I am getting is an image filled with zeros. And the weight parameters learned by the network become very huge. Below I have described the various outputs in detail.

In order to figure out the issue, I took a very small architecture(a toy example) which consists of a single convolutional layer, Relu layer, a pooling layer followed by a deconvolution layer. Also, to make it simple initially I am taking the (output label = input data), so currently my network works like an autoencoder. All it has to do is learn an approximation of the identity function. But it fails to do even that. Following are the prototxt files: deploy.prototxt, train_val.prototxt and solver.prototxt.

I trained the network for 1000 iteration and used the snapshot as my model during evaluation. Following is the code and output, which describes what I obtain after 1000 iterations. (NOTE: I have done the training in GPU mode as well as CPU mode. But I get the same result in each case.)

Initializing caffe and Loading the network

caffe.set_mode_cpu()
net = caffe.Net('MyNet_deploy.prototxt', 'snapshots/MyNet_iter_1000.caffemodel', caffe.TEST)

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
net.blobs['data'].reshape(1,3,256,256)
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image('Train/data/0001.jpg'))
out = net.forward()

The blobs

[(k, v.data.shape) for k, v in net.blobs.items()]

[('data', (1, 3, 256, 256)), ('conv1_1', (1, 64, 256, 256)), ('pool1', (1, 64, 128, 128)), ('upsample2', (1, 3, 256, 256))]

The parameters

[(k, v[0].data.shape) for k, v in net.params.items()]

[('conv1_1', (64, 3, 3, 3)), ('upsample2', (64, 3, 4, 4))]

The conv layer weights

print net.params['conv1_1'][0].data

[[[[ -8.90221119 -19.70544052 -21.9944973 ] [ -28.27580643 -44.15635681 -51.63126373] [ -39.88535309 -59.30950165 -62.64734268]]

[[ -9.73268604 -21.16998863 -23.3067379 ] [ -28.68981361 -45.53733826 -52.59268951] [ -40.60289001 -60.55314255 -63.270298 ]]

[[ -7.46913862 -18.73158836 -20.8146286 ] [ -26.17634583 -42.74364471 -49.60507965] [ -37.86455536 -57.53972244 -60.60445023]]]

...,

[[[-1756.36547852 -1774.34521484 -1799.48950195] [-1785.36962891 -1828.19641113 -1854.27050781] [-1797.99133301 -1837.64611816 -1851.94775391]]

[[-1765.79675293 -1784.0411377 -1808.77331543] [-1794.91149902 -1837.94580078 -1863.44091797] [-1807.38049316 -1847.3157959 -1861.21154785]]

[[-1588.7590332 -1605.53112793 -1629.23632812] [-1617.13195801 -1658.57910156 -1683.2623291 ] [-1629.62780762 -1667.82958984 -1681.2208252 ]]]

The deconv layer weights

print net.params['upsample2'][0].data

[[[[ -0.2453279 -0.42636055 -0.52841532 -0.63897181] [ -0.75671118 -0.7169919 -0.82515067 -1.17307651] [ -0.96557409 -0.9307059 -1.03437531 -1.36865413] [ -1.08291376 -1.28496742 -1.37371349 -1.44269586]]

[[ -0.2445658 -0.42509246 -0.52721226 -0.63792503] [ -0.75604206 -0.71580309 -0.82402509 -1.17214358] [ -0.96510863 -0.9297061 -1.03346813 -1.36791492] [ -1.08263409 -1.28413677 -1.37294734 -1.44208062]]

[[ -0.24634758 -0.42725337 -0.52933705 -0.64002675] [ -0.75833076 -0.71846634 -0.82661229 -1.17464745] [ -0.9673087 -0.93222517 -1.03590178 -1.37026465] [ -1.08475745 -1.28656888 -1.37528908 -1.44430864]]]

...,

[[[-83.92314148 -85.89565277 -86.49584961 -86.15866852] [-86.43471527 -88.2796402 -88.8900528 -88.71788788] [-87.11362457 -88.95469666 -89.52527618 -89.28121185] [-86.79399109 -88.77192688 -89.2594986 -88.69790649]]

[[-83.90159607 -85.87146759 -86.47241974 -86.13829803] [-86.41383362 -88.25655365 -88.86827087 -88.69919586] [-87.09313202 -88.93185425 -89.50371552 -89.26304626] [-86.77391815 -88.74938965 -89.23816681 -88.67989349]]

[[-84.18785858 -86.16223145 -86.7639389 -86.43247986] [-86.70091248 -88.54869843 -89.16176605 -88.99497223] [-87.38189697 -89.2256546 -89.79877472 -89.56040192] [-87.06370544 -89.04449463 -89.53528595 -88.9778595 ]]]

The data blob

print net.blobs['data'].data

[[[[ 0.02745098 0.00784314 0.02745098 ..., 0.19607843 0.14509805 0.11764706] [ 0. 0.13725491 0.66666669 ..., 0.93725491 0.89803922 0.90980393] [ 0.3019608 0.87058824 0.99607843 ..., 0.9137255 0.89411765 0.90980393] ..., [ 0.03921569 0.01960784 0.01176471 ..., 0.08235294 0.07843138 0.07843138] [ 0. 0. 0. ..., 0.08235294 0.07843138 0.07450981] [ 0.00392157 0. 0.00392157 ..., 0.07450981 0.07058824 0.07058824]]

[[ 0.03137255 0.01176471 0.03137255 ..., 0.21568628 0.16470589 0.13725491] [ 0.00392157 0.14509805 0.67450982 ..., 0.95686275 0.91764706 0.92941177] [ 0.30980393 0.87843138 1. ..., 0.93725491 0.91764706 0.93333334] ..., [ 0.06666667 0.04705882 0.03921569 ..., 0.1254902 0.12156863 0.12156863] [ 0.02352941 0.01960784 0.01568628 ..., 0.1254902 0.12156863 0.11764706] [ 0.03137255 0.01568628 0.02352941 ..., 0.11764706 0.11372549 0.11372549]]

[[ 0.01176471 0. 0.01176471 ..., 0.19215687 0.14117648 0.11372549] [ 0. 0.1254902 0.65490198 ..., 0.93333334 0.89411765 0.90588236] [ 0.29803923 0.86666667 0.99215686 ..., 0.92156863 0.90196079 0.91764706] ..., [ 0.03921569 0.01960784 0.01176471 ..., 0.10196079 0.09803922 0.09803922] [ 0. 0. 0. ..., 0.10196079 0.09803922 0.09411765] [ 0.00392157 0. 0. ..., 0.09411765 0.09019608 0.09019608]]]]

The conv1_1 blob

print net.blobs['conv1_1'].data

[[[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]]]

The pool1 blob

print net.blobs['pool1'].data

[[[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]]]

The upsample2 blob

print net.blobs['upsample2'].data

[[[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]

[[ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] ..., [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.] [ 0. 0. 0. ..., 0. 0. 0.]]]]

Some queries

As it can be seen above, the outputs of conv1_1 , pool1 and upsample2 are all filled with zeros. It seem like the net is learning to output a blank image irrespective of the input. Also the weights learned by the FCN consists of many large values. I am unable to understand what is causing this issues. [EDIT: I have taken a large weight decay which solves the problem of large weights. Is this a right way to do so. Does taking a large weight decay cause some other problems ?-] I have run the training in both CPU mode as well as GPU mode. But in both the cases I get the same result. So the problem is not related to GPU it seems. Also I wanted to ask, what is a better weight filler for convolutional layer : xavier or gaussian. Also in gaussian how to take the value of std. Could you please help me in understanding how to solve these issues. Thanks a lot.

aalok1993 avatar Aug 16 '15 19:08 aalok1993

Hi @aalok1993 -- it's great that you gave so much details, but a Github issue isn't the right place for this discussion.

From CONTRIBUTING.md:

Please do not post usage, installation, or modeling questions, or other requests for help to Issues. Use the caffe-users list instead. This helps developers maintain a clear, uncluttered, and efficient view of the state of Caffe.

seanbell avatar Aug 16 '15 20:08 seanbell

Thanks for your reply. I'll post it in the caffe-user list. Sorry for the inconvenience. I just had a small question : since I am getting all zeroes in the output, is the problem caused due to some incorrect lmdb input file, or is the problem with the solver parameters ?

aalok1993 avatar Aug 16 '15 20:08 aalok1993

Hmm, it does seem curious that the the parameters are nonzero, the data is nonzero, but the output activations are zero. Is there any chance that you are accessing the net blobs before the forwards pass, or using a stale reference to a blob?

seanbell avatar Aug 16 '15 21:08 seanbell

Hi, I ensured that I am using the proper reference for the blobs. I am using the exact code as mentioned above.

It is indeed very strange that the output is zero inspite of the nonzero input ans params. What it feels like is that : the network is learning to output zero irrespective of the input. So the autoencoder instead of learning an identity function, is learning a function that nullifies the image.

I had tried the same code with a different architecture : which consisted of 31 layers of convolution layers (no POOLING layers or upsampling layers). In that cases the network gave nonzero blob values. But the output values were in the range (0.3,0.35) which is a very small range. Ideally the range should be (0,1).

So I was wondering, is there any problems caused due to the upsampling(deconv) layer ?

While training, the test error stays fixed at a particular value. There is no decrease in the test error. It seems that the output is zero in all the iterations, hence resulting in the same error value. It seems like the network is learning nothing at all.

aalok1993 avatar Aug 16 '15 22:08 aalok1993

hi,

I meet the similar problems, i also train a simple network with image labels. I could get value from first two conv layers, but after it, I get zeros values from following conv layers. I tried to use two LMDB layers or two ImageData layers, but I got the same results. My network can not learn anything, but the loss could decrease while training.

Do u find some solutions? It will be very helpful.

xiaozai avatar Apr 12 '17 13:04 xiaozai

Please post your question on the Caffe Users mailing list and/or the stackoverflow.com with the "Caffe" tag (posting on both is fine, as long as you reference the stackoverflow post in your mailing list email).

williford avatar Apr 12 '17 14:04 williford