diff --git a/project/pipeline_modules/callbacks.py b/project/pipeline_modules/callbacks.py index 8ddcc9c..e26271d 100644 --- a/project/pipeline_modules/callbacks.py +++ b/project/pipeline_modules/callbacks.py @@ -1,12 +1,12 @@ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score -from pytorch_lightning.callbacks import Callback +import pytorch_lightning as pl import seaborn import torch import wandb import os -class ExampleCallback(Callback): +class ExampleCallback(pl.Callback): def __init__(self): pass @@ -20,7 +20,7 @@ class ExampleCallback(Callback): print('Do something when training ends.') -class SaveModelOnnxCallback(Callback): +class SaveModelOnnxCallback(pl.Callback): """ Save model in .onnx format. """ @@ -42,7 +42,7 @@ class SaveModelOnnxCallback(Callback): wandb.save(file_path) -class ImagePredictionLoggerCallback(Callback): +class ImagePredictionLoggerCallback(pl.Callback): """ Each epoch upload to wandb a couple of the same images with predicted labels. """ @@ -69,7 +69,7 @@ class ImagePredictionLoggerCallback(Callback): ]}, commit=False) -class UnfreezeModelCallback(Callback): +class UnfreezeModelCallback(pl.Callback): """ Unfreeze model after a few epochs. """ @@ -82,7 +82,7 @@ class UnfreezeModelCallback(Callback): param.requires_grad = True -class MetricsHeatmapLoggerCallback(Callback): +class MetricsHeatmapLoggerCallback(pl.Callback): """ Generate f1, precision and recall heatmap calculated from each validation epoch outputs. Expects validation step to return predictions and targets. diff --git a/project/pipeline_modules/lightning_wrapper.py b/project/pipeline_modules/lightning_wrapper.py index 7f2bafa..9e26ead 100644 --- a/project/pipeline_modules/lightning_wrapper.py +++ b/project/pipeline_modules/lightning_wrapper.py @@ -1,6 +1,5 @@ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score -from pl_bolts.models.regression import LogisticRegression -from pl_bolts.models.regression import LinearRegression +from pl_bolts.models.regression import LinearRegression, LogisticRegression from torch.nn.functional import one_hot import torch.nn.functional as F import pytorch_lightning as pl diff --git a/project/predict.py b/project/predict.py new file mode 100644 index 0000000..ea6af7e --- /dev/null +++ b/project/predict.py @@ -0,0 +1,24 @@ +from pipeline_modules.lightning_wrapper import LitModel +from pipeline_modules import transforms +import pytorch_lightning as pl +from train import load_config +import torch + + +# load config +config = load_config() + +# load model from checkpoint +pretrained_model = LitModel.load_from_checkpoint("example.ckpt", config=config) +pretrained_model.eval() + +# load data +img = None + +# preprocess +img = transforms.efficient_net_test_preprocess(img) +img = img.reshape((1, *img.size())) # reshape to form batch of size 1 + +# inference +output = pretrained_model(img) +print(output) diff --git a/project/train.py b/project/train.py index 62b89e1..65aef07 100644 --- a/project/train.py +++ b/project/train.py @@ -95,7 +95,7 @@ def init_wandb(config, model, datamodule): wandb_logger.watch(model.model, log=None) wandb_logger.log_hyperparams({ "model_name": model.model.__class__.__name__, - "dataset_name": datamodule.__class__.__name__, + "datamodule_name": datamodule.__class__.__name__, "optimizer": model.configure_optimizers().__class__.__name__, "train_size": len(datamodule.data_train) if datamodule.data_train is not None else 0, "val_size": len(datamodule.data_val) if datamodule.data_train is not None else 0, diff --git a/requirements.txt b/requirements.txt index 9719023..ec5fb04 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,8 @@ -scikit-learn>=0.23.2 torchvision>=0.8.1 pytorch-lightning>=1.0.6 pytorch-lightning-bolts>=0.2.5 wandb>=0.10.10 PyYAML>=5.3.1 +scikit-learn>=0.23.2 efficientnet-pytorch>=0.7.0 seaborn>=0.11.0