当前位置:网站首页>def fasterrcnn_resnet50_fpn()实例测试
def fasterrcnn_resnet50_fpn()实例测试
2022-07-29 04:38:00 【Eden_mm】
训练阶段
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)
结果:
{
'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>)}]
边栏推荐
- Simply change the picture color
- Tower of Hanoi classic recursion problem (C language implementation)
- Deploy Jenkins using containers
- UE plays video in scene or UMG
- File operation (Advanced C language)
- [c language] PTA 7-49 have fun with numbers (partially correct)
- [c language] PTA 7-63 falling ball
- Go面向并发的内存模型
- 【Express连接MySQL数据库】
- Down sampling and up sampling
猜你喜欢
Deep analysis of data storage in memory (Advanced C language)
Auto.js脚本开发环境搭建
Definition and implementation of stack and queue (detailed)
The most complete NLP Chinese and English stop words list in the whole station (including punctuation marks, which can be copied directly)
在线教育的推荐系统
Not 67 days, square root
MySQL - 深入解析MySQL索引数据结构
Pycharm reports an error when connecting to the virtual machine database
Basic operation of queue
JVM (heap and stack) memory allocation
随机推荐
异常处理:pyemd或PyEMD找不到
Oracle insert data
Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary
Don't insist on 66 days. Weight generates random numbers
Won't you just stick to 62 days? Sum of words
On the use of pyscript (realizing office preview)
There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)
Use of torch.optim optimizer in pytorch
删除word文档中的空白页
[C] PTA 6-8 finding the height of binary tree
RecyclerView通过DPAD按键上下切换焦点 切换到界面外的控件时焦点会左右乱跳
Not for 63 days. The biggest XOR
[c language] PTA 7-63 falling ball
Unity Foundation (3) -- various coordinate systems in unity
settings.xml
Dasctf2022.07 empowerment competition
Google browser opens the web page and out of memory appears
钉钉对话框文子转换成图片 不能复制粘贴到文档上
[c language] use the reverse order output of the linked list (bidirectional linked list)
pyscript无法引入包