首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复tensorflow中的“ValueError: Empty Training Data”错误

如何修复tensorflow中的“ValueError: Empty Training Data”错误
EN

Stack Overflow用户
提问于 2019-07-02 08:19:50
回答 3查看 5.8K关注 0票数 8

我是tensorflow和keras的新手。我正在尝试训练一个模型来识别岩石、布和剪刀的不同图像。我正在使用一个在线教程,他们为我提供了一个google collab工作表。当我在google collab上训练模型时,一切正常,但是如果我尝试在我的机器上训练模型,它会给我这个错误:ValueValueError: Empty training data

我试着改变批量大小,也试着改变数据集中图像的数量,但是没有帮助(它应该也不会)。

下面是我的代码:

代码语言:javascript
复制
###### 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中使用的数据集相同。我找不出这个错误背后的原因。

EN

回答 3

Stack Overflow用户

发布于 2019-11-16 01:23:55

这个错误可能还有其他原因,但我意识到我的批处理大小大于我的样本大小

票数 11
EN

Stack Overflow用户

发布于 2020-04-07 09:59:38

我也有同样的问题。我的模型在第一个时期结束时训练并给出这个错误(ValueValueError:空训练数据)。我发现这是因为验证路径中没有数据。

票数 1
EN

Stack Overflow用户

发布于 2021-11-18 19:30:12

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56843866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档