"我的NVIDIA开发者之旅” | 征文活动进行中.......

完成我的NVIDIA开发者之旅——Caffe教程(2)[Jetson TK1]Caffe工具环境(Linux)搭建实例-CSDN社区,搭建好Caffe环境后我们就可以开始我们的Caffe实践啦。

不知道大家写的第一个有关深度学习的代码是什么,博主个人是学习吴恩达老师的DeepLearning入门的,也按照课后作业进行了练习,自己第一次动手的就是实现了一个简单的线性回归的实践如下图,到现在依然记忆犹新,哈哈。

接下来我们开始吧,虽然Caffe用于深层网络,但它同样可以表示“浅层”模型,如用于分类的逻辑回归。我们将对合成数据进行简单的逻辑回归,我们将生成这些数据并保存到HDF5中,以向Caffe提供向量。完成该模型后,我们将添加层以提高精度。这就是Caffe的意义:定义一个模型,进行实验,然后部署。

首先我们导入所需要的一些包等资源:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inlineimport os
os.chdir('..')import sys
sys.path.insert(0, './python')
import caffeimport os
import h5py
import shutil
import tempfileimport sklearn
import sklearn.datasets
import sklearn.linear_modelimport pandas as pd

我们可以通过合成一个包含10000个4向量的数据集,用于具有2个信息特征和2个噪声特征的二元分类:

X, y = sklearn.datasets.make_classification(n_samples=10000, n_features=4, n_redundant=0, n_informative=2, n_clusters_per_class=2, hypercube=False, random_state=0
)
print 'data,',X.shape,y.shape # (10000, 4) (10000,) x0,x1,x2,x3, y# Split into train and test
X, Xt, y, yt = sklearn.model_selection.train_test_split(X, y)
print 'train,',X.shape,y.shape  #train: (7500, 4) (7500,)
print 'test,', Xt.shape,yt.shape#test:  (2500, 4) (2500,)# Visualize sample of the data
ind = np.random.permutation(X.shape[0])[:1000] # (7500,)--->(1000,)   x0,x1,x2,x3, y
df = pd.DataFrame(X[ind])
_ = pd.plotting.scatter_matrix(df, figsize=(9, 9), diagonal='kde', marker='o', s=40, alpha=.4, c=y[ind])

data, (10000, 4) (10000,)

train, (7500, 4) (7500,)

test, (2500, 4) (2500,)

使用随机梯度下降(SGD)训练学习和评估scikit Learn的logistic回归。计时并检查分类器的准确性:

%%timeit
# Train and test the scikit-learn SGD logistic regression.
clf = sklearn.linear_model.SGDClassifier(loss='log', n_iter=1000, penalty='l2', alpha=5e-4, class_weight='balanced')clf.fit(X, y)
yt_pred = clf.predict(Xt)
print('Accuracy: {:.3f}'.format(sklearn.metrics.accuracy_score(yt, yt_pred)))

Accuracy: 0.781

Accuracy: 0.781

Accuracy: 0.781

Accuracy: 0.781

1 loop, best of 3: 372 ms per loop

然后再将数据集保存到HDF5以加载到Caffe中:

# Write out the data to HDF5 files in a temp directory.
# This file is assumed to be caffe_root/examples/hdf5_classification.ipynb
dirname = os.path.abspath('./examples/hdf5_classification/data')
if not os.path.exists(dirname):os.makedirs(dirname)train_filename = os.path.join(dirname, 'train.h5')
test_filename = os.path.join(dirname, 'test.h5')# HDF5DataLayer source should be a file containing a list of HDF5 filenames.
# To show this off, we'll list the same data file twice.
with h5py.File(train_filename, 'w') as f:f['data'] = Xf['label'] = y.astype(np.float32)
with open(os.path.join(dirname, 'train.txt'), 'w') as f:f.write(train_filename + '\n')f.write(train_filename + '\n')# HDF5 is pretty efficient, but can be further compressed.
comp_kwargs = {'compression': 'gzip', 'compression_opts': 1}
with h5py.File(test_filename, 'w') as f:f.create_dataset('data', data=Xt, **comp_kwargs)f.create_dataset('label', data=yt.astype(np.float32), **comp_kwargs)
with open(os.path.join(dirname, 'test.txt'), 'w') as f:f.write(test_filename + '\n')

我们可以通过Python net规范在Caffe中定义逻辑回归。这是一种快速而自然的定义网络的方法,避免了手动编辑protobuf模型:

from caffe import layers as L
from caffe import params as Pdef logreg(hdf5, batch_size):# logistic regression: data, matrix multiplication, and 2-class softmax lossn = caffe.NetSpec()n.data, n.label = L.HDF5Data(batch_size=batch_size, source=hdf5, ntop=2)n.ip1 = L.InnerProduct(n.data, num_output=2, weight_filler=dict(type='xavier'))n.accuracy = L.Accuracy(n.ip1, n.label)n.loss = L.SoftmaxWithLoss(n.ip1, n.label)return n.to_proto()train_net_path = 'examples/hdf5_classification/logreg_auto_train.prototxt'
with open(train_net_path, 'w') as f:f.write(str(logreg('examples/hdf5_classification/data/train.txt', 10)))test_net_path = 'examples/hdf5_classification/logreg_auto_test.prototxt'
with open(test_net_path, 'w') as f:f.write(str(logreg('examples/hdf5_classification/data/test.txt', 10)))

现在,我们将定义“解算器”,该解算器通过指定上面定义的训练和测试网络的位置,以及用于学习、显示和“快照”的各种参数的设置值来训练网络:

from caffe.proto import caffe_pb2def solver(train_net_path, test_net_path):s = caffe_pb2.SolverParameter()# Specify locations of the train and test networks.s.train_net = train_net_paths.test_net.append(test_net_path)s.test_interval = 1000  # Test after every 1000 training iterations.s.test_iter.append(250) # Test 250 "batches" each time we test.s.max_iter = 10000      # # of times to update the net (training iterations)# Set the initial learning rate for stochastic gradient descent (SGD).s.base_lr = 0.01        # Set `lr_policy` to define how the learning rate changes during training.# Here, we 'step' the learning rate by multiplying it by a factor `gamma`# every `stepsize` iterations.s.lr_policy = 'step's.gamma = 0.1s.stepsize = 5000# Set other optimization parameters. Setting a non-zero `momentum` takes a# weighted average of the current gradient and previous gradients to make# learning more stable. L2 weight decay regularizes learning, to help prevent# the model from overfitting.s.momentum = 0.9s.weight_decay = 5e-4# Display the current training loss and accuracy every 1000 iterations.s.display = 1000# Snapshots are files used to store networks we've trained.  Here, we'll# snapshot every 10K iterations -- just once at the end of training.# For larger networks that take longer to train, you may want to set# snapshot < max_iter to save the network and training state to disk during# optimization, preventing disaster in case of machine crashes, etc.s.snapshot = 10000s.snapshot_prefix = 'examples/hdf5_classification/data/train'# We'll train on the CPU for fair benchmarking against scikit-learn.# Changing to GPU should result in much faster training!s.solver_mode = caffe_pb2.SolverParameter.CPUreturn ssolver_path = 'examples/hdf5_classification/logreg_solver.prototxt'
with open(solver_path, 'w') as f:f.write(str(solver(train_net_path, test_net_path)))

是时候查看学习和评估Python中的逻辑回归的loss和拟合效果了:

%%timeit
caffe.set_mode_cpu()
solver = caffe.get_solver(solver_path)
solver.solve()accuracy = 0
batch_size = solver.test_nets[0].blobs['data'].num
test_iters = int(len(Xt) / batch_size)
for i in range(test_iters):solver.test_nets[0].forward()accuracy += solver.test_nets[0].blobs['accuracy'].data
accuracy /= test_itersprint("Accuracy: {:.3f}".format(accuracy))

Accuracy: 0.770

Accuracy: 0.770

Accuracy: 0.770

Accuracy: 0.770

1 loop, best of 3: 195 ms per loop

通过命令行界面执行同样的操作,以获得关于模型和求解的详细输出:

!./build/tools/caffe train -solver examples/hdf5_classification/logreg_solver.prototxt
I0224 00:32:03.232779   655 caffe.cpp:178] Use CPU.
I0224 00:32:03.391911   655 solver.cpp:48] Initializing solver from parameters:
train_net: "examples/hdf5_classification/logreg_auto_train.prototxt"
test_net: "examples/hdf5_classification/logreg_auto_test.prototxt"
......
I0224 00:32:04.087514   655 solver.cpp:406]     Test net output #0: accuracy = 0.77
I0224 00:32:04.087532   655 solver.cpp:406]     Test net output #1: loss = 0.593815 (* 1 = 0.593815 loss)
I0224 00:32:04.087541   655 solver.cpp:323] Optimization Done.
I0224 00:32:04.087548   655 caffe.cpp:222] Optimization Done.

如果查看输出或logreg_auto_train.prototxt,您将看到该模型是简单的逻辑回归。

我们可以通过在接受输入的权重和给出输出的权重之间引入非线性,使其更高级一些——现在我们有了一个两层网络。

该网络在nonlinear_auto_train.proto,txt中给出,这是t解算器中所做的唯一更改。我们现在将使用的新网络的最终精度应高于逻辑回归!

from caffe import layers as L
from caffe import params as Pdef nonlinear_net(hdf5, batch_size):# one small nonlinearity, one leap for model kindn = caffe.NetSpec()n.data, n.label = L.HDF5Data(batch_size=batch_size, source=hdf5, ntop=2)# define a hidden layer of dimension 40n.ip1 = L.InnerProduct(n.data, num_output=40, weight_filler=dict(type='xavier'))# transform the output through the ReLU (rectified linear) non-linearityn.relu1 = L.ReLU(n.ip1, in_place=True)# score the (now non-linear) featuresn.ip2 = L.InnerProduct(n.ip1, num_output=2, weight_filler=dict(type='xavier'))# same accuracy and loss as beforen.accuracy = L.Accuracy(n.ip2, n.label)n.loss = L.SoftmaxWithLoss(n.ip2, n.label)return n.to_proto()train_net_path = 'examples/hdf5_classification/nonlinear_auto_train.prototxt'
with open(train_net_path, 'w') as f:f.write(str(nonlinear_net('examples/hdf5_classification/data/train.txt', 10)))test_net_path = 'examples/hdf5_classification/nonlinear_auto_test.prototxt'
with open(test_net_path, 'w') as f:f.write(str(nonlinear_net('examples/hdf5_classification/data/test.txt', 10)))solver_path = 'examples/hdf5_classification/nonlinear_logreg_solver.prototxt'
with open(solver_path, 'w') as f:f.write(str(solver(train_net_path, test_net_path)))
%%timeit
caffe.set_mode_cpu()
solver = caffe.get_solver(solver_path)
solver.solve()accuracy = 0
batch_size = solver.test_nets[0].blobs['data'].num
test_iters = int(len(Xt) / batch_size)
for i in range(test_iters):solver.test_nets[0].forward()accuracy += solver.test_nets[0].blobs['accuracy'].data
accuracy /= test_itersprint("Accuracy: {:.3f}".format(accuracy))

Accuracy: 0.838

Accuracy: 0.837

Accuracy: 0.838

Accuracy: 0.834

1 loop, best of 3: 277 ms per loop

再次通过命令行界面执行同样的操作,以获得关于模型和求解的详细输出:

!./build/tools/caffe train -solver examples/hdf5_classification/nonlinear_logreg_solver.prototxt
I0224 00:32:05.654265   658 caffe.cpp:178] Use CPU.
I0224 00:32:05.810444   658 solver.cpp:48] Initializing solver from parameters:
train_net: "examples/hdf5_classification/nonlinear_auto_train.prototxt"
test_net: "examples/hdf5_classification/nonlinear_auto_test.prototxt"
......
I0224 00:32:06.078208   658 solver.cpp:406]     Test net output #0: accuracy = 0.8388
I0224 00:32:06.078225   658 solver.cpp:406]     Test net output #1: loss = 0.382042 (* 1 = 0.382042 loss)
I0224 00:32:06.078234   658 solver.cpp:323] Optimization Done.
I0224 00:32:06.078241   658 caffe.cpp:222] Optimization Done.
# Clean up (comment this out if you want to examine the hdf5_classification/data directory).
shutil.rmtree(dirname)

相关参考:BVLC/caffe: Caffe: a fast open framework for deep learning. (github.com)

我的NVIDIA开发者之旅——Caffe教程(3)使用sklearn和caffe进行简单逻辑回归实践相关推荐

  1. 我的NVIDIA开发者之旅——NVIDIA云原生技术

    NVIDIA云原生技术:耐心看完受益匪浅 第一篇->NVIDIA云原生技术 [1]NVIDIA容器工具包 [2]GPU操作员 [3]带GPU的Kubernetes [3.1]安装Kubernet ...

  2. 我的NVIDIA开发者之旅-Jetson Nano 2gb教你怎么训练模型(完整的模型训练套路)

    我的NVIDIA开发者之旅" | 征文活动进行中....... 模型的保存和加载 pytorch的安装方法这里就不写了,之前的文章有记录,nvidia官网的资料已经很详细了附上连接(注意你的 ...

  3. 我的NVIDIA开发者之旅——作为一名初学者,我是如何开启 NVIDIA Jetson Nano 开发的

    "我的NVIDIA开发者之旅" | 征文活动进行中- ⭐️ 作者:前端修罗场 ⭐️ 本文名称:我的NVIDIA开发者之旅--作为一名初学者,我是如何开启 NVIDIA Jetson ...

  4. 我的NVIDIA开发者之旅-Jetson在没有显示器的状态下使用Vnc操控GUI界面开发的三种实用技巧

    "我的NVIDIA开发者之旅" | 征文活动进行中....... 首先是因为小编是没有显示器用,想着用vnc操作图形界面,使用了nvidia自带的vnc远程方法是无效的在没显示器的 ...

  5. 我的NVIDIA开发者之旅——利用NVIDIA TAO工具包3.0和Deepstream快速搭建车辆信息识别系统

    利用NVIDIA TAO工具包3.0和Deepstream快速搭建车辆信息识别系统 实现目标 部署工具:NVIDIA DeepStream SDK 简单设置参数 工作流程 注意事项 GPU深度学习推理 ...

  6. 我的NVIDIA开发者之旅--从CUDA种草AI梦

    我的NVIDIA开发者之旅" | 征文活动进行中....... 一.什么是CUDA? CUDA(Compute Unified Device Architecture),是显卡厂商NVIDI ...

  7. Ubuntu 16.04系统下CUDA8.0配置Caffe教程

    由于最近安装了Ubuntu 16.04,本文教程的特点是不需要降级gcc的版本,毕竟cuda8.0已经支持gcc5以上(默认不支持,实际支持). 本文是在参考caffe官网教程以及http://www ...

  8. TensorRT加速 ——NVIDIA终端AI芯片加速用,可以直接利用caffe或TensorFlow生成的模型来predict(inference)...

    官网:https://developer.nvidia.com/tensorrt 作用:NVIDIA TensorRT™ is a high-performance deep learning inf ...

  9. 报名 | NVIDIA线下交流会:手把手教你搭建TensorFlow Caffe深度学习服务器

    7月21日(周六)下午14:30,量子位与NVIDIA英伟达开发者社区联合举办线下交流会,拥有丰富一线开发经验的NVIDIA开发者社区经理Ken He,将手把手教你搭建TensorFlow & ...

  10. 给iOS开发者的Sketch入门教程

    给iOS开发者的Sketch入门教程 作为一名iOS开发者,我经历过几个没有设计师的项目,结果就是,痛苦的一逼. 做这种类型的项目,设计是非常重要的,特别是迭代设计. 在每个项目最开始的时候,客户其实 ...

最新文章

  1. inux 下查看服务器负载均衡
  2. crm开源系统 tp框架_thinkphp6学习教程与源码 tp6开源CMS系统源码研究
  3. 三代测序技术特点比较
  4. More Effective C++读书笔记(二)
  5. 使用vmware克隆CentOS后网卡名称修改(强迫症)
  6. spring中事务的设计和实现
  7. MySQL中常见的单行函数(上)
  8. qt显示rgba8888 如何改 frame_Qt音视频开发2-vlc回调处理
  9. CodeForces - 1332D Walk on Matrix(构造)
  10. 前端学习(2866):自定义组件库效果演示
  11. 视图请求限定装饰器-101.课时101.【Django视图高级】限制请求method装饰器(Av61533158,P101)
  12. 开博啦——半路出家做运维以来的一些杂感
  13. Android 音频焦点(Audio Focus)
  14. PKD-Bert:基于多层网络的Bert知识蒸馏
  15. 数字孪生管理系统,智慧校园建设规划方案
  16. hdu6287(分解质因数+二分)
  17. Java编程入门基础知识合集
  18. 苹果16g不够用怎么办_孩子不够自信怎么办?父母学会用这4个方法,孩子长大更优秀自信...
  19. 电脑蓝屏---错误代码:0xc0000185,修复过程转0xc0000001,最后成功修复
  20. SwiftUI嵌入Stack样式导航视图(NavigationView)中List显示怪异的解决

热门文章

  1. 《精通Javascript+jQuery》视频教程
  2. A Easy Game(FZU 2146)
  3. 看英文PDF利器 智器+ColorDict 辞典
  4. vim文本编辑器的操作和命令(可作手册查询)
  5. BPM波导matlab,用Matlab画平板波导色散图详解.doc
  6. Zemax—波长1550nm不在所选玻璃色散公式的有效范围内
  7. Web前端性能优化优秀文章集锦
  8. 读书笔记-《细说PHP》
  9. 华为手机文档里的html,如何在华为手机上编辑word文档
  10. Microsoft SQL Server Management Studio的快捷功能 与 搜狗五笔冲突记