{"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":"2023-03-14T11:01:32.884450Z","iopub.execute_input":"2023-03-14T11:01:32.884831Z","iopub.status.idle":"2023-03-14T11:01:32.910207Z","shell.execute_reply.started":"2023-03-14T11:01:32.884796Z","shell.execute_reply":"2023-03-14T11:01:32.908944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import the libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nfrom PIL import Image\nimport glob\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:02:12.631404Z","iopub.execute_input":"2023-03-14T11:02:12.632008Z","iopub.status.idle":"2023-03-14T11:02:19.168299Z","shell.execute_reply.started":"2023-03-14T11:02:12.631972Z","shell.execute_reply":"2023-03-14T11:02:19.167142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Load Images","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile('/kaggle/input/dogs-vs-cats/train.zip') as z:\n    z.extractall('.')\n\nwith zipfile.ZipFile('/kaggle/input/dogs-vs-cats/test1.zip') as z:\n    z.extractall('.')\n\n!ls /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:02:58.975486Z","iopub.execute_input":"2023-03-14T11:02:58.976201Z","iopub.status.idle":"2023-03-14T11:03:16.812338Z","shell.execute_reply.started":"2023-03-14T11:02:58.976167Z","shell.execute_reply":"2023-03-14T11:03:16.811134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glob.glob('/kaggle/working/train/*.jpg')[:5]","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:03:21.910037Z","iopub.execute_input":"2023-03-14T11:03:21.910442Z","iopub.status.idle":"2023-03-14T11:03:21.989262Z","shell.execute_reply.started":"2023-03-14T11:03:21.910401Z","shell.execute_reply":"2023-03-14T11:03:21.988004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create DataFrame","metadata":{}},{"cell_type":"code","source":"train_files= glob.glob('/kaggle/working/train/*.jpg')\ntrain_labels = [i.strip('/kaggle/working/train/')[:3] for i in train_files]\n\ntrain_df = pd.DataFrame({'filename': train_files, 'class': train_labels})","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:05:16.571261Z","iopub.execute_input":"2023-03-14T11:05:16.572003Z","iopub.status.idle":"2023-03-14T11:05:16.661988Z","shell.execute_reply.started":"2023-03-14T11:05:16.571965Z","shell.execute_reply":"2023-03-14T11:05:16.661039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:05:19.437974Z","iopub.execute_input":"2023-03-14T11:05:19.438740Z","iopub.status.idle":"2023-03-14T11:05:19.457600Z","shell.execute_reply.started":"2023-03-14T11:05:19.438701Z","shell.execute_reply":"2023-03-14T11:05:19.456468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:05:22.249836Z","iopub.execute_input":"2023-03-14T11:05:22.250599Z","iopub.status.idle":"2023-03-14T11:05:22.258031Z","shell.execute_reply.started":"2023-03-14T11:05:22.250549Z","shell.execute_reply":"2023-03-14T11:05:22.256763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Look at the image","metadata":{}},{"cell_type":"code","source":"train_df['filename'][2]","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:05:29.605867Z","iopub.execute_input":"2023-03-14T11:05:29.606270Z","iopub.status.idle":"2023-03-14T11:05:29.615660Z","shell.execute_reply.started":"2023-03-14T11:05:29.606226Z","shell.execute_reply":"2023-03-14T11:05:29.614632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(2, 2, figsize=(10,10))\naxs = axs.ravel()\nfor i in range(0,4):\n    idx = random.choice(train_df.index)\n    axs[i].imshow(Image.open(train_df['filename'][idx]))\n    axs[i].set_title(train_df['class'][idx])","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:05:43.881308Z","iopub.execute_input":"2023-03-14T11:05:43.882109Z","iopub.status.idle":"2023-03-14T11:05:44.790025Z","shell.execute_reply.started":"2023-03-14T11:05:43.882071Z","shell.execute_reply":"2023-03-14T11:05:44.788758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Built the loader.","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rotation_range=5,\n                                   rescale=1./255,\n                                   horizontal_flip=True,\n                                   shear_range=0.2,\n                                   zoom_range=0.2,\n                                   validation_split=0.2)\n\nimg_height, img_width = 224, 224\nbatch_size = 64\n                               ","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:08:44.926053Z","iopub.execute_input":"2023-03-14T11:08:44.926857Z","iopub.status.idle":"2023-03-14T11:08:44.932672Z","shell.execute_reply.started":"2023-03-14T11:08:44.926820Z","shell.execute_reply":"2023-03-14T11:08:44.931521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(train_df,\n                                target_size=(img_height, img_width),\n                                batch_size=batch_size,\n                                class_mode='categorical',\n                                subset='training')\n\nvalidation_generator = train_datagen.flow_from_dataframe(train_df,\n                                target_size=(img_height, img_width),\n                                batch_size=batch_size,\n                                class_mode='categorical',\n                                subset='validation')\nprint('Class Indices:', train_generator.class_indices)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:09:20.212369Z","iopub.execute_input":"2023-03-14T11:09:20.213345Z","iopub.status.idle":"2023-03-14T11:09:20.610947Z","shell.execute_reply.started":"2023-03-14T11:09:20.213293Z","shell.execute_reply":"2023-03-14T11:09:20.609966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Define a model","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu',\n                 input_shape=(img_width, img_height, 3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2 ,2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:12:19.946948Z","iopub.execute_input":"2023-03-14T11:12:19.947360Z","iopub.status.idle":"2023-03-14T11:12:22.685461Z","shell.execute_reply.started":"2023-03-14T11:12:19.947325Z","shell.execute_reply":"2023-03-14T11:12:22.684370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model, show_shapes=True, show_layer_names=False, dpi=60, show_layer_activations=True, rankdir='TB')","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:16:50.120366Z","iopub.execute_input":"2023-03-14T11:16:50.120890Z","iopub.status.idle":"2023-03-14T11:16:50.343734Z","shell.execute_reply.started":"2023-03-14T11:16:50.120848Z","shell.execute_reply":"2023-03-14T11:16:50.342227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n             optimizer='adam',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:18:06.248534Z","iopub.execute_input":"2023-03-14T11:18:06.249149Z","iopub.status.idle":"2023-03-14T11:18:06.274538Z","shell.execute_reply.started":"2023-03-14T11:18:06.249100Z","shell.execute_reply":"2023-03-14T11:18:06.273585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nhistory = model.fit(train_generator,\n                   epochs=epochs,\n                   validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T11:18:50.868138Z","iopub.execute_input":"2023-03-14T11:18:50.868870Z","iopub.status.idle":"2023-03-14T12:16:10.851951Z","shell.execute_reply.started":"2023-03-14T11:18:50.868827Z","shell.execute_reply":"2023-03-14T12:16:10.850859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_loss(history):\n    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(6, 6))\n    \n    ax1.plot(history.history['loss'], color='b', label='Training loss')\n    ax1.plot(history.history['val_loss'], color='r', label='Validation loss')\n    ax1.set_xticks(np.arange(1, epochs, 1))\n    legend = plt.legend(loc='best', shadow=True)\n    plt.tight_layout()\n    \n    \n    ax2.plot(history.history['accuracy'], color='b', label='Training accuracy')\n    ax2.plot(history.history['val_accuracy'], color='r', label='Validation accuracy')\n    ax2.set_xticks(np.arange(1, epochs, 1))\n    \n    legend = plt.legend(loc='best', shadow=True)\n    plt.tight_layout()\n    plt.show()\n\nplot_loss(history)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:16:40.997625Z","iopub.execute_input":"2023-03-14T12:16:40.998002Z","iopub.status.idle":"2023-03-14T12:16:41.672145Z","shell.execute_reply.started":"2023-03-14T12:16:40.997969Z","shell.execute_reply":"2023-03-14T12:16:41.671184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 6. Get the predictions","metadata":{}},{"cell_type":"code","source":"test_files = glob.glob('/kaggle/working/test1/*.jpg')\ntest_df = pd.DataFrame({'filename': test_files})\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(test_df,\n                                             x_col='filename',\n                                             y_col=None,\n                                             class_mode=None,\n                                        target_size=(img_height,img_width),\n                                             batch_size = batch_size,\n                                             shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:17:05.814743Z","iopub.execute_input":"2023-03-14T12:17:05.815710Z","iopub.status.idle":"2023-03-14T12:17:05.947861Z","shell.execute_reply.started":"2023-03-14T12:17:05.815643Z","shell.execute_reply":"2023-03-14T12:17:05.946911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_predictions(test_generator, model):\n    plt.figure(figsize=(12,12))\n    for i in range(0, 15):\n        plt.subplot(5, 3, i+1)\n        for X_batch in test_generator:\n            prediction = model.predict(X_batch)[0]\n            image = X_batch[0]\n            plt.imshow(image)\n            plt.title('cat' if np.argmax(prediction)==0 else 'dog')\n            break\n    plt.tight_layout()\n    plt.show()\n    \nvisualize_predictions(test_generator, model)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:17:31.025906Z","iopub.execute_input":"2023-03-14T12:17:31.026634Z","iopub.status.idle":"2023-03-14T12:17:38.362340Z","shell.execute_reply.started":"2023-03-14T12:17:31.026596Z","shell.execute_reply":"2023-03-14T12:17:38.361093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import Xception\nfrom tensorflow.keras import Input\nfrom tensorflow.keras.models import Model\n\nbasemodel = Xception(include_top = False,\n                    weights = 'imagenet',\n                    input_shape=(img_height, img_width, 3))\n\nbasemodel.trainable = False\n\nheadModel = basemodel.output\nheadModel = MaxPooling2D(pool_size=(5,5))(headModel)\nheadModel = Flatten(name='flatten')(headModel)\nheadModel = Dense(128, activation='relu')(headModel)\nheadModel = Dropout(0.8)(headModel)\nheadModel = Dense(2, activation='softmax')(headModel)\n\nmodel = Model(inputs=basemodel.input, outputs=headModel )","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:20:36.554200Z","iopub.execute_input":"2023-03-14T12:20:36.554953Z","iopub.status.idle":"2023-03-14T12:20:42.732787Z","shell.execute_reply.started":"2023-03-14T12:20:36.554914Z","shell.execute_reply":"2023-03-14T12:20:42.731769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n\nopt = Adam(learning_rate=0.001)\nmodel.compile(loss='categorical_crossentropy',\n             optimizer=opt,\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:22:01.197712Z","iopub.execute_input":"2023-03-14T12:22:01.198084Z","iopub.status.idle":"2023-03-14T12:22:01.215196Z","shell.execute_reply.started":"2023-03-14T12:22:01.198050Z","shell.execute_reply":"2023-03-14T12:22:01.214270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5\nhistory = model.fit_generator(train_generator,\n                             epochs=epochs,\n                             validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:22:30.915293Z","iopub.execute_input":"2023-03-14T12:22:30.916258Z","iopub.status.idle":"2023-03-14T12:51:21.817295Z","shell.execute_reply.started":"2023-03-14T12:22:30.916158Z","shell.execute_reply":"2023-03-14T12:51:21.816260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_loss(history)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:51:27.335742Z","iopub.execute_input":"2023-03-14T12:51:27.336696Z","iopub.status.idle":"2023-03-14T12:51:27.677464Z","shell.execute_reply.started":"2023-03-14T12:51:27.336638Z","shell.execute_reply":"2023-03-14T12:51:27.676503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_predictions(test_generator,model)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T12:52:43.227184Z","iopub.execute_input":"2023-03-14T12:52:43.227720Z","iopub.status.idle":"2023-03-14T12:52:53.991394Z","shell.execute_reply.started":"2023-03-14T12:52:43.227671Z","shell.execute_reply":"2023-03-14T12:52:53.990289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = [np.argmax(i) for i in model.predict(test_generator)]\nids = [i+1 for i in test_df.index]","metadata":{"execution":{"iopub.status.busy":"2023-03-14T14:43:44.411965Z","iopub.execute_input":"2023-03-14T14:43:44.412675Z","iopub.status.idle":"2023-03-14T14:44:40.830012Z","shell.execute_reply.started":"2023-03-14T14:43:44.412632Z","shell.execute_reply":"2023-03-14T14:44:40.829023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame(columns=['id','label'])\nresult['id'] = ids\nresult['label'] = predictions","metadata":{"execution":{"iopub.status.busy":"2023-03-14T14:34:04.664711Z","iopub.execute_input":"2023-03-14T14:34:04.665155Z","iopub.status.idle":"2023-03-14T14:34:04.688272Z","shell.execute_reply.started":"2023-03-14T14:34:04.665115Z","shell.execute_reply":"2023-03-14T14:34:04.687274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-14T14:36:49.512897Z","iopub.execute_input":"2023-03-14T14:36:49.513495Z","iopub.status.idle":"2023-03-14T14:36:49.548202Z","shell.execute_reply.started":"2023-03-14T14:36:49.513446Z","shell.execute_reply":"2023-03-14T14:36:49.547112Z"},"trusted":true},"execution_count":null,"outputs":[]}]}