Exemplo n.º 1
0
 def signal_plot() -> None:
     """
     :rtype: None
     """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         writer.line("Signal", SIGNAL, step=0)
Exemplo n.º 2
0
    def __call__(
        self,
        *,
        iterations: int = 9999,
        log_directory: Union[str, Path],
        render_frequency: int = 100,
        stat_frequency: int = 10,
    ):
        """

:param num_steps_per_btach:
:param num_updates:
:param iterations:
:param log_directory:
:param render_frequency:
:param stat_frequency:
:param kwargs:
:return:
"""
        with torch.no_grad():
            with TensorBoardPytorchWriter(log_directory) as metric_writer:

                B = tqdm(count(), f"step {0}, {iterations}", leave=False)

                for _ in B:
                    initial_state = self.environment.reset()
                    self.agent.rollout(initial_state, self.environment)

                    if self.early_stop:
                        break

        return self.agent.models
Exemplo n.º 3
0
    def main(train_model_=False):

        dataset_loader = DataLoader(DATASET,
                                    batch_size=BATCH_SIZE,
                                    shuffle=True,
                                    **DL_KWARGS)

        optimiser = optim.Adam(MODEL.parameters(), lr=LR, betas=(0.9, 0.999))

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "VggFace2" /
                                      "BetaVAE" /
                                      f"{time.time()}") as metric_writer:
            for epoch in range(1, EPOCHS + 1):
                if train_model_:
                    train_model(MODEL, optimiser, epoch, metric_writer,
                                dataset_loader)
                if not train_model_:
                    stest_model(MODEL, epoch, metric_writer, dataset_loader)
                with torch.no_grad():
                    inv_sample = DATASET.inverse_transform(MODEL.sample().view(
                        CHANNELS, INPUT_SIZE, INPUT_SIZE))
                    inv_sample.save(str(BASE_PATH /
                                        f"sample_{str(epoch)}.png"))
                    if ENCODING_SIZE == 2:
                        from neodroidvision.utilities import plot_manifold

                        plot_manifold(
                            MODEL.decoder,
                            out_path=BASE_PATH / f"manifold_{str(epoch)}.png",
                            img_w=INPUT_SIZE,
                            img_h=INPUT_SIZE,
                        )
Exemplo n.º 4
0
def test_invalid_val_type_scalars(tag, val, step):
    try:
        with TensorBoardPytorchWriter(path=PROJECT_APP_PATH.user_log) as w:
            w.scalar(tag, val, step)
        assert False
    except Exception as e:
        assert True
Exemplo n.º 5
0
    def module_param_histograms():
        """ """
        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                      "Writers") as writer:
            input_f = 4
            n_classes = 10
            num_updates = 20

            model = torch.nn.Sequential(
                torch.nn.Linear(input_f, 20),
                torch.nn.ReLU(),
                torch.nn.Linear(20, n_classes),
                torch.nn.LogSoftmax(-1),
            )

            for i in range(num_updates):
                normal_init(model, 0.2 * float((i - num_updates * 0.5)**2),
                            1 / (i + 1))
                weight_bias_histograms(writer, model, step=i, prefix="normal")

                xavier_init(model)
                weight_bias_histograms(writer, model, step=i, prefix="xavier")

                constant_init(model, i)
                weight_bias_histograms(writer,
                                       model,
                                       step=i,
                                       prefix="constant")

                fan_in_init(model)
                weight_bias_histograms(writer, model, step=i, prefix="fan_in")
Exemplo n.º 6
0
def test_precision_recall(tag, val, truth, step):
    try:
        with TensorBoardPytorchWriter(path=PROJECT_APP_PATH.user_log) as w:
            w.precision_recall_curve(tag, val, truth, step)
        assert False
    except Exception as e:
        print(e)
        assert True
Exemplo n.º 7
0
 def fft_plot() -> None:
     """
     :rtype: None
     """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         spectral = numpy.fft.fft(SIGNAL, NFFT)
         writer.line("FFT", spectral, title="Frequency", step=0)
Exemplo n.º 8
0
 def cepstral_plot():
     """ """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         fig = pyplot.figure()
         stft = librosa.core.stft(SIGNAL, n_fft=NFFT, hop_length=STEP_SIZE)
         specshow(stft, sr=SAMPLING_RATE, x_axis="time")
         pyplot.colorbar()
         writer.figure("STFT_Rosa", fig, step=0)
Exemplo n.º 9
0
 def spectral_plot():
     """ """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         writer.spectrogram("STFT",
                            SIGNAL,
                            int(1 / DELTA),
                            step=0,
                            n_fft=NFFT,
                            step_size=STEP_SIZE)
Exemplo n.º 10
0
    def main():
        """

"""
        data_dir = Path.home() / "Data" / "mnist_png"
        train_batch_size = 64
        train_number_epochs = 100
        save_path = PROJECT_APP_PATH.user_data / "models"
        model_name = "pair_siamese_mnist"
        load_prev = True
        train = False
        img_size = (28, 28)
        model = PairRankingSiamese(img_size).to(global_torch_device())
        optimiser = optim.Adam(model.parameters(), lr=3e-4)

        if train:
            if load_prev:
                model, optimer = load_model_parameters(
                    model,
                    optimiser=optimiser,
                    model_name=model_name,
                    model_directory=save_path,
                )

            with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log /
                                          model_name /
                                          str(time.time())) as writer:
                # with CaptureEarlyStop() as _:
                with suppress(KeyboardInterrupt):
                    model = train_siamese(
                        model,
                        optimiser,
                        nn.BCELoss().to(global_torch_device()),
                        train_number_epochs=train_number_epochs,
                        data_dir=data_dir,
                        train_batch_size=train_batch_size,
                        model_name=model_name,
                        save_path=save_path,
                        writer=writer,
                        img_size=img_size,
                    )
            save_model_parameters(
                model,
                optimiser=optimiser,
                model_name=f"{model_name}",
                save_directory=save_path,
            )
        else:
            model = load_model_parameters(model,
                                          model_name=model_name,
                                          model_directory=save_path)
            print("loaded best val")
            stest_many_versus_many2(model, data_dir, img_size)
Exemplo n.º 11
0
 def mel_cepstral_plot():
     """ """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         fig = pyplot.figure()
         mfccs = librosa.feature.mfcc(SIGNAL,
                                      sr=SAMPLING_RATE,
                                      n_mfcc=20,
                                      n_fft=NFFT,
                                      hop_length=STEP_SIZE)
         specshow(mfccs, sr=SAMPLING_RATE, x_axis="time")
         pyplot.colorbar()
         writer.figure("MFCC_Rosa", fig, step=0)
Exemplo n.º 12
0
    def main():
        """ """
        data_dir = Path.home() / "Data" / "mnist_png"
        train_batch_size = 64
        train_number_epochs = 100
        save_path = PROJECT_APP_PATH.user_data / "models"
        model_name = "triplet_siamese_mnist"
        load_prev = True
        train = False

        img_size = (28, 28)
        model = NLetConvNet(img_size).to(global_torch_device())
        optimiser = optim.Adam(model.parameters(), lr=3e-4)

        if train:
            if load_prev:
                model, optimiser = load_model_parameters(
                    model,
                    optimiser=optimiser,
                    model_name=model_name,
                    model_directory=save_path,
                )

            with TensorBoardPytorchWriter():
                # from draugr.stopping import CaptureEarlyStop

                # with CaptureEarlyStop() as _:
                with IgnoreInterruptSignal():
                    model = train_siamese(
                        model,
                        optimiser,
                        TripletMarginLoss().to(global_torch_device()),
                        train_number_epochs=train_number_epochs,
                        data_dir=data_dir,
                        train_batch_size=train_batch_size,
                        model_name=model_name,
                        save_path=save_path,
                        img_size=img_size,
                    )
            save_model_parameters(
                model,
                optimiser=optimiser,
                model_name=f"{model_name}",
                save_directory=save_path,
            )
        else:
            model = load_model_parameters(model,
                                          model_name=model_name,
                                          model_directory=save_path)
            print("loaded best val")
            stest_many_versus_many(model, data_dir, img_size)
Exemplo n.º 13
0
 def spectral_plot_scipy() -> None:
     """
     :rtype: None
     """
     with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                   "Writers") as writer:
         writer.spectrogram(
             "STFT_Scipy",
             SIGNAL,
             int(1 / DELTA),
             step=0,
             n_fft=NFFT,
             step_size=STEP_SIZE,
         )
Exemplo n.º 14
0
    def stest_cma_es_cartpole():
        """Test CMAES with Cartpole-v1 environment."""
        with LocalTFRunner(snapshot_config) as runner:
            with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log /
                                          "CMA") as writer:
                env = GarageEnv(env_name="CartPole-v1")

                algo = CovarianceMatrixAdaptationEvolutionStrategyAgent(
                    env_spec=env.spec, max_rollout_length=100)
                algo.build()

                runner.setup(algo, env, sampler_cls=LocalSampler)
                runner.train(n_epochs=1, batch_size=1000)

                env.close()
Exemplo n.º 15
0
    def main(*, EPOCHS=1000, training=False, continue_t=True):

        if continue_t:
            try:
                _list_of_files = list(
                    BASE_PATH.rglob(f"*{MODEL_CHECKPOINT_SUFFIX}"))
                latest_model_path = str(
                    max(_list_of_files, key=os.path.getctime))
                print(f"loading previous model: {latest_model_path}")
                if latest_model_path is not None:
                    MODEL.load_state_dict(torch.load(latest_model_path))
            except:
                pass

        dataset_loader = DataLoader(DATASET,
                                    batch_size=BATCH_SIZE,
                                    shuffle=True,
                                    **DL_KWARGS)

        optimiser = optim.Adam(MODEL.parameters(), lr=LR, betas=(0.9, 0.999))

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / name /
                                      f"{time.time()}") as metric_writer:
            for ith_epoch in range(1, EPOCHS + 1):
                if training:
                    train_model(MODEL, optimiser, ith_epoch, metric_writer,
                                dataset_loader)

                if max_frequency("test_model", 100):
                    stest_model(MODEL, ith_epoch, metric_writer,
                                dataset_loader)

                if max_frequency("sample_i", 100):
                    with torch.no_grad():
                        inv_sample = inverse_transform(MODEL.sample().view(
                            CHANNELS, INPUT_SIZE, INPUT_SIZE))
                        inv_sample.save(
                            str(BASE_PATH / f"sample_{str(ith_epoch)}.png"))
                        if ENCODING_SIZE == 2:
                            from neodroidvision.utilities import plot_manifold

                            plot_manifold(
                                MODEL.decoder,
                                out_path=BASE_PATH /
                                f"manifold_{str(ith_epoch)}.png",
                                img_w=INPUT_SIZE,
                                img_h=INPUT_SIZE,
                            )
Exemplo n.º 16
0
    def a():
        """ """
        from draugr.torch_utilities import TensorBoardPytorchWriter

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                      "Writers") as writer:
            input_f = 4
            n_classes = 10

            model = torch.nn.Sequential(
                torch.nn.Linear(input_f, 20),
                torch.nn.ReLU(),
                torch.nn.Linear(20, n_classes),
                torch.nn.LogSoftmax(-1),
            )
            weight_bias_histograms(writer, model)
Exemplo n.º 17
0
    def baa():
        """ """
        from draugr.torch_utilities import TensorBoardPytorchWriter

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                      "Writers") as writer:
            input_f = 4
            n_classes = 10

            model = torch.nn.Sequential(
                torch.nn.Linear(input_f, 20),
                torch.nn.ReLU(),
                torch.nn.Linear(20, n_classes),
                torch.nn.LogSoftmax(-1),
            )
            for id in range(2):
                for i in range(3):
                    writer.parameters(model, i, tag=f"m{id}")
Exemplo n.º 18
0
    def main() -> None:
        """
        :rtype: None
        """
        import gym

        env = gym.make("Pendulum-v0")
        state = env.reset()

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                      "Writers") as writer:
            frames = []
            done = False

            start = time.time()
            while not done:
                frames.append(env.render("rgb_array"))
                state, reward, done, info = env.step(env.action_space.sample())
            fps = len(frames) / (time.time() - start)

            env.close()
            video_array = numpy.array(frames)
            writer.video(
                "replay05",
                nhwc_to_nchw_tensor(to_tensor(video_array)).unsqueeze(0),
                frame_rate=fps,
            )
            writer.video(
                "replay06",
                video_array,
                0,
                input_dims=VideoInputDimsEnum.thwc,
                frame_rate=fps,
            )
            writer.video(
                "replay08",
                numpy.stack([video_array, video_array]),
                0,
                input_dims=VideoInputDimsEnum.nthwc,
                frame_rate=fps,
            )
Exemplo n.º 19
0
    def main():
        """
        TODO:!!!!"""

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "Tests" /
                                      "Writers") as writer:
            vertices_tensor = torch.as_tensor(
                [
                    [1, 1, 1],
                    [-1, -1, 1],
                    [1, -1, -1],
                    [-1, 1, -1],
                ],
                dtype=torch.float,
            ).unsqueeze(0)
            colors_tensor = torch.as_tensor(
                [
                    [255, 0, 0],
                    [0, 255, 0],
                    [0, 0, 255],
                    [255, 0, 255],
                ],
                dtype=torch.int,
            ).unsqueeze(0)
            faces_tensor = torch.as_tensor(
                [
                    [0, 2, 3],
                    [0, 3, 1],
                    [0, 1, 2],
                    [1, 3, 2],
                ],
                dtype=torch.int,
            ).unsqueeze(0)

            writer.mesh("my_mesh",
                        vertices_tensor,
                        colors=colors_tensor,
                        faces=faces_tensor)
Exemplo n.º 20
0
    def main():
        """
ds = [datasets.MNIST(PROJECT_APP_PATH.user_data,
             train=True,
             download=True,
             transform=transforms.ToTensor()), datasets.MNIST(PROJECT_APP_PATH.user_data,
                                                              train=False,
                                                              transform=transforms.ToTensor())]
                                                              """

        dataset_loader = DataLoader(DATASET,
                                    batch_size=BATCH_SIZE,
                                    shuffle=True,
                                    **DL_KWARGS)

        optimiser = optim.Adam(MODEL.parameters(), lr=LR, betas=(0.9, 0.999))

        with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log / "VggFace2" /
                                      "BetaVAE" /
                                      f"{time.time()}") as metric_writer:
            for epoch in range(1, EPOCHS + 1):
                train_model(MODEL, optimiser, epoch, metric_writer,
                            dataset_loader)
                test_model(MODEL, epoch, metric_writer, dataset_loader)
                with torch.no_grad():
                    a = MODEL.sample().view(CHANNELS, INPUT_SIZE, INPUT_SIZE)
                    A = DATASET.inverse_transform(a)
                    A.save(str(BASE_PATH / f"sample_{str(epoch)}.png"))
                    if ENCODING_SIZE == 2:
                        plot_manifold(
                            MODEL,
                            out_path=str(BASE_PATH /
                                         f"manifold_{str(epoch)}.png"),
                            img_w=INPUT_SIZE,
                            img_h=INPUT_SIZE,
                        )
Exemplo n.º 21
0
def main_worker(gpu, ngpus_per_node, config):
    global CONFIG, best_acc1
    CONFIG, best_acc1 = config, 0
    train_set = config.dataset_type(CONFIG.dataset_path, Split.Training)
    val_set = config.dataset_type(CONFIG.dataset_path, Split.Validation)

    if CONFIG.distributed:
        if CONFIG.dist_url == "env://" and CONFIG.rank == -1:
            CONFIG.rank = int(os.environ["RANK"])
        if CONFIG.multiprocessing_distributed:
            CONFIG.rank = CONFIG.rank * ngpus_per_node + gpu
        distributed.init_process_group(
            backend=CONFIG.dist_backend,
            init_method=CONFIG.dist_url,
            world_size=CONFIG.world_size,
            rank=CONFIG.rank,
        )

    model = make_san(
        self_attention_type=SelfAttentionTypeEnum(CONFIG.self_attention_type),
        layers=CONFIG.layers,
        kernels=CONFIG.kernels,
        num_classes=train_set.response_shape[0],
    )
    criterion = nn.CrossEntropyLoss(ignore_index=CONFIG.ignore_label)
    optimizer = torch.optim.SGD(
        model.parameters(),
        lr=CONFIG.base_lr,
        momentum=CONFIG.momentum,
        weight_decay=CONFIG.weight_decay,
    )
    if CONFIG.scheduler == "step":
        scheduler = lr_scheduler.MultiStepLR(optimizer,
                                             milestones=CONFIG.step_epochs,
                                             gamma=0.1)
    elif CONFIG.scheduler == "cosine":
        scheduler = lr_scheduler.CosineAnnealingLR(optimizer,
                                                   T_max=CONFIG.epochs)

    if is_main_process():
        global logger, writer
        logger = get_logger()
        writer = TensorBoardPytorchWriter(str(CONFIG.save_path))
        logger.info(CONFIG)
        logger.info("=> creating model ...")
        logger.info(f"Classes: {train_set.response_shape[0]}")
        logger.info(model)
    if CONFIG.distributed:
        torch.cuda.set_device(gpu)
        CONFIG.batch_size = int(CONFIG.batch_size / ngpus_per_node)
        CONFIG.batch_size_val = int(CONFIG.batch_size_val / ngpus_per_node)
        CONFIG.workers = int(
            (CONFIG.workers + ngpus_per_node - 1) / ngpus_per_node)
        model = torch.nn.parallel.DistributedDataParallel(model.cuda(),
                                                          device_ids=[gpu])
    else:
        model = torch.nn.DataParallel(model.cuda())

    if CONFIG.weight:
        if os.path.isfile(CONFIG.weight):
            if is_main_process():
                global logger
                logger.info(f"=> loading weight '{CONFIG.weight}'")
            checkpoint = torch.load(CONFIG.weight)
            model.load_state_dict(checkpoint["state_dict"])
            if is_main_process():
                global logger
                logger.info(f"=> loaded weight '{CONFIG.weight}'")
        else:
            if is_main_process():
                global logger
                logger.info(f"=> no weight found at '{CONFIG.weight}'")

    if CONFIG.resume:
        if os.path.isfile(CONFIG.resume):
            if is_main_process():
                global logger
                logger.info(f"=> loading checkpoint '{CONFIG.resume}'")
            checkpoint = torch.load(
                CONFIG.resume,
                map_location=lambda storage, loc: storage.cuda(gpu))
            CONFIG.start_epoch = checkpoint["epoch"]
            best_acc1 = checkpoint["top1_val"]
            model.load_state_dict(checkpoint["state_dict"])
            optimizer.load_state_dict(checkpoint["optimizer"])
            scheduler.load_state_dict(checkpoint["scheduler"])
            if is_main_process():
                global logger
                logger.info(
                    f"=> loaded checkpoint '{CONFIG.resume}' (epoch {checkpoint['epoch']})"
                )
        else:
            if is_main_process():
                global logger
                logger.info(f"=> no checkpoint found at '{CONFIG.resume}'")

    if CONFIG.distributed:
        train_sampler = torch.utils.data.distributed.DistributedSampler(
            train_set)
        val_sampler = torch.utils.data.distributed.DistributedSampler(val_set)
    else:
        train_sampler = None
        val_sampler = None

    train_loader = torch.utils.data.DataLoader(
        train_set,
        batch_size=CONFIG.batch_size,
        shuffle=(train_sampler is None),
        num_workers=CONFIG.workers,
        pin_memory=True,
        sampler=train_sampler,
    )
    val_loader = torch.utils.data.DataLoader(
        val_set,
        batch_size=CONFIG.batch_size_val,
        shuffle=False,
        num_workers=CONFIG.workers,
        pin_memory=True,
        sampler=val_sampler,
    )

    for epoch in range(CONFIG.start_epoch, CONFIG.epochs):
        if CONFIG.distributed:
            train_sampler.set_epoch(epoch)
        (
            loss_train,
            mIoU_train,
            mAcc_train,
            allAcc_train,
            top1_train,
            top5_train,
        ) = train(train_loader, model, criterion, optimizer, epoch)
        loss_val, mIoU_val, mAcc_val, allAcc_val, top1_val, top5_val = validate(
            val_loader, model, criterion)
        scheduler.step()
        epoch_log = epoch + 1
        if is_main_process():
            global writer
            writer.scalar("loss_train", loss_train, epoch_log)
            writer.scalar("mIoU_train", mIoU_train, epoch_log)
            writer.scalar("mAcc_train", mAcc_train, epoch_log)
            writer.scalar("allAcc_train", allAcc_train, epoch_log)
            writer.scalar("top1_train", top1_train, epoch_log)
            writer.scalar("top5_train", top5_train, epoch_log)
            writer.scalar("loss_val", loss_val, epoch_log)
            writer.scalar("mIoU_val", mIoU_val, epoch_log)
            writer.scalar("mAcc_val", mAcc_val, epoch_log)
            writer.scalar("allAcc_val", allAcc_val, epoch_log)
            writer.scalar("top1_val", top1_val, epoch_log)
            writer.scalar("top5_val", top5_val, epoch_log)

        if (epoch_log % CONFIG.save_freq == 0) and is_main_process():
            filename = CONFIG.save_path / "train_epoch_" + str(
                epoch_log) + ".pth"
            global logger
            logger.info("Saving checkpoint to: " + filename)
            torch.save(
                {
                    "epoch": epoch_log,
                    "state_dict": model.state_dict(),
                    "optimizer": optimizer.state_dict(),
                    "scheduler": scheduler.state_dict(),
                    "top1_val": top1_val,
                    "top5_val": top5_val,
                },
                filename,
            )
            if top1_val > best_acc1:
                best_acc1 = top1_val
                shutil.copyfile(filename, CONFIG.save_path / "model_best.pth")
            if epoch_log / CONFIG.save_freq > 2:
                deletename = (
                    CONFIG.save_path /
                    f"train_epoch_{str(epoch_log - CONFIG.save_freq * 2)}.pth")
                os.remove(deletename)
Exemplo n.º 22
0
def main():
    args = argparse.ArgumentParser()
    args.add_argument("--inference", "-i", action="store_true")
    args.add_argument("--continue_training", "-c", action="store_true")
    args.add_argument("--real_data", "-r", action="store_true")
    args.add_argument("--no_cuda", "-k", action="store_false")
    args.add_argument("--export", "-e", action="store_true")
    options = args.parse_args()

    train_model = True
    timeas = str(time.time())
    this_model_path = PROJECT_APP_PATH.user_data / timeas
    this_log = PROJECT_APP_PATH.user_log / timeas
    ensure_directory_exist(this_model_path)
    ensure_directory_exist(this_log)

    best_model_name = "best_validation_model.model"
    interrupted_path = str(this_model_path / best_model_name)

    torch.manual_seed(seed)

    if not options.no_cuda:
        global_torch_device("cpu")

    dataset = MNISTDataset2(PROJECT_APP_PATH.user_cache / "mnist", split=Split.Training)
    train_iter = iter(
        recycle(
            DataLoader(dataset, batch_size=batch_size, shuffle=True, pin_memory=True)
        )
    )

    val_iter = iter(
        recycle(
            DataLoader(
                MNISTDataset2(
                    PROJECT_APP_PATH.user_cache / "mnist", split=Split.Validation
                ),
                batch_size=batch_size,
                shuffle=True,
                pin_memory=True,
            )
        )
    )

    model, params_to_update = squeezenet_retrain(len(dataset.categories))
    print(params_to_update)
    model = model.to(global_torch_device())

    if options.continue_training:
        _list_of_files = list(PROJECT_APP_PATH.user_data.rglob("*.model"))
        latest_model_path = str(max(_list_of_files, key=os.path.getctime))
        print(f"loading previous model: {latest_model_path}")
        if latest_model_path is not None:
            model.load_state_dict(torch.load(latest_model_path))

    criterion = torch.nn.CrossEntropyLoss().to(global_torch_device())

    optimizer_ft = optim.SGD(
        model.parameters(), lr=learning_rate, momentum=momentum, weight_decay=wd
    )
    exp_lr_scheduler = torch.optim.lr_scheduler.StepLR(
        optimizer_ft, step_size=7, gamma=0.1
    )

    writer = TensorBoardPytorchWriter(this_log)

    if train_model:
        model = predictor_response_train_model(
            model,
            train_iterator=train_iter,
            criterion=criterion,
            optimizer=optimizer_ft,
            scheduler=exp_lr_scheduler,
            writer=writer,
            interrupted_path=interrupted_path,
            val_data_iterator=val_iter,
            num_updates=NUM_UPDATES,
        )

    inputs, true_label = next(train_iter)
    inputs = to_tensor(inputs, dtype=torch.float, device=global_torch_device()).repeat(
        1, 3, 1, 1
    )
    true_label = to_tensor(true_label, dtype=torch.long, device=global_torch_device())

    pred = model(inputs)
    predicted = torch.argmax(pred, -1)
    true_label = to_tensor(true_label, dtype=torch.long)
    print(predicted, true_label)
    horizontal_imshow(
        inputs, [f"p:{int(p)},t:{int(t)}" for p, t in zip(predicted, true_label)]
    )
    pyplot.show()

    writer.close()
    torch_clean_up()
Exemplo n.º 23
0
def main():
    """ """
    mnist_l = torchvision.datasets.MNIST(
        PROJECT_APP_PATH.user_cache / "data",
        train=True,
        download=True,
        transform=torchvision.transforms.ToTensor(),
        # target_transform=to_one_hot
    )
    mnist = torch.utils.data.DataLoader(mnist_l,
                                        batch_size=len(mnist_l.data),
                                        shuffle=True)

    z_dim = 10
    X_dim = 28 * 28
    h_dim = 128
    cnt = 0
    lr = 1e-4
    n_critics = 3
    lam1, lam2 = 100, 100
    num_samples = 4

    Generator1 = torch.nn.Sequential(
        torch.nn.Linear(X_dim + z_dim, h_dim),
        torch.nn.ReLU(),
        torch.nn.Linear(h_dim, X_dim),
        torch.nn.Sigmoid(),
    )

    Generator2 = torch.nn.Sequential(
        torch.nn.Linear(X_dim + z_dim, h_dim),
        torch.nn.ReLU(),
        torch.nn.Linear(h_dim, X_dim),
        torch.nn.Sigmoid(),
    )

    Discriminator1 = torch.nn.Sequential(torch.nn.Linear(X_dim, h_dim),
                                         torch.nn.ReLU(),
                                         torch.nn.Linear(h_dim, 1))

    Discriminator2 = torch.nn.Sequential(torch.nn.Linear(X_dim, h_dim),
                                         torch.nn.ReLU(),
                                         torch.nn.Linear(h_dim, 1))

    Generators_solver = optim.RMSprop(chain(Generator1.parameters(),
                                            Generator2.parameters()),
                                      lr=lr)
    Discriminator1_solver = optim.RMSprop(Discriminator1.parameters(), lr=lr)
    Discriminator2_solver = optim.RMSprop(Discriminator2.parameters(), lr=lr)

    X_train = next(iter(mnist))[0]
    half = int(X_train.shape[0] / 2)

    X_train1 = X_train[:half]

    X_train2: torch.Tensor = X_train[half:]
    X_train2 = X_train2.rot90(dims=(2, 3))

    X_train1 = X_train1.reshape(-1, 28 * 28)
    X_train2 = X_train2.reshape(-1, 28 * 28)

    del X_train  # Cleanup

    with TensorBoardPytorchWriter(PROJECT_APP_PATH.user_log /
                                  str(time.time())) as writer:

        for it in range(1000000):
            for _ in range(n_critics):
                # Sample data
                z1 = Variable(torch.randn(BATCH_SIZE, z_dim))
                z2 = Variable(torch.randn(BATCH_SIZE, z_dim))
                X1 = sample_x(X_train1, BATCH_SIZE)
                X2 = sample_x(X_train2, BATCH_SIZE)

                # D1
                X2_sample = Generator1(torch.cat([X1, z1], -1))  # G1: X1 -> X2
                D1_real = Discriminator1(X2)
                D1_fake = Discriminator1(X2_sample)

                D1_loss = -(torch.mean(D1_real) - torch.mean(D1_fake))

                D1_loss.backward(retain_graph=True)
                Discriminator1_solver.step()

                for p in Discriminator1.parameters():  # Weight clipping
                    p.data.clamp_(-0.01, 0.01)

                reset_grads(
                    [Generator1, Generator2, Discriminator1, Discriminator2])

                # D2
                X1_sample = Generator2(torch.cat([X2, z2], -1))  # G2: X2 -> X1
                D2_real = Discriminator2(X1)
                D2_fake = Discriminator2(X1_sample)

                D2_loss = -(torch.mean(D2_real) - torch.mean(D2_fake))

                D2_loss.backward()
                Discriminator2_solver.step()

                for p in Discriminator2.parameters():  # Weight clipping
                    p.data.clamp_(-0.01, 0.01)

                reset_grads(
                    [Generator1, Generator2, Discriminator1, Discriminator2])

            # Generator
            z1 = Variable(torch.randn(BATCH_SIZE, z_dim))
            z2 = Variable(torch.randn(BATCH_SIZE, z_dim))
            X1 = sample_x(X_train1, BATCH_SIZE)
            X2 = sample_x(X_train2, BATCH_SIZE)

            X1_sample = Generator2(torch.cat([X2, z2], 1))
            X2_sample = Generator1(torch.cat([X1, z1], 1))

            X1_recon = Generator2(torch.cat([X2_sample, z2], 1))
            X2_recon = Generator1(torch.cat([X1_sample, z1], 1))

            D1_fake = Discriminator1(X1_sample)
            D2_fake = Discriminator2(X2_sample)

            G_loss = -torch.mean(D1_fake) - torch.mean(D2_fake)
            reg1 = lam1 * torch.mean(torch.sum(torch.abs(X1_recon - X1), 1))
            reg2 = lam2 * torch.mean(torch.sum(torch.abs(X2_recon - X2), 1))

            G_loss += reg1 + reg2

            G_loss.backward()
            Generators_solver.step()
            reset_grads(
                [Generator1, Generator2, Discriminator1, Discriminator2])

            if it % 1000 == 0:
                print(f"Iter-{it};"
                      f" D_loss: {D1_loss.item() + D2_loss.item():.4};"
                      f" G_loss: {G_loss.item():.4}")

                real1: torch.Tensor = X1.data[:num_samples]
                real2: torch.Tensor = X2.data[:num_samples]
                samples1: torch.Tensor = X1_sample.data[:num_samples]
                samples2: torch.Tensor = X2_sample.data[:num_samples]
                real1 = real1.view(-1, 1, 28, 28)
                real2 = real2.view(-1, 1, 28, 28)
                samples1 = samples1.view(-1, 1, 28, 28)
                samples2 = samples2.view(-1, 1, 28, 28)
                real_generate_samples = torch.cat(
                    [real2, samples1, real1, samples2])

                grid = torchvision.utils.make_grid(real_generate_samples,
                                                   nrow=4).unsqueeze(0)

                writer.image(f"Samples", data=grid, step=it, cmap="Greys_r")

                cnt += 1
Exemplo n.º 24
0
def main():
    """ """
    args = argparse.ArgumentParser()
    args.add_argument("--inference", "-i", action="store_true")
    args.add_argument("--continue_training", "-c", action="store_true")
    args.add_argument("--no_cuda", "-k", action="store_false")
    args.add_argument("--export", "-e", action="store_true")
    options = args.parse_args()

    timeas = str(time.time())
    this_model_path = PROJECT_APP_PATH.user_data / timeas
    this_log = PROJECT_APP_PATH.user_log / timeas
    ensure_directory_exist(this_model_path)
    ensure_directory_exist(this_log)

    best_model_name = "best_validation_model.model"
    interrupted_path = str(this_model_path / best_model_name)

    torch.manual_seed(seed)

    if not options.no_cuda:
        global_torch_device("cpu")

    with MixedObservationWrapper() as env:
        env.seed(seed)
        train_iter = batch_generator(iter(env), batch_size)
        num_categories = env.sensor("Class").space.discrete_steps
        test_iter = train_iter

        model, params_to_update = squeezenet_retrain(num_categories)
        print(params_to_update)

        model = model.to(global_torch_device())

        if options.continue_training:
            _list_of_files = list(PROJECT_APP_PATH.user_data.rglob("*.model"))
            latest_model_path = str(max(_list_of_files, key=os.path.getctime))
            print(f"loading previous model: {latest_model_path}")
            if latest_model_path is not None:
                model.load_state_dict(torch.load(latest_model_path))

        criterion = torch.nn.CrossEntropyLoss().to(global_torch_device())

        optimiser_ft = optim.SGD(model.parameters(),
                                 lr=learning_rate,
                                 momentum=momentum,
                                 weight_decay=wd)
        exp_lr_scheduler = torch.optim.lr_scheduler.StepLR(optimiser_ft,
                                                           step_size=7,
                                                           gamma=0.1)

        with TensorBoardPytorchWriter(this_log) as writer:
            if True:
                model = pred_target_train_model(
                    model,
                    train_iter,
                    criterion,
                    optimiser_ft,
                    exp_lr_scheduler,
                    writer,
                    interrupted_path,
                    test_data_iterator=test_iter,
                    num_updates=num_updates,
                )

            inputs, true_label = zip(*next(train_iter))
            rgb_imgs = torch_vision_normalize_batch_nchw(
                uint_nhwc_to_nchw_float_batch(
                    rgb_drop_alpha_batch_nhwc(to_tensor(inputs))))

            predicted = torch.argmax(model(rgb_imgs), -1)
            true_label = to_tensor(true_label, dtype=torch.long)
            print(predicted, true_label)
            horizontal_imshow(
                inputs,
                [
                    f"p:{int(p)},t:{int(t)}"
                    for p, t in zip(predicted, true_label)
                ],
            )
            pyplot.show()

    torch.cuda.empty_cache()

    if options.export:
        with TorchEvalSession(model):
            example = torch.rand(1, 3, 256, 256)
            traced_script_module = torch.jit.trace(model.to("cpu"), example)
            traced_script_module.save("resnet18_v.model")
Exemplo n.º 25
0
def main(train_model=True, continue_training=True, no_cuda=False):
    """ """

    timeas = str(time.time())
    this_model_path = PROJECT_APP_PATH.user_data / timeas
    this_log = PROJECT_APP_PATH.user_log / timeas
    ensure_directory_exist(this_model_path)
    ensure_directory_exist(this_log)

    best_model_name = "best_validation_model.model"
    interrupted_path = str(this_model_path / best_model_name)

    torch.manual_seed(seed)

    if no_cuda:
        global_torch_device("cpu")

    dataset = MNISTDataset2(PROJECT_APP_PATH.user_cache / "mnist",
                            split=SplitEnum.training)
    train_iter = iter(
        recycle(
            DataLoader(dataset,
                       batch_size=batch_size,
                       shuffle=True,
                       pin_memory=True)))

    val_iter = iter(
        recycle(
            DataLoader(
                MNISTDataset2(PROJECT_APP_PATH.user_cache / "mnist",
                              split=SplitEnum.validation),
                batch_size=batch_size,
                shuffle=True,
                pin_memory=global_pin_memory(0),
            )))

    model, params_to_update = squeezenet_retrain(len(dataset.categories),
                                                 train_only_last_layer=True)
    # print(params_to_update)
    model = model.to(global_torch_device())

    if continue_training:
        _list_of_files = list(PROJECT_APP_PATH.user_data.rglob("*.model"))
        latest_model_path = str(max(_list_of_files, key=os.path.getctime))
        print(f"loading previous model: {latest_model_path}")
        if latest_model_path is not None:
            model.load_state_dict(torch.load(latest_model_path))

    criterion = torch.nn.CrossEntropyLoss().to(global_torch_device())

    optimiser_ft = optim.SGD(model.parameters(),
                             lr=learning_rate,
                             momentum=momentum,
                             weight_decay=wd)
    exp_lr_scheduler = torch.optim.lr_scheduler.StepLR(optimiser_ft,
                                                       step_size=7,
                                                       gamma=0.1)

    with TensorBoardPytorchWriter(this_log) as writer:
        if train_model:
            model = predictor_response_train_model(
                model,
                train_iterator=train_iter,
                criterion=criterion,
                optimiser=optimiser_ft,
                scheduler=exp_lr_scheduler,
                writer=writer,
                interrupted_path=interrupted_path,
                val_data_iterator=val_iter,
                num_updates=NUM_UPDATES,
            )

        inputs, true_label = next(val_iter)
        inputs = to_tensor(inputs,
                           dtype=torch.float,
                           device=global_torch_device()).repeat(1, 3, 1, 1)
        true_label = to_tensor(true_label,
                               dtype=torch.long,
                               device=global_torch_device())

        pred = model(inputs)
        predicted = torch.argmax(pred, -1)
        true_label = to_tensor(true_label, dtype=torch.long)
        # print(predicted, true_label)
        horizontal_imshow(
            [to_pil_image(i) for i in inputs],
            [f"p:{int(p)},t:{int(t)}" for p, t in zip(predicted, true_label)],
            num_columns=64 // 8,
        )
        pyplot.show()

    torch_clean_up()
Exemplo n.º 26
0
    def main():
        dataset_root = Path.home() / "Data"
        base_path = ensure_existence(PROJECT_APP_PATH.user_data / 'maskrcnn')
        log_path = ensure_existence(PROJECT_APP_PATH.user_log / 'maskrcnn')
        export_root = ensure_existence(base_path / 'models')
        model_name = f'maskrcnn_pennfudanped'

        batch_size = 4
        num_epochs = 10
        optimiser_spec = GDKC(torch.optim.Adam, lr=3e-4)
        scheduler_spec = GDKC(
            torch.optim.lr_scheduler.
            StepLR,  # a learning rate scheduler which decreases the learning rate by
            step_size=3,  # 10x every 3 epochs
            gamma=0.1,
        )
        num_workers = os.cpu_count()
        torch_seed(3825)

        dataset = PennFudanDataset(dataset_root / "PennFudanPed",
                                   Split.Training,
                                   return_variant=ReturnVariant.all)
        dataset_validation = PennFudanDataset(
            dataset_root / "PennFudanPed",
            Split.Validation,
            return_variant=ReturnVariant.all,
        )
        split = SplitIndexer(len(dataset), validation=0.3, testing=0)

        split_indices = torch.randperm(split.total_num).tolist()

        data_loader = DataLoader(
            Subset(dataset, split_indices[:-split.validation_num]),
            batch_size=batch_size,
            shuffle=True,
            num_workers=num_workers,
            collate_fn=collate_batch_fn,
        )

        data_loader_val = DataLoader(
            Subset(dataset_validation, split_indices[-split.validation_num:]),
            batch_size=1,
            shuffle=False,
            num_workers=num_workers,
            collate_fn=collate_batch_fn,
        )

        model = get_pretrained_instance_segmentation_maskrcnn(
            dataset.response_channels)
        optimiser = optimiser_spec(trainable_parameters(model))
        lr_scheduler = scheduler_spec(optimiser)

        if True:
            model = load_model(model_name=model_name,
                               model_directory=export_root)

        if True:
            with TorchTrainSession(model):
                with TensorBoardPytorchWriter(log_path / model_name) as writer:
                    for epoch_i in tqdm(range(num_epochs), desc="Epoch #"):
                        maskrcnn_train_single_epoch(model=model,
                                                    optimiser=optimiser,
                                                    data_loader=data_loader,
                                                    writer=writer)
                        lr_scheduler.step()  # update the learning rate
                        maskrcnn_evaluate(
                            model, data_loader_val, writer=writer
                        )  # evaluate on the validation dataset
                        save_model(model,
                                   model_name=model_name,
                                   save_directory=export_root)

        if True:
            with TorchEvalSession(model):  # put the model in evaluation mode
                img, _ = dataset_validation[
                    0]  # pick one image from the test set

                with torch.no_grad():
                    prediction = model([img.to(global_torch_device())])

                from matplotlib import pyplot
                pyplot.imshow(
                    Image.fromarray(
                        img.mul(255).permute(1, 2, 0).byte().numpy()))
                pyplot.show()

                import cv2

                pyplot.imshow(
                    Image.fromarray(prediction[0]["masks"][0, 0].mul(
                        255).byte().cpu().numpy()))
                pyplot.show()

                (boxes, labels, scores) = (
                    prediction[0]["boxes"].to('cpu').numpy(),
                    prediction[0]["labels"].to('cpu').numpy(),
                    torch.sigmoid(prediction[0]["scores"]).to('cpu').numpy(),
                )

                from draugr.opencv_utilities import draw_bounding_boxes
                from draugr.torch_utilities.images.conversion import quick_to_pil_image

                indices = scores > 0.1

                cv2.namedWindow(model_name, cv2.WINDOW_NORMAL)
                cv2.imshow(
                    model_name,
                    draw_bounding_boxes(
                        quick_to_pil_image(img),
                        boxes[indices],
                        labels=labels[indices],
                        scores=scores[indices],
                        #categories=categories,
                    ))

                cv2.waitKey()
Exemplo n.º 27
0
def train_mnist(load_earlier=False, train=True, denoise: bool = True):
    """

    :param load_earlier:
    :type load_earlier:
    :param train:
    :type train:"""
    seed = 251645
    batch_size = 32

    tqdm.monitor_interval = 0
    learning_rate = 3e-3
    lr_sch_step_size = int(10e4 // batch_size)
    lr_sch_gamma = 0.1
    unet_depth = 3
    unet_start_channels = 16
    input_channels = 1
    output_channels = (input_channels,)

    home_path = PROJECT_APP_PATH
    model_file_ending = ".model"
    model_base_path = ensure_existence(PROJECT_APP_PATH.user_data / "unet_mnist")
    interrupted_name = "INTERRUPTED_BEST"
    interrupted_path = model_base_path / f"{interrupted_name}{model_file_ending}"

    torch.manual_seed(seed)

    device = global_torch_device()

    img_transform = transforms.Compose(
        [
            transforms.ToTensor(),
            MinMaxNorm(),
            transforms.Lambda(lambda tensor: torch.round(tensor)),
            # transforms.RandomErasing()
        ]
    )
    dataset = MNIST(
        PROJECT_APP_PATH.user_data / "mnist", transform=img_transform, download=True
    )
    data_iter = iter(
        cycle(DataLoader(dataset, batch_size=batch_size, shuffle=True, pin_memory=True))
    )
    data_iter = to_device_iterator(data_iter, device)

    model = SkipHourglassFission(
        input_channels=input_channels,
        output_heads=output_channels,
        encoding_depth=unet_depth,
        start_channels=unet_start_channels,
    ).to(global_torch_device())

    optimiser_ft = optim.Adam(model.parameters(), lr=learning_rate)

    exp_lr_scheduler = optim.lr_scheduler.StepLR(
        optimiser_ft, step_size=lr_sch_step_size, gamma=lr_sch_gamma
    )

    if load_earlier:
        _list_of_files = list(
            model_base_path.rglob(f"{interrupted_name}{model_file_ending}")
        )
        if not len(_list_of_files):
            print(
                f"found no trained models under {model_base_path}{os.path.sep}**{os.path.sep}{interrupted_name}{model_file_ending}"
            )
            exit(1)
        latest_model_path = str(max(_list_of_files, key=os.path.getctime))
        print(f"loading previous model: {latest_model_path}")
        if latest_model_path is not None:
            model.load_state_dict(torch.load(latest_model_path))

    if train:
        with TensorBoardPytorchWriter(home_path.user_log / str(time.time())) as writer:
            model = training(
                model,
                data_iter,
                optimiser_ft,
                exp_lr_scheduler,
                writer,
                interrupted_path,
                denoise=denoise,
            )
            torch.save(
                model.state_dict(),
                model_base_path / f"unet_mnist_final{model_file_ending}",
            )
    else:
        inference(model, data_iter, denoise=denoise)

    torch.cuda.empty_cache()
Exemplo n.º 28
0
def main(
    agent: TorchAgent,
    environment: NeodroidCurriculumWrapper,
    *,
    log_directory: Union[str, Path],
    rollouts: int = 1000,
    render_frequency: int = 100,
    stat_frequency: int = 10,
    disable_stdout: bool = False,
    full_state_evaluation_frequency=20,
    **kwargs,
) -> TR:
    assert isinstance(environment, NeodroidCurriculumWrapper)

    with torch.autograd.detect_anomaly():
        with TensorBoardPytorchWriter(str(log_directory)) as metric_writer:
            _episode_i = 0
            _step_i = 0

            random_motion_length = C.RANDOM_MOTION_HORIZON
            training_start_timestamp = time.time()

            initial_configuration = get_initial_configuration_from_goal(
                environment)
            print("Generating initial state from goal configuration")
            S_prev = environment.generate_trajectory_from_configuration(
                initial_configuration,
                random_motion_length,
                random_process=_random_process,
            )

            train_session = range(1, rollouts + 1)
            train_session = tqdm(train_session, leave=False, disable=False)

            for i in train_session:
                if not environment.is_connected:
                    break

                S_initial = []
                S_candidate = []

                reached_dead_end = True

                if i % full_state_evaluation_frequency == 0:
                    print("Estimating entire state space")
                    estimate_entire_state_space(
                        environment,
                        agent,
                        C,
                        # statistics=None,
                        save_snapshot=save_snapshot,
                    )

                num_candidates = tqdm(range(1, C.CANDIDATE_SET_SIZE + 1),
                                      leave=False,
                                      disable=False)
                for c in num_candidates:
                    if _plot_stats:
                        # draugr.terminal_plot_stats_shared_x(stats, printer=train_session.write)
                        train_session.set_description(
                            f"Steps: {_step_i:9.0f}"
                            # f' | Ent: {stats.entropies.calc_moving_average():.2f}'
                        )
                        num_candidates.set_description(
                            f"Candidate #{c} of {C.CANDIDATE_SET_SIZE} | "
                            f"FP: {reached_dead_end} | "
                            f"L: {random_motion_length} | "
                            f"S_i: {len(S_initial)}")

                    seed = sample(S_prev)
                    S_candidate.extend(
                        environment.generate_trajectory_from_state(
                            seed,
                            random_motion_length,
                            random_process=_random_process))

                    candidate = sample(S_candidate)

                    est, _episode_i, _step_i = estimate_initial_state_expected_return(
                        candidate,
                        environment,
                        agent,
                        C,
                        save_snapshot=save_snapshot,
                        # statistics=stats,
                        train=True,
                    )

                    if C.LOW <= est <= C.HIGH:
                        S_initial.append(candidate)
                        random_motion_length = C.RANDOM_MOTION_HORIZON
                        reached_dead_end = False
                    elif _keep_seed_if_not_replaced:
                        S_initial.append(seed)

                display_actor_configurations(environment, S_candidate)

                if reached_dead_end:
                    print("Reached dead end")
                    print("Generating initial state from goal configuration")
                    S_initial = environment.generate_trajectory_from_configuration(
                        initial_configuration,
                        random_motion_length,
                        random_process=_random_process,
                    )
                    random_motion_length += 1

                S_prev = S_initial

            time_elapsed = time.time() - training_start_timestamp
            message = (
                f"Time elapsed: {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s"
            )
            print(f'\n{"-" * 9} {message} {"-" * 9}\n')

            agent.save(C)
            save_snapshot()
Exemplo n.º 29
0
def main():
    args = argparse.ArgumentParser()
    args.add_argument("-i", action="store_false")
    options = args.parse_args()

    seed = 42
    batch_size = 8  # 12
    depth = 4  # 5
    segmentation_channels = 3
    tqdm.monitor_interval = 0
    learning_rate = 3e-3
    lr_sch_step_size = int(1000 // batch_size) + 4
    lr_sch_gamma = 0.1
    model_start_channels = 16

    home_path = Path.home() / "Models" / "Vision"
    base_path = home_path / str(time.time())
    best_model_path = "INTERRUPTED_BEST.pth"
    interrupted_path = str(base_path / best_model_path)

    writer = TensorBoardPytorchWriter(str(base_path))
    env = CameraObservationWrapper()

    torch.manual_seed(seed)
    env.seed(seed)

    device = global_torch_device()

    aeu_model = SkipHourglassFission(
        segmentation_channels,
        (segmentation_channels,),
        encoding_depth=depth,
        start_channels=model_start_channels,
    )
    aeu_model = aeu_model.to(global_torch_device())

    optimizer_ft = optim.Adam(aeu_model.parameters(), lr=learning_rate)

    exp_lr_scheduler = lr_scheduler.StepLR(
        optimizer_ft, step_size=lr_sch_step_size, gamma=lr_sch_gamma
    )

    data_iter = iter(neodroid_camera_data_iterator(env, device, batch_size))

    if options.i:
        trained_aeu_model = train_model(
            aeu_model,
            data_iter,
            optimizer_ft,
            exp_lr_scheduler,
            writer,
            interrupted_path,
        )
        test_model(trained_aeu_model, data_iter)
    else:
        _list_of_files = list(home_path.glob("*"))
        latest_model_path = (
            str(max(_list_of_files, key=os.path.getctime)) + f"/{best_model_path}"
        )
        print("loading previous model: " + latest_model_path)
        test_model(aeu_model, data_iter, load_path=latest_model_path)

    torch.cuda.empty_cache()
    env.close()
    writer.close()
Exemplo n.º 30
0
def test_valid_scalars(tag, val, step):
    with TensorBoardPytorchWriter(path=PROJECT_APP_PATH.user_log) as w:
        w.scalar(tag, val, step)