keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.14k stars 19.49k forks source link

AttributeError: 'Node' object has no attribute 'output_masks' #10907

Closed GuillaumeDrillaud closed 6 years ago

GuillaumeDrillaud commented 6 years ago

I use Keras pretrained model VGG16. The problem is that after configuring tensorflow to use the GPU I get an error that I didn't have before when using the CPU.

The error is the following one:

 Traceback (most recent call last):
  File "/home/guillaume/Documents/Allianz/ConstatOrNotConstatv3/train_network.py",      line 109, in <module>
    model = LeNet.build(width=100, height=100, depth=3, classes=5)
  File "/home/guillaume/Documents/Allianz/ConstatOrNotConstatv3/lenet.py", line 39,    in build
    output = model(pretrainedOutput)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 443, in __call__
    previous_mask = _collect_previous_mask(inputs)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 1311, in _collect_previous_mask
mask = node.output_masks[tensor_index]
  AttributeError: 'Node' object has no attribute 'output_masks'

I get it after executing this code :

pretrained_model = VGG16(
    include_top=False,
    input_shape=(height, width, depth),
    weights='imagenet'
)
for layer in pretrained_model.layers:
    layer.trainable = False

model = Sequential()
# first (and only) set of FC => RELU layers
model.add(Flatten())
model.add(Dense(200, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())
model.add(Dense(400, activation='relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization())

# softmax classifier
model.add(Dense(classes,activation='softmax'))

pretrainedInput = pretrained_model.input
pretrainedOutput = pretrained_model.output
output = model(pretrainedOutput)
model = Model(pretrainedInput, output)

I've got keras (2.2.2) and tensorflow(1.10.0rc1). I've also tried on keras 2.2.0 and same error. The thing is that the python environment I use works on others non-pretrained NN.

Moreover, I'm able to connect two homemade models. It's only whith the pretrained ones there is a problem and not only VGG16.

kuabhish commented 6 years ago

Hi Please see the libraries you imported .. import everything of keras ..do not mix up tensorflow

keunwoochoi commented 6 years ago

+1. Native keras doesn't go well with tf.keras.

san-santra commented 6 years ago

Even this example from https://keras.io/applications/ is not working. The same error is coming on the line with GlobalAveragePooling2D.


from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

# create the base pre-trained model
base_model = InceptionV3(weights='imagenet', include_top=False)

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer -- let's say we have 200 classes
predictions = Dense(200, activation='softmax')(x)
NaoyukiIchimura commented 6 years ago

I have the same problem. I guess the output tensor of the vgg16 model cannot be used properly.

I used the output tensor of the vgg16 model in the following simple code. The versions of Keras and TensorFlow were 2.2.2 and 1.10.0rc1, respectively.

The error messages were:

Traceback (most recent call last):
  File "test_vgg16_1.py", line 31, in <module>
    model = Model( inputs=vgg16_conv_model.input , outputs=fc_model(vgg16_conv_model.output) )
  File "/usr/local/lib64/python3.6/site-packages/keras/engine/base_layer.py", line 443, in __call__
    previous_mask = _collect_previous_mask(inputs)
  File "/usr/local/lib64/python3.6/site-packages/keras/engine/base_layer.py", line 1311, in _collect_previous_mask
    mask = node.output_masks[tensor_index]
AttributeError: 'Node' object has no attribute 'output_masks'

I ran the following code:

#
# Setting the image attributes
#
resized_image_height = 150
resized_image_width = 150
no_image_channels = 3
max_pixel_value = 255

#
# Setting the convolutional network of the VGG16 model
#
from keras.applications.vgg16 import VGG16
vgg16_conv_model = VGG16( include_top=False , # remove the fully-connnected network
                          weights='imagenet' ,
                          input_shape=( resized_image_height , resized_image_width , no_image_channels ) )

#
# Setting a new fully-connected network
#
from keras.models import Sequential, Model
from keras.layers import Flatten, Dropout, Dense
fc_model = Sequential()
fc_model.add(Flatten(input_shape=vgg16_conv_model.output_shape[1:]))
fc_model.add(Dropout(0.2))
fc_model.add(Dense( 512 , activation='relu' ))
fc_model.add(Dense( 1 , activation='sigmoid' ))

#
# Combining the models
#
model = Model( inputs=vgg16_conv_model.input , outputs=fc_model(vgg16_conv_model.output) )

I would appreciate it if you could give me some advice to resolve this error.

arquolo commented 6 years ago

Nightly TF builds are, hmm, nightly, so Keras haven't become compatible with all its features yet. If using latest version of TF isn't important for you, downgrade to TensorFlow 1.9, there's no this bug.

SpikeKing commented 6 years ago

@arquolo you are right! I have the same problem. I downgrade to TensorFlow 1.9 with Keras 2.2.2, it's OK!

abdelrahmanmohamed commented 6 years ago

I have TF 1.9 and keras 2.2.2 and it's not working C:\Users\abdo\Anaconda3\python.exe C:/Users/abdo/PycharmProjects/TGSChallenge/modelscript.py C:\Users\abdo\Anaconda3\lib\site-packages\h5py__init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _registerconverters Using TensorFlow backend. Traceback (most recent call last): File "C:/Users/abdo/PycharmProjects/TGSChallenge/modelscript.py", line 91, in resizeInput=Lambda(function=resize)(imageInput) File "C:\Users\abdo\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 731, in call_ inputs, outputs, args, kwargs) File "C:\Users\abdo\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 848, in _set_connectivitymetadata input_tensors=inputs, outputtensors=outputs, arguments=kwargs) File "C:\Users\abdo\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 996, in _add_inboundnode arguments=arguments) File "C:\Users\abdo\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1665, in init layer.outbound_nodes.append(self) AttributeError: 'InputLayer' object has no attribute 'outbound_nodes'

KittyLiou commented 6 years ago

I had a similar problem. I was trying to use VGG16 and got this error AttributeError: 'InputLayer' object has no attribute 'outbound_nodes'. My tensor flow-gpu version was 1.11 and after I downgraded it to 1.9 according to @arquolo it works now! Thanks a lot!

felix-tracxpoint commented 6 years ago

I have encountered the same problem. The most minimal code example already reproduces it:

from keras.layers import GlobalAveragePooling2D
from keras.applications.inception_v3 import InceptionV3
base_model = InceptionV3(weights=None, include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)

The versions are: Keras 2.2.4
Keras-Applications 1.0.4
Keras-Preprocessing 1.0.2
tensorboard 1.12.0
tensorflow-gpu 1.12.0

Is there no solution save downgrading to older versions of keras and tf-gpu?

It feels so defeatist to have to do that...

arquolo commented 6 years ago

@felix-tracxpoint Update keras_applications and keras_prreprocessing to the latest versions, it should work

felix-tracxpoint commented 6 years ago

@arquolo Thanks, that did the trick!!

P.S. I had been under the impression that the main keras package upgrades the others during piping. Obviously I could not have been more wrong.

ymodak commented 6 years ago

Closing this issue since its resolved. Feel to reopen if the issue still persists. Thanks!

ronjian commented 5 years ago

I solved this issue by reinstalling keras to the latest version(2.2.4).

PriyankaSoni15 commented 5 years ago

same issue as @abdelrahmanmohamed i am getting even after installing tensorflow-gpu 1.9.0 and keras latest version 2.2.4 as well as 2.2.2, in both the cases i am getting error saying AttributeError: 'InputLayer' object has no attribute 'outbound_nodes'

Please help

stefanolafs commented 5 years ago

According to this post, it appears that keras-contrib requires Keras 2.1.1

Raseth commented 5 years ago

I had a similar issue, but with different architecture. As people suggested, it's important not to mix keras with tensorflow.keras, so try swapping

from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K

to:

from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras import backend as K

Also make sure, you don't use keras.something inside your code (not only imports) as well, hope it helps : ) Also, I used Keras 2.2.4 with tensorflow 1.10.0

hichamhendy commented 5 years ago

keras | 2.2.5 | 2.2.4 keras-applications | 1.0.8 | 1.0.8 keras-preprocessing | 1.1.0 | 1.1.0 scipy | 1.3.1 | 1.3.1 setuptools | 39.1.0 | 41.0.1 six | 1.12.0 | 1.12.0 tensorboard | 1.9.0 | 1.14.0 tensorflow | 1.9.0 | 1.14.0 tensorflow-estimator | 1.14.0 | 1.14.0 tensorflow-gpu | 1.9.0 | 1.14.0 termcolor | 1.1.0 | 1.1.0 theano | 1.0.2 | 1.0.3

I have the same issue ! although I downgraded everything to be compatible with the above mentioned versions !!

liuhh02 commented 5 years ago

keras | 2.2.5 | 2.2.4 keras-applications | 1.0.8 | 1.0.8 keras-preprocessing | 1.1.0 | 1.1.0 scipy | 1.3.1 | 1.3.1 setuptools | 39.1.0 | 41.0.1 six | 1.12.0 | 1.12.0 tensorboard | 1.9.0 | 1.14.0 tensorflow | 1.9.0 | 1.14.0 tensorflow-estimator | 1.14.0 | 1.14.0 tensorflow-gpu | 1.9.0 | 1.14.0 termcolor | 1.1.0 | 1.1.0 theano | 1.0.2 | 1.0.3

I have the same issue ! although I downgraded everything to be compatible with the above mentioned versions !!

Try using either keras or tensorflow.keras, but don't mix the two of them together. This was what fixed my issue.

unnir commented 4 years ago

extremely annoying issue, I have installed the latest versions of TF and keras. But, have the same problem.

tf - '2.2.0' keras - 2.3.0-tf

luisvivasg commented 4 years ago

If you are using YOLO. This what you have to do. As Raseth said.

from keras.preprocessing import image from keras.models import Model from keras.layers import Dense, GlobalAveragePooling2D from keras import backend as K to:

from tensorflow.keras.preprocessing import image from tensorflow.keras.models import Model from tensorflow.keras.layers import Dense, GlobalAveragePooling2D from tensorflow.keras import backend as K

My only solution was to fork the GIT and edit it there.

I did not need to downgrade Keras or Tensorflow.

Just in case using Yolo also Change K.control_flow_ops.while_loop to tf.while_loop

Pandey-Pankaj commented 4 years ago

@luisvivasg solution works. I used tensorflow.keras instead of keras. I am using tensorflow-gpu=2.0.0

xinsheng44 commented 2 years ago

I have same problem.

File "/data2/wangxinsheng/yft/yft_semantic_train_model/keras_textclassification/base/embedding.py", line 283, in build self.output = NonMaskingLayer()(encoder_layer) File "/data/anaconda3/envs/miss_torch_develop/lib/python3.7/site-packages/keras/engine/base_layer.py", line 443, in __call__ previous_mask = _collect_previous_mask(inputs) File "/data/anaconda3/envs/miss_torch_develop/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1311, in _collect_previous_mask mask = node.output_masks[tensor_index] AttributeError: 'Node' object has no attribute 'output_masks'

K! I can't get tensorflow 1.9. There is no tensorflow 1.9 in https://pypi.org/project/tensorflow-gpu/1.15.2/#history.

Do you know how to fix it?