1

I'm trying to fine-tune Pytorch model using their own tutorial from their page. I try it on dataset StaVer from Kaggle: rtatman/stamp-verification-staver-dataset.

The only change to their code is the paths to the dataset. The error occured in the training model section:

# let's train it for 10 epochs
from torch.optim.lr_scheduler import StepLR

num_epochs = 20

for epoch in range(num_epochs):
    # train for one epoch, printing every 10 iterations
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=1)
    # update the learning rate
    lr_scheduler.step()
    # evaluate on the test dataset
    evaluate(model, data_loader_test, device=device)

.

The same error can be replicated just by running this piece of code:

model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
dataset = StaverDataset("", get_transform(train=True))
data_loader = torch.utils.data.DataLoader(
    dataset, batch_size=2, shuffle=False, num_workers=0, collate_fn=utils.collate_fn
)
a = iter(data_loader)
for i in range(len(a)):
  images, targets = next(a)
  images = list(image for image in images)
  targets = [{k: v for k, v in t.items()} for t in targets]
  output = model(images, targets) 

The wierd part is that it worked ok until image_382. And if I delete all previous images and run the model on last approx. 50 images it will work as well. Seems the problem is not related to the image but the number of images. The number of images run through the model().

This is the error:

scans/scans/stampDS-00380.png
scans/scans/stampDS-00381.png
scans/scans/stampDS-00382.png
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-40-6ccb0398c1aa> in <module>()
      4   images = list(image for image in images)
      5   targets = [{k: v for k, v in t.items()} for t in targets]
----> 6   output = model(images, targets)
      7   model.eval()

6 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in interpolate(input, size, scale_factor, mode, align_corners, recompute_scale_factor)
   3710         return torch._C._nn.upsample_nearest1d(input, output_size, scale_factors)
   3711     if input.dim() == 4 and mode == "nearest":
-> 3712         return torch._C._nn.upsample_nearest2d(input, output_size, scale_factors)
   3713     if input.dim() == 5 and mode == "nearest":
   3714         return torch._C._nn.upsample_nearest3d(input, output_size, scale_factors)

RuntimeError: Input and output sizes should be greater than 0, but got input (H: 1605, W: 2) output (H: 799, W: 0)

Does anyone know what is the error related to? Thank you in advance. Michael

  • 1
    What happens if you load `scans/scans/stampDS-00382.png` outside of this loop? Is it a different shape to the others? The fact that you've said it only happened when you changed datasets leads me to believe that there are some errors in your images/targets. – jhso Feb 22 '22 at 07:06
  • If i delete first 350 images, and run the model on last 50 images (350 - 400), it went without issues all 20 epochs. The image 382 is completely same as others, all have the same shape. I checked even the masks, normal rectengular mask.... Also, the Pytorch tutorial has "Scaler" in the model function. Therefore, I assume that size doesn't matter:) – Michael Mateju Feb 22 '22 at 09:30
  • Also, I tried to delete the image 382 but the code fails on next image in row - 383. – Michael Mateju Feb 22 '22 at 09:51
  • Perhaps the input size of some image, is too small. It could be a good idea to check the minimum input size of the model and to check the transform you use on the dataset. – aretor Feb 22 '22 at 13:40
  • As @jhso said you should try to verify the size of stampDS-00382.png out of the loop. – Hamzah Feb 22 '22 at 14:08
  • The problem is not in the 382 image. If I, for example, delete image 381 then image 382 will go without peoblem and the problem occurs on image 383. I'll check the size of images in the dataset. They all have the same size, but I'll check if there is difference between the Penn dataset. – Michael Mateju Feb 22 '22 at 14:11
  • I found out that the images from PennFudanPed dataset are all different sizes - largest 924x486 pixels, smallest 252x323 pixels. Whereas StaVer dataset contains images of different sizes where largest is 1632x2302 and smallest is 1605x2280. Could be the size the issue? I'll try to rescale the images to smaller size. – Michael Mateju Feb 22 '22 at 15:12
  • OK, so I reduced the size of the images. And it started working. But why? – Michael Mateju Feb 22 '22 at 19:55

2 Answers2

0

It seems the problem was in the size of the images. Reducing the size of the image by factor of 2 made it working. But frankly, I don't know why.

0

I encountered the same problem using pytorch's Mask RCNN model. It seemed the error occured while the model was trying to perform up/downscaling transformations on the input images. Since modifying the source image sizes wasn't an option in my case, I found a fix to this problem by setting the min&max sizes to my image size in the model kwargs (in my case all my images were 1025x1025).