keras实现theano和tensorflow训练的模型相互转换


Posted in Python onJune 19, 2020

我就废话不多说了,大家还是直接看代码吧~

</pre><pre code_snippet_id="1947416" snippet_file_name="blog_20161025_1_3331239" name="code" class="python">

# coding:utf-8
"""
If you want to load pre-trained weights that include convolutions (layers Convolution2D or Convolution1D),
be mindful of this: Theano and TensorFlow implement convolution in different ways (TensorFlow actually implements correlation, much like Caffe),
and thus, convolution kernels trained with Theano (resp. TensorFlow) need to be converted before being with TensorFlow (resp. Theano).
"""
from keras import backend as K
from keras.utils.np_utils import convert_kernel
from text_classifier import keras_text_classifier
import sys
 
def th2tf( model):
  import tensorflow as tf
  ops = []
  for layer in model.layers:
    if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']:
      original_w = K.get_value(layer.W)
      converted_w = convert_kernel(original_w)
      ops.append(tf.assign(layer.W, converted_w).op)
  K.get_session().run(ops)
  return model
 
def tf2th(model):
  for layer in model.layers:
    if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']:
      original_w = K.get_value(layer.W)
      converted_w = convert_kernel(original_w)
      K.set_value(layer.W, converted_w)
  return model
 
def conv_layer_converted(tf_weights, th_weights, m = 0):
  """
  :param tf_weights:
  :param th_weights:
  :param m: 0-tf2th, 1-th2tf
  :return:
  """
  if m == 0: # tf2th
    tc = keras_text_classifier(weights_path=tf_weights)
    model = tc.loadmodel()
    model = tf2th(model)
    model.save_weights(th_weights)
  elif m == 1: # th2tf
    tc = keras_text_classifier(weights_path=th_weights)
    model = tc.loadmodel()
    model = th2tf(model)
    model.save_weights(tf_weights)
  else:
    print("0-tf2th, 1-th2tf")
    return
if __name__ == '__main__':
  if len(sys.argv) < 4:
    print("python tf_weights th_weights <0|1>\n0-tensorflow to theano\n1-theano to tensorflow")
    sys.exit(0)
  tf_weights = sys.argv[1]
  th_weights = sys.argv[2]
  m = int(sys.argv[3])
  conv_layer_converted(tf_weights, th_weights, m)

补充知识:keras学习之修改底层为TensorFlow还是theano

我们知道,keras的底层是TensorFlow或者theano

要知道我们是用的哪个为底层,只需要import keras即可显示

修改方法:

打开

keras实现theano和tensorflow训练的模型相互转换

修改

keras实现theano和tensorflow训练的模型相互转换

以上这篇keras实现theano和tensorflow训练的模型相互转换就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python isinstance函数介绍
Apr 14 Python
python通过imaplib模块读取gmail里邮件的方法
May 08 Python
python清除字符串里非字母字符的方法
Jul 02 Python
使用python实现rsa算法代码
Feb 17 Python
深入解答关于Python的11道基本面试题
Apr 01 Python
Python代码实现KNN算法
Dec 20 Python
Tensorflow使用tfrecord输入数据格式
Jun 19 Python
Python中作用域的深入讲解
Dec 10 Python
Python 读取串口数据,动态绘图的示例
Jul 02 Python
解决Python3下map函数的显示问题
Dec 04 Python
python 模拟登陆github的示例
Dec 04 Python
使用python将HTML转换为PDF pdfkit包(wkhtmltopdf) 的使用方法
Apr 21 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 #Python
python中怎么表示空值
Jun 19 #Python
Python调用OpenCV实现图像平滑代码实例
Jun 19 #Python
使用OpenCV对车道进行实时检测的实现示例代码
Jun 19 #Python
为什么python比较流行
Jun 19 #Python
查看keras的默认backend实现方式
Jun 19 #Python
Python图像阈值化处理及算法比对实例解析
Jun 19 #Python
You might like
PHP 中文乱码解决办法总结分析
2009/07/30 PHP
php+mysql实现无限分类实例详解
2015/01/15 PHP
smarty内部日期函数html_select_date()用法实例分析
2015/07/08 PHP
PHP入门教程之字符串处理技巧总结(转换,过滤,解析,查找,截取,替换等)
2016/09/11 PHP
php curl 模拟登录并获取数据实例详解
2016/12/22 PHP
PHP的反射机制实例详解
2017/03/29 PHP
PHP实现新型冠状病毒疫情实时图的实例
2020/02/04 PHP
判断是否输入完毕再激活提交按钮
2006/06/26 Javascript
Jquery Ajax学习实例6 向WebService发出请求,返回DataSet(XML) 异步调用
2010/03/18 Javascript
js+xml生成级联下拉框代码
2012/07/24 Javascript
jQuery的each终止或跳过示例代码
2013/12/12 Javascript
jQuery height()、innerHeight()、outerHeight()函数的区别详解
2016/05/23 Javascript
功能强大的Bootstrap组件(结合js)
2016/08/03 Javascript
微信小程序进行微信支付的步骤昂述
2016/12/01 Javascript
JS数字千分位格式化实现方法总结
2016/12/16 Javascript
基于Vue 2.0的模块化前端 UI 组件库小结
2017/12/21 Javascript
vue-cli脚手架-bulid下的配置文件
2018/03/27 Javascript
python采集百度百科的方法
2015/06/05 Python
Python中的TCP socket写法示例
2018/05/11 Python
Scrapy基于selenium结合爬取淘宝的实例讲解
2018/06/13 Python
python多进程使用及线程池的使用方法代码详解
2018/10/24 Python
Python中psutil的介绍与用法
2019/05/02 Python
Python3 main函数使用sys.argv传入多个参数的实现
2019/12/25 Python
Python实现分数序列求和
2020/02/25 Python
Python如何省略括号方法详解
2020/03/21 Python
如何导出python安装的所有模块名称和版本号到文件中
2020/06/05 Python
Python Flask异步发送邮件实现方法解析
2020/08/01 Python
详解torch.Tensor的4种乘法
2020/09/03 Python
Athleta官网:购买女士瑜伽服、技术运动服和休闲运动服
2020/11/12 全球购物
介绍一下Make? 为什么使用make
2016/07/31 面试题
外贸实习生自荐信范文
2013/11/24 职场文书
运动会方阵解说词
2014/02/12 职场文书
社区五一劳动节活动总结
2015/02/09 职场文书
2015年街道办事处工作总结
2015/05/22 职场文书
委托收款证明
2015/06/23 职场文书
python基于tkinter制作无损音乐下载工具
2021/03/29 Python