使用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 main
meta=meta)
File "/home/ma/mmrotate/mmrotate/apis/train.py", line 144, in train_detector
runner.run(data_loaders, cfg.workflow)
File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 136, in run
epoch_runner(data_loaders[i], **kwargs)
File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 49, in train
for 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_data
return self._process_data(data)
File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 1359, in _process_data
data.reraise()
File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/_utils.py", line 543, in reraise
raise 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_loop
data = fetcher.fetch(index)
File "/root/anaconda3/envs/mmrotate/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch
data = [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_img
return 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 Image
def 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_files
def 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
运行结果如下:
果然是有一个图像文件出错了,把这个文件删除,再次训练,正常了
困扰我两天的问题终于解决了!!!