根据通道

时间:2019-01-21 07:30:05

标签: python tensorflow deep-learning

我试图将形状为[batch_size,256,256,3]的输入图像的张量切片或拆分为形状为[batch_size,256,256,1]的三个独立变量,其中每个变量恰好包含输入图像的一个通道,即

R_channel =图像[-1,256,256,0]

G_channel =图像[-1,256,256,1]

B_channel =图像[-1,256,256,2]

我已经尝试了以下代码:

imgs, label = iterator.get_next()

channel_r=imgs[-1,:,:,0]
channel_g=imgs[-1,:,:,1]
channel_b=imgs[-1,:,:,2]
NN_network(imgs,channel_r,channel_g,channel_b)
...
def NN_network(imgs,c_r,c_g,c_b):
    conv1=tf.layers.conv2d(imgs,n_filter,kernel_Size,...)
    conv2=tf.layers.conv2d(c_r,n_filter,kernel_Size,...)
    conv3=tf.layers.conv2d(c_g,n_filter,kernel_Size,...)
    conv4=tf.layers.conv2d(c_b,n_filter,kernel_Size,...)
    concat_layer=tf.concat(axis=3,values=[imgs,c_r,c_g,c_b])

在tensorflow中,但出现以下错误:

  

InvalidArgumentError(请参阅上面的回溯):ConcatOp:尺寸   的输入应该匹配:shape [0] = [16,12,256,256] vs. shape [1] =   [1,12,256,256] [[节点LC_Nikon1 / concat(定义为)=   ConcatV2 [N = 4,T = DT_FLOAT,Tidx = DT_INT32,   _device =“ / job:localhost / replica:0 / task:0 / device:GPU:0”](/ conv2d / BiasAdd,   / conv2d_1 / BiasAdd,/ conv2d_2 / BiasAdd,/ conv2d_3 / BiasAdd,   渐变/ resize_image_with_crop_or_pad_1 / crop_to_bounding_box / Slice_grad / concat / axis)]]      [[{{节点   resize_image_with_crop_or_pad / pad_to_bounding_box / GreaterEqual_3 / _83}}   = _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_384_resize_image_with_crop_or_pad / pad_to_bounding_box / GreaterEqual_3”,   tensor_type = DT_BOOL,   _device =“ / job:localhost /副本:0 /任务:0 /设备:CPU:0”]]

我该怎么做,这是什么错误?

1 个答案:

答案 0 :(得分:1)

可运行的代码:

import tensorflow as tf
import numpy as np

# imgs is supposed to be a tensor
# tf.random.normal available in TF 1.12
imgs = tf.random.normal((16, 256, 256, 3))

channel_r=imgs[:,:,:,0:1]
channel_g=imgs[:,:,:,1:2]
channel_b=imgs[:,:,:,2:3]

n_filter = 16
kernel_Size = 3

def NN_network(imgs,c_r,c_g,c_b):
    conv1=tf.layers.conv2d(imgs,n_filter,kernel_Size)
    conv2=tf.layers.conv2d(c_r,n_filter,kernel_Size)
    conv3=tf.layers.conv2d(c_g,n_filter,kernel_Size)
    conv4=tf.layers.conv2d(c_b,n_filter,kernel_Size)
    concat_layer=tf.concat(axis=3,values=[imgs,c_r,c_g,c_b])

NN_network(imgs,channel_r,channel_g,channel_b)

您的错误消息是说shape[0](即imgs的形状)等于[16,12,256,256],但是shape[1](即c_r的形状)等于[1,12,256,256](第0维不匹配)。

这是因为您设置了channel_r=imgs[-1,:,:,0],其第0维与imgs的维不匹配。

我将channel_r=imgs[-1,:,:,0]更改为channel_r=imgs[:,:,:,0:1],以使生成的channel_r具有4个维度,并且仅与`imgsat第3个维度不同。