{"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":"NFrame=128\nunits=256\nnum_heads = 6\nd_model = 256\nfeed_dimension = 256\ndropout_rate = 0.2\ntarget_frams=128\ncolumn_len= 100\nphrase_length=32\nunique_character = 62\npad_token=59\nWD_RATIO = 0.05\nblocks=1\neos_token = 60\nsos_token = 61\ninpdir = \"/kaggle/input/asl-fingerspelling\"","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:48.772989Z","iopub.execute_input":"2023-07-31T08:42:48.773336Z","iopub.status.idle":"2023-07-31T08:42:48.785914Z","shell.execute_reply.started":"2023-07-31T08:42:48.773304Z","shell.execute_reply":"2023-07-31T08:42:48.784493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport pandas as pd\nimport json\nimport tensorflow as tf\nimport numpy as np\nimport glob\nimport os\nfrom tensorflow import keras\nfrom pathlib import Path\nimport logging","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:48.788083Z","iopub.execute_input":"2023-07-31T08:42:48.789136Z","iopub.status.idle":"2023-07-31T08:42:57.563519Z","shell.execute_reply.started":"2023-07-31T08:42:48.7891Z","shell.execute_reply":"2023-07-31T08:42:57.562198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open (\"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\", \"r\") as f:\n    char_to_num = json.load(f)\n\nnum_to_char = {j:i for i,j in char_to_num.items()}\n\nLPOSE = [15, 17, 19, 21]\nRPOSE = [16, 18, 20, 22]\nPOSE = LPOSE + RPOSE\nRHAND_LBLS = [f'x_right_hand_{i}' for i in range(21)] + [f'y_right_hand_{i}' for i in range(21)] + [f'z_right_hand_{i}' for i in range(21)]\nLHAND_LBLS = [ f'x_left_hand_{i}' for i in range(21)] + [ f'y_left_hand_{i}' for i in range(21)] + [ f'z_left_hand_{i}' for i in range(21)]\n#POSE_LBLS = [f'x_pose_{i}' for i in POSE] + [f'y_pose_{i}' for i in POSE] + [f'z_pose_{i}' for i in POSE]\n\nX = [f'x_right_hand_{i}' for i in range(21)] + [f'x_left_hand_{i}' for i in range(21)] + [f'x_pose_{i}' for i in POSE]\nY = [f'y_right_hand_{i}' for i in range(21)] + [f'y_left_hand_{i}' for i in range(21)] + [f'y_pose_{i}' for i in POSE]\n#Z = [f'z_right_hand_{i}' for i in range(21)] + [f'z_left_hand_{i}' for i in range(21)] + [f'z_pose_{i}' for i in POSE]\n\ncolumn_selected = X + Y\nX_IDX = [i for i, col in enumerate(column_selected)  if \"x_\" in col]\nY_IDX = [i for i, col in enumerate(column_selected)  if \"y_\" in col]\n#Z_IDX = [i for i, col in enumerate(column_selected)  if \"z_\" in col]\nRHAND_IDX = [i for i, col in enumerate(column_selected)  if \"right\" in col]\nLHAND_IDX = [i for i, col in enumerate(column_selected)  if  \"left\" in col]\n#RPOSE_IDX = [i for i, col in enumerate(column_selected)  if  \"pose\" in col and int(col[-2:]) in RPOSE]\n#LPOSE_IDX = [i for i, col in enumerate(column_selected)  if  \"pose\" in col and int(col[-2:]) in LPOSE]\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:57.568583Z","iopub.execute_input":"2023-07-31T08:42:57.569385Z","iopub.status.idle":"2023-07-31T08:42:57.592325Z","shell.execute_reply.started":"2023-07-31T08:42:57.569346Z","shell.execute_reply":"2023-07-31T08:42:57.591216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f'{inpdir}/train.csv')\nfolder = Path(f'{inpdir}/train_landmarks/')\nfilesname= [int(file.name.split ('.',1)[0]) for file in folder.rglob('*')]\ndf =df.loc[df['file_id'].isin(filesname)]","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:57.596564Z","iopub.execute_input":"2023-07-31T08:42:57.597239Z","iopub.status.idle":"2023-07-31T08:42:57.771167Z","shell.execute_reply.started":"2023-07-31T08:42:57.597206Z","shell.execute_reply":"2023-07-31T08:42:57.770214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"column_selected=np.concatenate((X,Y))","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:57.772526Z","iopub.execute_input":"2023-07-31T08:42:57.773122Z","iopub.status.idle":"2023-07-31T08:42:57.779165Z","shell.execute_reply.started":"2023-07-31T08:42:57.773086Z","shell.execute_reply":"2023-07-31T08:42:57.778128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_relevant_data_subset(pq_path):\n    return pd.read_parquet(pq_path, columns=column_selected)\n\nfile_id = df.file_id.iloc[0]\npqfile = f\"{inpdir}/train_landmarks/{file_id}.parquet\"\nseq_refs = df.loc[df.file_id == file_id]\nseqs = load_relevant_data_subset(pqfile)\n\nseq_id = seq_refs.sequence_id.iloc[0]\nframes = seqs.iloc[seqs.index == seq_id]\nphrase = str(df.loc[df.sequence_id == seq_id].phrase.iloc[0])\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:57.780862Z","iopub.execute_input":"2023-07-31T08:42:57.781462Z","iopub.status.idle":"2023-07-31T08:42:58.628469Z","shell.execute_reply.started":"2023-07-31T08:42:57.78143Z","shell.execute_reply":"2023-07-31T08:42:58.627484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CharacterTokenizer:\n    def __init__(self, character_to_prediction_index=None, pad_token='<PAD>', eos_token='<EOS>', sos_token='<SOS>'):\n\n        \n\n        self.character_to_prediction_index = character_to_prediction_index\n        self.prediction_index_to_character = {v: k for k, v in character_to_prediction_index.items()}\n        self.pad_token = pad_token\n        self.eos_token = eos_token\n        self.sos_token = sos_token\n\n        # Initialize vocabulary with special tokens\n        self.vocabulary = {self.pad_token, self.eos_token, self.sos_token}\n        self.update_vocabulary()\n\n    def update_vocabulary(self):\n        # Update vocabulary with the characters from the character_to_prediction_index\n        self.vocabulary.update(self.character_to_prediction_index.keys())\n\n    def encode(self, text, max_length):\n        # Convert text to a list of prediction indices\n        indices = [self.character_to_prediction_index.get(char, 0) for char in text] + [self.character_to_prediction_index[self.eos_token]]\n    \n        # Apply padding\n        if len(indices) < max_length:\n            indices += [self.character_to_prediction_index[self.pad_token]] * (max_length - len(indices))\n    \n        return indices\n\n\n    def decode(self, indices):\n        # Convert list of prediction indices back to text\n        text = [self.prediction_index_to_character[index] for index in indices if index != self.character_to_prediction_index[self.pad_token]]\n        return \"\".join(text)\n\n    def add_data_instance(self, text):\n        # Add new data instance and update vocabulary\n        for char in text:\n            if char not in self.vocabulary:\n                self.character_to_prediction_index[char] = len(self.vocabulary)\n                self.prediction_index_to_character[len(self.vocabulary)] = char\n                self.vocabulary.add(char)\n        self.update_vocabulary()\n\n\n# Example usage:\nchar2 = {}\nfor key, value in char_to_num.items():  # Step 2\n    char2[key] = value\nchar2[\"<PAD>\"] = 59\nchar2[\"<EOS>\"] = 60\nchar2[\"<SOS>\"] = 61\nprint (char2)\ncharacter_to_prediction_index = char2","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:58.630734Z","iopub.execute_input":"2023-07-31T08:42:58.631449Z","iopub.status.idle":"2023-07-31T08:42:58.646934Z","shell.execute_reply.started":"2023-07-31T08:42:58.631413Z","shell.execute_reply":"2023-07-31T08:42:58.645855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# based on: https://stackoverflow.com/questions/67342988/verifying-the-implementation-of-multihead-attention-in-transformer\n# replaced softmax with softmax layer to support masked softmax\ndef scaled_dot_product(q,k,v, softmax, attention_mask):\n    #calculates Q . K(transpose)\n    qkt = tf.matmul(q,k,transpose_b=True)\n    #caculates scaling factor\n    dk = tf.math.sqrt(tf.cast(q.shape[-1],dtype=tf.float32))\n    scaled_qkt = qkt/dk\n    softmax = softmax(scaled_qkt, mask=attention_mask)\n    z = tf.matmul(softmax,v)\n    #shape: (m,Tx,depth), same shape as q,k,v\n    return z\n\nclass MultiHeadA(tf.keras.layers.Layer):\n    def __init__(self,d_model, num_of_heads, dropout,name, d_out=None):\n        super(MultiHeadA,self).__init__(name=name)\n        self.d_model = d_model\n        self.num_of_heads = num_of_heads\n        self.depth = d_model//num_of_heads\n        self.wq = [tf.keras.layers.Dense(self.depth//2, use_bias=False) for i in range(num_of_heads)]\n        self.wk = [tf.keras.layers.Dense(self.depth//2, use_bias=False) for i in range(num_of_heads)]\n        self.wv = [tf.keras.layers.Dense(self.depth//2, use_bias=False) for i in range(num_of_heads)]\n        self.wo = tf.keras.layers.Dense(d_model if d_out is None else d_out, use_bias=False)\n        self.softmax = tf.keras.layers.Softmax()\n        self.do = tf.keras.layers.Dropout(dropout)\n        self.supports_masking = True\n        \n    def call(self, q, k, v, attention_mask=None, training=False):\n        \n        multi_attn = []\n        for i in range(self.num_of_heads):\n            Q = self.wq[i](q)\n            K = self.wk[i](k)\n            V = self.wv[i](v)\n            multi_attn.append(scaled_dot_product(Q,K,V, self.softmax, attention_mask))\n            \n        multi_head = tf.concat(multi_attn, axis=-1)\n        multi_head_attention = self.wo(multi_head)\n        multi_head_attention = self.do(multi_head_attention, training=training)\n        \n        return multi_head_attention\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:58.649337Z","iopub.execute_input":"2023-07-31T08:42:58.649634Z","iopub.status.idle":"2023-07-31T08:42:58.665035Z","shell.execute_reply.started":"2023-07-31T08:42:58.649609Z","shell.execute_reply":"2023-07-31T08:42:58.66408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LandmarkEmbedding(tf.keras.Model):\n    def __init__(self, output_units, name=None):\n        super(LandmarkEmbedding, self).__init__(name=name)\n        self.output_units = output_units\n        self.names=name\n        self.supports_masking=True\n\n    def build(self, input_shape):\n        \n        self.embeddings = self.add_weight(name=f\"embed_{self.names}\",\n            shape=(self.output_units),\n            initializer=\"zeros\",\n          \n        )\n        self.seq=tf.keras.Sequential ([\n          tf.keras.layers.Dense(self.output_units, activation ='gelu',name=f\"dense1_{self.names}\"),\n           tf.keras.layers.Dense(self.output_units, activation ='gelu',name=f\"dense2_{self.names}\")\n        ])\n        #self.dense = tf.keras.layers.Dense(self.output_units, activation ='gelu')\n        #self.dense2 = tf.keras.layers.Dense(self.output_units, activation ='gelu')\n\n\n    def call(self, inputs,training=False):\n        \n        return tf.where (\n             tf.reduce_sum(inputs, axis=2, keepdims=True) == 0,\n             self.embeddings,\n             self.seq (inputs)\n             \n                )\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:58.669641Z","iopub.execute_input":"2023-07-31T08:42:58.669941Z","iopub.status.idle":"2023-07-31T08:42:58.680293Z","shell.execute_reply.started":"2023-07-31T08:42:58.669917Z","shell.execute_reply":"2023-07-31T08:42:58.679326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class EmbeddingHand (tf.keras.Model) :\n    def __init__(self,frames,units, name=None):\n        super(EmbeddingHand, self).__init__(name=name)\n        self.names=name\n        self.frames=frames\n        self.units=units\n        self.supports_masking=True\n        \n\n    def build(self, input_shape):\n        self.norm=tf.keras.layers.LayerNormalization (epsilon=1e-6)\n        self.positional_embedding = tf.Variable(\n            initial_value=tf.zeros([self.frames, self.units], dtype=tf.float32),\n            trainable=True,\n            name='embedding_positional_encoder',\n        )\n        \n        self.dominant_hand_embedding = LandmarkEmbedding(self.units, name=\"hand_embed\")\n    def call(self, inputs,training=False):\n        x=inputs\n        x=self.dominant_hand_embedding (x)\n        x=x+self.positional_embedding\n        x=self.norm (x)\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:58.68309Z","iopub.execute_input":"2023-07-31T08:42:58.685288Z","iopub.status.idle":"2023-07-31T08:42:58.695068Z","shell.execute_reply.started":"2023-07-31T08:42:58.685253Z","shell.execute_reply":"2023-07-31T08:42:58.69413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nclass TransformerEncoder(tf.keras.Model):\n    def __init__(self, num_heads, d_model, feed_dimension, dropout_rate=0.1,blocks=4, name=None):\n        super(TransformerEncoder, self).__init__(name=name)\n        self.num_heads = num_heads\n        self.d_model = d_model\n        self.feed_dimension = feed_dimension\n        self.dropout_rate = dropout_rate\n        self.blocks = blocks\n        self.supports_masking=True\n        self.names=name\n        \n    def build(self, input_shape):\n        self.multi_head_attention = tf.keras.layers.MultiHeadAttention(key_dim=d_model, num_heads=num_heads,name=f'multihead_{self.names}')\n        self.dense1 = tf.keras.layers.Dense(units=feed_dimension, activation='relu',name=f'dense1_{self.names}')\n        self.dense2 = tf.keras.layers.Dense(units=d_model,activation='relu',name=f'dense1_{self.names}')\n        #self.conv1d = tf.keras.layers.Conv1D(256, kernel_size=50)\n        #self.conv1d1 = tf.keras.layers.Conv1D(256, kernel_size=48)\n        self.dropout1 = tf.keras.layers.Dropout(dropout_rate)\n        self.dropout2 = tf.keras.layers.Dropout(dropout_rate)\n        self.layer_norm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)\n        self.layer_norm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)\n\n    def call(self, inputs,training=False):\n        \n        \n        attention_mask = tf.where(tf.math.reduce_sum(inputs, axis=[2]) == 0.0, 0.0, 1.0)\n        attention_mask = tf.expand_dims(attention_mask, axis=1)\n        attention_mask = tf.repeat(attention_mask, repeats=128, axis=1)\n        \n        \n        attention_output = self.multi_head_attention(inputs,inputs,attention_mask=attention_mask\n                                                     )\n        attention_output = self.dropout1(attention_output)\n        output1 = self.layer_norm1(attention_output+ inputs) \n\n        ffn_output = self.dense2(self.dropout2(self.dense1(output1)))\n        ffn_output = self.dropout2(ffn_output)\n        output2 = self.layer_norm2(ffn_output  + output1)\n       \n        for i in range (self.blocks) :\n            attention_output = self.multi_head_attention(output2,output2, attention_mask=attention_mask\n                                                         )\n            attention_output = self.dropout1(attention_output)\n            output1 = self.layer_norm1(attention_output + inputs)\n\n            ffn_output = self.dense2(self.dropout2(self.dense1(output1)))\n            ffn_output = self.dropout2(ffn_output)\n            output2 = self.layer_norm2(ffn_output + output1) \n\n\n        return output2\n\ninput_sequence = tf.keras.layers.Input(shape=(128,256))   \nencoder_output = TransformerEncoder(num_heads, d_model, feed_dimension, dropout_rate, blocks=4)(input_sequence)\nmodel = tf.keras.Model(inputs=[input_sequence], outputs=encoder_output)\ntf.keras.utils.plot_model(model, \"cross-attention.png\",\n                          show_shapes=True, show_dtype=True, show_layer_names=True,\n                          rankdir='BT', show_layer_activations=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:42:58.698744Z","iopub.execute_input":"2023-07-31T08:42:58.699329Z","iopub.status.idle":"2023-07-31T08:43:02.505894Z","shell.execute_reply.started":"2023-07-31T08:42:58.699295Z","shell.execute_reply":"2023-07-31T08:43:02.504788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass PhraseInput (tf.keras.Model) :\n    def __init__(self,phrase_length,target_frames,d_model, name=None):\n        \n        super(PhraseInput, self).__init__(name=name)\n        self.phrase_length=phrase_length\n        self.target_frames=target_frames\n        self.d_model=d_model\n        self.supports_masking=True\n        self.names=name\n        self.supports_masking=True\n    \n    def build(self, input_shape):\n        self.positional_embedding = tf.Variable(\n            initial_value=tf.zeros([self.target_frames,self.d_model], dtype=tf.float32),\n            trainable=True,\n            name='embedding_positional_encoder',\n        )\n        self.char_emb = tf.keras.layers.Embedding(62,d_model, embeddings_initializer='zeros', name=f\"embed_{self.names}\")    \n    \n    def call(self, inputs,training=False):\n        phrase=tf.cast (inputs, dtype=\"int32\")\n        phrase = tf.pad(phrase, [[0,0], [1,0]], constant_values=sos_token, name='prepend_sos_token')\n        phrase=tf.pad (phrase, [(0, 0), (0,self.target_frames - self.phrase_length-1)], constant_values=59)\n        posit=self.positional_embedding\n        phrase =self.char_emb (phrase) + posit\n        return phrase\n    \n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:02.508781Z","iopub.execute_input":"2023-07-31T08:43:02.509392Z","iopub.status.idle":"2023-07-31T08:43:02.520094Z","shell.execute_reply.started":"2023-07-31T08:43:02.509357Z","shell.execute_reply":"2023-07-31T08:43:02.518945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TransformerDecoder(tf.keras.Model):\n    def __init__(self, num_heads, d_model, feed_dimension,target_frames,phrase_length,blocks, name=None, dropout_rate=0.1):\n        super(TransformerDecoder, self).__init__(name=name)\n        self.num_heads = num_heads\n        self.d_model = d_model\n        self.feed_dimension = feed_dimension\n        self.dropout_rate = dropout_rate\n        self.target_frames = target_frames\n        self.phrase_length = phrase_length\n        self.blocks=blocks\n        self.names=name\n       \n        self.supports_masking=True\n       \n        \n    def build(self, input_shape):\n        self.multi_head_attention = tf.keras.layers.MultiHeadAttention(key_dim=d_model, num_heads=num_heads, name=f\"multihead_{self.names}\")\n        self.dense1 = tf.keras.layers.Dense(units=feed_dimension, activation='relu', name=f\"dense1_{self.names}\")\n        self.dense2 = tf.keras.layers.Dense(units=d_model,activation='relu',name=f\"dense2_{self.names}\")\n        self.dropout1 = tf.keras.layers.Dropout(dropout_rate)\n        self.dropout2 = tf.keras.layers.Dropout(dropout_rate)\n        self.layer_norm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)\n        self.layer_norm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)\n        #self.char_emb = tf.keras.layers.Embedding(62,d_model, embeddings_initializer='zeros')\n        \n\n        \n    def get_causal_attention_mask(self, B):\n        i = tf.range(self.target_frames)[:, tf.newaxis]\n        j = tf.range(self.target_frames)\n        mask = tf.cast(i >= j, dtype=tf.int32)\n        mask = tf.reshape(mask, (1, self.target_frames, self.target_frames))\n        mult = tf.concat(\n            [tf.expand_dims(B, -1), tf.constant([1, 1], dtype=tf.int32)],\n            axis=0,\n        )\n        mask = tf.tile(mask, mult)\n        mask = tf.cast(mask, tf.float32)\n        return mask\n\n    def call(self, frames,phrase,training=False):\n        \n        B = tf.shape(frames)[0]\n        causal_mask = self.get_causal_attention_mask(B)\n        phrase_input = self.multi_head_attention(phrase,phrase,attention_mask=causal_mask)\n        phrase_input = self.dropout1(phrase_input)\n        output1 = self.layer_norm1(phrase_input + phrase)\n            \n        attention_output = self.multi_head_attention(query=output1,key=frames,value=frames)\n        attention_output = self.dropout1(attention_output)\n        output2 = self.layer_norm1(attention_output + output1 ) \n\n        ffn_output = self.dense2(self.dropout2(self.dense1(output1)))\n        ffn_output = self.dropout2(ffn_output)\n        output3 = self.layer_norm2(ffn_output  + output2)\n       \n        \n        for i in range (self.blocks) :\n            \n            \n            phrase_input = self.multi_head_attention(output3,output3,output3,attention_mask=causal_mask)\n            phrase_input = self.dropout1(attention_output)\n            output1 = self.layer_norm1(attention_output  + output3)\n            \n            attention_output = self.multi_head_attention(query=output2,key=frames,value=frames)\n            attention_output = self.dropout1(attention_output)\n            output2 = self.layer_norm1(attention_output  + output1)\n\n            ffn_output = self.dense2(self.dropout2(self.dense1(output1)))\n            ffn_output = self.dropout2(ffn_output)\n            output3= self.layer_norm2(ffn_output + output2)\n        \n        #output3 = tf.slice(output2, [0, 0, 0], [-1, self.phrase_length, -1])\n     \n        return output3\n\ninput_sequence = tf.keras.layers.Input(shape=(128,256))\ninput_sequence2 = tf.keras.layers.Input(shape=(128,256)) \nencoder_output = TransformerDecoder(num_heads=num_heads, d_model=d_model, feed_dimension=feed_dimension, dropout_rate=dropout_rate, \n                                    target_frames=target_frams, \n                                    phrase_length=phrase_length,\n                                    blocks=blocks)(input_sequence, input_sequence2)\nmodel = tf.keras.Model(inputs=[input_sequence, input_sequence2], outputs=encoder_output)\ntf.keras.utils.plot_model(model, \"cross-attention.png\",\n                          show_shapes=True, show_dtype=True, show_layer_names=True,\n                          rankdir='BT', show_layer_activations=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:02.523821Z","iopub.execute_input":"2023-07-31T08:43:02.52415Z","iopub.status.idle":"2023-07-31T08:43:03.67053Z","shell.execute_reply.started":"2023-07-31T08:43:02.524125Z","shell.execute_reply":"2023-07-31T08:43:03.669636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Classifier (tf.keras.Model) :\n    \n    def __init__(self,name=None):\n        super(Classifier, self).__init__(name=name)\n        #self.dense=tf.keras.layers.Dense (256, activation='linear', use_bias=False)\n        \n        self.names=name\n        #self.conv1d=tf.keras.layers.Conv1D (filters=62, kernel_size=65+32)\n    def build(self, input_shape):\n        self.dense2=tf.keras.layers.Dense (62, activation='linear', use_bias=False, name=f\"{self.names}\")\n        return super().build(input_shape) \n    def call(self, inputs,training=False):\n        inputs = tf.slice(inputs, [0, 0, 0], [-1, phrase_length, -1])\n        #dense=self.dense (inputs)\n        dense=self.dense2 (inputs)\n        #dense=self.conv1d(dense)\n     \n        return dense\n\ninput_data = tf.keras.layers.Input(shape=(128, 256))\nclassifier = Classifier(name='output')\noutputs=classifier(input_data)\nmodel = tf.keras.Model(inputs=input_data, outputs=outputs)\ntf.keras.utils.plot_model(model, to_file='classifier_block.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:03.672427Z","iopub.execute_input":"2023-07-31T08:43:03.673116Z","iopub.status.idle":"2023-07-31T08:43:03.816558Z","shell.execute_reply.started":"2023-07-31T08:43:03.67308Z","shell.execute_reply":"2023-07-31T08:43:03.815643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def scce_with_ls(y_true, y_pred):\n    # Filter Pad Tokens\n    idxs = tf.where(y_true != pad_token)\n    #y_pred = tf.argmax (y_pred,axis=-1, output_type=tf.dtypes.int64)\n    y_true = tf.gather_nd(y_true, idxs)\n    y_pred = tf.gather_nd(y_pred, idxs)\n    # One Hot Encode Sparsely Encoded Target Sign\n    y_true = tf.cast(y_true, tf.int32)\n    y_true = tf.one_hot(y_true, unique_character, axis=1)\n    # Categorical Crossentropy with native label smoothing support\n\n    loss = tf.keras.losses.categorical_crossentropy(y_true, y_pred, label_smoothing=0.25,from_logits=True)\n    \n    loss = tf.math.reduce_mean(loss)\n    return loss\n\n# Create Initial Loss Weights All Set To 1\nloss_weights = np.ones(unique_character, dtype=np.float32)\n# Set Loss Weight Of Pad Token To 0\nloss_weights[pad_token] = 0\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:03.817893Z","iopub.execute_input":"2023-07-31T08:43:03.818815Z","iopub.status.idle":"2023-07-31T08:43:03.826513Z","shell.execute_reply.started":"2023-07-31T08:43:03.81878Z","shell.execute_reply":"2023-07-31T08:43:03.825373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import backend as K\n\nclass TopKAccuracy(tf.keras.metrics.Metric):\n    def __init__(self, k, **kwargs):\n        super(TopKAccuracy, self).__init__(name=f'top{k}acc', **kwargs)\n        self.top_k_acc = tf.keras.metrics.SparseTopKCategoricalAccuracy(k=k)\n\n    def update_state(self, y_true, y_pred, sample_weight=None):\n        y_true = tf.reshape(y_true, [-1])\n        y_pred = tf.reshape(y_pred, [-1, unique_character])\n        character_idxs = tf.where(y_true < (unique_character-3))\n        y_true = tf.gather(y_true, character_idxs, axis=0)\n        y_pred = tf.gather(y_pred, character_idxs, axis=0)\n        self.top_k_acc.update_state(y_true, y_pred)\n\n    def result(self):\n        return self.top_k_acc.result()\n    \n    def reset_state(self):\n        self.top_k_acc.reset_state()\n        \n\nclass WeightDecayCallback(tf.keras.callbacks.Callback):\n    def __init__(self, wd_ratio=WD_RATIO):\n        self.step_counter = 0\n        self.wd_ratio = wd_ratio\n    \n    def on_epoch_begin(self, epoch, logs=None):\n        model.optimizer.weight_decay = model.optimizer.learning_rate * self.wd_ratio\n        print(f'learning rate: {model.optimizer.learning_rate.numpy():.2e}, weight decay: {model.optimizer.weight_decay.numpy():.2e}')","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:03.827945Z","iopub.execute_input":"2023-07-31T08:43:03.828422Z","iopub.status.idle":"2023-07-31T08:43:03.841522Z","shell.execute_reply.started":"2023-07-31T08:43:03.828361Z","shell.execute_reply":"2023-07-31T08:43:03.83976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model () :\n    frames_input = tf.keras.layers.Input ((target_frams, column_len), dtype=tf.float32, name='input_1')\n    \n    x = tf.keras.layers.Masking(mask_value=0.0, input_shape=(target_frams, column_len), name='mask_frame')(frames_input)\n    x = EmbeddingHand(frames=target_frams, units=units, name='embed_hand')(x)\n    x = TransformerEncoder(num_heads=num_heads, d_model=units, feed_dimension=units, dropout_rate=dropout_rate, blocks=blocks, name='encoder')(x)\n    \n    \n    phrase_input = tf.keras.layers.Input ((phrase_length,), dtype=tf.int8, name='input_2')\n    phrase_input_enc =PhraseInput(target_frames=target_frams,d_model=d_model,phrase_length=phrase_length, name='phrase_enc') (phrase_input)\n    \n\n    \n   \n    x = TransformerDecoder(num_heads=num_heads, d_model=d_model, feed_dimension=feed_dimension, dropout_rate=dropout_rate, \n                                    target_frames=128, \n                                    phrase_length=phrase_length,\n                                    blocks=blocks, name='decoder')(frames=x, phrase=phrase_input_enc)\n\n\n    x =Classifier(name='output')(x)\n\n \n    \n    model = tf.keras.models.Model(\n    [frames_input, phrase_input], x, name=\"transformer\"\n    )\n    loss=loss = scce_with_ls\n    metrics = [\n        TopKAccuracy(1),\n        TopKAccuracy(5),\n        'accuracy'\n    ]\n    model.compile(\n        loss=loss,\n        #loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n        optimizer='adam',\n        loss_weights=loss_weights,\n        metrics = metrics,\n        jit_compile=True\n    )\n    return model\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:03.843024Z","iopub.execute_input":"2023-07-31T08:43:03.843453Z","iopub.status.idle":"2023-07-31T08:43:03.855936Z","shell.execute_reply.started":"2023-07-31T08:43:03.843419Z","shell.execute_reply":"2023-07-31T08:43:03.854923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:03.857468Z","iopub.execute_input":"2023-07-31T08:43:03.857868Z","iopub.status.idle":"2023-07-31T08:43:05.480765Z","shell.execute_reply.started":"2023-07-31T08:43:03.857837Z","shell.execute_reply":"2023-07-31T08:43:05.479777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:05.48238Z","iopub.execute_input":"2023-07-31T08:43:05.482778Z","iopub.status.idle":"2023-07-31T08:43:05.517055Z","shell.execute_reply.started":"2023-07-31T08:43:05.482734Z","shell.execute_reply":"2023-07-31T08:43:05.516298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, to_file='classifier_block.png', show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.13279Z","iopub.status.idle":"2023-07-31T08:43:06.133336Z","shell.execute_reply.started":"2023-07-31T08:43:06.133087Z","shell.execute_reply":"2023-07-31T08:43:06.13311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N_EPOCHS = 40\nbatch_size=64\nN_WARMUP_EPOCHS = 5\nWARMUP_METHOD = 'exp'\nLR_MAX = 1e-3\nimport math\nimport matplotlib.pyplot as plt\ndef lrfn(current_step, num_warmup_steps, lr_max, num_cycles=0.50, num_training_steps=N_EPOCHS):\n    \n    if current_step < num_warmup_steps:\n        if WARMUP_METHOD == 'log':\n            return lr_max * 0.10 ** (num_warmup_steps - current_step)\n        else:\n            return lr_max * 2 ** -(num_warmup_steps - current_step)\n    else:\n        progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps))\n\n        return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) * lr_max","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.135544Z","iopub.status.idle":"2023-07-31T08:43:06.136014Z","shell.execute_reply.started":"2023-07-31T08:43:06.135779Z","shell.execute_reply":"2023-07-31T08:43:06.135801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_lr_schedule(lr_schedule, epochs):\n    fig = plt.figure(figsize=(20, 10))\n    plt.plot([None] + lr_schedule + [None])\n    # X Labels\n    x = np.arange(1, epochs + 1)\n    x_axis_labels = [i if epochs <= 40 or i % 5 == 0 or i == 1 else None for i in range(1, epochs + 1)]\n    plt.xlim([1, epochs])\n    plt.xticks(x, x_axis_labels) # set tick step to 1 and let x axis start at 1\n    \n    # Increase y-limit for better readability\n    plt.ylim([0, max(lr_schedule) * 1.1])\n    \n    # Title\n    schedule_info = f'start: {lr_schedule[0]:.1E}, max: {max(lr_schedule):.1E}, final: {lr_schedule[-1]:.1E}'\n    plt.title(f'Step Learning Rate Schedule, {schedule_info}', size=18, pad=12)\n    \n    # Plot Learning Rates\n    for x, val in enumerate(lr_schedule):\n        if epochs <= 40 or x % 5 == 0 or x is epochs - 1:\n            if x < len(lr_schedule) - 1:\n                if lr_schedule[x - 1] < val:\n                    ha = 'right'\n                else:\n                    ha = 'left'\n            elif x == 0:\n                ha = 'right'\n            else:\n                ha = 'left'\n            plt.plot(x + 1, val, 'o', color='black');\n            offset_y = (max(lr_schedule) - min(lr_schedule)) * 0.02\n            plt.annotate(f'{val:.1E}', xy=(x + 1, val + offset_y), size=12, ha=ha)\n    \n    plt.xlabel('Epoch', size=16, labelpad=5)\n    plt.ylabel('Learning Rate', size=16, labelpad=5)\n    plt.grid()\n    plt.show()\n\n# Learning rate for encoder\nLR_SCHEDULE = [lrfn(step, num_warmup_steps=N_WARMUP_EPOCHS, lr_max=LR_MAX, num_cycles=0.50) for step in range(N_EPOCHS)]\n# Plot Learning Rate Schedule\nplot_lr_schedule(LR_SCHEDULE, epochs=N_EPOCHS)\n# Learning Rate Callback\nlr_callback = tf.keras.callbacks.LearningRateScheduler(lambda step: LR_SCHEDULE[step], verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.137871Z","iopub.status.idle":"2023-07-31T08:43:06.138338Z","shell.execute_reply.started":"2023-07-31T08:43:06.138095Z","shell.execute_reply":"2023-07-31T08:43:06.138116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfrecords_path = glob.glob('/kaggle/input/data-aslfr/file/*.tfrecord')\ntrain, test = np.split(tfrecords_path, [int(len(tfrecords_path)*0.9993)])\n    \ndataset_train = tf.data.TFRecordDataset(train)\ndataset_test = tf.data.TFRecordDataset(test)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.139979Z","iopub.status.idle":"2023-07-31T08:43:06.14044Z","shell.execute_reply.started":"2023-07-31T08:43:06.140198Z","shell.execute_reply":"2023-07-31T08:43:06.140219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_input (dataset) :\n    def _parse_function(example_proto):\n  # Parse the input `tf.train.Example` proto using the dictionary above.\n        return tf.io.parse_single_example(example_proto, feature_description)\n\n    feature_description = {\n      'input_1': tf.io.FixedLenSequenceFeature((128,100), tf.float32, default_value=0.0,allow_missing=True),\n      'input_2': tf.io.FixedLenSequenceFeature([], tf.int64, default_value=0,allow_missing=True),\n      #'output': tf.io.FixedLenSequenceFeature([], tf.int64, default_value=0,allow_missing=True),\n      }\n\n    def _squeeze (data) :\n        data['input_1']=tf.squeeze(data['input_1'],axis=0)\n    \n        return data\n    parsed_dataset = dataset.map(_parse_function).map(_squeeze)\n    return parsed_dataset\n\ndata_input= load_input (dataset_train)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.142459Z","iopub.status.idle":"2023-07-31T08:43:06.143529Z","shell.execute_reply.started":"2023-07-31T08:43:06.143286Z","shell.execute_reply":"2023-07-31T08:43:06.14331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_output (dataset) :\n    feature_description = {\n      #'input_1': tf.io.FixedLenSequenceFeature((128,100), tf.float32, default_value=0.0,allow_missing=True),\n      #'input_2': tf.io.FixedLenSequenceFeature([], tf.int64, default_value=0,allow_missing=True),\n      'output': tf.io.FixedLenSequenceFeature([], tf.int64, default_value=0,allow_missing=True),\n  }\n    def _parse_function(example_proto):\n    # Parse the input `tf.train.Example` proto using the dictionary above.\n        return tf.io.parse_single_example(example_proto, feature_description)\n    parsed_dataset = dataset.map(_parse_function)\n    parsed_dataset\n    return parsed_dataset\n\ndata_output= load_output (dataset_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.145039Z","iopub.status.idle":"2023-07-31T08:43:06.145496Z","shell.execute_reply.started":"2023-07-31T08:43:06.145258Z","shell.execute_reply":"2023-07-31T08:43:06.145288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.zip((data_input, data_output)).batch(batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.14701Z","iopub.status.idle":"2023-07-31T08:43:06.147959Z","shell.execute_reply.started":"2023-07-31T08:43:06.147722Z","shell.execute_reply":"2023-07-31T08:43:06.147746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\nhistory = model.fit(dataset,\n                epochs=N_EPOCHS, \n                callbacks=[WeightDecayCallback(),lr_callback]\n    )","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.149556Z","iopub.status.idle":"2023-07-31T08:43:06.150018Z","shell.execute_reply.started":"2023-07-31T08:43:06.149786Z","shell.execute_reply":"2023-07-31T08:43:06.149807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.save_weights('model13.h5')","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.15192Z","iopub.status.idle":"2023-07-31T08:43:06.152379Z","shell.execute_reply.started":"2023-07-31T08:43:06.152138Z","shell.execute_reply":"2023-07-31T08:43:06.152159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for l in model.layers:\n    print(l.name)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.153935Z","iopub.status.idle":"2023-07-31T08:43:06.154392Z","shell.execute_reply.started":"2023-07-31T08:43:06.154152Z","shell.execute_reply":"2023-07-31T08:43:06.154173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class PreprocessLayer(tf.keras.layers.Layer):\n    def __init__(self):\n        super(PreprocessLayer, self).__init__()\n        \n    def build(self, input_shape):\n        return super().build(input_shape)\n    \n    @tf.function(\n        input_signature=(tf.TensorSpec(shape=[None,column_len], dtype=tf.float32),),\n    )\n    def call(self, data0, resize=True):\n        # Fill NaN Values With 0\n        data = tf.where(tf.math.is_nan(data0), 0.0, data0)\n        \n        # Hacky\n        data = data[None]\n        \n\n        hands = tf.slice(data, [0,0,0], [-1, -1, 84])\n        hands = tf.abs(hands)\n        mask = tf.reduce_sum(hands, axis=2)\n        mask = tf.not_equal(mask, 0)\n        data = data[mask][None]\n        \n        # Pad Zeros\n        N_FRAMES = len(data[0])\n        if N_FRAMES < target_frams:\n            data = tf.concat((\n                data,\n                tf.zeros([1,target_frams-N_FRAMES,column_len], dtype=tf.float32)\n            ), axis=1)\n        # Downsample\n        data = tf.image.resize(\n            data,\n            [1, target_frams],\n            method=tf.image.ResizeMethod.BILINEAR,\n        )\n        \n        # Squeeze Batch Dimension\n        data = tf.squeeze(data, axis=[0])\n        \n        return data\n    \npreprocess_layer = PreprocessLayer()","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.156108Z","iopub.status.idle":"2023-07-31T08:43:06.156575Z","shell.execute_reply.started":"2023-07-31T08:43:06.156344Z","shell.execute_reply":"2023-07-31T08:43:06.156365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass TfLiteModel (tf.Module) :\n    def __init__(self, model):\n        super(TfLiteModel, self).__init__()\n        self.model = model\n        self.preprocess_layer = preprocess_layer\n        #self.tf_constant = tf.constant ([59,60,61],dtype=tf.int32)\n        \n    @tf.function(jit_compile=True)\n    def encode (self, x) :\n        x= self.model.get_layer ('mask_frame') (x)\n        x= self.model.get_layer ('embed_hand') (x)\n        x= self.model.get_layer ('encoder') (x)\n        return x\n    \n    @tf.function(jit_compile=True)\n    def phrase_process (self, x) :\n        x= self.model.get_layer ('phrase_enc') (x)\n        return x\n    \n    @tf.function(jit_compile=True)\n    def decode (self, x,  phrase_input) :\n        x= self.model.get_layer ('decoder') (x, phrase_input)\n        x= self.model.get_layer ('output') (x)\n        return x\n    \n    @tf.function(input_signature=[tf.TensorSpec(shape=[None, column_len], dtype=tf.float32, name='inputs')])\n    def __call__(self, inputs, training=False):\n        input_frame = tf.shape (inputs) [0]\n        inputs = tf.cast (inputs, dtype = tf.float32)\n        frames = self.preprocess_layer (inputs)\n        frames = tf.expand_dims (frames, axis =0)\n        encode = self.encode (frames)\n        phrase = tf.fill ([1, phrase_length,], pad_token)\n        stop = False\n        \n        for idx in tf.range (phrase_length) :\n            phrase = tf.cast (phrase, tf.int8)\n           \n            enc_ph = self.phrase_process (phrase)\n            output = tf.cond (\n                stop,\n                lambda :tf.one_hot (tf.cast (phrase, tf.int32), unique_character),\n                lambda : self.decode (encode, enc_ph)\n                \n            )\n            phrase = tf.cast(phrase, tf.int32)\n            phrase = tf.where(\n                tf.range(phrase_length) < idx + 1,\n                tf.argmax(output, axis=2, output_type=tf.int32),\n                phrase,\n            )\n            predicted_token = phrase[0,idx]\n            if not stop:\n                stop = predicted_token == eos_token\n        outputs = tf.squeeze(phrase, axis=0)\n        outputs = tf.one_hot(outputs, 62)\n        #outputs = tf.argmax (outputs, axis=1)\n       # outputs = tf.cast(outputs, tf.int32)\n        #mask = tf.reduce_all(tf.not_equal(tf.expand_dims(outputs, axis=1), self.tf_constant), axis=1)\n        #outputs = tf.boolean_mask(outputs, mask)\n       # outputs = tf.one_hot(outputs, 59)\n        return {'outputs': outputs }\n    \n\n\ntflite_keras_model = TfLiteModel(model)        \n     \n    \n        ","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.158473Z","iopub.status.idle":"2023-07-31T08:43:06.158945Z","shell.execute_reply.started":"2023-07-31T08:43:06.158691Z","shell.execute_reply":"2023-07-31T08:43:06.158726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ord2char = {j:i for i,j in char2.items()}\ndef outputs2phrase(outputs):\n    if outputs.ndim == 2:\n        outputs = np.argmax(outputs, axis=1)\n    #print (outputs)\n    mask = np.isin(outputs, [eos_token,pad_token,sos_token])\n    outputs = outputs[~mask]\n    output=''.join([ord2char.get(s, '') for s in outputs])\n    \n    return output\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.160851Z","iopub.status.idle":"2023-07-31T08:43:06.161304Z","shell.execute_reply.started":"2023-07-31T08:43:06.161065Z","shell.execute_reply":"2023-07-31T08:43:06.161086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test= load_input (dataset_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.163126Z","iopub.status.idle":"2023-07-31T08:43:06.163582Z","shell.execute_reply.started":"2023-07-31T08:43:06.163351Z","shell.execute_reply":"2023-07-31T08:43:06.163372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from leven import levenshtein\ndef lavenstein_result (dataset_test) :\n    iterator = iter(dataset_test)\n    true_result = []\n    pred_result = []\n    distance_laven=[]\n    for item in iterator :\n        true= item [\"input_2\"].numpy()\n        pred = tflite_keras_model (item [\"input_1\"].numpy())['outputs'].numpy()\n        true=outputs2phrase(true)\n        pred =outputs2phrase(pred)\n        true_result.append(true)\n        pred_result.append(pred)\n        distance_laven.append (levenshtein(true,pred))\n    df=pd.DataFrame (\n        zip(true_result,pred_result,distance_laven),\n             columns= ['True', 'Pred', \"laven_distance\"])\n    \n    return df\n\ndf_result = lavenstein_result (data_test)\ndf_result.to_csv ('laven_result.csv')\n        ","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.165399Z","iopub.status.idle":"2023-07-31T08:43:06.16586Z","shell.execute_reply.started":"2023-07-31T08:43:06.165614Z","shell.execute_reply":"2023-07-31T08:43:06.165635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_result","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.167693Z","iopub.status.idle":"2023-07-31T08:43:06.168151Z","shell.execute_reply.started":"2023-07-31T08:43:06.167923Z","shell.execute_reply":"2023-07-31T08:43:06.167945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.backend.clear_session()\n# Write Model\n#@tf.function()\n\n    # Create Model Converter\nkeras_model_converter = tf.lite.TFLiteConverter.from_keras_model(tflite_keras_model)\nkeras_model_converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS]\n# Convert Model\ntflite_model = keras_model_converter.convert()  \nwith open('/kaggle/working/model.tflite', 'wb') as f:\n    f.write(tflite_model)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.170005Z","iopub.status.idle":"2023-07-31T08:43:06.17046Z","shell.execute_reply.started":"2023-07-31T08:43:06.170223Z","shell.execute_reply":"2023-07-31T08:43:06.170244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('inference_args.json', 'w') as f:\n     json.dump({ 'selected_columns': column_selected.tolist() }, f)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.172181Z","iopub.status.idle":"2023-07-31T08:43:06.172638Z","shell.execute_reply.started":"2023-07-31T08:43:06.172408Z","shell.execute_reply":"2023-07-31T08:43:06.17243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interpreter = tf.lite.Interpreter(\"/kaggle/working/model.tflite\")\n\nREQUIRED_SIGNATURE = \"serving_default\"\nREQUIRED_OUTPUT = \"outputs\"\n","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.174313Z","iopub.status.idle":"2023-07-31T08:43:06.175355Z","shell.execute_reply.started":"2023-07-31T08:43:06.175089Z","shell.execute_reply":"2023-07-31T08:43:06.175112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open (\"/kaggle/input/asl-fingerspelling/character_to_prediction_index.json\", \"r\") as f:\n    character_map = json.load(f)\nrev_character_map = {j:i for i,j in character_map.items()}","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.176449Z","iopub.status.idle":"2023-07-31T08:43:06.178058Z","shell.execute_reply.started":"2023-07-31T08:43:06.177815Z","shell.execute_reply":"2023-07-31T08:43:06.177838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"found_signatures = list(interpreter.get_signature_list().keys())\nif REQUIRED_SIGNATURE not in found_signatures:\n    raise KernelEvalException('Required input signature not found.')\nprint (found_signatures)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.179544Z","iopub.status.idle":"2023-07-31T08:43:06.180016Z","shell.execute_reply.started":"2023-07-31T08:43:06.179781Z","shell.execute_reply":"2023-07-31T08:43:06.179803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_test=tf.cast(np.random.random ((128,100)),dtype=tf.float32)\nprint (input_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.18184Z","iopub.status.idle":"2023-07-31T08:43:06.182295Z","shell.execute_reply.started":"2023-07-31T08:43:06.182055Z","shell.execute_reply":"2023-07-31T08:43:06.182076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_fn = interpreter.get_signature_runner(\"serving_default\")\niterator = iter(data_test)\nfor item in iterator :\n        true= item [\"input_2\"].numpy()\n        pred = item [\"input_1\"].numpy()\n        break\n\noutput = prediction_fn(inputs=pred)\nprint (output)\nprediction_str = \"\".join([rev_character_map.get(s, \"\") for s in np.argmax(output[REQUIRED_OUTPUT], axis=1)])\nprint(prediction_str)","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.183661Z","iopub.status.idle":"2023-07-31T08:43:06.184793Z","shell.execute_reply.started":"2023-07-31T08:43:06.184523Z","shell.execute_reply":"2023-07-31T08:43:06.184545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip submission.zip  '/kaggle/working/model.tflite' '/kaggle/working/inference_args.json'","metadata":{"execution":{"iopub.status.busy":"2023-07-31T08:43:06.186295Z","iopub.status.idle":"2023-07-31T08:43:06.186759Z","shell.execute_reply.started":"2023-07-31T08:43:06.186511Z","shell.execute_reply":"2023-07-31T08:43:06.186532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}