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
这是我收到以下错误的代码:
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.
我已经实现了一个旋转和翻译输入的自定义图层。当我在CNN的输入层之后添加这个图层时,我收到了上述错误。谁能告诉问题可能是什么?