def create_data_gen_train(patients_train, BATCH_SIZE, OUTPUT_PATCH_SIZE, losses, INPUT_PATCH_SIZE):
    data_gen_train = BraTS25015_2D_generator_lossSampling(patients_train, BATCH_SIZE, PATCH_SIZE=tuple(np.array(OUTPUT_PATCH_SIZE)+30), losses=losses, num_batches=None, seed=None)
    data_gen_train = DeepLearningBatchGeneratorUtils.seg_channel_selection_generator(data_gen_train, [2])
    data_gen_train = DeepLearningBatchGeneratorUtils.mirror_axis_generator(data_gen_train)
    data_gen_train = DeepLearningBatchGeneratorUtils.rotation_and_elastic_transform_generator(data_gen_train, 150., 10.)
    data_gen_train = DeepLearningBatchGeneratorUtils.pad_generator(data_gen_train, INPUT_PATCH_SIZE)
    data_gen_train = DeepLearningBatchGeneratorUtils.center_crop_seg_generator(data_gen_train, OUTPUT_PATCH_SIZE)
    data_gen_train = DeepLearningBatchGeneratorUtils.MultiThreadedGenerator(data_gen_train, 6, 6)
    data_gen_train._start()
    return data_gen_train
def compare_seg_with_gt(max_n_images=10, epoch=0):
    data_gen_validation = BraTS25015_2D_generator_lossSampling(all_validation_patients, BATCH_SIZE, PATCH_SIZE=INPUT_PATCH_SIZE, losses=None, num_batches=None, seed=10)
    data_gen_validation = DeepLearningBatchGeneratorUtils.seg_channel_selection_generator(data_gen_validation, [2])
    data_gen_validation = DeepLearningBatchGeneratorUtils.center_crop_seg_generator(data_gen_validation, OUTPUT_PATCH_SIZE)
    data_dict = data_gen_validation.next()
    data = data_dict["data"]
    seg = data_dict["seg"]
    seg = np.array(seg)
    seg_pred = get_segmentation(data)
    plt.figure(figsize=(6, 20))
    n_images = np.min((seg_pred.shape[0], max_n_images))
    for i in range(n_images):
        seg_pred[i][0, :6] = np.array([0,1,2,3,4,5])
        seg[i,0,0,:6] = np.array([0,1,2,3,4,5])
        plt.subplot(n_images, 2, 2*i+1)
        plt.imshow(seg[i, 0])
        plt.subplot(n_images, 2, 2*i+2)
        plt.imshow(seg_pred[i])
    plt.savefig(os.path.join(results_dir, "some_segmentations_ep_%d.png"%epoch))
plt.subplot(1, 3, 1)
plt.imshow(d[0,0], cmap="gray")
plt.subplot(1, 3, 2)
d1=elastic_transform_2d(d[0,0], 550., 20.)
plt.imshow(d1, cmap="gray")
plt.subplot(1, 3, 3)
plt.imshow(d[0,0]-d1)
plt.show()
plt.close()'''

net = build_UNet_relu_BN(20, BATCH_SIZE, num_output_classes=num_classes, base_n_filters=16, input_dim=INPUT_PATCH_SIZE, pad="valid")
output_layer_for_loss = net["output_flattened"]
OUTPUT_PATCH_SIZE = net["output_segmentation"].output_shape[2:]

data_gen_validation = BraTS25015_2D_generator_lossSampling(all_validation_patients, BATCH_SIZE, PATCH_SIZE=INPUT_PATCH_SIZE, losses=None, num_batches=None, seed=None)
data_gen_validation = DeepLearningBatchGeneratorUtils.seg_channel_selection_generator(data_gen_validation, [2])
data_gen_validation = DeepLearningBatchGeneratorUtils.center_crop_seg_generator(data_gen_validation, OUTPUT_PATCH_SIZE)

n_batches_per_epoch = 300
# n_batches_per_epoch = np.floor(n_training_samples/float(BATCH_SIZE))
n_test_batches = 30
# n_test_batches = np.floor(n_val_samples/float(BATCH_SIZE))

x_sym = T.tensor4()
seg_sym = T.ivector()
w_sym = T.vector()

# add some weight decay
l2_loss = lasagne.regularization.regularize_network_params(output_layer_for_loss, lasagne.regularization.l2) * 1e-5

# the distinction between prediction_train and test is important only if we enable dropout