{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":59093,"databundleVersionId":7469972,"sourceType":"competition"},{"sourceId":7594351,"sourceType":"datasetVersion","datasetId":4416534}],"dockerImageVersionId":30648,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\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\nimport os\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.train import Feature, Features, Example\nfrom tensorflow.train import Int64List, FloatList, BytesList\nfrom tensorflow.keras.applications import efficientnet_v2\n\n#from efficientnet_v2 import get_preprocessing_layer\n\nimport keras_cv\n\nfrom contextlib import ExitStack\n\nfrom PIL import Image\n\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","execution":{"iopub.status.busy":"2024-02-13T15:31:30.084059Z","iopub.execute_input":"2024-02-13T15:31:30.084333Z","iopub.status.idle":"2024-02-13T15:31:50.577518Z","shell.execute_reply.started":"2024-02-13T15:31:30.084308Z","shell.execute_reply":"2024-02-13T15:31:50.576745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TFRECORDS_FILES_DATASET = '/kaggle/input/hms-train-dataset-kfolds'","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:31:50.579205Z","iopub.execute_input":"2024-02-13T15:31:50.579910Z","iopub.status.idle":"2024-02-13T15:31:50.586167Z","shell.execute_reply.started":"2024-02-13T15:31:50.579874Z","shell.execute_reply":"2024-02-13T15:31:50.585319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(tfrecord):\n    feature_descriptions = {\n        \"image_eeg\": tf.io.FixedLenFeature([],tf.string),\n        \"image_spec\": tf.io.FixedLenFeature([],tf.string),\n        \"label\": tf.io.FixedLenFeature([6], tf.float32, default_value=[-0.,-0.,-0.,-0.,-0.,-0.])\n    }\n    \n    example = tf.io.parse_single_example(tfrecord, feature_descriptions)\n    #image = tf.io.parse_tensor(example[\"image\"], out_type=tf.uint8)\n    image_eeg_pt = tf.io.decode_jpeg(example[\"image_eeg\"])\n                                  #channels=1\n    image_eeg= tf.image.crop_to_bounding_box(image_eeg_pt,offset_height=12, offset_width=13, target_height=224, target_width=224)\n    #image_eeg = tf.image.resize(image_eeg, [224,224],antialias=True,preserve_aspect_ratio=True)                          \n    #image_eeg =tf.image.resize_with_pad(image_eeg_pt,224,224,antialias=True)\n\n    \n    image_spec = tf.io.decode_jpeg(example[\"image_spec\"])\n    image_spec = tf.image.crop_to_bounding_box(image_spec,offset_height=0,offset_width=11,target_height=240,target_width=240)\n    \n    \n    \n    #image_eeg = tf.image.convert_image_dtype(image_eeg, tf.float32)\n    #image_spec = tf.image.convert_image_dtype(image_spec, tf.float32)\n    \n    #images=[image_eeg, image_spec]\n    return image_eeg,image_spec, example[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:31:55.947679Z","iopub.execute_input":"2024-02-13T15:31:55.948551Z","iopub.status.idle":"2024-02-13T15:31:55.956446Z","shell.execute_reply.started":"2024-02-13T15:31:55.948518Z","shell.execute_reply":"2024-02-13T15:31:55.955511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def HMS_dataset(filepaths, n_read_threads=5, shuffle_buffer_size=None,\n                  n_parse_threads=5, batch_size=32, cache=True):\n    \n    dataset=tf.data.TFRecordDataset(filepaths, num_parallel_reads=n_read_threads)\n    \n    if cache:\n        dataset = dataset.cache()\n    if shuffle_buffer_size:\n        dataset = dataset.shuffle(shuffle_buffer_size)\n      \n    dataset = dataset.map(preprocess, num_parallel_calls=n_parse_threads)\n    dataset = dataset.batch(batch_size)\n    dataset = dataset.map(lambda img_eeg, img_spec, y: ({'eeg_input': img_eeg, 'spec_input': img_spec}, y))\n    \n    #img_eeg, img_spec, y = tf.unstack(dataset,axis=1)\n    return dataset.prefetch(1)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:31:57.960674Z","iopub.execute_input":"2024-02-13T15:31:57.961344Z","iopub.status.idle":"2024-02-13T15:31:57.967839Z","shell.execute_reply.started":"2024-02-13T15:31:57.961313Z","shell.execute_reply":"2024-02-13T15:31:57.966860Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_paths_tfrecords(name_subset, n_shards=5,path_file=TFRECORDS_FILES_DATASET):\n    #create paths\n    paths = [\"{}/{}.tfrecord-{:05d}-of-{:05d}\".format(path_file,name_subset, index, n_shards)\n             for index in range(n_shards)]\n    \n    return paths","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:32:00.312457Z","iopub.execute_input":"2024-02-13T15:32:00.313200Z","iopub.status.idle":"2024-02-13T15:32:00.318251Z","shell.execute_reply.started":"2024-02-13T15:32:00.313163Z","shell.execute_reply":"2024-02-13T15:32:00.317319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_filepaths=generate_paths_tfrecords('HMS.train', n_shards=50)\nvalid_filepaths = generate_paths_tfrecords('HMS.valid', n_shards=5)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:32:02.063541Z","iopub.execute_input":"2024-02-13T15:32:02.064373Z","iopub.status.idle":"2024-02-13T15:32:02.068452Z","shell.execute_reply.started":"2024-02-13T15:32:02.064337Z","shell.execute_reply":"2024-02-13T15:32:02.067579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS=20","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:32:03.861240Z","iopub.execute_input":"2024-02-13T15:32:03.861597Z","iopub.status.idle":"2024-02-13T15:32:03.865696Z","shell.execute_reply.started":"2024-02-13T15:32:03.861568Z","shell.execute_reply":"2024-02-13T15:32:03.864845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = HMS_dataset(train_filepaths, shuffle_buffer_size = 35000, batch_size=32)\nvalid_set = HMS_dataset(valid_filepaths)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:32:04.990858Z","iopub.execute_input":"2024-02-13T15:32:04.991196Z","iopub.status.idle":"2024-02-13T15:32:05.865505Z","shell.execute_reply.started":"2024-02-13T15:32:04.991168Z","shell.execute_reply":"2024-02-13T15:32:05.864762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.ignore_errors().take(1)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T14:56:59.776154Z","iopub.execute_input":"2024-02-13T14:56:59.776970Z","iopub.status.idle":"2024-02-13T14:56:59.787033Z","shell.execute_reply.started":"2024-02-13T14:56:59.776930Z","shell.execute_reply":"2024-02-13T14:56:59.785986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for X, y in valid_set.ignore_errors().take(1):\n    X['spec_input'].shape\n    for i in range(1):\n        print(X['eeg_input'][i].shape)\n        plt.subplot(1, 1, i + 1)\n        plt.imshow(X['eeg_input'][i].numpy(), cmap=\"binary\")\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:32:09.857563Z","iopub.execute_input":"2024-02-13T15:32:09.858357Z","iopub.status.idle":"2024-02-13T15:32:10.145144Z","shell.execute_reply.started":"2024-02-13T15:32:09.858327Z","shell.execute_reply":"2024-02-13T15:32:10.144234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:12:49.356949Z","iopub.execute_input":"2024-02-13T18:12:49.357822Z","iopub.status.idle":"2024-02-13T18:12:49.601520Z","shell.execute_reply.started":"2024-02-13T18:12:49.357781Z","shell.execute_reply":"2024-02-13T18:12:49.600452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_model = efficientnet_v2.EfficientNetV2B0(\n    include_top=False,\n    weights='imagenet',\n    #input_tensor=None,\n    input_shape=(224,224,3),\n    pooling= None,\n    include_preprocessing=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:03.304104Z","iopub.execute_input":"2024-02-13T18:13:03.304782Z","iopub.status.idle":"2024-02-13T18:13:06.477451Z","shell.execute_reply.started":"2024-02-13T18:13:03.304743Z","shell.execute_reply":"2024-02-13T18:13:06.476557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec_model =efficientnet_v2.EfficientNetV2B1(\n    include_top=False,\n    weights='imagenet',\n    #input_tensor=None,\n    input_shape=(240,240,3),\n    pooling= None,\n    include_preprocessing=True\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:06.479114Z","iopub.execute_input":"2024-02-13T18:13:06.479405Z","iopub.status.idle":"2024-02-13T18:13:09.775941Z","shell.execute_reply.started":"2024-02-13T18:13:06.479380Z","shell.execute_reply":"2024-02-13T18:13:09.774979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spec_model.trainable=False\neeg_model.trainable=False","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:12.655291Z","iopub.execute_input":"2024-02-13T18:13:12.656144Z","iopub.status.idle":"2024-02-13T18:13:12.685731Z","shell.execute_reply.started":"2024-02-13T18:13:12.656109Z","shell.execute_reply":"2024-02-13T18:13:12.684577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_eeg = keras.layers.Input(shape=[224,224,3],name='eeg_input')\n#norm_eeg = keras.layers.BatchNormalization()(input_eeg)\n\nmodel_eeg = eeg_model(input_eeg,training=False)\n#flatten_eeg = keras.layers.Flatten()(model_eeg)\ngap_eeg = keras.layers.GlobalAveragePooling2D()(model_eeg)\n\ninput_spec = keras.layers.Input(shape=[240,240,3],name='spec_input')\n#norm_spec = keras.layers.BatchNormalization()(input_spec)\nmodel_spec = spec_model(input_spec,training=False)\n#flatten_spec = keras.layers.Flatten()(model_spec)\ngap_spec = keras.layers.GlobalAveragePooling2D()(model_spec)\n\nconcat = keras.layers.Concatenate()([gap_eeg,gap_spec])\ndense_1 = keras.layers.Dense(64, kernel_initializer='he_normal')(concat)\nnorm_1 = keras.layers.BatchNormalization()(dense_1)\nact_1 = keras.layers.Activation(keras.activations.elu)(norm_1)\ndrop_1 = keras.layers.Dropout(0.4)(act_1)\n\ndense_2 = keras.layers.Dense(64, kernel_initializer='he_normal')(drop_1)\nnorm_2 = keras.layers.BatchNormalization()(dense_2)\nact_2 = keras.layers.Activation(keras.activations.elu)(norm_2)\ndrop_2 = keras.layers.Dropout(0.5)(act_2)\n\ndense_3 = keras.layers.Dense(64, kernel_initializer='he_normal')(drop_2)\nnorm_3 = keras.layers.BatchNormalization()(dense_3)\nact_3 = keras.layers.Activation(keras.activations.elu)(norm_3)\ndrop_3 = keras.layers.Dropout(0.6)(act_3)\n\noutput_layer = keras.layers.Dense(6, activation='softmax',dtype=tf.float32)(drop_3)\n\nmodel = keras.Model(inputs=[input_eeg,input_spec], outputs=[output_layer])","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:15.551769Z","iopub.execute_input":"2024-02-13T18:13:15.552204Z","iopub.status.idle":"2024-02-13T18:13:18.097490Z","shell.execute_reply.started":"2024-02-13T18:13:15.552170Z","shell.execute_reply":"2024-02-13T18:13:18.096538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('')","metadata":{"execution":{"iopub.status.busy":"2024-02-12T08:23:57.698011Z","iopub.execute_input":"2024-02-12T08:23:57.698367Z","iopub.status.idle":"2024-02-12T08:25:44.111461Z","shell.execute_reply.started":"2024-02-12T08:23:57.698337Z","shell.execute_reply":"2024-02-12T08:25:44.110616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=1e-4,\n    decay_steps=8000,\n    decay_rate=0.94,\n    staircase=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:33:56.452437Z","iopub.execute_input":"2024-02-13T15:33:56.453258Z","iopub.status.idle":"2024-02-13T15:33:56.457496Z","shell.execute_reply.started":"2024-02-13T15:33:56.453222Z","shell.execute_reply":"2024-02-13T15:33:56.456589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    #optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n    optimizer = tf.keras.optimizers.experimental.Nadam(learning_rate=5e-3),\n    loss=tf.keras.losses.KLDivergence(),\n    metrics=['binary_accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:34.581618Z","iopub.execute_input":"2024-02-13T18:13:34.582547Z","iopub.status.idle":"2024-02-13T18:13:34.613724Z","shell.execute_reply.started":"2024-02-13T18:13:34.582492Z","shell.execute_reply":"2024-02-13T18:13:34.612945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:38.526247Z","iopub.execute_input":"2024-02-13T18:13:38.526972Z","iopub.status.idle":"2024-02-13T18:13:38.643586Z","shell.execute_reply.started":"2024-02-13T18:13:38.526939Z","shell.execute_reply":"2024-02-13T18:13:38.642568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Entrenamiento","metadata":{}},{"cell_type":"code","source":"os.remove('/kaggle/working/best_model_st_8.keras')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T15:35:05.163108Z","iopub.execute_input":"2024-02-13T15:35:05.163836Z","iopub.status.idle":"2024-02-13T15:35:05.211796Z","shell.execute_reply.started":"2024-02-13T15:35:05.163795Z","shell.execute_reply":"2024-02-13T15:35:05.210899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PrintLearningRate(tf.keras.callbacks.Callback):\n    def on_epoch_begin(self, epoch, logs=None):\n        lr = self.model.optimizer.learning_rate.numpy()\n        print(f\"Epoch {epoch+1}, Learning Rate: {lr:.5f}\")","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:13:50.309823Z","iopub.execute_input":"2024-02-13T18:13:50.310222Z","iopub.status.idle":"2024-02-13T18:13:50.315665Z","shell.execute_reply.started":"2024-02-13T18:13:50.310192Z","shell.execute_reply":"2024-02-13T18:13:50.314586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_cb_2 = keras.callbacks.ModelCheckpoint('best_model_st_9.keras', save_best_only=True)\nearly_stoping_cb = keras.callbacks.EarlyStopping(patience=6)\nlearning_rate_callback = PrintLearningRate()\nreduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5,\n                              patience=2, min_lr=1e-5)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:14:04.233337Z","iopub.execute_input":"2024-02-13T18:14:04.233746Z","iopub.status.idle":"2024-02-13T18:14:04.239950Z","shell.execute_reply.started":"2024-02-13T18:14:04.233714Z","shell.execute_reply":"2024-02-13T18:14:04.238845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_set.ignore_errors(), epochs=EPOCHS*4, verbose=1,\n                  validation_data=valid_set,\n                  validation_steps= 200,\n                  #steps_per_epoch = 500,\n                  callbacks=[checkpoint_cb_2,early_stoping_cb,learning_rate_callback,reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2024-02-13T18:14:07.942300Z","iopub.execute_input":"2024-02-13T18:14:07.942719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_result=pd.DataFrame(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:35:50.337507Z","iopub.execute_input":"2024-02-13T16:35:50.338135Z","iopub.status.idle":"2024-02-13T16:35:50.345645Z","shell.execute_reply.started":"2024-02-13T16:35:50.338104Z","shell.execute_reply":"2024-02-13T16:35:50.344645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_result.plot()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:35:52.398579Z","iopub.execute_input":"2024-02-13T16:35:52.398923Z","iopub.status.idle":"2024-02-13T16:35:52.688682Z","shell.execute_reply.started":"2024-02-13T16:35:52.398897Z","shell.execute_reply":"2024-02-13T16:35:52.687912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft = keras.models.load_model('/kaggle/working/best_model_st_8.keras')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:36:29.953242Z","iopub.execute_input":"2024-02-13T16:36:29.953591Z","iopub.status.idle":"2024-02-13T16:37:29.903694Z","shell.execute_reply.started":"2024-02-13T16:36:29.953562Z","shell.execute_reply":"2024-02-13T16:37:29.902885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:37:30.013447Z","iopub.execute_input":"2024-02-13T16:37:30.013732Z","iopub.status.idle":"2024-02-13T16:37:30.118499Z","shell.execute_reply.started":"2024-02-13T16:37:30.013688Z","shell.execute_reply":"2024-02-13T16:37:30.117600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft.evaluate(valid_set)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:37:37.813308Z","iopub.execute_input":"2024-02-13T16:37:37.814174Z","iopub.status.idle":"2024-02-13T16:38:38.501871Z","shell.execute_reply.started":"2024-02-13T16:37:37.814141Z","shell.execute_reply":"2024-02-13T16:38:38.500933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eeg_model=model_ft.get_layer('efficientnetv2-b0')\nspec_model = model_ft.get_layer('efficientnetv2-b1')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T16:39:33.310749Z","iopub.execute_input":"2024-02-13T16:39:33.311114Z","iopub.status.idle":"2024-02-13T16:39:33.315893Z","shell.execute_reply.started":"2024-02-13T16:39:33.311084Z","shell.execute_reply":"2024-02-13T16:39:33.314925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in spec_model.layers[-61:]:\n    layer.trainable=True\n    \nfor layer in eeg_model.layers[-61:]:\n    layer.trainable=True","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:58:36.477147Z","iopub.execute_input":"2024-02-13T17:58:36.477571Z","iopub.status.idle":"2024-02-13T17:58:36.490202Z","shell.execute_reply.started":"2024-02-13T17:58:36.477537Z","shell.execute_reply":"2024-02-13T17:58:36.488999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in eeg_model.layers:\n    print(f'{layer.name}-----{layer.trainable}')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:04:06.837741Z","iopub.execute_input":"2024-02-13T13:04:06.838116Z","iopub.status.idle":"2024-02-13T13:04:06.845932Z","shell.execute_reply.started":"2024-02-13T13:04:06.838087Z","shell.execute_reply":"2024-02-13T13:04:06.845020Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in spec_model.layers:\n    print(f'{layer.name}-----{layer.trainable}')","metadata":{"execution":{"iopub.status.busy":"2024-02-13T11:33:18.176895Z","iopub.execute_input":"2024-02-13T11:33:18.178061Z","iopub.status.idle":"2024-02-13T11:33:18.185852Z","shell.execute_reply.started":"2024-02-13T11:33:18.178026Z","shell.execute_reply":"2024-02-13T11:33:18.184860Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft.compile(\n    #optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),\n    optimizer = tf.keras.optimizers.experimental.Nadam(learning_rate=5e-7),\n    loss=tf.keras.losses.KLDivergence(),\n    metrics=['binary_accuracy',tf.keras.metrics.AUC()]\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:58:39.441568Z","iopub.execute_input":"2024-02-13T17:58:39.441961Z","iopub.status.idle":"2024-02-13T17:58:39.486741Z","shell.execute_reply.started":"2024-02-13T17:58:39.441931Z","shell.execute_reply":"2024-02-13T17:58:39.485484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft.summary()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:58:47.042689Z","iopub.execute_input":"2024-02-13T17:58:47.043446Z","iopub.status.idle":"2024-02-13T17:58:47.169617Z","shell.execute_reply.started":"2024-02-13T17:58:47.043410Z","shell.execute_reply":"2024-02-13T17:58:47.168628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_cb_2 = keras.callbacks.ModelCheckpoint('best_model_st_8_ft.keras', save_best_only=True)\nearly_stoping_cb = keras.callbacks.EarlyStopping(patience=6)\nlearning_rate_callback = PrintLearningRate()\nreduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.8,\n                              patience=2, min_lr=1e-8)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:59:15.771657Z","iopub.execute_input":"2024-02-13T17:59:15.772056Z","iopub.status.idle":"2024-02-13T17:59:15.779523Z","shell.execute_reply.started":"2024-02-13T17:59:15.772028Z","shell.execute_reply":"2024-02-13T17:59:15.778718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model_ft.fit(train_set.ignore_errors(), epochs=EPOCHS*4, verbose=1,\n                  validation_data=valid_set,\n                  validation_steps= 200,\n                  #steps_per_epoch = 500,\n                  callbacks=[checkpoint_cb_2,early_stoping_cb,learning_rate_callback,reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:59:18.638364Z","iopub.execute_input":"2024-02-13T17:59:18.639040Z","iopub.status.idle":"2024-02-13T18:12:04.126415Z","shell.execute_reply.started":"2024-02-13T17:59:18.639009Z","shell.execute_reply":"2024-02-13T18:12:04.124872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_ft.evaluate(valid_set)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T17:54:18.790289Z","iopub.execute_input":"2024-02-13T17:54:18.790680Z","iopub.status.idle":"2024-02-13T17:55:12.746278Z","shell.execute_reply.started":"2024-02-13T17:54:18.790647Z","shell.execute_reply":"2024-02-13T17:55:12.744973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}