{"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":"raw","source":"Transfer learning model - SeXception","metadata":{}},{"cell_type":"code","source":"pip install plot-keras-history","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:26:44.920042Z","iopub.execute_input":"2023-06-24T12:26:44.920417Z","iopub.status.idle":"2023-06-24T12:27:01.037188Z","shell.execute_reply.started":"2023-06-24T12:26:44.920384Z","shell.execute_reply":"2023-06-24T12:27:01.035961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow.keras as keras\nfrom collections import defaultdict\nimport warnings\nfrom plot_keras_history import show_history, plot_history\nfrom keras.layers import Conv2D,MaxPool2D,GlobalAveragePooling2D,AveragePooling2D, Reshape, Dense\nfrom keras.layers import multiply, Flatten\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:09:01.780877Z","iopub.execute_input":"2023-06-24T13:09:01.781267Z","iopub.status.idle":"2023-06-24T13:09:01.791197Z","shell.execute_reply.started":"2023-06-24T13:09:01.781218Z","shell.execute_reply":"2023-06-24T13:09:01.790364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/train')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:33.386275Z","iopub.execute_input":"2023-06-24T12:45:33.386654Z","iopub.status.idle":"2023-06-24T12:47:54.95167Z","shell.execute_reply.started":"2023-06-24T12:45:33.386621Z","shell.execute_reply":"2023-06-24T12:47:54.950799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/valid')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:47:54.955649Z","iopub.execute_input":"2023-06-24T12:47:54.956539Z","iopub.status.idle":"2023-06-24T12:54:16.288855Z","shell.execute_reply.started":"2023-06-24T12:47:54.956496Z","shell.execute_reply":"2023-06-24T12:54:16.287823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-binary/data/test', shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:54:16.290299Z","iopub.execute_input":"2023-06-24T12:54:16.290655Z","iopub.status.idle":"2023-06-24T13:02:07.253386Z","shell.execute_reply.started":"2023-06-24T12:54:16.290616Z","shell.execute_reply":"2023-06-24T13:02:07.252543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Definir la función del bloque SE","metadata":{}},{"cell_type":"code","source":"def squeeze_excite_block(input_tensor, ratio=16):\n    channels = input_tensor.shape[-1]  # Obtener el número de canales\n    # Squeeze: Reducción de dimensionalidad\n    x = GlobalAveragePooling2D()(input_tensor)\n    x = Reshape((1, 1, channels))(x)\n    x = Dense(channels // ratio, activation='relu')(x)\n    x = Dense(channels, activation='sigmoid')(x)\n    # Excite: Actualización de características\n    x = multiply([input_tensor, x])\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:03:46.458522Z","iopub.execute_input":"2023-06-24T13:03:46.458908Z","iopub.status.idle":"2023-06-24T13:03:46.46635Z","shell.execute_reply.started":"2023-06-24T13:03:46.45887Z","shell.execute_reply":"2023-06-24T13:03:46.464947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Creación del modelo Xception donde le añadimos el squeeze_excite_block","metadata":{}},{"cell_type":"code","source":"models = defaultdict(None)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:04:46.179356Z","iopub.execute_input":"2023-06-24T13:04:46.179716Z","iopub.status.idle":"2023-06-24T13:04:46.184106Z","shell.execute_reply.started":"2023-06-24T13:04:46.179682Z","shell.execute_reply":"2023-06-24T13:04:46.183032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Cargar el modelo base Xception\nmodels[\"Transfer learning SeXception\"] =  tf.keras.applications.Xception(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:04:47.803675Z","iopub.execute_input":"2023-06-24T13:04:47.804055Z","iopub.status.idle":"2023-06-24T13:04:49.363835Z","shell.execute_reply.started":"2023-06-24T13:04:47.80402Z","shell.execute_reply":"2023-06-24T13:04:49.362849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models[\"Transfer learning SeXception\"].trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:04:57.049675Z","iopub.execute_input":"2023-06-24T13:04:57.050026Z","iopub.status.idle":"2023-06-24T13:04:57.059533Z","shell.execute_reply.started":"2023-06-24T13:04:57.049991Z","shell.execute_reply":"2023-06-24T13:04:57.058363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = models[\"Transfer learning SeXception\"].output\n\n# Agregar el bloque SE después de ciertas capas convolucionales\nx = squeeze_excite_block(x)  # Agregar el bloque SE aquí\n\narguments = {\"data_format\": \"channels_last\"}\nx = Flatten(**arguments)(x)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:09:04.416124Z","iopub.execute_input":"2023-06-24T13:09:04.416521Z","iopub.status.idle":"2023-06-24T13:09:04.466051Z","shell.execute_reply.started":"2023-06-24T13:09:04.416485Z","shell.execute_reply":"2023-06-24T13:09:04.465283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"units = 128\nactivation = \"relu\"\narguments = {\"units\": units, \"activation\": activation} \nx = keras.layers.Dense(**arguments)(x)\n\nunits = 1\nactivation = \"sigmoid\"\narguments = {\"units\": units, \"activation\": activation} \noutputs = keras.layers.Dense(**arguments)(x)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:09:07.290603Z","iopub.execute_input":"2023-06-24T13:09:07.290956Z","iopub.status.idle":"2023-06-24T13:09:07.312763Z","shell.execute_reply.started":"2023-06-24T13:09:07.290921Z","shell.execute_reply":"2023-06-24T13:09:07.311847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = models[\"Transfer learning SeXception\"].input\narguments = {\"inputs\": inputs, \"outputs\": outputs}\nmodels[\"Transfer learning SeXception\"] = keras.Model(**arguments)\nmodels[\"Transfer learning SeXception\"].summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:09:09.438478Z","iopub.execute_input":"2023-06-24T13:09:09.438823Z","iopub.status.idle":"2023-06-24T13:09:09.505947Z","shell.execute_reply.started":"2023-06-24T13:09:09.438788Z","shell.execute_reply":"2023-06-24T13:09:09.505122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histories = defaultdict(None)  # Dictionary with the histories","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:09:35.602043Z","iopub.execute_input":"2023-06-24T13:09:35.602437Z","iopub.status.idle":"2023-06-24T13:09:35.606437Z","shell.execute_reply.started":"2023-06-24T13:09:35.602401Z","shell.execute_reply":"2023-06-24T13:09:35.605469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = keras.losses.BinaryCrossentropy()\n\nmodels[\"Transfer learning SeXception\"].compile(loss=loss,\n              optimizer=keras.optimizers.Adam(1e-3),\n              metrics=[keras.metrics.AUC(),keras.metrics.Precision(),\n                       keras.metrics.Recall()])","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:10:30.61676Z","iopub.execute_input":"2023-06-24T13:10:30.617138Z","iopub.status.idle":"2023-06-24T13:10:30.65527Z","shell.execute_reply.started":"2023-06-24T13:10:30.6171Z","shell.execute_reply":"2023-06-24T13:10:30.654512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histories['Transfer learning SeXception'] = models['Transfer learning SeXception'].fit(training, epochs=7, validation_data=validation, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T13:12:13.257331Z","iopub.execute_input":"2023-06-24T13:12:13.257698Z","iopub.status.idle":"2023-06-24T18:21:31.494636Z","shell.execute_reply.started":"2023-06-24T13:12:13.257664Z","shell.execute_reply":"2023-06-24T18:21:31.493695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" models['Transfer learning SeXception'].save('/kaggle/working/model-SeXception-TransferLearning.h5')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T18:28:09.534387Z","iopub.execute_input":"2023-06-24T18:28:09.53477Z","iopub.status.idle":"2023-06-24T18:28:10.25422Z","shell.execute_reply.started":"2023-06-24T18:28:09.534737Z","shell.execute_reply":"2023-06-24T18:28:10.253221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for model in histories:\n    # Plot the model training history\n    history = histories[model]\n    plot_history(history)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T18:28:12.761362Z","iopub.execute_input":"2023-06-24T18:28:12.761714Z","iopub.status.idle":"2023-06-24T18:28:13.965805Z","shell.execute_reply.started":"2023-06-24T18:28:12.76168Z","shell.execute_reply":"2023-06-24T18:28:13.964793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Evaluamos el modelo","metadata":{}},{"cell_type":"code","source":"results = models[\"Transfer learning SeXception\"].evaluate(test, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T18:29:54.957384Z","iopub.execute_input":"2023-06-24T18:29:54.95779Z","iopub.status.idle":"2023-06-24T19:41:30.825828Z","shell.execute_reply.started":"2023-06-24T18:29:54.957753Z","shell.execute_reply":"2023-06-24T19:41:30.824878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"test loss, test auc, test precision, test recall:\", results)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T19:43:17.063528Z","iopub.execute_input":"2023-06-24T19:43:17.063976Z","iopub.status.idle":"2023-06-24T19:43:17.069367Z","shell.execute_reply.started":"2023-06-24T19:43:17.063934Z","shell.execute_reply":"2023-06-24T19:43:17.067969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = models[\"Transfer learning SeXception\"].predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T19:45:50.261602Z","iopub.execute_input":"2023-06-24T19:45:50.26196Z","iopub.status.idle":"2023-06-24T20:29:13.552082Z","shell.execute_reply.started":"2023-06-24T19:45:50.261924Z","shell.execute_reply":"2023-06-24T20:29:13.551019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ny_pred_binary = np.squeeze(np.round(y_pred)).astype(int)\ny_pred_binary","metadata":{"execution":{"iopub.status.busy":"2023-06-24T20:29:13.554929Z","iopub.execute_input":"2023-06-24T20:29:13.555341Z","iopub.status.idle":"2023-06-24T20:29:13.566064Z","shell.execute_reply.started":"2023-06-24T20:29:13.555298Z","shell.execute_reply":"2023-06-24T20:29:13.565052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba = models[\"Transfer learning SeXception\"].predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T20:29:13.568228Z","iopub.execute_input":"2023-06-24T20:29:13.568632Z","iopub.status.idle":"2023-06-24T20:29:13.595872Z","shell.execute_reply.started":"2023-06-24T20:29:13.568593Z","shell.execute_reply":"2023-06-24T20:29:13.594227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-06-24T20:29:13.597608Z","iopub.status.idle":"2023-06-24T20:29:13.598305Z"},"trusted":true},"execution_count":null,"outputs":[]}]}