AttributeError:'NoneType'对象没有属性'astype' [英] AttributeError: 'NoneType' object has no attribute 'astype'

查看:238
本文介绍了AttributeError:'NoneType'对象没有属性'astype'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我重现与ESRGAN相关的程序时遇到以下问题.libpng错误:读取错误

I encountered the following problem when I reproduce the ESRGAN related program. libpng error: Read Error

Traceback (most recent call last):
  File "/sda/ZTL/B/codes/train.py", line 173, in <module>
    main()
  File "/sda/ZTL/B/codes/train.py", line 97, in main
    for _, train_data in enumerate(train_loader):
  File "/root/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 637, in __next__
    return self._process_next_batch(batch)
  File "/root/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
    raise batch.exc_type(batch.exc_msg)
AttributeError: Traceback (most recent call last):
  File "/root/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/root/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in <listcomp>
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/sda/ZTL/B/data/LRHR_dataset.py", line 51, in __getitem__
    img_HR = util.read_img(self.HR_env, HR_path)
  File "/sda/ZTL/B/data/util.py", line 79, in read_img
    img = img.astype(np.float32) / 255.
AttributeError: 'NoneType' object has no attribute 'astype'

我试图找到发生错误的行的代码.

I tried to find the code for the line where the error occurred.

File "/sda/ZTL/B/data/util.py", line 79, in read_img
def read_img(env, path):
    # read image by cv2 or from lmdb
    # return: Numpy float32, HWC, BGR, [0,1]
    if env is None:  # img
        img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
    else:
        img = _read_lmdb_img(env, path)
    img = img.astype(np.float32) / 255.
    if img.ndim == 2:
        img = np.expand_dims(img, axis=2)
    # some images have 4 channels
    if img.shape[2] > 3:
        img = img[:, :, :3]
    return img

File "/sda/ZTL/B/data/LRHR_dataset.py", line 51, in __getitem__
 def __getitem__(self, index):
        HR_path, LR_path = None, None
        scale = self.opt['scale']
        HR_size = self.opt['HR_size']

        # get HR image
        HR_path = self.paths_HR[index]
        img_HR = util.read_img(self.HR_env, HR_path)
        # modcrop in the validation / test phase
        if self.opt['phase'] != 'train':
            img_HR = util.modcrop(img_HR, scale)
        # change color space if necessary
        if self.opt['color']:
            img_HR = util.channel_convert(img_HR.shape[2], self.opt['color'], [img_HR])[0]

        # get LR image
        if self.paths_LR:
            LR_path = self.paths_LR[index]
            img_LR = util.read_img(self.LR_env, LR_path)
        else:  # down-sampling on-the-fly
            # randomly scale during training
            if self.opt['phase'] == 'train':
                random_scale = random.choice(self.random_scale_list)
                H_s, W_s, _ = img_HR.shape

                def _mod(n, random_scale, scale, thres):
                    rlt = int(n * random_scale)
                    rlt = (rlt // scale) * scale
                    return thres if rlt < thres else rlt

                H_s = _mod(H_s, random_scale, scale, HR_size)
                W_s = _mod(W_s, random_scale, scale, HR_size)
                img_HR = cv2.resize(np.copy(img_HR), (W_s, H_s), interpolation=cv2.INTER_LINEAR)
                # force to 3 channels
                if img_HR.ndim == 2:
                    img_HR = cv2.cvtColor(img_HR, cv2.COLOR_GRAY2BGR)

            H, W, _ = img_HR.shape
            # using matlab imresize
            img_LR = util.imresize_np(img_HR, 1 / scale, True)
            if img_LR.ndim == 2:
                img_LR = np.expand_dims(img_LR, axis=2)

        if self.opt['phase'] == 'train':
            # if the image size is too small
            H, W, _ = img_HR.shape
            if H < HR_size or W < HR_size:
                img_HR = cv2.resize(
                    np.copy(img_HR), (HR_size, HR_size), interpolation=cv2.INTER_LINEAR)
                # using matlab imresize
                img_LR = util.imresize_np(img_HR, 1 / scale, True)
                if img_LR.ndim == 2:
                    img_LR = np.expand_dims(img_LR, axis=2)
                    print(img_LR)

            H, W, C = img_LR.shape
            LR_size = HR_size // scale

            # randomly crop
            rnd_h = random.randint(0, max(0, H - LR_size))
            rnd_w = random.randint(0, max(0, W - LR_size))
            img_LR = img_LR[rnd_h:rnd_h + LR_size, rnd_w:rnd_w + LR_size, :]
            rnd_h_HR, rnd_w_HR = int(rnd_h * scale), int(rnd_w * scale)
            img_HR = img_HR[rnd_h_HR:rnd_h_HR + HR_size, rnd_w_HR:rnd_w_HR + HR_size, :]

            # augmentation - flip, rotate
            img_LR, img_HR = util.augment([img_LR, img_HR], self.opt['use_flip'], \
                self.opt['use_rot'])

        # change color space if necessary
        if self.opt['color']:
            img_LR = util.channel_convert(C, self.opt['color'], [img_LR])[0] # TODO during val no definetion

        # BGR to RGB, HWC to CHW, numpy to tensor
        if img_HR.shape[2] == 3:
            img_HR = img_HR[:, :, [2, 1, 0]]
            img_LR = img_LR[:, :, [2, 1, 0]]
        img_HR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_HR, (2, 0, 1)))).float()
        img_LR = torch.from_numpy(np.ascontiguousarray(np.transpose(img_LR, (2, 0, 1)))).float()

        if LR_path is None:
            LR_path = HR_path
        return {'LR': img_LR, 'HR': img_HR, 'LR_path': LR_path, 'HR_path': HR_path}

我觉得我读的图片错了.其中之一读入并且为None.我不知道该如何处理.我正在使用NVIDIA Tesla P100GPU运行该程序.

I feel that the picture I read in has gone wrong. One of them reads in and is None. I don't know how to deal with this problem. I am running this program with the NVIDIA Tesla P100GPU.

19-08-30 06:12:28.193-信息:l_g_pix:3.9939e-03 l_g_fea:2.3352e + 00 l_g_gan:1.0448e-01 l_d_real:1.5721e-06 l_d_fake:1.6599e-05 D_real:7.0139e+00 D_fake:-1.3881e + 0119-08-30 06:14:34.038-信息:l_g_pix:2.9632e-03 l_g_fea:1.7633e + 00 l_g_gan:7.9122e-02 l_d_real:5.6028e-06 l_d_fake:4.7490e-05 D_real:7.1848e + 00 D_fake:-8.6396e + 0019-08-30 06:16:38.986-信息:l_g_pix:3.6181e-03 l_g_fea:2.2983e + 00 l_g_gan:3.5791e-02 l_d_real:3.3302e-03 l_d_fake:2.6311e-03 D_real:1.6663e + 01 D_fake:9.5084e + 0019-08-30 06:18:42.645-信息:l_g_pix:3.9908e-03 l_g_fea:2.1037e + 00 l_g_gan:5.0026e-02 l_d_real:2.2486e-04 l_d_fake:7.5957e-04 D_real:1.0516e + 00 D_fake:-8.9531e + 00libpng错误:读取错误追溯(最近一次通话):

19-08-30 06:12:28.193 - INFO: l_g_pix: 3.9939e-03 l_g_fea: 2.3352e+00 l_g_gan: 1.0448e-01 l_d_real: 1.5721e-06 l_d_fake: 1.6599e-05 D_real: 7.0139e+00 D_fake: -1.3881e+01 19-08-30 06:14:34.038 - INFO: l_g_pix: 2.9632e-03 l_g_fea: 1.7633e+00 l_g_gan: 7.9122e-02 l_d_real: 5.6028e-06 l_d_fake: 4.7490e-05 D_real: 7.1848e+00 D_fake: -8.6396e+00 19-08-30 06:16:38.986 - INFO: l_g_pix: 3.6181e-03 l_g_fea: 2.2983e+00 l_g_gan: 3.5791e-02 l_d_real: 3.3302e-03 l_d_fake: 2.6311e-03 D_real: 1.6663e+01 D_fake: 9.5084e+00 19-08-30 06:18:42.645 - INFO: l_g_pix: 3.9908e-03 l_g_fea: 2.1037e+00 l_g_gan: 5.0026e-02 l_d_real: 2.2486e-04 l_d_fake: 7.5957e-04 D_real: 1.0516e+00 D_fake: -8.9531e+00 libpng error: Read Error Traceback (most recent call last):

………………

推荐答案

1)检查图像路径是否正确.

1) check the image path is correct.

2)确保将图像读取为numpy ndarray,例如(使用matplotlib,cv2),使用PIL读取其他格式的图像,因此无法应用numpy数组操作.

2) make sure that image is read as numpy ndarray e.g(using matplotlib, cv2), using PIL it reads image in another format so it becomes impossible to apply numpy array operations.

这篇关于AttributeError:'NoneType'对象没有属性'astype'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆