Created
February 8, 2017 17:38
-
-
Save DataWraith/1983041b2ae9586a26cedeeffe63394f to your computer and use it in GitHub Desktop.
Simple image regression using Tensorflow and an ad hoc network that turned out to work well
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
import math | |
import tensorflow as tf | |
import numpy as np | |
import tensorflow.contrib.slim as slim | |
from scipy import misc | |
class IMR(object): | |
def __init__(self, sess, channels = 3): | |
self.sess = sess | |
self.num_channels= channels | |
self.input_tensor = tf.placeholder(tf.float32, [None, 3]) | |
self.net = slim.fully_connected(self.input_tensor, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net = slim.fully_connected(self.net, 512, activation_fn=tf.nn.relu) | |
self.net2 = slim.fully_connected(self.input_tensor, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net2 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.tanh) | |
self.net3 = slim.fully_connected(self.input_tensor, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net3 = slim.fully_connected(self.net, 512, activation_fn=tf.nn.elu) | |
self.net = self.net + self.net2 + self.net3 | |
self.output = slim.fully_connected(self.net, channels, activation_fn=None) | |
self.wanted = tf.placeholder(tf.float32, [None, channels]) | |
self.loss = tf.reduce_mean(tf.nn.l2_loss(self.wanted - self.output)) | |
self.optimizer = tf.train.AdamOptimizer(1e-4).minimize(self.loss) | |
def train(self, inputs, wanted): | |
_, loss = self.sess.run([self.optimizer, self.loss], feed_dict={ | |
self.input_tensor: inputs, | |
self.wanted: wanted | |
}) | |
return loss | |
def predict(self, inputs): | |
return self.sess.run(self.output, { | |
self.input_tensor: inputs | |
}) | |
def make_img(self, w, h): | |
X = [] | |
for y in range(h): | |
for x in range(w): | |
xi = float(x) / w | |
yi = float(y) / h | |
r = math.sqrt((x - w/2)**2 + (y - h/2)**2) / (0.5 * math.sqrt(w**2 + h**2)) | |
X.append([xi, yi, r]) | |
result = self.predict(X) | |
outimg = np.empty((h, w, self.num_channels)) | |
for y in range(h): | |
for x in range(w): | |
for j in range(self.num_channels): | |
outimg[y][x][j] = max(0, min(255, int(255 * result[0][j]))) | |
result = result[1:] | |
return outimg | |
batch_idx = 0 | |
fullX = [] | |
fullY = [] | |
def init_batch(source): | |
global fullX | |
global fullY | |
height, width, channels = source_img.shape | |
for y in range(height): | |
for x in range(width): | |
inputr = math.sqrt((float(x) - width / 2)**2 + (float(y) - height / 2)**2) / (0.5 * math.sqrt(width**2 + height**2)) | |
inputx = (float(x) / width) | |
inputy = (float(y) / height) | |
fullX.append([inputx, inputy, inputr]) | |
fst = [] | |
for j in range(channels): | |
fst.append(float(source[y][x][j]) / 255) | |
fullY.append(fst) | |
perm = np.random.permutation(len(fullX)) | |
fullX = [fullX[k] for k in perm] | |
fullY = [fullY[k] for k in perm] | |
def next_batch(batch_size=200): | |
global fullX | |
global fullY | |
global batch_idx | |
resX = fullX[batch_idx:(batch_idx + batch_size)] | |
resY = fullY[batch_idx:(batch_idx + batch_size)] | |
batch_idx += batch_size | |
if batch_idx >= len(fullX): | |
batch_idx = 0 | |
print("BATCH RESET") | |
perm = np.random.permutation(len(fullX)) | |
fullX = [fullX[k] for k in perm] | |
fullY = [fullY[k] for k in perm] | |
return (resX, resY) | |
with tf.Session() as sess: | |
imr = IMR(sess) | |
sess.run(tf.global_variables_initializer()) | |
sess.run(tf.local_variables_initializer()) | |
source_img = misc.imread("source.png") | |
init_batch(source_img) | |
i = -1 | |
while True: | |
i += 1 | |
bX, by = next_batch() | |
print(i, imr.train(bX, by)) | |
if (i % 1000 == 999): | |
misc.imsave('imr-%08d.png' % i, imr.make_img(400, 500)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot for sharing the code. I am comparatively novice in the field of neural network and image regression. Is there any way you could share some documentation of what the code does, specifically image input and output. Thanks in advance.