from fastai import * from fastai.vision import * from fastai.gen_doc.nbdoc import * show_doc(AdaptiveConcatPool2d, doc_string=False) from fastai.gen_doc.nbdoc import * from fastai.layers import * path = untar_data(URLs.MNIST_SAMPLE) data = ImageDataBunch.from_folder(path) def simple_cnn_max(actns:Collection[int], kernel_szs:Collection[int]=None, strides:Collection[int]=None) -> nn.Sequential: "CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`" nl = len(actns)-1 kernel_szs = ifnone(kernel_szs, [3]*nl) strides = ifnone(strides , [2]*nl) layers = [conv_layer(actns[i], actns[i+1], kernel_szs[i], stride=strides[i]) for i in range(len(strides))] layers.append(nn.Sequential(nn.AdaptiveMaxPool2d(1), Flatten())) return nn.Sequential(*layers) model = simple_cnn_max((3,16,16,2)) learner = Learner(data, model, metrics=[accuracy]) learner.fit(1) def simple_cnn_avg(actns:Collection[int], kernel_szs:Collection[int]=None, strides:Collection[int]=None) -> nn.Sequential: "CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`" nl = len(actns)-1 kernel_szs = ifnone(kernel_szs, [3]*nl) strides = ifnone(strides , [2]*nl) layers = [conv_layer(actns[i], actns[i+1], kernel_szs[i], stride=strides[i]) for i in range(len(strides))] layers.append(nn.Sequential(nn.AdaptiveAvgPool2d(1), Flatten())) return nn.Sequential(*layers) model = simple_cnn_avg((3,16,16,2)) learner = Learner(data, model, metrics=[accuracy]) learner.fit(1) def simple_cnn(actns:Collection[int], kernel_szs:Collection[int]=None, strides:Collection[int]=None) -> nn.Sequential: "CNN with `conv2d_relu` layers defined by `actns`, `kernel_szs` and `strides`" nl = len(actns)-1 kernel_szs = ifnone(kernel_szs, [3]*nl) strides = ifnone(strides , [2]*nl) layers = [conv_layer(actns[i], actns[i+1], kernel_szs[i], stride=strides[i]) for i in range(len(strides))] layers.append(nn.Sequential(AdaptiveConcatPool2d(1), Flatten())) return nn.Sequential(*layers) model = simple_cnn((3,16,16,2)) learner = Learner(data, model, metrics=[accuracy]) learner.fit(1) show_doc(Lambda, doc_string=False) model = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d(1), ) model.cuda() for xb, yb in data.train_dl: out = (model(*[xb])) print(out.size()) break model = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d(1), Lambda(lambda x: x.view(x.size(0),-1)) ) model.cuda() for xb, yb in data.train_dl: out = (model(*[xb])) print(out.size()) break show_doc(Flatten) model = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.AdaptiveAvgPool2d(1), Flatten(), ) model.cuda() for xb, yb in data.train_dl: out = (model(*[xb])) print(out.size()) break show_doc(PoolFlatten) model = nn.Sequential( nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1), nn.ReLU(), nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1), nn.ReLU(), PoolFlatten() ) model.cuda() for xb, yb in data.train_dl: out = (model(*[xb])) print(out.size()) break show_doc(ResizeBatch) a = torch.tensor([[1., -1.], [1., -1.]]) print(a) out = ResizeBatch(4) print(out(a)) show_doc(CrossEntropyFlat, doc_string=False) show_doc(MSELossFlat) show_doc(Debugger) show_doc(NoopLoss) show_doc(PixelShuffle_ICNR) show_doc(bn_drop_lin, doc_string=False) show_doc(conv2d) show_doc(conv2d_trans) show_doc(conv_layer, doc_string=False) show_doc(embedding, doc_string=False) show_doc(simple_cnn) show_doc(std_upsample_head, doc_string=False) show_doc(trunc_normal_) show_doc(icnr) show_doc(Debugger.forward) show_doc(MSELossFlat.forward) show_doc(CrossEntropyFlat.forward) show_doc(Lambda.forward) show_doc(AdaptiveConcatPool2d.forward) show_doc(NoopLoss.forward) show_doc(NoopLoss) show_doc(PixelShuffle_ICNR) show_doc(icnr)