0

First off I should mention that I've read this question already:

StopIteration: generator_output = next(output_generator)

But my issue is somewhat different so I don't think this is a duplicate (although the titles are the same!).

I'm running a relatively simple cnn to try and classify images into either [contains head] or [doesn't contain head], I call them [head] and [not] for short.

The issue I'm having is that I don't def my generator the way the other question does (see code below), so I'm not sure how to make my generator able to generate 'infinite' data.

Code:

# Importing the Keras libraries and packages
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

# Initialising the CNN
classifier = Sequential()
# Step 1 - Convolution
classifier.add(Conv2D(32, (3, 3), input_shape = (40, 40, 3), activation = 'relu'))
# Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# Step 3 - Flattening
classifier.add(Flatten())
# Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Part 2 - Fitting the CNN to the images
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size = (40, 40),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size = (40, 40),
batch_size = 64,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 4000,
epochs = 25,
validation_data = test_set,
validation_steps = 400)
classifier.save("CSGOHeads.h5")
# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/1.bmp', target_size = (40, 40))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'head'
else:
    prediction = 'not'

For completeness here's the entire dump from the cmd:

C:\Anaconda36\envs\tensorflow\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:135] successfully opened CUDA library cublas64_80.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:135] successfully opened CUDA library cudnn64_5.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:135] successfully opened CUDA library cufft64_80.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:135] successfully opened CUDA library nvcuda.dll locally
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:135] successfully opened CUDA library curand64_80.dll locally
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "BestSplits" device_type: "CPU"') for unknown op: BestSplits
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "CountExtremelyRandomStats" device_type: "CPU"') for unknown op: CountExtremelyRandomStats
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "FinishedNodes" device_type: "CPU"') for unknown op: FinishedNodes
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "GrowTree" device_type: "CPU"') for unknown op: GrowTree
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ReinterpretStringToFloat" device_type: "CPU"') for unknown op: ReinterpretStringToFloat
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "SampleInputs" device_type: "CPU"') for unknown op: SampleInputs
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "ScatterAddNdim" device_type: "CPU"') for unknown op: ScatterAddNdim
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNInsert" device_type: "CPU"') for unknown op: TopNInsert
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TopNRemove" device_type: "CPU"') for unknown op: TopNRemove
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "TreePredictions" device_type: "CPU"') for unknown op: TreePredictions
E c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\framework\op_kernel.cc:943] OpKernel ('op: "UpdateFertileSlots" device_type: "CPU"') for unknown op: UpdateFertileSlots
Found 177986 images belonging to 2 classes.
Found 19857 images belonging to 2 classes.
Epoch 1/25
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:885] Found device 0 with properties:
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.582
pciBusID 0000:23:00.0
Total memory: 11.00GiB
Free memory: 9.10GiB
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:906] DMA: 0
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:916] 0:   Y
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:975] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:23:00.0)
4000/4000 [==============================] - 109s - loss: 0.0039 - acc: 0.9987 - val_loss: 1.4612e-04 - val_acc: 1.0000
Epoch 2/25
 829/4000 [=====>........................] - ETA: 68s - loss: 4.8378e-06 - acc: 1.0000Exception in thread Thread-39:
Traceback (most recent call last):
  File "C:\Anaconda36\envs\tensorflow\lib\threading.py", line 914, in _bootstrap_inner
  File "C:\Anaconda36\envs\tensorflow\lib\threading.py", line 862, in run
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\utils\data_utils.py", line 568, in data_generator_task
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 737, in __next__
    return self.next(*args, **kwargs)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 1033, in next
    target_size=self.target_size)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 322, in load_img
    img = pil_image.open(path)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\PIL\Image.py", line 2539, in open
    prefix = fp.read(16)
OSError: [Errno 22] Invalid argument

Traceback (most recent call last):
  File "kerasmod.py", line 47, in <module>
    validation_steps = 400)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\models.py", line 1121, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "C:\Anaconda36\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 2011, in fit_generator
    generator_output = next(output_generator)
StopIteration
today
  • 32,602
  • 8
  • 95
  • 115
FraserOfSmeg
  • 1,128
  • 2
  • 23
  • 41
  • Your problem is that it seems that one of your image files is corrupt, it has nothing to do with the generator. – Dr. Snoopy Jun 17 '18 at 22:38
  • @MatiasValdenegro I agree with you, since the error `OSError: [Errno 22] Invalid argument` occurred in `open` method; but how do you explain the fact that this was occurred in the second epoch? All the images have been already read at least one time in the first epoch so there must not a corrupt file issue??!! – today Jun 17 '18 at 23:08
  • @MatiasValdenegro I think I found the explanation: If the number of training images is greater than `32*4000` or number of validation images is greater than `64*400`, it could happen, since by default in `flow_from_directory` the `shuffle` parameter is set to `True` and `seed` parameter is set to `None`. Hence each time a different set of images may be chosen for training/validation. – today Jun 17 '18 at 23:30
  • @today No, shuffle doesn't work like that. – Dr. Snoopy Jun 18 '18 at 05:52

0 Answers0