我正在使用 TensorFlow 创建 CNN 函数,但出现与形状相关的错误
I am creating a CNN function with TensorFlow, but I get a Shape related error
我尝试了使用 Tensorflow 的卷积神经网络。
但是,Shape 会导致错误。
首先是main函数的一部分。
while True:
with mss.mss() as sct:
Game_Scr = np.array(sct.grab(Game_Scr_pos))[:,:,:3]
cv2.imshow('Game_Src', Game_Scr)
cv2.waitKey(0)
Game_Scr = cv2.resize(Game_Scr, dsize=(960, 540), interpolation=cv2.INTER_AREA)
print(Game_Scr.shape)
print(Convolution(Game_Scr))
第二个是我调用的函数。
def Convolution(img):
kernel = tf.Variable(tf.truncated_normal(shape=[4], stddev=0.1))
sess = tf.Session()
with tf.Session() as sess:
img = img.astype('float32')
Bias1 = tf.Variable(tf.truncated_normal(shape=[4],stddev=0.1))
conv2d = tf.nn.conv2d(img, kernel, strides=[1, 1, 1, 1], padding='SAME')# + Bias1
conv2d = sess.run(conv2d)
return conv2d
ValueError:形状必须是等级 4 但对于 'Conv2D'(操作:'Conv2D')是等级 3,输入形状:[540,960,3], [4].
我多次尝试改变形状,但我得到了同样的错误。
尝试替换
img = img.astype('float32')
和
img = tf.expand_dims(img.astype('float32'), 0)
tf.nn.conv2d 输入的维度应为 4,(batch_size、image_hight、image_with、image_channels)。您在缺少 batch_size、tf.expand_dims
的地方只需添加该维度(batch_size 为 1,因为您只有一张图片)。
根据官方文档 here,输入张量的形状应为 [batch, in_height, in_width, in_channels]
,过滤器/内核张量的形状应为 [filter_height, filter_width, in_channels, out_channels]
.
尝试将 Convolution
函数更改为如下内容:
def Convolution(img):
kernel = tf.Variable(tf.truncated_normal(shape=[200, 200, 3, 3], stddev=0.1))
sess = tf.Session()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
img = img.astype('float32')
conv2d = tf.nn.conv2d(np.expand_dims(img, 0), kernel, strides=[1, 1, 1, 1], padding='SAME')# + Bias1
conv2d = sess.run(conv2d)
return conv2d
我尝试了使用 Tensorflow 的卷积神经网络。
但是,Shape 会导致错误。
首先是main函数的一部分。
while True:
with mss.mss() as sct:
Game_Scr = np.array(sct.grab(Game_Scr_pos))[:,:,:3]
cv2.imshow('Game_Src', Game_Scr)
cv2.waitKey(0)
Game_Scr = cv2.resize(Game_Scr, dsize=(960, 540), interpolation=cv2.INTER_AREA)
print(Game_Scr.shape)
print(Convolution(Game_Scr))
第二个是我调用的函数。
def Convolution(img):
kernel = tf.Variable(tf.truncated_normal(shape=[4], stddev=0.1))
sess = tf.Session()
with tf.Session() as sess:
img = img.astype('float32')
Bias1 = tf.Variable(tf.truncated_normal(shape=[4],stddev=0.1))
conv2d = tf.nn.conv2d(img, kernel, strides=[1, 1, 1, 1], padding='SAME')# + Bias1
conv2d = sess.run(conv2d)
return conv2d
ValueError:形状必须是等级 4 但对于 'Conv2D'(操作:'Conv2D')是等级 3,输入形状:[540,960,3], [4].
我多次尝试改变形状,但我得到了同样的错误。
尝试替换
img = img.astype('float32')
和
img = tf.expand_dims(img.astype('float32'), 0)
tf.nn.conv2d 输入的维度应为 4,(batch_size、image_hight、image_with、image_channels)。您在缺少 batch_size、tf.expand_dims
的地方只需添加该维度(batch_size 为 1,因为您只有一张图片)。
根据官方文档 here,输入张量的形状应为 [batch, in_height, in_width, in_channels]
,过滤器/内核张量的形状应为 [filter_height, filter_width, in_channels, out_channels]
.
尝试将 Convolution
函数更改为如下内容:
def Convolution(img):
kernel = tf.Variable(tf.truncated_normal(shape=[200, 200, 3, 3], stddev=0.1))
sess = tf.Session()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
img = img.astype('float32')
conv2d = tf.nn.conv2d(np.expand_dims(img, 0), kernel, strides=[1, 1, 1, 1], padding='SAME')# + Bias1
conv2d = sess.run(conv2d)
return conv2d