【问题标题】:Transfer learning with Keras ResNet50 model making python to stop working使用 Keras ResNet50 模型进行迁移学习使 python 停止工作
【发布时间】:2017-12-21 11:39:41
【问题描述】:

我在 Python 3.6 版上使用 Keras 2.0.5 版和 theano 作为后端。
我正在尝试使用ResNet50 模型来实现迁移学习,并使用了以下示例中给出的代码:https://keras.io/applications/ 将以下行添加到代码中使 python 停止工作:

model = ResNet50(weights='imagenet')

我已尝试按照其他链接中的建议更改模型定义:

model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

但这给了我另一个错误:

ValueError:输入必须有3个通道;得到 `input_shape=(3, 224, 第224章)

代码如下:

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

我的 theano 配置文件:(theanorc)

[global]
floatX = float32
device = cpu

[nvcc]
compiler_bindir=C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\14.0\VC\bin

我的 keras 配置文件:(keras.json):

{
"epsilon": 1e-07,
"image_data_format": "channels_first",
"image_dim_ordering": "th",
"backend": "theano",
"floatx": "float32"
}

【问题讨论】:

  • 您使用gpu 还是cpu?你能给我们展示一个完整的模型定义吗?你能打印出model.summary()吗?
  • 我正在使用 CPU。我已将 keras.json 中的 image_data_format 更改为 channels_first。我无法打印出 model.summary() 因为我的 python 在遇到模型定义行时停止,即我得到一个弹出窗口,指出“python 已停止执行”。
  • @MarcinMożejko 我猜 keras 在其库中提供了深度学习模型,请查看以下链接 keras.io/applications。我猜这是 keras 中的 resnet50 模型代码:github.com/fchollet/keras/blob/master/keras/applications/…

标签: python keras theano resnet


【解决方案1】:

我能够通过安装适用于 python 3.5 的 anaconda 和带有 theano 后端的 keras 来实现迁移学习,并且它可以工作。不知何故,没有 anaconda 的 Python 3.6 安装不起作用。

【讨论】:

    【解决方案2】:

    这是因为网络认为您正在使用 tensorflow data_format。在 tensorflow 中,图像的通道排在最后 (im_rows, im_cols, 3),而在 Theano 中,通道排在最后 (3, im_rows, im_cols)

    来自应用程序文档(您提供的链接):

    所有这些架构(Xception 除外)都与 TensorFlow 和 Theano 兼容,并且在实例化时,模型将根据您在 ~/.keras/keras.json 中的 Keras 配置文件中设置的图像数据格式构建。例如,如果您设置了 image_data_format=channels_last,那么从此存储库加载的任何模型都将根据 TensorFlow 数据格式约定“宽度-高度-深度”构建。

    我会建议你试试这个,将 image_data_format 更改为 channels_first。

    我希望这会有所帮助:)

    【讨论】:

    • 我已将 keras.json 文件 image_data_format 更改为 channel_first。现在,当我执行我的代码时,我得到一个弹出窗口,指出“Python 已停止工作”并且我的代码没有运行。
    • 我认为这与 keras 无关。我不确定是否可以进一步帮助您...抱歉
    • 是的@NassimBen 是对的。它不再与 Keras 相关
    猜你喜欢
    • 2020-02-11
    • 2017-04-08
    • 1970-01-01
    • 2023-03-27
    • 2020-06-02
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多