{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-04T10:52:46.957696Z","iopub.execute_input":"2021-08-04T10:52:46.958238Z","iopub.status.idle":"2021-08-04T10:53:05.279423Z","shell.execute_reply.started":"2021-08-04T10:52:46.958064Z","shell.execute_reply":"2021-08-04T10:53:05.253768Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport glob\nimport matplotlib.image as mpimg\nfrom PIL import Image\nfrom tqdm import tqdm\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import (\n        vgg16,\n        resnet50,\n        inception_v3)\nfrom tensorflow.keras.layers.experimental import preprocessing\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:05.281453Z","iopub.execute_input":"2021-08-04T10:53:05.2819Z","iopub.status.idle":"2021-08-04T10:53:10.629145Z","shell.execute_reply.started":"2021-08-04T10:53:05.281855Z","shell.execute_reply":"2021-08-04T10:53:10.628023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"train_dir = '../input/cassava-leaf-disease-classification/train_images'\ntest_dir = '../input/cassava-leaf-disease-classification/test_images'\ntrain_names = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv',dtype=str)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:10.632002Z","iopub.execute_input":"2021-08-04T10:53:10.63252Z","iopub.status.idle":"2021-08-04T10:53:10.668787Z","shell.execute_reply.started":"2021-08-04T10:53:10.632471Z","shell.execute_reply":"2021-08-04T10:53:10.667669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_names.head())\nprint(len(train_names))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:10.672653Z","iopub.execute_input":"2021-08-04T10:53:10.672978Z","iopub.status.idle":"2021-08-04T10:53:10.694447Z","shell.execute_reply.started":"2021-08-04T10:53:10.672947Z","shell.execute_reply":"2021-08-04T10:53:10.69261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot the Data\n\nFirst we will want to visualize our data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8,8), num=0)\nfor cntr, filename in enumerate(glob.iglob(train_dir + '**/*.jpg', recursive=True)):\n    if cntr == 9:\n        image = Image.open(filename)\n        image_shape = np.array(image).shape\n        print(f'The shape of the images is: {image_shape}')\n        break\n    elif cntr < 9:\n        img = mpimg.imread(filename)\n        plt.subplot(3,3,cntr+1)\n        plt.xticks([])\n        plt.yticks([])\n        plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:10.696109Z","iopub.execute_input":"2021-08-04T10:53:10.69659Z","iopub.status.idle":"2021-08-04T10:53:11.996842Z","shell.execute_reply.started":"2021-08-04T10:53:10.696545Z","shell.execute_reply":"2021-08-04T10:53:11.995718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 256\nimg_height = image_shape[0]\nimg_width = image_shape[1]","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:11.998551Z","iopub.execute_input":"2021-08-04T10:53:11.999344Z","iopub.status.idle":"2021-08-04T10:53:12.004822Z","shell.execute_reply.started":"2021-08-04T10:53:11.999299Z","shell.execute_reply":"2021-08-04T10:53:12.003527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:12.00692Z","iopub.execute_input":"2021-08-04T10:53:12.007799Z","iopub.status.idle":"2021-08-04T10:53:12.019438Z","shell.execute_reply.started":"2021-08-04T10:53:12.007744Z","shell.execute_reply":"2021-08-04T10:53:12.018109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_names.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:12.021385Z","iopub.execute_input":"2021-08-04T10:53:12.022153Z","iopub.status.idle":"2021-08-04T10:53:12.043457Z","shell.execute_reply.started":"2021-08-04T10:53:12.022095Z","shell.execute_reply":"2021-08-04T10:53:12.042146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Constructing the train generator","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255.,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:12.048617Z","iopub.execute_input":"2021-08-04T10:53:12.048986Z","iopub.status.idle":"2021-08-04T10:53:12.056532Z","shell.execute_reply.started":"2021-08-04T10:53:12.048955Z","shell.execute_reply":"2021-08-04T10:53:12.05497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\ndataframe = train_names,\ndirectory = train_dir,\nx_col = \"image_id\",\ny_col = \"label\",\nsubset = \"training\",\nbatch_size = batch_size,\nseed = 42,\nshuffle = True,\nclass_mode = \"sparse\",\ntarget_size = (img_height, img_width))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:12.059589Z","iopub.execute_input":"2021-08-04T10:53:12.059902Z","iopub.status.idle":"2021-08-04T10:53:20.291779Z","shell.execute_reply.started":"2021-08-04T10:53:12.059872Z","shell.execute_reply":"2021-08-04T10:53:20.290389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = train_datagen.flow_from_dataframe(\ndataframe = train_names,\ndirectory = train_dir,\nx_col = \"image_id\",\ny_col = \"label\",\nsubset = \"validation\",\nbatch_size = batch_size,\nseed = 42,\nshuffle = True,\nclass_mode = \"sparse\",\ntarget_size = (img_height, img_width))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:20.293597Z","iopub.execute_input":"2021-08-04T10:53:20.294057Z","iopub.status.idle":"2021-08-04T10:53:28.483449Z","shell.execute_reply.started":"2021-08-04T10:53:20.294011Z","shell.execute_reply":"2021-08-04T10:53:28.481355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Constructing the test generator","metadata":{}},{"cell_type":"code","source":"# Will be given to us at the end...","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:28.485169Z","iopub.execute_input":"2021-08-04T10:53:28.485685Z","iopub.status.idle":"2021-08-04T10:53:28.492064Z","shell.execute_reply.started":"2021-08-04T10:53:28.485625Z","shell.execute_reply":"2021-08-04T10:53:28.490529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Iterating over our Data and plotting it\n## Now the data and labels are together!","metadata":{}},{"cell_type":"code","source":"image_batch, labels_batch = next(iter(train_generator))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:28.494023Z","iopub.execute_input":"2021-08-04T10:53:28.494493Z","iopub.status.idle":"2021-08-04T10:53:33.676425Z","shell.execute_reply.started":"2021-08-04T10:53:28.494448Z","shell.execute_reply":"2021-08-04T10:53:33.67531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_batch","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:33.678045Z","iopub.execute_input":"2021-08-04T10:53:33.678466Z","iopub.status.idle":"2021-08-04T10:53:33.690975Z","shell.execute_reply.started":"2021-08-04T10:53:33.678428Z","shell.execute_reply":"2021-08-04T10:53:33.689684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14,14), num=1)\nfor i in range(32):\n    \n#     max_value = max(labels_batch[i])\n#     labels_list = list(labels_batch[i])\n#     max_index = labels_list.index(max_value)\n    max_index = labels_batch[i]\n    plt.subplot(8,4,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(image_batch[i])\n    plt.title(max_index)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:33.692804Z","iopub.execute_input":"2021-08-04T10:53:33.693653Z","iopub.status.idle":"2021-08-04T10:53:37.916958Z","shell.execute_reply.started":"2021-08-04T10:53:33.693562Z","shell.execute_reply":"2021-08-04T10:53:37.915968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, labels_batch = next(iter(train_generator))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:37.918627Z","iopub.execute_input":"2021-08-04T10:53:37.919401Z","iopub.status.idle":"2021-08-04T10:53:43.270991Z","shell.execute_reply.started":"2021-08-04T10:53:37.91935Z","shell.execute_reply":"2021-08-04T10:53:43.269867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_batch.shape)\nprint(labels_batch.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:43.272598Z","iopub.execute_input":"2021-08-04T10:53:43.273032Z","iopub.status.idle":"2021-08-04T10:53:43.28425Z","shell.execute_reply.started":"2021-08-04T10:53:43.272975Z","shell.execute_reply":"2021-08-04T10:53:43.282642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, labels_batch = next(iter(valid_generator))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:43:10.644864Z","iopub.execute_input":"2021-08-04T11:43:10.645328Z","iopub.status.idle":"2021-08-04T11:43:14.226848Z","shell.execute_reply.started":"2021-08-04T11:43:10.645295Z","shell.execute_reply":"2021-08-04T11:43:14.225732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_batch.shape)\nprint(labels_batch.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:43:17.803985Z","iopub.execute_input":"2021-08-04T11:43:17.804467Z","iopub.status.idle":"2021-08-04T11:43:17.810589Z","shell.execute_reply.started":"2021-08-04T11:43:17.804435Z","shell.execute_reply":"2021-08-04T11:43:17.809308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_batch","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:43:23.213321Z","iopub.execute_input":"2021-08-04T11:43:23.213688Z","iopub.status.idle":"2021-08-04T11:43:23.225268Z","shell.execute_reply.started":"2021-08-04T11:43:23.21364Z","shell.execute_reply":"2021-08-04T11:43:23.223906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14,14), num=1)\nfor i in range(32):\n    \n#     max_value = max(labels_batch[i])\n#     labels_list = list(labels_batch[i])\n#     max_index = labels_list.index(max_value)\n    max_index = labels_batch[i]\n\n    plt.subplot(8,4,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(image_batch[i])\n    plt.title(max_index)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:48.838893Z","iopub.execute_input":"2021-08-04T10:53:48.839382Z","iopub.status.idle":"2021-08-04T10:53:52.845267Z","shell.execute_reply.started":"2021-08-04T10:53:48.839328Z","shell.execute_reply":"2021-08-04T10:53:52.844144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building the Model","metadata":{}},{"cell_type":"code","source":"def my_block(inputs, block_name='MyBlock'):\n    if block_name == 'MyBlock':\n        with tf.name_scope(block_name):\n            conv = layers.Conv2D(filters=3, strides=2, kernel_size=2, padding='same')(inputs)\n            bn = layers.BatchNormalization()(conv)\n            relu = layers.ReLU()(bn)\n            return relu\n    \n    elif block_name == 'MyEnd':\n        with tf.name_scope(block_name):\n            conv = layers.Conv2D(filters=3, strides=2, kernel_size=2, padding='same')(inputs)\n            bn = layers.BatchNormalization()(conv)\n            relu = layers.ReLU()(bn)\n            return relu","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:52.84678Z","iopub.execute_input":"2021-08-04T10:53:52.847378Z","iopub.status.idle":"2021-08-04T10:53:52.857377Z","shell.execute_reply.started":"2021-08-04T10:53:52.847321Z","shell.execute_reply":"2021-08-04T10:53:52.856246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" def preprocessing_layer(inputs, block_name='MyPre'):\n    with tf.name_scope(block_name):\n        pre = preprocessing.Resizing(int(224*4), int(224*4), interpolation='bilinear')(inputs)\n        pre = preprocessing.RandomFlip('horizontal')(pre)\n        pre = preprocessing.RandomFlip('vertical')(pre)\n        pre = preprocessing.RandomContrast(0.5)(pre) \n    return pre","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:52.858867Z","iopub.execute_input":"2021-08-04T10:53:52.859448Z","iopub.status.idle":"2021-08-04T10:53:52.869403Z","shell.execute_reply.started":"2021-08-04T10:53:52.859406Z","shell.execute_reply":"2021-08-04T10:53:52.868269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model = resnet50.ResNet50(weights='imagenet')\nfor layer in resnet_model.layers[:161]:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:52.87082Z","iopub.execute_input":"2021-08-04T10:53:52.871552Z","iopub.status.idle":"2021-08-04T10:53:59.890123Z","shell.execute_reply.started":"2021-08-04T10:53:52.8715Z","shell.execute_reply":"2021-08-04T10:53:59.889012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = keras.Input(shape=(img_height, img_width, 3))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:59.891788Z","iopub.execute_input":"2021-08-04T10:53:59.892216Z","iopub.status.idle":"2021-08-04T10:53:59.899601Z","shell.execute_reply.started":"2021-08-04T10:53:59.892169Z","shell.execute_reply":"2021-08-04T10:53:59.898281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = preprocessing_layer(inputs, block_name='MyPre')\nx = my_block(x, block_name='MyBlock')\nx = my_block(x, block_name='MyEnd')\n\n# x = resnet_model(x)\n\nx = layers.GlobalAveragePooling2D()(x)\n\nx = layers.Dense(100, activation='relu')(x)\noutputs = layers.Dense(5, activation='relu')(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:53:59.901484Z","iopub.execute_input":"2021-08-04T10:53:59.902291Z","iopub.status.idle":"2021-08-04T10:54:00.03472Z","shell.execute_reply.started":"2021-08-04T10:53:59.902247Z","shell.execute_reply":"2021-08-04T10:54:00.033544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Model(inputs=inputs, outputs=outputs)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.036345Z","iopub.execute_input":"2021-08-04T10:54:00.036812Z","iopub.status.idle":"2021-08-04T10:54:00.048022Z","shell.execute_reply.started":"2021-08-04T10:54:00.03677Z","shell.execute_reply":"2021-08-04T10:54:00.046767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.053906Z","iopub.execute_input":"2021-08-04T10:54:00.054244Z","iopub.status.idle":"2021-08-04T10:54:00.074194Z","shell.execute_reply.started":"2021-08-04T10:54:00.054213Z","shell.execute_reply":"2021-08-04T10:54:00.07245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.076416Z","iopub.execute_input":"2021-08-04T10:54:00.076869Z","iopub.status.idle":"2021-08-04T10:54:00.096403Z","shell.execute_reply.started":"2021-08-04T10:54:00.076824Z","shell.execute_reply":"2021-08-04T10:54:00.094977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_samples = train_generator.samples\nvalid_samples = valid_generator.samples","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.098072Z","iopub.execute_input":"2021-08-04T10:54:00.098925Z","iopub.status.idle":"2021-08-04T10:54:00.104922Z","shell.execute_reply.started":"2021-08-04T10:54:00.098879Z","shell.execute_reply":"2021-08-04T10:54:00.103337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_samples, valid_samples)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.106901Z","iopub.execute_input":"2021-08-04T10:54:00.107426Z","iopub.status.idle":"2021-08-04T10:54:00.122911Z","shell.execute_reply.started":"2021-08-04T10:54:00.107379Z","shell.execute_reply":"2021-08-04T10:54:00.121759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n        train_generator,\n        steps_per_epoch = np.ceil(train_samples / batch_size),\n        epochs = 5,\n        validation_data = valid_generator,\n        validation_steps = np.ceil(valid_samples / batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T10:54:00.124472Z","iopub.execute_input":"2021-08-04T10:54:00.124787Z","iopub.status.idle":"2021-08-04T11:36:37.909917Z","shell.execute_reply.started":"2021-08-04T10:54:00.124756Z","shell.execute_reply":"2021-08-04T11:36:37.908805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_frame = pd.DataFrame(history.history)\n\nhistory_frame.loc[:, ['loss', 'val_loss']].plot()\nhistory_frame.loc[:, ['accuracy', 'val_accuracy']].plot();","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:37.912117Z","iopub.execute_input":"2021-08-04T11:36:37.912504Z","iopub.status.idle":"2021-08-04T11:36:38.580769Z","shell.execute_reply.started":"2021-08-04T11:36:37.912475Z","shell.execute_reply":"2021-08-04T11:36:38.579112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions on the test Data","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255.)\ntest_dir = '../input/cassava-leaf-disease-classification/test_images'","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.584784Z","iopub.execute_input":"2021-08-04T11:36:38.588436Z","iopub.status.idle":"2021-08-04T11:36:38.596409Z","shell.execute_reply.started":"2021-08-04T11:36:38.588376Z","shell.execute_reply":"2021-08-04T11:36:38.595018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_list = [i.split('/')[-1] for i in glob.glob(test_dir + \"/*.jpg\")]\nlabels = ['0' for i in range(len(file_list))]","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.598988Z","iopub.execute_input":"2021-08-04T11:36:38.600506Z","iopub.status.idle":"2021-08-04T11:36:38.613384Z","shell.execute_reply.started":"2021-08-04T11:36:38.600461Z","shell.execute_reply":"2021-08-04T11:36:38.612106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = {'image_id': file_list, 'label': labels}\ndf = pd.DataFrame(data=d)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.615533Z","iopub.execute_input":"2021-08-04T11:36:38.617324Z","iopub.status.idle":"2021-08-04T11:36:38.629598Z","shell.execute_reply.started":"2021-08-04T11:36:38.617278Z","shell.execute_reply":"2021-08-04T11:36:38.628392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_names.label[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.631591Z","iopub.execute_input":"2021-08-04T11:36:38.633405Z","iopub.status.idle":"2021-08-04T11:36:38.647068Z","shell.execute_reply.started":"2021-08-04T11:36:38.633342Z","shell.execute_reply":"2021-08-04T11:36:38.645787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.label[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.650178Z","iopub.execute_input":"2021-08-04T11:36:38.653744Z","iopub.status.idle":"2021-08-04T11:36:38.66483Z","shell.execute_reply.started":"2021-08-04T11:36:38.653702Z","shell.execute_reply":"2021-08-04T11:36:38.66341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_datagen.flow_from_dataframe(\ndataframe = df,\ndirectory = test_dir,\nx_col = \"image_id\",\ny_col = \"label\",\nsubset = \"training\",\nbatch_size = 1,\nseed = 42,\nshuffle = True,\nclass_mode = \"sparse\",\ntarget_size = (img_height, img_width))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.670889Z","iopub.execute_input":"2021-08-04T11:36:38.671698Z","iopub.status.idle":"2021-08-04T11:36:38.699777Z","shell.execute_reply.started":"2021-08-04T11:36:38.671642Z","shell.execute_reply":"2021-08-04T11:36:38.698639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = test_generator.filenames\nnb_samples = len(filenames)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.701324Z","iopub.execute_input":"2021-08-04T11:36:38.702003Z","iopub.status.idle":"2021-08-04T11:36:38.710887Z","shell.execute_reply.started":"2021-08-04T11:36:38.701962Z","shell.execute_reply":"2021-08-04T11:36:38.709549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_generator,steps = nb_samples)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:36:38.712796Z","iopub.execute_input":"2021-08-04T11:36:38.713613Z","iopub.status.idle":"2021-08-04T11:36:39.0608Z","shell.execute_reply.started":"2021-08-04T11:36:38.71357Z","shell.execute_reply":"2021-08-04T11:36:39.059589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions, axis=1) ","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:45:20.833553Z","iopub.execute_input":"2021-08-04T11:45:20.833946Z","iopub.status.idle":"2021-08-04T11:45:20.840056Z","shell.execute_reply.started":"2021-08-04T11:45:20.833913Z","shell.execute_reply":"2021-08-04T11:45:20.838131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:45:25.593565Z","iopub.execute_input":"2021-08-04T11:45:25.593977Z","iopub.status.idle":"2021-08-04T11:45:25.602198Z","shell.execute_reply.started":"2021-08-04T11:45:25.593929Z","shell.execute_reply":"2021-08-04T11:45:25.600683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.label = predictions","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:45:35.923845Z","iopub.execute_input":"2021-08-04T11:45:35.924308Z","iopub.status.idle":"2021-08-04T11:45:35.930553Z","shell.execute_reply.started":"2021-08-04T11:45:35.924275Z","shell.execute_reply":"2021-08-04T11:45:35.928819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exhibition of our results","metadata":{}},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:45:40.234589Z","iopub.execute_input":"2021-08-04T11:45:40.235484Z","iopub.status.idle":"2021-08-04T11:45:40.253562Z","shell.execute_reply.started":"2021-08-04T11:45:40.235427Z","shell.execute_reply":"2021-08-04T11:45:40.251293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(image_batch[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:41:44.954104Z","iopub.execute_input":"2021-08-04T11:41:44.954514Z","iopub.status.idle":"2021-08-04T11:41:45.296618Z","shell.execute_reply.started":"2021-08-04T11:41:44.954481Z","shell.execute_reply":"2021-08-04T11:41:45.295283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T11:48:01.714354Z","iopub.execute_input":"2021-08-04T11:48:01.714746Z","iopub.status.idle":"2021-08-04T11:48:01.725201Z","shell.execute_reply.started":"2021-08-04T11:48:01.714713Z","shell.execute_reply":"2021-08-04T11:48:01.723963Z"},"trusted":true},"execution_count":null,"outputs":[]}]}