{"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":"<center><span style=\"font-family:Comic Sans MS; color:navy \"><h1> Cats🙀 Vs Dogs🐶  Classification(Keras API) </h1></span></center>","metadata":{}},{"cell_type":"markdown","source":"# <b>1 <span style='color:#94D4F6'>|</span> Import Libraries</b>","metadata":{}},{"cell_type":"code","source":"import warnings # ignore warnings\nwarnings.filterwarnings('ignore')\nimport numpy as np \nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom matplotlib.image import imread\nimport os\nimport zipfile\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Sequential \nfrom tensorflow.keras.layers import Dense, Activation , Flatten, BatchNormalization, Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nimport tqdm\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-11T11:07:45.541691Z","iopub.execute_input":"2023-08-11T11:07:45.542077Z","iopub.status.idle":"2023-08-11T11:07:54.992082Z","shell.execute_reply.started":"2023-08-11T11:07:45.542043Z","shell.execute_reply":"2023-08-11T11:07:54.991125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"physical_device = tf.config.experimental.list_physical_devices('GPU')\nprint(\"Num GPU Avilibale: \", len(physical_device))\ntf.config.experimental.set_memory_growth(physical_device[0],True) ","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:07:54.994235Z","iopub.execute_input":"2023-08-11T11:07:54.995015Z","iopub.status.idle":"2023-08-11T11:07:55.209575Z","shell.execute_reply.started":"2023-08-11T11:07:54.994967Z","shell.execute_reply":"2023-08-11T11:07:55.207537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <b>2 <span style='color:#94D4F6'>|</span> Generate the datasets</b>","metadata":{}},{"cell_type":"code","source":"zip_ref = zipfile.ZipFile('../input/dogs-vs-cats/train.zip','r')\nzip_ref.extractall(\"../tmp/dogs-vs-cats\")\nzip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:07:55.211290Z","iopub.execute_input":"2023-08-11T11:07:55.211862Z","iopub.status.idle":"2023-08-11T11:08:05.944863Z","shell.execute_reply.started":"2023-08-11T11:07:55.211826Z","shell.execute_reply":"2023-08-11T11:08:05.943828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base = '../tmp/dogs-vs-cats'\ntrain = os.path.join(base,'train')","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:05.947737Z","iopub.execute_input":"2023-08-11T11:08:05.948119Z","iopub.status.idle":"2023-08-11T11:08:05.956512Z","shell.execute_reply.started":"2023-08-11T11:08:05.948086Z","shell.execute_reply":"2023-08-11T11:08:05.954946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>2.1 |</span></b> Visualize some images</b></p></div>","metadata":{}},{"cell_type":"code","source":"folder = train + '/' # Define the location\nfor i in range(6):\n    plt.subplot(330 + 1 + i) # Define the subplot\n    filename = folder + 'cat.' + str(i) + '.jpg' # get the filename\n    image = imread(filename) # image pixels\n    plt.imshow(image) # plot the row pixel data\nfolder = train + '/' # Define the location","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:05.957905Z","iopub.execute_input":"2023-08-11T11:08:05.958283Z","iopub.status.idle":"2023-08-11T11:08:07.663760Z","shell.execute_reply.started":"2023-08-11T11:08:05.958248Z","shell.execute_reply":"2023-08-11T11:08:07.662864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(6):\n    plt.subplot(330 + 1 + i) # Define the subplot\n    filename = folder + 'dog.' + str(i) + '.jpg' # get the filename\n    image = imread(filename) # image pixels\n    plt.imshow(image) # plot the row pixel data","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:07.664789Z","iopub.execute_input":"2023-08-11T11:08:07.665135Z","iopub.status.idle":"2023-08-11T11:08:08.717618Z","shell.execute_reply.started":"2023-08-11T11:08:07.665105Z","shell.execute_reply":"2023-08-11T11:08:08.716600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>2.2 |</span></b> Total image</b></p></div>","metadata":{}},{"cell_type":"code","source":"all_train = os.listdir(train + '/')\nprint('Num of images:', len(all_train))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:08.718986Z","iopub.execute_input":"2023-08-11T11:08:08.720115Z","iopub.status.idle":"2023-08-11T11:08:08.754861Z","shell.execute_reply.started":"2023-08-11T11:08:08.720081Z","shell.execute_reply":"2023-08-11T11:08:08.753950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>2.3 |</span></b> Split training set</b></p></div>","metadata":{}},{"cell_type":"code","source":"dog_file = [i for i in all_train if i.startswith('dog')]\ncat_file = [i for i in all_train if i.startswith('cat')]\ndata = train_test_split(dog_file,cat_file,test_size = 0.1,shuffle = True,random_state = 42)\ntrain_dog,valid_dog,train_cat,valid_cat = [x for x in data]\n\ntrain_dog_total,valid_dog_total,train_cat_total,valid_cat_total = [len(x) for x in data] \nprint(\"All train data: \" + str(train_dog_total + train_cat_total))\nprint(\"All valid data: \" + str(valid_dog_total + valid_cat_total))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:08.756208Z","iopub.execute_input":"2023-08-11T11:08:08.756631Z","iopub.status.idle":"2023-08-11T11:08:08.780493Z","shell.execute_reply.started":"2023-08-11T11:08:08.756597Z","shell.execute_reply":"2023-08-11T11:08:08.779577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>2.4 |</span></b> Manage Dataset</b></p></div>","metadata":{}},{"cell_type":"code","source":"train_dog_dir = train + '/dog'\ntrain_cat_dir = train + '/cat'\nvalid = base + '/valid'\nvalid_dog_dir = valid + '/dog'\nvalid_cat_dir = valid + '/cat'\n\nmake_dirs = [train_dog_dir, valid_dog_dir, train_cat_dir, valid_cat_dir]\nfor dir, x in zip(make_dirs, data):\n    os.makedirs(dir, exist_ok=True)\n    for i in tqdm.tqdm(x):\n        shutil.move(os.path.join(train, i), dir)\n    print('elements in {}: {}'.format(dir, len(os.listdir(dir))))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:08.781998Z","iopub.execute_input":"2023-08-11T11:08:08.782330Z","iopub.status.idle":"2023-08-11T11:08:10.299239Z","shell.execute_reply.started":"2023-08-11T11:08:08.782299Z","shell.execute_reply":"2023-08-11T11:08:10.298284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style='font-family:comic Sans MS; color:navy'>If we check the Training directory: </span>","metadata":{}},{"cell_type":"code","source":"!ls ../tmp/dogs-vs-cats/train","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:10.303019Z","iopub.execute_input":"2023-08-11T11:08:10.303905Z","iopub.status.idle":"2023-08-11T11:08:11.267044Z","shell.execute_reply.started":"2023-08-11T11:08:10.303870Z","shell.execute_reply":"2023-08-11T11:08:11.265770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <b>3 <span style='color:#94D4F6'>|</span> Preprocessing </b>","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>3.1 |</span></b> Rescaled images</b></p></div>","metadata":{}},{"cell_type":"code","source":"target_size = (150,150)\nbatch_size = 20\ntrain_gen = ImageDataGenerator(rescale=1./255)\nvalid_gen = ImageDataGenerator(rescale=1./255)\n\ntrain = train_gen.flow_from_directory(train,target_size=target_size,batch_size=batch_size,class_mode = \"binary\")\nvalid = valid_gen.flow_from_directory(valid,target_size=target_size,batch_size=batch_size,class_mode = \"binary\")","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:11.269413Z","iopub.execute_input":"2023-08-11T11:08:11.269823Z","iopub.status.idle":"2023-08-11T11:08:12.457840Z","shell.execute_reply.started":"2023-08-11T11:08:11.269786Z","shell.execute_reply":"2023-08-11T11:08:12.456880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <b>4 <span style='color:#94D4F6'>|</span> Build a model </b>","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32,(3,3),activation = 'relu',input_shape =(150,150,3)),\n    MaxPooling2D(2,2),\n    \n    Conv2D(64,(3,3),activation = 'relu'),\n    MaxPooling2D(2,2),\n    \n    Conv2D(128,(3,3),activation = 'relu'),\n    MaxPooling2D(2,2),\n    \n    Conv2D(128,(3,3),activation = 'relu'),\n    MaxPooling2D(2,2),\n    \n    Flatten(),\n    Dense(512,activation = 'relu'),\n    Dense(1,activation = 'sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:12.459294Z","iopub.execute_input":"2023-08-11T11:08:12.460952Z","iopub.status.idle":"2023-08-11T11:08:15.017156Z","shell.execute_reply.started":"2023-08-11T11:08:12.460902Z","shell.execute_reply":"2023-08-11T11:08:15.014989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:15.018680Z","iopub.execute_input":"2023-08-11T11:08:15.019051Z","iopub.status.idle":"2023-08-11T11:08:15.049331Z","shell.execute_reply.started":"2023-08-11T11:08:15.019015Z","shell.execute_reply":"2023-08-11T11:08:15.048580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>4.1 |</span></b> Compile Model</b></p></div>","metadata":{}},{"cell_type":"code","source":"model.compile(loss = 'binary_crossentropy',optimizer=RMSprop(lr=1e-4),metrics=['accuracy'] )","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:15.050330Z","iopub.execute_input":"2023-08-11T11:08:15.050644Z","iopub.status.idle":"2023-08-11T11:08:15.077035Z","shell.execute_reply.started":"2023-08-11T11:08:15.050614Z","shell.execute_reply":"2023-08-11T11:08:15.076124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>4.2 |</span></b> Create a Checkpoint</b></p></div>","metadata":{}},{"cell_type":"code","source":"checkpointer = [tf.keras.callbacks.EarlyStopping(monitor = 'val_accuracy', verbose = 1, restore_best_weights=True, mode=\"max\",patience = 10),\n                tf.keras.callbacks.ModelCheckpoint(\n                    filepath='/kaggle/working/model-{epoch}.hdf5',\n                    monitor=\"val_accuracy\",\n                    verbose=1,\n                    mode=\"max\")]","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:15.078385Z","iopub.execute_input":"2023-08-11T11:08:15.078958Z","iopub.status.idle":"2023-08-11T11:08:15.084473Z","shell.execute_reply.started":"2023-08-11T11:08:15.078911Z","shell.execute_reply":"2023-08-11T11:08:15.083534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>4.3 |</span></b> Fit Data</b></p></div>","metadata":{}},{"cell_type":"code","source":"Model = model.fit(train,steps_per_epoch=100, epochs=50, validation_data=valid, validation_steps=50,\n                    verbose=1, callbacks=[checkpointer])","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:08:15.085782Z","iopub.execute_input":"2023-08-11T11:08:15.086402Z","iopub.status.idle":"2023-08-11T11:12:21.695981Z","shell.execute_reply.started":"2023-08-11T11:08:15.086370Z","shell.execute_reply":"2023-08-11T11:12:21.695107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;display:fill;border-radius:8px;background-color:#03112A;font-size:150%;letter-spacing:1.0px\"><p style=\"padding: 10px;color:white;\"><b><b><span style='color:#94D4F6'>4.4 |</span></b> Training and Validation Accuracy </b></p></div>","metadata":{}},{"cell_type":"code","source":"accuracy = Model.history['accuracy']\nval_acc = Model.history['val_accuracy']\nloss = Model.history['loss']\nval_loss = Model.history['val_loss']\nepochs = range(len(accuracy))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:12:21.697625Z","iopub.execute_input":"2023-08-11T11:12:21.698000Z","iopub.status.idle":"2023-08-11T11:12:21.704189Z","shell.execute_reply.started":"2023-08-11T11:12:21.697963Z","shell.execute_reply":"2023-08-11T11:12:21.703236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 2, figsize=(15, 6))\n# Plot Training accuracy on the first subplot\naxes[0].plot(epochs, accuracy, 'bo', label='Training accuracy')\naxes[0].plot(epochs, val_acc, 'r', label='Validation accuracy')\naxes[0].set_title('Training and validation accuracy')\n\n# Plot Training Loss on the second subplot\naxes[1].plot(epochs, loss ,'bo', label='Training Loss')\naxes[1].plot(epochs,val_loss ,label='Validation Loss',color = 'lightblue')\naxes[1].set_title('Training and validation loss')\nfor ax in axes:\n    ax.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T11:12:21.705720Z","iopub.execute_input":"2023-08-11T11:12:21.706576Z","iopub.status.idle":"2023-08-11T11:12:26.525768Z","shell.execute_reply.started":"2023-08-11T11:12:21.706541Z","shell.execute_reply":"2023-08-11T11:12:26.524850Z"},"trusted":true},"execution_count":null,"outputs":[]}]}