# all_slow from fastai2.vision.all import * path = untar_data(URLs.IMAGENETTE_160) dls = ImageDataLoaders.from_folder(path, valid='val', item_tfms=RandomResizedCrop(128, min_scale=0.35), batch_tfms=Normalize.from_stats(*imagenet_stats)) dls.show_batch() fnames = get_image_files(path) dblock = DataBlock() dsets = dblock.datasets(fnames) dsets.train[0] dblock = DataBlock(get_items = get_image_files) dsets = dblock.datasets(path) dsets.train[0] parent_label(fnames[0]) lbl_dict = dict( n01440764='tench', n02102040='English springer', n02979186='cassette player', n03000684='chain saw', n03028079='church', n03394916='French horn', n03417042='garbage truck', n03425413='gas pump', n03445777='golf ball', n03888257='parachute' ) def label_func(fname): return lbl_dict[parent_label(fname)] dblock = DataBlock(get_items = get_image_files, get_y = label_func) dsets = dblock.datasets(path) dsets.train[0] dblock = DataBlock(blocks = (ImageBlock, CategoryBlock), get_items = get_image_files, get_y = label_func) dsets = dblock.datasets(path) dsets.train[0] dsets.vocab dblock = DataBlock(blocks = (ImageBlock, CategoryBlock), get_items = get_image_files, get_y = label_func, splitter = GrandparentSplitter()) dsets = dblock.datasets(path) dsets.train[0] dblock = DataBlock(blocks = (ImageBlock, CategoryBlock), get_items = get_image_files, get_y = label_func, splitter = GrandparentSplitter(), item_tfms = RandomResizedCrop(128, min_scale=0.35), batch_tfms=Normalize.from_stats(*imagenet_stats)) dls = dblock.dataloaders(path) dls.show_batch() imagenette = DataBlock(blocks = (ImageBlock, CategoryBlock), get_items = get_image_files, get_y = Pipeline([parent_label, lbl_dict.__getitem__]), splitter = GrandparentSplitter(valid_name='val'), item_tfms = RandomResizedCrop(128, min_scale=0.35), batch_tfms = Normalize.from_stats(*imagenet_stats)) dls = imagenette.dataloaders(path) dls.show_batch() source = untar_data(URLs.IMAGENETTE_160) fnames = get_image_files(source) PILImage.create(fnames[0]) lbl_dict[parent_label(fnames[0])] tfm = Pipeline([parent_label, lbl_dict.__getitem__, Categorize(vocab = lbl_dict.values())]) tfm(fnames[0]) splits = GrandparentSplitter(valid_name='val')(fnames) dsets = Datasets(fnames, [[PILImage.create], [parent_label, lbl_dict.__getitem__, Categorize]], splits=splits) dsets[0] dsets.show(dsets[0]); item_tfms = [ToTensor, RandomResizedCrop(128, min_scale=0.35)] batch_tfms = [IntToFloatTensor, Normalize.from_stats(*imagenet_stats)] dls = dsets.dataloaders(after_item=item_tfms, after_batch=batch_tfms, bs=64, num_workers=8) dls.show_batch() learn = cnn_learner(dls, resnet34, metrics=accuracy, pretrained=False) #slow learn.fit_one_cycle(5, 5e-3) learn = Learner(dls, xresnet34(n_out=10), metrics=accuracy) #slow learn.lr_find() #slow learn.fit_one_cycle(5, 1e-3) learn.show_results() class LabelSmoothingCE(Module): def __init__(self, eps=0.1, reduction='mean'): self.eps,self.reduction = eps,reduction def forward(self, output, target): c = output.size()[-1] log_preds = F.log_softmax(output, dim=-1) if self.reduction=='sum': loss = -log_preds.sum() else: loss = -log_preds.sum(dim=-1) #We divide by that size at the return line so sum and not mean if self.reduction=='mean': loss = loss.mean() return loss*self.eps/c + (1-self.eps) * F.nll_loss(log_preds, target.long(), reduction=self.reduction) def activation(self, out): return F.softmax(out, dim=-1) def decodes(self, out): return out.argmax(dim=-1) learn = Learner(dls, xresnet34(n_out=10), loss_func=LabelSmoothingCE(), metrics=accuracy) #slow learn.fit_one_cycle(5, 1e-3) learn.predict(fnames[0]) learn.show_results() @delegates(torch.optim.AdamW.__init__) def pytorch_adamw(param_groups, **kwargs): return OptimWrapper(torch.optim.AdamW([{'params': ps, **kwargs} for ps in param_groups])) learn = Learner(dls, xresnet18(), lr=1e-2, metrics=accuracy, loss_func=LabelSmoothingCrossEntropy(), opt_func=partial(pytorch_adamw, wd=0.01, eps=1e-3)) #slow learn.lr_find() #slow learn.fit_one_cycle(5, 5e-3) from torch.distributions.beta import Beta class Mixup(Callback): run_valid = False def __init__(self, alpha=0.4): self.distrib = Beta(tensor(alpha), tensor(alpha)) def before_batch(self): self.t = self.distrib.sample((self.y.size(0),)).squeeze().to(self.x.device) shuffle = torch.randperm(self.y.size(0)).to(self.x.device) x1,self.y1 = self.x[shuffle],self.y[shuffle] self.learn.xb = (x1 * (1-self.t[:,None,None,None]) + self.x * self.t[:,None,None,None],) def after_loss(self): with NoneReduce(self.loss_func) as lf: loss = lf(self.pred,self.y1) * (1-self.t) + lf(self.pred,self.y) * self.t self.learn.loss = loss.mean() learn = Learner(dls, xresnet18(), lr=1e-2, metrics=accuracy, loss_func=LabelSmoothingCrossEntropy(), cbs=Mixup(), opt_func=partial(pytorch_adamw, wd=0.01, eps=1e-3)) #slow learn.lr_find() #slow learn.fit_one_cycle(5, 5e-3)