Esempio n. 1
0
def test_create_all_services(artifact_dir):
    cfg_services = {
        "best_checkpoint": {
            "monitor": "val_loss",
            "mode": "min"
        },
        "tensorboard": {},
        "train_early_stopping": {
            "monitor": "loss",
            "mode": "min",
            "patience": 2,
            "min_delta": 1e-2,
            "verbose": 0,
        },
        "validation_early_stopping": {
            "monitor": "val_loss",
            "mode": "min",
            "patience": 2,
            "min_delta": 1e-2,
        },
    }
    metrics_names = ["loss"]
    result = services.create_all_services(artifact_dir, cfg_services,
                                          metrics_names)
    assert isinstance(result[0], tf.keras.callbacks.ModelCheckpoint)
    assert isinstance(result[1], tf.keras.callbacks.ModelCheckpoint)
    assert isinstance(result[2], tf.keras.callbacks.TensorBoard)
    assert isinstance(result[3], tf.keras.callbacks.CSVLogger)
    assert isinstance(result[4], tf.keras.callbacks.EarlyStopping)
    assert isinstance(result[5], tf.keras.callbacks.EarlyStopping)
    assert isinstance(result[6], tf.keras.callbacks.TerminateOnNaN)
Esempio n. 2
0
    def train(
        self,
        cfg: dict,
        records_train: api.InputRecords,
        records_validation: api.InputRecords,
    ) -> tf.keras.Model:
        """Train the network.

        Args:
            cfg: dict, config.
            records_train: InputRecords, training records.
            records_validation: InputRecords, validation records.

        Returns:
            tf.keras.Model, trained network.
        """
        logger.info("Starting training")
        tf_utils.reset()
        cfg = config.prepare_config(cfg)

        logger.info(f"Creating artifact directory: {self.artifact_dir}")
        services.make_artifact_dir(self.artifact_dir)
        io_utils.save_json(cfg, "config.json", self.artifact_dir)
        io_utils.save_pickle(cfg, "config.pkl", self.artifact_dir)

        logger.info("Creating datasets")
        ds_train = dataset.RecordDataset(
            artifact_dir=self.artifact_dir,
            cfg_dataset=cfg["dataset"],
            records=records_train,
            mode=api.RecordMode.TRAIN,
            batch_size=cfg["solver"]["batch_size"],
        )
        ds_validation = dataset.RecordDataset(
            artifact_dir=self.artifact_dir,
            cfg_dataset=cfg["dataset"],
            records=records_validation,
            mode=api.RecordMode.VALIDATION,
            batch_size=cfg["solver"]["batch_size"],
        )
        network_params = ds_train.transformer.network_params
        io_utils.save_json(network_params, "network_params.json", self.artifact_dir)
        io_utils.save_pickle(network_params, "network_params.pkl", self.artifact_dir)

        logger.info("Building network")
        net = model.build_network(cfg["model"], network_params)
        model.check_output_names(cfg["model"], net)

        logger.info("Compiling network")
        opt = solver.build_optimizer(cfg["solver"])
        objective = model.build_objective(cfg["model"])
        net.compile(optimizer=opt, **objective)

        logger.info("Creating services")
        callbacks = services.create_all_services(self.artifact_dir, cfg["services"])

        if "learning_rate_reducer" in cfg["solver"]:
            logger.info("Creating learning rate reducer")
            callbacks.append(solver.create_learning_rate_reducer(cfg["solver"]))

        logger.info("Training network")
        net.summary()
        net.fit(
            ds_train,
            validation_data=ds_validation,
            epochs=cfg["solver"]["epochs"],
            steps_per_epoch=cfg["solver"].get("steps"),
            callbacks=callbacks,
            verbose=1,
        )

        return net
Esempio n. 3
0
    def train(
        self,
        cfg: dict,
        records_train: Union[pd.DataFrame, api.Records],
        records_validation: Union[pd.DataFrame, api.Records],
        workers: int = 10,
        max_queue_size: int = 10,
    ) -> tf.keras.Model:
        """Train the network.

        Args:
            cfg: dict, config.
            records_train: Union[pd.DataFrame, Records], training records.
            records_validation: Union[pd.DataFrame, Records], validation records.
            workers: int (OPTIONAL = 10), number of process threads for the sequence.
            max_queue_size: int (OPTIONAL = 10), queue size for the sequence.

        Returns:
            tf.keras.Model, trained network.
        """
        logger.info("Starting training")
        tf_utils.reset()

        logger.info("Validating config schema and applying defaults")
        cfg = config.prepare_config(cfg)

        logger.info(f"Making artifact directory: {self._artifact_dir}")
        services.make_artifact_dir(self._artifact_dir)

        logger.info("Saving config")
        io_utils.save_json(cfg, "config.json", self._artifact_dir)
        io_utils.save_pickle(cfg, "config.pkl", self._artifact_dir)

        logger.info("Building datasets")
        ds_train = dataset.RecordDataset(
            artifact_dir=self._artifact_dir,
            cfg_dataset=cfg["dataset"],
            records=records_train,
            mode=api.RecordMode.TRAIN,
            batch_size=cfg["solver"]["batch_size"],
        )
        ds_validation = dataset.RecordDataset(
            artifact_dir=self._artifact_dir,
            cfg_dataset=cfg["dataset"],
            records=records_validation,
            mode=api.RecordMode.VALIDATION,
            batch_size=cfg["solver"]["batch_size"],
        )
        network_params = ds_train.transformer.network_params
        io_utils.save_json(network_params, "network_params.json", self._artifact_dir)
        io_utils.save_pickle(network_params, "network_params.pkl", self._artifact_dir)

        logger.info("Building network")
        net = model.build_network(cfg["model"], network_params)

        logger.info("Checking network output names match config output names")
        model.check_output_names(cfg["model"], net)

        logger.info("Building optimizer")
        opt = solver.build_optimizer(cfg["solver"])

        logger.info("Building objective")
        objective = model.build_objective(cfg["model"])

        logger.info("Compiling network")
        net.compile(optimizer=opt, **objective)
        metrics_names = net.metrics_names

        logger.info("Creating services")
        callbacks = services.create_all_services(
            self._artifact_dir, cfg["services"], metrics_names
        )

        if "learning_rate_reducer" in cfg["solver"]:
            logger.info("Creating learning rate reducer")
            callbacks.append(
                solver.create_learning_rate_reducer(cfg["solver"], metrics_names)
            )

        logger.info("Training network")
        logger.info(net.summary())
        net.fit_generator(
            ds_train,
            validation_data=ds_validation,
            epochs=cfg["solver"]["epochs"],
            steps_per_epoch=cfg["solver"].get("steps"),
            callbacks=callbacks,
            use_multiprocessing=(workers > 1),
            max_queue_size=max_queue_size,
            workers=workers,
            verbose=1,
        )

        return net