flyyufelix / DenseNet-Keras

DenseNet Implementation in Keras with ImageNet Pretrained Models
MIT License
568 stars 263 forks source link

AttributeError: 'Tensor' object has no attribute 'assign' #1

Closed petteriTeikari closed 7 years ago

petteriTeikari commented 7 years ago

Hi,

Thanks a lot for the repo, but I hit the following error when trying the python test_inference.py with the densenet169:

Traceback (most recent call last):
  File "test_inference.py", line 27, in <module>
    model = DenseNet(reduction=0.5, classes=1000, weights_path=weights_path)
  File "densenet169.py", line 78, in DenseNet
    model.load_weights(weights_path, by_name=True)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2498, in load_weights
    load_weights_from_hdf5_group_by_name(f, self.layers)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2976, in load_weights_from_hdf5_group_by_name
    K.batch_set_value(weight_value_tuples)
  File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2025, in batch_set_value
    assign_op = x.assign(assign_placeholder)
AttributeError: 'Tensor' object has no attribute 'assign'

So when printing the types of the variables in the saved weights I got the following:

<tf.Variable 'conv1/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref>
<tf.Variable 'conv1_bn/gamma:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/beta:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/moving_mean:0' shape=(64,) dtype=float32_ref>
<tf.Variable 'conv1_bn/moving_variance:0' shape=(64,) dtype=float32_ref>
Tensor("conv1_scale/Const:0", shape=(64,), dtype=float32)

And the batch_set_value(tuples) from the backend hit the Tensor problem with "conv1_scale", is there a fix for this to allow the assigning or are the weights somewhat weirdly saved?:

            if hasattr(x, '_assign_placeholder'):
                assign_placeholder = x._assign_placeholder
                assign_op = x._assign_op
            else:
                assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape)
                assign_op = x.assign(assign_placeholder)
                x._assign_placeholder = assign_placeholder
                x._assign_op = assign_op
            assign_ops.append(assign_op)
            feed_dict[assign_placeholder] = value
flyyufelix commented 7 years ago

What version of tensorflow are you using? We currently only supports 0.12.0. Will add support to the latest 1.0 version soon.

petteriTeikari commented 7 years ago

Aah, explaining factor then. Yes I switched to the latest 1.xx version already @flyyufelix

lef-fan commented 7 years ago

Hello, Check https://github.com/flyyufelix/cnn_finetune/issues/2 or my repo here for an update to Keras 2.0.3 and Tensorflow 1.1.0

flyyufelix commented 7 years ago

Apologize for the delay @petteriTeikari . I've already made the necessary changes to get the code to work with the latest versions of Keras (2.0.5) and TensorFlow (1.2.1)

KanuGT commented 7 years ago

hey @flyyufelix I am getting the same issue on tensorflow backend On theano, it is AttributeError: 'TensorConstant' object has no attribute 'set_value'

my keras version is 2.0.4 tf : 1.2.1 th: 0.9.0

flyyufelix commented 7 years ago

Oh yes, I forgot to erase a line in custom_layers.py which caused the error above. It should work now!

chhayac commented 6 years ago

Hey @flyyufelix, I am getting the below error: AttributeError: 'Tensor' object has no attribute 'assign' for generator.fit( noise_input, target_ohe, batch_size=batch_size, epochs = pre_generator_rounds, validation_split=0.1 ) line. I am using tensorflow version '1.4.1'. Could you please help or provide your views for resolving the error.

ubershmekel commented 6 years ago

@chhayac I had this error when my data was accidentally empty :(

Traceback (most recent call last):
  File "script.py", line 187, in <module>
    callbacks=[])
  File "/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1415, in fit_generator
    initial_epoch=initial_epoch)
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training_generator.py", line 39, in fit_generator
    model._make_train_function()
  File "/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 498, in _make_train_function
    loss=self.total_loss)
  File "/anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/keras/optimizers.py", line 197, in get_updates
    self.updates.append(K.update(m, v))
  File "/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 971, in update
    return tf.assign(x, new_x)
  File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/state_ops.py", line 217, in assign
    return ref.assign(value, name=name)
AttributeError: 'Tensor' object has no attribute 'assign'
Colmmm commented 6 years ago

@ubershmekel did you manage to sort this out? I'm having the same problem : , (

ubershmekel commented 6 years ago

I didn't solve the bug in Tensorflow. But I did notice it was only occurring in a degenerate scenario. E.g. if the matrix had no rows, no columns, no shape. If I sent in an empty iterator. I didn't debug exactly to figure out what. Just make sure whatever you're feeding into keras or tf isn't empty.

GuillaumeMougeot commented 6 years ago

@ubershmekel Thanks! I had the same issue and you were right: this error occurs in a degenerate scenario, in my case I controlled the number of filter in a layer through a variable that was null at a moment.