Update README.md
Browse files
README.md
CHANGED
|
@@ -24,6 +24,24 @@ pip install -q git+https://github.com/huggingface/transformers.git
|
|
| 24 |
|
| 25 |
Next you can use it like so:
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
```python
|
| 28 |
import requests
|
| 29 |
import torch
|
|
@@ -38,7 +56,7 @@ url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
|
| 38 |
image = Image.open(requests.get(url, stream=True).raw)
|
| 39 |
labels = ["a photo of a cat", "a photo of a dog", "a photo of a car"]
|
| 40 |
|
| 41 |
-
inputs = processor(text=labels, images=image, return_tensors="pt",
|
| 42 |
|
| 43 |
outputs = model(**inputs)
|
| 44 |
logits_per_image = outputs.logits_per_image
|
|
|
|
| 24 |
|
| 25 |
Next you can use it like so:
|
| 26 |
|
| 27 |
+
```python
|
| 28 |
+
import torch
|
| 29 |
+
from transformers import pipeline
|
| 30 |
+
|
| 31 |
+
clip = pipeline(
|
| 32 |
+
task="zero-shot-image-classification",
|
| 33 |
+
model="facebook/metaclip-2-worldwide-huge-quickgelu",
|
| 34 |
+
torch_dtype=torch.bfloat16,
|
| 35 |
+
device=0
|
| 36 |
+
)
|
| 37 |
+
labels = ["a photo of a cat", "a photo of a dog", "a photo of a car"]
|
| 38 |
+
|
| 39 |
+
results = clip("http://images.cocodataset.org/val2017/000000039769.jpg", candidate_labels=labels)
|
| 40 |
+
print(results)
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
In case you want to perform pre- and postprocessing yourself, you can use the `AutoModel` API:
|
| 44 |
+
|
| 45 |
```python
|
| 46 |
import requests
|
| 47 |
import torch
|
|
|
|
| 56 |
image = Image.open(requests.get(url, stream=True).raw)
|
| 57 |
labels = ["a photo of a cat", "a photo of a dog", "a photo of a car"]
|
| 58 |
|
| 59 |
+
inputs = processor(text=labels, images=image, return_tensors="pt", padding=True)
|
| 60 |
|
| 61 |
outputs = model(**inputs)
|
| 62 |
logits_per_image = outputs.logits_per_image
|