Skip to content

Instantly share code, notes, and snippets.

@tyoc213
Created May 14, 2020 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoc213/3e82f91095038f06f5b8d89edc9d70d1 to your computer and use it in GitHub Desktop.
Save tyoc213/3e82f91095038f06f5b8d89edc9d70d1 to your computer and use it in GitHub Desktop.
exception because incorrect format for dataloader
#%%
from fastai2.vision.all import *
# in the same folder I have downloaded with: kaggle competitions download -c digit-recognizer
path = untar_data("file://digit-recognizer.zip", dest=".")
print(path)
print(path.ls())
#%%
import pandas as pd
df = pd.read_csv(Path(path)/"train.csv", header='infer')
df.head()
#%%
df.describe()
# %%
imagenes = df.iloc[:,1:].apply(lambda x: x.values, axis=1).values
labels = df.iloc[:,:1].apply(lambda x: x.values[0], axis=1)
len(imagenes), len(labels)
# %%
from PIL import Image
from matplotlib import cm
def get_image(csv_row):
img = csv_row.reshape(28,28)
x = cm.gist_earth(img)*255
return Image.fromarray(np.uint8(x))
get_image(imagenes[110])
# %%
def get_my_images(uno):
print(f"get_my_images {uno}")
print(uno)
l = L()
for idx, i in enumerate(imagenes):
img = get_image(i)
# return i
# l.append({'img': img, 'lbl':labels[idx]})
# img.some = labels[idx]
# l.append(img)
# x = torch.from_numpy(i)
# x.numero = labels[idx]
# l.append(x)
p = TensorImage(img)
p.numero = labels[idx]
l.append(p)
return l
def get_my_labels(fname):
return fname.numero
dblock = DataBlock(get_items = get_my_images,
get_y = get_my_labels)
dsets = dblock.datasets("digit-recognizer")
dsets.train[10]
# %%
########## RUN TO THIS CELL ##########################
########## RUN TO THIS CELL ##########################
########## RUN TO THIS CELL ##########################
########## RUN TO THIS CELL ##########################
#
dls = dblock.dataloaders("digit-recognizer")
dls.show_batch() # <-- AttributeError: 'Tensor' object has no attribute 'show' from fastai2/data/core.py line 18
@tyoc213
Copy link
Author

tyoc213 commented May 14, 2020

The complete output is

get_my_images digit-recognizer
digit-recognizer
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/Documentos/github/fastai2/digitos.py in 
      66 #
      67 dls = dblock.dataloaders("digit-recognizer")
----> 68 dls.show_batch() # <-- AttributeError: 'Tensor' object has no attribute 'show' from fastai2/data/core.py line 18
      69 

~/Documentos/github/fastai2/fastai2/data/core.py in show_batch(self, b, max_n, ctxs, show, unique, **kwargs)
     97         if b is None: b = self.one_batch()
     98         if not show: return self._pre_show_batch(b, max_n=max_n)
---> 99         show_batch(*self._pre_show_batch(b, max_n=max_n), ctxs=ctxs, max_n=max_n, **kwargs)
    100         if unique: self.get_idxs = old_get_idxs
    101 

~/Documentos/github/fastcore/fastcore/dispatch.py in __call__(self, *args, **kwargs)
     96         if not f: return args[0]
     97         if self.inst is not None: f = MethodType(f, self.inst)
---> 98         return f(*args, **kwargs)
     99 
    100     def __get__(self, inst, owner):

~/Documentos/github/fastai2/fastai2/vision/data.py in show_batch(x, y, samples, ctxs, max_n, nrows, ncols, figsize, **kwargs)
     43 def show_batch(x:TensorImage, y, samples, ctxs=None, max_n=10, nrows=None, ncols=None, figsize=None, **kwargs):
     44     if ctxs is None: ctxs = get_grid(min(len(samples), max_n), nrows=nrows, ncols=ncols, figsize=figsize)
---> 45     ctxs = show_batch[object](x, y, samples, ctxs=ctxs, max_n=max_n, **kwargs)
     46     return ctxs
     47 

~/Documentos/github/fastai2/fastai2/data/core.py in show_batch(x, y, samples, ctxs, max_n, **kwargs)
     16     else:
     17         for i in range_of(samples[0]):
---> 18             ctxs = [b.show(ctx=c, **kwargs) for b,c,_ in zip(samples.itemgot(i),ctxs,range(max_n))]
     19     return ctxs
     20 

~/Documentos/github/fastai2/fastai2/data/core.py in (.0)
     16     else:
     17         for i in range_of(samples[0]):
---> 18             ctxs = [b.show(ctx=c, **kwargs) for b,c,_ in zip(samples.itemgot(i),ctxs,range(max_n))]
     19     return ctxs
     20 

AttributeError: 'Tensor' object has no attribute 'show'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment