Update README.md
#23
by
putul
- opened
README.md
CHANGED
@@ -1,90 +1,14 @@
|
|
1 |
-
|
2 |
-
tags:
|
3 |
-
- image-to-text
|
4 |
-
- image-captioning
|
5 |
-
license: apache-2.0
|
6 |
-
widget:
|
7 |
-
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/savanna.jpg
|
8 |
-
example_title: Savanna
|
9 |
-
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/football-match.jpg
|
10 |
-
example_title: Football Match
|
11 |
-
- src: https://huggingface.co/datasets/mishig/sample_images/resolve/main/airport.jpg
|
12 |
-
example_title: Airport
|
13 |
-
---
|
14 |
|
15 |
-
|
|
|
16 |
|
17 |
-
This
|
18 |
|
|
|
19 |
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
# Sample running code
|
28 |
-
|
29 |
-
```python
|
30 |
-
|
31 |
-
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
32 |
-
import torch
|
33 |
-
from PIL import Image
|
34 |
-
|
35 |
-
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
36 |
-
feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
37 |
-
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
38 |
-
|
39 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
-
model.to(device)
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
max_length = 16
|
45 |
-
num_beams = 4
|
46 |
-
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
47 |
-
def predict_step(image_paths):
|
48 |
-
images = []
|
49 |
-
for image_path in image_paths:
|
50 |
-
i_image = Image.open(image_path)
|
51 |
-
if i_image.mode != "RGB":
|
52 |
-
i_image = i_image.convert(mode="RGB")
|
53 |
-
|
54 |
-
images.append(i_image)
|
55 |
-
|
56 |
-
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
57 |
-
pixel_values = pixel_values.to(device)
|
58 |
-
|
59 |
-
output_ids = model.generate(pixel_values, **gen_kwargs)
|
60 |
-
|
61 |
-
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
62 |
-
preds = [pred.strip() for pred in preds]
|
63 |
-
return preds
|
64 |
-
|
65 |
-
|
66 |
-
predict_step(['doctor.e16ba4e4.jpg']) # ['a woman in a hospital bed with a woman in a hospital bed']
|
67 |
-
|
68 |
-
```
|
69 |
-
|
70 |
-
# Sample running code using transformers pipeline
|
71 |
-
|
72 |
-
```python
|
73 |
-
|
74 |
-
from transformers import pipeline
|
75 |
-
|
76 |
-
image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
77 |
-
|
78 |
-
image_to_text("https://ankur3107.github.io/assets/images/image-captioning-example.png")
|
79 |
-
|
80 |
-
# [{'generated_text': 'a soccer game with a player jumping to catch the ball '}]
|
81 |
-
|
82 |
-
|
83 |
-
```
|
84 |
-
|
85 |
-
|
86 |
-
# Contact for any help
|
87 |
-
* https://huggingface.co/ankur310794
|
88 |
-
* https://twitter.com/ankur310794
|
89 |
-
* http://github.com/ankur3107
|
90 |
-
* https://www.linkedin.com/in/ankur310794
|
|
|
1 |
+
What value will be printed?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
code snippet image
|
4 |
+
(Assume necessary headers are included)
|
5 |
|
6 |
+
This code will always print 3, because the thread t is started before the value of y is modified.
|
7 |
|
8 |
+
This code won’t compile.
|
9 |
|
10 |
+
This code will always print 7, because the thread t is joined after both modifications of y.
|
11 |
|
12 |
+
This code will print 3, 6 or 7 (depending on thread scheduling by the operating system)
|
13 |
|
14 |
+
This code has undefined behavior.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|