I was just following dogs breed classification example from kaggle, as Jeremy explains in lesson3 of Part 1.
log_preds, y = learn.TTA(is_test = True)
log_preds.shape
(5, 10357, 120)
which should be actually,
(10357, 120)
Following is how my code looks like,
# coding: utf-8
# In[1]:
# Put these at the top of every notebook, to get automatic reloading and inline plotting
get_ipython().run_line_magic('reload_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
# This file contains all the main external libs we'll use
from fastai.imports import *
# In[3]:
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
import torch
import matplotlib.pyplot as plt
# In[4]:
PATH = "C:\\Users\\Mahesh.Bhosale\\fastai\\data\\dogs_breed\\"
# In[5]:
torch.cuda.is_available() & torch.backends.cudnn.enabled
# In[38]:
sz = 224
arch = resnet50
bs = 58
# In[83]:
label_csv = f'{PATH}labels.csv'
n = len(list(open(label_csv))) - 1
val_idxs = get_cv_idxs(n)
# In[40]:
os.listdir(PATH)
# In[41]:
label_df = pd.read_csv(label_csv)
# In[42]:
label_df.head()
# In[43]:
label_df.pivot_table(index = 'breed', aggfunc=len).sort_values('id', ascending = False)
# In[44]:
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_csv(PATH, 'train', f'{PATH}labels.csv', test_name='test', val_idxs = val_idxs, suffix='.jpg'
,tfms=tfms, bs=bs)
# In[45]:
fn = PATH + data.trn_ds.fnames[0];fn
# In[46]:
img = PIL.Image.open(fn); img
# In[47]:
img.size
# In[48]:
size_d = {k: PIL.Image.open(PATH+k).size for k in data.trn_ds.fnames}
row_sz, col_sz = list(zip(*size_d.values()))
# In[49]:
row_sz = np.array(row_sz); col_sz = np.array(col_sz)
# In[50]:
plt.hist(row_sz)
# In[51]:
plt.hist(col_sz)
# In[52]:
def get_data(sz, bs):
tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_csv(PATH, 'train', f'{PATH}labels.csv', test_name='test', num_workers=4, val_idxs = val_idxs, suffix='.jpg'
,tfms=tfms, bs=bs)
return data if sz > 300 else data.resize(340, 'tmp')
# In[53]:
data = get_data(sz, bs)
# In[54]:
learn = ConvLearner.pretrained(arch, data, precompute=True)
# In[55]:
learn.fit(1e-2, 5)
# In[56]:
learn = ConvLearner.pretrained(arch, data, precompute = True, ps=0.5)
# In[57]:
learn.fit(1e-2, 10)
# In[58]:
learn.precompute=False
# In[62]:
learn.fit(1e-2, 5, cycle_len=1)
# In[64]:
learn.set_data(get_data(299, bs))
learn.freeze()
# In[65]:
learn.fit(1e-2, 1, cycle_len=1)
# In[66]:
learn.fit(1e-2, 1, cycle_len=1, cycle_mult=2)
# In[75]:
data.classes
# In[76]:
data.test_ds.fnames
# In[84]:
log_preds, y = learn.TTA(is_test = True)
# In[87]:
y.shape
# In[79]:
log_preds.shape
# In[ ]:
preds = np.exp(learn.predict(is_test = True))
# In[ ]:
preds.shape
# In[71]:
probs = np.mean(np.exp(log_preds))
probs.shape
# In[72]:
df = pd.DataFrame(probs)
df.columns = data.classes
df.insert(0, 'id', [o[5:-4] for o in data.test_ds.fnames])
df.head()
np.argmax(probs)