File size: 1,489 Bytes
a523faa
 
 
8c93272
c6876c4
ecf77f3
c6876c4
ecf77f3
 
c6876c4
 
b4908a3
c6876c4
ecf77f3
 
c6876c4
 
dc02fd8
c6876c4
 
 
 
 
 
42dc215
ecf77f3
d843ea2
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
os.system('pip install --upgrade torch torchvision')
os.system("wget https://huggingface.co/antonovmaxim/aiornot-kodIIm-14/resolve/main/model.pth -O model.pth")
import gradio as gr
import torch
from torch import nn
from torchvision import transforms
from PIL import Image
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = torch.hub.load("pytorch/vision", "resnet101", pretrained=False)
model.fc = nn.Sequential(nn.Linear(2048, 500), nn.ReLU(), nn.Linear(500, 2), nn.Softmax(1))
state_dict = torch.load('model.pth', map_location=device)
model.load_state_dict(state_dict)
model.to(device)
model.eval()

transform = transforms.Compose([
    # transforms.RandomHorizontalFlip(p=0.5),
    transforms.Resize(256),  # Resize the image to 256x256 pixels
    transforms.CenterCrop(224),  # Crop the center 224x224 pixels
    transforms.ToTensor(),  # Convert the image to a PyTorch tensor
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # Normalize the image
])

def classify(input_img):
  return model(transform(Image.fromarray(input_img)).to(device).unsqueeze(0))[0][0].item()
def img_classify(input_img):
    s = "Вероятность того, что изображение сгенерировано нейросетью равна: " + str(classify(input_img))
    return s
output1 = gr.inputs.Textbox(placeholder="Результат")
gui = gr.Interface(fn = img_classify, inputs="image", outputs=output1)
gui.launch()