PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.1k stars 5.55k forks source link

Normalize()出错 #47124

Closed zjliu-cs closed 1 year ago

zjliu-cs commented 1 year ago

请提出你的问题 Please ask your question

错误信息:

The loss value printed in the log is the current step, and the metric is the average value of previous steps.
Epoch 1/10000
fail to perform transform [<paddle.vision.transforms.transforms.Normalize object at 0x7f757ebbb1d0>] with error: operands could not be broadcast together with shapes (1444,1444,3) (3,1,1)  and stack:
Traceback (most recent call last):
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/transforms.py", line 113, in __call__
    data = f(data)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/transforms.py", line 269, in __call__
    outputs.append(apply_func(inputs[i]))
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/transforms.py", line 715, in _apply_image
    self.to_rgb)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional.py", line 679, in normalize
    return F_cv2.normalize(img, mean, std, data_format, to_rgb)
  File "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/vision/transforms/functional_cv2.py", line 565, in normalize
    img = (img - mean) / std
ValueError: operands could not be broadcast together with shapes (1444,1444,3) (3,1,1) 
from MyDataset import MyDataset
from Net import Net
import paddle
import os
import random
import paddle.vision.transforms as T
from paddle.vision.transforms import Resize, RandomHorizontalFlip, RandomVerticalFlip,\
         ColorJitter, RandomRotation, Normalize

IMAGE_SIZE = (1444, 1444)

def write_to_file():
    path = './PaddleSeg/dataset/'
    img_path = path + 'Train/fundus_image/'
    label_path = path + 'Train/Disc_Masks/'
    img_list = os.listdir(img_path)
    random.shuffle(img_list)
    with open(path + 'train.txt', 'w') as tf:
        with open(path + 'val.txt', 'w') as vf:
            for index, img in enumerate(img_list):
                if index % 10 != 0:
                    tf.write(img_path + img + ' ' + label_path + img.replace('.jpg', '.bmp') + '\n')
                else:
                    vf.write(img_path + img + ' ' + label_path + img.replace('.jpg', '.bmp') + '\n')

transforms = T.Compose([
                    T.Resize(size=[1444, 1444]), 
                    T.RandomRotation(30),
                    T.RandomHorizontalFlip(0.3),
                    T.RandomVerticalFlip(0.3),
                    T.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5),
                    T.Normalize()])

train_dataset = MyDataset(mode='train', train_path='./PaddleSeg/dataset/train.txt', transforms=transforms)
val_dataset = MyDataset(mode='val', val_path='./PaddleSeg/dataset/val.txt', transforms=transforms)
model = paddle.Model(Net(2))
optimizer = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters(), weight_decay=4.0e-5)

model.prepare(optimizer=optimizer, loss=paddle.nn.CrossEntropyLoss(axis=1))
model.fit(train_data=train_dataset, eval_data=val_dataset, batch_size=4, epochs=10000, 
        save_dir='./output/unet1')
from paddle.io import Dataset
from PIL import Image
from paddle.vision.transforms import transforms as T

class MyDataset(Dataset):
    # 初始化
    def __init__(self, mode, train_path=None, val_path=None, test_path=None, separator=' ', 
                img_color_mode='RGB', label_color_mode='L', transforms=None):
        super().__init__()
        self._mode = mode
        self._train_path = train_path
        self._val_path = val_path
        self._test_path = test_path
        self._img_color_mode = img_color_mode
        self._label_color_mode = label_color_mode
        self._separator = separator
        self._transforms = transforms

        self._path = ''
        self._images = []
        self._label_images = []

        # 断言语句,判断输入参数是否正确
        assert self._mode in ['train', 'val', 'test'], \
            "mode should be 'train' or 'val' or 'test', but got {}".format(self._mode)
        if __debug__:
            if self._mode == 'train':
                if self._train_path is None:
                    print('train_path must have value if mode is train')
                    raise AssertionError
                else:
                    self._path = self._train_path
            elif self._mode == 'val':
                if self._val_path is None:
                    print('val_path must have value if mode is val')
                else:
                    self._path = self._val_path
            else:
                if self._test_path is None:
                    print('test_path must have value if mode is test')
                else:
                    self._path = self._test_path
        # 初始化文件
        print(self._path)
        with open(self._path, 'r') as f:
            if self._mode == 'test':
                for l in f.readlines():
                    self._images.append(l)
            else:
                for l in f.readlines():
                    image, label = l.strip().split(separator)
                    self._images.append(image)
                    self._label_images.append(label)

    # 加载图片
    # def _load_img(self, path, img_color_mode='rgb', label_color_mode='L', transforms=None):

    def __getitem__(self, index):
        img_path = self._images[index]
        image = Image.open(img_path)
        if self._mode in ['train', 'val']:
            label = self._label_images[index]
            if self._transforms is not None:
                image = self._transforms(image)
                label = self._transforms(label)
            return image, label
        if self._transforms is not None:
            image = self._transforms(image)
        return image

    def __len__(self):
        return len(self._images)
paddle-bot[bot] commented 1 year ago

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

zjliu-cs commented 1 year ago

按源码看似乎是由于img = (img - mean) / std这一步出错,mean和std与img的尺寸不符合,但是这不是已经设置好的吗?

SigureMo commented 1 year ago

加个 Transpose 试试

 transforms = T.Compose([
                     T.Resize(size=[1444, 1444]), 
                     T.RandomRotation(30),
                     T.RandomHorizontalFlip(0.3),
                     T.RandomVerticalFlip(0.3),
                     T.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5),
+                    T.Transpose(),
                     T.Normalize()])
zjliu-cs commented 1 year ago

谢谢 解决了