.../generalized_rcnn.py", line 64, in forward boxes = target["boxes"] TypeError: string indices must be integers

Hi, I am trying to train rcnn model, but I have few problems when I am using batch_size>1.
My function which make dataset returns a single img tensor and a single target (dictionary):

        return (
            img,
            target,
        )

Later I am loading datasets to dataloader which loads data to the training process.

   with tqdm(train_loader) as _tqdm:
        for x, y in _tqdm:
            x = x.to(device)
            for key, value in y.items():
                y[key] = torch.tensor(value).to(device)

            outputs = model(x, y)

When batch_size is f.e. == 2, then x is a list of two tensors of shape [D, H, W] but y is a dict where every key have two values. Then occurs error:

.../generalized_rcnn.py", line 64, in forward boxes = target["boxes"] TypeError: string indices must be integers
So I assumed that probably it can’t handle with dictionaries and I pass:

output(x, [y])

Then error changes to:

.../transform.py", line 99, in forward
target_index = targets[i] if targets is not None else None
IndexError: list index out of range

Any ideas how to make it run properly?

Would it work, if you pass y as a list of two dicts?

Finally, I solved this by adding:

        y_list = []
        for i in range(0, len(x)):
            y_list.append(y)

TypeError: means that you are trying to perform an operation on a value whose type is not compatible with that operation. An Iterable is a collection of elements that can be accessed sequentially . In Python, iterable objects are indexed using numbers . When you try to access an iterable object using a string or a float as the index, an error will be returned as TypeError: string indices must be integers. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value.

For example, str[hello"] and str[2.1] as indexes. As these are not integers, a TypeError exception is raised. This means that when you’re accessing an iterable object like a string or float value, you must do it using an integer value . If you are accessing items from a dictionary , make sure that you are accessing the dictionary itself and not a key in the dictionary.

Python supports slice notation for any sequential data type like lists, strings , tuples, bytes, bytearrays, and ranges. When working with strings and slice notation, it can happen that a TypeError: string indices must be integers is raised, pointing out that the indices must be integers, even if they obviously are.