{"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 numpy as np\nimport pandas as pd\nfrom PIL import Image\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\nfrom sklearn.utils import shuffle\nfrom sklearn.utils import class_weight\nfrom sklearn.preprocessing import minmax_scale\nimport random\nimport cv2\nfrom imgaug import augmenters as iaa\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.metrics import classification_report, confusion_matrix \n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Input, BatchNormalization, GlobalAveragePooling2D\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.experimental import CosineDecay\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications import EfficientNetB4, EfficientNetB3\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomCrop,CenterCrop, RandomRotation","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-27T14:00:58.337128Z","iopub.execute_input":"2023-03-27T14:00:58.337539Z","iopub.status.idle":"2023-03-27T14:01:04.767049Z","shell.execute_reply.started":"2023-03-27T14:00:58.337425Z","shell.execute_reply":"2023-03-27T14:01:04.766126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nimport imblearn\nprint(imblearn.__version__)\n#visualizations\nimport plotly.graph_objs as go\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom plotly.subplots import make_subplots\nfrom sklearn.manifold import TSNE\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:04.768647Z","iopub.execute_input":"2023-03-27T14:01:04.768979Z","iopub.status.idle":"2023-03-27T14:01:05.164651Z","shell.execute_reply.started":"2023-03-27T14:01:04.768944Z","shell.execute_reply":"2023-03-27T14:01:05.163707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Conjuntos de entrenamiento y validación","metadata":{}},{"cell_type":"code","source":"training_folder = '../input/cassava-leaf-disease-classification/train_images/'\ntest_dir='../input/cassava-leaf-disease-classification/test_images/'","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:07.979767Z","iopub.execute_input":"2023-03-27T14:01:07.980166Z","iopub.status.idle":"2023-03-27T14:01:07.984568Z","shell.execute_reply.started":"2023-03-27T14:01:07.980131Z","shell.execute_reply":"2023-03-27T14:01:07.983712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples_df = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\nsamples_df = shuffle(samples_df, random_state=42)\n#samples_df[\"filepath\"] = training_folder+samples_df[\"image_id\"]\nsamples_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.128020Z","iopub.execute_input":"2023-03-27T14:01:08.128319Z","iopub.status.idle":"2023-03-27T14:01:08.178357Z","shell.execute_reply.started":"2023-03-27T14:01:08.128292Z","shell.execute_reply":"2023-03-27T14:01:08.177507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disease_names = open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json')\ndisease_names = json.load(disease_names)\n\nsamples_df['disease_name'] = samples_df['label'].apply(lambda x: disease_names[str(x)])\n#visualize the top five rows from table\nsamples_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.180105Z","iopub.execute_input":"2023-03-27T14:01:08.180416Z","iopub.status.idle":"2023-03-27T14:01:08.204777Z","shell.execute_reply.started":"2023-03-27T14:01:08.180381Z","shell.execute_reply":"2023-03-27T14:01:08.204166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = make_subplots(rows=1, cols=2,\n            specs=[[{\"type\": \"xy\"}, {\"type\": \"domain\"}]],)\n# value_counts: to count number of images in each class with respect to disease_name column\n# Bar plot \nt1 = go.Bar(x=samples_df['disease_name'].value_counts().index, \n            y=samples_df['disease_name'].value_counts().values,\n            text=samples_df['disease_name'].value_counts().values,\n            textposition='auto',name='Imágenes',\n           marker_color='darkblue')\n#Pie chart with labels and counts\nt2 = go.Pie(labels=samples_df['disease_name'].value_counts().index,\n           values=samples_df['disease_name'].value_counts().values,\n           hole=0)\nfig.add_trace(t1,row=1, col=1)\nfig.add_trace(t2,row=1, col=2)\nfig.update_layout(title='Distribución de clases')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.206196Z","iopub.execute_input":"2023-03-27T14:01:08.206712Z","iopub.status.idle":"2023-03-27T14:01:08.474062Z","shell.execute_reply.started":"2023-03-27T14:01:08.206674Z","shell.execute_reply":"2023-03-27T14:01:08.473391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_percentage = 0.6\nvalid_percent = 1 - training_percentage\nIMAGE_ID_COL_NAME = 'image_id'\nLABEL_ID_COL_NAME = 'disease_name'\nTARGET_SIZE = 300\ninput_shape = (TARGET_SIZE, TARGET_SIZE, 3)\ndropout_rate = 0.5\nbatch_size = 15\n\nCLASS_MODE = 'sparse'\ntraining_item_count = int(len(samples_df)*training_percentage)\nvalidation_item_count = len(samples_df)-int(len(samples_df)*training_percentage)\ntraining_df = samples_df[:training_item_count]\nclasses_to_predict = sorted(training_df.label.unique())\nvalidation_df = samples_df[training_item_count:]\nprint(\"Entrenamiento:\", len(training_df))\nprint(\"validación:\", len(validation_df))\nprint(classes_to_predict)\ntraining_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.475755Z","iopub.execute_input":"2023-03-27T14:01:08.476131Z","iopub.status.idle":"2023-03-27T14:01:08.492592Z","shell.execute_reply.started":"2023-03-27T14:01:08.476095Z","shell.execute_reply":"2023-03-27T14:01:08.491315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_crop(img, random_crop_size):\n    # Note: image_data_format is 'channel_last'\n    assert img.shape[2] == 3\n    height, width = img.shape[0], img.shape[1]\n    dy, dx = random_crop_size\n    x = np.random.randint(0, width - dx + 1)\n    y = np.random.randint(0, height - dy + 1)\n    return img[y:(y+dy), x:(x+dx), :]\n\n\ndef crop_generator(batches, crop_length):\n    \"\"\"Take as input a Keras ImageGen (Iterator) and generate random\n    crops from the image batches generated by the original iterator.\n    \"\"\"\n    while True:\n        batch_x, batch_y = next(batches)\n        batch_crops = np.zeros((batch_x.shape[0], crop_length, crop_length, 3))\n        for i in range(batch_x.shape[0]):\n            batch_crops[i] = random_crop(batch_x[i], (crop_length, crop_length))\n        yield batch_crops, batch_y","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.496528Z","iopub.execute_input":"2023-03-27T14:01:08.496818Z","iopub.status.idle":"2023-03-27T14:01:08.505168Z","shell.execute_reply.started":"2023-03-27T14:01:08.496792Z","shell.execute_reply":"2023-03-27T14:01:08.504389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = ImageDataGenerator(validation_split=valid_percent,\n                                horizontal_flip=True,\n                                vertical_flip=True,\n                                shear_range=0.1,\n                                rescale=1,\n                                zoom_range=0.2,\n                                width_shift_range=0.1,\n                                height_shift_range=0.1)\ntrain_gen = train_data.flow_from_dataframe(samples_df,\n                                           directory= training_folder,\n                                           subset=\"training\",\n                                           x_col= IMAGE_ID_COL_NAME,\n                                           y_col= LABEL_ID_COL_NAME,\n                                          target_size=(TARGET_SIZE,TARGET_SIZE),\n                                           batch_size=batch_size,\n                                           class_mode=CLASS_MODE)\n#train_images = crop_generator(train_gen,TARGET_SIZE)\n#valid_data = ImageDataGenerator(validation_split=valid_percent)\nvalid_gen = train_data.flow_from_dataframe(samples_df,\n                                           directory= training_folder,\n                                           subset=\"validation\",\n                                           x_col=IMAGE_ID_COL_NAME,\n                                           y_col= LABEL_ID_COL_NAME,\n                                          target_size=(TARGET_SIZE,TARGET_SIZE),\n                                           batch_size=batch_size,\n                                           class_mode=CLASS_MODE)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:01:08.973399Z","iopub.execute_input":"2023-03-27T14:01:08.973763Z","iopub.status.idle":"2023-03-27T14:02:00.170561Z","shell.execute_reply.started":"2023-03-27T14:01:08.973731Z","shell.execute_reply":"2023-03-27T14:02:00.169191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure()\nt1 = go.Bar(name='Entrenamiento',x=np.unique(train_gen.labels,return_counts=True)[0],y=np.unique(train_gen.labels,return_counts=True)[1],\n           text=np.unique(train_gen.labels,return_counts=True)[1],textposition='auto', marker_color='red')\nt2 = go.Bar(name='Prueba',x=np.unique(valid_gen.labels,return_counts=True)[0],y=np.unique(valid_gen.labels,return_counts=True)[1],\n           text=np.unique(valid_gen.labels,return_counts=True)[1],textposition='auto', marker_color='darkblue')\nfig.add_trace(t1)\nfig.add_trace(t2)\n#x-axis and y axis title\nfig.update_xaxes(title_text=\"Clase\")\nfig.update_yaxes(title_text=\"Número de imagenes\")\nfig.update_layout(title='Conjuntos de entrenamiento y prueba')\nfig.show()\n\n#Pie Chart\nfig = make_subplots(rows=1, cols=2,subplot_titles=['Entrenamiento', 'Prueba'],\n            specs=[[{\"type\": \"domain\"}, {\"type\": \"domain\"}]],)\n\n#Pie chart with labels and counts\nt1 = go.Pie(labels=np.unique(train_gen.labels,return_counts=True)[0],\n           values=np.unique(train_gen.labels,return_counts=True)[1],\n           hole=0.3)\nt2 = go.Pie(labels=np.unique(valid_gen.labels,return_counts=True)[0],\n           values=np.unique(valid_gen.labels,return_counts=True)[1],\n           hole=0.3)\nfig.add_trace(t1,row=1, col=1)\nfig.add_trace(t2,row=1, col=2)\nfig.update_layout(title='Distribución de los conjuntos de entrenamiento y prueba')\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:02:00.172254Z","iopub.execute_input":"2023-03-27T14:02:00.172537Z","iopub.status.idle":"2023-03-27T14:02:00.243404Z","shell.execute_reply.started":"2023-03-27T14:02:00.172509Z","shell.execute_reply":"2023-03-27T14:02:00.242554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Aug--","metadata":{}},{"cell_type":"code","source":"data_augmentation_layers = tf.keras.Sequential(\n    [\n        layers.experimental.preprocessing.RandomCrop(height=TARGET_SIZE, width=TARGET_SIZE),\n        layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n        layers.experimental.preprocessing.RandomRotation(factor=(-0.45,0.65), fill_mode=\"reflect\"),\n        layers.experimental.preprocessing.RandomZoom(height_factor=(-0.3,0.5),width_factor=(-0.2,0.5),fill_mode=\"wrap\",interpolation=\"bilinear\"),\n        layers.experimental.preprocessing.RandomContrast((0.1,0.6))\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:02:00.244803Z","iopub.execute_input":"2023-03-27T14:02:00.245115Z","iopub.status.idle":"2023-03-27T14:02:02.838329Z","shell.execute_reply.started":"2023-03-27T14:02:00.245080Z","shell.execute_reply":"2023-03-27T14:02:02.837250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelos","metadata":{}},{"cell_type":"markdown","source":"> # EfficientNet","metadata":{}},{"cell_type":"code","source":"base_eff = EfficientNetB4(weights=\"../input/tfkerasefficientnetimagenetnotop/efficientnetb4_notop.h5\", \n                              include_top=False, \n                              input_shape=input_shape, \n                              drop_connect_rate=dropout_rate)\n\ninputs = Input(shape=input_shape)\naugmented = data_augmentation_layers(inputs)\nbase_eff = base_eff(augmented)\npooling = layers.GlobalAveragePooling2D()(base_eff)\ndropout = layers.Dropout(dropout_rate)(pooling)\noutputs = Dense(len(classes_to_predict), activation=\"softmax\")(dropout)\neff = Model(inputs=inputs, outputs=outputs)\n    \neff.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T00:40:02.977139Z","iopub.execute_input":"2023-03-27T00:40:02.977495Z","iopub.status.idle":"2023-03-27T00:40:08.853395Z","shell.execute_reply.started":"2023-03-27T00:40:02.977460Z","shell.execute_reply":"2023-03-27T00:40:08.852566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 40\neff.compile(loss=\"sparse_categorical_crossentropy\", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T00:40:08.855080Z","iopub.execute_input":"2023-03-27T00:40:08.855622Z","iopub.status.idle":"2023-03-27T00:40:08.875068Z","shell.execute_reply.started":"2023-03-27T00:40:08.855580Z","shell.execute_reply":"2023-03-27T00:40:08.874168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN=train_gen.n//train_gen.batch_size\nSTEP_SIZE_VALID=valid_gen.n//valid_gen.batch_size\n\nhistory = eff.fit(train_gen,\n                  steps_per_epoch = STEP_SIZE_TRAIN,\n                  epochs = epochs,\n                  validation_data=valid_gen,\n                  validation_steps = STEP_SIZE_VALID)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T00:40:08.878595Z","iopub.execute_input":"2023-03-27T00:40:08.878863Z","iopub.status.idle":"2023-03-27T08:06:59.131181Z","shell.execute_reply.started":"2023-03-27T00:40:08.878837Z","shell.execute_reply":"2023-03-27T08:06:59.130299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Proceso de entrenamiento\n\n","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Loss en epocas')\nplt.ylabel('Loss')\nplt.xlabel('Epoca')\nplt.legend(['Entrenamiento', 'Validacion'], loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:06:59.134754Z","iopub.execute_input":"2023-03-27T08:06:59.135028Z","iopub.status.idle":"2023-03-27T08:06:59.354050Z","shell.execute_reply.started":"2023-03-27T08:06:59.135001Z","shell.execute_reply":"2023-03-27T08:06:59.353198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Accuracy en epocas')\nplt.ylabel('accuracy')\nplt.xlabel('Epoca')\nplt.legend(['Entrenamiento', 'Validacion'], loc='best')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:06:59.355411Z","iopub.execute_input":"2023-03-27T08:06:59.355741Z","iopub.status.idle":"2023-03-27T08:06:59.514897Z","shell.execute_reply.started":"2023-03-27T08:06:59.355706Z","shell.execute_reply":"2023-03-27T08:06:59.514089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = eff.predict(valid_gen)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:06:59.516109Z","iopub.execute_input":"2023-03-27T08:06:59.516459Z","iopub.status.idle":"2023-03-27T08:10:17.135015Z","shell.execute_reply.started":"2023-03-27T08:06:59.516424Z","shell.execute_reply":"2023-03-27T08:10:17.133969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.argmax(Y_pred, axis=1)\nprint(len(y_pred))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:17.137474Z","iopub.execute_input":"2023-03-27T08:10:17.138004Z","iopub.status.idle":"2023-03-27T08:10:17.143701Z","shell.execute_reply.started":"2023-03-27T08:10:17.137962Z","shell.execute_reply":"2023-03-27T08:10:17.142839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(valid_gen.classes))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:17.144998Z","iopub.execute_input":"2023-03-27T08:10:17.145569Z","iopub.status.idle":"2023-03-27T08:10:17.156312Z","shell.execute_reply.started":"2023-03-27T08:10:17.145533Z","shell.execute_reply":"2023-03-27T08:10:17.155351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(valid_gen.classes, y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:17.157596Z","iopub.execute_input":"2023-03-27T08:10:17.157974Z","iopub.status.idle":"2023-03-27T08:10:17.177973Z","shell.execute_reply.started":"2023-03-27T08:10:17.157935Z","shell.execute_reply":"2023-03-27T08:10:17.177224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_names = list(train_gen.class_indices.keys())\nprint(classification_report(valid_gen.classes, y_pred, target_names=target_names))","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:17.179095Z","iopub.execute_input":"2023-03-27T08:10:17.179454Z","iopub.status.idle":"2023-03-27T08:10:17.200406Z","shell.execute_reply.started":"2023-03-27T08:10:17.179418Z","shell.execute_reply":"2023-03-27T08:10:17.199538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(valid_gen.classes, y_pred)\nlabels = ['Cassava Bacterial Blight (CBB)', 'Cassava Brown Streak Disease (CBSD)', 'Cassava Green Mottle (CGM)', 'Cassava Mosaic Disease (CMD)','Healthy']\nplt.figure(figsize=(8,6))\nsns.heatmap(cm,xticklabels=labels, yticklabels=labels, annot=True, fmt='d', cmap=\"Blues\", vmin = 0.2);\nplt.title('Confusion Matrix')\nplt.ylabel('True Class')\nplt.xlabel('Predicted Class')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T08:10:17.201703Z","iopub.execute_input":"2023-03-27T08:10:17.202342Z","iopub.status.idle":"2023-03-27T08:10:17.540771Z","shell.execute_reply.started":"2023-03-27T08:10:17.202306Z","shell.execute_reply":"2023-03-27T08:10:17.539800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> # ResNet 50","metadata":{}},{"cell_type":"code","source":"from keras.applications.resnet50 import ResNet50\n\nbase_rn50 = ResNet50(input_shape=input_shape,\n                    include_top=False,\n                    weights='../input/tf-keras-22-pretrained-models/TF2.2_ResNet50_NoTop_ImageNet.h5')\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:02:02.843275Z","iopub.execute_input":"2023-03-27T14:02:02.843631Z","iopub.status.idle":"2023-03-27T14:02:05.647438Z","shell.execute_reply.started":"2023-03-27T14:02:02.843595Z","shell.execute_reply":"2023-03-27T14:02:05.646547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = Input(shape=input_shape)\naugmented = data_augmentation_layers(inputs)\nbase_rn50 = base_rn50(augmented)\npooling = layers.GlobalAveragePooling2D()(base_rn50)\ndropout = layers.Dropout(dropout_rate)(pooling)\noutputs = Dense(len(classes_to_predict), activation=\"softmax\")(dropout)\nrn50 = Model(inputs=inputs, outputs=outputs)\n    \nrn50.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:02:05.650266Z","iopub.execute_input":"2023-03-27T14:02:05.650632Z","iopub.status.idle":"2023-03-27T14:02:06.286254Z","shell.execute_reply.started":"2023-03-27T14:02:05.650596Z","shell.execute_reply":"2023-03-27T14:02:06.285434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 40\nrn50.compile(loss=\"sparse_categorical_crossentropy\", optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:02:06.288487Z","iopub.execute_input":"2023-03-27T14:02:06.289008Z","iopub.status.idle":"2023-03-27T14:02:06.304715Z","shell.execute_reply.started":"2023-03-27T14:02:06.288968Z","shell.execute_reply":"2023-03-27T14:02:06.303784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TRAIN=train_gen.n//train_gen.batch_size\nSTEP_SIZE_VALID=valid_gen.n//valid_gen.batch_size\n\nhistory = rn50.fit(train_gen,\n                   steps_per_epoch = STEP_SIZE_TRAIN,\n                   epochs = epochs,\n                   validation_data=valid_gen,\n                   validation_steps = STEP_SIZE_VALID)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:03:49.318325Z","iopub.execute_input":"2023-03-27T14:03:49.318670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> ## Proceso de entrenamiento","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Loss en epocas')\nplt.ylabel('Loss')\nplt.xlabel('Epoca')\nplt.legend(['Entrenamiento', 'Validacion'], loc='best')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Accuracy en epocas')\nplt.ylabel('accuracy')\nplt.xlabel('Epoca')\nplt.legend(['Entrenamiento', 'Validacion'], loc='best')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_pred = rn50.predict(valid_gen)\ny_pred = np.argmax(Y_pred, axis=1)\nprint(len(y_pred))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(valid_gen.classes, y_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_names = list(train_gen.class_indices.keys())\nprint(classification_report(valid_gen.classes, y_pred, target_names=target_names))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(valid_gen.classes, y_pred)\nlabels = ['Cassava Bacterial Blight (CBB)', 'Cassava Brown Streak Disease (CBSD)', 'Cassava Green Mottle (CGM)', 'Cassava Mosaic Disease (CMD)','Healthy']\nplt.figure(figsize=(8,6))\nsns.heatmap(cm,xticklabels=labels, yticklabels=labels, annot=True, fmt='d', cmap=\"Blues\", vmin = 0.2);\nplt.title('Confusion Matrix')\nplt.ylabel('True Class')\nplt.xlabel('Predicted Class')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}