In [27]:
# 거창하게 GUN이라고 했지만 사실 해상도를 거의 두배씩 두번 올렸다.
import tensorflow as tf
from PIL import Image
import numpy as np
#이미지, 상수들
learning_rate=1e-4
ratio=16/9
H1=360
W1=int(ratio*H1)
H15=480
W15=int(ratio*H15)
H2=720
W2=int(ratio*H2)
path="../06/"
pref1="360p/"
pref2="720p/"
suff1="_360.jpg"
suff2="_720.jpg"
train_num=500#1000
file_num=1#6#30
#batch_num=1000
In [39]:
#가중치 초기화 함수
def weight_variable(shape, name):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial, name=name)
#절편 초기화 함수
def bias_variable(shape, name):
initial = 0.#tf.constant(0.1, shape=shape)
return tf.Variable(initial, name=name)
#2D 컨벌루션 실행
def conv2d(x, W, B):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')#+B
def getimage(idx):
img_1=Image.open(path+pref1+str(idx)+suff1)
array_1=np.array(img_1)[:, :]
array_1=array_1.astype(np.float32)
img_1_720 = img_1.resize((W2, H2), Image.BILINEAR)
array_1_720=np.array(img_1_720)[:, :]
array_1_720=array_1_720.astype(np.float32)
img_2=Image.open(path+pref2+str(idx)+suff2)
array_2=np.array(img_2)[:, :, 0:3]
array_2=array_2.astype(np.float32)
return array_1,array_1_720, array_2
def l_relu(x, alpha=0.):
return tf.nn.relu(x)-alpha*tf.nn.relu(-x)
def asImage(tensor):
result = tensor[0].astype(np.uint8)
return Image.fromarray(result, 'RGB')
def showres(index, steps):
inputarray ,test144, test720 = getimage(index)
A=sess.run(y_result, feed_dict={x_image144:[inputarray], x_image:[test144], y_image:[test720]})
result = A.astype(np.uint8)
#Image.fromarray(array144, 'RGB').save('results/img144.jpg')
#Image.fromarray(array720, 'RGB').save('results/img720.jpg')
asImage(result).save('results/'+str(steps)+'.jpg')
In [47]:
x_image144 = tf.placeholder(np.float32, shape=[None, H1, W1, 3], name='original_image_360')
x_image = tf.placeholder(np.float32, shape=[None, H2, W2, 3], name='bilinear_magnified_image_720')
y_image = tf.placeholder(np.float32, shape=[None, H2, W2, 3], name='answer_image')
weight1 = weight_variable([20, 20, 3, 30], name = 'weight1')
bias1 = bias_variable([30], name = 'bias1')
weight2 = weight_variable([1, 1, 30, 20], name = 'weight2')
bias2 = bias_variable([20], name='bias2')
weight3 = weight_variable([5, 5, 20, 3], name = 'weight3')
bias3 = bias_variable([3], name = 'bias3')
weight4 = weight_variable([20, 20, 3, 30], name = 'weight4')
bias4 = bias_variable([30], name = 'bias4')
weight5 = weight_variable([1, 1, 30, 20], name = 'weight5')
bias5 = bias_variable([20], name='bias5')
weight6 = weight_variable([5, 5, 20, 3], name = 'weight6')
bias6 = bias_variable([3], name = 'bias6')
In [48]:
Img1=tf.image.resize_bilinear(x_image144, (H15,W15))
F1 = l_relu(conv2d(Img1, weight1, bias1), alpha = 0.3)
F2 = l_relu(conv2d(F1, weight2, bias2), alpha = 0.3)
F3 = l_relu(conv2d(F2, weight3, bias3), alpha = 0.3)
Img2=tf.image.resize_bilinear(F3, (H2,W2))
F4 = l_relu(conv2d(Img2, weight4, bias4), alpha = 0.3)
F5 = l_relu(conv2d(F4, weight5, bias5), alpha = 0.3)
F6 = l_relu(conv2d(F5, weight6, bias6), alpha = 0.3)
y_result= l_relu(x_image+F6,alpha=0.)
In [ ]:
cost = tf.reduce_mean(tf.square(y_image-y_result))
train_step = tf.train.AdamOptimizer(learning_rate).minimize(cost)
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#saver.restore(sess, "01/models.ckpt")
In [ ]:
for steps in range(train_num):
for index in range(1, file_num+1):
##array144은 144p인 이미지를 확대해서 720p으로 만들어놓은것.
inputarray, array144, array720 = getimage(index)
#asImage([array720]).show()
sess.run(train_step, feed_dict={x_image144:[inputarray], x_image:[array144], y_image:[array720]})
inputarray, array144, array720 = getimage(index)
print (str(steps).zfill(3), sess.run(cost, feed_dict={x_image144:[inputarray],x_image:[array144], y_image:[array720]}))
if(steps%5==0):
showres(index, steps)
print ("끝났다. 근데 사실 이걸 본적은 한번도 없다.")
In [ ]: