In [1]:
import torch
dtype = torch.float
device = torch.device("cpu")
# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10
# Create random input and output data
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)
# Randomly initialize weights
w1 = torch.randn(D_in, H, device=device, dtype=dtype)
w2 = torch.randn(H, D_out, device=device, dtype=dtype)
learning_rate = 1e-6
for t in range(500):
# Forward pass: compute predicted y
h = x.mm(w1)
h_relu = h.clamp(min=0)
y_pred = h_relu.mm(w2)
# Compute and print loss
loss = (y_pred - y).pow(2).sum().item()
if t % 100 == 99:
print(t, loss)
# Backprop to compute gradients of w1 and w2 with respect to loss
grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.t().mm(grad_y_pred)
grad_h_relu = grad_y_pred.mm(w2.t())
grad_h = grad_h_relu.clone()
grad_h[h < 0] = 0
grad_w1 = x.t().mm(grad_h)
# Update weights using gradient descent
w1 -= learning_rate * grad_w1
w2 -= learning_rate * grad_w2
99 359.71734619140625 199 0.9052625894546509 299 0.004071625415235758 399 0.00012559039168991148 499 2.669553941814229e-05
In [5]:
import sys
from IPython.display import display , HTML,Image as im
import urllib
from PIL import Image, ImageDraw, ImageFont, ImageTk
import torch
from torchvision import transforms
from torchvision.models.detection import fasterrcnn_resnet50_fpn, FasterRCNN_ResNet50_FPN_Weights
COCO_INSTANCE_CATEGORY_NAMES = ['__background__', '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']
# URL of the image that will be processed
IMAGE_URL = "https://github.com/pytorch/hub/raw/master/images/dog.jpg"
MIN_SCORE = 0.5
print('Downloading image...')
url, filename = (IMAGE_URL, "image.jpg")
urllib.request.urlretrieve(url, filename)
print('Loading model...')
model=fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.COCO_V1)
model.eval()
img="<img src='"+IMAGE_URL+"'>"
display(HTML(img))
print('Detecting objects, please wait...')
image = Image.open(filename)
# This model doesn't require resize
preprocess = transforms.Compose([
transforms.ToTensor(),
])
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0)
with torch.no_grad():
output = model(input_batch)
classes = [COCO_INSTANCE_CATEGORY_NAMES[i] for i in list(output[0]['labels'].numpy())]
boxes = [[(i[0], i[1]), (i[2], i[3])] for i in list(output[0]['boxes'].detach().numpy())]
scores = list(output[0]['scores'].detach().numpy())
for clss, box, score in zip(classes, boxes, scores):
print(clss,score,box)
Downloading image... Loading model...

Detecting objects, please wait... dog 0.96686065 [(137.84052, 67.79935), (1386.9043, 1172.8247)] cat 0.35221976 [(134.83649, 45.013256), (1471.7245, 1203.3082)] frisbee 0.31320092 [(253.01884, 306.1613), (406.6772, 365.1062)] dog 0.11326689 [(130.73816, 62.591957), (993.04944, 874.355)]
In [ ]:
In [ ]:
In [ ]:
沒有留言:
發佈留言