Exemplo n.º 1
0
def get_labels(report):
    parser = ArgParser()
    args = parser.parse_args()

    loader = SingleLoader(report)

    extractor = Extractor(args.mention_phrases_dir,
                          args.unmention_phrases_dir,
                          verbose=args.verbose)
    classifier = Classifier(args.pre_negation_uncertainty_path,
                            args.negation_path,
                            args.post_negation_uncertainty_path,
                            verbose=args.verbose)
    aggregator = Aggregator(CATEGORIES, verbose=args.verbose)

    # Load reports in place.
    loader.load()
    # Extract observation mentions in place.
    extractor.extract(loader.collection)
    # Classify mentions in place.
    classifier.classify(loader.collection)
    # Aggregate mentions to obtain one set of labels for each report.
    labels = aggregator.aggregate(loader.collection)

    return labels
Exemplo n.º 2
0
 def parser(self):
     """The associated ArgParser object."""
     if self._parser:
         return self._parser
     parser = ArgParser()
     parser.host = self.host
     parser.add_parsers()
     self._parser = parser
     return self._parser
Exemplo n.º 3
0
    def __init__(self):
        super().__init__()
        # initial hyperparameters
        self.learning_rate: float = DEFAULTS["MOMENTUM"]
        self.momentum: float = DEFAULTS["LEARNING_RATE"]
        self.weight_decay: float = DEFAULTS["WEIGHT_DECAY"]

        self.print_after_batch: int = DEFAULTS["PRINT_AFTER_BATCH"]
        self.batch_size: int = DEFAULTS["BATCH_SIZE"]
        self.epochs: int = DEFAULTS["EPOCHS"]
        self.no_cuda: bool = DEFAULTS["NO_CUDA"]

        # Global save
        self.no_save: bool = DEFAULTS["NO_SAVE"]
        self.root_dir: str = DEFAULTS["ROOT_DIR"]

        self.no_save_savepoint: bool = DEFAULTS["NO_SAVE_SAVEPOINT"]
        self.savepoint_dir: str = DEFAULTS["SAVEPOINT_DIR"]
        # Maximum savepoints at any given time
        # Oldest savepoint will be deleted if limit is reached
        self.max_savepoints: int = DEFAULTS["MAX_SAVEPOINTS"]

        # Should whitening matrix, mean_vector not be saved for whitening?
        self.no_save_prep: bool = DEFAULTS["NO_SAVE_PREP"]
        self.prep_dir: str = DEFAULTS["PREP_DIR"]

        # Where should data be loaded from?
        self.data_dir: str = DEFAULTS["DATA_DIR"]

        # Should the net train?
        self.train: bool = DEFAULTS["TRAIN"]
        # Should the net perform and log evaluations?
        self.evaluate: bool = DEFAULTS["EVALUATE"]

        parsed = ArgParser().parse_args().__dict__.items()
        self_keys = self.__dict__.keys()
        for (key, value) in parsed:
            assert key in self_keys, f"Key {key} from parser not found in Config object!{self_keys}"
            self[key] = value if value else self[key]

        # Having both values False makes no sense (this is the default too)
        # It is assumed that values have been omitted and both are set to True
        if not self.train and not self.evaluate:
            self.train = True
            self.evaluate = True

        # join paths with root path
        self.data_dir = os.path.join(self.root_dir, self.data_dir)
        self.prep_dir = os.path.join(self.root_dir, self.prep_dir)
        self.savepoint_dir = os.path.join(self.root_dir, self.savepoint_dir)

        # apply global save option if neccessary
        if self.no_save:
            self.no_save_prep = True
            self.no_save_savepoint = True
Exemplo n.º 4
0
def test(args):

    model_fn = models.__dict__[args_.model]
    model = model_fn(args.num_classes)
    model = nn.DataParallel(model, args.gpu_ids)

    ckpt_info = ModelSaver.load_model(args.ckpt_path, model)
    args.start_epoch = ckpt_info['epoch'] + 1
    model = model.to(args.device)
    model.eval()

    _, test_loader, _ = get_cifar_loaders(args.batch_size, args.num_workers)
    logger = TestLogger(args)

    logger.start_epoch()
    for inputs, labels in test_loader:
        logger.start_iter()

        with torch.set_grad_enabled(True):
            # Forward
            logits = model.forward(inputs.to(args.device))

        logger.end_iter(inputs, labels, logits)
    logger.end_epoch()


if __name__ == '__main__':
    parser = ArgParser()
    args_ = parser.parse_args()
    test(args_)
Exemplo n.º 5
0
 def main(self):   
     p = ArgParser()
     (options, in_path, out_path) = p.parse_args()
     self.options = options
     obj = self.process_file(in_path, out_path)
Exemplo n.º 6
0
                    out += '# %d "%s"\n' % (current_line + 1, current_file)
                elif is_asm:
                    line_mapping += [(current_file, current_line)]
                    asm += line
                else:
                    out += line
                current_line += 1

        # We have some left-over assembly code: missing an end marker
        if len(asm) > 0:
            raise Exception("Leftover assembler code. Did you miss a %s?" % self.marker[1])

        # Write result
        f = open(out_path, 'w')
        f.write(out)
        f.close()


if __name__ == '__main__':
    p = ArgParser()
    (options, in_path, out_path) = p.parse_args()
    if options.config == "x86" or options.config == "ia32":
        arch = Arch(X86)
    elif options.config == "arm":
        arch = Arch(ARM)
    else:
        raise Exception("Invalid configuration (-c): should be x86, ia32 or arm.")
    asm_rewriter = AssemblyRewriter(arch)
    asm_rewriter.process_file(in_path, out_path)
    sys.exit(0)
Exemplo n.º 7
0
def get_labels(report):
    parser = ArgParser()
    args = parser.parse_args()

    loader = SingleLoader(report)

    extractor = Extractor(args.mention_phrases_dir,
                          args.unmention_phrases_dir,
                          verbose=args.verbose)
    classifier = Classifier(args.pre_negation_uncertainty_path,
                            args.negation_path,
                            args.post_negation_uncertainty_path,
                            verbose=args.verbose)
    aggregator = Aggregator(CATEGORIES, verbose=args.verbose)

    # Load reports in place.
    loader.load()
    # Extract observation mentions in place.
    extractor.extract(loader.collection)
    # Classify mentions in place.
    classifier.classify(loader.collection)
    # Aggregate mentions to obtain one set of labels for each report.
    labels = aggregator.aggregate(loader.collection)

    return labels


if __name__ == "__main__":
    parser = ArgParser()
    label(parser.parse_args())
Exemplo n.º 8
0
        # Evaluate on validation set
        val_loss = evaluate(model, test_loader, loss_fn, device=args.device)
        logger.write('[epoch {}]: val_loss: {:.3g}'.format(
            logger.epoch, val_loss))
        logger.write_summaries({'loss': val_loss}, phase='val')
        if logger.epoch in args.save_epochs:
            saver.save(logger.epoch, val_loss)

        logger.end_epoch()
        scheduler.step()


def evaluate(model, data_loader, loss_fn, device='cpu'):
    """Evaluate the model."""
    model.eval()
    losses = []
    print('Evaluating model...')
    for inputs, labels in tqdm(data_loader):
        with torch.no_grad():
            # Forward
            outputs = model(inputs.to(device))
            loss = loss_fn(outputs, labels.to(device))
            losses.append(loss.item())

    return np.mean(losses)


if __name__ == '__main__':
    parser = ArgParser()
    train(parser.parse_args())
Exemplo n.º 9
0
 def main(self):   
     p = ArgParser()
     (options, in_path, out_path) = p.parse_args()
     self.options = options
     obj = self.process_file(in_path, out_path)
Exemplo n.º 10
0
Arquivo: parse.py Projeto: philix/Fbt
                    line_mapping += [(current_file, current_line)]
                    asm += line
                else:
                    out += line
                current_line += 1

        # We have some left-over assembly code: missing an end marker
        if len(asm) > 0:
            raise Exception("Leftover assembler code. Did you miss a %s?" %
                            self.marker[1])

        # Write result
        f = open(out_path, 'w')
        f.write(out)
        f.close()


if __name__ == '__main__':
    p = ArgParser()
    (options, in_path, out_path) = p.parse_args()
    if options.config == "x86" or options.config == "ia32":
        arch = Arch(X86)
    elif options.config == "arm":
        arch = Arch(ARM)
    else:
        raise Exception(
            "Invalid configuration (-c): should be x86, ia32 or arm.")
    asm_rewriter = AssemblyRewriter(arch)
    asm_rewriter.process_file(in_path, out_path)
    sys.exit(0)
Exemplo n.º 11
0
 def create_parser(self):
     """Returns an argument parser."""
     parser = ArgParser()
     parser.host = self.host
     parser.add_parsers()
     return parser