{"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":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nimport seaborn as sns\nimport os\nimport zipfile\nimport matplotlib.image as mpimg\nimport seaborn as sns\nimport random\nimport os\nfrom zipfile import ZipFile\nfrom skimage.io import imread, imshow\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:19.196057Z","iopub.execute_input":"2021-09-26T11:16:19.196420Z","iopub.status.idle":"2021-09-26T11:16:19.203852Z","shell.execute_reply.started":"2021-09-26T11:16:19.196376Z","shell.execute_reply":"2021-09-26T11:16:19.202911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(\"../input/dogs-vs-cats\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:19.209427Z","iopub.execute_input":"2021-09-26T11:16:19.209870Z","iopub.status.idle":"2021-09-26T11:16:19.218662Z","shell.execute_reply.started":"2021-09-26T11:16:19.209838Z","shell.execute_reply":"2021-09-26T11:16:19.217658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDataPath = \"/kaggle/input/dogs-vs-cats/train.zip\"\ntestDataPath = \"/kaggle/input/dogs-vs-cats/test1.zip\"\nZipFile(trainDataPath,mode = \"r\").extractall()\nZipFile(testDataPath,mode =  \"r\").extractall()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:19.222440Z","iopub.execute_input":"2021-09-26T11:16:19.222680Z","iopub.status.idle":"2021-09-26T11:16:33.118892Z","shell.execute_reply.started":"2021-09-26T11:16:19.222656Z","shell.execute_reply":"2021-09-26T11:16:33.118023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file= './train'\ntest_file = './test1'","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.120300Z","iopub.execute_input":"2021-09-26T11:16:33.120641Z","iopub.status.idle":"2021-09-26T11:16:33.125264Z","shell.execute_reply.started":"2021-09-26T11:16:33.120602Z","shell.execute_reply":"2021-09-26T11:16:33.124306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = load_img(train_file +  '/cat.0.jpg')\nplt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.127069Z","iopub.execute_input":"2021-09-26T11:16:33.127692Z","iopub.status.idle":"2021-09-26T11:16:33.307246Z","shell.execute_reply.started":"2021-09-26T11:16:33.127652Z","shell.execute_reply":"2021-09-26T11:16:33.306270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_list = os.listdir(train_file)\ntest_list = os.listdir(test_file)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.309089Z","iopub.execute_input":"2021-09-26T11:16:33.309471Z","iopub.status.idle":"2021-09-26T11:16:33.335863Z","shell.execute_reply.started":"2021-09-26T11:16:33.309435Z","shell.execute_reply":"2021-09-26T11:16:33.335129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (train_list[:5])\nprint (test_list[:5])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.337061Z","iopub.execute_input":"2021-09-26T11:16:33.337445Z","iopub.status.idle":"2021-09-26T11:16:33.343245Z","shell.execute_reply.started":"2021-09-26T11:16:33.337405Z","shell.execute_reply":"2021-09-26T11:16:33.342393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print ('Toplam eğitim veri sayısı:',len(train_list))\nprint ('Toplam test veri sayısı:',len(test_list))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.344640Z","iopub.execute_input":"2021-09-26T11:16:33.345173Z","iopub.status.idle":"2021-09-26T11:16:33.353216Z","shell.execute_reply.started":"2021-09-26T11:16:33.345132Z","shell.execute_reply":"2021-09-26T11:16:33.352235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"categories = []\nfor filename in train_list:\n    category = filename.split('.')[0]\n    if category == 'dog':\n        categories.append(1)\n    else:\n        categories.append(0)\n\ndata_frame = pd.DataFrame({ 'filename': train_list, 'category': categories})","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.354631Z","iopub.execute_input":"2021-09-26T11:16:33.355028Z","iopub.status.idle":"2021-09-26T11:16:33.387861Z","shell.execute_reply.started":"2021-09-26T11:16:33.354975Z","shell.execute_reply":"2021-09-26T11:16:33.387099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame[:15]","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.390702Z","iopub.execute_input":"2021-09-26T11:16:33.391039Z","iopub.status.idle":"2021-09-26T11:16:33.400535Z","shell.execute_reply.started":"2021-09-26T11:16:33.391013Z","shell.execute_reply":"2021-09-26T11:16:33.399512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (categories[:15])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.403071Z","iopub.execute_input":"2021-09-26T11:16:33.403455Z","iopub.status.idle":"2021-09-26T11:16:33.409200Z","shell.execute_reply.started":"2021-09-26T11:16:33.403417Z","shell.execute_reply":"2021-09-26T11:16:33.408130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (train_list[:15])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.410861Z","iopub.execute_input":"2021-09-26T11:16:33.411267Z","iopub.status.idle":"2021-09-26T11:16:33.417433Z","shell.execute_reply.started":"2021-09-26T11:16:33.411233Z","shell.execute_reply":"2021-09-26T11:16:33.416327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame[\"category\"] = data_frame[\"category\"].replace({0: 'cat', 1: 'dog'}) ","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.419180Z","iopub.execute_input":"2021-09-26T11:16:33.419635Z","iopub.status.idle":"2021-09-26T11:16:33.429022Z","shell.execute_reply.started":"2021-09-26T11:16:33.419597Z","shell.execute_reply":"2021-09-26T11:16:33.428120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, val_set = train_test_split(data_frame, test_size=0.20, random_state=42)\nx_train = x_train.reset_index(drop=True)\nval_set = val_set.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.430582Z","iopub.execute_input":"2021-09-26T11:16:33.430995Z","iopub.status.idle":"2021-09-26T11:16:33.443174Z","shell.execute_reply.started":"2021-09-26T11:16:33.430961Z","shell.execute_reply":"2021-09-26T11:16:33.442343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print ('Toplam eğitim veri sayısı:',len(x_train))\nprint ('Toplam val veri sayısı:',len(val_set))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.444855Z","iopub.execute_input":"2021-09-26T11:16:33.446390Z","iopub.status.idle":"2021-09-26T11:16:33.451926Z","shell.execute_reply.started":"2021-09-26T11:16:33.446337Z","shell.execute_reply":"2021-09-26T11:16:33.450913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_class_counts=x_train['category'].value_counts()\nprint(\"Train data setimizdeki cat sayısı:{}\".format(train_class_counts[0]))\nprint(\"Train data setimizdeki dog sayısı:{}\".format(train_class_counts[1]))\n\nval_class_counts=val_set['category'].value_counts()\nprint(\"Val data setimizdeki cat sayısı:{}\".format(val_class_counts[0]))\nprint(\"Val data setimizdeki dog sayısı:{}\".format(val_class_counts[1]))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.453351Z","iopub.execute_input":"2021-09-26T11:16:33.454061Z","iopub.status.idle":"2021-09-26T11:16:33.470064Z","shell.execute_reply.started":"2021-09-26T11:16:33.454023Z","shell.execute_reply":"2021-09-26T11:16:33.468983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_rescale=ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.471593Z","iopub.execute_input":"2021-09-26T11:16:33.471953Z","iopub.status.idle":"2021-09-26T11:16:33.477789Z","shell.execute_reply.started":"2021-09-26T11:16:33.471918Z","shell.execute_reply":"2021-09-26T11:16:33.476931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_model=train_rescale.flow_from_dataframe(   x_train,\n                                                 directory = train_file, \n                                                 x_col='filename',\n                                                 y_col='category',\n                                                 color_size='rgb',\n                                                 target_size=(128,128),\n                                                 class_mode='categorical',\n                                                 batch_size=64               )","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.478996Z","iopub.execute_input":"2021-09-26T11:16:33.479457Z","iopub.status.idle":"2021-09-26T11:16:33.668437Z","shell.execute_reply.started":"2021-09-26T11:16:33.479423Z","shell.execute_reply":"2021-09-26T11:16:33.667379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_rescale=ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.669889Z","iopub.execute_input":"2021-09-26T11:16:33.670250Z","iopub.status.idle":"2021-09-26T11:16:33.674976Z","shell.execute_reply.started":"2021-09-26T11:16:33.670213Z","shell.execute_reply":"2021-09-26T11:16:33.674139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_model=val_rescale.flow_from_dataframe(       val_set,\n                                                 directory = train_file, \n                                                 x_col='filename',\n                                                 y_col='category',\n                                                 color_size='rgb',\n                                                 target_size=(128,128),\n                                                 class_mode='categorical',\n                                                 batch_size=64                            )","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.676219Z","iopub.execute_input":"2021-09-26T11:16:33.676780Z","iopub.status.idle":"2021-09-26T11:16:33.733840Z","shell.execute_reply.started":"2021-09-26T11:16:33.676742Z","shell.execute_reply":"2021-09-26T11:16:33.732985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_17=Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.735123Z","iopub.execute_input":"2021-09-26T11:16:33.735666Z","iopub.status.idle":"2021-09-26T11:16:33.744069Z","shell.execute_reply.started":"2021-09-26T11:16:33.735628Z","shell.execute_reply":"2021-09-26T11:16:33.743218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_17.add(Conv2D(128, kernel_size=(3, 3), activation='relu', input_shape=(128,128,3)))\nmodel_17.add(MaxPooling2D(pool_size=(2,2)))\nmodel_17.add(Conv2D(64,kernel_size=(3, 3), activation='relu'))\nmodel_17.add(MaxPooling2D(pool_size=(2,2)))\nmodel_17.add(Dropout(0.25))\nmodel_17.add(Conv2D(32,kernel_size=(3,3),activation='relu'))\nmodel_17.add(MaxPooling2D(pool_size=(2,2)))\nmodel_17.add(Dropout(0.25))\nmodel_17.add(Flatten())\nmodel_17.add(Dense(128, activation='relu'))\nmodel_17.add(Dropout(0.5))\nmodel_17.add(Dense(2, activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.745508Z","iopub.execute_input":"2021-09-26T11:16:33.745866Z","iopub.status.idle":"2021-09-26T11:16:33.812906Z","shell.execute_reply.started":"2021-09-26T11:16:33.745832Z","shell.execute_reply":"2021-09-26T11:16:33.812096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_17.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.814342Z","iopub.execute_input":"2021-09-26T11:16:33.814700Z","iopub.status.idle":"2021-09-26T11:16:33.825998Z","shell.execute_reply.started":"2021-09-26T11:16:33.814666Z","shell.execute_reply":"2021-09-26T11:16:33.824948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_17.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.827700Z","iopub.execute_input":"2021-09-26T11:16:33.828249Z","iopub.status.idle":"2021-09-26T11:16:33.842016Z","shell.execute_reply.started":"2021-09-26T11:16:33.828205Z","shell.execute_reply":"2021-09-26T11:16:33.841180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_17.optimizer.lr=0.001","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.846832Z","iopub.execute_input":"2021-09-26T11:16:33.847066Z","iopub.status.idle":"2021-09-26T11:16:33.850550Z","shell.execute_reply.started":"2021-09-26T11:16:33.847044Z","shell.execute_reply":"2021-09-26T11:16:33.849456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback= EarlyStopping(monitor='val_loss',patience=14, restore_best_weights=True, verbose=1, mode='min')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.852807Z","iopub.execute_input":"2021-09-26T11:16:33.853399Z","iopub.status.idle":"2021-09-26T11:16:33.858750Z","shell.execute_reply.started":"2021-09-26T11:16:33.853342Z","shell.execute_reply":"2021-09-26T11:16:33.857627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size=64\nepochs=60","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.860159Z","iopub.execute_input":"2021-09-26T11:16:33.860525Z","iopub.status.idle":"2021-09-26T11:16:33.867085Z","shell.execute_reply.started":"2021-09-26T11:16:33.860489Z","shell.execute_reply":"2021-09-26T11:16:33.866278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_17= model_17.fit(train_model,\n          batch_size=batch_size,\n          epochs=epochs,\n          shuffle=True,\n          verbose=1,\n          callbacks= callback, \n          validation_data=val_model\n                      )","metadata":{"execution":{"iopub.status.busy":"2021-09-26T11:16:33.868410Z","iopub.execute_input":"2021-09-26T11:16:33.868753Z","iopub.status.idle":"2021-09-26T12:21:29.420293Z","shell.execute_reply.started":"2021-09-26T11:16:33.868708Z","shell.execute_reply":"2021-09-26T12:21:29.419165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_17.history['loss'])\nplt.plot(history_17.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['loss', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.423163Z","iopub.execute_input":"2021-09-26T12:21:29.423430Z","iopub.status.idle":"2021-09-26T12:21:29.543288Z","shell.execute_reply.started":"2021-09-26T12:21:29.423403Z","shell.execute_reply":"2021-09-26T12:21:29.542321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_17.history['accuracy'])\nplt.plot(history_17.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['acc', 'val',], loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.544618Z","iopub.execute_input":"2021-09-26T12:21:29.544978Z","iopub.status.idle":"2021-09-26T12:21:29.683330Z","shell.execute_reply.started":"2021-09-26T12:21:29.544939Z","shell.execute_reply":"2021-09-26T12:21:29.682408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainaug=ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.684777Z","iopub.execute_input":"2021-09-26T12:21:29.685148Z","iopub.status.idle":"2021-09-26T12:21:29.690336Z","shell.execute_reply.started":"2021-09-26T12:21:29.685112Z","shell.execute_reply":"2021-09-26T12:21:29.689261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_newset=trainaug.flow_from_dataframe(\n    x_train, \n    directory = train_file, \n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.691608Z","iopub.execute_input":"2021-09-26T12:21:29.692018Z","iopub.status.idle":"2021-09-26T12:21:29.879968Z","shell.execute_reply.started":"2021-09-26T12:21:29.691982Z","shell.execute_reply":"2021-09-26T12:21:29.879084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_val=ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.881415Z","iopub.execute_input":"2021-09-26T12:21:29.881775Z","iopub.status.idle":"2021-09-26T12:21:29.886666Z","shell.execute_reply.started":"2021-09-26T12:21:29.881737Z","shell.execute_reply":"2021-09-26T12:21:29.885409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_newset=new_val.flow_from_dataframe(          val_set,\n                                                 directory = train_file, \n                                                 x_col='filename',\n                                                 y_col='category',\n                                                 color_size='rgb',\n                                                 target_size=(128,128),\n                                                 class_mode='categorical',\n                                                 batch_size=64                        )","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.888246Z","iopub.execute_input":"2021-09-26T12:21:29.888704Z","iopub.status.idle":"2021-09-26T12:21:29.945914Z","shell.execute_reply.started":"2021-09-26T12:21:29.888668Z","shell.execute_reply":"2021-09-26T12:21:29.945044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_df = x_train.sample(n=1).reset_index(drop=True)\nexample_generator = trainaug.flow_from_dataframe(   \n    example_df,   \n    './train/', \n    x_col='filename',\n    y_col='category',\n    target_size=(128,128),\n    class_mode='categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.947107Z","iopub.execute_input":"2021-09-26T12:21:29.947576Z","iopub.status.idle":"2021-09-26T12:21:29.958458Z","shell.execute_reply.started":"2021-09-26T12:21:29.947540Z","shell.execute_reply":"2021-09-26T12:21:29.957345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:29.960023Z","iopub.execute_input":"2021-09-26T12:21:29.960530Z","iopub.status.idle":"2021-09-26T12:21:31.637561Z","shell.execute_reply.started":"2021-09-26T12:21:29.960495Z","shell.execute_reply":"2021-09-26T12:21:31.636543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_19=Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.638907Z","iopub.execute_input":"2021-09-26T12:21:31.639221Z","iopub.status.idle":"2021-09-26T12:21:31.646529Z","shell.execute_reply.started":"2021-09-26T12:21:31.639189Z","shell.execute_reply":"2021-09-26T12:21:31.645807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_19.add(Conv2D(128, kernel_size=(3, 3), activation='relu', input_shape=(128,128,3)))\nmodel_19.add(MaxPooling2D(pool_size=(2,2)))\nmodel_19.add(Dropout(0.25))\nmodel_19.add(Conv2D(64,kernel_size=(3, 3), activation='relu'))\nmodel_19.add(MaxPooling2D(pool_size=(2,2)))\nmodel_19.add(Dropout(0.25))\nmodel_19.add(Conv2D(32,kernel_size=(3,3),activation='relu'))\nmodel_19.add(MaxPooling2D(pool_size=(2,2)))\nmodel_19.add(Dropout(0.25))\nmodel_19.add(Flatten())\nmodel_19.add(Dense(128, activation='relu'))\nmodel_19.add(Dropout(0.3))\nmodel_19.add(Dense(128, activation='relu'))\nmodel_19.add(Dropout(0.5))\nmodel_19.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.647723Z","iopub.execute_input":"2021-09-26T12:21:31.648203Z","iopub.status.idle":"2021-09-26T12:21:31.725505Z","shell.execute_reply.started":"2021-09-26T12:21:31.648168Z","shell.execute_reply":"2021-09-26T12:21:31.724737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_19.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.726625Z","iopub.execute_input":"2021-09-26T12:21:31.726953Z","iopub.status.idle":"2021-09-26T12:21:31.737454Z","shell.execute_reply.started":"2021-09-26T12:21:31.726919Z","shell.execute_reply":"2021-09-26T12:21:31.736640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_19.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.738599Z","iopub.execute_input":"2021-09-26T12:21:31.738966Z","iopub.status.idle":"2021-09-26T12:21:31.752187Z","shell.execute_reply.started":"2021-09-26T12:21:31.738931Z","shell.execute_reply":"2021-09-26T12:21:31.751432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_19.optimizer.lr=0.001","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.753115Z","iopub.execute_input":"2021-09-26T12:21:31.753492Z","iopub.status.idle":"2021-09-26T12:21:31.760374Z","shell.execute_reply.started":"2021-09-26T12:21:31.753459Z","shell.execute_reply":"2021-09-26T12:21:31.759529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback_2= EarlyStopping(monitor='val_loss',patience=14, restore_best_weights=True, verbose=1, mode='min')","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.761532Z","iopub.execute_input":"2021-09-26T12:21:31.761869Z","iopub.status.idle":"2021-09-26T12:21:31.773742Z","shell.execute_reply.started":"2021-09-26T12:21:31.761836Z","shell.execute_reply":"2021-09-26T12:21:31.772891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_19= model_19.fit(train_newset,\n                batch_size=batch_size,\n                epochs=epochs,\n                shuffle=True,\n                verbose=1,\n                callbacks= callback_2, \n                validation_data=val_newset\n                                       )","metadata":{"execution":{"iopub.status.busy":"2021-09-26T12:21:31.774985Z","iopub.execute_input":"2021-09-26T12:21:31.775416Z","iopub.status.idle":"2021-09-26T14:49:00.818778Z","shell.execute_reply.started":"2021-09-26T12:21:31.775380Z","shell.execute_reply":"2021-09-26T14:49:00.817951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_19.history['accuracy'])\nplt.plot(history_19.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['acc', 'val',], loc='lower right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:00.820305Z","iopub.execute_input":"2021-09-26T14:49:00.820649Z","iopub.status.idle":"2021-09-26T14:49:00.954094Z","shell.execute_reply.started":"2021-09-26T14:49:00.820612Z","shell.execute_reply":"2021-09-26T14:49:00.953166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history_19.history['loss'])\nplt.plot(history_19.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['loss', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:00.955484Z","iopub.execute_input":"2021-09-26T14:49:00.955836Z","iopub.status.idle":"2021-09-26T14:49:01.099071Z","shell.execute_reply.started":"2021-09-26T14:49:00.955799Z","shell.execute_reply":"2021-09-26T14:49:01.098282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_file = './test1'\n# test_list = os.listdir(test_file)\n\n\ntest_data_files= pd.DataFrame({\n    \n    'filename': test_list\n    \n})\n","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:01.100394Z","iopub.execute_input":"2021-09-26T14:49:01.100745Z","iopub.status.idle":"2021-09-26T14:49:01.107536Z","shell.execute_reply.started":"2021-09-26T14:49:01.100708Z","shell.execute_reply":"2021-09-26T14:49:01.106369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nb_samples =test_data_files.shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:01.109034Z","iopub.execute_input":"2021-09-26T14:49:01.109493Z","iopub.status.idle":"2021-09-26T14:49:01.117912Z","shell.execute_reply.started":"2021-09-26T14:49:01.109457Z","shell.execute_reply":"2021-09-26T14:49:01.117032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generate_test_data_files= ImageDataGenerator(rescale=1./255)\ntest_gen = generate_test_data_files.flow_from_dataframe(\n    test_data_files, \n    './test1' , \n    x_col='filename',\n    #y_col=None,\n    color_size='rgb',\n    class_mode=None,\n    target_size=(128,128),\n    batch_size=64,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:01.119116Z","iopub.execute_input":"2021-09-26T14:49:01.119469Z","iopub.status.idle":"2021-09-26T14:49:01.226821Z","shell.execute_reply.started":"2021-09-26T14:49:01.119431Z","shell.execute_reply":"2021-09-26T14:49:01.225831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model_19.predict_generator(test_gen, steps=np.ceil(nb_samples/batch_size))","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:01.228170Z","iopub.execute_input":"2021-09-26T14:49:01.228534Z","iopub.status.idle":"2021-09-26T14:49:34.344622Z","shell.execute_reply.started":"2021-09-26T14:49:01.228498Z","shell.execute_reply":"2021-09-26T14:49:34.343770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_files['category'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.346109Z","iopub.execute_input":"2021-09-26T14:49:34.346477Z","iopub.status.idle":"2021-09-26T14:49:34.353951Z","shell.execute_reply.started":"2021-09-26T14:49:34.346439Z","shell.execute_reply":"2021-09-26T14:49:34.353212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_newset.class_indices.items())\ntest_data_files['category'] = test_data_files['category'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.355182Z","iopub.execute_input":"2021-09-26T14:49:34.355585Z","iopub.status.idle":"2021-09-26T14:49:34.365731Z","shell.execute_reply.started":"2021-09-26T14:49:34.355545Z","shell.execute_reply":"2021-09-26T14:49:34.364929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data_files['category'] = test_data_files['category'].replace({ 'dog': 1, 'cat': 0 })","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.366872Z","iopub.execute_input":"2021-09-26T14:49:34.367207Z","iopub.status.idle":"2021-09-26T14:49:34.382722Z","shell.execute_reply.started":"2021-09-26T14:49:34.367170Z","shell.execute_reply":"2021-09-26T14:49:34.381920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = test_data_files.copy()\nsubmission_df['fn'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['category']\n\n","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.384066Z","iopub.execute_input":"2021-09-26T14:49:34.384405Z","iopub.status.idle":"2021-09-26T14:49:34.412406Z","shell.execute_reply.started":"2021-09-26T14:49:34.384369Z","shell.execute_reply":"2021-09-26T14:49:34.411692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.drop(['filename', 'category'], axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.414085Z","iopub.execute_input":"2021-09-26T14:49:34.414644Z","iopub.status.idle":"2021-09-26T14:49:34.420763Z","shell.execute_reply.started":"2021-09-26T14:49:34.414609Z","shell.execute_reply":"2021-09-26T14:49:34.420011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print (submission_df)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.422036Z","iopub.execute_input":"2021-09-26T14:49:34.422435Z","iopub.status.idle":"2021-09-26T14:49:34.432006Z","shell.execute_reply.started":"2021-09-26T14:49:34.422379Z","shell.execute_reply":"2021-09-26T14:49:34.430948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame_sub= pd.DataFrame({\n    \n    'fn': test_list,\n    'label' : test_data_files['category']\n    \n})","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.433066Z","iopub.execute_input":"2021-09-26T14:49:34.433304Z","iopub.status.idle":"2021-09-26T14:49:34.443682Z","shell.execute_reply.started":"2021-09-26T14:49:34.433280Z","shell.execute_reply":"2021-09-26T14:49:34.442866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_frame_sub.head(10000)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.444921Z","iopub.execute_input":"2021-09-26T14:49:34.445283Z","iopub.status.idle":"2021-09-26T14:49:34.461124Z","shell.execute_reply.started":"2021-09-26T14:49:34.445241Z","shell.execute_reply":"2021-09-26T14:49:34.460250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_sub = load_img(test_file +  '/7038.jpg')\nplt.imshow(img_sub)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.462323Z","iopub.execute_input":"2021-09-26T14:49:34.462865Z","iopub.status.idle":"2021-09-26T14:49:34.642284Z","shell.execute_reply.started":"2021-09-26T14:49:34.462828Z","shell.execute_reply":"2021-09-26T14:49:34.641552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_test = data_frame_sub.head(25)\nimg_test.head(25)","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.644142Z","iopub.execute_input":"2021-09-26T14:49:34.644502Z","iopub.status.idle":"2021-09-26T14:49:34.655762Z","shell.execute_reply.started":"2021-09-26T14:49:34.644467Z","shell.execute_reply":"2021-09-26T14:49:34.654728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\n\nfor index, row in img_test.iterrows():\n    fn = row['fn']\n    label = row['label']\n    test_img= load_img('./test1/'+ fn, target_size=(128,128))\n    plt.subplot(5,5,index+1)\n    plt.imshow(test_img)\n    plt.xlabel(fn + '(' + \"{}\".format(label) + ')' )\n    \nplt.tight_layout()\nplt.show()    \n    \n \n","metadata":{"execution":{"iopub.status.busy":"2021-09-26T14:49:34.657376Z","iopub.execute_input":"2021-09-26T14:49:34.657828Z","iopub.status.idle":"2021-09-26T14:49:37.670817Z","shell.execute_reply.started":"2021-09-26T14:49:34.657790Z","shell.execute_reply":"2021-09-26T14:49:37.669896Z"},"trusted":true},"execution_count":null,"outputs":[]}]}