0
$\begingroup$

This will probably be a basic question since I am starting with computer vision. I am trying to use resnet18 from pytorch and work with CIFAR-100 dataset. Single image has size 3x32x32 and the model cannot forward this throwing error. It can process arrays of size 224, 128, 64. How should I structure my images to process them with resnet18 specifically (not other arch).

$\endgroup$

1 Answer 1

0
$\begingroup$

PyTorch offers a range of transformations and you can simply apply a transformation to resize your images when loading your data. For example by using these transforms (using CIFAR10 as an example here which comes with 3x32x32 images too):

# transform for train data
transform_train = transforms.Compose([
    transforms.RandomResizedCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
    ])

# transform for test data
transform_test = transforms.Compose([
    transforms.Resize(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
    ])

# load data and apply transforms
train_data = datasets.CIFAR10('data', train=True,
                              download=True, transform=transform_train)
test_data = datasets.CIFAR10('data', train=False,
                             download=True, transform=transform_test)

Both, transforms.RandomResizedCrop(224) and transforms.Resize(224) give the desired size. (the random cropping is applied as data augmentation to further diversify training data) You will find more details on available transforms in the PyTorch documentation.

As a side note: the size requirement is the same for all pre-trained models in PyTorch - not just Resnet18:

All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be at least 224. The images have to be loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.