Tensorflow2.3r CPU编译过程记录

编译Tensorflow2.2.0 CPU 版本

  1. 安装 Anaconda3-2020.02-Windows-x86_64.exe (管理python)

  2. 安装 Visual Studio 2019 配置VS2019编译环境 (Windows 需要) 添加系统变量 BAZEL_VC C:\Program Files (x86)\ Microsoft Visual Studio\2019\Community\VC
    BAZEL_VS C:\Program Files (x86)\ Microsoft Visual Studio\2019\Community

  3. 使用conda创建python虚拟环境 conda create -n xxx python=3.7

  4. 转到新创建的虚拟环境下 conda activate xxx

  5. 安装编译过程需要用到的模块(如果下载速度慢,可以修改pip源) pip install numpy six keras_preprocessing
    conda install protobuf

  6. 安装 git 和 bazel2.0.0
    windows: 直接配置bazel.exe的环境变量
    linux : sudo rm -rf ~/.bazel
    sudo rm -rf ~/bin
    sudo rm -rf /usr/bin/bazel
    sudo cp bazel /usr/bin/bazel
    sudo chmod a+x /usr/bin/bazel

  7. 安装 msys2 pacman -S zip unzip patch diffutils git (Windows需要)

  8. 下载tensorflow源码 https://github.com/tensorflow/tensorflow/releases/tag/v2.3r

  9. cd tensorflow

  10. 在创建的python虚拟环境下 python configure.py

                                                Please specify the location of python. [Default is xxx\Anaconda3\envs\pythonVersion3\python.exe]:
    
    
                                                Found possible Python library paths:
                                                xxx\Anaconda3\envs\pythonVersion3\lib\site-packages
                                                Please input the desired Python library path to use.  Default is [xxx\Anaconda3\envs\pythonVersion3\lib\site-packages]
    
                                                Do you wish to build TensorFlow with ROCm support? [y/N]:
                                                No ROCm support will be enabled for TensorFlow.
    
                                                Do you wish to build TensorFlow with CUDA support? [y/N]:
                                                No CUDA support will be enabled for TensorFlow.
    
                                                Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:
    
    
                                                Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
                                                Not overriding eigen strong inline, some compilations could take more than 20 mins.
    
                                                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.
    
  11. 开始使用bazel编译
    编译tensorflow_cc.dll : bazel --output_user_root="…/output_user_root" build --noincompatible_do_not_split_linking_cmdline --verbose_failures --config=opt //tensorflow:tensorflow_cc.dll
    编译tensorflowlite.dll : bazel --output_user_root="…/output_user_root" build --noincompatible_do_not_split_linking_cmdline --verbose_failures --config=opt //tensorflow/lite:tensorflowlite.dll

注: 编译会占用大量的CPU,建议不要在个人的电脑编译
编译过程会下载第三方库,放到output_user_root 中***此部分的库不要删掉***,使用tensorflow库时需要添加部分第三库的.lib

使用tensorflow lite模块需要在VS中配置以下内容
添加的头文件:
tensorflow2.3
flatbuffers

添加的库路径:
tensorflow2.3\tensorflow\lite
external

需要添加的链接库:
tensorflowlite.dll.if.lib
allocation.lib
util.lib
external_cpu_backend_context.lib
arena_planner.lib
simple_memory_arena.lib
c/common.lo.lib
core/api/api.lib
delegates\nnapi\nnapi_delegate.lib
experimental\resource\resource.lib
nnapi\nnapi_util.lib
nnapi\nnapi_implementation.lib
framework_lib.lo.lib
kernels\builtin_ops_all_linked.lo.lib
minimal_logging.lib
string_util.lib
kernels\builtin_op_kernels.lib
kernels\internal\audio_utils.lib
kernels\internal\kernel_utils.lib
kernels\internal\quantization_util.lib
kernels\internal\tensor_utils.lib
kernels\internal\portable_tensor_utils.lib
kernels\internal\transpose_utils.lib
kernels\internal\sse_tensor_utils.lib
kernels\kernel_util.lib
kernels\lstm_eval.lib
kernels\cpu_backend_context.lib
kernels\cpu_backend_gemm.lib
kernels\eigen_support.lib
tools\optimize\sparsity\format_converter.lib
fft2d\fft2d.lib
farmhash_archive\farmhash.lib
flatbuffers\src\flatbuffers.lib

编译android tflite:

python configure.py
You have bazel 3.2.0 installed.
Please specify the location of python. [Default is D:\SoftWare\Anaconda3\envs\pythonVersion3\python.exe]:

Found possible Python library paths:
D:\SoftWare\Anaconda3\envs\pythonVersion3\lib\site-packages
Please input the desired Python library path to use. Default is [D:\SoftWare\Anaconda3\envs\pythonVersion3\lib\site-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]:
No CUDA support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is /arch:AVX]:

Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is C:/Users/Admin/AppData/Roaming/Android/Sdk/ndk-bundle]: D:/SoftWare/Android/Sdk/ndk/21.3.6528147

WARNING: The NDK version in D:/SoftWare/Android/Sdk/ndk/21.3.6528147 is 21, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.

Please specify the (min) Android NDK API level to use. [Available levels: [‘16’, ‘17’, ‘18’, ‘19’, ‘21’, ‘22’, ‘23’, ‘24’, ‘26’, ‘27’, ‘28’, ‘29’, ‘30’]] [Default is 21]: 21

Please specify the home path of the Android SDK to use. [Default is C:/Users/Admin/AppData/Roaming/Android/Sdk]: D:/SoftWare/Android/Sdk

Please specify the Android SDK API level to use. [Available levels: [‘30’, ‘30.backup’]] [Default is 30.backup]: 30

Please specify an Android build tools version to use. [Available versions: [‘30.0.0’]] [Default is 30.0.0]:

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.

bazel build -c opt --cxxopt=–std=c++11 --config=android_arm64 //tensorflow/lite/c:tensorflowlite_c OK
bazel --output_user_root="…/output_user_root" build -c opt --cxxopt="–std=c++11" --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cpu=armeabi-v7a --config=android_arm //tensorflow/lite/java:tensorflowlite

bazel --output_user_root="…/output_user_root" build --cxxopt="–std=c++11" -c opt --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=armeabi-v7a --config=android_arm //tensorflow/lite/java:tensorflow-lite

bazel --output_user_root="…/output_user_root" build -c opt //tensorflow/lite:libtensorflowlite.so --crosstool_top=//external:android/crosstool --cpu=armeabi-v7a --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --cxxopt="-std=c++11" --verbose_failures --noincompatible_do_not_split_linking_cmdline --config=android

D:/SoftWare/Android/Sdk/ndk/21.3.6528147
D:/SoftWare/Android/Sdk/ndk/android-ndk-r13b

出现以下编译错误说明路径太深
external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/lib/…/lib/armv7-a/libatomic.a

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
花卉识别是计算机视觉中的一个重要应用,可以通过图像识别技术自动识别图像中的花卉种类。下面是一个基于TensorFlow 2.3实现的花卉识别程序示例。 首先,我们需要准备好训练数据和测试数据。可以使用公开的花卉数据集,例如Oxford 102花卉数据集。这个数据集包含了102种不同的花卉,每种花卉有大约80张图片。可以通过以下链接下载数据集: http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html 下载完成后,我们需要将数据集进行处理,将其转换为模型可以处理的形式。可以使用Python中的PIL库进行图片处理,并使用TensorFlow中的ImageDataGenerator类来进行数据增强和批量处理。 ```python import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator # 定义数据增强和预处理函数 train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) # 加载训练数据和测试数据 train_data = train_datagen.flow_from_directory( 'flower_dataset/train', target_size=(224, 224), batch_size=32, class_mode='categorical') test_data = test_datagen.flow_from_directory( 'flower_dataset/test', target_size=(224, 224), batch_size=32, class_mode='categorical') ``` 接下来,我们可以定义一个卷积神经网络模型来训练我们的数据。可以使用TensorFlow中的Keras API来定义模型。 ```python from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout # 定义一个卷积神经网络模型 model = Sequential() model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3))) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Conv2D(128, (3, 3), activation='relu')) model.add(MaxPooling2D((2, 2))) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(102, activation='softmax')) # 编译模型 model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) ``` 定义好模型后,我们可以使用fit()方法来训练模型。可以指定训练数据和测试数据,以及一些训练参数,例如批量大小、迭代次数等。 ```python # 训练模型 history = model.fit( train_data, steps_per_epoch=100, epochs=30, validation_data=test_data, validation_steps=50) ``` 训练完成后,我们可以使用模型来进行预测。可以使用predict()方法来对单个图片进行识别,或者使用evaluate()方法来对测试集进行整体评估。 ```python # 对单个图片进行预测 import numpy as np from tensorflow.keras.preprocessing import image img_path = 'flower_dataset/test/daisy/5547758_eea9edfd54_n.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x /= 255. preds = model.predict(x) print(preds) # 对测试集进行评估 test_loss, test_acc = model.evaluate(test_data, steps=50) print('Test accuracy:', test_acc) ``` 以上就是一个基于TensorFlow 2.3的花卉识别程序示例。需要注意的是,这个程序只是一个简单的示例,实际应用中可能需要进行更多的调参和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值