{"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 # linear algebra\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport tqdm\nimport gc\nimport sys\nimport math\nimport tensorflow as tf\nimport keras\nimport keras.layers as L\nfrom keras.models import Model,Sequential\nfrom keras import regularizers\nfrom sklearn.utils import class_weight\nimport re\nfrom tensorflow.keras.utils import Sequence\nimport warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-24T13:18:50.020234Z","iopub.execute_input":"2022-02-24T13:18:50.021024Z","iopub.status.idle":"2022-02-24T13:18:56.123752Z","shell.execute_reply.started":"2022-02-24T13:18:50.020919Z","shell.execute_reply":"2022-02-24T13:18:56.122926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/happy-whale-and-dolphin/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:56.125614Z","iopub.execute_input":"2022-02-24T13:18:56.125885Z","iopub.status.idle":"2022-02-24T13:18:56.228951Z","shell.execute_reply.started":"2022-02-24T13:18:56.125848Z","shell.execute_reply":"2022-02-24T13:18:56.228193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('../input/happy-whale-and-dolphin/sample_submission.csv')\ntest_list = sub['image']","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:56.230284Z","iopub.execute_input":"2022-02-24T13:18:56.230727Z","iopub.status.idle":"2022-02-24T13:18:56.287487Z","shell.execute_reply.started":"2022-02-24T13:18:56.230687Z","shell.execute_reply":"2022-02-24T13:18:56.286728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_base_addr = '../input/happy-whale-and-dolphin/train_images/'\ntest_base_addr = '../input/happy-whale-and-dolphin/test_images/'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:56.289777Z","iopub.execute_input":"2022-02-24T13:18:56.290057Z","iopub.status.idle":"2022-02-24T13:18:56.293889Z","shell.execute_reply.started":"2022-02-24T13:18:56.290018Z","shell.execute_reply":"2022-02-24T13:18:56.292972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(train_base_addr + train['image'][0])\nplt.imshow(img)\nprint(\"Mammal Id = \" + train['individual_id'][0])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:56.295630Z","iopub.execute_input":"2022-02-24T13:18:56.296099Z","iopub.status.idle":"2022-02-24T13:18:56.671515Z","shell.execute_reply.started":"2022-02-24T13:18:56.296061Z","shell.execute_reply":"2022-02-24T13:18:56.670832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread(train_base_addr + train['image'][1])\nplt.imshow(img)\nprint(\"Mammal Id = \" + train['individual_id'][1])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:56.672419Z","iopub.execute_input":"2022-02-24T13:18:56.672633Z","iopub.status.idle":"2022-02-24T13:18:58.100905Z","shell.execute_reply.started":"2022-02-24T13:18:56.672604Z","shell.execute_reply":"2022-02-24T13:18:58.100225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ID_list = train['individual_id'].to_list()\nUnique_IDs = set(ID_list)\nprint(\"Number of Unique Dolphins/Whales Identified = \" , len(Unique_IDs))\nprint(\"Total Number of Unique Dolphins/Whales Identified = \" , len(ID_list))","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:58.102341Z","iopub.execute_input":"2022-02-24T13:18:58.103165Z","iopub.status.idle":"2022-02-24T13:18:58.112713Z","shell.execute_reply.started":"2022-02-24T13:18:58.103123Z","shell.execute_reply":"2022-02-24T13:18:58.111770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 10\nsns.countplot(y=\"individual_id\", data=train, palette=\"Greens_d\",order=train.individual_id.value_counts().iloc[: count].index).set_title(f'Top {count} Whales/Dolphin sited')","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:58.114126Z","iopub.execute_input":"2022-02-24T13:18:58.114517Z","iopub.status.idle":"2022-02-24T13:18:58.474129Z","shell.execute_reply.started":"2022-02-24T13:18:58.114476Z","shell.execute_reply":"2022-02-24T13:18:58.473455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"occurence_count = train.individual_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:58.475501Z","iopub.execute_input":"2022-02-24T13:18:58.475953Z","iopub.status.idle":"2022-02-24T13:18:58.492034Z","shell.execute_reply.started":"2022-02-24T13:18:58.475913Z","shell.execute_reply":"2022-02-24T13:18:58.491263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count = 20\ntop_occurences = len([i for i in occurence_count if i > count])\nprint(f\"Number of Whales/Dolphin which occur more than {count} times = \", top_occurences)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:18:58.494506Z","iopub.execute_input":"2022-02-24T13:18:58.494797Z","iopub.status.idle":"2022-02-24T13:18:58.502924Z","shell.execute_reply.started":"2022-02-24T13:18:58.494760Z","shell.execute_reply":"2022-02-24T13:18:58.502134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn import preprocessing\nlabel_encoder = preprocessing.LabelEncoder() ","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:02.690182Z","iopub.execute_input":"2022-02-24T13:19:02.690470Z","iopub.status.idle":"2022-02-24T13:19:02.716204Z","shell.execute_reply.started":"2022-02-24T13:19:02.690437Z","shell.execute_reply":"2022-02-24T13:19:02.715579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.array(train['individual_id'])\nlabel_encoded = label_encoder.fit_transform(y)\ny = label_encoded","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:03.061834Z","iopub.execute_input":"2022-02-24T13:19:03.062287Z","iopub.status.idle":"2022-02-24T13:19:03.090310Z","shell.execute_reply.started":"2022-02-24T13:19:03.062254Z","shell.execute_reply":"2022-02-24T13:19:03.089654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:03.338609Z","iopub.execute_input":"2022-02-24T13:19:03.339137Z","iopub.status.idle":"2022-02-24T13:19:03.346978Z","shell.execute_reply.started":"2022-02-24T13:19:03.339106Z","shell.execute_reply":"2022-02-24T13:19:03.346222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le_name_mapping = dict(zip(label_encoder.classes_, label_encoder.transform(label_encoder.classes_)))\n#print(le_name_mapping)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:03.745653Z","iopub.execute_input":"2022-02-24T13:19:03.746091Z","iopub.status.idle":"2022-02-24T13:19:03.762205Z","shell.execute_reply.started":"2022-02-24T13:19:03.746060Z","shell.execute_reply":"2022-02-24T13:19:03.761573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reverse_le_name_mapping = dict(zip( label_encoder.transform(label_encoder.classes_), label_encoder.classes_))\n#print(reverse_le_name_mapping)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:04.470018Z","iopub.execute_input":"2022-02-24T13:19:04.470591Z","iopub.status.idle":"2022-02-24T13:19:04.486347Z","shell.execute_reply.started":"2022-02-24T13:19:04.470551Z","shell.execute_reply":"2022-02-24T13:19:04.485561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_to_id_mapping = dict(zip(train[\"image\"], train[\"individual_id\"]))\n#print(img_to_id_mapping)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:04.981659Z","iopub.execute_input":"2022-02-24T13:19:04.981977Z","iopub.status.idle":"2022-02-24T13:19:05.005148Z","shell.execute_reply.started":"2022-02-24T13:19:04.981947Z","shell.execute_reply":"2022-02-24T13:19:05.004472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:05.539098Z","iopub.execute_input":"2022-02-24T13:19:05.539650Z","iopub.status.idle":"2022-02-24T13:19:05.701530Z","shell.execute_reply.started":"2022-02-24T13:19:05.539609Z","shell.execute_reply":"2022-02-24T13:19:05.700687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"second_base_addr = '../input/happywhale-data/insta/'\nsecond_test_addr = '../input/happywhale-test/insta/'","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:06.225224Z","iopub.execute_input":"2022-02-24T13:19:06.225509Z","iopub.status.idle":"2022-02-24T13:19:06.230137Z","shell.execute_reply.started":"2022-02-24T13:19:06.225478Z","shell.execute_reply":"2022-02-24T13:19:06.229466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(Sequence):\n    def __init__(self,idx,base_addr,directory,batch_size=16,shuffle=True):\n        self.idx = idx\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        self.directory = directory\n        self.base_addr = base_addr\n        self.is_train = True\n        self.y = []\n        if self.directory == 'test':\n            self.is_train = False\n        \n    def __len__(self):\n        return math.ceil(len(self.idx)/self.batch_size)\n    def getimg(self,x):\n        idz = self.base_addr + x + '.npy'\n        p = np.load(idz)\n        #print(p.shape)\n        #print(p)\n        return p\n    def getlabel(self,x):\n        idz = img_to_id_mapping[x]\n        le_label = le_name_mapping[idz]\n        yz = le_label\n       # print(y.shape)\n        #print(y)\n        return yz\n                  \n    def __getitem__(self,ids):\n        batch_ids = self.idx[ids * self.batch_size:(ids + 1) * self.batch_size]    \n        list_x1 = np.array([self.getimg(x) for x in batch_ids])/255\n       # print(list_x1.shape)\n        if self.directory != 'test':\n            batch_y = np.array([self.getlabel(x) for x in batch_ids])\n            #print(batch_y.shape)\n            return [list_x1, batch_y], batch_y\n        else:\n            return list_x1","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:07.031723Z","iopub.execute_input":"2022-02-24T13:19:07.032570Z","iopub.status.idle":"2022-02-24T13:19:07.042642Z","shell.execute_reply.started":"2022-02-24T13:19:07.032520Z","shell.execute_reply":"2022-02-24T13:19:07.041890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:07.702041Z","iopub.execute_input":"2022-02-24T13:19:07.702796Z","iopub.status.idle":"2022-02-24T13:19:07.750278Z","shell.execute_reply.started":"2022-02-24T13:19:07.702743Z","shell.execute_reply":"2022-02-24T13:19:07.749534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(train['image'], y, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:08.065988Z","iopub.execute_input":"2022-02-24T13:19:08.066233Z","iopub.status.idle":"2022-02-24T13:19:08.078173Z","shell.execute_reply.started":"2022-02-24T13:19:08.066203Z","shell.execute_reply":"2022-02-24T13:19:08.077470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:08.491842Z","iopub.execute_input":"2022-02-24T13:19:08.492080Z","iopub.status.idle":"2022-02-24T13:19:08.497731Z","shell.execute_reply.started":"2022-02-24T13:19:08.492052Z","shell.execute_reply":"2022-02-24T13:19:08.496918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = Dataset(X_train,second_base_addr, 'train',32)\nvalid_dataset = Dataset(X_test,second_base_addr,'train',32)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:18.539923Z","iopub.execute_input":"2022-02-24T13:22:18.540235Z","iopub.status.idle":"2022-02-24T13:22:18.548242Z","shell.execute_reply.started":"2022-02-24T13:22:18.540194Z","shell.execute_reply":"2022-02-24T13:22:18.547603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_dataset = Dataset(X_test,second_base_addr,'test',32)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:18.827699Z","iopub.execute_input":"2022-02-24T13:22:18.828327Z","iopub.status.idle":"2022-02-24T13:22:18.832630Z","shell.execute_reply.started":"2022-02-24T13:22:18.828281Z","shell.execute_reply":"2022-02-24T13:22:18.831828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_embed_dataset = Dataset(X_train,second_base_addr, 'test',32)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:19.090676Z","iopub.execute_input":"2022-02-24T13:22:19.090888Z","iopub.status.idle":"2022-02-24T13:22:19.094500Z","shell.execute_reply.started":"2022-02-24T13:22:19.090862Z","shell.execute_reply":"2022-02-24T13:22:19.093802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = Dataset(test_list,second_test_addr,'test',32)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:19.668179Z","iopub.execute_input":"2022-02-24T13:22:19.668739Z","iopub.status.idle":"2022-02-24T13:22:19.672359Z","shell.execute_reply.started":"2022-02-24T13:22:19.668700Z","shell.execute_reply":"2022-02-24T13:22:19.671588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ef=tf.keras.applications.EfficientNetB6(input_shape=(128, 128, 3),weights='imagenet',include_top=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:11.101025Z","iopub.execute_input":"2022-02-24T13:19:11.101458Z","iopub.status.idle":"2022-02-24T13:19:19.045648Z","shell.execute_reply.started":"2022-02-24T13:19:11.101415Z","shell.execute_reply":"2022-02-24T13:19:19.044739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ilr = 0.001","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:19.047324Z","iopub.execute_input":"2022-02-24T13:19:19.047592Z","iopub.status.idle":"2022-02-24T13:19:19.052905Z","shell.execute_reply.started":"2022-02-24T13:19:19.047556Z","shell.execute_reply":"2022-02-24T13:19:19.052121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = class_weight.compute_class_weight('balanced',np.unique(train['individual_id'].values),train['individual_id'].values)\nclass_weights = dict(enumerate(class_weights))\n#class_weights","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:19.054225Z","iopub.execute_input":"2022-02-24T13:19:19.054490Z","iopub.status.idle":"2022-02-24T13:19:29.870309Z","shell.execute_reply.started":"2022-02-24T13:19:19.054456Z","shell.execute_reply":"2022-02-24T13:19:29.869590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\nimport math as m","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:29.872050Z","iopub.execute_input":"2022-02-24T13:19:29.872268Z","iopub.status.idle":"2022-02-24T13:19:29.877159Z","shell.execute_reply.started":"2022-02-24T13:19:29.872236Z","shell.execute_reply":"2022-02-24T13:19:29.876467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ArcFace(keras.layers.Layer):\n    def __init__(self, n_classes=15587, s=30.0, m=0.50, regularizer=None, **kwargs):\n        super(ArcFace, self).__init__(**kwargs)\n        self.n_classes = n_classes\n        self.s = s\n        self.m = m\n        self.regularizer = regularizers.get(regularizer)\n    def build(self, input_shape):\n        super(ArcFace, self).build(input_shape[0])\n        self.W = self.add_weight(name='W',\n                                shape=(input_shape[0][-1], self.n_classes),\n                                initializer='glorot_uniform',\n                                trainable=True,\n                                regularizer=self.regularizer)\n\n\n    def call(self, inputs):\n        x, y = inputs\n        c = K.shape(x)[-1]\n        \n        x = tf.nn.l2_normalize(x, axis=1)\n        \n        W = tf.nn.l2_normalize(self.W, axis=0)\n        \n        logits = x @ W\n        \n        theta = tf.acos(K.clip(logits, -1.0 + K.epsilon(), 1.0 - K.epsilon()))\n        target_logits = tf.cos(theta + self.m)\n\n        logits = logits * (1 - y) + target_logits * y\n        # feature re-scale\n        logits *= self.s\n        out = tf.nn.softmax(logits)\n        return -1*out\n    \n    def compute_output_shape(self, input_shape):\n        return (None, self.n_classes)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:29.878613Z","iopub.execute_input":"2022-02-24T13:19:29.879096Z","iopub.status.idle":"2022-02-24T13:19:29.890823Z","shell.execute_reply.started":"2022-02-24T13:19:29.879060Z","shell.execute_reply":"2022-02-24T13:19:29.890114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_lr_callback(plot=False):\n    lr_start   = 0.000001\n    lr_max     = 0.000005 * 256 \n    lr_min     = 0.000001\n    lr_ramp_ep = 4\n    lr_sus_ep  = 0\n    lr_decay   = 0.9\n   \n    def lrfn(epoch):\n        if epoch < lr_ramp_ep:\n            lr = (lr_max - lr_start) / lr_ramp_ep * epoch + lr_start\n            \n        elif epoch < lr_ramp_ep + lr_sus_ep:\n            lr = lr_max\n            \n        else:\n            lr = (lr_max - lr_min) * lr_decay**(epoch - lr_ramp_ep - lr_sus_ep) + lr_min\n            \n        return lr\n        \n    if plot:\n        epochs = list(range(10))\n        learning_rates = [lrfn(x) for x in epochs]\n        plt.scatter(epochs,learning_rates)\n        plt.show()\n\n    lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=False)\n    return lr_callback\n\nget_lr_callback(plot=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:29.892069Z","iopub.execute_input":"2022-02-24T13:19:29.892359Z","iopub.status.idle":"2022-02-24T13:19:30.081330Z","shell.execute_reply.started":"2022-02-24T13:19:29.892327Z","shell.execute_reply":"2022-02-24T13:19:30.080714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crt_model():\n    inp=L.Input(shape=(128, 128, 3))\n    label =L.Input(shape=(15587,), )\n    \n    l = ef(inp)\n\n    l = L.GlobalMaxPooling2D()(l)\n    l = L.Dense(512, kernel_initializer='normal',activation='relu')(l)\n    out = ArcFace(n_classes=15587)([l, label])\n    model = Model(inputs= [inp, label],outputs=out)\n    model.compile(optimizer=tf.keras.optimizers.Adam(ilr),loss=keras.losses.SparseCategoricalCrossentropy(), metrics=[tf.keras.metrics.SparseCategoricalAccuracy(),tf.keras.metrics.SparseTopKCategoricalAccuracy(k=5)])\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:19:36.906957Z","iopub.execute_input":"2022-02-24T13:19:36.907211Z","iopub.status.idle":"2022-02-24T13:19:36.915762Z","shell.execute_reply.started":"2022-02-24T13:19:36.907182Z","shell.execute_reply":"2022-02-24T13:19:36.914140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = crt_model()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:29.978954Z","iopub.execute_input":"2022-02-24T13:22:29.979277Z","iopub.status.idle":"2022-02-24T13:22:31.609251Z","shell.execute_reply.started":"2022-02-24T13:22:29.979241Z","shell.execute_reply":"2022-02-24T13:22:31.608545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:22:31.611592Z","iopub.execute_input":"2022-02-24T13:22:31.612354Z","iopub.status.idle":"2022-02-24T13:23:31.955911Z","shell.execute_reply.started":"2022-02-24T13:22:31.612316Z","shell.execute_reply":"2022-02-24T13:23:31.954334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, \n                    epochs = 10,\n                    validation_data = valid_dataset,\n                    class_weight = class_weights,\n                   callbacks=[get_lr_callback()])","metadata":{"execution":{"iopub.status.busy":"2022-02-24T13:23:31.959678Z","iopub.execute_input":"2022-02-24T13:23:31.959892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:32:35.751433Z","iopub.execute_input":"2022-02-16T08:32:35.752795Z","iopub.status.idle":"2022-02-16T08:33:05.045058Z","shell.execute_reply.started":"2022-02-16T08:32:35.752658Z","shell.execute_reply":"2022-02-16T08:33:05.044357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embed_model = Model(inputs=model.input[0], outputs=model.layers[-3].output)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:33:05.652623Z","iopub.execute_input":"2022-02-16T08:33:05.652925Z","iopub.status.idle":"2022-02-16T08:33:05.662521Z","shell.execute_reply.started":"2022-02-16T08:33:05.652884Z","shell.execute_reply":"2022-02-16T08:33:05.661788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedded_features = embed_model.predict(eval_dataset, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:33:05.665588Z","iopub.execute_input":"2022-02-16T08:33:05.665983Z","iopub.status.idle":"2022-02-16T08:33:20.595877Z","shell.execute_reply.started":"2022-02-16T08:33:05.665954Z","shell.execute_reply":"2022-02-16T08:33:20.595099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_embed_features = embed_model.predict(train_embed_dataset, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:33:25.850939Z","iopub.execute_input":"2022-02-16T08:33:25.851627Z","iopub.status.idle":"2022-02-16T08:34:36.591538Z","shell.execute_reply.started":"2022-02-16T08:33:25.85159Z","shell.execute_reply":"2022-02-16T08:34:36.590686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_feature = embed_model.predict(test_dataset, verbose=0)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_embed_features.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:34:36.593635Z","iopub.execute_input":"2022-02-16T08:34:36.593876Z","iopub.status.idle":"2022-02-16T08:34:36.600681Z","shell.execute_reply.started":"2022-02-16T08:34:36.593842Z","shell.execute_reply":"2022-02-16T08:34:36.599777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedded_features.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:34:36.601906Z","iopub.execute_input":"2022-02-16T08:34:36.60249Z","iopub.status.idle":"2022-02-16T08:34:36.609864Z","shell.execute_reply.started":"2022-02-16T08:34:36.602453Z","shell.execute_reply":"2022-02-16T08:34:36.609106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = np.concatenate([train_embed_features,embedded_features])","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:36:58.769183Z","iopub.execute_input":"2022-02-16T08:36:58.769505Z","iopub.status.idle":"2022-02-16T08:36:58.859782Z","shell.execute_reply.started":"2022-02-16T08:36:58.769472Z","shell.execute_reply":"2022-02-16T08:36:58.858996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-16T08:37:05.049384Z","iopub.execute_input":"2022-02-16T08:37:05.05003Z","iopub.status.idle":"2022-02-16T08:37:05.055513Z","shell.execute_reply.started":"2022-02-16T08:37:05.049987Z","shell.execute_reply":"2022-02-16T08:37:05.05475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dd1 = {'embeddings' :x_train}","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dd2 = {'test_embeddings' :test_feature}","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"embeddings\", dd1)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"test_embeddings\", dd2)","metadata":{},"execution_count":null,"outputs":[]}]}