def NST_model(num_iter=1000): content_img = load_img("con_niu.jpg") content_img = nu.reshape_and_normalize_image(content_img) style_img = load_img("style_cloud.jpg") style_img = nu.reshape_and_normalize_image(style_img) generated_img = nu.generate_noise_image(content_img) print(np.shape(content_img)) print(np.shape(style_img)) print(np.shape(generated_img)) with tf.Session() as sess: model = nu.load_vgg_model( "pretrained-model/imagenet-vgg-verydeep-19.mat") sess.run(model['input'].assign(content_img)) out = model['conv4_2'] a_c = sess.run(out) a_g = out J_content = content_cost(a_c, a_g) STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2), ('conv4_1', 0.2), ('conv5_1', 0.2)] sess.run(model['input'].assign(style_img)) J_style = style_cost(model, STYLE_LAYERS, sess) J = total_cost(J_content, J_style) optimizer = tf.train.AdamOptimizer(2.0) train_step = optimizer.minimize(J) tf.global_variables_initializer().run() sess.run(model['input'].assign(generated_img)) for i in range(num_iter): sess.run(train_step) generated_img = sess.run(model['input']) if i % 20 == 0: Jt, Jc, Js = sess.run([J, J_content, J_style]) print("Iteration " + str(i) + " :") print("total cost = " + str(Jt)) print("content cost = " + str(Jc)) print("style cost = " + str(Js)) print(generated_img.shape) nu.save_image("output4/" + str(i) + ".png", generated_img) nu.save_image("output4/generated_image.png", generated_img) return generated_img
def read_check_image(self, fname): plt.subplot(1, 2, 1) image = mpimg.imread(fname) plt.imshow(image) # prepare for VGG input plt.subplot(1, 2, 2) h, w, c = image.shape if h > w: flip = True image = np.transpose(image, axes=(1, 0, 2)) else: flip = False image = nst_utils.reshape_and_normalize_image(image) plt.imshow(image[0]) print("shape of normalized image:", fname, image.shape) plt.show() return image, flip
def main(): print("hello world") model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat") print(model) #img = r'images\louvre.jpg' # img2 = r"C:\Users\cracr\Desktop\python_projects\deep_learning_small_projects\deepart\images\louvre.jpg" # img3 = r"C:/Users/cracr/Desktop/python_projects/deep_learning_small_projects/deepart/images/louvre.jpg" #image = Image.open(img) # content_image = scipy.misc.imread("images/louvre.jpg") # imshow(content_image) # if DISPLAY_IMG: plt.show() # style_image = scipy.misc.imread("images/monet_800600.jpg") # imshow(style_image) # if DISPLAY_IMG: plt.show() # Reset the graph tf.reset_default_graph() # Start interactive session sess = tf.InteractiveSession() # content_image = scipy.misc.imread("images/louvre_small.jpg") content_image = scipy.misc.imread("images/" + INPUT_IMG + ".jpg") content_image = reshape_and_normalize_image(content_image) # style_image = scipy.misc.imread("images/monet.jpg") style_image = scipy.misc.imread("images/vangogh.jpg") style_image = reshape_and_normalize_image(style_image) generated_image = generate_noise_image(content_image) imshow(generated_image[0]) if DISPLAY_IMG: plt.show() model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat") # Assign the content image to be the input of the VGG model. sess.run(model['input'].assign(content_image)) # Select the output tensor of layer conv4_2 out = model['conv4_2'] # Set a_C to be the hidden layer activation from the layer we have selected a_C = sess.run(out) # Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2'] # and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that # when we run the session, this will be the activations drawn from the appropriate layer, with G as input. a_G = out # Compute the content cost J_content = compute_content_cost(a_C, a_G) # Assign the input of the model to be the "style" image sess.run(model['input'].assign(style_image)) # Compute the style cost J_style = compute_style_cost(model, STYLE_LAYERS, sess) J = total_cost(J_content, J_style) # define optimizer (1 line) optimizer = tf.train.AdamOptimizer(2.0) # define train_step (1 line) train_step = optimizer.minimize(J) model_nn(sess, generated_image, model, train_step, J, J_content, J_style)
def generate_noise_image2(content_image, noise_ratio=0.6): """ Generates a noisy image by adding random noise to the content_image """ height = content_image.shape[1] width = content_image.shape[2] # Generate a random noise_image noise_image = np.random.uniform(-20, 20, (1, height, width, 3)).astype('float32') # range_ = np.max(content_image) - np.min(content_image) # noise_image = np.random.normal(loc=np.mean(content_image), scale = 0.7*range_, size=(1, height, width, 3)).astype('float32') # Set the input_image to be a weighted average of the content_image and a noise_image input_image = noise_image * noise_ratio + content_image * (1 - noise_ratio) return input_image STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2), ('conv4_1', 0.2), ('conv5_1', 0.2)] # Reset the graph tf.reset_default_graph() # Start interactive session sess = tf.InteractiveSession() content_image = scipy.misc.imread("images/z_me3.jpg") content_image = reshape_and_normalize_image(content_image) style_image = scipy.misc.imread("images/constable.jpg") style_image = reshape_and_normalize_image(style_image) generated_image = generate_noise_image2(content_image) #print(generated_image.shape) imshow(generated_image[0]) model = load_vgg_model("pretrained-model/imagenet-vgg-verydeep-19.mat") # Assign the content image to be the input of the VGG model. sess.run(model['input'].assign(content_image)) # Select the output tensor of layer conv4_2 out = model['conv4_2'] # Set a_C to be the hidden layer activation from the layer we have selected a_C = sess.run(out) # Set a_G to be the hidden layer activation from same layer. Here, a_G references model['conv4_2'] # and isn't evaluated yet. Later in the code, we'll assign the image G as the model input, so that # when we run the session, this will be the activations drawn from the appropriate layer, with G as input. a_G = out # Compute the content cost J_content = compute_content_cost(a_C, a_G) # Assign the input of the model to be the "style" image sess.run(model['input'].assign(style_image)) # Compute the style cost J_style = compute_style_cost(sess, model, STYLE_LAYERS) ### START CODE HERE ### (1 line) J = total_cost(J_content, J_style, alpha=10, beta=40) ### END CODE HERE ### # define optimizer (1 line) optimizer = tf.train.AdamOptimizer(1.0) # define train_step (1 line) train_step = optimizer.minimize(J) # Initialize global variables (you need to run the session on the initializer) ### START CODE HERE ### (1 line) sess.run(tf.global_variables_initializer()) ### END CODE HERE ### # Run the noisy input image (initial generated image) through the model. Use assign(). ### START CODE HERE ### (1 line) sess.run(model['input'].assign(generated_image)) ### END CODE HERE ### for i in range(200): # Run the session on the train_step to minimize the total cost ### START CODE HERE ### (1 line) sess.run(train_step) ### END CODE HERE ### # Compute the generated image by running the session on the current model['input'] ### START CODE HERE ### (1 line) generated_image = sess.run(model['input']) ### END CODE HERE ### # Print every 20 iteration. if i % 2 == 0: Jt, Jc, Js = sess.run([J, J_content, J_style]) print("Iteration " + str(i) + " :") print("total cost = " + str(Jt)) print("content cost = " + str(Jc)) print("style cost = " + str(Js)) # save current generated image in the "/output" directory save_image("output/z_m3_3_constable_" + str(i) + ".png", generated_image) # save last generated image save_image('output/z_me_3_constable_generated_image.jpg', generated_image) return generated_image
J = total_cost(J_content, J_style) print("J = " + str(J)) test.close() """ STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2), ('conv4_1', 0.2), ('conv5_1', 0.2)] #重设图 tf.reset_default_graph() #第1步:创建交互会话 sess = tf.InteractiveSession() #第2步:加载内容图像(卢浮宫博物馆图片),并归一化图像 content_image = scipy.misc.imread("E:\wuenda\images/louvre_small.jpg") content_image = nst_utils.reshape_and_normalize_image(content_image) #第3步:加载风格图像(印象派的风格),并归一化图像 style_image = scipy.misc.imread("E:\wuenda\images/monet.jpg") style_image = nst_utils.reshape_and_normalize_image(style_image) #第4步:随机初始化生成的图像,通过在内容图像中添加随机噪声来产生噪声图像 generated_image = nst_utils.generate_noise_image(content_image) imshow(generated_image[0]) #第5步:加载VGG16模型 model = nst_utils.load_vgg_model( "E:\wuenda\pretrained-model/imagenet-vgg-verydeep-19.mat") #第6步:构建TensorFlow图: ##将内容图像作为VGG模型的输入。
def reshape_image(image): return reshape_and_normalize_image(image)
def total_cost(J_content, J_style, alpha=10, beta=100): J = alpha * J_content + beta * J_style return J ################################################################################ model = load_vgg_model('./model/imagenet-vgg-verydeep-19.mat') with tf.device('/gpu:0'): sess = tf.InteractiveSession() style_image = scipy.misc.imread('images/prisma3.jpg') style_image = scipy.misc.imresize(style_image, (300, 400, 3)) #print(style_image.shape) style_image = reshape_and_normalize_image(style_image) content_image = scipy.misc.imread('images/love.jpeg') content_image = scipy.misc.imresize(content_image, (300, 400, 3)) #plt.imshow(content_image) content_image = reshape_and_normalize_image(content_image) generated_image = generate_noise_image(content_image) sess.run(model['input'].assign(content_image)) out = model['conv4_2'] a_C = sess.run(out) a_G = out J_content = content_cost(a_C, a_G) sess.run(model['input'].assign(style_image))
def load_normalize_style_img(style_img_path): style_img = imread(style_img_path) style_img = reshape_and_normalize_image(style_img) return style_img
def load_normalize_content_img(content_img_path): content_img = imread(content_img_path) content_img = reshape_and_normalize_image(content_img) return content_img
model = util.load_vgg_model(util.CONFIG.VGG_MODEL) STYLE_LAYERS = [('conv1_1', 0.2), ('conv2_1', 0.2), ('conv3_1', 0.2), ('conv4_1', 0.2), ('conv5_1', 0.2)] #STYLE_LAYERS = [ # ('conv1_1', 0.01), # ('conv2_1', 0.01), # ('conv3_1', 0.01)] content_image = scipy.misc.imread(util.CONFIG.CONTENT_IMAGE) content_image = scipy.misc.imresize(content_image, size=(util.CONFIG.IMAGE_HEIGHT, util.CONFIG.IMAGE_WIDTH, util.CONFIG.COLOR_CHANNELS)) content_image = util.reshape_and_normalize_image(content_image) content_image = np.float32(content_image) style_image = scipy.misc.imread(util.CONFIG.STYLE_IMAGE) style_image = scipy.misc.imresize(style_image, size=(util.CONFIG.IMAGE_HEIGHT, util.CONFIG.IMAGE_WIDTH, util.CONFIG.COLOR_CHANNELS)) style_image = util.reshape_and_normalize_image(style_image) style_image = np.float32(style_image) generated_image = util.generate_noise_image(content_image) imshow(generated_image[0]) imshow(content_image[0]) imshow(style_image[0])