فهرست منبع

inceptionv3 nn structure, img_proccess, tfrecord read & write, multiThread, coordinator and queueRunner

youchen 5 سال پیش
والد
کامیت
acbb23da1d

+ 3 - 0
.gitignore

@@ -1,3 +1,6 @@
+inceptionv3/model/*
+inceptionv3/preprocess/*
+
 # Byte-compiled / optimized / DLL files
 __pycache__/
 *.py[cod]

BIN
img_proc/backGround.jpg


BIN
img_proc/backGround2.jpg


+ 72 - 0
img_proc/multiThread.py

@@ -0,0 +1,72 @@
+import tensorflow as tf
+import numpy as np
+import threading
+import time
+
+
+# ********** queue operation ***********
+def queue_op():
+    # FIFOQueue & RandomShuffleQueue
+    # maximum 2 int elements
+    q = tf.FIFOQueue(2, "int32")
+
+    init = q.enqueue_many(([0, 10],))
+
+    x = q.dequeue()
+    y = x + 1
+    q_inc = q.enqueue([y])
+
+    with tf.Session() as sess:
+        init.run()
+        for _ in range(5):
+            # including dequeue, add 1, enqueue
+            v, _ = sess.run([x, q_inc])
+            # print(v)
+
+
+# tf.train.Coordinator enable thread synchronization
+# request_stop, should_stop, join
+def MyLoop(coord, worker_id):
+    while not coord.should_stop():
+        if np.random.rand() < 0.1:
+            print("Stoping from id: %d" % worker_id)
+            coord.request_stop()
+        else:
+            time.sleep(0.5)
+            print("Working on id: %d" % worker_id)
+        time.sleep(1)
+
+
+# coord = tf.train.Coordinator()
+# threads = [
+#     threading.Thread(target=MyLoop, args=(coord, i), ) for i in range(5)
+# ]
+# # start all threads
+# for t in threads:
+#     t.start()
+# # wait for all threads to stop
+# coord.join(threads)
+
+# ******** tf.QueueRunner **********
+def threads_mgmt():
+    queue = tf.FIFOQueue(100, 'float')
+    enqueue_op = queue.enqueue([tf.random_normal([1])])
+    # create 5 threads
+    qr = tf.train.QueueRunner(queue, [enqueue_op] * 5)
+    # added to default collection tf.GraphKeys.QUEUE_RUNNERS,
+    # start_queue_runner() will start all threads in the specified collection
+    tf.train.add_queue_runner(qr)
+    out_tensor = queue.dequeue()
+
+    with tf.Session() as sess:
+        coord = tf.train.Coordinator()
+        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
+        for _ in range(15):
+            print(sess.run(out_tensor)[0])
+            time.sleep(0.2)
+    coord.request_stop()
+    coord.join(threads)
+
+
+if __name__ == '__main__':
+    threads_mgmt()

BIN
img_proc/output_mnist.tfrecords


+ 146 - 0
img_proc/preprocessing.py

@@ -0,0 +1,146 @@
+# -*- utf-8 -*-
+
+import matplotlib.pyplot as plt
+import tensorflow as tf
+import numpy as np
+
+
+def img_endecoding():
+    image_raw_data = tf.gfile.FastGFile("backGround.jpg", 'rb').read()
+
+    img_data = tf.image.decode_jpeg(image_raw_data)
+    print(type(img_data.eval()))
+    print(img_data.eval().ndim)
+    print(img_data.eval().dtype)
+    print(img_data.eval().size)
+    print(img_data.eval().shape)
+    # plt.imshow(img_data.eval())
+    # plt.show()
+
+    encoded_image = tf.image.encode_jpeg(img_data)
+    with tf.gfile.GFile("backGround2.jpg", 'wb') as f:
+        f.write(encoded_image.eval())
+    return img_data
+
+
+def img_proc(img_data):
+    # sugggest to convert img to real number domain 0.0-1.0, so as not to lose too much accuracy
+    img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32)
+
+    # change size
+    resized = tf.image.resize_images(img_data, [300, 300], method=0)
+    croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 500)
+    padded = tf.image.resize_image_with_crop_or_pad(img_data, 1000, 1000)
+    central_cropped = tf.image.central_crop(img_data, 0.5)
+
+    # flip
+    flipped_ud = tf.image.flip_up_down(img_data)
+    flipped_lr = tf.image.flip_left_right(img_data)
+    transpose = tf.image.transpose_image(img_data)
+    flipped_rndup = tf.image.random_flip_up_down(img_data)
+    flipped_rndlr = tf.image.random_flip_left_right(img_data)
+
+    # brightness
+    bright_adjusted = tf.image.adjust_brightness(img_data, -0.5)
+    bright_adjusted = tf.image.random_brightness(img_data, 0.5)  # -0.5 - 0.5
+    bright_adjusted_clip = tf.clip_by_value(bright_adjusted, 0.0, 1.0)
+
+    # contrast
+    contrast_adjusted = tf.image.adjust_contrast(img_data, 0.5)
+    contrast_adjusted = tf.image.random_contrast(img_data, 0.5, 5)
+
+    # hue 色相
+    hue_adjusted = tf.image.adjust_hue(img_data, 0.1)
+    hue_adjusted = tf.image.random_hue(img_data, 0.5)  # 0-0.5
+
+    # saturation 饱和度
+    saturation_adjusted = tf.image.adjust_saturation(img_data, -5)
+    saturation_adjusted = tf.image.random_saturation(img_data, 0, 5)
+
+    # standardization, N~(0,1)
+    adjusted = tf.image.per_image_standardization(img_data)
+
+    # labelling, 输入四维矩阵
+    batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, dtype=tf.float32), 0)
+    print(batched.eval().ndim)
+    # Ymin, Xmin, Ymax, Xmax
+    boxes = tf.constant([[[0.1, 0.5, 0.85, 0.8], [0.35, 0.47, 0.5, 0.56]]])
+    result = tf.image.draw_bounding_boxes(batched, boxes)
+
+    # clip by boxes, 0.4 means at least contain 40% area
+    begin, size, box = tf.image.sample_distorted_bounding_box(
+        tf.shape(img_data), bounding_boxes=boxes,
+        min_object_covered=0.4
+    )
+    image_with_box = tf.image.draw_bounding_boxes(batched, box)
+    distorted_img = tf.slice(img_data, begin, size)
+
+    plt.imshow(result[0].eval())
+    plt.show()
+
+    img_data = tf.image.convert_image_dtype(resized, dtype=tf.uint8)
+    return img_data
+
+
+def distort_color(image, color_ordering=0):
+    if color_ordering == 0:
+        image = tf.image.random_brightness(image, max_delta=32. / 255.)
+        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
+        image = tf.image.random_hue(image, max_delta=0.2)
+        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
+    elif color_ordering == 1:
+        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
+        image = tf.image.random_brightness(image, max_delta=32. / 255.)
+        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
+        image = tf.image.random_hue(image, max_delta=0.2)
+    elif color_ordering == 2:
+        image = tf.image.random_contrast(image, lower=0.5, upper=1.5)
+        image = tf.image.random_hue(image, max_delta=0.2)
+        image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
+        image = tf.image.random_brightness(image, max_delta=32. / 255.)
+    return tf.clip_by_value(image, 0.0, 1.0)
+
+
+def process_for_train(image, height, width, bbox):
+    if bbox is None:
+        bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4])
+
+    if image.dtype != tf.float32:
+        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
+
+    bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box(
+        tf.shape(image), bounding_boxes=bbox
+    )
+    distorted_img = tf.slice(image, bbox_begin, bbox_size)
+    # resize input image for train, all kinds of interpolation
+    distorted_img = tf.image.resize_images(
+        distorted_img, [height, width], method=np.random.randint(4)
+    )
+    # filp img
+    distorted_img = tf.image.random_flip_left_right(distorted_img)
+    distorted_img = tf.image.random_flip_up_down(distorted_img)
+    distorted_img = distort_color(distorted_img, np.random.randint(3))
+    return distorted_img
+
+
+def main():
+    # with tf.device('/cpu:0'):
+    with tf.Session() as sess:
+        img_data = img_endecoding()
+        boxes = tf.constant([[[0.1, 0.5, 0.85, 0.8]]])
+        # img_data = img_proc(img_data)
+
+        for i in range(6):
+            plt.figure(i)
+            result = process_for_train(img_data, 500,300,boxes)
+            plt.imshow(result.eval())
+
+        plt.show()
+
+        # print(img_data)
+        # plt.imshow(trans.eval())
+        # plt.show()
+
+
+if __name__ == '__main__':
+    main()

+ 82 - 0
img_proc/tfRecordExample.py

@@ -0,0 +1,82 @@
+import tensorflow as tf
+from tensorflow.examples.tutorials.mnist import input_data
+import numpy as np
+import matplotlib.pyplot as plt
+import time
+
+
+# tfRecord defined by tf.train.Example (Protocol Buffer)
+# message Example{ Features features=1;}
+# message Features{ Map<string, Feature> feature=1 }
+# message Feature{oneof kind{ BytesList bytes_list=1; FloatList float_list=1; Int64List int64_list=1;}}
+
+def _int64_feature(value):
+    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
+
+
+def _bytes_feature(value):
+    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
+
+
+def save_mnist_record(output_filename="output_mnist.tfrecords"):
+    mnist = input_data.read_data_sets("../MNIST_data", dtype=tf.uint8, one_hot=True)
+    images = mnist.train.images
+    labels = mnist.train.labels
+    # define resolution
+    pixels = images.shape[1]
+    num_examples = mnist.train.num_examples
+
+    writer = tf.python_io.TFRecordWriter(output_filename)
+    for index in range(num_examples):
+        # convert img to str
+        image_raw = images[index].tostring()
+        # create Example Protocol Buffer
+        example = tf.train.Example(features=tf.train.Features(feature={
+            'pixels': _int64_feature(pixels),
+            'label': _int64_feature(np.argmax(labels[index])),
+            'image_raw': _bytes_feature(image_raw)
+        }))
+        writer.write(example.SerializeToString())
+    writer.close()
+
+
+def read_mnist_record(input_filename="output_mnist.tfrecords"):
+    reader = tf.TFRecordReader()
+    filename_queue = tf.train.string_input_producer([input_filename])
+    # read an example
+    _, serialized_example = reader.read(filename_queue)
+    # resolve the example
+    features = tf.parse_single_example(
+        serialized_example,
+        features={
+            # tf.FixedLenFeature return a Tensor
+            # tf.VarLenFeature return a SparseTensor
+            'pixels': tf.FixedLenFeature([], tf.int64),
+            'label': tf.FixedLenFeature([], tf.int64),
+            'image_raw': tf.FixedLenFeature([], tf.string)
+        }
+    )
+
+    # convert from str to img
+    image = tf.decode_raw(features['image_raw'], tf.uint8)
+    label = tf.cast(features['label'], tf.int32)
+    pixels = tf.cast(features['pixels'], tf.int32)
+
+    sess = tf.Session()
+    coord = tf.train.Coordinator()
+    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
+    for i in range(10):
+        img, answer, num_pixels = sess.run([image, label, pixels])
+        print("answer: %d, num of pixels: %d" % (answer, num_pixels))
+        plt.imshow(img.reshape(28, 28))
+        plt.show()
+        time.sleep(3)
+
+
+def main():
+    # save_mnist_record()
+    read_mnist_record()
+
+
+if __name__ == '__main__':
+    main()

+ 102 - 0
inceptionv3/inception_preprocessing.py

@@ -0,0 +1,102 @@
+# -*- utf-8 -*-
+
+import glob
+import os.path
+import tensorflow as tf
+import numpy as np
+from tensorflow.python.platform import gfile
+
+INPUT_DATA = "../../dataset/flower_photos"
+
+OUTPUT_FILE = "preprocess/flower_processed_data.npy"
+
+# test and validation ratio
+VALIDATION_PERCENTAGE = 10
+TEST_PERCENTAGE = 10
+
+
+def create_image_lists(sess, testing_percentage, validation_percentage):
+    # '../../dataset/flower_photos', '../../dataset/flower_photos/daisy', '../../dataset/flower_photos/tulips',
+    # '../../dataset/flower_photos/dandelion', '../../dataset/flower_photos/sunflowers',
+    # '../../dataset/flower_photos/roses']
+    subdirs = [x[0] for x in os.walk(INPUT_DATA)]
+    # print(subdirs)
+    is_root_dir = True
+
+    count = 0
+    # init datasets
+    training_images = []
+    training_labels = []
+    testing_images = []
+    testing_labels = []
+    validation_images = []
+    validation_labels = []
+    current_label = 0
+
+    # read all subdirs
+    for sub_dir in subdirs:
+        if is_root_dir:
+            is_root_dir = False
+            continue
+
+        extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
+        file_list = []
+        dir_name = os.path.basename(sub_dir)
+        # print(dir_name)
+        for extension in extensions:
+            # find all images in sub_dir
+            file_glob = os.path.join(INPUT_DATA, dir_name, '*.' + extension)
+            file_list.extend(glob.glob(file_glob))
+            if not file_list:
+                continue
+
+        # deal with images
+        for file_name in file_list:
+            print(str(current_label) + file_name + "\t\t" + str(count))
+            count += 1
+            image_raw_data = gfile.FastGFile(file_name, 'rb').read()
+            image = tf.image.decode_jpeg(image_raw_data)
+            if image.dtype != tf.float32:
+                image = tf.image.convert_image_dtype(image, dtype=tf.float32)
+            image = tf.image.resize_images(image, [299, 299])
+            image_value = sess.run(image)
+
+            # split dataset randomly
+            chance = np.random.randint(100)
+            if chance < validation_percentage:
+                validation_images.append(image_value)
+                validation_labels.append(current_label)
+            elif chance < (validation_percentage + testing_percentage):
+                testing_images.append(image_value)
+                testing_labels.append(current_label)
+            else:
+                training_images.append(image_value)
+                training_labels.append(current_label)
+        current_label += 1
+
+    state = np.random.get_state()
+    np.random.shuffle(training_images)
+    np.random.set_state(state)
+    np.random.shuffle(training_labels)
+    return np.asarray(
+        [training_images, training_labels, validation_images, validation_labels, testing_images, testing_labels])
+
+
+def main():
+    with tf.Session() as sess:
+        processed_data = create_image_lists(sess, TEST_PERCENTAGE, VALIDATION_PERCENTAGE)
+
+        np.save(OUTPUT_FILE, processed_data)
+
+
+if __name__ == '__main__':
+    main()
+
+    # a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
+    # b = [9, 8, 7, 6, 5, 4, 3, 2, 1]
+    # state = np.random.get_state()
+    # np.random.shuffle(a)
+    # np.random.set_state(state)
+    # np.random.shuffle(b)
+    # print(a)
+    # print(b)

+ 182 - 0
inceptionv3/inception_transfer.py

@@ -0,0 +1,182 @@
+# -*- utf-8 -*-
+import glob
+import os.path
+import time
+import matplotlib.pyplot as plt
+import numpy as np
+import tensorflow as tf
+from tensorflow.python.platform import gfile
+import tensorflow.contrib.slim as slim
+
+import tensorflow.contrib.slim.python.slim.nets.inception_v3 as inception_v3
+
+INPUT_DATA = 'preprocess/flower_processed_data.npy'
+
+TRAIN_FILE = 'model/'
+
+CKPT_FILE = '../../dataset/inception_v3.ckpt'
+
+# params
+LEARNING_RATE = 0.0001
+STEPS = 1000
+BATCH = 32
+N_CLASSES = 5
+
+# lasers don't load from ckpt, i.e. the last fc layer
+CHECKPOINT_EXCLUDE_SCOPES = 'InceptionV3/Logits,InceptionV3/AuxLogits'
+
+TRAINABLE_SCOPES = 'InceptionV3/Logits,InceptionV3/AuxLogits'
+
+TRAINING = False
+
+
+flower_label = ["daisy雏菊", "roses玫瑰", "tulips郁金香", "sunflowers向日葵", "dandelion蒲公英"]
+
+
+def get_tuned_variables():
+    exclusions = [scope.strip() for scope in CHECKPOINT_EXCLUDE_SCOPES.split(',')]
+    variables_to_restore = []
+
+    # enumerate params in v3 model, check if it need to be loaded
+    for var in slim.get_model_variables():
+        excluded = False
+        for exclusion in exclusions:
+            if var.op.name.startswith(exclusion):
+                excluded = True
+                break
+        if not excluded:
+            variables_to_restore.append(var)
+    return variables_to_restore
+
+
+def get_trainable_variables():
+    scopes = [scope.strip() for scope in TRAINABLE_SCOPES.split(',')]
+    variables_to_train = []
+
+    for scope in scopes:
+        variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
+        variables_to_train.extend(variables)
+    return variables_to_train
+
+
+def main():
+    # processed_data = np.load("preprocess/test_flower.npy", allow_pickle=True)
+    # test_images = processed_data[0]
+    # test_labels = processed_data[1]
+
+    # load preprocessed data
+    processed_data = np.load(INPUT_DATA, allow_pickle=True)
+    training_images = processed_data[0]
+    n_training_example = len(training_images)
+    training_labels = processed_data[1]
+    # np.save("preprocess/training_flower.npy", np.asarray([training_images, training_labels]))
+    validation_images = processed_data[2]
+    validation_labels = processed_data[3]
+    # np.save("preprocess/validation_flower.npy", np.asarray([validation_images, validation_labels]))
+    test_images = processed_data[4]
+    test_labels = processed_data[5]
+    # np.save("preprocess/test_flower.npy", np.asarray([test_images, test_labels]))
+
+    print("%d training examples, %d validation examples and %d testing examples." % (
+        n_training_example, len(validation_labels), len(test_labels)))
+
+    # define inputs
+    images = tf.placeholder(
+        tf.float32, [None, 299, 299, 3], name='input_images')
+    labels = tf.placeholder(tf.int64, [None], name='labels')
+
+    # define model
+    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
+        logits, _ = inception_v3.inception_v3(images, num_classes=N_CLASSES, is_training=False)
+    # get trainable variable
+    trainable_variables = get_trainable_variables()
+    # define cross entropy
+    tf.losses.softmax_cross_entropy(tf.one_hot(labels, N_CLASSES), logits, weights=1.0)
+    train_step = tf.train.RMSPropOptimizer(LEARNING_RATE).minimize(tf.losses.get_total_loss())
+
+    # calc accuracy
+    with tf.name_scope('evaluation'):
+        prediction = tf.argmax(logits, 1)
+        correct_answer = labels
+        correct_prediction = tf.equal(tf.argmax(logits, 1), labels)
+        evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
+
+    # define func to load model
+    load_fn = slim.assign_from_checkpoint_fn(
+        CKPT_FILE,
+        get_tuned_variables(),
+        ignore_missing_vars=True
+    )
+
+    # define saver
+    saver = tf.train.Saver()
+    config = tf.ConfigProto(allow_soft_placement=True)
+    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25)
+    config.gpu_options.allow_growth = True
+    with tf.Session(config=config) as sess:
+        # init
+        init = tf.global_variables_initializer()
+        sess.run(init)
+
+        ckpt = tf.train.get_checkpoint_state(
+            TRAIN_FILE
+        )
+        if ckpt and ckpt.model_checkpoint_path:
+            saver.restore(sess, ckpt.model_checkpoint_path)
+        else:
+            # load origin model
+            print('loading tuned variables from %s' % CKPT_FILE)
+            load_fn(sess)
+
+        start = 0
+        end = BATCH
+        if TRAINING:
+            for i in range(STEPS):
+                sess.run(train_step, feed_dict={
+                    images: training_images[start:end],
+                    labels: training_labels[start:end]
+                })
+
+                if i % 20 == 0 or i + 1 == STEPS:
+                    saver.save(sess, TRAIN_FILE, global_step=i)
+                    validation_accuracy = sess.run(evaluation_step, feed_dict={
+                        images: validation_images,
+                        labels: validation_labels
+                    })
+                    print('step %d: validation accuracy = %.1f%%' % (i, validation_accuracy * 100.0))
+
+                start = end
+                if start == n_training_example:
+                    start = 0
+
+                end = start + BATCH
+                if end > n_training_example:
+                    end = n_training_example
+
+            # test accuracy
+            test_acccuracy = sess.run(evaluation_step, feed_dict={
+                images: test_images,
+                labels: test_labels
+            })
+            print('final test accuracy = %.1f%%' % (test_acccuracy * 100.0))
+        else:
+            while True:
+                index = np.random.randint(0, len(test_labels) - 2)
+                # test accuracy
+                prediction_score, correct_answer_score = sess.run([prediction, correct_answer], feed_dict={
+                    images: test_images[index:index+1],
+                    labels: test_labels[index:index+1]
+                })
+                result = [(flower_label[x]+str(x)) for x in prediction_score]
+                answer = [(flower_label[x]+str(x)) for x in correct_answer_score]
+                # print(result)
+                # print(answer)
+                plt.imshow(test_images[index])
+                print('test result: %s, correct answer: %s' % (
+                    result, answer))
+                plt.show()
+                time.sleep(3)
+
+
+if __name__ == '__main__':
+    main()