使用tensorflow在单个图表中对图像的不同部分进行分类

2024-04-16 05:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个象棋棋盘的图像,我想用CNN在一个计算图中以一个正方形一个正方形的方式处理它。在

每个图像如下所示:

enter image description here

假设一幅图像是200x200px,所以每个正方形是25x25px。我试图通过以下方式实施:

X = tf.placeholder(tf.float32, shape=[None, 200, 200, 3], name='X')
Y = tf.placeholder(tf.float32, shape=[None, 64, 13])

weights = {
   "conv1_w": tf.Variable(tf.truncated_normal([3,3,3,32], stddev=0.1)),
   "conv2_w": tf.Variable(tf.truncated_normal([3,3,32,32], stddev=0.1)),
   ...
}

with tf.Session() as sess:

    squares = split_into_squares(X)

    # i want to run some kind of loop here 
    # in order to process each square on the board
    # how can I do it ?
    for square in range(64):
        pred = cnn(square, weights)

    cost = tf.losses.mean_squared_error(Y, pred)
    adam = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

    sess.run(tf.global_variables_initializer())

    for batch in range(1000)
       x, y = get_batch()
       _, loss = sess.run(adam, cost, {X:x, Y:y})

棋盘上总共有64个方块,每个方块可以被6个白棋、6个黑棋或空白棋子占据,因此每个sess.run()的输出是(batch_size, 64, 13)形状的张量。在

split_into_squares()获取一批棋盘图像,并将其重塑为具有维数的张量(batch\u size,64,25,25,3),64是棋盘上的一个正方形数,25是每个正方形的宽度和高度,3是通道数。在

一个cnn()函数的精确实现并不重要,重要的部分是它接受神经网络权值,处理单个平方并返回形状(batch_size, 13)的预测张量。在

有了这个设置,我怎样才能用一个计算图对每个正方形运行cnn()?在


Tags: runin图像size棋盘tfbatch方式
1条回答
网友
1楼 · 发布于 2024-04-16 05:18:58
import tensorflow as tf
from skimage.util.shape import view_as_blocks
import numpy as np
import cv2

# Images size 400 x 400
IMG_SIZE = 400
# No:of Chess Board Images
N_IMAGES = 3 

# Generator that generates one batch of images with the 
# corresponding labels
def generate(batch_size=32):
    # Using same image again and again 
    image_paths = ["4sEvs.jpg"]*N_IMAGES
    k = IMG_SIZE//8
    images = np.zeros((len(image_paths)*64, k, k, 3))
    for i, p in enumerate(image_paths):
        # Read image
        img = cv2.imread("4sEvs.jpg") 
        # slice the image into 64 slices and store it in numpy array
        images[i*64:i*64+64] = view_as_blocks(img, block_shape=(k, k, 3)).reshape(64,k,k,3)

    # dummpy labels
    y = tf.keras.utils.to_categorical(np.random.randint(13, size=(len(images),1)))

    while True:
        for i in range(0, len(images), batch_size):
            # Generate a batch of train (data, labels)
            yield (images[i:i+batch_size], y[i:i+batch_size])

k = IMG_SIZE // 8

# Sample Model architecture 
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=8, kernel_size=3, activation='relu', 
    input_shape=(k,k,3)))
model.add(tf.keras.layers.Conv2D(filters=4, kernel_size=3, activation='relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(13, activation='softmax'))
model.compile(loss='categorical_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])

batch_size = 32
# Train for 10 epochs
model.fit_generator(generate(batch_size), epochs=10, 
    steps_per_epoch=np.ceil(N_IMAGES*64/batch_size))

代码在注释中解释。在

相关问题 更多 >