【问题标题】:InvalidArgumentError: input_14:0 is both fed and fetchedInvalidArgumentError: input_14:0 已输入和提取
【发布时间】:2019-01-27 10:06:39
【问题描述】:

我想在每一层上可视化我的 CNN 过滤器。我为此编写了一个代码,但这给了我一些错误。我想查看每一层的过滤图像,还想查看我的神经网络最常用于预测特定标签的区域的热图。通过这样做,我能够了解我的 cnn 的工作原理,并在我的模型上做进一步的工作以获得更好的结果

我在谷歌上搜索过,但我发现大部分都是理论,但我需要查看解决方案的代码

x = Conv2D(64,(3,3),strides = (1,1),name='layer_conv1',padding='same')(input)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2,2),name='maxPool1')(x)



x = Conv2D(64,(3,3),strides = (1,1),name='layer_conv2',padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2,2),name='maxPool2')(x)

x = Conv2D(32,(3,3),strides = (1,1),name='conv3',padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D((2,2),name='maxPool3')(x)


x = Flatten()(x)
x = Dense(64,activation = 'relu',name='fc0')(x)
x = Dropout(0.25)(x)
x = Dense(32,activation = 'relu',name='fc1')(x)
x = Dropout(0.25)(x)
x = Dense(2,activation = 'softmax',name='fc2')(x)

model = Model(inputs = input,outputs = x,name='Predict')




a=np.expand_dims( X_train[10],axis=0)
a.shape
from keras.models import Model
layer_outputs = [layer.output for layer in model.layers]
activation_model = Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(a)

我收到了这个错误

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-249-119bf7ea835a> in <module>()
      2 layer_outputs = [layer.output for layer in model.layers]
      3 activation_model = Model(inputs=model.input, outputs=layer_outputs)
----> 4 activations = activation_model.predict(a)
      5 
      6 

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks)
   1185                                             verbose=verbose,
   1186                                             steps=steps,
-> 1187                                             callbacks=callbacks)
   1188 
   1189     def train_on_batch(self, x, y,

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/engine/training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps, callbacks)
    320             batch_logs = {'batch': batch_index, 'size': len(batch_ids)}
    321             callbacks._call_batch_hook('predict', 'begin', batch_index, batch_logs)
--> 322             batch_outs = f(ins_batch)
    323             batch_outs = to_list(batch_outs)
    324             if batch_index == 0:

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2919                     return self._legacy_call(inputs)
   2920 
-> 2921             return self._call(inputs)
   2922         else:
   2923             if py_any(is_tensor(x) for x in inputs):

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py in _call(self, inputs)
   2873                                 feed_symbols,
   2874                                 symbol_vals,
-> 2875                                 session)
   2876         if self.run_metadata:
   2877             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)

/opt/conda/lib/python3.6/site-packages/Keras-2.2.4-py3.6.egg/keras/backend/tensorflow_backend.py in _make_callable(self, feed_arrays, feed_symbols, symbol_vals, session)
   2825             callable_opts.run_options.CopyFrom(self.run_options)
   2826         # Create callable.
-> 2827         callable_fn = session._make_callable_from_options(callable_opts)
   2828         # Cache parameters corresponding to the generated callable, so that
   2829         # we can detect future mismatches and refresh the callable.

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _make_callable_from_options(self, callable_options)
   1469     """
   1470     self._extend_graph()
-> 1471     return BaseSession._Callable(self, callable_options)
   1472 
   1473 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in __init__(self, session, callable_options)
   1423         with errors.raise_exception_on_not_ok_status() as status:
   1424           self._handle = tf_session.TF_SessionMakeCallable(
-> 1425               session._session, options_ptr, status)
   1426       finally:
   1427         tf_session.TF_DeleteBuffer(options_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: input_14:0 is both fed and fetched.

我尝试删除一些图层并添加一些图层,但它没有帮助我。我在 google 上发现的代码非常少。

【问题讨论】:

    标签: filter deep-learning visualization conv-neural-network


    【解决方案1】:

    要访问任何层的输出,您可以在keras 中使用function,如下所示:

    from keras import backend as K
    last_layer_output = K.function([model.layers[0].input],
                                      [model.layers[-1].output])
    layer_output = last_layer_output([x])[0]
    

    因此,要访问所有层的输出,您可以创建多个这样的函数,如下所示:

    outputs = [layer.output for layer in model.layers]
    functors = [K.function([model.input, K.learning_phase()], [out]) for out in outputs]
    
    layer_outs = [func([x_test[:4], 1.]) for func in functors]
    

    注意:keras-function 为一层生成一个输出。

    更多你可以阅读它here

    【讨论】:

    • 非常感谢您一直在考虑我的问题。我有另一个解决方案,我从一篇对初学者来说似乎很容易的文章中得到。我也试试这个,再次感谢兄弟
    【解决方案2】:

    使用这个模型我的问题没有解决,所以我制作了一个简单的模型并使用 keras 函数来获取图层输出,与我之前的模型相比,这很容易。

    model = Sequential()
    model.add(Conv2D(16,kernel_size = (5,5),activation = 'relu', activity_regularizer=regularizers.l2(1e-8)))
    model.add(Conv2D(32,kernel_size = (5,5),activation = 'relu', activity_regularizer = regularizers.l2(1e-8)))
    model.add(MaxPooling2D(3,3))
    model.add(Conv2D(64,kernel_size = (5,5),activation = 'relu', activity_regularizer = regularizers.l2(1e-8)))
    model.add(MaxPooling2D(3,3))
    model.add(Conv2D(128,activation = 'relu',kernel_size = (3,3),activity_regularizer = regularizers.l2(1e-8)))
    model.add(Flatten())
    model.add(Dropout(0.8))
    model.add(Dense(64,activation = 'relu',activity_regularizer = regularizers.l2(1e-8)))
    model.add(Dropout(0.8))
    model.add(Dense(64,activation = 'relu',activity_regularizer = regularizers.l2(1e-8)))
    model.add(Dropout(0.8))
    model.add(Dense(2,activation = 'softmax'))
    model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.SGD(lr = 0.001,clipnorm = 1,momentum= 0.9), metrics=["accuracy"])
    model.fit(X_train,y_train, epochs = 10 ,batch_size = 16,validation_data=(X_test,y_test_Categorical))
    model.summary()
    
    #a is my one example from test set
    a=np.expand_dims( X_train[10],axis=0)
    a.shape
    layer_outputs = [layer.output for layer in model.layers]
    activation_model = Model(inputs=model.input, outputs=layer_outputs)
    activations = activation_model.predict(a)
    
    def display_activation(activations, col_size, row_size, act_index): 
        activation = activations[act_index]
        activation_index=0
        fig, ax = plt.subplots(row_size, col_size, figsize=(row_size*2.5,col_size*1.5))
        for row in range(0,row_size):
            for col in range(0,col_size):
                ax[row][col].imshow(activation[0, :, :, activation_index])
                activation_index += 1
    display_activation(activations, 4, 4,0)
    

    通过这样做,我可以得到我的输出

    【讨论】:

      猜你喜欢
      • 2019-03-07
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2020-07-15
      • 2021-11-10
      相关资源
      最近更新 更多