&引用;ValueError:无不支持的值;使用自定义图层时在keras model.fit中

&引用;ValueError:无不支持的值;使用自定义图层时在keras model.fit中,keras,implementation,layer,keras-layer,Keras,Implementation,Layer,Keras Layer,这是我得到以下错误的代码: class Rotate_Translate(Layer): def __init__(self, no_of_angles, min_step, max_step, step_size, **kwargs): super(Rotate_Translate, self).__init__(**kwargs) self.no_of_angles = np.int64(no_of_angles) self.min_

这是我得到以下错误的代码:

class Rotate_Translate(Layer):

    def __init__(self, no_of_angles, min_step, max_step, step_size, **kwargs):
        super(Rotate_Translate, self).__init__(**kwargs)
        self.no_of_angles = np.int64(no_of_angles)
        self.min_step = np.int64(min_step)
        self.max_step = np.int64(max_step)
        self.step_size = np.int64(step_size)

    def build(self, input_shape):
        self.input_spec = [InputSpec(shape=input_shape)]
        self.shape = input_shape
        # init_no_of_angles = 8
        rot_weight_value = np.array(random.sample(range(0,180), self.no_of_angles), dtype='int')
        self.Wrotate = K.variable(rot_weight_value)

        trans_weight_value = np.array(range(self.min_step, self.max_step, self.step_size), dtype='int')
        self.Wtrans = K.variable(trans_weight_value)

        extra_values = np.array([self.no_of_angles, self.min_step, self.max_step, self.step_size], dtype='int')

        self.extr = K.variable(extra_values)
        weights = [self.Wrotate, self.Wtrans, self.extr]

        self.trainable_weights = weights
        super(Rotate_Translate, self).build(input_shape)

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim[1].value, self.output_dim[2].value, self.output_dim[3].value)

    def cart2pol(self, x1, y1):
        rho = np.sqrt( (x1**2) + (y1**2) )
        phi = np.arctan2(y1, x1)
        return(phi, rho)

    def pol2cart(self, phi, rho):
        x = rho * np.cos(phi)
        y = rho * np.sin(phi)
        return(x, y)

    def _translate_image(self, img, shifts): 

        sh = K.eval(shifts)
        img = tf.transpose(img, (2,3,1,0))
        h1 = img.shape[0]
        w1 = img.shape[1]
        end = w1
        newimg = img;

        for i in sh:
            i = np.int(i)
            if i > 0:
                tmp1 = img[:h1, w1-i:w1]
                tmp2 = img[:h1, 0:w1-i]
                both = tf.concat([tmp1,tmp2],1)
                newimg = tf.minimum(newimg, both)
            else:
                tmp1 = img[:h1, abs(i):w1]
                tmp2 = img[:h1, 0:abs(i)]
                both = tf.concat([tmp1,tmp2],1)
                newimg = tf.minimum(newimg, both)

        return newimg

    def _rotate_image(self, mat, directions, shifts): 

        bb, hh, ww, cc = mat.shape
        list1 = []
        output_sizes = [] 

        height = int(hh)
        width = int(ww)
        channels = int(cc)
        direc = K.eval(directions)
        a = []
        for angle in direc:
            theta, rho = self.cart2pol(height/2, width/2) # r, phi = cmath.polar(complex(height/2, width/2))
            newX, newY = self.pol2cart(theta+angle*(math.pi/180), rho)

            padX = int(abs(round(int(abs(newX))-round(np.float(height/2)))))
            padY = int(abs(round(int(abs(newY))-round(np.float(width/2)))))
            padd = max(padX, padY)

            if padd > 0:

                img_tmp = tf.transpose(mat, (0,3,1,2))

                ii = K.spatial_2d_padding(img_tmp, padding=((padd,padd), (padd,padd)), data_format='channels_first')
                h = ii.shape[2].value
                w = ii.shape[3].value
                image_center = (w/2, h/2)
                image = ii
                hh = h
                ww = w

            else:
                hh = height
                ww = width
                image_center = (width/2, height/2)
                image = tf.transpose(mat, (0,3,1,2))

            rot = tf.contrib.image.rotate(image, angle)

            trans_image = self._translate_image(rot, shifts)

            rev_h = trans_image.shape[0]
            rev_w = trans_image.shape[1]
            rev_rot = tf.contrib.image.rotate(trans_image, -angle)
            final_mat = tf.convert_to_tensor(rev_rot[padd:rev_h-padd, padd:rev_w-padd])
            final_mat = tf.transpose(final_mat, (3,0,1,2))

            list1.append(final_mat)

        cat_imgs = list1[0]
        for k in range(1,len(list1)):
            cat_imgs = K.concatenate([cat_imgs, list1[k]])

        result_imgs = tf.convert_to_tensor(cat_imgs)
        return result_imgs, result_imgs.shape

    def call(self, x):

        result_img, output_size = self._rotate_image(x, self.Wrotate, self.Wtrans)
        self.output_dim = output_size

        return result_img
回溯(最近一次呼叫最后一次):
文件“seg1.py”,第211行,在
历史=模型.fit(X_序列,X_序列,批量大小=批量大小,历代=1,详细=1)
文件“build/bdist.linux-x86_64/egg/keras/engine/training.py”,第1413行
文件“build/bdist.linux-x86_64/egg/keras/engine/training.py”,第937行,在函数中
文件“build/bdist.linux-x86_64/egg/keras/optimizers.py”,第420行,在get_更新中
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/ops/math_ops.py”,第829行,二进制文件
y=ops.convert_to_tensor(y,dtype=x.dtype.base_dtype,name=“y”)
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第676行,在convert_to_tensor中
as_ref=False)
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/framework/ops.py”,第741行,在内部转换为tensor
ret=conversion\u func(值,dtype=dtype,name=name,as\u ref=as\u ref)
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/framework/constant_op.py”,第113行,在_constant_tensor_conversion_函数中
返回常量(v,dtype=dtype,name=name)
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/framework/constant_op.py”,第102行,常量
tensor_util.make_tensor_proto(值,dtype=dtype,shape=shape,verify_shape=verify_shape))
文件“/home/divya/.local/lib/python2.7/site packages/tensorflow/python/framework/tensor\u util.py”,第364行,在make\u tensor\u proto中
raise VALUERROR(“不支持无值”)
ValueError:不支持任何值。
我已经实现了一个自定义层,它旋转和转换输入。当我在CNN的输入层之后添加这个层时,我得到了上面的错误。有人能告诉我问题出在哪里吗

Traceback (most recent call last):

  File "seg1.py", line 211, in <module>

    history = model.fit(X_train, X_train, batch_size=batch_size, epochs=1, verbose=1)
  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 1413, in fit

  File "build/bdist.linux-x86_64/egg/keras/engine/training.py", line 937, in _make_train_function

  File "build/bdist.linux-x86_64/egg/keras/optimizers.py", line 420, in get_updates

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 829, in binary_op_wrapper

    y = ops.convert_to_tensor(y, dtype=x.dtype.base_dtype, name="y")

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 676, in convert_to_tensor
    as_ref=False)

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 741, in internal_convert_to_tensor

    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 113, in _constant_tensor_conversion_function

    return constant(v, dtype=dtype, name=name)

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 102, in constant

    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))

  File "/home/divya/.local/lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 364, in make_tensor_proto

    raise ValueError("None values not supported.")
ValueError: None values not supported.