{"cells":[{"metadata":{},"cell_type":"markdown","source":"\n$$\\textrm{LogLoss} = - \\frac{1}{n} \\sum_{i=1}^n \\left[ y_i \\log(\\hat{y}_i) + (1 - y_i) \\log(1 - \\hat{y}_i)\\right]$$   \n\n\nreferences：\nhttps://www.kaggle.com/aravrs/siim-isic-melanoma-classification-ext-cv\n\na discussion on how to hidden output visualizations:   \nhttps://www.kaggle.com/product-feedback/181116#1002307"},{"metadata":{"trusted":false},"cell_type":"code","source":"!pip install -q efficientnet","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"%matplotlib inline\n%config InlineBackend.figure_format = 'svg'\n\nimport warnings\nwarnings.filterwarnings('ignore')\nimport os, cv2, re, random, time, zipfile, gc\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\nfrom sklearn.model_selection import train_test_split\nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras import layers, models, optimizers\n#from keras.callbacks import EarlyStopping\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import RMSprop, Adam\nimport efficientnet.tfkeras as efn","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PATH = '/kaggle/input/dogs-vs-cats-redux-kernels-edition/'\ntrain_image_path = os.path.join(PATH, 'train.zip')\ntest_image_path = os.path.join(PATH, 'test.zip')\n\nwith zipfile.ZipFile(train_image_path,\"r\") as z:\n    z.extractall(\"./data\") # target dir\n    z.close()\n    \nwith zipfile.ZipFile(test_image_path,\"r\") as z:\n    z.extractall(\"./data\")\n    z.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"start = time.time() \n\nTRAIN_DIR = './data/train/'\nTEST_DIR = './data/test/'\n\ntrain_images = [TRAIN_DIR+i for i in os.listdir(TRAIN_DIR)] # use this for full dataset\ntest_images = [TEST_DIR+i for i in os.listdir(TEST_DIR)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"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) ]","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"train_images.sort(key=natural_keys) # 依据编号进行重新排序\ntest_images.sort(key=natural_keys)\n\ntrain_images = train_images[0:7500] +  train_images[17500:25000]  #抽样\nrandom.seed(558)\nrandom.shuffle(train_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"IMG_WIDTH = 128\nIMG_HEIGHT = 128\nx = []\nfor img in train_images:\n    x.append(cv2.resize(cv2.imread(img), \n                        (IMG_WIDTH, IMG_HEIGHT), \n                        interpolation=cv2.INTER_CUBIC))\n    \ntest = []\nfor img in test_images:\n    test.append(cv2.resize(cv2.imread(img), \n                        (IMG_WIDTH, IMG_HEIGHT), \n                        interpolation=cv2.INTER_CUBIC))\n    \nprint('The shape of train data is {}'.format(np.array(x).shape))\nprint('The shape of test data is {}'.format(np.array(test).shape))\n\n# extract label vector\nplt.rcParams['figure.facecolor'] = 'white'\ny = []\nfor i in train_images:\n    if 'dog' in i:\n        y.append(1)\n    elif 'cat' in i:\n        y.append(0)\nlen(y)\n\nx = np.array(x)\ny = np.array(y)\ntest = np.array(test)\nsns.countplot(y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"random.seed(558)\nplt.subplots(facecolor='white',figsize=(10,20))\nsample = random.choice(train_images)\nimage = load_img(sample)\nplt.subplot(131)\nplt.imshow(image)\n\nsample = random.choice(train_images)\nimage = load_img(sample)\nplt.subplot(132)\nplt.imshow(image)\n\nsample = random.choice(train_images)\nimage = load_img(sample)\nplt.subplot(133)\nplt.imshow(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.subplots(facecolor='white',figsize=(10,20))\nplt.subplot(131)\nplt.imshow(cv2.cvtColor(x[1024,:,:,:], cv2.COLOR_BGR2RGB))\nplt.subplot(132)\nplt.imshow(cv2.cvtColor(x[546,:,:,:], cv2.COLOR_BGR2RGB))\nplt.subplot(133)\nplt.imshow(cv2.cvtColor(x[742,:,:,:], cv2.COLOR_BGR2RGB))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=0.2, random_state=2020)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"model = models.Sequential()\n\n\nefnModel = efn.EfficientNetB7(weights = 'imagenet', \n                       input_shape = (IMG_WIDTH, IMG_HEIGHT,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(1, 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=8e-5) \n\nmodel.compile(loss='binary_crossentropy',\n              optimizer = opt2, \n              metrics = ['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"datagen = ImageDataGenerator(\n            rescale=1. / 255,            # 将数据放缩到0-1范围内\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 = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_gened(train_images,seed=320):\n    '''plot pictures after processing\n    '''\n    df = pd.DataFrame({'filename': train_images})\n    np.random.seed(seed)\n    vis_df = df.sample(n=1).reset_index(drop=True)\n    vis_df['category'] = '0'\n#vis_df\n    vis_gen = ImageDataGenerator(\n            rescale=1. / 255,             # Scale data to 0-1 range\n            rotation_range=40,            # The angle range of the image randomly rotated\n            width_shift_range=0.2,        # The range of image translation in the horizontal direction\n            height_shift_range=0.2,       # The range of image translation in the vertical direction\n            shear_range=0.2,              # Random staggered transformation angle\n            zoom_range=0.2,               # Random image zoom range\n            horizontal_flip=True,         # Randomly flip half of the image horizontally\n            fill_mode='nearest')          # How to fill in newly created pixels\n\n    vis_gen0 = vis_gen.flow_from_dataframe(vis_df,\n                                       x_col='filename',\n                                       y_col='category',\n                                       target_size=(IMG_WIDTH, IMG_HEIGHT),\n                                       batch_size = 16)\n    plt.rcParams['figure.facecolor'] = 'white'\n    plt.figure(figsize=(8, 8))\n    for i in range(0, 9):\n        plt.subplot(3, 3, i+1)\n        for X_batch, Y_batch in vis_gen0:\n            image = X_batch[0]\n            plt.imshow(image)\n            break\n    plt.tight_layout()\n    plt.show()\n    \nplot_gened(train_images)    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"BATCH_SIZE = 16\ndatagen = datagen.flow(x_train, y_train, batch_size=BATCH_SIZE)\nval_datagen = val_datagen.flow(x_val, y_val, batch_size=BATCH_SIZE)\n\nearlystop1 = EarlyStopping(patience=5)\nearlystop2 = ReduceLROnPlateau(monitor = 'val_accuracy', min_lr = 0.001, \n                               patience = 5, mode = 'min', \n                               verbose = 1)\n\nhistory = model.fit(datagen, \n                    steps_per_epoch=45,\n                    epochs=20,\n                    validation_data=val_datagen,\n                    callbacks=[earlystop1, earlystop2],\n                    validation_steps=25)\n#model.save('dogs_cats_efficientnetb7.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"<!---\n```\ntrain_loss = history.history['loss']\nval_loss = history.history['val_loss']\n\ntrain_acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nepochs = range(1, len(train_acc) + 1)\n\n# train and val acc\nplt.plot(epochs, train_acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Val acc')\nplt.title('Training and Validation accuracy')\nplt.legend()\n\nepochs = range(1, len(train_loss) + 1)\n\n# train and val loss\nplt.figure()\nplt.plot(epochs, train_loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Val loss')\nplt.title('Training and Validation loss')\nplt.legend()\n\nplt.show()\n```\n--->"},{"metadata":{"trusted":false},"cell_type":"code","source":"plt.rcParams['figure.facecolor'] = 'white'\nmodel_loss = pd.DataFrame(history.history)\nmodel_loss.head()\nmodel_loss[['accuracy','val_accuracy']].plot(ylim=[0,1]);\nmodel_loss[['loss','val_loss']].plot(ylim=[0,1]);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"[why log loss is nan?](https://www.kaggle.com/c/jigsaw-toxic-comment-classification-challenge/discussion/48701)"},{"metadata":{"trusted":true},"cell_type":"code","source":"x_val = x_val.astype('float32') / 255\nval_preds = model.predict(x_val)\nprint(val_preds.ravel().dtype)\nval_preds_class = np.where(val_preds.ravel() > 0.5, 1, 0) \n\nprint('Out of Fold Accuracy is {:.5}'.format(accuracy_score(y_val, val_preds_class)))\nprint('Out of Fold log loss is {:.5}'.format(log_loss(y_val, val_preds.ravel() \\\n                                                      .astype('float64'))))","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"test = test.astype('float32') / 255\ntest_pred = model.predict(test)\nsubmission = pd.DataFrame({'id': range(1, len(test_images) + 1), 'label': test_pred.ravel()})\nsubmission.to_csv('submission.csv', index = False)\nprint('This program costs {:.2f} seconds'.format(time.time()-start))\nsubmission","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"# remove all imgs unzipped at /data folder\n!rm -rf /kaggle/working/data/ ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}