Getting predictions get_preds() for test sets

#1

Hello

I have posted this on another part but perhaps it is too obvious but I can’t seem to find any answer to this trivial question.

If I run val_preds,val_targets = learn.get_preds() I get the predictions and targets for the validation set. How do I run it for the test set?

So far all the docs show learn.get_preds(is_test=True) but is_test has been changed to something else. Please advise !

Cheers, Hud

0 Likes

(ali baltschun) #2

i dont know for one liner

but its work fo me

preds = []
for i in range(0,30):
    p = learn.predict(data.test_ds.x[i])
    preds.append(str(p[0]))
0 Likes

(Sam Ariabod) #3

Hello, with v1 you can pass in the DatasetType to get_preds().

preds, y = learn.get_preds(DatasetType.Test)

0 Likes

#4

Thanks this sort of worked, but now new problems arise.

I followed the solutions for vision in the inference tutorials and am able to predict different classes of boats from single images from the test set. However, images from a test_set folder returns only one class.

Have a look at the inference section in my gist-notebook

Has anyone had this problem?

0 Likes

(Erik Man) #5

I have the same problem. I’m just a beginner so I don’t know why, but my workaround is:

preds, y, losses = learn.get_preds(ds_type=DatasetType.Test, with_loss=True)
y = torch.argmax(preds, dim=1)
6 Likes

(Christopher Tan) #6

Thanks for this workaround! Works great for me too.

0 Likes

(Justin) #7

I also am seeing the y predictions returning all zeros, in my case using a text learner (in fastai v1.0.48):

preds, y = learn.get_preds(ds_type=DatasetType.Test) print(y)

tensor([0, 0, 0, ..., 0, 0, 0])

Even though taking the argmax reveals that not all the highest preds are at label position 0.

@sgugger Is this a known issue?

0 Likes

#8

That’s not an issue, it’s because the test set in fastai is unlabelled, so the y (your targets) are all set to zero.

1 Like

(Justin) #9

I see. That explains it. I thought y was the predicted y, but it’s actually the true y (which we don’t have in a test set). Thanks.

1 Like

#10

Are there plans to change this? For me it always feels kind of hacky to artificially add none labels to the test data loader in order for it to work.

0 Likes

#11

Not in the midterm, no, we have a lot on our plates with the ongoing course right now.

0 Likes