-
http://takatakamanbou.hatenablog.com/entry/2017/08/15/213714
-
http://www-tlab.math.ryukoku.ac.jp/tlab/index.php?takataka%2Fnote%2F2017-08-14
-
ex170815.py 学習
- getdata170815.py データ作成
- network170815.py ネットワークの定義
-
ex170815c.py 学習後のネットワークを用いて1枚の画像をカラー化.入力画像(カラー)を YCrCb に変換,Yを入力して Cr と Cb を推定し,それらを合わせたカラー画像を出力する.
-
-
Save takatakamanbou/88b75a2f77bee68c563a975a5b338600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import cv2 | |
import numpy as np | |
import tensorflow as tf | |
import sys | |
import os | |
import network170815 as network | |
import getdata170815 as getdata | |
if __name__ == '__main__': | |
dirResult = 'result_ex170815' | |
patchsizeX = 64 | |
### network initialization | |
# | |
eta = 0.001 | |
optimizer = tf.train.AdamOptimizer(learning_rate = eta) | |
with tf.Graph().as_default(): | |
nn = network.Network([patchsizeX, patchsizeX, 1], optimizer = optimizer) | |
nn.init() | |
Yshape = list(np.array(nn.Y.shape[1:], dtype = int)) | |
patchsizeY = Yshape[0] | |
### data preparation | |
# | |
data = getdata.Data(patchsizeX, 500) | |
XL = data.XL[:, :, :, np.newaxis] | |
nL = XL.shape[0] | |
delta = (patchsizeX - patchsizeY)//2 | |
YtL = data.YtL[:, delta:delta+patchsizeY, delta:delta+patchsizeY, :] | |
print('# XL: ', XL.shape) | |
print('# YtL: ', YtL.shape) | |
XV = data.XT[:, :, :, np.newaxis] | |
nV = XV.shape[0] | |
YtV = data.YtT[:, delta:delta+patchsizeY, delta:delta+patchsizeY, :] | |
print('# XV: ', XV.shape) | |
print('# YtV: ', YtV.shape) | |
### learning | |
# | |
batchsize = 128 | |
nitr = 1000 | |
for i in range(nitr+1): | |
if (i < 500 and i % 100 == 0) or i % 500 == 0: | |
path = dirResult + '/' + os.path.splitext(sys.argv[0])[0] + '-params-itr%05d.npz' % i | |
params_dict = nn.getWeight() | |
np.savez_compressed(path, **params_dict) | |
if (i < 100 and i % 10 == 0) or i % 100 == 0: | |
#sqeL = nn.cost(XL, YtL) / (patchsizeY*patchsizeY) | |
sqeV = nn.cost(XV, YtV) / (patchsizeY*patchsizeY) | |
#print(i, sqeL, sqeV) | |
print(i, sqeV) | |
idx = np.random.randint(0, nL, size = batchsize) | |
nn.train(XL[idx, ::], YtL[idx, ::]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import cv2 | |
import numpy as np | |
import tensorflow as tf | |
import sys | |
import os | |
import network170815 as network | |
if __name__ == '__main__': | |
if len(sys.argv) != 3: | |
exit('usage: %s fnSrc fnDst' % sys.argv[0]) | |
fnSrc, fnDst = sys.argv[1:] | |
with tf.Graph().as_default(): | |
nn = network.Network([None, None, 1]) | |
pathNetworkParameters = 'result_ex170815/ex170815-params-itr01000.npz' | |
with np.load(pathNetworkParameters) as hoge: | |
nn.setWeight(hoge) | |
delta = 4 | |
imgBGR = cv2.imread(fnSrc) | |
imgYCC = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2YCR_CB) | |
Xraw = imgYCC[:, :, 0, np.newaxis] | |
Ytraw = imgYCC[:, :, 1:] | |
X = Xraw.astype(np.float32) - 128 | |
Yt = Ytraw.astype(np.float32) - 128 | |
hX, wX = imgYCC.shape[:2] | |
Y = nn.output(X[np.newaxis, ::])[0] | |
hY, wY = Y.shape[:2] | |
msqe = np.mean(np.square(Yt[delta:delta+hY, delta:delta+wY, :] - Y)) | |
print(fnSrc, hX, wX, hY, wY, msqe) | |
YY = np.asarray(Y + 128, dtype = int) | |
Z = np.copy(imgYCC) | |
Z[delta:delta+hY, delta:delta+wY, 1] = YY[:, :, 0] | |
Z[delta:delta+hY, delta:delta+wY, 2] = YY[:, :, 1] | |
imgRec = cv2.cvtColor(Z, cv2.COLOR_YCR_CB2BGR) | |
cv2.imwrite(fnDst, imgRec) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import cv2 | |
import numpy as np | |
class Data(): | |
# path to the directory containing the images | |
dirImage = 'images/cherry' | |
nimg = 20 | |
def __init__(self, patchsize, numpatch_per_image): | |
X = np.empty((Data.nimg, numpatch_per_image, patchsize, patchsize), dtype = np.float32) | |
Yt = np.empty((Data.nimg, numpatch_per_image, patchsize, patchsize, 2), dtype = np.float32) | |
for i in range(Data.nimg): | |
fn = Data.dirImage + '/img%03d.png' % i # img000.png, img001.png,.. | |
X[i, ::], Yt[i, ::] = getPatches(fn, patchsize, numpatch_per_image) | |
X -= 128 | |
Yt -= 128 | |
### L & T ### | |
nimgL = 15 # the first nimgL images are used for training | |
nimgT = Data.nimg - nimgL # the remaining images are for testing | |
self.XL = X[:nimgL, ::].reshape((-1, patchsize, patchsize)) | |
self.YtL = Yt[:nimgL, ::].reshape((-1, patchsize, patchsize, 2)) | |
self.XT = X[nimgL:, ::].reshape((-1, patchsize, patchsize)) | |
self.YtT = Yt[nimgL:, ::].reshape((-1, patchsize, patchsize, 2)) | |
def getPatches(fn, patchsize, numpatch): | |
imgBGR = cv2.imread(fn) | |
imgYCC = cv2.cvtColor(imgBGR, cv2.COLOR_BGR2YCR_CB) | |
h, w = imgBGR.shape[:2] | |
X = np.empty((numpatch, patchsize, patchsize)) | |
Yt = np.empty((numpatch, patchsize, patchsize, 2)) | |
xi = np.random.randint(0, w - patchsize, size = numpatch) | |
yi = np.random.randint(0, h - patchsize, size = numpatch) | |
for i in range(numpatch): | |
imgBuf = imgYCC[yi[i]:yi[i]+patchsize, xi[i]:xi[i]+patchsize, :] | |
X[i, :, :] = imgBuf[:, :, 0] # Y | |
Yt[i, :, :, 0] = imgBuf[:, :, 1] # Cr | |
Yt[i, :, :, 1] = imgBuf[:, :, 2] # Cb | |
return X, Yt | |
if __name__ == '__main__': | |
d = Data(64, 100) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
from __future__ import unicode_literals | |
import tensorflow as tf | |
import tensorflow.contrib as tfc | |
import numpy as np | |
class Network(): | |
def __init__(self, Xshape, optimizer = None): | |
### network configuration | |
# | |
self.Xshape = Xshape | |
self.inputs = tf.placeholder(tf.float32, shape = [None] + Xshape) | |
self.params = dict() | |
### conv1 | |
# | |
Wshape = [5, 5, 1, 16] | |
W = tf.get_variable('W1', shape = Wshape, initializer = tfc.layers.xavier_initializer_conv2d()) | |
Y = tf.nn.conv2d(self.inputs, W, [1, 1, 1, 1], padding = 'VALID') | |
b = tf.get_variable('b1', shape = Wshape[-1], initializer = tf.zeros_initializer) | |
self.conv1 = tf.nn.relu(Y + b) | |
self.params['W1'] = W | |
self.params['b1'] = b | |
### conv2 | |
# | |
Wshape = [3, 3, 16, 32] | |
W = tf.get_variable('W2', shape = Wshape, initializer = tfc.layers.xavier_initializer_conv2d()) | |
Y = tf.nn.conv2d(self.conv1, W, [1, 1, 1, 1], padding = 'VALID') | |
b = tf.get_variable('b2', shape = Wshape[-1], initializer = tf.zeros_initializer) | |
self.conv2 = tf.nn.relu(Y + b) | |
self.params['W2'] = W | |
self.params['b2'] = b | |
### conv3 | |
# | |
Wshape = [3, 3, 32, 64] | |
W = tf.get_variable('W3', shape = Wshape, initializer = tfc.layers.xavier_initializer_conv2d()) | |
Y = tf.nn.conv2d(self.conv2, W, [1, 1, 1, 1], padding = 'VALID') | |
b = tf.get_variable('b3', shape = Wshape[-1], initializer = tf.zeros_initializer) | |
self.conv3 = tf.nn.relu(Y + b) | |
self.params['W3'] = W | |
self.params['b3'] = b | |
### conv4 | |
# | |
Wshape = [1, 1, 64, 2] | |
W = tf.get_variable('W4', shape = Wshape, initializer = tfc.layers.xavier_initializer_conv2d()) | |
Y = tf.nn.conv2d(self.conv3, W, [1, 1, 1, 1], padding = 'VALID') | |
b = tf.get_variable('b4', shape = Wshape[-1], initializer = tf.zeros_initializer) | |
#self.conv4 = tf.nn.relu(Y + b) | |
self.conv4 = Y + b | |
self.params['W4'] = W | |
self.params['b4'] = b | |
### definition for output computation | |
# | |
self.X = self.inputs | |
self.Xshape = Xshape | |
self.Y = self.conv4 | |
self.cg_output = self.Y | |
### definition for cost | |
# | |
#self.Yt = tf.placeholder(tf.float32, shape = [None] + self.Yshape) | |
self.Yt = tf.placeholder(tf.float32) | |
sqe = tf.reduce_sum(tf.squared_difference(self.Y, self.Yt), axis = [1,2,3]) | |
#sqe = tf.reduce_sum(tf.abs(self.Y - self.Yt), axis = [1,2,3]) | |
self.cg_cost = tf.reduce_mean(sqe) | |
### definition for training | |
# | |
self.optimizer = optimizer | |
if optimizer != None: | |
self.cg_train = self.optimizer.minimize(self.cg_cost) | |
### definition for parameter initialization | |
# | |
self.cg_init = tf.global_variables_initializer() | |
### starting the session | |
# | |
self.sess = tf.InteractiveSession() | |
def init(self): | |
rv = self.sess.run(self.cg_init) | |
return rv | |
def output(self, X): | |
d = {self.X: X} | |
rv = self.sess.run(self.cg_output, feed_dict = d) | |
return rv | |
def train(self, X, Yt): | |
d = {self.X: X, self.Yt: Yt} | |
rv = self.sess.run(self.cg_train, feed_dict = d) | |
return rv | |
def cost(self, X, Yt): | |
d = {self.X: X, self.Yt: Yt} | |
rv = self.sess.run(self.cg_cost, feed_dict = d) | |
return rv | |
def getWeight(self): | |
return self.sess.run(self.params) | |
def setWeight(self, vals_dict): | |
L = [] | |
for k in vals_dict.keys(): | |
L.append(tf.assign(self.params[k], vals_dict[k])) | |
self.sess.run(L) | |
if __name__ == '__main__': | |
Xshape = [64, 64, 1] | |
with tf.Graph().as_default(): | |
nn = Network(Xshape) | |
nn.init() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment