Iter(testloader).next() samples 3 images instead of one

Hi All I have a function which get the value from a scale and feeds a randomly sampled image using iter(testloader).next() to a network to perform testing. For the testing results, I print the testing time. Even though the iter(testloader).next() command is known to sample one image at random, the results after testing shows three testing times instead of just one as can be seen below. I am really lost as to why that’s happening, and can’t tell if three images are sampled which makes the code prints 3 results instead of 1 after testing. Below is my sample code for review:

Testing time report after testing

Testing complete in  0m 2s
Testing complete in  0m 3s
Testing complete in  0m 3s

Below is my sample code

test= data_utils.TensorDataset(torch.from_numpy(test_set),torch.from_numpy(label_set))
test_loader = data_utils.DataLoader(test,batch_size=TestBatchSize,shuffle=True)

def noise_scale(scale_value=0):
    since = time.time()
    images, labels = iter(test_loader).next()
    images = images.view(1, 10, 200, 301)
    labels = labels.view(1, 1, 201, 301) 
    if (scale.get()) == 0:
        for i in range(1):
            net.eval()
            outputs  = net(images,labels)
            for k in range(1):
                Img = images.view(10, 200, 301)
                Img = Img[1,:,:].numpy()

     elif (scale.get()) == 10:
        for i in range(1):
            net.eval()
            outputs  = net(images,labels)
            for k in range(1):
                Img = images.view(10, 200, 301)
                Img = Img[1,:,:].numpy()

     elif (scale.get()) == 20:
        for i in range(1):
            net.eval()
            outputs  = net(images,labels)
            for k in range(1):
                Img = images.view(10, 200, 301)
                Img = Img[1,:,:].numpy()
 
    time_elapsed = time.time() - since  
    print('Testing complete in  {:.0f}m {:.0f}s' .format(time_elapsed // 60, time_elapsed % 60))  
## This above line prints three times as shown above instead of just one.

## Scale ##
var3 = StringVar()
scale = tk.Scale(root, variable=var3, orient='horizontal', bg = "gray80", bd=1, from_=0, to=20, tickinterval=10, resolution=10, length=200, command=noise_scale)

This code snippet would return one batch of samples, so depending on the used batch_size it could return more than a single sample.

@ptrblck , the test batch size is just one, so it should have return only however getting results for three.

I don’t know what might be causing it and cannot reproduce the issue:

dataset = TensorDataset(torch.randn(10, 10), torch.randn(10))

loader = DataLoader(dataset, batch_size=1)
a, b = iter(loader).next()
print(a.shape, b.shape)
# torch.Size([1, 10]) torch.Size([1])

loader = DataLoader(dataset, batch_size=3)
a, b = iter(loader).next()
print(a.shape, b.shape)
# torch.Size([3, 10]) torch.Size([3])

loader = DataLoader(dataset, batch_size=5)
a, b = iter(loader).next()
print(a.shape, b.shape)
# torch.Size([5, 10]) torch.Size([5])

Is there way to select only the first image and label after using the iter(loader).next? I have used iter(leader).next()[0][0] but getting too many values to unpack (expected 2) error.

I would recommend to check why 3 values are returned when the batch_size is set to 1 as this seems to be an error.
Once this is done, check how many values are returned and which shape they have to index them if needed (which should not be needed after fixing the first issue).

@ptrblck , can you have a look at the extended code in my post to see if I am missing something. The scale levels are three (0,10, 20) and I am using the if and elif conditional statements to get the state of the scale and execute the code under. I am unsure if the if and elif statements are causing this issue.

I don’t fully understand the code as I don’t know how noise_scale is executed in tk.Scale.
The conditions also seem to be irrelevant as the images, labels = iter(test_loader).next() line is executed before and should already yield the single sample given the batch_size is set to 1.

Okay, so the noise_scale is the function command that controls the scale. There are 3 levels in the scale (0, 10, 20). If current scale value is set to 0, the first if statement is executed, if the current scale value is set to 10 then the middle elif is executed and finally if the scale is set to the 20, then the last elif is executed. Do you get it now?

I understand the code logic of the if conditions but don’t see how they can influence the return values of the DataLoader in any way.
Rip out all unneeded code and make sure that iter(loader).next() returns the expected shape first.