Ejemplo n.º 1
0
def get_predictions(p, dataloader, model, return_features=False):
    # Make predictions on a dataset with neighbors
    model.eval()
    predictions = [[] for _ in range(p['num_heads'])]
    probs = [[] for _ in range(p['num_heads'])]
    targets = []
    if return_features:
        ft_dim = get_feature_dimensions_backbone(p)
        features = torch.zeros((len(dataloader.sampler), ft_dim)).cuda()

    if isinstance(dataloader.dataset,
                  NeighborsDataset):  # Also return the neighbors
        key_ = 'anchor'
        include_neighbors = True
        neighbors = []

    else:
        key_ = 'image'
        include_neighbors = False

    ptr = 0
    for batch in dataloader:
        images = batch[key_].cuda(non_blocking=True)
        bs = images.shape[0]
        res = model(images, forward_pass='******')
        output = res['output']
        if return_features:
            features[ptr:ptr + bs] = res['features']
            ptr += bs
        for i, output_i in enumerate(output):
            predictions[i].append(torch.argmax(output_i, dim=1))
            probs[i].append(F.softmax(output_i, dim=1))
        targets.append(batch['target'])
        if include_neighbors:
            neighbors.append(batch['possible_neighbors'])

    predictions = [torch.cat(pred_, dim=0).cpu() for pred_ in predictions]
    probs = [torch.cat(prob_, dim=0).cpu() for prob_ in probs]
    targets = torch.cat(targets, dim=0)

    if include_neighbors:
        neighbors = torch.cat(neighbors, dim=0)
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets,
            'neighbors': neighbors
        } for pred_, prob_ in zip(predictions, probs)]

    else:
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets
        } for pred_, prob_ in zip(predictions, probs)]

    if return_features:
        return out, features.cpu()
    else:
        return out
Ejemplo n.º 2
0
def get_predictions(p,
                    dataloader,
                    model,
                    return_features=False,
                    return_thumbnails=False):
    # Make predictions on a dataset with neighbors
    model.eval()
    predictions = [[] for _ in range(p['num_heads'])]
    probs = [[] for _ in range(p['num_heads'])]
    targets = []
    if return_features:
        ft_dim = get_feature_dimensions_backbone(p)
        features = torch.zeros((len(dataloader.sampler), ft_dim)).cuda()

    if return_thumbnails:
        thumbnails = torch.zeros((len(dataloader.sampler), 3, 64, 64)).cuda()

    if isinstance(dataloader.dataset,
                  NeighborsDataset):  # Also return the neighbors
        key_ = 'anchor'
        include_neighbors = True
        neighbors = []

    else:
        key_ = 'image'
        include_neighbors = False

    ptr = 0
    for batch in dataloader:
        images = batch[key_].cuda(non_blocking=True)
        bs = images.shape[0]

        if p['setup'] == 'simpred':
            images2 = batch['neighbor'].cuda(non_blocking=True)
            res = model(images, images2, forward_pass='******')
        else:
            res = model(images, forward_pass='******')
        output = res['output']

        if return_features:
            if p['setup'] == 'simpred':
                features[ptr:ptr + bs] = res['features'][0]
            else:
                features[ptr:ptr + bs] = res['features']
        if return_thumbnails:
            means = p['transformation_kwargs']['normalize']['mean']
            stds = p['transformation_kwargs']['normalize']['std']
            for i in range(3):
                images[:, i] = (images[:, i] * stds[i]) + means[i]
            thumbnails[ptr:ptr + bs] = torch.nn.functional.interpolate(
                images, size=(64, 64), mode='bicubic', align_corners=False)
        if return_features or return_thumbnails:
            ptr += bs

        for i, output_i in enumerate(output):
            if p['setup'] == 'simpred':
                predictions[i].append((output_i > 0.5).double())
            else:
                predictions[i].append(torch.argmax(output_i, dim=1))
            probs[i].append(output_i)
        targets.append(batch['target'])
        if include_neighbors:
            neighbors.append(batch['possible_neighbors'])

    predictions = [torch.cat(pred_, dim=0).cpu() for pred_ in predictions]
    probs = [torch.cat(prob_, dim=0).cpu() for prob_ in probs]
    targets = torch.cat(targets, dim=0)

    if include_neighbors:
        neighbors = torch.cat(neighbors, dim=0)
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets,
            'neighbors': neighbors
        } for pred_, prob_ in zip(predictions, probs)]

    else:
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets
        } for pred_, prob_ in zip(predictions, probs)]

    if return_thumbnails:
        return out, features.cpu(), thumbnails.cpu()
    elif return_features:
        return out, features.cpu()
    else:
        return out
def get_predictions(p,
                    dataloader,
                    model,
                    return_features=False,
                    download=None):
    # Make predictions on a dataset with neighbors
    model.eval()
    predictions = [[] for _ in range(p['num_heads'])]
    probs = [[] for _ in range(p['num_heads'])]
    targets = []
    url2label = {}
    if return_features:
        ft_dim = get_feature_dimensions_backbone(p)
        features = torch.zeros((len(dataloader.sampler), ft_dim)).cuda()

    if isinstance(dataloader.dataset,
                  NeighborsDataset):  # Also return the neighbors
        key_ = 'anchor'
        include_neighbors = True
        neighbors = []

    else:
        key_ = 'image'
        include_neighbors = False

    ptr = 0
    for batch in dataloader:
        images = batch[key_].cuda(non_blocking=True)
        #import ipdb; ipdb.set_trace()
        urls = batch['meta']['url']
        bs = images.shape[0]
        res = model(images, forward_pass='******')
        output = res['output']
        if return_features:
            features[ptr:ptr + bs] = res['features']
            ptr += bs
        for i, output_i in enumerate(output):
            labels = torch.argmax(output_i, dim=1)
            if download is not None:
                for img_idx, url in enumerate(urls):
                    image_name = url.split("/")[-1]
                    response = requests.get(url)
                    url2label[url] = labels[img_idx].item()
                    out_dir = osp.join(download, str(labels[img_idx].item()))
                    if not os.path.exists(out_dir):
                        print(f"Creating {out_dir}...")
                        os.mkdir(out_dir)

                    with open(osp.join(out_dir, image_name), 'wb') as f:
                        f.write(response.content)

            predictions[i].append(labels)
            #import ipdb; ipdb.set_trace()
            probs[i].append(F.softmax(output_i, dim=1))
        targets.append(batch['target'])
        if include_neighbors:
            neighbors.append(batch['possible_neighbors'])

    with open("/mydata/FashionClassification/data/product_data.json") as f:
        data = json.load(f)

    for i, item in enumerate(data):
        url = item["image_url"]
        label = url2label[url]
        data[i]["label"] = int(label)

    with open('/mydata/FashionClassification/data/product_data_scan.json',
              'w') as file:
        json.dump(data, file, indent=4)

    predictions = [torch.cat(pred_, dim=0).cpu() for pred_ in predictions]
    probs = [torch.cat(prob_, dim=0).cpu() for prob_ in probs]
    targets = torch.cat(targets, dim=0)

    if include_neighbors:
        neighbors = torch.cat(neighbors, dim=0)
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets,
            'neighbors': neighbors
        } for pred_, prob_ in zip(predictions, probs)]

    else:
        out = [{
            'predictions': pred_,
            'probabilities': prob_,
            'targets': targets
        } for pred_, prob_ in zip(predictions, probs)]

    if return_features:
        return out, features.cpu()
    else:
        return out