{"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":"!pip install mediapipe","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:10.132189Z","iopub.execute_input":"2023-08-16T12:13:10.132734Z","iopub.status.idle":"2023-08-16T12:13:25.362329Z","shell.execute_reply.started":"2023-08-16T12:13:10.13269Z","shell.execute_reply":"2023-08-16T12:13:25.361166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport mediapipe as mp\nimport pyarrow.parquet as pq\nimport json\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:25.364662Z","iopub.execute_input":"2023-08-16T12:13:25.365046Z","iopub.status.idle":"2023-08-16T12:13:35.10778Z","shell.execute_reply.started":"2023-08-16T12:13:25.365014Z","shell.execute_reply":"2023-08-16T12:13:35.106398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')\nprint(train_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.110379Z","iopub.execute_input":"2023-08-16T12:13:35.111016Z","iopub.status.idle":"2023-08-16T12:13:35.274123Z","shell.execute_reply.started":"2023-08-16T12:13:35.110982Z","shell.execute_reply":"2023-08-16T12:13:35.273021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.276194Z","iopub.execute_input":"2023-08-16T12:13:35.27652Z","iopub.status.idle":"2023-08-16T12:13:35.29801Z","shell.execute_reply.started":"2023-08-16T12:13:35.276491Z","shell.execute_reply":"2023-08-16T12:13:35.297102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LIP = [\n    61, 185, 40, 39, 37, 0, 267, 269, 270, 409,\n    291, 146, 91, 181, 84, 17, 314, 405, 321, 375,\n    78, 191, 80, 81, 82, 13, 312, 311, 310, 415,\n    95, 88, 178, 87, 14, 317, 402, 318, 324, 308,\n]\nLPOSE = [13, 15, 17, 19, 21]\nRPOSE = [14, 16, 18, 20, 22]\nPOSE = LPOSE + RPOSE","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.299317Z","iopub.execute_input":"2023-08-16T12:13:35.29983Z","iopub.status.idle":"2023-08-16T12:13:35.305382Z","shell.execute_reply.started":"2023-08-16T12:13:35.299796Z","shell.execute_reply":"2023-08-16T12:13:35.304608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def features(axis):\n    AXIS = [f'{axis}_right_hand_{i}' for i in range(21)] + [f'{axis}_left_hand_{i}' for i in range(21)] + [f'{axis}_pose_{i}' for i in POSE] + [f'{axis}_face_{i}' for i in LIP]\n    return AXIS\n\nX = features('x')\nY = features('y')\nZ = features('z')\n\nFEATURE_COLUMNS = X + Y + Z","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.306722Z","iopub.execute_input":"2023-08-16T12:13:35.307041Z","iopub.status.idle":"2023-08-16T12:13:35.319961Z","shell.execute_reply.started":"2023-08-16T12:13:35.307012Z","shell.execute_reply":"2023-08-16T12:13:35.318878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def axis_idx(axis):\n    AXIS_IDX = [i for i, col in enumerate(FEATURE_COLUMNS) if f'{axis}_' in col]\n    return AXIS_IDX\n\nX_IDX = axis_idx('x')\nY_IDX = axis_idx('y')\nZ_IDX = axis_idx('z')\n\nRHAND_IDX = [i for i, col in enumerate(FEATURE_COLUMNS)  if \"right\" in col]\nLHAND_IDX = [i for i, col in enumerate(FEATURE_COLUMNS)  if  \"left\" in col]\nRPOSE_IDX = [i for i, col in enumerate(FEATURE_COLUMNS)  if  \"pose\" in col and int(col[-2:]) in RPOSE]\nLPOSE_IDX = [i for i, col in enumerate(FEATURE_COLUMNS)  if  \"pose\" in col and int(col[-2:]) in LPOSE]\nLIP_IDX = [i for i, col in enumerate(FEATURE_COLUMNS)  if \"face\" in col]","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.321497Z","iopub.execute_input":"2023-08-16T12:13:35.321944Z","iopub.status.idle":"2023-08-16T12:13:35.333523Z","shell.execute_reply.started":"2023-08-16T12:13:35.321903Z","shell.execute_reply":"2023-08-16T12:13:35.33262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FRAME_LEN = 128","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.334661Z","iopub.execute_input":"2023-08-16T12:13:35.334959Z","iopub.status.idle":"2023-08-16T12:13:35.347155Z","shell.execute_reply.started":"2023-08-16T12:13:35.334932Z","shell.execute_reply":"2023-08-16T12:13:35.346006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if not os.path.isdir(\"preprocessed\"):\n    os.mkdir(\"preprocessed\")\nelse:\n    shutil.rmtree(\"preprocessed\")\n    os.mkdir(\"preprocessed\")","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.348855Z","iopub.execute_input":"2023-08-16T12:13:35.349695Z","iopub.status.idle":"2023-08-16T12:13:35.359592Z","shell.execute_reply.started":"2023-08-16T12:13:35.349662Z","shell.execute_reply":"2023-08-16T12:13:35.358601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_id in tqdm(train_df.file_id.unique()):\n    pq_file = f'/kaggle/input/asl-fingerspelling/train_landmarks/{file_id}.parquet'\n    file_df = train_df.loc[train_df['file_id'] == file_id]\n    parquet_df = pq.read_table(pq_file, columns=['sequence_id']+FEATURE_COLUMNS).to_pandas()\n    tf_file = f'preprocessed/{file_id}.tfrecord'\n    parquet_numpy = parquet_df.to_numpy()\n    \n    with tf.io.TFRecordWriter(tf_file) as file_writer:\n        for seq_id, phrase in zip(file_df.sequence_id, file_df.phrase):\n            frames = parquet_numpy[parquet_df.index == seq_id]\n            r_nonan = np.sum(np.sum(np.isnan(frames[:, RHAND_IDX]), axis = 1) == 0)\n            l_nonan = np.sum(np.sum(np.isnan(frames[:, LHAND_IDX]), axis = 1) == 0)\n            no_nan = max(r_nonan, l_nonan)\n            if 2*len(phrase)<no_nan:\n                features = {FEATURE_COLUMNS[i]: tf.train.Feature(\n                    float_list=tf.train.FloatList(value=frames[:, i])) for i in range(len(FEATURE_COLUMNS))}\n                features[\"phrase\"] = tf.train.Feature(bytes_list=tf.train.BytesList(value=[bytes(phrase, 'utf-8')]))\n                record_bytes = tf.train.Example(features=tf.train.Features(feature=features)).SerializeToString()\n                file_writer.write(record_bytes)","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:13:35.36329Z","iopub.execute_input":"2023-08-16T12:13:35.363654Z","iopub.status.idle":"2023-08-16T12:29:18.074634Z","shell.execute_reply.started":"2023-08-16T12:13:35.363624Z","shell.execute_reply":"2023-08-16T12:29:18.073272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf_records = train_df.file_id.map(lambda x: f'/kaggle/working/preprocessed/{x}.tfrecord').unique()\nprint(f\"List of {len(tf_records)} TFRecord files.\")","metadata":{"execution":{"iopub.status.busy":"2023-08-16T12:29:18.13842Z","iopub.execute_input":"2023-08-16T12:29:18.138861Z","iopub.status.idle":"2023-08-16T12:29:18.192687Z","shell.execute_reply.started":"2023-08-16T12:29:18.138829Z","shell.execute_reply":"2023-08-16T12:29:18.191527Z"},"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\n    \npad_token = '^'\nstart_token = '<'\nend_token = '>'\npad_token_idx = 59\nstart_token_idx = 60\nend_token_idx = 61\n\nchar_to_num[pad_token] = pad_token_idx\nchar_to_num[start_token] = start_token_idx\nchar_to_num[end_token] = end_token_idx    \n\nnum_to_char = {j:i for i,j in char_to_num.items()}","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:44.974972Z","iopub.execute_input":"2023-08-16T14:44:44.975442Z","iopub.status.idle":"2023-08-16T14:44:44.984601Z","shell.execute_reply.started":"2023-08-16T14:44:44.97541Z","shell.execute_reply":"2023-08-16T14:44:44.983145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_pad(x):\n    if tf.shape(x)[0] < FRAME_LEN:\n        x = tf.pad(x, ([[0, FRAME_LEN-tf.shape(x)[0]], [0, 0], [0, 0]]))\n    else:\n        x = tf.image.resize(x, (FRAME_LEN, tf.shape(x)[1]))\n    return x","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:46.220859Z","iopub.execute_input":"2023-08-16T14:44:46.221275Z","iopub.status.idle":"2023-08-16T14:44:46.228119Z","shell.execute_reply.started":"2023-08-16T14:44:46.221242Z","shell.execute_reply":"2023-08-16T14:44:46.226972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pre_process(x):\n    rhand = tf.gather(x, RHAND_IDX, axis=1)\n    lhand = tf.gather(x, LHAND_IDX, axis=1)\n    rpose = tf.gather(x, RPOSE_IDX, axis=1)\n    lpose = tf.gather(x, LPOSE_IDX, axis=1)\n    lips = tf.gather(x, LIP_IDX, axis=1)\n    \n    \n    rhand_x = rhand[:, 0*(len(RHAND_IDX)//3) : 1*(len(RHAND_IDX)//3)]\n    rhand_y = rhand[:, 1*(len(RHAND_IDX)//3) : 2*(len(RHAND_IDX)//3)]\n    rhand_z = rhand[:, 2*(len(RHAND_IDX)//3) : 3*(len(RHAND_IDX)//3)]\n    rhand = tf.concat([rhand_x[..., tf.newaxis], rhand_y[..., tf.newaxis], rhand_z[..., tf.newaxis]], axis=-1)\n    \n    lhand_x = lhand[:, 0*(len(LHAND_IDX)//3) : 1*(len(LHAND_IDX)//3)]\n    lhand_y = lhand[:, 1*(len(LHAND_IDX)//3) : 2*(len(LHAND_IDX)//3)]\n    lhand_z = lhand[:, 2*(len(LHAND_IDX)//3) : 3*(len(LHAND_IDX)//3)]\n    lhand = tf.concat([lhand_x[..., tf.newaxis], lhand_y[..., tf.newaxis], lhand_z[..., tf.newaxis]], axis=-1)\n\n    \n    rmean = tf.math.reduce_mean(rhand, axis=1)[:, tf.newaxis, :]\n    rstd = tf.math.reduce_std(rhand, axis=1)[:, tf.newaxis, :]\n    rhand = (rhand - rmean) / rstd\n    \n    lmean = tf.math.reduce_mean(lhand, axis=1)[:, tf.newaxis, :]\n    lstd = tf.math.reduce_std(lhand, axis=1)[:, tf.newaxis, :]\n    lhand = (lhand - lmean) / lstd\n    \n    rpose_x = rpose[:, 0*(len(RPOSE_IDX)//3) : 1*(len(RPOSE_IDX)//3)]\n    rpose_y = rpose[:, 1*(len(RPOSE_IDX)//3) : 2*(len(RPOSE_IDX)//3)]\n    rpose_z = rpose[:, 2*(len(RPOSE_IDX)//3) : 3*(len(RPOSE_IDX)//3)]\n    rpose = tf.concat([rpose_x[..., tf.newaxis], rpose_y[..., tf.newaxis], rpose_z[..., tf.newaxis]], axis=-1)\n    \n    lpose_x = lpose[:, 0*(len(LPOSE_IDX)//3) : 1*(len(LPOSE_IDX)//3)]\n    lpose_y = lpose[:, 1*(len(LPOSE_IDX)//3) : 2*(len(LPOSE_IDX)//3)]\n    lpose_z = lpose[:, 2*(len(LPOSE_IDX)//3) : 3*(len(LPOSE_IDX)//3)]\n    lpose = tf.concat([lpose_x[..., tf.newaxis], lpose_y[..., tf.newaxis], lpose_z[..., tf.newaxis]], axis=-1)\n    \n    lip_x = lips[:, 0*(len(LIP_IDX)//3) : 1*(len(LIP_IDX)//3)]\n    lip_y = lips[:, 1*(len(LIP_IDX)//3) : 2*(len(LIP_IDX)//3)]\n    lip_z = lips[:, 2*(len(LIP_IDX)//3) : 3*(len(LIP_IDX)//3)]\n    lips   = tf.concat([lip_x[..., tf.newaxis], lip_y[..., tf.newaxis], lip_z[..., tf.newaxis]], axis=-1)\n    \n    x = tf.concat([rhand, lhand, rpose, lpose, lips], axis=1)\n    x = resize_pad(x)\n    x = tf.where(tf.math.is_nan(x), tf.zeros_like(x), x)\n    \n    x = tf.reshape(x, (FRAME_LEN, len(LHAND_IDX) + len(LPOSE_IDX) + len(RHAND_IDX) + len(LPOSE_IDX) + len(LIP_IDX)))\n    \n    return x","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:47.32228Z","iopub.execute_input":"2023-08-16T14:44:47.322711Z","iopub.status.idle":"2023-08-16T14:44:47.344521Z","shell.execute_reply.started":"2023-08-16T14:44:47.322675Z","shell.execute_reply":"2023-08-16T14:44:47.343608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decode_fn(record_bytes):\n    schema = {COL: tf.io.VarLenFeature(dtype=tf.float32) for COL in FEATURE_COLUMNS}\n    schema[\"phrase\"] = tf.io.FixedLenFeature([], dtype=tf.string)\n    features = tf.io.parse_single_example(record_bytes, schema)\n    phrase = features['phrase']\n    landmarks = ([tf.sparse.to_dense(features[COL]) for COL in FEATURE_COLUMNS])\n    landmarks = tf.transpose(landmarks)\n    \n    return landmarks, phrase","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:48.105471Z","iopub.execute_input":"2023-08-16T14:44:48.106516Z","iopub.status.idle":"2023-08-16T14:44:48.112325Z","shell.execute_reply.started":"2023-08-16T14:44:48.106477Z","shell.execute_reply":"2023-08-16T14:44:48.111426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"table = tf.lookup.StaticHashTable(\n    initializer=tf.lookup.KeyValueTensorInitializer(\n        keys=list(char_to_num.keys()),\n        values=list(char_to_num.values()),\n    ),\n    default_value=tf.constant(-1),\n    name=\"class_weight\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:49.051515Z","iopub.execute_input":"2023-08-16T14:44:49.052135Z","iopub.status.idle":"2023-08-16T14:44:49.060458Z","shell.execute_reply.started":"2023-08-16T14:44:49.052099Z","shell.execute_reply":"2023-08-16T14:44:49.05962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_fn(landmarks, phrase):\n    phrase = start_token + phrase + end_token\n    phrase = tf.strings.bytes_split(phrase)\n    phrase = table.lookup(phrase)\n    phrase = tf.pad(phrase, paddings=[[0, 64 - tf.shape(phrase)[0]]], mode='CONSTANT', constant_values = pad_token_idx)\n    \n    return pre_process(landmarks), phrase","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:50.019707Z","iopub.execute_input":"2023-08-16T14:44:50.020323Z","iopub.status.idle":"2023-08-16T14:44:50.025447Z","shell.execute_reply.started":"2023-08-16T14:44:50.020289Z","shell.execute_reply":"2023-08-16T14:44:50.02459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tffiles = \"/kaggle/working/preprocessed/5414471.tfrecord\"\ntrain_len = int(0.8 * len(tf_records))\ntrain_batch_size = 32\n\ntrain_dataset =  tf.data.TFRecordDataset(tf_records[:train_len]).map(decode_fn).map(convert_fn).batch(train_batch_size).prefetch(buffer_size=tf.data.AUTOTUNE).cache()\nval_dataset = tf.data.TFRecordDataset(tf_records[train_len:]).map(decode_fn).map(convert_fn).batch(train_batch_size).prefetch(buffer_size=tf.data.AUTOTUNE).cache()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:52.563025Z","iopub.execute_input":"2023-08-16T14:44:52.563906Z","iopub.status.idle":"2023-08-16T14:44:54.144919Z","shell.execute_reply.started":"2023-08-16T14:44:52.563848Z","shell.execute_reply":"2023-08-16T14:44:54.143577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = next(iter(train_dataset))\nbatch[0].shape, batch[1].shape","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:44:57.245925Z","iopub.execute_input":"2023-08-16T14:44:57.24673Z","iopub.status.idle":"2023-08-16T14:44:57.854161Z","shell.execute_reply.started":"2023-08-16T14:44:57.246692Z","shell.execute_reply":"2023-08-16T14:44:57.852792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:00.040829Z","iopub.execute_input":"2023-08-16T14:45:00.041279Z","iopub.status.idle":"2023-08-16T14:45:00.048602Z","shell.execute_reply.started":"2023-08-16T14:45:00.041232Z","shell.execute_reply":"2023-08-16T14:45:00.047275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for landmarks, phrase in train_dataset.map(lambda x, y: (x, y)):\n#   print(phrase[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:01.117124Z","iopub.execute_input":"2023-08-16T14:45:01.118054Z","iopub.status.idle":"2023-08-16T14:45:01.122408Z","shell.execute_reply.started":"2023-08-16T14:45:01.118013Z","shell.execute_reply":"2023-08-16T14:45:01.121556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    model = tf.keras.Sequential([\n        tf.keras.layers.Dense(128, activation='relu', input_shape=(128,276)),\n        tf.keras.layers.Flatten(),\n        tf.keras.layers.Dense(64)\n        \n        \n        \n    ])    \n    \n    \n    \n    loss = tf.keras.losses.CategoricalCrossentropy()\n    optimizer = tf.keras.optimizers.Adam(0.001)\n    \n    model.compile(loss=loss,\n                 optimizer= optimizer,\n                 metrics=['accuracy'])\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:04.977165Z","iopub.execute_input":"2023-08-16T14:45:04.978344Z","iopub.status.idle":"2023-08-16T14:45:04.984566Z","shell.execute_reply.started":"2023-08-16T14:45:04.978303Z","shell.execute_reply":"2023-08-16T14:45:04.983442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ndef create_model():\n    \n    inp = tf.keras.Input([128,276])\n    x = tf.keras.layers.Embedding(1000, 64)(inp)\n    x = tf.keras.layers.Embedding(100, 64)(x)\n        #tf.keras.layers.InputLayer(input_shape=(128,276))\n    x = tf.keras.layers.Masking(mask_value=0.0)(inp)\n    #x = tf.keras.layers.Dense(128, use_bias=False,name='stem_conv')(x) + positional_encoding(128, 128)    \n    \n    x = tf.keras.layers.BatchNormalization(momentum=0.95,name='stem_bn')(x)\n    x = tf.keras.layers.Conv1D(64, 3)(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    x = tf.keras.layers.Conv1D(128, 3)(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    x = tf.keras.layers.Conv1D(128, 3)(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    #x = tf.keras.layers.Flatten()(x)\n    x = tf.keras.layers.Dense(128, activation = 'relu')(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    x = tf.keras.layers.Dense(256, activation = 'relu')(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    x = tf.keras.layers.Dense(512, activation = 'relu')(x)\n    x = tf.keras.layers.Dropout(rate=0.4)(x)\n    x = tf.keras.layers.Dense(64)(x)\n    \n    model = tf.keras.Model(inp, x)\n    \n    loss = tf.keras.losses.CategoricalCrossentropy()\n    optimizer = tf.keras.optimizers.Adam(0.001)\n    \n    model.compile(loss=loss,\n                 optimizer= optimizer,\n                 metrics=['accuracy'])\n    return model\n    \n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:07.212993Z","iopub.execute_input":"2023-08-16T14:45:07.213412Z","iopub.status.idle":"2023-08-16T14:45:07.221926Z","shell.execute_reply.started":"2023-08-16T14:45:07.21338Z","shell.execute_reply":"2023-08-16T14:45:07.220636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:08.559222Z","iopub.execute_input":"2023-08-16T14:45:08.559663Z","iopub.status.idle":"2023-08-16T14:45:08.629254Z","shell.execute_reply.started":"2023-08-16T14:45:08.559627Z","shell.execute_reply":"2023-08-16T14:45:08.628021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model(batch[0])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:09.791937Z","iopub.execute_input":"2023-08-16T14:45:09.792355Z","iopub.status.idle":"2023-08-16T14:45:09.823886Z","shell.execute_reply.started":"2023-08-16T14:45:09.792324Z","shell.execute_reply":"2023-08-16T14:45:09.822502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_dataset,\n    validation_data=val_dataset,\n    epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:45:11.561296Z","iopub.execute_input":"2023-08-16T14:45:11.562056Z","iopub.status.idle":"2023-08-16T14:54:08.375632Z","shell.execute_reply.started":"2023-08-16T14:45:11.562016Z","shell.execute_reply":"2023-08-16T14:54:08.374511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.legend(['training loss', 'val_loss'])","metadata":{"execution":{"iopub.status.busy":"2023-08-16T14:33:19.756915Z","iopub.execute_input":"2023-08-16T14:33:19.757396Z","iopub.status.idle":"2023-08-16T14:33:20.098928Z","shell.execute_reply.started":"2023-08-16T14:33:19.757352Z","shell.execute_reply":"2023-08-16T14:33:20.097731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"model.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-08-16T15:01:04.694397Z","iopub.execute_input":"2023-08-16T15:01:04.694863Z","iopub.status.idle":"2023-08-16T15:01:04.720313Z","shell.execute_reply.started":"2023-08-16T15:01:04.694829Z","shell.execute_reply":"2023-08-16T15:01:04.718994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"converter = tf.lite.TFLiteConverter.from_keras_model(model)\nconverter.experimental_new_converter = True\ntflite_model = converter.convert()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T15:05:06.151Z","iopub.execute_input":"2023-08-16T15:05:06.151489Z","iopub.status.idle":"2023-08-16T15:05:08.49839Z","shell.execute_reply.started":"2023-08-16T15:05:06.151457Z","shell.execute_reply":"2023-08-16T15:05:08.497083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('/kaggle/working/model.tflite', 'wb') as f:\n    f.write(tflite_model)\n    \ninfargs = {\"selected_columns\" : FEATURE_COLUMNS}\n\nwith open('inference_args.json', \"w\") as json_file:\n    json.dump(infargs, json_file)","metadata":{"execution":{"iopub.status.busy":"2023-08-16T15:05:28.2379Z","iopub.execute_input":"2023-08-16T15:05:28.238494Z","iopub.status.idle":"2023-08-16T15:05:28.255518Z","shell.execute_reply.started":"2023-08-16T15:05:28.238452Z","shell.execute_reply":"2023-08-16T15:05:28.25449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip submission.zip  './model.tflite' './inference_args.json'","metadata":{"execution":{"iopub.status.busy":"2023-08-16T15:05:38.475782Z","iopub.execute_input":"2023-08-16T15:05:38.476231Z","iopub.status.idle":"2023-08-16T15:05:40.162767Z","shell.execute_reply.started":"2023-08-16T15:05:38.476196Z","shell.execute_reply":"2023-08-16T15:05:40.161178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}