{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport zipfile\nimport numpy as np\nfrom PIL import Image\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras import layers\nfrom keras.layers import Conv2D, MaxPooling2D , Dropout, Flatten, Dense, Activation, GlobalMaxPooling2D\nfrom keras.applications import VGG16\nfrom keras import optimizers\nfrom keras.models import Model\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\nimport warnings \nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-21T05:37:37.715725Z","iopub.execute_input":"2024-04-21T05:37:37.716055Z","iopub.status.idle":"2024-04-21T05:37:51.259294Z","shell.execute_reply.started":"2024-04-21T05:37:37.716014Z","shell.execute_reply":"2024-04-21T05:37:51.258227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_zip = '/kaggle/input/dogs-vs-cats/'\npath = '/kaggle/working/'\n\nwith zipfile.ZipFile(path_zip+'train.zip','r') as zip_ref:\n    zip_ref.extractall(path+'train')\nwith zipfile.ZipFile(path_zip+'test1.zip','r') as zip_ref:\n    zip_ref.extractall(path+'test')\n","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:37:51.261062Z","iopub.execute_input":"2024-04-21T05:37:51.261656Z","iopub.status.idle":"2024-04-21T05:38:07.346285Z","shell.execute_reply.started":"2024-04-21T05:37:51.261624Z","shell.execute_reply":"2024-04-21T05:38:07.345432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filenames = os.listdir(\"/kaggle/working/train/train\")\ncategories=[]\n\nfor filename in filenames:\n    category = filename.split('.')[0]\n    \n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndf = pd.DataFrame({\n    'filename' : filenames,\n    'category' : categories\n})\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:07.347527Z","iopub.execute_input":"2024-04-21T05:38:07.348315Z","iopub.status.idle":"2024-04-21T05:38:07.410682Z","shell.execute_reply.started":"2024-04-21T05:38:07.348276Z","shell.execute_reply":"2024-04-21T05:38:07.409668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See Total In Count\ndf['category'].value_counts().plot.bar()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:07.412658Z","iopub.execute_input":"2024-04-21T05:38:07.412955Z","iopub.status.idle":"2024-04-21T05:38:07.683343Z","shell.execute_reply.started":"2024-04-21T05:38:07.412930Z","shell.execute_reply":"2024-04-21T05:38:07.682393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample =random.choice(filenames)\nimg = np.asarray(Image.open(\"/kaggle/working/train/train/\"+sample))\n# image = load_img()\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:07.684474Z","iopub.execute_input":"2024-04-21T05:38:07.684762Z","iopub.status.idle":"2024-04-21T05:38:08.093549Z","shell.execute_reply.started":"2024-04-21T05:38:07.684737Z","shell.execute_reply":"2024-04-21T05:38:08.092615Z"},"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()\nvalidate_df = validate_df.reset_index()\n\n\ntotal_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.094750Z","iopub.execute_input":"2024-04-21T05:38:08.095109Z","iopub.status.idle":"2024-04-21T05:38:08.112284Z","shell.execute_reply.started":"2024-04-21T05:38:08.095068Z","shell.execute_reply":"2024-04-21T05:38:08.111213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['category']=train_df['category'].astype(str)\nvalidate_df['category']=validate_df['category'].astype(str)\n# # train_df\n","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.113244Z","iopub.execute_input":"2024-04-21T05:38:08.113484Z","iopub.status.idle":"2024-04-21T05:38:08.129554Z","shell.execute_reply.started":"2024-04-21T05:38:08.113463Z","shell.execute_reply":"2024-04-21T05:38:08.128606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 224\ninput_shape = (image_size, image_size, 3)\n\nepochs = 10\nbatch_size = 16","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.131121Z","iopub.execute_input":"2024-04-21T05:38:08.131546Z","iopub.status.idle":"2024-04-21T05:38:08.139802Z","shell.execute_reply.started":"2024-04-21T05:38:08.131514Z","shell.execute_reply":"2024-04-21T05:38:08.138932Z"},"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.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest',\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    \"/kaggle/working/train/train/\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.141089Z","iopub.execute_input":"2024-04-21T05:38:08.141435Z","iopub.status.idle":"2024-04-21T05:38:08.414956Z","shell.execute_reply.started":"2024-04-21T05:38:08.141411Z","shell.execute_reply":"2024-04-21T05:38:08.414227Z"},"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    \"/kaggle/working/train/train/\", \n    x_col='filename',\n    y_col='category',\n    class_mode='binary',\n    target_size=(image_size, image_size),\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.418285Z","iopub.execute_input":"2024-04-21T05:38:08.418589Z","iopub.status.idle":"2024-04-21T05:38:08.487945Z","shell.execute_reply.started":"2024-04-21T05:38:08.418563Z","shell.execute_reply":"2024-04-21T05:38:08.487295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop = True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df,\n    \"/kaggle/working/train/train/\",\n    x_col='filename',\n    y_col='category',\n    class_mode='categorical'\n)\nplt.figure(figsize=(12, 12))\nfor i in range(0, 9):\n    plt.subplot(3, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:08.488988Z","iopub.execute_input":"2024-04-21T05:38:08.489368Z","iopub.status.idle":"2024-04-21T05:38:10.879228Z","shell.execute_reply.started":"2024-04-21T05:38:08.489333Z","shell.execute_reply":"2024-04-21T05:38:10.878005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\npre_trained_model = VGG16(input_shape=input_shape, include_top=False, weights=\"imagenet\")\n    \nfor layer in pre_trained_model.layers[:15]:\n    layer.trainable = False\n\nfor layer in pre_trained_model.layers[15:]:\n    layer.trainable = True\n    \nlast_layer = pre_trained_model.get_layer('block5_pool')\nlast_output = last_layer.output\n    \n# Flatten the output layer to 1 dimension\nx = GlobalMaxPooling2D()(last_output)\n# Add a fully connected layer with 512 hidden units and ReLU activation\nx = Dense(512, activation='relu')(x)\n# Add a dropout rate of 0.5\nx = Dropout(0.5)(x)\n# Add a final sigmoid layer for classification\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.compile(loss='binary_crossentropy',\n              optimizer=optimizers.SGD(learning_rate=1e-4, momentum=0.9),\n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:10.880339Z","iopub.execute_input":"2024-04-21T05:38:10.880609Z","iopub.status.idle":"2024-04-21T05:38:12.239917Z","shell.execute_reply.started":"2024-04-21T05:38:10.880586Z","shell.execute_reply":"2024-04-21T05:38:12.239079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fine-tune the model\nhistory = model.fit(\n    train_generator,\n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//epochs,\n    steps_per_epoch=total_train//epochs)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-21T05:38:12.240961Z","iopub.execute_input":"2024-04-21T05:38:12.241252Z","iopub.status.idle":"2024-04-21T06:13:59.823753Z","shell.execute_reply.started":"2024-04-21T05:38:12.241228Z","shell.execute_reply":"2024-04-21T06:13:59.822563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(train_generator, verbose=1)\nprint(\"Train : accuracy = %f  ;  loss = %f\" % (accuracy, loss))\n\nloss_v, accuracy_v = model.evaluate(validation_generator, verbose=1)\nprint(\"Validation : accuracy = %f  ;  loss_v = %f\" % (accuracy_v, loss_v))","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:13:59.825291Z","iopub.execute_input":"2024-04-21T06:13:59.825587Z","iopub.status.idle":"2024-04-21T06:17:30.037249Z","shell.execute_reply.started":"2024-04-21T06:13:59.825561Z","shell.execute_reply":"2024-04-21T06:17:30.036282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2,1)\nax[0].plot(history.history['loss'], color='b', label=\"Training loss\")\nax[0].plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax[0])\nlegend = ax[0].legend(loc='best', shadow=True)\n\nax[1].plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax[1].plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax[1].legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:17:30.038605Z","iopub.execute_input":"2024-04-21T06:17:30.038976Z","iopub.status.idle":"2024-04-21T06:17:30.464905Z","shell.execute_reply.started":"2024-04-21T06:17:30.038943Z","shell.execute_reply":"2024-04-21T06:17:30.464085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test/test1\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:28:30.293232Z","iopub.execute_input":"2024-04-21T06:28:30.293940Z","iopub.status.idle":"2024-04-21T06:28:30.307778Z","shell.execute_reply.started":"2024-04-21T06:28:30.293907Z","shell.execute_reply":"2024-04-21T06:28:30.307059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/working/test/test1\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    batch_size=batch_size,\n    target_size=(image_size, image_size),\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:28:46.848482Z","iopub.execute_input":"2024-04-21T06:28:46.849156Z","iopub.status.idle":"2024-04-21T06:28:46.983777Z","shell.execute_reply.started":"2024-04-21T06:28:46.849122Z","shell.execute_reply":"2024-04-21T06:28:46.983079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator, steps=int(np.ceil(nb_samples/batch_size)))\nthreshold = 0.5\ntest_df['category'] = np.where(predict > threshold, 1,0)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:29:43.618707Z","iopub.execute_input":"2024-04-21T06:29:43.619085Z","iopub.status.idle":"2024-04-21T06:30:12.722214Z","shell.execute_reply.started":"2024-04-21T06:29:43.619051Z","shell.execute_reply":"2024-04-21T06:30:12.721347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_test = test_df.sample(n=9).reset_index()\nsample_test.head()\nplt.figure(figsize=(12, 12))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = 'Cat'if row['category']==0 else 'Dog'\n    img = np.asarray(Image.open(\"/kaggle/working/test/test1/\"+filename))\n    plt.subplot(3, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '  Category: (' + \"{}\".format(category) + ')')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:30:54.502254Z","iopub.execute_input":"2024-04-21T06:30:54.502970Z","iopub.status.idle":"2024-04-21T06:30:56.561604Z","shell.execute_reply.started":"2024-04-21T06:30:54.502938Z","shell.execute_reply":"2024-04-21T06:30:56.560512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\nsubmission_df.drop(['filename', 'category'], axis=1, inplace=True)\nsubmission_df.to_csv('submission_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-21T06:31:01.398870Z","iopub.execute_input":"2024-04-21T06:31:01.399490Z","iopub.status.idle":"2024-04-21T06:31:01.440380Z","shell.execute_reply.started":"2024-04-21T06:31:01.399458Z","shell.execute_reply":"2024-04-21T06:31:01.439668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}