{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2024-07-01T16:04:30.360114Z","iopub.execute_input":"2024-07-01T16:04:30.360854Z","iopub.status.idle":"2024-07-01T16:04:31.481846Z","shell.execute_reply.started":"2024-07-01T16:04:30.360824Z","shell.execute_reply":"2024-07-01T16:04:31.480817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = \"../input/dogs-vs-cats/test1.zip\"\ntest_path = \"../input/dogs-vs-cats/train.zip\"\n\nfiles = \"/kaggle/working/\"\n\nimport zipfile\n\nwith zipfile.ZipFile(train_path, 'r') as zipp:\n    zipp.extractall(files)\n    \nwith zipfile.ZipFile(test_path, 'r') as zipp:\n    zipp.extractall(files)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:04:34.384449Z","iopub.execute_input":"2024-07-01T16:04:34.385217Z","iopub.status.idle":"2024-07-01T16:04:47.996391Z","shell.execute_reply.started":"2024-07-01T16:04:34.385182Z","shell.execute_reply":"2024-07-01T16:04:47.995618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = \"../working/train/\"\n\nfilenames = os.listdir(image_dir)\n\nlabels = [x.split(\".\")[0] for x in filenames]\n\ndata = pd.DataFrame({\n     \"filename\": filenames, \n     \"label\": labels})\ndata.head()\n\nimage_dir = \"../working/test1/\"\n\nfilenames = os.listdir(image_dir)\n\nlabeltest = [x.split(\".\")[0] for x in filenames]\n\ndatatest = pd.DataFrame({\n    \"id\": labeltest,\n     \"filename\": filenames})\ndatatest.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:04:47.997831Z","iopub.execute_input":"2024-07-01T16:04:47.998119Z","iopub.status.idle":"2024-07-01T16:04:48.057463Z","shell.execute_reply.started":"2024-07-01T16:04:47.998094Z","shell.execute_reply":"2024-07-01T16:04:48.056616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nplt.figure(figsize=(20,20)) # specifying the overall grid size\nplt.subplots_adjust(hspace=0.4)\n\n\nfor i in range(10):\n    \n    plt.subplot(1,10,i+1)    # the number of images in the grid is 10*10 (100)\n    filename = 'train/' + 'cat.' + str(i) + '.jpg'\n    image = imread(filename)\n    plt.imshow(image)\n    plt.title('Cat',fontsize=12)\n    plt.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-29T10:12:45.189926Z","iopub.execute_input":"2024-06-29T10:12:45.190549Z","iopub.status.idle":"2024-06-29T10:12:46.215302Z","shell.execute_reply.started":"2024-06-29T10:12:45.190507Z","shell.execute_reply":"2024-06-29T10:12:46.214414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX=data['filename']\ny=data['label']\nX_train,X_val=train_test_split(data,random_state=42,stratify=y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:04:55.528857Z","iopub.execute_input":"2024-07-01T16:04:55.529687Z","iopub.status.idle":"2024-07-01T16:04:56.222805Z","shell.execute_reply.started":"2024-07-01T16:04:55.529655Z","shell.execute_reply":"2024-07-01T16:04:56.221805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\ndatagen=ImageDataGenerator(rescale=1./255.,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')\ntest_datagen=ImageDataGenerator(rescale=1./255.)\ntrain_idg = datagen.flow_from_dataframe(X_train, \"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = 64,\n                                    target_size=(224,224),class_mode='binary')\nval_idg = test_datagen.flow_from_dataframe(X_val,\"train/\", x_col= \"filename\", y_col= \"label\",\n                                    batch_size = 64,\n                                    target_size=(224,224),class_mode='binary')\ntest_gen = test_datagen.flow_from_dataframe(datatest,\"test1/\",x_col= \"filename\", y_col= None, \n                                          class_mode=None,\n                                          batch_size = 64,\n                                          target_size = (224,224),\n                                          shuffle = False\n                                         )\ntest_gen","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:42:04.526990Z","iopub.execute_input":"2024-07-01T16:42:04.527345Z","iopub.status.idle":"2024-07-01T16:42:04.944813Z","shell.execute_reply.started":"2024-07-01T16:42:04.527315Z","shell.execute_reply":"2024-07-01T16:42:04.944000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, InputLayer, Dropout, GlobalAveragePooling2D\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:42:07.848386Z","iopub.execute_input":"2024-07-01T16:42:07.848745Z","iopub.status.idle":"2024-07-01T16:42:07.853756Z","shell.execute_reply.started":"2024-07-01T16:42:07.848714Z","shell.execute_reply":"2024-07-01T16:42:07.852787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\nconv_base = VGG16(weights='imagenet',\n                  include_top=False,\n                 input_shape=(224,224,3))\nconv_base.trainable=False","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:42:15.444872Z","iopub.execute_input":"2024-07-01T16:42:15.445459Z","iopub.status.idle":"2024-07-01T16:42:15.691410Z","shell.execute_reply.started":"2024-07-01T16:42:15.445424Z","shell.execute_reply":"2024-07-01T16:42:15.690629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = conv_base.output\nx = Flatten()(x)\nx=Dropout(0.5)(x)\nx = Dense(1024, activation='relu')(x)\nx=Dropout(0.5)(x)\npredictions = Dense(1, activation='sigmoid')(x)\n\nmodel = Model(inputs=conv_base.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:47:11.542145Z","iopub.execute_input":"2024-07-01T16:47:11.542485Z","iopub.status.idle":"2024-07-01T16:47:11.573510Z","shell.execute_reply.started":"2024-07-01T16:47:11.542456Z","shell.execute_reply":"2024-07-01T16:47:11.572825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),\n    metrics=['accuracy'],\n    loss='binary_crossentropy'\n)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:47:12.781870Z","iopub.execute_input":"2024-07-01T16:47:12.782216Z","iopub.status.idle":"2024-07-01T16:47:12.827447Z","shell.execute_reply.started":"2024-07-01T16:47:12.782186Z","shell.execute_reply":"2024-07-01T16:47:12.826602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_idg,epochs=10,validation_data=val_idg)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T16:47:28.736404Z","iopub.execute_input":"2024-07-01T16:47:28.736767Z","iopub.status.idle":"2024-07-01T17:29:24.871160Z","shell.execute_reply.started":"2024-07-01T16:47:28.736735Z","shell.execute_reply":"2024-07-01T17:29:24.870288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = model.predict(test_gen)\ntest_pred","metadata":{"execution":{"iopub.status.busy":"2024-07-01T17:29:35.172089Z","iopub.execute_input":"2024-07-01T17:29:35.172425Z","iopub.status.idle":"2024-07-01T17:30:44.908963Z","shell.execute_reply.started":"2024-07-01T17:29:35.172397Z","shell.execute_reply":"2024-07-01T17:30:44.908056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_submit = (test_pred >= 0.5).astype(float)\ny_test_submit=y_pred_submit.reshape(1,-1).squeeze()\ny_test_submit","metadata":{"execution":{"iopub.status.busy":"2024-07-01T17:30:50.306424Z","iopub.execute_input":"2024-07-01T17:30:50.307388Z","iopub.status.idle":"2024-07-01T17:30:50.314592Z","shell.execute_reply.started":"2024-07-01T17:30:50.307344Z","shell.execute_reply":"2024-07-01T17:30:50.313685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_id=datatest['id']\nresult = pd.DataFrame({\n    'id': result_id,\n    'label': y_test_submit\n})","metadata":{"execution":{"iopub.status.busy":"2024-07-01T17:30:53.260513Z","iopub.execute_input":"2024-07-01T17:30:53.260875Z","iopub.status.idle":"2024-07-01T17:30:53.266510Z","shell.execute_reply.started":"2024-07-01T17:30:53.260847Z","shell.execute_reply":"2024-07-01T17:30:53.265625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T17:30:56.156432Z","iopub.execute_input":"2024-07-01T17:30:56.156769Z","iopub.status.idle":"2024-07-01T17:30:56.180524Z","shell.execute_reply.started":"2024-07-01T17:30:56.156743Z","shell.execute_reply":"2024-07-01T17:30:56.179710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}