gpt4 book ai didi

python - 是什么导致了这个 Keras 图像处理错误?

转载 作者:行者123 更新时间:2023-12-01 08:00:07 24 4
gpt4 key购买 nike

我正在尝试训练 CNN,但程序每次都会失败,但都是在随机位置。出现的错误是OSError:图像文件被截断(15 个字节未处理)。仅供引用,这是处理上一步中的增强数据。代码如下:

import os

from keras import backend as K
from keras.layers import Activation, Dense, Dropout
from keras.layers import Conv2D, Flatten, MaxPooling2D
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

cwd = os.getcwd()

# dimensions of our images.
img_width, img_height = 150, 150

train_data_dir = (str(cwd) + r'\augmented\train\\')
validation_data_dir = (str(cwd) + r'\augmented\validation\\')
nb_train_samples = 1000
nb_validation_samples = 500
epochs = 20
batch_size = 10

if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
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'])

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
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')

history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)

model.save_weights('chips.h5')

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)

plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()

CNN 开始迭代,但通常在 epoch 2 的某个地方我会得到这个被截断的图像。不过,所有图像都以相同的方式增强。有人有想法吗?

整个回溯是:

Traceback (most recent call last):
File "c:\Users\Mac2\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\ptvsd_launcher.py", line 45, in <module>
main(ptvsdArgs)
File "c:\Users\Mac2\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd\__main__.py", line 391, in main
run()
File "c:\Users\Mac2\.vscode\extensions\ms-python.python-2019.3.6558\pythonFiles\lib\python\ptvsd\__main__.py", line 272, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\Users\Mac2\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\Users\Mac2\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\Users\Mac2\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\Mac2\Desktop\CNN\Chips\train.py", line 119, in <module>
validation_steps=nb_validation_samples // batch_size)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", lineapper
return func(*args, **kwargs)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\engine\training.py", line 1t_generator
initial_epoch=initial_epoch)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\engine\training_generator.p81, in fit_generator
generator_output = next(output_generator)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\utils\data_utils.py", linet
six.reraise(*sys.exc_info())
File "C:\Users\Mac2\Anaconda3\lib\site-packages\six.py", line 693, in reraise
raise value
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\utils\data_utils.py", linet
inputs = self.queue.get(block=True).get()
File "C:\Users\Mac2\Anaconda3\lib\multiprocessing\pool.py", line 644, in get
raise self._value
File "C:\Users\Mac2\Anaconda3\lib\multiprocessing\pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras\utils\data_utils.py", linext_sample
return six.next(_SHARED_SEQUENCES[uid])
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras_preprocessing\image\iteratoe 100, in __next__
return self.next(*args, **kwargs)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras_preprocessing\image\iteratoe 112, in next
return self._get_batches_of_transformed_samples(index_array)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras_preprocessing\image\iteratoe 226, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\keras_preprocessing\image\utils.p26, in load_img
img = img.resize(width_height_tuple, resample)
File "C:\Users\Mac2\Anaconda3\lib\site-packages\PIL\Image.py", line 1763, in resi self.load()
File "C:\Users\Mac2\Anaconda3\lib\site-packages\PIL\ImageFile.py", line 232, in l "(%d bytes not processed)" % len(b))
OSError: image file is truncated (15 bytes not processed)

最佳答案

您的问题似乎是图像大小不同,您的程序似乎由于枕头中的设置而崩溃。这是official code枕头模块,如果您搜索 truncated 您可以看到为什么会引发此错误。 Here得票最高的答案提供了代码来防止截断图像的导入错误。

或者,您可以删除图像,here是有人遇到你的问题,只是简单地整理出所有小于 50kB 的图像。希望这会有所帮助。

关于python - 是什么导致了这个 Keras 图像处理错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55775003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com