def train(user_conf): """ Parameters ---------- user_conf : dict Json dict (created with json.dumps) with the user's configuration parameters that will replace the defaults. Must be loaded with json.loads() For example: user_conf={'num_classes': 'null', 'lr_step_decay': '0.1', 'lr_step_schedule': '[0.7, 0.9]', 'use_early_stopping': 'false'} """ CONF = config.CONF # Update the conf with the user input for group, val in sorted(CONF.items()): for g_key, g_val in sorted(val.items()): g_val['value'] = json.loads(user_conf[g_key]) # Check the configuration try: config.check_conf(conf=CONF) except Exception as e: raise BadRequest(e) CONF = config.conf_dict(conf=CONF) timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S') config.print_conf_table(CONF) K.clear_session() # remove the model loaded for prediction train_fn(TIMESTAMP=timestamp, CONF=CONF) # Sync with NextCloud folders (if NextCloud is available) try: mount_nextcloud(paths.get_models_dir(), 'ncplants:/models') except Exception as e: print(e)
def train(**args): """ Train an image classifier """ update_with_query_conf(user_args=args) CONF = config.conf_dict timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S') config.print_conf_table(CONF) K.clear_session() # remove the model loaded for prediction train_fn(TIMESTAMP=timestamp, CONF=CONF) # Sync with NextCloud folders (if NextCloud is available) try: mount_nextcloud(paths.get_models_dir(), 'rshare:/models') except Exception as e: print(e)
from imgclas import paths, utils, config, test_utils from imgclas.data_utils import load_class_names, load_class_info, mount_nextcloud from imgclas.train_runfile import train_fn # TODO: Move to proper marshalling for arguments # The point is that some fields need additional information than the one that is contained in the config.yaml # --> define an example for each arg in the config.yaml --> create the schema for that arg # type_map = {'int': fields.Int, 'str': fields.Str, 'float': fields.Float, 'dict': fields.Dict, # 'bool': fields.Bool, 'list': fields.List} # field_type = type_map.get(g_val['type'], fields.Field) # parser[g_key] = field_type(**opt_args) # --> another option is to add a marshmallow schema to each config args # Mount NextCloud folders (if NextCloud is available) try: mount_nextcloud('rshare:/data/dataset_files', paths.get_splits_dir()) mount_nextcloud('rshare:/data/images', paths.get_images_dir()) #mount_nextcloud('rshare:/models', paths.get_models_dir()) except Exception as e: print(e) # Empty model variables for inference (will be loaded the first time we perform inference) loaded_ts, loaded_ckpt = None, None graph, model, conf, class_names, class_info = None, None, None, None, None # Additional parameters allowed_extensions = set(['png', 'jpg', 'jpeg', 'PNG', 'JPG', 'JPEG']) # allow only certain file extensions top_K = 5 # number of top classes predictions to return
import numpy as np import requests from werkzeug.exceptions import BadRequest import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras import backend as K from imgclas import paths, utils, config from imgclas.data_utils import load_class_names, load_class_info, mount_nextcloud from imgclas.test_utils import predict from imgclas.train_runfile import train_fn # Mount NextCloud folders (if NextCloud is available) try: mount_nextcloud('ncplants:/data/dataset_files', paths.get_splits_dir()) mount_nextcloud('ncplants:/data/images', paths.get_images_dir()) #mount_nextcloud('ncplants:/models', paths.get_models_dir()) except Exception as e: print(e) # Empty model variables for inference (will be loaded the first time we perform inference) loaded = False graph, model, conf, class_names, class_info = None, None, None, None, None # Additional parameters allowed_extensions = set(['png', 'jpg', 'jpeg', 'PNG', 'JPG', 'JPEG']) # allow only certain file extensions top_K = 5 # number of top classes predictions to return