我在Keras中使用以下代码使用CNN对某些图像进行分类。我在第一个时期出现操作系统错误........................................... ................................................... ................................................... ........................
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import os, os.path
import glob
# dimensions of our images.
img_width, img_height = 224,224
train_data_dir ='D:/Sowmya/data/train/' #Note the forward slash
validation_data_dir = 'D:/Sowmya/data/valid'
epochs = 50
batch_size = 2
len(list(glob.iglob("D:/Sowmya/data/train/*/*.tif", recursive=True)))
num_train=sum(1 for i in glob.iglob("D:/Sowmya/data/train/*/*.tif"))
print ("Number fo training images =",num_train)
len(list(glob.iglob("D:/Sowmya/data/valid/*/*.tif", recursive=True)))
num_valid=sum(1 for i in glob.iglob("D:/Sowmya/data/valid/*/*.tif"))
print ("Number fo validation images =",num_valid)
input_shape = (img_width, img_height, 3)
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=num_train // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=num_valid // batch_size)
model.save_weights('first_try.h5')
我遇到以下错误。
Epoch 1/50
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-15-da2e7998cf61> in <module>
c:\users\sowmy\appdata\local\programs\python\python36\lib\site-packages\keras_preprocessing\image.py in next(self)
1968 # The transformation of images is not under thread lock
1969 # so it can be done in parallel
-> 1970 return self._get_batches_of_transformed_samples(index_array)
1971
1972
c:\users\sowmy\appdata\local\programs\python\python36\lib\site-packages\keras_preprocessing\image.py in _get_batches_of_transformed_samples(self, index_array)
1921 color_mode=self.color_mode,
1922 target_size=self.target_size,
-> 1923 interpolation=self.interpolation)
1924 x = img_to_array(img, data_format=self.data_format)
1925 # Pillow images should be closed after `load_img`,
c:\users\sowmy\appdata\local\programs\python\python36\lib\site-packages\keras_preprocessing\image.py in load_img(path, grayscale, color_mode, target_size, interpolation)
496 raise ImportError('Could not import PIL.Image. '
497 'The use of `array_to_img` requires PIL.')
--> 498 img = pil_image.open(path)
499 if color_mode == 'grayscale':
500 if img.mode != 'L':
c:\users\sowmy\appdata\local\programs\python\python36\lib\site-packages\PIL\Image.py in open(fp, mode)
2655 warnings.warn(message)
2656 raise IOError("cannot identify image file %r"
-> 2657 % (filename if filename else fp))
2658
2659 #
OSError: cannot identify image file 'D:/Sowmya/data/train/RP\\Extract_tif969.TIF'
#
有人可以帮助您理解该错误吗?