antonovmaxim commited on
Commit
ecf77f3
·
1 Parent(s): a7bc066

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -1,10 +1,16 @@
1
  import gradio as gr
2
  import torch
 
3
  from torchvision import transforms
 
 
 
4
  model = torch.hub.load("pytorch/vision", "resnet101", pretrained=False)
5
  model.fc = nn.Sequential(nn.Linear(2048, 500), nn.ReLU(), nn.Linear(500, 2), nn.Softmax(1))
6
- state_dict = torch.load('model.pth')
7
  model.load_state_dict(state_dict)
 
 
8
 
9
  transform = transforms.Compose([
10
  transforms.RandomHorizontalFlip(p=0.5),
@@ -15,7 +21,7 @@ transform = transforms.Compose([
15
  ])
16
 
17
  def classify(input_img):
18
- return model(transform(input_img))[0][1].item()
19
  def img_classify(input_img):
20
  s = "Вероятность того, что изображение сгенерировано нейросетью равна: " + str(classify(input_img))
21
  return s
 
1
  import gradio as gr
2
  import torch
3
+ from torch import nn
4
  from torchvision import transforms
5
+ from PIL import Image
6
+ !wget https://huggingface.co/antonovmaxim/aiornot-kodIIm-14/resolve/main/model.pth -O model.pth
7
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8
  model = torch.hub.load("pytorch/vision", "resnet101", pretrained=False)
9
  model.fc = nn.Sequential(nn.Linear(2048, 500), nn.ReLU(), nn.Linear(500, 2), nn.Softmax(1))
10
+ state_dict = torch.load('model.pth', map_location=torch.device('cpu'))
11
  model.load_state_dict(state_dict)
12
+ model.to(device)
13
+ model.eval()
14
 
15
  transform = transforms.Compose([
16
  transforms.RandomHorizontalFlip(p=0.5),
 
21
  ])
22
 
23
  def classify(input_img):
24
+ return model(transform(Image.fromarray(input_img)).to(device).unsqueeze(0))[0][0].item()
25
  def img_classify(input_img):
26
  s = "Вероятность того, что изображение сгенерировано нейросетью равна: " + str(classify(input_img))
27
  return s