gustproof commited on
Commit
accc6c7
1 Parent(s): 6564e3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -7,6 +7,7 @@ from torchvision.transforms import (
7
  ToTensor,
8
  Normalize,
9
  InterpolationMode,
 
10
  )
11
  from PIL import Image
12
  import gradio as gr
@@ -15,7 +16,7 @@ print("starting...")
15
  (ys,) = np.load("embs.npz").values()
16
  print("loaded embs")
17
  model = torch.load(
18
- "style-extractor-v0.2.0.ckpt",
19
  map_location="cpu",
20
  )
21
  print("loaded extractor")
@@ -27,15 +28,40 @@ d = ys.shape[1]
27
  index = faiss.IndexHNSWFlat(d, 32)
28
  print("building index")
29
  index.add(ys)
30
- print('index built')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  tf = Compose(
32
  [
33
- Resize(
34
- size=336,
35
- interpolation=InterpolationMode.BICUBIC,
36
- max_size=None,
37
- antialias=True,
38
- ),
39
  ToTensor(),
40
  Normalize(mean=[0.4850, 0.4560, 0.4060], std=[0.2290, 0.2240, 0.2250]),
41
  ]
@@ -56,6 +82,7 @@ def f(im):
56
  D, I = index.search(get_emb(im), n_outputs)
57
  return [f"Distance: {d:.1f}\n![]({urls[i]})" for d, i in zip(D[0], I[0])]
58
 
 
59
  print("preparing gradio")
60
  with gr.Blocks() as demo:
61
  gr.Markdown(
@@ -70,4 +97,4 @@ with gr.Blocks() as demo:
70
  outputs.append(gr.Markdown(label=f"#{len(outputs) + 1}"))
71
  btn.click(f, img, outputs)
72
  print("starting gradio")
73
- demo.launch()
 
7
  ToTensor,
8
  Normalize,
9
  InterpolationMode,
10
+ CenterCrop,
11
  )
12
  from PIL import Image
13
  import gradio as gr
 
16
  (ys,) = np.load("embs.npz").values()
17
  print("loaded embs")
18
  model = torch.load(
19
+ "style-extractor-v0.3.0.ckpt",
20
  map_location="cpu",
21
  )
22
  print("loaded extractor")
 
28
  index = faiss.IndexHNSWFlat(d, 32)
29
  print("building index")
30
  index.add(ys)
31
+ print("index built")
32
+
33
+
34
+ def MyResize(area, d):
35
+ def f(im: Image):
36
+ w, h = im.size
37
+ s = (area / w / h) ** 0.5
38
+ wd, hd = int(s * w / d), int(s * h / d)
39
+ e = lambda a, b: 1 - min(a, b) / max(a, b)
40
+ wd, hd = min(
41
+ (
42
+ (ww * d, hh * d)
43
+ for ww, hh in [(wd + i, hd + j) for i in (0, 1) for j in (0, 1)]
44
+ if ww * d * hh * d <= area
45
+ ),
46
+ key=lambda wh: e(wh[0] / wh[1], w / h),
47
+ )
48
+
49
+ return Compose(
50
+ [
51
+ Resize(
52
+ (int(h * wd / w), wd) if wd / w > hd / h else (hd, int(w * hd / h)),
53
+ InterpolationMode.BICUBIC,
54
+ ),
55
+ CenterCrop((hd, wd)),
56
+ ]
57
+ )(im)
58
+
59
+ return f
60
+
61
+
62
  tf = Compose(
63
  [
64
+ MyResize((518 * 1.3) ** 2, 14),
 
 
 
 
 
65
  ToTensor(),
66
  Normalize(mean=[0.4850, 0.4560, 0.4060], std=[0.2290, 0.2240, 0.2250]),
67
  ]
 
82
  D, I = index.search(get_emb(im), n_outputs)
83
  return [f"Distance: {d:.1f}\n![]({urls[i]})" for d, i in zip(D[0], I[0])]
84
 
85
+
86
  print("preparing gradio")
87
  with gr.Blocks() as demo:
88
  gr.Markdown(
 
97
  outputs.append(gr.Markdown(label=f"#{len(outputs) + 1}"))
98
  btn.click(f, img, outputs)
99
  print("starting gradio")
100
+ demo.launch()