{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":34426,"databundleVersionId":3218632,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-12-23T18:24:29.481227Z","iopub.execute_input":"2023-12-23T18:24:29.481684Z","iopub.status.idle":"2023-12-23T18:24:47.295178Z","shell.execute_reply.started":"2023-12-23T18:24:29.481647Z","shell.execute_reply":"2023-12-23T18:24:47.293519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, BatchNormalization, Dropout\n                    ","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:27:24.874235Z","iopub.execute_input":"2023-12-23T18:27:24.875111Z","iopub.status.idle":"2023-12-23T18:27:24.882242Z","shell.execute_reply.started":"2023-12-23T18:27:24.875072Z","shell.execute_reply":"2023-12-23T18:27:24.880648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = keras.utils.image_dataset_from_directory(\n    directory='/kaggle/input/dog-vs-cat-classification/train',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(256, 256)\n)\n\ntest = keras.utils.image_dataset_from_directory(\n    directory='/kaggle/input/dog-vs-cat-classification/test',\n    labels=\"inferred\",\n    label_mode=\"int\",\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(256, 256)\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:27:51.377562Z","iopub.execute_input":"2023-12-23T18:27:51.378076Z","iopub.status.idle":"2023-12-23T18:28:12.801079Z","shell.execute_reply.started":"2023-12-23T18:27:51.378026Z","shell.execute_reply":"2023-12-23T18:28:12.799979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create CNN model\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32,kernel_size=(3,3),padding='valid',activation='relu',input_shape=(256,256,3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Conv2D(64,kernel_size=(3,3),padding='valid',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Conv2D(128,kernel_size=(3,3),padding='valid',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(1,activation='sigmoid'))\n","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:41:43.010222Z","iopub.execute_input":"2023-12-23T18:41:43.010664Z","iopub.status.idle":"2023-12-23T18:41:44.075493Z","shell.execute_reply.started":"2023-12-23T18:41:43.010630Z","shell.execute_reply":"2023-12-23T18:41:44.074103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:41:47.044003Z","iopub.execute_input":"2023-12-23T18:41:47.044938Z","iopub.status.idle":"2023-12-23T18:41:47.113991Z","shell.execute_reply.started":"2023-12-23T18:41:47.044895Z","shell.execute_reply":"2023-12-23T18:41:47.112502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:41:57.019869Z","iopub.execute_input":"2023-12-23T18:41:57.020281Z","iopub.status.idle":"2023-12-23T18:41:57.042316Z","shell.execute_reply.started":"2023-12-23T18:41:57.020238Z","shell.execute_reply":"2023-12-23T18:41:57.041080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train,epochs=2,validation_data=test)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:54:48.719741Z","iopub.execute_input":"2023-12-23T18:54:48.720165Z","iopub.status.idle":"2023-12-23T20:33:32.646535Z","shell.execute_reply.started":"2023-12-23T18:54:48.720134Z","shell.execute_reply":"2023-12-23T20:33:32.645194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\n\nplot_model(model, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T18:54:17.167528Z","iopub.execute_input":"2023-12-23T18:54:17.168139Z","iopub.status.idle":"2023-12-23T18:54:17.548995Z","shell.execute_reply.started":"2023-12-23T18:54:17.168099Z","shell.execute_reply":"2023-12-23T18:54:17.547634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/input/dog-vs-cat-classification/test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})","metadata":{"execution":{"iopub.status.busy":"2023-12-23T20:43:15.190784Z","iopub.execute_input":"2023-12-23T20:43:15.191566Z","iopub.status.idle":"2023-12-23T20:43:15.202437Z","shell.execute_reply.started":"2023-12-23T20:43:15.191527Z","shell.execute_reply":"2023-12-23T20:43:15.201599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test)\ntest_df['labels'] = np.argmax(predict, axis=-1)\nprint(test_df)\ntest_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-23T20:44:08.727305Z","iopub.execute_input":"2023-12-23T20:44:08.727832Z","iopub.status.idle":"2023-12-23T20:48:30.676608Z","shell.execute_reply.started":"2023-12-23T20:44:08.727795Z","shell.execute_reply":"2023-12-23T20:48:30.675285Z"},"trusted":true},"execution_count":null,"outputs":[]}]}