我正在按照Jeff Heaton's tutorial的方法使用keras创建GAN。即使使用我自己的数据集,一切都可以正常工作。但是,我不知道如何创建一个新图像。 (希顿先生以拼贴的形式创建了28张图像!)
我尝试失败的尝试:
new_fixed_seed = np.random.normal(0, 1, (1, 100))
generated_images = generator.predict(new_fixed_seed)
im = Image.fromarray(generated_images)
结果:TypeError: Cannot handle this data type
我在做什么错了?
答案 0 :(得分:0)
通常,对我来说,当我计算generated images
时,我正在使用以下代码将它们存储在本地:
# combine a squared number of images.
def combine_images(generated_images):
generated_images = np.transpose(generated_images , (0, 3, 1, 2))
num = generated_images.shape[0]
width = int(math.sqrt(num))
height = int(math.ceil(float(num)/width))
shape = generated_images.shape[2:]
image = np.zeros((3,(height+3)*shape[0], (width+3)*shape[1]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
new_shape = (img.shape[0], img.shape[1]+ 4, img.shape[2] + 4)
img_ = np.zeros(new_shape)
img_[:, 2: 2+img.shape[1], 2: 2+img.shape[2]] = img
i = int(index/width)
j = index % width
image[:, i*new_shape[1]: (i+1)*new_shape[1], j*new_shape[2]: (j+1)*new_shape[2]] = img_[:, :, :]
return image
# store combined images
def store_image_maps(images_db, filename):
image = combine_images(images_db)
image = image * 127.5 + 127.5
image = np.swapaxes(image, 0, 1)
image = np.swapaxes(image, 1, 2)
cv2.imwrite(filename,image)
答案 1 :(得分:0)
我可以使用它,但是我并不完全满意,因为我认为它可能更干净,而且我认为其中涉及不必要的步骤:
# SEED_SIZE is 100
fixed_seed = np.random.normal(0, 1, (1, SEED_SIZE))
# used 64x64 because those were my image sizes
image_array = np.full((
64,64, 3),
255, dtype=np.uint8)
generated_images = generator.predict(fixed_seed)
#if you don't use 255 here the images are black
image_array[:] = generated_images * 255
im = Image.fromarray(image_array)
im.save('/content/drive/My Drive/Dataset/test.png')