我從頭開始構建 inception v3 模型。當我嘗試使用來自 Kaggle 的流行資料集狗和貓來訓練我的模型時,我遇到了Graph execution error。原因可能是我的資料集;我猜有些資料不在 RGB 比例中,或者可能是其他原因。
資料集
模型編譯
model.compile(optimizer=Adam(learning_rate=0.0001),loss = 'categorical_crossentropy', metrics= ['accuracy'])
資料預處理
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input) \
.flow_from_directory(directory=trin_path, target_size=(299,299), classes=['dogs', 'cats'], batch_size=10)
valid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input) \
.flow_from_directory(directory=valid_path, target_size=(299,299), classes=['dogs', 'cats'], batch_size=10)
test_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.inception_v3.preprocess_input) \
.flow_from_directory(directory=test_path, target_size=(299,299), classes=['dogs', 'cats'], batch_size=10, shuffle=False)
模型擬合
r = model.fit(x=train_batches, validation_data=valid_batches, epochs=5)
發生的錯誤
Epoch 1/5
72/500 [===>..........................] - ETA: 1:14 - loss: 0.6929 - accuracy: 0.6042/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 32 bytes but only got 0. Skipping tag 270
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 5 bytes but only got 0. Skipping tag 271
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 8 bytes but only got 0. Skipping tag 272
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 8 bytes but only got 0. Skipping tag 282
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 8 bytes but only got 0. Skipping tag 283
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 20 bytes but only got 0. Skipping tag 306
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:770: UserWarning: Possibly corrupt EXIF data. Expecting to read 48 bytes but only got 0. Skipping tag 532
" Skipping tag %s" % (size, len(data), tag)
/usr/local/lib/python3.7/dist-packages/PIL/TiffImagePlugin.py:788: UserWarning: Corrupt EXIF data. Expecting to read 2 bytes but only got 0.
warnings.warn(str(msg))
500/500 [==============================] - ETA: 0s - loss: 0.6609 - accuracy: 0.6318
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
<ipython-input-90-bd0e48768399> in <module>()
----> 1 r = model.fit(x=train_batches,validation_data=valid_batches,epochs=5)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnknownError: Graph execution error:
2 root error(s) found.
(0) UNKNOWN: UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fb8672df290>
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/script_ops.py", line 271, in __call__
ret = func(*args)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py", line 642, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/data/ops/dataset_ops.py", line 1004, in generator_py_func
values = next(generator_state.get_iterator(iterator_id))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 830, in wrapped_generator
for data in generator_fn():
File "/usr/local/lib/python3.7/dist-packages/keras/engine/data_adapter.py", line 956, in generator_fn
yield x[i]
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 65, in __getitem__
return self._get_batches_of_transformed_samples(index_array)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/iterator.py", line 230, in _get_batches_of_transformed_samples
interpolation=self.interpolation)
File "/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/utils.py", line 114, in load_img
img = pil_image.open(io.BytesIO(f.read()))
File "/usr/local/lib/python3.7/dist-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7fb8672df290>
[[{{node PyFunc}}]]
[[IteratorGetNext]]
(1) CANCELLED: Function was cancelled before it was started
0 successful operations.
0 derived errors ignored. [Op:__inference_test_function_69081]
uj5u.com熱心網友回復:
部分影像的 EXIF 資料損壞,因此 PIL 在訓練期間讀取影像失敗。
方法可以解決它:
PIL : 請先從訓練集中洗掉損壞的影像,例如 PetImages/Cat/666.jpg 和 PetImages/Dog/11702.jpg。另請參閱https://www.kaggle.com/code/sushovansaha9/cat-dog-classification-transferlearning-ipynb。
PIL :洗掉 EXIF 資料但保留影像。
import piexif piexif.remove(filename)
- tf.io :只讀取影像而不洗掉任何影像或 EXIF:
file = tf.io.read_file(filename) image = tf.image.decode_jpeg(file)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487678.html
上一篇:BigquerySoftmax中的TensorFlow預測
下一篇:Errno2沒有這樣的檔案或目錄
