sagivp commited on
Commit
46ae92f
·
verified ·
1 Parent(s): 26bc258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -19
app.py CHANGED
@@ -5,36 +5,78 @@ import requests
5
  import numpy as np
6
  from PIL import Image
7
 
8
- shapes = ["leggings", "jogger",
9
- "palazzo", "cargo",
10
- "dresspants", "chinos"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:Marqo/marqo-fashionCLIP')
13
- tokenizer = open_clip.get_tokenizer('hf-hub:Marqo/marqo-fashionCLIP')
14
 
15
-
16
- shapes_desc = list(map(lambda x: "a " + x + " pants shape", shapes))
17
- text = tokenizer(shapes_desc)
18
 
19
  with torch.no_grad(), torch.cuda.amp.autocast():
20
- text_features = model.encode_text(text)
21
- text_features /= text_features.norm(dim=-1, keepdim=True)
 
22
 
23
  def predict(inp):
24
  image = preprocess_val(inp).unsqueeze(0)
25
 
26
- with torch.no_grad(), torch.cuda.amp.autocast():
27
- image_features = model.encode_image(image)
28
- image_features /= image_features.norm(dim=-1, keepdim=True)
29
-
30
- text_probs = (100 * image_features @ text_features.T).softmax(dim=-1)
31
- confidences = {shapes[i]: float(text_probs[0, i]) for i in range(6)}
32
- return confidences
33
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  gr.Interface(fn=predict,
36
  inputs=gr.Image(type="pil"),
37
- outputs=gr.Label(num_top_classes=6),
38
  examples=["imgs/cargo.jpg", "imgs/palazzo.jpg",
39
  "imgs/leggings.jpg", "imgs/jogger.jpg",
40
  "imgs/chinos.jpg", "imgs/dresspants.jpg"]).launch(share=True)
 
5
  import numpy as np
6
  from PIL import Image
7
 
8
+ catgs = [
9
+ "Shirts",
10
+ "SetShirtsPants",
11
+ "SetJacketsPants",
12
+ "Pants",
13
+ "Jeans",
14
+ "JacketsCoats",
15
+ "Shoes",
16
+ "Underpants",
17
+ "Socks",
18
+ "Hats",
19
+ "Wallets",
20
+ "Bags",
21
+ "Scarfs",
22
+ "Parasols&Umbrellas",
23
+ "Necklaces",
24
+ "Towels&Robes",
25
+ "WallObjects",
26
+ "Rugs",
27
+ "Glassware",
28
+ "Mugs&Cups",
29
+ "OralCare"
30
+ ]
31
 
32
+ model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:Marqo/marqo-fashionSigLIP')
33
+ tokenizer = open_clip.get_tokenizer('hf-hub:Marqo/marqo-fashionSigLIP')
34
 
35
+ text = tokenizer(catgs)
 
 
36
 
37
  with torch.no_grad(), torch.cuda.amp.autocast():
38
+ text_features = model.encode_text(text)
39
+ text_features /= text_features.norm(dim=-1, keepdim=True)
40
+
41
 
42
  def predict(inp):
43
  image = preprocess_val(inp).unsqueeze(0)
44
 
45
+ with torch.no_grad(), torch.cuda.amp.autocast():
46
+ image_features = model.encode_image(image)
47
+ image_features /= image_features.norm(dim=-1, keepdim=True)
48
+ text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
49
+
50
+ max_prob_idx = np.argmax(text_probs)
51
+ pred_lbl = catgs[max_prob_idx]
52
+ pred_lbl_prob = text_probs[0, max_prob_idx].item()
53
+
54
+ mw = ["men", "women", "boy", "girl"]
55
+ catgs = [
56
+ mw[0] + "s " + pred_lbl,
57
+ mw[1] + "s " + pred_lbl,
58
+ mw[2] + "s " + pred_lbl,
59
+ mw[3] + "s " + pred_lbl
60
+ ]
61
+ text = tokenizer(catgs)
62
+
63
+ with torch.no_grad(), torch.cuda.amp.autocast():
64
+ image_features = model.encode_image(image)
65
+ text_features = model.encode_text(text)
66
+ image_features /= image_features.norm(dim=-1, keepdim=True)
67
+ text_features /= text_features.norm(dim=-1, keepdim=True)
68
+
69
+ text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
70
+
71
+ max_prob_idx = np.argmax(text_probs)
72
+ pred_lbl_f = mw[max_prob_idx]
73
+ pred_lbl_prob_f = text_probs[0, max_prob_idx].item()
74
+ tlt = f"{pred_lbl} <{100.0 * pred_lbl_prob:.1f}%> , {pred_lbl_f} <{100.0 * pred_lbl_prob_f:.1f}%>"
75
+ return(tlt)
76
 
77
  gr.Interface(fn=predict,
78
  inputs=gr.Image(type="pil"),
79
+ outputs=gr.Label(),
80
  examples=["imgs/cargo.jpg", "imgs/palazzo.jpg",
81
  "imgs/leggings.jpg", "imgs/jogger.jpg",
82
  "imgs/chinos.jpg", "imgs/dresspants.jpg"]).launch(share=True)