in matching_network.py [0:0]
def encode_lowshot_trainset(model, base_classes, train_file_handle, novel_idx, lowshotn, num_base=100):
all_labels = train_file_handle['all_labels'][...]
all_feats = train_file_handle['all_feats']
feats = []
Y = []
#for each base class, randomly pick 100 examples
for i, k in enumerate(base_classes):
idx = np.where(all_labels==k)[0]
idx = np.sort(np.random.choice(idx, num_base, replace=False))
feats.append(all_feats[list(idx)])
Y_this = np.zeros((num_base,1000))
Y_this[:,k] = 1
Y.append(Y_this)
#next get the novel classes
sorted_novel_idx = np.sort(novel_idx.reshape(-1))
novel_feats = all_feats[list(sorted_novel_idx)]
novel_labels = all_labels[sorted_novel_idx]
Y_novel = np.zeros((novel_feats.shape[0],1000))
Y_novel[np.arange(novel_feats.shape[0]), novel_labels] = 1
num_repeats = int(np.ceil(float(num_base)/float(lowshotn)))
novel_feats = np.tile(novel_feats, (num_repeats,1))
Y_novel = np.tile(Y_novel, (num_repeats,1))
feats.append(novel_feats)
Y.append(Y_novel)
feats = np.concatenate(feats, axis=0)
Y = np.concatenate(Y, axis=0)
model = model.cuda()
feats = Variable(torch.Tensor(feats).cuda())
Y = Variable(torch.Tensor(Y).cuda())
G, G_norm = model.encode_training_set(feats)
print(novel_feats.shape, len(base_classes))
return G, G_norm, Y