From f23a2e34f5a305204a1a3ebdb5495676e718a016 Mon Sep 17 00:00:00 2001 From: Lorenzo Bertoni <34957815+bertoni9@users.noreply.github.com> Date: Wed, 7 Aug 2019 12:10:57 +0200 Subject: [PATCH] Add compatibility with pifpaf 0.9 (#9) * remove dependancy on pifpaf.transform * add compatibility with pifpaf 0.9 * change verbose output * add image transform compatible with webcam --- README.md | 6 +++--- monoloco/__init__.py | 2 +- monoloco/eval/eval_kitti.py | 6 +++--- monoloco/network/pifpaf.py | 12 ++++-------- monoloco/network/process.py | 23 +++++++++++------------ monoloco/visuals/webcam.py | 5 ++--- setup.py | 2 +- 7 files changed, 25 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 0365124..cd417fb 100644 --- a/README.md +++ b/README.md @@ -136,9 +136,9 @@ Multiple visualizations can be combined in different windows. The above gif has been obtained running on a Macbook the command: -`pip3 install opencv-python` - -`python3 -m monoloco.run predict --webcam --scale 0.2 --output_types combined --z_max 10 --checkpoint resnet50` +```pip3 install opencv-python +python3 -m monoloco.run predict --webcam --scale 0.2 --output_types combined --z_max 10 --checkpoint resnet50 --model data/models/monoloco-190513-1437.pkl +``` # Preprocess diff --git a/monoloco/__init__.py b/monoloco/__init__.py index fdd0fe7..3131424 100644 --- a/monoloco/__init__.py +++ b/monoloco/__init__.py @@ -1,4 +1,4 @@ """Open implementation of MonoLoco.""" -__version__ = '0.4.3' +__version__ = '0.4.4' diff --git a/monoloco/eval/eval_kitti.py b/monoloco/eval/eval_kitti.py index 9a606ad..64f803c 100644 --- a/monoloco/eval/eval_kitti.py +++ b/monoloco/eval/eval_kitti.py @@ -347,7 +347,7 @@ class EvalKitti: def show_statistics(self): print('-'*90) - alp = [[str(100 * average(self.errors[key][perc]))[:4] + alp = [[str(100 * average(self.errors[key][perc]))[:5] for perc in ['<0.5m', '<1m', '<2m']] for key in self.METHODS] @@ -373,8 +373,8 @@ class EvalKitti: if key == 'our': print("% of annotation inside the confidence interval: {:.1f} %, " "of which {:.1f} % at higher risk" - .format(self.dic_stats['test'][key][clst]['interval'], - self.dic_stats['test'][key][clst]['at_risk'])) + .format(self.dic_stats['test'][key][clst]['interval']*100, + self.dic_stats['test'][key][clst]['at_risk']*100)) for perc in ['<0.5m', '<1m', '<2m']: print("{} Instances with error {}: {:.2f} %" diff --git a/monoloco/network/pifpaf.py b/monoloco/network/pifpaf.py index a4fe463..a47d948 100644 --- a/monoloco/network/pifpaf.py +++ b/monoloco/network/pifpaf.py @@ -5,22 +5,18 @@ import numpy as np import torchvision import torch from PIL import Image, ImageFile - from openpifpaf.network import nets from openpifpaf import decoder -from openpifpaf import transforms + +from .process import image_transform class ImageList(torch.utils.data.Dataset): """It defines transformations to apply to images and outputs of the dataloader""" - def __init__(self, image_paths, scale, image_transform=None): + def __init__(self, image_paths, scale): self.image_paths = image_paths - self.image_transform = image_transform or transforms.image_transform # to_tensor + normalize (from pifpaf) self.scale = scale - # data = datasets.ImageList(args.images, preprocess=transforms.RescaleRelative(2 - # .0) - def __getitem__(self, index): image_path = self.image_paths[index] ImageFile.LOAD_TRUNCATED_IMAGES = True @@ -34,7 +30,7 @@ class ImageList(torch.utils.data.Dataset): interpolation=Image.BICUBIC) # PIL images are not iterables original_image = torchvision.transforms.functional.to_tensor(image) # 0-255 --> 0-1 - image = self.image_transform(image) + image = image_transform(image) return image_path, original_image, image diff --git a/monoloco/network/process.py b/monoloco/network/process.py index 4915bf2..e967f95 100644 --- a/monoloco/network/process.py +++ b/monoloco/network/process.py @@ -3,6 +3,7 @@ import json import numpy as np import torch +import torchvision from ..utils import get_keypoints, pixel_to_camera @@ -82,18 +83,6 @@ def laplace_sampling(outputs, n_samples): return xx -def epistemic_variance(total_outputs): - """Compute epistemic variance""" - - # var_y = np.sum(total_outputs**2, axis=0) / total_outputs.shape[0] - (np.mean(total_outputs, axis=0))**2 - var_y = np.var(total_outputs, axis=0) - lower_b = np.quantile(a=total_outputs, q=0.25, axis=0) - upper_b = np.quantile(a=total_outputs, q=0.75, axis=0) - var_new = (upper_b - lower_b) - - return var_y, var_new - - def unnormalize_bi(outputs): """Unnormalize relative bi of a nunmpy array""" @@ -151,3 +140,13 @@ def prepare_pif_kps(kps_in): ccs = kps_in[2:][::3] return [xxs, yys, ccs] + + +def image_transform(image): + + normalize = torchvision.transforms.Normalize( + mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225] + ) + transforms = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), normalize, ]) + return transforms(image) diff --git a/monoloco/visuals/webcam.py b/monoloco/visuals/webcam.py index b621e81..974ef55 100644 --- a/monoloco/visuals/webcam.py +++ b/monoloco/visuals/webcam.py @@ -11,12 +11,11 @@ import time import torch import matplotlib.pyplot as plt from PIL import Image -from openpifpaf import transforms import cv2 from ..visuals import Printer from ..network import PifPaf, MonoLoco -from ..network.process import preprocess_pifpaf, factory_for_gt +from ..network.process import preprocess_pifpaf, factory_for_gt, image_transform def webcam(args): @@ -42,7 +41,7 @@ def webcam(args): height, width, _ = image.shape print('resized image size: {}'.format(image.shape)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - processed_image_cpu = transforms.image_transform(image.copy()) + processed_image_cpu = image_transform(image.copy()) processed_image = processed_image_cpu.contiguous().to(args.device, non_blocking=True) fields = pifpaf.fields(torch.unsqueeze(processed_image, 0))[0] _, _, pifpaf_out = pifpaf.forward(image, processed_image_cpu, fields) diff --git a/setup.py b/setup.py index 5b7276f..c2b72b0 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,7 @@ setup( zip_safe=False, install_requires=[ - 'openpifpaf==0.8.0', + 'openpifpaf', 'tabulate', # For evaluation ], extras_require={