{"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 - VGG19 con el filtro de Sobel","metadata":{}},{"cell_type":"code","source":"pip install plot-keras-history","metadata":{"execution":{"iopub.status.busy":"2023-07-01T14:57:32.365515Z","iopub.execute_input":"2023-07-01T14:57:32.365818Z","iopub.status.idle":"2023-07-01T14:57:48.732359Z","shell.execute_reply.started":"2023-07-01T14:57:32.365787Z","shell.execute_reply":"2023-07-01T14:57:48.731226Z"},"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\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-07-01T14:57:48.736264Z","iopub.execute_input":"2023-07-01T14:57:48.736565Z","iopub.status.idle":"2023-07-01T14:57:53.60234Z","shell.execute_reply.started":"2023-07-01T14:57:48.736532Z","shell.execute_reply":"2023-07-01T14:57:53.598746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-filtro-sobel/data_filtro_Sobel/train')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T14:57:53.606941Z","iopub.execute_input":"2023-07-01T14:57:53.607308Z","iopub.status.idle":"2023-07-01T14:59:06.314043Z","shell.execute_reply.started":"2023-07-01T14:57:53.607273Z","shell.execute_reply":"2023-07-01T14:59:06.313222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-filtro-sobel/data_filtro_Sobel/valid')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T14:59:06.317465Z","iopub.execute_input":"2023-07-01T14:59:06.317797Z","iopub.status.idle":"2023-07-01T15:01:01.460039Z","shell.execute_reply.started":"2023-07-01T14:59:06.31774Z","shell.execute_reply":"2023-07-01T15:01:01.459206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = keras.preprocessing.image_dataset_from_directory(directory = '/kaggle/input/data-filtro-sobel/data_filtro_Sobel/test', shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:01:01.463584Z","iopub.execute_input":"2023-07-01T15:01:01.463964Z","iopub.status.idle":"2023-07-01T15:05:08.333592Z","shell.execute_reply.started":"2023-07-01T15:01:01.463923Z","shell.execute_reply":"2023-07-01T15:05:08.332675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Instanciamos el modelo","metadata":{}},{"cell_type":"code","source":"models = defaultdict(None)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:08.336151Z","iopub.execute_input":"2023-07-01T15:05:08.336533Z","iopub.status.idle":"2023-07-01T15:05:08.341063Z","shell.execute_reply.started":"2023-07-01T15:05:08.336493Z","shell.execute_reply":"2023-07-01T15:05:08.34002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nweights = \"imagenet\"  # TPesos que de la red\ninclude_top = False  # Si queremos incluir las capas al principio de la red\ninput_shape=(256, 256, 3)\narguments = {\"weights\": weights, \"include_top\": include_top, \"input_shape\": input_shape}\nmodels[\"Transfer learning\"] = tf.keras.applications.VGG19(**arguments)\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:08.342846Z","iopub.execute_input":"2023-07-01T15:05:08.34338Z","iopub.status.idle":"2023-07-01T15:05:13.120651Z","shell.execute_reply.started":"2023-07-01T15:05:08.343343Z","shell.execute_reply":"2023-07-01T15:05:13.119717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Congelar las capas del modelo base para evitar que se actualicen durante el entrenamiento","metadata":{}},{"cell_type":"code","source":"models[\"Transfer learning\"].trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.121968Z","iopub.execute_input":"2023-07-01T15:05:13.122312Z","iopub.status.idle":"2023-07-01T15:05:13.131787Z","shell.execute_reply.started":"2023-07-01T15:05:13.122276Z","shell.execute_reply":"2023-07-01T15:05:13.130924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extraemos el vector de características profundas aplanando la salida.","metadata":{}},{"cell_type":"code","source":"x = models[\"Transfer learning\"].output\narguments = {\"data_format\": \"channels_last\"}\nx = keras.layers.Flatten(**arguments)(x)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.13518Z","iopub.execute_input":"2023-07-01T15:05:13.135454Z","iopub.status.idle":"2023-07-01T15:05:13.144268Z","shell.execute_reply.started":"2023-07-01T15:05:13.135428Z","shell.execute_reply":"2023-07-01T15:05:13.14336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Añadimos la capa completamente conectada y la capa de salida.","metadata":{}},{"cell_type":"markdown","source":"Cuando utilizas la activación sigmoid y estableces units en 1, la capa de salida generará una única unidad de salida con un valor en el rango [0, 1]. Este valor representa la probabilidad de que la muestra pertenezca a la clase positiva (en tu caso, \"Embolia\"). Un valor cercano a 0 indica una baja probabilidad de pertenencia a la clase positiva, mientras que un valor cercano a 1 indica una alta probabilidad.\n\nEn cambio, cuando utilizas la activación softmax y estableces units en 2, la capa de salida generará dos unidades de salida, una para cada clase (\"Embolia\" y \"No embolia\"). La activación softmax aplica una función exponencial a los valores de salida, normalizándolos para que sumen 1 y generen una distribución de probabilidad. Cada valor de salida representará la probabilidad de que la muestra pertenezca a la clase correspondiente. La clase con la probabilidad más alta será la clase asignada a la muestra.","metadata":{}},{"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-07-01T15:05:13.14578Z","iopub.execute_input":"2023-07-01T15:05:13.146328Z","iopub.status.idle":"2023-07-01T15:05:13.168229Z","shell.execute_reply.started":"2023-07-01T15:05:13.146289Z","shell.execute_reply":"2023-07-01T15:05:13.167571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Agurpamos todo","metadata":{}},{"cell_type":"code","source":"inputs = models[\"Transfer learning\"].input\narguments = {\"inputs\": inputs, \"outputs\": outputs}\nmodels[\"Transfer learning\"] = keras.Model(**arguments)\nmodels[\"Transfer learning\"].summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.169976Z","iopub.execute_input":"2023-07-01T15:05:13.170234Z","iopub.status.idle":"2023-07-01T15:05:13.198058Z","shell.execute_reply.started":"2023-07-01T15:05:13.170208Z","shell.execute_reply":"2023-07-01T15:05:13.197302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Compilamos","metadata":{}},{"cell_type":"code","source":"histories = defaultdict(None)  # Dictionary with the histories","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.200274Z","iopub.execute_input":"2023-07-01T15:05:13.200623Z","iopub.status.idle":"2023-07-01T15:05:13.207227Z","shell.execute_reply.started":"2023-07-01T15:05:13.200571Z","shell.execute_reply":"2023-07-01T15:05:13.206492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = keras.losses.BinaryCrossentropy()\n\nmodels[\"Transfer learning\"].compile(loss=loss,\n              optimizer=keras.optimizers.Adam(1e-3),\n              metrics=[keras.metrics.AUC(),keras.metrics.Precision(),\n                       keras.metrics.Recall(), keras.metrics.BinaryAccuracy()])","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.208426Z","iopub.execute_input":"2023-07-01T15:05:13.208766Z","iopub.status.idle":"2023-07-01T15:05:13.252348Z","shell.execute_reply.started":"2023-07-01T15:05:13.20873Z","shell.execute_reply":"2023-07-01T15:05:13.251705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"histories['Transfer learning'] = models['Transfer learning'].fit(training, epochs=7, validation_data=validation, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T15:05:13.253566Z","iopub.execute_input":"2023-07-01T15:05:13.253915Z","iopub.status.idle":"2023-07-01T19:04:12.011666Z","shell.execute_reply.started":"2023-07-01T15:05:13.253878Z","shell.execute_reply":"2023-07-01T19:04:12.010813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" models['Transfer learning'].save('/kaggle/working/model-VGG19-TransferLearning-SobelFilter.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-01T19:04:12.013585Z","iopub.execute_input":"2023-07-01T19:04:12.013914Z","iopub.status.idle":"2023-07-01T19:04:12.235403Z","shell.execute_reply.started":"2023-07-01T19:04:12.013882Z","shell.execute_reply":"2023-07-01T19:04:12.23443Z"},"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-07-01T19:04:12.236789Z","iopub.execute_input":"2023-07-01T19:04:12.237146Z","iopub.status.idle":"2023-07-01T19:04:13.778997Z","shell.execute_reply.started":"2023-07-01T19:04:12.237108Z","shell.execute_reply":"2023-07-01T19:04:13.777893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Evaluamos el modelo","metadata":{}},{"cell_type":"code","source":"results = models[\"Transfer learning\"].evaluate(test, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T19:04:13.780231Z","iopub.execute_input":"2023-07-01T19:04:13.780592Z","iopub.status.idle":"2023-07-01T19:53:50.463873Z","shell.execute_reply.started":"2023-07-01T19:04:13.780553Z","shell.execute_reply":"2023-07-01T19:53:50.462898Z"},"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-07-01T19:53:50.466655Z","iopub.execute_input":"2023-07-01T19:53:50.466966Z","iopub.status.idle":"2023-07-01T19:53:50.472431Z","shell.execute_reply.started":"2023-07-01T19:53:50.466935Z","shell.execute_reply":"2023-07-01T19:53:50.471438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = models[\"Transfer learning\"].predict(test)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T19:53:50.473604Z","iopub.execute_input":"2023-07-01T19:53:50.474411Z","iopub.status.idle":"2023-07-01T20:14:06.673139Z","shell.execute_reply.started":"2023-07-01T19:53:50.474369Z","shell.execute_reply":"2023-07-01T20:14:06.67219Z"},"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-07-01T20:14:06.675767Z","iopub.execute_input":"2023-07-01T20:14:06.67643Z","iopub.status.idle":"2023-07-01T20:14:06.684991Z","shell.execute_reply.started":"2023-07-01T20:14:06.676386Z","shell.execute_reply":"2023-07-01T20:14:06.684079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba = models[\"Transfer learning\"].predict_proba(test)","metadata":{"execution":{"iopub.status.busy":"2023-07-01T20:14:06.686792Z","iopub.execute_input":"2023-07-01T20:14:06.687175Z","iopub.status.idle":"2023-07-01T20:14:07.037097Z","shell.execute_reply.started":"2023-07-01T20:14:06.687138Z","shell.execute_reply":"2023-07-01T20:14:07.035746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_proba[:,0]","metadata":{"execution":{"iopub.status.busy":"2023-07-01T20:14:07.038228Z","iopub.status.idle":"2023-07-01T20:14:07.038931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}