added loading and testing the model again
This commit is contained in:
@@ -36,11 +36,15 @@ Root_dir/
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
once this is manually done, We can run finetunning a selected model. For now, 4 models are implemented.
|
once this is manually done, We can run finetunning a selected model. For now, 8 models are implemented.
|
||||||
- ResNet-18
|
- ResNet-18
|
||||||
- ResNet-50
|
- ResNet-50
|
||||||
- DenseNet121
|
- DenseNet121
|
||||||
- Inception
|
- Inception
|
||||||
|
- GoogleNet
|
||||||
|
- ShuffleNet
|
||||||
|
- EfficientNet
|
||||||
|
- WideResNet
|
||||||
|
|
||||||
## Fine tuning
|
## Fine tuning
|
||||||
### Preparation
|
### Preparation
|
||||||
|
|||||||
26
Tune.py
26
Tune.py
@@ -8,7 +8,7 @@ from IdentitySubset import IdentitySubset
|
|||||||
from architectures.Model import Model, Architecture
|
from architectures.Model import Model, Architecture
|
||||||
|
|
||||||
# numbre of classes
|
# numbre of classes
|
||||||
CLASS_SIZE = 50
|
CLASS_SIZE = 20
|
||||||
# batch
|
# batch
|
||||||
BATCH_SIZE = 16
|
BATCH_SIZE = 16
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ TRAINING_SMPLE = 28
|
|||||||
|
|
||||||
# learning rate
|
# learning rate
|
||||||
LR_RATE = 0.0001
|
LR_RATE = 0.0001
|
||||||
EPOCHS = 20
|
EPOCHS = 16
|
||||||
|
|
||||||
# depends on model architecture
|
# depends on model architecture
|
||||||
# ResNet, DenseNet = 224
|
# ResNet, DenseNet = 224
|
||||||
@@ -36,7 +36,7 @@ RESOLUTION = 224
|
|||||||
# - GOOGLENET
|
# - GOOGLENET
|
||||||
# - EFFICIENTNET
|
# - EFFICIENTNET
|
||||||
# - SHUFFLENET
|
# - SHUFFLENET
|
||||||
arch = Architecture.GOOGLENET
|
arch = Architecture.RESNET50
|
||||||
|
|
||||||
# DATA PREPARATION
|
# DATA PREPARATION
|
||||||
# load data set and prepare
|
# load data set and prepare
|
||||||
@@ -88,7 +88,7 @@ model = Model.create(
|
|||||||
device = device,
|
device = device,
|
||||||
size = CLASS_SIZE)
|
size = CLASS_SIZE)
|
||||||
|
|
||||||
# FINETUNING
|
# we may need to load existing model or finetune
|
||||||
model.train(
|
model.train(
|
||||||
epochs = EPOCHS,
|
epochs = EPOCHS,
|
||||||
loader = train_loader,
|
loader = train_loader,
|
||||||
@@ -96,11 +96,7 @@ model.train(
|
|||||||
|
|
||||||
# save.
|
# save.
|
||||||
model.save(filename=arch.name.lower())
|
model.save(filename=arch.name.lower())
|
||||||
'''
|
|
||||||
torch.save(
|
|
||||||
model.get().state_dict(),
|
|
||||||
f'trained/{arch.name.lower()}.pth'
|
|
||||||
)'''
|
|
||||||
|
|
||||||
# done tuning
|
# done tuning
|
||||||
print('Model saved!')
|
print('Model saved!')
|
||||||
@@ -125,3 +121,15 @@ print(f"Total test images for these {CLASS_SIZE} classes: {len(test_data)}")
|
|||||||
# Evaluate
|
# Evaluate
|
||||||
model.evaluate(
|
model.evaluate(
|
||||||
loader = test_loader)
|
loader = test_loader)
|
||||||
|
|
||||||
|
# test again
|
||||||
|
reloaded = Model.create(
|
||||||
|
arch=arch,
|
||||||
|
device = device,
|
||||||
|
size = CLASS_SIZE
|
||||||
|
)
|
||||||
|
reloaded.load(arch = arch)
|
||||||
|
print("Evaluating loaded")
|
||||||
|
reloaded.evaluate(
|
||||||
|
loader = test_loader
|
||||||
|
)
|
||||||
|
|||||||
@@ -10,5 +10,22 @@ class GoogleNet(Model):
|
|||||||
def get(self):
|
def get(self):
|
||||||
|
|
||||||
m = models.googlenet(weights=models.GoogLeNet_Weights.DEFAULT)
|
m = models.googlenet(weights=models.GoogLeNet_Weights.DEFAULT)
|
||||||
|
|
||||||
|
# 1. Handle the two Auxiliary Classifiers
|
||||||
|
# GoogLeNet has aux1 and aux2 to help training converge
|
||||||
|
#if m.aux_logits:
|
||||||
|
#m.aux1.fc = nn.Linear(m.aux1.fc.in_features, self.size)
|
||||||
|
#m.aux2.fc = nn.Linear(m.aux2.fc.in_features, self.size)
|
||||||
|
|
||||||
|
# 2. Handle the Main Classifier
|
||||||
m.fc = nn.Linear(m.fc.in_features, self.size)
|
m.fc = nn.Linear(m.fc.in_features, self.size)
|
||||||
|
|
||||||
|
#for param in m.parameters():
|
||||||
|
# param.requires_grad = False
|
||||||
|
|
||||||
|
# Unfreezing the final stages for identity recognition
|
||||||
|
#for name, param in m.named_parameters():
|
||||||
|
# if "inception5" in name or "fc" in name:
|
||||||
|
# param.requires_grad = True
|
||||||
|
|
||||||
return m
|
return m
|
||||||
@@ -62,9 +62,7 @@ class Model(ABC):
|
|||||||
|
|
||||||
|
|
||||||
def save(self, filename=None):
|
def save(self, filename=None):
|
||||||
"""
|
|
||||||
Saves the model state_dict. Creates the directory if it doesn't exist.
|
|
||||||
"""
|
|
||||||
save_dir = Path("trained_models")
|
save_dir = Path("trained_models")
|
||||||
save_dir.mkdir(parents=True, exist_ok=True)
|
save_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
@@ -72,8 +70,29 @@ class Model(ABC):
|
|||||||
if filename is None:
|
if filename is None:
|
||||||
filename = f"{self.__class__.__name__.lower()}.pth"
|
filename = f"{self.__class__.__name__.lower()}.pth"
|
||||||
|
|
||||||
|
if not filename.endswith('.pth'):
|
||||||
|
filename += '.pth'
|
||||||
|
|
||||||
save_path = save_dir / filename
|
save_path = save_dir / filename
|
||||||
torch.save(self.model.state_dict(), save_path)
|
torch.save(self.model.state_dict(), save_path)
|
||||||
|
print(f'Model saved to {save_path}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def load(self, arch):
|
||||||
|
|
||||||
|
file_path = Path("trained_models") / f'{arch.name.lower()}.pth'
|
||||||
|
# does file exist
|
||||||
|
if not file_path.exists():
|
||||||
|
raise FileNotFoundError(f'No checkpoint found at: {file_path}')
|
||||||
|
|
||||||
|
# Load the weights
|
||||||
|
state_dict = torch.load(file_path, map_location=self.device, weights_only=True)
|
||||||
|
|
||||||
|
self.model.load_state_dict(state_dict)
|
||||||
|
self.model.to(self.device)
|
||||||
|
print(f'Model loaded from {file_path}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Using the factory patern here
|
# Using the factory patern here
|
||||||
|
|||||||
Reference in New Issue
Block a user