TypeError: 'float' object is not subscriptable when creating a weighted sampler

peony: <p>Hello everyone, I am working on action recognition and my train folder is structured like this:</p>
<p>train<br>
-class 0<br>
–video1folder<br>
—video1 frame1.npy<br>
—video1 frame2.npy<br>
—video1 frame3.npy<br>
–video2folder<br>
—video2 frame1.npy<br>
—video2 frame2.npy<br>
—video2 frame3.npy<br>
-class 1<br>
–video1folder<br>
—video1 frame1.npy<br>
—video1 frame2.npy<br>
—video1 frame3.npy</p>
<p>Below is the code for my weighted sampler so far:</p>
<pre><code class="lang-auto">    class_sample_count = []
                                               
    for videos in traindir:                                                         
     
        for frames in videos:
            class_sample_count=+1                                                   
        weight_per_class = 1. / class_sample_count 
        samples_weight = np.array([weight_per_class[t] for t in frames])       
        samples_weight = torch.from_numpy(samples_weight)

    
    sampler = WeightedRandomSampler(samples_weight.type('torch.DoubleTensor'), len(samples_weight))   

</code></pre>
<p>However, I am getting an error</p>
<pre><code class="lang-auto">samples_weight = np.array([weight_per_class[t] for t in frames])

TypeError: 'float' object is not subscriptable
</code></pre>
<p>Any advice for me is appreciated. Thank you in advance</p>

thecho7: <p><code>weight_per_class</code> is a float type variable but not a list or something index-able.</p>

peony: <p>I have edited my code for the weighted sampler:</p>
<pre><code class="lang-auto">sample_weights=[]                                     
class_weights = []
   
    
    for classes, videos, frames in os.walk(traindir):                                                         
        if len(frames) &gt; 0:
            class_weights.append(1/len(frames))
            
    sample_weights = [0] *len(train_dataset)

    for idx, (frames, label, file_name) in enumerate(train_dataset):
        class_weight = class_weights[label]
       
        sample_weights[idx] = class_weight
        
    sampler = WeightedRandomSampler(sample_weights, num_samples=len(sample_weights), replacement=True)
</code></pre>
<p>I have put this all in my main. It’s not throwing the error I mentioned before anymore. However, there’s a new error firing in my dataset.py</p>
<pre><code class="lang-auto">dataset.py", line 35, in __getitem__
    class_path = self.root_dir + '/' + self.classes[label]

UnboundLocalError: local variable 'label' referenced before assignment
</code></pre>
<p>Here is the code for my dataset.py</p>
<pre><code class="lang-auto">class loadedDataset(Dataset):
    def __init__(self, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform = transform
        self.classes = sorted(os.listdir(self.root_dir))
        self.count = [len(os.listdir(self.root_dir + '/' + c)) for c in self.classes]
        print('self.count')
        print(self.count)
        self.acc_count = [self.count[0]]
        for i in range(1, len(self.count)):
                self.acc_count.append(self.acc_count[i-1] + self.count[i])
        # self.acc_count = [self.count[i] + self.acc_count[i-1] for i in range(1, len(self.count))]

    def __len__(self):
        l = np.sum(np.array([len(os.listdir(self.root_dir + '/' + c)) for c in self.classes]))
        return l

    def __getitem__(self, idx):
        for i in range(len(self.acc_count)):
            if idx &lt; self.acc_count[i]:
                label = i
                break

        class_path = self.root_dir + '/' + self.classes[label] 

        if label:
            file_path = class_path + '/' + sorted(os.listdir(class_path))[idx-self.acc_count[label]]
        else:
            file_path = class_path + '/' + sorted(os.listdir(class_path))[idx]

        _, file_name = os.path.split(file_path)

        frames = []
        #self.file_path = file_path
        # print os.listdir(file_path)
        file_list = sorted(os.listdir(file_path))
        # print file_list

        # v: maximum translation in every step
        v = 2
        offset = 0
        for i, f in enumerate(file_list):
           
      
            frame = torch.load(file_path + '/' + f)
            
            #translation
            offset += random.randrange(-v, v)
            offset = min(offset, 3 * v)
            offset = max(offset, -3 * v)
            #frame = frame.transform(frame.size, Image.AFFINE, (1, 0, offset, 0, 1, 0))
            if self.transform is not None:
                frame = self.transform[0](frame)
            frames.append(frame)

        return frames, label, file_name
</code></pre>
<p>I didn’t encounter this error before I put in my weight sampler code.</p>
<p>Any ideas on this? Thanks in advance</p>

thecho7: <p><code>label</code> is not assigned.<br>
Check if statement works as you designed.</p>