{"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)\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":"2022-08-22T19:43:44.162346Z","iopub.execute_input":"2022-08-22T19:43:44.163233Z","iopub.status.idle":"2022-08-22T19:43:44.181105Z","shell.execute_reply.started":"2022-08-22T19:43:44.163115Z","shell.execute_reply":"2022-08-22T19:43:44.179767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models, optimizers\nfrom keras.layers import Dense, MaxPool2D, Conv2D, Dropout, Flatten, GlobalAveragePooling2D, BatchNormalization, Activation, MaxPooling2D\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras.preprocessing.image import load_img\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:44.188681Z","iopub.execute_input":"2022-08-22T19:43:44.189447Z","iopub.status.idle":"2022-08-22T19:43:46.717611Z","shell.execute_reply.started":"2022-08-22T19:43:44.189405Z","shell.execute_reply":"2022-08-22T19:43:46.716449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_zip='../input/dogs-vs-cats-redux-kernels-edition/train.zip'\nzip_ref=zipfile.ZipFile(train_zip,'r')\nzip_ref.extractall('/kaggle/working/')\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:46.719162Z","iopub.execute_input":"2022-08-22T19:43:46.720234Z","iopub.status.idle":"2022-08-22T19:43:58.551647Z","shell.execute_reply.started":"2022-08-22T19:43:46.720164Z","shell.execute_reply":"2022-08-22T19:43:58.550457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '/kaggle/working/'\ntrain_dir = os.path.join(base_dir, 'train')\ntrain_img_path = os.listdir(train_dir)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.555701Z","iopub.execute_input":"2022-08-22T19:43:58.556219Z","iopub.status.idle":"2022-08-22T19:43:58.580928Z","shell.execute_reply.started":"2022-08-22T19:43:58.556152Z","shell.execute_reply":"2022-08-22T19:43:58.579747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('total training images:',len(train_img_path))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.582457Z","iopub.execute_input":"2022-08-22T19:43:58.583738Z","iopub.status.idle":"2022-08-22T19:43:58.591597Z","shell.execute_reply.started":"2022-08-22T19:43:58.583693Z","shell.execute_reply":"2022-08-22T19:43:58.590122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories=[]\nfor img in train_img_path :\n    category=img.split('.')[0]\n    if category=='dog':\n        categories.append('dog')\n    else :\n        categories.append('cat')\n        \ndf=pd.DataFrame({'Image':train_img_path,'Category':categories})","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.594007Z","iopub.execute_input":"2022-08-22T19:43:58.594486Z","iopub.status.idle":"2022-08-22T19:43:58.623571Z","shell.execute_reply.started":"2022-08-22T19:43:58.594439Z","shell.execute_reply":"2022-08-22T19:43:58.622409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nsns.countplot(data=df,x='Category');","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.625687Z","iopub.execute_input":"2022-08-22T19:43:58.626161Z","iopub.status.idle":"2022-08-22T19:43:58.874463Z","shell.execute_reply.started":"2022-08-22T19:43:58.626117Z","shell.execute_reply":"2022-08-22T19:43:58.873224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \ntrain,validation=train_test_split(df,test_size=0.2,random_state=43)\ntrain=train.reset_index(drop=True)\nvalidation=validation.reset_index(drop=True)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.876164Z","iopub.execute_input":"2022-08-22T19:43:58.877352Z","iopub.status.idle":"2022-08-22T19:43:58.955115Z","shell.execute_reply.started":"2022-08-22T19:43:58.877307Z","shell.execute_reply":"2022-08-22T19:43:58.953726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.957074Z","iopub.execute_input":"2022-08-22T19:43:58.957781Z","iopub.status.idle":"2022-08-22T19:43:58.971027Z","shell.execute_reply.started":"2022-08-22T19:43:58.957734Z","shell.execute_reply":"2022-08-22T19:43:58.969657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.973029Z","iopub.execute_input":"2022-08-22T19:43:58.974479Z","iopub.status.idle":"2022-08-22T19:43:58.982434Z","shell.execute_reply.started":"2022-08-22T19:43:58.974431Z","shell.execute_reply":"2022-08-22T19:43:58.981041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(train,\n                                                    directory=\"./train\",\n                                                    x_col='Image',\n                                                    y_col='Category',\n                                                    batch_size=32,\n                                                    class_mode='binary',\n                                                    target_size=(150, 150)) ","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:58.984702Z","iopub.execute_input":"2022-08-22T19:43:58.985978Z","iopub.status.idle":"2022-08-22T19:43:59.210134Z","shell.execute_reply.started":"2022-08-22T19:43:58.985932Z","shell.execute_reply":"2022-08-22T19:43:59.208903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = datagen.flow_from_dataframe(validation,\n                                                    directory=\"./train\",\n                                                    x_col='Image',\n                                                    y_col='Category',\n                                                    batch_size=32,\n                                                    class_mode='binary',\n                                                    target_size=(150, 150)) ","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:59.211896Z","iopub.execute_input":"2022-08-22T19:43:59.212448Z","iopub.status.idle":"2022-08-22T19:43:59.281586Z","shell.execute_reply.started":"2022-08-22T19:43:59.212400Z","shell.execute_reply":"2022-08-22T19:43:59.280257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in range(5):\n    img, label = train_generator.next()\n    print(img.shape) \n    plt.imshow(img[0])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:43:59.286506Z","iopub.execute_input":"2022-08-22T19:43:59.286973Z","iopub.status.idle":"2022-08-22T19:44:01.056176Z","shell.execute_reply.started":"2022-08-22T19:43:59.286924Z","shell.execute_reply":"2022-08-22T19:44:01.055077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape=(150,150,3)))\nmodel.add(layers.MaxPooling2D((2,2)))\n \nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\n \nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\n \nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\n\nmodel.add(layers.Flatten())\n\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy',optimizer=optimizers.Adam(learning_rate=1e-4),metrics=['acc'])\n \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:44:01.057776Z","iopub.execute_input":"2022-08-22T19:44:01.058852Z","iopub.status.idle":"2022-08-22T19:44:02.355757Z","shell.execute_reply.started":"2022-08-22T19:44:01.058817Z","shell.execute_reply":"2022-08-22T19:44:02.354514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator,steps_per_epoch=20000/32,epochs=20,validation_data=validation_generator,validation_steps=5000/32)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T19:44:02.357794Z","iopub.execute_input":"2022-08-22T19:44:02.358643Z","iopub.status.idle":"2022-08-22T20:13:53.929703Z","shell.execute_reply.started":"2022-08-22T19:44:02.358597Z","shell.execute_reply":"2022-08-22T20:13:53.928502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = ['loss', 'acc']  \nplt.figure(figsize=(10, 5)) \nfor i in range(len(metrics)):\n \n    metric = metrics[i]\n \n    plt.subplot(1, 2, i+1)  \n    plt.title(metric) \n    \n    plt_train = history.history[metric] \n    plt_test = history.history['val_' + metric]  \n    \n    plt.plot(plt_train, label='training')  \n    plt.plot(plt_test, label='test')  \n    plt.legend()  \n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:16:16.515400Z","iopub.execute_input":"2022-08-22T20:16:16.516263Z","iopub.status.idle":"2022-08-22T20:16:16.892094Z","shell.execute_reply.started":"2022-08-22T20:16:16.516182Z","shell.execute_reply":"2022-08-22T20:16:16.890774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('model_catsvsdogs.h5')\nmodel.save('model_catsvsdogs.h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:16:46.079706Z","iopub.execute_input":"2022-08-22T20:16:46.080282Z","iopub.status.idle":"2022-08-22T20:16:46.290740Z","shell.execute_reply.started":"2022-08-22T20:16:46.080215Z","shell.execute_reply":"2022-08-22T20:16:46.289176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_zip='../input/dogs-vs-cats-redux-kernels-edition/test.zip'\nzip_refe=zipfile.ZipFile(test_zip,'r')\nzip_refe.extractall('/kaggle/working/')\nzip_refe.close()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:16:55.523842Z","iopub.execute_input":"2022-08-22T20:16:55.524537Z","iopub.status.idle":"2022-08-22T20:17:03.806018Z","shell.execute_reply.started":"2022-08-22T20:16:55.524486Z","shell.execute_reply":"2022-08-22T20:17:03.804799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_files = os.listdir('/kaggle/working/test')\ntest_df = pd.DataFrame(data = test_image_files, columns = ['filename'])\ntest_df['id'] = test_df['filename'].apply(lambda f: int(f.split('.')[0]))\ntest_df.sort_values(by = 'id', inplace = True, ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:17:04.958137Z","iopub.execute_input":"2022-08-22T20:17:04.958662Z","iopub.status.idle":"2022-08-22T20:17:05.049445Z","shell.execute_reply.started":"2022-08-22T20:17:04.958616Z","shell.execute_reply":"2022-08-22T20:17:05.047370Z"},"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', \n    x_col='filename',\n    class_mode= None,\n    target_size=(150,150),\n    batch_size=32,\n    shuffle=False\n)\npredict = model.predict(test_generator,batch_size=32,max_queue_size=1, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:17:52.510620Z","iopub.execute_input":"2022-08-22T20:17:52.511029Z","iopub.status.idle":"2022-08-22T20:18:42.991593Z","shell.execute_reply.started":"2022-08-22T20:17:52.510996Z","shell.execute_reply":"2022-08-22T20:18:42.990293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"label\"] = predict\nresult = test_df[[\"id\", \"label\"]]\nresult.to_csv('submission_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T20:18:42.999125Z","iopub.execute_input":"2022-08-22T20:18:43.000164Z","iopub.status.idle":"2022-08-22T20:18:43.067355Z","shell.execute_reply.started":"2022-08-22T20:18:43.000113Z","shell.execute_reply":"2022-08-22T20:18:43.065159Z"},"trusted":true},"execution_count":null,"outputs":[]}]}