示例#1
0
def main():

    opt = 'tangent'

    url, filename = (
        "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt",
        "imagenet_classes.txt")
    urllib.request.urlretrieve(url, filename)
    with open("imagenet_classes.txt", "r") as f:
        categories = [s.strip() for s in f.readlines()]

    model = timm.create_model('vit_base_patch32_384', pretrained=True)
    model.eval()

    config = resolve_data_config({}, model=model)
    transform = create_transform(**config)

    # TODO: FOR PARA PERCORRER IMAGENS DE TESTE
    # TODO: SACAR METRICAS COM O GROUND TRUTH

    img_name = 'class_256.jpg'
    topk_prob, topk_catid = test(img_name,
                                 model,
                                 config,
                                 transform,
                                 opt,
                                 topk=10)
    for i in range(topk_prob.size(0)):
        print(categories[topk_catid[i]], topk_prob[i].item())
示例#2
0
def main():

    with open("/home/paulo/projects/pytorch-image-models/imagenet_classes.txt","r") as f:
        categories = [s.strip() for s in f.readlines()]
    categories = np.array(categories, dtype=object)

    parser = argparse.ArgumentParser(description='ViT')
    parser.add_argument('--data_dir', default='data/s-mnist')
    parser.add_argument('--dataset', default='smnist')
    parser.add_argument('--resume', default='s2r-mnist-sgd-normalmodel_last.pth')
    parser.add_argument('--mode', default='normal')
    parser.add_argument('--image_size', default=60)
    parser.add_argument('--patch_size', default=10)
    parser.add_argument('--num_classes', default=10)
    parser.add_argument('--batch', default=1024)
    parser.add_argument('--cuda', default=True)
    parser.add_argument('--output_csv')
    args = parser.parse_args()

    model = timm.create_model('vit_base_patch32_384', pretrained=True)

    cuda    = args.cuda
    batch   = args.batch
    dataset = {'smnist': SMNIST, 'dvsc': DVSC, 'simagenet': SIMAGENET}

    model.eval()
    if cuda:
        model = model.cuda()

    P=np.array([],dtype=int)
    
    config = resolve_data_config({}, model=model)
    transform = create_transform(**config)

    test_data   = dataset[args.dataset](args.data_dir, 'test',transform, 384, 384, None)
    test_loader = DataLoader(dataset=test_data, batch_size=batch, shuffle=False)

    print("number of files: {}".format(len(test_data.image_ids)))

    print("Test Start")
    for i, data in enumerate(tqdm(test_loader)):
        img, target = data
        if cuda:
            img    = img.cuda()

        with torch.no_grad():
            preds = model(img)
        probabilities = torch.nn.functional.softmax(preds, dim=1)
        preds = torch.argmax(probabilities, dim =1) 
        P = np.concatenate([P,preds.cpu().numpy()])



    pred_class = categories[P]

    result = pd.DataFrame({'pred_class': pred_class})

    result.to_csv(args.output_csv)
示例#3
0
 def __init__(self, args):
     super(SwinT_Image_Encoder, self).__init__()
     self.encoder = timm.create_model('swin_base_patch4_window7_224',
                                      pretrained=args.image_enc_pre_trained,
                                      num_classes=3)
     self.config = resolve_data_config({}, model=self.encoder)
     self.transform = create_transform(**self.config)
     for para in self.encoder.parameters():
         assert para.requires_grad is True
示例#4
0
 def __init__(self, args):
     super(PiT_Image_Encoder, self).__init__()
     self.encoder = timm.create_model('pit_b_224',
                                      pretrained=args.image_enc_pre_trained,
                                      num_classes=args.label_num)
     self.config = resolve_data_config({}, model=self.encoder)
     self.transform = create_transform(**self.config)
     for para in self.encoder.parameters():
         assert para.requires_grad is True
示例#5
0
    def __init__(self, model_id: str):

        self.hf_cfg, self.arch = load_model_config_from_hf(model_id)
        self.model = timm.create_model(f"hf_hub:{model_id}", pretrained=True)
        self.transform = create_transform(
            **resolve_data_config(self.hf_cfg, model=self.model)
        )
        self.model.eval()

        self.top_k = min(self.model.num_classes, 5)

        self.labels = self.hf_cfg.get("labels", None)
        if self.labels is None:
            self.labels = [f"LABEL_{i}" for i in range(self.model.num_classes)]
示例#6
0
def main():

    args = parser.parse_args()

    setup_default_logging(log_path=args.log_path)
    _logger = logging.getLogger('inference')
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # create model
    model = create_model(args.model,
                         num_classes=args.num_classes,
                         in_chans=3,
                         checkpoint_path=args.checkpoint)

    data_config = resolve_data_config(vars(args),
                                      model=model)  # 默认配置 mean, std
    img_transform = create_transform(data_config['input_size'],
                                     is_training=False,
                                     mean=data_config['mean'],
                                     std=data_config['std'],
                                     crop_pct=data_config['crop_pct'])

    model = model.to(device)
    model.eval()

    # confusion matrix
    conf_matrix = np.zeros((args.num_classes, args.num_classes))

    # predict
    class_dir = os.listdir(args.data)
    for cl in class_dir:
        imgs = os.listdir(os.path.join(args.data, cl))
        for img in imgs:
            im = Image.open(os.path.join(args.data, cl, img))
            im1 = img_transform(im).unsqueeze(0)
            im1 = im1.to(device)
            outputs = model(im1)
            idx = torch.max(outputs, 1)[1].tolist()[0]
            probs = torch.nn.functional.softmax(outputs, dim=1)[0].tolist()
            _logger.info('%s: %d, %.2f' %
                         (os.path.join(args.data, cl, img), idx, probs[idx]))
            conf_matrix[int(cl), idx] += 1

    _logger.info(conf_matrix)
示例#7
0
    def create_classification_model(self, model_name):

        if str(model_name.value) == "efficientnet_b3a":
            model_path = os.getenv("HOME") + '/ros2_models/effecient.pth'
            model = create_model('efficientnet_b3a',
                                 False,
                                 model_path,
                                 num_classes=6)

            config = resolve_data_config({}, model=model)
            self.transform = create_transform(**config)

            # sd = torch.load(model_path, map_location=self.device)
            # model = models.resnet18()
            # model.fc = torch.nn.Linear(512, 6)
            # model.load_state_dict(sd['state_dict'])
            return model

        print("Only valid model option currently is efficientnet_b3a")
示例#8
0
def create_loader(
        dataset,
        input_size,
        batch_size,
        is_training=False,
        use_prefetcher=True,
        no_aug=False,
        re_prob=0.,
        re_mode='const',
        re_count=1,
        re_split=False,
        scale=None,
        ratio=None,
        hflip=0.5,
        vflip=0.,
        color_jitter=0.4,
        auto_augment=None,
        num_aug_splits=0,
        interpolation='bilinear',
        mean=IMAGENET_DEFAULT_MEAN,
        std=IMAGENET_DEFAULT_STD,
        num_workers=1,
        distributed=False,
        crop_pct=None,
        collate_fn=None,
        pin_memory=False,
        fp16=False,
        tf_preprocessing=False,
        use_multi_epochs_loader=False,
        repeated_aug=False
):
    re_num_splits = 0
    if re_split:
        # apply RE to second half of batch if no aug split otherwise line up with aug split
        re_num_splits = num_aug_splits or 2
    dataset.transform = create_transform(
        input_size,
        is_training=is_training,
        use_prefetcher=use_prefetcher,
        no_aug=no_aug,
        scale=scale,
        ratio=ratio,
        hflip=hflip,
        vflip=vflip,
        color_jitter=color_jitter,
        auto_augment=auto_augment,
        interpolation=interpolation,
        mean=mean,
        std=std,
        crop_pct=crop_pct,
        tf_preprocessing=tf_preprocessing,
        re_prob=re_prob,
        re_mode=re_mode,
        re_count=re_count,
        re_num_splits=re_num_splits,
        separate=num_aug_splits > 0,
    )

    sampler = None
    if distributed:
        if is_training:
            if repeated_aug:
                print('using repeated_aug')
                num_tasks = get_world_size()
                global_rank = get_rank()
                sampler = RASampler(
                    dataset, num_replicas=num_tasks, rank=global_rank, shuffle=True
                )
            else:
                sampler = torch.utils.data.distributed.DistributedSampler(dataset)
        else:
            # This will add extra duplicate entries to result in equal num
            # of samples per-process, will slightly alter validation results
            sampler = OrderedDistributedSampler(dataset)
    else:
        if is_training and repeated_aug:
            print('using repeated_aug')
            num_tasks = get_world_size()
            global_rank = get_rank()
            sampler = RASampler(
                    dataset, num_replicas=num_tasks, rank=global_rank, shuffle=True
                )

    if collate_fn is None:
        collate_fn = fast_collate if use_prefetcher else torch.utils.data.dataloader.default_collate

    loader_class = torch.utils.data.DataLoader

    if use_multi_epochs_loader:
        loader_class = MultiEpochsDataLoader

    loader = loader_class(
        dataset,
        batch_size=batch_size,
        shuffle=sampler is None and is_training,
        num_workers=num_workers,
        sampler=sampler,
        collate_fn=collate_fn,
        pin_memory=pin_memory,
        drop_last=is_training,
    )
    if use_prefetcher:
        prefetch_re_prob = re_prob if is_training and not no_aug else 0.
        loader = PrefetchLoader(
            loader,
            mean=mean,
            std=std,
            fp16=fp16,
            re_prob=prefetch_re_prob,
            re_mode=re_mode,
            re_count=re_count,
            re_num_splits=re_num_splits
        )

    return loader
示例#9
0
url, filename = (
    "https://miil-public-eu.oss-eu-central-1.aliyuncs.com/model-zoo/ImageNet_21K_P/resources/imagenet21k_tree.pth",
    "imagenet21k_tree.pth")
if not os.path.isfile(filename):
    urllib.request.urlretrieve(url, filename)
args = Namespace()
args.tree_path = filename
semantic_softmax_processor = ImageNet21kSemanticSoftmax(args)
print("done")

############### Loading (ViT) model from timm package ##############
print("initilizing model...")
model = timm.create_model('vit_base_patch16_224_miil_in21k', pretrained=True)
model.eval()
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
print("done")

############## Loading sample image ##############
print("downloading sample image...")
url, filename = ("https://github.com/pytorch/hub/raw/master/images/dog.jpg",
                 "dog.jpg")
if not os.path.isfile(filename):
    urllib.request.urlretrieve(url, filename)
img = Image.open(filename).convert('RGB')
tensor = transform(img).unsqueeze(0)  # transform and add batch dimension
print("done")

############## Doing semantic inference ##############
print("doing semantic infernce...")
labels = []