{"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 pandas as pd\nimport tensorflow as tf\nimport os\n\nfrom tensorflow.keras.utils import load_img, img_to_array","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-03T14:50:38.633176Z","iopub.execute_input":"2022-05-03T14:50:38.633806Z","iopub.status.idle":"2022-05-03T14:50:43.789383Z","shell.execute_reply.started":"2022-05-03T14:50:38.633711Z","shell.execute_reply":"2022-05-03T14:50:43.788647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\nextracted = './training'\nwith zipfile.ZipFile('../input/dogs-vs-cats/train.zip', 'r') as zip_ref:\n    zip_ref.extractall(extracted)","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:50:43.790986Z","iopub.execute_input":"2022-05-03T14:50:43.791249Z","iopub.status.idle":"2022-05-03T14:50:55.218524Z","shell.execute_reply.started":"2022-05-03T14:50:43.791215Z","shell.execute_reply":"2022-05-03T14:50:55.217802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extracted = './test'\nwith zipfile.ZipFile('../input/dogs-vs-cats/test1.zip', 'r') as zip_ref:\n    zip_ref.extractall(extracted)","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:50:55.221648Z","iopub.execute_input":"2022-05-03T14:50:55.221880Z","iopub.status.idle":"2022-05-03T14:51:00.292623Z","shell.execute_reply.started":"2022-05-03T14:50:55.221855Z","shell.execute_reply":"2022-05-03T14:51:00.291888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = './test/test1'\nTRAIN_PATH = './training/train'\nIMAGE_SIZE = 128","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.294597Z","iopub.execute_input":"2022-05-03T14:51:00.294887Z","iopub.status.idle":"2022-05-03T14:51:00.300196Z","shell.execute_reply.started":"2022-05-03T14:51:00.294850Z","shell.execute_reply":"2022-05-03T14:51:00.299157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.301631Z","iopub.execute_input":"2022-05-03T14:51:00.301876Z","iopub.status.idle":"2022-05-03T14:51:00.316364Z","shell.execute_reply.started":"2022-05-03T14:51:00.301845Z","shell.execute_reply":"2022-05-03T14:51:00.315696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_paths = [os.path.join(TRAIN_PATH, file) for file in os.listdir(TRAIN_PATH) if 'cat' in file]\ndog_paths = [os.path.join(TRAIN_PATH, file) for file in os.listdir(TRAIN_PATH) if 'dog' in file]","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.317668Z","iopub.execute_input":"2022-05-03T14:51:00.317986Z","iopub.status.idle":"2022-05-03T14:51:00.390994Z","shell.execute_reply.started":"2022-05-03T14:51:00.317951Z","shell.execute_reply":"2022-05-03T14:51:00.390394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generator(filenames, labels):\n    def callable_generator():\n        for filename, lab in zip(filenames, labels):\n            img = img_to_array(load_img(filename, target_size=(IMAGE_SIZE, IMAGE_SIZE)))/255.\n            yield img, lab         \n    return callable_generator","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.392163Z","iopub.execute_input":"2022-05-03T14:51:00.392396Z","iopub.status.idle":"2022-05-03T14:51:00.397068Z","shell.execute_reply.started":"2022-05-03T14:51:00.392364Z","shell.execute_reply":"2022-05-03T14:51:00.396213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = len(cat_paths)*[0] + len(dog_paths)*[1]\ndata = cat_paths+dog_paths","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.398456Z","iopub.execute_input":"2022-05-03T14:51:00.399046Z","iopub.status.idle":"2022-05-03T14:51:00.406951Z","shell.execute_reply.started":"2022-05-03T14:51:00.399009Z","shell.execute_reply":"2022-05-03T14:51:00.406122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_val, y_train, y_val = train_test_split(data, labels, shuffle=True, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:51:00.408061Z","iopub.execute_input":"2022-05-03T14:51:00.408261Z","iopub.status.idle":"2022-05-03T14:51:01.015555Z","shell.execute_reply.started":"2022-05-03T14:51:00.408238Z","shell.execute_reply":"2022-05-03T14:51:01.014790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.from_generator(generator(X_train, y_train),\n                                              (tf.float32, tf.float32),\n                                              ((IMAGE_SIZE, IMAGE_SIZE, 3), ()))\n\nval_dataset = tf.data.Dataset.from_generator(generator(X_val, y_val),\n                                              (tf.float32, tf.float32),\n                                              ((IMAGE_SIZE, IMAGE_SIZE, 3), ()))\n\nBATCH_SIZE = 4\ndef prep_data(train_dataset):\n    train_dataset = train_dataset.cache().batch(BATCH_SIZE).repeat()\n    train_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n    return train_dataset\n\ntrain_dataset = prep_data(train_dataset)\n\nval_dataset = prep_data(val_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-05-03T14:57:59.080235Z","iopub.execute_input":"2022-05-03T14:57:59.080805Z","iopub.status.idle":"2022-05-03T14:57:59.127067Z","shell.execute_reply.started":"2022-05-03T14:57:59.080764Z","shell.execute_reply":"2022-05-03T14:57:59.126279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential()\nmodel.add(tf.keras.layers.Input((IMAGE_SIZE, IMAGE_SIZE, 3)))\nmodel.add(tf.keras.layers.Conv2D(16, 3, activation='relu'))\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Conv2D(32, 3, activation='relu'))\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Conv2D(32, 3, activation='relu'))\nmodel.add(tf.keras.layers.MaxPool2D(2))\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(128, activation='relu'))\nmodel.add(tf.keras.layers.Dense(32, activation='relu'))\nmodel.add(tf.keras.layers.Dense(1, activation='sigmoid'))\nmodel.summary()\n\nmodel.compile(loss='binary_crossentropy', metrics=['acc'], optimizer='adam')","metadata":{"execution":{"iopub.status.busy":"2022-05-03T15:03:08.835822Z","iopub.execute_input":"2022-05-03T15:03:08.836194Z","iopub.status.idle":"2022-05-03T15:03:08.957137Z","shell.execute_reply.started":"2022-05-03T15:03:08.836151Z","shell.execute_reply":"2022-05-03T15:03:08.956211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, validation_data=val_dataset, validation_steps=len(X_val)//(BATCH_SIZE),\n                    epochs=10, steps_per_epoch=len(X_train)//(BATCH_SIZE))","metadata":{"execution":{"iopub.status.busy":"2022-05-03T15:03:08.961263Z","iopub.execute_input":"2022-05-03T15:03:08.961622Z","iopub.status.idle":"2022-05-03T15:06:41.807772Z","shell.execute_reply.started":"2022-05-03T15:03:08.961584Z","shell.execute_reply":"2022-05-03T15:06:41.807059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\n# list all data in history\nprint(history.history.keys())\n# summarize history for accuracy\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'test'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.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":{"iopub.status.busy":"2022-05-03T15:08:56.153563Z","iopub.execute_input":"2022-05-03T15:08:56.154259Z","iopub.status.idle":"2022-05-03T15:08:56.546138Z","shell.execute_reply.started":"2022-05-03T15:08:56.154221Z","shell.execute_reply":"2022-05-03T15:08:56.545484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}