我正在尝试按照YouTube上的视频指南训练VGG16模型。
我抄了教官给的代码。在此之后,我尝试使用系统中可用的一些图像来训练模型。我上传了一些图片,只是为了给读者演示。
简介:
我试图为VGG16更改数据集并为我的数据集进行训练。VGG16使用IMAGE_SIZE = [224, 224]
,我不知道我拥有的图像的大小!这就是问题所在吗?
我已经在OneDrive上传了一些图像,但是当我改变数据集时,我遇到了多个错误,其中一个是内核死亡,这经常出现。在解决了这个问题之后,我在提供给培训和测试的图像上出现了一些错误。我需要帮助来训练模型。
# -*- coding: utf-8 -*-
"""
@author: Krish.Naik
"""
import tensorflow as tf
from keras.models import load_model
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
# re-size all the images to this
IMAGE_SIZE = [224, 224]
train_path = 'Datasets/Train'
valid_path = 'Datasets/Test'
# add preprocessing layer to the front of VGG
vgg = VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
# don't train existing weights
for layer in vgg.layers:
layer.trainable = False
# useful for getting number of classes
folders = glob('Datasets/Train/*')
# our layers - you can add more if you want
x = Flatten()(vgg.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='softmax')(x)
# create a model object
model = Model(inputs=vgg.input, outputs=prediction)
# view the structure of the model
model.summary()
# tell the model what cost and optimization method to use
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('Datasets/Train',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('Datasets/Test',
target_size = (224, 224),
batch_size = 32,
class_mode = 'categorical')
'''r=model.fit_generator(training_set,
samples_per_epoch = 8000,
nb_epoch = 5,
validation_data = test_set,
nb_val_samples = 2000)'''
# fit the model
r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=5,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)
# loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
plt.savefig('LossVal_loss')
# accuracies
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
plt.savefig('AccVal_acc')
model.save('facefeatures_new_model.h5')
当训练模型时得到这个错误
检查目标时错误:期望dense_3具有形状(2,),但得到形状(1,)的数组
我该怎么做才能解决这个问题?
如何改变数组的形状,以匹配dense_3的形状??
如果有人能做的变化,并显示将如何完成!!我会很高兴的。
所以有多个问题,我正面临其中一个是编译器(内核)不断死亡…我不确定是什么原因,所以我在网上尝试了所有可能的解决方案,但在搜索了几天之后,哈哈,我找到了我的解决方案…至少它解决了我的问题……数据集包含训练和测试数据集,训练集包含2个类,测试数据集包含1个类,然后我在测试数据集....中将其更改为2个类这样做解决了内核die错误,并解决了我在代码中面临的错误…