def load_model(filesystem: S3FS, config: PredictionConfig) -> ResUnetA: """ Copy the model locally if not existing and load it """ if not os.path.exists(f'{config.temp_model_path}/{config.model_name}'): if not filesystem.exists( f'{config.model_path}/{config.model_name}/checkpoints/'): filesystem.makedirs( f'{config.model_path}/{config.model_name}/checkpoints/') copy_dir(filesystem, f'{config.model_path}/{config.model_name}/checkpoints/', f'{config.temp_model_path}/{config.model_name}', 'checkpoints') copy_file(filesystem, f'{config.model_path}/{config.model_name}/model_cfg.json', f'{config.temp_model_path}/{config.model_name}', 'model_cfg.json') input_shape = dict( features=[None, config.height, config.width, config.n_channels]) with open(f'{config.temp_model_path}/{config.model_name}/model_cfg.json', 'r') as jfile: model_cfg = json.load(jfile) # initialise model from config, build, compile and load trained weights model = ResUnetA(model_cfg) model.build(input_shape) model.net.compile() model.net.load_weights( f'{config.temp_model_path}/{config.model_name}/checkpoints/model.ckpt') return model
def load_metadata(filesystem: S3FS, config: PredictionConfig) -> pd.DataFrame: """ Load DataFrame with info about normalisation factors """ metadata_dir = os.path.dirname(config.metadata_path) if not filesystem.exists(metadata_dir): filesystem.makedirs(metadata_dir) df = pd.read_csv(filesystem.open(f'{config.metadata_path}')) normalisation_factors = df.groupby( pd.to_datetime(df.timestamp).dt.to_period("M")).max() normalisation_factors['month'] = pd.to_datetime( normalisation_factors.timestamp).dt.month return normalisation_factors