jujeongho commited on
Commit
10c9c91
ยท
verified ยท
1 Parent(s): ffcb003

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +74 -33
README.md CHANGED
@@ -121,8 +121,8 @@ conversation = [
121
  {
122
  "role": "user",
123
  "content": [
124
- {"type": "image", "url": "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg"},
125
- {"type": "text", "text": "์ด ์ด๋ฏธ์ง€์—๋Š” ๋ฌด์—‡์ด ๋ณด์ด๋‚˜์š”?"},
126
  ],
127
  },
128
  ]
@@ -135,45 +135,86 @@ inputs = processor.apply_chat_template(
135
  return_tensors="pt"
136
  ).to(model.device, torch.float16)
137
 
138
- generate_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=False)
139
- outputs = processor.decode(generate_ids[0], skip_special_tokens=True)
140
- print(outputs)
 
 
 
141
  ```
142
- The following shows the input required for using OCR with text localization, along with the corresponding output:
 
 
 
143
  ```python
144
- # INPUT
145
- image_file = "./assets/ocr.jpg"
146
- raw_image = Image.open(image_file)
147
  conversation = [
148
  {
149
  "role": "user",
150
  "content": [
151
- {"type": "text", "text": ""},
152
- {"type": "image"},
 
153
  ],
154
  },
155
  ]
156
 
157
- # OUTPUT
158
- """
159
- <char>๋ฐฑ๋ฒ”๋กœ</char><bbox>0.172, 0.266, 0.328, 0.341</bbox>
160
- <char>124๋ฒˆ๊ธธ</char><bbox>0.347, 0.266, 0.512, 0.341</bbox>
161
- <char>Baekbeom-ro</char><bbox>0.171, 0.337, 0.433, 0.392</bbox>
162
- <char>124</char><bbox>0.444, 0.341, 0.508, 0.392</bbox>
163
- <char>๋งŒ์ˆ˜์ฃผ๊ณต์•„ํŒŒํŠธ</char><bbox>0.109, 0.531, 0.335, 0.601</bbox>
164
- <char>์‹œํฅ</char><bbox>0.443, 0.518, 0.522, 0.581</bbox>
165
- <char>์‹œ์ฒญ</char><bbox>0.711, 0.521, 0.811, 0.594</bbox>
166
- <char>Mansu</char><bbox>0.102, 0.601, 0.181, 0.648</bbox>
167
- <char>Jugong</char><bbox>0.186, 0.601, 0.273, 0.658</bbox>
168
- <char>Apt</char><bbox>0.28, 0.601, 0.327, 0.651</bbox>
169
- <char>42</char><bbox>0.377, 0.601, 0.416, 0.648</bbox>
170
- <char>Shieung</char><bbox>0.445, 0.578, 0.53, 0.625</bbox>
171
- <char>์ธ์ฒœ๋Œ€๊ณต์›</char><bbox>0.43, 0.621, 0.609, 0.684</bbox>
172
- <char>๋ชจ๋ž˜๋‚ด์‹œ์žฅ์—ญ</char><bbox>0.651, 0.59, 0.873, 0.665</bbox>
173
- <char>IncheonGrand</char><bbox>0.432, 0.681, 0.561, 0.723</bbox>
174
- <char>Park</char><bbox>0.564, 0.681, 0.611, 0.723</bbox>
175
- """
176
  ```
177
- <div align="center">
178
- <img src="./ocr.jpg" width="100%" />
179
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  {
122
  "role": "user",
123
  "content": [
124
+ {"type": "image", "url": "https://huggingface.co/NCSOFT/VARCO-VISION-2.0-14B/resolve/main/demo.jpg"},
125
+ {"type": "text", "text": "์ด ์ด๋ฏธ์ง€์— ํ‘œ์‹œ๋œ ๊ฒƒ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?"},
126
  ],
127
  },
128
  ]
 
135
  return_tensors="pt"
136
  ).to(model.device, torch.float16)
137
 
138
+ generate_ids = model.generate(**inputs, max_new_tokens=1024)
139
+ generate_ids_trimmed = [
140
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generate_ids)
141
+ ]
142
+ output = processor.decode(generate_ids_trimmed[0], skip_special_tokens=True)
143
+ print(output)
144
  ```
145
+
146
+ <details>
147
+ <summary>Multi image inference</summary>
148
+
149
  ```python
 
 
 
150
  conversation = [
151
  {
152
  "role": "user",
153
  "content": [
154
+ {"type": "image", "image": "file:///path/to/image1.jpg"},
155
+ {"type": "image", "image": "file:///path/to/image2.jpg"},
156
+ {"type": "text", "text": "์ด๋ฏธ์ง€ ๊ฐ„์˜ ์œ ์‚ฌ์ ์„ ํŒŒ์•…ํ•˜์„ธ์š”."},
157
  ],
158
  },
159
  ]
160
 
161
+ inputs = processor.apply_chat_template(
162
+ conversation,
163
+ add_generation_prompt=True,
164
+ tokenize=True,
165
+ return_dict=True,
166
+ return_tensors="pt"
167
+ ).to(model.device, torch.float16)
168
+
169
+ generate_ids = model.generate(**inputs, max_new_tokens=1024)
170
+ generate_ids_trimmed = [
171
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generate_ids)
172
+ ]
173
+ output = processor.decode(generate_ids_trimmed[0], skip_special_tokens=True)
174
+ print(output)
 
 
 
 
 
175
  ```
176
+ </details>
177
+
178
+ <details>
179
+ <summary>OCR inference</summary>
180
+
181
+ ```python
182
+ from PIL import Image
183
+
184
+ image = Image.open("file:///path/to/image.jpg")
185
+
186
+ # Image upscaling for OCR performance boost
187
+ w, h = image.size
188
+ target_size = 2304
189
+ if max(w, h) < target_size:
190
+ scaling_factor = target_size / max(w, h)
191
+ new_w = int(w * scaling_factor)
192
+ new_h = int(h * scaling_factor)
193
+ image = image.resize((new_w, new_h))
194
+
195
+ conversation = [
196
+ {
197
+ "role": "user",
198
+ "content": [
199
+ {"type": "image", "image": image},
200
+ {"type": "text", "text": "<ocr>"},
201
+ ],
202
+ },
203
+ ]
204
+
205
+ inputs = processor.apply_chat_template(
206
+ conversation,
207
+ add_generation_prompt=True,
208
+ tokenize=True,
209
+ return_dict=True,
210
+ return_tensors="pt"
211
+ ).to(model.device, torch.float16)
212
+
213
+ generate_ids = model.generate(**inputs, max_new_tokens=1024)
214
+ generate_ids_trimmed = [
215
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generate_ids)
216
+ ]
217
+ output = processor.decode(generate_ids_trimmed[0], skip_special_tokens=False)
218
+ print(output)
219
+ ```
220
+ </details>