Repository navigation
Expand file tree
/
Copy pathinference-image.py
More file actions
136 lines (128 loc) · 6.26 KB
/
Copy pathinference-image.py
File metadata and controls
136 lines (128 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- encoding: utf-8 -*-
import os, sys
import json
import argparse
import torch
from tqdm import tqdm # Import tqdm for the progress bar
from sat.model.mixins import CachedAutoregressiveMixin
from sat.model import AutoModel
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from utils.utils import chat, llama2_tokenizer, llama2_text_processor_inference, get_image_processor
from utils.utils import llama3_tokenizer
from utils.models import CogVLMModel
from utils.models import VisualLlamaEVA
from io import BytesIO
import pandas as pd
from PIL import Image
import numpy as np
from VisionReward_Image.t2v_metrics.vqascore import VQAScore
MASK_INDICES = [0, 1, 2] # Indices of mask features in original list
MASK_FEATURE_MAP = {
0: [22, 23, 24, 28, 29], # 'body(mask)' masks related features 'body correct' & 'harmfulness'
1: [25, 26], # 'face(mask)' masks related features 'face'
2: [27], # 'hands(mask)' masks related features 'hands'
}
def cal_score(args,image_path,prompt,model,text_processor_infer,image_processor):
with open(args.ques_file, 'r') as file:
ques_data = [line.strip() for line in file]
with open(args.weight_file, 'r') as file2:
weight_data = json.load(file2)
wegiht = weight_data['coef']
intercept = weight_data['intercept']
answer_list = []
alignment_score = VQAScore(model='clip-flant5-xxl') # our recommended scoring model
alignment = alignment_score(images=[image_path], texts=[prompt])[0][0].cpu().item()
for ques in tqdm(ques_data, f'scoring image:{image_path}'):
try:
response, _, _ = chat(
image_path=image_path,
image = None,
model=model,
text_processor=text_processor_infer,
img_processor=image_processor,
query=ques,
max_length=args.max_length,
top_p=args.top_p,
temperature=args.temperature,
top_k=args.top_k,
invalid_slices=text_processor_infer.invalid_slices,
args=args
)
answer_list.append(response)
except Exception as e:
answer_list.append(None)
print(f"Error processing {ques}: {str(e)}")
reward = [(1 if ans =='yes<|end_of_text|>' else -1 ) for ans in answer_list]
# add mask
for mask_index, feature_indices in MASK_FEATURE_MAP.items():
for feature_index in feature_indices:
reward[feature_index] *= (int)(reward[mask_index] > 0)
reward_filtered = [v for i, v in enumerate(reward) if i not in MASK_INDICES]
final_reward = [alignment] + reward_filtered
score = np.dot(final_reward, wegiht) + intercept
return score[0]
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--max_length", type=int, default=3328, help='max length of the total sequence')
parser.add_argument("--top_p", type=float, default=0.4, help='top p for nucleus sampling')
parser.add_argument("--top_k", type=int, default=1, help='top k for top k sampling')
parser.add_argument("--temperature", type=float, default=0.8, help='temperature for sampling')
parser.add_argument("--version", type=str, default="vqa", choices=['chat', 'vqa', 'chat_old', 'base'], help='version of language process')
parser.add_argument("--from_pretrained", type=str, default="THUDM/VisionReward-Image", help='pretrained ckpt') # You need to first download the model from https://huggingface.co/THUDM/VisionReward-Image and then refer to its README to extract the checkpoint.
parser.add_argument("--tokenizer_path", type=str, default="meta-llama/Meta-Llama-3-8B-Instruct", help='tokenizer path')
parser.add_argument("--fp16", action="store_true", help="Use fp16 precision")
parser.add_argument("--bf16", action="store_true", help="Use bf16 precision")
parser.add_argument("--stream_chat", action="store_true")
parser.add_argument("--ques_file", type=str, default="VisionReward_Image/VisionReward_image_qa_select.txt", help="Path to the meta question file")
parser.add_argument("--weight_file", type=str, default="VisionRewardImage/weight_select.json", help="Path to the weight file")
parser.add_argument('--question', type=str, help='Question to be answered', default='Is the image clear?')
parser.add_argument('--score', help='Whether to output the score', default=False, action='store_true')
args = parser.parse_args()
# Initialize model
model, model_args = VisualLlamaEVA.from_pretrained(
args.from_pretrained,
args=argparse.Namespace(
deepspeed=None,
local_rank=0,
rank=0,
world_size=1,
model_parallel_size=1,
mode='inference',
skip_init=True,
use_gpu_initialization=True,
device='cuda',
**vars(args)
)
)
model = model.eval()
model.add_mixin('auto-regressive', CachedAutoregressiveMixin())
tokenizer = llama3_tokenizer(args.tokenizer_path, signal_type=args.version)
image_processor = get_image_processor(model_args.eva_args["image_size"][0])
text_processor_infer = llama2_text_processor_inference(tokenizer, args.max_length, model.image_length)
# Set input
image_path1 = "asset/test/test1.jpg"
image_path2 = "asset/test/test2.jpg"
prompt = "A close-up portrait of a beautiful girl with an autumn leaves headdress and melting wax."
with torch.no_grad():
if args.score:
score = cal_score(args,image_path1,prompt,model,text_processor_infer,image_processor)
print(f"score: {score}")
else:
ques = args.question
response, _, _ = chat(
image_path=image_path1,
image = None,
model=model,
text_processor=text_processor_infer,
img_processor=image_processor,
query=ques,
max_length=args.max_length,
top_p=args.top_p,
temperature=args.temperature,
top_k=args.top_k,
invalid_slices=text_processor_infer.invalid_slices,
args=args
)
print(f"response:{response}")
if __name__ == "__main__":
main()