{"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 necessary libraries\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB4\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.models import load_model\nfrom tensorflow import keras\n\n# Set the path to the dataset files\ntrain_csv_path = '/kaggle/input/plant-pathology-2021-fgvc8/train.csv'\ntrain_images_dir = '/kaggle/input/plant-pathology-2021-fgvc8/train_images'\ntest_images_dir = '/kaggle/input/plant-pathology-2021-fgvc8/test_images'\nsubmission_file_path = '/kaggle/input/plant-pathology-2021-fgvc8/sample_submission.csv'\n\n# Read the train.csv file\ntrain_df = pd.read_csv(train_csv_path)\n\n# Create an ImageDataGenerator for data augmentation and preprocessing\ndatagen = ImageDataGenerator(\n    rescale=1.0/255.0,\n    validation_split=0.2\n)\n\n# Create the train and validation generators\ntrain_generator = datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=train_images_dir,\n    x_col='image',\n    y_col='labels',\n    subset='training',\n    batch_size=16,\n    seed=42,\n    shuffle=True,\n    class_mode='categorical',\n    target_size=(224, 224)\n)\n\nval_generator = datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=train_images_dir,\n    x_col='image',\n    y_col='labels',\n    subset='validation',\n    batch_size=16,\n    seed=42,\n    shuffle=True,\n    class_mode='categorical',\n    target_size=(224, 224)\n)","metadata":{"_uuid":"8b6017d7-0e30-466e-adb8-1ea6a7d4290a","_cell_guid":"7a4bfb1e-fce9-4910-9041-fdd1461ed44f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:17:59.907284Z","iopub.execute_input":"2023-06-26T19:17:59.907668Z","iopub.status.idle":"2023-06-26T19:18:55.254231Z","shell.execute_reply.started":"2023-06-26T19:17:59.907637Z","shell.execute_reply":"2023-06-26T19:18:55.253215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\nstrategy = tf.distribute.TPUStrategy(tpu)\"\"\"","metadata":{"_uuid":"9bbf2b40-163d-44f1-acb1-e0fa65ecae2d","_cell_guid":"9d7a7598-1f6f-409c-97bf-fc920487c9d9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:18:55.256031Z","iopub.execute_input":"2023-06-26T19:18:55.257260Z","iopub.status.idle":"2023-06-26T19:18:55.267842Z","shell.execute_reply.started":"2023-06-26T19:18:55.257189Z","shell.execute_reply":"2023-06-26T19:18:55.266529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#with strategy.scope():\n# Load the pre-trained EfficientNetB4 model without the top layers\n#base_model = EfficientNetB4(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n\n\n\n# Path to the downloaded weights file\nweights_path = '/kaggle/input/4testmodel/4testmodel2606.h5'#'/kaggle/input/weight-efficientnet/EfficientNet_V2.h5' #'/kaggle/input/tolga-efficientnetb4-model/tolga_efficientnetb4_model.h5'\n\ncustom_objects = {'FixedDropout': keras.layers.Dropout}\n\n# Load the model with the custom object scope\nmodel = keras.models.load_model(weights_path, custom_objects=custom_objects)\n\n# Rest of your code...\n\n\n\n# Freeze the base model\n#base_model.trainable = False\n\n# Create the model\n\"\"\"model = Sequential()\nmodel.add(base_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(6, activation='softmax'))\"\"\"\n\n# Compile the model\n#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"_uuid":"83d04bd1-f40f-4644-bd27-e5cdc7404d10","_cell_guid":"1e3c929c-5383-47b0-8b3c-2df2d54cc0e2","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:18:55.269872Z","iopub.execute_input":"2023-06-26T19:18:55.270379Z","iopub.status.idle":"2023-06-26T19:18:59.694308Z","shell.execute_reply.started":"2023-06-26T19:18:55.270319Z","shell.execute_reply":"2023-06-26T19:18:59.692906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath='model_checkpoint',\n    save_weights_only=True,\n    save_best_only=True,\n    save_freq='epoch'\n)\n# Train the model with the checkpoint callback\n\n\nmodel.fit(\n    train_generator,\n    validation_data=valid_generator,\n    epochs=1,\n    initial_epoch=0,  # Change this value to the last epoch completed before interruption\n    callbacks=[checkpoint_callback]\n)\n\nmodel.save('final_model.h5')\"\"\"","metadata":{"_uuid":"11e6c00e-c790-425a-806f-17169eff4066","_cell_guid":"67374a69-6a1b-4fb5-b296-b418af00e375","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:18:59.697361Z","iopub.execute_input":"2023-06-26T19:18:59.697739Z","iopub.status.idle":"2023-06-26T19:18:59.706419Z","shell.execute_reply.started":"2023-06-26T19:18:59.697706Z","shell.execute_reply":"2023-06-26T19:18:59.705056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the test data\ntest_df = pd.read_csv('/kaggle/input/plant-pathology-2021-fgvc8/sample_submission.csv')\ntest_datagen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    directory=test_images_dir,\n    x_col='image',\n    y_col=None,\n    batch_size=32,\n    seed=42,\n    shuffle=False,\n    class_mode=None,\n    target_size=(224, 224)\n)","metadata":{"_uuid":"bdb4ec6a-fb22-4146-8b34-bb07243d7fef","_cell_guid":"9523bc0b-82a7-48ff-b6ba-f13b661c3d5e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:18:59.708959Z","iopub.execute_input":"2023-06-26T19:18:59.709482Z","iopub.status.idle":"2023-06-26T19:18:59.734960Z","shell.execute_reply.started":"2023-06-26T19:18:59.709438Z","shell.execute_reply":"2023-06-26T19:18:59.734114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.load_weights('/kaggle/input/3testmodel/3testmodel.h5', by_name=True, skip_mismatch=True)\n\n# Convert the non-serializable tensors to numpy arrays\n#model.save_weights('/kaggle/input/model-weights/model_weights.h5')","metadata":{"_uuid":"3c339b46-48d0-4031-ab46-7a7f65891fdc","_cell_guid":"978d1326-d2c3-4b19-9699-66f7ca93dffe","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:18:59.736510Z","iopub.execute_input":"2023-06-26T19:18:59.737162Z","iopub.status.idle":"2023-06-26T19:19:01.077123Z","shell.execute_reply.started":"2023-06-26T19:18:59.737129Z","shell.execute_reply":"2023-06-26T19:19:01.075839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\nlabels_counts = train['labels'].value_counts()\nlabels = labels_counts.index.tolist()\nprint(labels)","metadata":{"_uuid":"109524aa-ff79-481d-9967-34fea9a49ccb","_cell_guid":"aab32405-a57e-4655-9bf5-e5014c13ecfc","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:19:01.078624Z","iopub.execute_input":"2023-06-26T19:19:01.079045Z","iopub.status.idle":"2023-06-26T19:19:01.114933Z","shell.execute_reply.started":"2023-06-26T19:19:01.078981Z","shell.execute_reply":"2023-06-26T19:19:01.113568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_label = labels #['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab']\n\n# Generate predictions for the test data\npredictions = model.predict(test_generator)\n\n# Convert the predicted labels to string representations\npredicted_labels = np.argmax(predictions, axis=1)\nprint(predicted_labels)\npredicted_labels = [n_label[label] for label in predicted_labels]\n\n# Prepare the submission file\ntest_df['labels'] = predicted_labels\ntest_df.to_csv('submission.csv', index=False)\nsubmission = pd.read_csv('/kaggle/working/submission.csv')\nsubmission.head()","metadata":{"_uuid":"943fdc39-8209-4f5b-b0da-5144fbb0e663","_cell_guid":"216e5902-3330-4e7a-a264-8dc1cd0fb98b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:19:01.116909Z","iopub.execute_input":"2023-06-26T19:19:01.117325Z","iopub.status.idle":"2023-06-26T19:19:03.655521Z","shell.execute_reply.started":"2023-06-26T19:19:01.117292Z","shell.execute_reply":"2023-06-26T19:19:03.654376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\nimport matplotlib.pyplot as plt\ny_val_true = val_generator.classes\ny_val_pred = model.predict(val_generator)\ny_val_pred = np.argmax(y_val_pred, axis=1)\n\nconfusion_mat = confusion_matrix(y_val_true, y_val_pred)\n\nplt.figure(figsize=(8, 6))\nsns.heatmap(confusion_mat, annot=True, fmt='d', cmap='Blues', xticklabels=train_generator.class_indices, yticklabels=train_generator.class_indices)\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"_uuid":"5b6e9157-7d04-444c-a87a-5839036a1cdf","_cell_guid":"7dc1f77f-27af-4016-b45a-9d946fab7cc1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-26T19:19:03.657109Z","iopub.execute_input":"2023-06-26T19:19:03.657983Z"},"trusted":true},"execution_count":null,"outputs":[]}]}