第14章コンピュータビジョン
14.7.3. Prediction14.7.3 予測
During prediction, the goal is to detect all the objects of interest on the image. Below we read and resize a test image, converting it to a four-dimensional tensor that is required by convolutional layers.
英語のヒント
予測のあいだ、目標は画像上の注目するすべての物体を検出することである。下では、テスト画像を読んで大きさを変え、畳み込み層が必要とする4次元テンソルに変換する。
X = torchvision.io.read_image('../img/banana.jpg').unsqueeze(0).float()
img = X.squeeze(0).permute(1, 2, 0).long()Using the
multibox_detection function below, the predicted bounding boxes are obtained from the anchor boxes and their predicted offsets. Then non-maximum suppression is used to remove similar predicted bounding boxes.英語のヒント
下の
multibox_detection関数を使うと、アンカーボックスとその予測したオフセットから、予測した境界ボックスが得られる。それから、非最大値抑制を使ってよく似た予測した境界ボックスを取り除く。def predict(X):
net.eval()
anchors, cls_preds, bbox_preds = net(X.to(device))
cls_probs = F.softmax(cls_preds, dim=2).permute(0, 2, 1)
output = d2l.multibox_detection(cls_probs, bbox_preds, anchors)
idx = [i for i, row in enumerate(output[0]) if row[0] != -1]
return output[0, idx]
output = predict(X)Finally, we display all the predicted bounding boxes with confidence 0.9 or above as output.
英語のヒント
最後に、信頼度が0.9以上の予測した境界ボックスをすべて出力として表示する。
def display(img, output, threshold):
d2l.set_figsize((5, 5))
fig = d2l.plt.imshow(img)
for row in output:
score = float(row[1])
if score < threshold:
continue
h, w = img.shape[:2]
bbox = [row[2:6] * torch.tensor((w, h, w, h), device=row.device)]
d2l.show_bboxes(fig.axes, bbox, '%.2f' % score, 'w')
display(img, output.cpu(), threshold=0.9)