- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个用于年龄和性别识别的 android 应用程序。我找到了一个有用的 model in GitHub 。他们正在构建基于 first-place winning paper 的 Keras 模型(tensorflow 后端)。他们提供了用于训练和构建网络的 Python 模块、可供下载和使用的已训练权重文件,以及网络摄像头上的工作演示。
我想在演示中使用提供的权重将他们的模型转换为 .pb 文件,以便它也可以在 android 上执行。
我使用 this code 进行了一些依赖于模型的小修改:
from keras.models import Sequential
from keras.models import model_from_json
from keras import backend as K
import tensorflow as tf
from tensorflow.python.tools import freeze_graph
import os
# Load existing model.
with open("model.json",'r') as f:
modelJSON = f.read()
model = model_from_json(modelJSON)
model.load_weights("weights.18-4.06.hdf5")
print(model.summary())
# All new operations will be in test mode from now on.
K.set_learning_phase(0)
# Serialize the model and get its weights, for quick re-building.
config = model.get_config()
weights = model.get_weights()
# Re-build a model where the learning phase is now hard-coded to 0.
#new_model = model.from_config(config)
#new_model.set_weights(weights)
temp_dir = "graph"
checkpoint_prefix = os.path.join(temp_dir, "saved_checkpoint")
checkpoint_state_name = "checkpoint_state"
input_graph_name = "input_graph.pb"
output_graph_name = "output_graph.pb"
# Temporary save graph to disk without weights included.
saver = tf.train.Saver()
checkpoint_path = saver.save(K.get_session(), checkpoint_prefix, global_step=0, latest_filename=checkpoint_state_name)
tf.train.write_graph(K.get_session().graph, temp_dir, input_graph_name)
input_graph_path = os.path.join(temp_dir, input_graph_name)
input_saver_def_path = ""
input_binary = False
output_node_names = "dense_1/Softmax,dense_2/Softmax" # model dependent
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
output_graph_path = os.path.join(temp_dir, output_graph_name)
clear_devices = False
# Embed weights inside the graph and save to disk.
freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
input_binary, checkpoint_path,
output_node_names, restore_op_name,
filename_tensor_name, output_graph_path,
clear_devices, "")
我直接从演示中生成了 model.json 文件。带有model.json的demo.py文件的main函数代码为:
def main():
args = get_args()
depth = args.depth
k = args.width
weight_file = args.weight_file
if not weight_file:
weight_file = get_file("weights.18-4.06.hdf5", pretrained_model, cache_subdir="pretrained_models",
file_hash=modhash, cache_dir=os.path.dirname(os.path.abspath(__file__)))
# for face detection
detector = dlib.get_frontal_face_detector()
# load model and weights
img_size = 64
model = WideResNet(img_size, depth=depth, k=k)()
model.load_weights(weight_file)
print(model.summary())
# write model to json
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
for img in yield_images():
input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_h, img_w, _ = np.shape(input_img)
# detect faces using dlib detector
detected = detector(input_img, 1)
faces = np.empty((len(detected), img_size, img_size, 3))
if len(detected) > 0:
for i, d in enumerate(detected):
x1, y1, x2, y2, w, h = d.left(), d.top(), d.right() + 1, d.bottom() + 1, d.width(), d.height()
xw1 = max(int(x1 - 0.4 * w), 0)
yw1 = max(int(y1 - 0.4 * h), 0)
xw2 = min(int(x2 + 0.4 * w), img_w - 1)
yw2 = min(int(y2 + 0.4 * h), img_h - 1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
# cv2.rectangle(img, (xw1, yw1), (xw2, yw2), (255, 0, 0), 2)
faces[i, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))
# predict ages and genders of the detected faces
results = model.predict(faces)
predicted_genders = results[0]
ages = np.arange(0, 101).reshape(101, 1)
predicted_ages = results[1].dot(ages).flatten()
# draw results
for i, d in enumerate(detected):
label = "{}, {}".format(int(predicted_ages[i]),
"F" if predicted_genders[i][0] > 0.5 else "M")
draw_label(img, (d.left(), d.top()), label)
cv2.imshow("result", img)
key = cv2.waitKey(30)
if key == 27:
break
if __name__ == '__main__':
main()
代码成功编译并生成多个检查点文件以及一个 .pb 文件。
这是模型的图表摘要:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 64, 64, 3) 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 64, 64, 16) 432 input_1[0][0]
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, 64, 64, 16) 64 conv2d_1[0][0]
__________________________________________________________________________________________________
activation_1 (Activation) (None, 64, 64, 16) 0 batch_normalization_1[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 64, 128) 18432 activation_1[0][0]
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, 64, 64, 128) 512 conv2d_2[0][0]
__________________________________________________________________________________________________
activation_2 (Activation) (None, 64, 64, 128) 0 batch_normalization_2[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 64, 128) 147456 activation_2[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 64, 64, 128) 2048 activation_1[0][0]
__________________________________________________________________________________________________
add_1 (Add) (None, 64, 64, 128) 0 conv2d_3[0][0]
conv2d_4[0][0]
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, 64, 64, 128) 512 add_1[0][0]
__________________________________________________________________________________________________
activation_3 (Activation) (None, 64, 64, 128) 0 batch_normalization_3[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D) (None, 64, 64, 128) 147456 activation_3[0][0]
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, 64, 64, 128) 512 conv2d_5[0][0]
__________________________________________________________________________________________________
activation_4 (Activation) (None, 64, 64, 128) 0 batch_normalization_4[0][0]
__________________________________________________________________________________________________
conv2d_6 (Conv2D) (None, 64, 64, 128) 147456 activation_4[0][0]
__________________________________________________________________________________________________
add_2 (Add) (None, 64, 64, 128) 0 conv2d_6[0][0]
add_1[0][0]
__________________________________________________________________________________________________
batch_normalization_5 (BatchNor (None, 64, 64, 128) 512 add_2[0][0]
__________________________________________________________________________________________________
activation_5 (Activation) (None, 64, 64, 128) 0 batch_normalization_5[0][0]
__________________________________________________________________________________________________
conv2d_7 (Conv2D) (None, 32, 32, 256) 294912 activation_5[0][0]
__________________________________________________________________________________________________
batch_normalization_6 (BatchNor (None, 32, 32, 256) 1024 conv2d_7[0][0]
__________________________________________________________________________________________________
activation_6 (Activation) (None, 32, 32, 256) 0 batch_normalization_6[0][0]
__________________________________________________________________________________________________
conv2d_8 (Conv2D) (None, 32, 32, 256) 589824 activation_6[0][0]
__________________________________________________________________________________________________
conv2d_9 (Conv2D) (None, 32, 32, 256) 32768 activation_5[0][0]
__________________________________________________________________________________________________
add_3 (Add) (None, 32, 32, 256) 0 conv2d_8[0][0]
conv2d_9[0][0]
__________________________________________________________________________________________________
batch_normalization_7 (BatchNor (None, 32, 32, 256) 1024 add_3[0][0]
__________________________________________________________________________________________________
activation_7 (Activation) (None, 32, 32, 256) 0 batch_normalization_7[0][0]
__________________________________________________________________________________________________
conv2d_10 (Conv2D) (None, 32, 32, 256) 589824 activation_7[0][0]
__________________________________________________________________________________________________
batch_normalization_8 (BatchNor (None, 32, 32, 256) 1024 conv2d_10[0][0]
__________________________________________________________________________________________________
activation_8 (Activation) (None, 32, 32, 256) 0 batch_normalization_8[0][0]
__________________________________________________________________________________________________
conv2d_11 (Conv2D) (None, 32, 32, 256) 589824 activation_8[0][0]
__________________________________________________________________________________________________
add_4 (Add) (None, 32, 32, 256) 0 conv2d_11[0][0]
add_3[0][0]
__________________________________________________________________________________________________
batch_normalization_9 (BatchNor (None, 32, 32, 256) 1024 add_4[0][0]
__________________________________________________________________________________________________
activation_9 (Activation) (None, 32, 32, 256) 0 batch_normalization_9[0][0]
__________________________________________________________________________________________________
conv2d_12 (Conv2D) (None, 16, 16, 512) 1179648 activation_9[0][0]
__________________________________________________________________________________________________
batch_normalization_10 (BatchNo (None, 16, 16, 512) 2048 conv2d_12[0][0]
__________________________________________________________________________________________________
activation_10 (Activation) (None, 16, 16, 512) 0 batch_normalization_10[0][0]
__________________________________________________________________________________________________
conv2d_13 (Conv2D) (None, 16, 16, 512) 2359296 activation_10[0][0]
__________________________________________________________________________________________________
conv2d_14 (Conv2D) (None, 16, 16, 512) 131072 activation_9[0][0]
__________________________________________________________________________________________________
add_5 (Add) (None, 16, 16, 512) 0 conv2d_13[0][0]
conv2d_14[0][0]
__________________________________________________________________________________________________
batch_normalization_11 (BatchNo (None, 16, 16, 512) 2048 add_5[0][0]
__________________________________________________________________________________________________
activation_11 (Activation) (None, 16, 16, 512) 0 batch_normalization_11[0][0]
__________________________________________________________________________________________________
conv2d_15 (Conv2D) (None, 16, 16, 512) 2359296 activation_11[0][0]
__________________________________________________________________________________________________
batch_normalization_12 (BatchNo (None, 16, 16, 512) 2048 conv2d_15[0][0]
__________________________________________________________________________________________________
activation_12 (Activation) (None, 16, 16, 512) 0 batch_normalization_12[0][0]
__________________________________________________________________________________________________
conv2d_16 (Conv2D) (None, 16, 16, 512) 2359296 activation_12[0][0]
__________________________________________________________________________________________________
add_6 (Add) (None, 16, 16, 512) 0 conv2d_16[0][0]
add_5[0][0]
__________________________________________________________________________________________________
batch_normalization_13 (BatchNo (None, 16, 16, 512) 2048 add_6[0][0]
__________________________________________________________________________________________________
activation_13 (Activation) (None, 16, 16, 512) 0 batch_normalization_13[0][0]
__________________________________________________________________________________________________
average_pooling2d_1 (AveragePoo (None, 16, 16, 512) 0 activation_13[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 131072) 0 average_pooling2d_1[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 2) 262144 flatten_1[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, 101) 13238272 flatten_1[0][0]
==================================================================================================
Total params: 24,463,856
Trainable params: 24,456,656
Non-trainable params: 7,200
__________________________________________________________________________________________________
我采用输出模型并使用以下脚本优化推理:
python -m tensorflow.python.tools.optimize_for_inference --input output_graph.pb --output g.pb --input_names=input_1 --output_names=dense_1/Softmax,dense_2/Softmax
在运行过程中,终端给我很多这样的警告。
FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (16,), for node batch_normalization_1/FusedBatchNorm
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (128,), for node batch_normalization_2/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_3/FusedBatchNorm'
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (128,), for node batch_normalization_4/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_5/FusedBatchNorm'
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (256,), for node batch_normalization_6/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_7/FusedBatchNorm'
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (256,), for node batch_normalization_8/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_9/FusedBatchNorm'
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (512,), for node batch_normalization_10/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_11/FusedBatchNorm'
WARNING:tensorflow:Incorrect shape for mean, found (0,), expected (512,), for node batch_normalization_12/FusedBatchNorm
WARNING:tensorflow:Didn't find expected Conv2D input to 'batch_normalization_13/FusedBatchNorm'
看来这些警告很糟糕!!
我已经在我的 Android 应用程序上尝试了这两个文件。优化文件根本不起作用,而未优化文件是可执行的,但会产生无意义的结果“例如 GUESSING”。
我知道问题有点长,但它是整个工作日的总结,我不想错过任何细节。
不知道问题出在哪里。是在输出节点名称、卡住图形、使用权重实例化模型还是在优化推理脚本中。
最佳答案
经过一番研究,终于解决了乱猜的问题。
问题不是像我最初预期的那样将模型转换为 .pb
文件,而是将图像正确地提供给 Android 中的模型。
我再次转换模型。以下几点将总结我的工作。
# 将模型保存到.h5 文件。
model.save('./saved_model/model.h5')
其次,我将生成的 .h5 文件转换为 .pb 文件。我使用了 this repository 中的代码.链接,以防您无法在超链接中访问它:https://github.com/amir-abdi/keras_to_tensorflow .这个存储库的代码证明了它的可靠性。它将模型转换为 .pb
文件并立即对其进行优化以进行推理。太棒了!
第三,我将生成的 .pb
文件放到 android assets 文件夹中,以便使用我的应用程序对其进行配置。
第四,我将预期的图像转换为像素值并进行按位移位以提取颜色。获得有关此代码的帮助以完成此任务。请记住,getPixels 方法会保留颜色 channel 。因此,如果您需要反转颜色 channel ,请遵循以下代码。我从 this answer 得到了帮助.
Bitmap bitmap = createScaledBitmap(faces[0], INPUT_SIZE , INPUT_SIZE , true);
// get pixel values
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
// extract colors using bit-wise shifting.
floatValues[i * 3 + 0] = ((val >> 16) & 0xFF );
floatValues[i * 3 + 1] = ((val >> 8) & 0xFF );
floatValues[i * 3 + 2] = (val & 0xFF );
// reverse the color orderings.
floatValues[i*3 + 2] = Color.red(val);
floatValues[i*3 + 1] = Color.green(val);
floatValues[i*3] = Color.blue(val);
}
最后,我可以使用 tensorflow 推理方法将图像提供给模型,进行推理并输出结果。
关于python - 将 Keras 模型导出到 .pb 文件并优化推理在 Android 上给出随机猜测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49474467/
我有一个经过训练的模型 (Faster R-CNN),我使用 export_inference_graph.py 将其导出以用于推理。我试图了解创建的 frozen_inference_graph.p
我正在开发一个使用Google Protocol Buffer (protobuf)和CMake的库。该项目具有以下目录树。 MyProject/ MyProject/include/myprojec
我有一个旧的训练有素的 tf1.x 模型(让它成为 Model1 ),用占位符、tf.contrib 等构建。我可以通过从 tf.Session(在 tf1.x 中)中的 .ckpt 检查点恢复图形来
我想预先执行查询并将结果存储在@query中: BEGIN DECLARE @query VARCHAR(50); SET @table = 'top20-img-link'; SET @query
有了这个玩具数据:: df = pd.DataFrame(pd.np.random.randint(2, 9, size=(8, 3))) df.index = pd.date_range(start
我使用的是 Twisted 16.1.1 和 python 3.4。在 twisted 的 16.1.1 版文档中,there is a tutorial上面写着“from twisted.sprea
最近尝试将模型(tf1.x)转换为saved_model,关注官方migrate document .但是在我的用例中,我手中的大部分模型或 tensorflow 模型动物园通常是 pb 文件,根据
有谁知道tensorflow_inception_graph.pb的原始源代码。 真不想知道示例项目中的操作 tensorflow/example/android - 读我。 Tensorflow A
完整的错误说: cv2.error:OpenCV(4.1.0)C:\ projects \ opencv-python \ opencv \ modules \ dnn \ src \ caffe \
我已将模型保存在图表(.pb 文件)中。但现在这个模型不准确,我想开发它。我有其他数据的图片需要学习,但我不知道是否可能或如何做到这一点?结果必须是新数据pb图的修改。 最佳答案 这是个好问题。实际上
假设我有模型(tf.keras.Model): class ContextExtractor(tf.keras.Model): def __init__(self): supe
我正在尝试加载取自 https://github.com/tensorflow/models/tree/master/official/resnet 的已训练模型,但是当我尝试加载 .pb 时,我在
我计划将 .aab 上传到 Play 商店进行发布,在发布之前,我正在尝试反编译以查看在逆向工程过程中哪些数据可能会暴露给用户。 在 .aab_FILES/base/中,我看到了 assets.pb
libphonenumber 中没有phonenumber.pb.h 和phonemetadata.pb.h (CPP) 库 那么有什么办法可以找出来吗? 谢谢! 最佳答案 这些是由 Google p
我需要在 CSV 甲酸盐文件中搜索超过 PB 的数据。使用 LUCENE 建立索引后,索引文件的大小是原始文件的两倍。是否可以减少索引文件的大小??? HADOOP中如何分发LUCENE索引文
我公司的产品每年将在我们的客户站点产生数 PB 的数据。我想填满一个多 PB 的 NAS 来模拟一个已经运行了很长时间(3 个月、6 个月、一年等)的系统。我们希望在我们的软件在负载下的存储系统上运行
我在 Windows 10 机器上创建了一个 tensorflow 模型并使用以下方法保存它: model.save('myfolder') 文件夹内myfolder我得到: - saved_mode
我使用 Pytorch 创建了一个对象检测模型然后从 .pth 转换而来至 .onnx然后 .pb , 但现在我需要将其转换为 .tflite对于安卓应用程序!怎么做?这是我的第一次。 input_a
在这种情况下,大文件会发生什么? 1)Spark从NameNode获取数据位置。 Spark是否会在同一时间停止,因为根据NameNode的信息,数据大小太长? 2)Spark根据数据节点块大小对数据
我在对象检测 API 中使用 ssd_mobilenets 来训练我自己的模型,并获取 .ckpt 文件。它在我的电脑上运行良好,但现在我想在我的手机上使用该模型。所以,我需要将它转换为 .pb 文件
我是一名优秀的程序员,十分优秀!