栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

keras老版本切换到新版本的代码问题

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

keras老版本切换到新版本的代码问题

1 参数名称问题

keras老版本:

keras新版本:


只需改成上图即可(_keras_shape改成shape,init改成kernel_initializer, border_model改成padding等), 特别需要注意data_format这个参数,新版本的keras中是把最后一个维度当做通道数的,比如【5, 256, 256, 32】,如果需要像pytorch一样,则需要改动为“channels_first".

参考链接:
【1】https://stackoverflow.com/questions/46965047/channels-first-with-keras
【2】https://stackoverflow.com/questions/66517434/keras-throwing-error-keyword-argument-not-understood-init-and-keyword
【3】https://github.com/Theano/Theano/issues/6765
【4】https://stackoverflow.com/questions/54614299/tensor-object-has-no-attribute-keras-shape

2 参数的类型

replaced merge with keras.layers.concatenate :
但是又报新错:

将Concatenate改为小写字母开头的concatenate:

又报错了,原因根据提示可知是连接的两个tensor的shape不一致,一个是54,37,另一个是53,73, 造成这种现象的原因是:下采样过程中出现了奇数的宽度或者长度,比如(53,73), 对其下采样一倍的尺寸是(27,37)而不是(26, 36), 而(27,37)上采样一倍的尺寸是直接乘以2,也就是(54,74)了。

在解决这个问题上查找了很多资料,最先开始误以为UpSampling2D里面的size参数可以上采样到某个指定的尺寸,后来才发现这个size的含义是高度或者宽度按照size进行整数倍扩充(不允许浮点倍数扩充),默认的size=(2,2),interpolation=“nearest”,也就是说使用最近邻插值将size直接扩充整2倍数。
后面找到的方式是使用转置卷积(下面代码中的drop8.shape[1]是上采样过程中底下一层的通道数,转置卷积不改变这个通道数,只改变图片尺寸),这参考了使用keras编写的Unet:

  up9 = Conv2DTranspose(drop8.shape[1], (2,2), strides=(2,2), padding='same', data_format="channels_first")(drop8)
  up9 = concatenate([up9, conv1], axis=1)
``

【1】[https://stackoverflow.com/questions/54561175/merging-in-keras-type-error-module-object-not-callable](https://stackoverflow.com/questions/54561175/merging-in-keras-type-error-module-object-not-callable)
【2】 [https://stackoverflow.com/questions/45584258/keras-concatenate-typeerror-init-got-multiple-values-for-argument-axis](https://stackoverflow.com/questions/45584258/keras-concatenate-typeerror-init-got-multiple-values-for-argument-axis)

## 3 Adam优化器的导入
将原本的from keras.optimizers import Adam替换为:
```python
from tensorflow.keras.optimizers import Adam
4 ('Keyword argument not understood: ', 'input ')


https://blog.csdn.net/weixin_43360896/article/details/112055269

5 fit_generator() got an unexpected keyword argument ‘nb_val_samples’

fit_generator() got an unexpected keyword argument 'max_q_size ’

改动参数名称为:

model.fit_generator(train_generator, validation_data=val_generator, validation_steps=X_val.shape[0],
                        steps_per_epoch=X_train.shape[0], epochs=nb_epoch, verbose=2,
                        callbacks=[model_checkpoint, reduce_lr, tb], max_queue_size=1000)

参考:【1】https://stackoverflow.com/questions/64180817/typeerror-fit-generator-got-an-unexpected-keyword-argument-nb-val-samples
【2】https://stackoverflow.com/questions/63359321/type-error-fit-generator-got-an-unexpected-keyword-argument-samples-per-epoc

6 UnImplemented Exception: no description


解决方案:

【1】https://www.codetd.com/article/36244
【2】https://stackoverflow.com/questions/60302081/keras-fit-generator-notimplementederror
【3】https://stackoverflow.com/questions/59380430/how-to-use-model-fit-which-supports-generators-after-fit-generator-deprecation/59381897#59381897

7 Conv2DCustomBackpropInputOp only supports NHWC

只支持输入的tensor的最后一个维度为通道

8 skimage.io中的版本问题:as_grey替换成as_gray

https://stackoverflow.com/questions/58993530/typeerror-self-open-got-an-unexpected-keyword-argument-as-grey-python

9 本文问题来源

在阅读代码
https://github.com/raghakot/ultrasound-nerve-segmentation
https://raghakot.github.io/2016/12/26/Ultrasound-nerve-segmentation-challenge-on-Kaggle.html
时,因为懒没有重新安装环境,keras为2.8.0版本,导致了很多运行问题。下次遇到keras的代码一律只看懂模型部分,其余的自己改写。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/926089.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号