From 911a892207489a9ddc7a59bb06576d4021fda62c Mon Sep 17 00:00:00 2001 From: Christopher Holzweber Date: Sun, 10 Dec 2023 15:00:05 +0100 Subject: [PATCH 1/4] Fixed shape error, allowing arbitrary image sizes. Replaced integer parsing by floor operation --- src/anomalib/models/efficient_ad/torch_model.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/anomalib/models/efficient_ad/torch_model.py b/src/anomalib/models/efficient_ad/torch_model.py index 5402b1bb52..0c51f56d6f 100644 --- a/src/anomalib/models/efficient_ad/torch_model.py +++ b/src/anomalib/models/efficient_ad/torch_model.py @@ -148,8 +148,8 @@ def __init__(self, out_channels, padding, img_size, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.img_size = img_size self.last_upsample = ( - int(img_size[0] / 4) if padding else int(img_size[0] / 4) - 8, - int(img_size[1] / 4) if padding else int(img_size[1] / 4) - 8, + (img_size[0] + 3) // 4 if padding else (img_size[0] + 3) // 4 - 8, + (img_size[1] + 3) // 4 if padding else (img_size[1] + 3) // 4 - 8, ) self.deconv1 = nn.Conv2d(64, 64, kernel_size=4, stride=1, padding=2) self.deconv2 = nn.Conv2d(64, 64, kernel_size=4, stride=1, padding=2) @@ -167,22 +167,22 @@ def __init__(self, out_channels, padding, img_size, *args, **kwargs) -> None: self.dropout6 = nn.Dropout(p=0.2) def forward(self, x): - x = F.interpolate(x, size=(int(self.img_size[0] / 64) - 1, int(self.img_size[1] / 64) - 1), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 64 - 1, self.img_size[1] // 64 - 1), mode="bilinear") x = F.relu(self.deconv1(x)) x = self.dropout1(x) - x = F.interpolate(x, size=(int(self.img_size[0] / 32), int(self.img_size[1] / 32)), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 32, self.img_size[1] // 32), mode="bilinear") x = F.relu(self.deconv2(x)) x = self.dropout2(x) - x = F.interpolate(x, size=(int(self.img_size[0] / 16) - 1, int(self.img_size[1] / 16) - 1), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 16 - 1, self.img_size[1] // 16 - 1), mode="bilinear") x = F.relu(self.deconv3(x)) x = self.dropout3(x) - x = F.interpolate(x, size=(int(self.img_size[0] / 8), int(self.img_size[1] / 8)), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 8, self.img_size[1] // 8), mode="bilinear") x = F.relu(self.deconv4(x)) x = self.dropout4(x) - x = F.interpolate(x, size=(int(self.img_size[0] / 4) - 1, int(self.img_size[1] / 4) - 1), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 4 - 1, self.img_size[1] // 4 - 1), mode="bilinear") x = F.relu(self.deconv5(x)) x = self.dropout5(x) - x = F.interpolate(x, size=(int(self.img_size[0] / 2) - 1, int(self.img_size[1] / 2) - 1), mode="bilinear") + x = F.interpolate(x, size=(self.img_size[0] // 2 - 1, self.img_size[1] // 2 - 1), mode="bilinear") x = F.relu(self.deconv6(x)) x = self.dropout6(x) x = F.interpolate(x, size=self.last_upsample, mode="bilinear") From 7d5d1a2bca7f3a615255bc70a1933b9adb50424c Mon Sep 17 00:00:00 2001 From: Christopher Holzweber Date: Sun, 10 Dec 2023 17:48:18 +0100 Subject: [PATCH 2/4] Replaced calculation by ceil operation. Solution of shape error is to round up and not down for the last upsample layer --- src/anomalib/models/efficient_ad/torch_model.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/anomalib/models/efficient_ad/torch_model.py b/src/anomalib/models/efficient_ad/torch_model.py index 0c51f56d6f..313fa1d848 100644 --- a/src/anomalib/models/efficient_ad/torch_model.py +++ b/src/anomalib/models/efficient_ad/torch_model.py @@ -7,6 +7,7 @@ import logging import random +import math from enum import Enum import torch @@ -148,8 +149,8 @@ def __init__(self, out_channels, padding, img_size, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.img_size = img_size self.last_upsample = ( - (img_size[0] + 3) // 4 if padding else (img_size[0] + 3) // 4 - 8, - (img_size[1] + 3) // 4 if padding else (img_size[1] + 3) // 4 - 8, + math.ceil(img_size[0] / 4) if padding else math.ceil(img_size[0] / 4) - 8, + math.ceil(img_size[1] / 4) if padding else math.ceil(img_size[1] / 4) - 8, ) self.deconv1 = nn.Conv2d(64, 64, kernel_size=4, stride=1, padding=2) self.deconv2 = nn.Conv2d(64, 64, kernel_size=4, stride=1, padding=2) From f56680b0aedff5097e483fb71dd3dc79da3383f3 Mon Sep 17 00:00:00 2001 From: Christopher Holzweber Date: Fri, 12 Jan 2024 14:45:18 +0100 Subject: [PATCH 3/4] Add comment for ceil oepration --- src/anomalib/models/efficient_ad/torch_model.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/anomalib/models/efficient_ad/torch_model.py b/src/anomalib/models/efficient_ad/torch_model.py index 313fa1d848..902468a419 100644 --- a/src/anomalib/models/efficient_ad/torch_model.py +++ b/src/anomalib/models/efficient_ad/torch_model.py @@ -148,6 +148,7 @@ class Decoder(nn.Module): def __init__(self, out_channels, padding, img_size, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.img_size = img_size + # use ceil to match output shape of PDN self.last_upsample = ( math.ceil(img_size[0] / 4) if padding else math.ceil(img_size[0] / 4) - 8, math.ceil(img_size[1] / 4) if padding else math.ceil(img_size[1] / 4) - 8, From bc40854472414b7c716e43127b94bdf91b116f35 Mon Sep 17 00:00:00 2001 From: Christopher Holzweber Date: Sat, 13 Jan 2024 11:45:09 +0100 Subject: [PATCH 4/4] Formatting with pre-commit hook --- src/anomalib/models/efficient_ad/torch_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/anomalib/models/efficient_ad/torch_model.py b/src/anomalib/models/efficient_ad/torch_model.py index 902468a419..278add5a8a 100644 --- a/src/anomalib/models/efficient_ad/torch_model.py +++ b/src/anomalib/models/efficient_ad/torch_model.py @@ -6,8 +6,8 @@ from __future__ import annotations import logging -import random import math +import random from enum import Enum import torch @@ -148,7 +148,7 @@ class Decoder(nn.Module): def __init__(self, out_channels, padding, img_size, *args, **kwargs) -> None: super().__init__(*args, **kwargs) self.img_size = img_size - # use ceil to match output shape of PDN + # use ceil to match output shape of PDN self.last_upsample = ( math.ceil(img_size[0] / 4) if padding else math.ceil(img_size[0] / 4) - 8, math.ceil(img_size[1] / 4) if padding else math.ceil(img_size[1] / 4) - 8,