【问题标题】:Pytorch: Add information to images in image predictionPytorch:在图像预测中为图像添加信息
【发布时间】:2022-01-03 19:29:18
【问题描述】:

我想向我当前的数据集添加信息。目前,我在文件夹中有六帧序列。 DataLoader 读取所有 6 个并使用前 3 个来预测最后一个 1/2/3(取决于我告诉他多少)。这是 DataLoader 的函数。

class TrainFeeder(Dataset):
    def init(self, data_set):
        super(TrainFeeder, self).init()
        self.input_data = data_set
        #print(torch.cuda.current_device())
        if torch.cuda.current_device() ==0:
            print('There are total %d sequences in trainset' % len(self.input_data))

    def getitem(self, index):
        path = self.input_data[index]
        imgs_path = sorted(glob.glob(path + '/*.png'))
        imgs = []
        for img_path in imgs_path:
            img = cv2.imread(img_path)
            img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            img = cv2.resize(img, (256,448))
            img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC) #has been 0.5 for official data, new is fx = 2.63 and fy = 2.84
            img_tensor = ToTensor()(img).float()
            imgs.append(img_tensor)
        imgs = torch.stack(imgs, dim=0)
        return imgs

    def len(self):
        return len(self.input_data)

现在我想为这些图像添加一个值。它是一个布尔值,我存储在同一个文件夹中的 .json 列表中,就像六帧序列一样。但我不知道如何将 .json 中列表的值添加到张量中。我应该使用哪个维度?如果我改变输入的形状,系统会正常工作吗?

【问题讨论】:

    标签: image-processing pytorch tensor dataloader


    【解决方案1】:

    getitem 函数可以返回任何内容,因此您可以返回元组而不仅仅是图像:

    def __getitem__(self, index):
        path = ...
        # load your 6 images
        imgs = torch.stack( ... )
        # load your boolean metadata
        metadata = load_json_data( ... )
        # return them both
        return (imgs, metadata)
    

    您需要在返回之前将 metadata 设为张量,否则我希望 pytorch 会抱怨无法整理(即堆叠)它们以进行批量处理

    “系统能否正常工作”是一个只有您可以回答的问题,因为您没有提供 ML 模型的代码。我敢打赌:“不,但它不需要进行重大更改即可工作”。很可能您目前有一个类似

    的循环
    for imgs in dataloader:
        # do some training 
        output = model(imgs)
        ...
    

    你必须让它像

    for imgs, metadata in dataloader:
         # do some training
         output = model(imgs)
         ...
    

    【讨论】:

    • 感谢您的回答!是的,它看起来像这样。这样,我可以加载元数据并对其进行处理,但不能在模型中,可以吗?因为模型只需要 imgs 张量。我想将元数据添加到模型的输入张量中。更具体地说:我有植物的图像。元数据是下一帧中光线来自左侧还是右侧的信息。我希望,系统可以学习解释这些附加信息,根据光线预测植物生长
    • 看你能不能修改模型的代码,还是直接从网上下载?因为如果可以,那么你只需要修改它的forward函数;如果没有,那么您无能为力,该模型并非旨在使用这些额外的元数据,您可能希望自己重新实现它或在互联网上找到更适合您的用例的不同架构
    • 是的,这是我自己的模型。但还是。在转发功能中:我可以调整它以接受更多输入,但随后它会被更深地馈送到系统中。在某些时候,我必须将布尔值与图像“连接”起来。就像将元组转换为张量一样。但由于尺寸的原因,这不起作用。无论如何,谢谢你。我将尝试在图像底部画一个矩形,指示灯光,看看会发生什么。
    • 这意味着它更像是一个机器学习问题而不是编程问题。有许多不同的方法可以将标签合并到模型中,具有不同的用例和注意事项。你需要弄清楚什么是最适合你的目的。也许阅读一些关于条件 CNN 的论文,看看在最先进的研究中做了什么。一旦你确切地知道你想要什么,你可以在这里寻求帮助来实现它:)
    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 2012-06-16
    • 2021-10-03
    • 1970-01-01
    • 2018-07-30
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多