{"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":"\n# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport json\nimport plotly.graph_objects as go\nimport plotly.io as pio\npio.templates.default = \"simple_white\"\n\n\nplt.style.use('seaborn-colorblind')\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n    #for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-17T22:52:24.416357Z","iopub.execute_input":"2023-04-17T22:52:24.416635Z","iopub.status.idle":"2023-04-17T22:52:34.183473Z","shell.execute_reply.started":"2023-04-17T22:52:24.416606Z","shell.execute_reply":"2023-04-17T22:52:34.182319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_DIR = \"/kaggle/input/asl-signs/\"\ntrain = pd.read_csv(f\"{INPUT_DIR}train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:34.185639Z","iopub.execute_input":"2023-04-17T22:52:34.186376Z","iopub.status.idle":"2023-04-17T22:52:34.366982Z","shell.execute_reply.started":"2023-04-17T22:52:34.186344Z","shell.execute_reply":"2023-04-17T22:52:34.365961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## The Model & Preprocessing Pipeline","metadata":{}},{"cell_type":"code","source":"# from sklearn.preprocessing import LabelEncorder\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:34.368410Z","iopub.execute_input":"2023-04-17T22:52:34.368771Z","iopub.status.idle":"2023-04-17T22:52:34.427521Z","shell.execute_reply.started":"2023-04-17T22:52:34.368735Z","shell.execute_reply":"2023-04-17T22:52:34.426550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_encoder = LabelEncoder()\nlabel_encoder.fit(train['sign'].unique())","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:34.431467Z","iopub.execute_input":"2023-04-17T22:52:34.432313Z","iopub.status.idle":"2023-04-17T22:52:34.459293Z","shell.execute_reply.started":"2023-04-17T22:52:34.432270Z","shell.execute_reply":"2023-04-17T22:52:34.458402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating the dataset\nimport pyarrow.parquet as pq\nimport os \nROWS_PER_FRAME = 543  # number of landmarks per frame\ndef read_parquet_file(file_path, label):\n    data_columns = ['x', 'y', 'z']\n    file_path = INPUT_DIR+file_path\n    file_path_str = file_path.numpy().decode('utf-8')\n    data = pd.read_parquet(file_path_str, columns=data_columns)\n    n_frames = int(len(data) / ROWS_PER_FRAME)\n    data = data.values.reshape(n_frames, ROWS_PER_FRAME, len(data_columns))\n    features = data\n    return features, label","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:39.367144Z","iopub.execute_input":"2023-04-17T22:52:39.367518Z","iopub.status.idle":"2023-04-17T22:52:39.374567Z","shell.execute_reply.started":"2023-04-17T22:52:39.367486Z","shell.execute_reply":"2023-04-17T22:52:39.373433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generator_function():\n    for idx, row in train.iterrows():\n        file_path = row['path']\n        label = row['sign']\n        encoded_label = label_encoder.transform([label])[0]\n        yield file_path, encoded_label\n        \ndataset = tf.data.Dataset.from_generator(\n    generator_function,\n    output_signature=(\n        tf.TensorSpec(shape=(), dtype=tf.string),\n        tf.TensorSpec(shape=(), dtype=tf.int32)\n    )\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:40.000951Z","iopub.execute_input":"2023-04-17T22:52:40.001331Z","iopub.status.idle":"2023-04-17T22:52:42.539674Z","shell.execute_reply.started":"2023-04-17T22:52:40.001297Z","shell.execute_reply":"2023-04-17T22:52:42.538628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_and_preprocess_data(file_path, encoded_label):\n    features, _ = tf.py_function(read_parquet_file, [file_path, encoded_label], [tf.float32, tf.int32])\n\n    return features, tf.cast(encoded_label, tf.int32)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:42.541698Z","iopub.execute_input":"2023-04-17T22:52:42.542094Z","iopub.status.idle":"2023-04-17T22:52:42.548109Z","shell.execute_reply.started":"2023-04-17T22:52:42.542039Z","shell.execute_reply":"2023-04-17T22:52:42.546549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = dataset.map(\n    load_and_preprocess_data,\n    num_parallel_calls=tf.data.experimental.AUTOTUNE,\n    deterministic=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:43.748566Z","iopub.execute_input":"2023-04-17T22:52:43.749568Z","iopub.status.idle":"2023-04-17T22:52:43.810716Z","shell.execute_reply.started":"2023-04-17T22:52:43.749519Z","shell.execute_reply":"2023-04-17T22:52:43.809738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Take one batch from the dataset\none_batch = dataset.take(1)\n\n# Extract features and labels from the batch\nfor features, labels in one_batch:\n    batch_features_shape = features.shape\n    batch_labels_shape = labels.shape\n    print(\"Batch features shape:\", batch_features_shape)\n    print(\"Batch labels shape:\", batch_labels_shape)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:44.360468Z","iopub.execute_input":"2023-04-17T22:52:44.361357Z","iopub.status.idle":"2023-04-17T22:52:44.635698Z","shell.execute_reply.started":"2023-04-17T22:52:44.361305Z","shell.execute_reply":"2023-04-17T22:52:44.634670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the padding shapes for features and labels\n'''padding_shapes = (\n    tf.TensorShape([537,543,3]),  # Set the first dimension of the features tensor to None to allow variable-length sequences\n    tf.TensorShape([])          # Keep the label tensor as a scalar\n)'''\n\n\n# Define the padding values for features and labels\n'''padding_values = (\n    tf.constant(0, dtype=tf.float32),  # Pad features with 0\n    tf.constant(-1, dtype=tf.int32)    # Pad labels with -1 (or any value that's not a valid label)\n)'''\n\n# Batch the dataset using padded_batch()\nbatch_size = 32\ndef preprocess(data, label):\n    target_shape = (20, 543, 3)\n\n    # Get the shape of data tensor\n    data_shape = tf.shape(data)\n\n    # Resize and preprocess the image and label\n    data = tf.slice(data, [0, 0, 0], [-1, -1, -1])\n\n    if data_shape[0] < target_shape[0]:\n        # Calculate the number of rows to add\n        rows_to_add = target_shape[0] - data_shape[0]\n\n        # Create rows of zeros with the same number of columns as the matrix\n        additional_rows = tf.zeros([rows_to_add, data_shape[1], data_shape[2]], dtype=tf.float32)\n\n        # Concatenate the additional rows to the matrix\n        data = tf.concat([data, additional_rows], axis=0)\n    elif data_shape[0] > target_shape[0]:\n        # Resize and preprocess the image and label\n        data = tf.slice(data, [0, 0, 0], [20, -1, -1])\n\n    return data, label\n\n# Assuming dataset is already defined\ntrain_dataset = dataset.map(preprocess)\ntrain_dataset = train_dataset.batch(batch_size, drop_remainder=True)\n#padded_batch(batch_size, padded_shapes=padding_shapes, padding_values=padding_values)\n\n# Take one batch from the dataset\none_batch = train_dataset.take(2)\n\n# Extract features and labels from the batch\nfor features, labels in one_batch:\n    \n    batch_features_shape = features.shape\n    batch_labels_shape = labels.shape\n    batch_features = features\n    print(\"Batch features shape:\", batch_features_shape)\n    print(\"Batch labels shape:\", batch_labels_shape)\n    #print(features)\n    #print(shape)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:45.731152Z","iopub.execute_input":"2023-04-17T22:52:45.731821Z","iopub.status.idle":"2023-04-17T22:52:46.616414Z","shell.execute_reply.started":"2023-04-17T22:52:45.731783Z","shell.execute_reply":"2023-04-17T22:52:46.614470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers\n\nclass ProcessingLayer(layers.Layer):\n    def __init__(self, lh_begin, lh_size, rh_begin, rh_size,\n                 ul_begin, ul_size, bl_begin, bl_size, **kwargs):\n        super(ProcessingLayer, self).__init__(**kwargs)\n        self.lh_begin = lh_begin\n        self.lh_size = lh_size\n        self.rh_begin = rh_begin\n        self.rh_size = rh_size\n        self.ul_begin = ul_begin\n        self.ul_size = ul_size\n        self.bl_begin = bl_begin\n        self.bl_size = bl_size\n    \n    def call(self, inputs): \n            def process_single_input(single_input):\n                vector_l_hands = tf.slice(single_input, self.lh_begin, self.lh_size)\n                vector_r_hands = tf.slice(single_input, self.rh_begin, self.rh_size)\n                vector_up_lip = tf.slice(single_input, self.ul_begin, self.ul_size)\n                vector_bottom_lip = tf.slice(single_input, self.bl_begin, self.bl_size)\n                concat_fet = tf.concat([vector_l_hands, vector_r_hands, vector_up_lip, vector_bottom_lip], axis=1)\n                concat_fet = tf.where(tf.math.is_nan(concat_fet), tf.zeros_like(concat_fet), concat_fet)\n                concat_fet = tf.reduce_mean(concat_fet, axis=0, keepdims=True)\n                return concat_fet\n            processed_tensor = tf.map_fn(process_single_input, inputs, dtype=tf.float32)\n            return processed_tensor\n\n\n    \nlh_begin = [0, 468, 0]\nlh_size =  [-1, 21, -1]\nrh_begin = [0, 522, 0]\nrh_size =  [-1, 21, -1]\nul_begin = [0, 0, 0]\nul_size = [-1, 1, -1]\nbl_begin = [0, 17, 0]\nbl_size = [-1, 1, -1]","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:46.719505Z","iopub.execute_input":"2023-04-17T22:52:46.720330Z","iopub.status.idle":"2023-04-17T22:52:46.734497Z","shell.execute_reply.started":"2023-04-17T22:52:46.720294Z","shell.execute_reply":"2023-04-17T22:52:46.733449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\nfrom keras.layers import Dense, Activation, Flatten\nfrom keras.layers import Dense, Dropout, BatchNormalization\n\n\ndef create_model():\n    input_features = 1086\n    output_classes = 250\n    \n    model = models.Sequential()\n    #model.add(layers.Dense(3, activation='relu'))\n    model.add( ProcessingLayer(lh_begin, lh_size, rh_begin, rh_size, ul_begin, ul_size, bl_begin, bl_size))  # Add the custom preprocessing layer\n    model.add(Flatten())\n    # Add the remaining layers of your model\n    \n    # Input layer\n    model.add(Dense(input_features, input_dim=input_features, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n\n    # Hidden layer 1\n    model.add(Dense(4096, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n    \n    # Hidden layer 2\n    model.add(Dense(1024, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n\n    # Hidden layer 3\n    model.add(Dense(512, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n    \n    # Hidden layer 4\n    model.add(Dense(512, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n\n\n\n    # Hidden layer 5\n    model.add(Dense(256, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n    \n    # Hidden layer 6\n    model.add(Dense(128, activation='sigmoid'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.2))\n\n\n    # Output layer\n    model.add(Dense(output_classes, activation='softmax'))\n    return model\n\nmodel = create_model()\nmodel(batch_features)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:47.841148Z","iopub.execute_input":"2023-04-17T22:52:47.841820Z","iopub.status.idle":"2023-04-17T22:52:50.332022Z","shell.execute_reply.started":"2023-04-17T22:52:47.841781Z","shell.execute_reply":"2023-04-17T22:52:50.331241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''from tensorflow.keras import layers, models\nfrom keras.layers import Dense, Activation, Flatten\n\ndef create_model():\n    \n    model = models.Sequential()\n    #model.add(layers.Dense(3, activation='relu'))\n    model.add( ProcessingLayer(lh_begin, lh_size, rh_begin, rh_size, ul_begin, ul_size, bl_begin, bl_size))  # Add the custom preprocessing layer\n    model.add(Flatten())\n    # Add the remaining layers of your model\n    model.add(layers.Dense(64, activation='relu'))\n    model.add(layers.Dense(32, activation='relu'))\n    model.add(layers.Dense(250, activation='softmax'))\n    return model\n\nmodel = create_model()\nmodel(batch_features)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()'''","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:50.333679Z","iopub.execute_input":"2023-04-17T22:52:50.334514Z","iopub.status.idle":"2023-04-17T22:52:50.343416Z","shell.execute_reply.started":"2023-04-17T22:52:50.334472Z","shell.execute_reply":"2023-04-17T22:52:50.342410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List all available GPUs\n# Set the device to be used by TensorFlow\ndevice_name = tf.test.gpu_device_name()\nif device_name != '/device:GPU:0':\n    raise SystemError('GPU device not found')\nprint('Found GPU at: {}'.format(device_name))\ntf.debugging.set_log_device_placement(True)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:50.588633Z","iopub.execute_input":"2023-04-17T22:52:50.589630Z","iopub.status.idle":"2023-04-17T22:52:50.598299Z","shell.execute_reply.started":"2023-04-17T22:52:50.589591Z","shell.execute_reply":"2023-04-17T22:52:50.597084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint\n\n# Define the checkpoint path and filename\ncheckpoint_path = \"/kaggle/working/model_best.h5\"\n\n# Define the ModelCheckpoint callback\ncheckpoint = ModelCheckpoint(\n    checkpoint_path,\n    monitor='val_loss',\n    save_best_only=True,\n    mode='min',\n    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:52:56.434982Z","iopub.execute_input":"2023-04-17T22:52:56.435678Z","iopub.status.idle":"2023-04-17T22:52:56.442466Z","shell.execute_reply.started":"2023-04-17T22:52:56.435640Z","shell.execute_reply":"2023-04-17T22:52:56.441377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset, epochs=1, callbacks=[checkpoint,\n    tf.keras.callbacks.LearningRateScheduler(\n        lambda epoch: 1e-3 * 10 ** (epoch / 30)\n    )])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T22:53:05.619145Z","iopub.execute_input":"2023-04-17T22:53:05.619505Z","iopub.status.idle":"2023-04-17T22:54:23.151764Z","shell.execute_reply.started":"2023-04-17T22:53:05.619474Z","shell.execute_reply":"2023-04-17T22:54:23.149975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''import xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nnum_classes = 250\nfeatures, labels = [], []\n# Iterate through the dataset and extract the features and labels\nfor feature, label in train_dataset.as_numpy_iterator():\n    features.append(feature)\n    labels.append(label)\n\nX = np.array(features)\ny = np.array(labels)\n\n# Split the dataset into training and testing sets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# Convert the dataset to DMatrix format for XGBoost\ndtrain = xgb.DMatrix(X_train, label=y_train)\ndtest = xgb.DMatrix(X_test, label=y_test)\n\n# Set parameters for the XGBoost model\nparams = {\n    'objective': 'multi:softmax',\n    'eval_metric': 'mlogloss',\n    'max_depth': 3,\n    'eta': 0.1,\n    'num_class': num_classes,\n    'tree_method': 'gpu_hist',  # Use GPU acceleration\n}\n\n# Train the XGBoost model\nnum_round = 1\nbst = xgb.train(params, dtrain, num_round)\n\n# Make predictions on the test set\ny_pred = bst.predict(dtest)\ny_pred = [round(value) for value in y_pred]\n\n# Calculate the accuracy of the model\naccuracy = accuracy_score(y_test, y_pred)\nprint(f\"Accuracy: {accuracy * 100.0:.2f}%\")'''\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:18:58.460053Z","iopub.execute_input":"2023-04-16T00:18:58.460678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''input_shape = load_relevant_data_subset(\"/kaggle/input/asl-signs/train_landmark_files/49445/1003700302.parquet\").shape\ninput_shape'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.074123Z","iopub.status.idle":"2023-04-16T00:03:35.074581Z","shell.execute_reply.started":"2023-04-16T00:03:35.074363Z","shell.execute_reply":"2023-04-16T00:03:35.074387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''# create a dataset from the load_relevant_data_subset\nmodel = tf.keras.Sequential([\n    layers.Input(shape=(19, 543, 3)),\n    #layers.Flatten(),\n    ProcessingLayer(lh_begin, lh_size, rh_begin, rh_size, ul_begin, ul_size, bl_begin, bl_size),\n    layers.Flatten()\n    #layers.BatchNormalization(),\n    #layers.Dense(64, activation='relu'),\n    #layers.Dense(10, activation='softmax')\n])'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.076627Z","iopub.status.idle":"2023-04-16T00:03:35.077086Z","shell.execute_reply.started":"2023-04-16T00:03:35.076875Z","shell.execute_reply":"2023-04-16T00:03:35.076899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation\n- from evaluation page","metadata":{}},{"cell_type":"code","source":"'''test = pd.read_parquet(f\"{INPUT_DIR}/train_landmark_files/28656/1000106739.parquet\")\n\nROWS_PER_FRAME = 543  # number of landmarks per frame\ndef load_relevant_data_subset(pq_path):\n    data_columns = ['x', 'y', 'z']\n    data = pd.read_parquet(pq_path, columns=data_columns)\n    n_frames = int(len(data) / ROWS_PER_FRAME)\n    data = data.values.reshape(n_frames, ROWS_PER_FRAME, len(data_columns))\n    return data.astype(np.float32)'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.079269Z","iopub.status.idle":"2023-04-16T00:03:35.079961Z","shell.execute_reply.started":"2023-04-16T00:03:35.079579Z","shell.execute_reply":"2023-04-16T00:03:35.079613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''test = pd.read_parquet(f\"/kaggle/input/asl-signs/train_landmark_files/22343/1001223069.parquet\")\ntest'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.083031Z","iopub.status.idle":"2023-04-16T00:03:35.083784Z","shell.execute_reply.started":"2023-04-16T00:03:35.083405Z","shell.execute_reply":"2023-04-16T00:03:35.083445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''test = pd.read_parquet(f\"/kaggle/input/asl-signs/train_landmark_files/32319/1005865446.parquet\")\nframe_ex = test.query(\"frame == 0\").reset_index()\npose = test.query(\"type == 'pose' and frame == 96\") #489\nl_hand = test.query(\"type == 'left_hand' and frame == 96\") #468\nr_hand = test.query(\"type == 'right_hand' and frame == 96\") #522\nface = test.query(\"type == 'face' and frame == 0\") #522\n#print(len(test.query(\"type == 'right_hand' or type=='left_hand'\")))`\n#len(frame_ex.iloc[489:522])\n#len(frame_ex.iloc[468:489])\n#en(frame_ex.iloc[522:543])\n#frame_ex.iloc[0]\n#frame_ex.iloc[17]\n#frame_ex.query(\"type == 'face' and (landmark_index == 0 or landmark_index == 17)\")\n#len(frame_ex)\n#frame_ex.query(\"type == 'left_hand'\")\nface'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.086058Z","iopub.status.idle":"2023-04-16T00:03:35.086759Z","shell.execute_reply.started":"2023-04-16T00:03:35.086392Z","shell.execute_reply":"2023-04-16T00:03:35.086430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''data = load_relevant_data_subset(f\"/kaggle/input/asl-signs/train_landmark_files/32319/1005865446.parquet\")\ndata.shape'''\n","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.089003Z","iopub.status.idle":"2023-04-16T00:03:35.090078Z","shell.execute_reply.started":"2023-04-16T00:03:35.089676Z","shell.execute_reply":"2023-04-16T00:03:35.089723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''import tflite_runtime.interpreter as tflite\ninterpreter = tflite.Interpreter(model_path)\n\nfound_signatures = list(interpreter.get_signature_list().keys())\n\nif REQUIRED_SIGNATURE not in found_signatures:\n    raise KernelEvalException('Required input signature not found.')\n\nprediction_fn = interpreter.get_signature_runner(\"serving_default\")\noutput = prediction_fn(inputs=frames)\nsign = np.argmax(output[\"outputs\"])'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.091907Z","iopub.status.idle":"2023-04-16T00:03:35.092938Z","shell.execute_reply.started":"2023-04-16T00:03:35.092575Z","shell.execute_reply":"2023-04-16T00:03:35.092611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Try this \n'''import numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Input, Dense, Concatenate\nfrom tensorflow.keras.layers.experimental.preprocessing import Normalization, CategoryEncoding, IntegerLookup\nfrom tensorflow.keras.models import Model\n\n# Generate random structured data\nbatch_size = 32\nnum_features = 8\nnum_categories = 4\nnum_classes = 3\n\ncontinuous_features = np.random.rand(batch_size, num_features)\ncategorical_features = np.random.randint(0, num_categories, size=(batch_size, 1))\n\n# Define the Keras model using functional API\ncontinuous_input = Input(shape=(num_features,), name='continuous_input')\ncategorical_input = Input(shape=(1,), name='categorical_input')\n\n# Normalize continuous features using Normalization layer\nnormalizer = Normalization()\nnormalizer.adapt(continuous_features)\nnormalized_continuous_input = normalizer(continuous_input)\n\n# Encode categorical features using IntegerLookup and CategoryEncoding layers\ninteger_lookup = IntegerLookup(output_mode='binary')\ninteger_lookup.adapt(categorical_features)\nencoded_categorical_input = integer_lookup(categorical_input)\n\n# Concatenate the processed features\nconcatenated_features = Concatenate()([normalized_continuous_input, encoded_categorical_input])\n\n# Add Dense layers\ndense_layer_1 = Dense(units=64, activation='relu')(concatenated_features)\noutput_layer = Dense(units=num_classes, activation='softmax')(dense_layer_1)\n\n# Create the model\nmodel = Model(inputs=[continuous_input, categorical_input], outputs=output_layer)\n\n# Process the structured data through the model\noutput_data = model([continuous_features, categorical_features])\n\n# Check the shape of the output data\nprint(\"Continuous input shape:\", continuous_features.shape)  # (32, 8)\nprint(\"Categorical input shape:\", categorical_features.shape)  # (32, 1)\nprint(\"Output shape:\", output_data.shape)  # (32, 3)\n'''","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:03:35.094815Z","iopub.status.idle":"2023-04-16T00:03:35.095799Z","shell.execute_reply.started":"2023-04-16T00:03:35.095468Z","shell.execute_reply":"2023-04-16T00:03:35.095502Z"},"trusted":true},"execution_count":null,"outputs":[]}]}