Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to tensorflow and keras. I am trying to train a model to identify different images for rock paper and scissors. I am using an online tutorial for that and they have provided me with a google collab worksheet. When I train the model on google collab everything works fine but if I try training the model on my machine, it gives me this error: ValueValueError: Empty training data
I have tried changing the batch size and also tried tried changing the amount of images in the dataset but it doesnt help(And it shouldn't).

Here is my code:

###### 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)


The dataset is the same as used in the google collab. I can't figure out the reason behind this error.

like image 478
visheshevi Avatar asked Jul 02 '19 00:07

visheshevi


2 Answers

There might be other reasons for this error, but I realized that I had a batch size that was greater than my sample size.

like image 107
mehmet.ali.anil Avatar answered Nov 13 '22 12:11

mehmet.ali.anil


I had the same problem. My model trains and gives this error (ValueValueError: Empty training data) at the end of the first epoch. I figured out that it was because there was no data in the validation path.

like image 1
Mohammed Alali Avatar answered Nov 13 '22 11:11

Mohammed Alali