Where to add the resize module?

username: rui_zhou
cooked: <p>where should I add the resize module in my code to make the image in the same size ? I have tried many times but it does not work at all.</p>
<p>from torchvision import transforms</p>
<p>from segmentation.data_loader.segmentation_dataset import SegmentationDataset<br>
from segmentation.data_loader.transform import Rescale, ToTensor<br>
from segmentation.trainer import Trainer<br>
from segmentation.predict import *<br>
from segmentation.models import all_models<br>
from util.logger import Logger</p>
<p>train_images = r’./plant/images/train’<br>
test_images = r’./plant/images/test’<br>
train_labled = r’./plant/labeled/train’<br>
test_labeled = r’./plant/labeled/test’</p>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:<br>
model_name = “unet_mobilenet_v2”<br>
device = ‘cuda’<br>
batch_size = 8<br>
n_classes = 256<br>
num_epochs = 10<br>
image_axis_minimum_size = 200<br>
pretrained = True<br>
fixed_feature = False</p>
<pre><code>logger = Logger(model_name=model_name, data_name='example')

### Loader
compose = transforms.Compose([
    Rescale(image_axis_minimum_size),
    ToTensor()
     ])

train_datasets = SegmentationDataset(train_images, train_labled, n_classes, compose)
train_loader = torch.utils.data.DataLoader(train_datasets, batch_size=batch_size, shuffle=True, drop_last=True)

test_datasets = SegmentationDataset(test_images, test_labeled, n_classes, compose)
test_loader = torch.utils.data.DataLoader(test_datasets, batch_size=batch_size, shuffle=True, drop_last=True)

### Model
model = all_models.model_from_name[model_name](n_classes, batch_size,
                        pretrained=pretrained,
                        fixed_feature=fixed_feature)
model.to(device)

###Load model
###please check the foloder: (.segmentation/test/runs/models)
#logger.load_model(model, 'epoch_15')

### Optimizers-
if pretrained and fixed_feature: #fine tunning
    params_to_update = model.parameters()
    print("Params to learn:")
    params_to_update = []
    for name, param in model.named_parameters():
        if param.requires_grad == True:
            params_to_update.append(param)
            print("\t", name)
    optimizer = torch.optim.Adadelta(params_to_update)
else:
    optimizer = torch.optim.Adadelta(model.parameters())

### Train
#scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
trainer = Trainer(model, optimizer, logger, num_epochs, train_loader, test_loader)
trainer.train()


#### Writing the predict result.
predict(model, r'./plant/input.png',
         r'./plant/output.png')
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/3/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c" title="64"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c_2_690x273.jpeg" alt="64" data-base62-sha1="uccyNGUIHat2JJbDAHUiqlA2UlS" width="690" height="273" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c_2_690x273.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/3/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c_2_1035x409.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/3/d3a2576c3c3af4d6be03c4dc2be3f9faf2145f3c.jpeg 2x" data-dominant-color="3D3D3D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">64</span><span class="informations">1296×514 66.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: pchandrasekaran
cooked: <p>Could you try passing the size value as a tuple to the Resize function. Resize behaves differently if your images have non-equal dimensions and you pass only a single value.</p>

username: rui_zhou
cooked: <p>can you write a example code snip? I can not realize that instantly…</p>

username: pchandrasekaran
cooked: <p>Sorry for the late reply. The error looks to be from the Dataloader, which in turn is coming from the Dataset due to incorrect dimensions. Try using the <code>compose</code> as below,</p>
<pre><code class="lang-auto">compose = transforms.Compose([
    Resize(image_axis_minimum_size, image_axis_minimum_size),
    ToTensor()
     ])
</code></pre>