Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,23 @@ utils = torch.hub.load('NVIDIA/DeepLearningExamples:torchhub', 'nvidia_convnets_
|
|
| 9 |
efficientnet.eval().to(device)
|
| 10 |
|
| 11 |
def inference(img):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
batch = torch.cat(
|
| 13 |
-
[
|
| 14 |
).to(device)
|
| 15 |
with torch.no_grad():
|
| 16 |
output = torch.nn.functional.softmax(efficientnet(batch), dim=1)
|
|
|
|
| 9 |
efficientnet.eval().to(device)
|
| 10 |
|
| 11 |
def inference(img):
|
| 12 |
+
|
| 13 |
+
img_transforms = transforms.Compose(
|
| 14 |
+
[transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor()]
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
img = img_transforms(img)
|
| 18 |
+
with torch.no_grad():
|
| 19 |
+
# mean and std are not multiplied by 255 as they are in training script
|
| 20 |
+
# torch dataloader reads data into bytes whereas loading directly
|
| 21 |
+
# through PIL creates a tensor with floats in [0,1] range
|
| 22 |
+
mean = torch.tensor([0.485, 0.456, 0.406]).view(1, 3, 1, 1)
|
| 23 |
+
std = torch.tensor([0.229, 0.224, 0.225]).view(1, 3, 1, 1)
|
| 24 |
+
img = img.float()
|
| 25 |
+
img = img.unsqueeze(0).sub_(mean).div_(std)
|
| 26 |
+
|
| 27 |
batch = torch.cat(
|
| 28 |
+
[img]
|
| 29 |
).to(device)
|
| 30 |
with torch.no_grad():
|
| 31 |
output = torch.nn.functional.softmax(efficientnet(batch), dim=1)
|