pytorch-torchvision transforms


1.
import torch
import torchvision as vision

rc = vision.transforms.RandomCrop([224, 224])
toPIL = vision.transforms.ToPILImage()
toTensor = vision.transforms.ToTensor()

input = torch.randn(3, 256, 256)

out = toTensor(rc(toPIL(input)))
print(out.size())


normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])




2. 

# desired size of the output image
imsize = 512 if use_cuda else 128  # use small size if no gpu

loader = transforms.Compose([
    transforms.Scale(imsize),  # scale imported image
    transforms.ToTensor()])  # transform it into a torch tensor


def image_loader(image_name):
    image = Image.open(image_name)
    image = Variable(loader(image))
    # fake batch dimension required to fit network's input dimensions
    image = image.unsqueeze(0)
    return image


style_img = image_loader("images/picasso.jpg").type(dtype)
content_img = image_loader("images/dancing.jpg").type(dtype)














-------------------------------------------reference-----------------------------

1. http://pytorch.org/docs/torchvision/transforms.html

2. http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

3. http://pytorch.org/tutorials/advanced/neural_style_tutorial.html




--------------------------------------------reference--------------------------------------------
1. https://discuss.pytorch.org/t/random-crop-resize/3571


你可能感兴趣的:(PyTorch)