我已阅读Keras code for PixelCNN。 我正在尝试基于 Keras PixelCNN 代码实现一个发生的图像生成函数,该代码接收作为输入模糊/发生的图像,并作为输出返回基于 PixelCNN 模型的生成图像。
我试过这段代码没有成功。 (从数据集中取出前 4 个图像,在大于 13 的行中设置零 - 使它们出现图像。)
#load data.
(x1, _), (y1, _) = keras.datasets.mnist.load_data()
#plot 4 images.
f, axarr = plt.subplots(4,1)
# use the created array to output your multiple images. In this case I have stacked 4 images vertically
axarr[0].imshow(x1[0], cmap='gray')
axarr[1].imshow(x1[1], cmap='gray')
axarr[2].imshow(x1[2], cmap='gray')
axarr[3].imshow(x1[3], cmap='gray')
# reset half of the images.
for i in range(4):
x1[i, 13:] = 0
f, axarr = plt.subplots(4,1)
axarr[0].imshow(x1[0], cmap='gray')
axarr[1].imshow(x1[1], cmap='gray')
axarr[2].imshow(x1[2], cmap='gray')
axarr[3].imshow(x1[3], cmap='gray')
#plot images
arr = x1[:4]
结果我得到 - this occurred images 这看起来不错,现在我试图用这个代码生成发生的图像像素。我所做的唯一更改是添加了一个 if 条件来检查行是否大于 13,以便我可以保存旧的图像像素值。
from IPython.display import Image, display
import matplotlib.pyplot as plt
# Create 4 D array with occurred images values.
batch = 4
pixels = arr
pixels = pixels.swapaxes(1, 2).reshape(batch, 28, 28, 1)
print(pixels.shape)
batch, rows, cols, channels = pixels.shape
# Iterate over the pixels because generation has to be done sequentially pixel by pixel.
for row in tqdm(range(rows)):
for col in range(cols):
for channel in range(channels):
if(row>=13):
# Feed the whole array and retrieving the pixel value probabilities for the next
# pixel.
probs = pixel_cnn.predict(pixels)[:, row, col, channel]
# Use the probabilities to pick pixel values and append the values to the image
# frame.
pixels[:, row, col, channel] = tf.math.ceil(
probs - tf.random.uniform(probs.shape)
)
print(row, " ", col, "", probs)
def deprocess_image(x):
# Stack the single channeled black and white image to RGB values.
x = np.stack((x, x, x), 2)
# Undo preprocessing
x *= 255
# Convert to uint8 and clip to the valid range [0, 255]
x = np.clip(x, 0, 255).astype("uint8")
return x
# Iterate over the generated images and plot them with matplotlib.
for i, pic in enumerate(pixels):
keras.preprocessing.image.save_img(
"generated_image_{}.png".format(i), deprocess_image(np.squeeze(pic, -1))
)
display(Image("generated_image_0.png"))
display(Image("generated_image_1.png"))
display(Image("generated_image_2.png"))
display(Image("generated_image_3.png"))
我越来越奇怪 results,我不明白这 4 张图像的旧像素是如何从 original occurred images 变为 generated images。
我正在尝试创建类似 this