2

我所做的是:

from keras.applications.vgg16 import VGG16
from keras.layers import *
from keras.models import Model
import numpy as np 

vgg_model = VGG16(weights='imagenet', include_top=False, input_shape = (224,224, 3)) 

block5_conv3 = vgg_model.get_layer("block5_conv3").output

input_image = Input(shape=(224,224, 3), name='image_input')
vgg_out = vgg_model(input_image)

f0 = Flatten()(block5_conv3)

test_model = Model(inputs=input_image, outputs=f0)
print(test_model.summary())

但我收到以下错误消息:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    test_model = Model(inputs=input_image, outputs=f0)
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\engine\network.py", line 1430, in _map_graph_network
    str(layers_with_complete_input))
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

我觉得我这样做的方式有问题,但无法找出正确的方式。

4

1 回答 1

2

在这种情况下,无需定义Input层。您可以使用inputVGG 模型的属性:

vgg_model = VGG16(weights='imagenet', include_top=False, input_shape = (224,224, 3)) 

block5_conv3 = vgg_model.get_layer("block5_conv3").output
f0 = Flatten()(block5_conv3)

test_model = Model(inputs=vgg_model.input, outputs=f0)

或者,您可以定义和使用后端函数

from keras import backend as K

# ... (use the code above except the last line)

func = K.function([vgg_model.input], [f0])

# to call it:
outputs = func([your_image_arrays])
于 2018-09-08T10:17:21.947 回答