def __init__(self, ensemble=True, split=None, model_id=None):
        """
        Constructor of COVID model evaluator class.
        
        Arguments:
            ensemble {str} -- Whether the model ensemble is used.
        """
        self.root = os.path.join('/', *DIR_PATH.split('/')[:-1])
        self.split = split
        self.ensemble = ensemble
        if model_id is None:
            self.model_id = 'vgg_base'
        elif model_id not in MODEL_FACTORY.keys():
            raise ValueError(
                f'Wrong model {model_id}. Options are:{MODEL_FACTORY.keys()}')
        else:
            self.model_id = model_id

        if ensemble:
            # retores 5 weight paths
            self.weights_paths = [
                os.path.join(self.root, 'trained_models', 'fold_' + str(fold),
                             "variables", "variables")
                for fold in range(NUM_FOLDS)
            ]
        else:
            if split is None or split < 0 or split > 4:
                raise ValueError(f'Provide split between 0 and 4, not {split}')
            fold = split
            self.weights_paths = [
                os.path.join(self.root, 'trained_models', 'fold_' + str(fold),
                             "variables", "variables")
            ]

        self.class_mappings = ['covid', 'pneunomia', 'regular']
        self.models = [
            MODEL_FACTORY[self.model_id]()
            for _ in range(len(self.weights_paths))
        ]

        # restore weights
        try:
            for model, path in zip(self.models, self.weights_paths):
                model.load_weights(path)
        except Exception:
            raise Exception('Error in model restoring.')

        print(f'Model restored. Class mappings are {self.class_mappings}')
# Initialize hyperparameters
DATA_DIR = args['data_dir']
MODEL_NAME = args['model_name']
FOLD = args['fold']
MODEL_DIR = os.path.join(args['model_dir'], MODEL_NAME, f'fold_{FOLD}')
LR = args['learning_rate']
EPOCHS = args['epochs']
BATCH_SIZE = args['batch_size']
MODEL_ID = args['model_id']
TRAINABLE_BASE_LAYERS = args['trainable_base_layers']
IMG_WIDTH, IMG_HEIGHT = args['img_width'], args['img_height']
LOG_SOFTMAX = args['log_softmax']
HIDDEN_SIZE = args['hidden_size']

# Check if model class exists
if MODEL_ID not in MODEL_FACTORY.keys():
    raise ValueError(
        f'Model {MODEL_ID} not implemented. Choose from {MODEL_FACTORY.keys()}'
    )

if not os.path.exists(MODEL_DIR):
    os.makedirs(MODEL_DIR)
print(f'Model parameters: {args}')
# grab the list of images in our dataset directory, then initialize
# the list of data (i.e., images) and class images
print('Loading images...')
imagePaths = list(paths.list_images(DATA_DIR))
data = []
labels = []

print(f'selected fold: {FOLD}')
Exemplo n.º 3
0
    def __init__(self,
                 weights_dir=None,
                 ensemble=True,
                 split=None,
                 model_id=None,
                 num_classes=3,
                 mc_dropout: bool = False,
                 test_augment: bool = False):
        """
        Constructor of COVID model evaluator class.
        
        Arguments:
            ensemble {str} -- Whether the model ensemble is used.
            num_classes: must be 3 or 4, how many classes the model was
            trained on
        """
        self.root = os.path.join('/', *DIR_PATH.split('/')[:-1])
        if weights_dir is None:
            weights_dir = os.path.join(self.root, "trained_models")
        self.split = split
        self.ensemble = ensemble
        assert num_classes == 3 or num_classes == 4, "must be 3 or 4 classes"
        if model_id is None:
            self.model_id = 'vgg_base'
        elif model_id not in MODEL_FACTORY.keys():
            raise ValueError(
                f'Wrong model {model_id}. Options are:{MODEL_FACTORY.keys()}')
        else:
            self.model_id = model_id

        if ensemble:
            # retores 5 weight paths
            self.weights_paths = [
                os.path.join(weights_dir, 'fold_' + str(fold), "best_weights",
                             "variables", "variables")
                for fold in range(NUM_FOLDS)
            ]
        else:
            if split is None or split < 0 or split > 4:
                raise ValueError(f'Provide split between 0 and 4, not {split}')
            fold = split
            self.weights_paths = [
                os.path.join(weights_dir, 'fold_' + str(fold), "best_weights",
                             "variables", "variables")
            ]

        self.class_mappings = CLASS_MAPPING[num_classes]
        self.models = [
            MODEL_FACTORY[self.model_id](num_classes=num_classes,
                                         mc_dropout=mc_dropout)
            for _ in range(len(self.weights_paths))
        ]
        self.mc_dropout = mc_dropout
        self.augmentor = ImageDataGenerator(rotation_range=10,
                                            fill_mode='nearest',
                                            horizontal_flip=True,
                                            vertical_flip=True,
                                            width_shift_range=0.1,
                                            height_shift_range=0.1)

        # restore weights
        try:
            for model, path in zip(self.models, self.weights_paths):
                model.load_weights(path)
        except Exception:
            raise Exception('Error in model restoring.')

        print(f'Model restored. Class mappings are {self.class_mappings}')