windows10 64bit 下的tensorflow 安装及demo
目前流行的深度学习库有Caffe,Keras,Theano,本文采用谷歌开源的曾用来制作AlphaGo的深度学习系统Tensorflow。
1:安装Tensorflow
最早TensorFlow只支持mac和Linux系统,目前也支持windows系统,但要求python3.5 (64bit)版本。TensorFlow有cpu和gpu版本,由于本文使用服务器是NVIDIA显卡,因此安装gpu版本,在cmd命令行键入
pip install --upgrade tensorflow-gpu
如果出现错误“Cannot remove entries from nonexistent file”,执行以下命令
“pip install --upgrade -I setuptools”,安装成功出现以下界面

2:安装CUDA库
用gpu来运行Tensorflow还需要配置CUDA和cuDnn库,
用以下link下载win10(64bit)版本CUDA安装包,大小约为1.2G https://developer.nvidia.com/cuda-downloads
安装cuda_8.0.61_win10.exe,完成后配置系统变量,在系统变量中的CUDA_PATH中,加上三个路径, C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin\lib\x64
3:安装cuDnn库
用以下link下载cuDnn库
https://developer.nvidia.com/cudnn
下载解压后,为了在运行tensorflow的时候也能将这个库加载进去,我们要将解压后的文件拷到CUDA对应的文件夹下C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0
4:测试安装
在Pycharm新建一个py文件键入
import tensorflow as tf
hello = tf.constant('Hello world, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
如果能够输出'Hello, TensorFlow!'像下面这样就代表配置成功了。

1: 导入必要的模块
import sys
import cv2
import numpy as np
import tensorflow as tf
2: 定义CNN的基本组件
按照LeNet5 的定义,采用32*32 图像输入,CNN的基本组件包括卷积C1,采样层S1、卷积C2,采样层S2、全结合层1、分类层2
3:训练CNN
将输入图像缩小至32*32大小,采用opencv中的resize函数
其变换参数有
CV_INTER_NN - 最近邻插值,
CV_INTER_LINEAR - 双线性插值 (缺省使用)
CV_INTER_AREA - 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
CV_INTER_CUBIC - 立方插值.
output=cv2.resize(img,(32,32),interpolation=cv2.INTER_CUBIC)
核心代码如下:
class CNNetwork:
NUM_CLASSES = 2 #分两类
IMAGE_SIZE = 28
IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE*3
def inference(images_placeholder, keep_prob):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
x_image = tf.reshape(images_placeholder, [-1, 28, 28, 1])
with tf.name_scope('conv1') as scope:
W_conv1 = weight_variable([5, 5, 3, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
with tf.name_scope('pool1') as scope:
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('conv2') as scope:
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
with tf.name_scope('pool2') as scope:
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('fc1') as scope:
W_fc1 = weight_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
with tf.name_scope('fc2') as scope:
W_fc2 = weight_variable([1024, NUM_CLASSES])
b_fc2 = bias_variable([NUM_CLASSES])
with tf.name_scope('softmax') as scope:
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
return y_conv
if __name__ == '__main__':
test_image = []
filenames = []
for i in range(1, len(sys.argv)):
img = cv2.imread(sys.argv[i])
img = cv2.resize(img, (28, 28))
test_image.append(img.flatten().astype(np.float32)/255.0)
filenames.append(sys.argv[i])
test_image = np.asarray(test_image)
images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
keep_prob = tf.placeholder("float")
logits = inference(images_placeholder, keep_prob)
sess = tf.InteractiveSession()
saver = tf.train.Saver()
sess.run(tf.initialize_all_variables())
saver.restore(sess, "model.ckpt")
for i in range(len(test_image)):
pred = np.argmax(logits.eval(feed_dict={
images_placeholder: [test_image[i]],
keep_prob: 1.0 })[0])
pred2 = logits.eval(feed_dict={
images_placeholder: [test_image[i]],
keep_prob: 1.0 })[0]
print filenames[i],pred,"{0:10.8f}".format(pred2[0]),"{0:10.8f}".format(pred2[1])
windows10 64bit 下的tensorflow 安装及demo的更多相关文章
- Windows环境下的TensorFlow安装过程
安装环境 Windows8.1 python3.5.x(TensorFlow only supports version 3.5.x of Python on Windows) pip 9.0.1 t ...
- windows下 zookeeper dubbo 安装+配置+demo 详细图文教程
Java集群优化——dubbo+zookeeper构建 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这 ...
- windows10环境下的RabbitMQ安装步骤(图文)
https://blog.csdn.net/weixin_39735923/article/details/79288578 记录下本人在win10环境下安装RabbitMQ的步骤,以作备忘. 第一步 ...
- windows10 环境下的amqp安装步骤(图文)
安装PHP扩展ampq 查看phpinfo()信息 下载ampq扩展 下载地址:http://pecl.php.net/package/amqp 选择一个dll版本下载,本文选择的是1.9.3 自己根 ...
- windows10 环境下的RabbitMQ安装步骤(图文)
第一步:下载并安装erlang 原因:RabbitMQ服务端代码是使用并发式语言Erlang编写的,安装Rabbit MQ的前提是安装Erlang. 下载地址:http://www.erlang.or ...
- 第一讲 Windows10系统下IDE-CLion的安装与配置
01 为什么使用CLion?02 CLion安装方法03 CLion的基本使用04 课程形式及答疑说明 toc 参考链接: Window10上CLion极简配置教程 学生免费注册Pycharm专业版 ...
- window 64bit 下react navtive安装
1.安装jdk 去这里安装对应的jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- windows10环境下的RabbitMQ安装_笔记
原文:https://blog.csdn.net/weixin_39735923/article/details/79288578 第一步:下载并安装erlang原因:RabbitMQ服务端代码是使用 ...
- tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案
tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案 一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...
随机推荐
- 火车时刻表WebApp
关键词 :Ajax 跨域访问 php 同源策略 JQueryMobile 前言 在面试的过程中,兄弟连的徐老师提出要求我用JQuery Mobile(前端框架)来实现一个具有“火车时刻表”功能的Web ...
- 【python027--集合】
一.集合会剔除字典内重复的数字 >>> num = {1,2,3,3,4,5,5,6}>>> num{1, 2, 3, 4, 5, 6}>>> t ...
- phpMyAdmin - Error
centos7.5 原因:权限问题 解决方法:授权 [root@web01 code]# groupadd -g666 www [root@web01 code]# useradd -u666 -g6 ...
- java Swing小知识点
private JTextArea jta=new JTextArea(1,2); private ScrollPane sp=new ScrollPane(); private JPasswordF ...
- Python常用库之functools
functools 是python2.5被引人的,一些工具函数放在此包里. python2.7中 python3.6中 import functools print(dir(functools)) [ ...
- CentOS7.2 安装Docker
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 uname -r 命令查看你当前的内核版本 [roo ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- Many Website Of WallPaper
我在这里给大家推荐几个不错的壁纸网站 毕竟一张赏心悦目的壁纸能让你的工作效率提高不少 注意前方高能 一大波网站即将来袭 一系列 如你所见 alphacoders wallpaperdj Wallhav ...
- SQL 多行合并一行
select stuff((select ',' + CONVERT(VARCHAR(50),id)+'' from tab_menu group by id for xml path('')), ...
- [bootstrapValidator] - bootstrap的验证工具
翻了下之前和同事做的一个验证 <!--bootstrapValidator--> <script type="text/javascript" th:inline ...