Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
from PIL import Image
|
6 |
+
import torch
|
7 |
+
from torchvision import transforms
|
8 |
+
from model_video import build_model
|
9 |
+
import numpy as np
|
10 |
+
import collections
|
11 |
+
import argparse
|
12 |
+
|
13 |
+
net = build_model('cpu').to('cpu')
|
14 |
+
#net=torch.nn.DataParallel(net)
|
15 |
+
model_path = '/content/models/image_best.pth'
|
16 |
+
print(model_path)
|
17 |
+
weight=torch.load(model_path,map_location=torch.device('cpu'))
|
18 |
+
#print(type(weight))
|
19 |
+
new_dict=collections.OrderedDict()
|
20 |
+
for k in weight.keys():
|
21 |
+
new_dict[k[len('module.'):]]=weight[k]
|
22 |
+
net.load_state_dict(new_dict)
|
23 |
+
net.eval()
|
24 |
+
net = net.to('cpu')
|
25 |
+
def test(gpu_id, net, img_list, group_size, img_size):
|
26 |
+
print('test')
|
27 |
+
device='cpu'
|
28 |
+
|
29 |
+
img_transform = transforms.Compose([transforms.Resize((img_size, img_size)), transforms.ToTensor(),
|
30 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
|
31 |
+
img_transform_gray = transforms.Compose([transforms.Resize((img_size, img_size)), transforms.ToTensor(),
|
32 |
+
transforms.Normalize(mean=[0.449], std=[0.226])])
|
33 |
+
with torch.no_grad():
|
34 |
+
|
35 |
+
group_img=torch.rand(5,3,224,224)
|
36 |
+
for i in range(5):
|
37 |
+
group_img[i]=img_transform(Image.fromarray(img_list[i]))
|
38 |
+
_,pred_mask=net(group_img)
|
39 |
+
print(pred_mask.shape)
|
40 |
+
result = [Image.fromarray((pred_mask[i].detach().squeeze().unsqueeze(2).repeat(1,1,3) * 255).numpy().astype(np.uint8)) for i in range(5)]
|
41 |
+
#w, h = 224,224#Image.open(image_list[i][j]).size
|
42 |
+
#result = result.resize((w, h), Image.BILINEAR)
|
43 |
+
#result.convert('L').save('0.png')
|
44 |
+
print('done')
|
45 |
+
return result
|
46 |
+
def sepia(img1,img2,img3,img4,img5):
|
47 |
+
print('sepia')
|
48 |
+
'''ans=[]
|
49 |
+
print(len(input_imgs))
|
50 |
+
for input_img in input_imgs:
|
51 |
+
sepia_filter = np.array(
|
52 |
+
[[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]
|
53 |
+
)
|
54 |
+
sepia_img = input_img.dot(sepia_filter.T)
|
55 |
+
sepia_img /= sepia_img.max()
|
56 |
+
ans.append(input_img)'''
|
57 |
+
img_list=[img1,img2,img3,img4,img5]
|
58 |
+
h_list,w_list=[_.shape[0] for _ in img_list],[_.shape[1] for _ in img_list]
|
59 |
+
#print(type(img1))
|
60 |
+
#print(img1.shape)
|
61 |
+
result_list=test('cpu',net,img_list,5,224)
|
62 |
+
#result_list=[result_list[i].resize((w_list[i], h_list[i]), Image.BILINEAR) for i in range(5)]
|
63 |
+
img1,img2,img3,img4,img5=result_list#test('cpu',net,img_list,5,224)
|
64 |
+
return img1,img2,img3,img4,img5
|
65 |
+
|
66 |
+
#gr.Image(shape=(224, 2))
|
67 |
+
demo = gr.Interface(sepia, inputs=["image","image","image","image","image"], outputs=["image","image","image","image","image"])#gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
|
68 |
+
|
69 |
+
demo.launch(debug=True)
|