samples for labeled data (sampling with balanced ratio over classes)
(args, data, target,
num_labels, num_classes,
index=None, name=None)
| 29 | |
| 30 | |
| 31 | def sample_labeled_data(args, data, target, |
| 32 | num_labels, num_classes, |
| 33 | index=None, name=None): |
| 34 | ''' |
| 35 | samples for labeled data |
| 36 | (sampling with balanced ratio over classes) |
| 37 | ''' |
| 38 | assert num_labels % num_classes == 0 |
| 39 | if not index is None: |
| 40 | index = np.array(index, dtype=np.int32) |
| 41 | return data[index], target[index], index |
| 42 | |
| 43 | dump_path = os.path.join(args.save_dir, args.save_name, 'sampled_label_idx.npy') |
| 44 | |
| 45 | if os.path.exists(dump_path): |
| 46 | lb_idx = np.load(dump_path) |
| 47 | lb_data = data[lb_idx] |
| 48 | lbs = target[lb_idx] |
| 49 | return lb_data, lbs, lb_idx |
| 50 | |
| 51 | samples_per_class = int(num_labels / num_classes) |
| 52 | |
| 53 | lb_data = [] |
| 54 | lbs = [] |
| 55 | lb_idx = [] |
| 56 | for c in range(num_classes): |
| 57 | idx = np.where(target == c)[0] |
| 58 | idx = np.random.choice(idx, samples_per_class, False) |
| 59 | lb_idx.extend(idx) |
| 60 | |
| 61 | lb_data.extend(data[idx]) |
| 62 | lbs.extend(target[idx]) |
| 63 | |
| 64 | np.save(dump_path, np.array(lb_idx)) |
| 65 | |
| 66 | return np.array(lb_data), np.array(lbs), np.array(lb_idx) |
| 67 | |
| 68 | |
| 69 | def get_sampler_by_name(name): |
no test coverage detected