Update README.md
Browse files
README.md
CHANGED
|
@@ -82,16 +82,31 @@ For a comprehensive overview, including the training setup and analysis of the m
|
|
| 82 |
Using [HuggingFace's transformers library](https://huggingface.co/docs/transformers/index) the model can be converted into a pipeline for image classification.
|
| 83 |
|
| 84 |
```python
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
model="StephanAkkerman/chart-recognizer",
|
| 91 |
-
)
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
```
|
| 96 |
|
| 97 |
## Citing & Authors
|
|
|
|
| 82 |
Using [HuggingFace's transformers library](https://huggingface.co/docs/transformers/index) the model can be converted into a pipeline for image classification.
|
| 83 |
|
| 84 |
```python
|
| 85 |
+
import timm
|
| 86 |
+
import torch
|
| 87 |
+
from PIL import Image
|
| 88 |
+
from timm.data import resolve_data_config, create_transform
|
| 89 |
|
| 90 |
+
# Load and set model to eval mode
|
| 91 |
+
model = timm.create_model("hf_hub:StephanAkkerman/chart-recognizer", pretrained=True)
|
| 92 |
+
model.eval()
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Create transform and get labels
|
| 95 |
+
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
|
| 96 |
+
labels = model.pretrained_cfg["label_names"]
|
| 97 |
+
|
| 98 |
+
# Load and preprocess image
|
| 99 |
+
image = Image.open("img/examples/tweet_example.png").convert("RGB")
|
| 100 |
+
x = transform(image).unsqueeze(0)
|
| 101 |
+
|
| 102 |
+
# Get model output and apply softmax
|
| 103 |
+
probabilities = torch.nn.functional.softmax(model(x)[0], dim=0)
|
| 104 |
+
|
| 105 |
+
# Map probabilities to labels
|
| 106 |
+
output = {label: prob.item() for label, prob in zip(labels, probabilities)}
|
| 107 |
+
|
| 108 |
+
# Print the predicted probabilities
|
| 109 |
+
print(output)
|
| 110 |
```
|
| 111 |
|
| 112 |
## Citing & Authors
|