{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-17T09:28:05.826341Z","iopub.execute_input":"2021-06-17T09:28:05.827105Z","iopub.status.idle":"2021-06-17T09:28:11.749467Z","shell.execute_reply.started":"2021-06-17T09:28:05.826974Z","shell.execute_reply":"2021-06-17T09:28:11.748529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load data\nimport zipfile\nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:28:11.751172Z","iopub.execute_input":"2021-06-17T09:28:11.751477Z","iopub.status.idle":"2021-06-17T09:28:32.0574Z","shell.execute_reply.started":"2021-06-17T09:28:11.751449Z","shell.execute_reply":"2021-06-17T09:28:32.056155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocessing","metadata":{}},{"cell_type":"code","source":"data = os.listdir('./train') #list of all files","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:00.344869Z","iopub.execute_input":"2021-06-17T09:29:00.345285Z","iopub.status.idle":"2021-06-17T09:29:00.367143Z","shell.execute_reply.started":"2021-06-17T09:29:00.34525Z","shell.execute_reply":"2021-06-17T09:29:00.365886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create labels\nlabels = []\nfor file_name in data:\n    label, _, __ = file_name.split('.')\n    labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:27.262165Z","iopub.execute_input":"2021-06-17T09:29:27.262549Z","iopub.status.idle":"2021-06-17T09:29:27.281012Z","shell.execute_reply.started":"2021-06-17T09:29:27.262517Z","shell.execute_reply":"2021-06-17T09:29:27.279834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create dataset\ntrain = pd.DataFrame({'files' : data, 'label' : labels })\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:28.808326Z","iopub.execute_input":"2021-06-17T09:29:28.808671Z","iopub.status.idle":"2021-06-17T09:29:28.850778Z","shell.execute_reply.started":"2021-06-17T09:29:28.80864Z","shell.execute_reply":"2021-06-17T09:29:28.849405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shuffle data\ntrain = train.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:36.914422Z","iopub.execute_input":"2021-06-17T09:29:36.914819Z","iopub.status.idle":"2021-06-17T09:29:36.924806Z","shell.execute_reply.started":"2021-06-17T09:29:36.914788Z","shell.execute_reply":"2021-06-17T09:29:36.923453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data augmentation\ntrain_d = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255,\n      rotation_range=20,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True) \n\nvalid_d = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255,\n      rotation_range=20,\n      width_shift_range=0.2,\n      height_shift_range=0.2,\n      shear_range=0.2,\n      zoom_range=0.2,\n      horizontal_flip=True) ","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:45.671623Z","iopub.execute_input":"2021-06-17T09:29:45.672013Z","iopub.status.idle":"2021-06-17T09:29:45.679737Z","shell.execute_reply.started":"2021-06-17T09:29:45.671979Z","shell.execute_reply":"2021-06-17T09:29:45.678293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split data into training and validation sets\ntrain_generator = train_d.flow_from_dataframe(train.iloc[:20000, :],\n                                              directory='./train', \n                                              x_col=\"files\",\n                                              y_col=\"label\",\n                                              target_size=(224, 224))\n                                              #,\n                                              #class_mode='binary')\n\nvalid_generator = valid_d.flow_from_dataframe(train.iloc[20000:, :],\n                                              directory='./train', \n                                              x_col=\"files\",\n                                              y_col=\"label\",\n                                              target_size=(224, 224) )\n                                              #,\n                                              #class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:29:54.533134Z","iopub.execute_input":"2021-06-17T09:29:54.533619Z","iopub.status.idle":"2021-06-17T09:29:54.867039Z","shell.execute_reply.started":"2021-06-17T09:29:54.533587Z","shell.execute_reply":"2021-06-17T09:29:54.8657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define the Network","metadata":{}},{"cell_type":"code","source":"inputs = tf.keras.layers.Input(shape=(224,224, 3))\nx = tf.keras.applications.resnet.ResNet50(input_shape=(224, 224, 3),\n                                               include_top=False,\n                                               weights='imagenet')(inputs)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Flatten()(x)\nx = tf.keras.layers.Dense(1024, activation=\"relu\")(x)\nx = tf.keras.layers.Dense(512, activation=\"relu\")(x)\nx = tf.keras.layers.Dense(2, activation=\"sigmoid\", name=\"classification\")(x)\nmodel = tf.keras.Model(inputs=inputs, outputs = x)\nmodel.compile(optimizer='adam', \n                loss='binary_crossentropy',\n                metrics = ['acc'])\n   ","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:38:11.473496Z","iopub.execute_input":"2021-06-17T09:38:11.473874Z","iopub.status.idle":"2021-06-17T09:38:14.467614Z","shell.execute_reply.started":"2021-06-17T09:38:11.47384Z","shell.execute_reply":"2021-06-17T09:38:14.466543Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:38:19.525396Z","iopub.execute_input":"2021-06-17T09:38:19.525823Z","iopub.status.idle":"2021-06-17T09:38:19.551539Z","shell.execute_reply.started":"2021-06-17T09:38:19.525783Z","shell.execute_reply":"2021-06-17T09:38:19.549943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, epochs=3, validation_data = valid_generator, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2021-06-17T09:39:29.945075Z","iopub.execute_input":"2021-06-17T09:39:29.945435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}