add predict.py
This commit is contained in:
1 parent
c6efd0e27e
commit
e1fb2c634b
5 files changed
+33
-10
No files matched your search
@@ -1,12 +1,12 @@
|
|||||||
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
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 seaborn
|
||||||
import torch
|
import torch
|
||||||
import wandb
|
import wandb
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class ExampleCallback(Callback):
|
class ExampleCallback(pl.Callback):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ class ExampleCallback(Callback):
|
|||||||
print('Do something when training ends.')
|
print('Do something when training ends.')
|
||||||
|
|
||||||
|
|
||||||
class SaveModelOnnxCallback(Callback):
|
class SaveModelOnnxCallback(pl.Callback):
|
||||||
"""
|
"""
|
||||||
Save model in .onnx format.
|
Save model in .onnx format.
|
||||||
"""
|
"""
|
||||||
@@ -42,7 +42,7 @@ class SaveModelOnnxCallback(Callback):
|
|||||||
wandb.save(file_path)
|
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.
|
Each epoch upload to wandb a couple of the same images with predicted labels.
|
||||||
"""
|
"""
|
||||||
@@ -69,7 +69,7 @@ class ImagePredictionLoggerCallback(Callback):
|
|||||||
]}, commit=False)
|
]}, commit=False)
|
||||||
|
|
||||||
|
|
||||||
class UnfreezeModelCallback(Callback):
|
class UnfreezeModelCallback(pl.Callback):
|
||||||
"""
|
"""
|
||||||
Unfreeze model after a few epochs.
|
Unfreeze model after a few epochs.
|
||||||
"""
|
"""
|
||||||
@@ -82,7 +82,7 @@ class UnfreezeModelCallback(Callback):
|
|||||||
param.requires_grad = True
|
param.requires_grad = True
|
||||||
|
|
||||||
|
|
||||||
class MetricsHeatmapLoggerCallback(Callback):
|
class MetricsHeatmapLoggerCallback(pl.Callback):
|
||||||
"""
|
"""
|
||||||
Generate f1, precision and recall heatmap calculated from each validation epoch outputs.
|
Generate f1, precision and recall heatmap calculated from each validation epoch outputs.
|
||||||
Expects validation step to return predictions and targets.
|
Expects validation step to return predictions and targets.
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
|
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, LogisticRegression
|
||||||
from pl_bolts.models.regression import LinearRegression
|
|
||||||
from torch.nn.functional import one_hot
|
from torch.nn.functional import one_hot
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
import pytorch_lightning as pl
|
import pytorch_lightning as pl
|
||||||
|
|||||||
@@ -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)
|
||||||
+1
-1
@@ -95,7 +95,7 @@ def init_wandb(config, model, datamodule):
|
|||||||
wandb_logger.watch(model.model, log=None)
|
wandb_logger.watch(model.model, log=None)
|
||||||
wandb_logger.log_hyperparams({
|
wandb_logger.log_hyperparams({
|
||||||
"model_name": model.model.__class__.__name__,
|
"model_name": model.model.__class__.__name__,
|
||||||
"dataset_name": datamodule.__class__.__name__,
|
"datamodule_name": datamodule.__class__.__name__,
|
||||||
"optimizer": model.configure_optimizers().__class__.__name__,
|
"optimizer": model.configure_optimizers().__class__.__name__,
|
||||||
"train_size": len(datamodule.data_train) if datamodule.data_train is not None else 0,
|
"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,
|
"val_size": len(datamodule.data_val) if datamodule.data_train is not None else 0,
|
||||||
|
|||||||
+1
-1
@@ -1,8 +1,8 @@
|
|||||||
scikit-learn>=0.23.2
|
|
||||||
torchvision>=0.8.1
|
torchvision>=0.8.1
|
||||||
pytorch-lightning>=1.0.6
|
pytorch-lightning>=1.0.6
|
||||||
pytorch-lightning-bolts>=0.2.5
|
pytorch-lightning-bolts>=0.2.5
|
||||||
wandb>=0.10.10
|
wandb>=0.10.10
|
||||||
PyYAML>=5.3.1
|
PyYAML>=5.3.1
|
||||||
|
scikit-learn>=0.23.2
|
||||||
efficientnet-pytorch>=0.7.0
|
efficientnet-pytorch>=0.7.0
|
||||||
seaborn>=0.11.0
|
seaborn>=0.11.0
|
||||||
Reference in new issue
Block a user