{"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":"!pip install -q efficientnet","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-23T20:55:03.389436Z","iopub.execute_input":"2022-02-23T20:55:03.390156Z","iopub.status.idle":"2022-02-23T20:55:12.798239Z","shell.execute_reply.started":"2022-02-23T20:55:03.390012Z","shell.execute_reply":"2022-02-23T20:55:12.797394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers, models, optimizers\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nimport efficientnet.tfkeras as efn\n\nimport os, cv2, re, random, time, gc, glob, tqdm\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import log_loss, accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:55:12.800402Z","iopub.execute_input":"2022-02-23T20:55:12.800678Z","iopub.status.idle":"2022-02-23T20:55:19.220034Z","shell.execute_reply.started":"2022-02-23T20:55:12.800640Z","shell.execute_reply":"2022-02-23T20:55:19.218492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start = time.time() \n\nTRAIN_DIR = '../input/dog-vs-cat-classification/train/train'\nTEST_DIR = '../input/dog-vs-cat-classification/test/test'\n\ntrain_images = [f for f in glob.glob(TRAIN_DIR + \"/*/*.jpg\")]\ntest_images = [f for f in glob.glob(TEST_DIR + \"/*.jpg\")]","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:55:19.221390Z","iopub.execute_input":"2022-02-23T20:55:19.221630Z","iopub.status.idle":"2022-02-23T20:55:20.076721Z","shell.execute_reply.started":"2022-02-23T20:55:19.221599Z","shell.execute_reply":"2022-02-23T20:55:20.075913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 128\nBATCH_SIZE = 32\nEPOCHS = 2","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:20.037780Z","iopub.execute_input":"2022-02-23T20:57:20.038049Z","iopub.status.idle":"2022-02-23T20:57:20.044407Z","shell.execute_reply.started":"2022-02-23T20:57:20.038017Z","shell.execute_reply":"2022-02-23T20:57:20.043716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def txt_dig(text):\n    '''Input string, if it is a number, output the number, \n       if not, output the original string'''\n    return int(text) if text.isdigit() else text\n\ndef natural_keys(text):\n    '''Enter a string, separate the number from the text, \n       and convert the number string to int'''\n    return [ txt_dig(c) for c in re.split('(\\d+)', text) ]","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:20.442334Z","iopub.execute_input":"2022-02-23T20:57:20.442847Z","iopub.status.idle":"2022-02-23T20:57:20.447617Z","shell.execute_reply.started":"2022-02-23T20:57:20.442809Z","shell.execute_reply":"2022-02-23T20:57:20.446913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.sort(key=natural_keys) \ntest_images.sort(key=natural_keys)\n\nrandom.seed(2022)\nrandom.shuffle(train_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:20.809695Z","iopub.execute_input":"2022-02-23T20:57:20.810398Z","iopub.status.idle":"2022-02-23T20:57:21.081313Z","shell.execute_reply.started":"2022-02-23T20:57:20.810355Z","shell.execute_reply":"2022-02-23T20:57:21.080471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.DataFrame({'image_path': [f for f in train_images], 'label': ['dog' if \"dog\" in os.path.basename(f) else 'cat' for f in train_images]})\ntrain_csv['split'] = 'train'","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:21.147295Z","iopub.execute_input":"2022-02-23T20:57:21.147530Z","iopub.status.idle":"2022-02-23T20:57:21.185710Z","shell.execute_reply.started":"2022-02-23T20:57:21.147501Z","shell.execute_reply":"2022-02-23T20:57:21.185010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:21.499048Z","iopub.execute_input":"2022-02-23T20:57:21.499573Z","iopub.status.idle":"2022-02-23T20:57:21.511814Z","shell.execute_reply.started":"2022-02-23T20:57:21.499538Z","shell.execute_reply":"2022-02-23T20:57:21.510967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\n\n\nefnModel = efn.EfficientNetB7(weights = 'imagenet', \n                       input_shape = (IMG_SIZE, IMG_SIZE,3), \n                       include_top = False)\nmodel.add(efnModel)\nmodel.add(layers.GlobalAveragePooling2D())\n#model.add(layers.Dense(512, activation= 'relu'))\n#model.add(layers.Dropout(0.3))\nmodel.add(layers.Dense(2, activation='sigmoid'))\n\n# decay is included for backward compatibility to allow time inverse decay of lr\nopt1 = RMSprop(lr=1e-5, decay=1e-6)\nopt2 = Adam(lr=1e-4) \n\nmodel.compile(loss='binary_crossentropy',\n              optimizer = opt2, \n              metrics = ['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:22.015202Z","iopub.execute_input":"2022-02-23T20:57:22.015651Z","iopub.status.idle":"2022-02-23T20:57:29.533558Z","shell.execute_reply.started":"2022-02-23T20:57:22.015614Z","shell.execute_reply":"2022-02-23T20:57:29.532756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n            rescale=1./255,            \n            rotation_range=40,           \n            width_shift_range=0.2,       \n            height_shift_range=0.2,      \n            shear_range=0.2,            \n            zoom_range=0.2,             \n            horizontal_flip=True,        \n            fill_mode='nearest')\n\nval_datagen = None\n# val_datagen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:29.535056Z","iopub.execute_input":"2022-02-23T20:57:29.535571Z","iopub.status.idle":"2022-02-23T20:57:29.541366Z","shell.execute_reply.started":"2022-02-23T20:57:29.535531Z","shell.execute_reply":"2022-02-23T20:57:29.540573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = datagen.flow_from_dataframe(\n    train_csv[train_csv['split'] == 'train'],\n    target_size=(IMG_SIZE, IMG_SIZE),\n    class_mode='categorical',\n    classes = {'dog' : 0, 'cat' : 1},\n    x_col='image_path', \n    y_col='label',\n    batch_size=BATCH_SIZE, \n    shuffle=True,\n    seed=2022,\n)\n\n# val_datagen = val_datagen.flow_from_dataframe(\n#     train_csv[train_csv['split']=='val]',\n#     target_size=(IMG_SIZE, IMG_SIZE),\n#     class_mode='categorical',\n#     classes = {'dog' : 0, 'cat' : 1},\n#     x_col='image_path', \n#     y_col='label',\n#     batch_size=BATCH_SIZE, \n#     shuffle=False,\n#     seed=2022,\n# )\n\n# earlystop1 = EarlyStopping(patience=3, monitor = 'val_accuracy' if val_datagen is not None else 'accuracy', mode = 'max')\n# earlystop2 = ReduceLROnPlateau(monitor = 'val_accuracy' if val_datagen is not None else 'accuracy', min_lr = 0.001, \n#                                patience = 3, mode = 'max', \n#                                verbose = 1)\n\nhistory = model.fit(datagen,\n                    epochs=EPOCHS,\n#                     validation_data=val_datagen,\n#                     callbacks=[earlystop1, earlystop2],\n                   )\n\n#model.save('dogs_cats_efficientnetb7.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:57:29.542500Z","iopub.execute_input":"2022-02-23T20:57:29.542775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.rcParams['figure.facecolor'] = 'white'\nmodel_loss = pd.DataFrame(history.history)\nmodel_loss.head()\nmodel_loss[['accuracy']].plot(ylim=[0,1]);\nmodel_loss[['loss']].plot(ylim=[0,1]);","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.599302Z","iopub.status.idle":"2022-02-23T20:56:59.599596Z","shell.execute_reply.started":"2022-02-23T20:56:59.599438Z","shell.execute_reply":"2022-02-23T20:56:59.599458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id' : [os.path.basename(f) for f in test_images],'image_path': [f for f in test_images]})","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.600853Z","iopub.status.idle":"2022-02-23T20:56:59.601248Z","shell.execute_reply.started":"2022-02-23T20:56:59.601020Z","shell.execute_reply":"2022-02-23T20:56:59.601040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testgen = ImageDataGenerator(\n            rescale=1. / 255,\n            )\ntestgen = testgen.flow_from_dataframe(\n    submission,\n    target_size=(IMG_SIZE, IMG_SIZE),\n    class_mode=None,\n    x_col='image_path', \n    y_col=None,\n    batch_size=BATCH_SIZE, \n    shuffle=False,\n    seed=2022,\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.602312Z","iopub.status.idle":"2022-02-23T20:56:59.602879Z","shell.execute_reply.started":"2022-02-23T20:56:59.602647Z","shell.execute_reply":"2022-02-23T20:56:59.602674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(testgen, verbose=1)\nsubmission['labels'] = np.argmax(preds, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.604822Z","iopub.status.idle":"2022-02-23T20:56:59.605480Z","shell.execute_reply.started":"2022-02-23T20:56:59.605232Z","shell.execute_reply":"2022-02-23T20:56:59.605258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.drop('image_path', inplace=True, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.606726Z","iopub.status.idle":"2022-02-23T20:56:59.607582Z","shell.execute_reply.started":"2022-02-23T20:56:59.607351Z","shell.execute_reply":"2022-02-23T20:56:59.607375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index = False)\nprint('This program costs {:.2f} seconds'.format(time.time()-start))","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.608642Z","iopub.status.idle":"2022-02-23T20:56:59.609477Z","shell.execute_reply.started":"2022-02-23T20:56:59.609236Z","shell.execute_reply":"2022-02-23T20:56:59.609260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T20:56:59.610608Z","iopub.status.idle":"2022-02-23T20:56:59.611437Z","shell.execute_reply.started":"2022-02-23T20:56:59.611200Z","shell.execute_reply":"2022-02-23T20:56:59.611224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}