{"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 matplotlib.pyplot as plt\nimport imageio\n\nTEST_FOLDER_PATH=\"../input/test/\"\nTRAIN_FOLDER_PATH=\"../input/train/\"\nTARGET_NUM=28\nlabel_columns=['Nucleoplasm','Nuclear membrane','Nucleoli',\n               'Nucleoli fibrillar center','Nuclear speckles'  ,\n               'Nuclear bodies','Endoplasmic reticulum',\n               'Golgi apparatus','Peroxisomes'  ,'Endosomes'  ,\n               'Lysosomes'  ,'Intermediate filaments'  ,'Actin filaments'  ,\n               'Focal adhesion sites'  ,'Microtubules'  ,'Microtubule ends'  ,\n               'Cytokinetic bridge'  ,'  Mitotic spindle'  ,\n               'Microtubule organizing center'  ,'Centrosome'  ,\n               'Lipid droplets'  ,'Plasma membrane'  ,'Cell junctions'  ,\n               'Mitochondria'  ,'Aggresome'  ,'Cytosol'  ,'Cytoplasmic bodies'  ,\n               'Rods & rings']\nlabel_columns_chinese=['核质','核膜','核仁','核仁纤维中心','核散斑','核机构','内质网',\n                       '高尔基体','过氧化物酶体','内体','溶酶体','中间长丝','肌动蛋白丝',\n                       '粘着位点','微管','微管末端','细胞动力学桥','有丝分裂纺锤','微管组织中心',\n                       '中心体','脂滴','质膜','细胞连接','线粒体','聚集小','细胞质','细胞质体',\n                       '杆和环']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"df80f5b2f49a43aecd1293e012a97b4c889464e8"},"cell_type":"code","source":"label=pd.read_csv('../input/train.csv')\ntrain_file_names=os.listdir(TRAIN_FOLDER_PATH)\ntest_file_names=os.listdir(TEST_FOLDER_PATH)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"55438d586df2012aea07f62777081100280a966f"},"cell_type":"code","source":"TRAIN_SAMPLE_SIZE=label.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bd382c4fd7309779f9ae12b2fa31a18092fdbcfa"},"cell_type":"code","source":"train_file_names.sort()\ntest_file_names.sort()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1c0c6e0445aa49430159812f5e099f78f5bd926f"},"cell_type":"code","source":"def generate_label_cube(label,label_columns):\n    cube=np.zeros((label.shape[0],TARGET_NUM),dtype=int)\n    target=label['Target'].values\n    for x in range(label.shape[0]):\n        for y in target[x].split():\n            cube[x][int(y)]=1\n    cube=pd.DataFrame(cube,columns=label_columns)\n    \n    return pd.concat([label,cube],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5aacc0f7b241b9bfa890f287cfa95e8691903b0a"},"cell_type":"code","source":"label_new=generate_label_cube(label,label_columns)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a540a8ccd19040e0a0ca0db5a29190d68aa03332"},"cell_type":"code","source":"tf=imageio.imread(\"../input/train/\"+label[0:1]['Id'].values[0]+'_green.png')\ntf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"afceec52d57dbf8f1d3a1c9e643d8e6b55c1fd87"},"cell_type":"code","source":"def showFourPic(names):\n    fig, axes = plt.subplots(4, 4,figsize=(25,25))\n    color=['Blues','Reds','YlOrBr','Greens']\n    apex=['_blue','_red','_yellow','_green']\n    for x in range(4):\n        for y in range(4):\n            axes[x,y].imshow(imageio.imread(TRAIN_FOLDER_PATH+names[x]+apex[y]+'.png'),cmap=plt.get_cmap(color[y]))\n            axes[x,y].set_title(names[x]+apex[y])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e46b962ef54c8027c5c288b439d87e0b62aaa038"},"cell_type":"markdown","source":"First, let's solve a puzzle that if red,yellow and green pictures are of some special types of protein organelle, because **endoplasmic reticulum (yellow)** and  **microtubules (red)** are also types showed in target list. If they are, they will look similar to green pictures. "},{"metadata":{"trusted":true,"_uuid":"62816ca8ae6b8c7ac806b63781fff2b3b7a1ebfd"},"cell_type":"code","source":"showFourPic(label_new[label_new['Microtubules']==1]['Id'][0:4].values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"295beea2135bdc29b867787817dcc8eae3d9b72a"},"cell_type":"code","source":"showFourPic(label_new[label_new['Endoplasmic reticulum']==1]['Id'][0:4].values)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"07708f97a460a101dffac5235c1adba794aeee7d"},"cell_type":"markdown","source":"I can see similar betweet green pictures with 'endoplasmic reticulum' and yellow pictures, and so do 'microtubules' labels and  red pictures. But I have not figure out the connection between them, I leave the part for later."},{"metadata":{"trusted":true,"_uuid":"467c514642c02e4b57ca1166acbc5b0550de38b8"},"cell_type":"markdown","source":"518x518 size of pictures are large for cnn models, it needs more weights and conputing time. In order to reduce the volume of weights and computing time, GoogLeNet is a better choice in my mind. It uses inception construct which has 1/4 numbers of weighs of an VGG. I will first go with GoogLeNet with V2."},{"metadata":{"trusted":true,"_uuid":"601ab8b4210b2a779ffc9261d4f0323b12c1ff30"},"cell_type":"code","source":"from keras.layers import Input\nfrom keras.layers import Dense\nfrom keras.layers import Conv2D\nfrom keras.layers import MaxPooling2D\nfrom keras.layers import BatchNormalization\nfrom keras.layers import Activation\nfrom keras.layers import AveragePooling2D\nfrom keras.layers import GlobalAveragePooling2D\nfrom keras.models import Model\nfrom keras import layers\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddb73fc47abfb5baecc0f144fb490619eebbc889"},"cell_type":"code","source":"def conv2d_bn(x,\n              filters,\n              num_row,\n              num_col,\n              padding='same',\n              strides=(1, 1),\n              name=None):\n    bn_axis = 3\n    x = Conv2D(\n        filters, (num_row, num_col),\n        strides=strides,\n        padding=padding,\n        use_bias=False,\n        name=name)(x)\n    x = BatchNormalization(axis=bn_axis, scale=False, name=name)(x)\n    x = Activation('relu', name=name)(x)\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"efdd91014eea0c3a4f0757af7f022753cf813730"},"cell_type":"code","source":"def InceptionV3():\n\n    channel_axis = 3\n    classes = 28\n    inputs=Input(shape=(512,512,1))\n\n    x = conv2d_bn(inputs,3,3,3,strides=(2,2),padding='valid')\n    x = conv2d_bn(x, 32, 3, 3, strides=(2, 2), padding='valid')\n    x = conv2d_bn(x, 32, 3, 3, padding='valid')\n    x = conv2d_bn(x, 64, 3, 3)\n    x = MaxPooling2D((3, 3), strides=(2, 2))(x)\n\n    x = conv2d_bn(x, 80, 1, 1, padding='valid')\n    x = conv2d_bn(x, 192, 3, 3, padding='valid')\n    x = MaxPooling2D((3, 3), strides=(2, 2))(x)\n\n    # mixed 0, 1, 2: 35 x 35 x 256\n    branch1x1 = conv2d_bn(x, 64, 1, 1)\n\n    branch5x5 = conv2d_bn(x, 48, 1, 1)\n    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)\n\n    branch3x3dbl = conv2d_bn(x, 64, 1, 1)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n\n    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n    branch_pool = conv2d_bn(branch_pool, 32, 1, 1)\n    x = layers.concatenate(\n        [branch1x1, branch5x5, branch3x3dbl, branch_pool],\n        axis=channel_axis,\n        name='mixed0')\n\n    # mixed 1: 35 x 35 x 256\n    branch1x1 = conv2d_bn(x, 64, 1, 1)\n\n    branch5x5 = conv2d_bn(x, 48, 1, 1)\n    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)\n\n    branch3x3dbl = conv2d_bn(x, 64, 1, 1)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n\n    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)\n    x = layers.concatenate(\n        [branch1x1, branch5x5, branch3x3dbl, branch_pool],\n        axis=channel_axis,\n        name='mixed1')\n\n    # mixed 2: 35 x 35 x 256\n    branch1x1 = conv2d_bn(x, 64, 1, 1)\n\n    branch5x5 = conv2d_bn(x, 48, 1, 1)\n    branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)\n\n    branch3x3dbl = conv2d_bn(x, 64, 1, 1)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n\n    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n    branch_pool = conv2d_bn(branch_pool, 64, 1, 1)\n    x = layers.concatenate(\n        [branch1x1, branch5x5, branch3x3dbl, branch_pool],\n        axis=channel_axis,\n        name='mixed2')\n\n    # mixed 3: 17 x 17 x 768\n    branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid')\n\n    branch3x3dbl = conv2d_bn(x, 64, 1, 1)\n    branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)\n    branch3x3dbl = conv2d_bn(\n        branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid')\n\n    branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)\n    x = layers.concatenate(\n        [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')\n\n    # mixed 4: 17 x 17 x 768\n    branch1x1 = conv2d_bn(x, 192, 1, 1)\n\n    branch7x7 = conv2d_bn(x, 128, 1, 1)\n    branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)\n    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)\n\n    branch7x7dbl = conv2d_bn(x, 128, 1, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)\n\n    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)\n    x = layers.concatenate(\n        [branch1x1, branch7x7, branch7x7dbl, branch_pool],\n        axis=channel_axis,\n        name='mixed4')\n\n    # mixed 5, 6: 17 x 17 x 768\n    for i in range(2):\n        branch1x1 = conv2d_bn(x, 192, 1, 1)\n\n        branch7x7 = conv2d_bn(x, 160, 1, 1)\n        branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)\n        branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)\n\n        branch7x7dbl = conv2d_bn(x, 160, 1, 1)\n        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)\n        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)\n        branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)\n        branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)\n\n        branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)\n        x = layers.concatenate(\n            [branch1x1, branch7x7, branch7x7dbl, branch_pool],\n            axis=channel_axis,\n            name='mixed' + str(5 + i))\n\n    # mixed 7: 17 x 17 x 768\n    branch1x1 = conv2d_bn(x, 192, 1, 1)\n\n    branch7x7 = conv2d_bn(x, 192, 1, 1)\n    branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)\n    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)\n\n    branch7x7dbl = conv2d_bn(x, 192, 1, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)\n    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)\n\n    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)\n    x = layers.concatenate(\n        [branch1x1, branch7x7, branch7x7dbl, branch_pool],\n        axis=channel_axis,\n        name='mixed7')\n\n    # mixed 8: 8 x 8 x 1280\n    branch3x3 = conv2d_bn(x, 192, 1, 1)\n    branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid')\n\n    branch7x7x3 = conv2d_bn(x, 192, 1, 1)\n    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)\n    branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)\n    branch7x7x3 = conv2d_bn(\n        branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid')\n\n    branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)\n    x = layers.concatenate(\n        [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')\n\n    # mixed 9: 8 x 8 x 2048\n    for i in range(2):\n        branch1x1 = conv2d_bn(x, 320, 1, 1)\n\n        branch3x3 = conv2d_bn(x, 384, 1, 1)\n        branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)\n        branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)\n        branch3x3 = layers.concatenate(\n            [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))\n\n        branch3x3dbl = conv2d_bn(x, 448, 1, 1)\n        branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)\n        branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)\n        branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)\n        branch3x3dbl = layers.concatenate(\n            [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)\n\n        branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)\n        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)\n        x = layers.concatenate(\n            [branch1x1, branch3x3, branch3x3dbl, branch_pool],\n            axis=channel_axis,\n            name='mixed' + str(9 + i))\n    \n    # Classification block\n    x = GlobalAveragePooling2D(name='avg_pool')(x)\n    x = Dense(classes, activation='softmax', name='predictions')(x)\n\n\n    # Create model.\n    model = Model(inputs, x, name='inception_v3')\n\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"172e572bb2595404aa022df21380c7156e5f28f7"},"cell_type":"code","source":"model = InceptionV3()\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f241494da88fc1c1c8fa66b63221b70523ac7931"},"cell_type":"code","source":"def generate_label(target):\n    label=np.zeros(TARGET_NUM,dtype=float)\n    for x in target.split():\n        label[int(x)]=1\n    return label","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d8f3b7de0562be0eaff38da32d8f98afc0823024"},"cell_type":"code","source":"\ndef image_generator(label,batch_size):\n    while True:\n        try:\n            #make batch size data a generator\n            batch=label.loc[np.random.randint(TRAIN_SAMPLE_SIZE, size=batch_size)]\n            img_list=[]\n            img_gen=iter(batch['Id'])\n            tar_list=[]\n            tar_gen=iter(batch['Target'])\n            \n            #create train and target batch\n            for x in range(batch_size):\n                file=next(img_gen)\n                target=next(tar_gen)\n                img=imageio.imread(TRAIN_FOLDER_PATH+file+'_green.png')\n                img_list.append(img)\n                tar=generate_label(target)   \n                tar_list.append(tar)\n                \n            #do pre-process and transform\n            img_out=np.array(img_list,dtype='float')\n            img_out/=255\n            img_out=img_out.reshape(batch_size,512,512,1)\n            tar_out=np.array(tar_list,dtype='float')\n\n            yield (img_out,tar_out)\n        except StopIteration:\n            break","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4bd8e51e253c40ea0a5a87af6a48e953a1c1bc9f"},"cell_type":"markdown","source":"InceptionV3 is heavy load for getting ideal accuracy. It took more than 4 hours to get accuracy over 0.60. One reason is that the InceptionV3 is 42 more layers which takes more time to update weights. The other reason is that the image size a little bit large. So I am going to resize the image to check if it works better on speeding up."},{"metadata":{"trusted":true,"_uuid":"d4cab5b091793f7dcf028d48fff5810030d2f9e7"},"cell_type":"markdown","source":"model = InceptionV3()\nmodel.compile(optimizer='rmsprop',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])"},{"metadata":{"trusted":true,"_uuid":"a3fd082bcd6f0c47cfe99b194e13eccf23c9fa48"},"cell_type":"markdown","source":"\nmodel.fit_generator(image_generator(label,100),steps_per_epoch=100,epochs=50)  # starts training"},{"metadata":{"trusted":true,"_uuid":"4cb5827de55ac9ab265066c75396b8189ffe5ba4"},"cell_type":"markdown","source":"model.save_weights('my_model_weights.h5')\nmodel.save('my_model.h5')"},{"metadata":{"trusted":true,"_uuid":"8477080fc3f5b77363ad788811f9431fe3710504"},"cell_type":"markdown","source":"test_files=[x for x in test_file_names if 'blue' in x]\ndef test_file_generator(test_files):\n    test_gen=iter(test_file_names)\n    while True:\n        try:\n            file = next(test_gen)\n            img=imageio.imread(TEST_FOLDER_PATH+file)\n            img=img.astype('float32')\n            img/=255\n            img=img.reshape(1,512,512,1)\n            yield img\n        except StopIteration:\n            break\n    "},{"metadata":{"trusted":true,"_uuid":"9f9b318b44d191e5cf7fad52524b8977202f5573"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f61cf9cf7181263bdd6c801626a96e9e9d0f41c"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2784f450822a2a9d2e653efbfc3c05be14e8a4a9"},"cell_type":"code","source":"predics=model.predict_generator(test_file_generator(test_files),steps=len(test_files))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"635e227f52cc4a3232b21b155c6257bdb6aa7f97"},"cell_type":"code","source":"predics[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cbadcee6f5b00bf1bec4bd3f75a96e6d374eade5"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}