텐서플로우 컴파일 설치하기

텐서플로우 GPU 버전을 설치하면 최고인데, CPU 버전만 사용해야 한다. CPU 버전을 쓰면 아래와 같이 메세지가 나온다.

(tensorflow) now0930@:cifar10$ python cifar10_testv1.py 
Using TensorFlow backend.
Couldn't import dot_parser, loading of dot files will not be possible.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896       
_________________________________________________________________
dropout_1 (Dropout)          (None, 32, 32, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 32, 32, 32)        9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 16, 16)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 8192)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               4194816   
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
=================================================================
Total params: 4,210,090
Trainable params: 4,210,090
Non-trainable params: 0
_________________________________________________________________
None
Train on 50000 samples, validate on 10000 samples
Epoch 1/25
2017-06-17 08:55:41.662236: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-17 08:55:41.663359: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-17 08:55:41.663384: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
  288/50000 [..............................] - ETA: 707s - loss: 2.3271 - acc: 0.1076

연산 시간이 CPU 최신 기술로 줄어든다는데, 전에 해보렸으나 적당한 tutorial이 없어 못했다. 그러던 중, 아래 사이트를 찾았다.
텐서플로우, 직접 소스코드 빌디하여 설치하기

정리하면..

  1. Git에서 clone으로 다운로드
    $ git clone https://github.com/tensorflow/tensorflow
    $ cd tensorflow
    $ git checkout r1.2  #r은 release..적용 버전이 1.2
  2. tensorflow을 컴파일하기 위한 Bazel 설치
    세가지 방법으로 Bazel 설치 가능하다. 나는 debian을 사용하므로, compile 했다.

    1. Install required Package
      apt-get install pkg-config zip g++ zlib1g-dev unzip
    2. Download..
    3. 실행 권한을 주고 run
    4. bazel 경로를 PATH 변수에 추가
      export PATH="$PATH:$HOME/bin"
  3. tensorflow 컴파일 옵션 설정..configure..
    #아까 다운로드 받은 tensorflow 디렉토리에서..
    ./configure
  4. Bazel로 빌드하기
    bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

    –config=opt 옵션이 아까 경고 부분을 해결한다.

  5. Pip 설치용 whl 만들기
    $./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

    bazel-bin이 링크다. 경로를 잘 확인한다. 패키지 파일을 /tmp 디렉토리에 넣어야 되는지 모르겠다.

  6. conda 가상 환경 활성화..
  7. 기존 tensorflow 설치버전 무시하고 설치
    (tensorflow)$ pip install --ignore-installed --upgrade tfBinaryURL

    tfBinaryURL이 아까 만든 /tmp/tensorflow_pkg의 whl 경로이다.

설치 후, python을 다시 실행하면 해당 메세지가 없어졌다.

(tensorflow) now0930@:cifar10$ python cifar10_testv1.py 
Using TensorFlow backend.
Couldn't import dot_parser, loading of dot files will not be possible.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 32, 32, 32)        896       
_________________________________________________________________
dropout_1 (Dropout)          (None, 32, 32, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 32, 32, 32)        9248      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 16, 16)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 8192)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 512)               4194816   
_________________________________________________________________
dropout_2 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
=================================================================
Total params: 4,210,090
Trainable params: 4,210,090
Non-trainable params: 0
_________________________________________________________________
None
Train on 50000 samples, validate on 10000 samples
Epoch 1/25

설치하기 전에는 속도가 많이 빨라질 것이라 기대 했으나..별 차이 없어보인다.

코멘트

댓글 남기기

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.