概述
1. 网络可视化
writer = tf.summary.FileWriter('logs/',sess.graph)
tensorboard --logdir=logs
2. keras进行训练
keras fit 和 fit_generator的区别,以及如何使用fit_generator进行训练:
https://blog.csdn.net/learning_tortosie/article/details/85243310
已测试,可用;下面是保存模型的方法
best_weights_filepath = 'a.h5'
saveBestModel = ModelCheckpoint(best_weights_filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
# train the network
print("[INFO] training w/ generator...")
H = model.fit_generator( trainGen, steps_per_epoch=NUM_TRAIN_IMAGES // BS, validation_data=testGen, validation_steps=NUM_TEST_IMAGES // BS, epochs=NUM_EPOCHS, callbacks=[saveBestModel])
或者采用:
model.save_weights('a.h5')
hdf5与h5的区别在于hdf5只保存weights,而h5会同时保存weights和structure。
3. 基于代码层的maskrcnn解释 https://www.cnblogs.com/wangyong/p/10614898.html
4. error:module keras.backend has no attribute control_flow_ops
在使用YOLO v3训练自己单目标检测的数据集时.
出现了backend’ has no attribute ‘control_flow_ops’错误,
which python (找到python的路径)
找到lib/python3.7/site-packages/keras/backend/__init__.py
在底部添加两项(需要在tensorflow的分支底下增加):
from .load_backend import control_flow_ops
from .load_backend import set_image_dim_ordering
5. tensorflow GPU使用说明
watch nvidia-smi 备注:实时返回GPU使用情况
1)指定GPU训练:
方法一、在python程序中设置:
代码:os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0’ 备注:使用 GPU 0
代码:os.environ[‘CUDA_VISIBLE_DEVICES’] = ‘0,1’ 备注:使用 GPU 0,1
方法二、在执行python程序时候:
指令:CUDA_VISIBLE_DEVICES=2 python yourcode.py
指令:CUDA_VISIBLE_DEVICES=0,1 python yourcode.py
备注:‘=’的左右不允许有空格
注:**TensorFlow会默认直接占满我们模型部署的GPU的存储资源,只允许一个小内存的程序也会占用所有GPU资源。因此有的时候我们通过nvidia-smi查看GPU状态的时候,会发现有些GPU的计算利用率很低或者计算利用率为0,但是存储被占满了,而这个时候其他人也不能使用这块GPU。但是现在公司的问题是模型多,卡不够用,所有只能“文明”使用GPU,如果设置为允许动态增长的话,这样这个GPU没有被占的存储还可以被其他人使用。
2)两种限定GPU占用量的方法:
方法一、设置定量的GPU显存使用量:
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4 # 占用GPU40%的显存
session = tf.Session(config=config)
方法二、设置最小的GPU显存使用量,动态申请显存:(建议)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
6. 打印pb的layer名字以及保存网络模型
import tensorflow as tf
import sys
from tensorflow.python.framework import graph_util
tf.reset_default_graph()
output_graph_path = sys.argv[1]
with tf.Session() as sess:
tf.global_variables_initializer().run()
output_graph_def = tf.GraphDef()
graph = tf.get_default_graph()
with open(output_graph_path, "rb") as f:
output_graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(output_graph_def, name="")
print("%d ops in the final graph." % len(output_graph_def.node))
tensor_name = [tensor.name for tensor in output_graph_def.node]
# print every layer
for tensor_name_1 in tensor_name:
print(tensor_name_1)
print('---------------------------')
# save graph for tensorboard to view the graph
summaryWriter = tf.summary.FileWriter(sys.argv[2], graph)
for op in graph.get_operations():
print(op.name, op.values())
7. 读取pb,并打印模型中间结果
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
print('pb import sucess')
with graph.as_default():
# with tf.Session() as sess:
sess = tf.Session()
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'Preprocessor/map/TensorArrayStack_1/TensorArrayGatherV3',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: image})
# all outputs are float32 numpy arrays, so convert types as appropriate
output_dict['Preprocessor/map/TensorArrayStack_1/TensorArrayGatherV3'] = output_dict['Preprocessor/map/TensorArrayStack_1/TensorArrayGatherV3']
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
8. 数据全量打印
需要使用numpy并设置一下内容
import numpy as np
np.set_printoptions(threshold=np.inf)
之后再使用print打印就会将数组中所有的内容都打印出来。
9. 关于tf.image.resize_images与opencv的resize不一致的问题:
提出一个解决方案(此处针对的是bilinear方式的resize函数):
void Resize_Image(cv::Mat& src, cv::Mat& dst, int target_height, int target_width, int channel)
{
auto clip = [](float in, float low, float high) -> float { return (in < low) ? low : (in > high ? high : in); };
int original_height = src.rows;
int original_width = src.cols;
assert(dst.rows == target_height);
assert(dst.cols == target_width);
float ratio_h = static_cast<float>(original_height - 1.0f) / static_cast<float>(target_height - 1.0f);
float ratio_w = static_cast<float>(original_width - 1.0f) / static_cast<float>(target_width - 1.0f);
for (int y = 0; y < target_height; ++y){
for (int x = 0; x < target_width; ++x){
float x0 = static_cast<float>(x) * ratio_w;
float y0 = static_cast<float>(y) * ratio_h;
int left = static_cast<int>(clip(std::floor(x0), 0.0f, static_cast<float>(original_width - 1.0f)));
int top = static_cast<int>(clip(std::floor(y0), 0.0f, static_cast<float>(original_height - 1.0f)));
int right = static_cast<int>(clip(std::ceil(x0), 0.0f, static_cast<float>(original_width - 1.0f)));
int bottom = static_cast<int>(clip(std::ceil(y0), 0.0f, static_cast<float>(original_height - 1.0f)));
for (int c = 0; c < channel; ++c){
// H, W, C ordering
float left_top_val = src.at<cv::Vec3f>(top, left)[c];
float right_top_val = src.at<cv::Vec3f>(top, right)[c];
float left_bottom_val = src.at<cv::Vec3f>(bottom, left)[c];
float right_bottom_val = src.at<cv::Vec3f>(bottom, right)[c];
float top_lerp = left_top_val + (right_top_val - left_top_val) * (x0 - left);
float bottom_lerp = left_bottom_val + (right_bottom_val - left_bottom_val) * (x0 - left);
float lerp = clip(std::round(top_lerp + (bottom_lerp - top_lerp) * (y0 - top)), 0.0f, 255.0f);
dst.at<cv::Vec3f>(y,x)[c] = lerp;
}
}
}
}
10. 更换pip install 源
pip install xxxx -i https://pypi.tuna.tsinghua.edu.cn/simple
11. 修改pb的某一层value
将pb转化为pbtxt,然后用文本打开pbtxt文件,手动修改某一层layer的值然后使用reverse,进行转回pb文件
import numpy as np
import tensorflow as tf
import argparse
def convert(pb_path, pbtxt_path):
with open(pb_path, 'rb') as f:
input_graph_def = tf.GraphDef()
input_graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(input_graph_def, name='')
with tf.Session(graph=graph) as sess:
tf.train.write_graph(graph, 'PBs', pbtxt_path, as_text=True)
def convert_back(pbtxt_path, pb_path):
from google.protobuf import text_format as pbtf
input_graph_def = tf.GraphDef()
with open(pbtxt_path, 'r') as f:
graph_str = f.read()
pbtf.Parse(graph_str, input_graph_def)
with tf.Graph().as_default() as graph:
tf.import_graph_def(input_graph_def, name='')
with tf.Session(graph=graph) as sess:
tf.train.write_graph(graph, 'PBs', pb_path, as_text=False)
if __name__ == '__main__':
"""Parse input arguments."""
parser = argparse.ArgumentParser(description='compare_graphs')
parser.add_argument('--pb', dest='pb_path', help='input pb file path')
parser.add_argument('--pbtxt', dest='pbtxt_path', help='output pbtxt file path')
parser.add_argument('--reverse', action='store_true', help='reverse convert')
args = parser.parse_args()
if args.pb_path is None:
print("empty input arguments")
elif args.reverse:
convert_back(args.pbtxt_path, args.pb_path)
else:
convert(args.pb_path, args.pbtxt_path)
最后
以上就是彪壮身影为你收集整理的tensorflow、keras使用记录的全部内容,希望文章能够帮你解决tensorflow、keras使用记录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复