Tensorflow 2 /Google Colab / EfficientNet Training - AttributeError:'Node'对象没有属性'output_masks'



我正试图在Google Colab上训练EfficientNetB1,并不断遇到来自Keras或Tensorflow的正确导入语句的不同问题。Keras,目前我的导入看起来像

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

这就是我的模型看起来像的样子

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

但就我的一生而言,我不明白为什么我会得到以下错误

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'

问题在于导入efficientnet的方式。

您可以从Keras程序包而不是从TensorFlow.Keras程序包导入它。

将您的efficientnet导入更改为

import efficientnet.tfkeras as enet

不确定,但这个错误可能是由错误的TF版本引起的。谷歌Colab现在默认配备TF1.x。尝试更改TF版本,看看这是否解决了问题。

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

最新更新