繁体   English   中英

如何修复张量流中的“ValueError: Empty Training Data”错误

[英]How to fix 'ValueError: Empty Training Data' error in tensorflow

我是 tensorflow 和 keras 的新手。 我正在尝试训练一个模型来识别石头纸和剪刀的不同图像。 我正在为此使用在线教程,他们为我提供了 google collab 工作表。 当我在 google collab 上训练模型时一切正常,但是如果我尝试在我的机器上训练模型,它会给我这个错误: ValueValueError: Empty training data
我试过改变批量大小,也试过改变数据集中的图像数量,但它没有帮助(它不应该)。

这是我的代码:

###### ROCK PAPER SISSORS #######

import os
import numpy as np
import cv2
import tensorflow as tf
import keras_preprocessing
from keras_preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
# import matplotlib.image as mpimg


# Provide the path to the directory of the classes
rock_dir = os.path.join('/media/visheshchanana/New Volume/Projects/datasets/RPS/rps/rock')
paper_dir = '/media/visheshchanana/New Volume/Projects/datasets/RPS/rps/paper'
scissors_dir = '/media/visheshchanana/New Volume/Projects/datasets/RPS/rps/scissors'


rock_files = os.listdir(rock_dir)
# print(rock_files[:10])
#  ​
paper_files = os.listdir(paper_dir)
# print(paper_files[:10])
# ​
scissors_files = os.listdir(scissors_dir)
# # print(scissors_files[:10])



# Use the augmentation tool to change the augmentation of the images so that we can have a better classifier
TRAINING_DIR = "/media/visheshchanana/New Volume/Projects/datasets/RPS/rps"
training_datagen = ImageDataGenerator(
      rescale = 1./255,
      rotation_range=40,
      width_shift_range=0.2,
      height_shift_range=0.2,
      shear_range=0.2,
      zoom_range=0.2,
      horizontal_flip=True,
      fill_mode='nearest')

# Provide the path to the validation dataset
VALIDATION_DIR = "/media/visheshchanana/New Volume/Projects/datasets/RPS/RPS_validation"
validation_datagen = ImageDataGenerator(rescale = 1./255)

train_generator = training_datagen.flow_from_directory(
    TRAINING_DIR,
    target_size=(150,150),
    class_mode='categorical'
)

validation_generator = validation_datagen.flow_from_directory(
    VALIDATION_DIR,
    target_size=(150,150),
    class_mode='categorical'
)

model = tf.keras.models.Sequential([
    # Note the input shape is the desired size of the image 150x150 with 3 bytes color
    # This is the first convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    # The second convolution
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The third convolution
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # The fourth convolution
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    # Flatten the results to feed into a DNN
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dropout(0.5),
    # 512 neuron hidden layer
    tf.keras.layers.Dense(512, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])


model.summary()
model.compile(loss = 'categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

history = model.fit_generator(train_generator, epochs=5, validation_data = validation_generator, verbose = 1)


该数据集与 google collab 中使用的相同。 我无法弄清楚这个错误背后的原因。

此错误可能还有其他原因,但我意识到我的批次大小大于我的样本大小

我有同样的问题。 我的模型在第一个纪元结束时进行训练并给出此错误 (ValueValueError: Empty training data)。 我发现这是因为验证路径中没有数据。

检查steps_per_epoch是否未设置为 0(例如,由于整数除法)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM