Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,23 +9,11 @@ torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/0000000397
|
|
| 9 |
torch.hub.download_url_to_file('https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png', 'stop_sign.png')
|
| 10 |
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
|
| 11 |
|
| 12 |
-
# git_processor_base = AutoProcessor.from_pretrained("microsoft/git-base-coco")
|
| 13 |
-
# git_model_base = AutoModelForCausalLM.from_pretrained("microsoft/git-base-coco")
|
| 14 |
-
|
| 15 |
git_processor_large_coco = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
| 16 |
-
git_model_large_coco = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
| 17 |
-
|
| 18 |
-
# git_processor_large_textcaps = AutoProcessor.from_pretrained("microsoft/git-large-r-textcaps")
|
| 19 |
-
# git_model_large_textcaps = AutoModelForCausalLM.from_pretrained("microsoft/git-large-r-textcaps")
|
| 20 |
-
|
| 21 |
-
# blip_processor_base = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 22 |
-
# blip_model_base = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 23 |
|
| 24 |
blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 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 |
blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-6.7b")
|
| 31 |
blip2_model_4_bit = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-6.7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
|
@@ -33,33 +21,15 @@ blip2_model_4_bit = Blip2ForConditionalGeneration.from_pretrained("Salesforce/bl
|
|
| 33 |
instructblip_processor = AutoProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")
|
| 34 |
instructblip_model_4_bit = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
| 35 |
|
| 36 |
-
# vitgpt_processor = AutoImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 37 |
-
# vitgpt_model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 38 |
-
# vitgpt_tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 39 |
-
|
| 40 |
-
# coca_model, _, coca_transform = open_clip.create_model_and_transforms(
|
| 41 |
-
# model_name="coca_ViT-L-14",
|
| 42 |
-
# pretrained="mscoco_finetuned_laion2B-s13B-b90k"
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 46 |
|
| 47 |
-
# git_model_base.to(device)
|
| 48 |
-
# blip_model_base.to(device)
|
| 49 |
-
git_model_large_coco.to(device)
|
| 50 |
-
# git_model_large_textcaps.to(device)
|
| 51 |
-
blip_model_large.to(device)
|
| 52 |
-
# vitgpt_model.to(device)
|
| 53 |
-
# coca_model.to(device)
|
| 54 |
-
# blip2_model.to(device)
|
| 55 |
-
|
| 56 |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
| 57 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 58 |
|
| 59 |
if use_float_16:
|
| 60 |
inputs = inputs.to(torch.float16)
|
| 61 |
|
| 62 |
-
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=
|
| 63 |
|
| 64 |
if tokenizer is not None:
|
| 65 |
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
@@ -69,45 +39,28 @@ def generate_caption(processor, model, image, tokenizer=None, use_float_16=False
|
|
| 69 |
return generated_caption
|
| 70 |
|
| 71 |
|
| 72 |
-
def
|
| 73 |
-
im = transform(image).unsqueeze(0).to(device)
|
| 74 |
-
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 75 |
-
generated = model.generate(im, seq_len=20)
|
| 76 |
-
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def generate_caption_instructblip(processor, model, image):
|
| 80 |
prompt = "Generate a caption for the image:"
|
| 81 |
|
| 82 |
inputs = processor(images=image, text=prompt, return_tensors="pt").to(device=device, torch_dtype=torch.float16)
|
| 83 |
|
| 84 |
generated_ids = model.generate(pixel_values=inputs.pixel_values,
|
| 85 |
-
num_beams=5, max_length=
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 89 |
|
| 90 |
|
| 91 |
def generate_captions(image):
|
| 92 |
-
# caption_git_base = generate_caption(git_processor_base, git_model_base, image)
|
| 93 |
-
|
| 94 |
caption_git_large_coco = generate_caption(git_processor_large_coco, git_model_large_coco, image)
|
| 95 |
|
| 96 |
-
# caption_git_large_textcaps = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
| 97 |
-
|
| 98 |
-
# caption_blip_base = generate_caption(blip_processor_base, blip_model_base, image)
|
| 99 |
-
|
| 100 |
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# caption_coca = generate_caption_coca(coca_model, coca_transform, image)
|
| 105 |
-
|
| 106 |
-
# caption_blip2 = generate_caption(blip2_processor, blip2_model, image, use_float_16=True).strip()
|
| 107 |
-
|
| 108 |
-
caption_blip2_8_bit = generate_caption(blip2_processor, blip2_model_8_bit, image, use_float_16=True).strip()
|
| 109 |
|
| 110 |
-
caption_instructblip_4_bit =
|
| 111 |
|
| 112 |
return caption_git_large_coco, caption_blip_large, caption_blip2_8_bit, caption_instructblip_4_bit
|
| 113 |
|
|
|
|
| 9 |
torch.hub.download_url_to_file('https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png', 'stop_sign.png')
|
| 10 |
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
git_processor_large_coco = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
| 13 |
+
git_model_large_coco = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco", device_map="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 16 |
+
blip_model_large = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large", device_map="auto")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
blip2_processor = AutoProcessor.from_pretrained("Salesforce/blip2-opt-6.7b")
|
| 19 |
blip2_model_4_bit = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-6.7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
|
|
|
| 21 |
instructblip_processor = AutoProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")
|
| 22 |
instructblip_model_4_bit = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b", device_map="auto", load_in_4bit=True, torch_dtype=torch.float16)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
| 27 |
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 28 |
|
| 29 |
if use_float_16:
|
| 30 |
inputs = inputs.to(torch.float16)
|
| 31 |
|
| 32 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, num_beams=3, max_length=20, min_length=5)
|
| 33 |
|
| 34 |
if tokenizer is not None:
|
| 35 |
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
|
|
|
| 39 |
return generated_caption
|
| 40 |
|
| 41 |
|
| 42 |
+
def generate_caption_blip2(processor, model, image, replace_token=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
prompt = "Generate a caption for the image:"
|
| 44 |
|
| 45 |
inputs = processor(images=image, text=prompt, return_tensors="pt").to(device=device, torch_dtype=torch.float16)
|
| 46 |
|
| 47 |
generated_ids = model.generate(pixel_values=inputs.pixel_values,
|
| 48 |
+
num_beams=5, max_length=50, min_length=1, top_p=0.9, repetition_penalty=1.5, length_penalty=1.0, temperature=1)
|
| 49 |
+
if replace_token:
|
| 50 |
+
# TODO remove once https://github.com/huggingface/transformers/pull/24492 is merged
|
| 51 |
+
generated_ids[generated_ids == 0] = 2
|
| 52 |
|
| 53 |
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 54 |
|
| 55 |
|
| 56 |
def generate_captions(image):
|
|
|
|
|
|
|
| 57 |
caption_git_large_coco = generate_caption(git_processor_large_coco, git_model_large_coco, image)
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
|
| 60 |
|
| 61 |
+
caption_blip2_8_bit = generate_caption_blip2(blip2_processor, blip2_model_8_bit, image).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
caption_instructblip_4_bit = generate_caption_blip2(instructblip_processor, instructblip_model_4_bit, image)
|
| 64 |
|
| 65 |
return caption_git_large_coco, caption_blip_large, caption_blip2_8_bit, caption_instructblip_4_bit
|
| 66 |
|