def __init__(self, args): self.run_name = args.run_name + '_' + str(time.time()).split('.')[0] self.current_run_basepath = args.network_metrics_basepath + '/' + self.run_name + '/' self.learning_rate = args.learning_rate self.epochs = args.epochs self.test_net = args.test_net self.train_net = args.train_net self.batch_size = args.batch_size self.num_classes = args.num_classes self.audio_basepath = args.audio_basepath self.train_data_file = args.train_data_file self.test_data_file = args.test_data_file self.data_read_path = args.data_save_path self.is_cuda_available = torch.cuda.is_available() self.display_interval = args.display_interval self.sampling_rate = args.sampling_rate self.sample_size_in_seconds = args.sample_size_in_seconds self.overlap = args.overlap self.network_metrics_basepath = args.network_metrics_basepath self.tensorboard_summary_path = self.current_run_basepath + args.tensorboard_summary_path self.network_save_path = self.current_run_basepath + args.network_save_path self.network_restore_path = args.network_restore_path self.device = torch.device("cuda" if self.is_cuda_available else "cpu") self.network_save_interval = args.network_save_interval self.normalise = args.normalise_while_training self.data_augment = args.data_augment self.dropout = args.dropout self.threshold = args.threshold self.debug_filename = self.current_run_basepath + '/' + args.debug_filename paths = [self.network_save_path, self.tensorboard_summary_path] file_utils.create_dirs(paths) self.pos_weight = None self.learning_rate_decay = args.learning_rate_decay self._min, self._max = float('inf'), -float('inf') self.logger = Logger(name=self.run_name, log_path=self.network_save_path).get_logger() self.logger.info(str(json.dumps(args, indent=4))) self.writer = SummaryWriter(self.tensorboard_summary_path) self.batch_loss, self.batch_accuracy, self.uar = [], [], [] self.logger.info(f'Configs used:\n{json.dumps(args, indent=4)}') self.data_loaders = {} self.dataset_sizes = {}
def __init__(self, args): self.run_name = args.run_name + '_' + str(time.time()).split('.')[0] self.current_run_basepath = args.network_metrics_basepath + '/' + self.run_name + '/' self.learning_rate = args.learning_rate self.epochs = args.epochs self.test_net = args.test_net self.train_net = args.train_net self.batch_size = args.batch_size self.num_classes = args.num_classes self.audio_basepath = args.audio_basepath self.train_data_file = args.train_data_file self.test_data_file = args.test_data_file self.data_read_path = args.data_save_path self.is_cuda_available = torch.cuda.is_available() self.display_interval = args.display_interval self.sampling_rate = args.sampling_rate self.sample_size_in_seconds = args.sample_size_in_seconds self.overlap = args.overlap self.alpha = args.alpha self.beta = args.beta self.network_metrics_basepath = args.network_metrics_basepath self.tensorboard_summary_path = self.current_run_basepath + args.tensorboard_summary_path self.network_save_path = self.current_run_basepath + args.network_save_path self.network_restore_path = args.network_restore_path self.device = torch.device("cuda" if self.is_cuda_available else "cpu") self.network_save_interval = args.network_save_interval self.normalise = args.normalise_while_training self.dropout = args.dropout self.threshold = args.threshold paths = [self.network_save_path, self.tensorboard_summary_path] file_utils.create_dirs(paths) self.network = ConvAutoEncoder().to(self.device) self.reconstruction_loss = nn.BCEWithLogitsLoss() self.optimiser = optim.Adam(self.network.parameters(), lr=self.learning_rate) self._min, self._max = float('inf'), -float('inf') if self.train_net: self.network.train() self.log_file = open(self.network_save_path + '/' + self.run_name + '.log', 'w') self.log_file.write(json.dumps(args)) if self.test_net: print('Loading Network') self.network.load_state_dict(torch.load(self.network_restore_path, map_location=self.device)) self.network.eval() self.log_file = open(self.network_restore_path.replace('_40.pt', '.log'), 'a') print('\n\n\n********************************************************', file=self.log_file) print('Testing Model - ', self.network_restore_path) print('Testing Model - ', self.network_restore_path, file=self.log_file) print('********************************************************', file=self.log_file) self.writer = SummaryWriter(self.tensorboard_summary_path) print("Network config:\n", self.network) print("Network config:\n", self.network, file=self.log_file) self.batch_accuracy, self.uar = [], [] print('Configs used:\n', json.dumps(args, indent=4)) print('Configs used:\n', json.dumps(args, indent=4), file=self.log_file)
def __init__(self, args): self.run_name = args.run_name + '_' + str(time.time()).split('.')[0] self.current_run_basepath = args.network_metrics_basepath + '/' + self.run_name + '/' self.learning_rate = args.learning_rate self.epochs = args.epochs self.test_net = args.test_net self.train_net = args.train_net self.batch_size = args.batch_size self.num_classes = args.num_classes self.audio_basepath = args.audio_basepath self.train_data_file = args.train_data_file self.test_data_file = args.test_data_file self.data_read_path = args.data_save_path self.is_cuda_available = torch.cuda.is_available() self.display_interval = args.display_interval self.sampling_rate = args.sampling_rate self.sample_size_in_seconds = args.sample_size_in_seconds self.overlap = args.overlap self.network_metrics_basepath = args.network_metrics_basepath self.tensorboard_summary_path = self.current_run_basepath + args.tensorboard_summary_path self.network_save_path = self.current_run_basepath + args.network_save_path self.network_restore_path = args.network_restore_path self.device = torch.device("cuda" if self.is_cuda_available else "cpu") self.network_save_interval = args.network_save_interval self.normalise = args.normalise_while_training self.dropout = args.dropout self.threshold = args.threshold self.debug_filename = self.current_run_basepath + '/' + args.debug_filename paths = [self.network_save_path, self.tensorboard_summary_path] file_utils.create_dirs(paths) args.sincnet_saved_model = args.data_save_path + args.sincnet_saved_model self.sincnet_params = [] self.network = SincNet(args).to(self.device) # for x in self.network.state_dict().keys(): # # if isinstance(self.network.state_dict()[x], float): # print(x, torch.max(self.network.state_dict()[x]), torch.min(self.network.state_dict()[x])) self.saved_model = \ torch.load(args['sincnet_saved_model'], map_location=self.device)[ 'CNN_model_par'] self.load_my_state_dict(self.saved_model) print(self.sincnet_params) # exit() # for i, param in enumerate(self.network.parameters()): # if name in self.sincnet_params: # self.network.state_dict()[name].requires_grad = False # if i <= 19: # param.requires_grad = False # for x in self.network.state_dict().keys(): # # if isinstance(self.network.state_dict()[x], float): # print(x, torch.max(self.network.state_dict()[x]), torch.min(self.network.state_dict()[x])) self.pos_weight = None self.loss_function = None self.learning_rate_decay = args.learning_rate_decay self.optimiser = optim.RMSprop(self.network.parameters(), lr=self.learning_rate, alpha=0.95, eps=1e-8) self.scheduler = torch.optim.lr_scheduler.ExponentialLR( self.optimiser, gamma=self.learning_rate_decay) self._min, self._max = float('inf'), -float('inf') if self.train_net: self.network.train() self.log_file = open( self.network_save_path + '/' + self.run_name + '.log', 'w') self.log_file.write(json.dumps(args)) if self.test_net: print('Loading Network') self.network.load_state_dict( torch.load(self.network_restore_path, map_location=self.device)) self.network.eval() self.log_file = open( self.network_restore_path.replace('_40.pt', '.log'), 'a') print( '\n\n\n********************************************************', file=self.log_file) print('Testing Model - ', self.network_restore_path) print('Testing Model - ', self.network_restore_path, file=self.log_file) print('********************************************************', file=self.log_file) self.writer = SummaryWriter(self.tensorboard_summary_path) print("Network config:\n", self.network) print("Network config:\n", self.network, file=self.log_file) self.batch_loss, self.batch_accuracy, self.uar = [], [], [] print('Configs used:\n', json.dumps(args, indent=4)) print('Configs used:\n', json.dumps(args, indent=4), file=self.log_file)
def __init__(self, args): self.run_name = args.run_name + '_' + str(time.time()).split('.')[0] self.current_run_basepath = args.network_metrics_basepath + '/' + self.run_name + '/' self.learning_rate = args.learning_rate self.epochs = args.epochs self.test_net = args.test_net self.train_net = args.train_net self.batch_size = args.batch_size self.num_classes = args.num_classes self.audio_basepath = args.audio_basepath self.train_data_file = args.train_data_file self.test_data_file = args.test_data_file self.data_read_path = args.data_save_path self.is_cuda_available = torch.cuda.is_available() self.display_interval = args.display_interval self.sampling_rate = args.sampling_rate self.sample_size_in_seconds = args.sample_size_in_seconds self.overlap = args.overlap self.network_metrics_basepath = args.network_metrics_basepath self.tensorboard_summary_path = self.current_run_basepath + args.tensorboard_summary_path self.network_save_path = self.current_run_basepath + args.network_save_path self.network_restore_path = args.network_restore_path self.device = torch.device("cuda" if self.is_cuda_available else "cpu") self.network_save_interval = args.network_save_interval self.normalise = args.normalise_while_training self.data_augment = args.data_augment self.dropout = args.dropout self.threshold = args.threshold self.debug_filename = self.current_run_basepath + '/' + args.debug_filename paths = [self.network_save_path, self.tensorboard_summary_path] file_utils.create_dirs(paths) self.network = ConvNet().to(self.device) self.pos_weight = None self.loss_function = None self.learning_rate_decay = args.learning_rate_decay self.optimiser = optim.AdamW(self.network.parameters(), lr=self.learning_rate) self.scheduler = torch.optim.lr_scheduler.ExponentialLR( self.optimiser, gamma=self.learning_rate_decay) self._min, self._max = float('inf'), -float('inf') if self.train_net: self.network.train() self.logger = Logger(name=self.run_name, log_path=self.network_save_path).get_logger() self.logger.info(str(json.dumps(args, indent=4))) if self.test_net: self.logger.info('Loading Network') self.network.load_state_dict( torch.load(self.network_restore_path, map_location=self.device)) self.network.eval() self.logger.info( '\n\n\n********************************************************' ) self.logger.info(f'Testing Model - {self.network_restore_path}') self.logger.info( '********************************************************') self.writer = SummaryWriter(self.tensorboard_summary_path) self.logger.info(f"Network Architecture:\n,{self.network}") self.batch_loss, self.batch_accuracy, self.uar = [], [], [] self.logger.info(f'Configs used:\n{json.dumps(args, indent=4)}')