def cal_acc(label, pred): dsc = [] print('------------------------------------------') for i in range(1, 4): dsc_i = dice(pred, label, i) dsc_i = round(dsc_i * 100, 2) dsc.append(dsc_i) print('Data | CSF | GM | WM | Avg |') print('Dice | %2.4f | %2.4f | %2.4f | %2.4f |' % (dsc[0], dsc[1], dsc[2], np.mean(dsc))) return dsc
print('[%s] Starting segmentation' % (datetime.datetime.now())) segmentation = segment_pixels(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)) 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)
def predict(args): # Read in the csv with the file names you would want to predict on file_names_list = pd.read_csv(args.csv, dtype=object, keep_default_na=False, na_values=[]).values # We trained on the last 15 subjects, so we predict on the rest file_names = file_names_list[:3] # From the model_path, parse the latest saved model and restore a predictor from it export_dir = [ os.path.join(args.model_path, o) for o in os.listdir(args.model_path) if os.path.isdir(os.path.join(args.model_path, o)) and o.isdigit() ][-1] print('Loading from {}'.format(export_dir)) my_predictor = predictor.from_saved_model(export_dir) # Fetch the output probability op of the trained network y_prob = my_predictor._fetch_tensors['y_prob'] num_classes = y_prob.get_shape().as_list()[-1] # Iterate through the files, predict on the full volumes and compute a Dice coefficient from collections import defaultdict total_dice = defaultdict(list) total_hd = defaultdict(list) for output in read_fn(file_references=file_names, mode=tf.estimator.ModeKeys.EVAL, params=READER_PARAMS): t0 = time.time() # Parse the read function output and add a dummy batch dimension as # required img = np.expand_dims(output['features']['x'], axis=0) lbl = np.expand_dims(output['labels']['y'], axis=0) # Do a sliding window inference with our DLTK wrapper pred = sliding_window_segmentation_inference( session=my_predictor.session, ops_list=[y_prob], sample_dict={my_predictor._feed_tensors['x']: img}, batch_size=1)[0] # Calculate the prediction from the probabilities pred = np.argmax(pred, -1) # Save the file as .nii.gz using the header information from the original sitk image output_fn = os.path.join(args.model_path, '{}_seg.nii.gz'.format(output['subject_id'])) new_sitk = sitk.GetImageFromArray(pred[0, :, :, :].astype(np.int32)) sitk.WriteImage(new_sitk, output_fn) # Calculate the AVG Dice coefficient for one image dsc = np.nanmean(metrics2.dice(pred, lbl, num_classes)[1:15]) hd = np.nanmean(metrics2.hd(pred[0], lbl[0], num_classes)[1:15]) # Calculate and Print each Dice coefficient for one image for idx, i in enumerate( [14, 13, 6, 5, 12, 11, 10, 9, 8, 7, 4, 3, 2, 1]): dsc_tmp = metrics2.dice(pred, lbl, num_classes)[i] total_dice.setdefault("dsc_{}".format(idx), []).append(dsc_tmp) print('Id={}; Dice_{}={:0.4f}; time={:0.2} secs;'.format( output['subject_id'], idx, dsc_tmp, time.time() - t0)) total_dice.setdefault("total_mean_dsc", []).append(dsc) print('Id={}; AVG Dice={:0.4f}; time={:0.2} secs; output_path={};'. format(output['subject_id'], dsc, time.time() - t0, output_fn)) for idx, i in enumerate( [14, 13, 6, 5, 12, 11, 10, 9, 8, 7, 4, 3, 2, 1]): hd_tmp = metrics2.hd(pred[0], lbl[0], num_classes)[i] total_hd.setdefault("hd_{}".format(idx), []).append(hd_tmp) print('Id={}; hd_{}={:0.4f}; time={:0.2} secs;'.format( output['subject_id'], idx, hd_tmp, time.time() - t0)) total_hd.setdefault("total_mean_hd", []).append(hd) print( 'Id={}; AVG HD={:0.4f}; time={:0.2} secs; output_path={};'.format( output['subject_id'], hd, time.time() - t0, output_fn)) print("\n") print( "~~~~~~~~~~~~~~~~~~~~~~ Dice Results on All Test Cases ~~~~~~~~~~~~~~~~~~~~~~" ) all_dice = [] for k, v in total_dice.items(): all_dice.append(np.mean(v)) print(k, "%.3f" % (np.mean(v)), "±", "%.3f" % (np.std(v))) print("\n") print( "~~~~~~~~~~~~~~~~~~~~~~ HD Results (mean, std) on All Test Cases ~~~~~~~~~~~~~~~~~~~~~~" ) all_hd = [] for k, v in total_hd.items(): v = [i for i in v if i != 0] print(k, "%.2f" % (np.mean(v)), "±", "%.2f" % (np.std(v))) all_hd.append(np.mean(v))
epochs=args.epochs, test_freq=args.test_freq, print_stats=args.print_stats, log_dir=args.log_dir) """ Validate the trained network """ print('[%s] Validating the trained network' % (datetime.datetime.now())) test_data = test.data test_labels = test.labels segmentation_last_checkpoint = segment_pixels(test_data, net, args.input_size, batch_size=args.test_batch_size) j = jaccard(segmentation_last_checkpoint, test_labels) d = dice(segmentation_last_checkpoint, test_labels) a, p, r, f = accuracy_metrics(segmentation_last_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)) 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)