{ "cells": [ { "cell_type": "markdown", "source": [ "# Introduction\n", "This notebook is used to test different ways to deploy the feature extraction model to the huggingface hub. The goal is to find the best way to deploy the model so that it can be used in the inference API and can be easy accessible for user. In the best way it would also be possible to simply use the huggingface library directly. The following methods will be tested:\n", "\n", "1. [Using ``timm`` to extract features](#Using-timm-to-extract-features) -> ❌\n", "2. [Using ``transformers`` to extract features](#Using-transformers-to-extract-features) --> ❌\n", " 1. [Feature extraction task](#Feature-extraction-task)\n", " 2. [``AutoModel``](#AutoModel)\n", " 3. [Batched feature extraction](#Batched-feature-extraction)\n", "3. [Using simple download](#Using-simple-download) -> ✅\n", "4. [Using custom model](#Using-custom-model) --> 🚧\n", "\n", "**Helpful links and resources**\n", "- https://huggingface.co/docs/transformers/custom_models - Alternative creating custom models\n", "- https://huggingface.co/templates/feature-extraction - Template for inference API\n", "- https://huggingface-widgets.netlify.app/ - Widgets for visualizing models in inference API\n", "- https://huggingface.co/docs/hub/models-widgets#how-can-i-control-my-models-widget-inference-api-parameters - Controlling inference API parameters" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "# Imports" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 12, "outputs": [], "source": [ "import timm\n", "import torch\n", "from pipeline_wrapper import MyPipeline" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T08:30:05.075994Z", "start_time": "2024-02-15T08:29:59.741405700Z" } } }, { "cell_type": "markdown", "source": [ "# 1. Using ``timm`` to extract features" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 10, "outputs": [ { "ename": "NameError", "evalue": "name 'torch' is not defined", "output_type": "error", "traceback": [ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[1;31mNameError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[10], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m test_tensor \u001B[38;5;241m=\u001B[39m \u001B[43mtorch\u001B[49m\u001B[38;5;241m.\u001B[39mrandn(\u001B[38;5;241m2\u001B[39m, \u001B[38;5;241m3\u001B[39m, \u001B[38;5;241m1\u001B[39m, \u001B[38;5;241m1\u001B[39m)\n", "\u001B[1;31mNameError\u001B[0m: name 'torch' is not defined" ] } ], "source": [ "test_tensor = torch.randn(2, 3, 1, 1)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T08:29:09.423837500Z", "start_time": "2024-02-15T08:29:09.397259300Z" } } }, { "cell_type": "code", "execution_count": 31, "outputs": [], "source": [ "feature_extractor = timm.create_model('resnet18', pretrained=True, num_classes=0, global_pool='')\n", "features = feature_extractor.forward_features(test_tensor)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T07:55:57.395514300Z", "start_time": "2024-02-15T07:55:56.561361800Z" } } }, { "cell_type": "code", "execution_count": 32, "outputs": [ { "data": { "text/plain": "tensor([[[[0.0000]],\n\n [[0.6944]],\n\n [[0.0000]],\n\n ...,\n\n [[0.0000]],\n\n [[0.0000]],\n\n [[0.0000]]],\n\n\n [[[0.0000]],\n\n [[0.0000]],\n\n [[0.0143]],\n\n ...,\n\n [[0.0000]],\n\n [[0.0000]],\n\n [[0.0000]]]], grad_fn=)" }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "features" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T07:55:58.303122200Z", "start_time": "2024-02-15T07:55:58.266709100Z" } } }, { "cell_type": "code", "execution_count": 15, "outputs": [ { "ename": "RuntimeError", "evalue": "Unknown model (ECG2HRV)", "output_type": "error", "traceback": [ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[1;31mRuntimeError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[15], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m feature_extractor \u001B[38;5;241m=\u001B[39m \u001B[43mtimm\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcreate_model\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mHUBII-Platform/ECG2HRV\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mpretrained\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;28;43;01mTrue\u001B[39;49;00m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnum_classes\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mglobal_pool\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\timm\\models\\_factory.py:113\u001B[0m, in \u001B[0;36mcreate_model\u001B[1;34m(model_name, pretrained, pretrained_cfg, pretrained_cfg_overlay, checkpoint_path, scriptable, exportable, no_jit, **kwargs)\u001B[0m\n\u001B[0;32m 110\u001B[0m pretrained_cfg \u001B[38;5;241m=\u001B[39m pretrained_tag\n\u001B[0;32m 112\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m is_model(model_name):\n\u001B[1;32m--> 113\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mRuntimeError\u001B[39;00m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mUnknown model (\u001B[39m\u001B[38;5;132;01m%s\u001B[39;00m\u001B[38;5;124m)\u001B[39m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m%\u001B[39m model_name)\n\u001B[0;32m 115\u001B[0m create_fn \u001B[38;5;241m=\u001B[39m model_entrypoint(model_name)\n\u001B[0;32m 116\u001B[0m \u001B[38;5;28;01mwith\u001B[39;00m set_layer_config(scriptable\u001B[38;5;241m=\u001B[39mscriptable, exportable\u001B[38;5;241m=\u001B[39mexportable, no_jit\u001B[38;5;241m=\u001B[39mno_jit):\n", "\u001B[1;31mRuntimeError\u001B[0m: Unknown model (ECG2HRV)" ] } ], "source": [ "feature_extractor = timm.create_model('HUBII-Platform/ECG2HRV', pretrained=True, num_classes=0, global_pool='')" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T08:30:32.648960900Z", "start_time": "2024-02-15T08:30:32.601345400Z" } } }, { "cell_type": "markdown", "source": [ "# 2. Using ``transformers`` to extract features" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "1. Feature extraction task" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 33, "outputs": [ { "data": { "text/plain": "config.json: 0%| | 0.00/1.72k [00:00 719\u001B[0m config_dict \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mcls\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_dict_from_json_file\u001B[49m\u001B[43m(\u001B[49m\u001B[43mresolved_config_file\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:818\u001B[0m, in \u001B[0;36mPretrainedConfig._dict_from_json_file\u001B[1;34m(cls, json_file)\u001B[0m\n\u001B[0;32m 817\u001B[0m text \u001B[38;5;241m=\u001B[39m reader\u001B[38;5;241m.\u001B[39mread()\n\u001B[1;32m--> 818\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mjson\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mloads\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtext\u001B[49m\u001B[43m)\u001B[49m\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\__init__.py:346\u001B[0m, in \u001B[0;36mloads\u001B[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001B[0m\n\u001B[0;32m 343\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m (\u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 344\u001B[0m parse_int \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m parse_float \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 345\u001B[0m parse_constant \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_pairs_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m kw):\n\u001B[1;32m--> 346\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_default_decoder\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdecode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 347\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:337\u001B[0m, in \u001B[0;36mJSONDecoder.decode\u001B[1;34m(self, s, _w)\u001B[0m\n\u001B[0;32m 333\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001B[39;00m\n\u001B[0;32m 334\u001B[0m \u001B[38;5;124;03mcontaining a JSON document).\u001B[39;00m\n\u001B[0;32m 335\u001B[0m \n\u001B[0;32m 336\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m--> 337\u001B[0m obj, end \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mraw_decode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43midx\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m_w\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mend\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 338\u001B[0m end \u001B[38;5;241m=\u001B[39m _w(s, end)\u001B[38;5;241m.\u001B[39mend()\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:355\u001B[0m, in \u001B[0;36mJSONDecoder.raw_decode\u001B[1;34m(self, s, idx)\u001B[0m\n\u001B[0;32m 354\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mStopIteration\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[1;32m--> 355\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m JSONDecodeError(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mExpecting value\u001B[39m\u001B[38;5;124m\"\u001B[39m, s, err\u001B[38;5;241m.\u001B[39mvalue) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m 356\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m obj, end\n", "\u001B[1;31mJSONDecodeError\u001B[0m: Expecting value: line 1 column 1 (char 0)", "\nDuring handling of the above exception, another exception occurred:\n", "\u001B[1;31mOSError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[7], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m feature_extractor \u001B[38;5;241m=\u001B[39m \u001B[43mpipeline\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mfeature-extraction\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmodel\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mHUBII-Platform/ECG2HRV\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\pipelines\\__init__.py:782\u001B[0m, in \u001B[0;36mpipeline\u001B[1;34m(task, model, config, tokenizer, feature_extractor, image_processor, framework, revision, use_fast, token, device, device_map, torch_dtype, trust_remote_code, model_kwargs, pipeline_class, **kwargs)\u001B[0m\n\u001B[0;32m 779\u001B[0m adapter_config \u001B[38;5;241m=\u001B[39m json\u001B[38;5;241m.\u001B[39mload(f)\n\u001B[0;32m 780\u001B[0m model \u001B[38;5;241m=\u001B[39m adapter_config[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mbase_model_name_or_path\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[1;32m--> 782\u001B[0m config \u001B[38;5;241m=\u001B[39m AutoConfig\u001B[38;5;241m.\u001B[39mfrom_pretrained(\n\u001B[0;32m 783\u001B[0m model, _from_pipeline\u001B[38;5;241m=\u001B[39mtask, code_revision\u001B[38;5;241m=\u001B[39mcode_revision, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mhub_kwargs, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mmodel_kwargs\n\u001B[0;32m 784\u001B[0m )\n\u001B[0;32m 785\u001B[0m hub_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m config\u001B[38;5;241m.\u001B[39m_commit_hash\n\u001B[0;32m 787\u001B[0m custom_tasks \u001B[38;5;241m=\u001B[39m {}\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\models\\auto\\configuration_auto.py:1100\u001B[0m, in \u001B[0;36mAutoConfig.from_pretrained\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 1097\u001B[0m trust_remote_code \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtrust_remote_code\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[0;32m 1098\u001B[0m code_revision \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcode_revision\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[1;32m-> 1100\u001B[0m config_dict, unused_kwargs \u001B[38;5;241m=\u001B[39m PretrainedConfig\u001B[38;5;241m.\u001B[39mget_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 1101\u001B[0m has_remote_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mAutoConfig\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m 1102\u001B[0m has_local_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;129;01min\u001B[39;00m CONFIG_MAPPING\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:634\u001B[0m, in \u001B[0;36mPretrainedConfig.get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 632\u001B[0m original_kwargs \u001B[38;5;241m=\u001B[39m copy\u001B[38;5;241m.\u001B[39mdeepcopy(kwargs)\n\u001B[0;32m 633\u001B[0m \u001B[38;5;66;03m# Get config dict associated with the base config file\u001B[39;00m\n\u001B[1;32m--> 634\u001B[0m config_dict, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m_get_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 635\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict:\n\u001B[0;32m 636\u001B[0m original_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:722\u001B[0m, in \u001B[0;36mPretrainedConfig._get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n\u001B[0;32m 721\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (json\u001B[38;5;241m.\u001B[39mJSONDecodeError, \u001B[38;5;167;01mUnicodeDecodeError\u001B[39;00m):\n\u001B[1;32m--> 722\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mEnvironmentError\u001B[39;00m(\n\u001B[0;32m 723\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIt looks like the config file at \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m is not a valid JSON file.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 724\u001B[0m )\n\u001B[0;32m 726\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_local:\n\u001B[0;32m 727\u001B[0m logger\u001B[38;5;241m.\u001B[39minfo(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mloading configuration file \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n", "\u001B[1;31mOSError\u001B[0m: It looks like the config file at 'C:\\Users\\merti\\.cache\\huggingface\\hub\\models--HUBII-Platform--ECG2HRV\\snapshots\\75f67e01de12e33cfb05cfbfed35ff621246b3f9\\config.json' is not a valid JSON file." ] } ], "source": [ "feature_extractor = pipeline(\"feature-extraction\", model = 'HUBII-Platform/ECG2HRV')" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T08:25:26.931190300Z", "start_time": "2024-02-15T08:25:25.877444800Z" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "PIPELINE_REGISTRY.register_pipeline(\n", " \"ecg2hrv\",\n", " pipeline_class=MyPipeline,\n", " # model_class=MyModel\n", ")\n", "feature_extractor = pipeline(\"ecg2hrv\")" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "feature_extractor = pipeline(\"ecg2hrv\", model=\"HUBII-Platform/ECG2HRV\")" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "2. ``AutoModel``" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 2, "outputs": [ { "ename": "OSError", "evalue": "It looks like the config file at 'C:\\Users\\merti\\.cache\\huggingface\\hub\\models--HUBII-Platform--ECG2HRV\\snapshots\\75f67e01de12e33cfb05cfbfed35ff621246b3f9\\config.json' is not a valid JSON file.", "output_type": "error", "traceback": [ "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m", "\u001B[1;31mJSONDecodeError\u001B[0m Traceback (most recent call last)", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:719\u001B[0m, in \u001B[0;36mPretrainedConfig._get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 717\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m 718\u001B[0m \u001B[38;5;66;03m# Load config dict\u001B[39;00m\n\u001B[1;32m--> 719\u001B[0m config_dict \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mcls\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_dict_from_json_file\u001B[49m\u001B[43m(\u001B[49m\u001B[43mresolved_config_file\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:818\u001B[0m, in \u001B[0;36mPretrainedConfig._dict_from_json_file\u001B[1;34m(cls, json_file)\u001B[0m\n\u001B[0;32m 817\u001B[0m text \u001B[38;5;241m=\u001B[39m reader\u001B[38;5;241m.\u001B[39mread()\n\u001B[1;32m--> 818\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43mjson\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mloads\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtext\u001B[49m\u001B[43m)\u001B[49m\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\__init__.py:346\u001B[0m, in \u001B[0;36mloads\u001B[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001B[0m\n\u001B[0;32m 343\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m (\u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 344\u001B[0m parse_int \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m parse_float \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m\n\u001B[0;32m 345\u001B[0m parse_constant \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m object_pairs_hook \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m kw):\n\u001B[1;32m--> 346\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[43m_default_decoder\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mdecode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 347\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mcls\u001B[39m \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:337\u001B[0m, in \u001B[0;36mJSONDecoder.decode\u001B[1;34m(self, s, _w)\u001B[0m\n\u001B[0;32m 333\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001B[39;00m\n\u001B[0;32m 334\u001B[0m \u001B[38;5;124;03mcontaining a JSON document).\u001B[39;00m\n\u001B[0;32m 335\u001B[0m \n\u001B[0;32m 336\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m--> 337\u001B[0m obj, end \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mraw_decode\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43midx\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m_w\u001B[49m\u001B[43m(\u001B[49m\u001B[43ms\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mend\u001B[49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m 338\u001B[0m end \u001B[38;5;241m=\u001B[39m _w(s, end)\u001B[38;5;241m.\u001B[39mend()\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\json\\decoder.py:355\u001B[0m, in \u001B[0;36mJSONDecoder.raw_decode\u001B[1;34m(self, s, idx)\u001B[0m\n\u001B[0;32m 354\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mStopIteration\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[1;32m--> 355\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m JSONDecodeError(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mExpecting value\u001B[39m\u001B[38;5;124m\"\u001B[39m, s, err\u001B[38;5;241m.\u001B[39mvalue) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m\n\u001B[0;32m 356\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m obj, end\n", "\u001B[1;31mJSONDecodeError\u001B[0m: Expecting value: line 1 column 1 (char 0)", "\nDuring handling of the above exception, another exception occurred:\n", "\u001B[1;31mOSError\u001B[0m Traceback (most recent call last)", "Cell \u001B[1;32mIn[2], line 3\u001B[0m\n\u001B[0;32m 1\u001B[0m \u001B[38;5;66;03m# Example with AutoModel\u001B[39;00m\n\u001B[0;32m 2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtransformers\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m AutoTokenizer, AutoModel\n\u001B[1;32m----> 3\u001B[0m model \u001B[38;5;241m=\u001B[39m \u001B[43mAutoModel\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mfrom_pretrained\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mHUBII-Platform/ECG2HRV\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\models\\auto\\auto_factory.py:526\u001B[0m, in \u001B[0;36m_BaseAutoModelClass.from_pretrained\u001B[1;34m(cls, pretrained_model_name_or_path, *model_args, **kwargs)\u001B[0m\n\u001B[0;32m 523\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m kwargs\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mquantization_config\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m) \u001B[38;5;129;01mis\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m \u001B[38;5;28;01mNone\u001B[39;00m:\n\u001B[0;32m 524\u001B[0m _ \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mquantization_config\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m--> 526\u001B[0m config, kwargs \u001B[38;5;241m=\u001B[39m AutoConfig\u001B[38;5;241m.\u001B[39mfrom_pretrained(\n\u001B[0;32m 527\u001B[0m pretrained_model_name_or_path,\n\u001B[0;32m 528\u001B[0m return_unused_kwargs\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m,\n\u001B[0;32m 529\u001B[0m trust_remote_code\u001B[38;5;241m=\u001B[39mtrust_remote_code,\n\u001B[0;32m 530\u001B[0m code_revision\u001B[38;5;241m=\u001B[39mcode_revision,\n\u001B[0;32m 531\u001B[0m _commit_hash\u001B[38;5;241m=\u001B[39mcommit_hash,\n\u001B[0;32m 532\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mhub_kwargs,\n\u001B[0;32m 533\u001B[0m \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs,\n\u001B[0;32m 534\u001B[0m )\n\u001B[0;32m 536\u001B[0m \u001B[38;5;66;03m# if torch_dtype=auto was passed here, ensure to pass it on\u001B[39;00m\n\u001B[0;32m 537\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m kwargs_orig\u001B[38;5;241m.\u001B[39mget(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtorch_dtype\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m) \u001B[38;5;241m==\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto\u001B[39m\u001B[38;5;124m\"\u001B[39m:\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\models\\auto\\configuration_auto.py:1100\u001B[0m, in \u001B[0;36mAutoConfig.from_pretrained\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 1097\u001B[0m trust_remote_code \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mtrust_remote_code\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[0;32m 1098\u001B[0m code_revision \u001B[38;5;241m=\u001B[39m kwargs\u001B[38;5;241m.\u001B[39mpop(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mcode_revision\u001B[39m\u001B[38;5;124m\"\u001B[39m, \u001B[38;5;28;01mNone\u001B[39;00m)\n\u001B[1;32m-> 1100\u001B[0m config_dict, unused_kwargs \u001B[38;5;241m=\u001B[39m PretrainedConfig\u001B[38;5;241m.\u001B[39mget_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 1101\u001B[0m has_remote_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mAutoConfig\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mauto_map\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n\u001B[0;32m 1102\u001B[0m has_local_code \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict \u001B[38;5;129;01mand\u001B[39;00m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mmodel_type\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;129;01min\u001B[39;00m CONFIG_MAPPING\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:634\u001B[0m, in \u001B[0;36mPretrainedConfig.get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 632\u001B[0m original_kwargs \u001B[38;5;241m=\u001B[39m copy\u001B[38;5;241m.\u001B[39mdeepcopy(kwargs)\n\u001B[0;32m 633\u001B[0m \u001B[38;5;66;03m# Get config dict associated with the base config file\u001B[39;00m\n\u001B[1;32m--> 634\u001B[0m config_dict, kwargs \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mcls\u001B[39m\u001B[38;5;241m.\u001B[39m_get_config_dict(pretrained_model_name_or_path, \u001B[38;5;241m*\u001B[39m\u001B[38;5;241m*\u001B[39mkwargs)\n\u001B[0;32m 635\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m \u001B[38;5;129;01min\u001B[39;00m config_dict:\n\u001B[0;32m 636\u001B[0m original_kwargs[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m]\n", "File \u001B[1;32m~\\anaconda3\\envs\\py310\\lib\\site-packages\\transformers\\configuration_utils.py:722\u001B[0m, in \u001B[0;36mPretrainedConfig._get_config_dict\u001B[1;34m(cls, pretrained_model_name_or_path, **kwargs)\u001B[0m\n\u001B[0;32m 720\u001B[0m config_dict[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m_commit_hash\u001B[39m\u001B[38;5;124m\"\u001B[39m] \u001B[38;5;241m=\u001B[39m commit_hash\n\u001B[0;32m 721\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m (json\u001B[38;5;241m.\u001B[39mJSONDecodeError, \u001B[38;5;167;01mUnicodeDecodeError\u001B[39;00m):\n\u001B[1;32m--> 722\u001B[0m \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mEnvironmentError\u001B[39;00m(\n\u001B[0;32m 723\u001B[0m \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mIt looks like the config file at \u001B[39m\u001B[38;5;124m'\u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m'\u001B[39m\u001B[38;5;124m is not a valid JSON file.\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m 724\u001B[0m )\n\u001B[0;32m 726\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_local:\n\u001B[0;32m 727\u001B[0m logger\u001B[38;5;241m.\u001B[39minfo(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mloading configuration file \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mresolved_config_file\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n", "\u001B[1;31mOSError\u001B[0m: It looks like the config file at 'C:\\Users\\merti\\.cache\\huggingface\\hub\\models--HUBII-Platform--ECG2HRV\\snapshots\\75f67e01de12e33cfb05cfbfed35ff621246b3f9\\config.json' is not a valid JSON file." ] } ], "source": [ "# Example with AutoModel\n", "from transformers import AutoTokenizer, AutoModel\n", "model = AutoModel.from_pretrained('HUBII-Platform/ECG2HRV')" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-15T08:19:30.471734700Z", "start_time": "2024-02-15T08:19:29.806536100Z" } } }, { "cell_type": "markdown", "source": [ "3. Batched feature extraction - not supported (see https://huggingface.co/docs/transformers/main_classes/feature_extractor#transformers.BatchFeature)\n", "Not possible since it is not a model itself but a component used in the pipeline" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "# 3. Using simple download\n", "(See https://huggingface.co/julien-c/wine-quality?structured_data=%7B%7D)" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 1, "outputs": [], "source": [ "from huggingface_hub import hf_hub_download\n", "import joblib\n", "import torch\n", "import numpy as np\n", "\n", "from src.model import ECG2HRV" ], "metadata": { "collapsed": false } }, { "cell_type": "markdown", "source": [ "**Instantiate model and save the model as a joblib file in the huggingface repository**" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 2, "outputs": [], "source": [ "# Instantiate model\n", "model = ECG2HRV()\n", "# Save\n", "joblib.dump(model, \"..\\ECG2HRV.joblib\")\n", "# Load in notebook\n", "model = joblib.load(\"..\\ECG2HRV.joblib\")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-21T16:08:51.659030Z", "start_time": "2024-02-21T16:08:51.605730100Z" } } }, { "cell_type": "markdown", "source": [ "**Test the model locally with random ecg**" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 3, "outputs": [], "source": [ "duration_seconds = 10 # Time duration for ECG signal (in seconds)\n", "sample_rate = 100 # Sample rate (samples per second)\n", "num_samples = duration_seconds * sample_rate # Number of samples\n", "\n", "t = np.linspace(0, duration_seconds, num_samples) # Time array\n", "\n", "# Generate ECG signal (example synthetic data)\n", "ecg_signal = (\n", " 0.2 * np.sin(2 * np.pi * 1 * t) +\n", " 0.5 * np.sin(2 * np.pi * 0.5 * t) -\n", " 0.1 * np.sin(2 * np.pi * 2.5 * t)\n", ")\n", "\n", "# Add some random noise\n", "ecg_signal += np.random.normal(scale=0.1, size=num_samples)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-21T16:08:51.669938Z", "start_time": "2024-02-21T16:08:51.635032600Z" } } }, { "cell_type": "code", "execution_count": 4, "outputs": [ { "data": { "text/plain": "[{'HRV_MeanNN': 413.4782608695652,\n 'HRV_SDNN': 100.97743652790477,\n 'HRV_SDANN1': nan,\n 'HRV_SDNNI1': nan,\n 'HRV_SDANN2': nan,\n 'HRV_SDNNI2': nan,\n 'HRV_SDANN5': nan,\n 'HRV_SDNNI5': nan,\n 'HRV_RMSSD': 92.78518690551262,\n 'HRV_SDSD': 94.96410805236795,\n 'HRV_CVNN': 0.24421462041449105,\n 'HRV_CVSD': 0.22440160870944167,\n 'HRV_MedianNN': 400.0,\n 'HRV_MadNN': 118.60799999999999,\n 'HRV_MCVNN': 0.29651999999999995,\n 'HRV_IQRNN': 150.0,\n 'HRV_SDRMSSD': 1.0882926455785953,\n 'HRV_Prc20NN': 320.0,\n 'HRV_Prc80NN': 490.0,\n 'HRV_pNN50': 52.17391304347826,\n 'HRV_pNN20': 69.56521739130434,\n 'HRV_MinNN': 310.0,\n 'HRV_MaxNN': 640.0,\n 'HRV_HTI': 5.75,\n 'HRV_TINN': 0.0}]" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model(input_data=ecg_signal, frequency=100.0)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-21T16:08:51.755181400Z", "start_time": "2024-02-21T16:08:51.671014900Z" } } }, { "cell_type": "markdown", "source": [ "**Test if the model can be loaded from the hub and used**" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": 19, "outputs": [], "source": [ "# Load from hub\n", "REPO_ID = \"HUBII-Platform/ECG2HRV\"\n", "FILENAME = \"feature-extractor.joblib\"\n", "\n", "model = joblib.load(\n", " hf_hub_download(repo_id=REPO_ID, filename=FILENAME)\n", ")" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-21T11:36:52.302912800Z", "start_time": "2024-02-21T11:36:52.145834500Z" } } }, { "cell_type": "code", "execution_count": 24, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'rmssd': tensor(1.), 'mean_rr': tensor(3.), 'sdnn': tensor(1.), 'mean_hr': tensor(20000.)}\n" ] } ], "source": [ "# Run model\n", "model(input_data=ecg_signal, frequency=100.0)" ], "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-02-21T11:37:12.239246200Z", "start_time": "2024-02-21T11:37:12.219556200Z" } } }, { "cell_type": "markdown", "source": [ "# 4. Using custom model (not tested yet)\n" ], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" } }, "nbformat": 4, "nbformat_minor": 0 }