11 files changed
+127
-50
No files matched your search
+2
-2
@@ -16,7 +16,7 @@ hparams:
|
|||||||
resume:
|
resume:
|
||||||
resume_from_ckpt: False
|
resume_from_ckpt: False
|
||||||
wandb_run_id: "8uuomodb"
|
wandb_run_id: "8uuomodb"
|
||||||
ckpt_path: "wandb/run-20201111_022721-8uuomodb/files/hackathon_template_test/8uuomodb/checkpoints/epoch=2.ckpt"
|
ckpt_path: "checkpoints/epoch=2.ckpt"
|
||||||
|
|
||||||
loggers:
|
loggers:
|
||||||
wandb:
|
wandb:
|
||||||
@@ -41,5 +41,5 @@ callbacks:
|
|||||||
|
|
||||||
printing:
|
printing:
|
||||||
progress_bar_refresh_rate: 5
|
progress_bar_refresh_rate: 5
|
||||||
weights_summary: "top" # or "full"
|
weights_summary: "top" # "full"
|
||||||
profiler: False
|
profiler: False
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,21 @@
|
|||||||
|
from torch.utils.data import Dataset
|
||||||
|
from PIL import Image
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class TestDataset(Dataset):
|
||||||
|
|
||||||
|
def __init__(self, img_dir, transform):
|
||||||
|
self.transform = transform
|
||||||
|
self.images = [os.path.join(img_dir, fname) for fname in os.listdir(img_dir)]
|
||||||
|
|
||||||
|
def __getitem__(self, idx):
|
||||||
|
image = Image.open(self.images[idx]).convert("RGB")
|
||||||
|
|
||||||
|
if self.transform is not None:
|
||||||
|
image = self.transform(image)
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.images)
|
||||||
@@ -12,10 +12,10 @@ from pipeline_modules.models import *
|
|||||||
|
|
||||||
class LitModel(pl.LightningModule):
|
class LitModel(pl.LightningModule):
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, hparams):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.save_hyperparameters(config["hparams"])
|
self.save_hyperparameters(hparams)
|
||||||
self.model = SimpleLinearMNIST(config=self.hparams)
|
self.model = SimpleLinearMNIST(config=self.hparams)
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
from pytorch_lightning.loggers import WandbLogger, TensorBoardLogger, CometLogger, MLFlowLogger, NeptuneLogger
|
||||||
|
|
||||||
|
|
||||||
|
def init_wandb(config, lit_model, datamodule):
|
||||||
|
wandb_logger = WandbLogger(
|
||||||
|
project=config["loggers"]["wandb"]["project"],
|
||||||
|
job_type=config["loggers"]["wandb"]["job_type"],
|
||||||
|
tags=config["loggers"]["wandb"]["tags"],
|
||||||
|
entity=config["loggers"]["wandb"]["team"],
|
||||||
|
id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None,
|
||||||
|
log_model=config["loggers"]["wandb"]["log_model"],
|
||||||
|
offline=config["loggers"]["wandb"]["offline"],
|
||||||
|
save_dir="logs/"
|
||||||
|
)
|
||||||
|
wandb_logger.watch(lit_model.model, log=None)
|
||||||
|
wandb_logger.log_hyperparams({
|
||||||
|
"model_name": lit_model.model.__class__.__name__,
|
||||||
|
"datamodule_name": datamodule.__class__.__name__,
|
||||||
|
"optimizer": lit_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,
|
||||||
|
"test_size": len(datamodule.data_test) if datamodule.data_train is not None else 0,
|
||||||
|
"input_dims": datamodule.input_dims,
|
||||||
|
"input_size": datamodule.input_size,
|
||||||
|
})
|
||||||
|
# download model from a specific wandb run
|
||||||
|
# wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d")
|
||||||
|
return wandb_logger
|
||||||
@@ -12,15 +12,21 @@ class SimpleLinearMNIST(nn.Module):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
# mnist images are (1, 28, 28) (channels, width, height)
|
# mnist images are (1, 28, 28) (channels, width, height)
|
||||||
self.model = nn.Sequential(
|
self.model.fc = nn.Sequential(
|
||||||
nn.Linear(28 * 28, config["lin1_size"]),
|
nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin1_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.3),
|
||||||
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin2_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.25),
|
||||||
nn.Linear(config["lin2_size"], config["lin3_size"]),
|
nn.Linear(config["lin2_size"], config["lin3_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin3_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.2),
|
||||||
nn.Linear(config["lin3_size"], config["output_size"]),
|
nn.Linear(config["lin3_size"], config["output_size"]),
|
||||||
nn.LogSoftmax(dim=1),
|
nn.LogSoftmax(dim=1)
|
||||||
)
|
)
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
@@ -42,11 +48,13 @@ class EfficientNetPretrained(nn.Module):
|
|||||||
for param in self.model.parameters():
|
for param in self.model.parameters():
|
||||||
param.requires_grad = False
|
param.requires_grad = False
|
||||||
|
|
||||||
self.model._fc = nn.Sequential(
|
self.model.fc = nn.Sequential(
|
||||||
nn.Linear(self.model._fc.in_features, config["lin1_size"]),
|
nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin1_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
nn.Dropout(p=0.25),
|
nn.Dropout(p=0.25),
|
||||||
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin2_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
nn.Dropout(p=0.2),
|
nn.Dropout(p=0.2),
|
||||||
nn.Linear(config["lin2_size"], config["output_size"]),
|
nn.Linear(config["lin2_size"], config["output_size"]),
|
||||||
@@ -68,9 +76,40 @@ class ResnetPretrained(nn.Module):
|
|||||||
|
|
||||||
self.model.fc = nn.Sequential(
|
self.model.fc = nn.Sequential(
|
||||||
nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin1_size"]),
|
||||||
nn.ReLU(),
|
nn.ReLU(),
|
||||||
nn.Dropout(p=0.15),
|
nn.Dropout(p=0.25),
|
||||||
nn.Linear(config["lin1_size"], config["output_size"]),
|
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin2_size"]),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.2),
|
||||||
|
nn.Linear(config["lin2_size"], config["output_size"]),
|
||||||
|
nn.LogSoftmax(dim=1)
|
||||||
|
)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.model(x)
|
||||||
|
|
||||||
|
|
||||||
|
class ResnextPretrained(nn.Module):
|
||||||
|
def __init__(self, config):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.model = models.resnext101_32x8d(pretrained=True)
|
||||||
|
|
||||||
|
for param in self.model.parameters():
|
||||||
|
param.requires_grad = False
|
||||||
|
|
||||||
|
self.model.fc = nn.Sequential(
|
||||||
|
nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin1_size"]),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.25),
|
||||||
|
nn.Linear(config["lin1_size"], config["lin2_size"]),
|
||||||
|
nn.BatchNorm1d(config["lin2_size"]),
|
||||||
|
nn.ReLU(),
|
||||||
|
nn.Dropout(p=0.2),
|
||||||
|
nn.Linear(config["lin2_size"], config["output_size"]),
|
||||||
nn.LogSoftmax(dim=1)
|
nn.LogSoftmax(dim=1)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -95,9 +134,14 @@ class ResnetPretrained(nn.Module):
|
|||||||
#
|
#
|
||||||
# self.model.fc = nn.Sequential(
|
# self.model.fc = nn.Sequential(
|
||||||
# nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
# nn.Linear(self.model.fc.in_features, config["lin1_size"]),
|
||||||
|
# nn.BatchNorm1d(config["lin1_size"]),
|
||||||
# nn.ReLU(),
|
# nn.ReLU(),
|
||||||
# nn.Dropout(p=0.15),
|
# nn.Dropout(p=0.25),
|
||||||
# nn.Linear(config["lin1_size"], config["output_size"]),
|
# nn.Linear(config["lin1_size"], config["lin2_size"]),
|
||||||
|
# nn.BatchNorm1d(config["lin2_size"]),
|
||||||
|
# nn.ReLU(),
|
||||||
|
# nn.Dropout(p=0.2),
|
||||||
|
# nn.Linear(config["lin2_size"], config["output_size"]),
|
||||||
# nn.LogSoftmax(dim=1)
|
# nn.LogSoftmax(dim=1)
|
||||||
# )
|
# )
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -3,14 +3,16 @@ from torchvision import transforms
|
|||||||
|
|
||||||
data_augmentation_transformations = [
|
data_augmentation_transformations = [
|
||||||
transforms.RandomAffine((-15, 15), translate=(0.2, 0.2)),
|
transforms.RandomAffine((-15, 15), translate=(0.2, 0.2)),
|
||||||
transforms.RandomHorizontalFlip(p=0.8),
|
transforms.RandomHorizontalFlip(p=0.65),
|
||||||
transforms.RandomRotation((-15, 15)),
|
transforms.RandomRotation((-15, 15)),
|
||||||
|
transforms.ColorJitter(hue=.05, saturation=.05),
|
||||||
]
|
]
|
||||||
|
|
||||||
efficient_net_train_preprocess = transforms.Compose([
|
efficient_net_train_preprocess = transforms.Compose([
|
||||||
transforms.Resize((224, 224)),
|
transforms.Resize((256, 256)), # the correct size for eff net b1 is 224x224 but we random crop it later
|
||||||
transforms.RandomApply(data_augmentation_transformations, 0.8),
|
transforms.RandomCrop(224),
|
||||||
transforms.ToTensor(),
|
transforms.ToTensor(),
|
||||||
|
transforms.RandomApply(data_augmentation_transformations, 0.8),
|
||||||
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]),
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|||||||
+11
-1
@@ -5,11 +5,12 @@ from train import load_config
|
|||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
def predict():
|
||||||
# load config
|
# load config
|
||||||
config = load_config()
|
config = load_config()
|
||||||
|
|
||||||
# load model from checkpoint
|
# load model from checkpoint
|
||||||
pretrained_model = LitModel.load_from_checkpoint("example.ckpt", config=config)
|
pretrained_model = LitModel.load_from_checkpoint("example.ckpt", config=config["hparams"])
|
||||||
pretrained_model.eval()
|
pretrained_model.eval()
|
||||||
|
|
||||||
# load data
|
# load data
|
||||||
@@ -22,3 +23,12 @@ img = img.reshape((1, *img.size())) # reshape to form batch of size 1
|
|||||||
# inference
|
# inference
|
||||||
output = pretrained_model(img)
|
output = pretrained_model(img)
|
||||||
print(output)
|
print(output)
|
||||||
|
|
||||||
|
|
||||||
|
def download_from_wanb():
|
||||||
|
MODEL_PATH = ""
|
||||||
|
CODE_PATH = ""
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
predict()
|
||||||
+5
-33
@@ -1,16 +1,15 @@
|
|||||||
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
|
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
|
||||||
from pytorch_lightning.profiler import SimpleProfiler, AdvancedProfiler
|
from pytorch_lightning.profiler import SimpleProfiler
|
||||||
from pytorch_lightning.loggers import WandbLogger
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
# custom
|
# custom
|
||||||
from pipeline_modules.lightning_wrapper import LitModel
|
from pipeline_modules.lightning_wrapper import LitModel
|
||||||
from project.pipeline_modules.data_modules import *
|
from pipeline_modules.logger_initializers import *
|
||||||
from project.pipeline_modules.callbacks import *
|
from pipeline_modules.data_modules import *
|
||||||
|
from pipeline_modules.callbacks import *
|
||||||
|
|
||||||
|
|
||||||
def train(config):
|
def train(config):
|
||||||
|
|
||||||
# Init data module
|
# Init data module
|
||||||
datamodule = MNISTDataModule(
|
datamodule = MNISTDataModule(
|
||||||
batch_size=config["hparams"]["batch_size"],
|
batch_size=config["hparams"]["batch_size"],
|
||||||
@@ -20,7 +19,7 @@ def train(config):
|
|||||||
datamodule.setup()
|
datamodule.setup()
|
||||||
|
|
||||||
# Init our model
|
# Init our model
|
||||||
model = LitModel(config)
|
model = LitModel(hparams=config["hparams"])
|
||||||
|
|
||||||
# Init wandb logger
|
# Init wandb logger
|
||||||
wandb_logger = init_wandb(config, model, datamodule)
|
wandb_logger = init_wandb(config, model, datamodule)
|
||||||
@@ -81,33 +80,6 @@ def train(config):
|
|||||||
trainer.test()
|
trainer.test()
|
||||||
|
|
||||||
|
|
||||||
def init_wandb(config, model, datamodule):
|
|
||||||
wandb_logger = WandbLogger(
|
|
||||||
project=config["loggers"]["wandb"]["project"],
|
|
||||||
job_type=config["loggers"]["wandb"]["job_type"],
|
|
||||||
tags=config["loggers"]["wandb"]["tags"],
|
|
||||||
entity=config["loggers"]["wandb"]["team"],
|
|
||||||
id=config["resume"]["wandb_run_id"] if config["resume"]["resume_from_ckpt"] else None,
|
|
||||||
log_model=config["loggers"]["wandb"]["log_model"],
|
|
||||||
offline=config["loggers"]["wandb"]["offline"],
|
|
||||||
save_dir="logs/"
|
|
||||||
)
|
|
||||||
wandb_logger.watch(model.model, log=None)
|
|
||||||
wandb_logger.log_hyperparams({
|
|
||||||
"model_name": model.model.__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,
|
|
||||||
"test_size": len(datamodule.data_test) if datamodule.data_train is not None else 0,
|
|
||||||
"input_dims": datamodule.input_dims,
|
|
||||||
"input_size": datamodule.input_size,
|
|
||||||
})
|
|
||||||
# download model from a specific wandb run
|
|
||||||
# wandb.restore('model-best.h5', run_path="kino/some_project/a1b2c3d")
|
|
||||||
return wandb_logger
|
|
||||||
|
|
||||||
|
|
||||||
def load_config():
|
def load_config():
|
||||||
with open("config.yaml", "r") as ymlfile:
|
with open("config.yaml", "r") as ymlfile:
|
||||||
config = yaml.load(ymlfile, Loader=yaml.FullLoader)
|
config = yaml.load(ymlfile, Loader=yaml.FullLoader)
|
||||||
|
|||||||
Reference in new issue
Block a user