7 files changed
+44
-20
No files matched your search
Whitespace-only changes.
@@ -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
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ data_augmentation_transformations = [
|
|||||||
transforms.RandomRotation((-15, 15)),
|
transforms.RandomRotation((-15, 15)),
|
||||||
]
|
]
|
||||||
|
|
||||||
train_preprocess = transforms.Compose([
|
efficient_net_train_preprocess = transforms.Compose([
|
||||||
transforms.Resize((224, 224)),
|
transforms.Resize((224, 224)),
|
||||||
transforms.RandomApply(data_augmentation_transformations, 0.8),
|
transforms.RandomApply(data_augmentation_transformations, 0.8),
|
||||||
transforms.ToTensor(),
|
transforms.ToTensor(),
|
||||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
||||||
])
|
])
|
||||||
|
|
||||||
test_preprocess = transforms.Compose([
|
efficient_net_test_preprocess = transforms.Compose([
|
||||||
transforms.Resize((224, 224)),
|
transforms.Resize((224, 224)),
|
||||||
transforms.ToTensor(),
|
transforms.ToTensor(),
|
||||||
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
|
||||||
|
|||||||
@@ -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)
|
||||||
+10
-9
@@ -57,7 +57,7 @@ def train(config):
|
|||||||
profiler=SimpleProfiler() if config["printing"]["profiler"] else None,
|
profiler=SimpleProfiler() if config["printing"]["profiler"] else None,
|
||||||
weights_summary=config["printing"]["weights_summary"],
|
weights_summary=config["printing"]["weights_summary"],
|
||||||
num_sanity_val_steps=3,
|
num_sanity_val_steps=3,
|
||||||
default_root_dir="logs/"
|
default_root_dir="logs/lightning_logs"
|
||||||
# fast_dev_run=True,
|
# fast_dev_run=True,
|
||||||
# min_epochs=10,
|
# min_epochs=10,
|
||||||
# limit_train_batches=0.1
|
# limit_train_batches=0.1
|
||||||
@@ -81,7 +81,7 @@ def train(config):
|
|||||||
trainer.test()
|
trainer.test()
|
||||||
|
|
||||||
|
|
||||||
def init_wandb(config, model, dataloader):
|
def init_wandb(config, model, datamodule):
|
||||||
wandb_logger = WandbLogger(
|
wandb_logger = WandbLogger(
|
||||||
project=config["loggers"]["wandb"]["project"],
|
project=config["loggers"]["wandb"]["project"],
|
||||||
job_type=config["loggers"]["wandb"]["job_type"],
|
job_type=config["loggers"]["wandb"]["job_type"],
|
||||||
@@ -89,18 +89,19 @@ def init_wandb(config, model, dataloader):
|
|||||||
entity=config["loggers"]["wandb"]["team"],
|
entity=config["loggers"]["wandb"]["team"],
|
||||||
id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None,
|
id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None,
|
||||||
log_model=config["loggers"]["wandb"]["log_model"],
|
log_model=config["loggers"]["wandb"]["log_model"],
|
||||||
offline=config["loggers"]["wandb"]["offline"]
|
offline=config["loggers"]["wandb"]["offline"],
|
||||||
|
save_dir="logs/"
|
||||||
)
|
)
|
||||||
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": dataloader.__class__.__name__,
|
"datamodule_name": datamodule.__class__.__name__,
|
||||||
"optimizer": model.configure_optimizers().__class__.__name__,
|
"optimizer": model.configure_optimizers().__class__.__name__,
|
||||||
"train_size": len(dataloader.data_train) if dataloader.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(dataloader.data_val) if dataloader.data_train is not None else 0,
|
"val_size": len(datamodule.data_val) if datamodule.data_train is not None else 0,
|
||||||
"test_size": len(dataloader.data_test) if dataloader.data_train is not None else 0,
|
"test_size": len(datamodule.data_test) if datamodule.data_train is not None else 0,
|
||||||
"input_dims": dataloader.input_dims,
|
"input_dims": datamodule.input_dims,
|
||||||
"input_size": dataloader.input_size,
|
"input_size": datamodule.input_size,
|
||||||
})
|
})
|
||||||
# download model from a specific wandb run
|
# download model from a specific wandb run
|
||||||
# wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d")
|
# wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d")
|
||||||
|
|||||||
+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