My code looks something like the below. I am working on the UCF101 dataset with only 10 classes, labelled from 0 to 9. But after weighted random sampler, the only classes that are fetched belong to the class 0, 1 or 2. Can you help me out here?
from torch.utils.data.sampler import WeightedRandomSampler
class_wts = len(label_train)/np.unique(label_train, return_counts=True)[1]
print(class_wts.T)
print(np.unique(label_train.T[0], return_counts=True))
wts = np.array([class_wts.T[int(t)-1] for t in label_train.T[0]])
print(wts)
wtd_resampler = WeightedRandomSampler(wts, len(wts))
image_dataloader = DataLoader(image_dataset, batch_size=32, num_workers=4, sampler=wtd_resampler)
for i,sample in enumerate(image_dataloader):
# npimg = sample['image'].numpy()
# for j in range(4):
# plt.imshow(np.transpose(npimg[j], (1,2,0)), interpolation='nearest')
# plt.show()
print(sample['Label'])
# print(i,sample['image'].shape,sample['img_path'],sample['Label'])
if i>5:
break
Output of the code above:
[ 8.53846154 8.53846154 9.14117647 9.14117647 11.77272727
9.83544304 11.77272727 9.25 11.77272727 12.140625 ]
(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.], dtype=float32), array([910, 910, 850, 850, 660, 790, 660, 840, 660, 640]))
[ 8.53846154 8.53846154 8.53846154 ..., 12.140625 12.140625
12.140625 ]
1
2
0
1
0
1
2
0
1
2
1
1
1
2
0
1
2
1
2
0
1
0
1
1
1
2
2
0
0
2
0
2
[torch.DoubleTensor of size 32]
1
0
1
0
1
1
2
1
2
2
1
1
2
0
2
0
1
2
1
2
2
0
1
1
0
1
0
0
0
1
0
0
[torch.DoubleTensor of size 32]