|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +This module defines the HapticNet context adapter. |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +# Standard imports |
| 9 | +import os |
| 10 | + |
| 11 | +# Third-party imports |
| 12 | +from ament_index_python.packages import get_package_share_directory |
| 13 | +import numpy as np |
| 14 | +import numpy.typing as npt |
| 15 | +from overrides import override |
| 16 | +import torch |
| 17 | + |
| 18 | +# Local imports |
| 19 | +from ada_feeding_action_select.helpers import logger |
| 20 | +from .models import HapticNetConfig, HapticNet |
| 21 | +from .base_adapters import PosthocAdapter |
| 22 | + |
| 23 | + |
| 24 | +class HapticNetPosthoc(PosthocAdapter): |
| 25 | + """ |
| 26 | + An adapter to run force/torque data through HapticNet |
| 27 | + and extract features. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + checkpoint: str, |
| 33 | + n_features: int = 4, |
| 34 | + gpu_index: int = 0, |
| 35 | + ) -> None: |
| 36 | + """ |
| 37 | + Load Checkpoint and Set Config Parameters |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + checkpoint: PTH file relative to share directory / data |
| 42 | + n_features: size of the HapticNet feature vectory (determined by checkpoint) |
| 43 | + gpu_index: which gpu to use for CUDA |
| 44 | + """ |
| 45 | + |
| 46 | + # Init CUDA |
| 47 | + self.use_cuda = torch.cuda.is_available() |
| 48 | + if self.use_cuda: |
| 49 | + logger.info("Init HapticNet with CUDA") |
| 50 | + os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" |
| 51 | + os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_index) |
| 52 | + |
| 53 | + # Init HapticNet |
| 54 | + self.config = HapticNetConfig(n_output=n_features) |
| 55 | + self.hapticnet = HapticNet(self.config) |
| 56 | + |
| 57 | + # Load Checkpoint |
| 58 | + ckpt_file = os.path.join( |
| 59 | + get_package_share_directory("ada_feeding_action_select"), "data", checkpoint |
| 60 | + ) |
| 61 | + ckpt = torch.load(ckpt_file) |
| 62 | + self.hapticnet.load_state_dict(ckpt["state_dict"]) |
| 63 | + self.hapticnet.eval() |
| 64 | + if self.use_cuda: |
| 65 | + self.hapticnet = self.hapticnet.cuda() |
| 66 | + |
| 67 | + @property |
| 68 | + @override |
| 69 | + def dim(self) -> int: |
| 70 | + # Docstring copied from @override |
| 71 | + # No Bias: Haptic bias apparently made it much, much worse |
| 72 | + return self.config.n_output |
| 73 | + |
| 74 | + @override |
| 75 | + def get_posthoc(self, data: npt.NDArray) -> npt.NDArray: |
| 76 | + # Docstring copied from @override |
| 77 | + |
| 78 | + ret = np.array([]) |
| 79 | + |
| 80 | + # Run data through hapticnet |
| 81 | + input_data = self.hapticnet.preprocess(data) |
| 82 | + if input_data is None: |
| 83 | + return ret |
| 84 | + if self.use_cuda: |
| 85 | + input_data = input_data.cuda() |
| 86 | + |
| 87 | + # Get HapticNet Features |
| 88 | + features = self.hapticnet(input_data) |
| 89 | + |
| 90 | + # Flatten, no bias |
| 91 | + ret = features.cpu().detach().numpy().flatten() |
| 92 | + |
| 93 | + assert ret.size == self.dim |
| 94 | + return ret |
0 commit comments