wuxiaojun commited on
Commit
d59b7ae
1 Parent(s): cf3b3c4

update modeling_ziya_blip2.py and add test.py

Browse files
__pycache__/modeling_ziya_blip2.cpython-310.pyc ADDED
Binary file (8.04 kB). View file
 
modeling_ziya_blip2.py CHANGED
@@ -20,7 +20,7 @@ from transformers import (
20
  logger = logging.get_logger(__name__)
21
 
22
 
23
- class ZiyaBlip2ForCausalLM(Blip2PreTrainedModel):
24
  config_class = Blip2Config
25
  main_input_name = "pixel_values"
26
  _keys_to_ignore_on_load_missing = [
 
20
  logger = logging.get_logger(__name__)
21
 
22
 
23
+ class ZiyaBLIP2ForConditionalGeneration(Blip2PreTrainedModel):
24
  config_class = Blip2Config
25
  main_input_name = "pixel_values"
26
  _keys_to_ignore_on_load_missing = [
test.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import LlamaForCausalLM, LlamaTokenizer, BlipImageProcessor
2
+ from modeling_ziya_blip2 import ZiyaBLIP2ForConditionalGeneration
3
+ from PIL import Image
4
+
5
+ # 请注意目前https://huggingface.co/IDEA-CCNL/Ziya-LLaMA-13B-v1是delta权重(即差值权重)
6
+ # LM_MODEL_PATH需要的是完整权重
7
+ # 因此请先根据Ziya-LLaMA-13B-v1的README.md中的说明进行转换,获取完整的Ziya-LLaMA-13B-v1权重
8
+ # 我这里是本地已经转换好的Ziya-LLaMA-13B-v1完整权重,所以直接使用
9
+ LM_MODEL_PATH="/cognitive_comp/wuxiaojun/pretrained/pytorch/huggingface/Ziya-LLaMA-13B-v1"
10
+ lm_model = LlamaForCausalLM.from_pretrained(LM_MODEL_PATH)
11
+ tokenizer = LlamaTokenizer.from_pretrained(LM_MODEL_PATH)
12
+
13
+ # visual model
14
+ OPENAI_CLIP_MEAN = [0.48145466, 0.4578275, 0.40821073]
15
+ OPENAI_CLIP_STD = [0.26862954, 0.26130258, 0.27577711]
16
+ # demo.py is in the project path, so we can use local path ".". Otherwise you should use "IDEA-CCNL/Ziya-BLIP2-14B-Visual-v1"
17
+ model = ZiyaBLIP2ForConditionalGeneration.from_pretrained(".", language_model=lm_model)
18
+ image_size = model.config.vision_config.image_size
19
+ image_processor = BlipImageProcessor(
20
+ size={"height": image_size, "width": image_size},
21
+ image_mean=OPENAI_CLIP_MEAN,
22
+ image_std=OPENAI_CLIP_STD,
23
+ )
24
+ model.cuda() # if you use on cpu, comment this line
25
+
26
+ generate_config = {
27
+ "max_new_tokens": 128,
28
+ "top_p": 0.1,
29
+ "temperature": 0.7
30
+ }
31
+ output = model.chat(
32
+ tokenizer=tokenizer,
33
+ pixel_values=image_processor(Image.open("wzry.jpg"), return_tensors="pt").pixel_values.to(model.device),
34
+ query="这是什么游戏",
35
+ previous_querys=[],
36
+ previous_outputs=[],
37
+ **generate_config,
38
+ )
39
+ print(output)
40
+ # 这是一款名为《王者荣耀》的多人在线竞技游戏。在游戏中,玩家扮演不同的角色,并与其他玩家进行战斗。游戏中的人物和环境都是虚拟的,但它们看起来非常逼真。玩家需要使用各种技能和策略来击败对手,并获得胜利。