首页 > 解决方案 > 在不同数据集上训练 tensorFlow Vgg16 算法

问题描述

我正在尝试按照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 的形状?

如果有人可以进行更改并展示如何进行更改!!!!我会很高兴的。

标签: pythontensorflowmachine-learningkerasvgg-net

解决方案


所以我面临着多个问题,其中一个是编译器(内核)一直在死去......我不确定是什么原因造成的,所以我在网上尝试了所有可能的解决方案,但是在搜索了几天后哈哈我遇到了我的问题的解决方案......至少它解决了我的问题......数据集包含训练和测试数据集,训练集包含2个类,测试数据集包含1个类,然后我将其更改为测试数据集中的2个类......这样做解决了内核死错误并解决了我在代码中面临的错误......


推荐阅读