{"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 *","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:06.153975Z","iopub.execute_input":"2022-03-19T13:24:06.15422Z","iopub.status.idle":"2022-03-19T13:24:08.780352Z","shell.execute_reply.started":"2022-03-19T13:24:06.154194Z","shell.execute_reply":"2022-03-19T13:24:08.779642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from fastai import test_utils\n#test_utils.show_install()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-19T13:24:08.781998Z","iopub.execute_input":"2022-03-19T13:24:08.783443Z","iopub.status.idle":"2022-03-19T13:24:08.787435Z","shell.execute_reply.started":"2022-03-19T13:24:08.783398Z","shell.execute_reply":"2022-03-19T13:24:08.786495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.set_device(0)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.788879Z","iopub.execute_input":"2022-03-19T13:24:08.78913Z","iopub.status.idle":"2022-03-19T13:24:08.79648Z","shell.execute_reply.started":"2022-03-19T13:24:08.789095Z","shell.execute_reply":"2022-03-19T13:24:08.795808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def seed_everything(seed=0):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n\nseed_everything()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.800158Z","iopub.execute_input":"2022-03-19T13:24:08.800629Z","iopub.status.idle":"2022-03-19T13:24:08.807403Z","shell.execute_reply.started":"2022-03-19T13:24:08.800588Z","shell.execute_reply":"2022-03-19T13:24:08.806707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/sorghum-id-fgvc-9'); path.ls()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.810206Z","iopub.execute_input":"2022-03-19T13:24:08.811016Z","iopub.status.idle":"2022-03-19T13:24:08.821822Z","shell.execute_reply.started":"2022-03-19T13:24:08.810973Z","shell.execute_reply":"2022-03-19T13:24:08.821141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorghum_df = pd.read_csv(path/'train_cultivar_mapping.csv');sorghum_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.823166Z","iopub.execute_input":"2022-03-19T13:24:08.823423Z","iopub.status.idle":"2022-03-19T13:24:08.866947Z","shell.execute_reply.started":"2022-03-19T13:24:08.823391Z","shell.execute_reply":"2022-03-19T13:24:08.866282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.868202Z","iopub.execute_input":"2022-03-19T13:24:08.868575Z","iopub.status.idle":"2022-03-19T13:24:08.873582Z","shell.execute_reply.started":"2022-03-19T13:24:08.86854Z","shell.execute_reply":"2022-03-19T13:24:08.872894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"allowed_image = []\nfor i in (path/'train_images').ls():\n    va = i.name in sorghum_df.image.to_list()\n    if va:\n        allowed_image.append(i.name)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:08.874937Z","iopub.execute_input":"2022-03-19T13:24:08.875363Z","iopub.status.idle":"2022-03-19T13:24:19.268669Z","shell.execute_reply.started":"2022-03-19T13:24:08.875287Z","shell.execute_reply":"2022-03-19T13:24:19.267776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorghum_df = sorghum_df[sorghum_df.image.isin(allowed_image)]","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:19.270021Z","iopub.execute_input":"2022-03-19T13:24:19.270279Z","iopub.status.idle":"2022-03-19T13:24:19.291064Z","shell.execute_reply.started":"2022-03-19T13:24:19.270244Z","shell.execute_reply":"2022-03-19T13:24:19.290238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorghum_df.cultivar.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:19.294079Z","iopub.execute_input":"2022-03-19T13:24:19.2945Z","iopub.status.idle":"2022-03-19T13:24:19.305835Z","shell.execute_reply.started":"2022-03-19T13:24:19.294462Z","shell.execute_reply":"2022-03-19T13:24:19.305064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_df(sorghum_df, path/'train_images', valid_pct=0.10,\n                                   item_tfms=Resize(128),\n                                   bs=32, num_workers=4, label_col=\"cultivar\")","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:19.307004Z","iopub.execute_input":"2022-03-19T13:24:19.307613Z","iopub.status.idle":"2022-03-19T13:24:23.882053Z","shell.execute_reply.started":"2022-03-19T13:24:19.307578Z","shell.execute_reply":"2022-03-19T13:24:23.881246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:23.883574Z","iopub.execute_input":"2022-03-19T13:24:23.883834Z","iopub.status.idle":"2022-03-19T13:24:26.708147Z","shell.execute_reply.started":"2022-03-19T13:24:23.883798Z","shell.execute_reply":"2022-03-19T13:24:26.707419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(dls.vocab)\nprint(dls.c)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:26.709071Z","iopub.execute_input":"2022-03-19T13:24:26.709276Z","iopub.status.idle":"2022-03-19T13:24:26.714713Z","shell.execute_reply.started":"2022-03-19T13:24:26.709247Z","shell.execute_reply":"2022-03-19T13:24:26.71364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics=[error_rate, accuracy], model_dir=\"/tmp/model/\").to_fp16()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:26.716259Z","iopub.execute_input":"2022-03-19T13:24:26.717098Z","iopub.status.idle":"2022-03-19T13:24:30.668586Z","shell.execute_reply.started":"2022-03-19T13:24:26.717062Z","shell.execute_reply":"2022-03-19T13:24:30.66786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:24:30.669803Z","iopub.execute_input":"2022-03-19T13:24:30.670126Z","iopub.status.idle":"2022-03-19T13:26:18.318987Z","shell.execute_reply.started":"2022-03-19T13:24:30.67009Z","shell.execute_reply":"2022-03-19T13:26:18.318142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T13:26:18.320404Z","iopub.execute_input":"2022-03-19T13:26:18.320643Z","iopub.status.idle":"2022-03-19T14:28:07.736059Z","shell.execute_reply.started":"2022-03-19T13:26:18.320603Z","shell.execute_reply":"2022-03-19T14:28:07.73524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T14:28:07.740878Z","iopub.execute_input":"2022-03-19T14:28:07.741094Z","iopub.status.idle":"2022-03-19T14:29:17.73591Z","shell.execute_reply.started":"2022-03-19T14:28:07.741065Z","shell.execute_reply":"2022-03-19T14:29:17.735101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(5,5), dpi=60)","metadata":{"execution":{"iopub.status.busy":"2022-03-19T14:29:17.737399Z","iopub.execute_input":"2022-03-19T14:29:17.737655Z","iopub.status.idle":"2022-03-19T14:29:54.127997Z","shell.execute_reply.started":"2022-03-19T14:29:17.737618Z","shell.execute_reply":"2022-03-19T14:29:54.127341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}