Skip to content
Snippets Groups Projects
Commit 3460b893 authored by Anjith GEORGE's avatar Anjith GEORGE
Browse files

Moving more options to config and some clean up

parent 5c1fa2d9
Branches
Tags
1 merge request!18MCCNN trainer
......@@ -80,8 +80,11 @@ learning_rate=0.0001
seed = 3
output_dir = 'training_mccn'
use_gpu = False
adapted_layers = 'conv1-block1'
adapt_reference_channel = False
verbose = 2
#==============================================================================
"""
Note: Running in GPU
......
......@@ -7,22 +7,24 @@
Usage:
%(prog)s <configuration>
[--model=<string>] [--batch-size=<int>] [--epochs=<int>]
[--learning-rate=<float>] [--seed=<int>]
[--learning-rate=<float>] [--adapted-layers=<string>] [--adapt-reference-channel] [--seed=<int>]
[--output-dir=<path>] [--use-gpu] [--verbose ...]
Arguments:
<configuration> A configuration file, defining the dataset and the network
Options:
-h, --help Shows this help message and exits
--model=<string> Filename of the model to load (if any).
--batch-size=<int> Batch size [default: 64]
--epochs=<int> Number of training epochs [default: 20]
--learning-rate=<float> Learning rate [default: 0.01]
-S, --seed=<int> The random seed [default: 3]
-o, --output-dir=<path> Dir to save stuff [default: training]
-g, --use-gpu Use the GPU
-v, --verbose Increase the verbosity (may appear multiple times).
-h, --help Shows this help message and exits
--model=<string> Filename of the model to load (if any).
--batch-size=<int> Batch size [default: 64]
--epochs=<int> Number of training epochs [default: 20]
--learning-rate=<float> Learning rate [default: 0.01]
--adapted-layers=<string> Layers to adapt in the training [default: conv1-block1-group1]
--adapt-reference-channel Flag deciding whether to adapt default channel as well [default: False]
-S, --seed=<int> The random seed [default: 3]
-o, --output-dir=<path> Dir to save stuff [default: training]
-g, --use-gpu Use the GPU
-v, --verbose Increase the verbosity (may appear multiple times).
Note that arguments provided directly by command-line will override the ones in the configuration file.
......@@ -80,6 +82,8 @@ def main(user_input=None):
output_dir = get_parameter(args, configuration, 'output_dir', 'training')
use_gpu = get_parameter(args, configuration, 'use_gpu', False)
verbosity_level = get_parameter(args, configuration, 'verbose', 0)
adapted_layers = get_parameter(args, configuration, 'adapted_layers', 'conv1-block1-group1')
adapt_reference_channel = get_parameter(args, configuration, 'adapt_reference_channel', False)
bob.core.log.set_verbosity_level(logger, verbosity_level)
bob.io.base.create_directories_safe(output_dir)
......@@ -92,6 +96,8 @@ def main(user_input=None):
logger.debug("Seed = {}".format(seed))
logger.debug("Output directory = {}".format(output_dir))
logger.debug("Use GPU = {}".format(use_gpu))
logger.debug("Adapted layers = {}".format(adapted_layers))
logger.debug("Adapt reference channel = {}".format(adapt_reference_channel))
# process on the arguments / options
torch.manual_seed(seed)
......@@ -110,7 +116,7 @@ def main(user_input=None):
# train the network
if hasattr(configuration, 'network'):
trainer = MCCNNTrainer(configuration.network, batch_size=batch_size, use_gpu=use_gpu, adapted_layers='conv1-block1-group1', adapt_reference_channel=False, verbosity_level=verbosity_level)
trainer = MCCNNTrainer(configuration.network, batch_size=batch_size, use_gpu=use_gpu, adapted_layers=adapted_layers, adapt_reference_channel=adapt_reference_channel, verbosity_level=verbosity_level)
trainer.train(dataloader, n_epochs=epochs, learning_rate=learning_rate, output_dir=output_dir, model=model)
else:
logger.error("Please provide a network in your configuration file !")
......
......@@ -19,8 +19,9 @@ import os
#1. Logging to tensorboardX or a simpler logger:
#2. Support for Validation set and validation loss
#3. Use to(device) instead of .cuda()?: Eventually migrate everything to this!
#4. Moving more arguments to config?
#4. Moving more arguments to config? Done
#5. Implement the selection of channels as a transform: Added now ChannelSelect
#6. Import the function `comp_bce_loss_weights` from utils
"""
def comp_bce_loss_weights(target):
......@@ -72,7 +73,8 @@ class MCCNNTrainer(object):
"""
def __init__(self, network, batch_size=64, use_gpu=False, adapted_layers='conv1-block1-group1',adapt_reference_channel=False, verbosity_level=2):
""" Init function
""" Init function . The layers to be adapted in the network is selected and the gradients are set to `True`
for the layers which needs to be adapted.
Parameters
----------
......@@ -97,15 +99,6 @@ class MCCNNTrainer(object):
self.use_gpu = use_gpu
self.criterion = nn.BCELoss()
# if selected_channel_indexes is not None and isinstance(selected_channel_indexes,list):
# assert(len(selected_channel_indexes)==self.network.num_channels)
# self.sel_ind=selected_channel_indexes
# else: # default is to select all channels
# self.sel_ind=[i for i in range(self.network.num_channels)]
if self.use_gpu:
self.network.cuda()
......@@ -136,17 +129,14 @@ class MCCNNTrainer(object):
layers_to_adapt = list(np.unique(layers_to_adapt))
#logger.info("Listing the layers which would be adapted:")
# Setting the gradients to true for the layers which needs to be adapted
for name, param in self.network.named_parameters():
param.requires_grad = False
for lta in layers_to_adapt:
if lta in name:
#logger.info(name)
param.requires_grad = True
def load_model(self, model_filename):
"""Loads an existing model
......@@ -222,8 +212,6 @@ class MCCNNTrainer(object):
The path to a pretrained model file to start training from; this is the PAD model; not the LightCNN model
"""
logger.debug("EPOCHS to train : {}".format(n_epochs))
logger.debug("Learning rate : {}".format(learning_rate))
# if model exists, load it
if model is not None:
......@@ -235,12 +223,14 @@ class MCCNNTrainer(object):
losses = []
logger.info('Starting training from scratch')
# debug print the layers where grad is true ADD
# print the layers where grad is True
logger.info("Number of channels: {}".format(self.network.num_channels))
for name, param in self.network.named_parameters():
if param.requires_grad == True:
logger.debug('Layer to be adapted from grad check : {}'.format(name))
logger.info('Layer to be adapted from grad check : {}'.format(name))
# setup optimizer
......@@ -258,13 +248,6 @@ class MCCNNTrainer(object):
img, labels = data
# print("img shape type", img.shape, type(img))
# img=imgt[:,self.sel_ind,:,:] # subselect channels
# print("img shape type", img.shape, type(img))
#sel_ind
labels=labels.float().unsqueeze(1)
......@@ -280,7 +263,8 @@ class MCCNNTrainer(object):
imagesv = Variable(img)
labelsv = Variable(labels)
self.criterion.weight=weights
# weights for samples, should help with data imbalance
self.criterion.weight = weights
output= self.network(imagesv)
loss = self.criterion(output, labelsv)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment