我是机器学习的新手。我在微调VGG16模型上遵循了tutorial。
此代码可以很好地加载模型:
vgg_model = tensorflow.keras.applications.vgg16.VGG16()
但出现此错误:
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer object at 0x000001FA104CBB70>
运行此代码时:
model = Sequential()
for layer in vgg_model.layers[:-1]:
model.add(layer)
依赖项:
我正在关注此blog,但我想使用VGG16。
任何解决此问题的帮助将不胜感激。非常感谢。
答案 0 :(得分:3)
这将不起作用,因为已将tensorflow.keras图层添加到了keras模型中。
vgg_model = tensorflow.keras.applications.vgg16.VGG16()
model = keras.Sequential()
model.add(vgg_model.layers[0])
实例化tensorflow.keras.Sequential()。这将起作用。
model = tensorflow.keras.Sequential()
model.add(vgg_model.layers[0])
答案 1 :(得分:1)
您无需创建InputLayer,只需以与Conv2D /其他层相同的方式导入BatchNormalization层,例如:
<section>
<div class="user">Need some help with HTML</div>
<div class="user">And CSS maybe</div>
<div class="user">Want it to work with long-lenth messages as well, you know. And in all the browsers, even IE...</div>
<div>Sure</div>
<div>Lets test this one</div>
<div>Quite a good in terms of browser support in 2019</div>
<div class="user">Awsome!</div>
<div class="user">Thank you so much</div>
<div>You are welcome</div>
<div class="user">Goodbye</div>
</section>
<footer>
<p>Sticky Footer</p>
</footer>
代替将其导入为独立的Keras图层,即:
from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dropout, BatchNormalization
答案 2 :(得分:1)
要添加到@Manoj Mohan的答案中,您可以使用input_layer
model
中的input_layer
将Keras
添加到layers
中,如下所示:
import keras
from keras.models import Sequential
from keras.layers import InputLayer
model = Sequential()
model.add(InputLayer(input_shape=shape, name=name))
....
如果您使用内置的TensorFlow
Keras
,则导入是不同的,其他情况仍然相同
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer
model = Sequential()
model.add(InputLayer(input_shape=shape, name=name))
....
主要部分,如果要将图层导入到顺序模型中,可以使用以下语法。
import keras
from keras.models import Sequential, load_model
from keras import optimizers
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19
# For VGG16 loading to sequential model
model = Sequential(VGG16().layers)
# For VGG19 loading to sequential model
model = Sequential(VGG19().layers)