Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在定制模型上微调Vgg16_Python_Tensorflow_Keras_Deep Learning_Vgg Net - Fatal编程技术网

Python 在定制模型上微调Vgg16

Python 在定制模型上微调Vgg16,python,tensorflow,keras,deep-learning,vgg-net,Python,Tensorflow,Keras,Deep Learning,Vgg Net,我试图使用转移学习在自定义预训练模型iimdata.h5上训练我的另一个数据集,这似乎产生了值错误 ValueError:您正试图将包含3个层的权重文件加载到包含0个层的模型中 import matplotlib.pyplot as plt from keras import applications from keras.preprocessing.image import ImageDataGenerator from keras import optimizers from keras.m

我试图使用转移学习在自定义预训练模型iimdata.h5上训练我的另一个数据集,这似乎产生了值错误

ValueError:您正试图将包含3个层的权重文件加载到包含0个层的模型中

import matplotlib.pyplot as plt
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense

# path to the model weights files.
weights_path = 'C:/Users/444/.spyder-py3/IAM/iimdata.h5'
top_model_weights_path = 'C:/Users/444/.spyder-py3/IAM/iimdata.h5'
# dimensions of our images.
img_width, img_height = 500, 500

nb_train_samples = 20
batch_size = 16

# build the VGG16 network
model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(500, 500,3))
print('Model loaded.')

# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten())
top_model.add(Dense(1024, activation='tanh'))
top_model.add(Dropout(0.5))
top_model.add(Dense(20, activation='softmax'))


# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)   # error here

# add the model on top of the convolutional base
model.add(top_model)

# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:-4]:
    layer.trainable = False

# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
              metrics=['accuracy'])

# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
    rescale=1. / 255)



# generator for reading traindata from folder
train_generator = train_datagen.flow_from_directory(
   "C:/Users/444/.spyder-py3/IAM/Training-test/train_patches/",
    target_size = (500,500),
    color_mode = 'rgb',
    batch_size = batch_size,
    class_mode = 'categorical')


# fine-tune the model
history= model.fit_generator(
    train_generator,
    samples_per_epoch=nb_train_samples,
    epochs=30)

model_json = model.to_json()
open('rus.json','w').write(model_json)
model.save_weights('rus.h5',overwrite=True) 
请帮助我解决错误。

替换代码

top\u模型。添加(展平())

有了代码

model.add(展平(输入形状=(无、15、15、512))

应该解决您的错误

这是因为,在
保存
模型
iimdata.h5
之前,您应该已经训练了
模型
,并且应该将
输入形状
传递到
顶层模型
的第一层,即
展平

在本例中,您将在
model
之后添加
top\u model
。因此,
模型的最后一层将是

block5_池(MaxPooling2D)(无、15、15、512)0

因此,
(None,15,15,512)
的形状应该传递给
展平层

这篇文章也解释了这一点

如果这不能解决您的问题,请告诉我,并提供
iimdata.h5
,我将很乐意帮助您

希望这有帮助。学习愉快

替换代码

top\u模型。添加(展平())

有了代码

model.add(展平(输入形状=(无、15、15、512))

应该解决您的错误

这是因为,在
保存
模型
iimdata.h5
之前,您应该已经训练了
模型
,并且应该将
输入形状
传递到
顶层模型
的第一层,即
展平

在本例中,您将在
model
之后添加
top\u model
。因此,
模型的最后一层将是

block5_池(MaxPooling2D)(无、15、15、512)0

因此,
(None,15,15,512)
的形状应该传递给
展平层

这篇文章也解释了这一点

如果这不能解决您的问题,请告诉我,并提供
iimdata.h5
,我将很乐意帮助您


希望这有帮助。学习愉快

你的问题现在解决了吗?另外,请分享
iimdata.h5
的链接,keras的版本是什么?这样我们才能帮助你。你的问题现在解决了吗?另外,请分享
iimdata.h5
的链接,keras的版本是什么?这样我们才能帮助你。