{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ngpus = tf.config.list_physical_devices('GPU')\nif gpus:\n    for gpu in gpus:\n        tf.config.experimental.set_memory_growth(gpu, True)\nlogical_gpus = tf.config.list_logical_devices('GPU')\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-19T19:43:02.633134Z","iopub.execute_input":"2023-06-19T19:43:02.633507Z","iopub.status.idle":"2023-06-19T19:43:02.65603Z","shell.execute_reply.started":"2023-06-19T19:43:02.633477Z","shell.execute_reply":"2023-06-19T19:43:02.654947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Unpack the existing archives","metadata":{}},{"cell_type":"code","source":"!unzip -qq /kaggle/input/dogs-vs-cats/train.zip\n!unzip -qq /kaggle/input/dogs-vs-cats/test1.zip","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:00:13.829106Z","iopub.execute_input":"2023-06-19T19:00:13.829788Z","iopub.status.idle":"2023-06-19T19:00:37.451905Z","shell.execute_reply.started":"2023-06-19T19:00:13.829756Z","shell.execute_reply":"2023-06-19T19:00:37.450546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's create separate subdirectories, we will need this in the future","metadata":{}},{"cell_type":"code","source":"os.mkdir(\"train/cats\")\nos.mkdir(\"train/dogs\")\nos.mkdir(\"test1/test\")","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:00:37.454405Z","iopub.execute_input":"2023-06-19T19:00:37.455152Z","iopub.status.idle":"2023-06-19T19:00:37.462073Z","shell.execute_reply.started":"2023-06-19T19:00:37.455101Z","shell.execute_reply":"2023-06-19T19:00:37.461084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Move the images to the appropriate subdirectories","metadata":{}},{"cell_type":"code","source":"! mv train/dog*.jpg train/dogs\n! mv train/cat*.jpg train/cats\n! mv test1/*.jpg test1/test","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:00:37.463652Z","iopub.execute_input":"2023-06-19T19:00:37.464368Z","iopub.status.idle":"2023-06-19T19:00:41.674281Z","shell.execute_reply.started":"2023-06-19T19:00:37.464336Z","shell.execute_reply":"2023-06-19T19:00:41.67291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Let's create data generators","metadata":{}},{"cell_type":"code","source":"train_dir = \"train/\"\ntest_dir = \"test1/\"\n\n# the generator for training images includes augmentation so that the input images are never repeated, which contributes to the stability of the model\ntrain_datagen = ImageDataGenerator(rescale = 1./255, \n                                  rotation_range = 20,\n                                  width_shift_range = 0.1,\n                                  height_shift_range = 0.1,\n                                  shear_range = 0.1,\n                                  zoom_range = 0.1,\n                                  horizontal_flip = True,\n                                  fill_mode = \"nearest\")\n\n# The generator for test data does not include augmentation, only normalization is done\ntest_datagen = ImageDataGenerator(rescale = 1./255)\n\n# We indicate the sources of information to the generators\ntrain_generator = train_datagen.flow_from_directory(\n                                                    train_dir,\n                                                    target_size = (150,150),\n                                                    batch_size = 256,\n                                                    class_mode = \"binary\")\n\ntest_generator = test_datagen.flow_from_directory(\n                                                    test_dir,\n                                                    target_size = (150,150),\n                                                    batch_size = 128,\n                                                    class_mode = None,\n                                                    shuffle = False)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:00:41.677788Z","iopub.execute_input":"2023-06-19T19:00:41.678196Z","iopub.status.idle":"2023-06-19T19:00:43.466279Z","shell.execute_reply.started":"2023-06-19T19:00:41.678157Z","shell.execute_reply":"2023-06-19T19:00:43.46365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### let's build a model","metadata":{}},{"cell_type":"code","source":"#  We implement a simple convolutional model\nmodel = Sequential([Conv2D(32, 3, activation = \"relu\", input_shape = (150,150, 3)),\n                    Conv2D(32, 3, activation = \"relu\"),\n                    MaxPooling2D(2),\n                    Conv2D(64, 3, activation = \"relu\"),\n                    Conv2D(64, 3, activation = \"relu\"),\n                    MaxPooling2D(2),\n                    Conv2D(128, 3, activation = \"relu\"),\n                    Conv2D(128, 3, activation = \"relu\"),\n                    MaxPooling2D(2),\n                    Conv2D(256, 3, activation = \"relu\"),\n                    Conv2D(256, 3, activation = \"relu\"),\n                    MaxPooling2D(2),\n                    Flatten(),\n                    Dense(512, activation = \"relu\"),\n                    Dropout(0.3),\n                    Dense(1, activation = \"sigmoid\")])\nmodel.compile(loss = \"binary_crossentropy\",\n              optimizer = \"adam\",\n              metrics = [\"accuracy\"])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:03:05.219652Z","iopub.execute_input":"2023-06-19T19:03:05.220042Z","iopub.status.idle":"2023-06-19T19:03:05.405105Z","shell.execute_reply.started":"2023-06-19T19:03:05.220004Z","shell.execute_reply":"2023-06-19T19:03:05.404301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Starting the learning process\nwith tf.device('/GPU:0'):\n    model.fit(train_generator, steps_per_epoch = 88, epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:03:06.161859Z","iopub.execute_input":"2023-06-19T19:03:06.162314Z","iopub.status.idle":"2023-06-19T19:34:05.697426Z","shell.execute_reply.started":"2023-06-19T19:03:06.162277Z","shell.execute_reply":"2023-06-19T19:34:05.696298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Saving the model for future use\nmodel.save(\"model_cat_vs_dogs.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:40:11.144803Z","iopub.execute_input":"2023-06-19T19:40:11.145226Z","iopub.status.idle":"2023-06-19T19:40:11.326494Z","shell.execute_reply.started":"2023-06-19T19:40:11.145192Z","shell.execute_reply":"2023-06-19T19:40:11.32539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_list = model.predict(test_generator)\npred_list = np.array(pred_list)\npred_list = np.round(pred_list.flatten()).astype(int)\n\nid_list = [ ]\nfor f in os.listdir(test_dir + \"/test\"):\n        _id = int(f.split('.')[0])\n        id_list.append(_id)\n# Creating a DataFrame with the IDs and predictions\nres = pd.DataFrame({\n    'id': id_list,\n    'label': pred_list.flatten()\n})\n\n# Sorting the DataFrame by ID and resetting the index\nres.sort_values(by='id', inplace=True)\nres.reset_index(drop=True, inplace=True)\n\n# Saving the DataFrame to a CSV file\nres.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:40:11.529735Z","iopub.execute_input":"2023-06-19T19:40:11.530107Z","iopub.status.idle":"2023-06-19T19:40:42.292615Z","shell.execute_reply.started":"2023-06-19T19:40:11.530074Z","shell.execute_reply":"2023-06-19T19:40:42.291621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## An example of how our model works","metadata":{}},{"cell_type":"code","source":"test_examples = next(test_generator)\nfor img in test_examples:\n    predict = model.predict(np.expand_dims(img, axis = 0), verbose = 0)[0][0]\n    if predict >= 0.5:\n        print(\"It seems like it's a dog! Estimation :\", np.round(predict,2), \"%\")\n    else:\n        print(\"It seems like it's a cat! Estimation :\", np.round(1 - predict,2), \"%\")\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-19T19:43:06.159772Z","iopub.execute_input":"2023-06-19T19:43:06.160359Z","iopub.status.idle":"2023-06-19T19:44:36.788621Z","shell.execute_reply.started":"2023-06-19T19:43:06.160315Z","shell.execute_reply":"2023-06-19T19:44:36.78554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}