def getopt(argv): p = argparse.ArgumentParser() # train options p.add_argument('--checkpoint-dir', type=str, default='./checkpoint') p.add_argument('--num-epochs', type=int, default=50) p.add_argument('--start-epoch', type=int, default=1) p.add_argument('--batch-size', type=int, default=64) p.add_argument('--lr', type=float, default=1e-3) # experiment options p.add_argument('--seed', type=int, default=0) p.add_argument('--cpus', type=int, default=-1) p.add_argument('--gpus', type=int, default=-1) p.add_argument('experiment_name', type=str) # p.add_argument('dataset_path', type=str, help='example: data/pth/C2-T18-win48-hop24.pth') args = p.parse_args(argv) # args.dataset_path = Path(args.dataset_path) args.dataset_path = 'data/pth_nostd/C2-T18-win48-hop1.pth' args.checkpoint_dir = Path(args.checkpoint_dir) / args.experiment_name args.checkpoint_dir.mkdir(parents=True, exist_ok=True) if args.cpus == -1: args.cpus = cpu_count() if args.gpus == -1: args.gpus = torch.cuda.device_count() tb.seed_everything(args.seed) return args
def getopt(argv): p = argparse.ArgumentParser() # train options p.add_argument('--checkpoint-dir', type=str, default='./checkpoint') p.add_argument('--num-epochs', type=int, default=20) p.add_argument('--start-epoch', type=int, default=1) p.add_argument('--batch-size', type=int, default=64) p.add_argument('--lr', type=float, default=1e-3) # experiment options p.add_argument('--cpus', type=int, default=-1) p.add_argument('--gpus', type=int, default=-1) p.add_argument('--seed', type=int, default=0) p.add_argument('experiment_name', type=str) p.add_argument('dataset_path', type=str, help='example: data/head/head-dataset-3166.hdf5') args = p.parse_args(argv) tb.seed_everything(args.seed) if args.cpus < 0: args.cpus = cpu_count() if args.gpus < 0: args.gpus = torch.cuda.device_count() args.dataset_path = Path(args.dataset_path) args.checkpoint_dir = Path(args.checkpoint_dir) / args.experiment_name args.checkpoint_dir.mkdir(parents=True, exist_ok=True) return args
def main(args): tb.seed_everything(args.seed) plt.switch_backend('agg') # matplotlib을 cli에서 사용 # 데이터셋 불러오기 data_train = np.load('data/1116/train-win_120-GAN.npz') data_test = np.load('data/1116/test-win_120-GAN.npz') ds_train = GANDataset(data_train['X'], data_train['Y']) ds_test = GANDataset(data_test['X'], data_test['Y']) dl_kwargs = dict(batch_size=args.batch_size, num_workers=2, pin_memory=True) dl_train = DataLoader(ds_train, **dl_kwargs, shuffle=True) dl_test = DataLoader(ds_test, **dl_kwargs, shuffle=False) # Create model G = CRNNC_GAN().cuda() D = ConvDetector(ResBlock1d, [2, 2, 2, 2]).cuda() g_criterion = nn.MSELoss().cuda() d_criterion = nn.BCELoss().cuda() g_optimizer = torch_optimizer.RAdam(G.parameters()) d_optimizer = torch_optimizer.RAdam(D.parameters()) div_means = MEANS[:3].reshape(1, 3, 1) div_stds = STDS[:3].reshape(1, 3, 1) for epoch in range(1, args.epochs + 1): losses = [[], [], [], [], [], []] G.train() D.train() for x_input_, x_real_ in dl_train: # D x_input = x_input_.cuda() x_real = x_real_.cuda() x_fake = G(x_input) p_real = D(x_real) p_fake = D(x_fake) y_real = torch.ones(x_real.shape[0], 1, dtype=torch.float32).cuda() y_fake = torch.zeros(x_fake.shape[0], 1, dtype=torch.float32).cuda() d_loss_real = d_criterion(p_real, y_real) d_loss_fake = d_criterion(p_fake, y_fake) d_loss = d_loss_real + d_loss_fake d_optimizer.zero_grad() d_loss.backward() d_optimizer.step() losses[0].append(d_loss.item()) losses[1].append(d_loss_real.item()) losses[2].append(d_loss_fake.item()) # G x_fake = G(x_input) # B, S, C p_fake = D(x_fake) g_loss_real = g_criterion(x_fake[:, -1:, :], x_real[:, -1:, :]) g_loss_fake = d_criterion(p_fake, y_fake) g_loss = g_loss_real * 0.05 + g_loss_fake * 0.95 g_optimizer.zero_grad() g_loss.backward() g_optimizer.step() losses[3].append(g_loss.item()) losses[4].append(g_loss_real.item()) losses[5].append(g_loss_fake.item()) losses = [sum(l) / len(l) for l in losses] print(f'[{epoch:03d}/{args.epochs:03d}] Train GAN: ' f'd_loss: {losses[0]:.4f}, ' f'd_loss_real: {losses[1]:.4f}, ' f'd_loss_fake: {losses[2]:.4f}, ' f'g_loss: {losses[3]:.4f}, ' f'g_loss_fake: {losses[5]:.4f}') G.eval() D.eval() with torch.no_grad(): losses = [[], [], [], [], [], []] X, Y, P = [], [], [] diffs = [] for x_input_, x_real_ in dl_test: # D x_input = x_input_.cuda() x_real = x_real_.cuda() x_fake = G(x_input) x_fake_ = x_fake.cpu() p_real = D(x_real) p_fake = D(x_fake) y_real = torch.ones(x_real.shape[0], 1, dtype=torch.float32).cuda() y_fake = torch.zeros(x_fake.shape[0], 1, dtype=torch.float32).cuda() d_loss_real = d_criterion(p_real, y_real) d_loss_fake = d_criterion(p_fake, y_fake) d_loss = d_loss_real + d_loss_fake losses[0].append(d_loss.item()) losses[1].append(d_loss_real.item()) losses[2].append(d_loss_fake.item()) # G x_fake = G(x_input) p_fake = D(x_fake) g_loss_real = g_criterion(x_fake[:, -1:, :], x_real[:, -1:, :]) g_loss_fake = d_criterion(p_fake, y_fake) g_loss = g_loss_real * 0.05 + g_loss_fake * 0.95 losses[3].append(g_loss.item()) losses[4].append(g_loss_real.item()) losses[5].append(g_loss_fake.item()) x = x_input_.transpose(1, 2)[:, :3, -18:] * div_stds + div_means y = x_real_.transpose(1, 2)[:, :3, -18:] * div_stds + div_means p = x_fake_.transpose(1, 2)[:, :3, -18:] * div_stds + div_means diffs.append((y - p)[:, :, -1]) X.append(torch.flatten(x, 0, 1)) Y.append(torch.flatten(y, 0, 1)) P.append(torch.flatten(p, 0, 1)) losses = [sum(l) / len(l) for l in losses] print(f'[{epoch:03d}/{args.epochs:03d}] Validate GAN: ' f'd_loss: {losses[0]:.4f}, ' f'd_loss_real: {losses[1]:.4f}, ' f'd_loss_fake: {losses[2]:.4f}, ' f'g_loss: {losses[3]:.4f}, ' f'g_loss_fake: {losses[5]:.4f}') diffs = torch.cat(diffs) # (B, 3) mae = diffs.abs().mean(dim=0) # (3, ) --> yaw, pitch, roll rms = mae.square().sum().div(3).sqrt() # (1, ) tile = diffs.square().mean(dim=1).sqrt().numpy() # (B, ) tile99 = np.percentile(tile, 99) print(f'[{epoch:03d}/{args.epochs:03d}] Validate GAN: ' f'yaw {mae[0].item():.4f}, ' f'pitch {mae[1].item():.4f}, ' f'roll {mae[2].item():.4f}, ' f'rms {rms.item():.4f}, ' f'tile99 {tile99:.4f}') X = torch.cat(X) # (L, 3) Y = torch.cat(Y) P = torch.cat(P) np.savez_compressed(args.experiment_path / f'data-epoch{epoch}.npz', X=X, Y=Y, P=P)
def main(args): tb.seed_everything(args.seed) plt.switch_backend('agg') # Create dataset ds_train = SingleFileDataset( Path(args.dataset) / f'train-win_{args.window_size}.npz') ds_test = SingleFileDataset( Path(args.dataset) / f'test-win_{args.window_size}.npz') # Create model model = get_model_by_name(args.network) if torch.cuda.device_count() > 0: model = model.cuda() criterion = nn.MSELoss().cuda() optimizer = torch_optimizer.RAdam(model.parameters()) hp_metric = HPMetric('hp_metric', args.experiment_path, 'history.log') metrics = [tb.metrics.ModuleMetric(criterion, 'loss'), hp_metric] callbacks = [ # tb.callbacks.EarlyStopping(metrics[0]), tb.callbacks.LRDecaying(optimizer, metrics[0], patience=3), tb.callbacks.SaveCheckpoint({'model': model}, metrics[0], args.experiment_path, 'best-ckpt.pth') ] # Training trainer = tb.Trainer(model, optimizer, metrics, callbacks, ncols=100, cpus=args.cpus) trainer.fit(ds_train, ds_test, num_epochs=args.epochs, batch_size=args.batch_size, shuffle=True, pin_memory=True) # Test model.eval() torch.set_grad_enabled(False) # Load best checkpoint ckpt = torch.load(args.experiment_path / 'best-ckpt.pth') model.load_state_dict(ckpt['model']) # Prediction dl = DataLoader(ds_test, batch_size=args.batch_size, num_workers=16) inputs, targets, preds = [], [], [] with tqdm(total=len(dl), position=0, ncols=100) as t: for x, y in dl: pred = model(x.cuda()).cpu() preds.append(pred) inputs.append(x) targets.append(y) t.update() X = torch.cat(inputs) Y = torch.cat(targets) P = torch.cat(preds) # Save prediction as file np.save(args.experiment_path / 'result-X.npy', X.numpy()) np.save(args.experiment_path / 'result-Y.npy', Y.numpy()) np.save(args.experiment_path / 'result-P.npy', P.numpy()) # Graph parameters height = 6 width = 4 # Plot graph as image file S = 600 L = 300 T = np.linspace(0, L / 60, L) plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 0]) plt.plot(T, P[S:S + L, 0]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Yaw') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_300-Yaw.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 1]) plt.plot(T, P[S:S + L, 1]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Pitch') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_300-Pitch.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 2]) plt.plot(T, P[S:S + L, 2]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Roll') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_300-Roll.png') S = 600 L = 600 T = np.linspace(0, L / 60, L) plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 0]) plt.plot(T, P[S:S + L, 0]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Yaw') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_600-Yaw.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 1]) plt.plot(T, P[S:S + L, 1]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Pitch') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_600-Pitch.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 2]) plt.plot(T, P[S:S + L, 2]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Roll') plt.tight_layout() plt.savefig(args.experiment_path / 'TrainingResult_600-Roll.png') S = 600 L = 3000 T = np.linspace(0, L / 60, L) plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 0]) plt.plot(T, P[S:S + L, 0]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Yaw') plt.tight_layout() plt.savefig(args.experiment_path / f'TrainingResult_{L}-Yaw.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 1]) plt.plot(T, P[S:S + L, 1]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Pitch') plt.tight_layout() plt.savefig(args.experiment_path / f'TrainingResult_{L}-Pitch.png') plt.figure(figsize=(height, width)) plt.plot(T, Y[S:S + L, 2]) plt.plot(T, P[S:S + L, 2]) plt.legend(['Real', model.__class__.__name__]) plt.xlabel('Time (s)') plt.ylabel('Degree') plt.title('Roll') plt.tight_layout() plt.savefig(args.experiment_path / f'TrainingResult_{L}-Roll.png') # Save loss history epochs_x = np.array(list(range(1, args.epochs + 1)), dtype=np.int) plt.figure(figsize=(height, width)) plt.plot(epochs_x, hp_metric.train_history['yaw']) plt.plot(epochs_x, hp_metric.train_history['pitch']) plt.plot(epochs_x, hp_metric.train_history['roll']) plt.plot(epochs_x, hp_metric.train_history['rms']) plt.plot(epochs_x, hp_metric.train_history['tile99']) plt.legend(['Yaw', 'Pitch', 'Roll', 'RMS', '99Percentile']) xpos = annot_min(epochs_x, np.array(hp_metric.train_history['tile99']), '99tile') annot_min(epochs_x, np.array(hp_metric.train_history['yaw']), 'yaw', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.train_history['pitch']), 'pitch', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.train_history['roll']), 'roll', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.train_history['rms']), 'rms', xpos=xpos) plt.xlabel('Epoch (Numbers)') plt.ylabel('Mean Error (Degree)') plt.ylim(0, 25) plt.title(f'Training Error ({model.__class__.__name__})') plt.tight_layout() plt.savefig(args.experiment_path / 'ErrorPlot-Training.png') plt.figure(figsize=(height, width)) plt.plot(epochs_x, hp_metric.valid_history['yaw']) plt.plot(epochs_x, hp_metric.valid_history['pitch']) plt.plot(epochs_x, hp_metric.valid_history['roll']) plt.plot(epochs_x, hp_metric.valid_history['rms']) plt.plot(epochs_x, hp_metric.valid_history['tile99']) plt.legend(['Yaw', 'Pitch', 'Roll', 'RMS', '99Percentile']) xpos = annot_min(epochs_x, np.array(hp_metric.valid_history['tile99']), '99tile') annot_min(epochs_x, np.array(hp_metric.valid_history['yaw']), 'yaw', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.valid_history['pitch']), 'pitch', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.valid_history['roll']), 'roll', xpos=xpos) annot_min(epochs_x, np.array(hp_metric.valid_history['rms']), 'rms', xpos=xpos) plt.xlabel('Epoch (Numbers)') plt.ylabel('Mean Error (Degree)') plt.ylim(0, 25) plt.title(f'Validation Error ({model.__class__.__name__})') plt.tight_layout() plt.savefig(args.experiment_path / 'ErrorPlot-Validation.png')