KodIIm_14 / app.py
antonovmaxim's picture
Update app.py
f970b64
raw
history blame
1.43 kB
import gradio as gr
import torch
from torch import nn
from torchvision import transforms
from PIL import Image
import os
os.system("wget https://huggingface.co/antonovmaxim/aiornot-kodIIm-14/resolve/main/model.pth -O model.pth")
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()