简体   繁体   English

“连接” object 没有属性“扁平化”

[英]'Concatenate' object has no attribute 'Flatten'

I'm trying to extract features from a pretrained model and use on my own model.我正在尝试从预训练的 model 中提取特征并在我自己的 model 上使用。 I can successfully instantiate the Inveption V3 Model and save the outputs tu use as inputs for my model, but as i try to use it i get error.我可以成功实例化 Inveption V3 Model 并将输出保存为我的 model 的输入,但是当我尝试使用它时出现错误。 I tried to delete the Flatten layer but looks like the problem isnt this.我试图删除 Flatten 层,但看起来问题不是这个。 I think the problem is about the last_output but have no clue on how to solve it.我认为问题是关于 last_output 但不知道如何解决它。 The code:编码:

#%% 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'])        

the error:错误:

---------------------------------------------------------------------------
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'

In your for loop, you overwrote the layers identifier from the import statement of在您的for layers中,您从

from tensorflow.keras import layers

So when you try to create a new Flatten() layer, the identifier layers contains a Concatenate object rather than the Keras layers module you were expecting.因此,当您尝试创建一个新的Flatten()层时,标识符layers包含一个Concatenate object 而不是您期望的 Keras layers模块。

Change the variable name in your for loop and you should be good.更改for循环中的变量名称,您应该会很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'int'对象没有属性'flatten' - 'int' object has no attribute 'flatten' AttributeError: 'Flatten' object 没有属性 'shape' - AttributeError: 'Flatten' object has no attribute 'shape' AttributeError:“连接”对象没有属性“编译” - AttributeError: 'Concatenate' object has no attribute 'compile' AttributeError: 'Concatenate' 对象没有属性 'shape' - AttributeError: 'Concatenate' object has no attribute 'shape' / edit /'RequestContext'对象处的AttributeError没有属性'flatten'(Django 1.6.7) - AttributeError at /edit/ 'RequestContext' object has no attribute 'flatten' (Django 1.6.7) 展平调整大小的图像 - AttributeError: 'Image' object 没有属性 'flatten' - Flattening a resized image - AttributeError: 'Image' object has no attribute 'flatten' 当我使用 y.flatten() 时,'Series' 对象没有属性 'flatten' - 'Series' object has no attribute 'flatten' when I use y.flatten() 创建多通道网络:“连接”对象没有“形状”属性 - Creating a multi-channel network: 'Concatenate' object has no attribute 'shape' 'numpy.ndarray' object 没有属性 '连接' 错误 - 'numpy.ndarray' object has no attribute 'concatenate' error 嘿,我正在制作一个赠品命令,但它会抛出错误“‘async_generator’对象没有属性‘flatten’” - hey im making a giveaway command but it throws a error "'async_generator' object has no attribute 'flatten'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM