How to obtain a single prediction using Learner (fastai)

username: univ
cooked: <p>For practice purposes, I built an encoder-decoder that receives images of 3 and outputs images of 7 using FastAI</p>
<pre><code class="lang-auto">!pip install -Uqq fastbook
import fastbook
fastbook.setup_book()
from fastbook import *

#Loading the images
path = untar_data(URLs.MNIST_SAMPLE)
Path.BASE_PATH = path

#converting them into 728 column tensors

threes = (path/'train'/'3').ls()
sevens = (path/'train'/'7').ls()
threes_valid = (path/'valid'/'3').ls()
sevens_valid = (path/'valid'/'7').ls()

threes_data = [Image.open(i) for i in threes]
sevens_data = [Image.open(i) for i in sevens]
threes_data_v = [Image.open(i) for i in threes_valid]
sevens_data_v = [Image.open(i) for i in sevens_valid]

#To tensors
tensor_3 =[tensor(i).float()/255 for i in threes_data]
tensor_7 = [tensor(i).float()/255 for i in sevens_data]
tensor_3_v =[tensor(i).float()/255 for i in threes_data_v]
tensor_7_v = [tensor(i).float()/255 for i in sevens_data_v]

stack_3 = torch.stack(tensor_3).view(-1,28*28)
stack_7 = torch.stack(tensor_7).view(-1,28*28)
stack_7 = stack_7[:len(stack_3),:] #Making sure 7 and 3 are the same size
stack_3_v =torch.stack(tensor_3_v).view(-1,28*28)
stack_7_v = torch.stack(tensor_7_v).view(-1,28*28)
stack_7_v = stack_7_v[:len(stack_3_v),:] #Making sure 7 and 3 are the same size

#Creating dataloaders
X_train = stack_3.view(-1,28*28)
y_train = stack_7.view(-1,28*28)
X_valid = stack_3_v.view(-1,28*28)
y_valid = stack_7_v.view(-1,28*28)

dset = list(zip(X_train,y_train))
dset_v = list(zip(X_valid,y_valid))

dl = DataLoader(dset,batch_size=256)
dl_v = DataLoader(dset_v,batch_size=256)

dls = DataLoaders(dl,dl_v)

#Modeling
def loss(p,t):
  p=p.sigmoid()
  criterion = nn.MSELoss()
  return criterion(p, t)

simple_net = nn.Sequential(
    nn.Linear(28*28,30),
    nn.ReLU(),
    nn.Linear(30,28*28)
)  

learn=Learner(dls,simple_net,opt_func=SGD,loss_func = loss)
learn.fit(80,0.1)


#Predicting
get_preds() gives me the predictions for the training set.
preds,targs = learn.get_preds()
show_image(preds[11].view(28,28)), show_image(targs[0].view(28,28))
</code></pre>
<p>This seems to work: <img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6bed65c3691de980adfd650f58197d48526fda48.png" alt="image" data-base62-sha1="foLI5Iggyn7fGsFO14oq8ECvLpm" width="68" height="68"> <img src="https://discuss.pytorch.org/uploads/default/original/3X/a/5/a59aadcd53678a9bd56731ddc29be78b3644d0d7.png" alt="image" data-base62-sha1="nD0bcuBIklNdSE6pZhQERsTgTyf" width="68" height="68"></p>
<p>However when I try to produce a prediction for a single image I get an error:</p>
<pre><code class="lang-auto">newp = X_train[1,:]
newimage = learn.predict(newp)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-118-6693f5f1dc19&gt; in &lt;module&gt;()
----&gt; 1 newimage = learn.predict(newp)

2 frames
/usr/local/lib/python3.6/dist-packages/fastcore/foundation.py in __getattr__(self, k)
    151         if self._component_attr_filter(k):
    152             attr = getattr(self,self._default,None)
--&gt; 153             if attr is not None: return getattr(attr,k)
    154         raise AttributeError(k)
    155     def __dir__(self): return custom_dir(self,self._dir())

AttributeError: 'list' object has no attribute 'decode_batch'
</code></pre>
<p>Can you please help me figure out where I went wrong here and how I can obtain a prediction for a single image?</p>

username: ptrblck
cooked: <p>Based on <a href="https://forums.fast.ai/t/how-to-make-predictions-with-vision-learner-solved/34456">this thread</a> it seems <code>predict()</code> expects an image as the input not a tensor or list.</p>

username: wpan
cooked: <p>I used learn.model(newp) to get the prediction.</p>