{"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 matplotlib.pyplot as plt\nimport tensorflow as tf\nimport zipfile\nimport pandas as pd\nimport numpy as np\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom tensorflow import keras\nfrom keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory","metadata":{"id":"aczs0Ud96WHY","papermill":{"duration":4.952168,"end_time":"2022-03-20T20:00:07.03682","exception":false,"start_time":"2022-03-20T20:00:02.084652","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:15.47848Z","iopub.execute_input":"2022-03-21T12:33:15.479464Z","iopub.status.idle":"2022-03-21T12:33:20.689018Z","shell.execute_reply.started":"2022-03-21T12:33:15.479349Z","shell.execute_reply":"2022-03-21T12:33:20.688259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"id":"QSvcLfoGyBaj","outputId":"3443faec-d05e-485d-9aae-a4a650d1375c","papermill":{"duration":0.027105,"end_time":"2022-03-20T20:00:07.082598","exception":false,"start_time":"2022-03-20T20:00:07.055493","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:20.69072Z","iopub.execute_input":"2022-03-21T12:33:20.690974Z","iopub.status.idle":"2022-03-21T12:33:20.700165Z","shell.execute_reply.started":"2022-03-21T12:33:20.690941Z","shell.execute_reply":"2022-03-21T12:33:20.69938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget --no-check-certificate \\\n    https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 \\\n    -O /tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5","metadata":{"id":"9eappJqurdjb","outputId":"c3b016da-7870-4048-ad2a-efe09999c19b","papermill":{"duration":1.235457,"end_time":"2022-03-20T20:00:08.33607","exception":false,"start_time":"2022-03-20T20:00:07.100613","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:20.701575Z","iopub.execute_input":"2022-03-21T12:33:20.702073Z","iopub.status.idle":"2022-03-21T12:33:22.144645Z","shell.execute_reply.started":"2022-03-21T12:33:20.702037Z","shell.execute_reply":"2022-03-21T12:33:22.143794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras import layers\n\nlocal_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\npre_trained_model = InceptionV3(\n    input_shape = (400,400,3),\n    weights = None,\n    include_top = False\n)\n\npre_trained_model.load_weights(local_weights_file)\n\nfor layer in pre_trained_model.layers:\n  layer.trainable = False","metadata":{"id":"53KzTQ7PoooD","papermill":{"duration":4.02007,"end_time":"2022-03-20T20:00:12.375878","exception":false,"start_time":"2022-03-20T20:00:08.355808","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:22.146491Z","iopub.execute_input":"2022-03-21T12:33:22.146771Z","iopub.status.idle":"2022-03-21T12:33:26.198099Z","shell.execute_reply.started":"2022-03-21T12:33:22.146717Z","shell.execute_reply":"2022-03-21T12:33:26.19732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pre_trained_model.summary()","metadata":{"id":"5WJwwGHepRSL","outputId":"6753b919-af40-44cf-99d4-4cd77a7a4a3b","papermill":{"duration":0.026217,"end_time":"2022-03-20T20:00:12.422352","exception":false,"start_time":"2022-03-20T20:00:12.396135","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:26.200618Z","iopub.execute_input":"2022-03-21T12:33:26.200934Z","iopub.status.idle":"2022-03-21T12:33:26.204047Z","shell.execute_reply.started":"2022-03-21T12:33:26.200891Z","shell.execute_reply":"2022-03-21T12:33:26.203396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ',last_layer.output_shape)\nlast_output = last_layer.output","metadata":{"id":"mj6LuuR4rl9m","outputId":"5694f690-20fe-4a01-9d91-b49a78870187","papermill":{"duration":0.028973,"end_time":"2022-03-20T20:00:12.470925","exception":false,"start_time":"2022-03-20T20:00:12.441952","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:26.205271Z","iopub.execute_input":"2022-03-21T12:33:26.205752Z","iopub.status.idle":"2022-03-21T12:33:26.40717Z","shell.execute_reply.started":"2022-03-21T12:33:26.205705Z","shell.execute_reply":"2022-03-21T12:33:26.406287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Model\n\nx = layers.Flatten()(last_output)\n\nx = layers.Dense(512,activation='relu')(x)\nx = layers.Dropout(0.2)(x)\nx = layers.Dense(512,activation='relu')(x)\nx = layers.Dropout(0.2)(x)\n\nx = layers.Dense(1, activation='sigmoid')(x)\n\nmodel = Model(pre_trained_model.input, x)\n\nmodel.summary()","metadata":{"id":"yO1x_Se2sH4U","outputId":"f0d17caa-dc55-4889-8472-da95e2246c41","papermill":{"duration":0.06717,"end_time":"2022-03-20T20:00:12.558678","exception":false,"start_time":"2022-03-20T20:00:12.491508","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:26.408871Z","iopub.execute_input":"2022-03-21T12:33:26.409663Z","iopub.status.idle":"2022-03-21T12:33:26.55805Z","shell.execute_reply.started":"2022-03-21T12:33:26.409618Z","shell.execute_reply":"2022-03-21T12:33:26.557307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss = 'binary_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"id":"A5Gd9EQOs0dd","papermill":{"duration":0.036542,"end_time":"2022-03-20T20:00:12.615204","exception":false,"start_time":"2022-03-20T20:00:12.578662","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:26.559752Z","iopub.execute_input":"2022-03-21T12:33:26.560006Z","iopub.status.idle":"2022-03-21T12:33:26.577576Z","shell.execute_reply.started":"2022-03-21T12:33:26.559972Z","shell.execute_reply":"2022-03-21T12:33:26.576684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install split-folders\n\n!mkdir validation\n\nimport splitfolders\ninput_folder = '../input/dog-vs-cat-classification/train/train'\n\nsplitfolders.ratio(input_folder, output=\"./validation\", \n                   seed=42, ratio=(.9, .1), \n                   group_prefix=None)","metadata":{"id":"NLnpEiNktArK","outputId":"5138e221-b138-4ef6-de0f-439bd0d9cc53","papermill":{"duration":169.265625,"end_time":"2022-03-20T20:03:11.282066","exception":true,"start_time":"2022-03-20T20:00:22.016441","status":"failed"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:33:26.579259Z","iopub.execute_input":"2022-03-21T12:33:26.57954Z","iopub.status.idle":"2022-03-21T12:36:48.888426Z","shell.execute_reply.started":"2022-03-21T12:33:26.579492Z","shell.execute_reply":"2022-03-21T12:36:48.887026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rescale = 1/255,\n    rotation_range = 40,\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    fill_mode = 'nearest'\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    './validation/train',\n    target_size = (400,400),\n    batch_size = 32,\n    class_mode = 'binary'\n)\n\nval_datagen = ImageDataGenerator(\n    rescale = 1/255,\n    rotation_range = 40,\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    fill_mode = 'nearest'\n)\n\nval_generator = train_datagen.flow_from_directory(\n    './validation/val',\n    target_size = (400,400),\n    batch_size = 32,\n    class_mode = 'binary'\n)","metadata":{"id":"JeKc2dlR6kNQ","outputId":"58ff8273-6e40-424a-a8c7-cc7cf351469a","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:37:15.184596Z","iopub.execute_input":"2022-03-21T12:37:15.185154Z","iopub.status.idle":"2022-03-21T12:37:16.418266Z","shell.execute_reply.started":"2022-03-21T12:37:15.185113Z","shell.execute_reply":"2022-03-21T12:37:16.416794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class myCallback(tf.keras.callbacks.Callback):\n  def on_epoch_end(self, epoch, logs={}):\n    if logs.get('accuracy') > 0.995:\n      print('\\n Accuracy has reached upto 99.5% so, cancelling training.')\n      self.model.stop_training = True\n\ncallbacks = myCallback()","metadata":{"id":"-T7vgABZVjwS","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:37:16.41995Z","iopub.execute_input":"2022-03-21T12:37:16.420228Z","iopub.status.idle":"2022-03-21T12:37:16.425657Z","shell.execute_reply.started":"2022-03-21T12:37:16.42018Z","shell.execute_reply":"2022-03-21T12:37:16.424786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    validation_data = val_generator,\n    steps_per_epoch = 704,\n    epochs = 3,\n    validation_steps = 79,\n    verbose = 1,\n    callbacks = [callbacks]\n)","metadata":{"id":"vyKrNOQBxchN","outputId":"cb56d438-99c6-4298-a7cf-0a96ab6aabb9","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:37:17.814329Z","iopub.execute_input":"2022-03-21T12:37:17.814576Z","iopub.status.idle":"2022-03-21T13:21:48.12598Z","shell.execute_reply.started":"2022-03-21T12:37:17.814548Z","shell.execute_reply":"2022-03-21T13:21:48.123615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)\nhistory_df.loc[:,['loss','val_loss']].plot()\nhistory_df.loc[:,['accuracy','val_accuracy']].plot()\nplt.show()","metadata":{"id":"CGi3VVTmxhNs","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.895005Z","iopub.status.idle":"2022-03-21T12:36:48.895577Z","shell.execute_reply.started":"2022-03-21T12:36:48.895342Z","shell.execute_reply":"2022-03-21T12:36:48.895365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\npath_test = '../input/dog-vs-cat-classification/test/test'\nall_test = os.listdir(path_test)\ntest_df = pd.DataFrame({'id': all_test})\n\ntest_gen = ImageDataGenerator(rescale = 1/255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df,\n    path_test,\n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=(400,400),\n    batch_size=32,\n    shuffle=False\n)","metadata":{"id":"nmgHpF6DBXM3","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.896686Z","iopub.status.idle":"2022-03-21T12:36:48.8973Z","shell.execute_reply.started":"2022-03-21T12:36:48.89706Z","shell.execute_reply":"2022-03-21T12:36:48.897084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator)","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.898395Z","iopub.status.idle":"2022-03-21T12:36:48.898958Z","shell.execute_reply.started":"2022-03-21T12:36:48.898723Z","shell.execute_reply":"2022-03-21T12:36:48.898758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = np.where(predict>0.5,1,0)\ntest_df['labels'] = predict","metadata":{"id":"A4E0myhJ8nmb","papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.900061Z","iopub.status.idle":"2022-03-21T12:36:48.900618Z","shell.execute_reply.started":"2022-03-21T12:36:48.900386Z","shell.execute_reply":"2022-03-21T12:36:48.90041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = test_df.sort_values(by='id')\ntest_df.to_csv('submission.csv',index=False)\ntest_df.head()","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.901763Z","iopub.status.idle":"2022-03-21T12:36:48.902315Z","shell.execute_reply.started":"2022-03-21T12:36:48.902084Z","shell.execute_reply":"2022-03-21T12:36:48.902108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'].value_counts()","metadata":{"papermill":{"duration":null,"end_time":null,"exception":null,"start_time":null,"status":"pending"},"tags":[],"execution":{"iopub.status.busy":"2022-03-21T12:36:48.9034Z","iopub.status.idle":"2022-03-21T12:36:48.903977Z","shell.execute_reply.started":"2022-03-21T12:36:48.903748Z","shell.execute_reply":"2022-03-21T12:36:48.903772Z"},"trusted":true},"execution_count":null,"outputs":[]}]}