{"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 sys\nimport tensorflow as tf\nimport os\nimport sys\nimport pandas as pd\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import RMSprop\n\n%matplotlib inline\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:34:18.031067Z","iopub.execute_input":"2022-06-02T09:34:18.031447Z","iopub.status.idle":"2022-06-02T09:34:19.015560Z","shell.execute_reply.started":"2022-06-02T09:34:18.031418Z","shell.execute_reply":"2022-06-02T09:34:19.014154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seed = 0\nnp.random.seed(seed)\ntf.random.set_seed(3)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:34:21.025567Z","iopub.execute_input":"2022-06-02T09:34:21.026713Z","iopub.status.idle":"2022-06-02T09:34:21.032965Z","shell.execute_reply.started":"2022-06-02T09:34:21.026671Z","shell.execute_reply":"2022-06-02T09:34:21.031608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nzip_files = ['test1', 'train']\n\nfor zip_file in zip_files:\n    with zipfile.ZipFile(\"../input/dogs-vs-cats/{}.zip\".format(zip_file),\"r\") as z:\n        z.extractall(\".\")\n        print(\"{} unzipped\".format(zip_file))","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:34:29.844700Z","iopub.execute_input":"2022-06-02T09:34:29.845358Z","iopub.status.idle":"2022-06-02T09:34:48.503748Z","shell.execute_reply.started":"2022-06-02T09:34:29.845320Z","shell.execute_reply":"2022-06-02T09:34:48.502339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''test1, train Data is in current working folder'''\nprint(os.listdir('../working'))","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:35:18.333660Z","iopub.execute_input":"2022-06-02T09:35:18.334616Z","iopub.status.idle":"2022-06-02T09:35:18.342836Z","shell.execute_reply.started":"2022-06-02T09:35:18.334551Z","shell.execute_reply":"2022-06-02T09:35:18.341728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FOLDER_PATH = \"../working/train\"\nFILE_NAMES = os.listdir(IMAGE_FOLDER_PATH)\nWIDTH = 150\nHEIGHT = 150","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:35:42.909247Z","iopub.execute_input":"2022-06-02T09:35:42.909968Z","iopub.status.idle":"2022-06-02T09:35:42.937176Z","shell.execute_reply.started":"2022-06-02T09:35:42.909921Z","shell.execute_reply":"2022-06-02T09:35:42.935830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILE_NAMES[0:5]","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:35:53.205782Z","iopub.execute_input":"2022-06-02T09:35:53.206170Z","iopub.status.idle":"2022-06-02T09:35:53.215548Z","shell.execute_reply.started":"2022-06-02T09:35:53.206140Z","shell.execute_reply":"2022-06-02T09:35:53.214354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = list()\nfull_paths = list()\ntrain_cats_dir = list()\ntrain_dogs_dir = list()\n\n\nfor file_name in FILE_NAMES:\n    target = file_name.split(\".\")[0] \n    full_path = os.path.join(IMAGE_FOLDER_PATH, file_name)\n    \n    if(target == \"dog\"):\n        train_dogs_dir.append(full_path)\n    if(target == \"cat\"):\n        train_cats_dir.append(full_path)\n    \n    full_paths.append(full_path)\n    targets.append(target)\n\ndataset = pd.DataFrame()\ndataset['image_path'] = full_paths \ndataset['target'] = targets ","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:36:14.701722Z","iopub.execute_input":"2022-06-02T09:36:14.702245Z","iopub.status.idle":"2022-06-02T09:36:14.788863Z","shell.execute_reply.started":"2022-06-02T09:36:14.702209Z","shell.execute_reply":"2022-06-02T09:36:14.788153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"total data counts:\", dataset['target'].count())\ncounts = dataset['target'].value_counts()\nprint(counts)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:36:40.394292Z","iopub.execute_input":"2022-06-02T09:36:40.394819Z","iopub.status.idle":"2022-06-02T09:36:40.412534Z","shell.execute_reply.started":"2022-06-02T09:36:40.394783Z","shell.execute_reply":"2022-06-02T09:36:40.411868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_train, dataset_test = train_test_split(dataset, test_size=0.2, random_state=seed)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:38:56.520551Z","iopub.execute_input":"2022-06-02T09:38:56.521009Z","iopub.status.idle":"2022-06-02T09:38:56.532415Z","shell.execute_reply.started":"2022-06-02T09:38:56.520977Z","shell.execute_reply":"2022-06-02T09:38:56.531373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\nrotation_range=15,\nrescale=1./255,\nshear_range=0.1,\nzoom_range=0.2,\nhorizontal_flip=True,\nwidth_shift_range=0.1,\nheight_shift_range=0.1)\n\ntrain_datagenerator=train_datagen.flow_from_dataframe(dataframe=dataset_train,\n                                                     x_col=\"image_path\",\n                                                     y_col=\"target\",\n                                                     target_size=(WIDTH, HEIGHT),\n                                                     class_mode=\"binary\",\n                                                     batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:40:04.134751Z","iopub.execute_input":"2022-06-02T09:40:04.135581Z","iopub.status.idle":"2022-06-02T09:40:04.347033Z","shell.execute_reply.started":"2022-06-02T09:40:04.135547Z","shell.execute_reply":"2022-06-02T09:40:04.346156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagenerator=test_datagen.flow_from_dataframe(dataframe=dataset_test,\n                                                   x_col=\"image_path\",\n                                                   y_col=\"target\",\n                                                   target_size=(WIDTH, HEIGHT),\n                                                   class_mode=\"binary\",\n                                                   batch_size=150)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:40:20.796824Z","iopub.execute_input":"2022-06-02T09:40:20.797251Z","iopub.status.idle":"2022-06-02T09:40:20.858592Z","shell.execute_reply.started":"2022-06-02T09:40:20.797215Z","shell.execute_reply":"2022-06-02T09:40:20.857930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n\n  model = tf.keras.models.Sequential([ \n    tf.keras.layers.Conv2D(16,(3,3), activation = 'relu', input_shape=(150,150,3)),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(32,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Conv2D(64,(3,3), activation = 'relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation = 'relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n  ])\n\n  \n  model.compile(optimizer=RMSprop(learning_rate=0.001),\n                loss='binary_crossentropy',\n                metrics=['accuracy']) \n    \n  return model","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:41:41.402287Z","iopub.execute_input":"2022-06-02T09:41:41.402702Z","iopub.status.idle":"2022-06-02T09:41:41.410435Z","shell.execute_reply.started":"2022-06-02T09:41:41.402669Z","shell.execute_reply":"2022-06-02T09:41:41.409224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:41:46.212769Z","iopub.execute_input":"2022-06-02T09:41:46.213584Z","iopub.status.idle":"2022-06-02T09:41:46.330815Z","shell.execute_reply.started":"2022-06-02T09:41:46.213547Z","shell.execute_reply":"2022-06-02T09:41:46.329725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=\"binary_crossentropy\", optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:42:04.734607Z","iopub.execute_input":"2022-06-02T09:42:04.735013Z","iopub.status.idle":"2022-06-02T09:42:04.747151Z","shell.execute_reply.started":"2022-06-02T09:42:04.734980Z","shell.execute_reply":"2022-06-02T09:42:04.746294Z"},"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('val_accuracy')>=0.90):\n            self.model.stop_training = True\n            \ncallback = mycallback()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:42:30.149761Z","iopub.execute_input":"2022-06-02T09:42:30.150693Z","iopub.status.idle":"2022-06-02T09:42:30.156434Z","shell.execute_reply.started":"2022-06-02T09:42:30.150623Z","shell.execute_reply":"2022-06-02T09:42:30.155708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelHistory=model.fit(train_datagenerator,\n                       epochs=50,\n                       validation_data=test_datagenerator,\n                       validation_steps=dataset_test.shape[0]/150,\n                       steps_per_epoch=dataset_train.shape[0]/150,\n                       callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2022-06-02T09:42:47.099772Z","iopub.execute_input":"2022-06-02T09:42:47.100629Z","iopub.status.idle":"2022-06-02T11:55:59.748070Z","shell.execute_reply.started":"2022-06-02T09:42:47.100589Z","shell.execute_reply":"2022-06-02T11:55:59.746530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = modelHistory.history['accuracy']\nval_acc = modelHistory.history['val_accuracy']\nloss = modelHistory.history['loss']\nval_loss = modelHistory.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\n\nplt.figure()\n\nplt.plot(epochs, loss, 'go', label='Training Loss')\nplt.plot(epochs, val_loss, 'g', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-06-02T12:27:17.669127Z","iopub.execute_input":"2022-06-02T12:27:17.670066Z","iopub.status.idle":"2022-06-02T12:27:17.998799Z","shell.execute_reply.started":"2022-06-02T12:27:17.670008Z","shell.execute_reply":"2022-06-02T12:27:17.997730Z"},"trusted":true},"execution_count":null,"outputs":[]}]}