| 12 | import torch |
| 13 | import torch.nn.functional as F |
| 14 | class CustomCub2011(Dataset): |
| 15 | base_folder = 'CUB_200_2011/images' |
| 16 | url = 'http://www.vision.caltech.edu/visipedia-data/CUB-200-2011/CUB_200_2011.tgz' |
| 17 | filename = 'CUB_200_2011.tgz' |
| 18 | tgz_md5 = '97eceeb196236b17998738112f37df78' |
| 19 | |
| 20 | def __init__(self, root, train=True, transform=None, target_transform=None, loader=default_loader, download=True): |
| 21 | |
| 22 | self.root = os.path.expanduser(root) |
| 23 | self.transform = transform |
| 24 | self.target_transform = target_transform |
| 25 | |
| 26 | self.loader = loader |
| 27 | self.train = train |
| 28 | |
| 29 | |
| 30 | if download: |
| 31 | self._download() |
| 32 | |
| 33 | if not self._check_integrity(): |
| 34 | raise RuntimeError('Dataset not found or corrupted.' + |
| 35 | ' You can use download=True to download it') |
| 36 | |
| 37 | self.uq_idxs = np.array(range(len(self))) |
| 38 | |
| 39 | def _load_metadata(self): |
| 40 | images = pd.read_csv(os.path.join(self.root, 'CUB_200_2011', 'images.txt'), sep=' ', |
| 41 | names=['img_id', 'filepath']) |
| 42 | image_class_labels = pd.read_csv(os.path.join(self.root, 'CUB_200_2011', 'image_class_labels.txt'), |
| 43 | sep=' ', names=['img_id', 'target']) |
| 44 | train_test_split = pd.read_csv(os.path.join(self.root, 'CUB_200_2011', 'train_test_split.txt'), |
| 45 | sep=' ', names=['img_id', 'is_training_img']) |
| 46 | |
| 47 | data = images.merge(image_class_labels, on='img_id') |
| 48 | data = data.merge(train_test_split, on='img_id') |
| 49 | |
| 50 | name_attribute = pd.read_csv(os.path.join(self.root, 'attributes.txt'), sep=' ', |
| 51 | names=['attribute_id', 'attribute_name']) |
| 52 | from collections import defaultdict |
| 53 | dict_attribute = defaultdict(list) |
| 54 | for _i, _name in zip(name_attribute['attribute_id'], name_attribute['attribute_name']): |
| 55 | dict_attribute[_name.split('::')[0]].append(_name.split('::')[1]) |
| 56 | |
| 57 | names_ = list(dict_attribute.keys()) |
| 58 | processed_attribute_file = os.path.join(self.root,'CUB_200_2011','processed_attributes.txt') |
| 59 | A_all = pd.read_csv(processed_attribute_file, sep=' ', names=names_) |
| 60 | A_all.insert(0, 'img_id', list(range(1, len(A_all)+1))) |
| 61 | self.data = data.merge(A_all, on='img_id') |
| 62 | self.dict_attribute = dict_attribute |
| 63 | class_attributes_file = os.path.join(self.root,'CUB_200_2011','attributes', |
| 64 | 'class_attribute_labels_continuous.txt') |
| 65 | C_A = np.zeros((200, 312)) |
| 66 | class_attr_rf = open(class_attributes_file, 'r') |
| 67 | i = 0 |
| 68 | for line in class_attr_rf.readlines(): |
| 69 | strs = line.strip().split(' ') |
| 70 | for j in range(len(strs)): |
| 71 | C_A[i][j] = 0.0 if strs[j] == '0.0' else float(strs[j]) * 0.01 |
no outgoing calls
no test coverage detected