{"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":"print(\"\\n...PIP INSTALL STARTING...\\n\")\n!pip install -q --upgrade tensorflow-io\ntry:\n    import mediapipe as mp\nexcept:\n    !pip install -q mediapipe\n    import mediapipe as mp\nprint(\"\\n...IMPORTS STARTING...\\n\")\nprint(\"\\n\\tVERSION INFORMATION\")\n\nimport tensorflow as tf;print(f\"\\t\\t- TENSORFLOW VERSION:{tf.__version__}\");\nimport tensorflow_io as tfio; print(f\"\\t\\t- TENSORFLOW-IO VERSION:{tfio.__version__}\");\nimport pandas as pd;pd.options.mode.chained_assignment=None;pd.set_option('display.max_columns',None)##没有后面这一串就会有warning??\nimport numpy as np;print(f\"\\t\\t- NUMPY VERSION:{np.__version__}\");\nimport sklearn; print(f\"\\t\\t- SKLEARN VERSION:{sklearn.__version__}\");\nimport sklearn.model_selection\n\nfrom collections import Counter\nfrom datetime import datetime\nfrom zipfile import ZipFile\nimport sklearn \nimport zipfile\nimport random\nimport json\nimport math\nimport time\nimport os\nimport gc\n\nfrom tqdm.notebook import tqdm; tqdm.pandas();\n\ndef seed_it_all(seed=7):\n    \"\"\" Attempt to be Reproducible \"\"\"\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n    tf.random.set_seed(seed)\n\nseed_it_all()\n\nprint(\"\\n\\n... IMPORTS COMPLETE ...\\n\")\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:11:13.105423Z","iopub.execute_input":"2023-07-02T12:11:13.105761Z","iopub.status.idle":"2023-07-02T12:11:55.262900Z","shell.execute_reply.started":"2023-07-02T12:11:13.105728Z","shell.execute_reply":"2023-07-02T12:11:55.261358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-05-01T12:15:46.819994Z","iopub.execute_input":"2023-05-01T12:15:46.820556Z","iopub.status.idle":"2023-05-01T12:15:46.968842Z","shell.execute_reply.started":"2023-05-01T12:15:46.820482Z","shell.execute_reply":"2023-05-01T12:15:46.967538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##跳过了一些看起来大概是visualization的代码","metadata":{"execution":{"iopub.status.busy":"2023-05-01T01:38:19.884598Z","iopub.execute_input":"2023-05-01T01:38:19.885086Z","iopub.status.idle":"2023-05-01T01:38:19.891654Z","shell.execute_reply.started":"2023-05-01T01:38:19.885031Z","shell.execute_reply":"2023-05-01T01:38:19.890337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def flatten_l_o_l(nested_list):\n    return [item for sublist in nested_list for item in sublist]\n\ndef print_ln(symbol='-',line_len=110,newline_before=False,newline_after=False):\n    if newline_before:print();\n    print(symbol*line_len)\n    if newline_after: print();\n\ndef read_json_file(file_path):\n    try:\n        with open(file_path,'r') as file:\n            json_data=json.load(file)\n        return json_data\n    except FileNotFoundError:\n        raise FileNotFoundError(f\"File not found:{file_path}\")\n    except ValueError:\n        raise ValueError(f\"Invalid JSON data in file:{file_path}\")\n\ndef get_sign_df(pd_path,invert_y=True):\n    sign_df=pd.read_parquet(pq_path)\n    \n    if invert_y:sign_df[\"y\"]*=-1 ##??啥invert？\n    \n    return sign_df","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:13:15.085247Z","iopub.execute_input":"2023-07-02T12:13:15.086181Z","iopub.status.idle":"2023-07-02T12:13:15.096960Z","shell.execute_reply.started":"2023-07-02T12:13:15.086140Z","shell.execute_reply":"2023-07-02T12:13:15.095882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROWS_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-07-02T12:13:17.486822Z","iopub.execute_input":"2023-07-02T12:13:17.487836Z","iopub.status.idle":"2023-07-02T12:13:17.495501Z","shell.execute_reply.started":"2023-07-02T12:13:17.487794Z","shell.execute_reply":"2023-07-02T12:13:17.494123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROWS_PER_FRAME=543 #?543怎么得来的\n\n\ndef load_relevant_data_subset(pq_path):\n    data_columns=['x','y','z']\n    data=pd.read_parquet(pd_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_columnd))\n    return data.astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:13:20.460932Z","iopub.execute_input":"2023-07-02T12:13:20.461599Z","iopub.status.idle":"2023-07-02T12:13:20.468581Z","shell.execute_reply.started":"2023-07-02T12:13:20.461558Z","shell.execute_reply":"2023-07-02T12:13:20.467110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR=\"/kaggle/input/asl-signs\"\n\nprint(\"\\n...BASIC DATA SETUP STARTING...\\n\")\nprint(\"\\n\\n...LOAD TRAIN DATAFRAM...\\n\")\n\ntrain_df=pd.read_csv(os.path.join(DATA_DIR,\"train.csv\"))\ntrain_df['path']=DATA_DIR+\"/\"+train_df['path']\ndisplay(train_df)\n\nprint(\"\\n\\n...LOAD SIGN TO PREDICTION INDEX MAP...\\n\")\n\ns2p_map={k.lower():v for k,v in read_json_file(os.path.join(DATA_DIR,\"sign_to_prediction_index_map.json\")).items()}\np2s_map={v:k for k,v in read_json_file(os.path.join(DATA_DIR,\"sign_to_prediction_index_map.json\")).items()}\nencoder=lambda x: s2p_map.get(x.lower())\ndecoder=lambda x: p2s_map.get(x)\n","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:13:23.444400Z","iopub.execute_input":"2023-07-02T12:13:23.445273Z","iopub.status.idle":"2023-07-02T12:13:23.678579Z","shell.execute_reply.started":"2023-07-02T12:13:23.445228Z","shell.execute_reply":"2023-07-02T12:13:23.677520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_sample=True","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:13:27.374561Z","iopub.execute_input":"2023-07-02T12:13:27.375284Z","iopub.status.idle":"2023-07-02T12:13:27.380183Z","shell.execute_reply.started":"2023-07-02T12:13:27.375241Z","shell.execute_reply":"2023-07-02T12:13:27.378889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X=np.load(\"/kaggle/input/gislr-feature-data-on-the-shoulders/feature_data.npy\").astype(np.float32)\nY=np.load(\"/kaggle/input/gislr-feature-data-on-the-shoulders/feature_labels.npy\").astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:13:29.518414Z","iopub.execute_input":"2023-07-02T12:13:29.519080Z","iopub.status.idle":"2023-07-02T12:13:58.456358Z","shell.execute_reply.started":"2023-07-02T12:13:29.519037Z","shell.execute_reply":"2023-07-02T12:13:58.455173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sub_sampling(x,y):\n    idx=np.random.randint(0,x.shape[0],1000)\n    return x[idx],y[idx]","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:35:32.957992Z","iopub.execute_input":"2023-07-02T12:35:32.959009Z","iopub.status.idle":"2023-07-02T12:35:32.965203Z","shell.execute_reply.started":"2023-07-02T12:35:32.958954Z","shell.execute_reply":"2023-07-02T12:35:32.963768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if sub_sampling:\n    x_train,y_train=sub_sampling(X,Y)\nelse:\n    x_train,y_train=X,Y","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:35:53.397161Z","iopub.execute_input":"2023-07-02T12:35:53.397945Z","iopub.status.idle":"2023-07-02T12:35:53.415415Z","shell.execute_reply.started":"2023-07-02T12:35:53.397902Z","shell.execute_reply":"2023-07-02T12:35:53.414304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,y_train=X,Y","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:35:56.554166Z","iopub.execute_input":"2023-07-02T12:35:56.554937Z","iopub.status.idle":"2023-07-02T12:35:56.560619Z","shell.execute_reply.started":"2023-07-02T12:35:56.554893Z","shell.execute_reply":"2023-07-02T12:35:56.559394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_val,y_train,y_val=sklearn.model_selection.train_test_split(x_train,y_train,test_size=0.1,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:36:28.595025Z","iopub.execute_input":"2023-07-02T12:36:28.595511Z","iopub.status.idle":"2023-07-02T12:36:29.336647Z","shell.execute_reply.started":"2023-07-02T12:36:28.595465Z","shell.execute_reply":"2023-07-02T12:36:29.335383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.shape(y_train)","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:36:31.692620Z","iopub.execute_input":"2023-07-02T12:36:31.693122Z","iopub.status.idle":"2023-07-02T12:36:31.702292Z","shell.execute_reply.started":"2023-07-02T12:36:31.693076Z","shell.execute_reply":"2023-07-02T12:36:31.700976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model():\n    inputs = tf.keras.Input((5796), dtype=tf.float32)\n    vector = tf.keras.layers.Dense(1024)(inputs)\n    vector = tf.keras.layers.BatchNormalization()(vector)\n    vector = tf.keras.layers.Activation(\"gelu\")(vector)\n    vector = tf.keras.layers.Dropout(0.1)(vector)\n    vector = tf.keras.layers.Dense(64, activation=\"swish\")(vector)\n    vector = tf.keras.layers.Dense(512, activation=\"swish\")(vector)\n    vector = tf.keras.layers.BatchNormalization()(vector)\n    vector = tf.keras.layers.Activation(\"gelu\")(vector)\n    vector = tf.keras.layers.Dense(16, activation=\"swish\")(vector)\n    vector = tf.keras.layers.Dropout(0.1)(vector)\n    vector = tf.keras.layers.Flatten()(vector)\n    output = tf.keras.layers.Dense(250, activation=\"softmax\")(vector)\n    model = tf.keras.Model(inputs=inputs, outputs=output)\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=1e-2),\n        loss=tf.keras.losses.SparseCategoricalCrossentropy(), \n        metrics=[\n            \"accuracy\", \n            tf.keras.metrics.SparseTopKCategoricalAccuracy(k=5, name=\"top-5-accuracy\"),\n            tf.keras.metrics.SparseTopKCategoricalAccuracy(k=10, name=\"top-10-accuracy\")\n        ]\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:36:46.737714Z","iopub.execute_input":"2023-07-02T12:36:46.738215Z","iopub.status.idle":"2023-07-02T12:36:46.754548Z","shell.execute_reply.started":"2023-07-02T12:36:46.738172Z","shell.execute_reply":"2023-07-02T12:36:46.753228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = \"model\"\nmodel = get_model()\ncallbacks = [\n    tf.keras.callbacks.ModelCheckpoint(model_name, save_best_only=True, restore_best_weights=True, monitor=\"val_accuracy\", mode=\"max\")\n]\nmodel.fit(x_train, y_train, epochs=100, validation_data=(x_val, y_val), batch_size=126, callbacks=callbacks)\nmodel.load_weights(model_name)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-02T12:36:52.197532Z","iopub.execute_input":"2023-07-02T12:36:52.197947Z","iopub.status.idle":"2023-07-02T12:47:23.385126Z","shell.execute_reply.started":"2023-07-02T12:36:52.197910Z","shell.execute_reply":"2023-07-02T12:47:23.384134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"./models/DNN_model\")","metadata":{"execution":{"iopub.status.busy":"2023-05-01T21:44:26.668378Z","iopub.execute_input":"2023-05-01T21:44:26.668769Z","iopub.status.idle":"2023-05-01T21:44:28.840267Z","shell.execute_reply.started":"2023-05-01T21:44:26.668732Z","shell.execute_reply":"2023-05-01T21:44:28.839089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_inference_model(model):\n    inputs = tf.keras.Input((5796), dtype=tf.float32, name=\"inputs\")\n    x = tf.where(tf.math.is_nan(inputs), tf.zeros_like(inputs), inputs)\n    x = tf.reduce_mean(x, axis=0, keepdims=True)\n    x = model(x)\n    output = tf.keras.layers.Activation(activation=\"linear\", name=\"outputs\")(x)\n    inference_model = tf.keras.Model(inputs=inputs, outputs=output) \n    inference_model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=[\"accuracy\"])\n    return inference_model","metadata":{"execution":{"iopub.status.busy":"2023-05-01T21:56:42.057662Z","iopub.execute_input":"2023-05-01T21:56:42.058128Z","iopub.status.idle":"2023-05-01T21:56:42.067076Z","shell.execute_reply.started":"2023-05-01T21:56:42.058091Z","shell.execute_reply":"2023-05-01T21:56:42.065276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inference_model = get_inference_model(model)\ninference_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-01T21:56:44.155213Z","iopub.execute_input":"2023-05-01T21:56:44.155690Z","iopub.status.idle":"2023-05-01T21:56:44.292044Z","shell.execute_reply.started":"2023-05-01T21:56:44.155638Z","shell.execute_reply":"2023-05-01T21:56:44.290581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# converter = tf.lite.TFLiteConverter.from_keras_model(inference_model)\n# tflite_model = converter.convert()\n# model_path = \"model.tflite\"\n# # Save the model.\n# with open(model_path, 'wb') as f:\n#     f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2023-05-01T21:57:31.009285Z","iopub.execute_input":"2023-05-01T21:57:31.009769Z","iopub.status.idle":"2023-05-01T21:57:36.427982Z","shell.execute_reply.started":"2023-05-01T21:57:31.009714Z","shell.execute_reply":"2023-05-01T21:57:36.426947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!zip submission.zip $model_path","metadata":{"execution":{"iopub.status.busy":"2023-05-01T21:57:56.044046Z","iopub.execute_input":"2023-05-01T21:57:56.045140Z","iopub.status.idle":"2023-05-01T21:57:59.511016Z","shell.execute_reply.started":"2023-05-01T21:57:56.045096Z","shell.execute_reply":"2023-05-01T21:57:59.509929Z"},"trusted":true},"execution_count":null,"outputs":[]}]}