{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30018,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-04-18T13:05:32.491387Z","iopub.execute_input":"2024-04-18T13:05:32.491753Z","iopub.status.idle":"2024-04-18T13:05:32.499404Z","shell.execute_reply.started":"2024-04-18T13:05:32.491723Z","shell.execute_reply":"2024-04-18T13:05:32.498385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport random\n\nimport matplotlib.pyplot as plt\n\nimport os\nimport zipfile\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-18T13:05:44.394606Z","iopub.execute_input":"2024-04-18T13:05:44.394943Z","iopub.status.idle":"2024-04-18T13:05:49.814714Z","shell.execute_reply.started":"2024-04-18T13:05:44.394915Z","shell.execute_reply":"2024-04-18T13:05:49.813982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting the zip file\n\nlocal_zip = '/kaggle/input/dogs-vs-cats/train.zip'\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/tmp')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:06:10.184122Z","iopub.execute_input":"2024-04-18T13:06:10.184488Z","iopub.status.idle":"2024-04-18T13:06:23.755432Z","shell.execute_reply.started":"2024-04-18T13:06:10.184456Z","shell.execute_reply":"2024-04-18T13:06:23.754387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/tmp/train'\nimg_names = os.listdir(os.path.join(base_dir))\nimg_names[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:06:27.601739Z","iopub.execute_input":"2024-04-18T13:06:27.602152Z","iopub.status.idle":"2024-04-18T13:06:27.627345Z","shell.execute_reply.started":"2024-04-18T13:06:27.602095Z","shell.execute_reply":"2024-04-18T13:06:27.626599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(img_names)\nimage = load_img(\"/tmp/train/\"+sample)\nplt.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:06:33.895429Z","iopub.execute_input":"2024-04-18T13:06:33.895796Z","iopub.status.idle":"2024-04-18T13:06:34.146017Z","shell.execute_reply.started":"2024-04-18T13:06:33.895764Z","shell.execute_reply":"2024-04-18T13:06:34.145148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's assign the label Dog and Cat for the images\n\nlabel = []\n\nfor i in img_names:\n    if(i.split(\".\")[0] == \"dog\"):\n        label.append(\"Dog\")\n    else:\n        label.append(\"Cat\")\n\nlabel[:10]","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:06:40.255749Z","iopub.execute_input":"2024-04-18T13:06:40.256112Z","iopub.status.idle":"2024-04-18T13:06:40.276472Z","shell.execute_reply.started":"2024-04-18T13:06:40.256080Z","shell.execute_reply":"2024-04-18T13:06:40.275502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\"Image\" : img_names, \"Label\" : label})\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:07:28.948790Z","iopub.execute_input":"2024-04-18T13:07:28.949152Z","iopub.status.idle":"2024-04-18T13:07:28.975341Z","shell.execute_reply.started":"2024-04-18T13:07:28.949118Z","shell.execute_reply":"2024-04-18T13:07:28.974242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:07:32.773575Z","iopub.execute_input":"2024-04-18T13:07:32.773929Z","iopub.status.idle":"2024-04-18T13:07:32.779727Z","shell.execute_reply.started":"2024-04-18T13:07:32.773898Z","shell.execute_reply":"2024-04-18T13:07:32.778719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.Label.value_counts().plot.bar(color = ['red','blue'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:07:45.868695Z","iopub.execute_input":"2024-04-18T13:07:45.869100Z","iopub.status.idle":"2024-04-18T13:07:46.015947Z","shell.execute_reply.started":"2024-04-18T13:07:45.869066Z","shell.execute_reply":"2024-04-18T13:07:46.014934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size = 0.2)\ntrain_df = train_df.reset_index(drop = True)\nvalidate_df = validate_df.reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:07:51.279932Z","iopub.execute_input":"2024-04-18T13:07:51.280451Z","iopub.status.idle":"2024-04-18T13:07:51.292142Z","shell.execute_reply.started":"2024-04-18T13:07:51.280392Z","shell.execute_reply":"2024-04-18T13:07:51.291236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    \n    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    tf.keras.layers.MaxPooling2D(2, 2),\n    \n    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='relu'),\n    \n    tf.keras.layers.Dense(1, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:07:56.479347Z","iopub.execute_input":"2024-04-18T13:07:56.479719Z","iopub.status.idle":"2024-04-18T13:07:58.549217Z","shell.execute_reply.started":"2024-04-18T13:07:56.479688Z","shell.execute_reply":"2024-04-18T13:07:58.548459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:08:02.465137Z","iopub.execute_input":"2024-04-18T13:08:02.465583Z","iopub.status.idle":"2024-04-18T13:08:02.473397Z","shell.execute_reply.started":"2024-04-18T13:08:02.465546Z","shell.execute_reply":"2024-04-18T13:08:02.472424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = 'binary_crossentropy',\n              optimizer = 'SGD',\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:08:06.402436Z","iopub.execute_input":"2024-04-18T13:08:06.402880Z","iopub.status.idle":"2024-04-18T13:08:06.419420Z","shell.execute_reply.started":"2024-04-18T13:08:06.402841Z","shell.execute_reply":"2024-04-18T13:08:06.418701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n      rescale=1./255,\n      rotation_range=40,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True,\n      fill_mode='nearest')\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/tmp/train/\", \n    x_col='Image',\n    y_col='Label',\n    target_size = (150, 150),\n    class_mode = 'binary',\n    batch_size = 20\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:08:12.857568Z","iopub.execute_input":"2024-04-18T13:08:12.857942Z","iopub.status.idle":"2024-04-18T13:08:13.217250Z","shell.execute_reply.started":"2024-04-18T13:08:12.857912Z","shell.execute_reply":"2024-04-18T13:08:13.216254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/tmp/train/\", \n    x_col='Image',\n    y_col='Label',\n    target_size = (150, 150),\n    class_mode = 'binary',\n    batch_size = 20\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:08:22.708154Z","iopub.execute_input":"2024-04-18T13:08:22.708520Z","iopub.status.idle":"2024-04-18T13:08:22.785648Z","shell.execute_reply.started":"2024-04-18T13:08:22.708483Z","shell.execute_reply":"2024-04-18T13:08:22.784835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(\n      train_generator,\n      steps_per_epoch = np.ceil(20000/20),  # 20000 images = batch_size * steps\n      epochs = 10,\n      validation_data=validation_generator,\n      validation_steps = np.ceil(5000/20),  # 5000 images = batch_size * steps\n      verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:08:25.779851Z","iopub.execute_input":"2024-04-18T13:08:25.780233Z","iopub.status.idle":"2024-04-18T13:34:43.504367Z","shell.execute_reply.started":"2024-04-18T13:08:25.780198Z","shell.execute_reply":"2024-04-18T13:34:43.503573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.figure(figsize=(7,7))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure(figsize=(7,7))\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:35:07.609604Z","iopub.execute_input":"2024-04-18T13:35:07.609962Z","iopub.status.idle":"2024-04-18T13:35:07.962390Z","shell.execute_reply.started":"2024-04-18T13:35:07.609930Z","shell.execute_reply":"2024-04-18T13:35:07.961489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting the zip file\n\nlocal_zip = '/kaggle/input/dogs-vs-cats/test1.zip'\nzip_ref = zipfile.ZipFile(local_zip, 'r')\nzip_ref.extractall('/tmp')\nzip_ref.close()\n\ntest_dir = '/tmp/test1/'\ntest_img = os.listdir(os.path.join(test_dir))\ntest_img[:10]\n\ntest_df = pd.DataFrame({'Image': test_img})","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:35:25.584729Z","iopub.execute_input":"2024-04-18T13:35:25.585073Z","iopub.status.idle":"2024-04-18T13:35:32.434847Z","shell.execute_reply.started":"2024-04-18T13:35:25.585044Z","shell.execute_reply":"2024-04-18T13:35:32.434126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\n\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/tmp/test1/\", \n    x_col = 'Image',\n    y_col = None,\n    class_mode = None,\n    target_size = (150, 150),\n    batch_size = 20,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:35:36.316850Z","iopub.execute_input":"2024-04-18T13:35:36.317245Z","iopub.status.idle":"2024-04-18T13:35:36.514438Z","shell.execute_reply.started":"2024-04-18T13:35:36.317211Z","shell.execute_reply":"2024-04-18T13:35:36.513450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps = np.ceil(12500/20))","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:35:39.697241Z","iopub.execute_input":"2024-04-18T13:35:39.697635Z","iopub.status.idle":"2024-04-18T13:36:10.807868Z","shell.execute_reply.started":"2024-04-18T13:35:39.697599Z","shell.execute_reply":"2024-04-18T13:36:10.806936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:13.763445Z","iopub.execute_input":"2024-04-18T13:36:13.763854Z","iopub.status.idle":"2024-04-18T13:36:13.770639Z","shell.execute_reply.started":"2024-04-18T13:36:13.763804Z","shell.execute_reply":"2024-04-18T13:36:13.769336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def label(predict):\n    if(predict > 0.5):\n        return \"Dog\"\n    else:\n        return \"Cat\"","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:16.829781Z","iopub.execute_input":"2024-04-18T13:36:16.830153Z","iopub.status.idle":"2024-04-18T13:36:16.834751Z","shell.execute_reply.started":"2024-04-18T13:36:16.830119Z","shell.execute_reply":"2024-04-18T13:36:16.833801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['Label'] = predict\ntest_df['Label'] = test_df['Label'].apply(label)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:21.812619Z","iopub.execute_input":"2024-04-18T13:36:21.812980Z","iopub.status.idle":"2024-04-18T13:36:21.824448Z","shell.execute_reply.started":"2024-04-18T13:36:21.812950Z","shell.execute_reply":"2024-04-18T13:36:21.823334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:33.665935Z","iopub.execute_input":"2024-04-18T13:36:33.666343Z","iopub.status.idle":"2024-04-18T13:36:33.677191Z","shell.execute_reply.started":"2024-04-18T13:36:33.666305Z","shell.execute_reply":"2024-04-18T13:36:33.676234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.Label.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:36.376776Z","iopub.execute_input":"2024-04-18T13:36:36.377118Z","iopub.status.idle":"2024-04-18T13:36:36.388096Z","shell.execute_reply.started":"2024-04-18T13:36:36.377089Z","shell.execute_reply":"2024-04-18T13:36:36.387198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.Label.value_counts().plot.bar(color = ['red','blue'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T13:36:40.764701Z","iopub.execute_input":"2024-04-18T13:36:40.765038Z","iopub.status.idle":"2024-04-18T13:36:40.893859Z","shell.execute_reply.started":"2024-04-18T13:36:40.765009Z","shell.execute_reply":"2024-04-18T13:36:40.893046Z"},"trusted":true},"execution_count":null,"outputs":[]}]}