net, args.input_size, batch_size=args.batch_size, crf_iterations=args.crf_iterations, mu=mu, std=std) """ Validate the segmentation """ print('[%s] Validating segmentation' % (datetime.datetime.now())) test_data_labels = read_tif(args.data_labels, dtype='uint8') test_data_labels = normalize(test_data_labels, 0, 255) j = jaccard(segmentation, test_data_labels) d = dice(segmentation, test_data_labels) a, p, r, f = accuracy_metrics(segmentation, test_data_labels) print('[%s] RESULTS:' % (datetime.datetime.now())) print('[%s] Jaccard: %f' % (datetime.datetime.now(), j)) print('[%s] Dice: %f' % (datetime.datetime.now(), d)) print('[%s] Accuracy: %f' % (datetime.datetime.now(), a)) print('[%s] Precision: %f' % (datetime.datetime.now(), p)) print('[%s] Recall: %f' % (datetime.datetime.now(), r)) print('[%s] F-score: %f' % (datetime.datetime.now(), f)) """ Write out the results """ if args.write_dir is not None: print('[%s] Writing the output' % (datetime.datetime.now())) imwrite3D(segmentation, args.write_dir, rescale=True) print('[%s] Finished!' % (datetime.datetime.now()))
Setup optimization for unsupervised training """ print('[%s] Setting up optimization for unsupervised training' % (datetime.datetime.now())) net = UNetAE(feature_maps=args.fm, levels=args.levels, group_norm=(args.group_norm == 1), lambda_dom=args.lambda_dom, s=args.input_size[0]) """ Train the network unsupervised """ print('[%s] Training network unsupervised' % (datetime.datetime.now())) net.train_net(src_train_loader=src_train_loader, src_test_loader=src_test_loader, tar_train_loader=tar_train_loader, tar_test_loader=tar_test_loader, loss_fn=loss_fn_rec, lr=args.lr, step_size=args.step_size, gamma=args.gamma, epochs=args.epochs, test_freq=args.test_freq, print_stats=args.print_stats, log_dir=args.log_dir) """ Write out the results """ if args.write_dir is not None: print('[%s] Running the trained network on the source dataset' % (datetime.datetime.now())) src_data = src_test.data transform_last_checkpoint = transform(src_data, net, args.input_size, batch_size=args.test_batch_size) print('[%s] Writing the output' % (datetime.datetime.now())) imwrite3D(transform_last_checkpoint, os.path.join(args.write_dir, 'src_transform'), rescale=True) tar_data = tar_test.data print('[%s] Running the trained network on the target dataset' % (datetime.datetime.now())) transform_last_checkpoint = transform(tar_data, net, args.input_size, batch_size=args.test_batch_size) print('[%s] Writing the output' % (datetime.datetime.now())) imwrite3D(transform_last_checkpoint, os.path.join(args.write_dir, 'tar_transform'), rescale=True) print('[%s] Finished!' % (datetime.datetime.now()))
print('[%s] F-score: %f' % (datetime.datetime.now(), f)) net = torch.load(os.path.join(args.log_dir, 'best_checkpoint.pytorch')) segmentation_best_checkpoint = segment_pixels(test_data, net, args.input_size, batch_size=args.test_batch_size) j = jaccard(segmentation_best_checkpoint, test_labels) d = dice(segmentation_best_checkpoint, test_labels) a, p, r, f = accuracy_metrics(segmentation_best_checkpoint, test_labels) print('[%s] RESULTS:' % (datetime.datetime.now())) print('[%s] Jaccard: %f' % (datetime.datetime.now(), j)) print('[%s] Dice: %f' % (datetime.datetime.now(), d)) print('[%s] Accuracy: %f' % (datetime.datetime.now(), a)) print('[%s] Precision: %f' % (datetime.datetime.now(), p)) print('[%s] Recall: %f' % (datetime.datetime.now(), r)) print('[%s] F-score: %f' % (datetime.datetime.now(), f)) print('[%s] Network performance (best checkpoint): Jaccard=%f - Dice=%f' % (datetime.datetime.now(), j, d)) """ Write out the results """ if args.write_dir is not None: print('[%s] Writing the output' % (datetime.datetime.now())) imwrite3D(segmentation_last_checkpoint, os.path.join(args.write_dir, 'segmentation_last_checkpoint'), rescale=True) imwrite3D(segmentation_best_checkpoint, os.path.join(args.write_dir, 'segmentation_best_checkpoint'), rescale=True) print('[%s] Finished!' % (datetime.datetime.now()))
net = torch.load(os.path.join(log_dir, 'best_checkpoint.pytorch')) segmentation_best_checkpoint = segment(test_data, net, input_size, batch_size=args.test_batch_size) j = jaccard(segmentation_best_checkpoint, test_labels) d = dice(segmentation_best_checkpoint, test_labels) a, p, r, f = accuracy_metrics(segmentation_best_checkpoint, test_labels) print('[%s] Results best checkpoint:' % (datetime.datetime.now())) print('[%s] Jaccard: %f' % (datetime.datetime.now(), j)) print('[%s] Dice: %f' % (datetime.datetime.now(), d)) print('[%s] Accuracy: %f' % (datetime.datetime.now(), a)) print('[%s] Precision: %f' % (datetime.datetime.now(), p)) print('[%s] Recall: %f' % (datetime.datetime.now(), r)) print('[%s] F-score: %f' % (datetime.datetime.now(), f)) """ Write out the results """ if args.write_dir is not None: print('[%s] Writing the output' % (datetime.datetime.now())) imwrite3D(segmentation_last_checkpoint, os.path.join(write_dir, 'segmentation_last_checkpoint', str(o)), rescale=True) imwrite3D(segmentation_best_checkpoint, os.path.join(write_dir, 'segmentation_best_checkpoint', str(o)), rescale=True) print('[%s] Finished!' % (datetime.datetime.now()))