{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Diabetic Retinopathy Detection Using Convolutional Neuarl Network\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport keras\nfrom keras.applications.resnet50 import ResNet50\nfrom keras.models import Model\nfrom keras.layers import Dense, Dropout, Flatten\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport os\nfrom tqdm import tqdm\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/diabetic-retinopathy-resized/trainLabels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(df_train['level'])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"targets_series = pd.Series(df_train['level'])\none_hot = pd.get_dummies(targets_series, sparse = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"one_hot_labels = np.asarray(one_hot)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"im_size1 = 64\nim_size2 = 64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = []\ny_train = []\nx_test = []","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = 0 \nfor f, breed in tqdm(df_train.values):\n    if type(cv2.imread('../input/diabetic-retinopathy-resized/resized_train/resized_train/{}.jpeg'.format(f)))==type(None):\n        continue\n    else:\n        img = cv2.imread('../input/diabetic-retinopathy-resized/resized_train/resized_train/{}.jpeg'.format(f))\n        label = one_hot_labels[i]\n        x_train.append(cv2.resize(img, (im_size1, im_size2)))\n        y_train.append(label)\n        i += 1\nnp.save('x_train2',x_train)\nnp.save('y_train2',y_train)\nprint('Done')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = np.load('./x_train2.npy')\ny_train = np.load('./y_train2.npy')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,5):\n    print(\"level:\",y_train[i])\n    img = Image.fromarray(x_train[i], 'RGB')\n    plt.figure()\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train_raw = np.array(y_train, np.uint8)\nx_train_raw = np.array(x_train, np.float32) / 255.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(x_train_raw.shape)\nprint(y_train_raw.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(0,5):\n    print(\"level:\",y_train_raw[i])\n    img = Image.fromarray(x_train_raw[i], 'RGB')\n    plt.figure()\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(x_train_raw, y_train_raw, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_class = y_train_raw.shape[1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model = ResNet50(weights = None, include_top=False, input_shape=(im_size1, im_size2, 3))\n\n# Add a new top layer\nx = base_model.output\nx = Flatten()(x)\nx = Dropout(0.2)(x)\nx = Dense(32, activation='relu')(x)\nx = Dense(16, activation='relu')(x)\npredictions = Dense(num_class, activation='softmax')(x)\n\n# This is the model we will train\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# First: train only the top layers (which were randomly initialized)\n#for layer in base_model.layers:\n#    layer.trainable = False\n\nmodel.compile(loss='categorical_crossentropy', \n              optimizer='rmsprop', \n              metrics=['accuracy'])\n\ncallbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', verbose=1)]\nmodel.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X_train, Y_train, validation_data=(X_valid, Y_valid),batch_size = 64, epochs=5, shuffle=True, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', \n              optimizer='sgd', \n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(X_train, Y_train, validation_data=(X_valid, Y_valid), epochs=5, shuffle=True, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"score = model.evaluate(X_valid, Y_valid, verbose=1)\nprint(score)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = model.predict(X_valid, verbose=1)\npredictions","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction_df = pd.DataFrame(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction_df.head(100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_valid_df = pd.DataFrame(Y_valid)\nY_valid_df.head(100)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}