def exp2(): """ Plot ged and time. """ dataset = 'aids50' models = BASELINE_MODELS rs = load_results_as_dict( dataset, models, row_graphs=load_data(dataset, train=False).graphs, col_graphs=load_data(dataset, train=True).graphs) metrics = [Metric('ged', 'ged'), Metric('time', 'time (msec)')] for metric in metrics: plot_ged_time_helper(dataset, models, metric, rs)
def runExperiment(): seed = int(cfg['model_tag'].split('_')[0]) torch.manual_seed(seed) torch.cuda.manual_seed(seed) dataset = fetch_dataset(cfg['data_name'], cfg['subset']) process_dataset(dataset['train']) if cfg['raw']: data_loader = make_data_loader(dataset)['train'] metric = Metric() img = [] for i, input in enumerate(data_loader): input = collate(input) img.append(input['img']) img = torch.cat(img, dim=0) output = {'img': img} evaluation = metric.evaluate(cfg['metric_name']['test'], None, output) is_result, fid_result = evaluation['InceptionScore'], evaluation['FID'] print('Inception Score ({}): {}'.format(cfg['data_name'], is_result)) print('FID ({}): {}'.format(cfg['data_name'], fid_result)) save(is_result, './output/result/is_generated_{}.npy'.format(cfg['data_name']), mode='numpy') save(fid_result, './output/result/fid_generated_{}.npy'.format(cfg['data_name']), mode='numpy') else: generated = np.load('./output/npy/generated_{}.npy'.format( cfg['model_tag']), allow_pickle=True) test(generated) return
def test(data_loader, ae, model, logger, epoch): with torch.no_grad(): metric = Metric() ae.train(False) model.train(False) for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input = to_device(input, cfg['device']) _, _, input['img'] = ae.encode(input['img']) input['img'] = input['img'].detach() output = model(input) output['loss'] = output['loss'].mean( ) if cfg['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(cfg['metric_name']['test'], input, output) logger.append(evaluation, 'test', input_size) logger.append(evaluation, 'test') info = { 'info': [ 'Model: {}'.format(cfg['model_tag']), 'Test Epoch: {}({:.0f}%)'.format(epoch, 100.) ] } logger.append(info, 'test', mean=False) logger.write('test', cfg['metric_name']['test']) return
def __init__(self, fold): self.fold = fold self.threshold = float(open(f"threshold_{self.fold}.txt", "r").read()) #0.5#0.1 self.metric = Metric()
def train(data_loader, model, optimizer, logger, epoch): metric = Metric() model.train(True) for i, input in enumerate(data_loader): start_time = time.time() input = collate(input) input_size = len(input['img']) input = to_device(input, config.PARAM['device']) model.zero_grad() output = model(input) output['loss'] = output['loss'].mean() if config.PARAM['world_size'] > 1 else output['loss'] output['loss'].backward() optimizer.step() if i % int((len(data_loader) * config.PARAM['log_interval']) + 1) == 0: batch_time = time.time() - start_time lr = optimizer.param_groups[0]['lr'] epoch_finished_time = datetime.timedelta(seconds=round(batch_time * (len(data_loader) - i - 1))) exp_finished_time = epoch_finished_time + datetime.timedelta( seconds=round((config.PARAM['num_epochs'] - epoch) * batch_time * len(data_loader))) info = {'info': ['Model: {}'.format(config.PARAM['model_tag']), 'Train Epoch: {}({:.0f}%)'.format(epoch, 100. * i / len(data_loader)), 'Learning rate: {}'.format(lr), 'Epoch Finished Time: {}'.format(epoch_finished_time), 'Experiment Finished Time: {}'.format(exp_finished_time)]} logger.append(info, 'train', mean=False) evaluation = metric.evaluate(config.PARAM['metric_names']['train'], input, output) logger.append(evaluation, 'train', n=input_size) logger.write('train', config.PARAM['metric_names']['train']) return
def runExperiment(): seed = int(cfg['model_tag'].split('_')[0]) torch.manual_seed(seed) torch.cuda.manual_seed(seed) dataset = fetch_dataset(cfg['data_name'], cfg['subset']) process_dataset(dataset['train']) if cfg['raw']: data_loader = make_data_loader(dataset)['train'] metric = Metric() img, label = [], [] for i, input in enumerate(data_loader): input = collate(input) img.append(input['img']) label.append(input['label']) img = torch.cat(img, dim=0) label = torch.cat(label, dim=0) output = {'img': img, 'label': label} evaluation = metric.evaluate(cfg['metric_name']['test'], None, output) dbi_result = evaluation['DBI'] print('Davies-Bouldin Index ({}): {}'.format(cfg['data_name'], dbi_result)) save(dbi_result, './output/result/dbi_created_{}.npy'.format(cfg['data_name']), mode='numpy') else: created = np.load('./output/npy/created_{}.npy'.format( cfg['model_tag']), allow_pickle=True) test(created) return
def main(): st = pst.SpeedTest() csv_file = os.path.join(os.path.dirname(__file__), "data", "metrics.csv") csv_writer = MetricWriterCSV(csv_file) json_file = os.path.join(os.path.dirname(__file__), "data", "metrics.json") json_writer = MetricWriterJSON(json_file) while True: try: ping = st.ping() upload = st.upload() download = st.download() except KeyboardInterrupt: sys.exit(0) except Exception: ping, upload, download = 0.0, 0.0, 0.0 is_online = False else: is_online = True metric = Metric(ping, upload, download, is_online) csv_writer.write(metric) json_writer.write(metric) print(metric)
def extract(self, context, data): context['origin'] = 'mw_profiler' metric_defs = { 'response_time': MetricType.TIME, 'database.queries.list': MetricType.QUERY_LIST, 'database.queries.time': MetricType.TIME, 'database.queries.master_count': MetricType.COUNT, 'database.queries.slave_count': MetricType.COUNT, 'memcached.time': MetricType.TIME, 'memcached.miss_count': MetricType.COUNT, 'memcached.hit_count': MetricType.COUNT, 'memcached.dupe_count': MetricType.COUNT, } name_template = 'server.app.{}' metrics = { name: Metric(name_template.format(name), context, type) for name, type in metric_defs.items() } for single_run in data: data = self.parse_data(single_run['content']) for name, raw_value in data.items(): metrics[name].add_value(raw_value, None) return Collection(metrics.values())
def test(dataset, data_split, label_split, model, logger, epoch): with torch.no_grad(): metric = Metric() model.train(False) for m in range(cfg['num_users']): data_loader = make_data_loader({'test': SplitDataset(dataset, data_split[m])})['test'] for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input['label_split'] = torch.tensor(label_split[m]) input = to_device(input, cfg['device']) output = model(input) output['loss'] = output['loss'].mean() if cfg['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(cfg['metric_name']['test']['Local'], input, output) logger.append(evaluation, 'test', input_size) data_loader = make_data_loader({'test': dataset})['test'] for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input = to_device(input, cfg['device']) output = model(input) output['loss'] = output['loss'].mean() if cfg['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(cfg['metric_name']['test']['Global'], input, output) logger.append(evaluation, 'test', input_size) info = {'info': ['Model: {}'.format(cfg['model_tag']), 'Test Epoch: {}({:.0f}%)'.format(epoch, 100.)]} logger.append(info, 'test', mean=False) logger.write('test', cfg['metric_name']['test']['Local'] + cfg['metric_name']['test']['Global']) return
def __init__(self, hparams, split_table_path, split_table_name, debug_folder, model, gpu,downsample): # load the model self.hparams = hparams self.model = model self.gpu = gpu self.downsample = downsample print('\n') print('Selected Learning rate:', self.hparams['lr']) print('\n') self.debug_folder = debug_folder self.split_table_path = split_table_path self.split_table_name = split_table_name self.exclusions = ['S0431', 'S0326' 'S0453' 'S0458' 'A5766' 'A0227' 'A0238' 'A1516' 'A5179' 'Q1807' 'Q3568' 'E10256' 'E07341' 'E05758'] self.splits = self.load_split_table() self.metric = Metric()
def test(data_loader, model, logger, epoch): with torch.no_grad(): metric = Metric() model.train(False) for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input = to_device(input, cfg['device']) output = model(input) output['loss'] = output['loss'].mean( ) if cfg['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(cfg['metric_name']['test'], input, output) logger.append(evaluation, 'test', input_size) logger.append(evaluation, 'test') info = { 'info': [ 'Model: {}'.format(cfg['model_tag']), 'Test Epoch: {}({:.0f}%)'.format(epoch, 100.) ] } logger.append(info, 'test', mean=False) logger.write('test', cfg['metric_name']['test']) if cfg['show']: input['reconstruct'] = True input['z'] = output['z'] output = model.reverse(input) save_img(input['img'][:100], './output/vis/input_{}.png'.format(cfg['model_tag']), range=(-1, 1)) save_img(output['img'][:100], './output/vis/output_{}.png'.format(cfg['model_tag']), range=(-1, 1)) return
def main(): # parse commands parser = ArgParser(sys.argv, mode='assess') args = Args(parser, mode='assess') # calculate STOI and SNR scores metric = Metric(args) metric.getSTOI()
def fan_out_by_url_and_push_metrics(metrics, out_name, base_context, data_type, values): by_url = defaultdict(list) for url, value in values: by_url[url].append(value) for url, values in by_url.items(): context = base_context.copy() context['url'] = url metrics.add(Metric(out_name, context, data_type, values=values))
def extract(self, context, data): context['origin'] = 'requests' metrics = Collection() metrics.add( Metric('server.app.response_time', context, MetricType.TIME, values=[(float(single_run['time']), None) for single_run in data])) metrics.add( Metric('server.app.response_size', context, MetricType.BYTES, values=[(single_run['content_length'], None) for single_run in data])) return metrics
def __init__(self, input_size, n_channels, hparams): self.hparams = hparams self.device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") # define the models self.model = WaveNet(n_channels=n_channels).to(self.device) summary(self.model, (input_size, n_channels)) # self.model.half() if torch.cuda.device_count() > 1: print("Number of GPUs will be used: ", torch.cuda.device_count() - 3) self.model = DP(self.model, device_ids=list( range(torch.cuda.device_count() - 3))) else: print('Only one GPU is available') self.metric = Metric() self.num_workers = 1 ########################## compile the model ############################### # define optimizer self.optimizer = torch.optim.Adam(params=self.model.parameters(), lr=self.hparams['lr'], weight_decay=1e-5) # weights = torch.Tensor([0.025,0.033,0.039,0.046,0.069,0.107,0.189,0.134,0.145,0.262,1]).cuda() self.loss = nn.BCELoss() # CompLoss(self.device) # define early stopping self.early_stopping = EarlyStopping( checkpoint_path=self.hparams['checkpoint_path'] + '/checkpoint.pt', patience=self.hparams['patience'], delta=self.hparams['min_delta'], ) # lr cheduler self.scheduler = ReduceLROnPlateau( optimizer=self.optimizer, mode='max', factor=0.2, patience=3, verbose=True, threshold=self.hparams['min_delta'], threshold_mode='abs', cooldown=0, eps=0, ) self.seed_everything(42) self.threshold = 0.75 self.scaler = torch.cuda.amp.GradScaler()
def test(data_loader, model): with torch.no_grad(): metric = Metric() model.train(False) for i, input in enumerate(data_loader): input = collate(input) input = to_device(input, config.PARAM['device']) output = model(input) output['loss'] = output['loss'].mean() if config.PARAM['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(config.PARAM['metric_names']['test'], input, output) print(evaluation) return evaluation
def extract(self, context, data): context['origin'] = 'phantomas' metrics = Collection() if len(data) == 0: return metrics metric_names = set() for run in data: metric_names.update(run['metrics'].keys()) for metric_name in metric_names: raw_id = 'raw.phantomas.' + metric_name id = raw_id type = MetricType.UNKNOWN if metric_name in self.KNOWN_METRICS: metric_def = self.KNOWN_METRICS[metric_name] if ':' in metric_def: type, id = metric_def.split(':') else: id = metric_def raw_values_and_infos = [ (self.normalize_phantomas_value(run['metrics'][metric_name], type), run['offenders'][metric_name] if metric_name in run['offenders'] else None) for run in data ] metrics.add(Metric(id, context, type, None, raw_values_and_infos)) if id != raw_id: metrics.add( Metric(raw_id, context, type, None, raw_values_and_infos)) return metrics
def test(dataset, model, logger, epoch): with torch.no_grad(): metric = Metric() model.train(False) batch_dataset = BatchDataset(dataset, cfg['bptt']) for i, input in enumerate(batch_dataset): input_size = input['label'].size(0) input = to_device(input, cfg['device']) output = model(input) output['loss'] = output['loss'].mean() if cfg['world_size'] > 1 else output['loss'] evaluation = metric.evaluate(cfg['metric_name']['test'], input, output) logger.append(evaluation, 'test', input_size) info = {'info': ['Model: {}'.format(cfg['model_tag']), 'Test Epoch: {}({:.0f}%)'.format(epoch, 100.)]} logger.append(info, 'test', mean=False) logger.write('test', cfg['metric_name']['test']) return
def test(created): with torch.no_grad(): metric = Metric() created = torch.tensor(created / 255 * 2 - 1) valid_mask = torch.sum(torch.isnan(created), dim=(1, 2, 3)) == 0 created = created[valid_mask] label = torch.arange(cfg['classes_size']) label = label.repeat(cfg['generate_per_mode']) label = label[valid_mask] output = {'img': created, 'label': label} evaluation = metric.evaluate(cfg['metric_name']['test'], None, output) dbi_result = evaluation['DBI'] print('Davies-Bouldin Index ({}): {}'.format(cfg['model_tag'], dbi_result)) save(dbi_result, './output/result/dbi_created_{}.npy'.format(cfg['model_tag']), mode='numpy') return evaluation
def __init__(self, env=None, mdp=None, policy=None): self.env = env self.mdp = mdp self.policy = policy self.model = self.get_model() # create metrics self.metrics = {'train': {}, 'test': {}, 'replay': {}} for mode in self.metrics.keys(): self.metrics[mode]['accumulated_reward'] = Metric( name='accumlated reward') self.metrics[mode]['reward'] = Metric(name='reward') self.metrics[mode]['pred_reward'] = Metric(name='reward') self.metrics[mode]['q_vals'] = Metric(name='q values') self.metrics[mode]['loss'] = Metric(name='loss') self.metrics[mode]['epsilon'] = Metric("epsilon") self.metrics[mode]['action'] = Metric("action")
def test(generated): with torch.no_grad(): metric = Metric() generated = torch.tensor(generated / 255 * 2 - 1) valid_mask = torch.sum(torch.isnan(generated), dim=(1, 2, 3)) == 0 generated = generated[valid_mask] output = {'img': generated} evaluation = metric.evaluate(cfg['metric_name']['test'], None, output) is_result, fid_result = evaluation['InceptionScore'], evaluation['FID'] print('Inception Score ({}): {}'.format(cfg['model_tag'], is_result)) print('FID ({}): {}'.format(cfg['model_tag'], fid_result)) save(is_result, './output/result/is_generated_{}.npy'.format(cfg['model_tag']), mode='numpy') save(fid_result, './output/result/fid_generated_{}.npy'.format(cfg['model_tag']), mode='numpy') return evaluation
def train(data_loader, ae, model, optimizer, logger, epoch): metric = Metric() ae.train(False) model.train(True) start_time = time.time() for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input = to_device(input, cfg['device']) with torch.no_grad(): _, _, input['img'] = ae.encode(input['img']) input['img'] = input['img'].detach() optimizer.zero_grad() output = model(input) output['loss'] = output['loss'].mean( ) if cfg['world_size'] > 1 else output['loss'] output['loss'].backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 1) optimizer.step() evaluation = metric.evaluate(cfg['metric_name']['train'], input, output) logger.append(evaluation, 'train', n=input_size) if i % int((len(data_loader) * cfg['log_interval']) + 1) == 0: batch_time = (time.time() - start_time) / (i + 1) lr = optimizer.param_groups[0]['lr'] epoch_finished_time = datetime.timedelta( seconds=round(batch_time * (len(data_loader) - i - 1))) exp_finished_time = epoch_finished_time + datetime.timedelta( seconds=round((cfg['num_epochs'] - epoch) * batch_time * len(data_loader))) info = { 'info': [ 'Model: {}'.format(cfg['model_tag']), 'Train Epoch: {}({:.0f}%)'.format( epoch, 100. * i / len(data_loader)), 'Learning rate: {}'.format(lr), 'Epoch Finished Time: {}'.format(epoch_finished_time), 'Experiment Finished Time: {}'.format(exp_finished_time) ] } logger.append(info, 'train', mean=False) logger.write('train', cfg['metric_name']['train']) return
def validate_epoch(val_loader, model, criterion, epoch, device): model.eval() val_loss = 0 metric = Metric(['accuracy', 'mean_iou'], len(val_loader.dataset.classes)) for image, label in tqdm(val_loader, total=len(val_loader)): image = image.to(device) label = label.to(device) with torch.no_grad(): pred = model(image) loss = criterion(pred, label) / len(image) val_loss += loss.item() metric.update(pred.data.cpu().numpy(), label.data.cpu().numpy()) metrics = metric.compute() return val_loss / len(val_loader), metrics['accuracy'], metrics['mean_iou']
def train(self, local_parameters, lr, logger): metric = Metric() model = eval('models.{}(model_rate=self.model_rate).to(cfg["device"])'.format(cfg['model_name'])) model.load_state_dict(local_parameters) model.train(True) optimizer = make_optimizer(model, lr) for local_epoch in range(1, cfg['num_epochs']['local'] + 1): for i, input in enumerate(self.data_loader): input = collate(input) input_size = input['img'].size(0) input['label_split'] = torch.tensor(self.label_split) input = to_device(input, cfg['device']) optimizer.zero_grad() output = model(input) output['loss'].backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 1) optimizer.step() evaluation = metric.evaluate(cfg['metric_name']['train']['Local'], input, output) logger.append(evaluation, 'train', n=input_size) local_parameters = model.state_dict() return local_parameters
def __init__(self, df_m15, df_h1, serial=False): self.df_m15 = standardize_data( df_m15, method="log_and_diff").dropna().reset_index(drop=True) self.df_h1 = df_h1 self.net_worth = INITIAL_BALANCE self.prev_net_worth = INITIAL_BALANCE self.usd_held = INITIAL_BALANCE self.eur_held = 0 self.current_step = 0 self.reward = 0 self.serial = serial # trade history self.trades = [] # our profit in last 5 trades self.returns = np.zeros(10) # index of episodes (1 episode equivalent to 1 week of trading) self.episode_indices_m15, self.h1_indices = get_episode( self.df_m15, self.df_h1) self.action_space = spaces.Discrete(6) # observation space, includes: OLHC prices (normalized), close price (unnormalized), # time in minutes(encoded), day of week(encoded), action history, net worth changes history # both minutes, days feature are encoded using sin and cos function to retain circularity self.observation_space = spaces.Box(low=-10, high=10, shape=(12, WINDOW_SIZE + 1), dtype=np.float16) self.metrics = Metric(INITIAL_BALANCE) self.setup_active_df() self.agent_history = { "actions": np.zeros(len(self.active_df) + WINDOW_SIZE), "net_worth": np.zeros(len(self.active_df) + WINDOW_SIZE), "eur_held": np.zeros(len(self.active_df) + WINDOW_SIZE), "usd_held": np.full(len(self.active_df), self.usd_held / BALANCE_NORM_FACTOR) }
def test(model, logger, epoch): sample_per_iter = cfg['batch_size']['test'] with torch.no_grad(): metric = Metric() model.train(False) C = torch.arange(cfg['classes_size']) C = C.repeat(cfg['generate_per_mode']) cfg['z'] = torch.randn([C.size(0), cfg['gan']['latent_size']]) if 'z' not in cfg else cfg['z'] C_generated = torch.split(C, sample_per_iter) z_generated = torch.split(cfg['z'], sample_per_iter) generated = [] for i in range(len(C_generated)): C_generated_i = C_generated[i].to(cfg['device']) z_generated_i = z_generated[i].to(cfg['device']) generated_i = model.generate(C_generated_i, z_generated_i) generated.append(generated_i.cpu()) generated = torch.cat(generated) output = {'img': generated} evaluation = metric.evaluate(cfg['metric_name']['test'], None, output) logger.append(evaluation, 'test') info = {'info': ['Model: {}'.format(cfg['model_tag']), 'Test Epoch: {}({:.0f}%)'.format(epoch, 100.)]} logger.append(info, 'test', mean=False) logger.write('test', cfg['metric_name']['test']) return
def __init__(self, input_size, n_channels, hparams, gpu, inference=False): self.hparams = hparams if inference: self.device = torch.device('cpu') self.model = ECGNet(n_channels=n_channels, hparams=self.hparams).to(self.device) else: if torch.cuda.device_count() > 1: if len(gpu) > 0: print("Number of GPUs will be used: ", len(gpu)) self.device = torch.device(f"cuda:{gpu[0]}" if torch.cuda. is_available() else "cpu") self.model = ECGNet(n_channels=n_channels, hparams=self.hparams).to(self.device) self.model = DP(self.model, device_ids=gpu, output_device=gpu[0]) else: print("Number of GPUs will be used: ", torch.cuda.device_count() - 5) self.device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") self.model = ECGNet(n_channels=n_channels, hparams=self.hparams).to(self.device) self.model = DP(self.model, device_ids=list( range(torch.cuda.device_count() - 5))) else: self.device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") self.model = ECGNet(n_channels=n_channels, hparams=self.hparams).to(self.device) print('Only one GPU is available') # define the models #summary(self.model, (input_size, n_channels)) #print(torch.cuda.is_available()) self.metric = Metric() self.num_workers = 18 self.threshold = 0.5 ########################## compile the model ############################### # define optimizer self.optimizer = torch.optim.Adam(params=self.model.parameters(), lr=self.hparams['lr']) weights = torch.Tensor([ 1., 1., 1., 1., 0.5, 1., 1., 1., 1., 1., 1., 1., 0.5, 0.5, 1., 1., 1., 1., 0.5, 1., 1., 1., 1., 0.5, 1., 1., 0.5 ]).to(self.device) self.loss = nn.BCELoss(weight=weights) # CompLoss(self.device) # self.decoder_loss = nn.MSELoss() # define early stopping self.early_stopping = EarlyStopping( checkpoint_path=self.hparams['checkpoint_path'] + '/checkpoint' + str(self.hparams['start_fold']) + '.pt', patience=self.hparams['patience'], delta=self.hparams['min_delta'], is_maximize=True, ) # lr cheduler self.scheduler = ReduceLROnPlateau( optimizer=self.optimizer, mode='max', factor=0.2, patience=1, verbose=True, threshold=self.hparams['min_delta'], threshold_mode='abs', cooldown=0, eps=0, ) self.seed_everything(42) self.postprocessing = PostProcessing(fold=self.hparams['start_fold']) self.scaler = torch.cuda.amp.GradScaler()
def train(data_loader, model, optimizer, logger, epoch): metric = Metric() model.train(True) start_time = time.time() for i, input in enumerate(data_loader): input = collate(input) input_size = input['img'].size(0) input = to_device(input, cfg['device']) ############################ # (1) Update D network ########################### for _ in range(cfg['iter']['discriminator']): # train with real optimizer['discriminator'].zero_grad() optimizer['generator'].zero_grad() D_x = model.discriminate(input['img'], input[cfg['subset']]) # train with fake z1 = torch.randn(input['img'].size(0), cfg['gan']['latent_size'], device=cfg['device']) generated = model.generate(input[cfg['subset']], z1) D_G_z1 = model.discriminate(generated.detach(), input[cfg['subset']]) if cfg['loss_type'] == 'BCE': D_loss = torch.nn.functional.binary_cross_entropy_with_logits( D_x, torch.ones((input['img'].size(0), 1), device=cfg['device'])) + \ torch.nn.functional.binary_cross_entropy_with_logits( D_G_z1, torch.zeros((input['img'].size(0), 1), device=cfg['device'])) elif cfg['loss_type'] == 'Hinge': D_loss = torch.nn.functional.relu(1.0 - D_x).mean() + torch.nn.functional.relu(1.0 + D_G_z1).mean() else: raise ValueError('Not valid loss type') D_loss.backward() optimizer['discriminator'].step() ############################ # (2) Update G network ########################### for _ in range(cfg['iter']['generator']): optimizer['discriminator'].zero_grad() optimizer['generator'].zero_grad() z2 = torch.randn(input['img'].size(0), cfg['gan']['latent_size'], device=cfg['device']) generated = model.generate(input[cfg['subset']], z2) D_G_z2 = model.discriminate(generated, input[cfg['subset']]) if cfg['loss_type'] == 'BCE': G_loss = torch.nn.functional.binary_cross_entropy_with_logits( D_G_z2, torch.ones((input['img'].size(0), 1), device=cfg['device'])) elif cfg['loss_type'] == 'Hinge': G_loss = -D_G_z2.mean() else: raise ValueError('Not valid loss type') G_loss.backward() optimizer['generator'].step() output = {'loss': abs(D_loss - G_loss), 'loss_D': D_loss, 'loss_G': G_loss} evaluation = metric.evaluate(cfg['metric_name']['train'], input, output) logger.append(evaluation, 'train', n=input_size) if i % int((len(data_loader) * cfg['log_interval']) + 1) == 0: batch_time = (time.time() - start_time) / (i + 1) generator_lr, discriminator_lr = optimizer['generator'].param_groups[0]['lr'], \ optimizer['discriminator'].param_groups[0]['lr'] epoch_finished_time = datetime.timedelta(seconds=round(batch_time * (len(data_loader) - i - 1))) exp_finished_time = epoch_finished_time + datetime.timedelta( seconds=round((cfg['num_epochs'] - epoch) * batch_time * len(data_loader))) info = {'info': ['Model: {}'.format(cfg['model_tag']), 'Train Epoch: {}({:.0f}%)'.format(epoch, 100. * i / len(data_loader)), 'Learning rate : (G: {}, D: {})'.format(generator_lr, discriminator_lr), 'Epoch Finished Time: {}'.format(epoch_finished_time), 'Experiment Finished Time: {}'.format(exp_finished_time)]} logger.append(info, 'train', mean=False) logger.write('train', cfg['metric_name']['train']) return
device = torch.device('cpu') ''' config = { "train": { "args": { "epochs": 5 } } } ''' path = 'config.json' config = json.load(open(path)) net = ToyModel(64, 13) criterion = nn.CrossEntropyLoss(ignore_index=-1, reduction='mean') optimizer = optim.Adam(net.parameters()) metric = Metric(nclasses=13) trainer = Trainer(device, config, net, criterion, optimizer, metric) train_dataset = [(torch.randn(size=(3, 100, 100)), torch.randn(size=(100, 100)), torch.randint(low=-1, high=13, size=(100, 100)).long()) for _ in range(99)] train_dataloader = data.DataLoader(train_dataset, batch_size=4) test_dataset = [(torch.randn(size=(3, 100, 100)), torch.randn(size=(100, 100)), torch.randint(low=-1, high=13, size=(100, 100)).long()) for _ in range(99)] test_dataloader = data.DataLoader(test_dataset, batch_size=4) trainer.train(train_dataloader, test_dataloader)