{"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":"# 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)\nimport zipfile\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-01-04T15:53:23.661295Z","iopub.execute_input":"2023-01-04T15:53:23.662087Z","iopub.status.idle":"2023-01-04T15:53:23.693959Z","shell.execute_reply.started":"2023-01-04T15:53:23.66198Z","shell.execute_reply":"2023-01-04T15:53:23.692508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip -d.","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:23.695945Z","iopub.execute_input":"2023-01-04T15:53:23.696295Z","iopub.status.idle":"2023-01-04T15:53:36.836085Z","shell.execute_reply.started":"2023-01-04T15:53:23.696264Z","shell.execute_reply":"2023-01-04T15:53:36.834819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip -d.","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:36.838434Z","iopub.execute_input":"2023-01-04T15:53:36.839248Z","iopub.status.idle":"2023-01-04T15:53:43.842062Z","shell.execute_reply.started":"2023-01-04T15:53:36.839186Z","shell.execute_reply":"2023-01-04T15:53:43.840821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'\ntest_dir = '/kaggle/working/test'\n","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:43.844566Z","iopub.execute_input":"2023-01-04T15:53:43.845546Z","iopub.status.idle":"2023-01-04T15:53:43.850147Z","shell.execute_reply.started":"2023-01-04T15:53:43.845504Z","shell.execute_reply":"2023-01-04T15:53:43.84916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_name = os.listdir(train_dir)\ntest_name = os.listdir(test_dir)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:43.851414Z","iopub.execute_input":"2023-01-04T15:53:43.852125Z","iopub.status.idle":"2023-01-04T15:53:43.883763Z","shell.execute_reply.started":"2023-01-04T15:53:43.852087Z","shell.execute_reply":"2023-01-04T15:53:43.882616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.DataFrame()\ntrain['file'] = train_name\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:43.885034Z","iopub.execute_input":"2023-01-04T15:53:43.885416Z","iopub.status.idle":"2023-01-04T15:53:43.937523Z","shell.execute_reply.started":"2023-01-04T15:53:43.885385Z","shell.execute_reply":"2023-01-04T15:53:43.936229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target=[]\nfor i in range(len(train)):\n    label=train['file'][i].split('.')[0]\n    if label=='dog':\n        target.append('1')\n    else:\n        target.append('0')\ntrain['class'] = target","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:43.938669Z","iopub.execute_input":"2023-01-04T15:53:43.938982Z","iopub.status.idle":"2023-01-04T15:53:44.127638Z","shell.execute_reply.started":"2023-01-04T15:53:43.938956Z","shell.execute_reply":"2023-01-04T15:53:44.126066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:44.12928Z","iopub.execute_input":"2023-01-04T15:53:44.129719Z","iopub.status.idle":"2023-01-04T15:53:44.134929Z","shell.execute_reply.started":"2023-01-04T15:53:44.129687Z","shell.execute_reply":"2023-01-04T15:53:44.13382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n  img = Image.open(train_dir + '/' + train['file'][i])\n  plt.imshow(img)\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:44.136641Z","iopub.execute_input":"2023-01-04T15:53:44.137107Z","iopub.status.idle":"2023-01-04T15:53:45.302731Z","shell.execute_reply.started":"2023-01-04T15:53:44.137043Z","shell.execute_reply":"2023-01-04T15:53:45.301464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport keras","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:45.307078Z","iopub.execute_input":"2023-01-04T15:53:45.308177Z","iopub.status.idle":"2023-01-04T15:53:52.009604Z","shell.execute_reply.started":"2023-01-04T15:53:45.308127Z","shell.execute_reply":"2023-01-04T15:53:52.008425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator=ImageDataGenerator(rescale=1/255, validation_split=0.2)\n\ntrain_gen = generator.flow_from_dataframe(train, directory=train_dir, x_col='file', \n                                y_col='class', target_size=(224,224), \n                                class_mode='binary', \n                                shuffle = True,\n                               subset='training')\n\nvalidation_gen = generator.flow_from_dataframe(train, directory=train_dir, x_col='file', \n                                y_col='class', target_size=(224, 224),\n                                shuffle=True,\n                                class_mode='binary', \n                               subset='validation')","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:52.011094Z","iopub.execute_input":"2023-01-04T15:53:52.011723Z","iopub.status.idle":"2023-01-04T15:53:52.496306Z","shell.execute_reply.started":"2023-01-04T15:53:52.011688Z","shell.execute_reply":"2023-01-04T15:53:52.494894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, MaxPooling2D, Dropout, BatchNormalization, Conv2D","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:52.497789Z","iopub.execute_input":"2023-01-04T15:53:52.498475Z","iopub.status.idle":"2023-01-04T15:53:52.50581Z","shell.execute_reply.started":"2023-01-04T15:53:52.498438Z","shell.execute_reply":"2023-01-04T15:53:52.5046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(96, (3,3), padding='same', activation='relu', input_shape=(224, 224, 3)))\n\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPooling2D((2, 2), strides=2))\n\nmodel.add(Conv2D(128, (3,3), padding='same', activation='relu'))\n\nmodel.add(BatchNormalization())\n\nmodel.add(MaxPooling2D((2, 2), strides=2))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation='relu'))\n\nmodel.add(Dense(1, activation='sigmoid'))\n","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:52.507286Z","iopub.execute_input":"2023-01-04T15:53:52.507668Z","iopub.status.idle":"2023-01-04T15:53:53.156292Z","shell.execute_reply.started":"2023-01-04T15:53:52.507635Z","shell.execute_reply":"2023-01-04T15:53:53.155141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:53.15757Z","iopub.execute_input":"2023-01-04T15:53:53.157902Z","iopub.status.idle":"2023-01-04T15:53:53.165713Z","shell.execute_reply.started":"2023-01-04T15:53:53.157871Z","shell.execute_reply":"2023-01-04T15:53:53.164534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:53.167267Z","iopub.execute_input":"2023-01-04T15:53:53.167703Z","iopub.status.idle":"2023-01-04T15:53:53.18714Z","shell.execute_reply.started":"2023-01-04T15:53:53.167661Z","shell.execute_reply":"2023-01-04T15:53:53.185906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_gen, epochs=5, validation_data=validation_gen)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T15:53:53.188725Z","iopub.execute_input":"2023-01-04T15:53:53.189172Z","iopub.status.idle":"2023-01-04T19:58:19.485842Z","shell.execute_reply.started":"2023-01-04T15:53:53.189122Z","shell.execute_reply":"2023-01-04T19:58:19.48485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('cnn1.h5')","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:04:47.569704Z","iopub.execute_input":"2023-01-04T20:04:47.570301Z","iopub.status.idle":"2023-01-04T20:04:48.655337Z","shell.execute_reply.started":"2023-01-04T20:04:47.570263Z","shell.execute_reply":"2023-01-04T20:04:48.65424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model = keras.models.load_model('cnn1')","metadata":{"execution":{"iopub.status.busy":"2023-01-04T19:58:19.493668Z","iopub.execute_input":"2023-01-04T19:58:19.49462Z","iopub.status.idle":"2023-01-04T19:58:19.505105Z","shell.execute_reply.started":"2023-01-04T19:58:19.494588Z","shell.execute_reply":"2023-01-04T19:58:19.504154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:22:59.983682Z","iopub.execute_input":"2023-01-04T20:22:59.984093Z","iopub.status.idle":"2023-01-04T20:23:00.007407Z","shell.execute_reply.started":"2023-01-04T20:22:59.984046Z","shell.execute_reply":"2023-01-04T20:23:00.006195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=df.copy()\ntest['file']=test['id'].astype('str')+'.jpg'\ntest","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:26:06.095358Z","iopub.execute_input":"2023-01-04T20:26:06.095791Z","iopub.status.idle":"2023-01-04T20:26:06.120606Z","shell.execute_reply.started":"2023-01-04T20:26:06.095754Z","shell.execute_reply":"2023-01-04T20:26:06.1195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = generator.flow_from_dataframe(test, directory=test_dir, x_col='file', y_col=None, target_size=(224,224),\n                               class_mode=None, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:31:22.815686Z","iopub.execute_input":"2023-01-04T20:31:22.81612Z","iopub.status.idle":"2023-01-04T20:31:22.942502Z","shell.execute_reply.started":"2023-01-04T20:31:22.816079Z","shell.execute_reply":"2023-01-04T20:31:22.941012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:45:39.255803Z","iopub.execute_input":"2023-01-04T20:45:39.256205Z","iopub.status.idle":"2023-01-04T20:45:39.262824Z","shell.execute_reply.started":"2023-01-04T20:45:39.256173Z","shell.execute_reply":"2023-01-04T20:45:39.261709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_gen)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:34:08.100819Z","iopub.execute_input":"2023-01-04T20:34:08.101249Z","iopub.status.idle":"2023-01-04T20:40:36.503117Z","shell.execute_reply.started":"2023-01-04T20:34:08.101211Z","shell.execute_reply":"2023-01-04T20:40:36.502149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label']=pred\ndf.to_csv('sumbission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T20:49:29.513106Z","iopub.execute_input":"2023-01-04T20:49:29.513487Z","iopub.status.idle":"2023-01-04T20:49:29.547153Z","shell.execute_reply.started":"2023-01-04T20:49:29.513456Z","shell.execute_reply":"2023-01-04T20:49:29.545806Z"},"trusted":true},"execution_count":null,"outputs":[]}]}