mlverse / torchvision

R interface to torchvision
https://torchvision.mlverse.org
Other
62 stars 15 forks source link

transform_center_crop() returning zero height output #45

Closed Athospd closed 4 months ago

Athospd commented 3 years ago

I don't know if I'm using this function as expected, but I think zero height is not what it suposed to return in this situation.

library(torch)
library(torchvision)
tf <- tempfile(fileext = ".jpg")
download.file("https://github.com/curso-r/assets/raw/master/007b5a16db9d9ff9d7ad39982703e429.jpg", destfile = tf)
img <-  # dim (3, 212, 374)
    img <- base_loader(tf) %>%  # dim (3, 212, 374)
    transform_to_tensor() %>%  # dim (3, 212, 374)
    transform_center_crop(size = c(224, 224)) # dim (3, 138, 0)
dim(img)
#> [1]   3 138   0

Created on 2021-03-07 by the reprex package (v0.3.0)

skeydan commented 3 years ago

Hi, this is also affected by the thing corrected in https://github.com/mlverse/torchvision/pull/46, but to completely fix transform_center_crop there is something else to be done:

In https://pytorch.org/vision/stable/_modules/torchvision/transforms/functional.html#center_crop, there is a paragraph that is missing in the R function, which deals with what should happen if the image is smaller than the desired size:

  if crop_width > image_width or crop_height > image_height:
        padding_ltrb = [
            (crop_width - image_width) // 2 if crop_width > image_width else 0,
            (crop_height - image_height) // 2 if crop_height > image_height else 0,
            (crop_width - image_width + 1) // 2 if crop_width > image_width else 0,
            (crop_height - image_height + 1) // 2 if crop_height > image_height else 0,
        ]
        img = pad(img, padding_ltrb, fill=0)  # PIL uses fill value 0
        image_width, image_height = _get_image_size(img)
        if crop_width == image_width and crop_height == image_height:
            return img

I can add that to #44 later today.

skeydan commented 3 years ago

should be fixed in that same PR now (https://github.com/mlverse/torchvision/pull/46/commits/6f7faaf3feb20c8bdcec917a72c60867ef18ce4c)

Athospd commented 3 years ago

Just to report that I installed from bug/img_size branch and it worked perfectly on my code. Thank you!

skeydan commented 3 years ago

Thanks for reporting! I'm going ahead and merge, then.