| import torch |
| from torchvision.models import resnet101 |
| from fastapi import FastAPI |
| |
| import gradio as gr |
| from PIL import Image |
| import numpy as np |
| import pickle |
| from base import BaseUI |
| |
| from torchvision.transforms import transforms |
| class Test(BaseUI): |
| def __init__(self): |
| super(Test, self).__init__() |
|
|
| def clear_gr(self): |
| image = None |
| text = "" |
| return image, text |
|
|
| def load_model(self): |
| |
| model = resnet101() |
| state_dict = torch.load(self.model_path) |
| model.load_state_dict(state_dict) |
| model.eval() |
| return model |
| def prediciton_model(self,model,img): |
| |
| pre_y = model(img) |
| print(pre_y.shape) |
| _, pre_y = torch.max(pre_y, axis=1) |
| print("预测的的label下标是: ", pre_y) |
| |
| with open(self.contrast_path, 'r', encoding='utf-8') as f: |
| meta = f.readlines() |
|
|
| for i in meta: |
| list = i.split() |
| if pre_y == int(list[0]): |
| return list[2].rstrip(',') |
| |
| |
|
|
| def model_VGG16(self,value): |
| |
| model = self.load_model() |
| if len(self.transforms_Resize) == 2: |
| |
| |
| image_pil = Image.fromarray((value * 255).astype(np.uint8)) |
| transform = transforms.Compose([ |
| transforms.Resize((self.transforms_Resize[0],self.transforms_Resize[1])), |
| transforms.ToTensor(), |
| transforms.Normalize(mean=self.transforms_Normalize_mean, |
| std=self.transforms_Normalize_std) |
| ]) |
| img = transform(image_pil) |
| img = torch.unsqueeze(img, 0) |
| return self.prediciton_model(model, img) |
|
|
| elif len(self.transforms_Resize) == 1: |
| |
| |
| image_pil = Image.fromarray((value * 255).astype(np.uint8)) |
| transform = transforms.Compose([ |
| transforms.Resize(self.transforms_Resize[0]), |
| transforms.CenterCrop(self.transforms_centercrop_value), |
| transforms.ToTensor(), |
| transforms.Normalize(mean=self.transforms_Normalize_mean, |
| std=self.transforms_Normalize_std) |
| ]) |
| img = transform(image_pil) |
| img = torch.unsqueeze(img, 0) |
| return self.prediciton_model(model, img) |
|
|
|
|
| def webui_framework(self): |
| |
| with gr.Blocks() as demo: |
| with gr.Column(): |
| input = gr.Image() |
| with gr.Row(): |
| clear = gr.Button(value="Clear") |
| submit = gr.Button(value="Submit") |
| text = gr.TextArea() |
| submit.click(self.model_VGG16, input, text) |
| clear.click(self.clear_gr, None, [input, text]) |
|
|
|
|
| if __name__ == '__main__': |
| with gr.Blocks() as demo: |
| Test().webui_framework() |
| demo.launch() |