danyalmalik commited on
Commit
5dfae46
1 Parent(s): e247352

switched to transfer learning

Browse files
Files changed (2) hide show
  1. app.py +10 -7
  2. net.py +0 -35
app.py CHANGED
@@ -1,27 +1,29 @@
1
  import gradio as gr
2
  import torch
3
- from torchvision import transforms
 
 
4
  import numpy as np
5
  import os
6
  import huggingface_hub
7
 
8
- from net import Net
9
-
10
  if torch.cuda.is_available():
11
  device = torch.device("cuda:0")
12
  else:
13
  device = torch.device("cpu")
14
 
15
 
16
- net = Net()
 
17
  net.to(device)
18
 
19
  HF_Token = os.environ['HF_Token']
20
 
21
  model = huggingface_hub.cached_download(huggingface_hub.hf_hub_url(
22
- 'danyalmalik/sceneryclassifier', '1655684183.7481008_Acc0.87_modelweights.pth'), use_auth_token=HF_Token)
23
 
24
  net.load_state_dict(torch.load(model, map_location=device))
 
25
 
26
  mean = np.array([0.5, 0.5, 0.5])
27
  std = np.array([0.25, 0.25, 0.25])
@@ -49,9 +51,10 @@ def predict(img):
49
  try:
50
  img = data_transforms(img)
51
  img = img.to(device)
 
52
 
53
  with torch.no_grad():
54
- output = net(img)
55
 
56
  pred = [output[0][i].item() for i in range(len(labels))]
57
 
@@ -62,5 +65,5 @@ def predict(img):
62
  return weightage
63
 
64
 
65
- gr.Interface(fn=predict, inputs=gr.Image(shape=(150, 150), type='pil'),
66
  outputs='label', title=title, examples=examples()).launch()
 
1
  import gradio as gr
2
  import torch
3
+ import torch.nn as nn
4
+ import torch.nn.functional as F
5
+ from torchvision import transforms, models
6
  import numpy as np
7
  import os
8
  import huggingface_hub
9
 
 
 
10
  if torch.cuda.is_available():
11
  device = torch.device("cuda:0")
12
  else:
13
  device = torch.device("cpu")
14
 
15
 
16
+ net = models.resnet18(pretrained=False)
17
+ net.fc = nn.Linear(net.fc.in_features, 6)
18
  net.to(device)
19
 
20
  HF_Token = os.environ['HF_Token']
21
 
22
  model = huggingface_hub.cached_download(huggingface_hub.hf_hub_url(
23
+ 'danyalmalik/sceneryclassifier', '1655988285.9725637_Acc0.88_modelweights.pth'), use_auth_token=HF_Token)
24
 
25
  net.load_state_dict(torch.load(model, map_location=device))
26
+ net.eval()
27
 
28
  mean = np.array([0.5, 0.5, 0.5])
29
  std = np.array([0.25, 0.25, 0.25])
 
51
  try:
52
  img = data_transforms(img)
53
  img = img.to(device)
54
+ img = img.unsqueeze(0)
55
 
56
  with torch.no_grad():
57
+ output = F.softmax(net(img), dim=1)
58
 
59
  pred = [output[0][i].item() for i in range(len(labels))]
60
 
 
65
  return weightage
66
 
67
 
68
+ gr.Interface(fn=predict, inputs=gr.Image(type='pil'),
69
  outputs='label', title=title, examples=examples()).launch()
net.py DELETED
@@ -1,35 +0,0 @@
1
- import torch
2
- import torch.nn as nn
3
- import torch.nn.functional as F
4
-
5
-
6
- class Net(nn.Module):
7
- def __init__(self):
8
- super().__init__()
9
- self.conv1 = nn.Conv2d(3, 32, 5)
10
- self.conv2 = nn.Conv2d(32, 64, 5)
11
- self.conv3 = nn.Conv2d(64, 128, 5)
12
-
13
- x = torch.randn(3, 150, 150).view(-1, 3, 150, 150)
14
- self._to_linear = None
15
- self.convs(x)
16
-
17
- self.fc1 = nn.Linear(self._to_linear, 512)
18
- self.fc2 = nn.Linear(512, 6)
19
-
20
- def convs(self, x):
21
- x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
22
- x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
23
- x = F.max_pool2d(F.relu(self.conv3(x)), (2, 2))
24
-
25
- if self._to_linear is None:
26
- self._to_linear = x[0].shape[0]*x[0].shape[1]*x[0].shape[2]
27
- return x
28
-
29
- def forward(self, x):
30
- x = self.convs(x)
31
- x = x.view(-1, self._to_linear)
32
- x = F.relu(self.fc1(x))
33
- x = self.fc2(x)
34
-
35
- return F.softmax(x, dim=1)