{"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\nimport os\nimport zipfile\ntrain_zip = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\")\ntrain_zip.extractall(\"/kaggle/working/dataset\")\ntrain_zip.close()\ntrain_dir='/kaggle/working/dataset/train'\ntrain_files=os.listdir(train_dir)\ncategories=[]\nfor filename in train_files:\n    if filename.split('.')[0]=='cat':\n        categories.append('cat')\n    else:\n        categories.append('dog')\ndf=pd.DataFrame({'filename':train_files,'category':categories})\ndf\nSEED=42\ndf = df.sample(frac = 1, random_state = SEED).reset_index(drop=True)\ndf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-16T07:55:53.280730Z","iopub.execute_input":"2023-01-16T07:55:53.281246Z","iopub.status.idle":"2023-01-16T07:56:01.168157Z","shell.execute_reply.started":"2023-01-16T07:55:53.281211Z","shell.execute_reply":"2023-01-16T07:56:01.166501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\nlabels = preprocessing.LabelEncoder()\ndf['label'] = labels.fit_transform(df['category'])\ndf\nfrom sklearn.model_selection import train_test_split\ntrain_df,test_df=train_test_split(df,test_size=0.1,random_state=42)\ntrain_df.reset_index(drop = True, inplace = True)\ntest_df.reset_index(drop = True, inplace = True)\n#print(train_df.head(10))\ntrain_df_final, validation_df = train_test_split(train_df, test_size = 0.2, random_state = SEED)\ntrain_df_final.reset_index(drop = True, inplace = True)\nvalidation_df.reset_index(drop = True, inplace = True)\nprint(validation_df.head(10))","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:01.170896Z","iopub.execute_input":"2023-01-16T07:56:01.171372Z","iopub.status.idle":"2023-01-16T07:56:01.209439Z","shell.execute_reply.started":"2023-01-16T07:56:01.171327Z","shell.execute_reply":"2023-01-16T07:56:01.207440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install gdown","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:01.212316Z","iopub.execute_input":"2023-01-16T07:56:01.213017Z","iopub.status.idle":"2023-01-16T07:56:10.986704Z","shell.execute_reply.started":"2023-01-16T07:56:01.212972Z","shell.execute_reply":"2023-01-16T07:56:10.984979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id https://drive.google.com/file/d/1nBK_UkO2bN3xHPhbm_nOEcA1Et0g-Kp5/view?usp=share_link","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:10.991112Z","iopub.execute_input":"2023-01-16T07:56:10.992266Z","iopub.status.idle":"2023-01-16T07:56:12.602752Z","shell.execute_reply.started":"2023-01-16T07:56:10.992183Z","shell.execute_reply":"2023-01-16T07:56:12.601643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown --id https://drive.google.com/file/d/1nBK_UkO2bN3xHPhbm_nOEcA1Et0g-Kp5/view?usp=share_link","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:12.604436Z","iopub.execute_input":"2023-01-16T07:56:12.605306Z","iopub.status.idle":"2023-01-16T07:56:13.996229Z","shell.execute_reply.started":"2023-01-16T07:56:12.605271Z","shell.execute_reply":"2023-01-16T07:56:13.994578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown 1nBK_UkO2bN3xHPhbm_nOEcA1Et0g-Kp5","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:13.999651Z","iopub.execute_input":"2023-01-16T07:56:14.000089Z","iopub.status.idle":"2023-01-16T07:56:19.319192Z","shell.execute_reply.started":"2023-01-16T07:56:14.000052Z","shell.execute_reply":"2023-01-16T07:56:19.317731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_zip = zipfile.ZipFile('/kaggle/working/train1.zip')\ntrain_zip.extractall('/kaggle/working/Dataset2')","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:19.320742Z","iopub.execute_input":"2023-01-16T07:56:19.321213Z","iopub.status.idle":"2023-01-16T07:56:26.964146Z","shell.execute_reply.started":"2023-01-16T07:56:19.321167Z","shell.execute_reply":"2023-01-16T07:56:26.961931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\n\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomFlip, RandomRotation\nBATCH_SIZE = 600\nIMG_SIZE = (96, 96)\ndirectory = \"/kaggle/working/Dataset2/train1\"\ntrain_dataset = image_dataset_from_directory(directory,\n                                             shuffle=True,\n                                             batch_size=BATCH_SIZE,\n                                             image_size=IMG_SIZE,\n                                             validation_split=0.2,\n                                             subset='training',\n                                             seed=42)\nvalidation_dataset = image_dataset_from_directory(directory,\n                                             shuffle=True,\n                                             batch_size=BATCH_SIZE,\n                                             image_size=IMG_SIZE,\n                                             validation_split=0.2,\n                                             subset='validation',\n                                             seed=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:26.965703Z","iopub.execute_input":"2023-01-16T07:56:26.966115Z","iopub.status.idle":"2023-01-16T07:56:28.257963Z","shell.execute_reply.started":"2023-01-16T07:56:26.966077Z","shell.execute_reply":"2023-01-16T07:56:28.256490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_dataset.class_names\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_dataset.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:28.262255Z","iopub.execute_input":"2023-01-16T07:56:28.262724Z","iopub.status.idle":"2023-01-16T07:56:30.410236Z","shell.execute_reply.started":"2023-01-16T07:56:28.262682Z","shell.execute_reply":"2023-01-16T07:56:30.409043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\ntrain_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:30.413278Z","iopub.execute_input":"2023-01-16T07:56:30.413622Z","iopub.status.idle":"2023-01-16T07:56:30.420407Z","shell.execute_reply.started":"2023-01-16T07:56:30.413591Z","shell.execute_reply":"2023-01-16T07:56:30.418773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout, MaxPool2D, Rescaling\nfrom tensorflow.keras.models import Model\ni=Input(IMG_SIZE+(3,))\nx=Rescaling(1./255)(i)\nx=Conv2D(120,(2,2),strides=2,activation='relu')(x)\n#x=Conv2D(80,(2,2),strides=2,activation='relu')(x)\n#x=MaxPool2D((2,2),strides=2)(x)\nx=Conv2D(120,(2,2),strides=2,activation='relu')(x)\nx=Conv2D(120,(2,2),strides=1,activation='relu')(x)\nx=MaxPool2D((2,2),strides=2)(x)\nx=Conv2D(120,(2,2),strides=2,activation='relu')(x)\n#x=MaxPool2D((2,2),strides=2)(x)\nx=Flatten()(x)\nx=Dense(512,activation='relu')(x)\nx=Dense(256,activation='relu')(x)\n#x=Dense(128,activation='relu')(x)\nx=Dense(64,activation='relu')(x)\nx=Dense(2,activation='softmax')(x)\nmodel=Model(i,x)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:30.422040Z","iopub.execute_input":"2023-01-16T07:56:30.422385Z","iopub.status.idle":"2023-01-16T07:56:30.529207Z","shell.execute_reply.started":"2023-01-16T07:56:30.422355Z","shell.execute_reply":"2023-01-16T07:56:30.528308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])\nr=model.fit(train_dataset, validation_data=validation_dataset,epochs=7,verbose=2)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T07:56:30.530354Z","iopub.execute_input":"2023-01-16T07:56:30.531642Z","iopub.status.idle":"2023-01-16T08:13:41.098953Z","shell.execute_reply.started":"2023-01-16T07:56:30.531581Z","shell.execute_reply":"2023-01-16T08:13:41.092604Z"},"trusted":true},"execution_count":null,"outputs":[]}]}