def run(): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) print('Test start.') with torch.no_grad(): for idx, (images, labels, coordinates, names) in enumerate(trainloader): images, coordinates = images.to(DEVICE), coordinates.to(DEVICE) _, locs = model(images) loss = criterion([locs], [coordinates]) / ITERATION_SIZE # mae_error = mae(input=outputs, target=labels) # statistical_mae_errors.update(mae_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar( 'Loss/Test', statistical_losses.val[0], idx + 1, ) if idx % 1 == 0: print( 'mae Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_losses)) return statistical_losses
def run(): statistical_angular_errors = StatisticalValue() sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) print('Test start.') with torch.no_grad(): for idx, (images, labels, names) in enumerate(testloader): images, labels = images.to(DEVICE), labels.to(DEVICE) predictions = model(images) angular_error = multi_angular_loss(predictions[-1], labels) statistical_angular_errors.update(angular_error.item(), names, sort=True) view_data = torch.zeros((4, *images.shape[1:])) view_data[0, :, :, :] = images.squeeze() view_data[ 1, :, :, :] = images.squeeze() / predictions[-1].squeeze() view_data[2, :, :, :] = predictions[-1].squeeze() view_data[3, :, :, :] = labels.squeeze() if not os.path.isdir(os.path.join(TMP_ROOT, 'test', sub_dir)): os.makedirs(os.path.join(TMP_ROOT, 'test', sub_dir)) torchvision.utils.save_image( view_data, os.path.join(TMP_ROOT, 'test/%s/%s' % (sub_dir, names[0]))) print( 'Angular Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_angular_errors)) print('Test end.')
def run(): statistical_angular_errors = StatisticalValue() with torch.no_grad(): for idx, (images, labels, names) in enumerate(fold1_test_loader): images, labels = images.to(DEVICE), labels.to(DEVICE) # print(images.shape) illus = model(images) angular_error = angular_loss(illus, labels) statistical_angular_errors.update(angular_error.item(), names) images, illus = images.cpu().detach().numpy().transpose( 0, 2, 3, 1), illus.cpu().detach().numpy() illus = illus[:, np.newaxis, np.newaxis, :] images = images[:, :, :, :] / illus # print(images.shape, illus.shape) # exit() # result = Image.fromarray((images[0]).astype(np.uint8)) # result.save(join(TMP_ROOT, "corrected_%s.png") % names[0]) print( 'Angular Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' \ .format(errors=statistical_angular_errors) )
def test(): statistical_mae_errors = StatisticalValue(record=False) statistical_mae_errors1 = StatisticalValue(record=False) statistical_mae_errors2 = StatisticalValue(record=False) print('Test start.') with torch.no_grad(): for idx, (images, labels, names) in enumerate(testloader): images, labels = images.to(DEVICE), labels.to(DEVICE) outputs, global_result, local_result, crops = model(images) _1 = local_result[0] # statistical_mae_errors.update(mae(input=outputs, target=labels).item(), names) statistical_mae_errors1.update( l1_loss(input=_1, target=labels).item(), names) # statistical_mae_errors2.update(l1_loss(input=_2, target=labels).item(), names) # print( # 'Final MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( # errors=statistical_mae_errors)) print( 'Local MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors1)) # print( # 'Global MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( # errors=statistical_mae_errors2)) for iidx in range(len(images)): xmin, ymin, xmax, ymax = crops[iidx]['box'] view_data = torch.zeros((4, *images.shape[1:])) view_data[0, :, :, :] = images[iidx] view_data[1, :, :, :] = labels[iidx] view_data[2, 0:3, ymin:ymax, xmin:xmax] = 1 view_data[3, :, :, :] = _1[iidx] # view_data[4, :, :, :] = _2[iidx] if not os.path.isdir(os.path.join(TMP_ROOT, 'test')): os.makedirs(os.path.join(TMP_ROOT, 'test')) torchvision.utils.save_image( view_data, os.path.join( TMP_ROOT, 'test/%s/%s' % (time.strftime( "%Y-%m-%d-%H-%M-%S", time.localtime()), names[0]))) break print('Test End.')
def run(): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) print('Test start.') with torch.no_grad(): for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) outputs = model(images) loss = criterion(outputs, labels) / ITERATION_SIZE mae_error = mae(input=outputs, target=labels) statistical_mae_errors.update(mae_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar( 'Loss/Test', statistical_losses.val[0], idx + 1, ) if idx % 1 == 0: view_data = torch.zeros((3, *images.shape[1:])) view_data[0, :, :, :] = images[0] view_data[1, :, :, :] = outputs[0] view_data[2, :, :, :] = labels[0] if not os.path.isdir(os.path.join(TMP_ROOT, 'test', sub_dir)): os.makedirs(os.path.join(TMP_ROOT, 'test', sub_dir)) torchvision.utils.save_image( view_data, os.path.join(TMP_ROOT, 'test/%s/%s' % (sub_dir, names[0])) ) print( 'mae Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( errors=statistical_mae_errors)) return statistical_losses
def run(): statistical_mae_errors = StatisticalValue() sub_dir = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) print('Test start.') with torch.no_grad(): for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) outputs, crops = model(images) mae_error = torch.tensor(0.0).to(DEVICE) for i, (output) in enumerate(outputs): xmin, ymin, xmax, ymax = crops[i]['box'] mae_error += mae(input=output[0], target=labels[i][:, ymin:ymax, xmin:xmax]) mae_error /= len(outputs) statistical_mae_errors.update(mae_error.item(), names) if idx % 1 == 0: xmin, ymin, xmax, ymax = crops[0]['box'] view_data = torch.zeros((4, *images.shape[1:])) view_data[0, :, :, :] = images[0] view_data[1, :, :, :] = labels[0] view_data[2, :, :, :] = torch.nn.ConstantPad2d( (xmin, images[0].shape[2] - xmax, ymin, images[0].shape[1] - ymax), 0)(outputs[0]) view_data[3, 0:3, ymin:ymax, xmin:xmax] = 1 if not os.path.isdir(os.path.join(TMP_ROOT, 'test', sub_dir)): os.makedirs(os.path.join(TMP_ROOT, 'test', sub_dir)) torchvision.utils.save_image( view_data, os.path.join(TMP_ROOT, 'test/%s/%s' % (sub_dir, names[0]))) print( 'mae Error: mean: {errors.avg}, mid: {errors.mid}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors))
def run(epoch): statistical_losses = StatisticalValue() statistical_angular_errors = StatisticalValue() optimizer.zero_grad() for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) predictions = model(images) loss = torch.zeros(1).to(DEVICE) for p in predictions: loss += criterion(p, labels) / ITERATION_SIZE loss.backward() angular_error = multi_angular_loss(predictions[-1], labels) statistical_angular_errors.update(angular_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar( 'Loss/Iteration', statistical_losses.val[0], (epoch - 1) * len(trainloader) + idx + 1 ) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(trainloader), statistical_losses.val[0], statistical_losses.avg) print( 'Angular Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( errors=statistical_angular_errors)) scheduler.step() return statistical_losses
def run(epoch): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() statistical_mae_errors1 = StatisticalValue() statistical_mae_errors2 = StatisticalValue() optimizer.zero_grad() for idx, (images, labels, names) in enumerate(trainloader): break images, labels = images.to(DEVICE), labels.to(DEVICE) outputs, _1, _2, crops = model(images) outputs_list = [outputs] loss = saliency_loss(outputs_list, labels) loss.backward() mae_error = mae(input=outputs, target=labels) statistical_mae_errors.update(mae_error.item(), names) statistical_mae_errors1.update( mae(input=_1, target=labels).item(), names) statistical_mae_errors2.update( mae(input=_2, target=labels).item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar('Loss/Iteration', statistical_losses.val[0], (epoch - 1) * len(trainloader) + idx + 1) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(trainloader), statistical_losses.val[0], statistical_losses.avg) print( 'Final MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors)) print( '1 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors1)) print( '2 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors2)) # scheduler.step() test() return statistical_losses
def run(epoch): statistical_losses = StatisticalValue() statistical_angular_errors = StatisticalValue() optimizer.zero_grad() for idx, (images, edges, labels, names) in enumerate(fold1_train_loader): images, labels, edges = images.to( DEVICE), labels.to(DEVICE), edges.to(DEVICE) illuminatios, boundaries = model(images) loss = (mse_loss(illuminatios, labels) + cross_entropy_loss(boundaries, edges)) / ITERATION_SIZE loss.backward() with torch.no_grad(): angular_error = angular_loss(illuminatios, labels) statistical_angular_errors.update(angular_error.item(), names) statistical_losses.update(loss.item()) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(fold1_train_loader), statistical_losses.val[0], statistical_losses.avg) print( 'Angular Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( errors=statistical_angular_errors)) if (idx + 1) % 30 == 0: save_training_process(os.path.join( TMP_ROOT, 'epoch-%s-training-view' % epoch), images, labels, names, illuminatios) scheduler.step() return statistical_losses
def run(epoch): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() optimizer.zero_grad() for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) pred_masks, ca_act_regs = model(images) loss = (criterion(pred_masks, labels) + ca_act_regs) / ITERATION_SIZE loss.backward() mae_error = mae(input=pred_masks, target=labels) statistical_mae_errors.update(mae_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar('Loss/Iteration', statistical_losses.val[0], (epoch - 1) * len(trainloader) + idx + 1) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(trainloader), statistical_losses.val[0], statistical_losses.avg) print( 'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors)) # scheduler.step() return statistical_losses
def run(epoch): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() optimizer.zero_grad() for idx, (images, _, coordinates, names) in enumerate(trainloader): images, coordinates = images.to(DEVICE), coordinates.to(DEVICE) locs = model(images) print(locs.shape, coordinates.shape) loss = criterion([locs], [coordinates]) loss.backward() # mae_error = mae(input=outputs, target=labels) # statistical_mae_errors.update(mae_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar('Loss/Iteration', statistical_losses.val[0], (epoch - 1) * len(trainloader) + idx + 1) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(trainloader), statistical_losses.val[0], statistical_losses.avg) # print( # 'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( # errors=statistical_mae_errors)) # scheduler.step() # exit() return statistical_losses
def test(): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() statistical_mae_errors1 = StatisticalValue() statistical_mae_errors2 = StatisticalValue() with torch.no_grad(): for idx, (images, labels, names) in enumerate(testloader): images, labels = images.to(DEVICE), labels.to(DEVICE) outputs, _1, _2, crops = model(images) outputs_list = [outputs] loss = saliency_loss(outputs_list, labels) statistical_mae_errors.update( mae(input=outputs, target=labels).item(), names) statistical_mae_errors1.update( mae(input=_1, target=labels).item(), names) statistical_mae_errors2.update( mae(input=_2, target=labels).item(), names) statistical_losses.update(loss.item()) print( 'Final MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors)) print( '1 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors1)) print( '2 MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors2)) for iidx in range(len(images)): xmin, ymin, xmax, ymax = crops[iidx]['box'] view_data = torch.zeros((5, *images.shape[1:])) view_data[0, :, :, :] = images[iidx] view_data[1, :, :, :] = labels[iidx] view_data[2, 0:3, ymin:ymax, xmin:xmax] = 1 view_data[3, :, :, :] = _1[iidx] view_data[4, :, :, :] = _2[iidx] if not os.path.isdir(os.path.join(TMP_ROOT, 'test')): os.makedirs(os.path.join(TMP_ROOT, 'test')) torchvision.utils.save_image( view_data, os.path.join(TMP_ROOT, 'test/%s' % (names[0]))) break
def run(epoch): statistical_losses = StatisticalValue() statistical_mae_errors = StatisticalValue() optimizer.zero_grad() for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) outputs, crops = model(images) # xmin, ymin, xmax, ymax = crops[0]['box'] # view_data = torch.zeros((4, *images.shape[1:])) # view_data[0, :, :, :] = images[0] # view_data[1, :, :, :] = labels[0] # view_data[2, 0:3, ymin: ymax, xmin: xmax] = 1 # view_data[3, :, :, :] = outputs[0] # if not os.path.isdir(os.path.join(TMP_ROOT, 'test')): # os.makedirs(os.path.join(TMP_ROOT, 'test')) # torchvision.utils.save_image( # view_data, # os.path.join(TMP_ROOT, 'test/%s' % (names[0])) # ) loss = torch.tensor(0.0).to(DEVICE) for i, (output) in enumerate(outputs): xmin, ymin, xmax, ymax = crops[i]['box'] output_list = [output[0]] reshaped_label = labels[i][:, ymin:ymax, xmin:xmax] total = torch.numel(reshaped_label) postive = torch.count_nonzero(reshaped_label) negative = total - postive # if postive > negative: # loss += saliency_loss(output_list, reshaped_label, weight_0=postive * 2 / total, weight_1=1.0) # else: # loss += saliency_loss(output_list, reshaped_label, weight_0=1.0, weight_1=negative * 2 / total) # print(output[0].unsqueeze(0).shape, reshaped_label.unsqueeze(0).shape) if postive > negative: loss += criterion(output[0].unsqueeze(0), reshaped_label.unsqueeze(0), weight_0=postive * 2 / total, weight_1=1.0) else: loss += criterion(output[0].unsqueeze(0), reshaped_label.unsqueeze(0), weight_0=1.0, weight_1=negative * 2 / total) loss /= len(outputs) loss.backward() mae_error = torch.tensor(0.0).to(DEVICE) for i, (output) in enumerate(outputs): xmin, ymin, xmax, ymax = crops[i]['box'] mae_error += mae(input=output[0], target=labels[i][:, ymin:ymax, xmin:xmax]) mae_error /= len(outputs) statistical_mae_errors.update(mae_error.item(), names) statistical_losses.update(loss.item()) iteration_writer.add_scalar('Loss/Iteration', statistical_losses.val[0], (epoch - 1) * len(trainloader) + idx + 1) if (idx + 1) % ITERATION_SIZE == 0: optimizer.step() optimizer.zero_grad() print_training_status(epoch, idx + 1, len(trainloader), statistical_losses.val[0], statistical_losses.avg) print( 'MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_mae_errors)) # scheduler.step() return statistical_losses
def run(epoch): statistical_local_losses = StatisticalValue(record=False) statistical_global_losses = StatisticalValue(record=False) statistical_local_l1_errors = StatisticalValue(record=False) statistical_global_l1_errors = StatisticalValue(record=False) optimizer_local.zero_grad() # optimizer_global.zero_grad() for idx, (images, labels, names) in enumerate(trainloader): images, labels = images.to(DEVICE), labels.to(DEVICE) _, global_result, local_result, crops = model(images) local_loss = torch.tensor(0.0).to(DEVICE) for i, (output) in enumerate(local_result[0]): xmin, ymin, xmax, ymax = crops[i]['box'] reshaped_output = output[:, ymin:ymax, xmin:xmax] reshaped_label = labels[i][:, ymin:ymax, xmin:xmax] total = torch.numel(reshaped_label) postive = torch.count_nonzero(reshaped_label) negative = total - postive reshaped_output = reshaped_output.unsqueeze(0) reshaped_label = reshaped_label.unsqueeze(0) if postive > negative: local_loss += criterion(reshaped_output, reshaped_label, weight_0=postive * 2 / total, weight_1=1.0) else: local_loss += criterion(reshaped_output, reshaped_label, weight_0=1.0, weight_1=negative * 2 / total) print(local_result[1]) exit() local_loss /= len(local_result[0]) local_loss += local_result[1] # global_loss = criterion(global_result[0], labels) + global_result[1] local_loss.backward() # global_loss.backward() statistical_local_losses.update(local_loss.item()) # statistical_global_losses.update(global_loss.item()) local_l1_error = torch.tensor(0.0).to(DEVICE) for i, (output) in enumerate(local_result[0]): xmin, ymin, xmax, ymax = crops[i]['box'] local_l1_error += l1_loss(input=output[:, ymin:ymax, xmin:xmax], target=labels[i][:, ymin:ymax, xmin:xmax]) local_l1_error /= len(local_result[0]) statistical_local_l1_errors.update(local_l1_error) # statistical_global_l1_errors.update(l1_loss(input=global_result[0], target=labels)) if (idx + 1) % 10 == 0: optimizer_local.step() optimizer_local.zero_grad() # optimizer_global.step() # optimizer_global.zero_grad() print('Local Loss: ') print_training_status(epoch, idx + 1, len(trainloader), statistical_local_losses.val[0], statistical_local_losses.avg) print('Global Loss: ') # print_training_status(epoch, idx + 1, len(trainloader), # statistical_global_losses.val[0], statistical_global_losses.avg) print( 'Local MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}' .format(errors=statistical_local_l1_errors)) # print( # 'Global MAE Error: mean: {errors.avg}, worst: {errors.max[0]}, best: {errors.min[0]}'.format( # errors=statistical_global_l1_errors)) return statistical_local_losses