Ejemplo n.º 1
0
 def test__init__custom_loss_function_type(self):
     classifier = gem.Classifier(
         model,
         train_dataset.classes('BodyPartExamined'),
         loss_function=gem.nn.MultiLabelMarginLoss())
     self.assertIsInstance(classifier.loss_function,
                           gem.nn.MultiLabelMarginLoss)
Ejemplo n.º 2
0
 def test__init__custom_layer_config_type(self):
     classifier = gem.Classifier(
         model,
         train_dataset.classes('BodyPartExamined'),
         layer_config=gem.functr.DefaultLastLayerConfig())
     self.assertIsInstance(classifier.layer_config,
                           gem.functr.DefaultLastLayerConfig)
Ejemplo n.º 3
0
 def test_train_correct_usage(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     classifier.train(train_dataset,
                      epochs=1,
                      pin_memory=True,
                      test_dataset=eval_dataset)
Ejemplo n.º 4
0
def _train(model, field_list, value_list, file):
    net = gem.Classifier(model, train_set_classes, enable_cuda=True)

    # set correct net params
    value_types = []
    if len(field_list):
        for index, field in enumerate(field_list):
            # check if the field exists if not just throw and terminate, a small anti oopsie feature
            getattr(net, field)
            setattr(net, field, value_list[index])
            value_types += [str(value_list[index])]

    # log train parameters to an excel file
    if len(value_types):
        log_train_options(value_types, file)
    else:
        log_train_options(["default"], file)

    # train and log the results
    net.train(train_set,
              epochs=80,
              test_dataset=eval_set,
              verbosity=2,
              num_workers=4,
              batch_size=4,
              output_policy=gem.ToConsoleAndExcelFile(file))
Ejemplo n.º 5
0
def demo_initialize_classifier():
    # Use resnet 18 as the base model for our new classifier
    resnet18 = models.resnet18(pretrained=True)
    dataset = gem.DicomoDataset.get_dicomo_dataset(train_dataset, labels=['BodyPartExamined'])
    dataset.summarize('BodyPartExamined')
    net = gem.Classifier(resnet18, dataset.classes('BodyPartExamined'), enable_cuda=True)
    net.save(classifier_path)
Ejemplo n.º 6
0
 def test__init__custom_optimizer_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'),
                                 optimizer=gem.torch.optim.SGD(
                                     model.parameters(),
                                     lr=0.001,
                                     momentum=0.9))
     self.assertIsInstance(classifier.optimizer, gem.torch.optim.SGD)
Ejemplo n.º 7
0
 def test__init__wrong_enable_cuda_type(self):
     try:
         with self.assertRaises(TypeError):
             gem.Classifier(model,
                            train_dataset.classes('BodyPartExamined'),
                            enable_cuda=list())
     except RuntimeError:
         None
Ejemplo n.º 8
0
 def test_train_negative_num_workers(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.train(train_dataset,
                          epochs=1,
                          pin_memory=True,
                          num_workers=-1)
Ejemplo n.º 9
0
 def test_train_wrong_batch_size_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.train(train_dataset,
                          batch_size="z",
                          epochs=1,
                          pin_memory=True)
Ejemplo n.º 10
0
 def test_train_wrong_output_policy_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.train(train_dataset,
                          epochs=1,
                          pin_memory=True,
                          output_policy=[])
Ejemplo n.º 11
0
 def test__init__correct_device_type(self):
     try:
         classifier = gem.Classifier(
             model,
             train_dataset.classes('BodyPartExamined'),
             enable_cuda=True)
         self.assertEqual(str(classifier.device), "cuda")
     except RuntimeError:
         None
Ejemplo n.º 12
0
 def test_init_correct_usage(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     self.assertIsInstance(classifier, gem.Classifier)
     self.assertIsInstance(classifier.module, gem.nn.Module)
     self.assertIsInstance(classifier.classes, list)
     self.assertIsInstance(classifier.layer_config,
                           gem.functr.GEMICAIABCFunctor)
     self.assertIsInstance(classifier.loss_function, gem.nn.Module)
     self.assertIsInstance(classifier.optimizer, gem.torch.optim.Optimizer)
def bench_initialize_tree():
    # Select the fields the tree should sequencially. The first item in the list will what the root of the tree
    # classifies. Initializing the Tree takes quite long, as it has to calculate the classes for every node.
    relevant_labels = ['BodyPartExamined', 'StudyDescription']
    resnet18 = models.resnet18(pretrained=True)
    ds = gem.DicomoDataset.get_dicomo_dataset('/mnt/SharedStor/dataset/DX', labels=relevant_labels)
    net = gem.Classifier(resnet18, [], enable_cuda=True)
    start = datetime.now()
    tree = gem.ClassifierTree(default_classifier=net, labels=relevant_labels, base_dataset=ds, path=tree_path)
    print('Initializing tree took : {}'.format(gem.utils.strfdelta(datetime.now() - start, '%H:%M:%S')))
    print(tree)
Ejemplo n.º 14
0
    def test_set_trainable_layers_correct_usage(self):
        classifier = gem.Classifier(model,
                                    train_dataset.classes('BodyPartExamined'))

        def test_mode(classifier, layer_mode):
            classifier.set_trainable_layers([("fc", layer_mode)])
            for name, param in classifier.module.named_parameters():
                name = '.'.join(name.split('.')[:-1])
                if name == "fc":
                    self.assertEqual(param.requires_grad, layer_mode)

        test_mode(classifier, True)
        test_mode(classifier, False)
Ejemplo n.º 15
0
 def test_save_correct_path(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     os.mkdir(test_classifier_dir)
     try:
         classifier_path = os.path.join(test_classifier_dir, "1.gemclas")
         classifier.save(classifier_path)
         self.assertEqual(os.path.isfile(classifier_path), True)
     finally:
         try:
             os.remove(classifier_path)
         except FileNotFoundError:
             None
         os.rmdir(test_classifier_dir)
Ejemplo n.º 16
0
 def test_from_file_correct_usage(self):
     os.mkdir(test_classifier_dir)
     try:
         self.maxDiff = None
         test_file_path = os.path.join(test_classifier_dir, "1.gemclas")
         classifier = gem.Classifier(
             model, train_dataset.classes('BodyPartExamined'))
         classifier.save(test_file_path)
         classifier = gem.Classifier.from_file(test_file_path)
         self.assertIsInstance(classifier, gem.Classifier)
     finally:
         try:
             os.remove(test_file_path)
         except FileNotFoundError:
             None
         os.rmdir(test_classifier_dir)
Ejemplo n.º 17
0
def bench_classify():
    relevant_labels = [
        'Modality', 'BodyPartExamined', 'StudyDescription', 'SeriesDescription'
    ]
    resnet18 = models.resnet18(pretrained=True)
    ds = gem.DicomoDataset.get_dicomo_dataset('examples',
                                              labels=relevant_labels)
    net = gem.Classifier(resnet18,
                         ds.classes('BodyPartExamined'),
                         enable_cuda=False)

    ds_iter = iter(ds)
    tensors = torch.cat(
        (torch.unsqueeze(next(ds_iter)[0],
                         0), torch.unsqueeze(next(ds_iter)[0], 0)))
    cls = net.classify(tensors)
Ejemplo n.º 18
0
 def test_train_negative_verbosity_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.train(train_dataset, epochs=1, verbosity=-1)
Ejemplo n.º 19
0
 def test_evaluate_correct_usage(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     classifier.evaluate(eval_dataset)
Ejemplo n.º 20
0
 def test_evaluate_wrong_output_policy_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.evaluate(train_dataset, output_policy=[])
Ejemplo n.º 21
0
 def test__init__wrong_optimizer_type(self):
     with self.assertRaises(TypeError):
         gem.Classifier(model,
                        train_dataset.classes('BodyPartExamined'),
                        optimizer=[])
Ejemplo n.º 22
0
 def test_evaluate_wrong_num_workers_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.evaluate(eval_dataset, num_workers="z")
Ejemplo n.º 23
0
 def test__init__wrong_loss_function_type(self):
     with self.assertRaises(TypeError):
         gem.Classifier(model,
                        train_dataset.classes('BodyPartExamined'),
                        loss_function=[])
Ejemplo n.º 24
0
 def test_evaluate_pin_memory_wrong_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.evaluate(eval_dataset, pin_memory="z")
Ejemplo n.º 25
0
 def test_evaluate_def_negative_num_workers(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.evaluate(eval_dataset, num_workers=-10)
Ejemplo n.º 26
0
 def test__init__wrong_layer_config_type(self):
     with self.assertRaises(TypeError):
         gem.Classifier(model,
                        train_dataset.classes('BodyPartExamined'),
                        layer_config=[])
Ejemplo n.º 27
0
 def test_set_trainable_layers_layers_wrong_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.set_trainable_layers({})
Ejemplo n.º 28
0
 def test_save_wrong_path_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     classifier_path = 1
     with self.assertRaises(TypeError):
         classifier.save(classifier_path)
Ejemplo n.º 29
0
 def test_save_invalid_path(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     classifier_path = os.path.join(test_classifier_dir, "1.gemclas")
     with self.assertRaises(FileNotFoundError):
         classifier.save(classifier_path)
Ejemplo n.º 30
0
 def test_evaluate_negative_verbosity_type(self):
     classifier = gem.Classifier(model,
                                 train_dataset.classes('BodyPartExamined'))
     with self.assertRaises(TypeError):
         classifier.evaluate(eval_dataset, verbosity=-1)