dimanche 17 décembre 2017

1000 pixel random sampling using Neural Network with Python and Tensorflow

I wanna classify images into 2 classes with Neural Network.

I wanna randomly extract 1,000 pixels out of 1024 × 768 pixels, and I tried it by various means such as random masking (tf.boolean mask) and extraction using an array, but it does not work very well.

If you know how to do 1000 pixel random sampling, please tell me the solution. Any means such as random mask or array extraction is not required.

Here is the code we are about to practice. Thank you.

import sys
sys.path.append('/usr/local/opt/opencv3/lib/python3.5.4/site-packages')
import cv2
import numpy as np
import tensorflow as tf
import tensorflow.python.platform
import tensorboard as tb   
import os
import math
import time
import random
start_time = time.time()



# TensorBoard information directory
log_dir = '/tmp/data1'  #tensorboard --logdir=/tmp/data1

#directory delete and reconstruction
if tf.gfile.Exists(log_dir):
    tf.gfile.DeleteRecursively(log_dir)
tf.gfile.MakeDirs(log_dir)



# Reserve memory
config = tf.ConfigProto(
   gpu_options=tf.GPUOptions(
       allow_growth=True
   )
)
sess = sess = tf.Session(config=config)




NUM_CLASSES = 2
IMAGE_SIZE_x = 1024
IMAGE_SIZE_y = 768
IMAGE_CHANNELS = 1
IMAGE_PIXELS = IMAGE_SIZE_x*IMAGE_SIZE_y*IMAGE_CHANNELS
SAMPLE_PIXELS = 1000

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('train', 'train.txt', 'File name of train data')
flags.DEFINE_string('test', 'test.txt', 'File name of train data')
flags.DEFINE_string('image_dir', 'trdata', 'Directory of images')
flags.DEFINE_string('train_dir', '/tmp/data', 'Directory to put the training data.')
flags.DEFINE_integer('max_steps', 20000, 'Number of steps to run trainer.')
flags.DEFINE_integer('batch_size', 10, 'Batch size'
                     'Must divide evenly into the dataset sizes.')
flags.DEFINE_float('learning_rate', 1e-5, 'Initial learning rate.')


# 

def inference(images_placeholder, keep_prob):
    """ Function to create predictive model

   argument:
        images_placeholder: image placeholder
        keep_prob: dropout rate placeholder

    Return:
        y_conv: 
    """
    # Initialie with normal distribution with weight of 0.1
    def weight_variable(shape):
      initial = tf.truncated_normal(shape, stddev=0.1)
      return tf.Variable(initial)

    # Initialized with normal distribution with bias of 0.1
    def bias_variable(shape):
      initial = tf.constant(0.1, shape=shape)
      return tf.Variable(initial)



    # Reshape input
    x_image = images_placeholder

    # Input
    with tf.name_scope('fc1') as scope:
        W_fc1 = weight_variable(SAMPLE_PIXELS)
        b_fc1 = bias_variable([10])
        h_fc1 = tf.nn.relu(tf.matmul(x_image,W_fc1) + b_fc1)

    # Affine1
    with tf.name_scope('fc2') as scope:
        W_fc2 = weight_variable([10,10])
        b_fc2 = bias_variable([10])
        h_fc2 = tf.nn.relu(tf.matmul(h_fc1,W_fc2) + b_fc2)

    # Affine2
    with tf.name_scope('fc3') as scope:
        W_fc3 = weight_variable([10,10])
        b_fc3 = bias_variable([10])
        h_fc3 = tf.nn.relu(tf.matmul(h_fc2,W_fc3) + b_fc3)

    # Affine3
    with tf.name_scope('fc4') as scope:
        W_fc4 = weight_variable([10,10])
        b_fc4 = bias_variable([10])
        h_fc4 = tf.nn.relu(tf.matmul(h_fc3,W_fc4) + b_fc4)
    # Affine4
    with tf.name_scope('fc5') as scope:
        W_fc5 = weight_variable([10,2])
        b_fc5 = bias_variable([2])

    # softmax regression
    with tf.name_scope('softmax') as scope:
        y_out=tf.nn.softmax(tf.matmul(h_fc4, W_fc5) + b_fc5)

    # return
    return y_out

def loss(logits, labels):
    """ loss function

    引数:
        logits: logit tensor, float - [batch_size, NUM_CLASSES]
        labels: labrl tensor, int32 - [batch_size, NUM_CLASSES]

    返り値:
        cross_entropy:tensor, float

    """
    # cross entropy
    cross_entropy = -tf.reduce_sum(labels*tf.log(tf.clip_by_value(logits,1e-10,1.0)))
    # TensorBoard
    tf.summary.scalar("cross_entropy", cross_entropy)
    return cross_entropy

def training(loss, learning_rate):

    #Adam
    train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
    return train_step

def accuracy(logits, labels):

    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1)) #original
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))#original
    tf.summary.scalar("accuracy", accuracy)#original
    return accuracy#original


if __name__ == '__main__':
    f = open(FLAGS.train, 'r')
    # array data
    train_image = []
    train_label = []
    for line in f:
        # Separate space and remove newlines
        line = line.rstrip()
        l = line.split()
        # Load data
        img = cv2.imread(FLAGS.image_dir + '/' + l[0])
        #transrate grayscale
        img_gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # transrate one row and 0-1 float
        train_image.append(img_gry.flatten().astype(np.float32)/255.0)
        # Prepare with label 1-of-k method
        tmp = np.zeros(NUM_CLASSES)
        tmp[int(l[1])] = 1
        train_label.append(tmp)
    # transrate numpy
    train_image = np.asarray(train_image)
    train_label = np.asarray(train_label)
    f.close()

    f = open(FLAGS.test, 'r')
    test_image = []
    test_label = []
    for line in f:
        line = line.rstrip()
        l = line.split()
        img = cv2.imread(FLAGS.image_dir + '/' + l[0])
        #transrate grayscale
        img_gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #  transrate one row and 0-1 float
        test_image.append(img_gry.flatten().astype(np.float32)/255.0)
        tmp = np.zeros(NUM_CLASSES)
        tmp[int(l[1])] = 1
        test_label.append(tmp)
    test_image = np.asarray(test_image)
    test_label = np.asarray(test_label)
    f.close()

    with tf.Graph().as_default():
        # Put the image Tensor
        images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
        # Put the label Tensor
        labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
        # Put dropout rate Tensor
        keep_prob = tf.placeholder("float")
        # Load inference() and make model
        logits = inference(images_placeholder, keep_prob)
        # Load loss() and calculate loss
        loss_value = loss(logits, labels_placeholder)
        # Load training() and train
        train_op = training(loss_value, FLAGS.learning_rate)
        # calculate accuracy
        acc = accuracy(logits, labels_placeholder)
        # save
        saver = tf.train.Saver()
        # Make Session
        sess = tf.Session()
        # Initialize variable
        sess.run(tf.global_variables_initializer())
        # TensorBoard
        summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)




# Start training
for step in range(FLAGS.max_steps):
    for i in range(int(len(train_image)/FLAGS.batch_size)):
        batch = FLAGS.batch_size*i
        sess.run(train_op, feed_dict={
          images_placeholder: train_image[batch:batch+FLAGS.batch_size],
          labels_placeholder: train_label[batch:batch+FLAGS.batch_size],
          keep_prob: 0.5})

    # Accuracy calculation for every steps
    train_accuracy = sess.run(acc, feed_dict={
        images_placeholder: train_image,
        labels_placeholder: train_label,
        keep_prob: 1.0})
    print("step %d, training accuracy %g" %(step, train_accuracy))


    # Added value to be displayed in Tensorflow every 1step
    summary_str = sess.run(summary_op, feed_dict={
        images_placeholder: train_image,
        labels_placeholder: train_label,
        keep_prob: 1.0})
    summary_writer.add_summary(summary_str, step)

    # Display accuracy on test data after training
    print("        test accuracy     %g"%sess.run(acc, feed_dict={
        images_placeholder: test_image,
        labels_placeholder: test_label,
        keep_prob: 1.0}))

    duration = time.time() - start_time

    print('%.3f sec' %duration)

    # Save model
    save_path = saver.save(sess, os.getcwd() + "\\model.ckpt")




Aucun commentaire:

Enregistrer un commentaire