{"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":1,"outputs":[{"output_type":"stream","text":"['diabetic-retinopathy-detection', 'labels']\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import glob\nfilelist = glob.glob('../input/diabetic-retinopathy-detection/*.jpeg')\nnp.size(filelist)","execution_count":2,"outputs":[{"output_type":"execute_result","execution_count":2,"data":{"text/plain":"1000"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# don't run this cell, used for augmentation tests\n#os.mkdir('/kaggle/working/preview')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# don't run this cell, used for augmentation tests\n'''\nimg=mpimg.imread(filelist[1])\nimgplot = plt.imshow(img)\nplt.show()\n'''","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# don't run this cell, used for augmentation tests\n'''\nfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img\n\ndatagen = ImageDataGenerator(\n        rescale=1./255,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.2,\n        fill_mode = \"nearest\"\n)\n\nfile = filelist[1]\n\nfnm = file.replace(\"../input/\",\"\")\nfnm = fnm.replace(\".jpeg\",\"\")\nfnm\n\nimg = load_img(file)  # this is a PIL image\nx = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)\nx = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1, 3, 150, 150)\n\n# the .flow() command below generates batches of randomly transformed images\n# and saves the results to the `preview/` directory\ni = 0\nfor batch in datagen.flow(x, batch_size=1,\n                          save_to_dir='/kaggle/working/preview',save_prefix= fnm, save_format='jpeg'):\n    i += 1\n    if i > 10:\n        break  # otherwise the generator would loop indefinitely\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# don't run this cell, used for augmentation tests\n'''\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\naug = glob.glob('/kaggle/working/preview/*.jpeg')\nfor file in aug:\n    img=mpimg.imread(file)\n    imgplot = plt.imshow(img)\n    plt.show()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainLabels = pd.read_csv(\"../input/labels/trainLabels.csv\")\nprint(trainLabels.head())","execution_count":3,"outputs":[{"output_type":"stream","text":"      image  level\n0   10_left      0\n1  10_right      0\n2   13_left      0\n3  13_right      0\n4   15_left      1\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# don't run this cell, used for rescale check\n'''\nfile = filelist[4]\ntmp = cv2.imread(file)\ntmp = np.array(tmp)\ntmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\nfrom PIL import Image\nimg = Image.fromarray(tmp, 'RGB')\nimg.save('new.png')\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimg=mpimg.imread('new.png')\nimgplot = plt.imshow(img)\nplt.show()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# all dataset\n'''\nimport cv2\nimg_data = []\nimg_label = []\nfor file in filelist:\n    tmp = cv2.imread(file)\n    tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n    #tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)\n    #img_data.append(np.array(tmp).flatten())\n    img_data.append(np.array(tmp))\n    #img_data.append(file)\n    tmpfn = file\n    ##tmpfn = tmpfn.replace(\"../input/bloodvessel/bloodvesselextraction/BloodVesselExtraction/\",\"\")\n    tmpfn = tmpfn.replace(\"../input/diabetic-retinopathy-detection/\",\"\")\n    tmpfn = tmpfn.replace(\".jpeg\",\"\")\n    img_label.append(trainLabels.loc[trainLabels.image==tmpfn, 'level'].values[0])\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# 0 and 2 excluded\n'''\nimport cv2\nimg_data = []\nimg_label = []\nfor file in filelist:\n    tmpfn = file\n    ##tmpfn = tmpfn.replace(\"../input/bloodvessel/bloodvesselextraction/BloodVesselExtraction/\",\"\")\n    tmpfn = tmpfn.replace(\"../input/diabetic-retinopathy-detection/\",\"\")\n    tmpfn = tmpfn.replace(\".jpeg\",\"\")\n    label = trainLabels.loc[trainLabels.image==tmpfn, 'level'].values[0]\n    if label != 0 and label != 2:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        #tmp = cv2.cvtColor(tmp, cv2.COLOR_BGR2GRAY)\n        #img_data.append(np.array(tmp).flatten())\n        img_data.append(np.array(tmp))\n        if label == 1:\n            img_label.append(0)\n        if label == 3:\n            img_label.append(1)\n        if label == 4:\n            img_label.append(2)\n'''   ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# find count of each label\nlab = []\nfor file in filelist:\n    tmpfn = file\n    tmpfn = tmpfn.replace(\"../input/diabetic-retinopathy-detection/\",\"\")\n    tmpfn = tmpfn.replace(\".jpeg\",\"\")\n    lab.append(trainLabels.loc[trainLabels.image==tmpfn, 'level'].values[0])  \n    \nprint(lab.count(0))\nprint(lab.count(1))\nprint(lab.count(2))\nprint(lab.count(3))\nprint(lab.count(4))","execution_count":4,"outputs":[{"output_type":"stream","text":"739\n59\n148\n28\n26\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"# balanced dataset\nimport cv2\nimg_data = []\nimg_label = []\nzero = 0\none = 0\ntwo = 0\nthree = 0\nfour = 0\nlimit = 26\nfor file in filelist:\n    tmpfn = file\n    tmpfn = tmpfn.replace(\"../input/diabetic-retinopathy-detection/\",\"\")\n    tmpfn = tmpfn.replace(\".jpeg\",\"\")\n    label = trainLabels.loc[trainLabels.image==tmpfn, 'level'].values[0]\n    if label == 0 and zero < limit:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        img_data.append(np.array(tmp))\n        img_label.append(label)\n        zero+=1\n    elif label == 1 and one < limit:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        img_data.append(np.array(tmp))\n        img_label.append(label)\n        one+=1\n    elif label == 2 and two < limit:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        img_data.append(np.array(tmp))\n        img_label.append(label)\n        two+=1\n    elif label == 3 and three < limit:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        img_data.append(np.array(tmp))\n        img_label.append(label)\n        three+=1\n    elif label == 4 and four < limit:\n        tmp = cv2.imread(file)\n        tmp = cv2.resize(tmp,(512, 512), interpolation = cv2.INTER_CUBIC)\n        img_data.append(np.array(tmp))\n        img_label.append(label)\n        four+=1","execution_count":5,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(img_data))\nset(img_label)","execution_count":6,"outputs":[{"output_type":"stream","text":"130\n","name":"stdout"},{"output_type":"execute_result","execution_count":6,"data":{"text/plain":"{0, 1, 2, 3, 4}"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"whole_data = np.array(img_data)\nwhole_data.shape","execution_count":7,"outputs":[{"output_type":"execute_result","execution_count":7,"data":{"text/plain":"(130, 512, 512, 3)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nnum_classes = 5\nwhole_labels = keras.utils.to_categorical(img_label, num_classes)","execution_count":8,"outputs":[{"output_type":"stream","text":"Using TensorFlow backend.\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(whole_data, whole_labels, test_size = 0.2, random_state = 0)","execution_count":9,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.layers.core import Layer\nimport keras.backend as K\nimport tensorflow as tf\n\nfrom keras.models import Model\nfrom keras.layers import Conv2D, MaxPool2D,  \\\n    Dropout, Dense, Input, concatenate,      \\\n    GlobalAveragePooling2D, AveragePooling2D,\\\n    Flatten\n\nimport cv2 \nimport numpy as np \nfrom keras.datasets import cifar10 \nfrom keras import backend as K \nfrom keras.utils import np_utils\n\nimport math \nfrom keras.optimizers import SGD \nfrom keras.callbacks import LearningRateScheduler\n\ndef inception_module(x,\n                     filters_1x1,\n                     filters_3x3_reduce,\n                     filters_3x3,\n                     filters_5x5_reduce,\n                     filters_5x5,\n                     filters_pool_proj,\n                     name=None):\n    \n    conv_1x1 = Conv2D(filters_1x1, (1, 1), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(x)\n    \n    conv_3x3 = Conv2D(filters_3x3_reduce, (1, 1), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(x)\n    conv_3x3 = Conv2D(filters_3x3, (3, 3), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(conv_3x3)\n\n    conv_5x5 = Conv2D(filters_5x5_reduce, (1, 1), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(x)\n    conv_5x5 = Conv2D(filters_5x5, (5, 5), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(conv_5x5)\n\n    pool_proj = MaxPool2D((3, 3), strides=(1, 1), padding='same')(x)\n    pool_proj = Conv2D(filters_pool_proj, (1, 1), padding='same', activation='relu', kernel_initializer=kernel_init, bias_initializer=bias_init)(pool_proj)\n\n    output = concatenate([conv_1x1, conv_3x3, conv_5x5, pool_proj], axis=3, name=name)\n    \n    return output\n\nkernel_init = keras.initializers.glorot_uniform()\nbias_init = keras.initializers.Constant(value=0.2)\n\ninput_layer = Input(shape=(512, 512, 3))\n\nx = Conv2D(64, (7, 7), padding='same', strides=(2, 2), activation='relu', name='conv_1_7x7/2', kernel_initializer=kernel_init, bias_initializer=bias_init)(input_layer)\nx = MaxPool2D((3, 3), padding='same', strides=(2, 2), name='max_pool_1_3x3/2')(x)\nx = Conv2D(64, (1, 1), padding='same', strides=(1, 1), activation='relu', name='conv_2a_3x3/1')(x)\nx = Conv2D(192, (3, 3), padding='same', strides=(1, 1), activation='relu', name='conv_2b_3x3/1')(x)\nx = MaxPool2D((3, 3), padding='same', strides=(2, 2), name='max_pool_2_3x3/2')(x)\n\nx = inception_module(x,\n                     filters_1x1=64,\n                     filters_3x3_reduce=96,\n                     filters_3x3=128,\n                     filters_5x5_reduce=16,\n                     filters_5x5=32,\n                     filters_pool_proj=32,\n                     name='inception_3a')\n\nx = inception_module(x,\n                     filters_1x1=128,\n                     filters_3x3_reduce=128,\n                     filters_3x3=192,\n                     filters_5x5_reduce=32,\n                     filters_5x5=96,\n                     filters_pool_proj=64,\n                     name='inception_3b')\n\nx = MaxPool2D((3, 3), padding='same', strides=(2, 2), name='max_pool_3_3x3/2')(x)\n\nx = inception_module(x,\n                     filters_1x1=192,\n                     filters_3x3_reduce=96,\n                     filters_3x3=208,\n                     filters_5x5_reduce=16,\n                     filters_5x5=48,\n                     filters_pool_proj=64,\n                     name='inception_4a')\n\n\n'''\nx1 = AveragePooling2D((5, 5), strides=3)(x)\nx1 = Conv2D(128, (1, 1), padding='same', activation='relu')(x1)\nx1 = Flatten()(x1)\nx1 = Dense(6400, activation='relu')(x1)\nx1 = Dense(3200, activation='relu')(x1)\nx1 = Dense(1600, activation='relu')(x1)\nx1 = Dense(1024, activation='relu')(x1)\nx1 = Dense(800, activation='relu')(x1)\nx1 = Dense(400, activation='relu')(x1)\nx1 = Dense(200, activation='relu')(x1)\nx1 = Dropout(0.7)(x1)\nx1 = Dense(5, activation='softmax', name='auxilliary_output_1')(x1)\n'''\n\nx = inception_module(x,\n                     filters_1x1=160,\n                     filters_3x3_reduce=112,\n                     filters_3x3=224,\n                     filters_5x5_reduce=24,\n                     filters_5x5=64,\n                     filters_pool_proj=64,\n                     name='inception_4b')\n\nx = inception_module(x,\n                     filters_1x1=128,\n                     filters_3x3_reduce=128,\n                     filters_3x3=256,\n                     filters_5x5_reduce=24,\n                     filters_5x5=64,\n                     filters_pool_proj=64,\n                     name='inception_4c')\n\nx = inception_module(x,\n                     filters_1x1=112,\n                     filters_3x3_reduce=144,\n                     filters_3x3=288,\n                     filters_5x5_reduce=32,\n                     filters_5x5=64,\n                     filters_pool_proj=64,\n                     name='inception_4d')\n\n'''\nx2 = AveragePooling2D((5, 5), strides=3)(x)\nx2 = Conv2D(128, (1, 1), padding='same', activation='relu')(x2)\nx2 = Flatten()(x2)\nx2 = Dense(6400, activation='relu')(x2)\nx2 = Dense(3200, activation='relu')(x2)\nx2 = Dense(1600, activation='relu')(x2)\nx2 = Dense(1024, activation='relu')(x2)\nx2 = Dense(800, activation='relu')(x2)\nx2 = Dense(400, activation='relu')(x2)\nx2 = Dense(200, activation='relu')(x2)\nx2 = Dropout(0.7)(x2)\nx2 = Dense(5, activation='softmax', name='auxilliary_output_2')(x2)\n'''\n\nx = inception_module(x,\n                     filters_1x1=256,\n                     filters_3x3_reduce=160,\n                     filters_3x3=320,\n                     filters_5x5_reduce=32,\n                     filters_5x5=128,\n                     filters_pool_proj=128,\n                     name='inception_4e')\n\nx = MaxPool2D((3, 3), padding='same', strides=(2, 2), name='max_pool_4_3x3/2')(x)\n\nx = inception_module(x,\n                     filters_1x1=256,\n                     filters_3x3_reduce=160,\n                     filters_3x3=320,\n                     filters_5x5_reduce=32,\n                     filters_5x5=128,\n                     filters_pool_proj=128,\n                     name='inception_5a')\n\nx = inception_module(x,\n                     filters_1x1=384,\n                     filters_3x3_reduce=192,\n                     filters_3x3=384,\n                     filters_5x5_reduce=48,\n                     filters_5x5=128,\n                     filters_pool_proj=128,\n                     name='inception_5b')\n\nx = GlobalAveragePooling2D(name='avg_pool_5_3x3/1')(x)\nx = Dense(6400, activation='relu')(x)\nx = Dense(3200, activation='relu')(x)\nx = Dense(1600, activation='relu')(x)\nx = Dense(1024, activation='relu')(x)\nx = Dense(800, activation='relu')(x)\nx = Dense(400, activation='relu')(x)\nx = Dense(200, activation='relu')(x)\nx = Dropout(0.4)(x)\n\nx = Dense(num_classes, activation='softmax', name='output')(x)\n\nmodel = Model(input_layer, x, name='inception_v1')\nprint(model.summary())","execution_count":10,"outputs":[{"output_type":"stream","text":"WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\nInstructions for updating:\nColocations handled automatically by placer.\nWARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\nInstructions for updating:\nPlease use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n__________________________________________________________________________________________________\nLayer (type)                    Output Shape         Param #     Connected to                     \n==================================================================================================\ninput_1 (InputLayer)            (None, 512, 512, 3)  0                                            \n__________________________________________________________________________________________________\nconv_1_7x7/2 (Conv2D)           (None, 256, 256, 64) 9472        input_1[0][0]                    \n__________________________________________________________________________________________________\nmax_pool_1_3x3/2 (MaxPooling2D) (None, 128, 128, 64) 0           conv_1_7x7/2[0][0]               \n__________________________________________________________________________________________________\nconv_2a_3x3/1 (Conv2D)          (None, 128, 128, 64) 4160        max_pool_1_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv_2b_3x3/1 (Conv2D)          (None, 128, 128, 192 110784      conv_2a_3x3/1[0][0]              \n__________________________________________________________________________________________________\nmax_pool_2_3x3/2 (MaxPooling2D) (None, 64, 64, 192)  0           conv_2b_3x3/1[0][0]              \n__________________________________________________________________________________________________\nconv2d_2 (Conv2D)               (None, 64, 64, 96)   18528       max_pool_2_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_4 (Conv2D)               (None, 64, 64, 16)   3088        max_pool_2_3x3/2[0][0]           \n__________________________________________________________________________________________________\nmax_pooling2d_1 (MaxPooling2D)  (None, 64, 64, 192)  0           max_pool_2_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_1 (Conv2D)               (None, 64, 64, 64)   12352       max_pool_2_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_3 (Conv2D)               (None, 64, 64, 128)  110720      conv2d_2[0][0]                   \n__________________________________________________________________________________________________\nconv2d_5 (Conv2D)               (None, 64, 64, 32)   12832       conv2d_4[0][0]                   \n__________________________________________________________________________________________________\nconv2d_6 (Conv2D)               (None, 64, 64, 32)   6176        max_pooling2d_1[0][0]            \n__________________________________________________________________________________________________\ninception_3a (Concatenate)      (None, 64, 64, 256)  0           conv2d_1[0][0]                   \n                                                                 conv2d_3[0][0]                   \n                                                                 conv2d_5[0][0]                   \n                                                                 conv2d_6[0][0]                   \n__________________________________________________________________________________________________\nconv2d_8 (Conv2D)               (None, 64, 64, 128)  32896       inception_3a[0][0]               \n__________________________________________________________________________________________________\nconv2d_10 (Conv2D)              (None, 64, 64, 32)   8224        inception_3a[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_2 (MaxPooling2D)  (None, 64, 64, 256)  0           inception_3a[0][0]               \n__________________________________________________________________________________________________\nconv2d_7 (Conv2D)               (None, 64, 64, 128)  32896       inception_3a[0][0]               \n__________________________________________________________________________________________________\nconv2d_9 (Conv2D)               (None, 64, 64, 192)  221376      conv2d_8[0][0]                   \n__________________________________________________________________________________________________\nconv2d_11 (Conv2D)              (None, 64, 64, 96)   76896       conv2d_10[0][0]                  \n__________________________________________________________________________________________________\nconv2d_12 (Conv2D)              (None, 64, 64, 64)   16448       max_pooling2d_2[0][0]            \n__________________________________________________________________________________________________\ninception_3b (Concatenate)      (None, 64, 64, 480)  0           conv2d_7[0][0]                   \n                                                                 conv2d_9[0][0]                   \n                                                                 conv2d_11[0][0]                  \n                                                                 conv2d_12[0][0]                  \n__________________________________________________________________________________________________\nmax_pool_3_3x3/2 (MaxPooling2D) (None, 32, 32, 480)  0           inception_3b[0][0]               \n__________________________________________________________________________________________________\nconv2d_14 (Conv2D)              (None, 32, 32, 96)   46176       max_pool_3_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_16 (Conv2D)              (None, 32, 32, 16)   7696        max_pool_3_3x3/2[0][0]           \n__________________________________________________________________________________________________\nmax_pooling2d_3 (MaxPooling2D)  (None, 32, 32, 480)  0           max_pool_3_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_13 (Conv2D)              (None, 32, 32, 192)  92352       max_pool_3_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_15 (Conv2D)              (None, 32, 32, 208)  179920      conv2d_14[0][0]                  \n__________________________________________________________________________________________________\nconv2d_17 (Conv2D)              (None, 32, 32, 48)   19248       conv2d_16[0][0]                  \n__________________________________________________________________________________________________\nconv2d_18 (Conv2D)              (None, 32, 32, 64)   30784       max_pooling2d_3[0][0]            \n__________________________________________________________________________________________________\ninception_4a (Concatenate)      (None, 32, 32, 512)  0           conv2d_13[0][0]                  \n                                                                 conv2d_15[0][0]                  \n                                                                 conv2d_17[0][0]                  \n                                                                 conv2d_18[0][0]                  \n__________________________________________________________________________________________________\nconv2d_20 (Conv2D)              (None, 32, 32, 112)  57456       inception_4a[0][0]               \n__________________________________________________________________________________________________\nconv2d_22 (Conv2D)              (None, 32, 32, 24)   12312       inception_4a[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_4 (MaxPooling2D)  (None, 32, 32, 512)  0           inception_4a[0][0]               \n__________________________________________________________________________________________________\nconv2d_19 (Conv2D)              (None, 32, 32, 160)  82080       inception_4a[0][0]               \n__________________________________________________________________________________________________\nconv2d_21 (Conv2D)              (None, 32, 32, 224)  226016      conv2d_20[0][0]                  \n__________________________________________________________________________________________________\nconv2d_23 (Conv2D)              (None, 32, 32, 64)   38464       conv2d_22[0][0]                  \n__________________________________________________________________________________________________\nconv2d_24 (Conv2D)              (None, 32, 32, 64)   32832       max_pooling2d_4[0][0]            \n__________________________________________________________________________________________________\ninception_4b (Concatenate)      (None, 32, 32, 512)  0           conv2d_19[0][0]                  \n                                                                 conv2d_21[0][0]                  \n                                                                 conv2d_23[0][0]                  \n                                                                 conv2d_24[0][0]                  \n__________________________________________________________________________________________________\nconv2d_26 (Conv2D)              (None, 32, 32, 128)  65664       inception_4b[0][0]               \n__________________________________________________________________________________________________\nconv2d_28 (Conv2D)              (None, 32, 32, 24)   12312       inception_4b[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_5 (MaxPooling2D)  (None, 32, 32, 512)  0           inception_4b[0][0]               \n__________________________________________________________________________________________________\nconv2d_25 (Conv2D)              (None, 32, 32, 128)  65664       inception_4b[0][0]               \n__________________________________________________________________________________________________\nconv2d_27 (Conv2D)              (None, 32, 32, 256)  295168      conv2d_26[0][0]                  \n__________________________________________________________________________________________________\nconv2d_29 (Conv2D)              (None, 32, 32, 64)   38464       conv2d_28[0][0]                  \n__________________________________________________________________________________________________\nconv2d_30 (Conv2D)              (None, 32, 32, 64)   32832       max_pooling2d_5[0][0]            \n__________________________________________________________________________________________________\ninception_4c (Concatenate)      (None, 32, 32, 512)  0           conv2d_25[0][0]                  \n                                                                 conv2d_27[0][0]                  \n                                                                 conv2d_29[0][0]                  \n                                                                 conv2d_30[0][0]                  \n__________________________________________________________________________________________________\nconv2d_32 (Conv2D)              (None, 32, 32, 144)  73872       inception_4c[0][0]               \n__________________________________________________________________________________________________\nconv2d_34 (Conv2D)              (None, 32, 32, 32)   16416       inception_4c[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_6 (MaxPooling2D)  (None, 32, 32, 512)  0           inception_4c[0][0]               \n__________________________________________________________________________________________________\nconv2d_31 (Conv2D)              (None, 32, 32, 112)  57456       inception_4c[0][0]               \n__________________________________________________________________________________________________\nconv2d_33 (Conv2D)              (None, 32, 32, 288)  373536      conv2d_32[0][0]                  \n__________________________________________________________________________________________________\nconv2d_35 (Conv2D)              (None, 32, 32, 64)   51264       conv2d_34[0][0]                  \n__________________________________________________________________________________________________\nconv2d_36 (Conv2D)              (None, 32, 32, 64)   32832       max_pooling2d_6[0][0]            \n__________________________________________________________________________________________________\ninception_4d (Concatenate)      (None, 32, 32, 528)  0           conv2d_31[0][0]                  \n                                                                 conv2d_33[0][0]                  \n                                                                 conv2d_35[0][0]                  \n                                                                 conv2d_36[0][0]                  \n__________________________________________________________________________________________________\nconv2d_38 (Conv2D)              (None, 32, 32, 160)  84640       inception_4d[0][0]               \n__________________________________________________________________________________________________\nconv2d_40 (Conv2D)              (None, 32, 32, 32)   16928       inception_4d[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_7 (MaxPooling2D)  (None, 32, 32, 528)  0           inception_4d[0][0]               \n__________________________________________________________________________________________________\nconv2d_37 (Conv2D)              (None, 32, 32, 256)  135424      inception_4d[0][0]               \n__________________________________________________________________________________________________\nconv2d_39 (Conv2D)              (None, 32, 32, 320)  461120      conv2d_38[0][0]                  \n__________________________________________________________________________________________________\nconv2d_41 (Conv2D)              (None, 32, 32, 128)  102528      conv2d_40[0][0]                  \n__________________________________________________________________________________________________\nconv2d_42 (Conv2D)              (None, 32, 32, 128)  67712       max_pooling2d_7[0][0]            \n__________________________________________________________________________________________________\ninception_4e (Concatenate)      (None, 32, 32, 832)  0           conv2d_37[0][0]                  \n                                                                 conv2d_39[0][0]                  \n                                                                 conv2d_41[0][0]                  \n                                                                 conv2d_42[0][0]                  \n__________________________________________________________________________________________________\nmax_pool_4_3x3/2 (MaxPooling2D) (None, 16, 16, 832)  0           inception_4e[0][0]               \n__________________________________________________________________________________________________\nconv2d_44 (Conv2D)              (None, 16, 16, 160)  133280      max_pool_4_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_46 (Conv2D)              (None, 16, 16, 32)   26656       max_pool_4_3x3/2[0][0]           \n__________________________________________________________________________________________________\nmax_pooling2d_8 (MaxPooling2D)  (None, 16, 16, 832)  0           max_pool_4_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_43 (Conv2D)              (None, 16, 16, 256)  213248      max_pool_4_3x3/2[0][0]           \n__________________________________________________________________________________________________\nconv2d_45 (Conv2D)              (None, 16, 16, 320)  461120      conv2d_44[0][0]                  \n__________________________________________________________________________________________________\nconv2d_47 (Conv2D)              (None, 16, 16, 128)  102528      conv2d_46[0][0]                  \n__________________________________________________________________________________________________\nconv2d_48 (Conv2D)              (None, 16, 16, 128)  106624      max_pooling2d_8[0][0]            \n__________________________________________________________________________________________________\ninception_5a (Concatenate)      (None, 16, 16, 832)  0           conv2d_43[0][0]                  \n                                                                 conv2d_45[0][0]                  \n                                                                 conv2d_47[0][0]                  \n                                                                 conv2d_48[0][0]                  \n__________________________________________________________________________________________________\nconv2d_50 (Conv2D)              (None, 16, 16, 192)  159936      inception_5a[0][0]               \n__________________________________________________________________________________________________\nconv2d_52 (Conv2D)              (None, 16, 16, 48)   39984       inception_5a[0][0]               \n__________________________________________________________________________________________________\nmax_pooling2d_9 (MaxPooling2D)  (None, 16, 16, 832)  0           inception_5a[0][0]               \n__________________________________________________________________________________________________\nconv2d_49 (Conv2D)              (None, 16, 16, 384)  319872      inception_5a[0][0]               \n__________________________________________________________________________________________________\nconv2d_51 (Conv2D)              (None, 16, 16, 384)  663936      conv2d_50[0][0]                  \n__________________________________________________________________________________________________\nconv2d_53 (Conv2D)              (None, 16, 16, 128)  153728      conv2d_52[0][0]                  \n__________________________________________________________________________________________________\nconv2d_54 (Conv2D)              (None, 16, 16, 128)  106624      max_pooling2d_9[0][0]            \n__________________________________________________________________________________________________\ninception_5b (Concatenate)      (None, 16, 16, 1024) 0           conv2d_49[0][0]                  \n                                                                 conv2d_51[0][0]                  \n                                                                 conv2d_53[0][0]                  \n                                                                 conv2d_54[0][0]                  \n__________________________________________________________________________________________________\navg_pool_5_3x3/1 (GlobalAverage (None, 1024)         0           inception_5b[0][0]               \n__________________________________________________________________________________________________\ndense_1 (Dense)                 (None, 6400)         6560000     avg_pool_5_3x3/1[0][0]           \n__________________________________________________________________________________________________\ndense_2 (Dense)                 (None, 3200)         20483200    dense_1[0][0]                    \n__________________________________________________________________________________________________\ndense_3 (Dense)                 (None, 1600)         5121600     dense_2[0][0]                    \n__________________________________________________________________________________________________\ndense_4 (Dense)                 (None, 1024)         1639424     dense_3[0][0]                    \n__________________________________________________________________________________________________\ndense_5 (Dense)                 (None, 800)          820000      dense_4[0][0]                    \n__________________________________________________________________________________________________\ndense_6 (Dense)                 (None, 400)          320400      dense_5[0][0]                    \n__________________________________________________________________________________________________\ndense_7 (Dense)                 (None, 200)          80200       dense_6[0][0]                    \n__________________________________________________________________________________________________\ndropout_1 (Dropout)             (None, 200)          0           dense_7[0][0]                    \n__________________________________________________________________________________________________\noutput (Dense)                  (None, 5)            1005        dropout_1[0][0]                  \n==================================================================================================\nTotal params: 40,999,381\nTrainable params: 40,999,381\nNon-trainable params: 0\n__________________________________________________________________________________________________\nNone\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"sgd = SGD(lr = 0.01, momentum=0.9, nesterov=False)\nmodel.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])","execution_count":11,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = x_train.astype('float32')\nx_test = x_test.astype('float32')\nx_train /= 255\nx_test /= 255","execution_count":12,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.2,\n        fill_mode = \"nearest\"\n)\n\ndatagen.fit(x_train)\n\nbatch_size = 20\nepochs = 10\nsteps_per_epoch = 40 #800 / 20\n# Fit the model on the batches generated by datagen.flow().\nmodel.fit_generator(datagen.flow(x_train, y_train,\n                                 batch_size=batch_size),\n                    epochs=epochs,\n                    steps_per_epoch = 30,\n                    validation_data=(x_test, y_test),\n                    workers=4,\n                    verbose=1)\n\nsave_dir = \"kaggle/working/saved_models\"\nmodel_name = 'data_aug.h5'\nif not os.path.isdir(save_dir):\n    os.makedirs(save_dir)\nmodel_path = os.path.join(save_dir, model_name)\nmodel.save(model_path)","execution_count":13,"outputs":[{"output_type":"stream","text":"WARNING:tensorflow:From /opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\nInstructions for updating:\nUse tf.cast instead.\nEpoch 1/10\n30/30 [==============================] - 31s 1s/step - loss: 1.6285 - acc: 0.1947 - val_loss: 1.6478 - val_acc: 0.1154\nEpoch 2/10\n30/30 [==============================] - 21s 696ms/step - loss: 1.6197 - acc: 0.1890 - val_loss: 1.6157 - val_acc: 0.2308\nEpoch 3/10\n30/30 [==============================] - 20s 670ms/step - loss: 1.6147 - acc: 0.2117 - val_loss: 1.6401 - val_acc: 0.1154\nEpoch 4/10\n30/30 [==============================] - 20s 680ms/step - loss: 1.6129 - acc: 0.2203 - val_loss: 1.6376 - val_acc: 0.1154\nEpoch 5/10\n30/30 [==============================] - 20s 669ms/step - loss: 1.6075 - acc: 0.2084 - val_loss: 1.6402 - val_acc: 0.1154\nEpoch 6/10\n30/30 [==============================] - 22s 722ms/step - loss: 1.6070 - acc: 0.2148 - val_loss: 1.6423 - val_acc: 0.1154\nEpoch 7/10\n30/30 [==============================] - 20s 677ms/step - loss: 1.6092 - acc: 0.1871 - val_loss: 1.6534 - val_acc: 0.1154\nEpoch 8/10\n30/30 [==============================] - 20s 678ms/step - loss: 1.6063 - acc: 0.2411 - val_loss: 1.6548 - val_acc: 0.1154\nEpoch 9/10\n30/30 [==============================] - 20s 680ms/step - loss: 1.6123 - acc: 0.2053 - val_loss: 1.6340 - val_acc: 0.1154\nEpoch 10/10\n30/30 [==============================] - 22s 749ms/step - loss: 1.6085 - acc: 0.2239 - val_loss: 1.6372 - val_acc: 0.1154\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nn_model = Model(input_layer, x, name='inception_v1')\nn_model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])\nn_model.load_weights(model_path)\nscores = n_model.evaluate(x_test, y_test, verbose=1)\nprint('Test loss:', scores[0])\nprint('Test accuracy:', scores[1])\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nn_model.fit_generator(datagen.flow(x_train, y_train,\n                                 batch_size=batch_size),\n                    epochs=90,\n                    steps_per_epoch = 20,\n                    validation_data=(x_test, y_test),\n                    workers=4,\n                    verbose=1)\nn_model.save(model_path)\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = model.evaluate(x_test, y_test, verbose=1)\nprint('Test loss:', scores[0])\nprint('Test accuracy:', scores[1])","execution_count":14,"outputs":[{"output_type":"stream","text":"26/26 [==============================] - 1s 38ms/step\nTest loss: 1.6371670961380005\nTest accuracy: 0.11538461595773697\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(x_test,verbose=1)","execution_count":16,"outputs":[{"output_type":"stream","text":"\r26/26 [==============================] - 0s 18ms/step\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\ndef onehot2int(li):\n    n_li = []\n    for row in li:\n        r = []\n        for lab in row:\n            if lab > 0.5:\n                r.append(1)\n            else:\n                r.append(0)\n        n_li.append(r)\n        \n    print(n_li)\n\n    a = np.array(n_li)\n    n_li = np.where(a==1)[1]\n    n_li = n_li.tolist()\n    return n_li\n'''","execution_count":27,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nfrom sklearn.metrics import classification_report\nprint(classification_report(onehot2int(y_test),onehot2int(pred)))\n'''","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}