Paddle 【论文复现】daload不支持动态修改图片shape

70gysomp  于 2022-04-21  发布在  Java
关注(0)|答案(2)|浏览(165)
import numpy as np
import paddle
import cv2
from paddle.io import DataLoader
from paddle.io import Dataset
import torch.utils.data as data
import torch

class testdata1(data.Dataset):
    def __init__(self):
        super(testdata1,self).__init__()
        self.img_list = [1,2,3,4,5,6,7]
        self.img = [np.ones((45,45,3)),np.ones((60,60,3)),np.ones((60,60,3)),
                    np.ones((60,60,3)),np.ones((60,60,3)),np.ones((60,60,3)),np.ones((60,60,3))]
        self.size = 1200
        self.shapesize = [200,2400]
    def get_rand(self,):
        self.size = np.random.choice(self.shapesize,1)[0]
    def __len__(self):
        return len(self.img_list)
    def __getitem__(self, index):
        x = cv2.resize(self.img[index],(self.size,self.size))
        x = torch.Tensor(x).permute((2,0,1))
        return x

data_loader = testdata1()
dataload =data.DataLoader(data_loader,
                batch_size=2,
                shuffle=True,
                num_workers=0,
                drop_last=True)

for _,x in enumerate(dataload):
    data_loader.get_rand()
    print(x.shape)

class testdata(Dataset):
    def __init__(self):
        super(testdata,self).__init__()
        self.img_list = [1,2,3,4,5,6,7]
        self.img = [np.ones((45,45,3)),np.ones((60,60,3)),np.ones((60,60,3)),
                    np.ones((60,60,3)),np.ones((60,60,3)),np.ones((60,60,3)),np.ones((60,60,3))]
        self.size = 1200
        self.shapesize = [200,2400]
    def get_rand(self,):
        self.size = np.random.choice(self.shapesize,1)[0]
    def __len__(self):
        return len(self.img_list)
    def __getitem__(self, index):
        x = cv2.resize(self.img[index],(self.size,self.size))
        x = paddle.to_tensor(x).transpose((2,0,1))
        return x

data_loader = testdata()
dataload =DataLoader(data_loader,
                batch_size=2,
                shuffle=True,
                num_workers=0,
                drop_last=True)

for _,x in enumerate(dataload):
    data_loader.get_rand()
    print(x.shape)

这里提供了torch的数据加载和paddle2.1.2加载的简单示例,torch可以修改,但是paddle会报错,

but5z9lq

but5z9lq1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网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!

9rygscc1

9rygscc12#

不同shape的图像没法组batch,可以在组完batch之后做reshape操作,参考https://github.com/PaddlePaddle/PaddleDetection/blob/4c09bff65c5a2906b9059b328c1cf8fe31417c46/ppdet/data/transform/batch_operators.py#L100

相关问题