背景
最近發(fā)現(xiàn)一個叫GroundingDINO的開集目標(biāo)檢測算法,所謂開集目標(biāo)檢測就是能檢測的目標(biāo)類別不局限于訓(xùn)練的類別,這個算法可以通過輸入文本的prompt然后輸出對應(yīng)的目標(biāo)框。可以用來做預(yù)標(biāo)注或者其他應(yīng)用,比如我們要訓(xùn)練某個細(xì)分場景的算法時,我們找不到足夠的已經(jīng)標(biāo)注的數(shù)據(jù),就可以先用這個算法預(yù)打標(biāo), 與SAM結(jié)合,還能做根據(jù)text去分割出物體。
GroundingDINO:https://github.com/IDEA-Research/GroundingDINO
將GroundingDINO服務(wù)化
為什么要服務(wù)化
原始的項(xiàng)目是一個python腳本,不適合單人使用,而不是和團(tuán)隊一起使用。服務(wù)化之后,其他人可以通過http請求的方式來訪問,而不需要每個人都搭建環(huán)境,也便于批量處理數(shù)據(jù)。
如何服務(wù)化
最簡單的是通過flask api把python腳本包裝一層,這種方式實(shí)現(xiàn)簡單,但擴(kuò)展性不夠,比如如果想要動態(tài)組batch,就需要自己寫這部分邏輯。更好的方式是使用成熟的模型推理服務(wù),TorchServe就是其中的一種,比較適合pytorch模型(其實(shí)其他格式比如onnx也可以),使用TorchServe,我們只用寫好模型的預(yù)處理、推理和后處理邏輯,其他的比如實(shí)例擴(kuò)展、動態(tài)batch、資源監(jiān)控這些都不需要我們自己實(shí)現(xiàn)。我們有其他模型,也可以用同樣的方式服務(wù)起來,而不需要為每個模型都寫一個服務(wù)。因此本文選擇TorchServe來作為模型的推理服務(wù)。
過程
克隆文末的項(xiàng)目后按順序執(zhí)行下面步驟:
1.下載模型
新建一個weights目錄,并把下面的模型放入:
wget -q https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth
新建一個bert-base-uncased 目錄,下載bert模型:
https://huggingface.co/bert-base-uncased/tree/main
config.json
pytorch_model.bin
tokenizer_config.json
tokenizer.json
vocab.txt
2.制作torchserve鏡像
Dockerfile:
FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-devel
ARG DEBIAN_FRONTEND=noninteractive
#for Chinese User, uncomment this line
# COPY sources.list /etc/apt/sources.list
RUN apt update && \
apt install openjdk-17-jdk -y
RUN apt install git -y
#install python packages
COPY requirements.txt /root/
RUN pip install -r /root/requirements.txt --no-cache -i https://repo.huaweicloud.com/repository/pypi/simple/
docker build -t torchserve:groundingdino .
3.轉(zhuǎn)換模型
docker run --rm -it -v $(pwd):/data -w /data torchserve:groundingdino bash -c "torch-model-archiver --model-name groundingdino --version 1.0 --serialized-file weights/groundingdino_swint_ogc.pth --handler grounding_dino_handler.py --extra-files GroundingDINO_SwinT_OGC.py,bert-base-uncased/*"
執(zhí)行完畢后,將得到一個groundingdino.mar文件。
4.開啟服務(wù)
根據(jù)需要修改服務(wù)的配置文章來源:http://www.zghlxwxcb.cn/news/detail-460079.html
docker run -d --name groundingdino -v $(pwd)/model_store:/model_store -p 8080:8080 -p 8081:8081 -p 8082:8082 torchserve:groundingdino bash -c "torchserve --start --foreground --model-store /model_store --models groundingdino=groundingdino.mar"
5.調(diào)用服務(wù)
import requests
import base64
import time
# URL for the web service
url = "http://ip:8080/predictions/groundingdino"
headers = {"Content-Type": "application/json"}
# Input data
with open("test.jpg", "rb") as f:
image = f.read()
data = {
"image": base64.b64encode(image).decode("utf-8"), # base64 encoded image or BytesIO
"caption": "steel pipe", # text prompt, split by "." for multiple phrases
"box_threshold": 0.25, # threshold for object detection
"caption_threshold": 0.25 # threshold for text similarity
}
# Make the request and display the response
resp = requests.post(url=url, headers=headers, json=data)
outputs = resp.json()
'''
the outputs will be like:
{
"boxes": [[0.0, 0.0, 1.0, 1.0]], # list of bounding boxes in xyxy format
"scores": [0.9999998807907104], # list of object detection scores
"phrases": ["steel pipe"] # list of text phrases
}
'''
完整項(xiàng)目:GroundingDINO-Service文章來源地址http://www.zghlxwxcb.cn/news/detail-460079.html
到了這里,關(guān)于GroundingDINO(一種開集目標(biāo)檢測算法)服務(wù)化,根據(jù)文本生成檢測框的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!