Пример #1
0
def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('model_name',
                        choices=('resnet50', 'resnet101', 'resnet152'))
    parser.add_argument('caffemodel')
    parser.add_argument('output', nargs='?', default=None)
    args = parser.parse_args()

    caffemodel = CaffeFunction(args.caffemodel)
    if args.model_name == 'resnet50':
        model = ResNet50(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet50(caffemodel, model)
    elif args.model_name == 'resnet101':
        model = ResNet101(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet101(caffemodel, model)
    elif args.model_name == 'resnet152':
        model = ResNet152(pretrained_model=None, n_class=1000, mode='he')
        model(np.zeros((1, 3, 224, 224), dtype=np.float32))
        _transfer_resnet152(caffemodel, model)

    if args.output is None:
        output = '{}_imagenet_convert.npz'.format(args.model_name)
    else:
        output = args.output
    chainer.serializers.save_npz(output, model)
Пример #2
0
def main():
    parser = argparse.ArgumentParser(
        description='Learning convnet from ILSVRC2012 dataset')
    parser.add_argument('val', help='Path to root of the validation dataset')
    parser.add_argument(
        '--model', choices=('vgg16', 'resnet50', 'resnet101', 'resnet152'))
    parser.add_argument('--pretrained_model', default='imagenet')
    parser.add_argument('--gpu', type=int, default=-1)
    parser.add_argument('--batchsize', type=int, default=32)
    parser.add_argument('--crop', choices=('center', '10'), default='center')
    parser.add_argument('--resnet_mode', default='he')
    args = parser.parse_args()

    dataset = DirectoryParsingLabelDataset(args.val)
    label_names = directory_parsing_label_names(args.val)
    n_class = len(label_names)
    iterator = iterators.MultiprocessIterator(
        dataset, args.batchsize, repeat=False, shuffle=False,
        n_processes=6, shared_mem=300000000)

    if args.model == 'vgg16':
        extractor = VGG16(n_class, args.pretrained_model)
    elif args.model == 'resnet50':
        extractor = ResNet50(
            n_class, args.pretrained_model, mode=args.resnet_mode)
    elif args.model == 'resnet101':
        extractor = ResNet101(
            n_class, args.pretrained_model, mode=args.resnet_mode)
    elif args.model == 'resnet152':
        extractor = ResNet152(
            n_class, args.pretrained_model, mode=args.resnet_mode)
    model = FeaturePredictor(
        extractor, crop_size=224, scale_size=256, crop=args.crop)

    if args.gpu >= 0:
        chainer.cuda.get_device(args.gpu).use()
        model.to_gpu()

    print('Model has been prepared. Evaluation starts.')
    in_values, out_values, rest_values = apply_to_iterator(
        model.predict, iterator, hook=ProgressHook(len(dataset)))
    del in_values

    pred_probs, = out_values
    gt_labels, = rest_values

    accuracy = F.accuracy(
        np.array(list(pred_probs)), np.array(list(gt_labels))).data
    print()
    print('Top 1 Error {}'.format(1. - accuracy))