{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":9946347,"sourceType":"datasetVersion","datasetId":6028009},{"sourceId":9975264,"sourceType":"datasetVersion","datasetId":6086667},{"sourceId":171571,"sourceType":"modelInstanceVersion","modelInstanceId":138508,"modelId":161161},{"sourceId":173654,"sourceType":"modelInstanceVersion","modelInstanceId":147789,"modelId":170317}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nfrom torchvision.models import vit_h_14,efficientnet_v2_l\nfrom torchvision import transforms\nfrom torch.utils.data import Dataset\nimport pandas as pd\nfrom PIL import Image\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.models import load_model\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.tree import DecisionTreeClassifier\nimport os\nfrom torchvision.transforms import v2\n\ntorch.cuda.empty_cache()\n\ndef tf_transforms(image):\n    image = tf.image.resize(image, [512, 512])\n    image = tf.cast(image, tf.float32) / 255.0\n    mean = [0.5, 0.5, 0.5]\n    std = [0.5, 0.5, 0.5]\n    image = (image - mean) / std\n    image = np.expand_dims(image, axis=0)\n    return image\n\n\ntorch_transforms_ResNet = transforms.Compose([\n    transforms.Resize((512, 512)),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n])\n\n# torch_transforms_VIT = transforms.Compose([\n#     transforms.Resize((518, 518)),\n#     transforms.ToTensor(),\n#     transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])\n# ])\n\ndef invert_square_pad(img): \n    width, height = img.size \n \n    # Perform cyclic shift \n    img = torch.tensor(np.array(img)).permute(2, 0, 1)  # Convert to tensor and permute to (C, H, W) \n    img = torch.roll(img, shifts=(height // 2, width // 2), dims=(1, 2))  # Cyclic shift \n \n    # Convert back to PIL image \n    img = transforms.functional.to_pil_image(img) \n \n    max_side = max(width, height) \n    padding = ( \n        (max_side - width) // 2, \n        (max_side - height) // 2, \n        (max_side - width) - (max_side - width) // 2, \n        (max_side - height) - (max_side - height) // 2 \n    )  # left, top, right, bottom \n \n    padded_img = transforms.functional.pad(img, padding, padding_mode='reflect') \n \n    return padded_img \n \ndef resize_max_side(img, size): \n    height, width = img.size \n    if max(width, height) > size: \n        return img \n \n    if width > height: \n        new_width = size \n        new_height = int(size * height / width) \n    else: \n        new_height = size \n        new_width = int(size * width / height) \n \n    return transforms.functional.resize(img, (new_width, new_height))\n\ntorch_transforms_VIT = transforms.Compose([ \n    v2.Lambda(invert_square_pad), \n    v2.ToImage(), \n    v2.ToDtype(torch.float32, scale=True), \n    v2.Resize((518, 518)), \n    v2.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) \n])\n\ntorch_transforms_EfficientNet = transforms.Compose([ \n    v2.ToImage(), \n    v2.ToDtype(torch.float32, scale=True), \n    v2.Resize((480, 480)), \n    v2.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) \n])","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:33.863263Z","iopub.execute_input":"2024-11-21T17:08:33.863977Z","iopub.status.idle":"2024-11-21T17:08:33.883804Z","shell.execute_reply.started":"2024-11-21T17:08:33.863944Z","shell.execute_reply":"2024-11-21T17:08:33.882839Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n\nmodel3 = torch.load(\"/kaggle/input/model/pytorch/default/4/vit_h_14_518_8907_ISP_CBP_model.pth\")\nmodel3.to(device)\n\nmodel4 = torch.load('/kaggle/input/model/pytorch/default/4/efficientnet_v2_l_480_8591_ISP_CBP_model.pth')\nmodel4.to(device)\n\nmodel1 = load_model(\"/kaggle/input/model/pytorch/default/4/Densenet_70_512x512_F (1).keras\")\n\nmodel2 = torch.load(\"/kaggle/input/resnet50_70_512x512/pytorch/default/2/Resnet50_70_512x512_F.pth\")\nmodel2.to(device)\n\n\nprint(\"Models loaded!!!\")\n","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:33.885299Z","iopub.execute_input":"2024-11-21T17:08:33.885615Z","iopub.status.idle":"2024-11-21T17:08:48.879015Z","shell.execute_reply.started":"2024-11-21T17:08:33.885589Z","shell.execute_reply":"2024-11-21T17:08:48.878119Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# #For making decision tree probs\n# csv_path = '/kaggle/input/train-percent/train70percent (1).csv'\n# image_dir = '/kaggle/input/cassava-leaf-disease-classification/train_images'\n# df = pd.read_csv(csv_path)\n# train_df, val_df = train_test_split(df,\n#                                     random_state=22,\n#                                     test_size = 0.1)\n# train_images = np.array(train_df[\"image_id\"])\n# train_labels = np.array(train_df[\"label\"])\n# val_images = np.array(val_df[\"image_id\"])\n# val_labels = np.array(val_df[\"label\"])\n\n# train_predictions = []\n# val_predictions = []\n\n# count = 0\n# length = len(train_images)\n# for image_id in train_images:\n#     image = Image.open(f\"{image_dir}/{image_id}\").convert(\"RGB\")\n    \n#     image1 = tf_transforms(image)\n    \n#     image2 = torch_transforms_ResNet(image)\n#     image2 = image2.unsqueeze(0).to(device)\n\n#     image3 = torch_transforms_VIT(image)\n#     image3 = image3.unsqueeze(0).to(device)\n    \n#     image4 = torch_transforms_EfficientNet(image)\n#     image4 = image4.unsqueeze(0).to(device)\n\n#     train_prediction_1 = np.array(model1.predict(image1,verbose=False)[0])\n\n#     with torch.no_grad():\n#         train_prediction_2 = np.array(model2(image2).cpu()[0])\n#         train_prediction_3 = np.array(model3(image3).cpu()[0])\n#         train_prediction_4 = np.array(model4(image4).cpu()[0])\n        \n#     del image\n#     del image1\n#     del image2\n#     del image3\n#     del image4\n\n#     train_predictions.append(np.concatenate((train_prediction_1,train_prediction_2, train_prediction_3, train_prediction_4)))\n#     count+=1\n#     print(f\"Count:{count}/{length}\", end=\"\\r\")\n\n    \n# train_tree_csv = pd.DataFrame(train_predictions)\n# train_tree_csv.columns = [\n#                           'DN0', 'DN1', 'DN2', 'DN3', 'DN4', \n#                           'RN0', 'RN1', 'RN2', 'RN3', 'RN4',\n#                           'VIT0', 'VIT1', 'VIT2', 'VIT3', 'VIT4',\n#                           'EN0', 'EN1', 'EN2', 'EN3', 'EN4',\n#                          ]\n# train_tree_csv.insert(0,'image_id',train_images)\n# train_tree_csv['label'] = train_labels\n# train_tree_csv.to_csv(\"train_tree_F_4.csv\", index=False)\n\n# count = 0\n# length = len(val_images)\n# for image_id in val_images:\n#     image = Image.open(f\"{image_dir}/{image_id}\").convert(\"RGB\")\n\n#     image1 = tf_transforms(image)\n\n#     image2 = torch_transforms_ResNet(image)\n#     image2 = image2.unsqueeze(0).to(device)\n\n#     image3 = torch_transforms_VIT(image)\n#     image3 = image3.unsqueeze(0).to(device)\n\n#     image4 = torch_transforms_EfficientNet(image)\n#     image4 = image4.unsqueeze(0).to(device)\n\n#     val_prediction_1 = np.array(model1.predict(image1,verbose=False)[0])\n\n#     with torch.no_grad():\n#         val_prediction_2 = np.array(model2(image2).cpu()[0])\n#         val_prediction_3 = np.array(model3(image3).cpu()[0])\n#         val_prediction_4 = np.array(model4(image4).cpu()[0])\n\n#     del image\n#     del image1\n#     del image2\n#     del image3\n#     del image4\n    \n#     val_predictions.append(np.concatenate((val_prediction_1,val_prediction_2, val_prediction_3, val_prediction_4)))\n#     count+=1\n#     print(f\"Count:{count}/{length}\", end=\"\\r\")\n    \n    \n\n# val_tree_csv = pd.DataFrame(val_predictions)\n# val_tree_csv.columns = [\n#                         'DN0', 'DN1', 'DN2', 'DN3', 'DN4', \n#                         'RN0', 'RN1', 'RN2', 'RN3', 'RN4',\n#                         'VIT0', 'VIT1', 'VIT2', 'VIT3', 'VIT4',\n#                         'EN0', 'EN1', 'EN2', 'EN3', 'EN4',\n#                         ]\n\n                        \n# val_tree_csv.insert(0,'image_id',val_images)\n# val_tree_csv['label'] = val_labels\n# val_tree_csv.to_csv(\"val_tree_F_4.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:48.880654Z","iopub.execute_input":"2024-11-21T17:08:48.881218Z","iopub.status.idle":"2024-11-21T17:08:48.887556Z","shell.execute_reply.started":"2024-11-21T17:08:48.881146Z","shell.execute_reply":"2024-11-21T17:08:48.886846Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#Test\n\nimage_dir = '/kaggle/input/cassava-leaf-disease-classification/test_images'\nimage_ids = os.listdir(image_dir)\n\ncombined_output = []\nprediction  = []\n\ncount = 0\nlength = len(image_ids)\nfor image_id in image_ids:\n    image = Image.open(f\"{image_dir}/{image_id}\").convert(\"RGB\")\n\n    image1 = tf_transforms(image)\n\n    image2 = torch_transforms_ResNet(image)\n    image2 = image2.unsqueeze(0).to(device)\n\n    image3 = torch_transforms_VIT(image)\n    image3 = image3.unsqueeze(0).to(device)\n\n    image4 = torch_transforms_EfficientNet(image)\n    image4 = image4.unsqueeze(0).to(device)\n\n    train_prediction_1 = np.array(model1.predict(image1,verbose=False)[0])\n\n    with torch.no_grad():\n        train_prediction_2 = np.array(model2(image2).cpu()[0])\n        train_prediction_3 = np.array(model3(image3).cpu()[0])\n        train_prediction_4 = np.array(model4(image4).cpu()[0])\n        del image\n\n    combined_output.append(np.concatenate((train_prediction_1,train_prediction_2, train_prediction_3, train_prediction_4)))\n    \n    count+=1\n    print(f\"Count:{count}/{length}\", end=\"\\r\")","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:48.889196Z","iopub.execute_input":"2024-11-21T17:08:48.889485Z","iopub.status.idle":"2024-11-21T17:08:55.690130Z","shell.execute_reply.started":"2024-11-21T17:08:48.889459Z","shell.execute_reply":"2024-11-21T17:08:55.689219Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_probs = pd.read_csv('/kaggle/input/train-tree/train_tree_F_4.csv')\ntrain_labels = train_probs[\"label\"]\ntrain_probs = train_probs.iloc[:,1:-1]\ntrain_probs = np.array(train_probs)\n\n# from sklearn.tree import DecisionTreeClassifier\n\n# decision_tree = DecisionTreeClassifier(criterion='gini', \n#                                        max_depth=5,\n#                                        # min_samples_split=3,\n#                                       )\n# decision_tree.fit(train_probs, train_labels)\n\n\nfrom sklearn.ensemble import RandomForestClassifier\ndecision_tree = RandomForestClassifier(\n    n_estimators=40,       \n    criterion='gini',       \n    max_depth=8,\n    random_state=11\n)\ndecision_tree.fit(train_probs, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:55.691338Z","iopub.execute_input":"2024-11-21T17:08:55.691630Z","iopub.status.idle":"2024-11-21T17:08:57.568391Z","shell.execute_reply.started":"2024-11-21T17:08:55.691603Z","shell.execute_reply":"2024-11-21T17:08:57.567537Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = decision_tree.predict(combined_output)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T17:08:57.569640Z","iopub.execute_input":"2024-11-21T17:08:57.569969Z","iopub.status.idle":"2024-11-21T17:08:57.578047Z","shell.execute_reply.started":"2024-11-21T17:08:57.569941Z","shell.execute_reply":"2024-11-21T17:08:57.577202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"image_id\":image_ids,\n    \"label\": prediction\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:57.579094Z","iopub.execute_input":"2024-11-21T17:08:57.579385Z","iopub.status.idle":"2024-11-21T17:08:57.592917Z","shell.execute_reply.started":"2024-11-21T17:08:57.579359Z","shell.execute_reply":"2024-11-21T17:08:57.592144Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import pandas as pd\n# import numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:57.593875Z","iopub.execute_input":"2024-11-21T17:08:57.594135Z","iopub.status.idle":"2024-11-21T17:08:57.599900Z","shell.execute_reply.started":"2024-11-21T17:08:57.594110Z","shell.execute_reply":"2024-11-21T17:08:57.599230Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train_probs = pd.read_csv('/kaggle/input/train-tree/train_tree_F_4.csv')\n# train_labels = train_probs[\"label\"]\n# train_probs = train_probs.iloc[:,1:-1]\n# train_probs = np.array(train_probs)\n\n# # from sklearn.tree import DecisionTreeClassifier\n\n# # decision_tree = DecisionTreeClassifier(criterion='gini', \n# #                                        max_depth=5,\n# #                                        # min_samples_split=3,\n# #                                       )\n# # decision_tree.fit(train_probs, train_labels)\n\n\n# from sklearn.ensemble import RandomForestClassifier\n# decision_tree = RandomForestClassifier(\n#     n_estimators=40,       \n#     criterion='gini',       \n#     max_depth=8,\n#     random_state=11\n# )\n# decision_tree.fit(train_probs, train_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:57.602510Z","iopub.execute_input":"2024-11-21T17:08:57.602791Z","iopub.status.idle":"2024-11-21T17:08:57.615824Z","shell.execute_reply.started":"2024-11-21T17:08:57.602766Z","shell.execute_reply":"2024-11-21T17:08:57.615159Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# val_probs = pd.read_csv('/kaggle/input/train-tree/val_tree_F_4.csv')\n# val_probs = val_probs.iloc[:,1:]\n# val_labels = []\n# val_probs_arr = [val_probs.sample(n=50, random_state=i) for i in range(20)]\n# val_probs_arr_D =[]\n# val_probs_arr_R =[]\n# val_probs_arr_V =[]\n# val_probs_arr_E =[]\n# for i in range(len(val_probs_arr)):\n#     val_labels.append(val_probs_arr[i]['label'])\n#     val_probs_arr_D.append(np.array(val_probs_arr[i].iloc[:, :5]))\n#     val_probs_arr_R.append(np.array(val_probs_arr[i].iloc[:, 5:10]))\n#     val_probs_arr_V.append(np.array(val_probs_arr[i].iloc[:, 10:15]))\n#     val_probs_arr_E.append(np.array(val_probs_arr[i].iloc[:, 15:-1]))\n#     val_probs_arr[i] = np.array(val_probs_arr[i].iloc[:, :-1])\n\n          \n#     # val_probs_arr_D.append(np.array(val_probs_arr[i].iloc[:, :5]))\n#     # # val_probs_arr_R.append(np.array(val_probs_arr[i].iloc[:, 5:10]))\n#     # val_probs_arr_V.append(np.array(val_probs_arr[i].iloc[:, 5:-1]))\n#     # # val_probs_arr_E.append(np.array(val_probs_arr[i].iloc[:, 15:-1]))\n#     # val_probs_arr[i] = np.array(val_probs_arr[i].iloc[:, :-1])\n","metadata":{"execution":{"iopub.status.busy":"2024-11-21T17:08:57.617034Z","iopub.execute_input":"2024-11-21T17:08:57.617339Z","iopub.status.idle":"2024-11-21T17:08:57.626521Z","shell.execute_reply.started":"2024-11-21T17:08:57.617312Z","shell.execute_reply":"2024-11-21T17:08:57.625759Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.metrics import accuracy_score\n# sum = 0\n# sumD = 0\n# sumR = 0\n# sumV = 0\n# sumE = 0\n\n# for i in range(len(val_probs_arr)):\n#     y_pred_D = []\n#     y_pred_R = []\n#     y_pred_V = []\n#     y_pred_E = []\n\n#     y_pred = decision_tree.predict(val_probs_arr[i])\n#     accuracy = accuracy_score(val_labels[i], y_pred)\n#     # print(f'Accuracy (With Decision Tree, sample: {i}): {accuracy:.2f}')\n#     sum+=accuracy\n\n#     for ii in range(len(val_probs_arr_D[i])):\n#         y_pred_D.append(np.argmax(val_probs_arr_D[i][ii]))\n#         y_pred_R.append(np.argmax(val_probs_arr_R[i][ii]))\n#         y_pred_V.append(np.argmax(val_probs_arr_V[i][ii]))\n#         y_pred_E.append(np.argmax(val_probs_arr_E[i][ii]))\n        \n#     accuracy = accuracy_score(val_labels[i], y_pred_D)\n#     sumD +=accuracy\n\n#     accuracy = accuracy_score(val_labels[i], y_pred_R)\n#     sumR +=accuracy\n    \n#     accuracy = accuracy_score(val_labels[i], y_pred_V)\n#     sumV +=accuracy\n\n#     accuracy = accuracy_score(val_labels[i], y_pred_V)\n#     sumE +=accuracy\n\n#     # print()\n\n# # print()\n# # print()\n# # print()\n# print(f'Accuracy (With Random Forest Average): {sum/20:.4f}')\n# print(f'Accuracy DenseNet: {sumD/20:.4f}')\n# print(f'Accuracy ResNet: {sumR/20:.4f}')\n# print(f'Accuracy VIT: {sumV/20:.4f}')\n# print(f'Accuracy Efficient: {sumE/20:.4f}')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T17:08:57.627442Z","iopub.execute_input":"2024-11-21T17:08:57.627721Z","iopub.status.idle":"2024-11-21T17:08:57.636803Z","shell.execute_reply.started":"2024-11-21T17:08:57.627684Z","shell.execute_reply":"2024-11-21T17:08:57.636066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}