我在keras中有一个简单的网络,我定义了一个自定义层,该层对输入张量进行一些操作,然后将其返回到网络,但是当我要实现它时,它会产生以下错误,并表示输入没有当我认为当我们使用拟合功能时,它可以喂食网络。您能帮我解决这个问题吗?我找不到合适的答案来解决我的问题。我也将代码放在这里。谢谢。
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()
回溯(最近通话最近一次):
文件“”,第124行,在 encoded_noise = JPEGLayer()(已解码)#16
文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ base_layer.py”, 第457行,致电 输出= self.call(输入,** kwargs)
文件“”,第94行,正在调用 返回噪声()
文件“”,第88行,出现噪音 slicAndJpeg(K.eval(noised_image_pad))
文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ backend \ tensorflow_backend.py”, 评估第673行 返回to_dense(x).eval(session = get_session())
文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ tensorflow \ python \ framework \ ops.py”, 评估中的第713行 返回_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”, _run中的1152行 feed_dict_tensor,选项,run_metadata)
文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py”, _do_run中的第1328行 run_metadata)
文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ tensorflow \ python \ client \ session.py”, _do_call中的第1348行 提高类型(e)(node_def,op,消息)
InvalidArgumentError:必须输入占位符张量的值 dtype浮点型且形状为[?,28,28,1] [[节点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 /副本:0 /任务:0 /设备:GPU:0”]]] [[{{node jpeg_layer_1 / zero_padding2d_1 / Pad / _9}} = _Recvclient_terminated = false,recv_device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”, send_device =“ / job:localhost /副本: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 /副本:0 /任务:0 /设备:CPU:0”]]
由操作“ input_1”引起,定义于:文件 “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ runpy.py”,第193行,在 _run_module_as_main “ 主要”,mod_spec)文件“ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ runpy.py”,第85行,用_run_code exec(代码,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年,在_run_once 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”, _run_callback中的第758行 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行,正在运行 产生= self.gen.send(值)文件“ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ ipykernel \ kernelbase.py”, 第357行,在process_one中 产生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中 产生gen.maybe_future(handler(stream(idents,msg,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 =静音)文件 “ 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”, _run_cell中的第2845行 返回运行器(coro)文件“ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ IPython \ core \ async_helpers.py”, _pseudo_sync_runner中的第67行 coro.send(无)文件“ 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中 如果(从self.run_code(代码,结果)获得):文件“ 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行,在 图片=输入((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行,初始化 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”, _apply_op_helper中的第787行 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 /副本:0 /任务:0 /设备:GPU:0”]]] [[{{node jpeg_layer_1 / zero_padding2d_1 / Pad / _9}} = _Recvclient_terminated = false,recv_device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”, send_device =“ / job:localhost /副本: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 /副本:0 /任务:0 /设备:CPU:0”]]
答案 0 :(得分:0)
这是由您的slicAndJpeg(K.eval(noised_image_pad))
类中的行JPEGLayer
引起的。基本上,您试图通过调用K.eval()
来评估张量,而不向其提供任何数据。您无法评估空张量,对吗?可以通过完全删除noise()
函数并在预处理中执行填充/切片和其他任务来解决此问题。
通过遵循以下步骤替换JPEGLayer
类,它应该可以工作(假设输入数据已被填充)
class JPEGLayer(Layer):
def __init__(self,**kwargs):
super(JPEGLayer, self).__init__(**kwargs)
self.supports_masking = True
def call(self, noised_image, training=True):
return noised_image