如何修复张量流中的“ ValueError:空训练数据”错误

时间:2019-07-02 00:19:50

标签: python python-3.x tensorflow image-processing keras

我是tensorflow和keras的新手。我正在尝试训练一个模型,以识别石头和剪刀的不同图像。我正在为此使用在线教程,他们为我提供了一个Google协作工作表。当我在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中使用的数据集相同。我无法弄清楚这个错误的原因。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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