@@ -675,5 +675,60 @@ Stage 2 是为了把 Q-Former 和冻结参数的 LLM 连接起来,以利用 LL
675
675
676
676
3 . 后将投影后的 $Z$ 添加到 input text embeddings前面,Queries 的输出蕴含了视觉信息,送入LLM时,充当了soft visual prompts 。
677
677
678
- 4 . 由于 Q-Former 已经过预训练以提取语言信息视觉表示,因此它有效地充当信息瓶颈,将最有用的信息提供给 LLM,同时删除不相关的视觉信息。这减少了LLM学习视觉语言对齐的负担,从而缓解了灾难性的遗忘问题。
678
+ > 由于 Q-Former 已经过预训练以提取语言信息视觉表示,因此它有效地充当信息瓶颈,将最有用的信息提供给 LLM,同时删除不相关的视觉信息。这减少了LLM学习视觉语言对齐的负担,从而缓解了灾难性的遗忘问题。
679
+
680
+
681
+ Blip2Qformer 的generate方法负责完成图像描述生成(图文到文本):
682
+
683
+ ``` python
684
+ class Blip2Qformer (Blip2Base ):
685
+ ...
686
+ def generate (
687
+ self ,
688
+ samples ,
689
+ use_nucleus_sampling = False ,
690
+ num_beams = 3 ,
691
+ max_length = 30 ,
692
+ min_length = 10 ,
693
+ top_p = 0.9 ,
694
+ repetition_penalty = 1.0 ,
695
+ ):
696
+ image = samples[" image" ]
697
+ image_embeds = self .ln_vision(self .visual_encoder(image))
698
+
699
+ if not use_nucleus_sampling:
700
+ image_embeds = image_embeds.repeat_interleave(num_beams, dim = 0 )
701
+ else :
702
+ num_beams = 1
703
+ image_atts = torch.ones(image_embeds.size()[:- 1 ], dtype = torch.long).to(
704
+ image.device
705
+ )
706
+
707
+ model_kwargs = {
708
+ " encoder_hidden_states" : image_embeds,
709
+ " encoder_attention_mask" : image_atts,
710
+ }
711
+
712
+ input_ids = (
713
+ torch.LongTensor(image.size(0 ), 1 )
714
+ .fill_(self .tokenizer.bos_token_id)
715
+ .to(image.device)
716
+ )
717
+ query_tokens = self .query_tokens.expand(image_embeds.shape[0 ], - 1 , - 1 )
718
+
719
+ outputs = self .Qformer.generate(
720
+ input_ids = input_ids,
721
+ query_embeds = query_tokens,
722
+ max_length = max_length,
723
+ min_length = min_length,
724
+ num_beams = num_beams,
725
+ do_sample = use_nucleus_sampling,
726
+ top_p = top_p,
727
+ eos_token_id = self .tokenizer.sep_token_id,
728
+ pad_token_id = self .tokenizer.pad_token_id,
729
+ ** model_kwargs
730
+ )
731
+ captions = self .tokenizer.batch_decode(outputs, skip_special_tokens = True )
732
+ return captions
733
+ ```
679
734
0 commit comments