目前流行的深度学习库有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的更多相关文章

  1. Windows环境下的TensorFlow安装过程

    安装环境 Windows8.1 python3.5.x(TensorFlow only supports version 3.5.x of Python on Windows) pip 9.0.1 t ...

  2. windows下 zookeeper dubbo 安装+配置+demo 详细图文教程

    Java集群优化——dubbo+zookeeper构建 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这 ...

  3. windows10环境下的RabbitMQ安装步骤(图文)

    https://blog.csdn.net/weixin_39735923/article/details/79288578 记录下本人在win10环境下安装RabbitMQ的步骤,以作备忘. 第一步 ...

  4. windows10 环境下的amqp安装步骤(图文)

    安装PHP扩展ampq 查看phpinfo()信息 下载ampq扩展 下载地址:http://pecl.php.net/package/amqp 选择一个dll版本下载,本文选择的是1.9.3 自己根 ...

  5. windows10 环境下的RabbitMQ安装步骤(图文)

    第一步:下载并安装erlang 原因:RabbitMQ服务端代码是使用并发式语言Erlang编写的,安装Rabbit MQ的前提是安装Erlang. 下载地址:http://www.erlang.or ...

  6. 第一讲 Windows10系统下IDE-CLion的安装与配置

    01 为什么使用CLion?02 CLion安装方法03 CLion的基本使用04 课程形式及答疑说明 toc 参考链接: Window10上CLion极简配置教程 学生免费注册Pycharm专业版 ...

  7. window 64bit 下react navtive安装

    1.安装jdk 去这里安装对应的jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...

  8. windows10环境下的RabbitMQ安装_笔记

    原文:https://blog.csdn.net/weixin_39735923/article/details/79288578 第一步:下载并安装erlang原因:RabbitMQ服务端代码是使用 ...

  9. tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案

    tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案   一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...

随机推荐

  1. Java 中的多线程你只要看这一篇就够了

    引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个 ...

  2. opencv学习之路(13)、图像阈值化threshold

    一.图像阈值化简介 二.固定阈值 三.自适应阈值 #include<opencv2/opencv.hpp> using namespace cv; void main(){ Mat src ...

  3. PHP命名空间(Namespace)的使用简介

    原文链接:https://www.cnblogs.com/zhouguowei/p/5200878.html 可以导入命名空间也可以导入命名空间的类 <?php namespace Blog\A ...

  4. topcoder srm 707 div1

    1 构造一个棋盘,长宽n,m不超过50,每个格子为障碍或者非障碍两种,使得从(0,0)到(n-1,m-1)的最短路为给定的值k. 思路:如果k小于等于98,那么一定存在没有障碍的棋盘满足要求.否则,最 ...

  5. Python 解析 XML 文件生成 HTML

    XML文件result.xml,内容如下: <ccm> <metric> <complexity>1</complexity> <unit> ...

  6. linux内核中侧async_tx是什么?

    答: 异步内存传输api(asynchronous memory transfer/transform API),这是一种api,用来为应用提供操作DMA的API. 下图是async_tx在架构中所处 ...

  7. linux内核中的vgaarb是什么?

    答: vga仲裁器(vga arbiter),是内核中的一个模块

  8. POJ 1182 食物链(并查集+偏移向量)题解

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 82346   Accepted: 24616 Description ...

  9. Why database migrations?

    https://flywaydb.org/getstarted/why First, let's start from the beginning and assume we have a proje ...

  10. 【Spring Security】四、自定义页面

    在前面例子中,登陆页面都是用的Spring Security自己提供的,这明显不符合实际开发场景,同时也没有退出和注销按钮,因此在每次测试的时候都要通过关闭浏览器来注销达到清除session的效果. ...