def w_train(model, X, Y, n_epoch): header = GL_get_value("header") affine = GL_get_value("affine") fig = plt.figure(figsize=(10, 5)) fig.show(False) save_path = '.\\mid_results\\' + GL_get_value("MODEL_ID") + "\\" if not os.path.exists(save_path): os.makedirs(save_path) for idx_epoch in range(n_epoch): curr_loss = model.train_on_batch(X, Y) if idx_epoch % GL_get_value("gap_flash") == 0: fig.clf() # a = fig.add_subplot(1, 3, 1) # plt.imshow(np.rot90(X[0, :, :, 0]), cmap='gray') # a.axis('off') # a.set_title('X') a = fig.add_subplot(1, 2, 1) plt.imshow(np.rot90(Y[0, :, :, 0], 3), cmap='gray') a.axis('off') a.set_title('Y') Y_ = model.predict(X) a = fig.add_subplot(1, 2, 2) plt.imshow(np.rot90(Y_[0, :, :, 0], 3), cmap='gray') a.axis('off') a.set_title('\^Y') fig.tight_layout() fig.canvas.draw() fig.savefig(save_path+'progress_dip_{0:05d}.jpg'.format(idx_epoch)) fig.canvas.flush_events()
def w_pred(model, X, Y, n_epoch): FA_NORM = GL_get_value("FA_NORM") for idx_epoch in range(n_epoch): curr_loss = model.train_on_batch(X, Y) fig = plt.figure(figsize=(10, 5)) fig.show(False) save_path = '.\\mid_results\\' + GL_get_value("MODEL_ID") + "\\" if not os.path.exists(save_path): os.makedirs(save_path) fig.clf() a = fig.add_subplot(1, 2, 1) plt.imshow(np.rot90(Y[0, :, :, 0]), cmap='gray') a.axis('off') a.set_title('Y') Y_ = model.predict(X) a = fig.add_subplot(1, 2, 2) plt.imshow(np.rot90(Y_[0, :, :, 0]), cmap='gray') a.axis('off') a.set_title('\^Y') fig.tight_layout() fig.canvas.draw() fig.savefig(save_path + 'progress_dip_{0:03d}.jpg'.format(GL_get_value("IDX_SLICE"))) fig.canvas.flush_events() np.save(save_path + 'progress_dip_{0:03d}.nii'.format(GL_get_value("IDX_SLICE")), Y_*FA_NORM) # nii = GL_get_value("nii") # nii.append(Y_) # GL_set_value("nii", nii) return None
def w_output(): header = GL_get_value("header") affine = GL_get_value("affine") save_path = '.\\mid_results\\' + GL_get_value("MODEL_ID") + "\\" if not os.path.exists(save_path): os.makedirs(save_path) nii = np.asarray(GL_get_value("nii")) nii_file_0 = nib.Nifti1Image(nii, affine, header) nib.save(nii_file_0, save_path + 'nii_syn.nii.gz')
def Gray_White_CSF_soomth(y_true, y_pred): w_pgwc = (GL_get_value("W_PGWC")) # [PET, Gray, White, CSF] weight = [int(w_pgwc[0]), int(w_pgwc[1]), int(w_pgwc[2]), int(w_pgwc[3])] smooth_kernel = make_kernel() y_true_smooth = K.conv2d(y_true, smooth_kernel, padding='same') pet_error = K.mean(K.square(y_pred[:, :, :, 0] - y_true_smooth[:, :, :, 0]), axis=-1) csf_mask = y_true_smooth[:, :, :, 1] csf_true = csf_mask * y_true[:, :, :, 0] csf_pred = csf_mask * y_pred[:, :, :, 0] # csf_error = K.mean(K.square(csf_pred - csf_true), axis=-1) csf_error = K.max(K.square(csf_pred)) - K.mean(K.square(csf_true)) gm_mask = y_true_smooth[:, :, :, 2] gm_true = gm_mask * y_true[:, :, :, 0] gm_pred = gm_mask * y_pred[:, :, :, 0] gm_error = K.mean(K.square(gm_pred - gm_true), axis=-1) wm_mask = y_true_smooth[:, :, :, 3] # wm_mask = np.bitwise_and(wm_mask == 1, y_true[:, :, :, 0] < MRI_TH) wm_true = wm_mask * y_true[:, :, :, 0] wm_pred = wm_mask * y_pred[:, :, :, 0] # wm_sum = K.sum( wm_pred, axis=-1 ) # wm_error = K.mean(K.square(wm_pred - wm_true), axis=-1) wm_error = K.max(K.square(wm_pred)) - K.mean(K.square(wm_true)) return pet_error * weight[0] + gm_error * weight[1] + wm_error * weight[ 2] + csf_error * weight[3]
def loss_breast_practical(y_true, y_pred): w_pet = GL_get_value("w_pet") w_water = GL_get_value("w_water") w_fat = GL_get_value("w_fat") fat_mask = GL_get_value("img_ff") # fat_mask = y_true[0, :, :, 2] fat_true = fat_mask * y_true[0, :, :, 0] fat_pred = fat_mask * y_pred[0, :, :, 0] loss1 = K.mean(K.square(y_pred[0, :, :, 0] - y_true[0, :, :, 0]), axis=-1) # PET loss2 = K.mean(K.square(y_pred[0, :, :, 0] - y_true[0, :, :, 1]), axis=-1) # water loss3 = K.max(K.square(fat_pred)) - K.mean(K.square(fat_true)) # fat return loss1 * w_pet + loss2 * w_water + loss3 * w_fat
def w_train_breast(model, X, Y, n_epoch): img_ff = GL_get_value("img_ff") img_wf = GL_get_value("img_wf") fig = plt.figure(figsize=(16, 4)) fig.show(False) save_path = '.\\mid_results\\' + GL_get_value("MODEL_ID") + "\\" if not os.path.exists(save_path): os.makedirs(save_path) for idx_epoch in range(n_epoch): curr_loss = model.train_on_batch(X, Y) if idx_epoch % GL_get_value("gap_flash") == 0: fig.clf() a = fig.add_subplot(1, 4, 1) plt.imshow(np.rot90(X[0, :, :, 0], 3), cmap='gray') a.axis('off') a.set_title('orginal PET') a = fig.add_subplot(1, 4, 2) plt.imshow(np.rot90(img_wf, 3), cmap='gray') a.axis('off') a.set_title('orginal water') a = fig.add_subplot(1, 4, 3) plt.imshow(np.rot90(img_ff, 3), cmap='gray') a.axis('off') a.set_title('orginal fat') Y_ = model.predict(X) a = fig.add_subplot(1, 4, 4) plt.imshow(np.rot90(Y_[0, :, :, 0], 3), cmap='gray') a.axis('off') a.set_title('modified PET') fig.tight_layout() fig.canvas.draw() fig.savefig(save_path+'progress_dip_{0:05d}.jpg'.format(idx_epoch)) fig.canvas.flush_events()
def data_pre_breast_practical(data_mri_water, data_mri_fat, data_pet): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") IDX_SLICE = GL_get_value("IDX_SLICE") X = np.zeros((1, IMG_ROWS, IMG_COLS, 3)) Y = np.zeros((1, IMG_ROWS, IMG_COLS, 3)) GL_set_value("FA_NORM", np.amax(data_pet)) img_p = data_pet[:, :, IDX_SLICE] mask_pet = np.asarray([img_p > 350]).reshape((256, 256)).astype(int) # data_pet = np.divide(data_pet, np.amax(data_pet)) # data_mri_water = np.divide(data_mri_water, np.amax(data_mri_water)) # data_mri_fat = np.divide(data_mri_fat, np.amax(data_mri_fat)) img_w = data_mri_water[:, :, IDX_SLICE] img_f = data_mri_fat[:, :, IDX_SLICE] img_sum = img_f + img_w + 1e-6 mask_sum = np.asarray([img_sum > 150]).reshape((256, 256)).astype(int) mask = mask_pet * mask_sum mask = sm.opening(mask, sm.disk(5)) mask = sm.closing(mask, sm.square(5)) # img_sum = img_sum / np.amax(img_sum) img_ff = np.divide(img_f, img_sum) * mask # img_f = img_f / np.amax(img_f) # img_ff[img_ff <= 0.8] = 0 X[0, :, :, 0] = np.divide(img_p, np.amax(data_pet)) X[0, :, :, 2] = np.divide(img_w, np.amax(data_mri_water)) X[0, :, :, 1] = np.divide(img_w, np.amax(data_mri_water)) print(img_ff.shape) GL_set_value("img_ff", img_ff) Y = X print("X shape:", X.shape) print("Y shape:", Y.shape) return X, Y
def data_pre_breast_m2p(data_mri_water, data_mri_fat, data_pet): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") IDX_SLICE = GL_get_value("IDX_SLICE") X = np.zeros((1, IMG_ROWS, IMG_COLS, 1)) Y = np.zeros((1, IMG_ROWS, IMG_COLS, 1)) GL_set_value("FA_NORM", np.amax(data_pet)) # input img_p = data_pet[:, :, IDX_SLICE] Y[0, :, :, 0] = np.divide(img_p, np.amax(data_pet)) # water/fat fraction img_w = data_mri_water[:, :, IDX_SLICE] img_f = data_mri_fat[:, :, IDX_SLICE] img_sum = img_f + img_w + 1e-6 X[0, :, :, 0] = np.divide(img_sum, np.amax(img_sum)) # mask mask_pet = np.asarray([img_p > 350]).reshape((256, 256)).astype(int) mask_sum = np.asarray([img_sum > 150]).reshape((256, 256)).astype(int) mask = mask_pet * mask_sum mask = sm.opening(mask, sm.disk(5)) mask = sm.closing(mask, sm.square(5)) # water/fat fraction img_ff = np.divide(img_f, img_sum) * mask img_wf = np.divide(img_w, img_sum) * mask # img_ff[img_ff <= 0.8] = 0 GL_set_value("img_ff", img_ff) GL_set_value("img_wf", img_wf) GL_set_value("mask_pet", mask_pet) print("X shape:", X.shape) print("Y shape:", Y.shape) return X, Y
def save_all(): model_id = GL_get_value("MODEL_ID") # logs log_path = '.\\logs\\' + model_id + "\\" if not os.path.exists(log_path): os.makedirs(log_path) global_dict = GL_all() np.save(log_path + 'global.npy', global_dict)
def data_pre_PVC(data_mri, data_pet): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") IDX_SLICE = GL_get_value("IDX_SLICE") # FA_NORM = GL_get_value("FA_NORM") FA_NORM = np.amax(data_pet[:, :, IDX_SLICE]) GL_set_value('FA_NORM', FA_NORM) mri_th = float(GL_get_value("mri_th")) X = np.zeros((1, IMG_ROWS, IMG_COLS, 4)) Y = np.zeros((1, IMG_ROWS, IMG_COLS, 4)) Z = np.zeros((1, IMG_ROWS, IMG_COLS, 4)) data_pet = np.divide(data_pet, FA_NORM) Z[0, :, :, 0] = data_pet[:, :, IDX_SLICE] <= mri_th Z[0, :, :, 1] = data_mri[:, :, IDX_SLICE] == 3 Z[0, :, :, 2] = data_mri[:, :, IDX_SLICE] != 0 Z[0, :, :, 2] = Z[0, :, :, 2].astype(bool).astype(int) Z[0, :, :, 3] = data_pet[:, :, IDX_SLICE] > mri_th X[0, :, :, 0] = data_pet[:, :, IDX_SLICE] * Z[0, :, :, 2] # PET X[0, :, :, 1] = data_mri[:, :, IDX_SLICE] == 1 # CSF X[0, :, :, 2] = data_mri[:, :, IDX_SLICE] == 2 # gray matter X[0, :, :, 2] = (Z[0, :, :, 3] + X[0, :, :, 2]).astype(bool).astype( int) # gray matter X[0, :, :, 3] = Z[0, :, :, 0] * Z[0, :, :, 1] # white matter # if GL_get_value("flag_reg"): # Y = X.flatten() # else: # Y = X Y = X del Z gc.collect() # print("X shape:", X.shape) # print("Y shape:", Y.shape) return X, Y
def unet(img_shape, out_ch=1, start_ch=64, depth=4, inc_rate=2., activation='relu', dropout=0.5, batchnorm=False, maxpool=True, upconv=True, residual=False): i = Input(shape=img_shape) o = level_block(i, start_ch, depth, inc_rate, activation, dropout, batchnorm, maxpool, upconv, residual) o = Conv2D(out_ch, 1, activation='sigmoid')(o) flag_reg = GL_get_value("flag_reg") type_wr = GL_get_value("flag_wr") type_yr = GL_get_value("flag_yr") para_wr = GL_get_value("para_wr") para_yr = GL_get_value("para_yr") if flag_reg: if type_wr == 'l2': o = Dense(1, activation='sigmoid', kernel_regularizer=regularizers.l2(para_wr))(o) if type_wr == 'l1': o = Dense(1, activation='sigmoid', kernel_regularizer=regularizers.l1(para_wr))(o) if type_yr == 'l2': o = Dense(1, activation='sigmoid', activity_regularizer=regularizers.l2(para_yr))(o) if type_yr == 'l1': o = Dense(1, activation='sigmoid', activity_regularizer=regularizers.l1(para_yr))(o) return Model(inputs=i, outputs=o)
def data_pre_breast(data_mri_water, data_mri_fat, data_pet): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") IDX_SLICE = GL_get_value("IDX_SLICE") X = np.zeros((1, IMG_ROWS, IMG_COLS, 3)) Y = np.zeros((1, IMG_ROWS, IMG_COLS, 3)) GL_set_value("FA_NORM", np.amax(data_pet)) img_p = data_pet[:, :, IDX_SLICE] mask = np.asarray([img_p > 350]).reshape((256, 256)).astype(int) data_pet = np.divide(data_pet, np.amax(data_pet)) data_mri_water = np.divide(data_mri_water, np.amax(data_mri_water)) data_mri_fat = np.divide(data_mri_fat, np.amax(data_mri_fat)) X[0, :, :, 0] = data_pet[:, :, IDX_SLICE] img_w = data_mri_water[:, :, IDX_SLICE] img_f = data_mri_fat[:, :, IDX_SLICE] img_sum = img_f + img_w + 1e-6 # img_sum = img_sum / np.amax(img_sum) img_ff = np.divide(img_f, img_sum) * mask # img_f = img_f / np.amax(img_f) #img_f[img_f <= 0.95] = 0 X[0, :, :, 2] = img_w X[0, :, :, 1] = img_w print(img_ff.shape) GL_set_value("img_ff", img_ff) Y = X print("X shape:", X.shape) print("Y shape:", Y.shape) return X, Y
def data_pre_seg(data_mri, data_pet): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") IMG_DEPT = GL_get_value("IMG_DEPT") IDX_SLICE = GL_get_value("IDX_SLICE") FA_NORM = GL_get_value("FA_NORM") X = np.zeros((1, IMG_ROWS, IMG_COLS, 1)) Y = np.zeros((1, IMG_ROWS, IMG_COLS, 3)) data_pet = np.divide(data_pet, FA_NORM) X[0, :, :, 0] = data_pet[:, :, IDX_SLICE] Y[0, :, :, 0] = data_mri[:, :, IDX_SLICE] == 1 Y[0, :, :, 1] = data_mri[:, :, IDX_SLICE] == 2 Y[0, :, :, 2] = data_mri[:, :, IDX_SLICE] == 3 print("X shape:", X.shape) print("Y shape:", Y.shape) return X, Y
def Gray_White_CSF(y_true, y_pred): w_pgwc = (GL_get_value("W_PGWC")) # [PET, Gray, White, CSF] weight = [int(w_pgwc[0]), int(w_pgwc[1]), int(w_pgwc[2]), int(w_pgwc[3])] # k1 = np.array([[-1, -1, -1], # [0, 0, 0], # [1, 1, 1]], dtype=np.float32) # k2 = np.array([[-1, 0, 1], # [-1, 0, 1], # [-1, 0, 1]], dtype=np.float32) # k1 = K.reshape(k1, (3, 3, 1, 1)) # k2 = K.reshape(k2, (3, 3, 1, 1)) # pet_true = K.reshape(y_true[:, :, :, 0], (1, 512, 512, 1)) # pet_pred = K.reshape(y_pred[:, :, :, 0], (1, 512, 512, 1)) # norm1 = K.conv2d(pet_true, k1, strides=(1, 1), padding='same', dilation_rate=(1, 1)) # norm2 = K.conv2d(pet_true, k2, strides=(1, 1), padding='same', dilation_rate=(1, 1)) # grads = K.sqrt(K.square(norm1) + K.square(norm2)) pet_error = K.mean(K.square(y_pred[:, :, :, 0] - y_true[:, :, :, 0]), axis=-1) # pet_error = K.mean(K.square(y_pred[:,:,:,0] - y_true[:,:,:,0]), axis=-1)*((-grads)/3500 + 10) # pet_loss = mean_squared_error(pet_true, pet_pred) csf_mask = y_true[:, :, :, 1] csf_true = csf_mask * y_true[:, :, :, 0] csf_pred = csf_mask * y_pred[:, :, :, 0] # csf_error = K.mean(K.square(csf_pred - csf_true), axis=-1) csf_error = K.max(K.square(csf_pred)) - K.mean(K.square(csf_true)) gm_mask = y_true[:, :, :, 2] gm_true = gm_mask * y_true[:, :, :, 0] gm_pred = gm_mask * y_pred[:, :, :, 0] gm_error = K.mean(K.square(gm_pred - gm_true), axis=-1) wm_mask = y_true[:, :, :, 3] # wm_mask = np.bitwise_and(wm_mask == 1, y_true[:, :, :, 0] < MRI_TH) wm_true = wm_mask * y_true[:, :, :, 0] wm_pred = wm_mask * y_pred[:, :, :, 0] # wm_sum = K.sum( wm_pred, axis=-1 ) # wm_error = K.mean(K.square(wm_pred - wm_true), axis=-1) wm_error = K.max(K.square(wm_pred)) - K.mean(K.square(wm_true)) return pet_error * weight[0] + gm_error * weight[1] + wm_error * weight[ 2] + csf_error * weight[3]
def loss_breast_p2p(y_true, y_pred): w_pet = GL_get_value("w_pet") w_water = GL_get_value("w_water") w_fat = GL_get_value("w_fat") img_ff = GL_get_value("img_ff") img_wf = GL_get_value("img_wf") mask_pet = 1 - GL_get_value("mask_pet") DC_pred = y_pred * mask_pet DC_true = y_true * mask_pet loss1 = K.mean(K.square(y_pred - y_true), axis=-1) # PET loss2 = K.mean(K.square(y_pred * img_wf - y_true * img_wf), axis=-1) # water # loss3 = K.max(K.square(fat_pred)) - K.mean(K.square(fat_true)) # fat loss3 = K.mean(K.square(y_pred * img_ff - y_true * img_ff), axis=-1) # fat loss4 = K.max(K.square(DC_pred)) - K.mean(K.square(DC_true)) # DC return loss1 * w_pet + loss2 * w_water + loss3 * w_fat
def add_regularizer(model): flag_wr = GL_get_value("flag_wr") flag_yr = GL_get_value("flag_yr") para_wr = GL_get_value("para_wr") para_yr = GL_get_value("para_yr") n_pixel = GL_get_value("IMG_ROWS") * GL_get_value("IMG_COLS") if flag_wr == 'l2': model.add(Flatten()) model.add( Dense(n_pixel, input_dim=n_pixel, activation='sigmoid', kernel_regularizer=regularizers.l2(para_wr))) if flag_wr == 'l1': model.add(Flatten()) model.add( Dense(n_pixel, input_dim=n_pixel, activation='sigmoid', kernel_regularizer=regularizers.l1(para_wr))) if flag_yr == 'l2': model.add(Flatten()) model.add( Dense(n_pixel, input_dim=n_pixel, activation='sigmoid', activity_regularizer=regularizers.l2(para_yr))) if flag_yr == 'l1': model.add(Flatten()) model.add( Dense(n_pixel, input_dim=n_pixel, activation='sigmoid', activity_regularizer=regularizers.l1(para_yr)))
def set_configuration(n_epoch=500, flag_aug=False): IMG_ROWS = GL_get_value("IMG_ROWS") IMG_COLS = GL_get_value("IMG_COLS") MODEL_ID = GL_get_value("MODEL_ID") model = None opt = None loss = None # logs log_path = '.\\logs\\' + MODEL_ID + "\\" if not os.path.exists(log_path): os.makedirs(log_path) # set traininig configurations conf = { "image_shape": (IMG_ROWS, IMG_COLS, 1), "out_channel": 1, "filter": GL_get_value("n_filter"), "depth": GL_get_value("depth"), "inc_rate": 2, "activation": 'relu', "dropout": GL_get_value("flag_Dropout"), "batchnorm": GL_get_value("flag_BN"), "maxpool": True, "upconv": True, "residual": True, "shuffle": True, "augmentation": False, "learning_rate": 1e-5, "decay": 0.0, "epsilon": 1e-8, "beta_1": 0.9, "beta_2": 0.999, "epochs": n_epoch, "loss": 'loss_breast_p2p', "metric": "mse", "optimizer": 'Adam', "batch_size": 10 } if GL_get_value("flag_smooth"): conf["loss"] = conf["loss"] + '_smooth' np.save(log_path + 'info.npy', conf) if flag_aug: # 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 + 'aug.npy', conf_a) # 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 if conf["loss"] == 'Gray_White_CSF': loss = Gray_White_CSF if conf["loss"] == 'Gray_White_CSF_smooth': loss = Gray_White_CSF_soomth if conf["loss"] == 'loss_breast': loss = loss_breast if conf["loss"] == 'loss_breast_practical': loss = loss_breast_practical if conf["loss"] == 'loss_breast_p2p': loss = loss_breast_p2p # callback callbacks_list = set_checkpoint(log_path=log_path, MODEL_ID=MODEL_ID, batch_size=conf["batch_size"]) return model, opt, loss, callbacks_list, conf