首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“‘Concatenate”对象没有属性“平坦”

“‘Concatenate”对象没有属性“平坦”
EN

Stack Overflow用户
提问于 2020-06-12 16:13:13
回答 1查看 1K关注 0票数 1

我试着从预先训练过的模型中提取特征,并在我自己的模型上使用。我可以成功地实例化输入V3模型,并保存tu用作模型输入的输出,但是当我尝试使用它时,我会得到错误。我试图删除扁平层,但问题似乎不是这个。我认为问题是关于last_output,但不知道如何解决它。守则:

代码语言:javascript
复制
#%% Imports.
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras import layers, Model
from tensorflow.keras.applications.inception_v3 import InceptionV3
import os, signal
import numpy as np

#%% Instatiate an Inception V3 model

url = "https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5" # Get the weights from the pretrained model
local_weights_file = tf.keras.utils.get_file("inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5", origin = url, extract = True) 

pre_trained_model = InceptionV3(input_shape=(150, 150, 3), include_top=False, weights=None) # include_top=False argument, we load a network that doesn't include
pre_trained_model.load_weights(local_weights_file)                                          # the classification layers at the top—ideal for feature extraction.

# Make the model non-trainable, since we will only use it for feature extraction; we won't update the weights of the pretrained model during training.
for layers in pre_trained_model.layers:
    layers.trainable = False

# The layer we will use for feature extraction in Inception v3 is called mixed7. It is not the bottleneck of the network, but we are using it to keep a
# sufficiently large feature map (7x7 in this case). (Using the bottleneck layer would have resulting in a 3x3 feature map, which is a bit small.)
last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape:', last_layer.output_shape)
last_output = last_layer.output
print(last_output)

# %% Stick a fully connected classifier on top of last_output
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)

# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)

# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)

# Add a final sigmoid layer for classification
x = layers.Dense(1, activation='sigmoid')(x)

# Configure and compile the model
model = Model(pre_trained_model.input, x)
model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=0.0001),
              metrics=['acc'])        

错误:

代码语言:javascript
复制
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
c:\Users\jpaul\Code\Google_ML_Crash_Course\02_Practica\02_Image_Classification\image_classification_part3.py in 
      39 # Flatten the output layer to 1 dimension
----> 40 x = layers.Flatten()(last_output)
      41 
      42 # Add a fully connected layer with 1,024 hidden units and ReLU activation
      43 x = layers.Dense(1024, activation='relu')(x)

AttributeError: 'Concatenate' object has no attribute 'Flatten'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 16:21:16

在您的for循环中,您从

from tensorflow.keras import layers

因此,当您尝试创建一个新的Flatten()层时,标识符layers包含一个Concatenate对象,而不是您期望的Keras layers模块。

更改for循环中的变量名,您应该会做得很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62348246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档