本文整理汇总了Python中utils.utils.to_cpu方法的典型用法代码示例。如果您正苦于以下问题:Python utils.to_cpu方法的具体用法?Python utils.to_cpu怎么用?Python utils.to_cpu使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.utils
的用法示例。
在下文中一共展示了utils.to_cpu方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward
# 需要导入模块: from utils import utils [as 别名]
# 或者: from utils.utils import to_cpu [as 别名]
def forward(self, x, targets=None):
img_dim = x.shape[2] # 取决于输入图片的大小,因为是正方形输入,所以只考虑height
loss = 0
layer_outputs, yolo_outputs = [], []
for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
if module_def["type"] in ["convolutional", "upsample", "maxpool"]:
x = module(x)
elif module_def["type"] == "route":
x = torch.cat([layer_outputs[int(layer_i)] for layer_i in module_def["layers"].split(",")], 1)
elif module_def["type"] == "shortcut":
layer_i = int(module_def["from"])
x = layer_outputs[-1] + layer_outputs[layer_i]
elif module_def["type"] == "yolo": # [82, 94, 106] for yolov3
x, layer_loss = module[0](x, targets, img_dim) # module是nn.Sequential(),所以要取[0]
loss += layer_loss
yolo_outputs.append(x)
layer_outputs.append(x) # 将每个块的output都保存起来
yolo_outputs = to_cpu(torch.cat(yolo_outputs, 1)) # 只保存yolo层的output
return yolo_outputs if targets is None else (loss, yolo_outputs)
示例2: forward
# 需要导入模块: from utils import utils [as 别名]
# 或者: from utils.utils import to_cpu [as 别名]
def forward(self, x, targets=None):
img_dim = x.shape[2]
loss = 0
layer_outputs, yolo_outputs = [], []
for i, (module_def, module) in enumerate(zip(self.module_defs, self.module_list)):
if module_def["type"] in ["convolutional", "upsample", "maxpool"]:
x = module(x)
elif module_def["type"] == "route":
x = torch.cat([layer_outputs[int(layer_i)] for layer_i in module_def["layers"].split(",")], 1)
elif module_def["type"] == "shortcut":
layer_i = int(module_def["from"])
x = layer_outputs[-1] + layer_outputs[layer_i]
elif module_def["type"] == "yolo":
x, layer_loss = module[0](x, targets, img_dim)
loss += layer_loss
yolo_outputs.append(x)
layer_outputs.append(x)
yolo_outputs = to_cpu(torch.cat(yolo_outputs, 1))
return yolo_outputs if targets is None else (loss, yolo_outputs)