ST-GCN复现以及配置环境遇到的问题(Colab)

ST-GCN复现以及配置环境遇到的问题(Colab)

ST-GCN介绍:解读:基于动态骨骼的动作识别方法ST-GCN(时空图卷积网络模型)_浪浪的博客-CSDN博客

这里我就不多介绍了,大家可以别的地方看看。这里我主要介绍复现过程遇到的坑。由于本人电脑比较辣鸡,在本地跑程序的时候环境出现了很多问题。所以只能去colab跑程序,优势有很多,首先有免费的GPU,第二环境配置出现问题较少,第三速度快。

本次实验是在colab(要科学上网)的基础上跑的,所以本地电脑会出现哪种状况我就不晓得了。

废话不多说,先上 github:Niki173/st-gcn

这里我是转载这个大神的github:https://github.com/1zgh/st-gcn

之前我用过两个链接都无法实现:https://github.com/yysijie/st-gcn(这个网上指导帖子比较多,但是我就差最后一步没有复现出来)

https://github.com/open-mmlab/mmskeleton(这个网上指导帖子比较少,我自己捣鼓了几次,果断放弃)

当然你们也可以试一下他们的方法,也许是我太菜了,你们可以复现出来。

首先是配置openpose,这个是真的非常难搞,我一大半的时间都是花在配置openpose环境的问题上,后来看到有个帖子试了一下,效果还是可以的。https://colab.research.google.com/github/tugstugi/dl-colab-notebooks/blob/master/notebooks/OpenPose.ipynb,但是有一个非常奇怪的现象,我刚开始以为是只要用到第一个cell,所以我只是复制了第一个cell过去我的ipynb,但是它配置环境的时候就会缺很多东西,导致后面无法运行(试了好多次,还没醒悟)。后来我是直接复制它这个ipynb,
在这里插入图片描述

点击复制到云端硬盘。然后只需要运行第一个cell,就是这个
在这里插入图片描述

在这里插入图片描述

出现了这个就说明openpose配置成功了

然后再执行其他命令。

接下来的程序是借鉴这个大神的文章:配置ST-GCN环境记录【Google colab】_ghw/article/details/107610136
但是他用的github:https://github.com/yysijie/st-gcn,刚开始我就是完全按照他的步骤走,还是卡死再最后一步。

所以我换了文章开头那个github,但是居然能跑通,我也觉得很奇怪。

1.先安装cuda,还有ubuntu等环境

!wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
!dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb # 安装软件包:dpkg -i <.deb file name>
!apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub # 下载的文件添加到本地数据库
!apt-get update
!apt-get install cuda=9.0.176-1
  1. 安装torchvision
!pip install torchvision==0.2.0

3.安装环境所需的其他python库

 !pip install -r /content/st-gcn/requirements.txt
  1. 安装ffmpeg
!sudo apt-get install ffmpeg
  1. 安装torchlight
 %cd /content/st-gcn/torchlight
 !python setup.py install
 %cd ..
  1. 获取预训练模型
!bash /content/st-gcn/tools/get_models.sh
  1. 安装PyYAML(如果不安装就会出现如下情况:AttributeError: module ‘yaml’ has no attribute 'FullLoader’AttributeError: module ‘yaml’ has no attribute ‘FullLoader’)
!pip install -U PyYAML
  1. clone本次实验的代码
!git clone Niki173/st-gcn

9.执行检测代码

!python main.py demo --video /content/st-gcn/resource/media/ta_chi.mp4 --openpose /content/openpose/build

然后就会出现结果了:

现实预测结果是tai_chi,可视化完成,并且把结果保存在 ./data/demo_result/ta_chi.mp4.
我们把它下载到本地,然后打开。
在这里插入图片描述

复现成功!!!
也可以换一个视频测试一下,比如执行

!python main.py demo --video /content/st-gcn/resource/media/skateboarding.mp4 --openpose /content/openpose/build

在这里插入图片描述

大功告成。

### 回答1: 时空图卷积神经网络的代码: # 导入需要的模块 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 加载时空图卷积神经网络模型 def load_tscnn_model(): pass # 定义数据占位符 x_input = tf.placeholder(tf.float32, shape=[None, 784]) y_target = tf.placeholder(tf.float32, shape=[None, 10]) # 定义时空图卷积神经网络 def tscnn(x_input): # 第一层卷积 conv1 = tf.layers.conv2d(inputs=x_input, filters=32, kernel_size=[5, 5], padding='same', activation=tf.nn.relu) # 第二层池化 pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) # 第三层卷积 conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], padding='same', activation=tf.nn.relu) # 第四层池化 pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) # 第五层全连接 pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) dense1 = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu) # 输出层 output = tf.layers.dense(inputs=dense1, units=10, activation=tf.nn.softmax) # 返回预测结果 return output # 训练模型 def train(): # 加载数据 mnist = tf.contrib.learn.datasets.load_dataset("mnist") train_data = mnist.train.images # Returns np.array train_labels = np.asarray(mnist.train.labels, dtype=np.int32) eval_data = mnist.test.images # Returns np.array eval_labels = np.asarray(mnist.test.labels, dtype=np.int32) # 加载时空图卷积神经网络模型 cnn_model = load_tscnn_model() # 预测结果 prediction = cnn_model(x_input) # 计算损失 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y_target)) # 定义优化器 train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) # 准确率 accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(prediction, 1), tf.argmax(y_target, 1)), dtype=tf.float32)) # 开始训练 with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) for i in range(1000): _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x_input: train_data, y_target: train_labels}) if i % 100 == 0: print("Step: {}, Accuracy: {}".format(i, accuracy_val)) # 测试模型 test_accuracy = sess.run(accuracy, feed_dict={x_input: eval_data, y_target: eval_labels}) print("Test Accuracy: {}".format(test_accuracy)) ### 回答2: 时空图卷积神经网络(Spatial-Temporal Graph Convolutional Network,STGCN)是一种用于处理时空数据的深度学习模型。以下是一个简单的时空图卷积神经网络的代码示例: 首先,我们需要导入所需的库和模块: ``` import torch import torch.nn as nn import torch.nn.functional as F ``` 然后,定义一个时空图卷积层的类: ``` class STConvLayer(nn.Module): def __init__(self, in_channels, out_channels, spatial_kernel_size, temporal_kernel_size): super(STConvLayer, self).__init__() self.spatial_conv = nn.Conv2d(in_channels, out_channels, spatial_kernel_size) self.temporal_conv = nn.Conv2d(out_channels, out_channels, (temporal_kernel_size, 1)) def forward(self, x): x = self.spatial_conv(x) x = self.temporal_conv(x) x = F.relu(x) return x ``` 在这个类中,我们使用一个二维卷积层来进行空间卷积,并使用一个一维卷积层来进行时间卷积。在forward方法中,我们首先进行空间卷积,然后进行时间卷积,并将结果通过ReLU激活函数进行非线性激活。 接下来,定义整个时空图卷积神经网络模型的类: ``` class STGCN(nn.Module): def __init__(self, in_channels, out_channels, num_timesteps, num_nodes): super(STGCN, self).__init__() self.conv1 = STConvLayer(in_channels, 64, (1, 3), 3) self.conv2 = STConvLayer(64, out_channels, (1, 3), 3) self.fc = nn.Linear(num_timesteps * out_channels * num_nodes, out_channels) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = x.view(x.size(0), -1) x = self.fc(x) return x ``` 在这个类中,我们定义两个STConvLayer层和一个线性层。在forward方法中,我们首先通过两个STConvLayer进行时空卷积,然后通过view方法将输出的张量展平,并最后通过线性层进行分类或回归等操作。 最后,我们可以创建一个STGCN模型实例并进行训练和测试等操作: ``` model = STGCN(in_channels, out_channels, num_timesteps, num_nodes) criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.001) # 在这里进行训练和测试数据的加载与预处理 for epoch in range(num_epochs): optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # 在这里进行每个epoch的测试准确率计算或其他操作 ``` 以上是一个简单的时空图卷积神经网络的代码示例。需要根据具体的问题进行适当的修改和调整。 ### 回答3: 时空图卷积神经网络(Spatial-Temporal Graph Convolutional Network,STGCN)是一种可以应用于时空数据的图卷积神经网络,适用于建模具有时空依赖关系的数据。 在构建STGCN模型之前,首先需要定义一个图结构,图中的节点表示空间位置,边表示空间位置之间的相邻关系。然后,根据时间序列数据和定义好的图结构,构建输入数据。输入数据可以表示为一个三维张量,维度为[N, T, D],其中N表示节点数目,T表示时间步长,D表示特征维度。 接下来,可以使用以下代码来构建STGCN模型: 1. 导入所需的库和模块 import torch import torch.nn as nn from torch.nn import functional as F 2. 定义STGCN模型类 class STGCN(nn.Module): def __init__(self, num_nodes, num_features, num_timesteps, num_classes): super(STGCN, self).__init__() self.conv1 = GraphConvolution(num_nodes, num_features, num_features) self.conv2 = GraphConvolution(num_nodes, num_features, num_features) self.fc1 = nn.Linear(num_nodes * num_features * num_timesteps, 256) self.fc2 = nn.Linear(256, num_classes) def forward(self, x, adj): x = self.conv1(x, adj) x = self.conv2(x, adj) x = x.view(x.size(0), -1) x = F.relu(self.fc1(x)) x = self.fc2(x) return x 3. 定义GraphConvolution类 class GraphConvolution(nn.Module): def __init__(self, num_nodes, in_features, out_features): super(GraphConvolution, self).__init__() self.weight = nn.Parameter(torch.FloatTensor(in_features, out_features)) self.bias = nn.Parameter(torch.FloatTensor(out_features)) def forward(self, x, adj): x = torch.matmul(adj, x) x = torch.matmul(x, self.weight) x = x + self.bias x = F.relu(x) return x 在以上代码中,STGCN模型中包括两个GraphConvolution层和两个全连接层。GraphConvolution层用于实现图卷积操作,全连接层用于分类任务。在模型的forward函数中,通过逐层传递数据进行前向计算。 总结起来,以上给出了一个简单的时空图卷积神经网络的代码框架,然后可以根据具体的时空数据集和任务需求进行模型的细化和改进。
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值