forked from ashleve/lightning-hydra-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding simple 'locsAndConfs' TCN input vector
- Loading branch information
1 parent
22866fc
commit 8eeb729
Showing
5 changed files
with
292 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# @package _global_ | ||
|
||
# to execute this experiment run: | ||
# python train.py experiment=example | ||
task: "m2" | ||
# feature_version: 6 | ||
topic: "medical" | ||
|
||
defaults: | ||
- override /data: ptg | ||
- override /model: ptg | ||
- override /callbacks: default | ||
- override /trainer: gpu | ||
- override /paths: default | ||
#- override /logger: aim | ||
- override /logger: csv | ||
|
||
# all parameters below will be merged with parameters from default configurations set above | ||
# this allows you to overwrite only specified parameters | ||
|
||
# Change this name to something descriptive and unique for this experiment. | ||
# This will differentiate the run logs and output to be separate from other | ||
# experiments that may have been run under the configured | ||
# Setting this value influences: | ||
# - the name of the directory under `${paths.root_dir}/logs/` in which training | ||
# run files are stored. | ||
# Default is "train" set in the "configs/train.yaml" file. | ||
#task_name: | ||
task_name: cameron_locs_and_confs | ||
|
||
# simply provide checkpoint path to resume training | ||
#ckpt_path: null | ||
|
||
tags: ["m2", "ms_tcn", "debug"] | ||
|
||
seed: 12345 | ||
|
||
trainer: | ||
min_epochs: 50 | ||
max_epochs: 500 | ||
log_every_n_steps: 1 | ||
|
||
model: | ||
compile: false | ||
net: | ||
# Length of feature vector for a single frame. | ||
# Currently derived from feature version and other hyperparameters. | ||
dim: 102 | ||
num_classes: 9 | ||
|
||
data: | ||
coco_train_activities: "${paths.coco_file_root}/TRAIN-activity_truth.coco.json" | ||
coco_train_objects: "${paths.coco_file_root}/TRAIN-object_detections.coco.json" | ||
coco_train_poses: "${paths.coco_file_root}/TRAIN-pose_estimates.coco.json" | ||
|
||
coco_validation_activities: "${paths.coco_file_root}/VALIDATION-activity_truth.coco.json" | ||
coco_validation_objects: "${paths.coco_file_root}/VALIDATION-object_detections.coco.json" | ||
coco_validation_poses: "${paths.coco_file_root}/VALIDATION-pose_estimates.coco.json" | ||
|
||
coco_test_activities: "${paths.coco_file_root}/TEST-activity_truth.coco.json" | ||
coco_test_objects: "${paths.coco_file_root}/TEST-object_detections.coco.json" | ||
coco_test_poses: "${paths.coco_file_root}/TEST-pose_estimates.coco.json" | ||
|
||
batch_size: 16384 | ||
num_workers: 16 | ||
target_framerate: 15 # BBN Hololens2 Framerate | ||
epoch_length: 200000 | ||
|
||
train_dataset: | ||
window_size: 25 | ||
vectorizer: | ||
_target_: tcn_hpl.data.vectorize.locs_and_confs.LocsAndConfs | ||
top_k: 1 | ||
num_classes: 7 | ||
use_joint_confs: True | ||
use_pixel_norm: True | ||
use_hand_obj_offsets: False | ||
background_idx: 0 | ||
transform: | ||
transforms: [] # no transforms | ||
# - _target_: tcn_hpl.data.components.augmentations.MoveCenterPts | ||
# hand_dist_delta: 0.05 | ||
# obj_dist_delta: 0.05 | ||
# joint_dist_delta: 0.025 | ||
# im_w: 1280 | ||
# im_h: 720 | ||
# num_obj_classes: 42 | ||
# feat_version: 2 | ||
# top_k_objects: 1 | ||
# - _target_: tcn_hpl.data.components.augmentations.NormalizePixelPts | ||
# im_w: 1280 | ||
# im_h: 720 | ||
# num_obj_classes: 42 | ||
# feat_version: 2 | ||
# top_k_objects: 1 | ||
val_dataset: | ||
transform: | ||
transforms: [] # no transforms | ||
# - _target_: tcn_hpl.data.components.augmentations.NormalizePixelPts | ||
# im_w: 1280 | ||
# im_h: 720 | ||
# num_obj_classes: 42 | ||
# feat_version: 2 | ||
# top_k_objects: 1 | ||
# Test dataset usually configured the same as val, unless there is some | ||
# different set of transforms that should be used during test/prediction. | ||
|
||
paths: | ||
# root_dir: "/data/PTG/medical/training/activity_classifier/TCN_HPL/" | ||
# root_dir: "/home/local/KHQ/paul.tunison/data/darpa-ptg/train-TCN-M2_bbn_hololens/training_root" | ||
root_dir: "/home/local/KHQ/cameron.johnson/code/TCN_HPL/tcn_hpl/train-TCN-M2_bbn_hololens/training_root" | ||
|
||
# Convenience variable to where your train/val/test split COCO file datasets | ||
# are stored. | ||
# coco_file_root: "/home/local/KHQ/paul.tunison/data/darpa-ptg/train-TCN-M2_bbn_hololens" | ||
coco_file_root: "/home/local/KHQ/cameron.johnson/code/TCN_HPL/train-TCN-M2_bbn_hololens" | ||
|
||
#exp_name: "tcn_training_revive" | ||
#logger: | ||
# aim: | ||
# experiment: ${task_name} | ||
# capture_terminal_logs: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import functools | ||
import typing as tg | ||
|
||
import numpy as np | ||
from numpy import typing as npt | ||
|
||
from tcn_hpl.data.vectorize._interface import Vectorize, FrameData | ||
from tcn_hpl.data.vectorize_classic import ( | ||
obj_det2d_set_to_feature, | ||
zero_joint_offset, | ||
HAND_STR_LEFT, | ||
HAND_STR_RIGHT, | ||
) | ||
|
||
|
||
class LocsAndConfs(Vectorize): | ||
""" | ||
Previous manual approach to vectorization. | ||
Arguments: | ||
feat_version: Version number of the feature to produce. | ||
top_k: The number of top per-class examples to use in vector | ||
construction. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
top_k: int = 1, | ||
num_classes: int = 7, | ||
use_joint_confs: bool = True, | ||
use_pixel_norm: bool = True, | ||
use_hand_obj_offsets: bool = False, | ||
background_idx: int = 0 | ||
): | ||
super().__init__() | ||
|
||
self._top_k = top_k | ||
self._num_classes = num_classes | ||
self._use_joint_confs = use_joint_confs | ||
self._use_pixel_norm = use_pixel_norm | ||
self._use_hand_obj_offsets = use_hand_obj_offsets | ||
self._background_idx = background_idx | ||
|
||
# Get the top "k" object indexes for each object | ||
@staticmethod | ||
def get_top_k_indexes_of_one_obj_type(f_dets, k, label_ind): | ||
""" | ||
Find all instances of a label index in object detections. | ||
Then sort them and return the top K. | ||
Inputs: | ||
- object_dets: | ||
""" | ||
labels = f_dets.labels | ||
scores = f_dets.scores | ||
# Get all labels of an obj type | ||
filtered_idxs = [i for i, e in enumerate(labels) if e == label_ind] | ||
if not filtered_idxs: | ||
return None | ||
filtered_scores = [scores[i] for i in filtered_idxs] | ||
# Sort labels by score values. | ||
sorted_inds = [i[1] for i in sorted(zip(filtered_scores, filtered_idxs))] | ||
return sorted_inds[:k] | ||
@staticmethod | ||
def append_vector(frame_feat, i, number): | ||
frame_feat[i] = number | ||
return frame_feat, i + 1 | ||
|
||
|
||
def vectorize(self, data: FrameData) -> npt.NDArray[np.float32]: | ||
|
||
######################### | ||
# Feature vector | ||
######################### | ||
# Length: pose confs * 22, pose X's * 22, pose Y's * 22, | ||
# obj confs * num_objects(7 for M2), | ||
# obj X * num_objects(7 for M2), | ||
# obj Y * num_objects(7 for M2) | ||
# obj W * num_objects(7 for M2) | ||
# obj H * num_objects(7 for M2) | ||
# casualty conf * 1 | ||
vector_len = 102 | ||
frame_feat = np.zeros(vector_len) | ||
vector_ind = 0 | ||
if self._use_pixel_norm: | ||
W = data.size[0] | ||
H = data.size[1] | ||
else: | ||
W = 1 | ||
H = 1 | ||
f_dets = data.object_detections | ||
|
||
# Loop through all classes: populate obj conf, obj X, obj Y. | ||
# Assumption: class labels are [0, 1, 2,... num_classes-1]. | ||
for obj_ind in range(0,self._num_classes): | ||
top_k_idxs = self.get_top_k_indexes_of_one_obj_type(f_dets, self._top_k, obj_ind) | ||
if top_k_idxs: # This is None if there were no detections to sort for this class | ||
for idx in top_k_idxs: | ||
# Conf | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_dets.scores[idx]) | ||
# X | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_dets.boxes[idx][0] / W) | ||
# Y | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_dets.boxes[idx][1] / H) | ||
# W | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_dets.boxes[idx][2] / W) | ||
# H | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_dets.boxes[idx][3] / H) | ||
else: | ||
for _ in range(0,5): | ||
# 5 Zeros | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, 0) | ||
|
||
f_poses = data.poses | ||
if f_poses: | ||
# Find most confident body detection | ||
confident_pose_idx = np.argmax(f_poses.scores) | ||
num_joints = f_poses.joint_positions.shape[1] | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_poses.scores[confident_pose_idx]) | ||
|
||
for joint_ind in range(0, num_joints): | ||
# Conf | ||
if self._use_joint_confs: | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_poses.joint_scores[confident_pose_idx][joint_ind]) | ||
# X | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_poses.joint_positions[confident_pose_idx][joint_ind][0] / W) | ||
# Y | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, f_poses.joint_positions[confident_pose_idx][joint_ind][1] / H) | ||
else: | ||
num_joints = f_poses.joint_positions.shape[1] | ||
if self._use_joint_confs: | ||
rows_per_joint = 3 | ||
else: | ||
rows_per_joint = 2 | ||
for _ in range(num_joints * rows_per_joint + 1): | ||
frame_feat, vector_ind = self.append_vector(frame_feat, vector_ind, 0) | ||
|
||
assert vector_ind == vector_len | ||
|
||
frame_feat = frame_feat.ravel().astype(feat_dtype) | ||
|
||
return frame_feat |