訓練時報錯報錯
-- Process 2 terminated with the following error:
Traceback (most recent call last):
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/multiprocessing/spawn.py", line 19, in _wrap
fn(i, *args)
File "/home/hxw/detectron2/detectron2/engine/launch.py", line 84, in _distributed_worker
main_func(*args)
File "/home/hxw/detectron2/tools/train_net.py", line 149, in main
return trainer.train()
File "/home/hxw/detectron2/detectron2/engine/defaults.py", line 330, in train
super().train(self.start_iter, self.max_iter)
File "/home/hxw/detectron2/detectron2/engine/train_loop.py", line 132, in train
self.run_step()
File "/home/hxw/detectron2/detectron2/engine/train_loop.py", line 206, in run_step
data = next(self._data_loader_iter)
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 801, in __next__
return self._process_data(data)
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
data.reraise()
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/_utils.py", line 385, in reraise
raise self.exc_type(msg)
OSError: Caught OSError in DataLoader worker process 3.
Original Traceback (most recent call last):
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
data = fetcher.fetch(index)
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/home/hxw/detectron2/detectron2/data/common.py", line 39, in __getitem__
data = self._map_func(self._dataset[cur_idx])
File "/home/hxw/detectron2/detectron2/data/dataset_mapper.py", line 72, in __call__
image = utils.read_image(dataset_dict["file_name"], format=self.img_format)
File "/home/hxw/detectron2/detectron2/data/detection_utils.py", line 53, in read_image
image = image.convert(conversion_format)
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/PIL/Image.py", line 930, in convert
self.load()
File "/home/hxw/anaconda3/envs/xinwen/lib/python3.6/site-packages/PIL/ImageFile.py", line 249, in load
"(%d bytes not processed)" % len(b)
OSError: image file is truncated (7 bytes not processed)
單獨讀取圖檔時報錯
“premature end of JPEG file”
原因
正常JPEG檔案結束辨別EOI,檔案尾2個位元組:0xff,0xd9
異常檔案則不是這兩個辨別符,可以自行檢視。
解決辦法
代碼需要的子產品
import os
import cv2
from io import BytesIO
import skimage
import numpy as np
from PIL import Image
- 方式一
def is_broken_img(img_path):
num = 0
index = 0
for file_ in os.listdir(img_path):
img_ = os.path.join(img_path, file_)
with open(img_, 'rb') as f:
check_chars = f.read()[-2:]
if check_chars != b'\xff\xd9':
print("%s image is broken. "%file_)
num += 1
if index%2000 == 0:
print("index of ok image: ", index)
index += 1
print("number of broken images: ",num)
應用舉例:
is_broken_img("val2017")
- 方式二
def is_valid_file(img_path):
num = 0
index = 0
try:
for file_ in os.listdir(img_path):
imgfile = None
img_ = os.path.join(img_path, file_)
with open(img_, 'rb') as f:
buff = BytesIO()
buff.write(f.read())
buff.seek(0)
temp_img = np.array(Image.open(buff), dtype=np.uint8)
imgfile = cv2.cvtColor(temp_img, cv2.COLOR_RGB2BGR)
buff.close()
if index%2000 == 0:
print("index of ok image: ", index)
index += 1
print("success for allfiles")
except TypeError:
print("%s image is broken. "%file_)
應用舉例:
is_valid_file("train2017")
兩種方式自行選用,如有問題可以留言~