序
Hi, 大家好。
我是个人开发者 红泥.
这是一篇如何快速在Win10上运行Tensorflow的笔记,写此篇的目的为了让跟多国内的开发者和兴趣爱好者快速上手。
我尝试了各种方式,目前此方式能够很好的使用到GPU在Windows下做数据训练。
Docker for Windows 不知道如何使用GPU版本。如有大牛,还望指点。
运行环境
- Windows 64 位 (笔者是 Win10 64 专业版)
- Python 3.5+ (必须是64位)
- vs community 2015 for c++
- Pip
- Git
- GTX 960
ps:注意检查pip是否正确安装 ,不能运行在python 2.7.*,不支持AMD的显卡。
推荐使用 Windows PowerShell 代替 CMD
安装配置
- 修改pip国内源,创建pip.ini文件 (非常重要)
%APPDATA%\pip\pip.ini(Win10 路径)其它
[global]
timeout = 6000
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/
- Tensorflow 安装
方式一:
pip install --upgrade tensorflow #CPU版本
pip install --upgrade tensorflow-gpu #GPU版本
方式二:
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl (CPU版本)
pip install --upgrade .\tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-0.12.0rc0-cp35-cp35m-win_amd64.whl (GPU版本)
pip install --upgrade .\tensorflow_gpu-0.12.0rc0-cp35-cp35m-win_amd64.whl
官网最新下载地址
- CUDA 安装 (CPU版本忽略)
- CUDNN 安装 (CPU版本忽略)
https://developer.nvidia.com/cudnn
解压后覆盖至CUDA的安装目录下
例如:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\
Hello TensorFlow
$ python
...
>>>>import tensorflow as tf
>>>>hello = tf.constant('Hello, TensorFlow!')
>>>>sess = tf.Session()
>>>>print(sess.run(hello))
Hello, TensorFlow!
>>>>a = tf.constant(10)
>>>>b = tf.constant(32)
>>>>print(sess.run(a + b))
42
运行 Demo :TensorFlow For Poets
- 下载Demo
迅雷:http://download.tensorflow.org/example_images/flower_photos.tgz
解压至:D:\tf_files\flower_photos
git clone https://github.com/tensorflow/tensorflow.git
- 训练数据
cd ./tensorflow
python tensorflow/examples/image_retraining/retrain.py --bottleneck_dir=/tf_files/bottlenecks --how_many_training_steps 500 --model_dir=/tf_files/inception --output_graph=/tf_files/retrained_graph.pb --output_labels=/tf_files/retrained_labels.txt --image_dir /tf_files/flower_photos
- 使用训练数据
label_image.py
import tensorflow as tf, sys
image_path = sys.argv[1]
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, \
{'DecodeJpeg/contents:0': image_data})
# Sort to show labels of first prediction in order of confidence
top_k = predictions[0].argsort()[-len(predictions[0]):][::-1]
for node_id in top_k:
human_string = label_lines[node_id]
score = predictions[0][node_id]
print('%s (score = %.5f)' % (human_string, score))
运行测试
python /tf_files/label_image.py /tf_files/flower_photos/daisy/21652746_cc379e0eea_m.jpg
结尾
参考文献
- https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html#pip-installation-on-windows
- https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html#optional-install-cuda-gpus-on-linux
- https://codelabs.developers.google.com/codelabs/tensorflow-for-poets
- https://github.com/tensorflow/tensorflow
- http://www.cnblogs.com/ccdc/p/4122641.html
ps:如有遗漏请及时联系我. xbhuang1994@gmail.com