Download patch/nuscenes_c_transform.py from giahuythai/secondlook-a100: direct link, hf CLI and curl.
- Browser
- Download file 26.6 kB
-
https://huggingface.co/giahuythai/secondlook-a100/resolve/main/patch/nuscenes_c_transform.py
- Command line
-
hf download hf://giahuythai/secondlook-a100/patch/nuscenes_c_transform.py
-
curl -L -o nuscenes_c_transform.py https://huggingface.co/giahuythai/secondlook-a100/resolve/main/patch/nuscenes_c_transform.py
26.6 kB
| """Adapter nuScenes-C (Dong et al. CVPR'23, thu-ml/3D_Corruptions_AD) vào pipeline | |
| eval của mmdet3d — áp corruption ON-THE-FLY thay vì sinh dataset offline | |
| (tương đương về mặt số học, tiết kiệm ~1TB đĩa). | |
| Cell hỗ trợ (đúng bộ MoME/PFS báo cáo + 3 cell noise/blur/dark của ta): | |
| ĐÚNG theo Appendix A.1 của paper nuScenes-C (2303.11040) — weather cell là | |
| BI-MODAL (camera + LiDAR), mỗi phía dùng đúng hàm chính chủ của toolkit: | |
| fog : camera ImageAddFog (6 view, seed 2022, gray 0.1-0.5) | |
| + LiDAR fog_sim/simulate_fog (alpha .005/.01/.02/.03/.06, gamma 1e-6) | |
| snow : camera ImageAddSnow (6 view, gray 0.30, brightness 0.70) | |
| + LiDAR snow_sim_nus (pattern gunn sinh lại — xem gen_snowflakes.py; | |
| protocol-consistent, KHÔNG bit-exact với snowflakes.zip gốc) | |
| sunlight : camera sun-flare CHỈ img[0]=CAM_FRONT ("only one sun") | |
| + LiDAR scene_glare_noise: Gaussian std 2.0 trên 1-5% điểm, | |
| KHÔNG xoá điểm (paper A.1; khác sun_sim_point upstream vốn | |
| xoá điểm trong đĩa mặt trời + std 3.0 trên 0.4-2% điểm) | |
| rain : camera ImageAddRain + LiDAR rain_sim (LISA) | |
| gaussL/uniformL/impulseL : LiDAR noise Dong'23 (anh giu nguyen); jitterL : xyz jitter (proposal_pipeline) | |
| Severity 1-5 KHAI BÁO TƯỜNG MINH trong tên run (MoME/PFS không ghi rõ severity). | |
| Seed: cố định theo (base_seed, chỉ số mẫu) ⇒ hai model được so trên CÙNG | |
| corruption đúng từng pixel/điểm. | |
| """ | |
| import os | |
| import sys | |
| import numpy as np | |
| import scipy.integrate as _si | |
| import torch | |
| from mmdet3d.datasets.builder import PIPELINES | |
| if not hasattr(_si, 'trapz'): # PyMieScatt (LISA rain) dùng API cũ | |
| _si.trapz = _si.trapezoid | |
| _TOOLKIT = os.path.join(os.path.dirname(os.path.abspath(__file__)), | |
| '..', '3D_Corruptions_AD') | |
| _TOOLKIT = os.path.normpath(_TOOLKIT) | |
| for _p in (_TOOLKIT, os.path.join(_TOOLKIT, 'utils')): | |
| if _p not in sys.path: | |
| sys.path.insert(0, _p) | |
| OBJ_CELLS = ('moveobj', 'densityO', 'cutoutO', 'gaussO', 'uniformO', 'impulseO', 'shear', 'scale', 'rotation') | |
| class NuScenesCCorruption(object): | |
| def __init__(self, corruption='none', severity=2, seed=2022, lidar=True, | |
| sun_mode='official', ann_file=None): | |
| # sun_mode='official' (MẶC ĐỊNH, đúng recipe thu-ml/3D_Corruptions_AD): | |
| # README của họ ghi rõ "# only one sun": chỉ img[0] (CAM_FRONT) được | |
| # thêm flare, points chỉ bị corrupt MỘT lần bằng lidar2img[0]. | |
| # 'points_once'/'per_view' chỉ giữ để đo độ nhạy (KHÔNG dùng báo cáo). | |
| self.sun_mode = sun_mode | |
| self._sun_seeded = False # code gốc seed MỘT LẦN lúc khởi tạo, | |
| # RNG chạy tiếp qua các frame => vị trí mặt | |
| # trời đổi theo frame (seed lại mỗi sample | |
| # sẽ ghim mặt trời một chỗ = nặng hơn thật) | |
| self.corruption = corruption | |
| self.severity = int(severity) | |
| self.seed = int(seed) | |
| self.lidar = lidar | |
| self._idx = 0 | |
| # ann_file: can cho cac cell theo doi tuong (moveobj, *O, shear/scale/rotation). Test pipeline cua mmdet3d | |
| # KHONG nap annotation, nen doc thang GT tu infos pkl va tra theo sample token. | |
| self.ann_file = ann_file | |
| self._gt_cache = None | |
| # ---------- helpers ---------- | |
| def _to_rgb_uint8(img): | |
| return np.ascontiguousarray(np.clip(img, 0, 255).astype(np.uint8)[..., ::-1]) | |
| def _to_bgr_like(img_rgb_uint8, ref): | |
| out = np.ascontiguousarray(img_rgb_uint8[..., ::-1]) | |
| return out.astype(ref.dtype) | |
| # ---------- GT boxes (cho cac cell theo doi tuong) ---------- | |
| def _gt_boxes(self, results): | |
| """(N,7) [x,y,z,dx,dy,dz,yaw] he LiDAR, lay theo sample token tu infos pkl.""" | |
| if self.ann_file is None: | |
| return None | |
| if self._gt_cache is None: | |
| import pickle | |
| with open(self.ann_file, 'rb') as f: | |
| d = pickle.load(f) | |
| infos = d['infos'] if isinstance(d, dict) else d | |
| self._gt_cache = {i['token']: np.asarray(i['gt_boxes'], np.float32) for i in infos} | |
| tok = results.get('sample_idx', None) | |
| g = self._gt_cache.get(tok, None) | |
| return g if (g is not None and len(g)) else None | |
| def _in_box(pts, box): | |
| """Mat na diem nam trong mot box (cung quy uoc voi bbox_util.check_point_in_box).""" | |
| sx, sy, sz = pts[:, 0] - box[0], pts[:, 1] - box[1], pts[:, 2] - box[2] | |
| ca, sa = np.cos(box[6]), np.sin(box[6]) | |
| lx = sx * ca + sy * sa | |
| ly = sy * ca - sx * sa | |
| return (np.abs(sz) <= box[5] / 2.0) & (np.abs(lx) <= box[3] / 2.0) & (np.abs(ly) <= box[4] / 2.0) | |
| def _pick_bbox(cor, severity, boxes, pointcloud): | |
| """Ban vong lap cua toolkit (`utils.bbox_util.pick_bbox`) voi 3 loi da sua. | |
| Ban phat hanh cua thu-ml/3D_Corruptions_AD khong chay duoc 6 trong 9 ho: | |
| 1. `if 'bbox' in cor` dung chung cho moi ten, nhung chi scale/shear/rotation nhan | |
| them tham so gt_boxes; 5 ho Local la ham 2 tham so -> TypeError. | |
| 2. `moving_noise_bbox` truyen cor='move_bbox' trong khi MAP dung khoa | |
| 'moving_noise_bbox' -> KeyError. | |
| 3. `np.append(pcd_2, pcd_1)` loi khi khong con diem nao ngoai hop. | |
| Thuat toan (chon diem trong tung hop, bien doi, ghep lai) giu nguyen; chi vector hoa | |
| phan chon diem cho nhanh. | |
| """ | |
| from utils import bbox_util | |
| MAP = {'density_dec_bbox': bbox_util.density, 'cutout_bbox': bbox_util.cutout, | |
| 'gaussian_noise_bbox': bbox_util.gaussian, 'uniform_noise_bbox': bbox_util.uniform, | |
| 'impulse_noise_bbox': bbox_util.impulse, 'scale_bbox': bbox_util.scale, | |
| 'shear_bbox': bbox_util.shear, 'rotation_bbox': bbox_util.rotation, | |
| 'moving_noise_bbox': bbox_util.moving_object} | |
| NEED_GT = ('scale_bbox', 'shear_bbox', 'rotation_bbox') | |
| fn = MAP[cor] | |
| xyz = np.asarray(pointcloud, np.float64) | |
| for box in boxes: | |
| gt = [float(v) for v in box[:7]] | |
| sx, sy, sz = xyz[:, 0] - gt[0], xyz[:, 1] - gt[1], xyz[:, 2] - gt[2] | |
| ca, sa = np.cos(gt[6]), np.sin(gt[6]) | |
| inside = (np.abs(sz) <= gt[5] / 2.0) & (np.abs(sx * ca + sy * sa) <= gt[3] / 2.0) \ | |
| & (np.abs(sy * ca - sx * sa) <= gt[4] / 2.0) | |
| if not inside.any(): | |
| continue | |
| pcd_2 = np.array(xyz[inside], np.float64, copy=True) | |
| pcd_1 = xyz[~inside] | |
| pcd_2 = np.asarray(fn(pcd_2, severity, gt) if cor in NEED_GT else fn(pcd_2, severity), np.float64) | |
| xyz = np.append(pcd_2, pcd_1, axis=0) if len(pcd_1) else pcd_2 | |
| return xyz | |
| def _rng_seed(self, results): | |
| tok = results.get('sample_idx') or results.get('pts_filename', '') | |
| return (self.seed + abs(hash(str(tok))) % 100000) % (2 ** 31 - 1) | |
| # ---------- main ---------- | |
| def __call__(self, results): | |
| if self.corruption in ('none', None): | |
| return results | |
| if self.corruption == 'sunlight': | |
| if not self._sun_seeded: # đúng CorruptionMethods.__init__ | |
| np.random.seed(self.seed) | |
| self._sun_seeded = True | |
| seed = None | |
| else: | |
| seed = self.seed if self.seed == 2022 else self._rng_seed(results) | |
| np.random.seed(seed) | |
| imgs = results['img'] | |
| pts = results['points'].tensor.numpy().copy() | |
| if self.corruption == 'sunlight': | |
| from Camera_corruptions import ImagePointAddSun | |
| sun = ImagePointAddSun(self.severity) | |
| l2i = results['lidar2img'] | |
| out = [] | |
| for i, im in enumerate(imgs): | |
| if i == 0: # "only one sun" | |
| rgb = self._to_rgb_uint8(im) | |
| l2i_t = torch.tensor(np.asarray(l2i[0])).float() | |
| rgb_aug = sun.sun_sim_img(rgb, l2i_t, self.severity, | |
| False, '', {}) | |
| out.append(self._to_bgr_like(rgb_aug, im)) | |
| else: | |
| out.append(im) | |
| results['img'] = out | |
| if self.lidar: | |
| # Paper A.1: Gaussian std 2.0 trên 1..5% điểm, không xoá điểm | |
| # (= scene_glare_noise của toolkit, áp lên toạ độ xyz). | |
| ratio = [0.01, 0.02, 0.03, 0.04, 0.05][self.severity - 1] | |
| n = pts.shape[0] | |
| k = int(n * ratio) | |
| idx = np.random.choice(n, k, replace=False) | |
| pts[idx, :3] += np.random.normal(size=(k, 3)).astype(np.float32) * 2.0 | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'fog': | |
| from Camera_corruptions import ImageAddFog | |
| cam = ImageAddFog(self.severity, seed) | |
| results['img'] = [self._to_bgr_like(cam(self._to_rgb_uint8(im)), im) | |
| for im in imgs] | |
| if self.lidar: | |
| try: | |
| from utils.fog_sim import simulate_fog, ParameterSet | |
| alpha = [0.005, 0.01, 0.02, 0.03, 0.06][self.severity - 1] | |
| ps = ParameterSet(alpha=alpha, gamma=0.000001) | |
| p_out, _, _ = simulate_fog(ps, pts[:, :4].astype(np.float32), 1) | |
| pts = self._merge_xyzi(pts, np.asarray(p_out)) | |
| except Exception as e: # noqa: BLE001 | |
| results['_corruption_note'] = f'lidar fog skipped: {e}' | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'snow': | |
| from Camera_corruptions import ImageAddSnow | |
| cam = ImageAddSnow(self.severity, seed) | |
| results['img'] = [self._to_bgr_like(cam(self._to_rgb_uint8(im)), im) | |
| for im in imgs] | |
| if self.lidar: | |
| cache = os.environ.get( | |
| 'SNOW_CACHE', | |
| '/data2/Medical/MoMe/KAVI/workspace/robust_fusion_workspace/' | |
| 'experiments/full_track/snow_cache') | |
| key = os.path.basename(results.get('pts_filename', '')) | |
| cf = os.path.join(cache, f'{key}_sev{self.severity}.npy') | |
| if os.path.exists(cf): | |
| # precompute cache: identical corruption cho mọi model | |
| pts = self._merge_xyzi(pts, np.load(cf).astype(np.float32)) | |
| else: | |
| try: | |
| pts = self._merge_xyzi(pts, self._snow_lidar( | |
| pts[:, :5].astype(np.float32), self.severity)) | |
| except Exception as e: # noqa: BLE001 | |
| results['_corruption_note'] = f'lidar snow skipped: {e}' | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'rain': | |
| from Camera_corruptions import ImageAddRain | |
| cam = ImageAddRain(self.severity, seed) | |
| results['img'] = [self._to_bgr_like(cam(self._to_rgb_uint8(im)), im) | |
| for im in imgs] | |
| if self.lidar: | |
| try: | |
| from LiDAR_corruptions import rain_sim | |
| pts = self._merge_xyzi(pts, np.asarray( | |
| rain_sim(pts[:, :4].astype(np.float32), self.severity))) | |
| except Exception as e: # noqa: BLE001 | |
| results['_corruption_note'] = f'lidar rain skipped: {e}' | |
| self._set_points(results, pts) | |
| return results | |
| # ---------- PARTIAL-CORRUPTION CELLS (bổ sung 2026-08-28) ---------- | |
| # Camera-only, recipe verbatim theo consumer chính chủ | |
| # TransFusion/mmdet3d/datasets/pipelines/test_time_aug.py của thu-ml: | |
| # object tạo MỘT LẦN (seed 2022 cho gauss/impulse, iaa RNG chạy tiếp | |
| # qua frame — cùng kỷ luật với sunlight), áp per-view 6 camera. | |
| if self.corruption in ('gauss', 'impulse', 'uniform'): | |
| if not hasattr(self, '_cam_noise'): | |
| from Camera_corruptions import (ImageAddGaussianNoise, | |
| ImageAddImpulseNoise, | |
| ImageAddUniformNoise) | |
| self._cam_noise = { | |
| 'gauss': ImageAddGaussianNoise(self.severity, seed=2022), | |
| 'impulse': ImageAddImpulseNoise(self.severity, seed=2022), | |
| 'uniform': ImageAddUniformNoise(self.severity), | |
| }[self.corruption] | |
| results['img'] = [ | |
| self._to_bgr_like(self._cam_noise(image=self._to_rgb_uint8(im)), im) | |
| for im in imgs] | |
| return results | |
| if self.corruption == 'motion': | |
| # Verbatim test_time_aug L249-253 + L563: upstream ĐẢO TÊN biến | |
| # (motion_sim_frontback = ImageMotionBlurLeftRight và ngược lại); | |
| # ta chép đúng hành vi: CAM_FRONT/CAM_BACK (i%3==0) nhận | |
| # ImageMotionBlurLeftRight, 4 camera chéo nhận FrontBack. | |
| if not hasattr(self, '_mb_fb'): | |
| from Camera_corruptions import (ImageMotionBlurFrontBack, | |
| ImageMotionBlurLeftRight) | |
| self._mb_fb = ImageMotionBlurLeftRight(self.severity) | |
| self._mb_lr = ImageMotionBlurFrontBack(self.severity) | |
| out = [] | |
| for i, im in enumerate(imgs): | |
| obj = self._mb_fb if i % 3 == 0 else self._mb_lr | |
| out.append(self._to_bgr_like(obj(image=self._to_rgb_uint8(im)), im)) | |
| results['img'] = out | |
| return results | |
| # LiDAR-only partial cells: hàm chính chủ LiDAR_corruptions.py, nhận | |
| # (N,C) numpy + severity 1..5. Khác upstream DUY NHẤT: seed np.random | |
| # theo (seed, sample) để hai model nhận corruption giống hệt nhau | |
| # (upstream chạy offline 1 process nên phụ thuộc thứ tự frame); | |
| # và bỏ dòng debug np.save path cứng của tác giả (crosstalk). | |
| if self.corruption in ('crosstalk', 'density', 'cutout', 'gaussL', 'uniformL', 'impulseL'): | |
| # gaussL/uniformL/impulseL (09/9): sensor-level LiDAR noise Dong'23 (Gaussian/Uniform/Impulse (L)) — cung ham va cung cach seed | |
| # nhu kitti_c/code/kitti_c_transform.py; day la 3 dong nuScenes-C con trong ('--') trong bang RoboFusion-format. | |
| from LiDAR_corruptions import (lidar_crosstalk_noise, density_dec_global, cutout_local, | |
| gaussian_noise, uniform_noise, impulse_noise) | |
| np.random.seed(self._rng_seed(results)) | |
| fn = {'crosstalk': lidar_crosstalk_noise, | |
| 'density': density_dec_global, | |
| 'cutout': cutout_local, | |
| 'gaussL': gaussian_noise, 'uniformL': uniform_noise, 'impulseL': impulse_noise}[self.corruption] | |
| pts = np.asarray(fn(pts, self.severity), dtype=np.float32) | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'jitterL': | |
| # xyz jitter (= LiDARCorruption 'motion_blur' cua proposal_pipeline, std {1:.05,2:.15,3:.30} m; 4/5 ngoai suy .45/.60): | |
| # cell duy nhat ma ablation kernel N2/N3 (nuScenes mini) cho thay heavy-tailed gate thang; chua tung do o full scale. | |
| std = {1: 0.05, 2: 0.15, 3: 0.30, 4: 0.45, 5: 0.60}[int(self.severity)] | |
| rng = np.random.RandomState(self._rng_seed(results)) | |
| pts = np.array(pts, dtype=np.float32, copy=True); pts[:, :3] += rng.normal(0.0, std, size=(pts.shape[0], 3)).astype(np.float32) | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'fovlost': | |
| # FOV Lost: giu lai mot phan goc phuong vi (fov_filter cua toolkit). Chi LiDAR. | |
| from LiDAR_corruptions import fov_filter | |
| pts = np.asarray(fov_filter(pts, self.severity), dtype=np.float32) | |
| if pts.shape[0] == 0: pts = np.zeros((1, results['points'].tensor.shape[1]), np.float32) | |
| self._set_points(results, pts) | |
| return results | |
| if self.corruption == 'spatial': | |
| # Spatial misalignment: nhieu vao ma tran lidar2img (ngoai lai camera), anh va points giu nguyen. | |
| from LiDAR_corruptions import spatial_alignment_noise | |
| rng = np.random.RandomState(self._rng_seed(results)); st = np.random.get_state(); np.random.set_state(rng.get_state()) | |
| try: | |
| # Nhieu vao NGOAI LAI lidar2cam (dung nghia sai lech lap dat camera). KHONG nhieu lidar2img vi | |
| # ResizeCropFlipImage dung lai lidar2img = cam_intrinsic @ lidar2cam o buoc sau => se ghi de. | |
| done = False | |
| for k in ('lidar2cam', 'extrinsics', 'lidar2img'): | |
| v = results.get(k, None) | |
| if v is None: continue | |
| results[k] = [spatial_alignment_noise(np.array(m, np.float64, copy=True), self.severity) for m in v] | |
| done = True | |
| if k != 'lidar2img': break | |
| if not done: results['_corruption_note'] = 'spatial: khong thay lidar2cam/extrinsics/lidar2img' | |
| finally: | |
| np.random.set_state(st) | |
| return results | |
| if self.corruption in OBJ_CELLS: | |
| # BAN CHINH CHU: theo dung `CorruptionMethods` cua thu-ml/3D_Corruptions_AD | |
| # (TransFusion/mmdet3d/datasets/pipelines/test_time_aug.py). Moi cell doi tuong co | |
| # HAI nhanh: nhanh ANH (ImageBBoxOperation / ImageBBoxMotionBlur*) va nhanh LiDAR (*_bbox). | |
| # Cac cell Local chi co nhanh LiDAR. Goi y nguyen chu ky cua ho: fn(points[:, :4], severity, [boxes]). | |
| gt = self._gt_boxes(results) | |
| if gt is None or len(gt) == 0: | |
| results['_corruption_note'] = 'no GT boxes -> bo qua' | |
| return results | |
| import torch | |
| sev = int(self.severity) | |
| np.random.seed(self._rng_seed(results)) | |
| # ---- nhanh ANH (chi 4 cell: moveobj / shear / scale / rotation) ---- | |
| if self.corruption in ('moveobj', 'shear', 'scale', 'rotation') and results.get('img', None) is not None: | |
| from mmdet3d.core.bbox import LiDARInstance3DBoxes | |
| boxes = LiDARInstance3DBoxes(torch.as_tensor(np.asarray(gt[:, :7], np.float32)), box_dim=7, origin=(0.5, 0.5, 0.5)) | |
| bboxes_corners, bboxes_centers = boxes.corners, boxes.center | |
| l2i = results['lidar2img']; imgs = results['img'] | |
| if self.corruption == 'moveobj': | |
| from Camera_corruptions import ImageBBoxMotionBlurFrontBack, ImageBBoxMotionBlurLeftRight | |
| fb = ImageBBoxMotionBlurFrontBack(severity=sev, corrput_list=[0.02 * i for i in range(1, 6)]) | |
| lr = ImageBBoxMotionBlurLeftRight(severity=sev, corrput_list=[0.02 * i for i in range(1, 6)]) | |
| out = [] | |
| for i, im in enumerate(imgs): | |
| op = fb if (i % 3 == 0) else lr | |
| out.append(self._to_bgr_like(op(image=self._to_rgb_uint8(im), lidar2img=np.asarray(l2i[i]), | |
| bboxes_centers=bboxes_centers, bboxes_corners=bboxes_corners), im)) | |
| results['img'] = out | |
| else: | |
| from Camera_corruptions import ImageBBoxOperation | |
| if self.corruption == 'shear': | |
| c = [0.05, 0.1, 0.15, 0.2, 0.25][sev - 1] | |
| bb, dd, ee, ff = [np.random.uniform(c - 0.05, c + 0.05) * np.random.choice([-1, 1]) for _ in range(4)] | |
| T = torch.tensor([[1.0, 0.0, bb], [dd, 1.0, ee], [ff, 0.0, 1.0]]).float() | |
| elif self.corruption == 'rotation': | |
| base = [4, 8, 12, 16, 20][sev - 1] | |
| th = np.random.uniform(base - 2, base + 2) * np.random.choice([-1, 1]) / 180.0 * np.pi | |
| ct, st = np.cos(th), np.sin(th) | |
| T = torch.tensor([[ct, st, 0.0], [-st, ct, 0.0], [0.0, 0.0, 1.0]]).float() | |
| else: # scale | |
| c = [0.1, 0.2, 0.3, 0.4, 0.5][sev - 1]; t = np.random.choice([-1, 1]) | |
| a_ = b_ = d_ = 1.0 + c * t | |
| T = torch.tensor([[a_, 0.0, 0.0], [0.0, b_, 0.0], [0.0, 0.0, d_]]).float() | |
| op = ImageBBoxOperation(sev) | |
| results['img'] = [self._to_bgr_like(op(image=self._to_rgb_uint8(im), lidar2img=np.asarray(l2i[i]), | |
| transform_matrix=T, bboxes_centers=bboxes_centers, | |
| bboxes_corners=bboxes_corners, is_nus=True), im) | |
| for i, im in enumerate(imgs)] | |
| # ---- nhanh LiDAR (moi cell) ---- | |
| if self.lidar: | |
| COR = {'moveobj': 'moving_noise_bbox', 'densityO': 'density_dec_bbox', 'cutoutO': 'cutout_bbox', | |
| 'gaussO': 'gaussian_noise_bbox', 'uniformO': 'uniform_noise_bbox', | |
| 'impulseO': 'impulse_noise_bbox', 'shear': 'shear_bbox', 'scale': 'scale_bbox', | |
| 'rotation': 'rotation_bbox'}[self.corruption] | |
| src = np.ascontiguousarray(pts[:, :4], np.float64) | |
| try: | |
| aug = np.asarray(self._pick_bbox(COR, sev, np.asarray(gt[:, :7], np.float64), src), np.float32) | |
| assert aug.ndim == 2 and aug.shape[1] >= 3 and np.isfinite(aug).all() | |
| out = np.array(pts, np.float32, copy=True) | |
| if aug.shape[0] == out.shape[0]: | |
| out[:, :min(4, out.shape[1])] = aug[:, :min(4, out.shape[1])] | |
| else: # bot diem (density/cutout): cot con lai dat 0 | |
| out = np.zeros((aug.shape[0], pts.shape[1]), np.float32) | |
| out[:, :min(aug.shape[1], pts.shape[1])] = aug[:, :min(aug.shape[1], pts.shape[1])] | |
| if out.shape[0] == 0: out = np.zeros((1, pts.shape[1]), np.float32) | |
| self._set_points(results, np.ascontiguousarray(out, np.float32)) | |
| except Exception as e: | |
| results['_corruption_note'] = f'{self.corruption} lidar loi: {type(e).__name__}: {e}' | |
| return results | |
| raise ValueError(f'unknown corruption {self.corruption}') | |
| def _snow_lidar(self, pts5, severity): | |
| """snow_sim_nus với pattern sinh lại tại 3D_Corruptions_AD/snow_npy. | |
| Gọi thẳng utils.snowfall.simulation.augment (bỏ wet-ground vì | |
| ground_water_augmentation cần calib DENSE không áp dụng cho nuScenes; | |
| chính snow_sim_nus upstream cũng chỉ chạy được trên STF đầy đủ).""" | |
| import sys | |
| npy_dir = os.path.join(_TOOLKIT, 'snow_npy') | |
| # simulation.py import 'lib.OpenPCDet...' (stub đã tạo) và | |
| # 'tools.snowfall/...' (symlink tools -> utils) => cần _TOOLKIT ĐỨNG ĐẦU | |
| # sys.path để không nhặt nhầm tools/ của repo MoME (cwd). | |
| if _TOOLKIT in sys.path: | |
| sys.path.remove(_TOOLKIT) | |
| sys.path.insert(0, _TOOLKIT) | |
| for bad in ('tools', 'tools.snowfall', 'tools.wet_ground'): | |
| m = sys.modules.get(bad) | |
| if m is not None and _TOOLKIT not in str(getattr(m, '__file__', '') or getattr(m, '__path__', [''])[0]): | |
| del sys.modules[bad] | |
| # augmentation.py có fallback `except ModuleNotFoundError` nhưng nhánh | |
| # đầu `from utils import plot_2d_hist` lại raise ImportError thường | |
| # (utils/ là package rỗng của ta) -> pre-import 3 module wet_ground | |
| # theo đường tools.* để nhánh fallback không cần chạy. | |
| import utils as _u # package $_TOOLKIT/utils | |
| from tools.wet_ground import utils as _wgu, planes as _wgp, phy_equations as _wgq | |
| _u.plot_2d_hist = _wgu.plot_2d_hist | |
| sys.modules.setdefault('planes', _wgp) | |
| sys.modules.setdefault('phy_equations', _wgq) | |
| from tools.snowfall import simulation as _snow_sim | |
| prefix = [ | |
| 'gunn_2.621627143512277_7.716049382716048e-07', | |
| 'gunn_42.730958596843955_4.9603174603174595e-06', | |
| 'gunn_200.20719573938692_1.3888888888888886e-05', | |
| 'gunn_367.80410429625914_2.083333333333333e-05', | |
| 'gunn_791.3884281145265_3.4722222222222215e-05', | |
| ][severity - 1] | |
| _, pc = _snow_sim.augment( | |
| pc=pts5, only_camera_fov=False, | |
| particle_file_prefix=os.path.join(npy_dir, prefix), | |
| noise_floor=0.7, | |
| beam_divergence=float(np.degrees(0.003)), | |
| shuffle=True, show_progressbar=False) | |
| return np.asarray(pc) | |
| def _merge_xyzi(orig, new): | |
| """LiDAR sim có thể đổi SỐ điểm -> dựng lại mảng (N,C) giữ cột phụ (time).""" | |
| new = np.asarray(new, dtype=np.float32) | |
| c = orig.shape[1] | |
| if new.shape[0] == orig.shape[0]: | |
| out = orig.copy() | |
| out[:, :min(4, new.shape[1])] = new[:, :min(4, new.shape[1])] | |
| return out | |
| out = np.zeros((new.shape[0], c), dtype=np.float32) | |
| out[:, :min(4, new.shape[1])] = new[:, :min(4, new.shape[1])] | |
| return out | |
| def _set_points(results, pts): | |
| p = results['points'] | |
| results['points'] = type(p)(torch.from_numpy(np.ascontiguousarray(pts)).float(), | |
| points_dim=pts.shape[1], | |
| attribute_dims=getattr(p, 'attribute_dims', None)) | |
| def __repr__(self): | |
| return (f'{self.__class__.__name__}(corruption={self.corruption}, ' | |
| f'severity={self.severity}, seed={self.seed}, lidar={self.lidar})') | |