训练阶段
import torch
import torchvision
from torchvision.models.detection import FasterRCNN_ResNet50_FPN_Weights
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
# for train
# 以前一直有误区以为rand会生成4,或者3,这不是乱套了嘛,应该按顺序生成呀,实际上这是维数。浅显理解就是逐渐堆叠,先生成1200列,再生成600个这样的,再生成3个这样的。。。。。
images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
# 为了保证x2,y2一定大于x1,y1
boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]
# 前面参数是randint的最大最小值,(4,11)是生成的列表形状
labels = torch.randint(1, 91, (4, 11))
# 把每个图片给单独放到list里面,符合input的格式
images = list(image for image in images)
#targets是字典的列表,每个列表包括boxes和labels
targets = []
for i in range(len(images)): # i = 0, 1, 2, 3
d = {}
d['boxes'] = boxes[i]
d['labels'] = labels[i]
targets.append(d)
output = model(images, targets)
print(output)
#
# # for inference
# model.eval()
#
# x = [torch.rand(3, 300, 400), torch.rand(3, 300, 400)]
# predictions = model(x)
结果:
{'loss_classifier': tensor(0.3633, grad_fn=<NllLossBackward0>), 'loss_box_reg': tensor(0.0262, grad_fn=<DivBackward0>), 'loss_objectness': tensor(1.9085, grad_fn=<BinaryCrossEntropyWithLogitsBackward0>), 'loss_rpn_box_reg': tensor(1.0729, grad_fn=<DivBackward0>)}
推理阶段
import torch
import torchvision
from torchvision.models.detection import FasterRCNN_ResNet50_FPN_Weights
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=FasterRCNN_ResNet50_FPN_Weights.DEFAULT)
# # for train
# images, boxes = torch.rand(4, 3, 600, 1200), torch.rand(4, 11, 4)
# boxes[:, :, 2:4] = boxes[:, :, 0:2] + boxes[:, :, 2:4]
# labels = torch.randint(1, 91, (4, 11))
#
# images = list(image for image in images)
# targets = []
# for i in range(len(images)): # i = 0, 1, 2, 3
# d = {}
# d['boxes'] = boxes[i]
# d['labels'] = labels[i]
# targets.append(d)
#
# output = model(images, targets)
# print(output)
#
# for inference
model.eval()
x = [torch.rand(3, 300, 400), torch.rand(3, 300, 400)]
predictions = model(x)
print(predictions)
结果:
推测没有预测框的原因是数据是随机生成的
[{'boxes': tensor([], size=(0, 4), grad_fn=<StackBackward0>), 'labels': tensor([], dtype=torch.int64), 'scores': tensor([], grad_fn=<IndexBackward0>)},
{'boxes': tensor([], size=(0, 4), grad_fn=<StackBackward0>), 'labels': tensor([], dtype=torch.int64), 'scores': tensor([], grad_fn=<IndexBackward0>)}]
版权声明:本文为qq_45583898原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。