Zubair Ahmed
Zubair Ahmed

Reputation: 91

Tensorflow 2 /Google Colab / EfficientNet Training - AttributeError: 'Node' object has no attribute 'output_masks'

I am trying to train EfficientNetB1 on Google Colab and constantly running into different issues with correct import statements from Keras or Tensorflow.Keras, currently this is how my imports look like

import tensorflow as tf
from tensorflow.keras import backend as K 
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.layers.pooling import AveragePooling2D
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import pickle
import cv2
import os
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels
import efficientnet.keras as enet
from tensorflow.keras.layers import Dense, Dropout, Activation, BatchNormalization, Flatten, Input

and this is how my model looks like

 load the ResNet-50 network, ensuring the head FC layer sets are left
# off
baseModel = enet.EfficientNetB1(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)), pooling='avg')

# Adding 2 fully-connected layers to B0.
x = baseModel.output
x = BatchNormalization()(x)
x = Dropout(0.7)(x)

x = Dense(512)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Dropout(0.5)(x)

x = Dense(512)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)


# Output layer
predictions = Dense(len(lb.classes_), activation="softmax")(x)

model = Model(inputs = baseModel.input, outputs = predictions)

# loop over all layers in the base model and freeze them so they will
# *not* be updated during the training process
for layer in baseModel.layers:
    layer.trainable = False

But for the life of me I can't figure out why I am getting the below error

AttributeError                            Traceback (most recent call last)
<ipython-input-19-269fe6fc6f99> in <module>()
----> 1 baseModel = enet.EfficientNetB1(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)), pooling='avg')
      2 
      3 # Adding 2 fully-connected layers to B0.
      4 x = baseModel.output
      5 x = BatchNormalization()(x)

5 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in _collect_previous_mask(input_tensors)
   1439             inbound_layer, node_index, tensor_index = x._keras_history
   1440             node = inbound_layer._inbound_nodes[node_index]
-> 1441             mask = node.output_masks[tensor_index]
   1442             masks.append(mask)
   1443         else:

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

Upvotes: 1

Views: 1381

Answers (2)

Timbus Calin
Timbus Calin

Reputation: 15023

The problem is the way you import the efficientnet.

You import it from the Keras package and not from the TensorFlow.Keras package.

Change your efficientnet import to

import efficientnet.tfkeras as enet

Upvotes: 2

Bahram Baloch
Bahram Baloch

Reputation: 31

Not sure, but this error maybe caused by wrong TF version. Google Colab for now comes with TF 1.x by default. Try this to change the TF version and see if this resolves the issue.

try:
    %tensorflow_version 2.x
except:
    print("Failed to load")

Upvotes: 0

Related Questions