{"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":"markdown","source":"* In this notebook we are trying to build a model that has the ability to classify cats and dogs. \n* The input data we will use is from a dataset provided by kaggle.\n* This dataset consists of 25,000 images of cats and dogs.\n* The dataset we will use is saved for us in '/kaggle/input/dogs-vs-cats'.\n* The following command is to show us the format of the data.","metadata":{"_uuid":"02157803-9187-45c6-9f68-20e0171e99d2","_cell_guid":"c831ba66-35dd-461a-9dda-1c57cc05c76e","trusted":true}},{"cell_type":"code","source":"import os\nfor file in os.listdir('/kaggle/input/dogs-vs-cats'):\n    print(file)","metadata":{"_uuid":"ea821b8e-9115-4ee0-8d51-14ba9a3d4eaf","_cell_guid":"3037c2a8-bb64-414b-b6e0-490d9b2852f6","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:02.784673Z","iopub.execute_input":"2021-07-24T14:27:02.785055Z","iopub.status.idle":"2021-07-24T14:27:02.797423Z","shell.execute_reply.started":"2021-07-24T14:27:02.784963Z","shell.execute_reply":"2021-07-24T14:27:02.796373Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As shown above, the data is stored in two zip files named test1.zip and train.zip. So in order to deal with the data, we first have to extract these files.**","metadata":{"_uuid":"13fc15b8-6c5f-4639-a6df-ebad1b22e181","_cell_guid":"01da1315-2a2c-4357-9321-c5948e3b704b","execution":{"iopub.status.busy":"2021-07-20T16:53:57.252742Z","iopub.execute_input":"2021-07-20T16:53:57.253165Z","iopub.status.idle":"2021-07-20T16:53:57.262173Z","shell.execute_reply.started":"2021-07-20T16:53:57.253128Z","shell.execute_reply":"2021-07-20T16:53:57.260371Z"},"trusted":true}},{"cell_type":"code","source":"from zipfile import ZipFile as zf\ntrain_zip = zf('/kaggle/input/dogs-vs-cats/train.zip', 'r')\ntrain_zip.extractall()\ntrain_zip.close()\ntest_zip = zf('/kaggle/input/dogs-vs-cats/test1.zip', 'r')\ntest_zip.extractall()\ntest_zip.close()","metadata":{"_uuid":"803dfac7-0bb1-46e4-a967-27c5fe1f6a65","_cell_guid":"24a61677-aa14-4b9b-8236-8dfa2e188b7a","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:02.800389Z","iopub.execute_input":"2021-07-24T14:27:02.800884Z","iopub.status.idle":"2021-07-24T14:27:20.176247Z","shell.execute_reply.started":"2021-07-24T14:27:02.800705Z","shell.execute_reply":"2021-07-24T14:27:20.175353Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have two main directories containing the dataset, a directory for the training set and another one for the test set. Both directories are in the working directory \"./\". Let's see how big is the dataset.","metadata":{"_uuid":"0a23457a-47fc-4506-8b35-ab50bf961246","_cell_guid":"3ca612d7-fd9d-45bd-adec-2be56ee31c2d","trusted":true}},{"cell_type":"code","source":"print('there exist ' + str(len(os.listdir('./train'))) + ' training examples')\nprint('there exist ' + str(len(os.listdir('./test1'))) + ' test examples')\nprint(os.listdir('./train')[0:10]) # print the first ten file names in the training set\nprint(os.listdir('./test1')[0:10]) # print the first ten file names in the training set","metadata":{"_uuid":"2f7cbf7b-1de2-417e-9f5d-284b746aa937","_cell_guid":"65475112-2609-46a8-ac41-2af1b12e451a","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:20.177557Z","iopub.execute_input":"2021-07-24T14:27:20.177889Z","iopub.status.idle":"2021-07-24T14:27:20.224019Z","shell.execute_reply.started":"2021-07-24T14:27:20.177856Z","shell.execute_reply":"2021-07-24T14:27:20.223094Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now that we have 25,000 training examples, we will split them into training set and validation set. We will not deal with the 12500 testing examoles now.","metadata":{"_uuid":"7e637c82-20bd-401b-ab59-e39be367ca60","_cell_guid":"ffb58adb-a1d2-4095-918b-52ac923e9a4e","trusted":true}},{"cell_type":"code","source":"os.mkdir('./train1')\nos.mkdir('./train1/cats')\nos.mkdir('./train1/dogs')","metadata":{"_uuid":"7fe36f7e-746b-4329-a35f-92c87b91cf0d","_cell_guid":"69a1490f-7e71-46dd-bcf9-12d0dffae007","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:20.225767Z","iopub.execute_input":"2021-07-24T14:27:20.226139Z","iopub.status.idle":"2021-07-24T14:27:20.230276Z","shell.execute_reply.started":"2021-07-24T14:27:20.226096Z","shell.execute_reply":"2021-07-24T14:27:20.229455Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\nfor file_name in os.listdir('./train'):\n    if file_name.split('.')[0] == 'cat':\n        shutil.copy(os.path.join('./train/', file_name) , os.path.join('./train1/cats', file_name))\n    elif file_name.split('.')[0] == 'dog':\n        shutil.copy(os.path.join('./train/', file_name) , os.path.join('./train1/dogs', file_name))","metadata":{"_uuid":"f2048147-7535-46d2-8565-5025beab429b","_cell_guid":"66929db7-3907-4553-803f-795c32803bda","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:20.231835Z","iopub.execute_input":"2021-07-24T14:27:20.232397Z","iopub.status.idle":"2021-07-24T14:27:23.087285Z","shell.execute_reply.started":"2021-07-24T14:27:20.232361Z","shell.execute_reply":"2021-07-24T14:27:23.086321Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('./train1/cats')))\nprint(len(os.listdir('./train1/dogs')))","metadata":{"_uuid":"3704662d-8491-41f8-9944-79f0406726a1","_cell_guid":"ca8de899-a4ad-43a2-aca4-971ae23e3851","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:23.088818Z","iopub.execute_input":"2021-07-24T14:27:23.089381Z","iopub.status.idle":"2021-07-24T14:27:23.109912Z","shell.execute_reply.started":"2021-07-24T14:27:23.089339Z","shell.execute_reply":"2021-07-24T14:27:23.108909Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we want to split the data into training and validation","metadata":{"_uuid":"4e319148-137f-4377-8196-49fb75313759","_cell_guid":"d8b6225c-88b2-4ebc-8a23-22d5a2cfcf5e","trusted":true}},{"cell_type":"code","source":"os.mkdir('./training_set')\nos.mkdir('./training_set/cats')\nos.mkdir('./training_set/dogs')\nos.mkdir('./val_set')\nos.mkdir('./val_set/cats')\nos.mkdir('./val_set/dogs')","metadata":{"_uuid":"350097e4-6517-4e15-893c-c47ddf2213e4","_cell_guid":"7cb3e794-37da-41fd-8efc-1222ec1497de","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:23.111163Z","iopub.execute_input":"2021-07-24T14:27:23.111539Z","iopub.status.idle":"2021-07-24T14:27:23.152286Z","shell.execute_reply.started":"2021-07-24T14:27:23.111501Z","shell.execute_reply":"2021-07-24T14:27:23.151479Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\ntraincats = os.listdir('./train1/cats')\nrandom.shuffle(traincats)\ntraindogs = os.listdir('./train1/dogs')\nrandom.shuffle(traindogs)\nfor file_name in traincats[:10000]:\n    shutil.copy(os.path.join('./train1/cats', file_name) , os.path.join('./training_set/cats', file_name))\nfor file_name in traincats[10000:]:\n    shutil.copy(os.path.join('./train1/cats', file_name) , os.path.join('./val_set/cats', file_name))\nfor file_name in traindogs[:10000]:\n    shutil.copy(os.path.join('./train1/dogs', file_name) , os.path.join('./training_set/dogs', file_name))\nfor file_name in traindogs[10000:]:\n    shutil.copy(os.path.join('./train1/dogs', file_name) , os.path.join('./val_set/dogs', file_name))","metadata":{"_uuid":"98603900-1718-4148-9255-248242d6208a","_cell_guid":"172ddc66-5db6-43e3-86f3-47aca364ea0f","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:23.153514Z","iopub.execute_input":"2021-07-24T14:27:23.153858Z","iopub.status.idle":"2021-07-24T14:27:27.234842Z","shell.execute_reply.started":"2021-07-24T14:27:23.153826Z","shell.execute_reply":"2021-07-24T14:27:27.233891Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(os.listdir('./training_set/cats/')))\nprint(len(os.listdir('./training_set/dogs/')))\nprint(len(os.listdir('./val_set/cats/')))\nprint(len(os.listdir('./val_set/dogs/')))","metadata":{"_uuid":"5dd91e02-aacb-4b79-96e5-43d29b132822","_cell_guid":"82a54daa-9b20-44bc-8b2f-33c0620318b1","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:27.237577Z","iopub.execute_input":"2021-07-24T14:27:27.240427Z","iopub.status.idle":"2021-07-24T14:27:27.294827Z","shell.execute_reply.started":"2021-07-24T14:27:27.240387Z","shell.execute_reply":"2021-07-24T14:27:27.294009Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now let's define our model.","metadata":{"_uuid":"b84d999a-c8b8-4a77-af95-5cc2a380ec78","_cell_guid":"07a32ab4-0c3b-4fef-a1e8-4983f2a00719","trusted":true}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras.layers as tfl\n\nmodel = tf.keras.Sequential([\n    tfl.Conv2D(64, (3,3), activation='relu', input_shape=(150, 150, 3)),\n    tfl.BatchNormalization(),\n    tfl.MaxPooling2D(2,2),\n    tfl.Conv2D(128, (3,3), activation='relu'),\n    tfl.BatchNormalization(),\n    tfl.MaxPooling2D(2,2),\n    tfl.Conv2D(256, (3,3), activation='relu'),\n    tfl.BatchNormalization(),\n    tfl.MaxPooling2D(2,2),\n    tfl.Flatten(),\n    tfl.Dense(512, activation='relu'),\n    tfl.Dense(1, activation='sigmoid')\n])","metadata":{"_uuid":"eb0c44e1-edef-4bc1-9eaa-cd1a043a337d","_cell_guid":"22abf461-abc3-40f2-afd4-31de7ad2b0a3","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:27.296156Z","iopub.execute_input":"2021-07-24T14:27:27.296474Z","iopub.status.idle":"2021-07-24T14:27:34.180819Z","shell.execute_reply.started":"2021-07-24T14:27:27.296439Z","shell.execute_reply":"2021-07-24T14:27:34.179987Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"_uuid":"217f002b-0576-4a62-a5d4-4c37de64d900","_cell_guid":"e3f956f6-3e5e-475d-a070-600314d19651","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:34.182228Z","iopub.execute_input":"2021-07-24T14:27:34.182567Z","iopub.status.idle":"2021-07-24T14:27:34.193804Z","shell.execute_reply.started":"2021-07-24T14:27:34.182533Z","shell.execute_reply":"2021-07-24T14:27:34.192785Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 0.001), loss='binary_crossentropy', metrics=['binary_accuracy'])","metadata":{"_uuid":"bab3839f-f5e9-4d39-9e1f-91538c64c212","_cell_guid":"a4beb62f-1115-478b-93d8-dcb87e2be03e","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:34.196326Z","iopub.execute_input":"2021-07-24T14:27:34.196561Z","iopub.status.idle":"2021-07-24T14:27:40.573759Z","shell.execute_reply.started":"2021-07-24T14:27:34.196539Z","shell.execute_reply":"2021-07-24T14:27:40.572940Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now, we are ready to train our Model. Except first we need to introduce our labeled data to our model. For this we will use the ImageDataGenerator.","metadata":{"_uuid":"9a3a0b93-0453-4469-b43d-0a1b1a70c6a6","_cell_guid":"45af6753-f405-4936-9149-966be32f3735","trusted":true}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"5ac49ee8-aeb8-419b-8e2a-4e4ba3824e85","_cell_guid":"e2cf433c-770a-4d51-b3b0-da250b9d0f3d","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:40.574934Z","iopub.execute_input":"2021-07-24T14:27:40.575292Z","iopub.status.idle":"2021-07-24T14:27:42.113342Z","shell.execute_reply.started":"2021-07-24T14:27:40.575259Z","shell.execute_reply":"2021-07-24T14:27:42.112481Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(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                                  )\ntrain_generator = train_datagen.flow_from_directory(\n    './training_set',\n    target_size = (150,150),\n    batch_size = 8,\n    class_mode = 'binary'\n)\nval_datagen = ImageDataGenerator(rescale=1./255)\nval_generator = val_datagen.flow_from_directory(\n    './val_set/',\n    target_size = (150,150),\n    batch_size = 8,\n    class_mode = 'binary'\n)","metadata":{"_uuid":"63882dca-a8a7-4e62-b5e3-3eda5fc37081","_cell_guid":"90ba0dd7-5354-4b38-b66f-41a9fc30151e","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:42.116406Z","iopub.execute_input":"2021-07-24T14:27:42.116765Z","iopub.status.idle":"2021-07-24T14:27:43.187350Z","shell.execute_reply.started":"2021-07-24T14:27:42.116734Z","shell.execute_reply":"2021-07-24T14:27:43.185878Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we are ready to train our Model","metadata":{"_uuid":"3a51336c-3a3b-4a8a-aead-e8e1e171c5fe","_cell_guid":"e5ab0b21-aa0b-4e71-8adf-3e95dac72466","trusted":true}},{"cell_type":"code","source":"history = model.fit_generator(train_generator, epochs=100, validation_data=val_generator)","metadata":{"_uuid":"651e19c4-a2c7-4892-9391-12d459f9ccd5","_cell_guid":"0fd44997-f959-47bb-b91b-8a58c5c156b0","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T14:27:43.188674Z","iopub.execute_input":"2021-07-24T14:27:43.189098Z","iopub.status.idle":"2021-07-24T19:21:42.256430Z","shell.execute_reply.started":"2021-07-24T14:27:43.189057Z","shell.execute_reply":"2021-07-24T19:21:42.255622Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nacc = history.history['binary_accuracy']\nval_acc = history.history['val_binary_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"_uuid":"3ac41dcf-4c1d-46c8-b855-f5749345fe0d","_cell_guid":"b573dbd9-ebb1-4d1c-8f18-aaaea152f09b","collapsed":false,"execution":{"iopub.status.busy":"2021-07-24T19:21:42.259460Z","iopub.execute_input":"2021-07-24T19:21:42.259756Z","iopub.status.idle":"2021-07-24T19:21:42.594870Z","shell.execute_reply.started":"2021-07-24T19:21:42.259724Z","shell.execute_reply":"2021-07-24T19:21:42.594058Z"},"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"38069796-d884-4b6a-883a-04d8f676f6f0","_cell_guid":"676aaef5-b8c4-4338-bff3-78f30f44fb94","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"091bd9da-8646-4f35-8af9-7a38dd0e5d8f","_cell_guid":"7c94d1ec-0eb1-4aca-a7a3-21464fff12c7","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}