StephanAkkerman commited on
Commit
bbfc983
1 Parent(s): 9a66c97

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +23 -8
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
- from transformers import pipeline
 
 
 
86
 
87
- # Create a sentiment analysis pipeline
88
- pipe = pipeline(
89
- "image-classification",
90
- model="StephanAkkerman/chart-recognizer",
91
- )
92
 
93
- # Get the predicted sentiment
94
- print(pipe(image))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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