restructure project again

This commit is contained in:
hobogalaxy committed 2020-11-27 01:19:11 +01:00
1 parent 20c451bc3d
commit ee7006a92a
24 files changed
+208 -633

No files matched your search

@@ -1 +0,0 @@
from .lightning_module import LitModel
@@ -67,4 +67,4 @@ class LitModel(pl.LightningModule):
return loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams["weight_decay"])
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams.weight_decay)
@@ -1,35 +0,0 @@
from models.simple_mnist_classifier.lightning_module import LitModel
from lightning_modules.data_modules import transforms
from PIL import Image
def predict():
"""
Loads model from checkpoint.
Model used in lightning_module.py and declaration of that model in models.py
should be the same as during training.
"""
CKPT_PATH = "epoch=0.ckpt"
# load model from checkpoint
pretrained_model = LitModel.load_from_checkpoint(checkpoint_path=CKPT_PATH)
# switch to evaluation mode
pretrained_model.eval()
pretrained_model.freeze()
# load data
img = Image.open("../../example_img.png").convert("L")
# preprocess
img = transforms.mnist_test_preprocess(img)
img = img.reshape((1, *img.size())) # reshape to form batch of size 1
# inference
output = pretrained_model(img)
print(output)
if __name__ == "__main__":
predict()
@@ -1 +0,0 @@
from .lightning_module import LitModel
@@ -67,4 +67,4 @@ class LitModel(pl.LightningModule):
return loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams["weight_decay"])
return torch.optim.Adam(self.parameters(), lr=self.hparams.lr, weight_decay=self.hparams.weight_decay)
@@ -1,4 +1,3 @@
from pl_bolts.models.self_supervised import CPCV2
from efficientnet_pytorch import EfficientNet
from torchvision import models
from torch import nn
@@ -9,7 +8,7 @@ class EfficientNetPretrained(nn.Module):
def __init__(self, config):
super().__init__()
self.model = EfficientNet.from_pretrained('efficientnet-b1')
self.model = EfficientNet.from_pretrained('efficientnet-b0')
for param in self.model.parameters():
param.requires_grad = False
@@ -81,35 +80,3 @@ class ResnextPretrained(nn.Module):
def forward(self, x):
return self.model(x)
# this model doesn't work yet, we wait for lightning-bolts patch
# class ResnetPretrainedUnsupervised(nn.Module):
# """
# Trained without labels on Imagenet.
# Perhaps the features when trained without labels are much better for classification or other tasks.
# """
# def __init__(self, config):
# super().__init__()
#
# self.model = CPCV2(encoder='resnet18', pretrained='imagenet128').encoder
# # self.model = CPCV2(encoder='resnet18', pretrained='stl10').encoder
#
# 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)
# )
#
# def forward(self, x):
# return self.model(x)
@@ -1,35 +0,0 @@
from models.transfer_learning_img_classifier.lightning_module import LitModel
from lightning_modules.data_modules import transforms
from PIL import Image
def predict():
"""
Loads model from checkpoint.
Model used in lightning_module.py and declaration of that model in models.py
should be the same as during training.
"""
CKPT_PATH = "epoch=0.ckpt"
# load model from checkpoint
pretrained_model = LitModel.load_from_checkpoint(checkpoint_path=CKPT_PATH)
# switch to evaluation mode
pretrained_model.eval()
pretrained_model.freeze()
# load data
img = Image.open("../../example_img.png").convert("RGB")
# preprocess
img = transforms.imagenet_test_preprocess(img)
img = img.reshape((1, *img.size())) # reshape to form batch of size 1
# inference
output = pretrained_model(img)
print(output)
if __name__ == "__main__":
predict()