question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: 'NoneType' object has no attribute 'shape'

See original GitHub issue

Checklist

  1. I have searched related issues but cannot get the expected help.
  2. The bug has not been fixed in the latest version.

Describe the bug Hello, the training of the last few days was very normal, and this error suddenly appeared today. The same data is not wrong on another machine.

Reproduction

  1. My config
# model settings
model = dict(
    type='RetinaNet',
    pretrained='torchvision://resnet50',
    backbone=dict(
        type='ResNet',
        depth=50,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        frozen_stages=1,
        style='pytorch'),
    neck=dict(
        type='FPN',
        in_channels=[256, 512, 1024, 2048],
        out_channels=256,
        start_level=1,
        add_extra_convs=True,
        num_outs=5),
    bbox_head=dict(
        type='FreeAnchorRetinaHead',
        num_classes=6,
        in_channels=256,
        stacked_convs=4,
        feat_channels=256,
        octave_base_scale=2,
        scales_per_octave=3,
        anchor_ratios=[0.5, 1.0, 2.0],
        anchor_strides=[8, 16, 32, 64, 128],
        target_means=[.0, .0, .0, .0],
        target_stds=[0.1, 0.1, 0.2, 0.2],
        loss_bbox=dict(type='SmoothL1Loss', beta=0.11, loss_weight=0.75)))
# training and testing settings
train_cfg = dict(
    assigner=dict(
        type='MaxIoUAssigner',
        pos_iou_thr=0.5,
        neg_iou_thr=0.4,
        min_pos_iou=0,
        ignore_iof_thr=-1),
    allowed_border=-1,
    pos_weight=-1,
    debug=False)
test_cfg = dict(
    nms_pre=3000,
    min_bbox_size=0,
    score_thr=0.05,
    nms=dict(type='nms', iou_thr=0.5),
    max_per_img=1800)
# dataset settings
dataset_type = 'VOCDataset'
data_root = '/home/shelly/mmdetection/data/VOCdevkit/'
img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', with_bbox=True),
    dict(type='Resize', img_scale=(1024, 1024), keep_ratio=True),
    dict(type='RandomFlip', flip_ratio=0.5),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='Pad', size_divisor=32),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
]

val_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', with_bbox=True),
    dict(type='Resize', img_scale=(1024, 1024), keep_ratio=True),
    dict(type='RandomFlip', flip_ratio=0.0),
    dict(type='Normalize', **img_norm_cfg),
    dict(type='Pad', size_divisor=32),
    dict(type='DefaultFormatBundle'),
    dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']),
]

test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(
        type='MultiScaleFlipAug',
        img_scale=(1024, 1024),
        flip=False,
        transforms=[
            dict(type='Resize', keep_ratio=True),
            dict(type='RandomFlip'),
            dict(type='Normalize', **img_norm_cfg),
            dict(type='Pad', size_divisor=32),
            dict(type='ImageToTensor', keys=['img']),
            dict(type='Collect', keys=['img']),
        ])
]
data = dict(
    imgs_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        type=dataset_type,
        ann_file=data_root + 'VOC2012/ImageSets/Main/train.txt',   
        img_prefix=data_root + 'VOC2012/',    
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        ann_file=data_root + 'VOC2012/ImageSets/Main/test.txt',
        img_prefix=data_root + 'VOC2012/',   
        pipeline=val_pipeline),
    test=dict(
        type=dataset_type,
        ann_file=data_root + 'VOC2012/ImageSets/Main/test.txt',
        img_prefix=data_root + 'VOC2012/',   
        pipeline=test_pipeline))
# optimizer
optimizer = dict(type='SGD', lr=0.00125, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
# learning policy
lr_config = dict(
    policy='step',
    warmup='linear',
    warmup_iters=500,
    warmup_ratio=1.0 / 3,
    step=[8, 11])
checkpoint_config = dict(interval=1)
# yapf:disable
log_config = dict(
    interval=50,
    hooks=[
        dict(type='TextLoggerHook'),
        # dict(type='TensorboardLoggerHook')
    ])
# yapf:enable
# runtime settings
total_epochs = 12
device_ids = range(8)
dist_params = dict(backend='nccl')
log_level = 'INFO'
work_dir = './work_dirs/retinanet_free_anchor_r50_fpn_1x'
load_from = None
resume_from = None
workflow = [('train', 4),('val', 1)]

Environment

  • OS: ubuntu16.04
  • GCC: 7.3.0
  • PyTorch version:1.2.0
  • How you installed PyTorch: conda
  • GPU mode: 1080ti
  • CUDA and CUDNN version: 9.0 7.3

Error traceback

2019-12-14 13:01:55,044 - INFO - Epoch [3][3050/3648]   lr: 0.00125, eta: 5:12:16, time: 0.306, data_time: 0.004, memory: 8879, positive_bag_loss: 1.3066, negative_bag_loss: 0.1516, loss: 1.4582
2019-12-14 13:02:10,510 - INFO - Epoch [3][3100/3648]   lr: 0.00125, eta: 5:11:08, time: 0.309, data_time: 0.004, memory: 8879, positive_bag_loss: 1.4534, negative_bag_loss: 0.1561, loss: 1.6096
2019-12-14 13:02:37,388 - INFO - Epoch [3][3150/3648]   lr: 0.00125, eta: 5:10:36, time: 0.538, data_time: 0.004, memory: 8879, positive_bag_loss: 1.3954, negative_bag_loss: 0.1542, loss: 1.5496
2019-12-14 13:02:52,779 - INFO - Epoch [3][3200/3648]   lr: 0.00125, eta: 5:09:29, time: 0.308, data_time: 0.004, memory: 8879, positive_bag_loss: 1.3824, negative_bag_loss: 0.1583, loss: 1.5406
libpng error: bad adaptive filter value
Traceback (most recent call last):

  File "/home/shelly/mmdetection/tools/train.py", line 113, in <module>
    main()

  File "/home/shelly/mmdetection/tools/train.py", line 109, in main
    logger=logger)

  File "/home/shelly/mmdetection/mmdet/apis/train.py", line 60, in train_detector
    _non_dist_train(model, dataset, cfg, validate=validate)

  File "/home/shelly/mmdetection/mmdet/apis/train.py", line 232, in _non_dist_train
    runner.run(data_loaders, cfg.workflow, cfg.total_epochs)

  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/runner.py", line 363, in run
    epoch_runner(data_loaders[i], **kwargs)

  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/runner.py", line 263, in train
    for i, data_batch in enumerate(data_loader):

  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 819, in __next__
    return self._process_data(data)

  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 846, in _process_data
    data.reraise()

  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/_utils.py", line 369, in reraise
    raise self.exc_type(msg)

AttributeError: Caught AttributeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/shelly/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/shelly/mmdetection/mmdet/datasets/custom.py", line 128, in __getitem__
    data = self.prepare_train_img(idx)
  File "/home/shelly/mmdetection/mmdet/datasets/custom.py", line 141, in prepare_train_img
    return self.pipeline(results)
  File "/home/shelly/mmdetection/mmdet/datasets/pipelines/compose.py", line 24, in __call__
    data = t(data)
  File "/home/shelly/mmdetection/mmdet/datasets/pipelines/loading.py", line 30, in __call__
    results['img_shape'] = img.shape
AttributeError: 'NoneType' object has no attribute 'shape'

Bug fix If you have already identified the reason, you can provide the information here. If you are willing to create a PR to fix it, please also leave a comment here and that would be much appreciated!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
SirRachacommented, Jun 21, 2022

Same issue here, and no way to find out which file exactly is broken (in 5M files)

I found the image this way, out side of the training code

`

FIND IMAGE THAT CANT BE OPENED

from PIL import Image pname = list/of/filepaths for it in pname: try: img = Image.open(it) img.verify() # to verify if its an img img.close() #to close img and free memory space except (IOError, SyntaxError) as e: print(‘Bad file:’, it) `

0reactions
dreamflashercommented, Jun 18, 2022

Same issue here, and no way to find out which file exactly is broken (in 5M files)

Read more comments on GitHub >

github_iconTop Results From Across the Web

OpenCV NoneType object has no attribute shape
It means that somewhere a function which should return a image just returned None and therefore has no shape attribute.
Read more >
How to Fix AttributeError: 'nonetype' object has no attribute ...
You can fix AttributeError: 'nonetype' object has no attribute 'shape' by checking to see if the object is None before accessing its attributes....
Read more >
AttributeError: 'NoneType' object has no attribute 'shape'
At a guess imread has failed and input is a NoneType instead of a numpy array. As such it doesn't have a shape...
Read more >
AttributeError: 'NoneType' object has no attribute 'shape' #21
this is an error I usually get, if the datasets' folder structure is either not correct or if some of the images are...
Read more >
AttributeError: \'NoneType\' object has no attribute \'shape\' on ...
The 'nonetype' object has no attribute 'shape' code which you have used is completely correct but then notice the path the file is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found