资源简介
采用全连接神经网络对012345的手势图片进行识别,基于Pytorch编写,附h5格式数据集
代码片段和文件信息
import torch as t
import h5py
import numpy as np
import torch.nn as nn
import matplotlib.pyplot as plt
import os warnings
os.environ[‘KMP_DUPLICATE_LIB_OK‘] = ‘True‘
warnings.filterwarnings(‘ignore‘)
class Net(nn.Module):
def __init__(self n_inputs n_hidden_1 n_hidden_2 n_outputs):
super(Net self).__init__()
self.fc = nn.Sequential(
nn.Linear(n_inputs n_hidden_1) nn.BatchNorm1d(n_hidden_1) nn.ReLU()
nn.Linear(n_hidden_1 n_hidden_2) nn.BatchNorm1d(n_hidden_2) nn.ReLU()
nn.Linear(n_hidden_2 n_outputs)
)
def forward(self x):
output = self.fc(x)
return output
train_dataset = h5py.File(‘train_signs.h5‘ “r“)
train_set_x_orig = t.from_numpy(np.array(train_dataset[“train_set_x“]))
train_set_y = t.tensor(np.array(train_dataset[“train_set_y“]))
test_dataset = h5py.File(‘test_signs.h5‘ “r“)
test_set_x_orig = t.from_numpy(np.array(test_dataset[“test_set_x“]))
test_set_y = t.tensor(np.array(test_dataset[“test_set_y“]))
print(train_set_x_orig.shape) # (108064643)
print(test_set_x_orig.shape) # (12064643)
print(train_set_y.shape) # (1080)
print(test_set_y.shape) # (120)
classes = np.array(test_dataset[“list_classes“])
print(classes) # [0 1 2 3 4 5]
fig = plt.figure()
for i in range(6):
plt.subplot(2 3 i+1)
plt.imshow(train_set_x_orig[i])
plt.title(“Labels: {}“.format(classes[int(train_set_y[i])]))
plt.xticks([])
plt.yticks([])
pass
plt.show()
num_train = train_set_x_orig.shape[0] # 1080
num_test = test_set_x_orig.shape[0] # 120
print(“训练集包含 %d 张图片; 测试集包含 %d 张图片。“ % (num_train num_test))
train_set_x = train_set_x_orig.reshape(num_train -1)/255.0
test_set_x = test_set_x_orig.reshape(num_test -1)/255.0
print(“训练集图片尺寸变为: “ train_set_x.shape) # (108064643) --> (108012288)
print(“测试集图片尺寸变为: “ test_set_x.shape) # (12064643) --> (12012288)
model = Net(12288 200 50 6)
print(model)
print(“model包含{}个参数。“.format(sum(x.numel() for x in model.parameters())))
cost_func = nn.CrossEntropyLoss()
optimizer = t.optim.SGD(model.parameters() lr=0.0005 momentum=0.9)
epochs = 200
for epoch in range(epochs):
model.train()
y_out = model(train_set_x)
train_loss = cost_func(y_out train_set_y)
optimizer.zero_grad()
train_loss.backward()
optimizer.step()
with t.no_grad():
_ y_pred = y_out.max(1)
num_correct_train = (y_pred == train_set_y).sum().item()
acc_rate_train = (num_correct_train / num_train) * 100.0
model.eval()
y_out = model(test_set_x)
_ y_pred = y_out.max(1)
num_correct_test = (y_pred == test_set_y).sum().item()
acc_rate_test = num_correct_test * 100.0 / num_test
print(“世代数: %d 训练集正确率: %.1f%% 测试集正确率: %.1f%%“ % (epoch+1 acc_rate_train
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3140 2020-08-12 10:09 012345手势识别-FCNet\Fully_connected_Net.py
文件 1477712 2019-07-09 23:16 012345手势识别-FCNet\test_signs.h5
文件 13281872 2019-07-09 23:16 012345手势识别-FCNet\train_signs.h5
目录 0 2020-09-13 10:55 012345手势识别-FCNet\
- 上一篇:猫-非猫图二分类识别
- 下一篇:python绘制 彩色蜂蜜窝(基于turtle)
相关资源
- 猫-非猫图二分类识别
- 深度学习(SPOT-RNA)
- 机器学习k means算法实现图像分割
- kmeans聚类算法的python实现程序
- Python100经典练习题
- 南瓜书(PumpkinBook)
- 深度学习YOLOv3分类算法
- 机器学习numpy和pandas基础
- python机器学习Sebastian Raschka中文最新完
- Python-DeepMoji模型的pyTorch实现
- Deep Learning Cookbook_ practical recipes to g
- 《机器学习实战》源代码Python3
- 深度学习视频中的行为识别
- Python-使用DeepFakes实现YouTube视频自动换
- deep learning with python 中文版
- Introduction to machine learning with python (
- python新浪微博爬虫,爬取微博和用户
- Python-一系列高品质的动漫人脸数据集
- Python-Insightface人脸检测识别的最小化
- 非线性回归Python代码
- pytorch_pose_proposal_networks-master.zip
- 093 2018北风网人工智能视频(完结)转
- python的色情图片识别
- 贝叶斯网络程序
- 吴恩达深度学习超参数调制完整程序
- 《机器学习实战》Python3代码
- 深度学习入门 基于python理论与实现
- Python-自然场景文本检测PSENet的一个
- Python-在特征金字塔网络FPN的Pytorch实现
- Python-PyTorch实时多人姿态估计项目的实
评论
共有 条评论