Delete clipseg/datasets/coco_wrapper.py
Browse files
clipseg/datasets/coco_wrapper.py
DELETED
|
@@ -1,99 +0,0 @@
|
|
| 1 |
-
import pickle
|
| 2 |
-
from types import new_class
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import os
|
| 6 |
-
import json
|
| 7 |
-
|
| 8 |
-
from os.path import join, dirname, isdir, isfile, expanduser, realpath, basename
|
| 9 |
-
from random import shuffle, seed as set_seed
|
| 10 |
-
from PIL import Image
|
| 11 |
-
|
| 12 |
-
from itertools import combinations
|
| 13 |
-
from torchvision import transforms
|
| 14 |
-
from torchvision.transforms.transforms import Resize
|
| 15 |
-
|
| 16 |
-
from datasets.utils import blend_image_segmentation
|
| 17 |
-
from general_utils import get_from_repository
|
| 18 |
-
|
| 19 |
-
COCO_CLASSES = {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'}
|
| 20 |
-
|
| 21 |
-
class COCOWrapper(object):
|
| 22 |
-
|
| 23 |
-
def __init__(self, split, fold=0, image_size=400, aug=None, mask='separate', negative_prob=0,
|
| 24 |
-
with_class_label=False):
|
| 25 |
-
super().__init__()
|
| 26 |
-
|
| 27 |
-
self.mask = mask
|
| 28 |
-
self.with_class_label = with_class_label
|
| 29 |
-
self.negative_prob = negative_prob
|
| 30 |
-
|
| 31 |
-
from third_party.hsnet.data.coco import DatasetCOCO
|
| 32 |
-
|
| 33 |
-
get_from_repository('COCO-20i', ['COCO-20i.tar'])
|
| 34 |
-
|
| 35 |
-
foldpath = join(dirname(__file__), '../third_party/hsnet/data/splits/coco/%s/fold%d.pkl')
|
| 36 |
-
|
| 37 |
-
def build_img_metadata_classwise(self):
|
| 38 |
-
with open(foldpath % (self.split, self.fold), 'rb') as f:
|
| 39 |
-
img_metadata_classwise = pickle.load(f)
|
| 40 |
-
return img_metadata_classwise
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
DatasetCOCO.build_img_metadata_classwise = build_img_metadata_classwise
|
| 44 |
-
# DatasetCOCO.read_mask = read_mask
|
| 45 |
-
|
| 46 |
-
mean = [0.485, 0.456, 0.406]
|
| 47 |
-
std = [0.229, 0.224, 0.225]
|
| 48 |
-
transform = transforms.Compose([
|
| 49 |
-
transforms.Resize((image_size, image_size)),
|
| 50 |
-
transforms.ToTensor(),
|
| 51 |
-
transforms.Normalize(mean, std)
|
| 52 |
-
])
|
| 53 |
-
|
| 54 |
-
self.coco = DatasetCOCO(expanduser('~/datasets/COCO-20i/'), fold, transform, split, 1, False)
|
| 55 |
-
|
| 56 |
-
self.all_classes = [self.coco.class_ids]
|
| 57 |
-
self.coco.base_path = join(expanduser('~/datasets/COCO-20i'))
|
| 58 |
-
|
| 59 |
-
def __len__(self):
|
| 60 |
-
return len(self.coco)
|
| 61 |
-
|
| 62 |
-
def __getitem__(self, i):
|
| 63 |
-
sample = self.coco[i]
|
| 64 |
-
|
| 65 |
-
label_name = COCO_CLASSES[int(sample['class_id'])]
|
| 66 |
-
|
| 67 |
-
img_s, seg_s = sample['support_imgs'][0], sample['support_masks'][0]
|
| 68 |
-
|
| 69 |
-
if self.negative_prob > 0 and torch.rand(1).item() < self.negative_prob:
|
| 70 |
-
new_class_id = sample['class_id']
|
| 71 |
-
while new_class_id == sample['class_id']:
|
| 72 |
-
sample2 = self.coco[torch.randint(0, len(self), (1,)).item()]
|
| 73 |
-
new_class_id = sample2['class_id']
|
| 74 |
-
img_s = sample2['support_imgs'][0]
|
| 75 |
-
seg_s = torch.zeros_like(seg_s)
|
| 76 |
-
|
| 77 |
-
mask = self.mask
|
| 78 |
-
if mask == 'separate':
|
| 79 |
-
supp = (img_s, seg_s)
|
| 80 |
-
elif mask == 'text_label':
|
| 81 |
-
# DEPRECATED
|
| 82 |
-
supp = [int(sample['class_id'])]
|
| 83 |
-
elif mask == 'text':
|
| 84 |
-
supp = [label_name]
|
| 85 |
-
else:
|
| 86 |
-
if mask.startswith('text_and_'):
|
| 87 |
-
mask = mask[9:]
|
| 88 |
-
label_add = [label_name]
|
| 89 |
-
else:
|
| 90 |
-
label_add = []
|
| 91 |
-
|
| 92 |
-
supp = label_add + blend_image_segmentation(img_s, seg_s, mode=mask)
|
| 93 |
-
|
| 94 |
-
if self.with_class_label:
|
| 95 |
-
label = (torch.zeros(0), sample['class_id'],)
|
| 96 |
-
else:
|
| 97 |
-
label = (torch.zeros(0), )
|
| 98 |
-
|
| 99 |
-
return (sample['query_img'],) + tuple(supp), (sample['query_mask'].unsqueeze(0),) + label
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|