Upload 3 files
Browse files- aesthetic_predictor_huber_ad_ep7.pth +3 -0
- app.py +151 -0
- requirements.txt +6 -0
aesthetic_predictor_huber_ad_ep7.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4791de2a7ce900a978d66a067a60bd3924bf8dc0b85f90426dfe9673a1de68cb
|
3 |
+
size 33252658
|
app.py
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
from transformers import AutoModel, BitImageProcessor, SiglipImageProcessor, SiglipVisionModel
|
5 |
+
from PIL import Image, ImageOps
|
6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
7 |
+
import torch.nn as nn
|
8 |
+
|
9 |
+
device = torch.device('cpu')
|
10 |
+
torch.set_num_threads(4)
|
11 |
+
|
12 |
+
processor_d = BitImageProcessor(do_center_crop=False, do_convert_rgb=False, do_normalize=True, do_rescale=True, do_resize=False, image_mean=[0.485, 0.456, 0.406], image_std=[0.229, 0.224, 0.225], resample=3, rescale_factor=0.00392156862745098)
|
13 |
+
model_d = AutoModel.from_pretrained('facebook/dinov2-base', attn_implementation="sdpa").to(device)
|
14 |
+
processor_s = SiglipImageProcessor.from_pretrained('google/siglip-so400m-patch14-384')
|
15 |
+
model_s = SiglipVisionModel.from_pretrained('google/siglip-so400m-patch14-384', attn_implementation="sdpa").to(device)
|
16 |
+
|
17 |
+
class ResidualBlock(nn.Module):
|
18 |
+
def __init__(self, input_size):
|
19 |
+
super(ResidualBlock, self).__init__()
|
20 |
+
self.linear1 = nn.Linear(input_size, input_size // 2)
|
21 |
+
self.LayerNorm1 = nn.LayerNorm(input_size // 2)
|
22 |
+
self.activation1 = nn.Mish()
|
23 |
+
|
24 |
+
self.linear2 = nn.Linear(input_size // 2, input_size // 4)
|
25 |
+
self.LayerNorm2 = nn.LayerNorm(input_size // 4)
|
26 |
+
self.activation2 = nn.Mish()
|
27 |
+
|
28 |
+
self.linear3 = nn.Linear(input_size // 4, input_size // 2)
|
29 |
+
self.LayerNorm3 = nn.LayerNorm(input_size // 2)
|
30 |
+
self.activation3 = nn.Mish()
|
31 |
+
|
32 |
+
self.linear4 = nn.Linear(input_size // 2, input_size)
|
33 |
+
self.LayerNorm4 = nn.LayerNorm(input_size)
|
34 |
+
self.activation4 = nn.Mish()
|
35 |
+
|
36 |
+
self.shortcut = nn.Linear(input_size, input_size)
|
37 |
+
|
38 |
+
def forward(self, x):
|
39 |
+
identity = self.shortcut(x)
|
40 |
+
out = self.linear1(x)
|
41 |
+
out = self.LayerNorm1(out)
|
42 |
+
out = self.activation1(out)
|
43 |
+
|
44 |
+
out = self.linear2(out)
|
45 |
+
out = self.LayerNorm2(out)
|
46 |
+
out = self.activation2(out)
|
47 |
+
|
48 |
+
out = self.linear3(out)
|
49 |
+
out = self.LayerNorm3(out)
|
50 |
+
out = self.activation3(out)
|
51 |
+
|
52 |
+
out = self.linear4(out)
|
53 |
+
out = self.LayerNorm4(out)
|
54 |
+
out = self.activation4(out)
|
55 |
+
|
56 |
+
out += identity
|
57 |
+
|
58 |
+
return out
|
59 |
+
|
60 |
+
class MLP(nn.Module):
|
61 |
+
def __init__(self, input_size, xcol='emb', ycol='avg_rating'):
|
62 |
+
super().__init__()
|
63 |
+
self.input_size = input_size
|
64 |
+
self.xcol = xcol
|
65 |
+
self.ycol = ycol
|
66 |
+
self.layers = nn.Sequential(
|
67 |
+
ResidualBlock(self.input_size),
|
68 |
+
nn.Mish(),
|
69 |
+
nn.Linear(1920, 1)
|
70 |
+
)
|
71 |
+
|
72 |
+
def forward(self, x):
|
73 |
+
return self.layers(x)
|
74 |
+
|
75 |
+
mlp = MLP(1920)
|
76 |
+
|
77 |
+
s = torch.load("./aesthetic_predictor_huber_ad_ep7.pth", map_location=torch.device('cpu'))
|
78 |
+
|
79 |
+
mlp.load_state_dict(s)
|
80 |
+
|
81 |
+
mlp.to(device)
|
82 |
+
|
83 |
+
mlp.eval()
|
84 |
+
|
85 |
+
def normalized(a, axis=-1, order=2):
|
86 |
+
l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
|
87 |
+
l2[l2 == 0] = 1
|
88 |
+
return a / np.expand_dims(l2, axis)
|
89 |
+
|
90 |
+
def process_image(image, device):
|
91 |
+
image = image.convert('RGBA')
|
92 |
+
background = Image.new('RGBA', image.size, (255, 255, 255, 255))
|
93 |
+
image = Image.alpha_composite(background, image).convert('RGB')
|
94 |
+
|
95 |
+
max_side = 518
|
96 |
+
ratio = max_side / max(image.size)
|
97 |
+
new_size = (int(image.size[0] * ratio), int(image.size[1] * ratio))
|
98 |
+
image_d = image.resize(new_size, Image.LANCZOS)
|
99 |
+
max_side_s = 384
|
100 |
+
ratio_s = max_side_s / max(image.size)
|
101 |
+
new_size_s = (int(image.size[0] * ratio_s), int(image.size[1] * ratio_s))
|
102 |
+
image_resized = image.resize(new_size_s, Image.LANCZOS)
|
103 |
+
|
104 |
+
image_s = ImageOps.pad(image_resized, (384, 384), color=(255, 255, 255))
|
105 |
+
|
106 |
+
inputs_d = processor_d(image_d, return_tensors="pt").to(device)
|
107 |
+
inputs_s = processor_s(image_s, return_tensors="pt").to(device)
|
108 |
+
|
109 |
+
with torch.no_grad():
|
110 |
+
outputs_d = model_d(**inputs_d)
|
111 |
+
outputs_s = model_s(**inputs_s)
|
112 |
+
class_token_d = normalized(outputs_d.pooler_output.cpu().detach().numpy())
|
113 |
+
class_token_s = normalized(outputs_s.pooler_output.cpu().detach().numpy())
|
114 |
+
im_emb_arr = np.concatenate((class_token_s, class_token_d), axis=1)
|
115 |
+
|
116 |
+
prediction_value = mlp(torch.from_numpy(im_emb_arr).to(device).type(torch.FloatTensor)).item()
|
117 |
+
|
118 |
+
return im_emb_arr, prediction_value
|
119 |
+
|
120 |
+
|
121 |
+
def infer(image1, image2):
|
122 |
+
try:
|
123 |
+
|
124 |
+
features1, prediction_value1 = process_image(image1, device)
|
125 |
+
features2, prediction_value2 = process_image(image2, device)
|
126 |
+
|
127 |
+
cos_sim_features = cosine_similarity(features1, features2)[0][0]
|
128 |
+
|
129 |
+
return cos_sim_features, prediction_value1, prediction_value2
|
130 |
+
except Exception as e:
|
131 |
+
print(f"Error during inference: {e}")
|
132 |
+
return "Error", "Error", "Error"
|
133 |
+
|
134 |
+
|
135 |
+
with gr.Blocks() as iface:
|
136 |
+
gr.Markdown("# Anime Aesthetic Predictor Based on Twitter User Preferences\nUpload two images to calculate the aesthetic score (0-10).")
|
137 |
+
with gr.Row():
|
138 |
+
image1 = gr.Image(type="pil")
|
139 |
+
image2 = gr.Image(type="pil")
|
140 |
+
with gr.Row():
|
141 |
+
prediction1 = gr.Textbox(label="Aesthetic Score 1")
|
142 |
+
prediction2 = gr.Textbox(label="Aesthetic Score 2")
|
143 |
+
with gr.Row():
|
144 |
+
feature_similarity = gr.Textbox(label="Feature Similarity")
|
145 |
+
with gr.Row():
|
146 |
+
submit_btn = gr.Button("Submit")
|
147 |
+
|
148 |
+
submit_btn.click(infer, inputs=[image1, image2], outputs=[feature_similarity, prediction1, prediction2])
|
149 |
+
|
150 |
+
iface.queue(max_size=10)
|
151 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy<3
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
Pillow
|
6 |
+
scikit-learn
|