【问题标题】:Tensorflow 2 /Google Colab / EfficientNet Training - AttributeError: 'Node' object has no attribute 'output_masks'Tensorflow 2 /Google Colab / EfficientNet Training - AttributeError: 'Node' object has no attribute 'output_masks'
【发布时间】:2020-07-09 19:42:38
【问题描述】:

我正在尝试在 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'

【问题讨论】:

    标签: tensorflow keras tensorflow2.0 keras-layer efficientnet


    【解决方案1】:

    不确定,但这个错误可能是由错误的 TF 版本引起的。 Google Colab 现在默认附带 TF 1.x。试试这个来更改 TF 版本,看看是否能解决问题。

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

    【讨论】:

      【解决方案2】:

      问题在于您导入高效网络的方式。

      您从Keras 包而不是TensorFlow.Keras 包中导入它。

      将您的高效网络导入更改为

      import efficientnet.tfkeras as enet
      

      【讨论】:

        猜你喜欢
        • 2022-12-01
        • 2020-04-26
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        • 2022-12-27
        • 1970-01-01
        • 2021-10-09
        相关资源
        最近更新 更多