def _test(self): self.model.eval() count_test = 0 with torch.no_grad(): for step, (image, target, target_weight, joints_data) in enumerate( tqdm(self.dl_test, desc='Test')): image = image.to(self.device) target = target.to(self.device) target_weight = target_weight.to(self.device) output = self.model(image) if self.flip_test_images: image_flipped = flip_tensor(image, dim=-1) output_flipped = self.model(image_flipped) output_flipped = flip_back(output_flipped, self.ds_test.flip_pairs) output = (output + output_flipped) * 0.5 loss = self.loss_fn(output, target, target_weight) # Evaluate accuracy # Get predictions on the input accs, avg_acc, cnt, joints_preds, joints_target = \ self.ds_test.evaluate_accuracy(output, target) if avg_acc == 0: continue self.mean_loss_test += loss.item() self.mean_acc_test += avg_acc.item() count_test += 1 if step == 0: save_images(image, target, joints_target, output, joints_preds, joints_data['joints_visibility']) self.len_dl_test = count_test print("count_test:", count_test, " self.mean_acc_test:", self.mean_acc_test) self.mean_loss_test /= self.len_dl_test self.mean_acc_test /= self.len_dl_test print('\nTest: Loss %f - Accuracy %f' % (self.mean_loss_test, self.mean_acc_test))
def _test(self): self.model.eval() with torch.no_grad(): for step, (image, target, target_weight, joints_data) in enumerate( tqdm(self.dl_test, desc='Test')): image = image.to(self.device) target = target.to(self.device) target_weight = target_weight.to(self.device) output = self.model(image) if self.flip_test_images: image_flipped = flip_tensor(image, dim=-1) output_flipped = self.model(image_flipped) output_flipped = flip_back(output_flipped, self.ds_test.flip_pairs) output = (output + output_flipped) * 0.5 loss = self.loss_fn(output, target, target_weight) # Evaluar la precisión # Obtenga predicciones sobre la entrada accs, avg_acc, cnt, joints_preds, joints_target = \ self.ds_test.evaluate_accuracy(output, target) self.mean_loss_test += loss.item() self.mean_acc_test += avg_acc.item() if step == 0: save_images(image, target, joints_target, output, joints_preds, joints_data['joints_visibility']) self.mean_loss_test /= self.len_dl_test self.mean_acc_test /= self.len_dl_test print('\nTest: Loss %f - Accuracy %f' % (self.mean_loss_test, self.mean_acc_test))
def _val(self): num_samples = len(self.ds_val) all_preds = np.zeros((num_samples, self.model_nof_joints, 3), dtype=np.float32) all_boxes = np.zeros((num_samples, 6), dtype=np.float32) image_paths = [] idx = 0 self.model.eval() with torch.no_grad(): for step, (image, target, target_weight, joints_data) in enumerate( tqdm(self.dl_val, desc='Validating')): image = image.to(self.device) target = target.to(self.device) target_weight = target_weight.to(self.device) output = self.model(image) if self.flip_test_images: image_flipped = flip_tensor(image, dim=-1) output_flipped = self.model(image_flipped) output_flipped = flip_back(output_flipped, self.ds_val.flip_pairs) output = (output + output_flipped) * 0.5 loss = self.loss_fn(output, target, target_weight) # Evaluate accuracy # Get predictions on the resized images (given as input) accs, avg_acc, cnt, joints_preds, joints_target = \ self.ds_train.evaluate_accuracy(output, target) # Original num_images = image.shape[0] # measure elapsed time c = joints_data['center'].numpy() s = joints_data['scale'].numpy() score = joints_data['score'].numpy() pixel_std = 200 # ToDo Parametrize this preds, maxvals = get_final_preds( True, output, c, s, pixel_std) # ToDo check what post_processing exactly does all_preds[idx:idx + num_images, :, 0:2] = preds[:, :, 0:2].detach().cpu().numpy() all_preds[idx:idx + num_images, :, 2:3] = maxvals.detach().cpu().numpy() # double check this all_boxes parts all_boxes[idx:idx + num_images, 0:2] = c[:, 0:2] all_boxes[idx:idx + num_images, 2:4] = s[:, 0:2] all_boxes[idx:idx + num_images, 4] = np.prod(s * pixel_std, 1) all_boxes[idx:idx + num_images, 5] = score image_paths.extend(joints_data['imgPath']) idx += num_images self.mean_loss_val += loss.item() self.mean_acc_val += avg_acc.item() if self.use_tensorboard: self.summary_writer.add_scalar( 'val_loss', loss.item(), global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar( 'val_acc', avg_acc.item(), global_step=step + self.epoch * self.len_dl_val) if step == 0: save_images(image, target, joints_target, output, joints_preds, joints_data['joints_visibility'], self.summary_writer, step=step + self.epoch * self.len_dl_train, prefix='test_') self.mean_loss_val /= len(self.dl_val) self.mean_acc_val /= len(self.dl_val) # COCO evaluation print('\nVal AP/AR') self.val_accs, self.mean_mAP_val = self.ds_val.evaluate_overall_accuracy( all_preds, all_boxes, image_paths, output_dir=self.log_path)
def _val(self): num_samples = len(self.ds_val) all_preds = np.zeros((num_samples, self.model_nof_joints, 3), dtype=np.float32) all_boxes = np.zeros((num_samples, 6), dtype=np.float32) image_paths = [] idx = 0 self.model.eval() with torch.no_grad(): for step, (image, target, target_weight, joints_data) in enumerate( tqdm(self.dl_val, desc='Validating')): image = image.to(self.device) target = target.to(self.device) target_weight = target_weight.to(self.device) output = self.model(image) if self.flip_test_images: image_flipped = flip_tensor(image, dim=-1) output_flipped = self.model(image_flipped) output_flipped = flip_back(output_flipped, self.ds_val.flip_pairs) output = (output + output_flipped) * 0.5 loss = self.loss_fn(output, target, target_weight) # Evalua la precision # Obtiene predicciones de las imagenes redimensionadas (como argumento) accs, avg_acc, cnt, joints_preds, joints_target = \ self.ds_train.evaluate_accuracy(output, target) # Original num_images = image.shape[0] # Mide el tiempo de calculo c = joints_data['center'].numpy() s = joints_data['scale'].numpy() score = joints_data['score'].numpy() pixel_std = 200 preds, maxvals = get_final_preds(True, output, c, s, pixel_std) all_preds[idx:idx + num_images, :, 0:2] = preds[:, :, 0:2].detach().cpu().numpy() all_preds[idx:idx + num_images, :, 2:3] = maxvals.detach().cpu().numpy() all_boxes[idx:idx + num_images, 0:2] = c[:, 0:2] all_boxes[idx:idx + num_images, 2:4] = s[:, 0:2] all_boxes[idx:idx + num_images, 4] = np.prod(s * pixel_std, 1) all_boxes[idx:idx + num_images, 5] = score image_paths.extend(joints_data['imgPath']) idx += num_images self.mean_loss_val += loss.item() self.mean_acc_val += avg_acc.item() if self.use_tensorboard: self.summary_writer.add_scalar( 'val_loss', loss.item(), global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar( 'val_acc', avg_acc.item(), global_step=step + self.epoch * self.len_dl_val) if step == 0: save_images(image, target, joints_target, output, joints_preds, joints_data['joints_visibility'], self.summary_writer, step=step + self.epoch * self.len_dl_train, prefix='test_') self.mean_loss_val /= len(self.dl_val) self.mean_acc_val /= len(self.dl_val) # evaluacion COCO print('\nVal AP/AR') self.val_accs, self.mean_mAP_val = self.ds_val.evaluate_overall_accuracy( all_preds, all_boxes, image_paths, output_dir=self.log_path)
def _val(self): num_samples = len(self.ds_val) all_preds = np.zeros((num_samples, self.model_nof_joints, 3), dtype=np.float32) all_boxes = np.zeros((num_samples, 6), dtype=np.float32) image_paths = [] idx = 0 self.model.eval() losses = AverageMeter() avg_accs = AverageMeter() pbar = tqdm(self.dl_val, ncols=170) for step, (image, target, target_weight, joints_data) in enumerate(self.dl_val): image = image.cuda() target = target.cuda() target_weight = target_weight.cuda() output = self.model(image) if self.flip_test_images: image_flipped = flip_tensor(image, dim=-1) output_flipped = self.model(image_flipped) output_flipped = flip_back(output_flipped, self.ds_val.flip_pairs) output = (output + output_flipped) * 0.5 loss = self.loss_fn(output, target, target_weight) # Evaluate accuracy # Get predictions on the resized images (given as input) accs, avg_acc, cnt, joints_preds, joints_target = \ self.ds_train.evaluate_accuracy(output, target) losses.update(loss) avg_accs.update(avg_acc) # Original num_images = image.shape[0] log = f'[Epoch {self.epoch}] ' log += f'Valid loss : {loss.item():.4f}({losses.avg:.4f}) ' log += f'Valid acc : {avg_acc.item():.4f}({avg_accs.avg:.4f}) ' pbar.set_description(log) pbar.update() # measure elapsed time c = joints_data['center'].numpy() s = joints_data['scale'].numpy() score = joints_data['score'].numpy() pixel_std = 200 # ToDo Parametrize this preds, maxvals = get_final_preds( True, output, c, s, pixel_std) # ToDo check what post_processing exactly does all_preds[idx:idx + num_images, :, 0:2] = preds[:, :, 0:2].detach().cpu().numpy() all_preds[idx:idx + num_images, :, 2:3] = maxvals.detach().cpu().numpy() # double check this all_boxes parts all_boxes[idx:idx + num_images, 0:2] = c[:, 0:2] all_boxes[idx:idx + num_images, 2:4] = s[:, 0:2] all_boxes[idx:idx + num_images, 4] = np.prod(s * pixel_std, 1) all_boxes[idx:idx + num_images, 5] = score image_paths.extend(joints_data['imgPath']) idx += num_images self.mean_loss_val += loss.item() self.mean_acc_val += avg_acc.item() if self.use_tensorboard: self.summary_writer.add_scalar('Valid/Loss', loss.item(), global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/Accuracy', avg_acc.item(), global_step=step + self.epoch * self.len_dl_val) if step == 0: save_images(image, target, joints_target, output, joints_preds, joints_data['joints_visibility'], self.summary_writer, step=step + self.epoch * self.len_dl_train, prefix='test_') self.mean_loss_val /= len(self.dl_val) self.mean_acc_val /= len(self.dl_val) # COCO evaluation # print('\nVal AP/AR') self.val_accs, self.mean_mAP_val = self.ds_val.evaluate_overall_accuracy( all_preds, all_boxes, image_paths, output_dir=self.log_path) mean_mAP = self.val_accs[ 'AP'] # Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets= 20 ] AP_5 = self.val_accs[ 'Ap .5'] # Average Precision (AP) @[ IoU=0.50 | area= all | maxDets= 20 ] AP_75 = self.val_accs[ 'AP .75'] # Average Precision (AP) @[ IoU=0.75 | area= all | maxDets= 20 ] mean_mAR = self.val_accs[ 'AR'] # Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 20 ] = 0.378 AR_5 = self.val_accs[ 'AR .5'] # Average Recall (AR) @[ IoU=0.50 | area= all | maxDets= 20 ] AR_75 = self.val_accs[ 'AR .75'] # Average Recall (AR) @[ IoU=0.75 | area= all | maxDets= 20 ] log = f'[EPOCH {self.epoch}] Valid Loss : {losses.avg:.4f}, ' log += f'Valid acc : {avg_accs.avg:.4f}, ' log += f'AP : {mean_mAP:.4f}, ' log += f'AP.5 : {AP_5:.4f}, ' log += f'AP.75 : {AP_75:.4f}, ' log += f'AR : {mean_mAR:.4f}, ' pbar.set_description(log) pbar.close() if self.use_tensorboard: self.summary_writer.add_scalar('Valid/mean_mAP', mean_mAP, global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/AP.5', AP_5, global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/AP.75', AP_75, global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/mean_mAR', mean_mAR, global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/AR.5', AR_5, global_step=step + self.epoch * self.len_dl_val) self.summary_writer.add_scalar('Valid/AR.75', AR_75, global_step=step + self.epoch * self.len_dl_val)