TypeError bool is not subscriptable

JohnPolo: <p>I’m using code from this <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#" rel="noopener nofollow ugc">tutorial</a>. I have my own data. It is a very small set of images, just seven with seven masks. The masks have a black background and four AOIs each with their own shade.</p>
<p>When I get to these lines:</p>
<pre><code class="lang-auto">dataset = sensor_image(root = '/home/nightjar/Documents/images', transforms = get_transform(train=True))
&gt;&gt;&gt; data_loader = torch.utils.data.DataLoader(
...  dataset)#, batch_size=1, num_workers=2,
&gt;&gt;&gt; images,targets = next(iter(data_loader))
</code></pre>
<p>… I get the following error</p>
<blockquote>
<p>:30: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.<br>
Traceback (most recent call last):<br>
File “”, line 1, in <br>
File “/home/nightjar/.local/share/virtualenvs/instance_seg_training-pW2YCmb4/lib64/python3.9/site-packages/torch/utils/data/dataloader.py”, line 530, in <strong>next</strong><br>
data = self._next_data()<br>
File “/home/nightjar/.local/share/virtualenvs/instance_seg_training-pW2YCmb4/lib64/python3.9/site-packages/torch/utils/data/dataloader.py”, line 570, in _next_data<br>
data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
File “/home/nightjar/.local/share/virtualenvs/instance_seg_training-pW2YCmb4/lib64/python3.9/site-packages/torch/utils/data/_utils/fetch.py”, line 49, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/home/nightjar/.local/share/virtualenvs/instance_seg_training-pW2YCmb4/lib64/python3.9/site-packages/torch/utils/data/_utils/fetch.py”, line 49, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “”, line 36, in <strong>getitem</strong><br>
TypeError: ‘bool’ object is not subscriptable</p>
</blockquote>
<p>I think the error is caused by the something to do with the masks, but I’m not sure of that, because I don’t understand everything the traceback says. But since it says “bool”, I’m guessing that is with the masks due to this comment in the code:<br>
# split the color-encoded mask into a set<br>
# of binary masks</p>
<p>That comment occurs early in the code where the class for the dataset is declared.</p>
<p>What can I do to resolve this error?</p>

ptrblck: <p>Based on the stacktrace it seems the error is raised in:</p>
<pre><code class="lang-python">data = [self.dataset[idx] for idx in possibly_batched_index]
</code></pre>
<p>which seems to be caused by the indexing of <code>self.dataset[idx]</code> which seems to be a <code>bool</code> scalar.<br>
What is <code>print(type(dataset))</code> returning before wrapping it into the <code>DataLoader</code>?<br>
If this is returning the proper <code>Dataset</code> class, are you able to iterate the object?</p>

JohnPolo: <p>Hi, ptrblck.</p>
<p>It looks like I have the right <code>Dataset</code> class (I renamed it).</p>
<pre><code class="lang-auto">&gt;&gt; print(type(dataset))
&lt;class '__main__.sensor_image'&gt;
</code></pre>
<p>But it seems that I can’t iterate the <code>Dataset</code>.</p>
<pre><code class="lang-auto">&gt;&gt; iter(dataset)
&lt;iterator object at 0x7fbc3c5ec220&gt;
&gt;&gt; for i in iter(dataset):
...     print(i)
... 
&lt;stdin&gt;:30: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 36, in __getitem__
TypeError: 'bool' object is not subscriptable
</code></pre>
<p>This is the code for building the <code>Dataset</code> class. I think I only edited one line. It’s in the section for <code>obj_ids</code>.</p>
<pre><code class="lang-auto">class sensor_image(torch.utils.data.Dataset):
     def __init__(self, root, transforms=None, target_transform=None):
         self.root = root
         self.transforms = transforms
         self.target_transform = target_transform
         # load all image files, sorting them to
         # ensure that they are aligned
         self.imgs = list(sorted(os.listdir(os.path.join(root))))
         self.masks = list(sorted(os.listdir(os.path.join(root))))
         
     def __getitem__(self, idx):
         # load images ad masks
         img_path = os.path.join(self.root, self.imgs[idx])
         mask_path = os.path.join(self.root, self.masks[idx])
         img = Image.open(img_path).convert("RGB")
         # note that we haven't converted the mask to RGB,
         # because each color corresponds to a different instance
         # with 0 being background
         mask = Image.open(mask_path)
         
         # Convert from image object to array
         mask = np.array(mask)
         
         obj_ids = np.unique(mask)
         # I have four classes. 
         obj_ids = obj_ids[-4:]
         # The line in original code is
         # obj_ids =  obj_ids[1:]
         
         # split the color-encoded mask into a set
         # of binary masks
         masks = mask == obj_ids[:, None, None]
         
         # get bounding box coordinates for each mask
         num_objs = len(obj_ids)
         boxes = []
         for i in range(num_objs):
             pos = np.where(masks[i])
             xmin = np.min(pos[1])
             xmax = np.max(pos[1])
             ymin = np.min(pos[0])
             ymax = np.max(pos[0])
             boxes.append([xmin, ymin, xmax, ymax])
         
         boxes = torch.as_tensor(boxes, dtype=torch.float32)
         # there is only one class
         labels = torch.ones((num_objs,), dtype=torch.int64)
         masks = torch.as_tensor(masks, dtype=torch.uint8)
         
         image_id = torch.tensor([idx])
         area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
         # suppose all instances are not crowd
         iscrowd = torch.zeros((num_objs,), dtype=torch.int64)
         
         target = {}
         target["boxes"] = boxes
         target["labels"] = labels
         target["masks"] = masks
         target["image_id"] = image_id
         target["area"] = area
         target["iscrowd"] = iscrowd
         
         if self.transforms is not None:
             img, target = self.transforms(img, target)
         
         return img, target
         
     def __len__(self):
         return len(self.imgs)
</code></pre>
<p>This is an example of one of the <a href="https://drive.google.com/file/d/1MKPuItPr-EqafvIvWFt7bXQzqI4IZtro/view?usp=sharing" rel="noopener nofollow ugc">masks</a>.</p>

ptrblck: <p>Thanks for the follow up as it seems the <code>bool</code> indexing issue is indeed created in the <code>__getitem__</code>.<br>
Could you check the types of all tensors where an indexing operation is used and make sure these are tensors? Your suspicion that the (bool) mask might be the issue could be right and maybe:</p>
<pre><code class="lang-python">pos = np.where(masks[i])
</code></pre>
<p>is failing as you might expect <code>mask</code> to be a tensor while it could be a single <code>bool</code> value.</p>

JohnPolo: <p>It took me a minute, since I’m as a lousy programmer. I wrote a bunch of loops to match the steps in the class that create the masks. I won’t include that tedious code here, but will add it if you insist on inspecting it. The following is probably tedious enough. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">for i in range(4):             
     pos.append(np.where(mask1[i]))
     pos.append(np.where(mask2[i]))
     pos.append(np.where(mask3[i]))
     pos.append(np.where(mask4[i]))
     pos.append(np.where(mask5[i]))
     pos.append(np.where(mask6[i]))
     pos.append(np.where(mask7[i]))

&gt;&gt;&gt; for i in boxes:
...     print(i)
... 
[470, 2337, 2354, 3238]
[472, 2346, 2339, 3238]
[470, 2340, 2344, 3238]
[470, 2372, 2329, 3237]
[470, 2337, 2354, 3238]
[472, 2346, 2339, 3238]
[470, 2340, 2344, 3238]
[470, 2372, 2329, 3237]
</code></pre>
<p>So it looks like the boxes are okay. I’m not sure what to check next…</p>

ptrblck: <p>I would add debug print statements to the <code>__getitem__</code> and try to isolate which lines exactly fails and what the indexed object contains.</p>

JohnPolo: <p>Thanks, ptrblck. I’ve got it now. It looks like I had my paths and files partially crossed. The TypeError is now replaced with another error. I’ll mess around with that some and maybe ask another question later.</p>
<p>I appreciate your help!</p>
<p>Best regards.</p>