Converting tensors to class labels

I am new to deep learning and after watching the first video of the course, I wrote the following code for image classification for a kaggle competition (cats vs dogs).
My code is as follows:

from fastai import *
from fastai.vision import *
path = “…/input”
bs = 64
np.random.seed(2)
pat = re.compile(r’…/([^/…]+).\d+.jpg$’)
path_img = path + “/train/train”
fnames = get_image_files(path_img)
fnames[:5]
data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(),
size=224, num_workers=0, bs=bs, test=‘test1’)
data.show_batch(rows=3, figsize=(7,6))
learn = create_cnn(data, models.resnet34, metrics=error_rate, model_dir="/tmp/model/")
learn.fit_one_cycle(2)
interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs)
interp.plot_top_losses(9, figsize=(15,11))

It works well till here without any errors. After this, I want to make predictions on the test set on save the results in a csv file so that I can make a submission on kaggle. For this I am using the learn.get_preds() method. It however returns an output in the form of tensors. My questions are as follows:

  1. Am I using the right method to make predictions on the test set? If not please point me in the right direction.
  2. learn.get_preds() returns a list of tensors. How can I convert these tensors to class labels (1 and 0) so that I can make a submission.
  3. I believe learn.get_preds() works on the validation set. However, when I try to execute learn.get_preds(is_test = True) I get an error which says “unknown parameter is_test”.
    How can I get around this.

Hi !

For question like this it’s useful to refer to fastai documentation. For example, here’s the answer to your third question :slight_smile:

For your other questions, I’d suggest looking at the bottom ofthe lesson3-planet notebook.

Good luck !

And for your second question, the correspondence index to class is stored in data.classes.

The answer-link for question three is outdatet: https://docs.fast.ai/tutorial.inference.html#A-classification-problem. Does anyone have an up-to-date link?