AttributeError: 'Node' object has no attribute 'output_masks'
Asked Answered
M

4

15

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)

EDIT1 : 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.

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

Magnetometer answered 13/8, 2018 at 11:41 Comment(2)
I've run this code without any error. keras version(2.2.0) and tensorflow version (1.8.0). I'm also using gpuRodriques
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.Magnetometer
P
47

You're likely importing tf.keras.layers or tf.keras.applications or other keras modules from tensorflow.keras, and mixing these objects with objects from the "pure" keras package, which is not compatible, based upon version, etc.

I recommend seeing if you can import and run everything from the "pure" keras modules; don't use tf.keras while debugging, as they're not necessarily compatible. I had the same problem, and this solution is working for me.

Piggery answered 13/8, 2018 at 22:33 Comment(0)
S
9

I had the same error when I import keras and tenerflow.keras simultaneously: from tensorflow.keras.optimizers import Adam from keras.utils import multi_gpu_model

I solved this problem after changing the code into: from tensorflow.keras.optimizers import Adam from tensorflow.keras.utils import multi_gpu_model

Sumptuary answered 16/11, 2018 at 12:21 Comment(0)
D
5

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 code like:

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

Domingodominguez answered 24/6, 2019 at 9:47 Comment(0)
P
1

if you are importing VGG16 from tensorflow.keras.applications.vgg16 then import all models from tensorflow

from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten

elif you are importing from keras like:

from keras.applications.vgg16 import VGG16

then use model import from keras

from keras.models import Sequential
from keras.layers import Dense
Prakash answered 14/6, 2020 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.