I using data generator as ORN as following in main.py.
#train_dataset = datasets.MNIST(root=args.dataset_dir, train=True,
# download=True, transform=train_transform)
#test_dataset = datasets.MNIST(root=args.dataset_dir, train=False,
# download=True,transform=test_transform)
#
#train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size,
# num_workers=args.workers, pin_memory=True, shuffle=True)
#test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=args.batch_size,
# num_workers=args.workers, pin_memory=True, shuffle=True)
import math
import numbers
import random
from PIL import Image, ImageOps
# custom transform
class RandomRotate(object):
"""Rotate the given PIL.Image counter clockwise around its centre by a random degree
(drawn uniformly) within angle_range. angle_range is a tuple (angle_min, angle_max).
Empty region will be padded with color specified in fill."""
def __init__(self, angle_range=(-180,180), fill='black'):
assert isinstance(angle_range, tuple) and len(angle_range) == 2 and angle_range[0] <= angle_range[1]
assert isinstance(fill, numbers.Number) or isinstance(fill, str) or isinstance(fill, tuple)
self.angle_range = angle_range
self.fill = fill
def __call__(self, img):
angle_min, angle_max = self.angle_range
angle = angle_min + random.random() * (angle_max - angle_min)
theta = math.radians(angle)
w, h = img.size
diameter = math.sqrt(w * w + h * h)
theta_0 = math.atan(float(h) / w)
w_new = diameter * max(abs(math.cos(theta-theta_0)), abs(math.cos(theta+theta_0)))
h_new = diameter * max(abs(math.sin(theta-theta_0)), abs(math.sin(theta+theta_0)))
pad = math.ceil(max(w_new - w, h_new - h) / 2)
img = ImageOps.expand(img, border=int(pad), fill=self.fill)
img = img.rotate(angle, resample=Image.BICUBIC)
return img.crop((pad, pad, w + pad, h + pad))
train_loader = torch.utils.data.DataLoader(
datasets.MNIST(root=args.dataset_dir, train=True, download=True,
transform=transforms.Compose([
transforms.Scale(32),
RandomRotate((-180, 180)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])), batch_size=args.batch_size,
num_workers=args.workers, pin_memory=True, shuffle=True)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST(root=args.dataset_dir, train=False, transform=transforms.Compose([
transforms.Scale(32),
RandomRotate((-180, 180)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])), batch_size=args.batch_size,
num_workers=args.workers, pin_memory=True, shuffle=True)
The original data size is 28x28.
The original size after x = self.model(x) is 128x320x1x1.
After x = x.view(-1, 80*2, self.channel) is 128x80x4
The mnist-rot data is padding to 32x32.
The size after x = self.model(x) is 128x320x2x2.
After x = x.view(-1, 80*2, self.channel) is 512x80x4 which change the batch size!!
Can you tell me how to fix it or just simply change the padding number in net_factory?
Thank you!