weight_decay=weight_decay) scheduler = LambdaLR(optimizer, lr_lambda=[lr_decay]) # Creation of the typegroup classifier tgc = TypegroupsClassifier({'handwritten': 0, 'printed': 1}, net) if os.path.exists( os.path.join('ocrd_typegroups_classifier', 'models', 'classifier.tgc')): tgc = TypegroupsClassifier.load( os.path.join('ocrd_typegroups_classifier', 'models', 'classifier.tgc')) else: print('Could not load a model to evaluate') quit(1) validation = ImageFolder('lines/validation', transform=None) validation.target_transform = tgc.classMap.get_target_transform( validation.class_to_idx) good = 0 bad = 0 with torch.no_grad(): tgc.network.eval() for idx in tqdm(range(validation.__len__()), desc='Evaluation'): sample, target = validation.__getitem__(idx) path, _ = validation.samples[idx] if target == -1: continue result = tgc.classify(sample, 224, 64, True) highscore = max(result) label = tgc.classMap.cl2id[result[highscore]] if target == label: good += 1 else:
trans = transforms.Compose([ transforms.RandomAffine(4, shear=10), transforms.RandomAffine(4, shear=10), transforms.RandomAffine(4, shear=10), transforms.RandomCrop(224), transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.1, hue=0.05), QLoss(min_q=5, max_q=80), transforms.ToTensor() ]) #training = ImageFolder('/cluster/seuret/patches/all', transform=trans) # TODO : replace by correct path training = ImageFolder('../extracted/samples', transform=trans) training.target_transform = tgc.classMap.get_target_transform( training.class_to_idx) data_loader = torch.utils.data.DataLoader(training, batch_size=64, shuffle=True, num_workers=4) # Iterating over the data print('Starting the training - grab a coffee and a good book!') for epoch in range(200): # Modify learning rate scheduler.step() # Iterate over the data lossSum = 0 good = 0
"folder", help="path to the folder containing the datasets (subfolders with images)", type=str) args = parser.parse_args() sampling = transforms.Compose( [transforms.RandomCrop(224), transforms.ToTensor()]) inv = transforms.ToPILImage() test = ImageFolder(args.folder, transform=sampling) dev = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") tgc = TypegroupsClassifier.load(args.classifier) test.target_transform = tgc.classMap.get_target_transform(test.class_to_idx) nb_classes = 1 + max(tgc.classMap.cl2id.values()) nb_outputs = tgc.network.fc.out_features print(nb_classes, 'classes to consider') print(nb_outputs, 'outputs to process') sm = torch.nn.Softmax() nb_good = 0 nb_bad = 0 tgc.network.eval() imgn = 0 with torch.no_grad(): f = open("foo.html", "w") f.write('<html><head></head><body><table>')