detectron2训练报错

训练时报错报错

-- 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")

两种方式自行选用,如有问题可以留言~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sophia_xw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值