0

The neural net was trained with inputs that have been imported and transformed in the following way:

transform = transforms.Compose(
    [transforms.ToTensor()])


data_path = '/home/tim/Documents/trainset/'
traindataset = torchvision.datasets.ImageFolder(
    root=data_path,
    transform=transform
)
trainloader = torch.utils.data.DataLoader(
    traindataset,
    batch_size=3,
    num_workers=1,
    shuffle=True
)

I now have a single numpy array called img that represents an image in RGB with pixel values ranging from 0 to 255. how can i apply trainloader and all the transforms to turn this array into a single element batch to be put through my neural network?

stavro
  • 103
  • 1
  • 5
  • there should normally be a `transforms.Normalize()` after `transform.ToTensor()` assuming you are working with natural images. Anyway, transform expects PIL images, not numpy. First, check the answer in this [link](https://stackoverflow.com/questions/10965417/how-to-convert-numpy-array-to-pil-image-applying-matplotlib-colormap) and follow the steps but don't save your image, keep the output of `Image.fromarray` in a variable called `img_pil`. Then use `traindataset.transform(img_pil)` and the same transformations will be applied. – D_Serg Dec 13 '18 at 21:42
  • But how do i know the parameters to normalise it by? – stavro Dec 13 '18 at 21:44
  • that's a good answer, that solves most of my problem – stavro Dec 13 '18 at 21:45
  • it depends on your dataset which parameters to normalise it by. But usually for natural images it is quite consistent. I suggest you to use the [example](https://github.com/pytorch/examples/blob/master/imagenet/main.py#L194) provided for ImageNet from PyTorch. – D_Serg Dec 13 '18 at 21:50
  • for general images, is it the average green pixel value, the average red pixel value and the average blue pixel value and the standard deviation of red pixel values, ... etc? – stavro Dec 13 '18 at 22:31
  • yes, the order is RGB for PyTorch, just a heads up. By the way, let me know if this solved your problem all the way so that I can write this as an answer – D_Serg Dec 13 '18 at 23:57

0 Answers0