Commit 41b18fd8 authored by zhe chen's avatar zhe chen
Browse files

Use pre-commit to reformat code


Use pre-commit to reformat code
parent ff20ea39
# ---------------------------------------------
# Copyright (c) OpenMMLab. All rights reserved.
# ---------------------------------------------
# Modified by Zhiqi Li
# ---------------------------------------------
from mmcv.ops.multi_scale_deform_attn import multi_scale_deformable_attn_pytorch
import math
import warnings
import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import xavier_init, constant_init
from mmcv.cnn.bricks.registry import (ATTENTION,
TRANSFORMER_LAYER,
TRANSFORMER_LAYER_SEQUENCE)
from mmcv.cnn import constant_init, xavier_init
from mmcv.cnn.bricks.registry import ATTENTION
from mmcv.cnn.bricks.transformer import build_attention
import math
from mmcv.runner import force_fp32, auto_fp16
from mmcv.ops.multi_scale_deform_attn import \
multi_scale_deformable_attn_pytorch
from mmcv.runner import force_fp32
from mmcv.runner.base_module import BaseModule
from mmcv.utils import ext_loader
from mmcv.runner.base_module import BaseModule, ModuleList, Sequential
from .multi_scale_deformable_attn_function import \
MultiScaleDeformableAttnFunction_fp32
from mmcv.utils import ext_loader
from .multi_scale_deformable_attn_function import MultiScaleDeformableAttnFunction_fp32, \
MultiScaleDeformableAttnFunction_fp16
ext_module = ext_loader.load_ext(
'_ext', ['ms_deform_attn_backward', 'ms_deform_attn_forward'])
......@@ -149,7 +147,8 @@ class SpatialCrossAttention(BaseModule):
for i, reference_points_per_img in enumerate(reference_points_cam):
index_query_per_img = indexes[i]
queries_rebatch[j, i, :len(index_query_per_img)] = query[j, index_query_per_img]
reference_points_rebatch[j, i, :len(index_query_per_img)] = reference_points_per_img[j, index_query_per_img]
reference_points_rebatch[j, i, :len(index_query_per_img)] = reference_points_per_img[
j, index_query_per_img]
num_cams, l, bs, embed_dims = key.shape
......@@ -158,9 +157,13 @@ class SpatialCrossAttention(BaseModule):
value = value.permute(2, 0, 1, 3).reshape(
bs * self.num_cams, l, self.embed_dims)
queries = self.deformable_attention(query=queries_rebatch.view(bs*self.num_cams, max_len, self.embed_dims), key=key, value=value,
reference_points=reference_points_rebatch.view(bs*self.num_cams, max_len, D, 2), spatial_shapes=spatial_shapes,
level_start_index=level_start_index).view(bs, self.num_cams, max_len, self.embed_dims)
queries = self.deformable_attention(query=queries_rebatch.view(bs * self.num_cams, max_len, self.embed_dims),
key=key, value=value,
reference_points=reference_points_rebatch.view(bs * self.num_cams, max_len,
D, 2),
spatial_shapes=spatial_shapes,
level_start_index=level_start_index).view(bs, self.num_cams, max_len,
self.embed_dims)
for j in range(bs):
for i, index_query_per_img in enumerate(indexes):
slots[j, index_query_per_img] += queries[j, i, :len(index_query_per_img)]
......
......@@ -4,19 +4,21 @@
# Modified by Zhiqi Li
# ---------------------------------------------
from .multi_scale_deformable_attn_function import MultiScaleDeformableAttnFunction_fp32
from mmcv.ops.multi_scale_deform_attn import multi_scale_deformable_attn_pytorch
import math
import warnings
import torch
import torch.nn as nn
from mmcv.cnn import xavier_init, constant_init
from mmcv.cnn import constant_init, xavier_init
from mmcv.cnn.bricks.registry import ATTENTION
import math
from mmcv.runner.base_module import BaseModule, ModuleList, Sequential
from mmcv.utils import (ConfigDict, build_from_cfg, deprecated_api_warning,
to_2tuple)
from mmcv.ops.multi_scale_deform_attn import \
multi_scale_deformable_attn_pytorch
from mmcv.runner.base_module import BaseModule
from mmcv.utils import ext_loader
from .multi_scale_deformable_attn_function import \
MultiScaleDeformableAttnFunction_fp32
ext_module = ext_loader.load_ext(
'_ext', ['ms_deform_attn_backward', 'ms_deform_attn_forward'])
......@@ -96,9 +98,9 @@ class TemporalSelfAttention(BaseModule):
self.num_points = num_points
self.num_bev_queue = num_bev_queue
self.sampling_offsets = nn.Linear(
embed_dims*self.num_bev_queue, num_bev_queue*num_heads * num_levels * num_points * 2)
self.attention_weights = nn.Linear(embed_dims*self.num_bev_queue,
num_bev_queue*num_heads * num_levels * num_points)
embed_dims * self.num_bev_queue, num_bev_queue * num_heads * num_levels * num_points * 2)
self.attention_weights = nn.Linear(embed_dims * self.num_bev_queue,
num_bev_queue * num_heads * num_levels * num_points)
self.value_proj = nn.Linear(embed_dims, embed_dims)
self.output_proj = nn.Linear(embed_dims, embed_dims)
self.init_weights()
......@@ -113,7 +115,7 @@ class TemporalSelfAttention(BaseModule):
grid_init = (grid_init /
grid_init.abs().max(-1, keepdim=True)[0]).view(
self.num_heads, 1, 1,
2).repeat(1, self.num_levels*self.num_bev_queue, self.num_points, 1)
2).repeat(1, self.num_levels * self.num_bev_queue, self.num_points, 1)
for i in range(self.num_points):
grid_init[:, :, i, :] *= i + 1
......@@ -176,7 +178,7 @@ class TemporalSelfAttention(BaseModule):
if value is None:
assert self.batch_first
bs, len_bev, c = query.shape
value = torch.stack([query, query], 1).reshape(bs*2, len_bev, c)
value = torch.stack([query, query], 1).reshape(bs * 2, len_bev, c)
# value = torch.cat([query, query], 0)
......@@ -203,7 +205,7 @@ class TemporalSelfAttention(BaseModule):
if key_padding_mask is not None:
value = value.masked_fill(key_padding_mask[..., None], 0.0)
value = value.reshape(bs*self.num_bev_queue,
value = value.reshape(bs * self.num_bev_queue,
num_value, self.num_heads, -1)
sampling_offsets = self.sampling_offsets(query)
......@@ -219,10 +221,10 @@ class TemporalSelfAttention(BaseModule):
self.num_levels,
self.num_points)
attention_weights = attention_weights.permute(0, 3, 1, 2, 4, 5)\
.reshape(bs*self.num_bev_queue, num_query, self.num_heads, self.num_levels, self.num_points).contiguous()
sampling_offsets = sampling_offsets.permute(0, 3, 1, 2, 4, 5, 6)\
.reshape(bs*self.num_bev_queue, num_query, self.num_heads, self.num_levels, self.num_points, 2)
attention_weights = attention_weights.permute(0, 3, 1, 2, 4, 5) \
.reshape(bs * self.num_bev_queue, num_query, self.num_heads, self.num_levels, self.num_points).contiguous()
sampling_offsets = sampling_offsets.permute(0, 3, 1, 2, 4, 5, 6) \
.reshape(bs * self.num_bev_queue, num_query, self.num_heads, self.num_levels, self.num_points, 2)
if reference_points.shape[-1] == 2:
offset_normalizer = torch.stack(
......
......@@ -4,22 +4,17 @@
# Modified by Zhiqi Li
# ---------------------------------------------
import numpy as np
import torch
import torch.nn as nn
from mmcv.cnn import xavier_init
from mmcv.cnn.bricks.transformer import build_transformer_layer_sequence
from mmcv.runner import auto_fp16
from mmcv.runner.base_module import BaseModule
from mmdet.models.utils.builder import TRANSFORMER
from torch.nn.init import normal_
from mmcv.runner.base_module import BaseModule
from torchvision.transforms.functional import rotate
from .temporal_self_attention import TemporalSelfAttention
from .spatial_cross_attention import MSDeformableAttention3D
from .decoder import CustomMSDeformableAttention
from mmcv.runner import force_fp32, auto_fp16
import pdb
from .spatial_cross_attention import MSDeformableAttention3D
from .temporal_self_attention import TemporalSelfAttention
@TRANSFORMER.register_module()
......@@ -62,7 +57,6 @@ class PerceptionTransformer(BaseModule):
m.init_weights()
xavier_init(self.reference_points, distribution='uniform', bias=0.)
@auto_fp16(apply_to=('mlvl_feats', 'bev_queries', 'object_query_embed', 'prev_bev', 'bev_pos'))
def forward(self,
mlvl_feats,
......
from .custom_fpn import *
from .custom_ipm_view_transformer import *
......@@ -22,7 +22,6 @@
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import ConvModule
from mmcv.runner import BaseModule
from mmdet3d.models import NECKS
......
......@@ -20,12 +20,9 @@
# limitations under the License.
# ==============================================================================
import copy
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.runner import BaseModule
from mmdet3d.models import NECKS
......@@ -63,7 +60,7 @@ def get_campos(reference_points, ego2cam, img_shape):
eps = 1e-9
mask = (reference_points_cam[..., 2:3] > eps)
reference_points_cam =\
reference_points_cam = \
reference_points_cam[..., 0:2] / \
reference_points_cam[..., 2:3] + eps
......@@ -80,10 +77,11 @@ def get_campos(reference_points, ego2cam, img_shape):
# (B, num_cam, num_query)
mask = mask.view(B, num_cam, num_query)
reference_points_cam = reference_points_cam.view(B*num_cam, num_query, 2)
reference_points_cam = reference_points_cam.view(B * num_cam, num_query, 2)
return reference_points_cam, mask
def construct_plane_grid(xbound, ybound, height: float, dtype=torch.float32):
'''
Returns:
......@@ -108,6 +106,7 @@ def construct_plane_grid(xbound, ybound, height: float, dtype=torch.float32):
return plane
@NECKS.register_module()
class CustomIPMViewTransformer(BaseModule):
r"""
......@@ -116,6 +115,7 @@ class CustomIPMViewTransformer(BaseModule):
Adapted from https://github.com/Mrmoore98/VectorMapNet_code/blob/mian/plugin/models/backbones/ipm_backbone.py#L238.
"""
def __init__(self,
num_cam,
xbound,
......@@ -126,20 +126,20 @@ class CustomIPMViewTransformer(BaseModule):
super().__init__()
self.x_bound = xbound
self.y_bound = ybound
heights = [zbound[0]+i*zbound[2] for i in range(int((zbound[1]-zbound[0])//zbound[2])+1)]
heights = [zbound[0] + i * zbound[2] for i in range(int((zbound[1] - zbound[0]) // zbound[2]) + 1)]
self.heights = heights
self.num_cam = num_cam
self.outconvs =\
nn.Conv2d((out_channels+3)*len(heights), out_channels,
self.outconvs = \
nn.Conv2d((out_channels + 3) * len(heights), out_channels,
kernel_size=3, stride=1, padding=1) # same
# bev_plane
bev_planes = [construct_plane_grid(
xbound, ybound, h) for h in self.heights]
self.register_buffer('bev_planes', torch.stack(
bev_planes),) # nlvl,bH,bW,2
bev_planes), ) # nlvl,bH,bW,2
def forward(self, cam_feat, ego2cam, img_shape):
'''
......@@ -161,7 +161,7 @@ class CustomIPMViewTransformer(BaseModule):
# bev_grid_pos: B*ncam, nlvl*bH*bW, 2
bev_grid_pos, bev_cam_mask = get_campos(bev_grid, ego2cam, img_shape)
# B*cam, nlvl*bH, bW, 2
bev_grid_pos = bev_grid_pos.unflatten(-2, (nlvl*bH, bW))
bev_grid_pos = bev_grid_pos.unflatten(-2, (nlvl * bH, bW))
# project feat from 2D to bev plane
projected_feature = F.grid_sample(
......@@ -173,7 +173,7 @@ class CustomIPMViewTransformer(BaseModule):
# eliminate the ncam
# The bev feature is the sum of the 6 cameras
bev_feat_mask = bev_feat_mask.unsqueeze(2)
projected_feature = (projected_feature*bev_feat_mask).sum(1)
projected_feature = (projected_feature * bev_feat_mask).sum(1)
num_feat = bev_feat_mask.sum(1)
projected_feature = projected_feature / \
......
......@@ -19,7 +19,7 @@ model = dict(
init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet18')),
img_neck=dict(
type='CustomFPN',
in_channels=[_dim_*2, _dim_*4],
in_channels=[_dim_ * 2, _dim_ * 4],
out_channels=_dim_,
num_outs=1,
start_level=0,
......@@ -38,7 +38,7 @@ model = dict(
num_query=50,
object_type='lane',
num_layers=1,
num_reg_dim=method_para['n_control']*3,
num_reg_dim=method_para['n_control'] * 3,
loss_cls=dict(
type='FocalLoss',
use_sigmoid=True,
......
......@@ -18,14 +18,13 @@ input_modality = dict(
use_external=False)
num_cams = 7
Map_size = [(-50, 50), (-25, 25)]
method_para = dict(n_control=5) # #point for each curve
code_size = 3 * method_para['n_control']
_dim_ = 256
_pos_dim_ = _dim_//2
_ffn_dim_ = _dim_*2
_pos_dim_ = _dim_ // 2
_ffn_dim_ = _dim_ * 2
_ffn_cfg_ = dict(
type='FFN',
embed_dims=_dim_,
......@@ -71,7 +70,7 @@ model = dict(
pc_range=point_cloud_range,
bev_h=bev_h_,
bev_w=bev_w_,
rotate_center=[bev_h_//2, bev_w_//2],
rotate_center=[bev_h_ // 2, bev_w_ // 2],
encoder=dict(
type='BEVFormerEncoder',
num_layers=3,
......@@ -169,7 +168,7 @@ model = dict(
with_box_refine=False,
with_shared_param=False,
code_size=code_size,
code_weights= [1.0 for i in range(code_size)],
code_weights=[1.0 for i in range(code_size)],
pc_range=point_cloud_range,
transformer=dict(
type='PerceptionTransformer',
......@@ -243,7 +242,6 @@ model = dict(
iou_cost=dict(type='IoUCost', weight=0.0), # Fake cost. This is just to make it compatible with DETR head.
))))
train_pipeline = [
dict(type='CustomLoadMultiViewImageFromFiles', to_float32=True),
dict(type='NormalizeMultiviewImage', **img_norm_cfg),
......
......@@ -18,14 +18,13 @@ input_modality = dict(
use_external=False)
num_cams = 7
Map_size = [(-50, 50), (-25, 25)]
method_para = dict(n_control=5) # #point for each curve
code_size = 3 * method_para['n_control']
_dim_ = 256
_pos_dim_ = _dim_//2
_ffn_dim_ = _dim_*2
_pos_dim_ = _dim_ // 2
_ffn_dim_ = _dim_ * 2
_ffn_cfg_ = dict(
type='FFN',
embed_dims=_dim_,
......@@ -78,7 +77,7 @@ model = dict(
pc_range=point_cloud_range,
bev_h=bev_h_,
bev_w=bev_w_,
rotate_center=[bev_h_//2, bev_w_//2],
rotate_center=[bev_h_ // 2, bev_w_ // 2],
encoder=dict(
type='BEVFormerEncoder',
num_layers=3,
......@@ -176,7 +175,7 @@ model = dict(
with_box_refine=False,
with_shared_param=False,
code_size=code_size,
code_weights= [1.0 for i in range(code_size)],
code_weights=[1.0 for i in range(code_size)],
pc_range=point_cloud_range,
transformer=dict(
type='PerceptionTransformer',
......@@ -250,7 +249,6 @@ model = dict(
iou_cost=dict(type='IoUCost', weight=0.0), # Fake cost. This is just to make it compatible with DETR head.
))))
train_pipeline = [
dict(type='CustomLoadMultiViewImageFromFiles', to_float32=True),
dict(type='NormalizeMultiviewImage', **img_norm_cfg),
......
tqdm
ninja
chardet
iso3166
jupyter
openmim
matplotlib
ninja
numpy >=1.22.0, <1.24.0
scikit-learn
similaritymeasures
opencv-python
scipy ==1.8.0
openmim
ortools ==9.2.9972
iso3166
chardet
scikit-learn
scipy ==1.8.0
similaritymeasures
tqdm
......@@ -20,8 +20,7 @@
# limitations under the License.
# ==============================================================================
from setuptools import setup, find_packages
from setuptools import find_packages, setup
setup(
name='openlanev2',
......
......@@ -86,7 +86,7 @@ def plot_curve(log_dicts, args):
xs = []
ys = []
num_iters_per_epoch = \
log_dict[epochs[args.interval-1]]['iter'][-1]
log_dict[epochs[args.interval - 1]]['iter'][-1]
for epoch in epochs[args.interval - 1::args.interval]:
iters = log_dict[epoch]['iter']
if log_dict[epoch]['mode'][-1] == 'val':
......
......@@ -6,7 +6,6 @@ import torch
from mmcv import Config
from mmcv.parallel import MMDataParallel
from mmcv.runner import load_checkpoint, wrap_fp16_model
from mmdet3d.datasets import build_dataloader, build_dataset
from mmdet3d.models import build_detector
from tools.misc.fuse_conv_bn import fuse_module
......
......@@ -3,7 +3,6 @@ import argparse
import torch
from mmcv import Config, DictAction
from mmdet3d.models import build_model
try:
......@@ -42,7 +41,6 @@ def parse_args():
def main():
args = parse_args()
if args.modality == 'point':
......@@ -52,7 +50,7 @@ def main():
if len(args.shape) == 1:
input_shape = (3, args.shape[0], args.shape[0])
elif len(args.shape) == 2:
input_shape = (3, ) + tuple(args.shape)
input_shape = (3,) + tuple(args.shape)
else:
raise ValueError('invalid input shape')
elif args.modality == 'multi':
......
......@@ -6,12 +6,11 @@ import mmcv
import numpy as np
from mmcv import track_iter_progress
from mmcv.ops import roi_align
from pycocotools import mask as maskUtils
from pycocotools.coco import COCO
from mmdet3d.core.bbox import box_np_ops as box_np_ops
from mmdet3d.datasets import build_dataset
from mmdet.core.evaluation.bbox_overlaps import bbox_overlaps
from pycocotools import mask as maskUtils
from pycocotools.coco import COCO
def _poly2mask(mask_ann, img_h, img_w):
......
......@@ -3,7 +3,6 @@ import os
import mmcv
import numpy as np
from tools.data_converter.s3dis_data_utils import S3DISData, S3DISSegData
from tools.data_converter.scannet_data_utils import ScanNetData, ScanNetSegData
from tools.data_converter.sunrgbd_data_utils import SUNRGBDData
......
......@@ -4,9 +4,9 @@ from pathlib import Path
import mmcv
import numpy as np
from mmdet3d.core.bbox import box_np_ops, points_cam2img
from nuscenes.utils.geometry_utils import view_points
from mmdet3d.core.bbox import box_np_ops, points_cam2img
from .kitti_data_utils import WaymoInfoGatherer, get_kitti_image_info
from .nuscenes_converter import post_process_coords
......
......@@ -151,7 +151,7 @@ def get_label_anno(label_path):
if len(content) != 0 and len(content[0]) == 16: # have score
annotations['score'] = np.array([float(x[15]) for x in content])
else:
annotations['score'] = np.zeros((annotations['bbox'].shape[0], ))
annotations['score'] = np.zeros((annotations['bbox'].shape[0],))
index = list(range(num_objects)) + [-1] * (num_gt - num_objects)
annotations['index'] = np.array(index, dtype=np.int32)
annotations['group_ids'] = np.arange(num_gt, dtype=np.int32)
......@@ -547,9 +547,9 @@ def add_difficulty_to_annos(info):
occlusion = annos['occluded']
truncation = annos['truncated']
diff = []
easy_mask = np.ones((len(dims), ), dtype=np.bool)
moderate_mask = np.ones((len(dims), ), dtype=np.bool)
hard_mask = np.ones((len(dims), ), dtype=np.bool)
easy_mask = np.ones((len(dims),), dtype=np.bool)
moderate_mask = np.ones((len(dims),), dtype=np.bool)
hard_mask = np.ones((len(dims),), dtype=np.bool)
i = 0
for h, o, t in zip(height, occlusion, truncation):
if o > max_occlusion[0] or h <= min_height[0] or t > max_trunc[0]:
......
......@@ -6,9 +6,9 @@ from os import path as osp
import mmcv
import numpy as np
from lyft_dataset_sdk.lyftdataset import LyftDataset as Lyft
from mmdet3d.datasets import LyftDataset
from pyquaternion import Quaternion
from mmdet3d.datasets import LyftDataset
from .nuscenes_converter import (get_2d_boxes, get_available_scenes,
obtain_sensor2top)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment