{"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":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"},{"sourceId":9971252,"sourceType":"datasetVersion","datasetId":6134503},{"sourceId":168840,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":143635,"modelId":166224},{"sourceId":168854,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":143648,"modelId":166236},{"sourceId":169386,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":144114,"modelId":166690},{"sourceId":172576,"sourceType":"modelInstanceVersion","modelInstanceId":146899,"modelId":169420},{"sourceId":172909,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":147194,"modelId":169718},{"sourceId":173670,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":147827,"modelId":170354},{"sourceId":173702,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":147851,"modelId":170379}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import load_model\nimport tensorflow_hub as hub\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nimport os\nfrom collections import Counter\nimport shutil\nimport matplotlib.pyplot as plt\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-21T14:17:47.979806Z","iopub.execute_input":"2024-11-21T14:17:47.980248Z","iopub.status.idle":"2024-11-21T14:17:47.985901Z","shell.execute_reply.started":"2024-11-21T14:17:47.980212Z","shell.execute_reply":"2024-11-21T14:17:47.984931Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Exploration","metadata":{}},{"cell_type":"code","source":"train_image_path = '/kaggle/input/cassava-leaf-disease-classification/train_images'\ntrain_labels = '/kaggle/input/cassava-leaf-disease-classification/train.csv'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T14:07:33.845901Z","iopub.execute_input":"2024-11-21T14:07:33.846344Z","iopub.status.idle":"2024-11-21T14:07:33.851131Z","shell.execute_reply.started":"2024-11-21T14:07:33.846308Z","shell.execute_reply":"2024-11-21T14:07:33.849969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(train_labels)\ntrain_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T14:09:37.374800Z","iopub.execute_input":"2024-11-21T14:09:37.375211Z","iopub.status.idle":"2024-11-21T14:09:37.413876Z","shell.execute_reply.started":"2024-11-21T14:09:37.375177Z","shell.execute_reply":"2024-11-21T14:09:37.412642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_count = train_data['label'].value_counts().sort_index()\ndata_count","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T14:13:35.164911Z","iopub.execute_input":"2024-11-21T14:13:35.165298Z","iopub.status.idle":"2024-11-21T14:13:35.174925Z","shell.execute_reply.started":"2024-11-21T14:13:35.165266Z","shell.execute_reply":"2024-11-21T14:13:35.173711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 6))\nplt.bar(data_count.index, data_count.values, width=0.6, edgecolor='black')\nplt.xlabel('Labels', fontsize=14)\nplt.ylabel('Counts', fontsize=14)\nplt.title('Distribution of Labels in Train Data', fontsize=16)\nplt.xticks(data_count.index, fontsize=12)\nplt.yticks(fontsize=12)\nplt.grid(axis='y', linestyle='--', alpha=0.7)\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T14:15:57.196534Z","iopub.execute_input":"2024-11-21T14:15:57.196935Z","iopub.status.idle":"2024-11-21T14:15:57.495941Z","shell.execute_reply.started":"2024-11-21T14:15:57.196902Z","shell.execute_reply":"2024-11-21T14:15:57.494928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_classes = [0, 1, 2, 3, 4]\n\nunique_images = train_data[train_data['label'].isin(target_classes)].groupby('label').first().reset_index()\n\n# Dictionary to store one image per target class\nclass_images = {}\n\nfor _, row in unique_images.iterrows():\n    image_id = row['image_id']\n    label = row['label']\n    image_path = os.path.join(train_image_path, image_id)\n    \n    if os.path.exists(image_path):\n        # Load the image\n        image = Image.open(image_path)\n        class_images[label] = image\n    else:\n        print(f\"Image {image_id} not found at {image_path}\")\n\n# Show images for the specified classes only\nfor label in target_classes:\n    if label in class_images:\n        plt.figure()\n        plt.imshow(class_images[label])\n        plt.axis('off')\n        plt.title(f\"Class: {label}\")\n        plt.show()\n    else:\n        print(f\"No image found for class {label}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T14:23:12.614909Z","iopub.execute_input":"2024-11-21T14:23:12.615401Z","iopub.status.idle":"2024-11-21T14:23:14.295682Z","shell.execute_reply.started":"2024-11-21T14:23:12.615356Z","shell.execute_reply":"2024-11-21T14:23:14.294580Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Directories","metadata":{}},{"cell_type":"code","source":"# Define source and destination directories\nsource_dir = '/kaggle/input/cp-model'\ndest_dir = '/kaggle/working/cp-model'\n\n# Remove the destination directory if it already exists\nif os.path.exists(dest_dir):\n    shutil.rmtree(dest_dir)\n\n# Copy the cached model to a writable location\nshutil.copytree(source_dir, dest_dir)\n\n# Set up the cache directory to point to the writable location\nos.environ[\"TFHUB_CACHE_DIR\"] = dest_dir\n\n# Load the model from the cache\nmodel_url = \"https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2\"\nclassifier = hub.load(model_url)\n\nprint(\"Model loaded successfully from writable cache directory!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-21T09:39:15.206402Z","iopub.execute_input":"2024-11-21T09:39:15.207385Z","iopub.status.idle":"2024-11-21T09:39:17.896173Z","shell.execute_reply.started":"2024-11-21T09:39:15.207340Z","shell.execute_reply":"2024-11-21T09:39:17.895117Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the trained model and test directories\nmodel_path_1 = '/kaggle/input/combinedmodel3/tensorflow2/default/1/BestModel_3454_8937.h5'\nmodel_path_2 = '/kaggle/input/combinedmodel3/tensorflow2/default/1/best_model_0.37458707.h5'\nmodel_path_3 = '/kaggle/input/combinedmodel3/tensorflow2/default/1/googlenet_inceptionv3.h5'\nmodel_path_4 = '/kaggle/input/bestmodel_550_2/tensorflow2/default/1/BestModel_3577_8940.h5'\nmodel_path_5 = '/kaggle/input/bestmodel_8878/tensorflow2/default/1/BestModel_8878_0358.h5'\nmodel_path_6 = '/kaggle/input/bestmodel_8875/tensorflow2/default/1/BestModel_8875.h5'\nmodel_path_7 = '/kaggle/input/googlenet_512/tensorflow2/default/1/GOOG1.h5'\nmodel_path_8 = classifier\ntest_image_dir = '/kaggle/input/cassava-leaf-disease-classification/test_images'\nsample = '/kaggle/input/cassava-leaf-disease-classification/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2024-11-21T09:43:23.023322Z","iopub.execute_input":"2024-11-21T09:43:23.023797Z","iopub.status.idle":"2024-11-21T09:43:23.029704Z","shell.execute_reply.started":"2024-11-21T09:43:23.023755Z","shell.execute_reply":"2024-11-21T09:43:23.028749Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_csv = pd.read_csv(sample)\nsample_csv","metadata":{"execution":{"iopub.status.busy":"2024-11-21T09:43:28.454731Z","iopub.execute_input":"2024-11-21T09:43:28.455730Z","iopub.status.idle":"2024-11-21T09:43:28.483347Z","shell.execute_reply.started":"2024-11-21T09:43:28.455624Z","shell.execute_reply":"2024-11-21T09:43:28.482353Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load Model","metadata":{}},{"cell_type":"code","source":"# Paths to each model and their respective input sizes\nmodels_info = [\n    (model_path_1, (550, 550)),\n    (model_path_2, (512, 512)),\n    (model_path_3, (448, 448)),\n    (model_path_6, (512, 512)),\n]\n\n# Load models with specified input sizes\nmodels = [(load_model(path), input_size) for path, input_size in models_info]\nmodels.append((model_path_8, (224, 224)))","metadata":{"execution":{"iopub.status.busy":"2024-11-21T09:57:12.804042Z","iopub.execute_input":"2024-11-21T09:57:12.805083Z","iopub.status.idle":"2024-11-21T09:57:15.889365Z","shell.execute_reply.started":"2024-11-21T09:57:12.805040Z","shell.execute_reply":"2024-11-21T09:57:15.888447Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_labels = {\n    0: 'Cassava Bacterial Blight (CBB)',\n    1: 'Cassava Brown Streak Disease (CBSD)',\n    2: 'Cassava Green Mottle (CGM)',\n    3: 'Cassava Mosaic Disease (CMD)',\n    4: 'Healthy'\n}\n\nimage_predictions = []\n\nfor image_id in os.listdir(test_image_dir):\n    if image_id.endswith(('.jpg', '.jpeg', '.png')):\n        model_predictions = []\n        confidence_scores = {}\n\n        for model, input_size in models:\n            img = load_img(os.path.join(test_image_dir, image_id), target_size=input_size)\n            img_array = np.expand_dims(img_to_array(img) / 255.0, axis=0)\n            \n            if (model == model_path_8):\n                # Get prediction from the CropNet model\n                predictions = classifier(img_array, training=False)  # Add training=False\n                predicted_class = tf.math.argmax(predictions, axis=-1).numpy()[0]  # Get the predicted class index\n                #print(predicted_class)\n            else:\n                predictions = model.predict(img_array)\n                predicted_class = np.argmax(predictions, axis=1)[0]\n                \n            confidence_score = predictions[0][predicted_class]\n            \n            model_predictions.append(predicted_class)\n            if predicted_class not in confidence_scores:\n                confidence_scores[predicted_class] = []\n            confidence_scores[predicted_class].append(confidence_score)\n\n        #print(model_predictions)\n        class_votes = Counter(model_predictions)\n        most_common = class_votes.most_common()\n        final_predicted_class = most_common[0][0]\n        \n        if len(most_common) > 1 and most_common[0][1] == most_common[1][1]:\n            tied_classes = [cls for cls, count in most_common if count == most_common[0][1]]\n            for cls in tied_classes:\n                avg_confidence = sum(confidence_scores[cls]) / len(confidence_scores[cls])\n                #print(f\"Class: {cls}, Confidence Scores: {confidence_scores[cls]}, Average Confidence: {avg_confidence}\")\n            \n            final_predicted_class = max(\n                tied_classes,\n                key=lambda cls: sum(confidence_scores[cls]) / len(confidence_scores[cls])\n            )\n\n        image_predictions.append({'image_id': image_id, 'label': final_predicted_class})\n\nsubmission_df = pd.DataFrame(image_predictions)\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-21T09:57:18.593498Z","iopub.execute_input":"2024-11-21T09:57:18.594531Z","iopub.status.idle":"2024-11-21T09:57:21.408299Z","shell.execute_reply.started":"2024-11-21T09:57:18.594485Z","shell.execute_reply":"2024-11-21T09:57:21.407393Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-11-21T09:57:22.066044Z","iopub.execute_input":"2024-11-21T09:57:22.066505Z","iopub.status.idle":"2024-11-21T09:57:22.079373Z","shell.execute_reply.started":"2024-11-21T09:57:22.066452Z","shell.execute_reply":"2024-11-21T09:57:22.077878Z"},"trusted":true},"outputs":[],"execution_count":null}]}