def _generate_samples_from_descr(categories, attributes, n_samples_per_class, augment): use_cat_id, attributes = attributes assert use_cat_id and not attributes, \ "usage of attributes isn't supporte in v1." samples = [] labels = [] for i, cat_concepts in enumerate(categories): mixture = ComposedConcept(cat_concepts, id=None) cat_samples = [] cat_labels = [] for s_id, n in enumerate(n_samples_per_class): split_samples, split_attrs = mixture._get_samples(n, attributes, split_id=s_id) if s_id in augment: split_samples = augment_samples(split_samples) split_labels = torch.Tensor().long() cat_id = torch.tensor([i]).expand(split_samples.shape[0], 1) split_labels = torch.cat([split_labels, cat_id], dim=1) cat_samples.append(split_samples) cat_labels.append(split_labels) samples.append(cat_samples) labels.append(cat_labels) if torch.is_tensor(samples[0][0]): cat_func = torch.cat else: cat_func = np.concatenate samples = (cat_func(split) for split in zip(*samples)) labels = (torch.cat(split) for split in zip(*labels)) return samples, labels
def _generate_samples_from_descr(categories, attributes, n_samples_per_class, augment): use_cat_id, attributes = attributes assert use_cat_id or attributes, 'Each task should at least use the ' \ 'category id or an attribute as labels' if not use_cat_id: all_concepts = np.array( [concept for cat in categories for concept in cat]) all_attrs = np.array([c.attrs for c in all_concepts]) selected_attr = all_attrs[:, attributes[0]] categories = [tuple(all_concepts[selected_attr == val]) for val in np.unique(selected_attr)] if use_cat_id or isinstance(all_concepts[0], AtomicConcept): samples = [] labels = [] for i, cat_concepts in enumerate(categories): mixture = ComposedConcept(cat_concepts, id=None) cat_samples = [] cat_labels = [] for s_id, n in enumerate(n_samples_per_class): split_samples, split_attrs = mixture._get_samples(n, attributes, split_id=s_id) if s_id in augment: split_samples = augment_samples(split_samples) split_labels = torch.Tensor().long() if use_cat_id: cat_id = torch.tensor([i]).expand(split_samples.shape[0], 1) split_labels = torch.cat([split_labels, cat_id], dim=1) if attributes: raise NotImplementedError('Attrs aren\'t supported ' 'anymore') split_labels = torch.cat([split_labels, split_attrs], dim=1) cat_samples.append(split_samples) cat_labels.append(split_labels) samples.append(cat_samples) labels.append(cat_labels) if torch.is_tensor(samples[0][0]): cat_func = torch.cat else: cat_func = np.concatenate samples = (cat_func(split) for split in zip(*samples)) labels = (torch.cat(split) for split in zip(*labels)) else: # Grouping the concepts by attribute value to create the categories all_concepts = np.array( [concept for cat in categories for concept in cat]) samples, labels = get_samples_using_attrs(all_concepts, attributes, n_samples_per_class) return samples, labels
def _generate_samples_from_descr(categories, attributes, n_samples_per_class, augment, rnd): use_cat_id, attributes = attributes assert use_cat_id and not attributes, \ "usage of attributes isn't supporte in v1." samples = [] labels = [] for i, cat_concepts in enumerate(categories): mixture = ComposedConcept(cat_concepts, id=None) cat_samples = [] cat_labels = [] for s_id, n in enumerate(n_samples_per_class): if isinstance(n, list): n_samples, n_labeled = n[0], n[1] else: n_samples, n_labeled = n, n split_samples, split_attrs = mixture._get_samples(n_samples, attributes, split_id=s_id, rng=rnd) if s_id in augment: split_samples = augment_samples(split_samples) split_labels = torch.Tensor().long() cat_id = torch.tensor([i]).expand(split_samples.shape[0], 1) split_labels = torch.cat([split_labels, cat_id], dim=1) #Create unlabeled samples n_unlabeled = len(split_samples) - n_labeled if n_unlabeled > 0: idx_unlabeled = rnd.choice(list(range(len(split_samples))), n_unlabeled, replace=False) split_labels[ idx_unlabeled] = -1 #mark unlabeled samples with -1 cat_samples.append(split_samples) cat_labels.append(split_labels) samples.append(cat_samples) labels.append(cat_labels) if torch.is_tensor(samples[0][0]): cat_func = torch.cat else: cat_func = np.concatenate samples = (cat_func(split) for split in zip(*samples)) labels = (torch.cat(split) for split in zip(*labels)) return samples, labels