使用 alexNet 和 HDF5 DataSetGenerator 以及 AlexNet architecur 进行dogs_vs_cats 分类

问题描述 投票:0回答:1

我正在尝试使用 google colab 笔记本运行代码来分类图像是狗还是猫,但出现错误。我想问是否有人找到了这个问题的解决方案。

我想在colab笔记本上使用tensorflow和keras对狗和猫图像进行分类。这是代码

# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")

from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam`your text`
import json
import os

# construct the training image generator for data augmentation
aug = ImageDataGenerator(rotation_range = 20, zoom_range = 0.15,width_shift_range = 0.2, 
                     height_shift_range = 0.2, shear_range = 0.15,horizontal_flip = True,         fill_mode = "nearest")

# load the RGB means for the training set
means = json.loads(open("dogs_vs_cats_mean.json").read())

# initialize the image preprocessors
sp = ImageResize(227, 227)
pp = PatchPreprocessor(227, 227)
mp = MeanPreprocessor(means["R"], means["G"], means["B"])
iap = ImageToArrayPreprocessor()

# initialize the training and validation dataset generators

trainGen = HDF5DatasetGenerator("/content/train.hdf5", 128, aug = aug,preprocessors = [pp, mp,    iap], classes = 2)
valGen = HDF5DatasetGenerator("/content/val.hdf5", 128,preprocessors = [sp, mp, iap], classes = 2)

# initialize the optimizer
print("[INFO] compiling model...")
opt = Adam(lr = 1e-3)
model = AlexNet.build(width = 227, height = 227, depth = 3, classes = 2, reg = 0.0002)
model.compile(loss = "binary_crossentropy", optimizer = opt, metrics = ["accuracy"],  run_eagerly=True)

#model.compile(optimizer="adam", loss="mean_squared_error", metrics=["acc"], run_eagerly=True)

# construct the set of callbacks
path = os.path.sep.join([OUTPUT_PATH, "{}.png".format(os.getpid())])
callbacks = [TrainingMonitor(path)]

# train the network
model.fit_generator(trainGen.generator(),steps_per_epoch = trainGen.numImages // 128,
                validation_data = valGen.generator(),validation_steps = valGen.numImages // 128,
                epochs =1,max_queue_size = 10,callbacks = callbacks, verbose = 1)"

但是当我编译代码时,我收到此消息错误:

ValueError: Unexpected result of train_function (Empty logs). This could be due to issues in input pipeline that resulted in an empty dataset. Otherwise, please use Model.compile(..., run_eagerly=True), or tf.config.run_functions_eagerly(True) for more information of where went wrong, or file a issue/bug to tf.keras.
python tensorflow keras conv-neural-network hdf5
1个回答
0
投票

经过多次尝试,我想出了解决方案,而且很简单:只需将“steps_per_epoch = trainGen.numImages // 128 from 128 to 32”的数量减少,代码就会立即运行。 祝你好运。

© www.soinside.com 2019 - 2024. All rights reserved.