Zayn commited on
Commit
61f6b81
·
1 Parent(s): 05ecfc9

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -0
README.md ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
3
+
4
+ model = VisionEncoderDecoderModel.from_pretrained("/Zayn/vit2distilgpt2")
5
+ feature_extractor = ViTFeatureExtractor.from_pretrained("/Zayn/vit2distilgpt2")
6
+ tokenizer = AutoTokenizer.from_pretrained("/Zayn/vit2distilgpt2")
7
+
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ model.to(device)
10
+
11
+
12
+
13
+ max_length = 16
14
+ num_beams = 4
15
+ gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
16
+ def predict_step(image_paths):
17
+ images = []
18
+ for image_path in image_paths:
19
+ i_image = Image.open(image_path)
20
+ if i_image.mode != "RGB":
21
+ i_image = i_image.convert(mode="RGB")
22
+
23
+ images.append(i_image)
24
+
25
+ pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
26
+ pixel_values = pixel_values.to(device)
27
+
28
+ output_ids = model.generate(pixel_values, **gen_kwargs)
29
+
30
+ preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
31
+ preds = [pred.strip() for pred in preds]
32
+ return preds
33
+
34
+
35
+ predict_step(['doctor.e16ba4e4.jpg'])