本文整理匯總了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)