RNN for pictures genaration

This notebook is an experiment. I tryed to generate a picture pixel by pixel using an RNN.
each pixel can be black or white. WIP

Import needed for Jupiter


In [1]:
%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plt

from IPython.display import Image

Import needed for the code


In [2]:
import numpy as np
import tensorflow as tf

import fnmatch, os
import time

from PIL import Image as Img

Helpers functions

to save a picture


In [3]:
#need to be called within a session
def write_png(tensor, name):
    casted_to_uint8 = tf.cast(tensor, tf.uint8)
    converted_to_png = tf.image.encode_png(casted_to_uint8)
    f = open(name, "wb+")
    f.write(converted_to_png.eval())
    f.close()

A class to define all args


In [35]:
class Args():
    def __init__(self):
        '''directory to store checkpointed models'''
        self.save_dir = 'save_face_training_0.4'
        
        '''Picture size'''
        self.picture_size = 32
    
        '''size of RNN hidden state'''
        self.rnn_size = 20

        '''RNN sequence length'''
        self.seq_length = self.picture_size*4
        '''number of epochs'''
        self.num_epochs = 1 # was 5
        '''save frequency'''
        self.save_every = 100 # was 500
        '''Print frequency'''
        self.print_every = 10
        '''clip gradients at this value'''
        self.grad_clip = 5.
        '''learning rate'''
        self.learning_rate = 0.002 # was 0.002
        '''decay rate for rmsprop'''
        self.decay_rate = 0.98
        """continue training from saved model at this path.
        Path must contain files saved by previous training process: """
        self.init_from = 'save_face_training_0.4'
        #self.init_from = None

In [5]:
class FaceLoader:
    def prepare_reading_faces(self):
        self.matches = []
    
        for root, dirnames, filenames in os.walk('./turing/'):
        #for root, dirnames, filenames in os.walk('./lfw/'):
            #print filenames
            for filename in fnmatch.filter(filenames, '*.jpg'):
                self.matches.append(os.path.join(root, filename))

        size = len(self.matches)

        filenames = tf.constant(self.matches)
        self.filename_queue = tf.train.string_input_producer(filenames)
        self.image_reader = tf.WholeFileReader()
        return size
    
    def do_when_session(self):   
        # For some reason, we need a coordinator and some threads
        self.coord = tf.train.Coordinator()
        self.threads = tf.train.start_queue_runners(coord=self.coord)

    def stop_reading_faces(self):
        # Finish off the filename queue coordinator.
        self.coord.request_stop()
        self.coord.join(self.threads)
              
    def load_one_face(self, image_size):
        self.size = image_size
        # read and decode image, will give a uint8 with shape [250, 250, 1]
        filename, image_file = self.image_reader.read(self.filename_queue)     
        image = tf.image.decode_jpeg(image_file, channels=1)
        #resize
        image = tf.image.resize_images(image, image_size, image_size)

        # remove channel dimension
        tensor_uint8 = tf.squeeze(image, squeeze_dims=[2])

        # convert to float32 and scale
        face = tf.cast(tensor_uint8, tf.float32)/255.0
        face = tf.sign(face-0.5)/2+0.5
        self.picture = tf.constant(face.eval())
        im = Img.fromarray((face.eval()* 255).astype(np.uint8))
        im.save("training.jpeg")
                  
    def create_batch(self, batch_size, num_batches):
        xdata = tf.reshape(self.picture,[self.size*self.size]).eval()
        ydata = np.copy(xdata)
        ydata[:-1] = xdata[1:]
        ydata[-1] = xdata[0]
        #print 'aaaaaa', xdata
        self.x_batches = np.split(xdata.reshape(batch_size, -1), num_batches, 1)
        self.y_batches = np.split(ydata.reshape(batch_size, -1), num_batches, 1)
        self.pointer = 0
        #print 'aazera', self.x_batches
        
    def vectorize(self, x):
        vocab_size = 2 # witch is B or W
        vectorized = np.zeros([len(x), vocab_size])
        for i in range(0, len(x)):
            if x[i]<=0.5:
                vectorized[i][0] = 1
            else:
                vectorized[i][1] = 1
        return vectorized
        
    def next_batch(self):
        x, y = self.x_batches[self.pointer], self.y_batches[self.pointer]
        self.pointer += 1
        return self.vectorize(x), self.vectorize(y)

    def reset_batch_pointer(self):
        self.pointer = 0

Check!


In [7]:
tf.reset_default_graph()
args = Args()
seq_length = args.seq_length
seq_count = (args.picture_size*args.picture_size)/args.seq_length

faceloader = FaceLoader()
face_count = faceloader.prepare_reading_faces()
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    faceloader.do_when_session()
    faceloader.load_one_face(args.picture_size)
    faceloader.create_batch(seq_length,seq_count)
    x, y = faceloader.next_batch();
    print x, y
    x, y = faceloader.next_batch();
    print x, y
    x, y = faceloader.next_batch();
    print x, y


[[ 0.  1.]
 [ 0.  1.]
 [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 1.  0.]
 [ 1.  0.]
 [ 1.  0.]
 [ 1.  0.]]

In [8]:
Image("training.jpeg")


Out[8]:

In [18]:
class Model():
    def __init__(self, args, infer=False):
        self.args = args
        #if infer:
        #    '''Infer is true when the model is used for sampling'''
        #    args.seq_length = 1
   
        hidden_size = args.rnn_size
        vector_size = 2 # for B & W
        
        # define place holder to for the input data and the target.
        self.input_data = tf.placeholder(tf.float32, [ args.seq_length, vector_size], name='input_data')
        self.target_data = tf.placeholder(tf.float32, [ args.seq_length, vector_size], name='target_data') 
        # define the input xs
        xs = tf.split(0, args.seq_length, self.input_data)
        # define the target
        targets = tf.split(0, args.seq_length, self.target_data)  
        #initial_state
        self.initial_state = tf.zeros((hidden_size,1))
        
        # model parameters
        Wxh = tf.Variable(tf.random_uniform((hidden_size, vector_size))*0.01, name='Wxh') # input to hidden
        Whh = tf.Variable(tf.random_uniform((hidden_size, hidden_size))*0.01, name='Whh') # hidden to hidden
        Why = tf.Variable(tf.random_uniform((vector_size, hidden_size))*0.01, name='Why') # hidden to output
        bh = tf.Variable(tf.zeros((hidden_size, 1)), name='bh') # hidden bias
        by = tf.Variable(tf.zeros((vector_size, 1)), name='by') # output bias
        loss = tf.zeros([1], name='loss')
        hs, ys, ps = {}, {}, {}
        
        hs[-1] = self.initial_state
        # forward pass                                                                                                                                                                              
        for t in xrange(args.seq_length):
            xs_t = tf.transpose(xs[t])
            if infer and t>0: # to goes in generative mode
                xs_t = ps[t-1] # use the previous prediction instead of of the training data
           
            targets_t = tf.transpose(targets[t])
            
            hs[t] = tf.tanh(tf.matmul(Wxh, xs_t) + tf.matmul(Whh, hs[t-1]) + bh) # hidden state
            ys[t] = tf.matmul(Why, hs[t]) + by # unnormalized log probabilities for next pixel
            ps[t] = tf.exp(ys[t]) / tf.reduce_sum(tf.exp(ys[t])) # probabilities for next Pixel
            
            loss += -tf.log(tf.reduce_sum(tf.mul(ps[t], targets_t))) # softmax (cross-entropy loss)
            ps[t] = tf.tanh(1000.*ps[t]-500.)/2 + 0.5 
            
        self.probs = tf.pack([ps[key] for key in ps])
        self.cost = loss / args.seq_length
        self.final_state = hs[args.seq_length-1]
        self.lr = tf.Variable(0.0, trainable=False, name='learning_rate')
        tvars = tf.trainable_variables()
        grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars),
                args.grad_clip)
        optimizer = tf.train.AdamOptimizer(self.lr)
        self.train_op = optimizer.apply_gradients(zip(grads, tvars))
    
    def inspect(self, draw=False):
        for var in tf.all_variables():
            if var in tf.trainable_variables():
                print ('t', var.name, var.eval().shape)
                if draw:
                    plt.figure(figsize=(1,1))
                    plt.figimage(var.eval())
                    plt.show()
            else:
                print ('nt', var.name, var.eval().shape)

Training


In [50]:
tf.reset_default_graph()
args = Args()
model = Model(args)
print ("model created")
faceloader = FaceLoader()
face_count = faceloader.prepare_reading_faces()
print ('faces count', face_count)

cost_optimisation = []

seq_length = args.seq_length
seq_count = (args.picture_size*args.picture_size)/args.seq_length


with tf.Session() as sess:
    tf.initialize_all_variables().run()
    print ("variable initialized")
    faceloader.do_when_session()
    saver = tf.train.Saver(tf.all_variables())

    # restore model
    if args.init_from is not None:
        ckpt = tf.train.get_checkpoint_state(args.init_from)
        assert ckpt,"No checkpoint found"
        assert ckpt.model_checkpoint_path,"No model path found in checkpoint"
        saver.restore(sess, ckpt.model_checkpoint_path)
        print ("model restored")
    for e in range(args.num_epochs):
        faceloader.image_reader.reset()
        sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e)))
        #state = model.initial_state.eval()
        faceloader.load_one_face(args.picture_size)
        for b in range(10000):
            start = time.time()
            # Get learning data
            #faceloader.load_one_face(args.picture_size)
            faceloader.create_batch(seq_length, seq_count)
            state = model.initial_state.eval()
            for l in range(1):
                x, y = faceloader.next_batch()
                # Create the structure for the learning data
                feed = {model.input_data: x, model.target_data: y, model.initial_state: state}
                # Run a session using train_op
                [train_loss], state, _ = sess.run([model.cost, model.final_state, model.train_op], feed)
                end = time.time()
            if (e * face_count + b) % args.print_every == 0:
                cost_optimisation.append(train_loss)
                print("{}/{} (epoch {}), train_loss = {:.6f}, time/batch = {:.3f}" \
                    .format(e * face_count + b,
                            args.num_epochs * face_count,
                            e, train_loss, end - start))
            if (e * face_count + b) % args.save_every == 0:
                checkpoint_path = os.path.join(args.save_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step = e * face_count + b)
                print("model saved to {}".format(checkpoint_path))
                np.save('cost', cost_optimisation)


model created
('faces count', 16)
variable initialized
model restored
0/16 (epoch 0), train_loss = 0.000257, time/batch = 1.180
model saved to save_face_training_0.4/model.ckpt
10/16 (epoch 0), train_loss = 0.000255, time/batch = 0.324
20/16 (epoch 0), train_loss = 0.000253, time/batch = 0.322
30/16 (epoch 0), train_loss = 0.000250, time/batch = 0.319
40/16 (epoch 0), train_loss = 0.000248, time/batch = 0.306
50/16 (epoch 0), train_loss = 0.000246, time/batch = 0.316
60/16 (epoch 0), train_loss = 0.000243, time/batch = 0.314
70/16 (epoch 0), train_loss = 0.000241, time/batch = 0.329
80/16 (epoch 0), train_loss = 0.000239, time/batch = 0.309
90/16 (epoch 0), train_loss = 0.000237, time/batch = 0.307
100/16 (epoch 0), train_loss = 0.000235, time/batch = 0.313
model saved to save_face_training_0.4/model.ckpt
110/16 (epoch 0), train_loss = 0.000232, time/batch = 0.323
120/16 (epoch 0), train_loss = 0.000230, time/batch = 0.315
130/16 (epoch 0), train_loss = 0.000228, time/batch = 0.336
140/16 (epoch 0), train_loss = 0.000226, time/batch = 0.320
150/16 (epoch 0), train_loss = 0.000224, time/batch = 0.324
160/16 (epoch 0), train_loss = 0.000222, time/batch = 0.309
170/16 (epoch 0), train_loss = 0.000220, time/batch = 0.324
180/16 (epoch 0), train_loss = 0.000218, time/batch = 0.313
190/16 (epoch 0), train_loss = 0.000216, time/batch = 0.309
200/16 (epoch 0), train_loss = 0.000215, time/batch = 0.325
model saved to save_face_training_0.4/model.ckpt
210/16 (epoch 0), train_loss = 0.000213, time/batch = 0.320
220/16 (epoch 0), train_loss = 0.000211, time/batch = 0.341
230/16 (epoch 0), train_loss = 0.000209, time/batch = 0.324
240/16 (epoch 0), train_loss = 0.000207, time/batch = 0.310
250/16 (epoch 0), train_loss = 0.000206, time/batch = 0.377
260/16 (epoch 0), train_loss = 0.000204, time/batch = 0.319
270/16 (epoch 0), train_loss = 0.000202, time/batch = 0.315
280/16 (epoch 0), train_loss = 0.000200, time/batch = 0.316
290/16 (epoch 0), train_loss = 0.000199, time/batch = 0.359
300/16 (epoch 0), train_loss = 0.000197, time/batch = 0.341
model saved to save_face_training_0.4/model.ckpt
310/16 (epoch 0), train_loss = 0.000195, time/batch = 0.323
320/16 (epoch 0), train_loss = 0.000194, time/batch = 0.349
330/16 (epoch 0), train_loss = 0.000192, time/batch = 0.330
340/16 (epoch 0), train_loss = 0.000190, time/batch = 0.347
350/16 (epoch 0), train_loss = 0.000189, time/batch = 0.390
360/16 (epoch 0), train_loss = 0.000187, time/batch = 0.336
370/16 (epoch 0), train_loss = 0.000186, time/batch = 0.344
380/16 (epoch 0), train_loss = 0.000184, time/batch = 0.349
390/16 (epoch 0), train_loss = 0.000183, time/batch = 0.319
400/16 (epoch 0), train_loss = 0.000181, time/batch = 0.349
model saved to save_face_training_0.4/model.ckpt
410/16 (epoch 0), train_loss = 0.000180, time/batch = 0.396
420/16 (epoch 0), train_loss = 0.000178, time/batch = 0.404
430/16 (epoch 0), train_loss = 0.000177, time/batch = 0.314
440/16 (epoch 0), train_loss = 0.000175, time/batch = 0.319
450/16 (epoch 0), train_loss = 0.000174, time/batch = 0.321
460/16 (epoch 0), train_loss = 0.000173, time/batch = 0.314
470/16 (epoch 0), train_loss = 0.000171, time/batch = 0.321
480/16 (epoch 0), train_loss = 0.000170, time/batch = 0.344
490/16 (epoch 0), train_loss = 0.000169, time/batch = 0.335
500/16 (epoch 0), train_loss = 0.000167, time/batch = 0.341
model saved to save_face_training_0.4/model.ckpt
510/16 (epoch 0), train_loss = 0.000166, time/batch = 0.323
520/16 (epoch 0), train_loss = 0.000165, time/batch = 0.324
530/16 (epoch 0), train_loss = 0.000163, time/batch = 0.336
540/16 (epoch 0), train_loss = 0.000162, time/batch = 0.344
550/16 (epoch 0), train_loss = 0.000161, time/batch = 0.320
560/16 (epoch 0), train_loss = 0.000159, time/batch = 0.342
570/16 (epoch 0), train_loss = 0.000158, time/batch = 0.380
580/16 (epoch 0), train_loss = 0.000157, time/batch = 0.322
590/16 (epoch 0), train_loss = 0.000156, time/batch = 0.330
600/16 (epoch 0), train_loss = 0.000155, time/batch = 0.324
model saved to save_face_training_0.4/model.ckpt
610/16 (epoch 0), train_loss = 0.000153, time/batch = 0.320
620/16 (epoch 0), train_loss = 0.000152, time/batch = 0.338
630/16 (epoch 0), train_loss = 0.000151, time/batch = 0.327
640/16 (epoch 0), train_loss = 0.000150, time/batch = 0.319
650/16 (epoch 0), train_loss = 0.000149, time/batch = 0.324
660/16 (epoch 0), train_loss = 0.000148, time/batch = 0.327
670/16 (epoch 0), train_loss = 0.000146, time/batch = 0.326
680/16 (epoch 0), train_loss = 0.000145, time/batch = 0.315
690/16 (epoch 0), train_loss = 0.000144, time/batch = 0.344
700/16 (epoch 0), train_loss = 0.000143, time/batch = 0.325
model saved to save_face_training_0.4/model.ckpt
710/16 (epoch 0), train_loss = 0.000142, time/batch = 0.329
720/16 (epoch 0), train_loss = 0.000141, time/batch = 0.334
730/16 (epoch 0), train_loss = 0.000140, time/batch = 0.341
740/16 (epoch 0), train_loss = 0.000139, time/batch = 0.322
750/16 (epoch 0), train_loss = 0.000138, time/batch = 0.328
760/16 (epoch 0), train_loss = 0.000137, time/batch = 0.333
770/16 (epoch 0), train_loss = 0.000136, time/batch = 0.318
780/16 (epoch 0), train_loss = 0.000135, time/batch = 0.325
790/16 (epoch 0), train_loss = 0.000134, time/batch = 0.323
800/16 (epoch 0), train_loss = 0.000133, time/batch = 0.318
model saved to save_face_training_0.4/model.ckpt
810/16 (epoch 0), train_loss = 0.000132, time/batch = 0.322
820/16 (epoch 0), train_loss = 0.000131, time/batch = 0.322
830/16 (epoch 0), train_loss = 0.000130, time/batch = 0.321
840/16 (epoch 0), train_loss = 0.000129, time/batch = 0.340
850/16 (epoch 0), train_loss = 0.000128, time/batch = 0.322
860/16 (epoch 0), train_loss = 0.000127, time/batch = 0.322
870/16 (epoch 0), train_loss = 0.000126, time/batch = 0.343
880/16 (epoch 0), train_loss = 0.000125, time/batch = 0.322
890/16 (epoch 0), train_loss = 0.000124, time/batch = 0.323
900/16 (epoch 0), train_loss = 0.000123, time/batch = 0.329
model saved to save_face_training_0.4/model.ckpt
910/16 (epoch 0), train_loss = 0.000123, time/batch = 0.323
920/16 (epoch 0), train_loss = 0.000122, time/batch = 0.331
930/16 (epoch 0), train_loss = 0.000121, time/batch = 0.334
940/16 (epoch 0), train_loss = 0.000120, time/batch = 0.342
950/16 (epoch 0), train_loss = 0.000119, time/batch = 0.341
960/16 (epoch 0), train_loss = 0.000118, time/batch = 0.321
970/16 (epoch 0), train_loss = 0.000117, time/batch = 0.345
980/16 (epoch 0), train_loss = 0.000116, time/batch = 0.330
990/16 (epoch 0), train_loss = 0.000116, time/batch = 0.331
1000/16 (epoch 0), train_loss = 0.000115, time/batch = 0.338
model saved to save_face_training_0.4/model.ckpt
1010/16 (epoch 0), train_loss = 0.000114, time/batch = 0.345
1020/16 (epoch 0), train_loss = 0.000113, time/batch = 0.332
1030/16 (epoch 0), train_loss = 0.000112, time/batch = 0.333
1040/16 (epoch 0), train_loss = 0.000112, time/batch = 0.349
1050/16 (epoch 0), train_loss = 0.000111, time/batch = 0.350
1060/16 (epoch 0), train_loss = 0.000110, time/batch = 0.322
1070/16 (epoch 0), train_loss = 0.000109, time/batch = 0.337
1080/16 (epoch 0), train_loss = 0.000108, time/batch = 0.329
1090/16 (epoch 0), train_loss = 0.000108, time/batch = 0.345
1100/16 (epoch 0), train_loss = 0.000107, time/batch = 0.326
model saved to save_face_training_0.4/model.ckpt
1110/16 (epoch 0), train_loss = 0.000106, time/batch = 0.331
1120/16 (epoch 0), train_loss = 0.000105, time/batch = 0.336
1130/16 (epoch 0), train_loss = 0.000105, time/batch = 0.338
1140/16 (epoch 0), train_loss = 0.000104, time/batch = 0.350
1150/16 (epoch 0), train_loss = 0.000103, time/batch = 0.324
1160/16 (epoch 0), train_loss = 0.000103, time/batch = 0.325
1170/16 (epoch 0), train_loss = 0.000102, time/batch = 0.334
1180/16 (epoch 0), train_loss = 0.000101, time/batch = 0.328
1190/16 (epoch 0), train_loss = 0.000100, time/batch = 0.348
1200/16 (epoch 0), train_loss = 0.000100, time/batch = 0.351
model saved to save_face_training_0.4/model.ckpt
1210/16 (epoch 0), train_loss = 0.000099, time/batch = 0.338
1220/16 (epoch 0), train_loss = 0.000098, time/batch = 0.339
1230/16 (epoch 0), train_loss = 0.000098, time/batch = 0.357
1240/16 (epoch 0), train_loss = 0.000097, time/batch = 0.340
1250/16 (epoch 0), train_loss = 0.000096, time/batch = 0.338
1260/16 (epoch 0), train_loss = 0.000096, time/batch = 0.335
1270/16 (epoch 0), train_loss = 0.000095, time/batch = 0.336
1280/16 (epoch 0), train_loss = 0.000094, time/batch = 0.340
1290/16 (epoch 0), train_loss = 0.000094, time/batch = 0.332
1300/16 (epoch 0), train_loss = 0.000093, time/batch = 0.329
model saved to save_face_training_0.4/model.ckpt
1310/16 (epoch 0), train_loss = 0.000092, time/batch = 0.330
1320/16 (epoch 0), train_loss = 0.000092, time/batch = 0.337
1330/16 (epoch 0), train_loss = 0.000091, time/batch = 0.350
1340/16 (epoch 0), train_loss = 0.000091, time/batch = 0.367
1350/16 (epoch 0), train_loss = 0.000090, time/batch = 0.381
1360/16 (epoch 0), train_loss = 0.000089, time/batch = 0.358
1370/16 (epoch 0), train_loss = 0.000089, time/batch = 0.356
1380/16 (epoch 0), train_loss = 0.000088, time/batch = 0.358
1390/16 (epoch 0), train_loss = 0.000088, time/batch = 0.362
1400/16 (epoch 0), train_loss = 0.000087, time/batch = 0.362
model saved to save_face_training_0.4/model.ckpt
1410/16 (epoch 0), train_loss = 0.000086, time/batch = 0.370
1420/16 (epoch 0), train_loss = 0.000086, time/batch = 0.363
1430/16 (epoch 0), train_loss = 0.000085, time/batch = 0.351
1440/16 (epoch 0), train_loss = 0.000085, time/batch = 0.362
1450/16 (epoch 0), train_loss = 0.000084, time/batch = 0.376
1460/16 (epoch 0), train_loss = 0.000084, time/batch = 0.367
1470/16 (epoch 0), train_loss = 0.000083, time/batch = 0.375
1480/16 (epoch 0), train_loss = 0.000082, time/batch = 0.362
1490/16 (epoch 0), train_loss = 0.000082, time/batch = 0.390
1500/16 (epoch 0), train_loss = 0.000081, time/batch = 0.360
model saved to save_face_training_0.4/model.ckpt
1510/16 (epoch 0), train_loss = 0.000081, time/batch = 0.376
1520/16 (epoch 0), train_loss = 0.000080, time/batch = 0.379
1530/16 (epoch 0), train_loss = 0.000080, time/batch = 0.374
1540/16 (epoch 0), train_loss = 0.000079, time/batch = 0.373
1550/16 (epoch 0), train_loss = 0.000079, time/batch = 0.367
1560/16 (epoch 0), train_loss = 0.000078, time/batch = 0.355
1570/16 (epoch 0), train_loss = 0.000078, time/batch = 0.422
1580/16 (epoch 0), train_loss = 0.000077, time/batch = 0.356
1590/16 (epoch 0), train_loss = 0.000077, time/batch = 0.367
1600/16 (epoch 0), train_loss = 0.000076, time/batch = 0.331
model saved to save_face_training_0.4/model.ckpt
1610/16 (epoch 0), train_loss = 0.000076, time/batch = 0.339
1620/16 (epoch 0), train_loss = 0.000075, time/batch = 0.374
1630/16 (epoch 0), train_loss = 0.000075, time/batch = 0.343
1640/16 (epoch 0), train_loss = 0.000074, time/batch = 0.351
1650/16 (epoch 0), train_loss = 0.000074, time/batch = 0.361
1660/16 (epoch 0), train_loss = 0.000073, time/batch = 0.344
1670/16 (epoch 0), train_loss = 0.000073, time/batch = 0.342
1680/16 (epoch 0), train_loss = 0.000072, time/batch = 0.340
1690/16 (epoch 0), train_loss = 0.000072, time/batch = 0.353
1700/16 (epoch 0), train_loss = 0.000071, time/batch = 0.358
model saved to save_face_training_0.4/model.ckpt
1710/16 (epoch 0), train_loss = 0.000071, time/batch = 0.336
1720/16 (epoch 0), train_loss = 0.000070, time/batch = 0.347
1730/16 (epoch 0), train_loss = 0.000070, time/batch = 0.345
1740/16 (epoch 0), train_loss = 0.000069, time/batch = 0.339
1750/16 (epoch 0), train_loss = 0.000069, time/batch = 0.346
1760/16 (epoch 0), train_loss = 0.000068, time/batch = 0.341
1770/16 (epoch 0), train_loss = 0.000068, time/batch = 0.356
1780/16 (epoch 0), train_loss = 0.000068, time/batch = 0.361
1790/16 (epoch 0), train_loss = 0.000067, time/batch = 0.361
1800/16 (epoch 0), train_loss = 0.000067, time/batch = 0.362
model saved to save_face_training_0.4/model.ckpt
1810/16 (epoch 0), train_loss = 0.000066, time/batch = 0.337
1820/16 (epoch 0), train_loss = 0.000066, time/batch = 0.339
1830/16 (epoch 0), train_loss = 0.000065, time/batch = 0.348
1840/16 (epoch 0), train_loss = 0.000065, time/batch = 0.361
1850/16 (epoch 0), train_loss = 0.000065, time/batch = 0.342
1860/16 (epoch 0), train_loss = 0.000064, time/batch = 0.360
1870/16 (epoch 0), train_loss = 0.000064, time/batch = 0.362
1880/16 (epoch 0), train_loss = 0.000063, time/batch = 0.355
1890/16 (epoch 0), train_loss = 0.000063, time/batch = 0.345
1900/16 (epoch 0), train_loss = 0.000063, time/batch = 0.340
model saved to save_face_training_0.4/model.ckpt
1910/16 (epoch 0), train_loss = 0.000062, time/batch = 0.337
1920/16 (epoch 0), train_loss = 0.000062, time/batch = 0.361
1930/16 (epoch 0), train_loss = 0.000061, time/batch = 0.355
1940/16 (epoch 0), train_loss = 0.000061, time/batch = 0.340
1950/16 (epoch 0), train_loss = 0.000061, time/batch = 0.336
1960/16 (epoch 0), train_loss = 0.000060, time/batch = 0.338
1970/16 (epoch 0), train_loss = 0.000060, time/batch = 0.358
1980/16 (epoch 0), train_loss = 0.000059, time/batch = 0.345
1990/16 (epoch 0), train_loss = 0.000059, time/batch = 0.355
2000/16 (epoch 0), train_loss = 0.000059, time/batch = 0.365
model saved to save_face_training_0.4/model.ckpt
2010/16 (epoch 0), train_loss = 0.000058, time/batch = 0.352
2020/16 (epoch 0), train_loss = 0.000058, time/batch = 0.352
2030/16 (epoch 0), train_loss = 0.000058, time/batch = 0.365
2040/16 (epoch 0), train_loss = 0.000057, time/batch = 0.345
2050/16 (epoch 0), train_loss = 0.000057, time/batch = 0.360
2060/16 (epoch 0), train_loss = 0.000056, time/batch = 0.345
2070/16 (epoch 0), train_loss = 0.000056, time/batch = 0.356
2080/16 (epoch 0), train_loss = 0.000056, time/batch = 0.353
2090/16 (epoch 0), train_loss = 0.000055, time/batch = 0.359
2100/16 (epoch 0), train_loss = 0.000055, time/batch = 0.362
model saved to save_face_training_0.4/model.ckpt
2110/16 (epoch 0), train_loss = 0.000055, time/batch = 0.353
2120/16 (epoch 0), train_loss = 0.000054, time/batch = 0.359
2130/16 (epoch 0), train_loss = 0.000054, time/batch = 0.352
2140/16 (epoch 0), train_loss = 0.000054, time/batch = 0.355
2150/16 (epoch 0), train_loss = 0.000053, time/batch = 0.359
2160/16 (epoch 0), train_loss = 0.000053, time/batch = 0.357
2170/16 (epoch 0), train_loss = 0.000053, time/batch = 0.354
2180/16 (epoch 0), train_loss = 0.000052, time/batch = 0.354
2190/16 (epoch 0), train_loss = 0.000052, time/batch = 0.348
2200/16 (epoch 0), train_loss = 0.000052, time/batch = 0.352
model saved to save_face_training_0.4/model.ckpt
2210/16 (epoch 0), train_loss = 0.000051, time/batch = 0.343
2220/16 (epoch 0), train_loss = 0.000051, time/batch = 0.358
2230/16 (epoch 0), train_loss = 0.000051, time/batch = 0.340
2240/16 (epoch 0), train_loss = 0.000050, time/batch = 0.351
2250/16 (epoch 0), train_loss = 0.000050, time/batch = 0.350
2260/16 (epoch 0), train_loss = 0.000050, time/batch = 0.348
2270/16 (epoch 0), train_loss = 0.000049, time/batch = 0.374
2280/16 (epoch 0), train_loss = 0.000049, time/batch = 0.360
2290/16 (epoch 0), train_loss = 0.000049, time/batch = 0.360
2300/16 (epoch 0), train_loss = 0.000049, time/batch = 0.342
model saved to save_face_training_0.4/model.ckpt
2310/16 (epoch 0), train_loss = 0.000048, time/batch = 0.356
2320/16 (epoch 0), train_loss = 0.000048, time/batch = 0.353
2330/16 (epoch 0), train_loss = 0.000048, time/batch = 0.364
2340/16 (epoch 0), train_loss = 0.000047, time/batch = 0.357
2350/16 (epoch 0), train_loss = 0.000047, time/batch = 0.374
2360/16 (epoch 0), train_loss = 0.000047, time/batch = 0.375
2370/16 (epoch 0), train_loss = 0.000046, time/batch = 0.368
2380/16 (epoch 0), train_loss = 0.000046, time/batch = 0.351
2390/16 (epoch 0), train_loss = 0.000046, time/batch = 0.371
2400/16 (epoch 0), train_loss = 0.000046, time/batch = 0.376
model saved to save_face_training_0.4/model.ckpt
2410/16 (epoch 0), train_loss = 0.000045, time/batch = 0.351
2420/16 (epoch 0), train_loss = 0.000045, time/batch = 0.357
2430/16 (epoch 0), train_loss = 0.000045, time/batch = 0.346
2440/16 (epoch 0), train_loss = 0.000045, time/batch = 0.351
2450/16 (epoch 0), train_loss = 0.000044, time/batch = 0.354
2460/16 (epoch 0), train_loss = 0.000044, time/batch = 0.353
2470/16 (epoch 0), train_loss = 0.000044, time/batch = 0.361
2480/16 (epoch 0), train_loss = 0.000043, time/batch = 0.354
2490/16 (epoch 0), train_loss = 0.000043, time/batch = 0.347
2500/16 (epoch 0), train_loss = 0.000043, time/batch = 0.365
model saved to save_face_training_0.4/model.ckpt
2510/16 (epoch 0), train_loss = 0.000043, time/batch = 0.364
2520/16 (epoch 0), train_loss = 0.000042, time/batch = 0.347
2530/16 (epoch 0), train_loss = 0.000042, time/batch = 0.355
2540/16 (epoch 0), train_loss = 0.000042, time/batch = 0.361
2550/16 (epoch 0), train_loss = 0.000042, time/batch = 0.380
2560/16 (epoch 0), train_loss = 0.000041, time/batch = 0.361
2570/16 (epoch 0), train_loss = 0.000041, time/batch = 0.370
2580/16 (epoch 0), train_loss = 0.000041, time/batch = 0.403
2590/16 (epoch 0), train_loss = 0.000041, time/batch = 0.476
2600/16 (epoch 0), train_loss = 0.000040, time/batch = 0.378
model saved to save_face_training_0.4/model.ckpt
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-50-388fc46a3728> in <module>()
     42                 feed = {model.input_data: x, model.target_data: y, model.initial_state: state}
     43                 # Run a session using train_op
---> 44                 [train_loss], state, _ = sess.run([model.cost, model.final_state, model.train_op], feed)
     45                 end = time.time()
     46             if (e * face_count + b) % args.print_every == 0:

/Users/damienhenry/code/jupyter/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    338     try:
    339       result = self._run(None, fetches, feed_dict, options_ptr,
--> 340                          run_metadata_ptr)
    341       if run_metadata:
    342         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/Users/damienhenry/code/jupyter/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    562     try:
    563       results = self._do_run(handle, target_list, unique_fetches,
--> 564                              feed_dict_string, options, run_metadata)
    565     finally:
    566       # The movers are no longer used. Delete them.

/Users/damienhenry/code/jupyter/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
    635     if handle is None:
    636       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 637                            target_list, options, run_metadata)
    638     else:
    639       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/Users/damienhenry/code/jupyter/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
    642   def _do_call(self, fn, *args):
    643     try:
--> 644       return fn(*args)
    645     except tf_session.StatusNotOK as e:
    646       error_message = compat.as_text(e.error_message)

/Users/damienhenry/code/jupyter/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
    626       else:
    627         return tf_session.TF_Run(
--> 628             session, None, feed_dict, fetch_list, target_list, None)
    629 
    630     def _prun_fn(session, handle, feed_dict, fetch_list):

KeyboardInterrupt: 

In [51]:
cost_optimisation = np.load('cost.npy')
plt.figure(figsize=(12,5))
plt.plot(range(len(cost_optimisation)), cost_optimisation, label='cost')
plt.legend()
plt.show()



In [ ]:
tf.reset_default_graph()
args = Args()
model = Model(args, True)  # True to generate the model in sampling mode
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    saver = tf.train.Saver(tf.all_variables())
    ckpt = tf.train.get_checkpoint_state(args.save_dir)
    print (ckpt)
    
    model.inspect(draw=True)

sampling


In [105]:
tf.reset_default_graph()
args = Args()
model = Model(args, infer=True)
seq_length = args.seq_length
seq_count = (args.picture_size*args.picture_size)/args.seq_length
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    print 'intialisation done'
    saver = tf.train.Saver(tf.all_variables())
    ckpt = tf.train.get_checkpoint_state(args.save_dir)
    print (ckpt)
    
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
               
    state = model.initial_state.eval()
    x = np.random.random([seq_length, 2])
    
    pict = np.zeros([seq_count, seq_length])
    for i in range(seq_count):
        feed = {model.input_data: x, model.initial_state: state}
        [lines] = sess.run([model.probs], feed)
        x = np.squeeze(lines)
        #print x
        for j in range(seq_length):
            if x[j][0] > x[j][1]:
                pict[i][j] = 0
            else:
                pict[i][j] = 1
    picture = np.reshape(pict,[args.picture_size*args.picture_size])
    picture = np.reshape(pict,[args.picture_size,args.picture_size])
    plt.figure(figsize=(1,1))
    plt.figimage(picture)
    plt.show()


intialisation done
model_checkpoint_path: "save_face_training_0.4/model.ckpt-2600"
all_model_checkpoint_paths: "save_face_training_0.4/model.ckpt-2200"
all_model_checkpoint_paths: "save_face_training_0.4/model.ckpt-2300"
all_model_checkpoint_paths: "save_face_training_0.4/model.ckpt-2400"
all_model_checkpoint_paths: "save_face_training_0.4/model.ckpt-2500"
all_model_checkpoint_paths: "save_face_training_0.4/model.ckpt-2600"


In [106]:
print picture


[[ 1.  1.  1. ...,  1.  1.  1.]
 [ 1.  1.  1. ...,  1.  1.  1.]
 [ 1.  1.  1. ...,  1.  1.  1.]
 ..., 
 [ 0.  1.  1. ...,  1.  1.  1.]
 [ 0.  1.  1. ...,  1.  1.  1.]
 [ 0.  1.  1. ...,  1.  1.  1.]]

In [107]:
from PIL import Image as Img
im = Img.fromarray((picture * 255).astype(np.uint8))
im.save("result.jpeg")

In [108]:
Image("training.jpeg")


Out[108]:

In [109]:
Image("result.jpeg")


Out[109]:

In [110]:
x = np.linspace(1, 100, num=100)
x = np.reshape(x, [10,10])
#print x

pict = np.zeros([32/4, 32*4])
for i in range(32/4):
    line = np.linspace(1, 32*4, num=32*4)
    
    for j in range(32*4):
        pict[i][j]=line[j]+i*32*4
        pict[i][j]=i*32*4

picture = np.reshape(pict,[32*32])
picture = np.reshape(pict,[32,32])
print picture
plt.figure(figsize=(1,1))
plt.figimage(picture)
plt.show()


[[   0.    0.    0. ...,    0.    0.    0.]
 [   0.    0.    0. ...,    0.    0.    0.]
 [   0.    0.    0. ...,    0.    0.    0.]
 ..., 
 [ 896.  896.  896. ...,  896.  896.  896.]
 [ 896.  896.  896. ...,  896.  896.  896.]
 [ 896.  896.  896. ...,  896.  896.  896.]]

Feedback wellcome @dh7net