torch.save(model.state_dict(), r'./saved_model/' + str(args.arch) + '_' + str(args.batch_size) + '_' + str(args.dataset) + '_' + str(args.epoch) + '.pth') # 计算GFLOPs flops = 0 for name, module in model.named_modules(): if isinstance(module, torch.nn.Conv2d): flops += module.weight.numel() * 2 * module.in_channels * module.out_channels * module.kernel_size[ 0] * module.kernel_size[1] / module.stride[0] / module.stride[1] elif isinstance(module, torch.nn.Linear): flops += module.weight.numel() * 2 * module.in_features start_event = torch.cuda.Event(enable_timing=True) end_event = torch.cuda.Event(enable_timing=True) start_event.record() with torch.no_grad(): output = UNet(args,3,1).to(device) end_event.record() torch.cuda.synchronize() elapsed_time_ms = start_event.elapsed_time(end_event) gflops = flops / (elapsed_time_ms * 10 ** 6) print("GFLOPs: {:.2f}".format(gflops)) return best_iou, aver_iou, aver_dice, aver_hd, aver_accuracy, aver_recall, aver_precision, aver_f1score, aver_memory, fps, parameters, gflops出现错误 best_iou,aver_iou,aver_dice,aver_hd, aver_accuracy, aver_recall, aver_precision, aver_f1score, aver_memory, FPS, parameters, gflops = val(model,best_iou,val_dataloader) File "D:/BaiduNetdiskDownload/0605_ghostv2unet _tunnelcrack/ghostunet++/UNET++/main.py", line 143, in val return best_iou, aver_iou, aver_dice, aver_hd, aver_accuracy, aver_recall, aver_precision, aver_f1score, aver_memory, fps, parameters, gflops UnboundLocalError: local variable 'gflops' referenced before assignment怎么修改
时间: 2024-03-12 16:47:52 浏览: 125
pytorch 状态字典:state_dict使用详解
5星 · 资源好评率100%
这个错误是因为在函数中定义了 `gflops` 变量,但是在函数返回时并没有为它赋值,导致出现了未赋值的情况。可以将 `gflops` 变量在函数一开始就赋一个初始值,比如设为0。代码修改如下:
```
def val(model, best_iou, val_dataloader, device):
model.eval()
aver_iou = 0
aver_dice = 0
aver_hd = 0
aver_accuracy = 0
aver_recall = 0
aver_precision = 0
aver_f1score = 0
aver_memory = 0
fps = 0
parameters = sum(param.numel() for param in model.parameters())
gflops = 0 # 在这里为 gflops 赋一个初始值
with torch.no_grad():
for step, (images, labels) in enumerate(val_dataloader):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
iou, dice, hd, accuracy, recall, precision, f1score = eval_metrics(outputs, labels)
memory = torch.cuda.max_memory_allocated() / 1024.0 / 1024.0
aver_iou += iou
aver_dice += dice
aver_hd += hd
aver_accuracy += accuracy
aver_recall += recall
aver_precision += precision
aver_f1score += f1score
aver_memory += memory
aver_iou /= len(val_dataloader)
aver_dice /= len(val_dataloader)
aver_hd /= len(val_dataloader)
aver_accuracy /= len(val_dataloader)
aver_recall /= len(val_dataloader)
aver_precision /= len(val_dataloader)
aver_f1score /= len(val_dataloader)
aver_memory /= len(val_dataloader)
fps = len(val_dataloader.dataset) / (time.time() - start_time)
# 统计模型的GFLOPs
flops = 0
for name, module in model.named_modules():
if isinstance(module, torch.nn.Conv2d):
flops += module.weight.numel() * 2 * module.in_channels * module.out_channels * module.kernel_size[0] * module.kernel_size[1] / module.stride[0] / module.stride[1]
elif isinstance(module, torch.nn.Linear):
flops += module.weight.numel() * 2 * module.in_features
start_event = torch.cuda.Event(enable_timing=True)
end_event = torch.cuda.Event(enable_timing=True)
start_event.record()
with torch.no_grad():
output = UNet(args, 3, 1).to(device)
end_event.record()
torch.cuda.synchronize()
elapsed_time_ms = start_event.elapsed_time(end_event)
gflops = flops / (elapsed_time_ms * 10 ** 6)
print("GFLOPs: {:.2f}".format(gflops))
return best_iou, aver_iou, aver_dice, aver_hd, aver_accuracy, aver_recall, aver_precision, aver_f1score, aver_memory, fps, parameters, gflops
```
阅读全文