[英]I want to do some operation and make new array of object from result i'm getting in another AOB
[英]I want to recognize an image with TensorFlow. I'm getting a shape error, but what do I do?
GmdMiss_Folder = os.path.join(os.getcwd(), '..', 'Photo', 'GMD Miss')
GmdMiss_List = os.listdir(GmdMiss_Folder)
for i in range(0, len(GmdMiss_List)):
Img = os.path.join(os.getcwd(), GmdMiss_Folder, GmdMiss_List[i])
Img = cv2.imread(Img, cv2.IMREAD_GRAYSCALE)
Img = np.array(Img)
Img = cv2.resize(Img, dsize=(1920, 1080), interpolation=cv2.INTER_AREA)
Img_Miss_List.append(Img)
i=0
while True:
Img = Img_Miss_List[i]
with tf.Session() as sess:
graph = tf.Graph()
with graph.as_default():
with tf.name_scope("Convolution"):
Img = Convolution(Img)
i += 1
...
The codes below are omitted
...
def Convolution(img):
kernel = tf.Variable(tf.truncated_normal(shape=[180, 180, 3, 3], stddev=0.1))
img = img.astype('float32')
img = tf.nn.conv2d(np.expand_dims(img, 0), kernel, strides=[ 1, 15, 15, 1], padding='VALID')
return img
錯誤是..
ValueError:形狀必須為 4 級,但對於輸入形狀為 [1,1080,1920]、[180,180,3,3] 的“卷積/Conv2D”(操作:“Conv2D”)為 3 級。
您的conv2D
kernel [filter_height, filter_width, in_channels, out_channels]
期望輸入圖像有 3 個通道,並且它是一個形狀為[batch, in_height, in_width, in_channels]
的 4D 張量,而您已經以灰度格式加載了輸入圖像。 因此,您需要加載具有 3 個通道的彩色圖像:
Img = cv2.imread(Img) # By default cv2 load image in BGR format
Img = cv2.cvtColor(Img, cv2.COLOR_BGR2RGB) # convert BGR to RGB format
希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.