{"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":"# Notebook Contents\n<li>Defining The Problem</li>\n<li>Importing The Dependencies</li>\n<li>Defining The Functions</li>\n<li>Preparing The Data Directories</li>\n<li>Preprocessing The Data</li>\n<li>Building The CNN</li>\n<li>CCN Optimization</li>\n<li>Preparing Testing Data</li>\n<li>Submission</li>","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"# Defining The Problem","metadata":{}},{"cell_type":"markdown","source":"> This project for classify the images as dogs images or cats images, my target in this project is building a CNN for predicting if the image for dog or a cat.","metadata":{}},{"cell_type":"markdown","source":"# Importing The Dependencies","metadata":{}},{"cell_type":"code","source":"# Main libraries\nimport pandas as pd\nfrom zipfile import ZipFile\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport shutil\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Preprocessing\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Network\nfrom tensorflow.keras import models\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:18:10.868224Z","iopub.execute_input":"2022-12-01T01:18:10.868577Z","iopub.status.idle":"2022-12-01T01:18:10.875653Z","shell.execute_reply.started":"2022-12-01T01:18:10.868548Z","shell.execute_reply":"2022-12-01T01:18:10.874774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining The Functions","metadata":{}},{"cell_type":"code","source":"# For data directories\ndef extract_files(dir):\n    '''\n    This function for extracting the files.\n    INPUT:\n    dir: str. The main file direction.\n    OUTPUT:\n    Extracted files.\n    '''\n    with ZipFile(dir,'r') as zip:\n        zip.extractall()\n\ndef copy_files(rng,path1,name,path2):\n    '''\n    This function for copying the files from the source to destination.\n    INPUT:\n    rng: list . A list that contains the ranges.\n    path1: str. The data directory.\n    name: str. The animal name.\n    path2: str. The destination directory\n    '''\n    fnames = [f'{name}.{idx}.jpg' for idx in range(rng[0],rng[1])]\n    for fname in fnames:\n        src = os.path.join(path1,fname)\n        dst = os.path.join(path2,fname)\n        shutil.copyfile(src,dst)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:34:51.583566Z","iopub.execute_input":"2022-11-30T19:34:51.584534Z","iopub.status.idle":"2022-11-30T19:34:51.596407Z","shell.execute_reply.started":"2022-11-30T19:34:51.584496Z","shell.execute_reply":"2022-11-30T19:34:51.594993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing The Data Directories","metadata":{}},{"cell_type":"code","source":"# Extracting the data\ntrain_dir = '../input/dogs-vs-cats/train.zip'\ntest_dir = '../input/dogs-vs-cats/test1.zip'\nextract_files(train_dir)\nextract_files(test_dir)\nprint('Extraction process is done')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:34:56.615571Z","iopub.execute_input":"2022-11-30T19:34:56.616230Z","iopub.status.idle":"2022-11-30T19:35:13.572975Z","shell.execute_reply.started":"2022-11-30T19:34:56.616196Z","shell.execute_reply":"2022-11-30T19:35:13.571918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The main path\npath = '/kaggle/working/'\n\n# Training directory\ntrain_dir = os.path.join(path,'Train')\nos.mkdir(train_dir)\n\n# Validation directory\nval_dir = os.path.join(path,'Val')\nos.mkdir(val_dir)\n\n# Cats training directory\ntrain_cats_dir = os.path.join(train_dir,'cats')\nos.mkdir(train_cats_dir)\n\n# Cats validation directory\nval_cats_dir = os.path.join(val_dir,'cats')\nos.mkdir(val_cats_dir)\n\n# Dogs training directory\ntrain_dogs_dir = os.path.join(train_dir,'dogs')\nos.mkdir(train_dogs_dir)\n\n# Dogs validation directory\nval_dogs_dir = os.path.join(val_dir,'dogs')\nos.mkdir(val_dogs_dir)\n\n# Copying training cats and dogs files\ncopy_files([0,10000],os.path.join(path,'train'),'cat',train_cats_dir)\ncopy_files([0,10000],os.path.join(path,'train'),'dog',train_dogs_dir)\n\n# Copying validation cats and dogs files\ncopy_files([10000,12500],os.path.join(path,'train'),'cat',val_cats_dir)\ncopy_files([10000,12500],os.path.join(path,'train'),'dog',val_dogs_dir)\n\n# Display message\nprint('Preparing the data directories is done')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:35:16.725335Z","iopub.execute_input":"2022-11-30T19:35:16.726003Z","iopub.status.idle":"2022-11-30T19:35:20.028506Z","shell.execute_reply.started":"2022-11-30T19:35:16.725962Z","shell.execute_reply":"2022-11-30T19:35:20.027489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing The Data","metadata":{}},{"cell_type":"code","source":"# Creating image generator steps\ntrain_data = ImageDataGenerator(rescale=1/255)\nvalidate_data = ImageDataGenerator(rescale=1/255)\ntrain_generator = train_data.flow_from_directory(directory=train_dir,target_size=(128,128),batch_size=20,class_mode='binary')\nvalidate_generator = validate_data.flow_from_directory(directory=val_dir,target_size=(128,128),batch_size=20,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T18:37:32.139130Z","iopub.execute_input":"2022-11-30T18:37:32.139564Z","iopub.status.idle":"2022-11-30T18:37:32.703814Z","shell.execute_reply.started":"2022-11-30T18:37:32.139526Z","shell.execute_reply":"2022-11-30T18:37:32.702644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Building The CNN","metadata":{}},{"cell_type":"code","source":"# The CNN architecture\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape = (128,128,3)))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-11-29T20:46:54.257153Z","iopub.execute_input":"2022-11-29T20:46:54.257532Z","iopub.status.idle":"2022-11-29T20:46:54.315243Z","shell.execute_reply.started":"2022-11-29T20:46:54.257498Z","shell.execute_reply":"2022-11-29T20:46:54.314322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T20:46:56.827266Z","iopub.execute_input":"2022-11-29T20:46:56.828078Z","iopub.status.idle":"2022-11-29T20:46:56.836926Z","shell.execute_reply.started":"2022-11-29T20:46:56.828041Z","shell.execute_reply":"2022-11-29T20:46:56.835881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nhistory = model.fit(train_generator,steps_per_epoch=1000,epochs=20,validation_data=validate_generator,validation_steps=250,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-29T20:47:09.597097Z","iopub.execute_input":"2022-11-29T20:47:09.597884Z","iopub.status.idle":"2022-11-29T21:09:47.909384Z","shell.execute_reply.started":"2022-11-29T20:47:09.597842Z","shell.execute_reply":"2022-11-29T21:09:47.908246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- The over fitting issue.","metadata":{"execution":{"iopub.status.busy":"2022-11-29T21:19:41.716338Z","iopub.execute_input":"2022-11-29T21:19:41.717003Z","iopub.status.idle":"2022-11-29T21:20:34.935065Z","shell.execute_reply.started":"2022-11-29T21:19:41.716969Z","shell.execute_reply":"2022-11-29T21:20:34.933962Z"}}},{"cell_type":"code","source":"# Get the results\ntrain_acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(len(train_acc))\n\n# Plotting the results\nfig,axs = plt.subplots(1,2,figsize = (12,7))\naxs[0].plot(epochs,train_acc,'bo',label = 'Training Accuracy')\naxs[0].plot(epochs,val_acc,'go--',label = 'Validation Accuracy')\naxs[0].set_title('Training Acc VS Validation Acc',color='red')\naxs[0].set_xlabel('Epochs',color = 'red')\naxs[0].set_ylabel('Accuracy',color='red')\naxs[0].legend()\n\naxs[1].plot(epochs,train_loss,'bo',label = 'Training Loss')\naxs[1].plot(epochs,val_loss,'go--',label = 'Validation Loss')\naxs[1].set_title('Training Loss VS Validation Loss',color='red')\naxs[1].set_xlabel('Epochs',color = 'red')\naxs[1].set_ylabel('Loss',color='red')\naxs[1].legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-29T22:00:49.152474Z","iopub.execute_input":"2022-11-29T22:00:49.152840Z","iopub.status.idle":"2022-11-29T22:00:49.496821Z","shell.execute_reply.started":"2022-11-29T22:00:49.152809Z","shell.execute_reply":"2022-11-29T22:00:49.495886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- The loss is increased and the accuracy decreased in the validation process.","metadata":{}},{"cell_type":"markdown","source":"# CCN Optimization","metadata":{}},{"cell_type":"code","source":"# Creating image generator steps\ntrain_datagen = ImageDataGenerator(rescale = 1/255,rotation_range=30,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)\nval_datagen = ImageDataGenerator(rescale=1/255)\ntrain_gen = train_datagen.flow_from_directory(train_dir,target_size=(130,130),batch_size=50,class_mode='binary')\nval_gen = val_datagen.flow_from_directory(val_dir,target_size=(130,130),batch_size=50,class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2022-11-30T23:48:10.998610Z","iopub.execute_input":"2022-11-30T23:48:10.999197Z","iopub.status.idle":"2022-11-30T23:48:11.565602Z","shell.execute_reply.started":"2022-11-30T23:48:10.999163Z","shell.execute_reply":"2022-11-30T23:48:11.564555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The CNN architecture\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32,(3,3),activation='relu',input_shape = (130,130,3)))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(32,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(64,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Conv2D(128,(3,3),activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dense(1,activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2022-11-30T23:48:36.841652Z","iopub.execute_input":"2022-11-30T23:48:36.842607Z","iopub.status.idle":"2022-11-30T23:48:36.914659Z","shell.execute_reply.started":"2022-11-30T23:48:36.842572Z","shell.execute_reply":"2022-11-30T23:48:36.913721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T23:48:43.709754Z","iopub.execute_input":"2022-11-30T23:48:43.710113Z","iopub.status.idle":"2022-11-30T23:48:43.716846Z","shell.execute_reply.started":"2022-11-30T23:48:43.710084Z","shell.execute_reply":"2022-11-30T23:48:43.715696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fitting the model\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\nhistory = model.fit(train_gen,steps_per_epoch=400,epochs=20,validation_data=val_gen,validation_steps=100,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T23:49:58.481755Z","iopub.execute_input":"2022-11-30T23:49:58.482110Z","iopub.status.idle":"2022-12-01T00:35:34.111199Z","shell.execute_reply.started":"2022-11-30T23:49:58.482081Z","shell.execute_reply":"2022-12-01T00:35:34.109892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the results\ntrain_acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(len(train_acc))\n\n# Plotting the results\nfig,axs = plt.subplots(1,2,figsize = (12,7))\naxs[0].plot(epochs,train_acc,'bo',label = 'Training Accuracy')\naxs[0].plot(epochs,val_acc,'go--',label = 'Validation Accuracy')\naxs[0].set_title('Training Acc VS Validation Acc',color='red')\naxs[0].set_xlabel('Epochs',color = 'red')\naxs[0].set_ylabel('Accuracy',color='red')\naxs[0].legend()\n\naxs[1].plot(epochs,train_loss,'bo',label = 'Training Loss')\naxs[1].plot(epochs,val_loss,'go--',label = 'Validation Loss')\naxs[1].set_title('Training Loss VS Validation Loss',color='red')\naxs[1].set_xlabel('Epochs',color = 'red')\naxs[1].set_ylabel('Loss',color='red')\naxs[1].legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T00:35:47.993868Z","iopub.execute_input":"2022-12-01T00:35:47.994226Z","iopub.status.idle":"2022-12-01T00:35:48.348181Z","shell.execute_reply.started":"2022-12-01T00:35:47.994198Z","shell.execute_reply":"2022-12-01T00:35:48.347289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- The preformance increased after tuning and optimizing the network.","metadata":{}},{"cell_type":"markdown","source":"# Preparing Testing Data","metadata":{}},{"cell_type":"code","source":"# The path\ntest_dir = '../input/dogs-vs-cats/test1.zip'\n# Extracting data\nextract_files(test_dir)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T00:42:19.084237Z","iopub.execute_input":"2022-12-01T00:42:19.084594Z","iopub.status.idle":"2022-12-01T00:43:05.203459Z","shell.execute_reply.started":"2022-12-01T00:42:19.084563Z","shell.execute_reply":"2022-12-01T00:43:05.202450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the image generator\ntest_data_dir = '/kaggle/working/test1' \ntest_df = pd.DataFrame({'Filename':os.listdir(test_data_dir)})\ntest_datagen = ImageDataGenerator(rescale=1/255)\ntest_gen = test_datagen.flow_from_dataframe(test_df,test_data_dir,target_size=(130,130),batch_size=50,x_col='Filename',y_col=None,class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:29:44.048603Z","iopub.execute_input":"2022-12-01T01:29:44.049300Z","iopub.status.idle":"2022-12-01T01:29:44.198696Z","shell.execute_reply.started":"2022-12-01T01:29:44.049266Z","shell.execute_reply":"2022-12-01T01:29:44.197757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The first five rows \ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:29:47.053194Z","iopub.execute_input":"2022-12-01T01:29:47.053583Z","iopub.status.idle":"2022-12-01T01:29:47.062589Z","shell.execute_reply.started":"2022-12-01T01:29:47.053550Z","shell.execute_reply":"2022-12-01T01:29:47.061592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The shape\ntest_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:29:47.961785Z","iopub.execute_input":"2022-12-01T01:29:47.962159Z","iopub.status.idle":"2022-12-01T01:29:47.971826Z","shell.execute_reply.started":"2022-12-01T01:29:47.962122Z","shell.execute_reply":"2022-12-01T01:29:47.970610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Return the classes\ndict((k,v) for k,v in train_gen.class_indices.items())","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:36:06.869022Z","iopub.execute_input":"2022-12-01T01:36:06.869375Z","iopub.status.idle":"2022-12-01T01:36:06.877099Z","shell.execute_reply.started":"2022-12-01T01:36:06.869346Z","shell.execute_reply":"2022-12-01T01:36:06.876022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get the prediction\npred = model.predict(test_gen,steps=250)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:29:48.795831Z","iopub.execute_input":"2022-12-01T01:29:48.796879Z","iopub.status.idle":"2022-12-01T01:30:22.192528Z","shell.execute_reply.started":"2022-12-01T01:29:48.796835Z","shell.execute_reply":"2022-12-01T01:30:22.191556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save results\ntest_df['Pred'] = np.where(pred > 0.5,1,0)\n","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:40:35.495070Z","iopub.execute_input":"2022-12-01T01:40:35.495432Z","iopub.status.idle":"2022-12-01T01:40:35.502675Z","shell.execute_reply.started":"2022-12-01T01:40:35.495405Z","shell.execute_reply":"2022-12-01T01:40:35.501400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:40:44.291289Z","iopub.execute_input":"2022-12-01T01:40:44.291731Z","iopub.status.idle":"2022-12-01T01:40:44.304360Z","shell.execute_reply.started":"2022-12-01T01:40:44.291681Z","shell.execute_reply":"2022-12-01T01:40:44.303468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# Taking a copy\ntest_copy = test_df.copy()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:46:02.721862Z","iopub.execute_input":"2022-12-01T01:46:02.722221Z","iopub.status.idle":"2022-12-01T01:46:02.727268Z","shell.execute_reply.started":"2022-12-01T01:46:02.722186Z","shell.execute_reply":"2022-12-01T01:46:02.726085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_copy.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:46:15.763008Z","iopub.execute_input":"2022-12-01T01:46:15.763368Z","iopub.status.idle":"2022-12-01T01:46:15.772581Z","shell.execute_reply.started":"2022-12-01T01:46:15.763339Z","shell.execute_reply":"2022-12-01T01:46:15.771646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Submit the results\ntest_copy['id'] = test_copy['Filename'].str.split('.').str[0]\ntest_copy['label'] = test_copy['Pred']\ntest_copy.drop(['Filename','Pred'],axis=1,inplace=True)\ntest_copy.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-12-01T01:52:20.308387Z","iopub.execute_input":"2022-12-01T01:52:20.308775Z","iopub.status.idle":"2022-12-01T01:52:20.350690Z","shell.execute_reply.started":"2022-12-01T01:52:20.308740Z","shell.execute_reply":"2022-12-01T01:52:20.349764Z"},"trusted":true},"execution_count":null,"outputs":[]}]}