{"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":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30700,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import keras\nimport keras.backend as K\nimport tensorflow as tf\nimport tensorflow_io as tfio\n\nimport numpy as np \nimport pandas as pd\nimport os\nos.environment = 'jax'\nfrom glob import glob\nfrom tqdm import tqdm\n\nimport librosa\nimport IPython.display as ipd\nimport librosa.display as lid\n\nimport matplotlib.pyplot as plt\nimport matplotlib as mpl\n\ncmap = mpl.cm.get_cmap('coolwarm')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-23T18:05:32.981094Z","iopub.execute_input":"2024-04-23T18:05:32.981468Z","iopub.status.idle":"2024-04-23T18:05:47.367714Z","shell.execute_reply.started":"2024-04-23T18:05:32.981436Z","shell.execute_reply":"2024-04-23T18:05:47.366652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(\"--> Checking configured TensorFlow devices\")\nprint(tf.config.list_logical_devices('GPU'))","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:05:47.369472Z","iopub.execute_input":"2024-04-23T18:05:47.370016Z","iopub.status.idle":"2024-04-23T18:05:47.688190Z","shell.execute_reply.started":"2024-04-23T18:05:47.369989Z","shell.execute_reply":"2024-04-23T18:05:47.687071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    seed = 42\n    \n    # Input image size and batch size\n    img_size = [128, 384]\n    batch_size = 64\n    \n    # Audio duration, sample rate, and length\n    duration = 15 # second\n    sample_rate = 32000\n    audio_len = duration*sample_rate\n    \n    # STFT parameters\n    nfft = 2028\n    window = 2048\n    hop_length = audio_len // (img_size[1] - 1)\n    fmin = 20\n    fmax = 16000\n    \n    # Number of epochs, model name\n    epochs = 30\n#     preset = 'efficientnetv2_b2_imagenet'\n    \n    # Data augmentation parameters\n    augment=True\n\n    # Class Labels for BirdCLEF 24\n    class_names = sorted(os.listdir('/kaggle/input/birdclef-2024/train_audio/'))\n    num_classes = len(class_names)\n    class_labels = list(range(num_classes))\n    label2name = dict(zip(class_labels, class_names))\n    name2label = {v:k for k,v in label2name.items()}","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:06:39.273677Z","iopub.execute_input":"2024-04-23T18:06:39.274555Z","iopub.status.idle":"2024-04-23T18:06:39.287844Z","shell.execute_reply.started":"2024-04-23T18:06:39.274514Z","shell.execute_reply":"2024-04-23T18:06:39.286621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.set_random_seed(Config.seed)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:05:47.807841Z","iopub.execute_input":"2024-04-23T18:05:47.808155Z","iopub.status.idle":"2024-04-23T18:05:47.812604Z","shell.execute_reply.started":"2024-04-23T18:05:47.808128Z","shell.execute_reply":"2024-04-23T18:05:47.811658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_PATH = '/kaggle/input/birdclef-2024'","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:05:47.813803Z","iopub.execute_input":"2024-04-23T18:05:47.814094Z","iopub.status.idle":"2024-04-23T18:05:47.823568Z","shell.execute_reply.started":"2024-04-23T18:05:47.814071Z","shell.execute_reply":"2024-04-23T18:05:47.822558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f'{BASE_PATH}/train_metadata.csv')\ndf['filepath'] = BASE_PATH + '/train_audio/' + df.filename\ndf['target'] = df.primary_label.map(Config.name2label)\ndf['filename'] = df.filepath.map(lambda x: x.split('/')[-1])\ndf['xc_id'] = df.filepath.map(lambda x: x.split('/')[-1].split('.')[0])\n\n# Display rwos\ndf.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:06:43.417220Z","iopub.execute_input":"2024-04-23T18:06:43.417655Z","iopub.status.idle":"2024-04-23T18:06:43.703144Z","shell.execute_reply.started":"2024-04-23T18:06:43.417621Z","shell.execute_reply":"2024-04-23T18:06:43.701983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_audio(filepath):\n    audio, sr = librosa.load(filepath)\n    return audio, sr\n\ndef get_spectrogram(audio):\n    spec = librosa.feature.melspectrogram(y=audio, \n                                   sr=Config.sample_rate, \n                                   n_mels=256,\n                                   n_fft=2048,\n                                   hop_length=512,\n                                   fmax=Config.fmax,\n                                   fmin=Config.fmin,\n                                   )\n    spec = librosa.power_to_db(spec, ref=1.0)\n    min_ = spec.min()\n    max_ = spec.max()\n    if max_ != min_:\n        spec = (spec - min_)/(max_ - min_)\n    return spec\n\ndef display_audio(row):\n    # Caption for viz\n    caption = f'Id: {row.filename} | Name: {row.common_name} | Sci.Name: {row.scientific_name} | Rating: {row.rating}'\n    # Read audio file\n    audio, sr = load_audio(row.filepath)\n    # Keep fixed length audio\n    audio = audio[:Config.audio_len]\n    # Spectrogram from audio\n    spec = get_spectrogram(audio)\n    # Display audio\n    print(\"# Audio:\")\n    display(ipd.Audio(audio, rate=Config.sample_rate))\n    print('# Visualization:')\n    fig, ax = plt.subplots(2, 1, figsize=(12, 2*3), sharex=True, tight_layout=True)\n    fig.suptitle(caption)\n    # Waveplot\n    lid.waveshow(audio,\n                 sr=Config.sample_rate,\n                 ax=ax[0],\n                 color= cmap(0.1))\n    # Specplot\n    lid.specshow(spec, \n                 sr = Config.sample_rate, \n                 hop_length=512,\n                 n_fft=2048,\n                 fmin=Config.fmin,\n                 fmax=Config.fmax,\n                 x_axis = 'time', \n                 y_axis = 'mel',\n                 cmap = 'coolwarm',\n                 ax=ax[1])\n    ax[0].set_xlabel('');\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:06:45.887952Z","iopub.execute_input":"2024-04-23T18:06:45.888371Z","iopub.status.idle":"2024-04-23T18:06:45.901760Z","shell.execute_reply.started":"2024-04-23T18:06:45.888339Z","shell.execute_reply":"2024-04-23T18:06:45.900587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# row = df.iloc[35]\n\n# # Display audio\n# display_audio(row)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T15:36:36.434814Z","iopub.execute_input":"2024-04-23T15:36:36.435183Z","iopub.status.idle":"2024-04-23T15:36:36.439583Z","shell.execute_reply.started":"2024-04-23T15:36:36.435153Z","shell.execute_reply":"2024-04-23T15:36:36.438542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df, valid_df = train_test_split(df, test_size=0.2, stratify=df['target'])\n\nprint(f\"Num Train: {len(train_df)} | Num Valid: {len(valid_df)}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:06:47.843987Z","iopub.execute_input":"2024-04-23T18:06:47.844384Z","iopub.status.idle":"2024-04-23T18:06:48.367274Z","shell.execute_reply.started":"2024-04-23T18:06:47.844357Z","shell.execute_reply":"2024-04-23T18:06:48.366195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Decodes Audio\ndef build_decoder(with_labels=True, dim=1024):\n    def get_audio(filepath):\n        file_bytes = tf.io.read_file(filepath)\n        audio = tfio.audio.decode_vorbis(file_bytes)  # decode .ogg file\n        audio = tf.cast(audio, tf.float32)\n        if tf.shape(audio)[1] > 1:  # stereo -> mono\n            audio = audio[..., 0:1]\n        audio = tf.squeeze(audio, axis=-1)\n        return audio\n\n    def crop_or_pad(audio, target_len, pad_mode=\"constant\"):\n        audio_len = tf.shape(audio)[0]\n        diff_len = abs(\n            target_len - audio_len\n        )  # find difference between target and audio length\n        if audio_len < target_len:  # do padding if audio length is shorter\n            pad1 = tf.random.uniform([], maxval=diff_len, dtype=tf.int32)\n            pad2 = diff_len - pad1\n            audio = tf.pad(audio, paddings=[[pad1, pad2]], mode=pad_mode)\n        elif audio_len > target_len:  # do cropping if audio length is larger\n            idx = tf.random.uniform([], maxval=diff_len, dtype=tf.int32)\n            audio = audio[idx : (idx + target_len)]\n        return tf.reshape(audio, [target_len])\n\n    def apply_preproc(spec):\n        # Standardize\n        mean = tf.math.reduce_mean(spec)\n        std = tf.math.reduce_std(spec)\n        spec = tf.where(tf.math.equal(std, 0), spec - mean, (spec - mean) / std)\n\n        # Normalize using Min-Max\n        min_val = tf.math.reduce_min(spec)\n        max_val = tf.math.reduce_max(spec)\n        spec = tf.where(\n            tf.math.equal(max_val - min_val, 0),\n            spec - min_val,\n            (spec - min_val) / (max_val - min_val),\n        )\n        return spec\n\n    def get_target(target):\n        target = tf.reshape(target, [1])\n        target = tf.cast(tf.one_hot(target, Config.num_classes), tf.float32)\n        target = tf.reshape(target, [Config.num_classes])\n        return target\n\n    def decode(path):\n        # Load audio file\n        audio = get_audio(path)\n        # Crop or pad audio to keep a fixed length\n        audio = crop_or_pad(audio, dim)\n        # Audio to Spectrogram\n        spec = keras.layers.MelSpectrogram(\n            num_mel_bins=Config.img_size[0],\n            fft_length=Config.nfft,\n            sequence_stride=Config.hop_length,\n            sampling_rate=Config.sample_rate,\n        )(audio)\n        # Apply normalization and standardization\n        spec = apply_preproc(spec)\n        # Spectrogram to 3 channel image (for imagenet)\n        spec = tf.tile(spec[..., None], [1, 1, 3])\n        spec = tf.reshape(spec, [*Config.img_size, 3])\n        return spec\n\n    def decode_with_labels(path, label):\n        label = get_target(label)\n        return decode(path), label\n\n    return decode_with_labels if with_labels else decode","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:06:51.587938Z","iopub.execute_input":"2024-04-23T18:06:51.588631Z","iopub.status.idle":"2024-04-23T18:06:51.606561Z","shell.execute_reply.started":"2024-04-23T18:06:51.588595Z","shell.execute_reply":"2024-04-23T18:06:51.605576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_dataset(paths, labels=None, batch_size=32, \n                  decode_fn=None, cache=True,\n                  augment=False, shuffle=2048):\n\n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None, dim=Config.audio_len)\n        \n    AUTO = tf.data.experimental.AUTOTUNE\n    slices = (paths,) if labels is None else (paths, labels)\n    ds = tf.data.Dataset.from_tensor_slices(slices)\n    ds = ds.map(decode_fn, num_parallel_calls=AUTO)\n    ds = ds.cache() if cache else ds\n    if shuffle:\n        opt = tf.data.Options()\n        ds = ds.shuffle(shuffle, seed=Config.seed)\n        opt.experimental_deterministic = False\n        ds = ds.with_options(opt)\n    ds = ds.batch(batch_size, drop_remainder=True)\n#     ds = ds.map(augment_fn, num_parallel_calls=AUTO) if augment else ds\n    ds = ds.prefetch(AUTO)\n    return ds","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:02.937053Z","iopub.execute_input":"2024-04-23T18:07:02.937458Z","iopub.status.idle":"2024-04-23T18:07:02.946444Z","shell.execute_reply.started":"2024-04-23T18:07:02.937426Z","shell.execute_reply":"2024-04-23T18:07:02.945316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train\ntrain_paths = train_df.filepath.values\ntrain_labels = train_df.target.values\ntrain_ds = build_dataset(train_paths, train_labels, batch_size=Config.batch_size,\n                         shuffle=True, augment=Config.augment)\n\n# Valid\nvalid_paths = valid_df.filepath.values\nvalid_labels = valid_df.target.values\nvalid_ds = build_dataset(valid_paths, valid_labels, batch_size=Config.batch_size,\n                         shuffle=False, augment=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:06.755368Z","iopub.execute_input":"2024-04-23T18:07:06.755887Z","iopub.status.idle":"2024-04-23T18:07:11.284090Z","shell.execute_reply.started":"2024-04-23T18:07:06.755846Z","shell.execute_reply":"2024-04-23T18:07:11.283131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i, (x,y) in enumerate(train_ds):\n#     print(x.shape, y.shape)\n#     if i == 5:\n#         break","metadata":{"execution":{"iopub.status.busy":"2024-04-23T15:36:49.321030Z","iopub.execute_input":"2024-04-23T15:36:49.321377Z","iopub.status.idle":"2024-04-23T15:36:49.325998Z","shell.execute_reply.started":"2024-04-23T15:36:49.321347Z","shell.execute_reply":"2024-04-23T15:36:49.324948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_batch(batch, row=3, col=3, label2name=None,):\n    \"\"\"Plot one batch data\"\"\"\n    if isinstance(batch, tuple) or isinstance(batch, list):\n        specs, tars = batch\n    else:\n        specs = batch\n        tars = None\n    plt.figure(figsize=(col*5, row*3))\n    for idx in range(row*col):\n        ax = plt.subplot(row, col, idx+1)\n        lid.specshow(np.array(specs[idx, ..., 0]), \n                     n_fft=Config.nfft, \n                     hop_length=Config.hop_length, \n                     sr=Config.sample_rate,\n                     x_axis='time',\n                     y_axis='mel',\n                     cmap='coolwarm')\n        if tars is not None:\n            label = tars[idx].numpy().argmax()\n            name = label2name[label]\n            plt.title(name)\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:14.113318Z","iopub.execute_input":"2024-04-23T18:07:14.113729Z","iopub.status.idle":"2024-04-23T18:07:14.123641Z","shell.execute_reply.started":"2024-04-23T18:07:14.113698Z","shell.execute_reply":"2024-04-23T18:07:14.122564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_ds = train_ds.take(100)\n# batch = next(iter(sample_ds))\n# plot_batch(batch, label2name=Config.label2name)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T15:36:49.732868Z","iopub.execute_input":"2024-04-23T15:36:49.733792Z","iopub.status.idle":"2024-04-23T15:36:49.737454Z","shell.execute_reply.started":"2024-04-23T15:36:49.733757Z","shell.execute_reply":"2024-04-23T15:36:49.736370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\n\ndef get_lr_callback(batch_size=8, mode='step', epochs=30, plot=False):\n    lr_start, lr_max, lr_min = 5e-5, 8e-6 * batch_size, 1e-5\n    lr_ramp_ep, lr_sus_ep, lr_decay = 3, 0, 0.75\n\n    def lrfn(epoch):  # Learning rate update function\n        if epoch < lr_ramp_ep: lr = (lr_max - lr_start) / lr_ramp_ep * epoch + lr_start\n        elif epoch < lr_ramp_ep + lr_sus_ep: lr = lr_max\n        elif mode == 'exp': lr = (lr_max - lr_min) * lr_decay**(epoch - lr_ramp_ep - lr_sus_ep) + lr_min\n        elif mode == 'step': lr = lr_max * lr_decay**((epoch - lr_ramp_ep - lr_sus_ep) // 2)\n        elif mode == 'cos':\n            decay_total_epochs, decay_epoch_index = epochs - lr_ramp_ep - lr_sus_ep + 3, epoch - lr_ramp_ep - lr_sus_ep\n            phase = math.pi * decay_epoch_index / decay_total_epochs\n            lr = (lr_max - lr_min) * 0.5 * (1 + math.cos(phase)) + lr_min\n        return lr\n\n    if plot:  # Plot lr curve if plot is True\n        plt.figure(figsize=(10, 5))\n        plt.plot(np.arange(epochs), [lrfn(epoch) for epoch in np.arange(epochs)], marker='o')\n        plt.xlabel('epoch'); plt.ylabel('lr')\n        plt.title('LR Scheduler')\n        plt.show()\n\n    return keras.callbacks.LearningRateScheduler(lrfn, verbose=False)  # Create lr callback","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:25.097633Z","iopub.execute_input":"2024-04-23T18:07:25.097987Z","iopub.status.idle":"2024-04-23T18:07:25.109090Z","shell.execute_reply.started":"2024-04-23T18:07:25.097961Z","shell.execute_reply":"2024-04-23T18:07:25.107726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_cb = get_lr_callback(Config.batch_size, plot=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:27.107168Z","iopub.execute_input":"2024-04-23T18:07:27.107600Z","iopub.status.idle":"2024-04-23T18:07:27.451729Z","shell.execute_reply.started":"2024-04-23T18:07:27.107565Z","shell.execute_reply":"2024-04-23T18:07:27.450682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_save_path = \"/kaggle/working/EfficientNetV2B0_v1.weights.h5\"","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:37.660574Z","iopub.execute_input":"2024-04-23T18:07:37.661421Z","iopub.status.idle":"2024-04-23T18:07:37.665698Z","shell.execute_reply.started":"2024-04-23T18:07:37.661386Z","shell.execute_reply":"2024-04-23T18:07:37.664760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ckpt_cb = keras.callbacks.ModelCheckpoint(model_save_path,\n                                         monitor='val_auc',\n                                         save_best_only=True,\n                                         save_weights_only=True,\n                                         mode='max')","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:39.896806Z","iopub.execute_input":"2024-04-23T18:07:39.897703Z","iopub.status.idle":"2024-04-23T18:07:39.902717Z","shell.execute_reply.started":"2024-04-23T18:07:39.897663Z","shell.execute_reply":"2024-04-23T18:07:39.901574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Efficient net without augmentation4\nclass EfficientNetModel:\n    \n    def __init__(self,num_classes, input_shape=(128, 384, 3)):\n        self.input_shape = input_shape\n        self.num_classes = num_classes\n        self.data_augmentation = tf.keras.Sequential([\n            tf.keras.layers.RandomFlip(\"horizontal_and_vertical\"),\n            tf.keras.layers.RandomContrast(0.2),\n            tf.keras.layers.RandomZoom(0.2),\n            tf.keras.layers.RandomRotation(factor=0.15),\n            tf.keras.layers.RandomTranslation(height_factor=0.1, width_factor=0.1),\n            tf.keras.layers.RandomContrast(factor=0.1),\n            ],\n            name=\"img_augmentation\",\n            )\n        self.base_model = tf.keras.applications.EfficientNetV2L(input_shape=self.input_shape, include_top=False, weights='imagenet')\n#         self.base_model.trainable = False\n        self.model = self.build_model()\n        \n    def build_model(self):\n        inputs = tf.keras.layers.Input(shape=self.input_shape)\n#         x = self.data_augmentation(inputs)\n        x = self.base_model(inputs)\n        \n        for layer in self.base_model.layers[:60]:  #-20\n#             if not isinstance(layer, tf.keras.layers.BatchNormalization):\n                layer.trainable = False\n                \n        x = tf.keras.layers.GlobalAveragePooling2D()(x)\n        \n        predictions = tf.keras.layers.Dense(self.num_classes, activation='softmax')(x)\n        model = tf.keras.models.Model(inputs=inputs, outputs=predictions)\n        return model\n     \n    def compile(self, learning_rate = 0.0001):\n            self.model.compile(optimizer='adam',\n                               loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.02),\n                               metrics=[keras.metrics.AUC(name='auc')])\n\n    def train(self, train_data, val_data, epochs, batch_size):\n        callbacks=[lr_cb, ckpt_cb]\n#         self.compile()\n        history = self.model.fit(train_data, \n                                 epochs=epochs,\n                                 batch_size=batch_size, \n                                 validation_data=val_data, \n                                 callbacks=callbacks,\n                                 verbose=1)\n        return history\n    def evaluate(self, data):\n        loss, accuracy = self.model.evaluate(data)\n        return loss, accuracy\n    \n    def predict(self, data):\n        return self.model.predict(data)\n    \n    def summary(self):\n        return self.model.summary()\n    \n    def save_model(self, filepath):\n        self.model.save(filepath)\n        \n    def load_model(self, model_path):\n        self.model.load_weights(model_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:44.372075Z","iopub.execute_input":"2024-04-23T18:07:44.372718Z","iopub.status.idle":"2024-04-23T18:07:44.389223Z","shell.execute_reply.started":"2024-04-23T18:07:44.372685Z","shell.execute_reply":"2024-04-23T18:07:44.388123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = EfficientNetModel(\n                        num_classes = Config.num_classes)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:07:46.699439Z","iopub.execute_input":"2024-04-23T18:07:46.699857Z","iopub.status.idle":"2024-04-23T18:07:57.955939Z","shell.execute_reply.started":"2024-04-23T18:07:46.699804Z","shell.execute_reply":"2024-04-23T18:07:57.955008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if model_save_path is not None and os.path.isfile(model_save_path):\n    print(\"INFO ===========Running the Partially Trained Model===============\")\n    #This code is implemented to load the partly trained model which was stopped due to some reason\n    model.load_model(model_save_path)\n    with tf.device('/GPU:0'):  # Wrap training in device block\n        model.compile()\n        history = model.train(\n            train_data= train_ds,\n            val_data=valid_ds,\n            epochs=Config.epochs,\n            batch_size = Config.batch_size\n        )\nelse:\n    print(\"INFO ===========Running the Training of Model from Scratch===============\")\n    \n    with tf.device('/GPU:0'):  # Wrap training in device block\n        model.compile()\n        history = model.train(\n            train_data= train_ds,\n            val_data=valid_ds,\n            epochs=Config.epochs,\n            batch_size = Config.batch_size\n        )\n\nprint(f\"INFO ===========Training Finished===============\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T18:08:05.294378Z","iopub.execute_input":"2024-04-23T18:08:05.294745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inp = keras.layers.Input(shape=(None, None, 3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"data_augmentation = keras.Sequential([\n  keras.layers.RandomFlip(\"horizontal_and_vertical\"),\n  keras.layers.RandomContrast(0.2),\n  keras.layers.RandomZoom(0.2),\n  keras.layers.RandomRotation(factor=0.15),\n  keras.layers.RandomTranslation(height_factor=0.1, width_factor=0.1),\n  keras.layers.RandomContrast(factor=0.1),\n], name=\"img_augmentation\")","metadata":{}},{"cell_type":"markdown","source":"inp = keras.layers.Input(shape=(None, None, 3))\nx = data_augmentation(inp)\nefficientnet_base = keras.applications.EfficientNetV2L(\n    weights=\"imagenet\", include_top=False, input_shape=inp.shape\n)\nfor layer in efficientnet_base.layers[:60]:\n    layer.trainable = False\n\nx = keras.layers.GlobalAveragePooling2D()(x)\n\n# Dense output layer\nout = keras.layers.Dense(CFG.num_classes, activation=\"softmax\")(x)\n\n# Build the model\nmodel = keras.models.Model(inputs=inp, outputs=out)\n\n# Load weights of trained model\nmodel.load_weights(\"/kaggle/input/birdclef24_effnet/tensorflow2/v2b3/1/efficientnetv2B3_v1.weights.h5\")","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}