In [2]:
import tensorflow as tf
from PIL import Image
import numpy as np
from datetime import datetime
In [30]:
#이미지, 상수들
W1=640
H1=360
W2=1280
H2=720
path="/home/alpha/Dev/R&E/"
pref1="360p/"
pref2="720p/"
suff1="_360.jpg"
suff2="_720.jpg"
train_num=50
file_num=86#30
#batch_num=1000
In [31]:
#가중치 초기화 함수
def weight_variable(shape, name):
initial = tf.truncated_normal(shape, stddev=0.1)
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 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_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_2
In [32]:
#학습때 사용하는 변수들
x_image = tf.placeholder(np.float32, shape=[None, H1, W1, 3])
y_image = tf.placeholder(np.float32, shape=[None, H2, W2, 3])
#가중치, 절편, 결과
W_conv = weight_variable([10, 10, 3, 12], name='weight')
b_conv = bias_variable([12], name='bias')
y_conv = tf.nn.relu(conv2d(x_image, W_conv)+b_conv)
y_res = tf.reshape(y_conv, [-1, H2, W2, 3])
In [33]:
cost = tf.reduce_sum((y_image-y_res)*(y_image-y_res))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cost)
saver = tf.train.Saver()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
#saver.restore(sess, "01/models.ckpt")
In [34]:
for steps in range(train_num):
for index in range(file_num):
array360, array720 = getimage(index)
sess.run(train_step, feed_dict={x_image:[array360], y_image:[array720]})
#print(sess.run(cost, feed_dict={x_image:[array360], y_image:[array720]}))
if(steps%10==0):
print(sess.run(cost, feed_dict={x_image:[array360], y_image:[array720]}))
f=open("01/logs.txt", "a")
f.write(str(datetime.now())+" | "+str(steps)+" : "+str(sess.run(cost, feed_dict={x_image:[array360], y_image:[array720]}))+'\n')
f.close()
#if(steps%10==):
save_path=saver.save(sess, "01/models.ckpt")
In [36]:
#pixel_array=CD_array.astype(np.uint8)
#img=Image.frombytes('RGB', (Width, Height), pixel_array);
#img.show()
#print(CD_array[0])
#img_GS.show()
'''
A=np.array(img_GS)
B=np.reshape(GS_array, (Height, Width))
B=B.astype(np.uint8)
Image.fromarray(B, 'L').show()'''
test360, test720 = getimage(50)
A=sess.run(y_res, feed_dict={x_image:[test360], y_image:[test720]})
result = A[0].astype(np.uint8)
img360=test360.astype(np.uint8)
img720=test720.astype(np.uint8)
Image.fromarray(img360, 'RGB').save('res/img360.jpg')
Image.fromarray(img720, 'RGB').save('res/img720.jpg')
Image.fromarray(result, 'RGB').save('res/result.jpg')
'''
bird=Image.open("person_GS.jpg")
GS_B=np.array(bird)[:, :]
GS_B=np.reshape(GS_B, (Height, Width, 1))
GS_B=GS_B.astype(np.float32)
bird_CD=Image.open("person_CD.jpg")
CD_B=np.array(bird_CD)[:, :, 0:3]
CD_B=CD_B.astype(np.float32)
#Image.fromarray(CD_array, 'RGB').show()
A=sess.run(y_conv, feed_dict={x_image:[GS_B], y_image:[CD_B]})
A=A[0].astype(np.uint8)
B=np.reshape(GS_B, (Height, Width))
B=B.astype(np.uint8)
img = Image.fromarray(A, 'RGB')
img.save('img3.jpg')
img.show()
img1=Image.fromarray(B, 'L')
img1.show()
img1.save('img4.jpg')
'''
Out[36]: