{"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\nimport seaborn as sns\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\n\nfrom tensorflow.keras.datasets import cifar10\n\nfrom tensorflow import device\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.optimizers import Adam,Adadelta\nfrom tensorflow.keras.layers import Dense,Conv2D,Activation,BatchNormalization,MaxPooling2D,Dropout,Flatten,experimental,Input,concatenate,GlobalAveragePooling2D\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import backend as K ","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-27T11:29:03.991852Z","iopub.execute_input":"2022-01-27T11:29:03.992387Z","iopub.status.idle":"2022-01-27T11:29:11.930793Z","shell.execute_reply.started":"2022-01-27T11:29:03.99235Z","shell.execute_reply":"2022-01-27T11:29:11.929833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_set = \"dogs-vs-cats\"\nimport zipfile \nwith zipfile.ZipFile(\"/kaggle/input/\"+ data_set +\"/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    # save all files to kaggle/files/images\n    destination = '/kaggle/files/images/train'\n    z.extractall(destination)\n    \nwith zipfile.ZipFile(\"/kaggle/input/\"+ data_set +\"/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    # save all files to kaggle/files/images\n    destination = '/kaggle/files/images/test'\n    z.extractall(destination)\ndef list_full_paths(directory):\n    return [os.path.join(directory, file) for file in os.listdir(directory)]\n\ntrain = pd.DataFrame({'filepath': list_full_paths('/kaggle/files/images/train/train')})\ntrain['truth_label'] = np.where(train['filepath'].str.contains('dog'), 'dog', 'cat')\n\ntest = pd.DataFrame({'filepath': list_full_paths('/kaggle/files/images/test/test1')})","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:00:15.718468Z","iopub.execute_input":"2022-01-26T21:00:15.718723Z","iopub.status.idle":"2022-01-26T21:00:45.142364Z","shell.execute_reply.started":"2022-01-26T21:00:15.718694Z","shell.execute_reply":"2022-01-26T21:00:45.141584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val = train_test_split(train, test_size=0.2,random_state = 10)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:00:45.144103Z","iopub.execute_input":"2022-01-26T21:00:45.144373Z","iopub.status.idle":"2022-01-26T21:00:45.15574Z","shell.execute_reply.started":"2022-01-26T21:00:45.144334Z","shell.execute_reply":"2022-01-26T21:00:45.155116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_shape = 224\nks = 16\ndrop_size = 0.1\nact = \"relu\"\nfrom keras.callbacks import LearningRateScheduler\ndef lr_schedule(epoch):\n    lrate = 0.001\n    if epoch > 10:\n        lrate = 0.0005\n    if epoch > 20:\n        lrate = 0.00025\n    if epoch > 30:\n        lrate = 0.0001\n    return lrate","metadata":{"execution":{"iopub.status.busy":"2022-01-27T11:30:33.317593Z","iopub.execute_input":"2022-01-27T11:30:33.318635Z","iopub.status.idle":"2022-01-27T11:30:33.326433Z","shell.execute_reply.started":"2022-01-27T11:30:33.318578Z","shell.execute_reply":"2022-01-27T11:30:33.325348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(\n    \n    rotation_range=35,\n    horizontal_flip=True,\n    width_shift_range=0.25,\n    height_shift_range=0.25,\n    rescale = 1./255,\n    zoom_range =0.3\n    \n    )\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:00:45.185982Z","iopub.execute_input":"2022-01-26T21:00:45.18618Z","iopub.status.idle":"2022-01-26T21:00:45.193903Z","shell.execute_reply.started":"2022-01-26T21:00:45.186155Z","shell.execute_reply":"2022-01-26T21:00:45.193145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = train_datagen.flow_from_dataframe(dataframe=X_train, x_col='filepath', y_col='truth_label', class_mode='categorical', target_size = (img_shape, img_shape), batch_size = 32)\nval_set = test_datagen.flow_from_dataframe(dataframe=X_val, x_col='filepath', y_col='truth_label', class_mode='categorical', target_size = (img_shape, img_shape), batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:00:45.195354Z","iopub.execute_input":"2022-01-26T21:00:45.195691Z","iopub.status.idle":"2022-01-26T21:00:45.473479Z","shell.execute_reply.started":"2022-01-26T21:00:45.195656Z","shell.execute_reply":"2022-01-26T21:00:45.471757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test_datagen.flow_from_dataframe(dataframe=test,\n    directory = '/kaggle/files/images/test',\n    x_col = 'filepath',\n    y_col = None,\n    class_mode = None,\n    target_size = (img_shape, img_shape),\n    batch_size = 32,\n    shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:00:45.474725Z","iopub.execute_input":"2022-01-26T21:00:45.475142Z","iopub.status.idle":"2022-01-26T21:00:45.590582Z","shell.execute_reply.started":"2022-01-26T21:00:45.475104Z","shell.execute_reply":"2022-01-26T21:00:45.5897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50","metadata":{"execution":{"iopub.status.busy":"2022-01-27T11:29:20.402337Z","iopub.execute_input":"2022-01-27T11:29:20.402873Z","iopub.status.idle":"2022-01-27T11:29:20.408166Z","shell.execute_reply.started":"2022-01-27T11:29:20.402821Z","shell.execute_reply":"2022-01-27T11:29:20.407195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model= ResNet50(weights='imagenet',\n                   include_top=False,\n                   pooling = \"max\",\n                  input_shape=(224,224,3))\nx = Dense(2048,activation = act)(model.output)\nx = BatchNormalization()(x)\nx = Dropout(drop_size*4)(x)\nx = Dense(2048,activation = act)(x)\nx = BatchNormalization()(x)\nx = Dropout(drop_size*4)(x)\nx = Dense(2,activation = \"softmax\")(x)\nmodel = Model(model.input,outputs = x)\nmodel.compile(optimizer = \"adam\",\n                     loss = \"binary_crossentropy\",\n                     metrics = [\"accuracy\"])\n\nfor layer in model.layers[:-8]:\n    layer.trainable = False\nfor layer in model.layers[-8:]:\n    layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-01-27T11:32:09.537345Z","iopub.execute_input":"2022-01-27T11:32:09.537705Z","iopub.status.idle":"2022-01-27T11:32:11.765038Z","shell.execute_reply.started":"2022-01-27T11:32:09.537667Z","shell.execute_reply":"2022-01-27T11:32:11.763931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-27T11:32:11.886693Z","iopub.execute_input":"2022-01-27T11:32:11.887707Z","iopub.status.idle":"2022-01-27T11:32:12.009163Z","shell.execute_reply.started":"2022-01-27T11:32:11.887667Z","shell.execute_reply":"2022-01-27T11:32:12.008189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(training_set,validation_data = val_set,callbacks=[LearningRateScheduler(lr_schedule)],epochs = 40)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T21:05:11.524094Z","iopub.execute_input":"2022-01-26T21:05:11.524632Z","iopub.status.idle":"2022-01-27T00:24:00.165528Z","shell.execute_reply.started":"2022-01-26T21:05:11.524595Z","shell.execute_reply":"2022-01-27T00:24:00.164785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-27T07:32:24.060113Z","iopub.execute_input":"2022-01-27T07:32:24.06041Z","iopub.status.idle":"2022-01-27T07:32:24.148817Z","shell.execute_reply.started":"2022-01-27T07:32:24.060319Z","shell.execute_reply":"2022-01-27T07:32:24.14776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_preds = model.predict(test, steps = np.ceil(12500 /32))\n\n# test[\"test_preds\"] = np.argmax(test_preds, axis = 1)\n# labels = dict((v,k) for k,v in training_set.class_indices.items())\n\n# test['test_preds'] = test['test_preds'].map(labels)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T20:19:51.886585Z","iopub.status.idle":"2022-01-26T20:19:51.886952Z","shell.execute_reply.started":"2022-01-26T20:19:51.886771Z","shell.execute_reply":"2022-01-26T20:19:51.886789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}