test_data = normalize(test_data, mu, std)
if len(test_data.shape) < 3:
    test_data = test_data[np.newaxis, ...]
"""
    Load the network
"""
print('[%s] Loading network' % (datetime.datetime.now()))
net = load_net(args.net)
"""
    Segmentation
"""
print('[%s] Starting segmentation' % (datetime.datetime.now()))
segmentation = segment(test_data,
                       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))
Exemple #2
0
              lambdas=args.lambdas,
              optimizer=optimizer,
              scheduler=scheduler,
              epochs=args.epochs,
              test_freq=args.test_freq,
              print_stats=args.print_stats,
              log_dir=args.log_dir)
"""
    Validate the trained network
"""
test_data = tar_test.data
test_labels = tar_test.labels
print('[%s] Validating the trained network' % (datetime.datetime.now()))
seg_net = net.get_segmentation_net()
segmentation_last_checkpoint = segment(test_data,
                                       seg_net,
                                       args.input_size,
                                       batch_size=args.test_batch_size)
j = jaccard(segmentation_last_checkpoint, test_labels)
d = dice(segmentation_last_checkpoint, test_labels)
print('[%s] Network performance (last checkpoint): Jaccard=%f - Dice=%f' %
      (datetime.datetime.now(), j, d))
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, 'tar_segmentation_last'),
              rescale=True)
net = torch.load(os.path.join(args.log_dir, 'best_checkpoint.pytorch'))
seg_net = net.get_segmentation_net()
segmentation_best_checkpoint = segment(test_data,
                                       seg_net,
                                       args.input_size,
input_sizes = (args.input_size_xy, args.input_size_zx, args.input_size_yz)
for i, o in enumerate(orientations):
    """
        Load the network
    """
    print('[%s] Loading network' % (datetime.datetime.now()))
    net = load_net(args.nets[i])
    """
        Segmentation
    """
    print('[%s] Starting segmentation' % (datetime.datetime.now()))
    segmentation_cum += np.transpose(
        segment(np.transpose(test_data, o),
                net,
                input_sizes[i],
                batch_size=args.batch_size,
                crf_iterations=args.crf_iterations,
                mu=mu,
                std=std), orientations_inv[i])

segmentation = segmentation_cum / 3
"""
    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()))