{"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":"import pandas  as pd\nimport numpy as np\nimport matplotlib.pyplot  as plt\nfrom sklearn.utils import shuffle\nimport cv2\n\nimport tensorflow as tf \nfrom tensorflow.keras import applications\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization, GlobalAveragePooling2D\n\nimport os, json\nfrom PIL import Image\nimport seaborn as sns\nimport datetime\nimport albumentations as A","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:12.878424Z","iopub.execute_input":"2021-08-01T15:38:12.878844Z","iopub.status.idle":"2021-08-01T15:38:12.886233Z","shell.execute_reply.started":"2021-08-01T15:38:12.878807Z","shell.execute_reply":"2021-08-01T15:38:12.884477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path = \"../input/cassava-leaf-disease-classification/\"\ntrain_csv_data_path = data_path+\"train.csv\"\nlabel_json_data_path = data_path+\"label_num_to_disease_map.json\"\nimages_dir_data_path = data_path+\"train_images\"","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.022636Z","iopub.execute_input":"2021-08-01T15:38:13.023210Z","iopub.status.idle":"2021-08-01T15:38:13.027005Z","shell.execute_reply.started":"2021-08-01T15:38:13.023161Z","shell.execute_reply":"2021-08-01T15:38:13.026140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nprint('Train images: %d' %len(os.listdir(\n    os.path.join(data_path, \"train_images\"))))","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.168841Z","iopub.execute_input":"2021-08-01T15:38:13.169478Z","iopub.status.idle":"2021-08-01T15:38:13.188968Z","shell.execute_reply.started":"2021-08-01T15:38:13.169427Z","shell.execute_reply":"2021-08-01T15:38:13.188041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(os.path.join(data_path, \"label_num_to_disease_map.json\")) as file:\n    print(json.dumps(json.loads(file.read()), indent=4))","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.324436Z","iopub.execute_input":"2021-08-01T15:38:13.324996Z","iopub.status.idle":"2021-08-01T15:38:13.332182Z","shell.execute_reply.started":"2021-08-01T15:38:13.324949Z","shell.execute_reply":"2021-08-01T15:38:13.331201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(train_csv_data_path)\ntrain_csv['label'] = train_csv['label'].astype('string')\n\nlabel_class = pd.read_json(label_json_data_path, orient='index')\nlabel_class = label_class.values.flatten().tolist()","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.481494Z","iopub.execute_input":"2021-08-01T15:38:13.481843Z","iopub.status.idle":"2021-08-01T15:38:13.539757Z","shell.execute_reply.started":"2021-08-01T15:38:13.481812Z","shell.execute_reply":"2021-08-01T15:38:13.538536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"whitegrid\")\nfig, ax = plt.subplots(figsize = (6, 4))\n\nfor i in ['top', 'right', 'left']:\n    ax.spines[i].set_visible(False)\nax.spines['bottom'].set_color('black')\n\nsns.countplot(train_csv.label, edgecolor = 'black',\n              palette = reversed(sns.color_palette(\"viridis\", 5)))\nplt.xlabel('Classes', fontfamily = 'serif', size = 15)\nplt.ylabel('Count', fontfamily = 'serif', size = 15)\nplt.xticks(fontfamily = 'serif', size = 12)\nplt.yticks(fontfamily = 'serif', size = 12)\nax.grid(axis = 'y', linestyle = '--', alpha = 0.9)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.647776Z","iopub.execute_input":"2021-08-01T15:38:13.648159Z","iopub.status.idle":"2021-08-01T15:38:13.862709Z","shell.execute_reply.started":"2021-08-01T15:38:13.648128Z","shell.execute_reply":"2021-08-01T15:38:13.861360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize(image):\n    plt.figure(figsize=(10, 10))\n    plt.axis('off')\n    plt.imshow(image)\n    \ndef visualize_multiple(nrows, ncols, img, transform):\n    fig, axes = plt.subplots(nrows,ncols)\n    fig.set_figheight(15)\n    fig.set_figwidth(15)\n    num_iter = 0\n    for row in range(nrows):\n        for col in range(ncols):\n            augmented_img = transform[num_iter](image=img)['image']\n            axes[row,col].imshow(augmented_img)\n            axes[row,col].grid(False)\n            axes[row,col].set_xticks([])\n            axes[row,col].set_yticks([])\n            num_iter += 1\n    return fig, axes","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.865045Z","iopub.execute_input":"2021-08-01T15:38:13.865496Z","iopub.status.idle":"2021-08-01T15:38:13.875008Z","shell.execute_reply.started":"2021-08-01T15:38:13.865447Z","shell.execute_reply":"2021-08-01T15:38:13.873619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#seed_everything(100)\nimg = cv2.imread('../input/cassava-leaf-disease-classification/train_images/100042118.jpg')\nimg = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nvisualize(img)","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:13.936559Z","iopub.execute_input":"2021-08-01T15:38:13.936939Z","iopub.status.idle":"2021-08-01T15:38:14.375364Z","shell.execute_reply.started":"2021-08-01T15:38:13.936899Z","shell.execute_reply":"2021-08-01T15:38:14.373202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # Blur","metadata":{}},{"cell_type":"code","source":"blur_limits = np.arange(3,39,4)\ntransform = [A.Blur(p=1, blur_limit=[limit,limit], always_apply=True) for limit in blur_limits]\nfig, axes = visualize_multiple(3,3,img,transform)\n\nnum_iter = 0\nfor row in range(3):\n    for col in range(3):\n        text = 'Blur kernel size: ({}, {})'.format(blur_limits[num_iter], blur_limits[num_iter])\n        axes[row, col].set_title(text)\n        num_iter += 1","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:14.378172Z","iopub.execute_input":"2021-08-01T15:38:14.378709Z","iopub.status.idle":"2021-08-01T15:38:15.748536Z","shell.execute_reply.started":"2021-08-01T15:38:14.378673Z","shell.execute_reply":"2021-08-01T15:38:15.747293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # CLAHE","metadata":{}},{"cell_type":"code","source":"params = np.arange(3,30,3)\ntransform = [A.CLAHE(clip_limit=[param, param], tile_grid_size=(param, param), always_apply=True) for param in params]\nfig, axes = visualize_multiple(3,3,img,transform)\n\nnum_iter = 0\nfor row in range(3):\n    for col in range(3):\n        text = 'Clip limit: ({}, {}), tile grid size: ({}, {})'.format(params[num_iter], params[num_iter], params[num_iter], params[num_iter])\n        axes[row, col].set_title(text)\n        num_iter += 1","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:15.750034Z","iopub.execute_input":"2021-08-01T15:38:15.750369Z","iopub.status.idle":"2021-08-01T15:38:17.037575Z","shell.execute_reply.started":"2021-08-01T15:38:15.750335Z","shell.execute_reply":"2021-08-01T15:38:17.036639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Label names :\")\nfor i, label in enumerate(label_class):\n    print(f\" {i}. {label}\")","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:17.038729Z","iopub.execute_input":"2021-08-01T15:38:17.039192Z","iopub.status.idle":"2021-08-01T15:38:17.045553Z","shell.execute_reply.started":"2021-08-01T15:38:17.039159Z","shell.execute_reply":"2021-08-01T15:38:17.044368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:17.048206Z","iopub.execute_input":"2021-08-01T15:38:17.048739Z","iopub.status.idle":"2021-08-01T15:38:17.066601Z","shell.execute_reply.started":"2021-08-01T15:38:17.048701Z","shell.execute_reply":"2021-08-01T15:38:17.065660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 18\nIMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:17.067819Z","iopub.execute_input":"2021-08-01T15:38:17.068261Z","iopub.status.idle":"2021-08-01T15:38:17.081993Z","shell.execute_reply.started":"2021-08-01T15:38:17.068230Z","shell.execute_reply":"2021-08-01T15:38:17.081024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n# model_model = load_model('../input/resnet/ResNet50 (1).h5') # for resnet50 224px\n# model_model = load_model('../input/private01/EfficientNetB7.h5') # for EfficientNetB7 300px\n# model_model = load_model('../input/private02/ResNet152(1).h5') # for ResNet152 224px\nmodel_model = load_model('../input/4feb2/4Feb-2.h5') # for ResNet152 224px","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:17.083163Z","iopub.execute_input":"2021-08-01T15:38:17.083644Z","iopub.status.idle":"2021-08-01T15:38:34.906661Z","shell.execute_reply.started":"2021-08-01T15:38:17.083610Z","shell.execute_reply":"2021-08-01T15:38:34.905492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_model.summary()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-01T15:38:34.914766Z","iopub.execute_input":"2021-08-01T15:38:34.915183Z","iopub.status.idle":"2021-08-01T15:38:34.968673Z","shell.execute_reply.started":"2021-08-01T15:38:34.915151Z","shell.execute_reply":"2021-08-01T15:38:34.966859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_path = data_path+\"test_images/2216849948.jpg\"\n\nimg = cv2.imread(test_img_path)\nresized_img = cv2.resize(img, (IMG_SIZE, IMG_SIZE)).reshape(-1, IMG_SIZE, IMG_SIZE, 3)/255\n\nplt.figure(figsize=(8,4))\nplt.title(\"TEST IMAGE\")\nplt.imshow(resized_img[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:34.970012Z","iopub.execute_input":"2021-08-01T15:38:34.970291Z","iopub.status.idle":"2021-08-01T15:38:35.249354Z","shell.execute_reply.started":"2021-08-01T15:38:34.970263Z","shell.execute_reply":"2021-08-01T15:38:35.247901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nss = pd.read_csv(data_path+'sample_submission.csv')\n\nfor image in ss.image_id:\n    img = tf.keras.preprocessing.image.load_img(data_path+'test_images/' + image)\n    img = tf.keras.preprocessing.image.img_to_array(img)\n    img = tf.keras.preprocessing.image.smart_resize(img, (IMG_SIZE, IMG_SIZE))\n    img = tf.reshape(img, (-1, IMG_SIZE, IMG_SIZE, 3))\n    prediction = model_model.predict(img/255)\n    preds.append(np.argmax(prediction))\n\nmy_submission = pd.DataFrame({'image_id': ss.image_id, 'label': preds})\nmy_submission.to_csv('submission.csv', index=False) ","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:35.250942Z","iopub.execute_input":"2021-08-01T15:38:35.251400Z","iopub.status.idle":"2021-08-01T15:38:39.024622Z","shell.execute_reply.started":"2021-08-01T15:38:35.251336Z","shell.execute_reply":"2021-08-01T15:38:39.023626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submission file ouput\nprint(\"Submission File: \\n---------------\\n\")\nprint(my_submission.head())","metadata":{"execution":{"iopub.status.busy":"2021-08-01T15:38:39.025815Z","iopub.execute_input":"2021-08-01T15:38:39.026249Z","iopub.status.idle":"2021-08-01T15:38:39.035548Z","shell.execute_reply.started":"2021-08-01T15:38:39.026217Z","shell.execute_reply":"2021-08-01T15:38:39.034467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}