Skip to content

Commit

Permalink
Add compatibility with pifpaf 0.9 (#9)
Browse files Browse the repository at this point in the history
* remove dependancy on pifpaf.transform

* add compatibility with pifpaf 0.9

* change verbose output

* add image transform compatible with webcam
  • Loading branch information
bertoni9 authored Aug 7, 2019
1 parent 63e5e5e commit f23a2e3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 31 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion monoloco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

"""Open implementation of MonoLoco."""

__version__ = '0.4.3'
__version__ = '0.4.4'
6 changes: 3 additions & 3 deletions monoloco/eval/eval_kitti.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def update_uncertainty(self, std_ale, std_epi, dd, dd_gt, cat):
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]

Expand All @@ -373,8 +373,8 @@ def show_statistics(self):
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} %"
Expand Down
12 changes: 4 additions & 8 deletions monoloco/network/pifpaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
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
Expand All @@ -34,7 +30,7 @@ def __getitem__(self, index):
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

Expand Down
23 changes: 11 additions & 12 deletions monoloco/network/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import torch
import torchvision

from ..utils import get_keypoints, pixel_to_camera

Expand Down Expand Up @@ -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"""

Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions monoloco/visuals/webcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
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):
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
zip_safe=False,

install_requires=[
'openpifpaf==0.8.0',
'openpifpaf',
'tabulate', # For evaluation
],
extras_require={
Expand Down

0 comments on commit f23a2e3

Please sign in to comment.