From 14eaa4005e16d8a473464e0ee72f83cb0cc05baf Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Thu, 15 Jun 2017 16:35:50 +0200 Subject: [PATCH] Added persistence of the model parameters. --- cnn-svrt.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/cnn-svrt.py b/cnn-svrt.py index 3e20f8e..e7e4574 100755 --- a/cnn-svrt.py +++ b/cnn-svrt.py @@ -81,7 +81,7 @@ def generate_set(p, n): t = time.time() input = svrt.generate_vignettes(p, target) t = time.time() - t - log_string('DATA_SET_GENERATION {:.02f} sample/s'.format(n / t)) + log_string('data_set_generation {:.02f} sample/s'.format(n / t)) input = input.view(input.size(0), 1, input.size(1), input.size(2)).float() return Variable(input), Variable(target) @@ -101,9 +101,9 @@ def generate_set(p, n): # -- full(120x84) -> 84 1 # -- full(84x2) -> 2 1 -class Net(nn.Module): +class AfrozeShallowNet(nn.Module): def __init__(self): - super(Net, self).__init__() + super(AfrozeShallowNet, self).__init__() self.conv1 = nn.Conv2d(1, 6, kernel_size=21) self.conv2 = nn.Conv2d(6, 16, kernel_size=19) self.conv3 = nn.Conv2d(16, 120, kernel_size=18) @@ -119,16 +119,10 @@ class Net(nn.Module): x = self.fc2(x) return x -def train_model(train_input, train_target): - model, criterion = Net(), nn.CrossEntropyLoss() - - nb_parameters = 0 - for p in model.parameters(): - nb_parameters += p.numel() - log_string('NB_PARAMETERS {:d}'.format(nb_parameters)) +def train_model(model, train_input, train_target): + criterion = nn.CrossEntropyLoss() if torch.cuda.is_available(): - model.cuda() criterion.cuda() optimizer, bs = optim.SGD(model.parameters(), lr = 1e-2), 100 @@ -142,7 +136,7 @@ def train_model(train_input, train_target): model.zero_grad() loss.backward() optimizer.step() - log_string('TRAIN_LOSS {:d} {:f}'.format(k, acc_loss)) + log_string('train_loss {:d} {:f}'.format(k, acc_loss)) return model @@ -164,25 +158,41 @@ def nb_errors(model, data_input, data_target, bs = 100): ###################################################################### for arg in vars(args): - log_string('ARGUMENT ' + str(arg) + ' ' + str(getattr(args, arg))) + log_string('argument ' + str(arg) + ' ' + str(getattr(args, arg))) for problem_number in range(1, 24): train_input, train_target = generate_set(problem_number, args.nb_train_samples) test_input, test_target = generate_set(problem_number, args.nb_test_samples) + model = AfrozeShallowNet() if torch.cuda.is_available(): train_input, train_target = train_input.cuda(), train_target.cuda() test_input, test_target = test_input.cuda(), test_target.cuda() + model.cuda() mu, std = train_input.data.mean(), train_input.data.std() train_input.data.sub_(mu).div_(std) test_input.data.sub_(mu).div_(std) - model = train_model(train_input, train_target) + nb_parameters = 0 + for p in model.parameters(): + nb_parameters += p.numel() + log_string('nb_parameters {:d}'.format(nb_parameters)) + + model_filename = 'model_' + str(problem_number) + '.param' + + try: + model.load_state_dict(torch.load(model_filename)) + log_string('loaded_model ' + model_filename) + except: + log_string('training_model') + train_model(model, train_input, train_target) + torch.save(model.state_dict(), model_filename) + log_string('saved_model ' + model_filename) nb_train_errors = nb_errors(model, train_input, train_target) - log_string('TRAIN_ERROR {:d} {:.02f}% {:d} {:d}'.format( + log_string('train_error {:d} {:.02f}% {:d} {:d}'.format( problem_number, 100 * nb_train_errors / train_input.size(0), nb_train_errors, @@ -191,7 +201,7 @@ for problem_number in range(1, 24): nb_test_errors = nb_errors(model, test_input, test_target) - log_string('TEST_ERROR {:d} {:.02f}% {:d} {:d}'.format( + log_string('test_error {:d} {:.02f}% {:d} {:d}'.format( problem_number, 100 * nb_test_errors / test_input.size(0), nb_test_errors, -- 2.39.5