{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from fastai.vision.all import *\nimport numpy as np\nfrom ipywidgets import widgets\nfrom pandas.api.types import CategoricalDtype\nimport pandas as pd\nfrom ipywidgets import widgets\nfrom pandas.api.types import CategoricalDtype\nimport os\nimport matplotlib as mpl","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:31.399852Z","iopub.execute_input":"2021-06-21T14:23:31.400123Z","iopub.status.idle":"2021-06-21T14:23:33.895758Z","shell.execute_reply.started":"2021-06-21T14:23:31.400060Z","shell.execute_reply":"2021-06-21T14:23:33.894936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\nprint(len(df))\nprint(df.columns)\nprint(df['label'].value_counts().plot.bar())","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:33.897103Z","iopub.execute_input":"2021-06-21T14:23:33.897434Z","iopub.status.idle":"2021-06-21T14:23:34.074764Z","shell.execute_reply.started":"2021-06-21T14:23:33.897399Z","shell.execute_reply":"2021-06-21T14:23:34.073832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:34.076723Z","iopub.execute_input":"2021-06-21T14:23:34.077073Z","iopub.status.idle":"2021-06-21T14:23:34.089508Z","shell.execute_reply.started":"2021-06-21T14:23:34.077036Z","shell.execute_reply":"2021-06-21T14:23:34.088686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/cassava-leaf-disease-classification')\nPath.BASE_PATH=path\npath.ls()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:34.090804Z","iopub.execute_input":"2021-06-21T14:23:34.091037Z","iopub.status.idle":"2021-06-21T14:23:34.097730Z","shell.execute_reply.started":"2021-06-21T14:23:34.091014Z","shell.execute_reply":"2021-06-21T14:23:34.096799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_items=get_image_files(path/'train_images')","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:34.099368Z","iopub.execute_input":"2021-06-21T14:23:34.099923Z","iopub.status.idle":"2021-06-21T14:23:47.048675Z","shell.execute_reply.started":"2021-06-21T14:23:34.099888Z","shell.execute_reply":"2021-06-21T14:23:47.047780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(get_items[1])","metadata":{"execution":{"iopub.status.busy":"2021-06-21T14:23:47.049938Z","iopub.execute_input":"2021-06-21T14:23:47.050298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Check the images file, if there are a few that are corrupt\nfailed = verify_images(get_items)\nfailed","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show the Categoories ","metadata":{}},{"cell_type":"code","source":"count_dict = df.label.value_counts()\ncount_dict","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_x(r): return path/'train_images'/r['image_id']\ndef get_y(r): return r['label']\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_y(df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks=(ImageBlock,CategoryBlock),\n                get_x=get_x, \n                get_y=get_y,\n                batch_tfms=[*aug_transforms(min_scale=0.5, size=128),\n                Normalize.from_stats(*imagenet_stats)],\n                item_tfms = RandomResizedCrop(224, min_scale=0.35))\n                   \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(df,bs=128)\ndls.show_batch()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(dls)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.valid.show_batch()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#Dataset\ndsets = dblock.datasets(df)\ndsets.train[0]\nx,y = dsets.train[0]\nx,y","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock.summary(df)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl_train=dls.train\ndl_valid=dls.valid\nlen(dl_train),len(dl_valid)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xb,yb = first(dls[0])\nxb.shape,yb.shape\n#Batch_size contains 128 images\n#Images Pixels are 128*128\n#3 channel","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check the torch size","metadata":{}},{"cell_type":"code","source":"xb,yb = first(dls.valid)\nprint(\"Print the Input and Output Shape: \\n\", xb.shape,yb.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Categories number \n#output Categories\ndls.c","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Input has a 128 Pixel","metadata":{}},{"cell_type":"code","source":"# I try to grabb a mini batch from our DataLoader and then passing it to the model:\n#we have a batch side of 64 and 5 categories \nx,y = to_cpu(dls.train.one_batch())\nsubset_model = learn.model(x)\nprint(\"shape of the Submodel: \\n\"subset_model.shape\nsubset_model[0]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet18,\n                    loss_func=F.cross_entropy, metrics=accuracy)\nlearn.fine_tune(4, base_lr=1e-3, freeze_epochs=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics=accuracy)\nlearn.fine_tune(4, base_lr=1e-3, freeze_epochs=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#79%\nlearn = cnn_learner(dls, resnet50,\n                    loss_func=F.cross_entropy, metrics=accuracy)\nlearn.fine_tune(5, base_lr=1e-3, freeze_epochs=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet101,\n                    loss_func=F.cross_entropy, metrics=accuracy)\nlearn.fine_tune(4, base_lr=1e-3, freeze_epochs=4)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#  I defined a new Model","metadata":{}},{"cell_type":"code","source":"def conv(ni, nf, ks=3, act=True):\n    res = nn.Conv2d(ni, nf, stride=2, kernel_size=ks, padding=ks//2)\n    if act: res = nn.Sequential(res, nn.ReLU())\n    return res","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"simple_cnn = sequential(\n    conv(3 ,4),            #14x14\n    conv(4 ,8),            #7x7\n    conv(8 ,16),           #4x4\n    conv(16,32),           #2x2\n    conv(32,5, act=False), #1x1\n    Flatten(),\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = Learner(dls, simple_cnn, loss_func=F.cross_entropy, metrics=accuracy)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(2, 0.01)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = learn.model[0]\nm","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m[0].weight.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m[0].bias.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(epochs=1):\n    learn = Learner(dls, simple_cnn, loss_func=F.cross_entropy,\n                    metrics=accuracy, cbs=ActivationStats(with_hist=True))\n    learn.fit(epochs, 0.06)\n    return learn","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = fit()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.activation_stats.plot_layer_stats(0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}