def predict(self, dataset): weights_original = self.copy_param() metrics = Metrics() while dataset.next_rel(): metrics.reset_task_metrics(dataset.cur_rel) for idx_iter in range(self.hyp.num_test_inner_iter): for idx_data in range(self.hyp.num_shot): dscp1, dscp2, dscpn, dscpr = dataset.get_sup_inner_data() for idx_iter in range(self.hyp.num_test_inner_iter): self.__inner_train_step(dscp1, dscp2, dscpn, dscpr, False) batch_dscp_cand, dscpr = dataset.get_qur_task_cand_rel() cands_emb = [] if self.memory: self.qur_dscpr_emb, self.qur_head_memory_dscpr_emb, self.qur_tail_memory_dscpr_emb = self.encoder.encode_rel( dscpr) cands_emb = [ self.encoder(bucket, self.qur_tail_memory_dscpr_emb) for bucket in batch_dscp_cand ] else: self.qur_dscpr_emb = self.encoder(dscpr) cands_emb = [ self.encoder(bucket) for bucket in batch_dscp_cand ] self.qur_cands_emb = T.cat(cands_emb, 0) while 1: qur_task = dataset.get_qur_inner_data() if qur_task is None: break sim_cands = self.__predict_triplet(*qur_task) sim_cands = sim_cands.detach( ).data # sim_cands[0]: y_true sim, sim_cands[1:]: y_cands sim sim_cands = sim_cands.cpu().numpy() metrics.add(sim_cands) self.set_param(weights_original) if self.memory: self.qur_dscpr_emb = self.qur_cands_emb = self.qur_head_memory_dscpr_emb = self.qur_tail_memory_dscpr_emb = None else: self.qur_dscpr_emb = self.qur_cands_emb = None metrics.log_task_metric() metrics.log_overall_metric()
with torch.no_grad(): for batch_idx, (images, masks) in enumerate(validation_loader): images = images.cuda() masks = masks.cuda() preds = net(images) loss = loss_fn(preds, masks) test_loss += loss.item() preds = preds.argmax(dim=1) preds = preds.view(-1).cpu().data.numpy() masks = masks.view(-1).cpu().data.numpy() metrics.add(preds, masks) n_iter = (epoch - 1) * iter_per_epoch + batch_idx + 1 miou = metrics.iou() precision = metrics.precision() recall = metrics.recall() metrics.clear() utils.visualize_scalar( writer, 'Test/mIOU', miou, epoch, ) utils.visualize_scalar(