Exemplo n.º 1
0
def score_images(model, paths, device=-1, batch_size=1):
    if model is not None and model != 'none': # score each image with the model
        ## set the device
        use_cuda = topaz.cuda.set_device(device)
        ## load the model
        from topaz.model.factory import load_model
        model = load_model(model)
        model.eval()
        model.fill()
        if use_cuda:
            model.cuda()
        scores = topaz.predict.score_stream(model, stream_images(paths), use_cuda=use_cuda
                                           , batch_size=batch_size)
    else: # load scores directly
        scores = stream_images(paths)
    for path,score in zip(paths, scores):
        yield path, score
Exemplo n.º 2
0
def main(args):
    verbose = args.verbose

    # set the number of threads
    num_threads = args.num_threads
    from topaz.torch import set_num_threads
    set_num_threads(num_threads)

    ## set the device
    use_cuda = topaz.cuda.set_device(args.device)

    ## load the model
    from topaz.model.factory import load_model
    model = load_model(args.model)
    model.eval()
    model.fill()

    if use_cuda:
        model.cuda()

    ## make output directory if doesn't exist
    destdir = args.destdir
    if not os.path.exists(destdir):
        os.makedirs(destdir)

    ## load the images and process with the model
    for path in args.paths:
        basename = os.path.basename(path)
        image_name = os.path.splitext(basename)[0]
        image = load_image(path)

        ## process image with the model
        with torch.no_grad():
            X = torch.from_numpy(np.array(
                image, copy=False)).unsqueeze(0).unsqueeze(0)
            if use_cuda:
                X = X.cuda()
            score = model(X).data[0, 0].cpu().numpy()

        im = Image.fromarray(score)
        path = os.path.join(destdir, image_name) + '.tiff'
        if verbose:
            print('# saving:', path)
        im.save(path, 'tiff')
Exemplo n.º 3
0
def make_model(args):
    from topaz.model.factory import get_feature_extractor
    import topaz.model.classifier as C
    from topaz.model.classifier import LinearClassifier

    report('Loading model:', args.model)
    if args.model.endswith('.sav'):  # loading pretrained model
        model = torch.load(args.model)
        model.train()
        return model

    report('Model parameters: units={}, dropout={}, bn={}'.format(
        args.units, args.dropout, args.bn))

    # initialize the model
    units = args.units
    dropout = args.dropout
    bn = args.bn == 'on'
    pooling = args.pooling
    unit_scaling = args.unit_scaling

    arch = args.model
    flag = None
    if args.pretrained:
        # check if model parameters match an available pretrained model
        if arch == 'resnet8' and units == 32:
            flag = 'resnet8_u32'
        elif arch == 'resnet8' and units == 64:
            flag = 'resnet8_u64'
        elif arch == 'resnet16' and units == 32:
            flag = 'resnet16_u32'
        elif arch == 'resnet16' and units == 64:
            flag = 'resnet16_u64'

    if flag is not None:
        from topaz.model.factory import load_model
        report('Loading pretrained model:', flag)
        classifier = load_model(flag)
        classifier.train()
    else:
        feature_extractor = get_feature_extractor(args.model,
                                                  units,
                                                  dropout=dropout,
                                                  bn=bn,
                                                  unit_scaling=unit_scaling,
                                                  pooling=pooling)
        classifier = C.LinearClassifier(feature_extractor)

    ## if the method is generative, create the generative model as well
    generative = None
    if args.autoencoder > 0:
        from topaz.model.generative import ConvGenerator
        ngf = args.ngf
        depth = int(np.log2(classifier.width + 1) - 3)
        generative = ConvGenerator(classifier.latent_dim,
                                   units=ngf,
                                   depth=depth)
        ## attach the generative model
        classifier.generative = generative
        report('Generator: units={}, size={}'.format(ngf, generative.width))

    report('Receptive field:', classifier.width)

    return classifier