├── README.md ├── mcnn ├── README.md ├── data.py ├── model.py ├── network-1.png ├── network-2.png └── train_test.py └── reading_data ├── example_tfrecords.py └── read_data_time_test.py /README.md: -------------------------------------------------------------------------------- 1 | # TensorFlowLaboratory 2 | 3 | **This repo has been deprecated currently and I will not maintain it. Meanwhile, I strongly recommend you can refer to my new repo: [TorchSeg](https://github.com/ycszen/TorchSeg), which offers fast, modular reference implementation and easy training of semantic segmentation algorithms in PyTorch.** 4 | 5 | ----- 6 | 7 | Some Reasearch about Tensorflow's Features 8 | - Write and Read TFRecords 9 | - Threading and Queues 10 | - Multi-task Models 11 | - joint training and alternative training 12 | 13 | More information in my [post](https://zhuanlan.zhihu.com/dlcode) 14 | -------------------------------------------------------------------------------- /mcnn/README.md: -------------------------------------------------------------------------------- 1 | # Multi-task Learning CNN 2 | 这个demo是针对于行人精细化属性识别的,需要识别行人性别,发长,衣服类别,裤子类别,鞋子类别等等,所以构造了一个多任务的CNN,需要同时识别很多属性。 3 | 4 | 同时,demo中包含完整的数据读写的应用,可做参考。 5 | 6 | ![image](https://github.com/ycszen/TensorFlowLaboratory/blob/master/mcnn/network-1.png) 7 | ![image](https://github.com/ycszen/TensorFlowLaboratory/blob/master/mcnn/network-2.png) 8 | -------------------------------------------------------------------------------- /mcnn/data.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PIL import Image 3 | import numpy as np 4 | import tensorflow as tf 5 | import tensorlayer as tl 6 | from xml.etree.ElementTree import ElementTree as ET 7 | 8 | img_height = 256 9 | img_width = 128 10 | 11 | 12 | def load_index(is_train): 13 | if is_train == True: 14 | index_file = "../TRAIN/indexs.txt" 15 | else: 16 | index_file = "../TEST/indexs.txt" 17 | 18 | with open(index_file) as f: 19 | indexs = f.readlines() 20 | 21 | indexs = [i.strip() for i in indexs] 22 | 23 | return indexs 24 | 25 | 26 | def _load_img(idx, is_train): 27 | if is_train == True: 28 | img_path = "../TRAIN/IMAGES_TRAIN" 29 | else: 30 | img_path = "../TEST/IMAGES_TEST" 31 | 32 | im = Image.open("{}/{}.jpg".format(img_path, idx)) 33 | im = im.resize((img_width, img_height)) 34 | in_ = np.array(im, dtype=np.uint8) 35 | 36 | #in_ = in_.transpose((2, 0, 1)) 37 | return in_ 38 | 39 | 40 | def _load_label(idx): 41 | xml_path = "../TRAIN/ANNOTATIONS_TRAIN" 42 | 43 | tree = ET() 44 | tree.parse("{}/{}.xml".format(xml_path, idx)) 45 | labels = {} 46 | labels["hair"] = int(tree.find("hairlength").text) 47 | labels["gender"] = int(tree.find("gender").text) 48 | 49 | objs = tree.findall("subcomponent") 50 | for obj in objs: 51 | name = obj.find("name").text 52 | if name in ["top", "down", "shoes", "bag"]: 53 | category = obj.find("category").text 54 | if category == "NULL" and name in ["bag", "shoes"]: 55 | category = 5 # represent there is no bag or shoes 56 | labels[name] = int(category) 57 | if name == "hat": 58 | if obj.find("bndbox").find("xmin").text == "NULL": 59 | labels[name] = 0 # represent here is no hat 60 | else: 61 | labels[name] = 1 62 | return labels 63 | 64 | 65 | def data_to_tfrecord(indexs, filename, is_train): 66 | print("Converting data into %s ..."%filename) 67 | writer = tf.python_io.TFRecordWriter(filename) 68 | 69 | for idx in indexs: 70 | img = _load_img(idx, is_train) 71 | #tl.visualize.frame(I=img, second=5, saveable=False, name='frame', fig_idx=12836) 72 | img_raw = img.tobytes() 73 | if is_train == True: 74 | labels = _load_label(idx) 75 | hat_label = int(labels['hat']) 76 | hair_label = int(labels['hair']) 77 | gender_label = int(labels['gender']) 78 | top_label = int(labels['top']) 79 | down_label = int(labels['down']) 80 | shoes_label = int(labels['shoes']) 81 | bag_label = int(labels['bag']) 82 | 83 | example = tf.train.Example(features=tf.train.Features(feature={ 84 | "hat_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[hat_label])), 85 | "hair_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[hair_label])), 86 | "gender_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[gender_label])), 87 | "top_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[top_label])), 88 | "down_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[down_label])), 89 | "shoes_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[shoes_label])), 90 | "bag_label": tf.train.Feature(int64_list=tf.train.Int64List(value=[bag_label])), 91 | "img_raw": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) 92 | })) 93 | else: 94 | example = tf.train.Example(features=tf.train.Features(feature={ 95 | "img_raw": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) 96 | })) 97 | writer.write(example.SerializeToString()) 98 | writer.close() 99 | 100 | 101 | 102 | def read_and_decode(filename, is_train): 103 | filename_queue = tf.train.string_input_producer([filename]) 104 | reader = tf.TFRecordReader() 105 | _,serialized_example = reader.read(filename_queue) 106 | 107 | if is_train == True: 108 | features = tf.parse_single_example(serialized_example, 109 | features={ 110 | "hat_label": tf.FixedLenFeature([], tf.int64), 111 | "hair_label": tf.FixedLenFeature([], tf.int64), 112 | "gender_label": tf.FixedLenFeature([], tf.int64), 113 | "top_label": tf.FixedLenFeature([], tf.int64), 114 | "down_label": tf.FixedLenFeature([], tf.int64), 115 | "shoes_label": tf.FixedLenFeature([], tf.int64), 116 | "bag_label": tf.FixedLenFeature([], tf.int64), 117 | "img_raw": tf.FixedLenFeature([], tf.string), 118 | }) 119 | img = tf.decode_raw(features['img_raw'], tf.uint8) 120 | img = tf.reshape(img, [128, 256, 3]) 121 | #image = Image.frombytes('RGB', (224, 224), img[0]) 122 | img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 123 | #print(type(img)) 124 | #img = np.asarray(img, dtype=np.uint8) 125 | #print(type(img)) 126 | #tl.visualize.frame(I=img, second=5, saveable=False, name='frame', fig_idx=12836) 127 | 128 | hat_label = tf.cast(features['hat_label'], tf.int32) 129 | hair_label = tf.cast(features['hair_label'], tf.int32) 130 | gender_label = tf.cast(features['gender_label'], tf.int32) 131 | top_label = tf.cast(features['top_label'], tf.int32) 132 | down_label = tf.cast(features['down_label'], tf.int32) 133 | shoes_label = tf.cast(features['shoes_label'], tf.int32) 134 | bag_label = tf.cast(features['bag_label'], tf.int32) 135 | labels = {"hat":hat_label, "hair":hair_label, "gender":gender_label, 136 | "top":top_label, "down":down_label, "shoes":shoes_label, 137 | "bag":bag_label} 138 | 139 | return img, labels 140 | else: 141 | features = tf.parse_single_example(serialized_example, 142 | features={ 143 | "img_raw": tf.FixedLenFeature([], tf.string), 144 | }) 145 | img = tf.decode_raw(features['img_raw'], tf.uint8) 146 | img = tf.reshape(img, [128, 256, 3]) 147 | img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 148 | #tl.visualize.frame(I=img, second=5, saveable=False, name='frame', fig_idx=12833) 149 | 150 | return img 151 | 152 | if __name__ == "__main__": 153 | print("Prepare Data ...") 154 | train_indexs = load_index(is_train=True) 155 | test_indexs = load_index(is_train=False) 156 | data_to_tfrecord(train_indexs, "train_tfrecord", is_train=True) 157 | data_to_tfrecord(test_indexs, "test_tfrecord", is_train=False) 158 | print("Data Success.") 159 | -------------------------------------------------------------------------------- /mcnn/model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import tensorlayer as tl 3 | from tensorlayer.layers import set_keep 4 | 5 | def conv_lrn_pool(input, conv_shape, conv_strides, pool_size, pool_strides, name): 6 | with tf.variable_scope("model", None): 7 | tl.layers.set_name_reuse(None) 8 | network = tl.layers.Conv2dLayer(input, 9 | act=tf.nn.relu, 10 | shape=conv_shape, 11 | strides=conv_strides, 12 | padding="SAME", 13 | W_init=tf.truncated_normal_initializer(stddev=5e-2), 14 | b_init=tf.constant_initializer(value=0.0), 15 | name="conv_" + name) 16 | network.outputs = tf.nn.lrn(network.outputs, 5, bias=1.0, 17 | alpha=0.00005, beta=0.75, name="norm_" + name) 18 | network = tl.layers.PoolLayer(network, ksize=pool_size, 19 | strides=pool_strides, 20 | padding="SAME", 21 | pool=tf.nn.max_pool, 22 | name="pool_" + name) 23 | return network 24 | 25 | 26 | def branch(input, name): 27 | with tf.variable_scope("model", None): 28 | tl.layers.set_name_reuse(None) 29 | network = tl.layers.DenseLayer(input, n_units=512, 30 | act=tf.nn.relu, 31 | W_init=tf.truncated_normal_initializer(stddev=5e-2), 32 | b_init=tf.constant_initializer(value=0.0), 33 | name="dense1_" + name) 34 | network = tl.layers.DropoutLayer(network, keep=0.5, name="drop1_" + name) 35 | network = tl.layers.DenseLayer(network, n_units=512, 36 | act=tf.nn.relu, 37 | W_init=tf.truncated_normal_initializer(stddev=5e-2), 38 | b_init=tf.constant_initializer(value=0.0), 39 | name="dense2_" + name) 40 | network = tl.layers.DropoutLayer(network, keep=0.5, name="drop2_" + name) 41 | return network 42 | 43 | 44 | def single_branch(input, name): 45 | with tf.variable_scope("model", None): 46 | tl.layers.set_name_reuse(None) 47 | network = conv_lrn_pool(input, conv_shape=[3, 3, 200, 300], 48 | conv_strides=[1, 1, 1, 1], 49 | pool_size=[1, 5, 5, 1], 50 | pool_strides=[1, 3, 3, 1],name=name) 51 | 52 | network = tl.layers.FlattenLayer(network, name='flatten_'+name) 53 | network = branch(network, name) 54 | 55 | return network 56 | 57 | 58 | def double_branch(input, name, name1, name2): 59 | with tf.variable_scope("model", None): 60 | tl.layers.set_name_reuse(None) 61 | network = conv_lrn_pool(input, conv_shape=[3, 3, 200, 300], 62 | conv_strides=[1, 1, 1, 1], 63 | pool_size=[1, 5, 5, 1], 64 | pool_strides=[1, 3, 3, 1], name=name) 65 | 66 | network = tl.layers.FlattenLayer(network, name='flatten_'+name) 67 | 68 | branch1 = branch(network, name1) 69 | branch2 = branch(network, name2) 70 | 71 | return branch1, branch2 72 | 73 | 74 | def score(input, classes, name): 75 | with tf.variable_scope("model", None): 76 | tl.layers.set_name_reuse(None) 77 | return tl.layers.DenseLayer(input, n_units=classes, 78 | W_init=tf.truncated_normal_initializer(stddev=0.001), 79 | b_init=tf.constant_initializer(value=0.0), 80 | name=name+"_score") 81 | 82 | 83 | def loss_acc(y, y_): 84 | with tf.variable_scope("model", None): 85 | tl.layers.set_name_reuse(None) 86 | cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_)) 87 | correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.int32), y_) 88 | acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 89 | return cost, acc 90 | 91 | 92 | def inference(x, y_, reuse): 93 | with tf.variable_scope("model", reuse=reuse): 94 | tl.layers.set_name_reuse(reuse) 95 | 96 | network = tl.layers.InputLayer(x, name="input_layer") 97 | network = conv_lrn_pool(network, conv_shape=[7, 7, 3, 75], 98 | conv_strides=[1, 2, 2, 1], 99 | pool_size=[1, 3, 3, 1], 100 | pool_strides=[1, 2, 2, 1], name="layer1") 101 | 102 | network = conv_lrn_pool(network, conv_shape=[5, 5, 75, 200], 103 | conv_strides=[1, 2, 2, 1], 104 | pool_size=[1, 3, 3, 1], 105 | pool_strides=[1, 2, 2, 1], name="layer2") 106 | 107 | hair_output, hat_output = double_branch(network, "head", "hair", "hat") 108 | hair_score = score(hair_output, 3, "hair") 109 | hair_y = hair_score.outputs 110 | if y_ != None: 111 | hair_loss, hair_acc = loss_acc(hair_y, y_["hair"]) 112 | 113 | hat_score = score(hat_output, 2, "hat") 114 | hat_y = hat_score.outputs 115 | if y_ != None: 116 | hat_loss, hat_acc = loss_acc(hat_y, y_["hat"]) 117 | 118 | gender_output = single_branch(network, "gender") 119 | gender_score = score(gender_output, 3, "gender") 120 | gender_y = gender_score.outputs 121 | if y_ != None: 122 | gender_loss, gender_acc = loss_acc(gender_y, y_["gender"]) 123 | 124 | top_output = single_branch(network, "top") 125 | top_score = score(top_output, 6, "top") 126 | top_y = top_score.outputs 127 | if y_ != None: 128 | top_loss, top_acc = loss_acc(top_y, y_["top"]) 129 | 130 | down_output = single_branch(network, "down") 131 | down_score = score(down_output, 5, "down") 132 | down_y = down_score.outputs 133 | if y_ != None: 134 | down_loss, down_acc = loss_acc(down_y, y_["down"]) 135 | 136 | shoes_output = single_branch(network, "shoes") 137 | shoes_score = score(shoes_output, 6, "shoes") 138 | shoes_y = shoes_score.outputs 139 | if y_ != None: 140 | shoes_loss, shoes_acc = loss_acc(shoes_y, y_["shoes"]) 141 | 142 | bag_output = single_branch(network, "bag") 143 | bag_score = score(bag_output, 6, "bag") 144 | bag_y = bag_score.outputs 145 | if y_ != None: 146 | bag_loss, bag_acc = loss_acc(bag_y, y_["bag"]) 147 | 148 | if y_!=None: 149 | cost = {"all": hair_loss+hat_loss+gender_loss+top_loss+down_loss+shoes_loss+bag_loss, 150 | "hair":hair_loss, "hat":hat_loss, "gender":gender_loss, 151 | "top":top_loss, "down":down_loss, "shoes":shoes_loss, "bag":bag_loss} 152 | acc = {"all":hair_acc+hat_acc+gender_acc+top_acc+down_acc+shoes_acc+bag_acc, 153 | "hair":hair_acc, "hat":hat_acc, "gender":gender_acc, 154 | "top":top_acc, "down":down_acc, "shoes":shoes_acc, "bag":bag_acc} 155 | net = {"hair":hair_score, "hat":hat_score, "gender":gender_score, 156 | "top":top_score, "down":down_score, "shoes":shoes_score, "bag":bag_score} 157 | 158 | if y_!=None: 159 | return cost, acc, net 160 | return net 161 | 162 | 163 | -------------------------------------------------------------------------------- /mcnn/network-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yu-changqian/TensorFlowLaboratory/0593d73a93e31ae9a1ecdec1d080497deee7d9fd/mcnn/network-1.png -------------------------------------------------------------------------------- /mcnn/network-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yu-changqian/TensorFlowLaboratory/0593d73a93e31ae9a1ecdec1d080497deee7d9fd/mcnn/network-2.png -------------------------------------------------------------------------------- /mcnn/train_test.py: -------------------------------------------------------------------------------- 1 | import time 2 | import tensorflow as tf 3 | import tensorlayer as tl 4 | from data import load_index, data_to_tfrecord, read_and_decode 5 | from model import inference 6 | 7 | learning_rate = 0.1 8 | batch_size = 100 9 | epoches = 100 10 | n_step_epoch = int(20000/100) 11 | n_step = n_step_epoch*epoches 12 | print_freq =1 13 | 14 | print("Start.") 15 | with tf.device("/gpu:3"): 16 | sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) 17 | 18 | #1.prepare data in cpu 19 | #print("Prepare Data ...") 20 | #train_indexs = load_index(is_train=True) 21 | #test_indexs = load_index(is_train=False) 22 | #data_to_tfrecord(train_indexs, "train_tfrecord", is_train=True) 23 | #data_to_tfrecord(test_indexs, "test_tfrecord", is_train=False) 24 | #print("Data Success.") 25 | 26 | x_train, y_train = read_and_decode("train_tfrecord", is_train=True) 27 | x_test = read_and_decode("test_tfrecord", is_train=False) 28 | 29 | x_train_batch, hair_batch, hat_batch, \ 30 | gender_batch, top_batch, down_batch, \ 31 | shoes_batch, bag_batch = tf.train.shuffle_batch([x_train, y_train['hair'], 32 | y_train['hat'], y_train['gender'], 33 | y_train['top'], y_train['down'], 34 | y_train['shoes'], y_train['bag']], 35 | batch_size=batch_size, 36 | capacity=2000, 37 | min_after_dequeue=1000, 38 | num_threads=12) 39 | y_train_batch = {"hat":hat_batch, "hair":hair_batch, "gender":gender_batch, 40 | "top":top_batch, "down":down_batch, "shoes":shoes_batch, 41 | "bag":bag_batch} 42 | x_test_train = tf.train.batch([x_test], 43 | batch_size=batch_size, 44 | capacity=2000, 45 | num_threads=12) 46 | 47 | #2. 48 | with tf.device("/gpu:3"): 49 | cost, acc, network = inference(x_train_batch, y_train_batch, None) 50 | 51 | #cost, 52 | all_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 53 | epsilon=1e-08, use_locking=False).minimize(cost['all']) 54 | hair_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 55 | epsilon=1e-08, use_locking=False).minimize(cost['hair']) 56 | hat_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 57 | epsilon=1e-08, use_locking=False).minimize(cost['hat']) 58 | gender_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 59 | epsilon=1e-08, use_locking=False).minimize(cost['gender']) 60 | top_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 61 | epsilon=1e-08, use_locking=False).minimize(cost['top']) 62 | down_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 63 | epsilon=1e-08, use_locking=False).minimize(cost['down']) 64 | shoes_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 65 | epsilon=1e-08, use_locking=False).minimize(cost['shoes']) 66 | bag_train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, 67 | epsilon=1e-08, use_locking=False).minimize(cost['bag']) 68 | train_op = {"hair": hair_train_op, "hat": hat_train_op, "gender": gender_train_op, 69 | "top": top_train_op, "down": down_train_op, "shoes": shoes_train_op, "bag": bag_train_op} 70 | all_drop = [network["hair"].all_drop, network["hat"].all_drop, network["gender"].all_drop, 71 | network["top"].all_drop, network["down"].all_drop, network["shoes"].all_drop, 72 | network["bag"].all_drop] 73 | 74 | with tf.device("/gpu:3"): 75 | sess.run(tf.initialize_all_variables()) 76 | coord = tf.train.Coordinator() 77 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 78 | 79 | print("Train Start ...") 80 | step = 0 81 | for epoch in range(epoches): 82 | start_time = time.time() 83 | hat_loss, hair_loss, gender_loss, top_loss, down_loss, shoes_loss, bag_loss = 0, 0, 0, 0, 0, 0, 0 84 | hat_acc, hair_acc, gender_acc, top_acc, down_acc, shoes_acc, bag_acc = 0, 0, 0, 0, 0, 0, 0 85 | train_loss, train_acc, n_batch = 0, 0, 0 86 | for s in range(n_step_epoch): 87 | feed_dict = {} 88 | for i in range(len(all_drop)): 89 | feed_dict.update(all_drop[i]) 90 | hat_err, hat_ac, _ = sess.run([cost['hat'], acc['hat'], hat_train_op], feed_dict) 91 | hair_err, hair_ac, _ = sess.run([cost['hair'], acc['hair'], hair_train_op], feed_dict) 92 | gender_err, gender_ac, _ = sess.run([cost['gender'], acc['gender'], gender_train_op], feed_dict) 93 | top_err, top_ac, _ = sess.run([cost['top'], acc['top'], top_train_op], feed_dict) 94 | down_err, down_ac, _ = sess.run([cost['down'], acc['down'], down_train_op], feed_dict) 95 | shoes_err, shoes_ac, _ = sess.run([cost['shoes'], acc['shoes'], shoes_train_op], feed_dict) 96 | bag_err, bag_ac, _ = sess.run([cost['bag'], acc['bag'], bag_train_op], feed_dict) 97 | #err, ac, _ = sess.run([cost['all'], acc['all'], all_train_op],feed_dict) 98 | step += 1 99 | 100 | hat_loss += hat_err 101 | hair_loss += hair_err 102 | gender_loss += gender_err 103 | top_loss += top_err 104 | down_loss += down_err 105 | shoes_loss += shoes_err 106 | bag_loss += bag_err 107 | 108 | hat_acc += hat_ac 109 | hair_acc += hair_ac 110 | gender_acc += gender_ac 111 | top_acc += top_ac 112 | down_acc += down_ac 113 | shoes_acc += shoes_ac 114 | bag_acc += bag_ac 115 | 116 | train_loss += (hat_err+hair_err+gender_err+top_err+down_err+shoes_err+bag_err) 117 | train_acc += (hat_ac+hair_ac+gender_ac+top_ac+down_ac+shoes_ac+bag_ac) 118 | n_batch += 1 119 | 120 | if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: 121 | print("Epoch %d : Step %d-%d of %d took %fs" % ( 122 | epoch, step, step + n_step_epoch, n_step, time.time() - start_time)) 123 | print(" train loss: %f" % (train_loss / n_batch)) 124 | print(" train acc: %f" % (train_acc / n_batch)) 125 | print(" hat loss: %f" % (hat_loss / n_batch)) 126 | print(" hat acc: %f" % (hat_acc / n_batch)) 127 | print(" hair loss: %f" % (hair_loss / n_batch)) 128 | print(" hair acc: %f" % (hair_acc / n_batch)) 129 | print(" gender loss: %f" % (gender_loss / n_batch)) 130 | print(" gender acc: %f" % (gender_acc / n_batch)) 131 | print(" top loss: %f" % (top_loss / n_batch)) 132 | print(" top acc: %f" % (top_acc / n_batch)) 133 | print(" down loss: %f" % (down_loss / n_batch)) 134 | print(" down acc: %f" % (down_acc / n_batch)) 135 | print(" shoes loss: %f" % (shoes_loss / n_batch)) 136 | print(" shoes acc: %f" % (shoes_acc / n_batch)) 137 | print(" bag loss: %f" % (bag_loss / n_batch)) 138 | print(" bag acc: %f" % (bag_acc / n_batch)) 139 | 140 | saver = tf.train.Saver() 141 | save_path = saver.save(sess, "mcnn_model.ckpt") 142 | coord.request_stop() 143 | coord.join(threads) 144 | sess.close() 145 | 146 | 147 | -------------------------------------------------------------------------------- /reading_data/example_tfrecords.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | from PIL import Image 4 | 5 | cwd = os.getcwd() 6 | 7 | def create_record(): 8 | ''' 9 | 此处我加载的数据目录如下: 10 | 0 -- img1.jpg 11 | img2.jpg 12 | img3.jpg 13 | ... 14 | 1 -- img1.jpg 15 | img2.jpg 16 | ... 17 | 2 -- ... 18 | ... 19 | ''' 20 | writer = tf.python_io.TFRecordWriter("train.tfrecords") 21 | for index, name in enumerate(num_classes): 22 | class_path = cwd + name + "/" 23 | for img_name in os.listdir(class_path): 24 | img_path = class_path + img_name 25 | img = Image.open(img_path) 26 | img = img.resize((224, 224)) 27 | img_raw = img.tobytes() #将图片转化为原生bytes 28 | example = tf.train.Example(features=tf.train.Features(feature={ 29 | "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])), 30 | 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) 31 | })) 32 | writer.write(example.SerializeToString()) 33 | writer.close() 34 | 35 | def read_and_decode(filename): 36 | filename_queue = tf.train.string_input_producer([filename]) 37 | 38 | reader = tf.TFRecordReader() 39 | _, serialized_example = reader.read(filename_queue) 40 | features = tf.parse_single_example(serialized_example, 41 | features={ 42 | 'label': tf.FixedLenFeature([], tf.int64), 43 | 'img_raw' : tf.FixedLenFeature([], tf.string), 44 | }) 45 | 46 | img = tf.decode_raw(features['img_raw'], tf.uint8) 47 | img = tf.reshape(img, [224, 224, 3]) 48 | img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 49 | label = tf.cast(features['label'], tf.int32) 50 | 51 | return img, label 52 | 53 | if __name__ == '__main__': 54 | img, label = read_and_decode("train.tfrecords") 55 | 56 | img_batch, label_batch = tf.train.shuffle_batch([img, label], 57 | batch_size=30, capacity=2000, 58 | min_after_dequeue=1000) 59 | #初始化所有的op 60 | init = tf.initialize_all_variables() 61 | 62 | with tf.Session() as sess: 63 | sess.run(init) 64 | #启动队列 65 | threads = tf.train.start_queue_runners(sess=sess) 66 | for i in range(3): 67 | val, l= sess.run([img_batch, label_batch]) 68 | #l = to_categorical(l, 12) 69 | print(val.shape, l) 70 | -------------------------------------------------------------------------------- /reading_data/read_data_time_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | from tensorflow.python import debug as tf_debug 4 | import matplotlib.pyplot as plt 5 | from PIL import Image 6 | import time 7 | 8 | root = "./ADE20K/images/training/" 9 | 10 | def get_filenames(path): 11 | filenames = [] 12 | for root, dirs, files in os.walk(path): 13 | for f in files: 14 | if ".jpg" in f: 15 | filenames.append(os.path.join(root, f)) 16 | return filenames 17 | 18 | def convert_to_tfrecord(): 19 | writer = tf.python_io.TFRecordWriter("./training.tfrecords") 20 | filenames = get_filenames(root) 21 | for name in filenames: 22 | img = Image.open(name) 23 | if img.mode == "RGB": 24 | img = img.resize((256, 256), Image.NEAREST) 25 | img_raw = img.tobytes() 26 | example = tf.train.Example(features=tf.train.Features(feature={ 27 | "img_raw":tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) 28 | })) 29 | writer.write(example.SerializeToString()) 30 | writer.close() 31 | 32 | def read_img(filenames, num_epochs, shuffle=True): 33 | filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs, shuffle=True) 34 | 35 | reader = tf.WholeFileReader() 36 | key, value = reader.read(filename_queue) 37 | img = tf.image.decode_jpeg(value, channels=3) 38 | img = tf.image.resize_images(img, size=(256, 256), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) 39 | 40 | return img 41 | 42 | def read_tfrecord(filenames, num_epochs, shuffle=True): 43 | filename_queue = tf.train.string_input_producer([filenames], num_epochs=num_epochs, shuffle=True) 44 | 45 | reader = tf.TFRecordReader() 46 | _, serialized_example = reader.read(filename_queue) 47 | features = tf.parse_single_example(serialized_example, features={ 48 | "img_raw": tf.FixedLenFeature([], tf.string), 49 | }) 50 | img = tf.decode_raw(features["img_raw"], tf.uint8) 51 | img = tf.reshape(img, [256, 256, 3]) 52 | 53 | return img 54 | 55 | if __name__ == '__main__': 56 | #create_tfrecord_start_time = time.time() 57 | #convert_to_tfrecord() 58 | #create_tfrecord_duration = time.time() - create_tfrecord_start_time 59 | #print("Create TFrecord Duration: %.3f" % (create_tfrecord_duration)) 60 | 61 | with tf.Session() as sess: 62 | min_after_dequeue = 1000 63 | capacity = min_after_dequeue + 3*4 64 | 65 | img = read_img(get_filenames(root), 1, True) 66 | # img = read_tfrecord("training.tfrecords", 1, True) 67 | img_batch = tf.train.shuffle_batch([img], batch_size=4, num_threads=8, 68 | capacity=capacity, 69 | min_after_dequeue=min_after_dequeue) 70 | 71 | 72 | init = (tf.global_variables_initializer(), tf.local_variables_initializer()) 73 | sess.run(init) 74 | # sess = tf_debug.LocalCLIDebugWrapperSession(sess) 75 | # print(sess.run(img)) 76 | coord = tf.train.Coordinator() 77 | threads = tf.train.start_queue_runners(coord=coord) 78 | i = 0 79 | read_tfrecord_start_time = time.time() 80 | try: 81 | while not coord.should_stop(): 82 | imgs = sess.run([img_batch]) 83 | for img in imgs: 84 | print(img.shape) 85 | except Exception, e: 86 | coord.request_stop(e) 87 | finally: 88 | coord.request_stop() 89 | coord.join(threads) 90 | read_tfrecord_duration = time.time() - read_tfrecord_start_time 91 | print("Read TFrecord Duration: %.3f" % read_tfrecord_duration) 92 | --------------------------------------------------------------------------------