那些年踩过的深度学习坑一 维度问题

报错: ValueError: axes don't match array

ValueError:轴与数组不匹配 问题,下面是报错:

报错行代码经常为:image = np.transpose(image, (2, 0, 1))

这行代码的作用是调换整图像的shape顺序

[width, height, channels]

(2, 0, 1)即为结果的顺序

[channels, height, width]

  0%|          | 0/1 [00:00<?, ?it/s]Epoch: 0
  0%|          | 0/1 [00:00<?, ?it/s]
Traceback (most recent call last):
  File "D:/cver/Others/Auto_Classification/Auto_Classification/train.py", line 174, in <module>
    run(images_list, val_list)
  File "D:/cver/Others/Auto_Classification/Auto_Classification/train.py", line 147, in run
    train_one(train_dataloader, model, optimizer, loss_fn, None)
  File "D:/cver/Others/Auto_Classification/Auto_Classification/train.py", line 76, in train_one
    for bi, data in enumerate(tk):
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\tqdm\std.py", line 1180, in __iter__
    for obj in iterable:
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py", line 521, in __next__
    data = self._next_data()
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\dataloader.py", line 561, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\_utils\fetch.py", line 49, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\torch\utils\data\_utils\fetch.py", line 49, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "D:/cver/Others/Auto_Classification/Auto_Classification/train.py", line 46, in __getitem__
    "image": self.normalize(image),
  File "D:/cver/Others/Auto_Classification/Auto_Classification/train.py", line 26, in normalize
    image = np.transpose(image, (2, 0, 1)) # [3,224,224]
  File "<__array_function__ internals>", line 6, in transpose
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\numpy\core\fromnumeric.py", line 660, in transpose
    return _wrapfunc(a, 'transpose', axes)
  File "D:\Users\11145999\AppData\Roaming\Python\Python37\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
ValueError: axes don't match array

但此时发生了一些问题,如果图像混杂进了灰度图等图片,那么这行代码就会报错(如上)

解决方法

from skimage.color import gray2rgb
image = Image.open(image_name)
if image.mode == 'L':      # 判断是否为灰度图
    image = gray2rgb(image)  # 如果是,就转称彩色 (通道数变成三)

那些年踩过的深度学习坑一 维度问题

仔细看可以发发现,opencv 的坑,所以大家在一步步排查问题的时候,比如 当一个训练任务你原本能跑,换了数据后不能跑了,有可能就是混入了灰度图,你第一次看到ValueError: axes don't match array ,百度会说是维度问题,然后大家根据正常debug,用opencv读的图片,发现所有图片都是三通道,没问题,不过实际是opencv 的一个玩笑,大家看我上面的图片就知道了。本期opencv 坑 就到这,下期再见。

上一篇:Flink学习


下一篇:ajax获取键盘提交