当前位置:网站首页>[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)
边栏推荐
- Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
- flink yarn-session的两种使用方式
- 序列基础练习题
- 【过一下9】卷积
- Mesos学习
- BFC(Block Formatting Context)
- [Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
- el-pagination左右箭头替换成文字上一页和下一页
- Returned object not currently part of this pool
- [Let's pass 14] A day in the study room
猜你喜欢
随机推荐
CAP+BASE
CAP+BASE
shell函数
RDD和DataFrame和Dataset
flink项目开发-配置jar依赖,连接器,类库
The role of the range function
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
vscode要安装的插件
如何停止flink job
Xiaobai, you big bulls are lightly abused
实现跨域的几种方式
range函数作用
学习总结week2_5
[Go through 9] Convolution
el-pagination左右箭头替换成文字上一页和下一页
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
Detailed Explanation of Redis Sentinel Mode Configuration File
【过一下7】全连接神经网络视频第一节的笔记
vscode+pytorch使用经验记录(个人记录+不定时更新)
"PHP8 Beginner's Guide" A brief introduction to PHP









