Deep Learning 2: Part 2 Lesson 14

Hiromi Suenaga
Jun 16, 2018 · 92 min read

Show and tell from last week

Super-Resolution [2:06]

Notebook [5:06]

matplotlib inline
%reload_ext autoreload
%autoreload 2

Super resolution data

from fastai.conv_learner import *
from pathlib import Path

torch.backends.cudnn.benchmark=True
PATH = Path('data/imagenet')
PATH_TRN = PATH/'train'
fnames_full,label_arr_full,all_labels = folder_source(PATH, 'train')
fnames_full = ['/'.join(Path(fn).parts[-2:]) for fn in fnames_full]
list(zip(fnames_full[:5],label_arr_full[:5]))
[('n01440764/n01440764_9627.JPEG', 0),
('n01440764/n01440764_9609.JPEG', 0),
('n01440764/n01440764_5176.JPEG', 0),
('n01440764/n01440764_6936.JPEG', 0),
('n01440764/n01440764_4005.JPEG', 0)]
all_labels[:5]['n01440764', 'n01443537', 'n01484850', 'n01491361', 'n01494475']
np.random.seed(42)
# keep_pct = 1.
keep_pct = 0.02
keeps = np.random.rand(len(fnames_full)) < keep_pct
fnames = np.array(fnames_full, copy=False)[keeps]
label_arr = np.array(label_arr_full, copy=False)[keeps]
arch = vgg16
sz_lr = 72
scale,bs = 2,64
# scale,bs = 4,32
sz_hr = sz_lr*scale
class MatchedFilesDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
assert(len(fnames)==len(y))
super().__init__(fnames, transform, path)
def get_y(self, i):
return open_image(os.path.join(self.path, self.y[i]))
def get_c(self): return 0
aug_tfms = [RandomDihedral(tfm_y=TfmType.PIXEL)]
val_idxs = get_cv_idxs(len(fnames), val_pct=min(0.01/keep_pct, 0.1))
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs,
np.array(fnames), np.array(fnames))
len(val_x),len(trn_x)
(12811, 1268356)img_fn = PATH/'train'/'n01558993'/'n01558993_9684.JPEG'
tfms = tfms_from_model(arch, sz_lr, tfm_y=TfmType.PIXEL, 
aug_tfms=aug_tfms, sz_y=sz_hr)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH_TRN)
md = ImageData(PATH, datasets, bs, num_workers=16, classes=None)
denorm = md.val_ds.denorm
def show_img(ims, idx, figsize=(5,5), normed=True, ax=None):
if ax is None: fig,ax = plt.subplots(figsize=figsize)
if normed: ims = denorm(ims)
else: ims = np.rollaxis(to_np(ims),1,4)
ax.imshow(np.clip(ims,0,1)[idx])
ax.axis('off')
x,y = next(iter(md.val_dl))
x.size(),y.size()
(torch.Size([32, 3, 72, 72]), torch.Size([32, 3, 288, 288]))
idx=1
fig,axes = plt.subplots(1, 2, figsize=(9,5))
show_img(x,idx, ax=axes[0])
show_img(y,idx, ax=axes[1])
batches = [next(iter(md.aug_dl)) for i in range(9)]
fig, axes = plt.subplots(3, 6, figsize=(18, 9))
for i,(x,y) in enumerate(batches):
show_img(x,idx, ax=axes.flat[i*2])
show_img(y,idx, ax=axes.flat[i*2+1])

Model [21:48]

def conv(ni, nf, kernel_size=3, actn=False):
layers = [nn.Conv2d(ni, nf, kernel_size,
padding=kernel_size//2)]
if actn: layers.append(nn.ReLU(True))
return nn.Sequential(*layers)
https://arxiv.org/abs/1707.02921
class ResSequential(nn.Module):
def __init__(self, layers, res_scale=1.0):
super().__init__()
self.res_scale = res_scale
self.m = nn.Sequential(*layers)

def forward(self, x): return x + self.m(x) * self.res_scale
def res_block(nf):
return ResSequential(
[conv(nf, nf, actn=True), conv(nf, nf)],
0.1)
def upsample(ni, nf, scale):
layers = []
for i in range(int(math.log(scale,2))):
layers += [conv(ni, nf*4), nn.PixelShuffle(2)]
return nn.Sequential(*layers)
class SrResnet(nn.Module):
def __init__(self, nf, scale):
super().__init__()
features = [conv(3, 64)]
for i in range(8): features.append(res_block(64))
features += [conv(64,64), upsample(64, 64, scale),
nn.BatchNorm2d(64),
conv(64, 3)]
self.features = nn.Sequential(*features)

def forward(self, x): return self.features(x)
http://deeplearning.net/software/theano/tutorial/conv_arithmetic.html
Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network

Checkerboard pattern [44:19]

https://arxiv.org/abs/1707.02937

Pixel loss [46:41]

m = to_gpu(SrResnet(64, scale))
m = nn.DataParallel(m, [0,2])
learn = Learner(md, SingleModel(m), opt_fn=optim.Adam)
learn.crit = F.mse_loss
learn.lr_find(start_lr=1e-5, end_lr=10000)
learn.sched.plot()
31%|███▏ | 225/720 [00:24<00:53, 9.19it/s, loss=0.0482]
lr=2e-3learn.fit(lr, 1, cycle_len=1, use_clr_beta=(40,10))2%|▏         | 15/720 [00:02<01:52,  6.25it/s, loss=0.042]  
epoch trn_loss val_loss
0 0.007431 0.008192
[array([0.00819])]x,y = next(iter(md.val_dl))
preds = learn.model(VV(x))
idx=4
show_img(y,idx,normed=False)
show_img(preds,idx,normed=False);
show_img(x,idx,normed=True);
x,y = next(iter(md.val_dl))
preds = learn.model(VV(x))
show_img(y,idx,normed=False)
show_img(preds,idx,normed=False);
show_img(x,idx);

Perceptual loss [50:57]

def icnr(x, scale=2, init=nn.init.kaiming_normal):
new_shape = [int(x.shape[0] / (scale ** 2))] + list(x.shape[1:])
subkernel = torch.zeros(new_shape)
subkernel = init(subkernel)
subkernel = subkernel.transpose(0, 1)
subkernel = subkernel.contiguous().view(subkernel.shape[0],
subkernel.shape[1], -1)
kernel = subkernel.repeat(1, 1, scale ** 2)
transposed_shape = [x.shape[1]] + [x.shape[0]] +
list(x.shape[2:])
kernel = kernel.contiguous().view(transposed_shape)
kernel = kernel.transpose(0, 1)
return kernel
m_vgg = vgg16(True)

blocks = [i-1 for i,o in enumerate(children(m_vgg))
if isinstance(o,nn.MaxPool2d)]
blocks, [m_vgg[i] for i in blocks]
([5, 12, 22, 32, 42],
[ReLU(inplace), ReLU(inplace), ReLU(inplace), ReLU(inplace), ReLU(inplace)])
vgg_layers = children(m_vgg)[:23]
m_vgg = nn.Sequential(*vgg_layers).cuda().eval()
set_trainable(m_vgg, False)
def flatten(x): return x.view(x.size(0), -1)
class SaveFeatures():
features=None
def __init__(self, m):
self.hook = m.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()
class FeatureLoss(nn.Module):
def __init__(self, m, layer_ids, layer_wgts):
super().__init__()
self.m,self.wgts = m,layer_wgts
self.sfs = [SaveFeatures(m[i]) for i in layer_ids]

def forward(self, input, target, sum_layers=True):
self.m(VV(target.data))
res = [F.l1_loss(input,target)/100]
targ_feat = [V(o.features.data.clone()) for o in self.sfs]
self.m(input)
res += [F.l1_loss(flatten(inp.features),flatten(targ))*wgt
for inp,targ,wgt in zip(self.sfs, targ_feat,
self.wgts)]
if sum_layers: res = sum(res)
return res

def close(self):
for o in self.sfs: o.remove()
m = SrResnet(64, scale)
conv_shuffle = m.features[10][0][0]
kernel = icnr(conv_shuffle.weight, scale=scale)
conv_shuffle.weight.data.copy_(kernel);
m = to_gpu(m)learn = Learner(md, SingleModel(m), opt_fn=optim.Adam)t = torch.load(learn.get_model_path('sr-samp0'), 
map_location=lambda storage, loc: storage)
learn.model.load_state_dict(t, strict=False)
learn.freeze_to(999)for i in range(10,13): set_trainable(m.features[i], True)conv_shuffle = m.features[10][2][0]
kernel = icnr(conv_shuffle.weight, scale=scale)
conv_shuffle.weight.data.copy_(kernel);
m = nn.DataParallel(m, [0,2])
learn = Learner(md, SingleModel(m), opt_fn=optim.Adam)
learn.set_data(md)
learn.crit = FeatureLoss(m_vgg, blocks[:3], [0.2,0.7,0.1])lr=6e-3
wd=1e-7
learn.lr_find(1e-4, 0.1, wds=wd, linear=True) 1%|          | 15/1801 [00:06<12:55,  2.30it/s, loss=0.0965]
12%|█▏ | 220/1801 [01:16<09:08, 2.88it/s, loss=0.42]
learn.sched.plot(n_skip_end=1)
learn.fit(lr, 1, cycle_len=2, wds=wd, use_clr=(20,10))epoch      trn_loss   val_loss                                  
0 0.04523 0.042932
1 0.043574 0.041242
[array([0.04124])]learn.save('sr-samp0')learn.save('sr-samp1')

Progressive resizing [59:07]

learn.load('sr-samp1')lr=3e-3learn.fit(lr, 1, cycle_len=1, wds=wd, use_clr=(20,10))epoch      trn_loss   val_loss                                
0 0.069054 0.06638
[array([0.06638])]learn.save('sr-samp2')learn.unfreeze()learn.load('sr-samp2')learn.fit(lr/3, 1, cycle_len=1, wds=wd, use_clr=(20,10))epoch trn_loss val_loss
0 0.06042 0.057613
[array([0.05761])]learn.save('sr1')learn.sched.plot_loss()
def plot_ds_img(idx, ax=None, figsize=(7,7), normed=True):
if ax is None: fig,ax = plt.subplots(figsize=figsize)
im = md.val_ds[idx][0]
if normed: im = denorm(im)[0]
else: im = np.rollaxis(to_np(im),0,3)
ax.imshow(im)
ax.axis('off')
fig,axes=plt.subplots(6,6,figsize=(20,20))
for i,ax in enumerate(axes.flat):
plot_ds_img(i+200,ax=ax, normed=True)
x,y=md.val_ds[215]y=y[None]learn.model.eval()
preds = learn.model(VV(x[None]))
x.shape,y.shape,preds.shape
((3, 72, 72), (1, 3, 288, 288), torch.Size([1, 3, 288, 288]))learn.crit(preds, V(y), sum_layers=False)[Variable containing:
1.00000e-03 *
1.1935
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
1.00000e-03 *
8.5054
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
1.00000e-02 *
3.4656
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
1.00000e-03 *
3.8243
[torch.cuda.FloatTensor of size 1 (GPU 0)]]
learn.crit.close()
_,axes=plt.subplots(1,2,figsize=(14,7))
show_img(x[None], 0, ax=axes[0])
show_img(preds,0, normed=True, ax=axes[1])

Ask Jeremy Anything

Super resolution network to a style transfer network [1:17:57]

Style transfer net [1:19:19]

%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai.conv_learner import *
from pathlib import Path
torch.cuda.set_device(0)
torch.backends.cudnn.benchmark=TruePATH = Path('data/imagenet')
PATH_TRN = PATH/'train'
fnames_full,label_arr_full,all_labels = folder_source(PATH, 'train')
fnames_full = ['/'.join(Path(fn).parts[-2:]) for fn in fnames_full]
list(zip(fnames_full[:5],label_arr_full[:5]))
[('n01440764/n01440764_9627.JPEG', 0),
('n01440764/n01440764_9609.JPEG', 0),
('n01440764/n01440764_5176.JPEG', 0),
('n01440764/n01440764_6936.JPEG', 0),
('n01440764/n01440764_4005.JPEG', 0)]
all_labels[:5]['n01440764', 'n01443537', 'n01484850', 'n01491361', 'n01494475']np.random.seed(42)
# keep_pct = 1.
# keep_pct = 0.01
keep_pct = 0.1
keeps = np.random.rand(len(fnames_full)) < keep_pct
fnames = np.array(fnames_full, copy=False)[keeps]
label_arr = np.array(label_arr_full, copy=False)[keeps]
arch = vgg16
# sz,bs = 96,32
sz,bs = 256,24
# sz,bs = 128,32
class MatchedFilesDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
assert(len(fnames)==len(y))
super().__init__(fnames, transform, path)
def get_y(self, i):
return open_image(os.path.join(self.path, self.y[i]))
def get_c(self): return 0
val_idxs = get_cv_idxs(len(fnames), val_pct=min(0.01/keep_pct, 0.1))
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs,
np.array(fnames), np.array(fnames))
len(val_x),len(trn_x)
(12800, 115206)img_fn = PATH/'train'/'n01558993'/'n01558993_9684.JPEG'tfms = tfms_from_model(arch, sz, tfm_y=TfmType.PIXEL)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH_TRN)
md = ImageData(PATH, datasets, bs, num_workers=16, classes=None)
denorm = md.val_ds.denormdef show_img(ims, idx, figsize=(5,5), normed=True, ax=None):
if ax is None: fig,ax = plt.subplots(figsize=figsize)
if normed: ims = denorm(ims)
else: ims = np.rollaxis(to_np(ims),1,4)
ax.imshow(np.clip(ims,0,1)[idx])
ax.axis('off')

Model [1:19:30]

def conv(ni, nf, kernel_size=3, stride=1, actn=True, pad=None, 
bn=True):
if pad is None: pad = kernel_size//2
layers = [nn.Conv2d(ni, nf, kernel_size, stride=stride,
padding=pad, bias=not bn)]
if actn: layers.append(nn.ReLU(inplace=True))
if bn: layers.append(nn.BatchNorm2d(nf))
return nn.Sequential(*layers)
class ResSequentialCenter(nn.Module):
def __init__(self, layers):
super().__init__()
self.m = nn.Sequential(*layers)
def forward(self, x): return x[:, :, 2:-2, 2:-2] + self.m(x)def res_block(nf):
return ResSequentialCenter([conv(nf, nf, actn=True, pad=0),
conv(nf, nf, pad=0)])
def upsample(ni, nf):
return nn.Sequential(nn.Upsample(scale_factor=2), conv(ni, nf))
class StyleResnet(nn.Module):
def __init__(self):
super().__init__()
features = [nn.ReflectionPad2d(40),
conv(3, 32, 9),
conv(32, 64, stride=2), conv(64, 128, stride=2)]
for i in range(5): features.append(res_block(128))
features += [upsample(128, 64), upsample(64, 32),
conv(32, 3, 9, actn=False)]
self.features = nn.Sequential(*features)

def forward(self, x): return self.features(x)

Style Image [1:21:02]

style_fn = PATH/'style'/'starry_night.jpg'
style_img = open_image(style_fn)
style_img.shape
(1198, 1513, 3)plt.imshow(style_img);
h,w,_ = style_img.shape
rat = max(sz/h,sz/h)
res = cv2.resize(style_img, (int(w*rat), int(h*rat)), interpolation=cv2.INTER_AREA)
resz_style = res[:sz,-sz:]
plt.imshow(resz_style);
style_tfm,_ = tfms[1](resz_style,resz_style)style_tfm = np.broadcast_to(style_tfm[None], (bs,)+style_tfm.shape)
style_tfm.shape(24, 3, 256, 256)

Perceptual loss [1:21:51]

m_vgg = vgg16(True)blocks = [i-1 for i,o in enumerate(children(m_vgg))
if isinstance(o,nn.MaxPool2d)]
blocks, [m_vgg[i] for i in blocks[1:]]
([5, 12, 22, 32, 42],
[ReLU(inplace), ReLU(inplace), ReLU(inplace), ReLU(inplace)])
vgg_layers = children(m_vgg)[:43]
m_vgg = nn.Sequential(*vgg_layers).cuda().eval()
set_trainable(m_vgg, False)
def flatten(x): return x.view(x.size(0), -1)class SaveFeatures():
features=None
def __init__(self, m):
self.hook = m.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()
def ct_loss(input, target): return F.mse_loss(input,target)def gram(input):
b,c,h,w = input.size()
x = input.view(b, c, -1)
return torch.bmm(x, x.transpose(1,2))/(c*h*w)*1e6
def gram_loss(input, target):
return F.mse_loss(gram(input), gram(target[:input.size(0)]))
class CombinedLoss(nn.Module):
def __init__(self, m, layer_ids, style_im, ct_wgt, style_wgts):
super().__init__()
self.m,self.ct_wgt,self.style_wgts = m,ct_wgt,style_wgts
self.sfs = [SaveFeatures(m[i]) for i in layer_ids]
m(VV(style_im))
self.style_feat = [V(o.features.data.clone())
for o in self.sfs]
def forward(self, input, target, sum_layers=True):
self.m(VV(target.data))
targ_feat = self.sfs[2].features.data.clone()
self.m(input)
inp_feat = [o.features for o in self.sfs]

res = [ct_loss(inp_feat[2],V(targ_feat)) * self.ct_wgt]
res += [gram_loss(inp,targ)*wgt for inp,targ,wgt
in zip(inp_feat, self.style_feat, self.style_wgts)]

if sum_layers: res = sum(res)
return res

def close(self):
for o in self.sfs: o.remove()
m = StyleResnet()
m = to_gpu(m)
learn = Learner(md, SingleModel(m), opt_fn=optim.Adam)
learn.crit = CombinedLoss(m_vgg, blocks[1:], style_tfm, 1e4,
[0.025,0.275,5.,0.2])
wd=1e-7learn.lr_find(wds=wd)
learn.sched.plot(n_skip_end=1)
1%|▏ | 7/482 [00:04<05:32, 1.43it/s, loss=2.48e+04]
53%|█████▎ | 254/482 [02:27<02:12, 1.73it/s, loss=1.13e+12]
lr=5e-3
learn.fit(lr, 1, cycle_len=1, wds=wd, use_clr=(20,10))epoch      trn_loss   val_loss                               
0 105.351372 105.833994
[array([105.83399])]learn.save('style-2')x,y=md.val_ds[201]learn.model.eval()
preds = learn.model(VV(x[None]))
x.shape,y.shape,preds.shape
((3, 256, 256), (3, 256, 256), torch.Size([1, 3, 256, 256]))
learn.crit(preds, VV(y[None]), sum_layers=False)[Variable containing:
53.2221
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
3.8336
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
4.0612
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
5.0639
[torch.cuda.FloatTensor of size 1 (GPU 0)], Variable containing:
53.0019
[torch.cuda.FloatTensor of size 1 (GPU 0)]]
learn.crit.close()_,axes=plt.subplots(1,2,figsize=(14,7))
show_img(x[None], 0, ax=axes[0])
show_img(preds, 0, ax=axes[1])

Segmentation [1:29:13]

Doing it the simple way [1:31:26]

%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai.conv_learner import *
from fastai.dataset import *

from pathlib import Path
import json

Setup

PATH = Path('data/carvana')
list(PATH.iterdir())
[PosixPath('data/carvana/train_masks.csv'),
PosixPath('data/carvana/train_masks-128'),
PosixPath('data/carvana/sample_submission.csv'),
PosixPath('data/carvana/train_masks_png'),
PosixPath('data/carvana/train.csv'),
PosixPath('data/carvana/train-128'),
PosixPath('data/carvana/train'),
PosixPath('data/carvana/metadata.csv'),
PosixPath('data/carvana/tmp'),
PosixPath('data/carvana/models'),
PosixPath('data/carvana/train_masks')]
MASKS_FN = 'train_masks.csv'
META_FN = 'metadata.csv'
TRAIN_DN = 'train'
MASKS_DN = 'train_masks'
masks_csv = pd.read_csv(PATH/MASKS_FN)
masks_csv.head()
meta_csv = pd.read_csv(PATH/META_FN)
meta_csv.head()
def show_img(im, figsize=None, ax=None, alpha=None):
if not ax: fig,ax = plt.subplots(figsize=figsize)
ax.imshow(im, alpha=alpha)
ax.set_axis_off()
return ax
CAR_ID = '00087a6bd4dc'list((PATH/TRAIN_DN).iterdir())[:5][PosixPath('data/carvana/train/5ab34f0e3ea5_15.jpg'),
PosixPath('data/carvana/train/de3ca5ec1e59_07.jpg'),
PosixPath('data/carvana/train/28d9a149cb02_13.jpg'),
PosixPath('data/carvana/train/36a3f7f77e85_12.jpg'),
PosixPath('data/carvana/train/843763f47895_08.jpg')]
Image.open(PATH/TRAIN_DN/f'{CAR_ID}_01.jpg').resize((300,200))
list((PATH/MASKS_DN).iterdir())[:5][PosixPath('data/carvana/train_masks/6c0cd487abcd_03_mask.gif'),
PosixPath('data/carvana/train_masks/351c583eabd6_01_mask.gif'),
PosixPath('data/carvana/train_masks/90fdd8932877_02_mask.gif'),
PosixPath('data/carvana/train_masks/28d9a149cb02_10_mask.gif'),
PosixPath('data/carvana/train_masks/88bc32b9e1d9_14_mask.gif')]
Image.open(PATH/MASKS_DN/f'{CAR_ID}_01_mask.gif').resize((300,200))
ims = [open_image(PATH/TRAIN_DN/f'{CAR_ID}_{i+1:02d}.jpg') 
for i in range(16)]
fig, axes = plt.subplots(4, 4, figsize=(9, 6))
for i,ax in enumerate(axes.flat): show_img(ims[i], ax=ax)
plt.tight_layout(pad=0.1)

Resize and convert [1:33:27]

(PATH/'train_masks_png').mkdir(exist_ok=True)def convert_img(fn):
fn = fn.name
Image.open(PATH/'train_masks'/fn).save(PATH/'train_masks_png'/
f'{fn[:-4]}.png')
files = list((PATH/'train_masks').iterdir())
with ThreadPoolExecutor(8) as e: e.map(convert_img, files)
(PATH/'train_masks-128').mkdir(exist_ok=True)def resize_mask(fn):
Image.open(fn).resize((128,128)).save((fn.parent.parent)
/'train_masks-128'/fn.name)

files = list((PATH/'train_masks_png').iterdir())
with ThreadPoolExecutor(8) as e: e.map(resize_img, files)
(PATH/'train-128').mkdir(exist_ok=True)def resize_img(fn):
Image.open(fn).resize((128,128)).save((fn.parent.parent)
/'train-128'/fn.name)

files = list((PATH/'train').iterdir())
with ThreadPoolExecutor(8) as e: e.map(resize_img, files)

Dataset [1:35:33]

TRAIN_DN = 'train-128'
MASKS_DN = 'train_masks-128'
sz = 128
bs = 64
ims = [open_image(PATH/TRAIN_DN
/f'{CAR_ID}_{i+1:02d}.jpg') for i in range(16)]
im_masks = [open_image(PATH/MASKS_DN
/f'{CAR_ID}_{i+1:02d}_mask.png') for i in range(16)]
fig, axes = plt.subplots(4, 4, figsize=(9, 6))
for i,ax in enumerate(axes.flat):
ax = show_img(ims[i], ax=ax)
show_img(im_masks[i][...,0], ax=ax, alpha=0.5)
plt.tight_layout(pad=0.1)
class MatchedFilesDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
assert(len(fnames)==len(y))
super().__init__(fnames, transform, path)
def get_y(self, i):
return open_image(os.path.join(self.path, self.y[i]))
def get_c(self): return 0
x_names = np.array([Path(TRAIN_DN)/o for o in masks_csv['img']])
y_names = np.array([Path(MASKS_DN)/f'{o[:-4]}_mask.png'
for o in masks_csv['img']])
len(x_names)//16//5*161008
val_idxs = list(range(1008))
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names,
y_names)
len(val_x),len(trn_x)
(1008, 4080)
aug_tfms = [RandomRotate(4, tfm_y=TfmType.CLASS),
RandomFlip(tfm_y=TfmType.CLASS),
RandomLighting(0.05, 0.05)]
# aug_tfms = []
tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO, tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y), (val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=8, classes=None)
denorm = md.trn_ds.denorm
x,y = next(iter(md.aug_dl))
x = denorm(x)
fig, axes = plt.subplots(5, 6, figsize=(12, 10))
for i,ax in enumerate(axes.flat):
ax=show_img(x[i], ax=ax)
show_img(y[i], ax=ax, alpha=0.5)
plt.tight_layout(pad=0.1)

Model [1:38:30]

class Empty(nn.Module): 
def forward(self,x): return x

models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=Empty())
learn = ConvLearner(md, models)
learn.summary()
class StdUpsample(nn.Module):
def __init__(self, nin, nout):
super().__init__()
self.conv = nn.ConvTranspose2d(nin, nout, 2, stride=2)
self.bn = nn.BatchNorm2d(nout)

def forward(self, x): return self.bn(F.relu(self.conv(x)))
flatten_channel = Lambda(lambda x: x[:,0])simple_up = nn.Sequential(
nn.ReLU(),
StdUpsample(512,256),
StdUpsample(256,256),
StdUpsample(256,256),
StdUpsample(256,256),
nn.ConvTranspose2d(256, 1, 2, stride=2),
flatten_channel
)
models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5)]
learn.lr_find()
learn.sched.plot()
94%|█████████▍| 30/32 [00:05<00:00, 5.48it/s, loss=10.6]
lr=4e-2learn.fit(lr,1,cycle_len=5,use_clr=(20,5))epoch      trn_loss   val_loss   <lambda>                  
0 0.124078 0.133566 0.945951
1 0.111241 0.112318 0.954912
2 0.099743 0.09817 0.957507
3 0.090651 0.092375 0.958117
4 0.084031 0.086026 0.963243
[0.086025625, 0.96324310824275017]
learn.save('tmp')learn.load('tmp')py,ay = learn.predict_with_targs()ay.shape(1008, 128, 128)
show_img(ay[0]);
show_img(py[0]>0);
learn.unfreeze()learn.bn_freeze(True)lrs = np.array([lr/100,lr/10,lr])/4learn.fit(lrs,1,cycle_len=20,use_clr=(20,10))epoch      trn_loss   val_loss   <lambda>                   
0 0.06577 0.053292 0.972977
1 0.049475 0.043025 0.982559
2 0.039146 0.035927 0.98337
3 0.03405 0.031903 0.986982
4 0.029788 0.029065 0.987944
5 0.027374 0.027752 0.988029
6 0.026041 0.026718 0.988226
7 0.024302 0.025927 0.989512
8 0.022921 0.026102 0.988276
9 0.021944 0.024714 0.989537
10 0.021135 0.0241 0.990628
11 0.020494 0.023367 0.990652
12 0.01988 0.022961 0.990989
13 0.019241 0.022498 0.991014
14 0.018697 0.022492 0.990571
15 0.01812 0.021771 0.99105
16 0.017597 0.02183 0.991365
17 0.017192 0.021434 0.991364
18 0.016768 0.021383 0.991643
19 0.016418 0.021114 0.99173
[0.021113895, 0.99172959849238396]
learn.save('0')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
ax = show_img(denorm(x)[0])
show_img(py[0]>0, ax=ax, alpha=0.5);
ax = show_img(denorm(x)[0])
show_img(y[0], ax=ax, alpha=0.5);

512x512 [1:42:50]

TRAIN_DN = 'train'
MASKS_DN = 'train_masks_png'
sz = 512
bs = 16
x_names = np.array([Path(TRAIN_DN)/o for o in masks_csv['img']])
y_names = np.array([Path(MASKS_DN)/f'{o[:-4]}_mask.png'
for o in masks_csv['img']])
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names,
y_names)
len(val_x),len(trn_x)
(1008, 4080)tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO,
tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=8, classes=None)
denorm = md.trn_ds.denorm
x,y = next(iter(md.aug_dl))
x = denorm(x)
fig, axes = plt.subplots(4, 4, figsize=(10, 10))
for i,ax in enumerate(axes.flat):
ax=show_img(x[i], ax=ax)
show_img(y[i], ax=ax, alpha=0.5)
plt.tight_layout(pad=0.1)
simple_up = nn.Sequential(
nn.ReLU(),
StdUpsample(512,256),
StdUpsample(256,256),
StdUpsample(256,256),
StdUpsample(256,256),
nn.ConvTranspose2d(256, 1, 2, stride=2),
flatten_channel
)
models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5)]
learn.load('0')learn.lr_find()
learn.sched.plot()
85%|████████▌ | 218/255 [02:12<00:22, 1.64it/s, loss=8.91]
lr=4e-2learn.fit(lr,1,cycle_len=5,use_clr=(20,5))epoch      trn_loss   val_loss   <lambda>                     
0 0.02178 0.020653 0.991708
1 0.017927 0.020653 0.990241
2 0.015958 0.016115 0.993394
3 0.015172 0.015143 0.993696
4 0.014315 0.014679 0.99388
[0.014679321, 0.99388032489352751]learn.save('tmp')learn.load('tmp')learn.unfreeze()
learn.bn_freeze(True)
lrs = np.array([lr/100,lr/10,lr])/4learn.fit(lrs,1,cycle_len=8,use_clr=(20,8))epoch trn_loss val_loss mask_acc
0 0.038687 0.018685 0.992782
1 0.024906 0.014355 0.994933
2 0.025055 0.014737 0.995526
3 0.024155 0.014083 0.995708
4 0.013446 0.010564 0.996166
5 0.01607 0.010555 0.996096
6 0.019197 0.010883 0.99621
7 0.016157 0.00998 0.996393
[0.0099797687, 0.99639255659920833]learn.save('512')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
ax = show_img(denorm(x)[0])
show_img(py[0]>0, ax=ax, alpha=0.5);
ax = show_img(denorm(x)[0])
show_img(y[0], ax=ax, alpha=0.5);

1024x1024 [1:43:17]

sz = 1024
bs = 4
tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO,
tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=8, classes=None)
denorm = md.trn_ds.denorm
x,y = next(iter(md.aug_dl))
x = denorm(x)
y = to_np(y)
fig, axes = plt.subplots(2, 2, figsize=(8, 8))
for i,ax in enumerate(axes.flat):
show_img(x[i], ax=ax)
show_img(y[i], ax=ax, alpha=0.5)
plt.tight_layout(pad=0.1)
simple_up = nn.Sequential(
nn.ReLU(),
StdUpsample(512,256),
StdUpsample(256,256),
StdUpsample(256,256),
StdUpsample(256,256),
nn.ConvTranspose2d(256, 1, 2, stride=2),
flatten_channel,
)
models = ConvnetBuilder(resnet34, 0, 0, 0, custom_head=simple_up)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5)]
learn.load('512')learn.lr_find()
learn.sched.plot()
85%|████████▌ | 218/255 [02:12<00:22, 1.64it/s, loss=8.91]
lr=4e-2learn.fit(lr,1,cycle_len=2,use_clr=(20,4))epoch      trn_loss   val_loss   <lambda>                       
0 0.01066 0.011119 0.996227
1 0.009357 0.009696 0.996553
[0.0096957013, 0.99655332546385511]learn.save('tmp')learn.load('tmp')learn.unfreeze()
learn.bn_freeze(True)
lrs = np.array([lr/100,lr/10,lr])/8learn.fit(lrs,1,cycle_len=40,use_clr=(20,10))epoch trn_loss val_loss mask_acc
0 0.015565 0.007449 0.997661
1 0.01979 0.008376 0.997542
2 0.014874 0.007826 0.997736
3 0.016104 0.007854 0.997347
4 0.023386 0.009745 0.997218
5 0.018972 0.008453 0.997588
6 0.013184 0.007612 0.997588
7 0.010686 0.006775 0.997688
8 0.0293 0.015299 0.995782
9 0.018713 0.00763 0.997638
10 0.015432 0.006575 0.9978
11 0.110205 0.060062 0.979043
12 0.014374 0.007753 0.997451
13 0.022286 0.010282 0.997587
14 0.015645 0.00739 0.997776
15 0.013821 0.00692 0.997869
16 0.022389 0.008632 0.997696
17 0.014607 0.00677 0.997837
18 0.018748 0.008194 0.997657
19 0.016447 0.007237 0.997899
20 0.023596 0.008211 0.997918
21 0.015721 0.00674 0.997848
22 0.01572 0.006415 0.998006
23 0.019519 0.007591 0.997876
24 0.011159 0.005998 0.998053
25 0.010291 0.005806 0.998012
26 0.010893 0.005755 0.998046
27 0.014534 0.006313 0.997901
28 0.020971 0.006855 0.998018
29 0.014074 0.006107 0.998053
30 0.01782 0.006561 0.998114
31 0.01742 0.006414 0.997942
32 0.016829 0.006514 0.9981
33 0.013148 0.005819 0.998033
34 0.023495 0.006261 0.997856
35 0.010931 0.005516 0.99812
36 0.015798 0.006176 0.998126
37 0.021636 0.005931 0.998067
38 0.012133 0.005496 0.998158
39 0.012562 0.005678 0.998172
[0.0056782686, 0.99817223208291195]learn.save('1024')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
ax = show_img(denorm(x)[0])
show_img(py[0][0]>0, ax=ax, alpha=0.5);
ax = show_img(denorm(x)[0])
show_img(y[0,...,-1], ax=ax, alpha=0.5);
show_img(py[0][0]>0);
show_img(y[0,...,-1]);

U-Net [1:43:45]

%matplotlib inline
%reload_ext autoreload
%autoreload 2
from fastai.conv_learner import *
from fastai.dataset import *
from fastai.models.resnet import vgg_resnet50

import json
torch.backends.cudnn.benchmark=True

Data

PATH = Path('data/carvana')
MASKS_FN = 'train_masks.csv'
META_FN = 'metadata.csv'
masks_csv = pd.read_csv(PATH/MASKS_FN)
meta_csv = pd.read_csv(PATH/META_FN)
def show_img(im, figsize=None, ax=None, alpha=None):
if not ax: fig,ax = plt.subplots(figsize=figsize)
ax.imshow(im, alpha=alpha)
ax.set_axis_off()
return ax
TRAIN_DN = 'train-128'
MASKS_DN = 'train_masks-128'
sz = 128
bs = 64
nw = 16
TRAIN_DN = 'train'
MASKS_DN = 'train_masks_png'
sz = 128
bs = 64
nw = 16
class MatchedFilesDataset(FilesDataset):
def __init__(self, fnames, y, transform, path):
self.y=y
assert(len(fnames)==len(y))
super().__init__(fnames, transform, path)
def get_y(self, i):
return open_image(os.path.join(self.path, self.y[i]))
def get_c(self): return 0
x_names = np.array([Path(TRAIN_DN)/o for o in masks_csv['img']])
y_names = np.array([Path(MASKS_DN)/f'{o[:-4]}_mask.png'
for o in masks_csv['img']])
val_idxs = list(range(1008))
((val_x,trn_x),(val_y,trn_y)) = split_by_idx(val_idxs, x_names,
y_names)
aug_tfms = [RandomRotate(4, tfm_y=TfmType.CLASS),
RandomFlip(tfm_y=TfmType.CLASS),
RandomLighting(0.05, 0.05, tfm_y=TfmType.CLASS)]
tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO,
tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=16, classes=None)
denorm = md.trn_ds.denorm
x,y = next(iter(md.trn_dl))x.shape,y.shape(torch.Size([64, 3, 128, 128]), torch.Size([64, 128, 128]))

Simple upsample

f = resnet34
cut,lr_cut = model_meta[f]
def get_base():
layers = cut_model(f(True), cut)
return nn.Sequential(*layers)
def dice(pred, targs):
pred = (pred>0).float()
return 2. * (pred*targs).sum() / (pred+targs).sum()
class StdUpsample(nn.Module):
def __init__(self, nin, nout):
super().__init__()
self.conv = nn.ConvTranspose2d(nin, nout, 2, stride=2)
self.bn = nn.BatchNorm2d(nout)

def forward(self, x): return self.bn(F.relu(self.conv(x)))
class Upsample34(nn.Module):
def __init__(self, rn):
super().__init__()
self.rn = rn
self.features = nn.Sequential(
rn, nn.ReLU(),
StdUpsample(512,256),
StdUpsample(256,256),
StdUpsample(256,256),
StdUpsample(256,256),
nn.ConvTranspose2d(256, 1, 2, stride=2))

def forward(self,x): return self.features(x)[:,0]
class UpsampleModel():
def __init__(self,model,name='upsample'):
self.model,self.name = model,name

def get_layer_groups(self, precompute):
lgs = list(split_by_idxs(children(self.model.rn), [lr_cut]))
return lgs + [children(self.model.features)[1:]]
m_base = get_base() m = to_gpu(Upsample34(m_base))
models = UpsampleModel(m)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5),dice]
learn.freeze_to(1)learn.lr_find()
learn.sched.plot()
86%|█████████████████████████████████████████████████████████████ | 55/64 [00:22<00:03, 2.46it/s, loss=3.21]
lr=4e-2
wd=1e-7
lrs = np.array([lr/100,lr/10,lr])/2
learn.fit(lr,1, wds=wd, cycle_len=4,use_clr=(20,8))0%| | 0/64 [00:00<?, ?it/s]
epoch trn_loss val_loss <lambda> dice
0 0.216882 0.133512 0.938017 0.855221
1 0.169544 0.115158 0.946518 0.878381
2 0.153114 0.099104 0.957748 0.903353
3 0.144105 0.093337 0.964404 0.915084
[0.09333742126112893, 0.9644036065964472, 0.9150839788573129]learn.save('tmp')learn.load('tmp')learn.unfreeze()
learn.bn_freeze(True)
learn.fit(lrs,1,cycle_len=4,use_clr=(20,8))epoch trn_loss val_loss <lambda> dice
0 0.174897 0.061603 0.976321 0.94382
1 0.122911 0.053625 0.982206 0.957624
2 0.106837 0.046653 0.985577 0.965792
3 0.099075 0.042291 0.986519 0.968925
[0.042291240323157536, 0.986519161670927, 0.9689251193924556]
learn.save('128')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
show_img(py[0]>0);
show_img(y[0]);

U-net (ish) [1:48:16]

class SaveFeatures():
features=None
def __init__(self, m):
self.hook = m.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()
class UnetBlock(nn.Module):
def __init__(self, up_in, x_in, n_out):
super().__init__()
up_out = x_out = n_out//2
self.x_conv = nn.Conv2d(x_in, x_out, 1)
self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2,
stride=2)
self.bn = nn.BatchNorm2d(n_out)

def forward(self, up_p, x_p):
up_p = self.tr_conv(up_p)
x_p = self.x_conv(x_p)
cat_p = torch.cat([up_p,x_p], dim=1)
return self.bn(F.relu(cat_p))
class Unet34(nn.Module):
def __init__(self, rn):
super().__init__()
self.rn = rn
self.sfs = [SaveFeatures(rn[i]) for i in [2,4,5,6]]
self.up1 = UnetBlock(512,256,256)
self.up2 = UnetBlock(256,128,256)
self.up3 = UnetBlock(256,64,256)
self.up4 = UnetBlock(256,64,256)
self.up5 = nn.ConvTranspose2d(256, 1, 2, stride=2)

def forward(self,x):
x = F.relu(self.rn(x))
x = self.up1(x, self.sfs[3].features)
x = self.up2(x, self.sfs[2].features)
x = self.up3(x, self.sfs[1].features)
x = self.up4(x, self.sfs[0].features)
x = self.up5(x)
return x[:,0]

def close(self):
for sf in self.sfs: sf.remove()
class UnetModel():
def __init__(self,model,name='unet'):
self.model,self.name = model,name

def get_layer_groups(self, precompute):
lgs = list(split_by_idxs(children(self.model.rn), [lr_cut]))
return lgs + [children(self.model)[1:]]
m_base = get_base()
m = to_gpu(Unet34(m_base))
models = UnetModel(m)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5),dice]
learn.summary()OrderedDict([('Conv2d-1',
OrderedDict([('input_shape', [-1, 3, 128, 128]),
('output_shape', [-1, 64, 64, 64]),
('trainable', False),
('nb_params', 9408)])),
('BatchNorm2d-2',
OrderedDict([('input_shape', [-1, 64, 64, 64]),
('output_shape', [-1, 64, 64, 64]),
('trainable', False),
('nb_params', 128)])),
('ReLU-3',
OrderedDict([('input_shape', [-1, 64, 64, 64]),
('output_shape', [-1, 64, 64, 64]),
('nb_params', 0)])),
('MaxPool2d-4',
OrderedDict([('input_shape', [-1, 64, 64, 64]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-5',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-6',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-7',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-8',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-9',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-10',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('BasicBlock-11',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-12',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-13',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-14',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-15',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-16',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-17',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('BasicBlock-18',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-19',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-20',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-21',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-22',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 36864)])),
('BatchNorm2d-23',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('trainable', False),
('nb_params', 128)])),
('ReLU-24',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('BasicBlock-25',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 64, 32, 32]),
('nb_params', 0)])),
('Conv2d-26',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 73728)])),
('BatchNorm2d-27',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-28',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-29',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-30',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('Conv2d-31',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 8192)])),
('BatchNorm2d-32',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-33',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('BasicBlock-34',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-35',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-36',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-37',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-38',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-39',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-40',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('BasicBlock-41',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-42',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-43',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-44',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-45',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-46',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-47',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('BasicBlock-48',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-49',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-50',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-51',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-52',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 147456)])),
('BatchNorm2d-53',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', False),
('nb_params', 256)])),
('ReLU-54',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('BasicBlock-55',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('nb_params', 0)])),
('Conv2d-56',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 294912)])),
('BatchNorm2d-57',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-58',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-59',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-60',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('Conv2d-61',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 32768)])),
('BatchNorm2d-62',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-63',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-64',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-65',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-66',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-67',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-68',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-69',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-70',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-71',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-72',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-73',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-74',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-75',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-76',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-77',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-78',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-79',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-80',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-81',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-82',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-83',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-84',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-85',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-86',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-87',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-88',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-89',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-90',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-91',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-92',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-93',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-94',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-95',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-96',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 589824)])),
('BatchNorm2d-97',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', False),
('nb_params', 512)])),
('ReLU-98',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('BasicBlock-99',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('Conv2d-100',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1179648)])),
('BatchNorm2d-101',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-102',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('Conv2d-103',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 2359296)])),
('BatchNorm2d-104',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('Conv2d-105',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 131072)])),
('BatchNorm2d-106',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-107',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('BasicBlock-108',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('Conv2d-109',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 2359296)])),
('BatchNorm2d-110',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-111',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('Conv2d-112',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 2359296)])),
('BatchNorm2d-113',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-114',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('BasicBlock-115',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('Conv2d-116',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 2359296)])),
('BatchNorm2d-117',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-118',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('Conv2d-119',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 2359296)])),
('BatchNorm2d-120',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('trainable', False),
('nb_params', 1024)])),
('ReLU-121',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('BasicBlock-122',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 512, 4, 4]),
('nb_params', 0)])),
('ConvTranspose2d-123',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 128, 8, 8]),
('trainable', True),
('nb_params', 262272)])),
('Conv2d-124',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 128, 8, 8]),
('trainable', True),
('nb_params', 32896)])),
('BatchNorm2d-125',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 8, 8]),
('trainable', True),
('nb_params', 512)])),
('UnetBlock-126',
OrderedDict([('input_shape', [-1, 512, 4, 4]),
('output_shape', [-1, 256, 8, 8]),
('nb_params', 0)])),
('ConvTranspose2d-127',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 128, 16, 16]),
('trainable', True),
('nb_params', 131200)])),
('Conv2d-128',
OrderedDict([('input_shape', [-1, 128, 16, 16]),
('output_shape', [-1, 128, 16, 16]),
('trainable', True),
('nb_params', 16512)])),
('BatchNorm2d-129',
OrderedDict([('input_shape', [-1, 256, 16, 16]),
('output_shape', [-1, 256, 16, 16]),
('trainable', True),
('nb_params', 512)])),
('UnetBlock-130',
OrderedDict([('input_shape', [-1, 256, 8, 8]),
('output_shape', [-1, 256, 16, 16]),
('nb_params', 0)])),
('ConvTranspose2d-131',
OrderedDict([('input_shape', [-1, 256, 16, 16]),
('output_shape', [-1, 128, 32, 32]),
('trainable', True),
('nb_params', 131200)])),
('Conv2d-132',
OrderedDict([('input_shape', [-1, 64, 32, 32]),
('output_shape', [-1, 128, 32, 32]),
('trainable', True),
('nb_params', 8320)])),
('BatchNorm2d-133',
OrderedDict([('input_shape', [-1, 256, 32, 32]),
('output_shape', [-1, 256, 32, 32]),
('trainable', True),
('nb_params', 512)])),
('UnetBlock-134',
OrderedDict([('input_shape', [-1, 256, 16, 16]),
('output_shape', [-1, 256, 32, 32]),
('nb_params', 0)])),
('ConvTranspose2d-135',
OrderedDict([('input_shape', [-1, 256, 32, 32]),
('output_shape', [-1, 128, 64, 64]),
('trainable', True),
('nb_params', 131200)])),
('Conv2d-136',
OrderedDict([('input_shape', [-1, 64, 64, 64]),
('output_shape', [-1, 128, 64, 64]),
('trainable', True),
('nb_params', 8320)])),
('BatchNorm2d-137',
OrderedDict([('input_shape', [-1, 256, 64, 64]),
('output_shape', [-1, 256, 64, 64]),
('trainable', True),
('nb_params', 512)])),
('UnetBlock-138',
OrderedDict([('input_shape', [-1, 256, 32, 32]),
('output_shape', [-1, 256, 64, 64]),
('nb_params', 0)])),
('ConvTranspose2d-139',
OrderedDict([('input_shape', [-1, 256, 64, 64]),
('output_shape', [-1, 1, 128, 128]),
('trainable', True),
('nb_params', 1025)]))])
[o.features.size() for o in m.sfs][torch.Size([3, 64, 64, 64]),
torch.Size([3, 64, 32, 32]),
torch.Size([3, 128, 16, 16]),
torch.Size([3, 256, 8, 8])]
learn.freeze_to(1)learn.lr_find()
learn.sched.plot()
0%| | 0/64 [00:00<?, ?it/s]92%|█████████████████████████████████████████████████████████████████▍ | 59/64 [00:22<00:01, 2.68it/s, loss=2.45]
lr=4e-2
wd=1e-7

lrs = np.array([lr/100,lr/10,lr])
learn.fit(lr,1,wds=wd,cycle_len=8,use_clr=(5,8))epoch trn_loss val_loss <lambda> dice
0 0.12936 0.03934 0.988571 0.971385
1 0.098401 0.039252 0.990438 0.974921
2 0.087789 0.02539 0.990961 0.978927
3 0.082625 0.027984 0.988483 0.975948
4 0.079509 0.025003 0.99171 0.981221
5 0.076984 0.022514 0.992462 0.981881
6 0.076822 0.023203 0.992484 0.982321
7 0.075488 0.021956 0.992327 0.982704
[0.021955982234979434, 0.9923273126284281, 0.9827044502137199]learn.save('128urn-tmp')learn.load('128urn-tmp')learn.unfreeze()
learn.bn_freeze(True)
learn.fit(lrs/4, 1, wds=wd, cycle_len=20,use_clr=(20,10))0%| | 0/64 [00:00<?, ?it/s]
epoch trn_loss val_loss <lambda> dice
0 0.073786 0.023418 0.99297 0.98283
1 0.073561 0.020853 0.992142 0.982725
2 0.075227 0.023357 0.991076 0.980879
3 0.074245 0.02352 0.993108 0.983659
4 0.073434 0.021508 0.993024 0.983609
5 0.073092 0.020956 0.993188 0.983333
6 0.073617 0.019666 0.993035 0.984102
7 0.072786 0.019844 0.993196 0.98435
8 0.072256 0.018479 0.993282 0.984277
9 0.072052 0.019479 0.993164 0.984147
10 0.071361 0.019402 0.993344 0.984541
11 0.070969 0.018904 0.993139 0.984499
12 0.071588 0.018027 0.9935 0.984543
13 0.070709 0.018345 0.993491 0.98489
14 0.072238 0.019096 0.993594 0.984825
15 0.071407 0.018967 0.993446 0.984919
16 0.071047 0.01966 0.993366 0.984952
17 0.072024 0.018133 0.993505 0.98497
18 0.071517 0.018464 0.993602 0.985192
19 0.070109 0.018337 0.993614 0.9852
[0.018336569653853538, 0.9936137114252362, 0.9852004420189631]
learn.save('128urn-0')learn.load('128urn-0')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
show_img(py[0]>0);
show_img(y[0]);
m.close()

512x512 [1:56:26]

sz=512
bs=16
tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO,
tfm_y=TfmType.CLASS, aug_tfms=aug_tfms)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=4, classes=None)
denorm = md.trn_ds.denorm
m_base = get_base()
m = to_gpu(Unet34(m_base))
models = UnetModel(m)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5),dice]
learn.freeze_to(1)learn.load('128urn-0')learn.fit(lr,1,wds=wd, cycle_len=5,use_clr=(5,5))epoch trn_loss val_loss <lambda> dice
0 0.071421 0.02362 0.996459 0.991772
1 0.070373 0.014013 0.996558 0.992602
2 0.067895 0.011482 0.996705 0.992883
3 0.070653 0.014256 0.996695 0.992771
4 0.068621 0.013195 0.996993 0.993359
[0.013194938530288046, 0.996993034604996, 0.993358936574724]
learn.save('512urn-tmp')learn.unfreeze()
learn.bn_freeze(True)
learn.load('512urn-tmp')learn.fit(lrs/4,1,wds=wd, cycle_len=8,use_clr=(20,8))epoch trn_loss val_loss <lambda> dice
0 0.06605 0.013602 0.997 0.993014
1 0.066885 0.011252 0.997248 0.993563
2 0.065796 0.009802 0.997223 0.993817
3 0.065089 0.009668 0.997296 0.993744
4 0.064552 0.011683 0.997269 0.993835
5 0.065089 0.010553 0.997415 0.993827
6 0.064303 0.009472 0.997431 0.994046
7 0.062506 0.009623 0.997441 0.994118
[0.009623114736602894, 0.9974409020136273, 0.9941179137381296]
learn.save('512urn')learn.load('512urn')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
show_img(py[0]>0);
show_img(y[0]);
m.close()

1024x1024 [1:56:53]

sz=1024
bs=4
tfms = tfms_from_model(resnet34, sz, crop_type=CropType.NO,
tfm_y=TfmType.CLASS)
datasets = ImageData.get_ds(MatchedFilesDataset, (trn_x,trn_y),
(val_x,val_y), tfms, path=PATH)
md = ImageData(PATH, datasets, bs, num_workers=16, classes=None)
denorm = md.trn_ds.denorm
m_base = get_base()
m = to_gpu(Unet34(m_base))
models = UnetModel(m)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5),dice]
learn.load('512urn')learn.freeze_to(1)learn.fit(lr,1, wds=wd, cycle_len=2,use_clr=(5,4))epoch      trn_loss   val_loss   <lambda>   dice                 
0 0.007656 0.008155 0.997247 0.99353
1 0.004706 0.00509 0.998039 0.995437
[0.005090427414942828, 0.9980387706605215, 0.995437301104031]
learn.save('1024urn-tmp')learn.load('1024urn-tmp')learn.unfreeze()
learn.bn_freeze(True)
lrs = np.array([lr/200,lr/30,lr])learn.fit(lrs/10,1, wds=wd,cycle_len=4,use_clr=(20,8))epoch trn_loss val_loss <lambda> dice
0 0.005688 0.006135 0.997616 0.994616
1 0.004412 0.005223 0.997983 0.995349
2 0.004186 0.004975 0.99806 0.99554
3 0.004016 0.004899 0.99812 0.995627
[0.004898778487196458, 0.9981196409180051, 0.9956271404784823]learn.fit(lrs/10,1, wds=wd,cycle_len=4,use_clr=(20,8))epoch trn_loss val_loss <lambda> dice
0 0.004169 0.004962 0.998049 0.995517
1 0.004022 0.004595 0.99823 0.995818
2 0.003772 0.004497 0.998215 0.995916
3 0.003618 0.004435 0.998291 0.995991
[0.004434524739663753, 0.9982911745707194, 0.9959913929776539]
learn.sched.plot_loss()
learn.save('1024urn')learn.load('1024urn')x,y = next(iter(md.val_dl))
py = to_np(learn.model(V(x)))
show_img(py[0]>0);
show_img(y[0]);

Back to Bounding Box [1:58:15]


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade