{"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 numpy as np\nimport pandas as pd \nfrom zipfile import ZipFile\nimport matplotlib.pyplot as plt\nimport random\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nimport tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nfrom sklearn.model_selection import train_test_split\nimport os\n\ninput = '../input/dogs-vs-cats/'\noutput = '/kaggle/working/'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-12T09:24:53.009259Z","iopub.execute_input":"2021-06-12T09:24:53.009579Z","iopub.status.idle":"2021-06-12T09:24:53.015482Z","shell.execute_reply.started":"2021-06-12T09:24:53.009549Z","shell.execute_reply":"2021-06-12T09:24:53.014209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_zip(file_name: str)-> str:\n    with ZipFile(file_name, 'r') as zip:\n        print('Extracting all the files now...')\n        zip.extractall()\n        print('Done!')","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:24:20.944067Z","iopub.execute_input":"2021-06-12T09:24:20.944377Z","iopub.status.idle":"2021-06-12T09:24:20.948146Z","shell.execute_reply.started":"2021-06-12T09:24:20.944344Z","shell.execute_reply":"2021-06-12T09:24:20.947363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_random_image_from(filenames: list, path: str)-> None:\n    sample = random.choice(filenames)\n    image = load_img(output+path+sample)\n    plt.imshow(image)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:24:20.949978Z","iopub.execute_input":"2021-06-12T09:24:20.950337Z","iopub.status.idle":"2021-06-12T09:24:20.962913Z","shell.execute_reply.started":"2021-06-12T09:24:20.950300Z","shell.execute_reply":"2021-06-12T09:24:20.962162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_random_image_shape(filenames:list, path:str)-> np.ndarray:\n    return np.asarray(Image.open(output+path+random.choice(filenames))).shape","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:24:20.964301Z","iopub.execute_input":"2021-06-12T09:24:20.964664Z","iopub.status.idle":"2021-06-12T09:24:20.971426Z","shell.execute_reply.started":"2021-06-12T09:24:20.964638Z","shell.execute_reply":"2021-06-12T09:24:20.970465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(input + \"sampleSubmission.csv\")\nextract_zip(input + \"test1.zip\")\nextract_zip(input + \"train.zip\")\n\ntest_filenames = os.listdir(output + \"test1/\")\ntrain_filenames = os.listdir(output + \"train/\")","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:24:56.494711Z","iopub.execute_input":"2021-06-12T09:24:56.495024Z","iopub.status.idle":"2021-06-12T09:25:10.110650Z","shell.execute_reply.started":"2021-06-12T09:24:56.494997Z","shell.execute_reply":"2021-06-12T09:25:10.109197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_random_image_from(train_filenames, 'train/')","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:17.424976Z","iopub.execute_input":"2021-06-12T09:25:17.425311Z","iopub.status.idle":"2021-06-12T09:25:17.686106Z","shell.execute_reply.started":"2021-06-12T09:25:17.425280Z","shell.execute_reply":"2021-06-12T09:25:17.684562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = {\"image\": [], \"is_dog\": []}\nfor path in train_filenames:\n    train_data['image'].append(path)\n    train_data['is_dog'].append(int(path.split('.')[0] == 'dog'))\n\ndf = pd.DataFrame.from_dict(train_data)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:21.696686Z","iopub.execute_input":"2021-06-12T09:25:21.697026Z","iopub.status.idle":"2021-06-12T09:25:21.735087Z","shell.execute_reply.started":"2021-06-12T09:25:21.696997Z","shell.execute_reply":"2021-06-12T09:25:21.734292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:24.120477Z","iopub.execute_input":"2021-06-12T09:25:24.120834Z","iopub.status.idle":"2021-06-12T09:25:24.138621Z","shell.execute_reply.started":"2021-06-12T09:25:24.120804Z","shell.execute_reply":"2021-06-12T09:25:24.137540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['is_dog'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:27.962024Z","iopub.execute_input":"2021-06-12T09:25:27.962354Z","iopub.status.idle":"2021-06-12T09:25:28.081403Z","shell.execute_reply.started":"2021-06-12T09:25:27.962325Z","shell.execute_reply":"2021-06-12T09:25:28.080636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\n\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dropout(0.25))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dropout(0.25))\nmodel.add(layers.MaxPooling2D((2, 2)))\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dropout(0.5))\n\nmodel.add(layers.Dense(2, activation='softmax'))\n\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:37.541260Z","iopub.execute_input":"2021-06-12T09:25:37.541587Z","iopub.status.idle":"2021-06-12T09:25:39.490287Z","shell.execute_reply.started":"2021-06-12T09:25:37.541553Z","shell.execute_reply":"2021-06-12T09:25:39.489478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"is_dog\"] = df[\"is_dog\"].replace({0: 'cat', 1: 'dog'}) \ntrain_df, validate_df = train_test_split(df, test_size=0.20)\n\ntrain_df.reset_index(drop=True)\nvalidate_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:25:49.414816Z","iopub.execute_input":"2021-06-12T09:25:49.415148Z","iopub.status.idle":"2021-06-12T09:25:49.436124Z","shell.execute_reply.started":"2021-06-12T09:25:49.415118Z","shell.execute_reply":"2021-06-12T09:25:49.435336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['is_dog'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:13.353009Z","iopub.execute_input":"2021-06-12T09:26:13.353330Z","iopub.status.idle":"2021-06-12T09:26:13.464907Z","shell.execute_reply.started":"2021-06-12T09:26:13.353301Z","shell.execute_reply":"2021-06-12T09:26:13.463948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    output + \"train/\", \n    x_col='image',\n    y_col='is_dog',\n    target_size=[128,128],\n    class_mode='categorical',\n    batch_size=15\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:19.432046Z","iopub.execute_input":"2021-06-12T09:26:19.432376Z","iopub.status.idle":"2021-06-12T09:26:19.633948Z","shell.execute_reply.started":"2021-06-12T09:26:19.432345Z","shell.execute_reply":"2021-06-12T09:26:19.633092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    output + \"train/\",  \n    x_col='image',\n    y_col='is_dog',\n    target_size=[128,128],\n    class_mode='categorical',\n    batch_size=15\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:28.233175Z","iopub.execute_input":"2021-06-12T09:26:28.233496Z","iopub.status.idle":"2021-06-12T09:26:28.289561Z","shell.execute_reply.started":"2021-06-12T09:26:28.233463Z","shell.execute_reply":"2021-06-12T09:26:28.288792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_random_aug_image(df: pd.DataFrame, datagen: ImageDataGenerator)-> None:   \n    example_generator = datagen.flow_from_dataframe(\n        df.sample(n=1).reset_index(drop=True), \n        output + \"train/\", \n        x_col='image',\n        y_col='is_dog',\n        target_size=[128,128],\n        class_mode='categorical'\n    )\n\n    plt.figure(figsize=(15, 15))\n    for i in range(0, 15):\n        plt.subplot(5, 3, i+1)\n        for X_batch, Y_batch in example_generator:\n            image = X_batch[0]\n            plt.title(\"Dog\" if Y_batch[0] == 1 else \"Cat\")\n            plt.imshow(image)\n            break\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:32.160861Z","iopub.execute_input":"2021-06-12T09:26:32.161186Z","iopub.status.idle":"2021-06-12T09:26:32.170224Z","shell.execute_reply.started":"2021-06-12T09:26:32.161156Z","shell.execute_reply":"2021-06-12T09:26:32.169030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_random_aug_image(train_df, train_datagen)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:39.305463Z","iopub.execute_input":"2021-06-12T09:26:39.305833Z","iopub.status.idle":"2021-06-12T09:26:41.343937Z","shell.execute_reply.started":"2021-06-12T09:26:39.305802Z","shell.execute_reply":"2021-06-12T09:26:41.343151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 15\nval_total = validate_df.shape[0]\ntrain_total = train_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:47.914581Z","iopub.execute_input":"2021-06-12T09:26:47.914922Z","iopub.status.idle":"2021-06-12T09:26:47.919480Z","shell.execute_reply.started":"2021-06-12T09:26:47.914893Z","shell.execute_reply":"2021-06-12T09:26:47.918386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator, \n    epochs=3,\n    validation_data=validation_generator,\n    validation_steps=val_total//batch_size,\n    steps_per_epoch=train_total//batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T09:26:50.859590Z","iopub.execute_input":"2021-06-12T09:26:50.859940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history.history","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot([1,2,3], history.history['accuracy'], label='accuracy')\nplt.plot([1,2,3], history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.xlim([1, 3])\nplt.legend(loc='lower right')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot([1,2,3],history.history['loss'], label='loss')\nplt.plot([1,2,3], history.history['val_loss'], label = 'val_loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.ylim([0, 1.5])\nplt.xlim([1, 3])\nplt.legend(loc='lower right')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_loss, val_acc = model.evaluate(x=validation_generator, verbose=2)\nval_acc","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data ={\"image\": []}\nfor path in test_filenames:\n    test_data['image'].append(path)\n\ntest_df = pd.DataFrame.from_dict(test_data)\n\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    output+\"test1/\", \n    x_col='image',\n    y_col=None,\n    class_mode=None,\n    target_size=[128,128],\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(test_df.shape[0]/batch_size))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'] = np.argmax(predict, axis=-1)\nlabel_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['category'] = test_df['category'].replace(label_map)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['category'].value_counts().plot.bar()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_provided_number_of_random_predictions(num: int)->None:\n    plt.figure(figsize=(12, 24))\n    for index, row in test_df.sample(n=num).reset_index(drop=True).iterrows():\n        filename = row['image']\n        category = row['category']\n        img = load_img(output+\"test1/\"+filename, target_size=[128,128])\n        plt.subplot(num//2, num//3, index+1)\n        plt.imshow(img)\n        plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\n    plt.tight_layout()\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_provided_number_of_random_predictions(20)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}