File size: 3,920 Bytes
489cd9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "\n",
    "import torch\n",
    "from transformers import (\n",
    "    AutoModelForImageClassification,\n",
    "    AutoImageProcessor,\n",
    ")\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "MODEL_NAME = \"p1atdev/siglip-tagger-test-2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = AutoModelForImageClassification.from_pretrained(\n",
    "    MODEL_NAME, torch_dtype=torch.bfloat16, trust_remote_code=True\n",
    ")\n",
    "model.eval()\n",
    "processor = AutoImageProcessor.from_pretrained(MODEL_NAME)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = Image.open(\"sample.jpg\")\n",
    "inputs = processor(image, return_tensors=\"pt\").to(model.device, model.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "logits = model(**inputs).logits.detach().cpu().float()[0]\n",
    "logits = np.clip(logits, 0.0, 1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = {\n",
    "    model.config.id2label[i]: logit for i, logit in enumerate(logits) if logit > 0\n",
    "}\n",
    "results = sorted(results.items(), key=lambda x: x[1], reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1girl: 100.00%\n",
      "outdoors: 100.00%\n",
      "sky: 100.00%\n",
      "solo: 100.00%\n",
      "school uniform: 96.88%\n",
      "skirt: 92.97%\n",
      "day: 89.06%\n",
      "cloud: 85.94%\n",
      "scenery: 79.69%\n",
      "pleated skirt: 72.27%\n",
      "black hair: 66.80%\n",
      "standing: 65.62%\n",
      "sailor collar: 59.38%\n",
      "sitting: 57.81%\n",
      "long sleeves: 53.52%\n",
      "serafuku: 53.12%\n",
      "holding: 52.34%\n",
      "tree: 47.46%\n",
      "dress: 46.48%\n",
      "shoes: 43.55%\n",
      "building: 42.77%\n",
      "neckerchief: 40.82%\n",
      "short hair: 38.09%\n",
      "water: 38.09%\n",
      "cloudy sky: 37.30%\n",
      "looking at viewer: 32.23%\n",
      "long hair: 32.03%\n",
      "brown eyes: 31.45%\n",
      "plant: 31.05%\n",
      "bag: 29.30%\n",
      "railing: 29.10%\n",
      "sunlight: 28.12%\n",
      "from side: 27.73%\n",
      "window: 27.54%\n",
      "brown hair: 26.37%\n",
      "white shirt: 25.78%\n",
      "shirt: 25.39%\n",
      "blue sky: 23.93%\n",
      "hairclip: 23.44%\n",
      "blunt bangs: 21.58%\n",
      "picture frame: 19.34%\n",
      "hand up: 18.26%\n",
      "black skirt: 17.87%\n",
      "smile: 17.87%\n",
      "from behind: 13.57%\n",
      "cowboy shot: 10.99%\n",
      "indoors: 10.74%\n",
      "curtains: 10.25%\n",
      "facing away: 9.23%\n",
      "white socks: 6.08%\n",
      "bottle: 6.01%\n",
      "mountain: 5.66%\n",
      "blue skirt: 5.13%\n",
      "drinking straw: 3.37%\n",
      "kneehighs: 1.71%\n"
     ]
    }
   ],
   "source": [
    "for tag, score in results:\n",
    "    print(f\"{tag}: {score*100:.2f}%\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}