Total Pageviews

Tuesday 6 March 2018

安装Tensorflow

Tensorflow 是 Google 开源的机器学习框架。通过提供简单可用的 API 来让开发者可以轻松的实现各种机器学习和深度神经网络学习。并且通过优化还可以运行在 Android 和 iOS 等移动平台。这一篇文章是最基础的安装说明。注意,文章具有时效性,最新文档建议你参考阅读官方。

安装 Conda

  • 首先安装 Conda 虚拟环境,这样可以在统一机器中存在多个 python 环境和版本,切换和管理都比较方便。安装也比较简单,安装之后加入目录到环境变量中。
1
2
3
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
chmod +x Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh
  • 创建 tensorflow 环境
1
conda create -n tensorflow python=3.5 # -n 后参数为环境名字,你也可以使用其它名字。
  • 切换当前环境到 Tensorflow
1
source activate tensorflow
  • 其它命令
1
2
3
source deactivate # 退出当前环境
conda remove -n 环境名字 --all # 删除一个环境
conda info -e #列出当前所有环境并显示当前是在那个环境中

TensorFlow 安装

直接使用 pip 工具安装
1
pip install --ignore-installed --upgrade TF_PYTHON_URL
进入 ipython 进行测试
1
2
3
4
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
输出结果:
1
Hello, TensorFlow!
安装成功。

相关文档

官方安装教程:https://www.tensorflow.org/install/
官方开始入门教程:https://www.tensorflow.org/get_started/get_started
------------------------
 
相关帖子:

在Windows 上,安装 tensorflow-gpu

----------------------------------------------

深度学习的自动对对联开源系统eq2seq-couplet

eq2seq-couplet是基于深度学习Tensorflow编写的开源对对联系统,作者从网上抓了几百万的对联数据,用 seq2seq 模型做为自然语言处理技术 ,普通PC计算一幅对联可能需要几秒时间。

自行搭建时,需要以下环境以及数据集:
    Tensorflow
    Python 3.6
    Dataset (https://github.com/wb14123/couplet-dataset)

[repo owner=”wb14123″ name=”seq2seq-couplet”]

------------------------------------------------------------------

人工智能学习系统TensorFlow的Swift版

谷歌开源发布了 Swift for TensorFlow, 已在 GitHub 上开源。Swift for TensorFlow 为 TensorFlow 提供了新的编程模型,将 TensorFlow 计算图与 Eager Execution 的灵活性和表达能力结合在一起。Swift 自动分析  Tensor 编码并生成图 。同时 Swift 在运行代码前捕捉Tensor 代码的错误。 此外用户可以直接通过 Swift 代码使用任意 Python API。目前项目还处于早期开发阶段,不建议直接应用于生产环境。

[repo owner=”tensorflow” name=”swift”]

--------------------------------------------------------

TensorFlow 2.x version's Tutorials and Examples, including CNN, RNN, GAN, Auto-Encoders, FasterRCNN, GPT, BERT examples, etc. TF 2.0版入门实例代码,实战教程。

TensorFlow 2.0 Tutorials

Our repo. is the Winner of ⚡#PoweredByTF 2.0 Challenge!.

Timeline:

Installation

make sure you are using python 3.x.

  • CPU install
pip install tensorflow -U
  • GPU install

Install CUDA 10.0(or after) and cudnn by yourself. and set LD_LIBRARY_PATH up.

pip install tensorflow-gpu  -U

Test installation:

In [2]: import tensorflow  as tf

In [3]: tf.__version__
Out[3]: '2.0.0'
In [4]: tf.test.is_gpu_available()
...
totalMemory: 3.95GiB freeMemory: 3.00GiB
...
Out[4]: True

配套TF2视频教程

 

TensorFlow 2.0的视频教程链接:深度学习与TensorFlow 2实战

Acknowledgement

  • 爱可可-爱生活 友情推荐 

Includes

  • TensorFlow 2.0 Overview
  • TensorFlow 2.0 Basic Usage
  • Linear Regression
  • MNIST, FashionMNIST
  • CIFAR10
  • Fully Connected Layer
  • VGG16
  • Inception Network
  • ResNet18
  • Naive RNN
  • LSTM
  • ColorBot
  • Auto-Encoders
  • Variational Auto-Encoders
  • DCGAN
  • CycleGAN
  • WGAN
  • Pixel2Pixel
  • Faster RCNN
  • A2C
  • GPT
  • BERT
  • GCN

Feel free to submit a PR request to make this repo. more complete!

Refered Repos.

Our work is not built from scratch. Great appreciation to these open works!


from https://github.com/dragen1860/TensorFlow-2.x-Tutorials
------------------------------------------------------------
 

Windows10上,搭建 TensorFlow, 试玩 fast-style-transfer 

    本文适用于 TensorFlow 新手搭建试玩“图片快速风格迁移”,系统环境:

    • Windows 10
    • 显卡 NVIDIA GTX 1070

    安装 TensorFlow 环境

    Anaconda 来装环境可以省很多事。

    1. 安装 Anaconda,步骤中有两个选项记得勾上(添加 PATH 环境变量和使用 Anaconda 作为 Python 环境)

    2. 现在的 Anaconda 似乎已经内置了国内的几个镜像源,所以不用手动切换镜像,用默认配置即可

      如果已经折腾过,可以考虑用如下命令恢复:

    conda config --remove-key channels
    
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
    
    1. conda config --set show_channel_urls yes
      
    2. 如果你像我一样只是个入门玩家(并且平时没装过 Python 环境),那就不用搞 conda 的环境隔离了。直接执行 conda install tensorflow-gpu,然后等待

    如果一切顺利的话 Tensorflow 环境就装好了(如果不顺利的话可能多半是网络问题,重试执行下试试)。

    目前我用默认参数装好的 TF 版本是 1.14,lengstrom/fast-style-transfer 刚好只能在 TF v1.x 上用。

    图片快速风格迁移

    克隆仓库,然后下载已经训练好的风格,放到 model 目录下。

    弄好以后就可以开始玩了,仓库根目录下执行:

    python evaluate.py --checkpoint model/udnie.ckpt --in-path eval/ --out-path result/
    

    如果报错,请参考报错处理解决。

    如果要创建新风格,需要自己训练。先下载训练所需数据:

    不要下其他的,就用 beta16 目录下的,否则后面训练时会报错。新手入门就按部就班吧,别折腾。

    下载好后将 train2014.zip 解压到 data 目录下,imagenet-vgg-verydeep-19.mat 直接放到 data 目录下,可参考根目录下的 setup.sh 脚本。

    ├─data
    
    │  │  imagenet-vgg-verydeep-19.mat
    
    │  │
    
    │  ├─bin
    
    │  └─train2014
    
    │          COCO_train2014_000000000009.jpg
    
    │          COCO_train2014_000000000025.jpg
    

    然后根目录下执行:

    python style.py --style style/1.jpg --checkpoint-dir checkpoint --content-weight 1.5e1 --checkpoint-iterations 1000 --batch-size 12
    

    大概经过 6 小时以后训练完成,在 checkpoint 目录下生成了模型文件:

    checkpoint
    
    fns.ckpt.data-00000-of-00001
    
    fns.ckpt.index
    
    fns.ckpt.meta
    

    将 fns.ckpt.meta 复制一个,重命名为 fns.ckpt,然后就可以使用评估方法来生成图片了:

    python evaluate.py --checkpoint checkpoint/fns.ckpt --in-path eval/ --out-path result/
    

    报错处理

    1. AttributeError: 'module' object has no attribute 'imread',执行:
  • pip install --upgrade scipy==1.1.0
    
  • ImportError: No module named moviepy.video.io.VideoFileClip,执行:
    1. pip install moviepy
      
    2. cudaGetDevice() failed. Status: CUDA driver version is insufficient for runtime version,升级显卡驱动就行了
    3. tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape,降低 --bach-size 参数就行了,8G 显存的话设置为 12

    其他好玩的库

    • anishathalye/neural-style

      该仓库的输入是待处理图片 + 风格图片,边训练边生成,优点是方便切换风格,缺点是每次生成大概需要好几分钟。imagenet-vgg-verydeep-19.mat 和 train2014 可以用上面下载好的。

  • python neural_style.py --content examples/test.png --styles examples/style/udnie.jpg --output test-result.jpg
    
  • 如果觉得本地搭建 TensorFlow 太麻烦,可以通过 IBM/MAX-Fast-Neural-Style-Transfer(PyTorch)用 Docker 搭建试玩,或者直接在线试玩.

    from http://web.archive.org/web/20240720224156/https://88250.b3log.org/articles/2020/03/15/1584270480065.html

 

 
 

 

 

No comments:

Post a Comment