$ ./configure You have bazel 3.7.2 installed. Please specify the location of python. [Default is /Users/qkboo/.pyenv/versions/tf2_build/bin/python3]:
Found possible Python library paths: /Users/qkboo/.pyenv/versions/tf2_build/lib/python3.8/site-packages Please input the desired Python library path to use. Default is [/Users/qkboo/.pyenv/versions/tf2_build/lib/python3.8/site-packages]
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n No OpenCL SYCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with ROCm support? [y/N]: n No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n Not configuring the WORKSPACE for Android builds.
Do you wish to build TensorFlow with iOS support? [y/N]: y iOS support will be enabled for TensorFlow.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=ngraph # Build with Intel nGraph support. --config=numa # Build with NUMA support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. --config=v2 # Build TensorFlow 2.x instead of 1.x. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished
빌드 시작
Intel 기반의 macOS에서 기본 배포되는 tensorflow@2.0 을 설치하고 tensorflow 를 실행하면,
1 2 3 4 5 6 7 8 9 10 11
(tf2_p37)~$ python Python 3.7.9 (default, Aug 31 2020, 07:22:35) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license"for more information. >>> import tensorflow as tf >>> >>> tf.add(1,2).numpy() 2021-02-01 14:11:37.000601: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations: SSE4.1 SSE4.2 AVX AVX2 FMA To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags. 2021-02-01 14:11:37.001166: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance. 3
특화 명령 을 –copt 부분에 명시해서 빌드한다.
bazel build: Intel CPU 특화 명령 세트
빌드할 때 아래와 같이 cpu option을 추가로주면 해당 cpu에 대해 최적화된 빌드로 진행됩니다. 아래의 copt 들은 머신마다 다르며, tensorflow warning을 보고 적절히 추가해주면 됩니다.
$ ./configure You have bazel 3.7.2 installed. Please specify the location of python. [Default is /Users/qkboo/.pyenv/versions/miniconda3-latest/bin/python3]: /Users/qkboo/.pyenv/versions/tf2_p38/bin/python
Found possible Python library paths: /Users/qkboo/.pyenv/versions/tf2_p38/lib/python3.8/site-packages Please input the desired Python library path to use. Default is [/Users/qkboo/.pyenv/versions/tf2_p38/lib/python3.8/site-packages]
Do you wish to build TensorFlow with ROCm support? [y/N]: n No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -Wno-sign-compare]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n Not configuring the WORKSPACE for Android builds.
Do you wish to build TensorFlow with iOS support? [y/N]: y iOS support will be enabled for TensorFlow.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details. --config=mkl # Build with MKL support. --config=mkl_aarch64 # Build with oneDNN support for Aarch64. --config=monolithic # Config for mostly static monolithic build. --config=ngraph # Build with Intel nGraph support. --config=numa # Build with NUMA support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. --config=v2 # Build TensorFlow 2.x instead of 1.x. Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished
>>> t = tf.constant('logcg') 2020-01-18 20:04:43.088104: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fefdc983db0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-01-18 20:04:43.088134: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
(cv3python3) ~/$ python Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
$ python Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
시스템에서 파이썬이 두 개 이상 설치되어 있으면 일반적을 Python2.7 버전이 기본 파아썬 이다.
1 2 3 4 5 6 7 8
$ python3 Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0-dev' >>>
만약 main module과 extra module을 같이 사용하고 싶다면 아래처럼 설치합니다.
1
> pip install opencv-contrib-python
conda 명령으로 설치하기
Anaconda 3가 설치되어 있다면 명령어 한 줄 입력만으로도 거의 최신 버전(글 작성시 버전 3.6.0)의 Python-OpenCV 라이브러리를 설치할 수 있다:
1
> conda install -c conda-forge opencv
conda-forge에 등록되어 있는 OpenCV 라이브러리에 대한 상세한 설명은 링크를 통해 확인할 수 있습니다. 필요한 경우, python virtualenv를 생성하여 개발환경 패키지를 관리할 수 있다.
OpenCV 3.3의 가장 큰 변화는 Deep Learning in OpenCV 라고 할 수 있겠습니다. 해당 링크에서 Deep Learning 관련 추가된 사항을 확인할 수 있습니다. 이외에 자세한 변경 로그는 OpenCV Change Logs Version:3.3을 참고하시면 되겠습니다.
여담으로 최신 버전 OpenCV 3.3.1 버전 이후에는 JavaScript Interface가 추가되어 인터랙티브 웹-기반 OpenCV 어플리케이션 구현이 공식적으로 가능해졌다고 한다.
$ python Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
시스템에서 파이썬이 두 개 이상 설치되어 있으면 일반적을 Python2.7 버전이 기본 파아썬 이다.
1 2 3 4 5 6 7 8
$ python3 Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0-dev' >>>
Python 가상환경과 Opencv
파이썬 개발시 virtualenv 를 사용한다면 OpenCV 라이브러리를 연결해 줄 필요가 있다. virtualenv 가상환경 생성시 파이썬 라이브러리는 복사가 안된다 그래서 가상환경 생성후에 cv2.so 라이브러리를 링크해줄 필요가 있다.
가상환경에 cv2.so 연결하기
python2 가상환경 cv3python2 가 있고, 여기에 OpenCV 를 사용하려면 다음 같이 cv2.so 라이브러리를 링크해 준다.
1 2
$ cd ~/.virtualenvs/cv3python2/lib/python2.7/site-packages/ $ ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
역시 python3 가상환경 cv3python3 가 있다면 python3 라이브러리에 있는 cv2.so 라이브러리를 링크해 준다.
1 2
$ cd ~/.virtualenvs/cv3python3/lib/python3.4/site-packages/ $ ln -s /usr/local/lib/python3.4/dist-packages/cv2.cpython-34m.so cv2.so
또한 cv2.so를 사용하기 위해서 각 가상환경에 numpy를 설치해 준다. Raspberry Pi 2에서 numpy를 pip로 설치할 때 약 10분 이상 소요된다.
(cv3python3) ~/$ python Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
2015년 쯤 작성한 것으로 Raspberry Pi, Orange Pi, Odroid SBC 보드에서 3.4를 빌드했다. 2017년 현재 배포본, Debian Jessie, Ubuntu Xeniel 등은 3.4, 3.5가 내장되어 배포되고 있다. Python 3.6 이상 최신 소스를 빌드하는데 참고할 수 있다.
Build Python 3.4
Raspbian Wheezy에는 3.2가 설치되어 있다. Jessie 가 출시된 이후에 3.2에 대한 모듈 의존성 관리가 되지 않고 있어서 3.4 이상이 설치가 필요하다.