{"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":"\nimport os\nimport zipfile\n\nimport numpy as np \nimport pandas as pd \n\nimport tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nfrom tensorflow import keras\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.models import load_model\n\n\nfrom sklearn.model_selection import train_test_split\n\nimport matplotlib.pyplot as plt\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-15T21:04:48.423363Z","iopub.execute_input":"2023-02-15T21:04:48.423696Z","iopub.status.idle":"2023-02-15T21:04:48.430902Z","shell.execute_reply.started":"2023-02-15T21:04:48.423661Z","shell.execute_reply":"2023-02-15T21:04:48.430171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nTRAIN_PATH = \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\"\nTEST_PATH = \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\"\n\nUNZIP_DATA = \"../kaggle/files/unzipped/\"\nUNZIP_TRAIN = \"../kaggle/files/unzipped/train\"\nUNZIP_TEST = \"../kaggle/files/unzipped/test\"\n\nBATCH_SIZE = 32\n\nSEED = 88888\nIMG_SIZE = 224\nEPOCHS = 100\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:38.924498Z","iopub.execute_input":"2023-02-15T17:31:38.924809Z","iopub.status.idle":"2023-02-15T17:31:38.933020Z","shell.execute_reply.started":"2023-02-15T17:31:38.924771Z","shell.execute_reply":"2023-02-15T17:31:38.932207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith zipfile.ZipFile(TRAIN_PATH, 'r') as zipp:\n    zipp.extractall(UNZIP_DATA)\n    print('Done!')\n    \nwith zipfile.ZipFile(TEST_PATH, 'r') as zipp:\n    zipp.extractall(UNZIP_DATA)\n    print('Done!')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:38.934481Z","iopub.execute_input":"2023-02-15T17:31:38.935361Z","iopub.status.idle":"2023-02-15T17:31:57.449152Z","shell.execute_reply.started":"2023-02-15T17:31:38.935279Z","shell.execute_reply":"2023-02-15T17:31:57.448379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_images_files = os.listdir(\"../kaggle/files/unzipped/train\")\ntest_image_files =os.listdir(\"../kaggle/files/unzipped/test\")\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:57.452069Z","iopub.execute_input":"2023-02-15T17:31:57.452277Z","iopub.status.idle":"2023-02-15T17:31:57.480331Z","shell.execute_reply.started":"2023-02-15T17:31:57.452248Z","shell.execute_reply":"2023-02-15T17:31:57.479692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating DataFrame with file names, class names and paths  for training samples\nclasses = [name.split('.')[0] for name in training_images_files]\n\ntrain = pd.DataFrame({\n    'filename': training_images_files,\n    'class': classes,\n})\n\ntrain['Path'] = train['filename'].apply(\n    lambda filename: os.path.join('../kaggle/files/unzipped/test', filename))\n\ndisplay(train.head())\n\n\n#Creating DataFrame with file names and  IDs  for test samples\ntest = pd.DataFrame(data = test_image_files, columns = ['filename'])\ntest['id'] = test['filename'].apply(lambda f: int(f.split('.')[0]))\ntest.sort_values(by = 'id', inplace = True, ignore_index = True)\ntest.head()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:57.481795Z","iopub.execute_input":"2023-02-15T17:31:57.482054Z","iopub.status.idle":"2023-02-15T17:31:57.571064Z","shell.execute_reply.started":"2023-02-15T17:31:57.482018Z","shell.execute_reply":"2023-02-15T17:31:57.570336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split train data tu train and validation sets\ntrain_df, valid_df = train_test_split(train, \n                                      test_size = .10, \n                                      shuffle=True ,\n                                      random_state=SEED)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:57.572423Z","iopub.execute_input":"2023-02-15T17:31:57.572989Z","iopub.status.idle":"2023-02-15T17:31:57.584790Z","shell.execute_reply.started":"2023-02-15T17:31:57.572939Z","shell.execute_reply":"2023-02-15T17:31:57.583932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \ntrain_generator = ImageDataGenerator(preprocessing_function=preprocess_input,\n                                    rotation_range=65,\n                                    shear_range=0.1,\n                                    zoom_range=0.2,\n                                    horizontal_flip=False,\n                                    vertical_flip=True,\n                                    width_shift_range=0.2,\n                                    height_shift_range=0.2\n                                    )\n\n    \n\ntrain_generator = train_generator.flow_from_dataframe(\n            train_df,\n            UNZIP_TRAIN,\n            x_col='filename',\n            y_col='class',\n            target_size=(IMG_SIZE,IMG_SIZE),\n            batch_size=32,\n            class_mode='binary')\n\n\nvalidation_generator = ImageDataGenerator(preprocessing_function=preprocess_input)\n\n\nvalidation_generator = validation_generator.flow_from_dataframe(\n            valid_df,\n            UNZIP_TRAIN,\n            x_col = 'filename',\n            y_col = 'class',\n            target_size = (IMG_SIZE,IMG_SIZE),\n            batch_size=32,\n            class_mode='binary'\n\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:57.586445Z","iopub.execute_input":"2023-02-15T17:31:57.586993Z","iopub.status.idle":"2023-02-15T17:31:57.878019Z","shell.execute_reply.started":"2023-02-15T17:31:57.586957Z","shell.execute_reply":"2023-02-15T17:31:57.877181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pre_trained_model = EfficientNetB0(input_shape = (IMG_SIZE, IMG_SIZE, 3),\n                                include_top = False,\n                                weights = 'imagenet')\n\nfor layer in pre_trained_model.layers:\n    layer.trainable = False\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:31:57.879334Z","iopub.execute_input":"2023-02-15T17:31:57.879581Z","iopub.status.idle":"2023-02-15T17:32:04.446378Z","shell.execute_reply.started":"2023-02-15T17:31:57.879545Z","shell.execute_reply":"2023-02-15T17:32:04.445449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_layer = pre_trained_model.get_layer('top_activation')\n\nlast_output=last_layer.output\n\ndef create_model(last_output):\n    \n    x=keras.layers.GlobalAveragePooling2D()(last_output)\n    x=keras.layers.BatchNormalization()(x)\n    x=keras.layers.Dense(1, activation='sigmoid')(x)\n\n    model = Model(pre_trained_model.input, x)\n\n    model.compile(optimizer = RMSprop(learning_rate=0.001),\n                  loss = 'binary_crossentropy',\n                  metrics = ['acc'])\n    return model\n\nmodel=create_model(last_output)\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:32:04.451692Z","iopub.execute_input":"2023-02-15T17:32:04.454694Z","iopub.status.idle":"2023-02-15T17:32:04.540651Z","shell.execute_reply.started":"2023-02-15T17:32:04.454639Z","shell.execute_reply":"2023-02-15T17:32:04.539885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',\n                                            patience=5,\n                                            verbose=1,\n                                            factor=0.5,\n                                            min_delt=0.001,\n                                            min_lr=0.00001)\n\nearly_stopping = EarlyStopping(monitor = \"val_loss\",\n                               patience = 20,\n                               verbose = 1,\n                               mode = \"min\"\n                              )\n\nmodel_save = ModelCheckpoint(filepath=\"/kaggle/working/\", \n                             monitor = \"val_loss\",\n                             verbose=1, \n                             save_best_only=True,\n                             mode=\"min\")\n","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:32:04.543928Z","iopub.execute_input":"2023-02-15T17:32:04.544308Z","iopub.status.idle":"2023-02-15T17:32:06.382353Z","shell.execute_reply.started":"2023-02-15T17:32:04.544277Z","shell.execute_reply":"2023-02-15T17:32:06.381525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history = model.fit(\n #   train_generator,\n #   validation_data = validation_generator,\n #   epochs = EPOCHS,\n #   callbacks = [learning_rate_reduction, early_stopping, model_save],\n #   )","metadata":{"execution":{"iopub.status.busy":"2023-02-15T17:32:06.384014Z","iopub.execute_input":"2023-02-15T17:32:06.384533Z","iopub.status.idle":"2023-02-15T20:29:28.505407Z","shell.execute_reply.started":"2023-02-15T17:32:06.384465Z","shell.execute_reply":"2023-02-15T20:29:28.504281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('./dog_cat_model')","metadata":{"execution":{"iopub.status.busy":"2023-02-15T20:29:28.507021Z","iopub.execute_input":"2023-02-15T20:29:28.507365Z","iopub.status.idle":"2023-02-15T20:30:29.339969Z","shell.execute_reply.started":"2023-02-15T20:29:28.507327Z","shell.execute_reply":"2023-02-15T20:30:29.339217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model(\"/kaggle/input/trained-model/ModelCat_Doc\")","metadata":{"execution":{"iopub.status.busy":"2023-02-15T21:07:26.165355Z","iopub.execute_input":"2023-02-15T21:07:26.166073Z","iopub.status.idle":"2023-02-15T21:07:37.783316Z","shell.execute_reply.started":"2023-02-15T21:07:26.166035Z","shell.execute_reply":"2023-02-15T21:07:37.782544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef plot_hist(hist):\n    plt.plot(hist.history[\"acc\"])\n    plt.plot(hist.history[\"val_acc\"])\n    plt.title(\"model accuracy\")\n    plt.ylabel(\"accuracy\")\n    plt.xlabel(\"epoch\")\n    plt.legend([\"train\", \"validation\"], loc=\"upper left\")\n    plt.show()\n\n\n#plot_hist(history)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T20:30:29.350100Z","iopub.execute_input":"2023-02-15T20:30:29.350317Z","iopub.status.idle":"2023-02-15T20:30:29.604661Z","shell.execute_reply.started":"2023-02-15T20:30:29.350289Z","shell.execute_reply":"2023-02-15T20:30:29.603960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_gen = ImageDataGenerator(preprocessing_function=preprocess_input)\ntest_generator = test_gen.flow_from_dataframe(\n    test, \n    UNZIP_TEST, \n    x_col='filename',\n    #y_col=None,\n    class_mode= None,\n    target_size=(IMG_SIZE,IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    shuffle=False\n)\n\n\npredict = model.predict(test_generator, verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T21:07:48.934304Z","iopub.execute_input":"2023-02-15T21:07:48.934605Z","iopub.status.idle":"2023-02-15T21:08:30.075905Z","shell.execute_reply.started":"2023-02-15T21:07:48.934569Z","shell.execute_reply":"2023-02-15T21:08:30.075187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest[\"predict\"] = predict\ntest[\"label\"] = test[\"predict\"]\nresult = test[[\"id\", \"label\"]]","metadata":{"execution":{"iopub.status.busy":"2023-02-15T21:08:44.465092Z","iopub.execute_input":"2023-02-15T21:08:44.465358Z","iopub.status.idle":"2023-02-15T21:08:44.474639Z","shell.execute_reply.started":"2023-02-15T21:08:44.465326Z","shell.execute_reply":"2023-02-15T21:08:44.473867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-15T21:08:46.316789Z","iopub.execute_input":"2023-02-15T21:08:46.317070Z","iopub.status.idle":"2023-02-15T21:08:46.347923Z","shell.execute_reply.started":"2023-02-15T21:08:46.317037Z","shell.execute_reply":"2023-02-15T21:08:46.347229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}