{"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":159233,"sourceType":"modelInstanceVersion","modelInstanceId":135368,"modelId":158093},{"sourceId":159683,"sourceType":"modelInstanceVersion","modelInstanceId":135755,"modelId":158486},{"sourceId":160331,"sourceType":"modelInstanceVersion","modelInstanceId":136327,"modelId":159051},{"sourceId":160357,"sourceType":"modelInstanceVersion","modelInstanceId":136351,"modelId":159075},{"sourceId":160631,"sourceType":"modelInstanceVersion","modelInstanceId":136587,"modelId":159313},{"sourceId":170146,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":144763,"modelId":167320}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-11-08T19:02:12.220720Z","iopub.execute_input":"2024-11-08T19:02:12.221407Z","iopub.status.idle":"2024-11-08T19:02:12.226337Z","shell.execute_reply.started":"2024-11-08T19:02:12.221360Z","shell.execute_reply":"2024-11-08T19:02:12.225286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import os\n# import re\n# from datetime import datetime\n# import numpy as np\n# import pandas as pd\n# import matplotlib.pyplot as plt\n\n# import tensorflow as tf\n# import tensorflow_hub as hub","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T05:56:59.270352Z","iopub.execute_input":"2024-11-09T05:56:59.271313Z","iopub.status.idle":"2024-11-09T05:56:59.276159Z","shell.execute_reply.started":"2024-11-09T05:56:59.271264Z","shell.execute_reply":"2024-11-09T05:56:59.275116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split\n# from tensorflow.keras.preprocessing.image import ImageDataGenerator\n# import matplotlib.pyplot as plt\n# from tensorflow.keras.applications.efficientnet import preprocess_input\n# from tensorflow.keras.applications import EfficientNetB0\n# from tensorflow.keras.layers import Dense, GlobalAveragePooling2D\n# from tensorflow.keras.models import Model\n# from sklearn.preprocessing import LabelEncoder\n\n# label_to_disease = pd.read_json('/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json', typ='series')\n# train_csv = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\n\n# train_csv['disease'] = train_csv['label'].map(label_to_disease)\n# train_csv['path'] = '/kaggle/input/cassava-leaf-disease-classification/train_images/' + train_csv['image_id']\n\n# train_csv['label_encoded'] = LabelEncoder().fit_transform(train_csv['disease'])\n\n# # Convert 'disease' and 'label' columns to string type\n# train_csv['disease'] = train_csv['disease'].astype(str)\n# train_csv['label'] = train_csv['label'].astype(str)\n\n# # Split the data into train and validation sets with stratified sampling\n# train, valid = train_test_split(train_csv, test_size=0.2, stratify=train_csv['label'])\n\n\n# # Data augmentation and preprocessing for training\n# datagen_aug = ImageDataGenerator(\n#     preprocessing_function=preprocess_input,\n#     rotation_range=45,\n#     width_shift_range=0.2,\n#     height_shift_range=0.2,\n#     shear_range=0.2,\n#     zoom_range=0.2,\n#     horizontal_flip=True,\n#     vertical_flip=True,\n#     fill_mode='nearest'\n# )\n\n# # Generator for the training set\n# train_generator = datagen_aug.flow_from_dataframe(\n#     dataframe=train,\n#     x_col='path',\n#     y_col='disease',\n#     target_size=(224, 224),\n#     batch_size=32,\n#     class_mode='categorical',\n#     shuffle=True\n# )\n\n# # Generator for the validation set without augmentation\n# datagen_valid = ImageDataGenerator(preprocessing_function=preprocess_input)\n\n# valid_generator = datagen_valid.flow_from_dataframe(\n#     dataframe=valid,\n#     x_col='path',\n#     y_col='disease',\n#     target_size=(224, 224),\n#     batch_size=32,\n#     class_mode='categorical',\n#     shuffle=False\n# )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T05:56:59.277683Z","iopub.execute_input":"2024-11-09T05:56:59.278052Z","iopub.status.idle":"2024-11-09T05:58:01.830424Z","shell.execute_reply.started":"2024-11-09T05:56:59.278011Z","shell.execute_reply":"2024-11-09T05:58:01.829409Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# def load_and_preprocess_image(path, label):\n#     image = tf.io.read_file(path)\n#     image = tf.image.decode_jpeg(image, channels=3)  # Assuming the images are JPEGs\n#     image = tf.image.resize(image, [224, 224])  # Resize to the expected input size\n#     image = image / 255.0  # Normalize to [0, 1]\n#     return image, label\n\n# # Create datasets\n# train_ds = tf.data.Dataset.from_tensor_slices((train['path'].values, train['label_encoded'].values))\n# valid_ds = tf.data.Dataset.from_tensor_slices((valid['path'].values, valid['label_encoded'].values))\n\n# # Map the loading and preprocessing function to the datasets\n# train_ds = train_ds.map(load_and_preprocess_image).batch(16).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n# valid_ds = valid_ds.map(load_and_preprocess_image).batch(16).prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T05:58:01.831895Z","iopub.execute_input":"2024-11-09T05:58:01.832228Z","iopub.status.idle":"2024-11-09T05:58:02.571115Z","shell.execute_reply.started":"2024-11-09T05:58:01.832166Z","shell.execute_reply":"2024-11-09T05:58:02.570160Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from tensorflow.keras.models import Model, load_model\n# from tensorflow.keras.preprocessing.image import load_img, img_to_array\n# from tensorflow.keras.layers import Dense,BatchNormalization\n# from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, Callback\n\n# # since cannot load model, redefine and retrain lol \n# # Load CropNet feature extractor\n# import tensorflow as tf\n# import tensorflow_hub as hub\n# import numpy as np\n\n# from tensorflow.keras.applications import DenseNet169\n\n# class EarlyStoppingCallback(Callback):\n#     def on_epoch_end(self, epoch, logs=None):\n#         # Check if early stopping has been triggered by checking if patience has run out\n#         if self.model.stop_training:\n#             print(f\"Early stopping triggered at epoch {epoch + 1}.\")\n\n# early_stopping = EarlyStopping(\n#     monitor='val_loss', \n#     patience=3, \n#     restore_best_weights=True\n# )\n\n# learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(\n#     monitor='val_loss', \n#     patience=2, \n#     factor=0.5, \n#     min_lr=1e-6, \n#     verbose=1\n# )\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T05:58:41.715139Z","iopub.execute_input":"2024-11-09T05:58:41.715561Z","iopub.status.idle":"2024-11-09T05:58:41.724601Z","shell.execute_reply.started":"2024-11-09T05:58:41.715521Z","shell.execute_reply":"2024-11-09T05:58:41.723426Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# import tensorflow as tf\n# import tensorflow_hub as hub\n# from tensorflow.keras.layers import Dense, Input, Lambda\n# from tensorflow.keras.models import Model\n\n# # Define the URL for the pretrained feature extractor\n# model_url = 'https://tfhub.dev/google/cropnet/classifier/cassava_disease_V1/2'\n# cropnet_classifier = hub.KerasLayer(model_url, trainable=True)\n\n# # Define the model architecture with a Lambda wrapper for the KerasLayer\n# input_layer = Input(shape=(224, 224, 3))\n# cropnet_features = Lambda(lambda x: cropnet_classifier(x), output_shape=(6,))(input_layer)  # Replace (5,) with actual output shape\n\n# # Add additional Dense layers\n# x = Dense(1024, activation='relu')(cropnet_features)\n# x = BatchNormalization()(x)\n# x = Dense(512, activation='relu')(x)\n# x = BatchNormalization()(x)\n# x = Dense(256, activation='relu')(x)\n# x = Dense(128, activation='relu')(x)\n# x = Dense(64, activation='relu')(x)\n# output = Dense(5, activation='softmax')(x)\n\n\n# # Create Model\n# model = Model(inputs=input_layer, outputs=output)\n\n\n# # Compile Model\n# model.compile(\n#     optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5), # Change1: Apply Lower LR\n#     loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n#     metrics=['accuracy']\n# )\n\n# model.summary()\n\n# # train the model\n# history_feature_extraction = model.fit(\n#     train_ds,\n#     validation_data=valid_ds,\n#     epochs=30,\n#     callbacks=[early_stopping, learning_rate_reduction]\n# )\n\n# # evaluate the model\n# model.evaluate(valid_ds)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T05:58:43.704747Z","iopub.execute_input":"2024-11-09T05:58:43.705489Z","iopub.status.idle":"2024-11-09T06:16:14.940602Z","shell.execute_reply.started":"2024-11-09T05:58:43.705449Z","shell.execute_reply":"2024-11-09T06:16:14.939593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # # # Export the model as a TensorFlow SavedModel\n# model.export('/kaggle/working/model_feature_extraction_tf')\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T06:16:31.516040Z","iopub.execute_input":"2024-11-09T06:16:31.516856Z","iopub.status.idle":"2024-11-09T06:16:36.128839Z","shell.execute_reply.started":"2024-11-09T06:16:31.516814Z","shell.execute_reply":"2024-11-09T06:16:36.127863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !zip -r /kaggle/working/model_feature_extraction_normalized.zip /kaggle/working/model_feature_extraction_tf\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-09T06:16:48.947759Z","iopub.execute_input":"2024-11-09T06:16:48.948160Z","iopub.status.idle":"2024-11-09T06:16:51.211179Z","shell.execute_reply.started":"2024-11-09T06:16:48.948117Z","shell.execute_reply":"2024-11-09T06:16:51.210132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# Load the TFSMLayer as a layer\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Input, TFSMLayer\nfrom tensorflow.keras.models import Model\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\nimport os\n\n# frozen path\n# model_path = '/kaggle/input/cropnet_feature_selection_zip/tensorflow2/default/1/kaggle/working/model_feature_extraction_tf'\n\n# #  unfrozen path\n# model_path = '/kaggle/input/cropnet_feature_extraction_unfrozen/tensorflow2/default/1/kaggle/working/model_feature_extraction_tf'\n\n# 200 training unfrozen path\nmodel_path = '/kaggle/input/cropnet_from_kaggle/tensorflow2/default/1/kaggle/working/cropnet_model_tf'\nlayer = TFSMLayer(model_path, call_endpoint='serving_default')\n\n# Wrap TFSMLayer in a new model for prediction\ninput_layer = Input(shape=(224, 224, 3))\noutput_layer = layer(input_layer)\nmodel = Model(inputs=input_layer, outputs=output_layer)\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T04:32:06.083368Z","iopub.execute_input":"2024-11-18T04:32:06.083902Z","iopub.status.idle":"2024-11-18T04:32:22.976075Z","shell.execute_reply.started":"2024-11-18T04:32:06.083857Z","shell.execute_reply":"2024-11-18T04:32:22.974936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\n# Set image path and define empty lists to store data\nimage_dir = '/kaggle/input/cassava-leaf-disease-classification/test_images'\npredictions = []\nimage_names = []\n\n# Define image size as per model's expected input\nimg_size = (224, 224)\n\n# Process each image in the folder\nfor filename in os.listdir(image_dir):\n    if filename.endswith(\".jpg\"): \n        # Load and preprocess image\n        img_path = os.path.join(image_dir, filename)\n        img = load_img(img_path, target_size=img_size)\n        img_array = img_to_array(img) / 255.0  # Normalize image to [0, 1] range\n        img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension\n        \n        # Predict using the wrapped model\n        pred = model(img_array)\n        \n        # Inspect available keys in the prediction output\n        print(f\"Keys in prediction dictionary for {filename}: {pred.keys()}\")\n        \n        # Access the correct output using the first available key (adjust this if needed)\n        pred_output = list(pred.values())[0]  # Get the first tensor in the dictionary\n        \n        # Print the shape of the output tensor for debugging\n        print(f\"Prediction shape for {filename}: {pred_output.shape}\")\n        \n        # Determine the predicted class based on shape\n        if len(pred_output.shape) == 1:  # 1D output\n            predicted_class = np.argmax(pred_output)\n        else:  # 2D output\n            predicted_class = np.argmax(pred_output, axis=1)[0]\n        \n        predictions.append(predicted_class)\n        image_names.append(filename)\n\n# Create DataFrame for submission\nsubmission_df = pd.DataFrame({\n    'image_id': image_names,\n    'label': predictions\n})\n\n# Save to CSV\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\nprint(\"Submission file created: submission.csv\")\n\n# Display the first few rows to verify\nprint(submission_df.head())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-18T04:32:26.344068Z","iopub.execute_input":"2024-11-18T04:32:26.344633Z","iopub.status.idle":"2024-11-18T04:32:27.453612Z","shell.execute_reply.started":"2024-11-18T04:32:26.344578Z","shell.execute_reply":"2024-11-18T04:32:27.452244Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}