{"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":"#@title Import thư viện cần thiết\nimport numpy as np\nimport pandas as pd \nimport os, sys\nimport matplotlib.pyplot as plt\nimport cv2\nimport tensorflow as tf\nIMG_SIZE = 512","metadata":{"cellView":"form","id":"_p7I9xiAbRd5","execution":{"iopub.status.busy":"2023-04-03T04:26:20.562198Z","iopub.execute_input":"2023-04-03T04:26:20.562516Z","iopub.status.idle":"2023-04-03T04:26:30.324954Z","shell.execute_reply.started":"2023-04-03T04:26:20.562483Z","shell.execute_reply":"2023-04-03T04:26:30.323817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Đường dẫn\ntrain_dir = '/kaggle/input/aptos2019-blindness-detection/train_images'\ntest_dir = '/kaggle/input/aptos2019-blindness-detection/test_images'\nid_code_train = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\nid_code_test = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')\n\n\nresnet_weights_path = \"/kaggle/input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\"\n\ndf_train = pd.read_csv('../input/aptos2019-blindness-detection/train.csv')\ny = df_train['diagnosis']\n# os.listdir(train_dir)","metadata":{"cellView":"form","id":"NYNzIPeOcErj","outputId":"3af05937-379a-4a31-d60c-6dc1e6724f0c","execution":{"iopub.status.busy":"2023-04-03T04:26:30.327454Z","iopub.execute_input":"2023-04-03T04:26:30.328655Z","iopub.status.idle":"2023-04-03T04:26:30.371047Z","shell.execute_reply.started":"2023-04-03T04:26:30.328602Z","shell.execute_reply":"2023-04-03T04:26:30.370048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Ảnh ban đầu, mà sao nó hiện màu xanh v\nfig = plt.figure(figsize=(10, 6))\nfor i in range(3):\n    ax = plt.subplot(1, 3, i+1)\n    path = train_dir +'/'+ str(id_code_train.loc[i,'id_code']) + '.png'\n    img = cv2.imread(path)\n    plt.imshow(img)\n    plt.axis('off')","metadata":{"cellView":"form","id":"UEsmKALeknCg","outputId":"4d0e9d57-ca09-42fa-e659-1a3856a06b62","execution":{"iopub.status.busy":"2023-04-03T04:26:30.372595Z","iopub.execute_input":"2023-04-03T04:26:30.373002Z","iopub.status.idle":"2023-04-03T04:26:34.281606Z","shell.execute_reply.started":"2023-04-03T04:26:30.372963Z","shell.execute_reply":"2023-04-03T04:26:34.280564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Gray scale\nfig = plt.figure(figsize=(10, 6))\nfor i in range(3):\n    ax = plt.subplot(1, 3, i+1)\n    img_full_dir = train_dir + '/'+ str(id_code_train.loc[i,'id_code']) + '.png'\n    img = cv2.imread(img_full_dir, cv2.IMREAD_GRAYSCALE)\n    plt.imshow(img, cmap='gray')\n    plt.axis('off')\n","metadata":{"id":"Z70BceIJcSs8","outputId":"b9d408bc-2b2d-48ff-a3e3-87b1850eef55","execution":{"iopub.status.busy":"2023-04-03T04:26:34.284731Z","iopub.execute_input":"2023-04-03T04:26:34.285125Z","iopub.status.idle":"2023-04-03T04:26:36.540059Z","shell.execute_reply.started":"2023-04-03T04:26:34.285088Z","shell.execute_reply":"2023-04-03T04:26:36.539053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Sử dụng Ben Graham's để cải thiện ánh sáng của ảnh xám, chứ nhìn nó tối quá\nfig = plt.figure(figsize=(10, 6))\nfor i in range(3):\n    ax = plt.subplot(1, 3, i+1)\n    path = train_dir+'/' + str(id_code_train.loc[i,'id_code']) + '.png'\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n    image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))\n    image=cv2.addWeighted (image,4, cv2.GaussianBlur( image , (0,0) , IMG_SIZE/10) ,-4 ,128)\n    plt.imshow(image, cmap='gray')\n    plt.axis('off')","metadata":{"cellView":"form","id":"yX1H4Iphhwh0","outputId":"908c18ee-e37a-4045-e384-071c80ffef12","execution":{"iopub.status.busy":"2023-04-03T04:26:36.541847Z","iopub.execute_input":"2023-04-03T04:26:36.542279Z","iopub.status.idle":"2023-04-03T04:26:38.140960Z","shell.execute_reply.started":"2023-04-03T04:26:36.542236Z","shell.execute_reply":"2023-04-03T04:26:38.139927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Các hàm tiền xử lý để cắt phần đen đen xung quanh mắt\ndef crop_image_from_gray(img,tol=7):\n    if img.ndim ==2:\n        mask = img>tol\n        return img[np.ix_(mask.any(1),mask.any(0))]\n    elif img.ndim==3:\n        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n        mask = gray_img>tol\n        \n        check_shape = img[:,:,0][np.ix_(mask.any(1),mask.any(0))].shape[0]\n        if (check_shape == 0): # image is too dark so that we crop out everything,\n            return img # return original image\n        else:\n            img1=img[:,:,0][np.ix_(mask.any(1),mask.any(0))]\n            img2=img[:,:,1][np.ix_(mask.any(1),mask.any(0))]\n            img3=img[:,:,2][np.ix_(mask.any(1),mask.any(0))]\n    #         print(img1.shape,img2.shape,img3.shape)\n            img = np.stack([img1,img2,img3],axis=-1)\n    #         print(img.shape)\n        return img\n        \ndef load_ben_color(path, sigmaX=10):\n    image = cv2.imread(path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (256, 256))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"cellView":"form","id":"pr2skqRVi2Ns","execution":{"iopub.status.busy":"2023-04-03T04:26:38.142466Z","iopub.execute_input":"2023-04-03T04:26:38.143459Z","iopub.status.idle":"2023-04-03T04:26:38.154214Z","shell.execute_reply.started":"2023-04-03T04:26:38.143417Z","shell.execute_reply":"2023-04-03T04:26:38.153121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(10, 6))\nfor i in range(3):\n  ax = plt.subplot(1, 3, i+1)\n  path = train_dir +'/' + str(id_code_train.loc[i,'id_code']) + '.png'\n  image = load_ben_color(path,sigmaX=30)\n  plt.imshow(image)","metadata":{"id":"4dNO21Vej1N6","outputId":"9311885c-0f7b-4de6-c232-2891ca7d7f88","execution":{"iopub.status.busy":"2023-04-03T04:26:38.155701Z","iopub.execute_input":"2023-04-03T04:26:38.156521Z","iopub.status.idle":"2023-04-03T04:26:39.931472Z","shell.execute_reply.started":"2023-04-03T04:26:38.156471Z","shell.execute_reply":"2023-04-03T04:26:39.929940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.imagenet_utils import preprocess_input\ndef arr_img():\n    for i in range(len(train_dir)):\n        xdata = np.zeros((id_code_train.shape[0], 256, 256, 3))\n        path = train_dir +'/' + str(id_code_train.loc[i,'id_code']) + '.png'\n        image = load_ben_color(path,sigmaX=30)\n        x = tf.keras.preprocessing.image.img_to_array(image)\n        xdata[i] = x\n\n    xdata = xdata / 255.0\n    return xdata","metadata":{"id":"hGxhb2DRoD6o","execution":{"iopub.status.busy":"2023-04-03T04:26:39.933317Z","iopub.execute_input":"2023-04-03T04:26:39.934164Z","iopub.status.idle":"2023-04-03T04:26:39.942321Z","shell.execute_reply.started":"2023-04-03T04:26:39.934123Z","shell.execute_reply":"2023-04-03T04:26:39.940669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xa = arr_img()\nxa.shape","metadata":{"id":"RCPC6xQ6piPT","outputId":"ad8e1fed-ff8f-447e-a949-29712f9e96ca","execution":{"iopub.status.busy":"2023-04-03T04:26:39.944150Z","iopub.execute_input":"2023-04-03T04:26:39.945103Z","iopub.status.idle":"2023-04-03T04:27:00.711302Z","shell.execute_reply.started":"2023-04-03T04:26:39.945054Z","shell.execute_reply":"2023-04-03T04:27:00.710091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.value_counts().plot(kind = 'bar')","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:27:00.714939Z","iopub.execute_input":"2023-04-03T04:27:00.715378Z","iopub.status.idle":"2023-04-03T04:27:00.935803Z","shell.execute_reply.started":"2023-04-03T04:27:00.715336Z","shell.execute_reply":"2023-04-03T04:27:00.934810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nfrom keras.utils.np_utils import to_categorical\n\nlb = LabelEncoder()\ny = lb.fit_transform(y)\ny = to_categorical(y, num_classes = 5)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:27:00.937439Z","iopub.execute_input":"2023-04-03T04:27:00.937798Z","iopub.status.idle":"2023-04-03T04:27:01.189013Z","shell.execute_reply.started":"2023-04-03T04:27:00.937760Z","shell.execute_reply":"2023-04-03T04:27:01.188012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.models import Model\n\nresnet = ResNet50(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\n\n# Freeze all layers in ResNet50\nfor layer in resnet.layers:\n    layer.trainable = False\n\n# Add new output layer for classification with 5 classes\nx = Flatten()(resnet.output)\noutput_layer = Dense(5, activation='softmax')(x)\nmodel = Model(inputs=resnet.input, outputs=output_layer)\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model using your data\nhistory = model.fit(xa, y, batch_size=32, epochs=10, validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:27:01.190605Z","iopub.execute_input":"2023-04-03T04:27:01.191041Z","iopub.status.idle":"2023-04-03T04:30:38.631278Z","shell.execute_reply.started":"2023-04-03T04:27:01.190971Z","shell.execute_reply":"2023-04-03T04:30:38.628392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img_path = '/kaggle/input/aptos2019-blindness-detection/test_images/0005cfc8afb6.png'\n\ntestdata = np.zeros((0, 256, 256, 3))\nimage = load_ben_color(test_img_path,sigmaX=30)\nx = tf.keras.preprocessing.image.img_to_array(image)\ntestdata = x\ntestdata = testdata / 255.0\n","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:51:00.975634Z","iopub.execute_input":"2023-04-03T04:51:00.976358Z","iopub.status.idle":"2023-04-03T04:51:01.077588Z","shell.execute_reply.started":"2023-04-03T04:51:00.976318Z","shell.execute_reply":"2023-04-03T04:51:01.076501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdata.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:51:02.712191Z","iopub.execute_input":"2023-04-03T04:51:02.712978Z","iopub.status.idle":"2023-04-03T04:51:02.723538Z","shell.execute_reply.started":"2023-04-03T04:51:02.712930Z","shell.execute_reply":"2023-04-03T04:51:02.722375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testdata = testdata.reshape((1, 256, 256, 3))","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:52:27.023323Z","iopub.execute_input":"2023-04-03T04:52:27.023752Z","iopub.status.idle":"2023-04-03T04:52:27.029480Z","shell.execute_reply.started":"2023-04-03T04:52:27.023712Z","shell.execute_reply":"2023-04-03T04:52:27.028398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.summary()\nmodel.predict(testdata)","metadata":{"execution":{"iopub.status.busy":"2023-04-03T04:52:28.425432Z","iopub.execute_input":"2023-04-03T04:52:28.425839Z","iopub.status.idle":"2023-04-03T04:52:30.021706Z","shell.execute_reply.started":"2023-04-03T04:52:28.425806Z","shell.execute_reply":"2023-04-03T04:52:30.020674Z"},"trusted":true},"execution_count":null,"outputs":[]}]}