def instantiate_bunet(params, adam): input_img = Input(params["img_shape"], name='img') model = mt.get_bunet(input_img, n_filters=16, dropout=0.05, batchnorm=True, training=False) '''Compile model''' adam = adam(lr=params["learning_rate"], beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(optimizer=adam, loss="mse", metrics=["mse"]) #,percentual_deviance]) model.summary() '''train and save model''' save_model_path = os.path.join(evaluation_params["model_path"], "weights.hdf5") '''Load models trained weights''' model.load_weights(save_model_path) return model
def return_model(params, model_iter, filters): '''get model''' input_img = Input(params["img_shape"], name='img') if model_iter == 1: model = mt.get_bunet(input_img, n_filters=4 * filters, dropout=params["drop_out"], batchnorm=True, training=True) if model_iter == 2: model = mt.get_shallow_bunet(input_img, n_filters=4 * filters, dropout=params["drop_out"], batchnorm=True, training=True) if model_iter == 3: model = mt.get_very_shallow_bunet(input_img, n_filters=4 * filters, dropout=params["drop_out"], batchnorm=True, training=True) return model
def sample_espistemic_loss(params, IDs): ''' :param IDs: record ids to be evaluated :param T: number of times to sample the network weights :return: a nested list with each row being a record and each sublist a sample Inspired by paper: https://arxiv.org/pdf/1703.04977.pdf ''' from skimage.transform import resize # init nested list epi_variances = [] epi_record_id = [] # retrieve each sample # load model with dropout in training mode input_img = Input(params["img_shape"], name='img') epistemic_model = mt.get_bunet(input_img, n_filters=16, dropout=0.5, batchnorm=True, training=True) '''train and save model''' save_model_path = os.path.join(evaluation_params["model_path"], "weights.hdf5") '''Load models trained weights''' epistemic_model.load_weights(save_model_path) print("loaded model with dropout in training mode") for i in IDs: im_path = os.path.join(gen_params["fundus_path"], i + ".png") lbl_path = os.path.join(gen_params["thickness_path"], i + ".npy") im, lbl = evaluation_load(im_path, lbl_path) pred = epistemic_model.predict(im.reshape(1, params["img_shape"][0], params["img_shape"][1], 1), batch_size=1) #get only uncertainties for fovea region prediction = pred[0, :, :, 0].flatten().tolist() epi_variances.extend(prediction) epi_record_id.extend([i] * len(prediction)) return (epi_variances, epi_record_id)
def instantiate_bunet(params,adam, training): ''' :param params: params stating config info :param opt: an optimizer for the network :return: model object for prediction ''' '''get model''' input_img = Input(params["img_shape"], name='img') model = mt.get_bunet(input_img, n_filters=16, dropout=0.5, batchnorm=True, training=training) adam = adam(lr=params["learning_rate"], beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) '''Compile model''' model.compile(optimizer=adam, loss="sparse_categorical_crossentropy", metrics=[iou]) model.summary() '''train and save model''' save_model_path = os.path.join(params["save_path"], "weights.hdf5") model.load_weights(save_model_path) return model