akhaliq HF staff commited on
Commit
6b0f689
1 Parent(s): a2c4cfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -1
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
- [utils.prepare_input_from_uri(img)]
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)