Exemplo n.º 1
0
def sa_data_generator(data):

    # n_slice, n_pixel, n_pixel, slice_x
    slice_x = gbl_get_value("slice_x")
    n_pixel = gbl_get_value("img_shape")[0]
    n_slice = gbl_get_value("img_shape")[2]
    X = np.zeros((n_slice, n_pixel, n_pixel, slice_x))
    Y = np.zeros((n_slice, n_pixel, n_pixel, 1))

    # write X
    if slice_x == 1:
        for idx in range(n_slice):
            X[idx, :, :, 0] = data[:, :, idx]
            Y[idx, :, :, 0] = data[:, :, idx]

    if slice_x == 3:
        for idx in range(n_slice):
            idx_0 = idx - 1 if idx > 0 else 0
            idx_1 = idx
            idx_2 = idx + 1 if idx < n_slice - 1 else n_slice - 1
            X[idx, :, :, 0] = data[:, :, idx_0]
            X[idx, :, :, 1] = data[:, :, idx_1]
            X[idx, :, :, 2] = data[:, :, idx_2]
            Y[idx, :, :, 0] = data[:, :, idx]

    X = X / np.amax(X)
    Y = Y / np.amax(Y)

    return X, Y
Exemplo n.º 2
0
def write_XY(temp_x, data):

    # n_slice, n_pixel, n_pixel, slice_x
    slice_x = gbl_get_value("slice_x")
    n_pixel = gbl_get_value("img_shape")[0]
    n_slice = gbl_get_value("img_shape")[2]
    X = np.zeros((n_slice, n_pixel, n_pixel, slice_x))
    Y = np.zeros((n_slice, n_pixel, n_pixel, 1))

    # write X
    if slice_x == 1:
        for idx in range(n_slice):
            X[idx, :, :, 0] = temp_x[:, :, idx]
            Y[idx, :, :, 0] = data[:, :, idx]

    if slice_x == 3:
        for idx in range(n_slice):
            idx_0 = idx - 1 if idx > 0 else 0
            idx_1 = idx
            idx_2 = idx + 1 if idx < n_slice - 1 else n_slice - 1
            X[idx, :, :, 0] = temp_x[:, :, idx_0]
            X[idx, :, :, 1] = temp_x[:, :, idx_1]
            X[idx, :, :, 2] = temp_x[:, :, idx_2]
            Y[idx, :, :, 0] = data[:, :, idx]

    return X, Y
Exemplo n.º 3
0
def content_loss(y_true, y_pred):
    content_weight = gbl_get_value('content_weight')
    discriminator_path = gbl_get_value('discriminator_path')
    input_size = gbl_get_value('input_size')
    y_true = K.reshape(y_true, (-1, input_size, input_size, 3))

    print('+++++++++++++++++++++++++++++++++++++')
    print('content_loss')
    print(y_true.shape)
    print(y_pred.shape)
    print('+++++++++++++++++++++++++++++++++++++')

    # load the model
    model_path = discriminator_path
    model = load_model(model_path, compile=False)
    for layer in model.layers:
        layer.trainable = False

    selected_layers = [
        'block1_conv2', 'block2_conv2', 'block3_conv3', 'block4_conv3',
        'final_pred'
    ]
    selected_output = [
        model.get_layer(name).output for name in selected_layers
    ]
    layer_model = Model(inputs=model.input, outputs=selected_output)

    feature_pred = layer_model(y_pred)
    feature_true = layer_model(y_true)

    content_loss = mean_squared_error_123(feature_true[2], feature_pred[2])
    content_loss = content_weight * content_loss

    return content_loss
Exemplo n.º 4
0
def style_loss(y_pred, y_true):
    style_weight = gbl_get_value('style_weight')
    batch_size = gbl_get_value("batch_size")
    discriminator_path = gbl_get_value('discriminator_path')
    input_size = gbl_get_value('input_size')

    print(style_weight)
    y_true = K.reshape(y_true, (-1, input_size, input_size, 3))
    y_pred = K.reshape(y_pred, (-1, input_size, input_size, 3))

    print('+++++++++++++++++++++++++++++++++++++')
    print('style_loss')
    print(y_true.shape)
    print(y_pred.shape)
    print('+++++++++++++++++++++++++++++++++++++')

    # load the model
    model_path = discriminator_path
    model = load_model(model_path, compile=False)
    for layer in model.layers:
        layer.trainable = False

    selected_layers = [
        'block1_conv2', 'block2_conv2', 'block3_conv3', 'block4_conv3',
        'final_pred'
    ]
    selected_output = [
        model.get_layer(name).output for name in selected_layers
    ]
    layer_model = Model(inputs=model.input, outputs=selected_output)

    feature_pred = layer_model(y_pred)
    feature_true = layer_model(y_true)

    # feature_gram
    gram_pred = []
    gram_true = []
    for i in range(len(feature_pred) - 1):
        gram_pred.append(gram_matrix(feature_pred[i]))
        gram_true.append(gram_matrix(feature_true[i]))

    style_loss = 0
    for i in range(len(selected_layers) - 1):
        temp = mean_squared_error_12(gram_true[i], gram_pred[i][:batch_size])
        style_loss += temp
    style_loss = style_weight * style_loss

    return style_loss
Exemplo n.º 5
0
def data_generator(data, blur_method, blur_para):

    temp_x = None

    # kernel convolution
    if blur_method == 'kernel_conv':
        ksize = int(blur_para)
        kernel = gaussian_kernel(ksize)
        temp_x = np.zeros(data.shape)
        for idx in range(data.shape[2]):
            temp_x[:, :, idx] = ndimage.convolve(data[:, :, idx],
                                                 kernel,
                                                 mode='constant')

    # skimage gaussian
    if blur_method == 'skimage_gaus':
        sigma = float(blur_para)
        temp_x = gaussian(data, sigma=sigma, multichannel=True)

    # nibabel smooth
    if blur_method == 'nib_smooth':
        fwhm = float(blur_para)
        dir_mri = gbl_get_value("dir_mri")
        file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                             fwhm=fwhm,
                                             mode='nearest')
        temp_x = file_X.get_fdata()

    X, Y = write_XY(temp_x, data)

    X = X / np.amax(X)
    Y = Y / np.amax(Y)

    return X, Y
Exemplo n.º 6
0
def tv_loss(y_true, y_pred):

    tv_weight = gbl_get_value('tv_weight')

    diff_i = K.sum(K.abs(y_pred[:, :, :, 1:] - y_pred[:, :, :, :-1]))
    diff_j = K.sum(K.abs(y_pred[:, :, 1:, :] - y_pred[:, :, :-1, :]))

    tv_loss = tv_weight * (diff_i + diff_j)

    return tv_loss
Exemplo n.º 7
0
def predict(model, data):
    dir_pet = gbl_get_value("dir_pet")
    n_pixel = gbl_get_value("img_shape")[0]
    n_slice = gbl_get_value("img_shape")[2]
    slice_x = gbl_get_value("slice_x")
    model_id = gbl_get_value("model_id")
    # dir_syn = gbl_get_value('dir_syn')
    dir_syn = './'

    run_aim = gbl_get_value("run_aim")
    flag_save = True
    if run_aim == 'see_aug':
        flag_save = False


    file_pet = nib.load(dir_pet)
    factor = np.sum(data)
    data = data / np.amax(data)
    y_hat = np.zeros(data.shape)

    X = np.zeros((1, n_pixel, n_pixel, slice_x))

    if slice_x == 1:
        for idx in range(n_slice):
            X[0, :, :, 0] = data[:, :, idx]
            y_hat[:, :, idx] = np.squeeze(model.predict(X))

    if slice_x == 3:
        for idx in range(n_slice):
            idx_0 = idx-1 if idx > 0 else 0
            idx_1 = idx
            idx_2 = idx+1 if idx < n_slice-1 else n_slice - 1
            X[0, :, :, 0] = data[:, :, idx_0]
            X[0, :, :, 1] = data[:, :, idx_1]
            X[0, :, :, 2] = data[:, :, idx_2]
            y_hat[:, :, idx] = np.squeeze(model.predict(X))

    dif = y_hat - data
    y_hat = y_hat / np.sum(y_hat) * factor

    if flag_save:

        # save nifty file
        affine = file_pet.affine
        header = file_pet.header
        nii_file = nib.Nifti1Image(y_hat, affine, header)
        nib.save(nii_file, dir_syn + 'syn_'+model_id+'.nii')

        # save difference

        dif_file = nib.Nifti1Image(dif, affine, header)
        nib.save(dif_file, dir_syn + 'dif_' + model_id + '.nii')
Exemplo n.º 8
0
def deeprad_backend_norm():

    ui = gbl_get_value("ui")

    # QtWidgets.QMessageBox.information(ui.button_start, "test", "I am in the glue!")

    value_folder = ui.norm_folder.toPlainText().split('\n')
    value_vn = ui.norm_radio_vn.isChecked()
    value_gn = ui.norm_radio_gn.isChecked()
    value_vz = ui.norm_radio_vz.isChecked()
    value_gz = ui.norm_radio_gz.isChecked()
    value_cn = ui.norm_radio_cn.isChecked()
    value_nn = ui.norm_radio_nn.isChecked()

    if ui.norm_radio_cn.isChecked():
        value_shift = float(ui.norm_text_shift.text())
        value_scale = float(ui.norm_text_scale.text())
        value_cropa = float(ui.norm_text_cropa.text())
        value_cropb = float(ui.norm_text_cropb.text())
    else:
        value_shift = 0.0
        value_scale = 1.0
        value_cropa = 100.0
        value_cropb = 0.0

    args = SimpleNamespace(folder=value_folder,
                           volumenorm=value_vn,
                           globalnorm=value_gn,
                           volumezscore=value_vz,
                           globalzscore=value_gz,
                           customnorm=value_cn,
                           nonorm=value_nn,
                           shift=value_shift,
                           scale=value_scale,
                           cropabove=value_cropa,
                           cropbelow=value_cropb,
                           log_output=ui.norm_log_text)
    dr_norm.process_norm(args)
Exemplo n.º 9
0
def enhance_data_generator(data):

    # # kernel convolution
    # ksize = 7
    # kernel = gaussian_kernel(ksize)
    # temp_x = np.zeros(data.shape)
    # for idx in range(data.shape[2]):
    #     temp_x[:, :, idx] = ndimage.convolve(data[:, :, idx], kernel, mode='constant')
    # X1, Y1 = write_XY(temp_x, data)
    #
    # # skimage gaussian
    # sigma = 3
    # temp_x = gaussian(data, sigma=sigma, multichannel=True)
    # X2, Y2 = write_XY(temp_x, data)

    # nibabel smooth
    fwhm = 4
    dir_mri = gbl_get_value("dir_mri")
    file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                         fwhm=fwhm,
                                         mode='nearest')
    temp_x = file_X.get_fdata()
    X1, Y1 = write_XY(temp_x, data)

    # nibabel smooth
    fwhm = 5
    dir_mri = gbl_get_value("dir_mri")
    file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                         fwhm=fwhm,
                                         mode='nearest')
    temp_x = file_X.get_fdata()
    X2, Y2 = write_XY(temp_x, data)

    # nibabel smooth
    fwhm = 6
    dir_mri = gbl_get_value("dir_mri")
    file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                         fwhm=fwhm,
                                         mode='nearest')
    temp_x = file_X.get_fdata()
    X3, Y3 = write_XY(temp_x, data)

    # nibabel smooth
    fwhm = 7
    dir_mri = gbl_get_value("dir_mri")
    file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                         fwhm=fwhm,
                                         mode='nearest')
    temp_x = file_X.get_fdata()
    X4, Y4 = write_XY(temp_x, data)

    # nibabel smooth
    fwhm = 8
    dir_mri = gbl_get_value("dir_mri")
    file_X = nib.processing.smooth_image(nib.load(dir_mri),
                                         fwhm=fwhm,
                                         mode='nearest')
    temp_x = file_X.get_fdata()
    X5, Y5 = write_XY(temp_x, data)

    X = np.vstack((X1, X2, X3, X4, X5))
    Y = np.vstack((Y1, Y2, Y3, Y4, Y5))

    X = X / np.amax(X)
    Y = Y / np.amax(Y)

    return X, Y
Exemplo n.º 10
0
def train_a_unet(train_path, val_path):
    slice_x = gbl_get_value("slice_x")
    n_pixel = gbl_get_value("n_pixel")
    n_slice = gbl_get_value("n_slice")
    model_id = gbl_get_value("model_id")
    dir_model = gbl_get_value('dir_model')
    epochs = gbl_get_value("n_epoch")
    batch_size = gbl_get_value("batch_size")
    flag_save = True

    # ----------------------------------------------Configurations----------------------------------------------#

    # save the log
    log_path = dir_model.split('model')[0] + 'log/'
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    tensorboard = TensorBoard(log_dir=log_path, batch_size=batch_size,
                              write_graph=True, write_grads=True,
                              write_images=True)

    # set traininig configurations
    conf = {"image_shape": (n_pixel, n_pixel, slice_x), "out_channel": 1, "shuffle": True, "augmentation": True,
            "learning_rate": 1e-4, "validation_split": 0.3, "batch_size": batch_size, "epochs": epochs,
            "model_id": model_id}
    np.save(log_path + model_id + '_info.npy', conf)

    # set augmentation configurations
    conf_a = {"rotation_range": 15, "shear_range": 10,
              "width_shift_range": 0.33, "height_shift_range": 0.33, "zoom_range": 0.33,
              "horizontal_flip": True, "vertical_flip": True, "fill_mode": 'nearest',
              "seed": 314, "batch_size": conf["batch_size"]}
    np.save(log_path + model_id + '__aug.npy', conf_a)

    # save the models which has the minimum validation loss or highest accuracy
    if flag_save:
        check_path1 = dir_model+'loss_model_'+model_id+'.hdf5'
        checkpoint1 = ModelCheckpoint(check_path1, monitor='val_loss',
                                      verbose=1, save_best_only=True, save_weights_only=False, mode='min')
        check_path2 = dir_model+'acc_model_'+model_id+'.hdf5'
        checkpoint2 = ModelCheckpoint(check_path2, monitor='val_acc',
                                      verbose=1, save_best_only=True, save_weights_only=False, mode='max')
        callbacks_list = [checkpoint1, checkpoint2, tensorboard]
    else:
        callbacks_list = [tensorboard]

    # ----------------------------------------------Create Model----------------------------------------------#
    # build up the model
    if gbl_get_value("pretrained_flag") == 0:
        model = vgg16_model()
        # model = multi_gpu_model(model, 2)
    else:
        model_path = gbl_get_value("pretrained_path")
        model = load_model(model_path)

    opt = RAdam(learning_rate=conf['learning_rate'], total_steps=10000, warmup_proportion=0.1, min_lr=1e-6)

    # ----------------------------------------------Data Generator----------------------------------------------#
    # train data_generator
    data_generator1 = ImageDataGenerator(rescale=1./255,
                                         rotation_range=conf_a["rotation_range"],
                                         shear_range=conf_a["shear_range"],
                                         width_shift_range=conf_a["width_shift_range"],
                                         height_shift_range=conf_a["height_shift_range"],
                                         zoom_range=conf_a["zoom_range"],
                                         horizontal_flip=conf_a["horizontal_flip"],
                                         vertical_flip=conf_a["vertical_flip"],
                                         fill_mode=conf_a["fill_mode"])
                                         # preprocessing_function=aug_noise)

    # validation data_generator
    data_generator3 = ImageDataGenerator(rescale=1./255,
                                         width_shift_range=conf_a["width_shift_range"],
                                         height_shift_range=conf_a["height_shift_range"],
                                         zoom_range=conf_a["zoom_range"],
                                         horizontal_flip=conf_a["horizontal_flip"],
                                         vertical_flip=conf_a["vertical_flip"],
                                         fill_mode=conf_a["fill_mode"])
                                         # preprocessing_function=aug_noise)
    aug_dir = ''

    # ----------------------------------------------Train Model----------------------------------------------#

    # compile
    model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['binary_crossentropy', 'acc'])

    train_path = train_path
    val_path = val_path

    # train
    model.fit_generator(generator=data_generator1.flow_from_directory(directory=train_path, target_size=(512, 512),
                                                       color_mode='rgb', classes=None, class_mode="binary",
                                                       batch_size=conf_a["batch_size"], seed=conf_a["seed"],
                                                       shuffle=True, save_to_dir=aug_dir, save_prefix='train'),
                        steps_per_epoch=int(int(n_slice * (1-conf["validation_split"])) / conf_a["batch_size"]),
                        epochs=conf["epochs"],
                        callbacks=callbacks_list,
                        validation_data=data_generator3.flow_from_directory(directory=val_path, target_size=(512, 512),
                                                             color_mode='rgb', classes=None, class_mode="binary",
                                                             batch_size=conf_a["batch_size"], seed=conf_a["seed"],
                                                             shuffle=True, save_to_dir=aug_dir, save_prefix='val'),
                        validation_steps=int(int(n_slice * conf["validation_split"]) / conf_a["batch_size"]))

    return model
Exemplo n.º 11
0
def train_a_unet(X, Y):

    slice_x = gbl_get_value("slice_x")
    n_pixel = X.shape[2]
    n_slice = X.shape[0]
    model_id = gbl_get_value("model_id")
    # dir_model = gbl_get_value('dir_model')
    dir_model = './'

    epochs = gbl_get_value("n_epoch")
    n_fliter = gbl_get_value("n_filter")
    depth = gbl_get_value("depth")
    batch_size = gbl_get_value("batch_size")
    optimizer = 'Adam'

    run_aim = gbl_get_value("run_aim")
    flag_save = True
    if run_aim == 'see_aug':
        flag_save = False

    # ----------------------------------------------Configurations----------------------------------------------#

    # logs
    log_path = './logs/' + model_id + "/"
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    tensorboard = TensorBoard(log_dir=log_path,
                              batch_size=batch_size,
                              write_graph=True,
                              write_grads=True,
                              write_images=True)

    # set traininig configurations
    conf = {
        "image_shape": (n_pixel, n_pixel, slice_x),
        "out_channel": 1,
        "filter": n_fliter,
        "depth": depth,
        "inc_rate": 2,
        "activation": 'relu',
        "dropout": True,
        "batchnorm": True,
        "maxpool": True,
        "upconv": True,
        "residual": True,
        "shuffle": True,
        "augmentation": True,
        "learning_rate": 1e-5,
        "decay": 0.0,
        "epsilon": 1e-8,
        "beta_1": 0.9,
        "beta_2": 0.999,
        "validation_split": 0.2632,
        "batch_size": batch_size,
        "epochs": epochs,
        "loss": "mse1e6",
        "metric": "mse",
        "optimizer": optimizer,
        "model_id": model_id
    }
    np.save(log_path + model_id + '_info.npy', conf)

    # set augmentation configurations
    conf_a = {
        "rotation_range": 15,
        "shear_range": 10,
        "width_shift_range": 0.33,
        "height_shift_range": 0.33,
        "zoom_range": 0.33,
        "horizontal_flip": True,
        "vertical_flip": True,
        "fill_mode": 'nearest',
        "seed": 314,
        "batch_size": conf["batch_size"]
    }
    np.save(log_path + model_id + '__aug.npy', conf_a)

    # checkpoint
    #     check_path= './training_models/' + model_type + '_' + str(LOOCV) + '/'
    #     if not os.path.exists(check_path):
    #         os.makedirs(check_path)
    if flag_save:
        check_path = dir_model + 'model_' + model_id + '.hdf5'  # _{epoch:03d}_{val_loss:.4f}
        checkpoint1 = ModelCheckpoint(check_path,
                                      monitor='val_psnr',
                                      verbose=1,
                                      save_best_only=True,
                                      mode='max')
        #     checkpoint2 = ModelCheckpoint(check_path, period=100)
        callbacks_list = [checkpoint1, tensorboard]
    else:
        callbacks_list = [tensorboard]

    # ----------------------------------------------Create Model----------------------------------------------#

    # build up the model
    model = unet(img_shape=conf["image_shape"],
                 out_ch=conf["out_channel"],
                 start_ch=conf["filter"],
                 depth=conf["depth"],
                 inc_rate=conf["inc_rate"],
                 activation=conf["activation"],
                 dropout=conf["dropout"],
                 batchnorm=conf["batchnorm"],
                 maxpool=conf["maxpool"],
                 upconv=conf["upconv"],
                 residual=conf["residual"])

    # Adam optimizer
    # if conf["optimizer"] == 'Adam':
    #     opt = Adam(lr=conf["learning_rate"], decay=conf["decay"],
    #                epsilon=conf["epsilon"], beta_1=conf["beta_1"], beta_2=conf["beta_2"])
    # if conf["loss"] == 'mse1e6':
    #     loss = mean_squared_error_1e6

    loss = mean_squared_error_1e6
    opt = Adam(lr=conf["learning_rate"],
               decay=conf["decay"],
               epsilon=conf["epsilon"],
               beta_1=conf["beta_1"],
               beta_2=conf["beta_2"])

    # load dataset [80, n_pixel, n_pixel, 1]
    # x_val = np.zeros((int(n_slice * 0.3), n_pixel, n_pixel, 1), dtype=np.float32)
    # y_val = np.zeros((int(n_slice * 0.3), n_pixel, n_pixel, 1), dtype=np.float32)
    # x_train = np.zeros((int(n_slice * 0.7) + 1, n_pixel, n_pixel, 1), dtype=np.float32)
    # y_train = np.zeros((int(n_slice * 0.7) + 1, n_pixel, n_pixel, 1), dtype=np.float32)
    #
    # temp_x = X
    # temp_y = Y
    # list_cand = []
    #
    # idx_x = 0
    # while idx_x < int(n_slice * 0.3):
    #     idx_slice = int(np.random.rand() * n_slice)
    #     if not (idx_slice in list_cand):
    #         list_cand.append(idx_slice)
    #         x_val[idx_x, :, :, :] = temp_x[:, :, idx_slice].reshape((1, n_pixel, n_pixel, 1))
    #         y_val[idx_x, :, :, :] = temp_x[:, :, idx_slice].reshape((1, n_pixel, n_pixel, 1))
    #         idx_x += 1
    #
    # idx_x = 0
    # for i in range(n_slice):
    #     if not (i in list_cand):
    #         x_train[idx_x, :, :, :] = temp_x[:, :, i].reshape((1, n_pixel, n_pixel, 1))
    #         y_train[idx_x, :, :, :] = temp_y[:, :, i].reshape((1, n_pixel, n_pixel, 1))
    #         idx_x = idx_x + 1

    X = X.reshape((n_slice, n_pixel, n_pixel, slice_x))
    Y = Y.reshape((n_slice, n_pixel, n_pixel, 1))

    x_train, x_val, y_train, y_val = train_test_split(X,
                                                      Y,
                                                      test_size=0.33,
                                                      random_state=42)
    x_train = x_train / np.amax(x_train)
    y_train = y_train / np.amax(y_train)
    x_val = x_val / np.amax(x_val)
    y_val = y_val / np.amax(y_val)

    # ----------------------------------------------Data Generator----------------------------------------------#

    # train data_generator
    data_generator1 = ImageDataGenerator(
        rotation_range=conf_a["rotation_range"],
        shear_range=conf_a["shear_range"],
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"],
        preprocessing_function=aug_noise)
    data_generator2 = ImageDataGenerator(
        rotation_range=conf_a["rotation_range"],
        shear_range=conf_a["shear_range"],
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"],
        preprocessing_function=aug_noise)

    # validation data_generator
    data_generator3 = ImageDataGenerator(
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"],
        preprocessing_function=aug_noise)
    data_generator4 = ImageDataGenerator(
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"],
        preprocessing_function=aug_noise)

    # set generator
    # data_generator1.fit(x_train, seed=conf_a["seed"])
    # data_generator2.fit(y_train, seed=conf_a["seed"])
    # data_generator3.fit(x_val, seed=conf_a["seed"])
    # data_generator4.fit(y_val, seed=conf_a["seed"])

    if run_aim == "see_aug":
        # aug dir
        aug_dir = './aug_files/' + model_id + '/'
        if not os.path.exists(aug_dir):
            os.makedirs(aug_dir)
    else:
        aug_dir = ''

    # # save files
    # data_generator1.flow(x_train, seed=conf_a["seed"], save_to_dir=aug_dir, save_prefix='train_x')
    # data_generator2.flow(y_train, seed=conf_a["seed"], save_to_dir=aug_dir, save_prefix='train_y')
    # data_generator3.flow(x_val, seed=conf_a["seed"], save_to_dir=aug_dir, save_prefix='val_x')
    # data_generator4.flow(y_val, seed=conf_a["seed"], save_to_dir=aug_dir, save_prefix='val_y')

    # zip files
    data_generator_t = zip(
        data_generator1.flow(x=x_train,
                             y=None,
                             batch_size=conf_a["batch_size"],
                             seed=conf_a["seed"],
                             save_to_dir=aug_dir,
                             save_prefix='train_x'),
        data_generator2.flow(x=y_train,
                             y=None,
                             batch_size=conf_a["batch_size"],
                             seed=conf_a["seed"],
                             save_to_dir=aug_dir,
                             save_prefix='train_y'))
    data_generator_v = zip(
        data_generator3.flow(x=x_val,
                             y=None,
                             batch_size=conf_a["batch_size"],
                             seed=conf_a["seed"],
                             save_to_dir=aug_dir,
                             save_prefix='val_x'),
        data_generator4.flow(x=y_val,
                             y=None,
                             batch_size=conf_a["batch_size"],
                             seed=conf_a["seed"],
                             save_to_dir=aug_dir,
                             save_prefix='val_y'))

    # ----------------------------------------------Train Model----------------------------------------------#

    # compile
    model.compile(loss=loss,
                  optimizer=opt,
                  metrics=[mean_squared_error_1e6, psnr])

    # train
    model.fit_generator(
        generator=data_generator_t,
        steps_per_epoch=int(int(n_slice * 0.7) / conf_a["batch_size"]),  #
        epochs=conf["epochs"],
        callbacks=callbacks_list,
        validation_data=data_generator_v,
        validation_steps=int(int(n_slice * 0.3) / conf_a["batch_size"]))  #

    return model
Exemplo n.º 12
0
def deeprad_backend_n2i():

    ui = gbl_get_value("ui")

    value_outfolder = ui.n2i_folder_output.toPlainText()
    value_X = [ui.n2i_folder_X.toPlainText()]
    value_Y = [ui.n2i_folder_Y.toPlainText()]

    try:
        value_axes = [int(ui.n2i_text_axes.text())]
    except ValueError:
        value_axes = None

    try:
        value_imsize_w = int(ui.n2i_text_imsize_w.text())
    except ValueError:
        value_imsize_w = None

    try:
        value_imsize_h = int(ui.n2i_text_imsize_h.text())
    except ValueError:
        value_imsize_h = None

    try:
        value_testfraction = int(ui.n2i_text_testfraction.text())
    except ValueError:
        value_testfraction = None

    try:
        value_valfraction = int(ui.n2i_text_valfraction.text())
    except ValueError:
        value_valfraction = None

    try:
        value_Xslices = int(ui.n2i_text_xslices.text())
    except ValueError:
        value_Xslices = None

    try:
        value_Yslices = int(ui.n2i_text_yslices.text())
    except ValueError:
        value_Yslices = None

    value_force = ui.n2i_check_force.isChecked()
    value_shuffle = ui.n2i_check_shuffle.isChecked()

    if value_imsize_w is None and value_imsize_h is None:
        value_imsize = None
    else:
        value_imsize = [value_imsize_w, value_imsize_h]

    # augmentation part
    if ui.n2i_check_aug.isChecked():
        value_augfactor = int(ui.n2i_text_augfactor.text())
        value_augseed = int(ui.n2i_text_augseed.text())
        value_addnoise = float(ui.n2i_text_addnoise.text())
        value_rotations = float(ui.n2i_text_rotations.text())
        value_scalings = float(ui.n2i_text_scalings.text())
        value_shears = float(ui.n2i_text_shears.text())
        value_translations = float(ui.n2i_text_translations.text())
        value_hflips = ui.n2i_check_hflips.isChecked()
        value_vflips = ui.n2i_check_vflips.isChecked()

        value_augmode = None
        if ui.n2i_radio_augmode_reflect.isChecked():
            value_augmode = 'reflect'
        if ui.n2i_radio_augmode_nearest.isChecked():
            value_augmode = 'nearest'
        if ui.n2i_radio_augmode_mirror.isChecked():
            value_augmode = 'mirror'
        if ui.n2i_radio_augmode_warp.isChecked():
            value_augmode = 'warp'

    else:
        value_augfactor = 1
        value_augseed = 0
        value_addnoise = 0
        value_rotations = 0
        value_scalings = 0
        value_shears = 0
        value_translations = 0
        value_hflips = False
        value_vflips = False
        value_augmode = None

    args = SimpleNamespace(outfolder=value_outfolder,
                           X=value_X,
                           Y=value_Y,
                           axes=value_axes,
                           imsize=value_imsize,
                           Xslices=value_Xslices,
                           Yslices=value_Yslices,
                           force=value_force,
                           shuffle=value_shuffle,
                           testfraction=value_testfraction,
                           valfraction=value_valfraction,
                           augfactor=value_augfactor,
                           augseed=value_augseed,
                           addnoise=value_addnoise,
                           rotations=value_rotations,
                           scalings=value_scalings,
                           shears=value_shears,
                           translations=value_translations,
                           hflips=value_hflips,
                           vflips=value_vflips,
                           augmode=value_augmode,
                           log_output=ui.n2i_log_text)

    # np.save('args_n2i.npy', args)
    dr_n2i.process_n2i(args)
Exemplo n.º 13
0
def predict_MRCT(model, test_path, tag=''):
    path_X = glob.glob(test_path + '*Align*.nii')[-1]
    file_X = nib.load(path_X)
    data_X = file_X.get_fdata()

    path_Y = glob.glob(test_path + '*CT*.nii')[-1]
    file_Y = nib.load(path_Y)
    data_Y = file_Y.get_fdata()

    # MaxMin-norm
    data_input, X_max, X_min = MaxMinNorm(data_X, keep_para=True)

    print("data shape", data_input.shape)

    n_pixel = data_X.shape[0]
    n_slice = data_X.shape[2]
    slice_x = gbl_get_value("slice_x")
    model_id = gbl_get_value("model_id")
    dir_syn = gbl_get_value('dir_syn')

    # print("y_hat shape: ", y_hat.shape)

    X = np.zeros((1, n_pixel, n_pixel, slice_x))
    y_hat = np.zeros((n_pixel, n_pixel, n_slice))
    if slice_x == 1:
        for idx in range(n_slice):
            X[0, :, :, 0] = data_input[:, :, idx]
            y_hat[:, :, idx] = np.squeeze(model.predict(X))

    if slice_x == 3:
        for idx in range(n_slice):
            idx_0 = idx - 1 if idx > 0 else 0
            idx_1 = idx
            idx_2 = idx + 1 if idx < n_slice - 1 else n_slice - 1
            X[0, :, :, 0] = data_input[:, :, idx_0]
            X[0, :, :, 1] = data_input[:, :, idx_1]
            X[0, :, :, 2] = data_input[:, :, idx_2]
            y_hat[:, :, idx] = np.squeeze(model.predict(X))

    print("Output:")
    print("Mean:", np.mean(y_hat))
    print("STD:", np.std(y_hat))
    print("Max:", np.amax(y_hat))
    print("Min:", np.amax(y_hat))

    # norm y_hat
    y_hat_norm = MaxMinNorm(y_hat)

    # restore norm
    y_hat_norm *= (X_max - X_min)
    y_hat_norm += X_min
    dif = y_hat_norm - data_Y

    # save nifty file
    affine = file_Y.affine
    header = file_Y.header
    nii_file = nib.Nifti1Image(y_hat_norm, affine, header)
    nib.save(nii_file, dir_syn + 'syn_' + model_id + '_' + tag + '.nii')
    print(dir_syn + 'syn_' + model_id + '.nii')

    # save difference

    dif_file = nib.Nifti1Image(dif, affine, header)
    nib.save(dif_file, dir_syn + 'dif_' + model_id + '_' + tag + '.nii')
Exemplo n.º 14
0
def train_a_unet(data_path):

    slice_x = gbl_get_value("slice_x")
    n_slice_train = gbl_get_value("n_slice_train")
    n_slice_val = gbl_get_value("n_slice_val")
    n_pixel = gbl_get_value('input_size')
    model_id = gbl_get_value("model_id")
    dir_model = gbl_get_value('dir_model')

    epochs = gbl_get_value("n_epoch")
    n_fliter = gbl_get_value("n_filter")
    depth = gbl_get_value("depth")
    batch_size = gbl_get_value("batch_size")
    optimizer = 'RAdam'
    flag_save = True

    # ----------------------------------------------Configurations----------------------------------------------#

    # save logs
    log_path = dir_model.split('model')[0] + 'log/'
    if not os.path.exists(log_path):
        os.makedirs(log_path)
    tensorboard = TensorBoard(log_dir=log_path,
                              batch_size=batch_size,
                              write_graph=True,
                              write_grads=True,
                              write_images=True)

    # set traininig configurations
    conf = {
        "image_shape": (n_pixel, n_pixel, slice_x),
        "out_channel": 1,
        "filter": n_fliter,
        "depth": depth,
        "inc_rate": 2,
        "activation": 'relu',
        "dropout": True,
        "batchnorm": True,
        "maxpool": True,
        "upconv": True,
        "residual": True,
        "shuffle": True,
        "augmentation": True,
        "learning_rate": 1e-4,
        "decay": 0.0,
        "epsilon": 1e-8,
        "beta_1": 0.9,
        "beta_2": 0.999,
        "validation_split": 0.3,
        "batch_size": batch_size,
        "epochs": epochs,
        "loss": "mse1e6",
        "metric": "mse",
        "optimizer": optimizer,
        "model_id": model_id
    }
    np.save(log_path + model_id + '_info.npy', conf)

    # set augmentation configurations
    conf_a = {
        "rotation_range": 15,
        "shear_range": 10,
        "width_shift_range": 0.33,
        "height_shift_range": 0.33,
        "zoom_range": 0.33,
        "horizontal_flip": True,
        "vertical_flip": True,
        "fill_mode": 'nearest',
        "seed": 314,
        "batch_size": conf["batch_size"]
    }
    np.save(log_path + model_id + '__aug.npy', conf_a)

    if flag_save:
        check_path_1 = dir_model + 'psnr_model_' + model_id + '.hdf5'  # _{epoch:03d}_{val_loss:.4f}
        checkpoint1 = ModelCheckpoint(check_path_1,
                                      monitor='val_psnr',
                                      verbose=1,
                                      save_best_only=True,
                                      mode='max')
        check_path_2 = dir_model + 'loss_model_' + model_id + '.hdf5'  # _{epoch:03d}_{val_loss:.4f}
        checkpoint2 = ModelCheckpoint(check_path_2,
                                      monitor='val_loss',
                                      verbose=1,
                                      save_best_only=True,
                                      mode='min')
        callbacks_list = [checkpoint1, checkpoint2, tensorboard]
    else:
        callbacks_list = [tensorboard]

    # ----------------------------------------------Create Model----------------------------------------------#

    # build up the model
    print(conf)

    if gbl_get_value("pretrained_flag") == 0:
        print('-----------------start with new model---------------')
        model = unet(img_shape=conf["image_shape"],
                     out_ch=conf["out_channel"],
                     start_ch=conf["filter"],
                     depth=conf["depth"],
                     inc_rate=conf["inc_rate"],
                     activation=conf["activation"],
                     dropout=conf["dropout"],
                     batchnorm=conf["batchnorm"],
                     maxpool=conf["maxpool"],
                     upconv=conf["upconv"],
                     residual=conf["residual"])
        # model = multi_gpu_model(model, 3)

    else:
        # load model
        print('-----------------fine tune previous models----------------')
        model_path = gbl_get_value("pretrained_path")
        model = load_model(model_path, compile=False)

    # for the perceptual loss model, the loss function is perceptual loss
    loss = perceptual_loss

    # for the mse loss model, the loss function is mse loss
    # loss = mean_squared_error_1e6

    opt = RAdam(learning_rate=conf['learning_rate'],
                total_steps=10000,
                warmup_proportion=0.1,
                min_lr=1e-6)

    # ----------------------------------------------Data Generator----------------------------------------------#

    # train data_generator
    data_generator1 = ImageDataGenerator(
        rescale=1. / 255,
        rotation_range=conf_a["rotation_range"],
        shear_range=conf_a["shear_range"],
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"])
    # preprocessing_function=aug_noise)
    data_generator2 = ImageDataGenerator(
        rescale=1. / 255,
        rotation_range=conf_a["rotation_range"],
        shear_range=conf_a["shear_range"],
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"])
    # preprocessing_function=aug_noise)

    # validation data_generator
    data_generator3 = ImageDataGenerator(
        rescale=1. / 255,
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"])
    # preprocessing_function=aug_noise)
    data_generator4 = ImageDataGenerator(
        rescale=1. / 255,
        width_shift_range=conf_a["width_shift_range"],
        height_shift_range=conf_a["height_shift_range"],
        zoom_range=conf_a["zoom_range"],
        horizontal_flip=conf_a["horizontal_flip"],
        vertical_flip=conf_a["vertical_flip"],
        fill_mode=conf_a["fill_mode"])
    # preprocessing_function=aug_noise)

    aug_dir = ''

    train_x_path = data_path + 'train/train_x/'
    train_y_path = data_path + 'train/train_y/'
    val_x_path = data_path + 'val/val_x/'
    val_y_path = data_path + 'val/val_y'

    # zip files
    data_generator_t = zip(
        data_generator1.flow_from_directory(train_x_path,
                                            target_size=(512, 512),
                                            color_mode='grayscale',
                                            classes=None,
                                            class_mode=None,
                                            batch_size=conf_a["batch_size"],
                                            shuffle=True,
                                            seed=conf_a["seed"],
                                            save_to_dir=aug_dir,
                                            save_prefix='train_x'),
        data_generator2.flow_from_directory(train_y_path,
                                            target_size=(512, 512),
                                            color_mode='rgb',
                                            classes=None,
                                            class_mode=None,
                                            batch_size=conf_a["batch_size"],
                                            shuffle=True,
                                            seed=conf_a["seed"],
                                            save_to_dir=aug_dir,
                                            save_prefix='train_y'))

    data_generator_v = zip(
        data_generator3.flow_from_directory(val_x_path,
                                            target_size=(512, 512),
                                            color_mode='grayscale',
                                            classes=None,
                                            class_mode=None,
                                            batch_size=conf_a["batch_size"],
                                            shuffle=True,
                                            seed=conf_a["seed"],
                                            save_to_dir=aug_dir,
                                            save_prefix='val_x'),
        data_generator4.flow_from_directory(val_y_path,
                                            target_size=(512, 512),
                                            color_mode='rgb',
                                            classes=None,
                                            class_mode=None,
                                            batch_size=conf_a["batch_size"],
                                            shuffle=True,
                                            seed=conf_a["seed"],
                                            save_to_dir=aug_dir,
                                            save_prefix='val_y'))

    # ----------------------------------------------Train Model----------------------------------------------#

    # compile
    model.compile(loss=loss,
                  optimizer=opt,
                  metrics=[
                      content_loss, style_loss, psnr, mean_squared_error_1e6,
                      mean_absolute_error_1e6
                  ])

    # train
    model.fit_generator(
        generator=data_generator_t,
        steps_per_epoch=int(n_slice_train / conf_a["batch_size"]),
        epochs=conf["epochs"],
        callbacks=callbacks_list,
        validation_data=data_generator_v,
        validation_steps=int(n_slice_val / conf_a["batch_size"]))  #

    return model
Exemplo n.º 15
0
def perceptual_loss(y_true, y_pred):

    batch_size = gbl_get_value("batch_size")
    style_weight = gbl_get_value('style_weight')
    content_weight = gbl_get_value('content_weight')
    tv_weight = gbl_get_value('tv_weight')
    binary_weight = gbl_get_value('binary_weight')
    discriminator_path = gbl_get_value('discriminator_path')
    input_size = gbl_get_value('input_size')

    y_true = K.reshape(y_true, (-1, input_size, input_size, 3))
    mse_loss = mean_squared_error_1e6(y_true, y_pred)

    print('+++++++++++++++++++++++++++++++++++++')
    print('perceptual_loss')
    print(y_true.shape)
    print(y_pred.shape)
    print('+++++++++++++++++++++++++++++++++++++')

    # load the model
    model_path = discriminator_path
    model = load_model(model_path, compile=False)
    for layer in model.layers:
        layer.trainable = False

    selected_layers = [
        'block1_conv2', 'block2_conv2', 'block3_conv3', 'block4_conv3',
        'final_pred'
    ]
    selected_output = [
        model.get_layer(name).output for name in selected_layers
    ]
    layer_model = Model(inputs=model.input, outputs=selected_output)

    feature_pred = layer_model(y_pred)
    feature_true = layer_model(y_true)

    # feature_gram
    gram_pred = []
    gram_true = []
    for i in range(len(feature_pred) - 1):
        gram_pred.append(gram_matrix(feature_pred[i]))
        gram_true.append(gram_matrix(feature_true[i]))

    one = tf.ones_like(feature_true[-1])
    feature_true[-1] = tf.where(feature_true[-1] >= 0, x=one, y=one)

    style_loss = 0
    for i in range(len(selected_layers) - 1):
        temp = mean_squared_error_12(gram_true[i], gram_pred[i][:batch_size])
        style_loss += temp
    style_loss = style_weight * style_loss

    content_loss = mean_squared_error_123(feature_true[2], feature_pred[2])
    content_loss = content_weight * content_loss

    diff_i = K.sum(K.abs(y_pred[:, :, :, 1:] - y_pred[:, :, :, :-1]))
    diff_j = K.sum(K.abs(y_pred[:, :, 1:, :] - y_pred[:, :, :-1, :]))

    tv_loss = tv_weight * (diff_i + diff_j)

    final_percep_loss = style_loss + content_loss + tv_loss

    return final_percep_loss