{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":4104,"databundleVersionId":46661,"sourceType":"competition"},{"sourceId":952401,"sourceType":"datasetVersion","datasetId":517172}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"d1f08076-e869-48ea-bcf8-93fe61a3435b","_cell_guid":"f37108c5-61b4-41aa-9188-ee3a6d58489e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import lite\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport numpy as np\nimport pandas as pd\nimport random, os\nimport shutil\nimport matplotlib.pyplot as plt\nfrom matplotlib.image import imread\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.metrics import categorical_accuracy\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"99c0024b-a055-4315-b475-f771d9fd8741","_cell_guid":"3931bac8-e68c-4ab7-b312-9687105cfcf1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:42.831606Z","iopub.execute_input":"2024-02-14T08:03:42.831983Z","iopub.status.idle":"2024-02-14T08:03:42.838554Z","shell.execute_reply.started":"2024-02-14T08:03:42.831954Z","shell.execute_reply":"2024-02-14T08:03:42.837512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(r'../input/diabetic-retinopathy-224x224-gaussian-filtered/train.csv')\n\ndiagnosis_dict_binary = {\n    0: 'No_DR',\n    1: 'DR',\n    2: 'DR',\n    3: 'DR',\n    4: 'DR'\n}\n\ndiagnosis_dict = {\n    0: 'No_DR',\n    1: 'Mild',\n    2: 'Moderate',\n    3: 'Severe',\n    4: 'Proliferate_DR',\n}\n\n\ndf['binary_type'] =  df['diagnosis'].map(diagnosis_dict_binary.get)\ndf['type'] = df['diagnosis'].map(diagnosis_dict.get)\ndf.head()","metadata":{"_uuid":"393301e9-8bcb-4d9f-a095-3f5c34d5bc40","_cell_guid":"ef69b641-0103-444b-bfb9-73f1a39dc675","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:42.840231Z","iopub.execute_input":"2024-02-14T08:03:42.840542Z","iopub.status.idle":"2024-02-14T08:03:42.866091Z","shell.execute_reply.started":"2024-02-14T08:03:42.840516Z","shell.execute_reply":"2024-02-14T08:03:42.865336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['type'].value_counts().plot(kind='barh')","metadata":{"_uuid":"dcf0d5f2-071d-47be-95e3-b71e30a1b67f","_cell_guid":"2fdd7d80-d239-4ab1-89d9-e3e403f51ea1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:42.867205Z","iopub.execute_input":"2024-02-14T08:03:42.867550Z","iopub.status.idle":"2024-02-14T08:03:43.055506Z","shell.execute_reply.started":"2024-02-14T08:03:42.867516Z","shell.execute_reply":"2024-02-14T08:03:43.054585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_intermediate, val = train_test_split(df, test_size = 0.15, stratify = df['type'])\ntrain, test = train_test_split(train_intermediate, test_size = 0.15 / (1 - 0.15), stratify = train_intermediate['type'])\n\nprint(\"For Training Dataset :\")\nprint(train['type'].value_counts(), '\\n')\nprint(\"For Testing Dataset :\")\nprint(test['type'].value_counts(), '\\n')\nprint(\"For Validation Dataset :\")\nprint(val['type'].value_counts(), '\\n')","metadata":{"_uuid":"606bb8af-f602-4df5-8a40-b8c501bdeca6","_cell_guid":"2d93e18f-49fc-4d40-90be-447911ad31b4","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:43.057415Z","iopub.execute_input":"2024-02-14T08:03:43.057699Z","iopub.status.idle":"2024-02-14T08:03:43.082984Z","shell.execute_reply.started":"2024-02-14T08:03:43.057674Z","shell.execute_reply":"2024-02-14T08:03:43.082143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = ''\n\ntrain_dir = os.path.join(base_dir, 'train')\nval_dir = os.path.join(base_dir, 'val')\ntest_dir = os.path.join(base_dir, 'test')\n\nif os.path.exists(base_dir):\n    shutil.rmtree(base_dir)\n\nif os.path.exists(train_dir):\n    shutil.rmtree(train_dir)\nos.makedirs(train_dir)\n\nif os.path.exists(val_dir):\n    shutil.rmtree(val_dir)\nos.makedirs(val_dir)\n\nif os.path.exists(test_dir):\n    shutil.rmtree(test_dir)\nos.makedirs(test_dir)","metadata":{"_uuid":"02ec4c50-53df-4199-91d5-5161f2562ffa","_cell_guid":"370feb0f-4e22-48f0-8797-3cb403c72dc3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:43.084214Z","iopub.execute_input":"2024-02-14T08:03:43.084555Z","iopub.status.idle":"2024-02-14T08:03:43.256209Z","shell.execute_reply.started":"2024-02-14T08:03:43.084522Z","shell.execute_reply":"2024-02-14T08:03:43.255428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_dir = r'../input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images'\nfor index, row in train.iterrows():\n    diagnosis = row['type']\n    binary_diagnosis = row['binary_type']\n    id_code = row['id_code'] + \".png\"\n    srcfile = os.path.join(src_dir, diagnosis, id_code)\n    dstfile = os.path.join(train_dir, binary_diagnosis)\n    os.makedirs(dstfile, exist_ok = True)\n    shutil.copy(srcfile, dstfile)\n\nfor index, row in val.iterrows():\n    diagnosis = row['type']\n    binary_diagnosis = row['binary_type']\n    id_code = row['id_code'] + \".png\"\n    srcfile = os.path.join(src_dir, diagnosis, id_code)\n    dstfile = os.path.join(val_dir, binary_diagnosis)\n    os.makedirs(dstfile, exist_ok = True)\n    shutil.copy(srcfile, dstfile)\n \nfor index, row in test.iterrows():\n    diagnosis = row['type']\n    binary_diagnosis = row['binary_type']\n    id_code = row['id_code'] + \".png\"\n    srcfile = os.path.join(src_dir, diagnosis, id_code)\n    dstfile = os.path.join(test_dir, binary_diagnosis)\n    os.makedirs(dstfile, exist_ok = True)\n    shutil.copy(srcfile, dstfile)","metadata":{"_uuid":"097814d3-5330-4b4f-b18d-94e15a8281db","_cell_guid":"7dbd2c3a-ae80-41e4-af59-b82a5b873535","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:43.257399Z","iopub.execute_input":"2024-02-14T08:03:43.257736Z","iopub.status.idle":"2024-02-14T08:03:50.146107Z","shell.execute_reply.started":"2024-02-14T08:03:43.257704Z","shell.execute_reply":"2024-02-14T08:03:50.145296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = 'train'\nval_path = 'val'\ntest_path = 'test'\n\ntrain_batches = ImageDataGenerator(rescale = 1./255).flow_from_directory(train_path, target_size=(224,224), shuffle = True)\nval_batches = ImageDataGenerator(rescale = 1./255).flow_from_directory(val_path, target_size=(224,224), shuffle = True)\ntest_batches = ImageDataGenerator(rescale = 1./255).flow_from_directory(test_path, target_size=(224,224), shuffle = False)","metadata":{"_uuid":"8d8fbd7d-9989-4740-8d6d-e2b927b7e9a5","_cell_guid":"96eaad38-a21a-4605-ab11-fa5d28481ed8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:50.147536Z","iopub.execute_input":"2024-02-14T08:03:50.148131Z","iopub.status.idle":"2024-02-14T08:03:50.336829Z","shell.execute_reply.started":"2024-02-14T08:03:50.148096Z","shell.execute_reply":"2024-02-14T08:03:50.335940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    layers.Conv2D(8, (3,3), padding=\"valid\", input_shape=(224,224,3), activation = 'relu'),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.BatchNormalization(),\n    \n    layers.Conv2D(16, (3,3), padding=\"valid\", activation = 'relu'),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.BatchNormalization(),\n    \n    layers.Conv2D(32, (4,4), padding=\"valid\", activation = 'relu'),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.BatchNormalization(),\n    \n    layers.Conv2D(64, (4,4), padding=\"valid\", activation = 'relu'),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.BatchNormalization(),\n \n    layers.Flatten(),\n    layers.Dense(64, activation = 'relu'),\n    layers.Dropout(0.15),\n    layers.Dense(2, activation = 'softmax')\n])\n\nmodel.compile(optimizer=tf.keras.optimizers.Adam(lr = 1e-5),\n              loss=tf.keras.losses.BinaryCrossentropy(),\n              metrics=['acc'])\n\nhistory = model.fit(train_batches,\n                    epochs=15,\n                    validation_data=val_batches)","metadata":{"_uuid":"2bc63626-7a47-45ee-bcb2-efde981fe7a7","_cell_guid":"cba59394-4493-46ed-9032-92f1d9bf3e43","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:03:50.338180Z","iopub.execute_input":"2024-02-14T08:03:50.338978Z","iopub.status.idle":"2024-02-14T08:06:00.692180Z","shell.execute_reply.started":"2024-02-14T08:03:50.338943Z","shell.execute_reply":"2024-02-14T08:06:00.691284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"224eeed2-1ed3-433a-8fd1-cdfa60b17d11","_cell_guid":"36a77125-1bb9-4452-9329-121f34d38336","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Assuming you have a TensorFlow model named 'model'\nmodel_json = model.to_json()\n\n# Save the model architecture in JSON format\nwith open(\"model.json\", \"w\") as json_file:\n    json_file.write(model_json)\n    \nweights = [np.array(w) for w in model.get_weights()]\n\n# Save weights to a binary file\nwith open(\"model_weights.bin\", \"wb\") as binary_file:\n    for weight in weights:\n        binary_file.write(weight.tobytes())\n","metadata":{"_uuid":"22d5e71f-6709-4fe9-81ab-4a23e1860bd4","_cell_guid":"8aa5aaf2-8f73-4ffb-8567-0f918a9c8624","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:06:00.695159Z","iopub.execute_input":"2024-02-14T08:06:00.695540Z","iopub.status.idle":"2024-02-14T08:06:00.725709Z","shell.execute_reply.started":"2024-02-14T08:06:00.695509Z","shell.execute_reply":"2024-02-14T08:06:00.724527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load Json\n# Load the model architecture from the JSON file\nwith open(\"model.json\", \"r\") as json_file:\n    loaded_model_json = json_file.read()\n\nloaded_model = tf.keras.models.model_from_json(loaded_model_json)\n\n# Load the weights into the model\nwith open(\"model_weights.bin\", \"rb\") as bin_file:\n    for layer in loaded_model.layers:\n        if isinstance(layer, tf.keras.layers.BatchNormalization):\n            # For BatchNormalization layers, load gamma and beta\n            gamma_beta = np.fromfile(bin_file, dtype=np.float32, count=2 * layer.input_shape[-1])\n            gamma = gamma_beta[:layer.input_shape[-1]]\n            beta = gamma_beta[layer.input_shape[-1]:]\n            moving_mean = np.fromfile(bin_file, dtype=np.float32, count=layer.input_shape[-1])\n            moving_variance = np.fromfile(bin_file, dtype=np.float32, count=layer.input_shape[-1])\n\n            layer.set_weights([gamma, beta, moving_mean, moving_variance])\n        else:\n            # For other layers, load weights as usual\n            layer_weights = [np.fromfile(bin_file, dtype=np.float32, count=np.prod(param.shape)).reshape(param.shape)\n                             for param in layer.trainable_variables]\n            layer.set_weights(layer_weights)","metadata":{"execution":{"iopub.status.busy":"2024-02-14T08:06:00.727076Z","iopub.execute_input":"2024-02-14T08:06:00.727738Z","iopub.status.idle":"2024-02-14T08:06:00.912994Z","shell.execute_reply.started":"2024-02-14T08:06:00.727699Z","shell.execute_reply":"2024-02-14T08:06:00.912153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loaded_model.compile(optimizer=tf.keras.optimizers.Adam(lr = 1e-5),\n              loss=tf.keras.losses.BinaryCrossentropy(),\n              metrics=['acc'])\nprint(\"Original: -\\n\")\nloss, acc = model.evaluate_generator(test_batches, verbose=1)\nprint(\"Loss: \", loss)\nprint(\"Accuracy: \", acc)\nprint(\"Loaded: -\\n\")\nloss, acc = loaded_model.evaluate_generator(test_batches, verbose=1)\nprint(\"Loss: \", loss)\nprint(\"Accuracy: \", acc)","metadata":{"_uuid":"f053c127-f24d-4120-9ad1-25db4d312486","_cell_guid":"9f326646-f98c-4051-b957-3c90a5f05a0d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:06:00.914136Z","iopub.execute_input":"2024-02-14T08:06:00.914481Z","iopub.status.idle":"2024-02-14T08:06:04.486172Z","shell.execute_reply.started":"2024-02-14T08:06:00.914454Z","shell.execute_reply":"2024-02-14T08:06:04.485154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\ndef predict_class(path):\n    img = cv2.imread(path)\n\n    RGBImg = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n    RGBImg= cv2.resize(RGBImg,(224,224))\n    plt.imshow(RGBImg)\n    image = np.array(RGBImg) / 255.0\n#     new_model = tf.keras.models.load_model(\"64x3-CNN.model\")\n    predict=loaded_model.predict(np.array([image]))\n    per=np.argmax(predict,axis=1)\n    if per==1:\n        print('Diabetic Retinopathy Not Detected')\n    else:\n        print('Diabetic Retinopathy Detected')","metadata":{"_uuid":"424841e0-9269-477a-9c97-1375e5811491","_cell_guid":"276d5cf0-144e-406c-b2da-5bb39acc6c47","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:06:04.487467Z","iopub.execute_input":"2024-02-14T08:06:04.487758Z","iopub.status.idle":"2024-02-14T08:06:04.494646Z","shell.execute_reply.started":"2024-02-14T08:06:04.487732Z","shell.execute_reply":"2024-02-14T08:06:04.493696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_class('/kaggle/input/diabetic-retinopathy-224x224-gaussian-filtered/gaussian_filtered_images/gaussian_filtered_images/Severe/1b495ac025b7.png')","metadata":{"_uuid":"d9abd59e-8f36-4bc0-8a2a-8b512c7660b0","_cell_guid":"72ef977c-868f-47d5-8b8a-e6044b5f9ad8","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-02-14T08:06:04.495924Z","iopub.execute_input":"2024-02-14T08:06:04.496291Z","iopub.status.idle":"2024-02-14T08:06:05.002921Z","shell.execute_reply.started":"2024-02-14T08:06:04.496258Z","shell.execute_reply":"2024-02-14T08:06:05.002006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"ec4425da-4592-458d-af20-7ddf97c28f1a","_cell_guid":"3a4f8a50-e199-4e24-9b18-7d6a9c38076e","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]}]}