Update README.md
Browse filesexample code to do inference
README.md
CHANGED
@@ -27,9 +27,31 @@ I created at `phunc20/nj_biergarten_captcha_v2`.
|
|
27 |
|
28 |
### Direct Use
|
29 |
|
30 |
-
|
|
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
|
35 |
## Bias, Risks, and Limitations
|
|
|
27 |
|
28 |
### Direct Use
|
29 |
|
30 |
+
```python
|
31 |
+
import torch
|
32 |
|
33 |
+
if torch.cuda.is_available():
|
34 |
+
device = torch.device("cuda")
|
35 |
+
else:
|
36 |
+
device = torch.device("cpu")
|
37 |
+
|
38 |
+
|
39 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
40 |
+
|
41 |
+
hub_dir = "phunc20/trocr-base-handwritten_nj_biergarten_captcha_v2"
|
42 |
+
processor = TrOCRProcessor.from_pretrained(hub_dir)
|
43 |
+
model = VisionEncoderDecoderModel.from_pretrained(hub_dir)
|
44 |
+
model = model.to(device)
|
45 |
+
|
46 |
+
|
47 |
+
from PIL import Image
|
48 |
+
|
49 |
+
image = Image.open("/path/to/image")
|
50 |
+
pixel_values = processor(image, return_tensors='pt').pixel_values
|
51 |
+
pixel_values = pixel_values.to(device)
|
52 |
+
outputs = model.generate(pixel_values)
|
53 |
+
pred_str = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
54 |
+
```
|
55 |
|
56 |
|
57 |
## Bias, Risks, and Limitations
|