{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras import layers,models\nfrom keras.preprocessing.image import img_to_array\nimport warnings\nwarnings.filterwarnings('always')\nwarnings.filterwarnings('ignore')\nimport os,shutil\nfrom sklearn.model_selection import train_test_split\nfrom keras import applications\nfrom keras.preprocessing.image import image\nfrom keras import layers,models,optimizers\nimport math","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_dir=\"../input/aptos2019-blindness-detection/train_images\"\ntest_images_dir=\"../input/aptos2019-blindness-detection/test_images\"\ntrain_df=pd.read_csv(\"../input/aptos2019-blindness-detection/train.csv\")\ntest_df=pd.read_csv(\"../input/aptos2019-blindness-detection/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.diagnosis.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.hist()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow \nfrom tensorflow.python.keras.applications import ResNet50, InceptionV3, Xception\nprint(os.listdir((\"../input/keras-pretrained-models/\")))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nIMG_SIZE=256\nmodel_inception_v3 = InceptionV3(\n    weights=\"../input/keras-pretrained-models/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5\", \n    include_top=False, \n    input_tensor=tf.keras.layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_inception_v3.trainable=False\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_inception_v3.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=tf.keras.models.Sequential()\nmodel.add(model_inception_v3)\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(256,activation='relu',input_dim=6*6*2048))\nmodel.add(tf.keras.layers.Dropout(0.4))\nmodel.add(tf.keras.layers.Dense(5,activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size=32\nseed=666\n\ntrain_df.id_code=train_df.id_code.apply(lambda x :x+'.png')\ntest_df.id_code=test_df.id_code.apply(lambda x :x+'.png')\ntrain_df['diagnosis']=train_df['diagnosis'].astype(str)\nx_train,x_val=train_test_split(train_df,test_size=0.2,random_state=seed)\ntrain_datagen=image.ImageDataGenerator(rescale=1./255,\n                                       horizontal_flip=True,rotation_range=40,\n                                       width_shift_range=0.2,shear_range=0.2,\n                                      zoom_range=0.2,fill_mode='nearest')\n\nval_datagen=image.ImageDataGenerator(rescale=1./255)\n    \ntest_datagen=image.ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    dataframe=x_train, \n    directory=train_images_dir,\n    x_col='id_code',\n    y_col='diagnosis',\n    target_size=(IMG_SIZE,IMG_SIZE),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=batch_size,\n    seed=seed)\nvalidation_generator = val_datagen.flow_from_dataframe(\n    dataframe=x_val, \n    directory=train_images_dir,\n    x_col='id_code',\n    y_col='diagnosis',\n    target_size=(IMG_SIZE,IMG_SIZE),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=batch_size,\n    shuffle=False,\n    seed=seed)\ntest_generator=test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    directory=test_images_dir,\n    x_col='id_code',\n    y_col=None,\n    target_size=(IMG_SIZE,IMG_SIZE),\n    color_mode='rgb',\n    class_mode=None,\n    batch_size=batch_size,\n    shuffle=False,\n    seed=seed)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"callback_list=[tf.keras.callbacks.ReduceLROnPlateau\n               (monitor='val_loss',\n                factor=0.1,\n                patience=5)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# from keras.engine.topology import Layer\n# class MixUpSoftmaxLoss(Layer):\n#     def __init__(self, crit, reduction='mean'):\n#         super().__init__()\n#         self.crit = crit\n#         setattr(self.crit, 'reduction', 'none')\n#         self.reduction = reduction\n\n#     def forward(self, output, target):\n#         if len(target.size()) == 2:\n#             loss1 = self.crit(output, target[:, 0].long())\n#             loss2 = self.crit(output, target[:, 1].long())\n#             lambda_ = target[:, 2]\n#             d = (loss1 * lambda_ + loss2 * (1-lambda_)).mean()\n#         else:\n#             # This handles the cases without MixUp for backward compatibility\n#             d = self.crit(output, target)\n#         if self.reduction == 'mean':\n#             return d.mean()\n#         elif self.reduction == 'sum':\n#             return d.sum()\n#         return d","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=tf.train.RMSPropOptimizer(2e-4),\n             loss='categorical_crossentropy',\n             metrics=['acc'])\nhistory=model.fit_generator(train_generator,  \n                steps_per_epoch=math.ceil(len(x_train)/batch_size),\n                 epochs=20,\n                 callbacks=callback_list,\n                 validation_data=validation_generator,\n                 validation_steps=32\n                         )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# acc=history.history['acc']\n# val_acc=history.history['val_acc']\n# loss=history.history['loss']\n# val_loss=history.history['val_loss']\n\n# epochs=range(1,len(acc)+1)\n\n# plt.plot(epochs,acc,'bo',label='training acc')\n# plt.plot(epochs,val_acc,'r',label='val acc')\n# plt.title('training and validation accuracy')\n# plt.legend()\n\n# plt.figure()\n\n# plt.plot(epochs,loss,'bo',label='training loss')\n# plt.plot(epochs,val_loss,'r',label='val loss')\n# plt.title('training and validation loss')\n# plt.legend()\n# plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm\ntta_steps = 10\nstep=math.ceil(len(test_df)/batch_size)\npreds_tta=[]\nfor i in tqdm(range(tta_steps)):\n    test_generator.reset()\n    preds = model.predict_generator(generator=test_generator,steps =step)\n    preds_tta.append(preds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_mean = np.mean(preds_tta, axis=0)\npred_argmax = np.argmax(pred_mean, axis=1)\ntest_df['diagnosis']=pred_argmax.astype(int)\ntest_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.diagnosis.value_counts()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}