titu1994 / DenseNet

DenseNet implementation in Keras
MIT License
707 stars 294 forks source link

Error when loading weights without top layer #16

Closed joheras closed 7 years ago

joheras commented 7 years ago

Hi,

first of all, thanks for your work providing this Keras implementation of DenseNets.

I am trying to create a DenseNet model without the last layer using the instruction: model = densenet.DenseNet((32, 32,3), depth=40, growth_rate=12, nb_filter=16,include_top=False)

but it produces the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "densenet.py", line 169, in DenseNet
    model.load_weights(weights_path)
  File "/home/joheras/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2572, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "/home/joheras/.virtualenvs/keras/local/lib/python2.7/site-packages/keras/engine/topology.py", line 3012, in load_weights_from_hdf5_group
    g = f[name]
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-build-YjgiH2/h5py/h5py/_objects.c:2687)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-build-YjgiH2/h5py/h5py/_objects.c:2645)
  File "/home/joheras/.virtualenvs/keras/local/lib/python2.7/site-packages/h5py/_hl/group.py", line 166, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-build-YjgiH2/h5py/h5py/_objects.c:2687)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-build-YjgiH2/h5py/h5py/_objects.c:2645)
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open (/tmp/pip-build-YjgiH2/h5py/h5py/h5o.c:3440)
KeyError: "Unable to open object (Object 'dense_2' doesn't exist)"

It does not matter whether I use theano or tensorflow as backend.

Do you know how to solve this?

Thanks in advance. Jónathan

titu1994 commented 7 years ago

I will have to look at the weights file. Sadly, I'm travelling right now so I can't do that for the next 11 days or so.

joheras commented 7 years ago

Thanks, whenever you have time to do it will be fine for me. Since loading the weights including the top layer works properly, and I am planning to try this network for transfer learning, I wonder if it makes sense to load the weights including the top layer, and then remove the last layer of the network. Will it produce the same results as loading the weights without the last layer?

titu1994 commented 7 years ago

DenseNet doesn't have Fully Connected Layers. It feeds directly into a Softmax class layer after the global average pooling.

If you load up all the laters, then pop the last layer (it would be the same as using the no top model). To make sure how many layers to pop, look at the last section of the DenseNet code.

Sorry for the hassle and I'll look into it asap

joheras commented 7 years ago

Yes, that was what I meant.

Thank you so much, and congratulations for this nice work.