目标检测
用DETR(Detection Transformer)的最小实现来实现目标检测,本实现与原始文献中的baseline略有差异。代码来源于官方代码库:Standalone Colab Notebook:,我个人对注释做了补充。下述代码可直接用Jupyter记事本运行。
from PIL import Image
import requests
import matplotlib.pyplot as plt
import torch
from torch import nn
from torchvision.models import resnet50
import torchvision.transforms as T
torch.set_grad_enabled(False)
class DETRDemo(nn.Module):
"""
利用最少的代码来实现DETR,相较于原始文献中的DETR,此处实现有3点不同。
- 可学习的位置编码(取代sine)
- 位置编码在输入时传递(取代注意力)
- 全连接层定义的边界框预测器(取代MLP)
"""
def __init__(self,num_classes,hidden_dim=256,nheads=8,num_encoder_layers=6,num_decoder_layers=6):
super().__init__()
self.backbone = resnet50()
del self.backbone.fc
self.conv = nn.Conv2d(2048,hidden_dim,1)
self.transformer = nn.Transformer(hidden_dim,nheads,num_encoder_layers,num_decoder_layers)
self.linear_class = nn.Linear(hidden_dim,num_classes+1)
self.linear_bbox = nn.Linear(hidden_dim,4)
self.query_pos = nn.Parameter(torch.rand(100,hidden_dim))
self.row_embed = nn.Parameter(torch.rand(50,hidden_dim//2))
self.col_embed = nn.Parameter(torch.rand(50,hidden_dim//2))
def forward(self,inputs):
x = self.backbone.conv1(inputs)
x = self.backbone.bn1(x)
x = self.backbone.relu(x)
x = self.backbone.maxpool(x)
x = self.backbone.layer1(x)
x = self.backbone.layer2(x)
x = self.backbone.layer3(x)
x = self.backbone.layer4(x)
h = self.conv(x)
H,W = h.shape[-2:]
pos = torch.cat([self.col_embed[:W].unsqueeze(0).repeat(H,1,1),
self.row_embed[:H].unsqueeze(1).repeat(1,W,1),],dim=-1).flatten(0,1).unsqueeze(1)
h = self.transformer(pos+0.1*h.flatten(2).permute(2,0,1),self.query_pos.unsqueeze(1)).transpose(0,1)
return {'pred_logits':self.linear_class(h),'pred_boxes':self.linear_bbox(h).sigmoid()}
CLASSES = [
'N/A', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A',
'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack',
'umbrella', 'N/A', 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
'skateboard', 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass',
'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table', 'N/A',
'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A',
'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier',
'toothbrush'
]
COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
[0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
transform = T.Compose([T.Resize(800),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
def box_cxcywh_to_xyxy(x):
x_c,y_c,w,h = x.unbind(1)
b = [(x_c-0.5*w),(y_c-0.5*h),(x_c+0.5*w),(y_c+0.5*h)]
return torch.stack(b,dim=1)
def rescale_bboxes(out_bbox,size):
img_w,img_h = size
b = box_cxcywh_to_xyxy(out_bbox)
b = b*torch.tensor([img_w,img_h,img_w,img_h],dtype=torch.float32)
return b
def detect(im,model,transform):
img = transform(im).unsqueeze(0)
assert img.shape[-2] <= 1600 and img.shape[-1] <= 1600 , '网络支持的输入图像单边最大像素值不可超过1600!'
outputs = model(img)
probas = outputs['pred_logits'].softmax(-1)[0,:,:-1]
keep = probas.max(-1).values >0.7
bboxes_scaled = rescale_bboxes(outputs['pred_boxes'][0,keep], im.size)
return probas[keep],bboxes_scaled
def plot_results(pil_img, prob, boxes):
plt.figure(figsize=(16,10))
plt.imshow(pil_img)
ax = plt.gca()
for p, (xmin, ymin, xmax, ymax), c in zip(prob, boxes.tolist(), COLORS * 100):
ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
fill=False, color=c, linewidth=3))
cl = p.argmax()
text = f'{CLASSES[cl]}: {p[cl]:0.2f}'
ax.text(xmin, ymin, text, fontsize=15,
bbox=dict(facecolor='yellow', alpha=0.5))
plt.axis('off')
plt.show()
detr = DETRDemo(num_classes=91)
state_dict = torch.hub.load_state_dict_from_url(
url='https://dl.fbaipublicfiles.com/detr/detr_demo-da2a99e9.pth',
map_location='cpu',check_hash=True)
detr.load_state_dict(state_dict)
detr.eval()
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
im = Image.open(requests.get(url, stream=True).raw)
scores, boxes = detect(im, detr, transform)
plot_results(im, scores, boxes)
输出结果
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)