def testOptimizersCompatibility(self, opt_str, test_weights, test_numeric): np.random.seed(1331) with self.cached_session(): train_samples = 20 input_dim = 3 num_classes = 2 (x, y), _ = testing_utils.get_test_data(train_samples=train_samples, test_samples=10, input_shape=(input_dim, ), num_classes=num_classes) y = keras.utils.to_categorical(y) num_hidden = 5 model = testing_utils.get_small_sequential_mlp( num_hidden=num_hidden, num_classes=num_classes, input_dim=input_dim) old_mode = os.environ.get('TF2_BEHAVIOR', None) # Disable tf2 to create V1 optimizer. disable_tf2() if opt_str == 'momentum': opt_v1 = optimizers.SGD(momentum=0.9) else: opt_v1 = optimizers.get(opt_str) # Test compile and fit with v1 optimizer. model.compile(opt_v1, loss='categorical_crossentropy', metrics=[]) model.fit(x, y, batch_size=5, epochs=1) model_dir = tempfile.mkdtemp() gfile.MakeDirs(model_dir) file_name = os.path.join(model_dir, 'model.h5') model.save(file_name) enable_tf2() # Test load and fit with v2 optimizer. model_2 = saving.load_model(file_name) opt_v2 = model_2.optimizer self.assertIsInstance(opt_v2, optimizer_v2.OptimizerV2) # set_weights is called inside load_model but exception is swallowed, # this call checks the weights can be set correctly. if test_weights: opt_v2.set_weights(opt_v1.get_weights()) if test_numeric: hist_1 = model.fit(x, y, batch_size=5, epochs=1, shuffle=False) hist_2 = model_2.fit(x, y, batch_size=5, epochs=1, shuffle=False) self.assertAllClose(model.get_weights(), model_2.get_weights()) self.assertAllClose(model.get_weights(), model_2.get_weights()) self.assertAllClose(hist_1.history['loss'], hist_2.history['loss']) if old_mode is not None: os.environ['TF2_BEHAVIOR'] = old_mode
def testOptimizersCompatibility(self, opt_str, test_weights, test_numeric): np.random.seed(1331) with self.cached_session(): train_samples = 20 input_dim = 3 num_classes = 2 (x, y), _ = testing_utils.get_test_data( train_samples=train_samples, test_samples=10, input_shape=(input_dim,), num_classes=num_classes) y = keras.utils.to_categorical(y) num_hidden = 5 model = testing_utils.get_small_sequential_mlp( num_hidden=num_hidden, num_classes=num_classes, input_dim=input_dim) old_mode = os.environ.get('TF2_BEHAVIOR', None) # Disable tf2 to create V1 optimizer. disable_tf2() if opt_str == 'momentum': opt_v1 = optimizers.SGD(momentum=0.9) else: opt_v1 = optimizers.get(opt_str) # Test compile and fit with v1 optimizer. model.compile(opt_v1, loss='categorical_crossentropy', metrics=[]) model.fit(x, y, batch_size=5, epochs=1) model_dir = tempfile.mkdtemp() gfile.MakeDirs(model_dir) file_name = os.path.join(model_dir, 'model.h5') model.save(file_name) enable_tf2() # Test load and fit with v2 optimizer. model_2 = saving.load_model(file_name) opt_v2 = model_2.optimizer self.assertIsInstance(opt_v2, optimizer_v2.OptimizerV2) # set_weights is called inside load_model but exception is swallowed, # this call checks the weights can be set correctly. if test_weights: opt_v2.set_weights(opt_v1.get_weights()) if test_numeric: hist_1 = model.fit(x, y, batch_size=5, epochs=1, shuffle=False) hist_2 = model_2.fit(x, y, batch_size=5, epochs=1, shuffle=False) self.assertAllClose(model.get_weights(), model_2.get_weights()) self.assertAllClose(model.get_weights(), model_2.get_weights()) self.assertAllClose(hist_1.history['loss'], hist_2.history['loss']) if old_mode is not None: os.environ['TF2_BEHAVIOR'] = old_mode
def load_model(model_path, batch_size): model = load_model(model_path) # first prediction takes more time model.predict(np.array([State.empty().to_numpy()])) return BatchEvaluator(model, batch_size)
from tensorflow.python.keras.engine.saving import load_model from chroma_instance.model.fusion import fusion_network load_weight_path = '../../../weights/chroma_gan/imagenet.h5' img_shape = (224, 224) batch_size = 2 fusion_generator = fusion_network(img_shape, 2) chroma_gan = load_model(load_weight_path) chroma_gan_layers = [layer.name for layer in chroma_gan.layers] fusion_layer_names = [layer.name for layer in fusion_generator.layers] for i, layer in enumerate(fusion_layer_names): if layer == 'model_3': print('model 3 skip') continue try: j = chroma_gan_layers.index(layer) print('Before:') print(' fusion') print(fusion_generator.layers[i].get_weights()[0]) print(' chroma gan') print(chroma_gan.layers[j].get_weights()[0]) fusion_generator.layers[i].set_weights( chroma_gan.layers[j].get_weights())
def train_on_subsets(config, number_splits, CV_split_to_use, number_classifiers, subset_seeds, overlap_ratio): """ Trains several classifiers with similar training set, while preserving test and validation set the same. The aim is to compare the performance of these classifiers later in stability module. The script takes a specific cross validation split of training, validation and testing set, and then drops a portion of the samples from the training set. Validation and test set are not changed - they are as the original split. Then the script trains a classifier with each of the training subsets. :param config: yaml config file :param number_splits: number of cross validation splits used in cross validation (CV) (run_cross_validation.py) :param CV_split_to_use: specific CV split for defining train/test/validation set. Value is between [0, number_splits-1] :param number_classifiers: number of classifiers to train :param subset_seeds: seeds used to drop observations from original training set. :param overlap_ratio: ration of observations which are preserved from the original training set, defined by the specific CV split. :return: Returns saved .npy file for the predictions, image_indices and patch labels for the train/test/valid set for each subset. """ skip_processing = config['skip_processing_labels'] image_path = config['image_path'] classication_labels_path = config['classication_labels_path'] localization_labels_path = config['localization_labels_path'] results_path = config['results_path'] processed_labels_path = config['processed_labels_path'] train_mode = config['train_mode'] dataset_name = config['dataset_name'] class_name = config['class_name'] mura_test_img_path = config['mura_test_img_path'] mura_train_labels_path = config['mura_train_labels_path'] mura_train_img_path = config['mura_train_img_path'] mura_test_labels_path = config['mura_test_labels_path'] mura_processed_train_labels_path = config[ 'mura_processed_train_labels_path'] mura_processed_test_labels_path = config['mura_processed_test_labels_path'] mura_interpolation = config['mura_interpolation'] pascal_image_path = config['pascal_image_path'] resized_images_before_training = config['resized_images_before_training'] nr_epochs = config['nr_epochs'] lr = config['lr'] reg_weight = config['reg_weight'] pooling_operator = config['pooling_operator'] IMAGE_SIZE = 512 BATCH_SIZE = 10 BATCH_SIZE_TEST = 1 BOX_SIZE = 16 use_xray, use_pascal = set_dataset_flag(dataset_name) script_suffix = 'subsets' trained_models_path = build_path_results(results_path, dataset_name, pooling_operator, script_suffix=script_suffix, result_suffix='trained_models') prediction_results_path = build_path_results(results_path, dataset_name, pooling_operator, script_suffix=script_suffix, result_suffix='predictions') make_directory(trained_models_path) make_directory(prediction_results_path) if use_xray: if resized_images_before_training: xray_df = fetch_preprocessed_images_csv(image_path, 'processed_imgs') # todo: delete - just for testing # xray_df = xray_df[-50:] else: xray_df = load_xray(skip_processing, processed_labels_path, classication_labels_path, image_path, localization_labels_path, results_path) xray_df = ld.filter_observations(xray_df, class_name, 'No Finding') elif use_pascal: pascal_df = load_pascal(pascal_image_path) else: df_train_val, test_df_all_classes = load_mura( skip_processing, mura_processed_train_labels_path, mura_processed_test_labels_path, mura_train_img_path, mura_train_labels_path, mura_test_labels_path, mura_test_img_path) for split in range(0, number_splits): if use_xray: df_train, df_val, df_test, df_bbox_train, \ df_bbox_test, train_only_class = split_xray_cv(xray_df, number_splits, split, class_name) elif use_pascal: df_train, df_val, df_test = construct_train_test_cv( pascal_df, number_splits, split) else: df_train, df_val = split_data_cv(df_train_val, number_splits, split, random_seed=1, diagnose_col=class_name, ratio_to_keep=None) df_test = filter_rows_and_columns(test_df_all_classes, class_name) for curr_classifier in range(0, number_classifiers): if train_mode and split == CV_split_to_use: print("#####################################################") print("SPLIT :" + str(split)) print("classifier #: " + str(curr_classifier)) if use_xray: class_train_subset = ld.get_train_subset_xray( train_only_class, df_bbox_train.shape[0], random_seed=subset_seeds[curr_classifier], ratio_to_keep=overlap_ratio) print("new subset is :" + str(class_train_subset.shape)) df_train_subset = pd.concat( [df_bbox_train, class_train_subset]) print(df_bbox_train.shape) print(class_train_subset.shape) elif use_pascal: df_train_subset = get_train_subset_mura( df_train, random_seed=subset_seeds[curr_classifier], ratio_to_keep=overlap_ratio) else: df_train_subset = get_train_subset_mura( df_train, random_seed=subset_seeds[curr_classifier], ratio_to_keep=overlap_ratio) tf.keras.backend.clear_session() K.clear_session() ##O##O##_##O#O##_################################ TRAIN ########################################################### train_generator = gen.BatchGenerator( instances=df_train_subset.values, resized_image=resized_images_before_training, batch_size=BATCH_SIZE, net_h=IMAGE_SIZE, net_w=IMAGE_SIZE, norm=keras_utils.normalize, box_size=BOX_SIZE, processed_y=skip_processing, interpolation=mura_interpolation, shuffle=True) valid_generator = gen.BatchGenerator( instances=df_val.values, resized_image=resized_images_before_training, batch_size=BATCH_SIZE, net_h=IMAGE_SIZE, net_w=IMAGE_SIZE, box_size=BOX_SIZE, norm=keras_utils.normalize, processed_y=skip_processing, interpolation=mura_interpolation, shuffle=True) model = keras_model.build_model(reg_weight) model = keras_model.compile_model_accuracy( model, lr, pooling_operator) lrate = LearningRateScheduler(keras_model.step_decay, verbose=1) filepath = trained_models_path + "CV_" + str( split) + '_' + str( curr_classifier) + "_-{epoch:02d}-{val_loss:.2f}.hdf5" checkpoint_on_epoch_end = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=False, mode='min') print("df train STEPS") print(len(df_train) // BATCH_SIZE) print(train_generator.__len__()) history = model.fit_generator( generator=train_generator, steps_per_epoch=train_generator.__len__(), epochs=nr_epochs, validation_data=valid_generator, validation_steps=valid_generator.__len__(), verbose=1) filepath = trained_models_path + 'subset_' + class_name + "_CV" + str(split) + '_' + str( curr_classifier) + '_' + \ str(overlap_ratio) + ".hdf5" model.save(filepath) print("history") print(history.history) print(history.history['keras_accuracy']) np.save( trained_models_path + 'train_info_' + str(split) + '_' + str(curr_classifier) + '_' + str(overlap_ratio) + '.npy', history.history) settings = np.array({ 'lr: ': lr, 'reg_weight: ': reg_weight, 'pooling_operator: ': pooling_operator }) np.save(trained_models_path + 'train_settings.npy', settings) keras_utils.plot_train_validation( history.history['loss'], history.history['val_loss'], 'train loss', 'validation loss', 'CV_loss' + str(split) + str(curr_classifier), 'loss', trained_models_path) ############################################ PREDICTIONS ############################################# ########################################### TRAINING SET######################################################## predict_patch_and_save_results( model, 'train_set_CV' + str(split) + '_' + str(curr_classifier), df_train, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) ########################################## VALIDATION SET###################################################### predict_patch_and_save_results( model, 'val_set_CV' + str(split) + '_' + str(curr_classifier), df_val, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) ########################################### TESTING SET######################################################## predict_patch_and_save_results( model, 'test_set_CV' + str(split) + '_' + str(curr_classifier), df_test, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) elif not train_mode: files_found = 0 print(trained_models_path) for file_path in Path(trained_models_path).glob( "subset_Cardiomegaly_CV1_" + str(curr_classifier) + "*.hdf5"): print(file_path) files_found += 1 assert files_found == 1, "No model found/ Multiple models found, not clear which to use " print(str(files_found)) model = load_model(str(file_path), custom_objects={ 'keras_loss_v3_nor': keras_loss_v3_nor, 'keras_accuracy': keras_accuracy, 'accuracy_asloss': accuracy_asloss }) model = keras_model.compile_model_accuracy( model, lr, pooling_operator) predict_patch_and_save_results( model, "train_set_CV" + str(split) + str(curr_classifier), df_train, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results( model, "val_set_CV" + str(split) + str(curr_classifier), df_val, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results( model, "test_set_CV" + str(split) + str(curr_classifier), df_test, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training)
def load_model(path, generator=None, augmenter=None, custom_objects=None, compile=True): """ Load the model Example ------- model = load_model('model.h5', augmenter) """ if custom_objects: if isinstance(custom_objects, dict): base_objects = CUSTOM_LAYERS custom_objects = dict( list(base_objects.items()) + list(custom_objects.items())) else: custom_objects = CUSTOM_LAYERS if isinstance(path, str): if path.endswith(".h5") or path.endswith(".hdf5"): filepath = path else: raise ValueError("file must be .h5 file") else: raise TypeError("file must be type `str`") train_model = saving.load_model(filepath, custom_objects=custom_objects, compile=compile) with h5py.File(filepath, "r") as h5file: train_generator_config = h5file.attrs.get("train_generator_config") if train_generator_config is None: raise ValueError("No data generator found in config file") train_generator_config = json.loads( train_generator_config.decode("utf-8"))["config"] model_config = h5file.attrs.get("pose_model_config") if model_config is None: raise ValueError("No pose model found in config file") model_name = json.loads(model_config.decode("utf-8"))["class_name"] model_config = json.loads(model_config.decode("utf-8"))["config"] if generator: signature = inspect.signature(TrainingGenerator.__init__) keys = [key for key in signature.parameters.keys()] keys.remove("self") keys.remove("augmenter") keys.remove("generator") kwargs = {key: train_generator_config[key] for key in keys} kwargs["augmenter"] = augmenter kwargs["generator"] = generator train_generator = TrainingGenerator(**kwargs) else: train_generator = None Model = MODELS[model_name] signature = inspect.signature(Model.__init__) keys = [key for key in signature.parameters.keys()] keys.remove("self") keys.remove("train_generator") if "kwargs" in keys: keys.remove("kwargs") kwargs = {key: model_config[key] for key in keys} kwargs["train_generator"] = train_generator # Pass to skip initialization and manually intialize kwargs["skip_init"] = True model = Model(**kwargs) model.train_model = train_model model.__init_train_model__() model.__init_input__(model_config["image_shape"]) kwargs = {} kwargs["output_shape"] = model_config["output_shape"] kwargs["keypoints_shape"] = model_config["keypoints_shape"] kwargs["downsample_factor"] = model_config["downsample_factor"] kwargs["output_sigma"] = model_config["output_sigma"] model.__init_predict_model__(**kwargs) return model
def trained_autoencoder(): return load_model("autoencoder_model")
def trained_encoder(): """Return a keras model""" return load_model("encoder_model", compile=True)
def cross_validation(config, number_splits=5): """ performs cross validation on a specific architecture :param config: yaml config file :param number_splits: number of different cross validation splits to test on :return: Returns predictions, image indices and patch labels saved in .npy file for train,test and validation set and for each CV split. """ skip_processing = config['skip_processing_labels'] image_path = config['image_path'] classication_labels_path = config['classication_labels_path'] localization_labels_path = config['localization_labels_path'] results_path = config['results_path'] train_mode = config['train_mode'] dataset_name = config['dataset_name'] class_name = config['class_name'] mura_test_img_path = config['mura_test_img_path'] mura_train_labels_path = config['mura_train_labels_path'] mura_train_img_path = config['mura_train_img_path'] mura_test_labels_path = config['mura_test_labels_path'] mura_processed_train_labels_path = config[ 'mura_processed_train_labels_path'] mura_processed_test_labels_path = config['mura_processed_test_labels_path'] mura_interpolation = config['mura_interpolation'] pascal_image_path = config['pascal_image_path'] resized_images_before_training = config['resized_images_before_training'] nr_epochs = config['nr_epochs'] lr = config['lr'] reg_weight = config['reg_weight'] pooling_operator = config['pooling_operator'] use_xray, use_pascal = set_dataset_flag(dataset_name) script_suffix = 'CV' trained_models_path = build_path_results(results_path, dataset_name, pooling_operator, script_suffix=script_suffix, result_suffix='trained_models') prediction_results_path = build_path_results(results_path, dataset_name, pooling_operator, script_suffix=script_suffix, result_suffix='predictions') make_directory(trained_models_path) make_directory(prediction_results_path) if use_xray: if resized_images_before_training: xray_df = fetch_preprocessed_images_csv(image_path, 'processed_imgs') #todo: delete - just for testing # xray_df = xray_df[-50:] else: xray_df = load_process_xray14(config) elif use_pascal: pascal_df = load_pascal(pascal_image_path) else: df_train_val, test_df_all_classes = load_mura( skip_processing, mura_processed_train_labels_path, mura_processed_test_labels_path, mura_train_img_path, mura_train_labels_path, mura_test_labels_path, mura_test_img_path) for split in range(0, number_splits): if use_xray: df_train, df_val, df_test, _, _, _ = ld.split_xray_cv( xray_df, number_splits, split, class_name) elif use_pascal: df_train, df_val, df_test = construct_train_test_cv( pascal_df, number_splits, split) else: df_train, df_val = split_data_cv(df_train_val, number_splits, split, random_seed=1, diagnose_col=class_name, ratio_to_keep=None) # df_test = filter_rows_on_class(test_df_all_classes, class_name=class_name) df_test = filter_rows_and_columns(test_df_all_classes, class_name) if train_mode: tf.keras.backend.clear_session() K.clear_session() ############################################ TRAIN ########################################################### train_generator = gen.BatchGenerator( instances=df_train.values, resized_image=resized_images_before_training, batch_size=BATCH_SIZE, net_h=IMAGE_SIZE, net_w=IMAGE_SIZE, norm=keras_utils.normalize, box_size=BOX_SIZE, processed_y=skip_processing, interpolation=mura_interpolation, shuffle=True) valid_generator = gen.BatchGenerator( instances=df_val.values, resized_image=resized_images_before_training, batch_size=BATCH_SIZE, net_h=IMAGE_SIZE, net_w=IMAGE_SIZE, box_size=BOX_SIZE, norm=keras_utils.normalize, processed_y=skip_processing, interpolation=mura_interpolation, shuffle=True) model = keras_model.build_model(reg_weight) model = keras_model.compile_model_accuracy( model, lr, pool_op=pooling_operator) # checkpoint on every epoch is not really needed here, not used, CALLBACK REMOVED from the generator filepath = trained_models_path + "CV_" + str( split) + "_epoch-{epoch:02d}-{val_loss:.2f}.hdf5" checkpoint_on_epoch_end = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=False, mode='min') lrate = LearningRateScheduler(keras_model.step_decay, verbose=1) print("df train STEPS") print(len(df_train) // BATCH_SIZE) print(train_generator.__len__()) history = model.fit_generator( generator=train_generator, steps_per_epoch=train_generator.__len__(), epochs=nr_epochs, validation_data=valid_generator, validation_steps=valid_generator.__len__(), verbose=1, callbacks=[checkpoint_on_epoch_end]) print("history") print(history.history) print(history.history['keras_accuracy']) np.save(trained_models_path + 'train_info_' + str(split) + '.npy', history.history) settings = np.array({ 'lr: ': lr, 'reg_weight: ': reg_weight, 'pooling_operator: ': pooling_operator }) np.save(trained_models_path + 'train_settings.npy', settings) keras_utils.plot_train_validation(history.history['loss'], history.history['val_loss'], 'train loss', 'validation loss', 'CV_loss' + str(split), 'loss', trained_models_path) ############################################ PREDICTIONS ############################################# predict_patch_and_save_results(model, 'test_set_CV' + str(split), df_test, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results(model, 'train_set_CV' + str(split), df_train, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results(model, 'val_set_CV' + str(split), df_val, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) ##### EVALUATE function print("evaluate validation") evaluate = model.evaluate_generator( generator=valid_generator, steps=valid_generator.__len__(), verbose=1) evaluate_train = model.evaluate_generator( generator=train_generator, steps=train_generator.__len__(), verbose=1) test_generator = gen.BatchGenerator( instances=df_test.values, resized_image=resized_images_before_training, batch_size=BATCH_SIZE, net_h=IMAGE_SIZE, net_w=IMAGE_SIZE, shuffle=True, norm=keras_utils.normalize, box_size=BOX_SIZE, processed_y=skip_processing, interpolation=mura_interpolation) evaluate_test = model.evaluate_generator( generator=test_generator, steps=test_generator.__len__(), verbose=1) print("Evaluate Train") print(evaluate_train) print("Evaluate Valid") print(evaluate) print("Evaluate test") print(evaluate_test) else: files_found = 0 print(trained_models_path) for file_path in Path(trained_models_path).glob( "CV_patient_split_" + str(split) + "*.hdf5"): print(file_path) files_found += 1 assert files_found == 1, "No model found/ Multiple models found, not clear which to use " print(str(files_found)) model = load_model(str(file_path), custom_objects={ 'keras_loss_v3_nor': keras_loss_v3_nor, 'keras_accuracy': keras_accuracy, 'accuracy_asloss': accuracy_asloss }) model = keras_model.compile_model_accuracy(model, lr, pooling_operator) predict_patch_and_save_results( model, "train_set_CV" + (str(split)), df_train, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results(model, "val_set_CV" + (str(split)), df_val, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) predict_patch_and_save_results(model, "test_set_CV" + (str(split)), df_test, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training)
def from_file(path: str): """ :arg path directory path to a file that contains, config, model and weights. :return a model populated from a file path. """ return load_model('{}/model.h5'.format(path))
df_test, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation=mura_interpolation, resized_images_before_training=resized_images_before_training) else: ###################################################################################### # deserealize a model and do predictions with it model = load_model(trained_models_path + 'Cardiomegaly-01-14.67.hdf5', compile=True, custom_objects={ 'keras_loss_v3_nor': keras_loss_v3_nor, 'keras_accuracy': keras_accuracy, 'accuracy_asloss': accuracy_asloss }) ########################################### TRAINING SET######################################################## predict_patch_and_save_results(model, 'train_set', df_train, skip_processing, BATCH_SIZE_TEST, BOX_SIZE, IMAGE_SIZE, prediction_results_path, mura_interpolation, resized_images_before_training) # ########################################### VALIDATION SET###################################################### predict_patch_and_save_results(model, 'val_set', df_val, skip_processing,
def __init__(self, config, load_weight_path=None, ab_loss='mse'): img_shape = (config.IMAGE_SIZE, config.IMAGE_SIZE) # Creating generator and discriminator optimizer = Adam(0.00002, 0.5) self.foreground_generator = instance_network(img_shape) self.fusion_discriminator = discriminator_network(img_shape) self.fusion_discriminator.compile(loss=wasserstein_loss_dummy, optimizer=optimizer) self.fusion_generator = fusion_network(img_shape, config.BATCH_SIZE) self.fusion_generator.compile(loss=[ab_loss, 'kld'], optimizer=optimizer) if load_weight_path: chroma_gan = load_model(load_weight_path) chroma_gan_layers = [layer.name for layer in chroma_gan.layers] print('Loading chroma GAN parameter to instance network...') instance_layer_names = [ layer.name for layer in self.foreground_generator.layers ] for i, layer in enumerate(instance_layer_names): if layer == 'fg_model_3': print('model 3 skip') continue if len(layer) < 2: continue if layer[:3] == 'fg_': try: j = chroma_gan_layers.index(layer[3:]) self.foreground_generator.layers[i].set_weights( chroma_gan.layers[j].get_weights()) print(f'Successfully set weights for layer {layer}') except ValueError: print(f'Layer {layer} not found in chroma gan.') except Exception as e: print(e) print('Loading chroma GAN parameter to fusion network...') fusion_layer_names = [ layer.name for layer in self.fusion_generator.layers ] for i, layer in enumerate(fusion_layer_names): if layer == 'model_3': print('model 3 skip') continue try: j = chroma_gan_layers.index(layer) self.fusion_generator.layers[i].set_weights( chroma_gan.layers[j].get_weights()) print(f'Successfully set weights for layer {layer}') except ValueError: print(f'Layer {layer} not found in chroma gan.') except Exception as e: print(e) # Fg=instance prediction fg_img_l = Input(shape=(*img_shape, 1, MAX_INSTANCES)) # self.foreground_generator.trainable = False fg_model_3, fg_conv2d_11, fg_conv2d_13, fg_conv2d_15, fg_conv2d_17 = self.foreground_generator( fg_img_l) # Fusion prediction fusion_img_l = Input(shape=(*img_shape, 1)) fusion_img_real_ab = Input(shape=(*img_shape, 2)) fg_bbox = Input(shape=(4, MAX_INSTANCES)) fg_mask = Input(shape=(*img_shape, MAX_INSTANCES)) self.fusion_generator.trainable = False fusion_img_pred_ab, fusion_class_vec = self.fusion_generator([ fusion_img_l, fg_model_3, fg_conv2d_11, fg_conv2d_13, fg_conv2d_15, fg_conv2d_17, fg_bbox, fg_mask ]) dis_pred_ab = self.fusion_discriminator( [fusion_img_pred_ab, fusion_img_l]) dis_real_ab = self.fusion_discriminator( [fusion_img_real_ab, fusion_img_l]) # Sample the gradient penalty img_ab_interp_samples = RandomWeightedAverage()( [fusion_img_pred_ab, fusion_img_real_ab]) dis_interp_ab = self.fusion_discriminator( [img_ab_interp_samples, fusion_img_l]) partial_gp_loss = partial( gradient_penalty_loss, averaged_samples=img_ab_interp_samples, gradient_penalty_weight=GRADIENT_PENALTY_WEIGHT) partial_gp_loss.__name__ = 'gradient_penalty' # Compile D and G as well as combined self.discriminator_model = Model( inputs=[ fusion_img_l, fusion_img_real_ab, fg_img_l, fg_bbox, fg_mask ], outputs=[dis_real_ab, dis_pred_ab, dis_interp_ab]) self.discriminator_model.compile(optimizer=optimizer, loss=[ wasserstein_loss_dummy, wasserstein_loss_dummy, partial_gp_loss ], loss_weights=[-1.0, 1.0, 1.0]) self.fusion_generator.trainable = True self.fusion_discriminator.trainable = False self.combined = Model( inputs=[fusion_img_l, fg_img_l, fg_bbox, fg_mask], outputs=[fusion_img_pred_ab, fusion_class_vec, dis_pred_ab]) self.combined.compile(loss=[ab_loss, 'kld', wasserstein_loss_dummy], loss_weights=[1.0, 0.003, -0.1], optimizer=optimizer) # Monitor stuff self.callback = TensorBoard(config.LOG_DIR) self.callback.set_model(self.combined) self.train_names = [ 'loss', 'mse_loss', 'kullback_loss', 'wasserstein_loss' ] self.disc_names = ['disc_loss', 'disc_valid', 'disc_fake', 'disc_gp'] self.test_loss_array = [] self.g_loss_array = []
def post(self, request: Request): if "zippy" not in request.data.keys(): return Response("Where is my zip?", 400) elif "dataset" not in request.data.keys(): return Response("Where is the dataset?", 400) elif request.data["dataset"] not in [x.name for x in DATASETS]: return Response("Dataset not valid", 400) try: input_zip = zipfile.ZipFile(request.data["zippy"]) except zipfile.BadZipFile: return Response("What kind of zip is this", 400) predictions = None model = None dataset = None pnames = [] for i in DATASETS: if i.name == request.data["dataset"]: if i.name == "reuters" or i.name == "imdb": return Response({ "predictions": [{ "feature": "We cannot predict for {} dataset".format(i.name), "prediction": "The reason is: https://snyk.io/blog/numpy-arbitrary-code-execution-vulnerability/" }] }) dataset = i break if dataset is None: return Response("This is not a valid dataset", 400) for x in input_zip.namelist(): if x.endswith(".h5"): file = h5py.File(io.BytesIO(input_zip.read(x))) model = load_model( file, custom_objects={'ClusteringLayer': ClusteringLayer}) elif x.endswith(".csv"): csv = pd.read_csv(io.StringIO(input_zip.read(x)), header=None) predictions = csv.values pnames = [x for x in range(predictions)] else: try: image = Image.open(io.BytesIO(input_zip.read(x))) image.load() image = np.asarray(image, dtype="float32") / 255 try: image = image.reshape(dataset.input_shape) except ValueError: return Response( "image {} is not in good format".format(x), 400) if predictions is None: predictions = np.array([image]) else: predictions = np.append(predictions, [image], axis=0) pnames.append(x) except IOError: pass if model is not None: predictions = model.predict(predictions) predictions = predictions.argmax(axis=1) print("PREDICTIONS", predictions) return Response({ "predictions": [{ "feature": n, "prediction": p } for n, p in zip(pnames, predictions)] }) else: return Response("Where is the h5?", 400)