Skip to content

Commit

Permalink
fastai v1
Browse files Browse the repository at this point in the history
  • Loading branch information
sgugger committed Sep 22, 2018
1 parent b01caf5 commit a5082fd
Show file tree
Hide file tree
Showing 43 changed files with 3,003 additions and 706 deletions.
2 changes: 2 additions & 0 deletions dev_nb/009_rossmann.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"from nb_008 import *"
]
},
Expand Down Expand Up @@ -498,6 +499,7 @@
"metadata": {},
"outputs": [],
"source": [
"#export\n",
"ListSizes = Collection[Tuple[int,int]]\n",
"OptRange = Optional[Tuple[float,float]]\n",
"\n",
Expand Down
38 changes: 38 additions & 0 deletions dev_nb/Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from nb_009 import *"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ItemsList"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
5 changes: 1 addition & 4 deletions dev_nb/nb_006c.py → dev_nb/nb_001a.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: dev_nb/006c_camvid.ipynb

from nb_006a import *
from concurrent.futures import ProcessPoolExecutor
# file to edit: dev_nb/001a_nn_basics.ipynb
19 changes: 13 additions & 6 deletions dev_nb/nb_006a.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
from nb_006 import *
import gc

def in_channels(m):
Sizes = List[List[int]]

def in_channels(m:Model) -> List[int]:
"Returns the shape of the first weight layer"
for l in flatten_model(m):
if hasattr(l, 'weight'): return l.weight.shape[1]
raise Exception('No weight layer')

def model_sizes(m, size=(256,256), full=True):
def model_sizes(m:Model, size:tuple=(256,256), full:bool=True) -> Tuple[Sizes,Tensor,Hooks]:
"Passes a dummy input through the model to get the various sizes"
hooks = hook_outputs(m)
ch_in = in_channels(m)
x = torch.zeros(1,ch_in,*size)
Expand All @@ -21,7 +25,8 @@ def model_sizes(m, size=(256,256), full=True):
if not full: hooks.remove()
return res,x,hooks if full else res

def get_sfs_idxs(sizes, last=True):
def get_sfs_idxs(sizes:Sizes, last:bool=True) -> List[int]:
"Get the indexes of the layers where the size of the activation changes"
if last:
feature_szs = [size[-1] for size in sizes]
sfs_idxs = list(np.where(np.array(feature_szs[:-1]) != np.array(feature_szs[1:]))[0])
Expand All @@ -30,7 +35,8 @@ def get_sfs_idxs(sizes, last=True):
return sfs_idxs

class UnetBlock(nn.Module):
def __init__(self, up_in_c, x_in_c, hook):
"An basic unet block"
def __init__(self, up_in_c:int, x_in_c:int, hook:Hook):
super().__init__()
self.hook = hook
ni = up_in_c
Expand All @@ -41,15 +47,16 @@ def __init__(self, up_in_c, x_in_c, hook):
self.conv2 = conv2d(ni, ni)
self.bn = nn.BatchNorm2d(ni)

def forward(self, up_in):
def forward(self, up_in:Tensor) -> Tensor:
up_out = self.upconv(up_in)
cat_x = torch.cat([up_out, self.hook.stored], dim=1)
x = F.relu(self.conv1(cat_x))
x = F.relu(self.conv2(x))
return self.bn(x)

class DynamicUnet(nn.Sequential):
def __init__(self, encoder, n_classes, last=True):
"Unet created from a given architecture"
def __init__(self, encoder:Model, n_classes:int, last:bool=True):
imsize = (256,256)
sfs_szs,x,self.sfs = model_sizes(encoder, size=imsize)
sfs_idxs = reversed(get_sfs_idxs(sfs_szs, last))
Expand Down
4 changes: 2 additions & 2 deletions dev_nb/nb_007b.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def pad_collate(samples:BatchSamples, pad_idx:int=1, pad_first:bool=True) -> Tup
return res, LongTensor([s[1] for s in samples]).squeeze()

def classifier_data(datasets:Collection[TextDataset], path:PathOrStr, **kwargs) -> DataBunch:
"Function that transform the datasets in a DataBunch for classification"
"Function that transform the `datasets` in a `DataBunch` for classification"
bs = kwargs.pop('bs') if 'bs' in kwargs else 64
pad_idx = kwargs.pop('pad_idx') if 'pad_idx' in kwargs else 1
train_sampler = SortishSampler(datasets[0].ids, key=lambda x: len(datasets[0].ids[x]), bs=bs//2)
Expand Down Expand Up @@ -184,7 +184,7 @@ def load_pretrained(self, wgts_fname:str, itos_fname:str):
def language_model(cls, data:DataBunch, bptt:int=70, emb_sz:int=400, nh:int=1150, nl:int=3, pad_token:int=1,
drop_mult:float=1., tie_weights:bool=True, bias:bool=True, qrnn:bool=False,
pretrained_fnames:OptStrTuple=None, **kwargs) -> 'RNNLearner':
"Creates a Learner with a language model."
"Creates a `Learner` with a language model."
dps = np.array([0.25, 0.1, 0.2, 0.02, 0.15]) * drop_mult
vocab_size = len(data.train_ds.vocab.itos)
model = get_language_model(vocab_size, emb_sz, nh, nl, pad_token, input_p=dps[0], output_p=dps[1],
Expand Down
2 changes: 2 additions & 0 deletions dev_nb/nb_009.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#################################################
# file to edit: dev_nb/009_rossmann.ipynb

from nb_008 import *

StrList = Collection[str]
@dataclass
class TabularTransform():
Expand Down
3 changes: 3 additions & 0 deletions fastai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
from .metrics import *
from .torch_core import *
from .train import *
from .rnn_learner import *
from .conv_learner import *
from .colab import *

Loading

2 comments on commit a5082fd

@stas00
Copy link
Contributor

@stas00 stas00 commented on a5082fd Sep 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think dev_nb/Untitled.ipynb got in unintentionally, right?

@jph00
Copy link
Member

@jph00 jph00 commented on a5082fd Sep 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup I removed it.

Please sign in to comment.