settings:
Tensorflow = 2.4.1, Keras = 2.4.3, Python = 3.7.9, OpenCV = 4.5.1我使用VGG16的Keras基础模型进行迁移学习。我自己的模型架构是这样开始的:
inputs = tf.keras.layers.Input(shape=(img_height, img_width, 3), name="input_layer")
x = tf.keras.applications.vgg16.preprocess_input(inputs)
x = tf.keras.applications.VGG16(weights=weights, include_top=include_top, input_shape=(img_height, img_width, 3))(x)诸若此类
之后,我冻结了我的模型并使用freeze_optimize_inference.py。因此,我的冻结层看起来像:
x
VGG16/tf.__operators__.getitem/strided_slice/stack
VGG16/tf.__operators__.getitem/strided_slice/stack_1
VGG16/tf.__operators__.getitem/strided_slice/stack_2
VGG16/tf.__operators__.getitem/strided_slice
VGG16/tf.nn.bias_add/BiasAdd/bias
VGG16/tf.nn.bias_add/BiasAdd
VGG16/vgg16/block1_conv1/Conv2D/ReadVariableOp/resource
VGG16/vgg16/block1_conv1/Conv2D/ReadVariableOp诸若此类
在C++中,我使用OpenCV并使用以下命令读取模型:
tensorflowNet = cv::dnn::readNetFromTensorflow("freeze_model_inference.pb");这给了我一个错误:
OpenCV(4.5.1) C:\build\master_winpack-build-win64-
vc14\opencv\modules\dnn\src\tensorflow\tf_importer.cpp:1527: error: (-213:The
function/feature is not implemented) StridedSlice with stride -1 in function
'cv::dnn::dnn4_v20201117::`anonymous-namespace'::TFImporter::populateNet'
empty model对我来说,它说问题来自vgg16.preprocess_input层。我可以在没有这些层的情况下创建一个模型,但是我有一个问题:“我如何才能在C++中准确地实现VGG16预处理?”
发布于 2021-04-07 03:40:02
根据tf.keras.applications.vgg16.preprocess_input documentation的说法,VGG的预处理只涉及从RGB到BGR的转换,并减去ImageNet平均值,这些平均值是[123.68, 116.779, 103.939]而没有任何缩放。
只需使用cv::dnn::blobFromImage函数即可完成此操作:
const double SCALE_FACTOR = 1.0; # No scaling
const cv::Size INPUT_SIZE = cv::Size( 224, 224 ); # Adjust accordingly
const cv::Scalar RGB_MEAN = cv::Scalar( 123.68, 116.779, 103.939 ); # RGB order
const bool SWAP_RB = false; # OpenCV images are already handled in BGR
cv::Mat blob = cv::dnn::blobFromImage( image, SCALE_FACTOR, INPUT_SIZE, RGB_MEAN, SWAP_RB );https://stackoverflow.com/questions/66883884
复制相似问题