-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdemo.py
155 lines (132 loc) · 7.13 KB
/
demo.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from utils import create_logger,set_seed
import os
import time
import argparse
import json
from PIL import Image
import torch
from clip.clip import CLIP
from gen_utils import generate_caption
from control_gen_utils import control_generate_caption
from transformers import AutoModelForMaskedLM, AutoTokenizer
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--batch_size", type=int, default=1, help = "Only supports batch_size=1 currently.")
parser.add_argument("--device", type=str,
default='cuda',choices=['cuda','cpu'])
## Generation and Controllable Type
parser.add_argument('--run_type',
default='controllable',
nargs='?',
choices=['caption', 'controllable'])
parser.add_argument('--prompt',
default='Image of a',type=str)
parser.add_argument('--order',
default='shuffle',
nargs='?',
choices=['sequential', 'shuffle', 'span', 'random'],
help="Generation order of text")
parser.add_argument('--control_type',
default='sentiment',
nargs='?',
choices=["sentiment","pos"],
help="which controllable task to conduct")
parser.add_argument('--pos_type', type=list,
default=[['DET'], ['ADJ','NOUN'], ['NOUN'],
['VERB'], ['VERB'],['ADV'], ['ADP'],
['DET','NOUN'], ['NOUN'], ['NOUN','.'],
['.','NOUN'],['.','NOUN']],
help="predefined part-of-speech templete")
parser.add_argument('--sentiment_type',
default="positive",
nargs='?',
choices=["positive", "negative"])
parser.add_argument('--samples_num',
default=2,type=int)
## Hyperparameters
parser.add_argument("--sentence_len", type=int, default=10)
parser.add_argument("--candidate_k", type=int, default=200)
parser.add_argument("--alpha", type=float, default=0.02, help="weight for fluency")
parser.add_argument("--beta", type=float, default=2.0, help="weight for image-matching degree")
parser.add_argument("--gamma", type=float, default=5.0, help="weight for controllable degree")
parser.add_argument("--lm_temperature", type=float, default=0.1)
parser.add_argument("--num_iterations", type=int, default=10, help="predefined iterations for Gibbs Sampling")
## Models and Paths
parser.add_argument("--lm_model", type=str, default='bert-base-uncased',
help="Path to language model") # bert,roberta
parser.add_argument("--match_model", type=str, default='openai/clip-vit-base-patch32',
help="Path to Image-Text model") # clip,align
parser.add_argument("--caption_img_path", type=str, default='./examples/girl.jpg',
help="file path of the image for captioning")
parser.add_argument("--stop_words_path", type=str, default='stop_words.txt',
help="Path to stop_words.txt")
parser.add_argument("--add_extra_stopwords", type=list, default=[],
help="you can add some extra stop words")
args = parser.parse_args()
return args
def run_caption(args, image_path, lm_model, lm_tokenizer, clip, token_mask, logger):
logger.info(f"Processing: {image_path}")
image_instance = Image.open(image_path).convert("RGB")
img_name = [image_path.split("/")[-1]]
for sample_id in range(args.samples_num):
logger.info(f"Sample {sample_id}: ")
gen_texts, clip_scores = generate_caption(img_name,lm_model, clip, lm_tokenizer, image_instance, token_mask, logger,
prompt=args.prompt, batch_size=args.batch_size, max_len=args.sentence_len,
top_k=args.candidate_k, temperature=args.lm_temperature,
max_iter=args.num_iterations,alpha=args.alpha,beta=args.beta,
generate_order = args.order)
def run_control(run_type, args, image_path, lm_model, lm_tokenizer, clip, token_mask, logger):
logger.info(f"Processing: {image_path}")
image_instance = Image.open(image_path).convert("RGB")
img_name = [image_path.split("/")[-1]]
for sample_id in range(args.samples_num):
logger.info(f"Sample {sample_id}: ")
gen_texts, clip_scores = control_generate_caption(img_name,lm_model, clip, lm_tokenizer, image_instance, token_mask, logger,
prompt=args.prompt, batch_size=args.batch_size, max_len=args.sentence_len,
top_k=args.candidate_k, temperature=args.lm_temperature,
max_iter=args.num_iterations, alpha=args.alpha,
beta=args.beta, gamma=args.gamma,
ctl_type = args.control_type, style_type=args.sentiment_type,pos_type=args.pos_type, generate_order=args.order)
if __name__ == "__main__":
args = get_args()
set_seed(args.seed)
run_type = "caption" if args.run_type=="caption" else args.control_type
if run_type=="sentiment":
run_type = args.sentiment_type
if os.path.exists("logger")== False:
os.mkdir("logger")
logger = create_logger(
"logger",'demo_{}_{}_len{}_topk{}_alpha{}_beta{}_gamma{}_lmtemp{}_{}.log'.format(
run_type, args.order,args.sentence_len,
args.candidate_k, args.alpha,args.beta,args.gamma,args.lm_temperature,
time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())))
logger.info(f"Generating order:{args.order}")
logger.info(f"Run type:{run_type}")
logger.info(args)
# Load pre-trained model (weights)
lm_model = AutoModelForMaskedLM.from_pretrained(args.lm_model)
lm_tokenizer = AutoTokenizer.from_pretrained(args.lm_model)
lm_model.eval()
clip = CLIP(args.match_model)
clip.eval()
lm_model = lm_model.to(args.device)
clip = clip.to(args.device)
## Remove stop words, token mask
with open(args.stop_words_path,'r',encoding='utf-8') as stop_words_file:
stop_words = stop_words_file.readlines()
stop_words_ = [stop_word.rstrip('\n') for stop_word in stop_words]
stop_words_ += args.add_extra_stopwords
stop_ids = lm_tokenizer.convert_tokens_to_ids(stop_words_)
token_mask = torch.ones((1,lm_tokenizer.vocab_size))
for stop_id in stop_ids:
token_mask[0,stop_id]=0
token_mask = token_mask.to(args.device)
img_path = args.caption_img_path
with torch.no_grad():
if args.run_type == 'caption':
run_caption(args, img_path, lm_model, lm_tokenizer, clip, token_mask, logger)
elif args.run_type == 'controllable':
run_control(run_type, args, img_path, lm_model, lm_tokenizer, clip, token_mask, logger)
else:
raise Exception('run_type must be caption or controllable!')