kaggle上mmdet的使用与tensorboard的解决方案
kaggle上mmdet的使用与tensorboard的解决方案

kaggle上mmdet的使用与tensorboard的解决方案

mmdet 安装与使用

!pip install openmim
!mim install mmdet

上述指令即可完成对mmdet的安装,而在kaggle上大概要花20min左右,主要是造轮子的时间太久了。

这边有一个取巧的办法,可以直接copy&edit这个工程可以很快的完成mmdet的安装。

mmdet + tensorboard

在kaggle上我们无法直接访问tensorboard,这里可以用ngrok做转发,代码如下

import datetime
import tensorflow as tf
from tensorflow import summary
%load_ext tensorboard

log_dir="runs/"

# summary_writer = tf.summary.create_file_writer(
#   log_dir + "fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
!./ngrok authtoken xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

import os
import multiprocessing

pool = multiprocessing.Pool(processes = 10)
results_of_processes = [pool.apply_async(os.system, args=(cmd, ), callback = None )
                        for cmd in [
                            f"tensorboard --logdir ./runs/ --host 0.0.0.0 --port 6006 &",
                            "./ngrok http 6006 &"
                        ]]
! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

其中authtoken需要注册ngrok可以获得,ngrok的链接:ngrok

config文件与训练

%%writefile configs/myconfig/xxxxxxxxx.py

使用上述指令可以自行创建一个config文件,方便训练与修改。

!python tools/train.py configs/myconfig/xxxxxxxxx.py

验证

from mmcv import Config
cfg = Config.fromfile('configs/myconfig/yolox_l_4x4_80e_coco.py')

#--------------------------------------------------------------------------------#

def xyxy2xywh(bbox):
    """Convert ``xyxy`` style bounding boxes to ``xywh`` style for COCO
    evaluation.

    Args:
        bbox (numpy.ndarray): The bounding boxes, shape (4, ), in
            ``xyxy`` order.

    Returns:
        list[float]: The converted bounding boxes, in ``xywh`` order.
    """

    _bbox = bbox.tolist()
    return [
        _bbox[0],
        _bbox[1],
        _bbox[2] - _bbox[0],
        _bbox[3] - _bbox[1],
    ]

#--------------------------------------------------------------------------------#

from mmdet.apis import inference_detector, init_detector, show_result_pyplot
import numpy as np 
from pycocotools.coco import COCO
import json
from tqdm import tqdm
import mmcv

model = init_detector(cfg, '/kaggle/working/runs/epoch_80.pth')

# test_json_path = '/kaggle/working/mmdetection/data_anno/test2017.json'
test_json_path = '/kaggle/working/mmdetection/data_anno/instances_val2017.json'
coco = COCO(test_json_path)

imgIds = coco.getImgIds()
res = []

with tqdm(total=len(imgIds)) as pbar:
    for i in imgIds:
        img_info = coco.loadImgs(i)
        img = mmcv.imread('/kaggle/input/fewshotlogodetection/val/images/' + img_info[0]['file_name'])
        result = inference_detector(model, img)

        bboxes = np.vstack(result)
        labels = [
            np.full(bbox.shape[0], j, dtype=np.int32)
            for j, bbox in enumerate(result)
        ]
        labels = np.concatenate(labels)

        for k in range(len(labels)):
            res_tmp = {}
            box = xyxy2xywh(bboxes[k,:4])
            res_tmp['image_id'] = int(i)
            res_tmp['category_id'] = int(labels[k] + 1)
            res_tmp['bbox'] = box
            res_tmp['score'] = float(bboxes[k,4])
            res.append(res_tmp)
        pbar.update(1)

当然也可以直接使用现成的。

!python tools/test.py configs/myconfig/yolox_l_4x4_80e_coco.py /kaggle/input/modelpara/yolox_l_0.58.pth --format-only --options "jsonfile_prefix=/kaggle/working/res"

参考

https://blog.csdn.net/weixin_45564209/article/details/119881300

https://www.kaggle.com/code/alvin369/predicting-math-functions/notebook

发表回复

您的电子邮箱地址不会被公开。