【发布时间】:2019-04-26 13:13:46
【问题描述】:
我在 keras 中有一个简单的网络,我定义了一个自定义层,它对输入张量进行一些操作,然后将其返回给网络,但是当我想实现它时,它会产生以下错误并说输入没有我认为当我们使用 fit 函数时,它会为网络提供信息。你能帮我解决这个问题吗?我找不到合适的答案来解决我的问题。我也把我的代码放在这里。谢谢。
def C(u):
if u == 0:
return 1. / np.sqrt(2.)
else:
return 1.
def DCT(a, b):
for u in range(8):
for v in range(8):
for x in range(8):
for y in range(8):
b[u,v] = b[u, v] + 0.25 * C(u) * C(v) * a[x, y]* np.cos((2 * x+1) * (u) * np.pi / 16) * np.cos((2 * y+1) * (v) * np.pi / 16)
def IDCT(a, b):
for u in range(8):
for v in range(8):
for x in range(8):
for y in range(8):
b[x,y] = b[x, y] + 0.25 * C(u) * C(v) * a[u,v] * np.cos((2 * x+1) * (u) * np.pi / 16) * np.cos((2 * y+1) * (v) * np.pi / 16)
def quntize_mask(window_size: int, keep_count: int):
mask = np.zeros((window_size, window_size), dtype=np.uint8)
index_order = sorted(((x, y) for x in range(window_size) for y in range(window_size)),
key=lambda p: (p[0] + p[1], -p[1] if (p[0] + p[1]) % 2 else p[1]))
for i, j in index_order[0:keep_count]:
mask[i, j] = 1
return mask
def slicAndJpeg(img):
for i in range (int(img.shape[1].value/8)):
for j in range(int(img.shape[2].value/8)):
temp=(img[:,i*8:i*8+8,j*8:j*8+8])
tempb=np.zeros((8,8))
DCT(temp,tempb)
mask=quntize_mask(8,9)
qunz=Kr.layers.multiply(mask,tempb)
tempc=K.zeros((8,8))
IDCT(qunz,tempc)
img[:,i*8:i*8+8,j*8:j*8+8]=tempc
class JPEGLayer(Layer):
def __init__(self,**kwargs):
super(JPEGLayer, self).__init__(**kwargs)
self.supports_masking = True
def call(self, noised_image, training=True):
def noise():
# noised_image = noised_and_cover
# pad the image so that we can do dct on 8x8 blocks
pad_height = (8 - noised_image.shape[1] % 8) % 8
pad_width = (8 - noised_image.shape[2] % 8) % 8
noised_image_pad = Kr.layers.ZeroPadding2D(padding=(( pad_width, 0),( pad_height,0)))(noised_image)
slicAndJpeg(K.eval(noised_image_pad))
# un-pad
noised_and_cover = noised_image_pad[ :, :noised_image_pad.shape[1]-pad_height, :noised_image_pad.shape[2]-pad_width]
return noised_and_cover
return noise()
#-----------------building w train---------------------------------------------
wt_random=np.random.randint(2, size=(49999,4,4))
w_expand=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_expand=wv_random.astype(np.float32)
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))
#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
w_test=w_test.reshape((1,4,4,1))
#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
image = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5),activation='relu',padding='same', name='convl1e')(image)
wtm=Input((4,4,1))
#--------------------------------------------------------------
wpad=Kr.layers.Lambda(lambda xy: xy[0] + Kr.backend.spatial_2d_padding(xy[1], padding=((0, 24), (0, 24))))
encoded_merged=wpad([conv1,wtm])#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
decoded = Conv2D(1, (5, 5),activation='relu', padding='same', name='decoder_output')(encoded_merged)
model=Model(inputs=[image,wtm],outputs=decoded)
model.summary()
decoded_noise=JPEGLayer()(decoded)#16
#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (5,5),activation='relu' , name='conl1w')(decoded_noise)#24
convw2 = Conv2D(64, (5,5),activation='relu' , name='conl2w')(convw1)#20
#Avw1=AveragePooling2D(pool_size=(2,2))(convw2)
convw3 = Conv2D(64, (5,5),activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
#Avw2=AveragePooling2D(pool_size=(2,2))(convw4)
convw5 = Conv2D(64, (5,5), activation='relu' ,name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu' ,name='conl6w')(convw5)#4
pred_w = Conv2D(1, (1, 1),activation='relu' ,padding='same', name='reconstructed_W')(convw6)
model1=Model(inputs=[image,wtm],outputs=[decoded,pred_w])
model1.summary()
#----------------------training the model--------------------------------------
#------------------------------------------------------------------------------
#----------------------Data preparesion----------------------------------------
(x_train, _), (x_test, _) = mnist.load_data()
x_validation=x_train[1:10000,:,:]
x_train=x_train[10001:60000,:,:]
#
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_validation = x_validation.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format
x_validation = np.reshape(x_validation, (len(x_validation), 28, 28, 1))
#---------------------compile and train the model------------------------------
opt=SGD(momentum=0.99,lr=0.0001)
model1.compile(optimizer='adam', loss={'imageprim':'mse','wprim':'binary_crossentropy'}, loss_weights={'imageprim': 0.5, 'wprim': 1.0},metrics=['mae'])
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=40)
#rlrp = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=20, min_delta=1E-4, verbose=1)
mc = ModelCheckpoint('sendAct.h5', monitor='val_loss', mode='min', verbose=1, save_best_only=True)
history=model1.fit([x_train,w_expand], [x_train,w_expand],
epochs=4000,
batch_size=32,
validation_data=([x_validation,wv_expand], [x_validation,wv_expand]),
callbacks=[TensorBoard(log_dir='/home/jamalm8/tensorboardGNWLoss/', histogram_freq=0, write_graph=False),es,mc])
model1.summary()
Traceback(最近一次调用最后一次):
文件“”,第 124 行,在 decoded_noise=JPEGLayer()(已解码)#16
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", 第 457 行,在 调用 output = self.call(inputs, **kwargs)
文件“”,第 94 行,调用中 返回噪声()
文件“”,第 88 行,有噪音 slicAndJpeg(K.eval(noised_image_pad))
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py", 第 673 行,在评估中 返回到_dense(x).eval(session=get_session())
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", 第 713 行,在评估中 return _eval_using_default_session(self, feed_dict, self.graph, session)
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", 第 5157 行,在 _eval_using_default_session 返回 session.run(tensors, feed_dict)
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", 第 929 行,运行中 run_metadata_ptr)
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", 第 1152 行,在 _run feed_dict_tensor、选项、run_metadata)
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", 第 1328 行,在 _do_run 运行元数据)
文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\client\session.py", 第 1348 行,在 _do_call 中 raise type(e)(node_def, op, message)
InvalidArgumentError:您必须为占位符张量提供一个值 'input_1' dtype float 和 shape [?,28,28,1] [[node input_1 (定义在 D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py:517) = Placeholderdtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]] [[{{node jpeg_layer_1/zero_padding2d_1/垫/_9}} = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_39_jpeg_layer_1/zero_padding2d_1/Pad", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
由操作“input_1”引起,定义在:文件 “D:\software\Anaconda3\envs\py36\lib\runpy.py”,第 193 行,在 _run_module_as_main "main", mod_spec) 文件 "D:\software\Anaconda3\envs\py36\lib\runpy.py",第 85 行,_run_code exec(code, run_globals) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\console__main__.py", 第 11 行,在 start.main() 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\spyder_kernels\console\start.py", 第 310 行,主要 kernel.start() 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\kernelapp.py", 第 505 行,开始 self.io_loop.start() 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\platform\asyncio.py", 第 132 行,开始 self.asyncio_loop.run_forever() 文件“D:\software\Anaconda3\envs\py36\lib\asyncio\base_events.py”,行 438,在 run_forever self._run_once() 文件“D:\software\Anaconda3\envs\py36\lib\asyncio\base_events.py”,行 第1451章 handle._run() 文件“D:\software\Anaconda3\envs\py36\lib\asyncio\events.py”,第 145 行,在 _跑 self._callback(*self._args) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\ioloop.py", 第 758 行,在 _run_callback 中 ret = callback() 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\stack_context.py", 第 300 行,在 null_wrapper 中 返回 fn(*args, **kwargs) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\gen.py", 第 1233 行,在内部 self.run() 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\gen.py", 第 1147 行,运行中 yielded = self.gen.send(value) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\kernelbase.py", 第 357 行,在 process_one yield gen.maybe_future(dispatch(*args)) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\gen.py", 第 326 行,在包装器中 产生 = 下一个(结果)文件“D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\kernelbase.py”, 第 267 行,在 dispatch_shell 中 yield gen.maybe_future(handler(stream, idents, msg)) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\gen.py", 第 326 行,在包装器中 产生 = 下一个(结果)文件“D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\kernelbase.py”, 第 534 行,在 execute_request 中 user_expressions,allow_stdin,文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tornado\gen.py”, 第 326 行,在包装器中 产生 = 下一个(结果)文件“D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\ipkernel.py”, 第 294 行,在 do_execute 中 res = shell.run_cell(代码,store_history=store_history,silent=silent)文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\ipykernel\zmqshell.py", 第 536 行,在 run_cell 中 返回 super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", 第 2819 行,在 run_cell 中 raw_cell、store_history、silent、shell_futures)文件“D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\interactiveshell.py”, 第 2845 行,在 _run_cell 返回 runner(coro) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\async_helpers.py", 第 67 行,在 _pseudo_sync_runner coro.send(None) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", 第 3020 行,在 run_cell_async 中 交互性=交互性,编译器=编译器,结果=结果)文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", 第 3185 行,在 run_ast_nodes if (yield from self.run_code(code, result)): File "D:\software\Anaconda3\envs\py36\lib\site-packages\IPython\core\interactiveshell.py", 第 3267 行,在 run_code 中 exec(code_obj, self.user_global_ns, self.user_ns) 文件“”,第 114 行,在 image = Input((28, 28, 1)) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\input_layer.py", 第 178 行,在输入中 input_tensor=tensor) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\legacy\interfaces.py", 第 91 行,在包装器中 返回 func(*args, **kwargs) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\input_layer.py", 第 87 行,在 init 中 name=self.name) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py", 第 517 行,在占位符中 x = tf.placeholder(dtype, shape=shape, name=name) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\array_ops.py", 第 1747 行,在占位符中 返回 gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", 第 5206 行,在占位符中 “占位符”,dtype=dtype,shape=shape,name=name)文件“D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py”, 第 787 行,在 _apply_op_helper op_def=op_def) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\util\deprecation.py", 第 488 行,在 new_func 中 返回 func(*args, **kwargs) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", 第 3274 行,在 create_op 中 op_def=op_def) 文件 "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", 第 1770 行,在 init 中 self._traceback = tf_stack.extract_stack()
InvalidArgumentError(参见上面的回溯):您必须输入一个值 对于具有 dtype float 和 shape 的占位符张量“input_1” [?,28,28,1] [[节点输入_1(定义在 D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py:517) = Placeholderdtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:GPU:0"]] [[{{node jpeg_layer_1/zero_padding2d_1/垫/_9}} = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_39_jpeg_layer_1/zero_padding2d_1/Pad", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
【问题讨论】:
-
不完全确定,但我最好的猜测是它是由该填充功能引起的。也许你可以预先填充数据,让模型假设数据已经填充。
-
不,它不起作用:(((((我在将张量发送到jpeg层之前进行了零填充,但它产生了同样的错误。
-
当我将这行代码 slicAndJpeg(noised_image_pad) 更改为 slicAndJpeg(K.eval(noised_image_pad)) 时也会产生此错误。如果我不这样做,它会产生这个错误 ValueError: setting an array element with a sequence。与DCT函数相关的
-
就像我在回答中提到的,主要错误是因为
K.eval()。没有那行的错误可能是因为这行temp=(img[:,i*8:i*8+8,j*8:j*8+8])。尝试用temp=img[:,i*8:i*8+8,j*8:j*8+8](不带括号)替换它
标签: python tensorflow keras keras-layer tensor