nielsr HF staff commited on
Commit
3072768
1 Parent(s): cfd7bcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -25,7 +25,7 @@ blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-capt
25
  blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
26
 
27
  blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
28
- blip2_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b")
29
 
30
  # vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
31
  # vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
@@ -47,8 +47,11 @@ blip_model_large.to(device)
47
  coca_model.to(device)
48
  blip2_model.to(device)
49
 
50
- def generate_caption(processor, model, image, tokenizer=None):
51
  inputs = processor(images=image, return_tensors="pt").to(device)
 
 
 
52
 
53
  generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
54
 
@@ -82,7 +85,7 @@ def generate_captions(image):
82
 
83
  caption_coca = generate_caption_coca(coca_model, coca_transform, image)
84
 
85
- caption_blip2 = generate_caption(blip2_processor, blip2_model, image)
86
 
87
  return caption_git_large_coco, caption_git_large_textcaps, caption_blip_large, caption_coca, caption_blip2
88
 
 
25
  blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
26
 
27
  blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-2.7b")
28
+ blip2_model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
29
 
30
  # vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
31
  # vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
 
47
  coca_model.to(device)
48
  blip2_model.to(device)
49
 
50
+ def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
51
  inputs = processor(images=image, return_tensors="pt").to(device)
52
+
53
+ if use_float_16:
54
+ inputs = inputs.to(torch.float16)
55
 
56
  generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
57
 
 
85
 
86
  caption_coca = generate_caption_coca(coca_model, coca_transform, image)
87
 
88
+ caption_blip2 = generate_caption(blip2_processor, blip2_model, image, use_float_16=True)
89
 
90
  return caption_git_large_coco, caption_git_large_textcaps, caption_blip_large, caption_coca, caption_blip2
91