{"cells":[{"metadata":{},"cell_type":"markdown","source":"<h1>Diabetic Retinopathy</h1>\n<p>Diabetic retinopathy is damage to the retina caused by complications of diabetes mellitus.\nThe condition can lead to blindness if left untreated. Early blindness due to diabetic retinopathy (DR) is usually preventable with routine checks and effective management of the underlying diabetes.</p>\n<br><br><p>The retina is the membrane that covers the back of the eye. It is highly sensitive to light.\n<br>\nIt converts any light that hits the eye into signals that can be interpreted by the brain. This process produces visual images, and it is how sight functions in the human eye.\n<br>\nDiabetic retinopathy damages the blood vessels within the retinal tissue, causing them to leak fluid and distort vision.</p>\n\n<h4>Data description from the competition:</h4>\n\n<p>You are provided with a large set of high-resolution retina images taken under a variety of imaging conditions. A left and right field is provided for every subject. >Images are labeled with a subject id as well as either left or right (e.g. 1_left.jpeg is the left eye of patient id 1).\n\nA clinician has rated the presence of diabetic retinopathy in each image on a scale of 0 to 4, according to the following scale:\n<ol><li>\n    No DR</li>\n<li>\nMild\n    </li>\n    <li>\nModerate\n    </li>\n<li>Severe\n    </li>\n    <li>Proliferative DR</li>\n</ol>\nYour task is to create an automated analysis system capable of assigning a score based on this scale.\n\n</p>"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os, sys\nfrom PIL import Image\nimport glob\nimport cv2\nfrom keras.utils import to_categorical\nimport keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('../input/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_list = [[Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png'),j] for i,j in zip(train.id_code[:5],train.diagnosis[:5])]\ntrain_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i,j in train_list:\n    plt.figure(figsize=(5,3))\n    i = cv2.resize(np.asarray(i),(256,256))\n    plt.title(j)\n    plt.imshow(i)\n    plt.show","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = [cv2.resize(np.asarray(Image.open('../input/aptos2019-blindness-detection/train_images/'+i+'.png')),(256,256)) for i in train.id_code]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = np.array(x_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = train.diagnosis","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = to_categorical(y_train)\ny_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.applications.densenet.DenseNet121(input_shape=(256,256,3),include_top=True,weights=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.load_weights('../input/densenet-keras/DenseNet-BC-121-32.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = model.layers[-2].output\nd = keras.layers.Dense(512,activation='relu')(x)\ne = keras.layers.Dense(5,activation='softmax')(d)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model1 = keras.models.Model(model.input,e)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model1.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model1.fit(x_train,y_train,validation_split=0.20,epochs=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv('../input/aptos2019-blindness-detection/test.csv')\ntest = []\nfor i in test_df.id_code:\n    temp = np.array(cv2.resize(np.array(Image.open('../input/aptos2019-blindness-detection/test_images/'+i+'.png')),(256,256)))\n    test.append(temp)\ntest = np.array(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\nresult = model1.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res = []\nfor i in result:\n    res.append(np.argmax(i))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test = pd.DataFrame({\"id_code\": test_df[\"id_code\"].values, \"diagnosis\": res})\ndf_test.head(20)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.to_csv('submission.csv',index=False)","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}