keras中遇到的错误

在运行其他开源keras项目时,遇到了一些问题:

1、在导入_obtain_input_shape时

from keras.applications.imagenet_utils import _obtain_input_shape

出现错误如下:

ImportError: cannot import name '_obtain_input_shape'

原因是在keras 2.2.2中,keras.applications.imagenet_utils模块不再有_obtain_input_shape方法。解决方法:

将导入语句修改如下

from keras_applications.imagenet_utils import _obtain_input_shape
  • 重点:使用keras_applications代替keras.applications

2、在使用_obtain_input_shape方法时出现错误:

_obtain_input_shape() got an unexpected keyword argument 'include_top'

原因是我们的调用中使用了

input_shape = _obtain_input_shape(input_shape,
                                  default_size=224,
                                  min_size=32,
                                  data_format=K.image_data_format(),
                                  include_top=include_top or weights)

而在keras 2.2.2中函数 _obtain_input_shape() 的形式为:

def _obtain_input_shape(input_shape,
                        default_size,
                        min_size,
                        data_format,
                        require_flatten,
                        weights=None):

自然没有include_top属性。因此把调用中的include_top改为require_flatten即可