Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
from DataSet import QuestionDataSet
|
| 9 |
+
import TractionModel as plup
|
| 10 |
+
|
| 11 |
+
import random
|
| 12 |
+
from tqdm import tqdm
|
| 13 |
+
|
| 14 |
+
import gradio as gr
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def snap(image):
|
| 18 |
+
return np.flipud(image)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def init_model(path):
|
| 22 |
+
model = plup.create_model()
|
| 23 |
+
model = plup.load_weights(model, path)
|
| 24 |
+
model.eval()
|
| 25 |
+
return model
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def inference(image):
|
| 29 |
+
image = vanilla_transform(image).to(device).unsqueeze(0)
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
pred = model(image)
|
| 32 |
+
res = float(torch.sigmoid(pred[1].to("cpu")).numpy()[0])
|
| 33 |
+
return {'pull-up': res, 'no pull-up': 1 - res}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
norm_mean = [0.485, 0.456, 0.406]
|
| 37 |
+
norm_std = [0.229, 0.224, 0.225]
|
| 38 |
+
vanilla_transform = torchvision.transforms.Compose([
|
| 39 |
+
torchvision.transforms.Resize(224),
|
| 40 |
+
torchvision.transforms.ToTensor(),
|
| 41 |
+
torchvision.transforms.Normalize(norm_mean, norm_std)])
|
| 42 |
+
|
| 43 |
+
model = init_model("output/model/model-score0.96-f1_10.9-f1_20.99.pt")
|
| 44 |
+
if torch.cuda.is_available():
|
| 45 |
+
device = torch.device("cuda")
|
| 46 |
+
else:
|
| 47 |
+
device = torch.device("cpu")
|
| 48 |
+
model = model.to(device)
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(inference, live=True, inputs=gr.inputs.Image(source="upload", tool=None, type='pil'),
|
| 51 |
+
outputs=gr.outputs.Label())
|
| 52 |
+
|
| 53 |
+
iface.test_launch()
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
iface.launch()
|