{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os, sys\nimport tensorflow as tf","metadata":{"_uuid":"3f7e6ba7-43d1-48bf-80df-e956a827cd51","_cell_guid":"b36a997e-f907-4286-8f01-fcbb598b96ba","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:21:59.362772Z","iopub.execute_input":"2023-04-07T01:21:59.363727Z","iopub.status.idle":"2023-04-07T01:22:08.443293Z","shell.execute_reply.started":"2023-04-07T01:21:59.363672Z","shell.execute_reply":"2023-04-07T01:22:08.442149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow import keras\n\nfrom keras.preprocessing import image\nfrom keras.applications.imagenet_utils import preprocess_input\nfrom tqdm import tqdm\nimport PIL\nfrom PIL import Image, ImageOps\nimport cv2\nfrom sklearn.utils import class_weight, shuffle\nfrom keras.losses import binary_crossentropy\nfrom pathlib import Path\n\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.applications.imagenet_utils import preprocess_input\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nIMG_SIZE_LOAD=64\nNUM_CLASSES = 5\nSEED = 77\nTRAIN_NUM = 1000 # use 1000 when you just want to explore new idea, use -1 for full train","metadata":{"_uuid":"333c3ae6-eeb9-4650-b624-97cf35b80547","_cell_guid":"a5e720e4-fdc6-426b-af2d-8a71c70ea3dd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:22:08.445588Z","iopub.execute_input":"2023-04-07T01:22:08.446185Z","iopub.status.idle":"2023-04-07T01:22:09.113918Z","shell.execute_reply.started":"2023-04-07T01:22:08.446153Z","shell.execute_reply":"2023-04-07T01:22:09.112852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Đường dẫn\ntrain_dir = '/kaggle/input/diabetic-retinopathy-resized/resized_train/resized_train/'\n# os.listdir(train_dir)1","metadata":{"_uuid":"4ec25cfe-9549-4c05-bacf-e87072e98e17","_cell_guid":"1e869674-34ed-4169-8122-54f488a53c1d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:22:09.115735Z","iopub.execute_input":"2023-04-07T01:22:09.116113Z","iopub.status.idle":"2023-04-07T01:22:09.121241Z","shell.execute_reply.started":"2023-04-07T01:22:09.116074Z","shell.execute_reply":"2023-04-07T01:22:09.120110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_resize = '/kaggle/input/diabetic-retinopathy-resized/trainLabels.csv'\nd_resize = pd.read_csv(p_resize)\nd_resize.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:22:09.124144Z","iopub.execute_input":"2023-04-07T01:22:09.124843Z","iopub.status.idle":"2023-04-07T01:22:09.184977Z","shell.execute_reply.started":"2023-04-07T01:22:09.124806Z","shell.execute_reply":"2023-04-07T01:22:09.183285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def turn2DF(path):\n    dataframe_train = pd.read_csv(path)\n    \n    for i in range(len(dataframe_train)):\n        dataframe_train=dataframe_train.replace(dataframe_train.loc[i,'image'], train_dir + str(dataframe_train.loc[i,'image']) + '.jpeg')\n        \n    dataframe_train.to_csv('mydata.csv', index=False) # index=False để không lưu index của DataFrame vào file CSV","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:58:17.122024Z","iopub.execute_input":"2023-04-07T01:58:17.122416Z","iopub.status.idle":"2023-04-07T01:58:17.129222Z","shell.execute_reply.started":"2023-04-07T01:58:17.122379Z","shell.execute_reply":"2023-04-07T01:58:17.128011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"turn2DF('/kaggle/input/diabetic-retinopathy-resized/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:58:19.813122Z","iopub.execute_input":"2023-04-07T01:58:19.813897Z","iopub.status.idle":"2023-04-07T01:59:05.603535Z","shell.execute_reply.started":"2023-04-07T01:58:19.813853Z","shell.execute_reply":"2023-04-07T01:59:05.602385Z"},"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_RGB2GRAY)\n    image = crop_image_from_gray(image)\n    image = cv2.resize(image, (IMG_SIZE_LOAD, IMG_SIZE_LOAD))\n    image=cv2.addWeighted ( image,4, cv2.GaussianBlur( image , (0,0) , sigmaX) ,-4 ,128)\n        \n    return image","metadata":{"_uuid":"06b1c018-c494-40c9-aa26-8295e6b6ccc3","_cell_guid":"62b64dbb-1604-4b97-8e61-2ca04f0dec78","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:22:09.203595Z","iopub.execute_input":"2023-04-07T01:22:09.204079Z","iopub.status.idle":"2023-04-07T01:22:09.217640Z","shell.execute_reply.started":"2023-04-07T01:22:09.204043Z","shell.execute_reply":"2023-04-07T01:22:09.216403Z"},"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(d_resize)):\n        xdata = np.zeros((d_resize.shape[0], IMG_SIZE_LOAD, IMG_SIZE_LOAD, 3))\n        path = '/kaggle/input/diabetic-retinopathy-resized/resized_train/resized_train/'  + str(d_resize.loc[i,'image']) + '.jpeg'\n        image = cv2.imread(path)\n        image = cv2.resize(image, (IMG_SIZE_LOAD, IMG_SIZE_LOAD))\n        x = tf.keras.preprocessing.image.img_to_array(image)\n        xdata[i] = x\n\n    xdata = xdata / 255.0\n    return xdata","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:22:09.231283Z","iopub.execute_input":"2023-04-07T01:22:09.232026Z","iopub.status.idle":"2023-04-07T01:22:09.242303Z","shell.execute_reply.started":"2023-04-07T01:22:09.231996Z","shell.execute_reply":"2023-04-07T01:22:09.241372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = arr_img()\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:22:09.246300Z","iopub.execute_input":"2023-04-07T01:22:09.246701Z","iopub.status.idle":"2023-04-07T01:31:06.111284Z","shell.execute_reply.started":"2023-04-07T01:22:09.246655Z","shell.execute_reply":"2023-04-07T01:31:06.110205Z"},"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(d_resize['level'])\ny = to_categorical(y, num_classes = NUM_CLASSES)","metadata":{"_uuid":"216bb2c2-cfc8-4353-a802-f41a61e5b5fe","_cell_guid":"cf2f022b-5087-4f74-ab30-a47c459671c0","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:32:25.831035Z","iopub.execute_input":"2023-04-07T01:32:25.831964Z","iopub.status.idle":"2023-04-07T01:32:25.844735Z","shell.execute_reply.started":"2023-04-07T01:32:25.831905Z","shell.execute_reply":"2023-04-07T01:32:25.843664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten\nfrom keras.layers.convolutional import Conv2D, MaxPooling2D\n\nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 30, kernel_size = (5, 5), input_shape = (64, 64, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Conv2D(filters = 15, kernel_size = (3, 3), activation = 'relu'))\nmodel.add(Conv2D(filters = 15, kernel_size = (3, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Dropout(0.2))\n\nmodel.add(Flatten())\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dense(32, activation = 'relu'))\n\nmodel.add(Dense(5, activation = 'softmax'))\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['categorical_accuracy'])","metadata":{"_uuid":"f345066c-d1c9-498e-8720-7071461af131","_cell_guid":"7557dd80-d700-4a70-8ba2-36720a66654b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:33:12.900112Z","iopub.execute_input":"2023-04-07T01:33:12.900591Z","iopub.status.idle":"2023-04-07T01:33:13.012056Z","shell.execute_reply.started":"2023-04-07T01:33:12.900554Z","shell.execute_reply":"2023-04-07T01:33:13.010729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y, epochs = 50, batch_size = 200)","metadata":{"_uuid":"5511e282-3cd4-46bb-9a2b-aa73b9f1dae7","_cell_guid":"00a20b34-7d84-41c6-9620-594a24e64700","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:33:15.711491Z","iopub.execute_input":"2023-04-07T01:33:15.711938Z","iopub.status.idle":"2023-04-07T01:37:02.719417Z","shell.execute_reply.started":"2023-04-07T01:33:15.711898Z","shell.execute_reply":"2023-04-07T01:37:02.718400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n\n# Train mô hình ở đây\n\n# Lưu mô hình thành file\nmodel.save('model_handmade.h5')\n\n# Tải lại mô hình từ file\nloaded_handmade_model = load_model('model_handmade.h5')","metadata":{"_uuid":"6443ce17-ea33-43e4-bb86-7bfeb26533a8","_cell_guid":"94f666b8-2605-49d2-b19f-917836ea8d7b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:52:28.529424Z","iopub.execute_input":"2023-04-07T01:52:28.530220Z","iopub.status.idle":"2023-04-07T01:52:30.827252Z","shell.execute_reply.started":"2023-04-07T01:52:28.530178Z","shell.execute_reply":"2023-04-07T01:52:30.826235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xtest = np.zeros((1, 64, 64, 3))\npath = '/kaggle/input/aptos2019-blindness-detection/test_images/0005cfc8afb6.png'\nimage = cv2.imread(path)\nimage = cv2.resize(image, (IMG_SIZE_LOAD, IMG_SIZE_LOAD))\nx_test_array = tf.keras.preprocessing.image.img_to_array(image)\nxtest[0] = x_test_array\nxtest = xtest / 255.0","metadata":{"_uuid":"eb8b9561-8c33-43a9-853e-7efa78e71824","_cell_guid":"c2ef523e-ae16-4411-b283-f9d58cc1f996","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-04-07T01:31:06.470045Z","iopub.status.idle":"2023-04-07T01:31:06.470599Z","shell.execute_reply.started":"2023-04-07T01:31:06.470302Z","shell.execute_reply":"2023-04-07T01:31:06.470330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Dự đoán\npreds = loaded_handmade_model.predict(xtest)\n\n# Hiển thị kết quả dự đoán\nprint('Kết quả dự đoán:', preds)","metadata":{"execution":{"iopub.status.busy":"2023-04-07T01:52:32.471181Z","iopub.execute_input":"2023-04-07T01:52:32.472194Z","iopub.status.idle":"2023-04-07T01:52:33.620145Z","shell.execute_reply.started":"2023-04-07T01:52:32.472138Z","shell.execute_reply":"2023-04-07T01:52:33.618758Z"},"trusted":true},"execution_count":null,"outputs":[]}]}