X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=cnn-svrt.py;h=f3d350eb9ea9203f408a0603e7c0458b88801e95;hb=aca8ab8e7d30f1f79829d57897238469df5468b0;hp=142b81f00b271d9604dc3c80c65ba229fc0401f5;hpb=d150b39b0cf1ee7cbfcecc9d2b3bbc01411662ff;p=pysvrt.git diff --git a/cnn-svrt.py b/cnn-svrt.py index 142b81f..f3d350e 100755 --- a/cnn-svrt.py +++ b/cnn-svrt.py @@ -56,6 +56,13 @@ parser.add_argument('--nb_train_samples', parser.add_argument('--nb_test_samples', type = int, default = 10000) +parser.add_argument('--nb_validation_samples', + type = int, default = 10000) + +parser.add_argument('--validation_error_threshold', + type = float, default = 0.0, + help = 'Early training termination criterion') + parser.add_argument('--nb_epochs', type = int, default = 50) @@ -77,11 +84,15 @@ parser.add_argument('--test_loaded_models', type = distutils.util.strtobool, default = 'False', help = 'Should we compute the test errors of loaded models') +parser.add_argument('--problems', + type = str, default = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23', + help = 'What problems to process') + args = parser.parse_args() ###################################################################### -log_file = open(args.log_file, 'w') +log_file = open(args.log_file, 'a') pred_log_t = None print(Fore.RED + 'Logging into ' + args.log_file + Style.RESET_ALL) @@ -190,7 +201,22 @@ class AfrozeDeepNet(nn.Module): ###################################################################### -def train_model(model, train_set): +def nb_errors(model, data_set): + ne = 0 + for b in range(0, data_set.nb_batches): + input, target = data_set.get_batch(b) + output = model.forward(Variable(input)) + wta_prediction = output.data.max(1)[1].view(-1) + + for i in range(0, data_set.batch_size): + if wta_prediction[i] != target[i]: + ne = ne + 1 + + return ne + +###################################################################### + +def train_model(model, train_set, validation_set): batch_size = args.batch_size criterion = nn.CrossEntropyLoss() @@ -212,25 +238,24 @@ def train_model(model, train_set): loss.backward() optimizer.step() dt = (time.time() - start_t) / (e + 1) + log_string('train_loss {:d} {:f}'.format(e + 1, acc_loss), ' [ETA ' + time.ctime(time.time() + dt * (args.nb_epochs - e)) + ']') - return model - -###################################################################### + if validation_set is not None: + nb_validation_errors = nb_errors(model, validation_set) -def nb_errors(model, data_set): - ne = 0 - for b in range(0, data_set.nb_batches): - input, target = data_set.get_batch(b) - output = model.forward(Variable(input)) - wta_prediction = output.data.max(1)[1].view(-1) + log_string('validation_error {:.02f}% {:d} {:d}'.format( + 100 * nb_validation_errors / validation_set.nb_samples, + nb_validation_errors, + validation_set.nb_samples) + ) - for i in range(0, data_set.batch_size): - if wta_prediction[i] != target[i]: - ne = ne + 1 + if nb_validation_errors / validation_set.nb_samples <= args.validation_error_threshold: + log_string('below validation_error_threshold') + break - return ne + return model ###################################################################### @@ -250,16 +275,18 @@ def int_to_suffix(n): class vignette_logger(): def __init__(self, delay_min = 60): self.start_t = time.time() + self.last_t = self.start_t self.delay_min = delay_min def __call__(self, n, m): t = time.time() - if t > self.start_t + self.delay_min: + if t > self.last_t + self.delay_min: dt = (t - self.start_t) / m log_string('sample_generation {:d} / {:d}'.format( m, n), ' [ETA ' + time.ctime(time.time() + dt * (n - m)) + ']' ) + self.last_t = t ###################################################################### @@ -267,6 +294,8 @@ if args.nb_train_samples%args.batch_size > 0 or args.nb_test_samples%args.batch_ print('The number of samples must be a multiple of the batch size.') raise +log_string('############### start ###############') + if args.compress_vignettes: log_string('using_compressed_vignettes') VignetteSet = svrtset.CompressedVignetteSet @@ -274,7 +303,7 @@ else: log_string('using_uncompressed_vignettes') VignetteSet = svrtset.VignetteSet -for problem_number in range(1, 24): +for problem_number in map(int, args.problems.split(',')): log_string('############### problem ' + str(problem_number) + ' ###############') @@ -321,7 +350,15 @@ for problem_number in range(1, 24): train_set.nb_samples / (time.time() - t)) ) - train_model(model, train_set) + if args.validation_error_threshold > 0.0: + validation_set = VignetteSet(problem_number, + args.nb_validation_samples, args.batch_size, + cuda = torch.cuda.is_available(), + logger = vignette_logger()) + else: + validation_set = None + + train_model(model, train_set, validation_set) torch.save(model.state_dict(), model_filename) log_string('saved_model ' + model_filename) @@ -345,10 +382,6 @@ for problem_number in range(1, 24): args.nb_test_samples, args.batch_size, cuda = torch.cuda.is_available()) - log_string('data_generation {:0.2f} samples / s'.format( - test_set.nb_samples / (time.time() - t)) - ) - nb_test_errors = nb_errors(model, test_set) log_string('test_error {:d} {:.02f}% {:d} {:d}'.format(