In [ ]:
import tensorflow as tf
from PIL import Image
import numpy as np

#이미지, 상수들
learning_rate=5e-5
W1=192
H1=144
W2=960
H2=720
path="../04/"
pref1="144p/"
pref2="720p/"
suff1=".jpg"
suff2=".jpg"
train_num=1000
file_num=2#6#30
#batch_num=1000


#가중치 초기화 함수
def weight_variable(shape, name):
  initial = tf.truncated_normal(shape, stddev=0.00000001)
  return tf.Variable(initial, name=name)
#절편 초기화 함수
def bias_variable(shape, name):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial, name=name)
#2D 컨벌루션 실행
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_unpool(inp, argmax, argmax_mask, k=2):
    return tf.nn.max_unpool(inp, argmax, argmax_mask, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding="SAME")

def getimage(idx):
    img_1=Image.open(path+pref1+str(idx).zfill(5)+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).zfill(5)+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 showres(index, steps):
    test360, test_res, test720 = getimage(index)
    A=sess.run(y_res, feed_dict={x_image:[test360], x_image_720:[test_res], y_image:[test720]})
    result = A[0].astype(np.uint8)
    #Image.fromarray(img360, 'RGB').save('res/img360_'+str(steps)+'.jpg')
    #Image.fromarray(img720, 'RGB').save('res/img720_'+str(steps)+'.jpg')
    Image.fromarray(result, 'RGB').save('results/result_'+str(steps)+'.jpg')


#학습때 사용하는 변수들
x_image = tf.placeholder(np.float32, shape=[None, H1, W1, 3])
x_image_720 = tf.placeholder(np.float32, shape=[None, H2, W2, 3])
y_image = tf.placeholder(np.float32, shape=[None, H2, W2, 3])
#가중치, 절편, 결과
W_conv = weight_variable([15, 15, 3, 7], name='weight')
W_conv2= weight_variable([10,10, 7, 3], name='weight')#####
#b_conv = bias_variable([7], name='bias')
#b_conv2= bias_variable([3], name='bias')
y_conv = l_relu(conv2d(x_image_720, W_conv), alpha=0.5)##
y_conv2 = l_relu(conv2d(y_conv, W_conv2), alpha=0.5)##
y_res = tf.reshape(y_conv2, [-1, H2, W2, 3])+x_image_720# 고쳐야됨


cost = tf.reduce_sum((y_image-y_res)*(y_image-y_res))
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")

for steps in range(train_num):
    for index in range(1, file_num):
        array360, array360_720, array720 = getimage(index)
        sess.run(train_step, feed_dict={x_image:[array360], x_image_720:[array360_720], y_image:[array720]})
    print (steps)
    print(sess.run(cost, feed_dict={x_image:[array360], x_image_720:[array360_720],y_image:[array720]}))
    if(steps%5==0):
        showres(index, steps)
print ("끝났")


0
9.07889e+08
1
9.079e+08
2
9.0787e+08
3
9.07821e+08
4
9.07778e+08
5
9.07669e+08
6
9.07492e+08
7
9.07242e+08
8
9.06923e+08
9
9.06525e+08
10
9.06055e+08
11
9.05539e+08
12
9.04982e+08
13
9.0437e+08
14
9.03681e+08
15
9.02913e+08
16
9.02085e+08
17
9.01207e+08
18
9.00279e+08
19
8.99289e+08
20
8.98247e+08
21
8.97168e+08
22
8.96049e+08
23
8.9489e+08
24
8.93709e+08
25
8.9251e+08
26
8.91298e+08
27
8.90087e+08
28
8.88878e+08
29
8.87696e+08
30
8.86535e+08
31
8.85412e+08
32
8.84328e+08
33
8.83297e+08
34
8.82322e+08
35
8.81412e+08
36
8.80566e+08
37
8.79783e+08
38
8.79054e+08
39
8.78385e+08
40
8.77765e+08
41
8.77188e+08
42
8.76645e+08
43
8.76127e+08
44
8.75629e+08
45
8.75137e+08
46
8.74652e+08
47
8.74174e+08
48
8.73696e+08
49
8.73218e+08
50
8.72735e+08
51
8.72253e+08
52
8.71773e+08
53
8.71293e+08
54
8.70819e+08
55
8.70343e+08
56
8.69878e+08
57
8.69417e+08
58
8.68957e+08
59
8.685e+08
60
8.68045e+08
61
8.67591e+08
62
8.67139e+08
63
8.66689e+08
64
8.66238e+08
65
8.65786e+08
66
8.65339e+08
67
8.64896e+08
68
8.64461e+08
69
8.64022e+08
70
8.63585e+08
71
8.63142e+08
72
8.62697e+08
73
8.62249e+08
74
8.61802e+08
75
8.61359e+08
76
8.60911e+08
77
8.60473e+08
78
8.60036e+08
79
8.59607e+08
80
8.59181e+08
81
8.58758e+08
82
8.5834e+08
83
8.57926e+08
84
8.57519e+08
85
8.57118e+08
86
8.56723e+08
87
8.56333e+08
88
8.55948e+08
89
8.55567e+08
90
8.55195e+08
91
8.54829e+08
92
8.54468e+08
93
8.54116e+08
94
8.53769e+08
95
8.53433e+08
96
8.53101e+08
97
8.52781e+08
98
8.52459e+08
99
8.52148e+08
100
8.5184e+08
101

In [ ]:


In [ ]: