scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(my_optim, mode='min', factor=0.1, patience=5, verbose=True) loss = torch.nn.MSELoss() # 读取数据集 train_dataset = ImagePairDataset_y(INPUTS_FOLDER_TRAIN, LABELS_FOLDER_TRAIN) test_dataset = ImagePairDataset_y(INPUTS_FOLDER_TEST, LABELS_FOLDER_TEST) train_iter = DataLoader(train_dataset, batch_size, shuffle=True) test_iter = DataLoader(test_dataset, 1, shuffle=True) print('Datasets loaded!') # 训练 train(train_iter, test_iter, net, loss, my_optim, num_epochs, scheduler) # 测试 print('Full test loss %.4f' % eval(test_iter, net, loss, 0)) # 保存网络 torch.save(net.state_dict(), SAVE_PATH) print('Network saved: ' + SAVE_PATH) # 读取网络 # net.load_state_dict(torch.load(LOAD_PATH)) print('Network loaded: ' + LOAD_PATH) # 应用完整图片并写入 IMG_NAME = r'img_0' IMG_OUT = OUTPUTS_FOLDER+r'/'+IMG_NAME+r'_' + \
patience=5, verbose=True) loss = torch.nn.MSELoss() # 读取数据集 train_dataset = ImagePairDataset_y(INPUTS_FOLDER_TRAIN, LABELS_FOLDER_TRAIN) test_dataset = ImagePairDataset_y(INPUTS_FOLDER_TEST, LABELS_FOLDER_TEST) train_iter = DataLoader(train_dataset, batch_size, shuffle=True) test_iter = DataLoader(test_dataset, 1, shuffle=True) print('Datasets loaded!') # 训练 train(train_iter, test_iter, net, loss, my_optim, num_epochs, scheduler, need_gclip=True) # 测试 print('Full test loss %.4f' % eval(test_iter, net, loss, 0)) # 保存网络 torch.save(net.state_dict(), SAVE_PATH) print('Network saved: ' + SAVE_PATH) # 读取网络 # net.load_state_dict(torch.load(LOAD_PATH)) print('Network loaded: ' + LOAD_PATH)