def test_training(self): num_epochs = 2 dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.training_save_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=16, train_print_frequency=10, epochs=num_epochs, num_workers=4, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.BCELoss(), classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.start_training() dirs = [e for e in self.training_save_path.iterdir() if e.is_dir()] with open(dirs[0] / 'output.log') as f: content = f.read() epochs = re.findall('Mean Loss on Eval', content) self.assertEqual(num_epochs, len(epochs)) # Check if steps are growing / if there are doubled steps in the output steps = [ int(re.findall(r'\d+', x)[0]) for x in re.findall(r'Duration of step.+\d:', content) ] self.assertEqual(len(set(steps)), len(steps))
def test_eval_preparation(self): dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.eval_output_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=2, train_print_frequency=10, epochs=0, num_workers=4, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.start_training() dirs = [e for e in self.eval_output_path.iterdir() if e.is_dir()] code_dir = dirs[0] / 'rtm-predictions' slurm_script = dirs[0] / 'run_model_eval.sh' self.assertTrue(os.path.isdir(code_dir)) self.assertTrue(os.path.isfile(slurm_script)) with open(slurm_script) as f: lines = f.read().splitlines() tokens = lines[-1].split() self.assertEqual(dirs[0], Path(tokens[-3])) st.writer.flush() st.writer.close()
def test_training_load_optimizer(self): dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.training_save_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=16, train_print_frequency=10, epochs=self.expected_num_epochs_during_training, num_workers=4, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.BCELoss(), optimizer_path=self.checkpoint, classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.start_training() after = len(st.optimizer.state.keys()) """ Optimizer has now more than 0 states, therefore was loaded """ self.assertGreater(after, 0)
def test_save_load_training(self): num_epochs = 2 dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.training_save_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=16, train_print_frequency=10, epochs=num_epochs, num_workers=4, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.BCELoss(), classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.start_training() num_epochs = 2 dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.training_save_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=16, train_print_frequency=10, epochs=num_epochs, num_workers=4, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.BCELoss(), classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.start_training()
def test_eval(self): dl = DataloaderImages((149, 117), ignore_useless_states=False) st = ModelTrainer( lambda: DeconvModelEfficient(), self.test_src_dir, self.eval_output_path, load_datasets_path=self.test_split_dir, cache_path=None, batch_size=2, train_print_frequency=10, epochs=self.expected_num_epochs_during_training, num_workers=10, num_validation_samples=2, num_test_samples=self.num_test_samples, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.BCELoss(), classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(self.eval_output_path / "eval_on_test_set", skip_images=False, summary_writer=summary_writer), data_root=test_resources.test_src_dir, ) st.inference_on_test_set( self.eval_output_path, self.checkpoint, classification_evaluator_function=lambda summary_writer: SensorToFlowfrontEvaluator(self.eval_output_path / "eval_on_test_set", skip_images=False, summary_writer=summary_writer)) with open(self.eval_output_path / "eval_on_test_set" / "test_output.log") as f: content = f.read() loss = float( re.findall(r'\d+.\d+', re.findall(r'Eval: \d+\.\d+', content)[0])[0]) self.assertEqual(np.round(loss, 4), self.expected_loss) img_path = self.eval_output_path / 'eval_on_test_set' / 'images' list_all_imgs = list(img_path.glob('**/*.jpg')) self.assertEqual(len(list_all_imgs), self.expected_num_frames)
from Models.erfh5_DeconvModel import S80DeconvModelEfficient2 from Pipeline.data_gather import get_filelist_within_folder_blacklisted from Pipeline.data_loaders_IMG import DataloaderImages from Trainer.ModelTrainer import ModelTrainer from Trainer.evaluation import SensorToFlowfrontEvaluator from Utils.training_utils import read_cmd_params if __name__ == "__main__": """ This is the starting point for training the Deconv/Conv Part of the FlowFrontNet with 80 sensor data to Flowfront images. """ args = read_cmd_params() img_size = (112, 96) dl = DataloaderImages(image_size=img_size, sensor_indizes=((1, 4), (1, 4))) m = ModelTrainer( lambda: S80DeconvModelEfficient2(demo_mode=True if args.demo is not None else False), data_source_paths=r.get_data_paths_base_0(), save_path=r.save_path if args.demo is None else Path(args.demo), load_datasets_path=r.datasets_dryspots, cache_path=r.cache_path, batch_size=128, train_print_frequency=100, epochs=1000, num_workers=75, num_validation_samples=131072, num_test_samples=1048576, data_processing_function=dl.get_sensordata_and_flowfront,
import torch import Resources.training as r from Models.erfh5_DeconvModel import DeconvModelEfficient from Pipeline.data_gather import get_filelist_within_folder_blacklisted from Pipeline.data_loaders_IMG import DataloaderImages from Trainer.ModelTrainer import ModelTrainer from Trainer.evaluation import SensorToFlowfrontEvaluator if __name__ == "__main__": dl = DataloaderImages((149, 117)) checkpoint_p = r.chkp_S1140_to_ff_0_basepr adv_output_dir = checkpoint_p.parent / "advanced_eval" m = ModelTrainer( lambda: DeconvModelEfficient(), data_source_paths=r.get_data_paths_base_0(), save_path=r.save_path, load_datasets_path=r.datasets_dryspots, cache_path=r.cache_path, batch_size=2048, train_print_frequency=10, epochs=1000, num_workers=75, num_validation_samples=131072, num_test_samples=1048576, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.MSELoss(), optimizer_function=lambda params: torch.optim.AdamW(params, lr=0.0001),
import torch import Resources.training as r from Models.erfh5_DeconvModel import S80DeconvModelEfficient2 from Pipeline.data_gather import get_filelist_within_folder_blacklisted from Pipeline.data_loaders_IMG import DataloaderImages from Trainer.ModelTrainer import ModelTrainer from Trainer.evaluation import SensorToFlowfrontEvaluator if __name__ == "__main__": dl = DataloaderImages(image_size=(112, 96), sensor_indizes=((1, 4), (1, 4))) checkpoint_p = r.chkp_S80_to_ff2 adv_output_dir = checkpoint_p.parent / "advanced_eval" m = ModelTrainer( lambda: S80DeconvModelEfficient2(pretrained="all", freeze_nlayers=9, checkpoint_path=checkpoint_p, round_at=.8), data_source_paths=r.get_data_paths_base_0(), save_path=r.save_path, dataset_split_path=r.dataset_split, cache_path=r.cache_path, batch_size=2048, train_print_frequency=100, epochs=1000, num_workers=75, num_validation_samples=131072, num_test_samples=1048576,
import Resources.training as r from Models.erfh5_DeconvModel import DeconvModelEfficient from Pipeline.data_gather import get_filelist_within_folder_blacklisted from Pipeline.data_loaders_IMG import DataloaderImages from Trainer.ModelTrainer import ModelTrainer from Trainer.evaluation import SensorToFlowfrontEvaluator from Utils.training_utils import read_cmd_params if __name__ == "__main__": """ This is the starting point for training the Deconv/Conv Part of the FlowFrontNet with 1140 sensor data to Flowfront images. """ args = read_cmd_params() dl = DataloaderImages(image_size=(149, 117)) m = ModelTrainer( lambda: DeconvModelEfficient(), data_source_paths=r.get_data_paths_base_0(), save_path=r.save_path if args.demo is None else Path(args.demo), load_datasets_path=r.datasets_dryspots, cache_path=r.cache_path, batch_size=2048, train_print_frequency=100, epochs=1000, num_workers=75, num_validation_samples=131072, num_test_samples=1048576, data_processing_function=dl.get_sensordata_and_flowfront, data_gather_function=get_filelist_within_folder_blacklisted, loss_criterion=torch.nn.MSELoss(),
def test_ignore_useless(self): dl = DataloaderImages() dl2 = DataloaderImages(ignore_useless_states=False) res = dl.get_sensordata_and_flowfront(resources.test_useless_file) res2 = dl2.get_sensordata_and_flowfront(resources.test_useless_file) self.assertEqual(len(res2) - len(res), 17) # since 17 frames are ignored in this file