我有一个国际象棋棋盘的图像,我想在单个计算图中以张量流的形式在Tensorflow中使用CNN对其进行处理。
每个图像如下:
假设图片的大小为200 x 200像素,因此每个正方形的像素为25 x 25像素。我正在尝试通过以下方式实现它:
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_size,64、25、25、3)的张量,其中64是棋盘上的正方形数,25是宽度和每个正方形的高度,3是通道数。
cnn()
函数的确切实现并不重要,重要的部分是它接受神经网络权重,处理单个正方形并返回形状为(batch_size, 13)
的预测张量。
给出该设置后,如何使用单个计算图为每个正方形运行cnn()
?
答案 0 :(得分:0)
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))
代码在注释中说明。