A demo file
import torch
import requests
from PIL import Image, ImageDraw, ImageFont
import torchvision.transforms as T
import os
import ipyparams
<IPython.core.display.Javascript object>
model=torch.hub.load('facebookresearch/detr', 'detr_resnet101', pretrained = True)
Using cache found in /home/user/.cache/torch/hub/facebookresearch_detr_master
model.eval()
model = model.cuda()
url = url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
im = Image.open(requests.get(url, stream=True).raw).convert('RGB')
im
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'
]
img = Image.open(requests.get(url, stream = True).raw).resize((800, 600))
transform = T.Compose([T.ToTensor(), T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
img_tens = transform(img).unsqueeze(0).cuda()
im2 = img.copy()
drw = ImageDraw.Draw(im2)
with torch.no_grad():
output = model(img_tens)
for logit, box in zip(output['pred_logits'][0], output['pred_boxes'][0]):
cls = logit.argmax()
if cls >= len(CLASSES):
continue
label = CLASSES[cls]
print(label)
box = box.cpu() * torch.Tensor([800, 600, 800, 600])
x, y, w, h = box
x0, x1 = x - w//2, x + w//2
y0, y1 = y - h//2, y + h//2
drw.rectangle([x0, y0, x1, y1], outline = 'red', width = 5)
drw.text((x, y), label, fill = 'blue')
cat
remote
remote
cat
couch
im2
os.system('jupyter nbconvert --to markdown' + ipyparams.notebook_name)
os.system('embed-images ' + ipyparams.notebook_name[:-6] + '.md > ' + ipyparams.notebook_name[:-6] + '_emb.md')
0