【问题标题】:trying to append a dense layer to vgg19 network试图将密集层附加到 vgg19 网络
【发布时间】:2020-03-24 19:40:19
【问题描述】:

我正在尝试将密集层附加到 vgg19 网络,但它给了我以下错误。谁能帮我解决这个问题?

import tensorflow 
from tensorflow.keras.applications.vgg19 import VGG19 
model = VGG19()  
x = tensorflow.keras.layers.Dense(10,
activation="relu",name="",trainable=True)(model.layers[-1]) 
model = tensorflow.keras.Model(inputs = model.layers[0], outputs = x)

Python 3.7.0(默认,2018 年 6 月 28 日,07:39:16)键入“版权”, “credits”或“license”了解更多信息。

IPython 7.8.0 -- 增强的交互式 Python。

runfile('/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test', wdir='/Users/sadegh/Dropbox/Moosavi Khorzooghi-04') 2019-11-29 01:51:22.516366:我 tensorflow/core/platform/cpu_feature_guard.cc:142] 您的 CPU 支持此 TensorFlow 二进制文件不支持的指令 编译使用:AVX2 FMA 2019-11-29 01:51:22.526913: I tensorflow/compiler/xla/service/service.cc:168] XLA 服务 0x7fc84c7a2700 在平台主机上执行计算。设备: 2019-11-29 01:51:22.526926: 我 tensorflow/compiler/xla/service/service.cc:175] StreamExecutor 设备(0):主机,默认版本回溯(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test', wdir='/Users/sadegh/Dropbox/Moosavi Khorzooghi-04')

文件 "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", 第 827 行,在运行文件中 execfile(文件名,命名空间)

文件 "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", 第 110 行,在 execfile 中 exec(编译(f.read(),文件名,'exec'),命名空间)

文件“/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test”,第 11 行,在 x = tensorflow.keras.layers.Dense(10, activation="relu",name="",trainable=True)(model.layers[-1])

文件 “/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py”, 第 887 行,在 调用 self._maybe_build(输入)

文件 “/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py”, 第 2122 行,在 _maybe_build self.input_spec,输入,self.name)

文件 “/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_spec.py”, 第 163 行,在 assert_input_compatibility 如果 x.shape.ndims 为无:

AttributeError: 'Dense' 对象没有属性 'shape'

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    假设这是一个分类问题,因此,您应该在输出层使用softmax 激活而不是relu。您还可以访问主干VGG19 模型的输入和输出。如果使用默认设置实例化基本模型,则应手动合并或展平基本模型的输出。相反,您可以将pooling="avg" or pooling="max" 分别设置为全局平均或最大池。此外,您可以使用以下内容:

    base_model = VGG19(input_shape=(224, 224, 3), weights='imagenet', pooling="avg", include_top=False)
    x = Dense(10, activation="softmax", name="output")(base_model.output)
    model = Model(inputs=base_model.input, outputs=x)
    print(model.summary())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      相关资源
      最近更新 更多