"Could not infer dtype of slice" during training

droid: <p>I am training an inpainting GAN model. I get the error <code>RuntimeError: Could not infer dtype of slice</code> when trying to utilize array slices for selecting parts of an image. I don’t understand why this error occurs.</p>
<p>My batch data is composed of 4 items: a tensor of images, a tensor of masked images, a tensor of mask patches, and a list of “mask slices”. Each slice denotes a region in the image that the mask patch was cut out from.</p>
<p>Each slice is generated  in my dataset’s <code>__getitem__</code> method. Using numpy, a single “mask slice” is just a tuple of ordinary slice objects:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; mask_slice = np.s_[:, row:col, row:col]
&gt;&gt;&gt; mask_slice
(slice(None, None, None), slice(31, 47, None), slice(31, 47, None))
</code></pre>
<hr>
<p>I use a customized <code>collate_fn</code> to collect all slices of a batch together into a list of slices:</p>
<pre><code class="lang-auto">def custom_collate_fn():
    ...
    for i, (img, masked_img, true_mask_patch, mask_slice) in enumerate(batch_list):
        mask_slices_batch[i] = mask_slice
    ...
    return imgs, masked_imgs, mask_patches, mask_slices_batch
</code></pre>
<hr>
<p>The error occurs in the training loop while actually utilizing the mask slices.</p>
<pre><code class="lang-auto">imgs, masked_imgs, mask_patches, mask_slices = batch

# discriminator output should be all 1's except where the mask patch was taken
ideal_discrim_output = torch.ones(batch_size, 1, 256, 256)

for i, mask_slice in enumerate(mask_slices):
    ideal_discrim_output[i, mask_slice] = 0    # &lt;----- ERROR
</code></pre>
<hr>
<p>The actual error is <code>RuntimeError: Could not infer dtype of slice</code>. Is there a reason why this doesn’t work? Or is there a better way to do this? I can’t eliminate the slicing information because I need to do a variety of operations in the training loop using it.</p>

ptrblck: <p>It seems tuples or lists of <code>slice</code> objects are unsupported and the error is raised <a href="https://github.com/pytorch/pytorch/blob/b756fd98bb0acd10827a8efb4972e5a326a3d966/torch/csrc/utils/tensor_new.cpp#L194">here</a>.<br>
The same seems to apply for numpy, too:</p>
<pre><code class="lang-python">ideal_discrim_output = torch.ones(1, 1, 256, 256)
mask_slice = (slice(None, None, None), slice(31, 47, None), slice(31, 47, None))
ideal_discrim_output[0, [slice(None, None, None), slice(None, None, None)]]
# RuntimeError: Could not infer dtype of slice
ideal_discrim_output[0, slice(None, None, None)] # works

x = np.random.randn(10, 10, 256, 256)
x[0, (slice(None, None, None), slice(None, None, None))]
# IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
x[0, slice(None, None, None)] # works
</code></pre>
<p>so you might need to either iterate the <code>slice</code>s or try to create a single index.</p>

droid: <p>Thanks, you’re right. That’s too bad, because <code>data[i, mask_slice]</code> would be robust and easy to read. Unpacking also doesn’t work, but would be equally nice, like <code>data[i, *mask_slice]</code>.</p>
<p>As you suggest, the following does work:</p>
<pre><code class="lang-auto">ideal_discrim_output[i, mask_slice[0], mask_slice[1], mask_slice[2]] = 0
</code></pre>