如何解决如何使用学习者fastai获得单个预测
出于练习目的,我构建了一个编码器/解码器,它接收3张图像并输出7张图像。
!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))
newp = X_train[1,:]
newimage = learn.predict(newp)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-118-6693f5f1dc19> in <module>()
----> 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)
--> 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'
您能帮我弄清楚我哪里出了问题以及如何获得单个图像的预测吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。