InvalidArgumentError:形状必须为4级,但必须为1级

时间:2020-05-13 10:45:37

标签: python tensorflow keras deep-learning image-preprocessing

首先,大家好;

我只想过滤二进制图像数据集(两种不同类型的图像具有两种不同类型的过滤矩阵),过滤后我只想添加两层回到正常的cnn

>>> import builtins
>>> builtins is __builtins__
True

我尽力而为,但是出现了这个错误:

import keras
import keras.backend as K
import numpy as np
from keras import Input, layers
from keras.models import Model


def my_filter1(shape, dtype=None):
  kernelValues_cat = np.array([-0.03861977, -0.00681433,  0.19763936,  0.16233025,  0.01814837,
        -0.10037766,  0.025743  ,  0.07676292,  0.10076649, -0.01162791,
          0.277762  , -0.03399856,  0.06740112,  0.08228938, -0.22894219,
        -0.01097343,  0.10896538, -0.33455664,  0.01249053, -0.07131144,
        -0.30083737,  0.0450361 ,  0.02632423,  0.0326063 ,  0.07632089,
          0.08170559,  0.17779405,  0.14002198, -0.0534982 ,  0.03667643,
          0.00984352, -0.22772053, -0.11768475, -0.1448647 ,  0.05544433,
        -0.02038156,  0.07040382,  0.03736563, -0.04353458, -0.18037376,
        -0.11258822,  0.24918582, -0.17927748,  0.04648639,  0.09226271,
          0.14498603,  0.0091729 , -0.01569342,  0.12471238, -0.17353317])



  return K.variable(kernelValues_cat , dtype='float32')



def my_filter2(shape, dtype=None):

  kernelValues_dog = np.array([ 0.00562452,  0.07233931,  0.01056713, -0.00271974,  0.01258311,
        0.01658106, -0.07546224,  0.00367415,  0.00498003, -0.06350716,
        0.00421606, -0.02176326,  0.0160836 ,  0.01281294,  0.05058465,
       -0.03861704,  0.0773253 , -0.03759991, -0.06474894, -0.056652  ,
       -0.06309445,  0.03082605, -0.06260598,  0.00689435, -0.04214211,
        0.10993017,  0.03741956,  0.0137707 , -0.02764472, -0.05047289,
        0.07559353, -0.0010061 , -0.06301045, -0.00553294, -0.01028815,
        0.01860766, -0.02061985, -0.02880365,  0.0324572 ,  0.03683988,
       -0.04732571, -0.02691277,  0.01911413,  0.03938391, -0.02919731,
        0.03790436,  0.06117813, -0.06713407,  0.07434538,  0.04775169])



  return K.variable(kernelValues_dog , dtype='float32')


img_input1 = layers.Input(shape=(150, 150, 3))
x1 = layers.Conv2D(filters=1,
                    kernel_size = 3,
                    kernel_initializer=my_filter1,
                    strides=2, 
                    padding='valid') (img_input1)

img_input2 = layers.Input(shape=(150, 150, 3))
x2 = layers.Conv2D(filters=1, 
                    kernel_size = 3,
                    kernel_initializer=my_filter2,
                    strides=2, 
                    padding='valid') (img_input2)


added = keras.layers.Add()([x1, x2])


x = layers.Conv2D(16, 3, activation='relu')(added)
x = layers.MaxPooling2D(2)(x)


x = layers.Conv2D(32, 3, activation='relu')(x)
x = layers.MaxPooling2D(2)(x)

x = layers.Conv2D(64, 3, activation='relu')(x)
x = layers.MaxPooling2D(2)(x)

在那之后,只想看看我的模型,对我来说任何帮助都将非常感谢

InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
   1653   try:
-> 1654     c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
   1655   except errors.InvalidArgumentError as e:

InvalidArgumentError: Shape must be rank 4 but is rank 1 for '{{node conv2d_8/convolution}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true](input_10, conv2d_8/convolution/ReadVariableOp)' with input shapes: [?,150,150,3], [50].

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
12 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
   1655   except errors.InvalidArgumentError as e:
   1656     # Convert to ValueError for backwards compatibility.
-> 1657     raise ValueError(str(e))
   1658 
   1659   return c_op

ValueError: Shape must be rank 4 but is rank 1 for '{{node conv2d_8/convolution}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="SAME", strides=[1, 2, 2, 1], use_cudnn_on_gpu=true](input_10, conv2d_8/convolution/ReadVariableOp)' with input shapes: [?,150,150,3], [50].

0 个答案:

没有答案