0
votes

I am trying to append a dense layer to vgg19 network, but it gives me the below error. Can anyone help me with this?

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 (default, Jun 28 2018, 07:39:16) Type "copyright", "credits" or "license" for more information.

IPython 7.8.0 -- An enhanced Interactive Python.

runfile('/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test', wdir='/Users/sadegh/Dropbox/Moosavi Khorzooghi-04') 2019-11-29 01:51:22.516366: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-11-29 01:51:22.526913: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fc84c7a2700 executing computations on platform Host. Devices: 2019-11-29 01:51:22.526926: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version Traceback (most recent call last):

File "", line 1, in runfile('/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test', wdir='/Users/sadegh/Dropbox/Moosavi Khorzooghi-04')

File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "/opt/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/sadegh/Dropbox/Moosavi Khorzooghi-04/test", line 11, in x = tensorflow.keras.layers.Dense(10, activation="relu",name="",trainable=True)(model.layers[-1])

File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 887, in call self._maybe_build(inputs)

File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 2122, in _maybe_build self.input_spec, inputs, self.name)

File "/opt/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_spec.py", line 163, in assert_input_compatibility if x.shape.ndims is None:

AttributeError: 'Dense' object has no attribute 'shape'

1

1 Answers

0
votes

Assuming it's a classification problem, therefore, you should use softmax activation instead of relu in the output layer. Also you can access input and output of the backbone VGG19 model. You should manually pool or flatten the output of base model if you instantiate it with default setting. Instead you can set pooling="avg" or pooling="max" for global average or max pooling respectively. Moreover, You can use something like following:

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())