Pytorch can process its own image data into image types that can be trained

Posted on

In order to use your own image data, you need to create a new class following the pytorch data input, where the data format is numpy. Ndarray.

Save your picture to numpy. Ndarray, and then create a class


from torch.utils.data import Dataset
import numpy as np
 
 
class Dataset(Dataset):
  def __init__(self, path_img, path_target, transforms=None):
    self.train = path_img
    self.targets = path_target
    self.transforms = transforms
 
  def __len__(self):
    return len(self.train)
 
  def __getitem__(self, idx):
    img = self.train[idx]
    target = self.targets[idx]
 
    if self.transforms:
      img = self.transforms(img)
      target = self.transforms(target)
 
    return img, target

Use the same way as MNIST data


isbi = Dataset(imgs_train, imgs_mask_train,
            transforms=transform)
dataload=torch.utils.data.DataLoader(isbi,batch_size=4,shuffle=True)
for i, data in enumerate(dataload, 1):
  img,label=data
  print img.shape
  print img.shape
  print 10*'*'

The above pytorch to achieve their own image data processing can be trained image type is Xiaobian to share with you all the content, I hope to give you a reference, also hope you can support developer.

Leave a Reply

Your email address will not be published.