使用MMrotate训练自定义数据集报错:
AttributeError: ‘NoneType’ object has no attribute ‘shape’
2024-05-31 17:48:06,121 - mmrotate - INFO - workflow: [('train', 1)], max: 12 epochs
2024-05-31 17:48:06,121 - mmrotate - INFO - Checkpoints will be saved to /home/ma/mmrotate/work_dirs/opt_plane by HardDiskBackend.
2024-05-31 17:48:32,707 - mmrotate - INFO - Epoch [1][50/154] lr: 9.967e-04, eta: 0:15:55, time: 0.532, data_time: 0.352, memory: 2920, loss_cls: 1.1560, loss_bbox: 2.4025, loss: 3.5584, grad_norm: 15.9278
2024-05-31 17:48:56,689 - mmrotate - INFO - Epoch [1][100/154] lr: 1.163e-03, eta: 0:14:43, time: 0.480, data_time: 0.325, memory: 2920, loss_cls: 1.1616, loss_bbox: 1.6778, loss: 2.8393, grad_norm: 15.6813
libpng error: IDAT: CRC error
Traceback (most recent call last):File "tools/train.py", line 194, in <module>main()File "tools/train.py", line 190, in mainmeta=meta)File "/home/ma/mmrotate/mmrotate/apis/train.py", line 144, in train_detectorrunner.run(data_loaders, cfg.workflow)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 136, in runepoch_runner(data_loaders[i], **kwargs)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 49, in trainfor i, data_batch in enumerate(self.data_loader):File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 628, in __next__data = self._next_data()File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1333, in _next_datareturn self._process_data(data)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1359, in _process_datadata.reraise()File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/_utils.py", line 543, in reraiseraise exception
AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loopdata = fetcher.fetch(index)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetchdata = [self.dataset[idx] for idx in possibly_batched_index]File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 58, in <listcomp>data = [self.dataset[idx] for idx in possibly_batched_index]File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmdet/datasets/custom.py", line 220, in __getitem__data = self.prepare_train_img(idx)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmdet/datasets/custom.py", line 243, in prepare_train_imgreturn self.pipeline(results)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmdet/datasets/pipelines/compose.py", line 41, in __call__data = t(data)File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmdet/datasets/pipelines/loading.py", line 76, in __call__results['img_shape'] = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'
解决措施
并非路径加载问题
并非配置文件问题
害我找老半天,看错误信息似乎是开始训练了一两条之后才报错,那么有没有可能是图片加载问题?或者是图片文件损坏?
于是写了以下文件检查python:
import os
from PIL import Imagedef check_images_in_directory(directory):# 记录损坏的图像文件corrupted_files = []# 遍历目录中的所有文件for root, _, files in os.walk(directory):for file in files:# 生成文件的完整路径file_path = os.path.join(root, file)try:# 尝试打开图像文件with Image.open(file_path) as img:# 尝试加载图像内容img.verify()except (IOError, SyntaxError) as e:# 如果图像损坏或无法读取,则记录文件路径corrupted_files.append(file_path)print(f"Corrupted image file: {file_path}, error: {e}")return corrupted_filesdef main():train_images_dir = "/home/ma/dataset/Opt_plane/train/images"val_images_dir = "/home/ma/dataset/Opt_plane/val/images"print("Checking training images...")corrupted_train_images = check_images_in_directory(train_images_dir)print("Checking validation images...")corrupted_val_images = check_images_in_directory(val_images_dir)# 输出损坏的图像文件列表if corrupted_train_images:print("\nCorrupted training images:")for img in corrupted_train_images:print(img)else:print("\nNo corrupted training images found.")if corrupted_val_images:print("\nCorrupted validation images:")for img in corrupted_val_images:print(img)else:print("\nNo corrupted validation images found.")if __name__ == "__main__":main()
执行 python check_images.py,检查文件是否损坏
pip install Pillow
python check_images.py
运行结果如下:
果然是有一个图像文件出错了,把这个文件删除,再次训练,正常了
困扰我两天的问题终于解决了!!!