{"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":"# 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)\n\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\nfor 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-06-14T08:44:26.993488Z","iopub.execute_input":"2023-06-14T08:44:26.994478Z","iopub.status.idle":"2023-06-14T08:44:27.053803Z","shell.execute_reply.started":"2023-06-14T08:44:26.994442Z","shell.execute_reply":"2023-06-14T08:44:27.052568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf = pd.read_csv(\"/kaggle/input/asl-fingerspelling/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:47:21.607941Z","iopub.execute_input":"2023-06-14T08:47:21.609112Z","iopub.status.idle":"2023-06-14T08:47:21.718657Z","shell.execute_reply.started":"2023-06-14T08:47:21.609058Z","shell.execute_reply":"2023-06-14T08:47:21.717678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.columns","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:47:52.443726Z","iopub.execute_input":"2023-06-14T08:47:52.444156Z","iopub.status.idle":"2023-06-14T08:47:52.452837Z","shell.execute_reply.started":"2023-06-14T08:47:52.444121Z","shell.execute_reply":"2023-06-14T08:47:52.451741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, optimizers, constraints, regularizers\nimport numpy as np\nimport pandas as pd\nimport json\n\nprint(\"Tensorflow\", tf.__version__)\n!python --version\n\nbasedir = \"/kaggle/working/\"\nNUM_CHARACTERS = 59","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:52:07.594125Z","iopub.execute_input":"2023-06-14T08:52:07.59456Z","iopub.status.idle":"2023-06-14T08:52:16.594517Z","shell.execute_reply.started":"2023-06-14T08:52:07.594528Z","shell.execute_reply":"2023-06-14T08:52:16.593223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/train.csv')\ndf[\"phrase_bytes\"] = df[\"phrase\"].map(lambda x: x.encode(\"utf-8\"))\ndisplay(df.head())","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:52:48.048852Z","iopub.execute_input":"2023-06-14T08:52:48.050429Z","iopub.status.idle":"2023-06-14T08:52:48.215207Z","shell.execute_reply.started":"2023-06-14T08:52:48.050389Z","shell.execute_reply":"2023-06-14T08:52:48.2143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/asl-fingerspelling/train.csv')\n\nSEL_FEATURES = ['x_right_hand_0','y_right_hand_0']\n\nc2p = json.load(open('/kaggle/input/asl-fingerspelling/character_to_prediction_index.json', 'r'))\np2c = {p: c for c, p in c2p.items()}","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:53:16.532178Z","iopub.execute_input":"2023-06-14T08:53:16.532574Z","iopub.status.idle":"2023-06-14T08:53:16.650141Z","shell.execute_reply.started":"2023-06-14T08:53:16.532546Z","shell.execute_reply":"2023-06-14T08:53:16.649123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from itertools import permutations\n\nally = df_train['phrase'].values\ntotaly = sum([len(y) for y in ally])\n\ndef eval_string(s):\n    d = 0\n    for y in ally:\n        d += distance(s, y)\n    return (totaly - d) / totaly\n\nbest_str = ''\nbest_score = 0\nchars = list(c2p.keys())\n\nfor i in range(20):  # max length\n    inner_best = best_str\n    inner_best_score = best_score\n\n    for position in range(len(best_str) + 1):  # at all insertion points\n        for newchar in chars:  # try all characters\n            new_str = best_str[:position] + str(newchar) + best_str[position:]\n            score = eval_string(new_str)\n\n            if score > inner_best_score:\n                inner_best = new_str\n                inner_best_score = score\n                print(f'New best @ {len(inner_best)}=\"{inner_best}\", score {inner_best_score:.4f}')\n\n    if best_score >= inner_best_score:\n        print('No improvement, best is', best_str)\n        break\n\n    best_str = inner_best\n    best_score = inner_best_score\n    print(f'Best str @ {len(best_str)}=\"{best_str}\", score {best_score:.4f}')\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T08:58:24.041857Z","iopub.execute_input":"2023-06-14T08:58:24.042273Z","iopub.status.idle":"2023-06-14T09:03:05.967398Z","shell.execute_reply.started":"2023-06-14T08:58:24.042216Z","shell.execute_reply":"2023-06-14T09:03:05.966282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inpdir = \"/kaggle/input/asl-fingerspelling\"\ndf = pd.read_csv(f'{inpdir}/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T09:11:12.114326Z","iopub.execute_input":"2023-06-14T09:11:12.114731Z","iopub.status.idle":"2023-06-14T09:11:12.228367Z","shell.execute_reply.started":"2023-06-14T09:11:12.114701Z","shell.execute_reply":"2023-06-14T09:11:12.22721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"const_pred = np.zeros((len(best_str), 59))\nfor i, c in enumerate(best_str):\n    const_pred[i, c2p[c]] = 1","metadata":{"execution":{"iopub.status.busy":"2023-06-14T09:11:16.347638Z","iopub.execute_input":"2023-06-14T09:11:16.348296Z","iopub.status.idle":"2023-06-14T09:11:16.352845Z","shell.execute_reply.started":"2023-06-14T09:11:16.34825Z","shell.execute_reply":"2023-06-14T09:11:16.351944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Layer, Input, Dense\n\nclass ConstantLayer(Layer):\n    def __init__(self, constant_vector, name=None):\n        super(ConstantLayer, self).__init__(name=name)\n        self.constant_vector = constant_vector\n\n    def build(self, input_shape):\n        self.constant_layer = Dense(1, activation='linear', trainable=False, use_bias=False,\n                                     kernel_initializer=tf.constant_initializer(self.constant_vector))\n        super(ConstantLayer, self).build(input_shape)\n\n    def call(self, inputs):\n        return self.constant_layer(inputs)\n\n# Create the input layer\ninput_layer = Input(shape=(len(SEL_FEATURES),), name='inputs')\n\n# Specify your constant vector\nconst_pred = 10.0  # Replace with your desired constant value\n\n# Create the output layer\noutput_layer = ConstantLayer(const_pred, name='outputs')(input_layer)\n\n# Create the model\nmodel = tf.keras.models.Model(inputs=input_layer, outputs=output_layer)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T09:14:34.550335Z","iopub.execute_input":"2023-06-14T09:14:34.550796Z","iopub.status.idle":"2023-06-14T09:14:34.620849Z","shell.execute_reply.started":"2023-06-14T09:14:34.550761Z","shell.execute_reply":"2023-06-14T09:14:34.61971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.keras.layers import Layer, Input\n\nclass ConstantLayer(Layer):\n    def __init__(self, constant_vector, name=None):\n        super(ConstantLayer, self).__init__(name=name)\n        self.constant_vector = tf.Variable(initial_value=constant_vector, trainable=False, dtype=tf.float32)\n\n    def call(self, inputs):\n        return self.constant_vector","metadata":{"execution":{"iopub.status.busy":"2023-06-14T09:14:51.272222Z","iopub.execute_input":"2023-06-14T09:14:51.27269Z","iopub.status.idle":"2023-06-14T09:14:51.280034Z","shell.execute_reply.started":"2023-06-14T09:14:51.272653Z","shell.execute_reply":"2023-06-14T09:14:51.279004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nimport zipfile\n\n# Convert the model to TensorFlow Lite format\ntflite_model = tf.lite.TFLiteConverter.from_keras_model(model).convert()\n\n# Save the TensorFlow Lite model to a file\nmodel_path = 'model.tflite'\nwith open(model_path, 'wb') as f:\n    f.write(tflite_model)\n\n# Save inference arguments as a JSON file\ninference_args = {\n    'input_size': len(SEL_FEATURES)\n}\ninference_args_path = 'inference_args.json'\nwith open(inference_args_path, 'w') as f:\n    json.dump(inference_args, f)\n\n# Zip the model and other files\nzip_path = 'submission.zip'\nwith zipfile.ZipFile(zip_path, 'w') as zf:\n    zf.write(model_path)\n    zf.write(inference_args_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-14T09:15:50.538691Z","iopub.execute_input":"2023-06-14T09:15:50.53911Z","iopub.status.idle":"2023-06-14T09:15:51.016786Z","shell.execute_reply.started":"2023-06-14T09:15:50.539076Z","shell.execute_reply":"2023-06-14T09:15:51.015605Z"},"trusted":true},"execution_count":null,"outputs":[]}]}