def start_validation(model_name, class_hash): """ :return: """ img_width = Constants.get_image_dimensions(model_name) img_height = img_width cph = PrintHelper() validation_data_dir = os.path.dirname(os.path.realpath(__file__)) + test_data_dir + class_hash base_validation_dir = os.path.dirname(os.path.realpath(__file__)) + test_data_dir save_weights = os.path.dirname( os.path.realpath(__file__)) + Constants.weights_directory_path + 'Weights#' + class_hash + '.h5' save_model_path = os.path.dirname( os.path.realpath(__file__)) + Constants.model_file_directory_path + 'Model_file#' + class_hash + '.h5' batch_size = 5 number_of_files = ModelValidation.number_of_images(validation_data_dir) cph.info_print('Loading Model') model = load_model(save_model_path) model.load_weights(save_weights) models_result = SaveModelToMongo.validation_started(class_hash) PrintHelper.info_print(' Model Loaded') model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) PrintHelper.info_print(' Model Compiled') # prepare data augmentation configuration PrintHelper.info_print('creating image data generator') validation_generator = DataGeneratorUtility.validation_data_generator(img_height, img_width, batch_size=batch_size, validation_data_dir=validation_data_dir) PrintHelper.info_print('Starting Evaluate Generator') loss, accuracy = model.evaluate_generator(validation_generator,number_of_files) PrintHelper.info_print('Loss: ', loss, ' Accuracy: ', accuracy) shutil.rmtree(validation_data_dir) SaveModelToMongo.validation_completed(class_hash=class_hash, stats=cph.return_string('Accuracy: ', round(accuracy, 4), ' Loss: ', round(loss, 4)), models_result=models_result)
def __init__(self, model_name, images, image_count, model_hash=None, base_model=None): """ :param model_name: name/hash of the model to predict/extract-features from :param images: path for the images downlaoded :param model_hash: hash saved in the database: required for loading weights and files :param base_model: required only when a custom generated model is being evaluated, check what is the base model of the model being used """ self._images_path = images self._model_name = model_name size = Constants.get_image_dimensions(model_name) self._target_size = (size, size) self._image_count = image_count self._model_path = None self._model_weights = None # only required for custom models, in which case base_model should be supplied if base_model is not None and model_hash is not None: try: self._model_path = os.path.dirname(os.path.realpath( __file__)) + Constants.saving_model_specific_file( base_model ) + base_model + '----model_file' + model_hash + '.h5' self._model_weights = os.path.dirname( os.path.realpath( __file__)) + Constants.saving_model_weights( base_model ) + base_model + '----weights' + model_hash + '.h5' except Exception as e: PrintHelper.failure_print("model file creation", e)