示例#1
0
for patient_id in validation_patients:
    print patient_id
    output_folder = os.path.join(results_folder, "%03.0d" % patient_id)
    if not os.path.isdir(output_folder):
        os.mkdir(output_folder)

    t1_img, t1km_img, t2_img, flair_img, seg_combined = load_pat_orig(patient_id)
    import matplotlib.pyplot as plt

    t1_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1_img, input_shape, pad_value=None)
    t1km_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1km_img, input_shape, pad_value=None)
    t2_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t2_img, input_shape, pad_value=None)
    flair_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(flair_img, input_shape, pad_value=None)
    seg_combined = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(seg_combined, input_shape, pad_value=0)

    seg_combined = center_crop_image(seg_combined, output_shape)

    print "predicting image"
    cmap = ListedColormap([(0,0,0), (0,0,1), (0,1,0), (1,0,0), (1,1,0), (0.3, 0.5, 1)])

    z = 2
    data = np.zeros((1, 20, input_shape[0], input_shape[1])).astype(np.float32)
    res = np.zeros((t1km_img.shape[0], output_shape[0], output_shape[1], 6))
    while z < t1km_img.shape[0]-2:
        data[0,0:5] = t1_img[z-2:z+3]
        data[0,5:10] = t1km_img[z-2:z+3]
        data[0,10:15] = t2_img[z-2:z+3]
        data[0,15:20] = flair_img[z-2:z+3]
        pred = pred_fn(data).transpose((0,2,3,1)).reshape((output_shape[0], output_shape[1], 6))
        res[z] = pred
        z += 1
seg_combined = np.load(os.path.join(output_folder, "seg_gt.npy")).astype(np.int32)
image_pred_postprocessed = np.load(os.path.join(output_folder, "seg_pred.npy")).astype(np.int32)

t1_img, t1km_img, flair_img, adc_img, cbv_img, _ = load_patient_david(patient_id)
cmap = ListedColormap([(0,0,0),  (1,1,0), (0,0,1), (0.3, 0.5, 1),(0,1,0), (1,0,0)])

OUTPUT_PATCH_SIZE = (388, 324)
INPUT_PATCH_SIZE =(370 + 16-370%16 + 180 + 16, 309 + 16-309%16 + 180 + 16)

t1_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1_img, INPUT_PATCH_SIZE, pad_value=None)
t1km_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1km_img, INPUT_PATCH_SIZE, pad_value=None)
flair_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(flair_img, INPUT_PATCH_SIZE, pad_value=None)
adc_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(adc_img, INPUT_PATCH_SIZE, pad_value=None)
cbv_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(cbv_img, INPUT_PATCH_SIZE, pad_value=None)

t1_img = center_crop_image(t1_img, OUTPUT_PATCH_SIZE)
t1km_img = center_crop_image(t1km_img, OUTPUT_PATCH_SIZE)
flair_img = center_crop_image(flair_img, OUTPUT_PATCH_SIZE)
adc_img = center_crop_image(adc_img, OUTPUT_PATCH_SIZE)
cbv_img = center_crop_image(cbv_img, OUTPUT_PATCH_SIZE)

title_fonstize = 24


i = slice_id
image_pred_postprocessed[i-2][0,0:6] = [0,1,2,3,4,5]
seg_combined[i-2][0,0:6] = [0,1,2,3,4,5]
errors = seg_combined[i-2] == image_pred_postprocessed[i-2]
errors[0, 0:2] = [True, False]
plt.figure(figsize=(19,12))
plt.subplot(2,4,1)
        os.mkdir(output_folder)
    t1_img, t1km_img, flair_img, adc_img, cbv_img, seg_combined = load_patient_david(patient_id)
    if t1_img is None:
        continue
    import matplotlib.pyplot as plt
    assert t1km_img.shape == flair_img.shape
    assert t1km_img.shape == adc_img.shape
    assert t1km_img.shape == cbv_img.shape
    t1_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1_img, INPUT_PATCH_SIZE, pad_value=None)
    t1km_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(t1km_img, INPUT_PATCH_SIZE, pad_value=None)
    flair_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(flair_img, INPUT_PATCH_SIZE, pad_value=None)
    adc_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(adc_img, INPUT_PATCH_SIZE, pad_value=None)
    cbv_img = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(cbv_img, INPUT_PATCH_SIZE, pad_value=None)
    seg_combined = SegmentationBatchGeneratorFromRawData.resize_image_by_padding(seg_combined, INPUT_PATCH_SIZE, pad_value=None)
    
    seg_combined = center_crop_image(seg_combined, OUTPUT_PATCH_SIZE)

    print "compiling theano functions"
    # uild_UNet(n_input_channels=1, BATCH_SIZE=None, num_output_classes=2, pad='same', nonlinearity=lasagne.nonlinearities.elu, input_dim=(128, 128), base_n_filters=64, do_dropout=False):
    net = UNet.build_UNet(25, 1, 5, input_dim=INPUT_PATCH_SIZE, base_n_filters=16, pad="valid")
    output_layer = net["output_segmentation"]
    with open(os.path.join(results_folder, "%s_allLossesNAccur_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
        tmp = cPickle.load(f)

    with open(os.path.join(results_folder, "%s_Params_ep%d.pkl" % (experiment_name, epoch)), 'r') as f:
        params = cPickle.load(f)
        lasagne.layers.set_all_param_values(output_layer, params)

    import theano.tensor as T
    data_sym = T.tensor4()