当前位置:网站首页>[Over 17] Pytorch rewrites keras
[Over 17] Pytorch rewrites keras
2022-08-05 05:26:00 【Mosu playing computer】
任务
pytorchBuild your own modelkeras
想法
- 有pytorchself-built model code
- can be found line by linekerasreplace it with the corresponding layer(手工转写
- Pytorch转keras的有效方法,以FlowNet为例_咆哮的阿杰的博客-CSDN博客_pytorch转keras
- tensorflow和keras_手动 pytorch 模型转 tensorflow keras 模型_weixin_39637920的博客-CSDN博客
There are self-built models exportedonnx,作为中间件可以onnx转keras(自动转- 搞不出来
查到的博客
首先,We must have a clear understanding,网上以及githubSome of the so-called abovepytorch转换Keras或者Keras转换成PytorchThe tool code is almost inoperable or has limitations of use(For example, only certain models can be converted),But we can see some clues in these conversion codes,For example, the size of the parameters of the two(shape)的形式、channel的排序(first or last)是否一样,Master the difference,You can write your own conversion code based on these differences,没错,Write the conversion code yourself,是最稳妥的办法.The whole process is divided into two parts.The author will use Nvidia开源的FlowNet为例,将开源的Pytorch代码转化为Keras模型.
按照Pytorch中模型的结构,编写对应的Keras代码,用keras的函数式API,It would be very convenient to build.
把Pytorch的模型参数,Assigned to in order according to the name of the layerKeras的模型
Although the above two steps seem simple,But in reality, I also took a lot of detours.A key point here,就是参数的shapeIs it unified in the two frameworks,That is of course not uniform.查到的博客
pytorch 到 tensorflow 可以用onnxConvert as an intermediate tool,将pytorch转为onnx,再从onnx转为tensorflow,But there may be some messy problems in the middle.In fact, it is also very convenient to manually read the parameters and refill the corresponding model,This article summarizes manual model conversion.
过程
I would definitely try it first 自动转
pytorch模型转keras模型_AI算法-图哥的博客-CSDN博客_pytorch2keras
ONNX系列二 — 使用ONNX使KerasModels are portable_Igloo's blog-CSDN博客_keras onnx
安装一下
接触到了(load和load_state_dict)的问题
Pytorch保存和加载模型(load和load_state_dict)_木盏的博客-CSDN博客_pytorch 加载模型
插曲
安装whl
真是服了,onnx1.8.1It was later removedoptimizer那个包,然后pytorch2kerasThis package uses the version is1.8.1及之前的,onnx1.8.1支持py3.8,然后我Py3.9.So had to be in another virtual environment(py3.6)Reinstalled the corresponding package inside(400M的tensorflow还有torch等等).安好了,Thought it could be used now
然后报 modulelist的错
Then I export as onnx,再load,就报“TypeError: ‘ModelProto’ object is not callable”的错
我真是服了.
It seems that it can only be written by hand
python关于onnx模型的一些基本操作_A glass of salt water blog-CSDN博客_onnx 静态量化
学习keras,主要是 The parameters on both sides are different
It's better to change.(不问对错,就是搭积木)
结果
input_data = keras.layers.Input(shape=(4,1), dtype='float64')
cnn_out = keras.layers.Conv1D(1, kernel_size=2, strides=1)(input_data)
cnn_out = keras.layers.MaxPool1D(2)(cnn_out)
lstm_out =keras.layers.LSTM(4)(cnn_out)
dense_out = keras.layers.Dense(3,activation='softmax')(lstm_out)
model = keras.Model(input_data, dense_out)
model.compile(optimizer='adam',loss='mean_absolute_error',metrics=['accuracy'])
model.summary()
The rewrite was successful,But what about the accuracy,very rubbish
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OSjRcl88-1659412492791)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220802115449998.png)]](/img/a2/7f0c7eebd119373bf20c44de9f7947.png)
边栏推荐
猜你喜欢

Multi-threaded query results, add List collection

如何停止flink job

【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】

Mesos学习

【过一下14】自习室的一天
![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
[Go through 10] sklearn usage record

Pandas(五)—— 分类数据、读取数据库

Flink Table API 和 SQL之概述

Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)

关于基于若依框架的路由跳转
随机推荐
HQL语句执行过程
Error creating bean with name 'configDataContextRefresher' defined in class path resource
Mesos learning
学习总结week3_4类与对象
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package
【过一下10】sklearn使用记录
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
学习总结week3_2函数进阶
怎样在Disciples门徒获得收益?
coppercam primer [6]
[Go through 7] Notes from the first section of the fully connected neural network video
"PHP8 Beginner's Guide" A brief introduction to PHP
HQL statement execution process
Flink HA安装配置实战
es6迭代协议
Geek卸载工具
Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
Lecture 4 Backpropagation Essays
el-pagination分页分页设置