1

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

2 回答 2

2

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

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

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

import efficientnet.tfkeras as enet
于 2020-04-16T07:57:57.537 回答
0

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

try:
    %tensorflow_version 2.x
except:
    print("Failed to load")
于 2020-03-29T12:10:41.187 回答