{"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":"<div style=\"border-radius:10px;\n            border : #fc9c9c solid;\n            background-color:#ffe6e6;\n           font-size:110%;\n           letter-spacing:0.5px;\n            text-align: center\">\n\n<center><h1 style=\"padding: 25px 0px; color:#ff4d4d; font-weight: bold; font-family: Cursive\">\n🐱 Cats vs. Dogs 🐶</h1></center>\n\n</div>","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"\"\"\"\nImports\n\"\"\"\nimport glob\nimport cv2\nfrom skimage.io import imread_collection\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport random\nimport matplotlib.pyplot as plt\n\nfrom zipfile import ZipFile\nimport os, shutil\n\nfrom tensorflow import keras\nfrom keras import layers\nfrom keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau, TensorBoard\nfrom keras import optimizers, losses, activations, models\nfrom keras.layers import Conv2D, Dense, Input, Flatten, Dropout, MaxPool2D, BatchNormalization, GlobalAveragePooling2D, Concatenate\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print()#os.path.join(dirname, filename)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-13T19:57:57.481872Z","iopub.execute_input":"2022-07-13T19:57:57.482495Z","iopub.status.idle":"2022-07-13T19:57:57.49301Z","shell.execute_reply.started":"2022-07-13T19:57:57.482457Z","shell.execute_reply":"2022-07-13T19:57:57.491861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Read Files and Extract","metadata":{}},{"cell_type":"code","source":"\"\"\"\nTrain Data\n\"\"\"\ntrain_zip = \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\"\n\nwith ZipFile(train_zip, 'r') as zip:\n    zip.printdir()\n    print('Extracting all the files now...')\n    zip.extractall(\"/kaggle/working/\")\n    print('Done train!')\n    zip.close()\n\ntrain_data = '/kaggle/working/train'","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:57:58.142597Z","iopub.execute_input":"2022-07-13T19:57:58.143662Z","iopub.status.idle":"2022-07-13T19:58:11.085003Z","shell.execute_reply.started":"2022-07-13T19:57:58.143614Z","shell.execute_reply":"2022-07-13T19:58:11.083887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nTest Data\n\"\"\"\ntest_zip = \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\"\n\nwith ZipFile(test_zip, 'r') as zip:\n    zip.printdir()\n    print('Extracting all the files now...')\n    zip.extractall(\"/kaggle/working/\")\n    print('Done test!')\n    zip.close()\n    \ntest_data = '/kaggle/working/test'","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:11.087033Z","iopub.execute_input":"2022-07-13T19:58:11.087744Z","iopub.status.idle":"2022-07-13T19:58:16.27425Z","shell.execute_reply.started":"2022-07-13T19:58:11.087697Z","shell.execute_reply":"2022-07-13T19:58:16.273107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nCreate valid_ ,train_, test_, all_ folders\nall_ --> all images before split to test train valid\n\"\"\"\nos.mkdir('/kaggle/working/valid_')\nos.mkdir('/kaggle/working/train_')\nos.mkdir('/kaggle/working/test_')\nos.mkdir('/kaggle/working/all_')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:16.276084Z","iopub.execute_input":"2022-07-13T19:58:16.276465Z","iopub.status.idle":"2022-07-13T19:58:16.30067Z","shell.execute_reply.started":"2022-07-13T19:58:16.276428Z","shell.execute_reply":"2022-07-13T19:58:16.298594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nMake classes files in train, test and valid folders\n\"\"\"\nclasses = ('dog','cat')\n\nif os.path.isdir('/kaggle/working') is True:\n    for c in classes:\n        print(c)\n        os.makedirs(f'./train_/{c}')    \n        os.makedirs(f'./valid_/{c}') \n        os.mkdir(f'./test_/{c}')\n        os.mkdir(f'./all_/{c}')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:37.197213Z","iopub.execute_input":"2022-07-13T19:58:37.197807Z","iopub.status.idle":"2022-07-13T19:58:37.205429Z","shell.execute_reply.started":"2022-07-13T19:58:37.19777Z","shell.execute_reply":"2022-07-13T19:58:37.204489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nGet names of images\n\"\"\"\nfolder_name = ''\nfolder_path = \"./train/\"\nnameList = set()\nfor image in os.listdir(folder_path):\n    folder_name = image.split('.')[0]\n    nameList.add(folder_name)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:37.641924Z","iopub.execute_input":"2022-07-13T19:58:37.64353Z","iopub.status.idle":"2022-07-13T19:58:37.674367Z","shell.execute_reply.started":"2022-07-13T19:58:37.643485Z","shell.execute_reply":"2022-07-13T19:58:37.673337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nCopy images to cat and dog folders in all parent folder\n\"\"\"\nbase_dir = './train'\nfor f in os.listdir(folder_path):\n    for path in nameList:\n        if f.endswith('.jpg'):\n            print(f)\n            if f.startswith(path):\n                shutil.copy(os.path.join(base_dir, str(f)), f'/kaggle/working/all_/{path}')\n                break","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:41.447547Z","iopub.execute_input":"2022-07-13T19:58:41.448074Z","iopub.status.idle":"2022-07-13T19:58:45.213852Z","shell.execute_reply.started":"2022-07-13T19:58:41.448041Z","shell.execute_reply":"2022-07-13T19:58:45.21272Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nCheck classes size\n\"\"\"\nprint('total training cat images:', len(os.listdir('/kaggle/working/all_/cat')))\nprint('total training dog images:', len(os.listdir('/kaggle/working/all_/dog')))\nprint(\"data is balanced\")","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:58:45.215725Z","iopub.execute_input":"2022-07-13T19:58:45.216574Z","iopub.status.idle":"2022-07-13T19:58:45.236741Z","shell.execute_reply.started":"2022-07-13T19:58:45.216523Z","shell.execute_reply":"2022-07-13T19:58:45.235735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nThe data was divided into a fixed number of samples for prediction (test) \nto ensure that there is no bias towards the majority category that contains a more significant number of images.\nThe data was divided into 70% training, and 30% validation. \n\"\"\"\nsize = 12500\nfor c in classes:\n    for files in random.sample(glob.glob(f'./all_/{c}/*'), int(size*.7)):\n        shutil.copy(files, f'./train_/{c}') \n    \n    for files in random.sample(glob.glob(f'./all_/{c}/*'), int(size*.2)):\n        shutil.copy(files, f'./valid_/{c}')\n    \n    for files in random.sample(glob.glob(f'./all_/{c}/*'), int(size*.1)):\n        shutil.copy(files, f'./test_/{c}')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:59:00.099167Z","iopub.execute_input":"2022-07-13T19:59:00.100083Z","iopub.status.idle":"2022-07-13T19:59:03.153278Z","shell.execute_reply.started":"2022-07-13T19:59:00.100045Z","shell.execute_reply":"2022-07-13T19:59:03.152192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nCheck classes size in train , test , valid\n\"\"\"\nfor c in classes:\n    print(f'total training {c} images:', len(os.listdir(f'/kaggle/working/train_/{c}')))\n    print(f'total validation {c} images:', len(os.listdir(f'/kaggle/working/valid_/{c}')))\n    print(f'total testing {c} images:', len(os.listdir(f'/kaggle/working/test_/{c}')))\nprint(\"data is balanced\")","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:59:03.155295Z","iopub.execute_input":"2022-07-13T19:59:03.155663Z","iopub.status.idle":"2022-07-13T19:59:03.177678Z","shell.execute_reply.started":"2022-07-13T19:59:03.155628Z","shell.execute_reply":"2022-07-13T19:59:03.176665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Images","metadata":{}},{"cell_type":"code","source":"\"\"\"\nRead train, valid, test using ImageDataGenerator with changing color\n\"\"\"\ntrain_batches = ImageDataGenerator().flow_from_directory(directory='./train_',shuffle=False,color_mode = 'grayscale',target_size=(200, 200), classes=classes, batch_size=128)\nvalid_batches = ImageDataGenerator().flow_from_directory(directory='./valid_',shuffle=False,color_mode = 'grayscale',target_size=(200, 200), classes=classes, batch_size=128)\ntest_batches =  ImageDataGenerator().flow_from_directory(directory='./test_',shuffle=False,color_mode = 'grayscale',target_size=(200, 200), classes=classes, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:59:04.632254Z","iopub.execute_input":"2022-07-13T19:59:04.632662Z","iopub.status.idle":"2022-07-13T19:59:06.221199Z","shell.execute_reply.started":"2022-07-13T19:59:04.632631Z","shell.execute_reply":"2022-07-13T19:59:06.220237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nRead train, valid, test using ImageDataGenerator without changing color\n\"\"\"\ntrain_batches_n = ImageDataGenerator().flow_from_directory(directory='./train_',shuffle=False,color_mode = 'rgb',target_size=(200, 200), classes=classes, batch_size=128)\nvalid_batches_n = ImageDataGenerator().flow_from_directory(directory='./valid_',shuffle=False,color_mode = 'rgb',target_size=(200, 200), classes=classes, batch_size=128)\ntest_batches_n =  ImageDataGenerator().flow_from_directory(directory='./test_',shuffle=False,color_mode = 'rgb',target_size=(200, 200), classes=classes, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T19:59:06.223322Z","iopub.execute_input":"2022-07-13T19:59:06.223674Z","iopub.status.idle":"2022-07-13T19:59:07.710923Z","shell.execute_reply.started":"2022-07-13T19:59:06.223639Z","shell.execute_reply":"2022-07-13T19:59:07.709129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Customized CNN1","metadata":{}},{"cell_type":"code","source":"\"\"\"\nOptimizer: Adam\nBatch Size: 128\nColor: RGB\nLR: .01\nDroppout: No\nTesting acc:  0.8307999968528748\n\"\"\"\nnclass = 2\nepoch = 100\n\nmodel = Sequential([\n    Conv2D(filters= 32, kernel_size= (3, 3), activation= 'relu', padding= 'same', input_shape= (200, 200, 1)),\n    MaxPool2D(pool_size= (2,2), strides= 2),\n    Conv2D(filters= 64, kernel_size= (3, 3), activation='relu', padding= 'same'),\n    MaxPool2D(pool_size= (2,2), strides= 2),\n    Flatten(),\n    Dense(units= 1, activation= 'sigmoid')\n])\nmodel.summary()\nmodel.compile(optimizer= keras.optimizers.Adam(learning_rate= .001), loss= 'categorical_crossentropy', metrics= ['accuracy'])\nes = EarlyStopping(monitor= 'val_loss', mode= 'auto', verbose= 1 ,  patience = 15)\n\nsave_best = ModelCheckpoint(\nfilepath = 'best_model.hdf5',\nverbose=1, save_best_only=True\n)\n\ntraind= model.fit(x= train_batches, validation_data= valid_batches,\n                  verbose= 1,validation_steps= valid_batches.samples // 128, \nepochs= epoch, callbacks= [save_best,es])\n\npredictions = model.predict(x= test_batches, verbose= 0)\nscore, accuracy = model.evaluate(x= test_batches, batch_size= 128)\n\nprint(\"Testing acc: \",accuracy)\nplt.plot(traind.history['accuracy'])\nplt.plot(traind.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\nplt.plot(traind.history['loss'])\nplt.plot(traind.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-07-13T19:59:17.130589Z","iopub.execute_input":"2022-07-13T19:59:17.131054Z","iopub.status.idle":"2022-07-13T20:22:26.207027Z","shell.execute_reply.started":"2022-07-13T19:59:17.131Z","shell.execute_reply":"2022-07-13T20:22:26.20597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nLoad model\n\"\"\"\nbest = keras.models.load_model('best_model.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-07-13T20:22:32.444709Z","iopub.execute_input":"2022-07-13T20:22:32.445085Z","iopub.status.idle":"2022-07-13T20:22:32.526548Z","shell.execute_reply.started":"2022-07-13T20:22:32.445054Z","shell.execute_reply":"2022-07-13T20:22:32.525605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n1- Labelling test images\n\"\"\"\ntest_images = os.listdir('./test')\ntest_df = pd.DataFrame(data = test_images, columns = ['Image'])\ntest_df['id'] = test_df['Image'].apply(lambda f: int(f.split('.')[0]))\ntest_df.sort_values(by = 'id', inplace = True, ignore_index = True)\n\ntest_gen = ImageDataGenerator().flow_from_dataframe(\n    test_df, \n    './test', \n    x_col='Image',\n    class_mode= None,\n    target_size=(200, 200),\n    batch_size=128,\n    shuffle=False\n)\n\npredict = best.predict(x= test_gen, verbose = 0)\ntest_df[\"label\"] = predict\nsub1 = test_df[[\"id\", \"label\"]]\nsub1.to_csv('sub_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T20:22:52.762277Z","iopub.execute_input":"2022-07-13T20:22:52.762619Z","iopub.status.idle":"2022-07-13T20:22:53.986404Z","shell.execute_reply.started":"2022-07-13T20:22:52.76259Z","shell.execute_reply":"2022-07-13T20:22:53.982981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\n2- Labelling test images\n\"\"\"\ntest_gen = ImageDataGenerator().flow_from_directory(\n    directory= './test', \n    class_mode= None,\n    target_size= (200, 200),\n    batch_size= 128,\n    shuffle= False\n)\n\npredict = best.predict(x= test_gen, verbose = 0)\nsubmit = pd.read_csv('../input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nsubmit['label'] = predict\nsubmit.to_csv('sub_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-13T20:22:40.823523Z","iopub.execute_input":"2022-07-13T20:22:40.823875Z","iopub.status.idle":"2022-07-13T20:22:41.036509Z","shell.execute_reply.started":"2022-07-13T20:22:40.823845Z","shell.execute_reply":"2022-07-13T20:22:41.035016Z"},"trusted":true},"execution_count":null,"outputs":[]}]}