当前位置:网站首页>[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)
边栏推荐
猜你喜欢
随机推荐
ESP32 485 Illuminance
The role of the range function
vscode+pytorch使用经验记录(个人记录+不定时更新)
【Reading】Long-term update
Geek卸载工具
【过一下11】随机森林和特征工程
转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
coppercam入门手册[6]
flink基本原理及应用场景分析
第5讲 使用pytorch实现线性回归
怎样在Disciples门徒获得收益?
redis 持久化
将照片形式的纸质公章转化为电子公章(不需要下载ps)
CAP+BASE
day10-字符串作业
2022 The 4th C.Easy Counting Problem (EGF+NTT)
【过一下15】学习 lstm的一周
es6迭代协议
The fourth back propagation back propagation
el-pagination左右箭头替换成文字上一页和下一页

![coppercam primer [6]](/img/d3/a7d44aa19acfb18c5a8cacdc8176e9.png)

![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
![[Go through 3] Convolution & Image Noise & Edge & Texture](/img/7b/2214020cadf06d9211fd40fb5f1b63.png)




