def get_model_data(self, argument, models): # check argument if argument not in models: return None # get model model_json_config = models[argument]['json_config'] # load json self.config.load_json(model_json_config, True) # get model path model_path = models[argument]['model_path'] model_name = os.path.basename(model_path) model_size = get_formatted_file_size(model_path) if os.path.isfile( model_path) else '121.12 MB' model_classes = len(self.config.get_environment('classes')) model_learning_epochs = self.config.getml('epochs') model_date = get_changed_date(model_path) if os.path.isfile( model_path) else '2019-10-20T11:54:25.125386+00:00' model_version = '1.02' return { 'model_name': model_name, 'model_size': model_size, 'model_classes': model_classes, 'model_learning_epochs': model_learning_epochs, 'model_date': model_date, 'model_version': model_version }
def do_GET_prediction(self, argument, hook_results): """ route GET /prediction """ model_data = None hook_name = 'GET_prediction_get_model' if hook_name in hook_results: model_data = hook_results[hook_name] if model_data is None: model_data = self.get_empty_model_data() database_path = get_database_path(argument) database = get_database(argument) used_model = self.get_template('used_model') % { 'MODEL_NAME': model_data['model_name'], 'MODEL_SIZE': model_data['model_size'], 'CLASSES': model_data['model_classes'], 'LEARNING_EPOCHS': model_data['model_learning_epochs'], 'MODEL_DATE': model_data['model_date'], 'VERSION': model_data['model_version'], 'DATABASE': os.path.basename(database_path), 'DATABASE_SIZE': get_formatted_file_size(database_path), 'DATABASE_VERSION': database['version'] } # unknown model type if argument not in self.allowed_model_types: return False # build html html_form = self.get_template('form') % { 'ERROR_MESSAGE': '', 'TEXT_UPLOAD': self.TEXT_UPLOAD, 'MODEL_TYPE': argument } html_content = self.get_template(argument) % { 'PREDICTION_FORM': html_form, 'USED_MODEL': used_model } # send html to browser self.respond_html( self.get_template('body') % {'CONTENT': html_content}) return True
def get_model_data(argument): if argument not in SimpleHTTPRequestHandler.allowed_model_types: raise AssertionError('Unknown model type "%s"' % argument) model_path = 'C:/Users/bjoern/data/processed/flower_10/flower_10_1.inceptionv3.best.17-0.95.h5' model_name = os.path.basename(model_path) model_size = get_formatted_file_size(model_path) if os.path.isfile( model_path) else '121.12 MB' model_classes = 12 model_learning_epochs = 20 model_date = get_changed_date(model_path) if os.path.isfile( model_path) else '2019-10-20T11:54:25.125386+00:00' model_version = '1.02' return { 'model_name': model_name, 'model_size': model_size, 'model_classes': model_classes, 'model_learning_epochs': model_learning_epochs, 'model_date': model_date, 'model_version': model_version }
def do_POST_prediction(self, argument, hook_results): upload_data = self.write_upload_file() model_data = None hook_name = 'POST_prediction_get_model' if hook_name in hook_results: model_data = hook_results[hook_name] if model_data is None: model_data = self.get_empty_model_data() database_path = get_database_path(argument) database = get_database(argument) used_model = self.get_template('used_model') % { 'MODEL_NAME': model_data['model_name'], 'MODEL_SIZE': model_data['model_size'], 'CLASSES': model_data['model_classes'], 'LEARNING_EPOCHS': model_data['model_learning_epochs'], 'MODEL_DATE': model_data['model_date'], 'VERSION': model_data['model_version'], 'DATABASE': os.path.basename(database_path), 'DATABASE_SIZE': get_formatted_file_size(database_path), 'DATABASE_VERSION': database['version'] } # check if an error occurred if upload_data['error']: html_error = self.get_template('error') % upload_data['message'] html_form = self.get_template('form') % { 'ERROR_MESSAGE': html_error, 'TEXT_UPLOAD': self.TEXT_UPLOAD, 'MODEL_TYPE': argument } html_content = self.get_template(argument) % { 'PREDICTION_FORM': html_form, 'USED_MODEL': used_model } html_body = self.get_template('body') % {'CONTENT': html_content} self.respond_html(html_body) return True # call post hook hook_name = 'POST_%s' % ('prediction') evaluation_data = self.call_hook(hook_name, argument, upload_data) # get data from post hook evaluated_file_web_size = get_formatted_file_size( evaluation_data['evaluated_file']) evaluated_file_web = evaluation_data['evaluated_file_web'] graph_file_web = evaluation_data['graph_file_web'] prediction_class = evaluation_data['prediction_class'] prediction_accuracy = evaluation_data['prediction_accuracy'] upload_form = self.get_template('form') % { 'ERROR_MESSAGE': '', 'TEXT_UPLOAD': self.TEXT_UPLOAD, 'MODEL_TYPE': argument } prediction_time = evaluation_data['prediction_time'] prediction_overview_array = evaluation_data[ 'prediction_overview_array'] icons = {'flower': '🌻', 'food': '🍔'} html_content = self.get_template('prediction') % { 'ICON': icons[argument], 'MODEL_TYPE': argument, 'MODEL_TYPE_TITLE': argument.title(), 'EVALUATED_FILE_WEB_SIZE': evaluated_file_web_size, 'EVALUATED_FILE_WEB': evaluated_file_web, 'PREDICTION_CLASS': prediction_class.replace('_', ' ').title(), 'PREDICTION_ACCURACY': '%.2f' % prediction_accuracy, 'GRAPH_FILE_WEB': graph_file_web, 'UPLOAD_FORM': upload_form, 'PREDICTION_TIME': prediction_time, 'USED_MODEL': used_model, 'DATABASE': json.dumps(database, cls=NumpyEncoder), 'EVALUATION_DATA': json.dumps(evaluation_data, cls=NumpyEncoder) } self.respond_html( self.get_template('body') % {'CONTENT': html_content}) return True