2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Windowsにおけるtensorflow1.14.0 をビルドして、Visual Studioで使えるようにするまで

Last updated at Posted at 2019-10-25

事のきっかけ

macOS、Ubuntuとtensorflowを始める為のメモとして書いてきたけど、そういえばWindowsだけそのまま書いていなかったと思い、これもメモとして残しておこうと思った次第。

最近は、マイクロソフト社の気が変わったのか、インターフェイスがOSもWebサイト側も良い感じになってきた気がする。知らんけど。(何時もの関西風)

注意

最後のincudeとlibがまだ安定していないので、参考程度にしてください。

準備

  • Visual Studio Communityのインストール

1.https://visualstudio.microsoft.com/ja/ にアクセスして、インストーラーをダウンロードし、Visual Studioをインストールする。とても親切に案内してくれるので、わかるはず。

  • Chocolateyのインストール

1.PowerShellを管理者権限で起動。

2.Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
を実行して、インストール。

3.choco install visualstudio2017buildtoolsを実行し、Visual Studio 2017のビルドツールをインストールする。

4.Windowsメニューに従来のvisual studio installerが追加されているので起動し、" ワークロード "の" Visual C++ Build Tools "にチェックを入れ、インストールを行う。

  • Cygwinのインストール
  1. https://www.cygwin.com にアクセスし、" Install Cygwin by running "の隣に書いてある " setup...."をクリックし、ダウンロードする。

2.起動後、基本的にそのまま次へを押すが、サーバー選択では基本的にJPサーバーを選んだ方が良い。

3.viewを" Category " 、 Searchに " default " と入力して検索し、Develの所を installに変えて、インストールを完了させる。

  • Bazelのインストール

1.https://github.com/bazelbuild/bazel/releases/tag/0.26.1 にアクセスし、bazel-0.26.1-windows-x86_64.zipをダウンロードして解凍させる。

  • PATHを設定

1.コントロールパネル->システムとセキュリティ->システム->システムの詳細設定->環境変数->ユーザー環境変数(又は、システム環境変数)->Pathを編集

2.以下を追記する。

[cygwinの場所]¥bin
[bazelの場所]
  • pythonのインストール

1.https://www.python.org/downloads/ にアクセスして、3.7系のインストーラーをダウンロードし、インストールする。必ず、add python 3.7 to PATHにチェック。

2.下記のコマンドを実行し、インストール。

pip install six bumpy wheel
pip install keras_application==1.0.6 --no-deps
pip install keras_preprocessing==1.0.5 --no-deps
  • Widnows日本語版におけるトラップ回避

1.コントロールパネル->時計と地域->地域->管理タブ->システムロケールの変更->UTF-8使用にチェック->OKボタン->再起動

2.再起動後、UTF-8ベースのWindowsに様変わり。ただし、大半のユーザーがSJISの為、文字化け沢山。盛り沢山。

  • tensorflowをビルド

1.コマンドプロンプト(管理者権限)を起動し、git cloneしても良い場所に移動。

2.git clone https://github.com/tensorflow/tensorflow.gitを実行しクローン。

3.git checkout r2.0で2.0系に移動。

4.python configure.pyを実行し、質問に答えていく。基本的にYes or No形式の質問は全てNOを選択し、[Default ...]といったYes or Noとは違う形式の質問は、基本的にエンターキーを押す。

5.bazel build --config=opt //tensorflow:tensorflow.dll

  • visual studioに反映させる

1.C++コンソールアプリケーションで、新規プロジェクトを作成

2.プロジェクトフォルダ内にincludeフォルダとlibフォルダを作成

3.下記を参考に必要なものをコピー

includeフォルダに入れるもの

- tensorflow/bazel-genfiles/*
- tensorflow/tensorflow/cc
- tensorflow/tensorflow/core
- tensorflow/tensorflow/c
- tenosrflow/thirdparty
  1.thirdparty/eigen3の中に入れるもの
  - tensorflow/bazel-tensorflow/external/eigen_archive/unsupported
  - tensorflow/bazel-tensorflow/external/eigen_arfhive/Eigen(includeフォルダ直下にも)
- git clone https://github.com/abseil/abseil-cpp.gitしたフォルダの中にあるabslフォルダ


libフォルダに入れるもの
- tensorflow/bazel-bin/tensorflow/tensorflow.dll.if.lib

プロジェクトフォルダに入れるもの
- tensorflow/bazel-bin/tensorflow/tensorflow.dll

  • visual studio プロパティ設定

1.VSを起動し、プロジェクトのプロパティを表示させる。
2.Cファイルを作成していれば、"C/C++"の設定項目が増えているので、それの全般にある"追加のインクルードディレクトリ"に、$(SolutionDir)include;を追記し、プリプロセッサの"プリプロセッサの定義"に、COMPILER_MSVC;を追記する。
3.設定項目"リンカー"の全般にある"追加のライブラリディレクトリ"に$(SolutionDir)libを追記し、"入力"の"追加の依存ファイル"にtensorflow.dll.if.lib;を追記する。

まとめ

自分が調べて限りでは、GithubのTensorflowにあるIssuesで"Complete support for building C++ API on windows using Bazel"という露骨に欲しい情報が書かれてそうなタイトルに辿り着くまでが大変だった。Tensorflowを使った開発者達って、macOSとUbuntuの割合が多いのかもしれない。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?