MCPcopy Create free account
hub / github.com/LTH14/mar / evaluate

Function evaluate

engine_mar.py:104–221  ·  view source on GitHub ↗
(model_without_ddp, vae, ema_params, args, epoch, batch_size=16, log_writer=None, cfg=1.0,
             use_ema=True)

Source from the content-addressed store, hash-verified

102
103
104def evaluate(model_without_ddp, vae, ema_params, args, epoch, batch_size=16, log_writer=None, cfg=1.0,
105 use_ema=True):
106 model_without_ddp.eval()
107 num_steps = args.num_images // (batch_size * misc.get_world_size()) + 1
108 save_folder = os.path.join(args.output_dir, "ariter{}-diffsteps{}-temp{}-{}cfg{}-image{}".format(args.num_iter,
109 args.num_sampling_steps,
110 args.temperature,
111 args.cfg_schedule,
112 cfg,
113 args.num_images))
114 if use_ema:
115 save_folder = save_folder + "_ema"
116 if args.evaluate:
117 save_folder = save_folder + "_evaluate"
118 print("Save to:", save_folder)
119 if misc.get_rank() == 0:
120 if not os.path.exists(save_folder):
121 os.makedirs(save_folder)
122
123 # switch to ema params
124 if use_ema:
125 model_state_dict = copy.deepcopy(model_without_ddp.state_dict())
126 ema_state_dict = copy.deepcopy(model_without_ddp.state_dict())
127 for i, (name, _value) in enumerate(model_without_ddp.named_parameters()):
128 assert name in ema_state_dict
129 ema_state_dict[name] = ema_params[i]
130 print("Switch to ema")
131 model_without_ddp.load_state_dict(ema_state_dict)
132
133 class_num = args.class_num
134 assert args.num_images % class_num == 0 # number of images per class must be the same
135 class_label_gen_world = np.arange(0, class_num).repeat(args.num_images // class_num)
136 class_label_gen_world = np.hstack([class_label_gen_world, np.zeros(50000)])
137 world_size = misc.get_world_size()
138 local_rank = misc.get_rank()
139 used_time = 0
140 gen_img_cnt = 0
141
142 for i in range(num_steps):
143 print("Generation step {}/{}".format(i, num_steps))
144
145 labels_gen = class_label_gen_world[world_size * batch_size * i + local_rank * batch_size:
146 world_size * batch_size * i + (local_rank + 1) * batch_size]
147 labels_gen = torch.Tensor(labels_gen).long().cuda()
148
149
150 torch.cuda.synchronize()
151 start_time = time.time()
152
153 # generation
154 with torch.no_grad():
155 with torch.cuda.amp.autocast():
156 sampled_tokens = model_without_ddp.sample_tokens(bsz=batch_size, num_iter=args.num_iter, cfg=cfg,
157 cfg_schedule=args.cfg_schedule, labels=labels_gen,
158 temperature=args.temperature)
159 sampled_images = vae.decode(sampled_tokens / 0.2325)
160
161 # measure speed after the first generation batch

Callers 1

mainFunction · 0.90

Calls 5

printFunction · 0.85
state_dictMethod · 0.80
load_state_dictMethod · 0.80
sample_tokensMethod · 0.80
decodeMethod · 0.80

Tested by

no test coverage detected