我正在训练卷积神经网络,以对特定语言(古吉拉特语)的11种11个不同字符进行分类。以下是我的代码:
TRAINING_DIR = "/tmp/characters/train1_set/"
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(TRAINING_DIR,
batch_size=64,
class_mode='categorical',
target_size=(64,64))
VALIDATION_DIR = "/tmp/characters/test1_test/"
validation_datagen = ImageDataGenerator(rescale=1./255)
validation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,
batch_size=64,
class_mode='categorical',
target_size=(64,64))
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(11, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
history = model.fit_generator(train_generator,
steps_per_epoch = 8000,
epochs = 30,
verbose = 1,
validation_data=validation_generator)
但是我遇到以下错误:
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-25-04de914ce972> in <module>()
3 epochs = 30,
4 verbose = 1,
----> 5 validation_data=validation_generator)
6
11 frames
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f338ba64938>
请帮助我解决问题!!!预先感谢。
答案 0 :(得分:0)
ImageDataGenerator
试图读取的文件似乎不是图像,或者可能是缺陷图像。您可以使用Pillow
非常轻松地检测这些类型的缺陷。但是,此处显示的verify()
方法无法检测到截断的图像。您可以使用类似这样的东西-
try:
im = Image.load(filename)
im.verify()
im.close()
except:
#exceptions