# ====================可选, colab并不自带conda=====================
!conda create -n openmmlab -y
!conda activate openmmlab
!conda init
# ================== 一键装好环境 =======================
# 如果这里有问题可以重启一下内核: 代码执行程序 -> 重新启动代码执行程序

!python -m pip install --upgrade pip

# 注意对应机子配置: https://pytorch.org/
# install dependencies: (use cu111 because colab has CUDA 11.1)
%pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html

# windows 平台不用装了, 一定会卡在这
# https://github.com/open-mmlab/mmcv/blob/master/README_zh-CN.md
%pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html

# 与pip/conda同级的专门给mm-lab用的包管理器, 报错率很高
%pip install openmim

# 依赖 mmcv, 如果用mim装的话大概率有问题
# 后面要用到源码库的 config, 可选用源库安装
%pip install mmdet mmsegmentation
# 验证安装




    

!nvcc -V
!pip list | grep mm

from mmcv import collect_env
collect_env()

# !rm -rf /content/mmdetection
%cd /content
!git clone https://github.com/open-mmlab/mmdetection.git
%cd mmdetection
# %pip install -e .

# 后续 pwd = /content/mmdetection
# !rm -rf /content/mmdetection3d
%cd /content
!git clone https://github.com/open-mmlab/mmdetection3d.git
%cd mmdetection3d

# mmdetection3d 有一些额外的依赖需要安装
%pip install -e .

# --show 用到open3D来展示, 但只能在本机create窗口, colab 上会报错
# %pip install open3d

# 后续 pwd = /content/mmdetection3d

# mim 也可以用来search/download,不过 doc 在捉迷藏..不知道怎么用
# https://github.com/open-mmlab/mmdetection/tree/master/configs/mask_rcnn
# 跟教程一样的命令...失效了?
# !mim search mmdet --model 'mask r-cnn'
# !mim download mmdet --config mask_rcnn_r50_fpn_2x_coco --dest ./_model

%cd /content/mmdetection

# 下载并分别测试下面两个 pre-trained model-checkpoints
!mkdir checkpoints
!wget -c https://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_fpn_2x_coco/mask_rcnn_r50_fpn_2x_coco_bbox_mAP-0.392__segm_mAP-0.354_20200505_003907-3e542a40.pth \
-O checkpoints/mask_rcnn_r50_fpn_2x_coco_bbox_mAP-0.392__segm_mAP-0.354_20200505_003907-3e542a40.pth
!wget -c https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth \
-O checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth

from mmdet.apis import init_detector, inference_detector, show_result_pyplot

config = 'configs/mask_rcnn/mask_rcnn_r50_fpn_2x_coco.py'
checkpoint = 'checkpoints/mask_rcnn_r50_fpn_2x_coco_bbox_mAP-0.392__segm_mAP-0.354_20200505_003907-3e542a40.pth'

# 在 CPU 上需要设置 device='cpu' ; GPU 上设置 device='cuda:0'
# 使用 mmdetection 源库自带的 demo/demo.jpg
model = init_detector(config, checkpoint, device='cuda:0')
result = inference_detector(model, 'demo/demo.jpg')

show_result_pyplot(model, 'demo/demo.jpg', result)

import mmcv
from mmcv.runner import load_checkpoint

from mmdet.apis import inference_detector, show_result_pyplot
from mmdet.models import build_detector

# Choose to use a config and initialize the detector
config = 'configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco.py'
# Setup a checkpoint file to load
checkpoint = 'checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth'

# Set the device to be used for evaluation
device='cuda:0'

# Load the config
config = mmcv.Config.fromfile(config)
# Set pretrained to be None since we do not need pretrained model here
config.model.pretrained = None

# Initialize the detector
model = build_detector(config.model)

# Load checkpoint
checkpoint = load_checkpoint(model, checkpoint, map_location=device)

# Set the classes of models for inference
model.CLASSES = checkpoint['meta']['CLASSES']
# We need to set the model's cfg for inference
model.cfg = config
# Convert the model to GPU
model.to(device)
# Convert the model into evaluation mode
model.eval()

# Use the detector to do inference
img = 'demo/demo.jpg'

result = inference_detector(model, img)
# Let's plot the result
show_result_pyplot(model, img, result, score_thr=0.3)

%cd /content/mmdetection3d




    

!mkdir checkpoints
!wget -c https://download.openmmlab.com/mmdetection3d/v1.0.0_models/votenet/votenet_8x8_scannet-3d-18class/votenet_8x8_scannet-3d-18class_20210823_234503-cf8134fa.pth \
-O checkpoints/votenet_8x8_scannet-3d-18class_20210823_234503-cf8134fa.pth
# https://mmdetection3d.readthedocs.io/zh_CN/latest/getting_started.html#id10

from mmdet3d.apis import init_model, inference_detector

config_file = 'configs/votenet/votenet_8x8_scannet-3d-18class.py'
checkpoint_file = 'checkpoints/votenet_8x8_scannet-3d-18class_20210823_234503-cf8134fa.pth'

# 从配置文件和预训练的模型文件中构建模型
model = init_model(config_file, checkpoint_file, device='cuda:0')

# 测试单个文件并可视化结果
point_cloud = 'demo/data/scannet/scene0000_00.bin'
result, data = inference_detector(model, point_cloud)
# 可视化结果并且将结果保存到 'results' 文件夹
model.show_results(data, result, out_dir='results')


# pre-trained model 在上面下载好了, 这里只下载数据集
!wget https://download.openmmlab.com/mmdetection/data/kitti_tiny.zip
!unzip kitti_tiny.zip > /dev/null

# Let's take a look at the dataset image
import mmcv
import matplotlib.pyplot as plt

img = mmcv.imread('kitti_tiny/training/image_2/000073.jpeg')
plt.figure(figsize=(25, 20))
plt.imshow(mmcv.bgr2rgb(img))
plt.show()

# 自定义数据集格式 'KittiTinyDataset' 并注册到 mmdet
import copy
import os.path as osp

import mmcv
import numpy as np

from mmdet.datasets.builder import DATASETS
from mmdet.datasets.custom import CustomDataset

@DATASETS.register_module()
class KittiTinyDataset(CustomDataset):

CLASSES = ('Car', 'Pedestrian', 'Cyclist')

def load_annotations(self, ann_file):
cat2label = {k: i for i, k in enumerate(self.CLASSES)}
# load image list from file
image_list = mmcv.list_from_file(self.ann_file)

data_infos = []
# convert annotations to middle format
for image_id in image_list:
filename = f'{self.img_prefix}/{image_id}.jpeg'
image = mmcv.imread(filename)
height, width = image.shape[:2]

data_info = dict(filename=f'{image_id}.jpeg', width=width, height=height)

# load annotations
label_prefix = self.img_prefix.replace('image_2', 'label_2')
lines = mmcv.list_from_file(osp.join(label_prefix, f'{image_id}.txt'))

content = [line.strip().split(' ') for line in lines]
bbox_names = [x[0] for x in content]
bboxes = [[float(info) for info in x[4:8]] for x in content]

gt_bboxes = []
gt_labels = []
gt_bboxes_ignore = []
gt_labels_ignore = []

# filter 'DontCare'
for bbox_name, bbox in zip(bbox_names, bboxes):
if bbox_name in cat2label:
gt_labels.append(cat2label[bbox_name])
gt_bboxes.append(bbox)
else:
gt_labels_ignore.append(-1)
gt_bboxes_ignore.append(bbox)

data_anno = dict(
bboxes=np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),
labels=np.array(gt_labels, dtype=np.long),
bboxes_ignore=np.array(gt_bboxes_ignore,
dtype=np.float32).reshape(-1, 4),
labels_ignore=np.array(gt_labels_ignore, dtype=np.long))

data_info.update(ann=data_anno)
data_infos.append(data_info)

return data_infos
# mmdet 配置
from mmcv import Config
from mmdet.apis import set_random_seed

cfg = Config.fromfile('configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_1x_coco.py')
# If we need to finetune a model based on a pre-trained detector, we need to use load_from to set the path of checkpoints.
cfg.load_from = 'checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_20210526_095054-1f77628b.pth'

# Modify dataset type and path
cfg.dataset_type = 'KittiTinyDataset'
cfg.data_root = 'kitti_tiny/'

cfg.data.test.type = 'KittiTinyDataset'
cfg.data.test.data_root = 'kitti_tiny/'
cfg.data.test.ann_file = 'train.txt'
cfg.data.test.img_prefix = 'training/image_2'

cfg.data.train.type = 'KittiTinyDataset'
cfg.data.train.data_root = 'kitti_tiny/'
cfg.data.train.ann_file = 'train.txt'
cfg.data.train.img_prefix = 'training/image_2'

cfg.data.val.type = 'KittiTinyDataset'
cfg.data.val.data_root = 'kitti_tiny/'
cfg.data.val.ann_file = 'val.txt'
cfg.data.val.img_prefix = 'training/image_2'

# modify num classes of the model in box head
cfg.model.roi_head.bbox_head.num_classes = 3

# Set up working dir to save files and logs.
cfg.work_dir = './tutorial_exps'

# The original learning rate (LR) is set for 8-GPU training.
# We divide it by 8 since we only use one GPU.
cfg.optimizer.lr = 0.02 / 8
cfg.lr_config.warmup = None
cfg.log_config.interval = 10

# Change the evaluation metric since we use customized dataset.
cfg.evaluation.metric = 'mAP'
# We can set the evaluation interval to reduce the evaluation times
cfg.evaluation.interval = 12
# We can set the checkpoint saving interval to reduce the storage cost
cfg.checkpoint_config.interval = 12

# Set seed thus the results are more reproducible
cfg.seed = 0
set_random_seed(0, deterministic=False)

cfg.device='cuda'
cfg.gpu_ids = range(1)

# We can also use tensorboard to log the training process
cfg.log_config.hooks = [
dict(type='TextLoggerHook'),
dict(type='TensorboardLoggerHook')]

# We can initialize the logger for training and have a look at the final config used for training
# print(f'Config:\n{cfg.pretty_text}')

# Train a new detector
from mmdet.datasets import build_dataset
from mmdet.models import build_detector
from mmdet.apis import train_detector


# Build dataset
datasets = [build_dataset(cfg.data.train)]

# Build the detector
model = build_detector(cfg.model)
# build_detector( cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))
# Add an attribute for visualization convenience
model.CLASSES = datasets[0].CLASSES

# Create work_dir
mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))

# 报错:AttributeError: 'ConfigDict' object has no attribute 'device'
# https://github.com/open-mmlab/mmdetection/issues/7901
# 在上面 cfg 添加了 cfg.device='cuda'
train_detector(model, datasets, cfg, distributed=False, validate=True)
# load tensorboard in colab
%load_ext tensorboard

# see curves in tensorboard
%tensorboard --logdir ./tutorial_exps
img = mmcv.imread('kitti_tiny/training/image_2/000068.jpeg')

model.cfg = cfg
result = inference_detector(model, img)
show_result_pyplot(model, img, result)

%cd /content/mmdetection3d
!mkdir checkpoints
!wget -c https://download.openmmlab.com/mmdetection3d/v0.1.0_models/second/hv_second_secfpn_6x8_80e_kitti-3d-car/hv_second_secfpn_6x8_80e_kitti-3d-car_20200620_230238-393f000c.pth \
-O checkpoints/hv_second_secfpn_6x8_80e_kitti-3d-car_20200620_230238-393f000c.pth

!wget -c https://download.openmmlab.com/mmdetection3d/v1.0.0_models/votenet/votenet_16x8_sunrgbd-3d-10class/votenet_16x8_sunrgbd-3d-10class_20210820_162823-bf11f014.pth \
-O checkpoints/votenet_16x8_sunrgbd-3d-10class_20210820_162823-bf11f014.pth

!python \
demo/pcd_demo.py \
demo/data/kitti/kitti_000008.bin \
configs/second/hv_second_secfpn_6x8_80e_kitti-3d-car.py \
checkpoints/hv_second_secfpn_6x8_80e_kitti-3d-car_20200620_230238-393f000c.pth \
--out-dir /content/mmdetection3d/result/

!python \
demo/pcd_demo.py \
demo/data/sunrgbd/sunrgbd_000017.bin \
configs/votenet/votenet_16x8_sunrgbd-3d-10class.py \
checkpoints/votenet_16x8_sunrgbd-3d-10class_20210820_162823-bf11f014.pth \
--out-dir /content/mmdetection3d/result/

!wget -c https://download.openmmlab.com/mmdetection3d/v1.0.0_models/mvxnet/dv_mvx-fpn_second_secfpn_adamw_2x8_80e_kitti-3d-3class/dv_mvx-fpn_second_secfpn_adamw_2x8_80e_kitti-3d-3class_20210831_060805-83442923.pth \
-O checkpoints/dv_mvx-fpn_second_secfpn_adamw_2x8_80e_kitti-3d-3class_20210831_060805-83442923.pth

!rm -rf result/kitti_000008

!python \
demo/multi_modality_demo.py \
demo/data/kitti/kitti_000008.bin \
demo/data/kitti/kitti_000008.png \
demo/data/kitti/kitti_000008_infos.pkl \
configs/mvxnet/dv_mvx-fpn_second_secfpn_adamw_2x8_80e_kitti-3d-3class.py \
checkpoints/dv_mvx-fpn_second_secfpn_adamw_2x8_80e_kitti-3d-3class_20210831_060805-83442923.pth \
--out-dir /content/mmdetection3d/result/

import matplotlib.pyplot as plt

plt.figure(figsize=(25, 20))
plt.imshow(plt.imread('result/kitti_000008/kitti_000008_pred.png'))

!wget -c https://download.openmmlab.com/mmdetection3d/v0.1.0_models/fcos3d/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d_20210715_235813-4bed5239.pth \
-O checkpoints/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d_20210715_235813-4bed5239.pth

!python \
demo/mono_det_demo.py \
demo/data/nuscenes/n015-2018-07-24-11-22-45+0800__CAM_BACK__1532402927637525.jpg \
demo/data/nuscenes/n015-2018-07-24-11-22-45+0800__CAM_BACK__1532402927637525_mono3d.coco.json \
configs/fcos3d/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d_finetune.py \
checkpoints/fcos3d_r101_caffe_fpn_gn-head_dcn_2x8_1x_nus-mono3d_20210715_235813-4bed5239.pth \
--out-dir /content/mmdetection3d/result/

import matplotlib.pyplot as plt

plt.figure(figsize=(25, 20))
plt.imshow(plt.imread('result/n015-2018-07-24-11-22-45+0800__CAM_BACK__1532402927637525/n015-2018-07-24-11-22-45+0800__CAM_BACK__1532402927637525_pred.png'))

!wget -c https://download.openmmlab.com/mmdetection3d/v0.1.0_models/pointnet2/pointnet2_ssg_16x2_cosine_200e_scannet_seg-3d-20class/pointnet2_ssg_16x2_cosine_200e_scannet_seg-3d-20class_20210514_143644-ee73704a.pth \
-O checkpoints/pointnet2_ssg_16x2_cosine_200e_scannet_seg-3d-20class_20210514_143644-ee73704a.pth


!python \
demo/pc_seg_demo.py \
demo/data/scannet/scene0000_00.bin \
configs/pointnet2/pointnet2_ssg_16x2_cosine_200e_scannet_seg-3d-20class.py \
checkpoints/pointnet2_ssg_16x2_cosine_200e_scannet_seg-3d-20class_20210514_143644-ee73704a.pth \
--out-dir /content/mmdetection3d/result/