{"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":"import pandas as pd\nimport tensorflow as tf\nimport numpy as np\nimport keras\nimport matplotlib.pyplot as plt\nimport os\nimport time\nimport seaborn as sns\nimport cv2\nfrom PIL import Image\nfrom collections import Counter\nimport glob\nimport imageio\nfrom sklearn.model_selection import train_test_split\nfrom keras import layers\nfrom tensorflow.keras.layers import Conv2D, Dense\nfrom sklearn.preprocessing import MultiLabelBinarizer\n\nimport plotly.express as px","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-02T09:33:53.266555Z","iopub.execute_input":"2021-06-02T09:33:53.267101Z","iopub.status.idle":"2021-06-02T09:34:02.831588Z","shell.execute_reply.started":"2021-06-02T09:33:53.266984Z","shell.execute_reply":"2021-06-02T09:34:02.830436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##UPLOAD DATABASE\n\nLABELS = pd.read_csv('../input/hpa-single-cell-image-classification/train.csv')\n\nLABELS.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:34:43.91701Z","iopub.execute_input":"2021-06-02T09:34:43.917494Z","iopub.status.idle":"2021-06-02T09:34:43.954341Z","shell.execute_reply.started":"2021-06-02T09:34:43.917457Z","shell.execute_reply":"2021-06-02T09:34:43.953273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/hpa-single-cell-image-classification\"\ntrain = pd.read_csv(os.path.join(DATA_DIR,'train.csv'))\n\ncolours = ['_red.png', '_blue.png', '_yellow.png', '_green.png']\nTRAIN = '../input/hpa-single-cell-image-classification/train'\npaths = [[os.path.join(TRAIN, train.iloc[idx,0])+ colour for colour in colours] for idx in range(len(train))]","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:34:45.9518Z","iopub.execute_input":"2021-06-02T09:34:45.952317Z","iopub.status.idle":"2021-06-02T09:34:49.071367Z","shell.execute_reply.started":"2021-06-02T09:34:45.952278Z","shell.execute_reply":"2021-06-02T09:34:49.069911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = train.copy()\ntrain_csv['Label'] = train_csv['Label'].apply(lambda x: list(map(int,x.split(\"|\"))))\nmlb = MultiLabelBinarizer()\ntrain_csv[list(range(19))] = mlb.fit_transform(train_csv['Label'])\ntrain_csv.columns = [\"ID\", \"Label\"] + list(LABELS.values())","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:34:50.451637Z","iopub.execute_input":"2021-06-02T09:34:50.452068Z","iopub.status.idle":"2021-06-02T09:34:50.768587Z","shell.execute_reply.started":"2021-06-02T09:34:50.452032Z","shell.execute_reply":"2021-06-02T09:34:50.766898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:02.682717Z","iopub.execute_input":"2021-06-02T09:35:02.683178Z","iopub.status.idle":"2021-06-02T09:35:02.699159Z","shell.execute_reply.started":"2021-06-02T09:35:02.683138Z","shell.execute_reply":"2021-06-02T09:35:02.697607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_count = train_csv.iloc[:, 2:].sum()\npx.bar(label_count)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:05.38306Z","iopub.execute_input":"2021-06-02T09:35:05.383698Z","iopub.status.idle":"2021-06-02T09:35:07.008507Z","shell.execute_reply.started":"2021-06-02T09:35:05.38366Z","shell.execute_reply":"2021-06-02T09:35:07.007447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['num_classes'] = train['Label'].apply(lambda r: len(r.split('|')))\ntrain['num_classes'].value_counts().plot.bar(title='Examples with multiple labels', xlabel='number of labels per example', ylabel='# train examples')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:17.740882Z","iopub.execute_input":"2021-06-02T09:35:17.741687Z","iopub.status.idle":"2021-06-02T09:35:17.978118Z","shell.execute_reply.started":"2021-06-02T09:35:17.741614Z","shell.execute_reply":"2021-06-02T09:35:17.976837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##ONE HOT ENCODE LABELS\nmatrix = np.empty(shape=(len(labels['ID']),19),dtype='str')\nfor image in range(len(labels['ID'])):\n    for i in ['0','1','2','3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18']:\n        f = int(i)\n        if i in labels['Label'][image]:\n            matrix[image][f] = '1'\n        else:\n            matrix[image][f] = '0'\n            \noneHotDf = pd.DataFrame(matrix, columns = ['0','1','2','3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18'])\nlabels = pd.concat([labels['ID'], oneHotDf], axis=1)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:22.232834Z","iopub.execute_input":"2021-06-02T09:35:22.233285Z","iopub.status.idle":"2021-06-02T09:35:26.242606Z","shell.execute_reply.started":"2021-06-02T09:35:22.233243Z","shell.execute_reply":"2021-06-02T09:35:26.2411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = imageio.imread('../input/hpa-single-cell-image-classification/train/5e3a2e6a-bb9c-11e8-b2b9-ac1f6b6435d0_red.png')\nim = np.resize(im,(3072,3072))\nnp.size(im)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:27.749113Z","iopub.execute_input":"2021-06-02T09:35:27.749552Z","iopub.status.idle":"2021-06-02T09:35:27.88481Z","shell.execute_reply.started":"2021-06-02T09:35:27.749519Z","shell.execute_reply":"2021-06-02T09:35:27.883477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"blue_images, red_images, yellow_images, green_images = [], [], [], []\nfor iterator in range(100):\n    id = labels['ID'][iterator]\n    path_blue = '../input/hpa-single-cell-image-classification/train/'+ id +'_blue.png'\n    path_red = '../input/hpa-single-cell-image-classification/train/'+ id +'_red.png'\n    path_yellow = '../input/hpa-single-cell-image-classification/train/'+ id +'_yellow.png'\n    path_green = '../input/hpa-single-cell-image-classification/train/'+ id +'_green.png'\n    blue_images.append(np.resize(imageio.imread(path_blue), (2048, 2048)))\n    red_images.append(np.resize(imageio.imread(path_red), (2048, 2048)))\n    yellow_images.append(np.resize(imageio.imread(path_yellow), (2048, 2048)))\n    green_images.append(np.resize(imageio.imread(path_green), (2048, 2048)))\n","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:35:34.256824Z","iopub.execute_input":"2021-06-02T09:35:34.257285Z","iopub.status.idle":"2021-06-02T09:36:11.03942Z","shell.execute_reply.started":"2021-06-02T09:35:34.257244Z","shell.execute_reply":"2021-06-02T09:36:11.038229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keys = labels['ID'][:100]\ndict_blue = dict(zip(keys, blue_images))\ndict_red = dict(zip(keys, red_images))\ndict_yellow = dict(zip(keys, yellow_images))\ndict_green = dict(zip(keys, green_images))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:36:23.516937Z","iopub.execute_input":"2021-06-02T09:36:23.517393Z","iopub.status.idle":"2021-06-02T09:36:23.524436Z","shell.execute_reply.started":"2021-06-02T09:36:23.517352Z","shell.execute_reply":"2021-06-02T09:36:23.523176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dict_green.get(labels['ID'][2])","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:36:25.884631Z","iopub.execute_input":"2021-06-02T09:36:25.885107Z","iopub.status.idle":"2021-06-02T09:36:25.892982Z","shell.execute_reply.started":"2021-06-02T09:36:25.885065Z","shell.execute_reply":"2021-06-02T09:36:25.891826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\n\nmodel.add(Conv2D(32, kernel_size=5, strides=2, activation='relu', input_shape=(268, 182, 3)))\nmodel.add(Conv2D(64, kernel_size=3, strides=1, activation='relu'))       \n\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(8, activation='sigmoid'))   # Final Layer using Softmax\n\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:36:29.816268Z","iopub.execute_input":"2021-06-02T09:36:29.816691Z","iopub.status.idle":"2021-06-02T09:36:29.992394Z","shell.execute_reply.started":"2021-06-02T09:36:29.816657Z","shell.execute_reply":"2021-06-02T09:36:29.991039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(matrix))\nprint(len(blue_images))","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:43:52.291254Z","iopub.execute_input":"2021-06-02T09:43:52.291711Z","iopub.status.idle":"2021-06-02T09:43:52.297731Z","shell.execute_reply.started":"2021-06-02T09:43:52.291674Z","shell.execute_reply":"2021-06-02T09:43:52.296653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    keras.callbacks.ModelCheckpoint(\"save_at_{epoch}.h5\"),\n]\nmodel.compile(\n    optimizer=keras.optimizers.Adam(1e-3),\n    loss=\"binary_crossentropy\",\n    metrics=[\"accuracy\"],\n)\nmodel.fit(\n    blue_images, matrix, epochs= 10\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-02T09:43:08.078138Z","iopub.execute_input":"2021-06-02T09:43:08.078549Z","iopub.status.idle":"2021-06-02T09:43:08.475521Z","shell.execute_reply.started":"2021-06-02T09:43:08.078519Z","shell.execute_reply":"2021-06-02T09:43:08.473038Z"},"trusted":true},"execution_count":null,"outputs":[]}]}