Ejemplo n.º 1
0
    def __init__(self,
                 model: torch.nn.Module,
                 train_data: str,
                 val_data: str,
                 test_data: str,
                 batch_size: int,
                 num_classes: int = 2,
                 num_workers: int = 1):
        super().__init__()
        self.num_workers = num_workers
        self.train_data = train_data
        self.val_data = val_data
        self.test_data = test_data
        self.batch_size = batch_size

        self.model = model

        self.criterion = torch.nn.BCELoss()

        self.collate_fn = PaddingCollateFn(150)

        self.train_metrics = []
        self.val_metrics = [
            Accuracy(num_classes=num_classes),
            F1(num_classes=num_classes),
            Precision(num_classes=num_classes),
            Recall(num_classes=num_classes)
        ]
Ejemplo n.º 2
0
def test_recall(num_classes):
    recall = Recall(num_classes=num_classes)
    assert recall.name == 'recall'

    pred, target = torch.tensor([1, 2, 3, 4]), torch.tensor([1, 0, 0, 1])
    rec = recall(pred=pred, target=target)
    assert isinstance(rec, torch.Tensor)
Ejemplo n.º 3
0
def recall_score(labels, predictions):
    predictions = torch.Tensor(predictions)
    labels = torch.Tensor(labels)
    recall = Recall(num_classes=len(labels[0]),
                    average='macro',
                    multilabel=True,
                    threshold=cfg['training']['threshold'])
    return recall(predictions, labels).item()
Ejemplo n.º 4
0
 def __init__(self, backbone, feature_indices, feature_channels):
     super().__init__()
     self.model = get_segmentation_model(backbone, feature_indices,
                                         feature_channels)
     self.criterion = BCEWithLogitsLoss()
     self.prec = Precision(num_classes=1, threshold=0.5)
     self.rec = Recall(num_classes=1, threshold=0.5)
     self.f1 = F1(num_classes=1, threshold=0.5)
Ejemplo n.º 5
0
    def __init__(
        self,
        regression_task,
        classification_task,
        input_size,
        hidden_size,
        learning_rate,
        classifier_lambda,
        tanh_loss,
        fill_missing_regression,
        regressor_activation,
        classifier_loss_weights=None,
        **kwargs
    ):
        super().__init__()

        self.save_hyperparameters()

        # Sanity checks
        assert 0 <= classifier_lambda <= 1

        # Layers
        self.hidden_fc = nn.Linear(
            in_features=input_size, out_features=hidden_size, bias=True
        )
        
        if classification_task:
            self.classifier_hidden_fc = nn.Linear(
                in_features=hidden_size,
                out_features=hidden_size,
                bias=True
            )
            self.classifier_fc = nn.Linear(
                in_features=hidden_size,
                out_features=2,  # It's a binary classification
                bias=True,
            )
            self._build_classifier_loss_weigths(classifier_loss_weights)

        if regression_task:
            self.regressor_hidden_fc = nn.Linear(
                in_features=hidden_size,
                out_features=hidden_size,
                bias=True,
            )
            self.regressor_fc = nn.Linear(
                in_features=hidden_size, out_features=1, bias=True  # It's a regression
            )

        # Metrics
        if classification_task:
            self.f1 = F1()
            self.precision= Precision()
            self.recall = Recall()
        if regression_task:
            self.mse = MSE()
Ejemplo n.º 6
0
def class_wise_recall_scores(labels, predictions):
    predictions = torch.Tensor(predictions)
    labels = torch.Tensor(labels)
    recall = Recall(num_classes=len(labels[0]),
                    average='macro',
                    multilabel=True,
                    threshold=cfg['training']['threshold'])
    recall(predictions, labels)
    class_wise_scores = recall.true_positives.float() / (
        recall.actual_positives + METRIC_EPS)
    return class_wise_scores.tolist()
if __name__ == "__main__":

    # 1. Download the data
    download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", 'data/')

    # 2. Load the data
    datamodule = TabularData.from_csv(
        "./data/titanic/titanic.csv",
        test_csv="./data/titanic/test.csv",
        categorical_input=["Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"],
        numerical_input=["Fare"],
        target="Survived",
        val_size=0.25,
    )

    # 3. Build the model
    model = TabularClassifier.from_data(datamodule, metrics=[Accuracy(), Precision(), Recall()])

    # 4. Create the trainer. Run 10 times on data
    trainer = flash.Trainer(max_epochs=10)

    # 5. Train the model
    trainer.fit(model, datamodule=datamodule)

    # 6. Test model
    trainer.test()

    # 7. Save it!
    trainer.save_checkpoint("tabular_classification_model.pt")
Ejemplo n.º 8
0
download_data("https://pl-flash-data.s3.amazonaws.com/titanic.zip", 'data/')

# 2. Load the data
datamodule = TabularData.from_csv(
    "./data/titanic/titanic.csv",
    test_csv="./data/titanic/test.csv",
    categorical_input=[
        "Sex", "Age", "SibSp", "Parch", "Ticket", "Cabin", "Embarked"
    ],
    numerical_input=["Fare"],
    target="Survived",
    val_size=0.25,
)

# 3. Build the model
model = TabularClassifier.from_data(
    datamodule, metrics=[Accuracy(), Precision(),
                         Recall()])

# 4. Create the trainer. Run 10 times on data
trainer = flash.Trainer(max_epochs=10)

# 5. Train the model
trainer.fit(model, datamodule=datamodule)

# 6. Test model
trainer.test()

# 7. Save it!
trainer.save_checkpoint("tabular_classification_model.pt")
Ejemplo n.º 9
0

if __name__ == '__main__':

    with open("params.yaml", 'r') as fd:
        params = yaml.safe_load(fd)

    MODEL_PATH = params['test']['model_path']

    module = TrainingModule.load_from_checkpoint(MODEL_PATH).to('cuda')
    module.eval()

    acc = Accuracy()
    f1 = F1()
    precision = Precision()
    recall = Recall()

    mlflow.set_tracking_uri('file-plugin:/content/NLP_Emotions/mlruns')
    if get_closest_gittag() == 'v2.0':
        mlflow.set_experiment('SGD')
        mlflow.set_tag('Version', 'SGD')
        mlflow.set_tag('Stage', 'test')
        mlflow.set_tag('Commit', get_commit())
        mlflow.set_tag('Time', get_commit_time())
        mlflow.set_tag('Model', module.model_name)
        mlflow.log_params({
            'batch_size': module.hparams.batch_size,
            'epochs': module.hparams.epochs,
            'learning_rate': module.hparams.lr
        })
    else: