Skip to content

Commit

Permalink
add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KGallyamov committed Aug 15, 2024
1 parent 110c5a8 commit a0316a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
35 changes: 22 additions & 13 deletions innofw/core/integrations/mmdetection/datamodule.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# standard libraries
import json
import logging
import os
import os.path as osp
import pickle as pkl
import pathlib
import pickle as pkl
import random
from abc import ABC
from collections import namedtuple
Expand All @@ -14,8 +15,6 @@
import open3d as o3d
from tqdm import tqdm

import logging

# local modules
from innofw.constants import Frameworks, Stages
from innofw.core.datamodules.base import BaseDataModule
Expand All @@ -24,6 +23,7 @@
target_maxes = np.array([[136.7853946685791, 135.2938232421875, 45.29965019226074]])
target_mins = np.array([[-136.7853946685791, -135.2938232421875, -45.29965019226074]])


class Mmdetection3DDataModuleAdapter(BaseDataModule, ABC):
"""
...
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(
self.train_set = None
self.val_set = None
self.state = {
'data_path': self.train_source or self.test_source or self.infer_source,
'data_path': self.train_source.parent or self.test_source.parent or self.infer_source.parent,
'save_path': None,
'train_size': 0.9,
'center_coords': [True, True, True], # x, y, z
Expand Down Expand Up @@ -167,7 +167,8 @@ def get_ann_in_framework_format(self, item, sample_idx, pcd_np, pcd_sboxes, sly_
intensity = np.zeros((pcd_slide.shape[0], 1), dtype=np.float32)
pcd_mins = np.array(self.state['point_cloud_range'][:3])
pcd_maxes = np.array(self.state['point_cloud_range'][3:])
pcd_slide = (pcd_slide - pcd_mins) / (pcd_maxes - pcd_mins) * (target_maxes - target_mins) + target_mins
pcd_slide = (pcd_slide - pcd_mins) / (pcd_maxes - pcd_mins) * (
target_maxes - target_mins) + target_mins
pcd_slide = np.hstack((pcd_slide, intensity))
pcd_slide.astype(np.float32).tofile(osp.join(self.state['save_path'], bin_filename))
ptc_info['lidar_points']['lidar_path'] = bin_filename.split('/')[-1]
Expand Down Expand Up @@ -200,9 +201,14 @@ def get_ann_in_framework_format(self, item, sample_idx, pcd_np, pcd_sboxes, sly_
self.state["selectedClasses"].index(objects2class[fig['objectKey']]))
ptc_info['instances']['gt_bboxes_3d'] = np.array(ptc_info['instances']['gt_bboxes_3d'],
dtype=np.float32)
ptc_info['instances']['gt_bboxes_3d'][:, :3] = (ptc_info['instances']['gt_bboxes_3d'][:, :3] - pcd_mins) / (pcd_maxes - pcd_mins) * (target_maxes - target_mins) + target_mins
ptc_info['instances']['gt_bboxes_3d'][:, 3:6] = (ptc_info['instances']['gt_bboxes_3d'][:, 3:6] - pcd_mins) / (pcd_maxes - pcd_mins) * (target_maxes - target_mins) + target_mins

ptc_info['instances']['gt_bboxes_3d'][:, :3] = (ptc_info['instances']['gt_bboxes_3d'][:,
:3] - pcd_mins) / (
pcd_maxes - pcd_mins) * (
target_maxes - target_mins) + target_mins
ptc_info['instances']['gt_bboxes_3d'][:, 3:6] = (ptc_info['instances']['gt_bboxes_3d'][
:, 3:6] - pcd_mins) / (
pcd_maxes - pcd_mins) * (
target_maxes - target_mins) + target_mins

ptc_info['instances']['gt_labels_3d'] = np.array(ptc_info['instances']['gt_labels_3d'],
dtype=np.int32)
Expand Down Expand Up @@ -261,8 +267,9 @@ def save_set_to_annotation(self, save_path, items, slide_boxes, subset):
for ann in annotations:
print(ann['lidar_points']['lidar_path'][:-4], file=f)

annotations = {'metainfo': {'categories': {cat: label for cat, label in enumerate(all_gt_names)}},
'data_list': annotations}
annotations = {
'metainfo': {'categories': {cat: label for cat, label in enumerate(all_gt_names)}},
'data_list': annotations}
with open(save_path, 'wb') as f:
pkl.dump(annotations, f)

Expand Down Expand Up @@ -333,9 +340,11 @@ def prepare_data(self):
pcr = self.state["point_cloud_range"]
self.state['point_cloud_range'] = pcr

self.save_set_to_annotation(osp.join(self.state['save_path'], 'custom_infos_train.pkl'), self.train_set,
self.save_set_to_annotation(osp.join(self.state['save_path'], 'custom_infos_train.pkl'),
self.train_set,
sboxes, 'train')
self.save_set_to_annotation(osp.join(self.state['save_path'], 'custom_infos_val.pkl'), self.val_set,
self.save_set_to_annotation(osp.join(self.state['save_path'], 'custom_infos_val.pkl'),
self.val_set,
sboxes, 'val')

logging.info('Finished data preparation')
Expand Down Expand Up @@ -364,4 +373,4 @@ def test_dataloader(self):
if __name__ == '__main__':
t = Mmdetection3DDataModuleAdapter(data={'source': '/home/karim/workdir/innofw/data/lep3d'},
num_classes=4)
t.setup_train_test_val()
t.setup_train_test_val()
8 changes: 6 additions & 2 deletions innofw/core/integrations/mmdetection/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ def train(self, data, ckpt_path=None):

def test(self, data, ckpt_path=None):
data.setup()
self.update_configs(os.path.abspath(data.state['save_path']))
logging.info('Testing')
devices = [] if self.device == 'cpu' else self.devices
os.system(
f'cd {self.mmdet_path} && sudo -E env "PATH=$PATH" "PYTHONPATH=." "CUDA_VISIBLE_DEVICES={devices}" python tools/test.py configs/pointpillars/pointpillars_hv_secfpn_8xb6_custom.py')
try:
os.system(
f'cd {self.mmdet_path} && sudo -E env "PATH=$PATH" "PYTHONPATH=." "CUDA_VISIBLE_DEVICES={devices}" python tools/test.py configs/pointpillars/pointpillars_hv_secfpn_8xb6_custom.py {ckpt_path}')
except:
logging.info('Failed')
self.rollback_configs()


Expand Down

0 comments on commit a0316a3

Please sign in to comment.