def main(): # Download the data for the VGG-16 model if it doesn't already exist in the directory. vgg16.maybe_download() run_style_transfer() return 0
def initializeVGG16(): """ Downloads the VGG16 model if ot does not already exist """ vgg16.data_dir = 'vgg16/' if not vgg16.check_if_vgg16_exists(): vgg16.maybe_download()
def create_textures(): image_dir = '{0}'.format(IMAGE_DIRECTORY) style_dir = '{0}'.format(STYLE_DIRECTORY) images = sorted( [image for image in os.listdir(image_dir) if image not in BLACKLIST]) styles = sorted( [style for style in os.listdir(style_dir) if style not in BLACKLIST]) #print('Creating Background %s' % images[i]) content_layer_ids = [4] style_layer_ids = list(range(13)) for i in tqdm(range(len(images))): vgg16.maybe_download() #print (images[i], labels[i], backgrounds[i]) style_current = random.choice(styles) print("current style " + style_current) content_image = Image.open('{0}/{1}'.format(IMAGE_DIRECTORY, images[i])) style_image = Image.open('{0}/{1}'.format(STYLE_DIRECTORY, style_current)) style_image = resizeimage.resize_crop(style_image, [WIDTH, HEIGHT]) iterations = random.randint(15, 25) print("ok") #print (list(im_bk.getdata())) content_image = np.float32(content_image) style_image = np.float32(style_image) #%%time img = style_transfer( content_image=content_image, style_image=style_image, content_layer_ids=content_layer_ids, style_layer_ids=style_layer_ids, weight_content=1.5, weight_style=10.0, weight_denoise=0.3, num_iterations=iterations, #60 step_size=10.0) print("image created") save_image(img, 'results/' + images[i] + ".png")
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import tensorflow as tf import numpy as np import PIL.Image import vgg16 from keras.preprocessing import image vgg16.maybe_download() #----------------------------------------------------------------------------------------------------------------------- # Functions def load_image(filename, max_size=None): image = PIL.Image.open(filename) if max_size is not None: # Calculate the appropriate rescale-factor for # ensuring a max height and width, while keeping # the proportion between them. factor = max_size / np.max(image.size) # Scale the image's height and width. size = np.array(image.size) * factor # The size is now floating-point because it was scaled. # But PIL requires the size to be integers. size = size.astype(int) # Resize the image. image = image.resize(size, PIL.Image.LANCZOS)
style_image=style_image, mixed_image=mixed_image) print() print("Final image:") plot_image_big(mixed_image) # Close the TensorFlow session to release its resources. session.close() # Return the mixed-image. return mixed_image if __name__ == "__main__": vgg16.maybe_download() # 从网上下载训练完成的模型参数 # 若下载不成功 可以手动下载 # mkdir vgg16 # wget https://s3.amazonaws.com/cadl/models/vgg16.tfmodel # 首先,我们载入内容图像,它有混合图像想要的大体轮廓。 content_filename = 'images/willy_wonka_old.jpg' content_image = load_image(content_filename, max_size=None) # 然后我们载入风格图像,它拥有混合图像想要的颜色和纹理。 style_filename = 'images/style7.jpg' style_image = load_image(style_filename, max_size=300) # 接着我们定义一个整数列表,它代表神经网络中我们用来匹配内容图像的层次。 # 这些是神经网络层次的索引。对于VGG16模型,第5层(索引4)似乎是唯一有效的内容层。 content_layer_ids = [4]