프로젝트명 : CNN을 이용한 스타일 매트릭스 ( 그람 매트릭스 )활용 그림의 화풍을 추출.


   < 실제 원본 사진>

< 스타일을 추출할 사진>

     

       < 아래 사진의 화풍으로 재 구성된 사진>


참고자료 :  A Neural Algorithm of Artistic Style Leon A. GatysAlexander S. EckerMatthias Bethge

Google Colab 노트북 주소 : https://colab.research.google.com/drive/1oxLoq292ZAfrfiJUaXJri5CaxWn6bdJk











1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
"""_Style_Transfer.ipynb
Automatically generated by Colaboratory.
Original file is located at
    https://colab.research.google.com/drive/1oxLoq292ZAfrfiJUaXJri5CaxWn6bdJk
"""
 
from google.colab import drive
drive.mount('/gdrive')
PATH = "/gdrive/My Drive/Colab Notebooks/project/Style_Trasfer/"
 
# Commented out IPython magic to ensure Python compatibility.
!rm --*
!cp /gdrive/My\ Drive/Colab\ Notebooks/project/Style_Trasfer/*.py ./
 
import os
import sys
import scipy.io
import scipy.misc
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from PIL import Image
from nst_utils import *
import numpy as np
import tensorflow as tf
 
# %matplotlib inline
 
model = load_vgg_model( PATH + "pretrained-model/imagenet-vgg-verydeep-19.mat" )
 
content_image = Image.open(PATH + 'images/bird_orig.png')
 
def compute_content_cost(a_C, a_G):
    
    m, n_H, n_W, n_C = a_G.get_shape().as_list()
    
    a_C_unrolled = tf.reshape(tf.transpose(a_C),shape = [n_C, -1])
    a_G_unrolled = tf.reshape(tf.transpose(a_G),shape = [n_C, -1])
    
    J_content = tf.reduce_sum(tf.squared_difference(a_C_unrolled, a_G_unrolled)) / (4 * n_H * n_W * n_C ) 
    
    return J_content
 
style_image = Image.open(PATH + 'images/starry_night.png')
imshow(style_image)
 
def compute_layer_style_cost(a_S, a_G):
    try :
        m, n_H, n_W, n_C = a_S.get_shape().as_list()
    except AttributeError  as e:
        m, n_H, n_W, n_C = a_S.shape
    
    a_S = tf.reshape(tf.transpose(a_S), [n_C, -1]) 
    a_G = tf.reshape(tf.transpose(a_G), [n_C, -1]) 
 
    GS = tf.matmul(a_S , tf.transpose(a_S)) 
    GG = tf.matmul(a_G , tf.transpose(a_G))
 
    J_style_layer = tf.reduce_sum(tf.squared_difference(GS, GG)) / (4 * (n_C ** 2* ((n_H * n_W) ** 2))
    
    
    return J_style_layer
 
STYLE_LAYERS = [
    ('conv1_1'0.5),
    ('conv2_1'0.5),
    ('conv3_1'0.5),
    ('conv4_1'0.5),
    ('conv5_1'0.5)]
 
def compute_style_cost(model, STYLE_LAYERS):
    J_style = 0
 
    for layer_name, coeff in STYLE_LAYERS:
        
        out = model[layer_name]
 
        a_S = sess.run(out)
        a_G = out
        
        J_style_layer = compute_layer_style_cost(a_S, a_G)
        J_style += coeff * J_style_layer
 
    return J_style
 
def total_cost(J_content, J_style, alpha = 10, beta = 40):
    J = alpha * J_content + beta * J_style
    return J
 
tf.reset_default_graph()
 
sess = tf.InteractiveSession()
 
import PIL
content_image = np.array(Image.open(PATH + 'images/bird_orig.png').resize([400,300])) 
content_image = reshape_and_normalize_image(content_image)
 
style_image = np.array(Image.open(PATH + 'images/starry_night.png').resize([400,300])) 
style_image = reshape_and_normalize_image(style_image)
 
generated_image = generate_noise_image( content_image)
imshow(generated_image[0])
 
model = load_vgg_model( PATH + "pretrained-model/imagenet-vgg-verydeep-19.mat" )
sess.run(model['input'].assign( content_image ))
 
out = model['conv3_2']
a_C = sess.run(out)
 
a_G = out
 
J_content = compute_content_cost(a_C, a_G)
 
 
 
sess.run(model['input'].assign(style_image))
 
J_style = compute_style_cost(model, STYLE_LAYERS)
 
= total_cost(alpha = 10, beta = 160, J_content = J_content, J_style = J_style)
 
# define optimizer (1 line)
optimizer = tf.train.AdamOptimizer(2.0)
 
# define train_step (1 line)
train_step = optimizer.minimize(J)
 
import matplotlib.pyplot as plt
def save_image(path, image):
    
    # Un-normalize the image so that it looks good
    image = image + CONFIG.MEANS
    
    # Clip and Save the image
    image = np.clip(image[0], 0255).astype('uint8')
    plt.imsave(path, image)
 
def model_nn(sess, input_image, num_iterations = 200):
    
    # 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(input_image))
    ### END CODE HERE ###
    
    for i in range(num_iterations):
    
        # 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%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))
            
            # save current generated image in the "/output" directory
            save_image(PATH+"output/" + str(i) + ".png", generated_image)
    
    # save last generated image
    save_image(PATH+'output/generated_image.jpg', generated_image)
    
    return generated_image
 
model_nn(sess, generated_image)
 
Image.open(PATH+'output/generated_image.jpg')
 
cs


+ Recent posts