Skip to content

pemo-x/modelDisplay

Repository files navigation

{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#功能需求一:\n",
    "#   数据集展示\n",
    "#       了解到需要展示的数据集格式大致分为两种,一种是【骨架】,一种是【轨迹】\n",
    "#       软件可以展示三维环境下的数据,但格式较为严格\n",
    "#       请各位将展示模型所要用到的数据集的【处理函数】提取出来,稍作修改,将原始数据集\n",
    "#      处理成软件这边能够接收并处理的格式\n",
    "#       考虑到不同数据集可能需要不同的呈现效果,软件这边提供可选参数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#       处理后数据格式:\n",
    "#           datas,类型为字典\n",
    "#########################################################################\n",
    "#               ----必要参数:datas['points']  !!要绘制的数据点!!\n",
    "#                   类型为numpy.NDArray, \n",
    "#                   shape=[frame, num, XYZcoordinate], \n",
    "#                   帧数,点数,坐标。  \n",
    "#                   表示每帧要绘制的数据点。\n",
    "\n",
    "#                   例:shape=[106, 25, 3]  即25个点在106帧中,xyz的坐标值\n",
    "#########################################################################\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#########################################################################\n",
    "#               ----可选参数:datas['links']    !!用于【骨架数据】!!\n",
    "#\t\t\t\t\t类型为list,应该用于【骨架数据】,表示数据点间的连线关系,\n",
    "#                   内部数据值应为【点的索引】\n",
    "\n",
    "#\t\t\t\t\t例:[ (0, 1), (1, 11), (3, 4)...]\n",
    "#########################################################################\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#########################################################################\n",
    "#               ----可选参数:datas['lines']    !!用于【轨迹数据】!!\n",
    "#\t\t\t\t\t类型为list[numpy.NDArray], \n",
    "#                   其中每一个NDArray.shape=[num, XYZcoordinate],\n",
    "#                   点数,坐标。  \n",
    "#                   表示轨迹线,应该用于【轨迹数据】,\n",
    "#                   软件将根据该数据将运动点的轨迹用拖尾的形式绘制\n",
    "\n",
    "#\t\t\t\t\t例: 从上面的例子中抽取点的轨迹  \n",
    "#                           line1=datas['points'][ : , 0, : ]  #shape=[106,3]\n",
    "#########################################################################"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#########################################################################\n",
    "#               如有其他呈现效果需要请单独说明,后续可添加参数\n",
    "#               如无异议请将数据集处理方式提取成方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#骨架数据集处理函数示例\n",
    "#该数据集单个文件内只包含单个对象\n",
    "import numpy as np\n",
    "\n",
    "def dealSkeleton(file):\n",
    "    '''return datas['points'] shape in [frame,point,RGBcoordinate]\n",
    "        return datas['links'] like [(1,2),(2,21),...]'''\n",
    "    def read_xyz(file, max_body=2, num_joint=25):\n",
    "        seq_info = read_skeleton(file)\n",
    "        data = np.zeros((3, seq_info['numFrame'], num_joint, max_body))\n",
    "        for n, f in enumerate(seq_info['frameInfo']):\n",
    "            for m, b in enumerate(f['bodyInfo']):\n",
    "                for j, v in enumerate(b['jointInfo']):\n",
    "                    if m < max_body and j < num_joint:\n",
    "                        data[:, n, j, m] = [v['x'], v['y'], v['z']]\n",
    "                    else:\n",
    "                        pass\n",
    "        return data\n",
    "\n",
    "    def read_skeleton(file):\n",
    "        with open(file, 'r', encoding = 'utf-8', errors=\"ignore\") as f:\n",
    "            skeleton_sequence = {}\n",
    "            skeleton_sequence['numFrame'] = int(f.readline())\n",
    "            skeleton_sequence['frameInfo'] = []\n",
    "            for t in range(skeleton_sequence['numFrame']):\n",
    "                frame_info = {}\n",
    "                frame_info['numBody'] = int(f.readline())\n",
    "                frame_info['bodyInfo'] = []\n",
    "                for m in range(frame_info['numBody']):\n",
    "                    body_info = {}\n",
    "                    body_info_key = [\n",
    "                        'bodyID', 'clipedEdges', 'handLeftConfidence',\n",
    "                        'handLeftState', 'handRightConfidence', 'handRightState',\n",
    "                        'isResticted', 'leanX', 'leanY', 'trackingState'\n",
    "                    ]\n",
    "                    body_info = {\n",
    "                        k: float(v)\n",
    "                        for k, v in zip(body_info_key, f.readline().split())\n",
    "                    }\n",
    "                    body_info['numJoint'] = int(f.readline())\n",
    "                    body_info['jointInfo'] = []\n",
    "                    for v in range(body_info['numJoint']):\n",
    "                        joint_info_key = [\n",
    "                            'x', 'y', 'z', 'depthX', 'depthY', 'colorX', 'colorY',\n",
    "                            'orientationW', 'orientationX', 'orientationY',\n",
    "                            'orientationZ', 'trackingState'\n",
    "                        ]\n",
    "                        joint_info = {\n",
    "                            k: float(v)\n",
    "                            for k, v in zip(joint_info_key, f.readline().split())\n",
    "                        }\n",
    "                        body_info['jointInfo'].append(joint_info)\n",
    "                    frame_info['bodyInfo'].append(body_info)\n",
    "                skeleton_sequence['frameInfo'].append(frame_info)\n",
    "        return skeleton_sequence\n",
    "    \n",
    "    def get_links():\n",
    "        \n",
    "        neighbor_1base = [(1, 2), (2, 21), (3, 21), (4, 3), (5, 21),\n",
    "                              (6, 5), (7, 6), (8, 7), (9, 21), (10, 9),\n",
    "                              (11, 10), (12, 11), (13, 1), (14, 13), (15, 14),\n",
    "                              (16, 15), (17, 1), (18, 17), (19, 18), (20, 19),\n",
    "                              (22, 23), (23, 8), (24, 25), (25, 12)]\n",
    "        neighbor_link = [(i - 1, j - 1) for (i, j) in neighbor_1base]\n",
    "        return neighbor_link\n",
    "    datas={}\n",
    "    points=read_xyz(file)\n",
    "    points=points[:,:,:,0]\n",
    "    points=np.transpose(points,[1,2,0])\n",
    "    points[:,:,[0,1,2]]=points[:,:,[2,0,1]]\n",
    "    points[:,:,0]-=np.median(points[:,:,0])\n",
    "    points[:,:,2]+=np.abs(np.min(points[:,:,2]))\n",
    "    \n",
    "    datas['points']=points\n",
    "    datas['links']=get_links()\n",
    "    return datas\n",
    "\n",
    "path=f'./datas/S001C001P001R001A001.skeleton'\n",
    "datas=dealSkeleton(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(103, 25, 3)\n",
      "[(0, 1), (1, 20), (2, 20), (3, 2), (4, 20), (5, 4), (6, 5), (7, 6), (8, 20), (9, 8), (10, 9), (11, 10), (12, 0), (13, 12), (14, 13), (15, 14), (16, 0), (17, 16), (18, 17), (19, 18), (21, 22), (22, 7), (23, 24), (24, 11)]\n"
     ]
    }
   ],
   "source": [
    "print(datas['points'].shape)#该副骨架中25个点在103帧中的三维坐标\n",
    "print(datas['links'])#25个点的连接关系,两两为一组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#轨迹数据集处理函数示例:\n",
    "#该数据集内包含多组对象\n",
    "def plane_two(data):\n",
    "    real = {}\n",
    "    data = data.transpose(1,2,0,3)\n",
    "    data = data.squeeze()\n",
    "    for i in range(3):\n",
    "        temp = np.max(data[:,:,i]) - np.min(data[:,:,i])\n",
    "        data[:,:,i]= (data[:,:,i] - np.min(data[:,:,i]))/temp*5\n",
    "    # print(data.shape)\n",
    "    real['points'] = data\n",
    "    # print(real['points'].shape)\n",
    "    line1=data[:,0,:]\n",
    "    line2=data[:,1,:]\n",
    "    lines=[line1,line2]\n",
    "    real['lines']=lines\n",
    "\n",
    "    return real\n",
    "\n",
    "data = (np.load(\"./datas/rechange_val_data_plane_Radar.npy\")[10])[0:3]\n",
    "datas=plane_two(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(601, 2, 3)\n",
      "(601, 3)\n",
      "(601, 3)\n"
     ]
    }
   ],
   "source": [
    "print(datas['points'].shape)#两个点在601帧的三维坐标\n",
    "for each in datas['lines']:\n",
    "    print(each.shape)#分别为两个点在601帧的三维坐标,两条轨迹线\n",
    "#总之就是提供一个函数,能够将原始数据集按照你们的模型处理方法先提取出三维坐标,然后需要轨迹的往里面添加所有轨迹线的各个坐标,需要连接骨架的添加索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#功能需求二:\n",
    "#   模型运行\n",
    "#   软件设计的运行模型的方法为利用子进程的方式,通过命令行运行模型\n",
    "#   所以需要各位将模型的【运行方式调整成命令行的形式】\n",
    "#   同时,因为需要在软件上选择数据集后运行,要求模型可以【通过命令行提供数据集为参数】\n",
    "#   这边有个通过命令行运行模型的示例,请在此代码下【测试】各位的【模型】能否【正常运行并输出】\n",
    "#   运行结束后(或者出现报错)才会有结果被打印出来,请耐心等待,如果运行时间超过预期,可能是中间出现死循环\n",
    "\n",
    "#       需要提供的参数为command,directory\n",
    "#       其中,command为命令行执行的指令\n",
    "#       directory为模型的运行目录,也可以简单理解为模型代码所在的目录\n",
    "#       比如:\n",
    "#       command = f'python /data/home/temp/Desktop/model/st-gcn-cam-org/main.py shapleycam --skeleton S003C001P007R002A019'#记得要像这样能够提供数据集作为参数\n",
    "#       directory = f'/data/home/temp/Desktop/model/st-gcn-cam-org'\n",
    "#       这里的directory即为要运行的main.py所在的目录\n",
    "#\n",
    "#       测试代码如下:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Command executed successfully\n",
      "shapleycam\n",
      "Using xsub weights.\n",
      "Processing skeleton: S003C001P007R002A019\n",
      "各个部位的夏普利值: [[('left_hand', 0.19403504945882277)], [('right_hand', 0.22231027729592182)], [('left_leg', 0.3444637328346931)], [('right_leg', 0.12986051474784824)], [('trunk', 0.1093303529557566)]]\n",
      "Insertion Curve:\n",
      " [0.01042567 0.7766312  0.9999993  0.9999999  0.99992704 0.9999994\n",
      " 0.9999999  0.9999962  0.99999785 1.         1.         0.9999999\n",
      " 0.9999999  0.99999976 0.9999999  1.         1.         1.\n",
      " 0.9999995  0.9999994  0.9999881  0.9999815  0.9999918  0.99998355\n",
      " 0.9999994  0.99997723 0.9999999  0.99999964 0.9999908  0.9998946\n",
      " 0.9999908  0.99999964 0.9999999  1.         0.9999999  1.\n",
      " 1.         1.         1.         1.         1.         0.9999993\n",
      " 0.9999999  0.9999999  0.9999999  0.9999999  0.9999999  0.9999999\n",
      " 0.9999999  0.9999999 ]\n",
      "\n",
      "Insertion AUC: 0.9757354736328125\n",
      "\n",
      "Deletion Curve:\n",
      " [9.9999988e-01 9.9944144e-01 9.9947602e-01 8.7712735e-01 1.5768479e-04\n",
      " 4.7682218e-07 1.2344638e-07 3.3179981e-10 1.1800954e-06 5.6910890e-06\n",
      " 3.3908452e-06 2.2973032e-05 2.0552365e-04 9.4728937e-05 2.1309437e-05\n",
      " 1.4888797e-05 9.6873802e-05 2.8431448e-04 1.9107631e-04 4.3591663e-06\n",
      " 6.8343402e-04 7.1087011e-06 3.3452324e-10 6.3473125e-13 2.1064932e-10\n",
      " 9.1147215e-11 2.8057201e-10 4.2727434e-06 1.1469865e-06 1.3229203e-07\n",
      " 7.8059166e-08 2.1579388e-06 5.3705799e-06 3.2089003e-05 7.6579381e-06\n",
      " 4.4348885e-06 6.2464738e-07 1.4266070e-06 1.3074256e-06 7.3253835e-04\n",
      " 3.0314442e-04 7.2161161e-04 6.1681434e-03 1.0425666e-02 1.0425666e-02\n",
      " 1.0425666e-02 1.0425666e-02 1.0425666e-02 1.0425666e-02 1.0425666e-02]\n",
      "\n",
      "Deletion AUC: 0.07917610645294189\n",
      "ave_drop 2.2649767e-05 ave_increase 0 inse_auc 0.9757354736328125 del_auc 0.07917610645294189\n",
      "saveing\n",
      "代码运行时间: 17.603412866592407 秒\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "\n",
    "def CreateTask(command, directory):\n",
    "    process=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=directory)\n",
    "    return process\n",
    "def getRunstatu(process):\n",
    "    exit_code = process.poll()\n",
    "\n",
    "    if exit_code is None:\n",
    "        return \"子进程仍在运行\"\n",
    "    else:\n",
    "        return f\"子进程已结束,退出码为:{exit_code}\"\n",
    "    \n",
    "def getReturn(process):\n",
    "    global stdout, stderr\n",
    "    Runstatu=getRunstatu(process)\n",
    "    if Runstatu!=\"子进程仍在运行\":\n",
    "        stdout, stderr = process.communicate()\n",
    "        # 检查子进程是否成功完成\n",
    "        if process.returncode == 0:\n",
    "            print(\"Command executed successfully\")\n",
    "            print(stdout)\n",
    "        else:\n",
    "            print(\"Command failed with error\")\n",
    "            print(stderr)\n",
    "        return 1\n",
    "    else:\n",
    "        return 0\n",
    "\n",
    "#!!!!!!!!!!\n",
    "stdout, stderr=None,None\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    #需要提供的参数command,directory\n",
    "    # command = f'python /data/home/temp/Desktop/model/st-gcn-cam-org/main.py shapleycam --skeleton S003C001P007R002A019'\n",
    "    command = f'python /data/home/temp/Desktop/model/st-gcn-cam-org/main.py shapleycam'#原始运行代码?\n",
    "    command=command.split(' ')\n",
    "    command.extend('--skeleton S003C001P007R002A019'.split(' '))#追加提供数据集参数\n",
    "    directory = f'/data/home/temp/Desktop/model/st-gcn-cam-org'\n",
    "    process=CreateTask(command,directory)\n",
    "    while not getReturn(process):\n",
    "        continue\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------\n",
      "stdout:shapleycam\n",
      "Using xsub weights.\n",
      "Processing skeleton: S003C001P007R002A019\n",
      "各个部位的夏普利值: [[('left_hand', 0.19403504945882277)], [('right_hand', 0.22231027729592182)], [('left_leg', 0.3444637328346931)], [('right_leg', 0.12986051474784824)], [('trunk', 0.1093303529557566)]]\n",
      "Insertion Curve:\n",
      " [0.01042567 0.7766312  0.9999993  0.9999999  0.99992704 0.9999994\n",
      " 0.9999999  0.9999962  0.99999785 1.         1.         0.9999999\n",
      " 0.9999999  0.99999976 0.9999999  1.         1.         1.\n",
      " 0.9999995  0.9999994  0.9999881  0.9999815  0.9999918  0.99998355\n",
      " 0.9999994  0.99997723 0.9999999  0.99999964 0.9999908  0.9998946\n",
      " 0.9999908  0.99999964 0.9999999  1.         0.9999999  1.\n",
      " 1.         1.         1.         1.         1.         0.9999993\n",
      " 0.9999999  0.9999999  0.9999999  0.9999999  0.9999999  0.9999999\n",
      " 0.9999999  0.9999999 ]\n",
      "\n",
      "Insertion AUC: 0.9757354736328125\n",
      "\n",
      "Deletion Curve:\n",
      " [9.9999988e-01 9.9944144e-01 9.9947602e-01 8.7712735e-01 1.5768479e-04\n",
      " 4.7682218e-07 1.2344638e-07 3.3179981e-10 1.1800954e-06 5.6910890e-06\n",
      " 3.3908452e-06 2.2973032e-05 2.0552365e-04 9.4728937e-05 2.1309437e-05\n",
      " 1.4888797e-05 9.6873802e-05 2.8431448e-04 1.9107631e-04 4.3591663e-06\n",
      " 6.8343402e-04 7.1087011e-06 3.3452324e-10 6.3473125e-13 2.1064932e-10\n",
      " 9.1147215e-11 2.8057201e-10 4.2727434e-06 1.1469865e-06 1.3229203e-07\n",
      " 7.8059166e-08 2.1579388e-06 5.3705799e-06 3.2089003e-05 7.6579381e-06\n",
      " 4.4348885e-06 6.2464738e-07 1.4266070e-06 1.3074256e-06 7.3253835e-04\n",
      " 3.0314442e-04 7.2161161e-04 6.1681434e-03 1.0425666e-02 1.0425666e-02\n",
      " 1.0425666e-02 1.0425666e-02 1.0425666e-02 1.0425666e-02 1.0425666e-02]\n",
      "\n",
      "Deletion AUC: 0.07917610645294189\n",
      "ave_drop 2.2649767e-05 ave_increase 0 inse_auc 0.9757354736328125 del_auc 0.07917610645294189\n",
      "saveing\n",
      "代码运行时间: 30.197386741638184 秒\n",
      "\n",
      "--------------------\n",
      "stderr:/data/home/temp/Desktop/model/st-gcn-cam-org/torchlight/io.py:64: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
      "  weights = torch.load(weights_path)\n",
      "/data/home/temp/Desktop/model/.conda/lib/python3.9/site-packages/torch/nn/modules/module.py:1827: FutureWarning: Using a non-full backward hook when the forward contains multiple autograd Nodes is deprecated and will be removed in future versions. This hook will be missing some grad_input. Please use register_full_backward_hook to get the documented behavior.\n",
      "  self._maybe_warn_non_full_backward_hook(args, result, grad_fn)\n",
      "\n",
      "--------------------\n"
     ]
    }
   ],
   "source": [
    "#功能需求三:\n",
    "#   结果展示\n",
    "#       上面的测试代码中stdout, stderr分别表示标准输出和标准报错的内容\n",
    "print('-'*20)\n",
    "print(f'stdout:{stdout}')\n",
    "print('-'*20)\n",
    "print(f'stderr:{stderr}')\n",
    "print('-'*20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "#       请各位从中抽取出你们需要在软件的结果界面展示的数据,以字典的形式返回\n",
    "#       比如:\n",
    "#           result['AUC']=0.9757\n",
    "#           result['代码运行时间']=30.197386741638184 秒'\n",
    "#           ...\n",
    "#       同时,若模型代码在运行结束后有文件输出,请从代码中提取出每次运行的结果文件路径\n",
    "#       可以保存在刚才的字典里:\n",
    "#           result['file_path']=['******.png','******.png',...]\n",
    "#           最好是绝对路径\n",
    "#\n",
    "#       如若有自定义的其他格式,请单独说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#有任何疑问请及时提出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".conda",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages