Created
July 12, 2018 19:44
-
-
Save amirhfarzaneh/96bfc5ea66fc44757fa4a6a869bc3db9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torchvision.datasets as datasets | |
import torchvision.transforms as transforms | |
import torch | |
import torchvision | |
import matplotlib.pyplot as plt | |
import numpy as np | |
from custom_transforms import NRandomCrop | |
def imshow(img): | |
img = img / 2 + 0.5 | |
npimg = img.numpy() | |
plt.imshow(np.transpose(npimg, (1, 2, 0))) | |
plt.show() | |
mean, sd = (0.5, 0.5, 0.5), (0.5, 0.5, 0.5) | |
transform = transforms.Compose( | |
[NRandomCrop(size=32, n=5, padding=4), | |
transforms.Lambda( | |
lambda crops: torch.stack([transforms.Normalize(mean, sd)(transforms.ToTensor()(crop)) for crop in crops])), | |
] | |
) | |
train_data = datasets.CIFAR10(root='./data', | |
train=True, | |
download=True, | |
transform=transform) | |
train_loader = torch.utils.data.DataLoader(train_data, | |
batch_size=1, | |
shuffle=True, | |
num_workers=4) | |
classes = ('plane', 'car', 'bird', 'cat', | |
'deer', 'dog', 'frog', 'horse', 'ship', 'truck') | |
dataiter = iter(train_loader) | |
images, labels = dataiter.next() | |
# show images | |
imshow(torchvision.utils.make_grid(images.squeeze(0))) | |
# print labels | |
print(' '.join('%5s' % classes[labels[j]] for j in range(0))) |
'trainval': transforms.Compose([
NRandomCrop(size=args.crop_size, n=args.num_crop),
transforms.Lambda(
lambda crops: torch.stack([transforms.ToPILImage()
(transforms.Resize((args.scale_size, args.scale_size)))
(transforms.ToTensor())
(transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(crop))
for crop in crops]
)
),
]),
Resize
is done first, then ToTensor
and then Normalize
:
transform = transforms.Compose(
[NRandomCrop(size=32, n=5, padding=4),
transforms.Lambda(
lambda crops: torch.stack([transforms.Normalize(mean, sd)(transforms.ToTensor()(transforms.Resize(224, 224)(crop))) for crop in crops])),
]
)
I get a 5-dimensional tensor but expected a 4-dimensional tensor. May be I need cat n crops to one tensor?
I get a 5-dimensional tensor but expected a 4-dimensional tensor. May be I need cat n crops to one tensor?
You can do something this in you training code. It only works with batch_size=1
.
for batch in loader:
images = batch['images']
if len(images.shape) == 5:
images = torch.squeeze(images)
assert len(images.shape)<5, "Please use batch size 1"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
'trainval': transforms.Compose([ NRandomCrop(size=args.crop_size, n=args.num_crop), transforms.Lambda( lambda crops: torch.stack([transforms.ToPILImage() (transforms.Resize((args.scale_size, args.scale_size))) (transforms.ToTensor()) (transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(crop)) for crop in crops] ) ), ]),
This is my code but I donot know how to solve this error:
TypeError: pic should be Tensor or ndarray. Got <class 'torchvision.transforms.transforms.Resize'>.