-
-
Save amirhfarzaneh/96bfc5ea66fc44757fa4a6a869bc3db9 to your computer and use it in GitHub Desktop.
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))) |
May I ask how to resize crops after NRandomCrop?
You can use a transforms.Resize(size) when stacking them after transforms.Normalize() in line 22.
'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'>.
'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"
May I ask how to resize crops after NRandomCrop?