├── 01 cats vs dogs ├── README.md ├── data │ └── data directory ├── images │ ├── 011.png │ ├── 012.png │ └── starry night dd3.jpg ├── input_data.py ├── model.py ├── new_version │ ├── images │ │ ├── 101.png │ │ ├── 102.png │ │ ├── 103.png │ │ └── readme │ ├── input_train_val_split.py │ ├── model.py │ ├── readme.md │ └── train_and_val.py └── training.py ├── 02 CIFAR10 ├── README.md ├── cifar10.py ├── cifar10_input.py ├── data │ └── put the data in this folder └── images │ ├── 001.JPG │ ├── 002.JPG │ ├── 003.JPG │ └── 004.JPG ├── 03 TFRecord ├── Data │ └── README.md ├── Images │ ├── 001.JPG │ ├── 002.JPG │ ├── 003.JPG │ ├── 004.JPG │ ├── 005.JPG │ ├── 006.JPG │ └── README.md ├── README.md └── notMNIST_input.py ├── 04 VGG Tensorflow ├── VGG.py ├── images │ ├── 000.JPG │ ├── 001.JPG │ ├── 002.JPG │ ├── 007.png │ ├── 008.JPG │ ├── 009.JPG │ └── 010.JPG ├── input_data.py ├── readme.md ├── tool_show_feature_map.py ├── tool_show_size.py ├── tool_variable_name.py ├── tools.py └── training_and_val.py ├── 05 object localization └── README.md └── README.md /01 cats vs dogs/README.md: -------------------------------------------------------------------------------- 1 | # How to use TensorFlow to process our own data? 2 | 1. read in data 3 | 2. make an input queue 4 | 3. generate batches 5 | 4. train 6 | 5. save model architecture and parameters 7 | 6. restore model architecture and parameters 8 | 7. test new data 9 | 10 | # The data 11 | - Data: The cats vs. dogs dataset from Kaggle were used 12 | - Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 13 | # The goal 14 | - the goal of this project is to show how to use Tensorflow to process our own data. 15 | - only 2 conv layers are used here, more complex and powerful models will be covered in the future 16 | # Screenshots 17 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/images/011.png) 18 | 19 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/images/012.png) 20 | 21 | -------------------------------------------------------------------------------- /01 cats vs dogs/data/data directory: -------------------------------------------------------------------------------- 1 | You can put the data in this directory or any other directories, as long as you specify the directory correctly in the code. 2 | -------------------------------------------------------------------------------- /01 cats vs dogs/images/011.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/images/011.png -------------------------------------------------------------------------------- /01 cats vs dogs/images/012.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/images/012.png -------------------------------------------------------------------------------- /01 cats vs dogs/images/starry night dd3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/images/starry night dd3.jpg -------------------------------------------------------------------------------- /01 cats vs dogs/input_data.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | 4 | # 11.08 2017 更新 5 | # 最近入驻了网易云课堂讲师,我的第一门课《使用亚马逊云计算训练深度学习模型》。 6 | # 有兴趣的同学可以学习交流。 7 | # * 我的网易云课堂主页: http://study.163.com/provider/400000000275062/index.htm 8 | 9 | # 深度学习QQ群, 1群满): 153032765 10 | # 2群:462661267 11 | #The aim of this project is to use TensorFlow to process our own data. 12 | # - input_data.py: read in data and generate batches 13 | # - model: build the model architecture 14 | # - training: train 15 | 16 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 17 | # With current settings, 10000 traing steps needed 50 minutes on my laptop. 18 | 19 | 20 | # data: cats vs. dogs from Kaggle 21 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 22 | # data size: ~540M 23 | 24 | # How to run? 25 | # 1. run the training.py once 26 | # 2. call the run_training() in the console to train the model. 27 | 28 | # Note: 29 | # it is suggested to restart your kenel to train the model multiple times 30 | #(in order to clear all the variables in the memory) 31 | # Otherwise errors may occur: conv1/weights/biases already exist...... 32 | 33 | 34 | #%% 35 | 36 | import tensorflow as tf 37 | import numpy as np 38 | import os 39 | 40 | #%% 41 | 42 | # you need to change this to your data directory 43 | train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 44 | 45 | def get_files(file_dir): 46 | ''' 47 | Args: 48 | file_dir: file directory 49 | Returns: 50 | list of images and labels 51 | ''' 52 | cats = [] 53 | label_cats = [] 54 | dogs = [] 55 | label_dogs = [] 56 | for file in os.listdir(file_dir): 57 | name = file.split(sep='.') 58 | if name[0]=='cat': 59 | cats.append(file_dir + file) 60 | label_cats.append(0) 61 | else: 62 | dogs.append(file_dir + file) 63 | label_dogs.append(1) 64 | print('There are %d cats\nThere are %d dogs' %(len(cats), len(dogs))) 65 | 66 | image_list = np.hstack((cats, dogs)) 67 | label_list = np.hstack((label_cats, label_dogs)) 68 | 69 | temp = np.array([image_list, label_list]) 70 | temp = temp.transpose() 71 | np.random.shuffle(temp) 72 | 73 | image_list = list(temp[:, 0]) 74 | label_list = list(temp[:, 1]) 75 | label_list = [int(i) for i in label_list] 76 | 77 | 78 | return image_list, label_list 79 | 80 | 81 | #%% 82 | 83 | def get_batch(image, label, image_W, image_H, batch_size, capacity): 84 | ''' 85 | Args: 86 | image: list type 87 | label: list type 88 | image_W: image width 89 | image_H: image height 90 | batch_size: batch size 91 | capacity: the maximum elements in queue 92 | Returns: 93 | image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32 94 | label_batch: 1D tensor [batch_size], dtype=tf.int32 95 | ''' 96 | 97 | image = tf.cast(image, tf.string) 98 | label = tf.cast(label, tf.int32) 99 | 100 | # make an input queue 101 | input_queue = tf.train.slice_input_producer([image, label]) 102 | 103 | label = input_queue[1] 104 | image_contents = tf.read_file(input_queue[0]) 105 | image = tf.image.decode_jpeg(image_contents, channels=3) 106 | 107 | ###################################### 108 | # data argumentation should go to here 109 | ###################################### 110 | 111 | image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H) 112 | 113 | # if you want to test the generated batches of images, you might want to comment the following line. 114 | # 如果想看到正常的图片,请注释掉111行(标准化)和 126行(image_batch = tf.cast(image_batch, tf.float32)) 115 | # 训练时不要注释掉! 116 | image = tf.image.per_image_standardization(image) 117 | 118 | image_batch, label_batch = tf.train.batch([image, label], 119 | batch_size= batch_size, 120 | num_threads= 64, 121 | capacity = capacity) 122 | 123 | #you can also use shuffle_batch 124 | # image_batch, label_batch = tf.train.shuffle_batch([image,label], 125 | # batch_size=BATCH_SIZE, 126 | # num_threads=64, 127 | # capacity=CAPACITY, 128 | # min_after_dequeue=CAPACITY-1) 129 | 130 | label_batch = tf.reshape(label_batch, [batch_size]) 131 | image_batch = tf.cast(image_batch, tf.float32) 132 | 133 | return image_batch, label_batch 134 | 135 | 136 | 137 | #%% TEST 138 | # To test the generated batches of images 139 | # When training the model, DO comment the following codes 140 | 141 | 142 | 143 | 144 | #import matplotlib.pyplot as plt 145 | # 146 | #BATCH_SIZE = 2 147 | #CAPACITY = 256 148 | #IMG_W = 208 149 | #IMG_H = 208 150 | # 151 | #train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 152 | # 153 | #image_list, label_list = get_files(train_dir) 154 | #image_batch, label_batch = get_batch(image_list, label_list, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) 155 | # 156 | #with tf.Session() as sess: 157 | # i = 0 158 | # coord = tf.train.Coordinator() 159 | # threads = tf.train.start_queue_runners(coord=coord) 160 | # 161 | # try: 162 | # while not coord.should_stop() and i<1: 163 | # 164 | # img, label = sess.run([image_batch, label_batch]) 165 | # 166 | # # just test one batch 167 | # for j in np.arange(BATCH_SIZE): 168 | # print('label: %d' %label[j]) 169 | # plt.imshow(img[j,:,:,:]) 170 | # plt.show() 171 | # i+=1 172 | # 173 | # except tf.errors.OutOfRangeError: 174 | # print('done!') 175 | # finally: 176 | # coord.request_stop() 177 | # coord.join(threads) 178 | 179 | 180 | #%% 181 | 182 | 183 | 184 | 185 | 186 | 187 | -------------------------------------------------------------------------------- /01 cats vs dogs/model.py: -------------------------------------------------------------------------------- 1 | 2 | #By @Kevin Xu 3 | #kevin28520@gmail.com 4 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 5 | # 6 | 7 | #The aim of this project is to use TensorFlow to process our own data. 8 | # - input_data.py: read in data and generate batches 9 | # - model: build the model architecture 10 | # - training: train 11 | 12 | # data: cats vs. dogs from Kaggle 13 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 14 | # data size: ~540M 15 | 16 | # How to run? 17 | # 1. run the training.py once 18 | # 2. call the run_training() in the console to train the model. 19 | 20 | 21 | #%% 22 | 23 | import tensorflow as tf 24 | 25 | #%% 26 | def inference(images, batch_size, n_classes): 27 | '''Build the model 28 | Args: 29 | images: image batch, 4D tensor, tf.float32, [batch_size, width, height, channels] 30 | Returns: 31 | output tensor with the computed logits, float, [batch_size, n_classes] 32 | ''' 33 | #conv1, shape = [kernel size, kernel size, channels, kernel numbers] 34 | 35 | with tf.variable_scope('conv1') as scope: 36 | weights = tf.get_variable('weights', 37 | shape = [3,3,3, 16], 38 | dtype = tf.float32, 39 | initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) 40 | biases = tf.get_variable('biases', 41 | shape=[16], 42 | dtype=tf.float32, 43 | initializer=tf.constant_initializer(0.1)) 44 | conv = tf.nn.conv2d(images, weights, strides=[1,1,1,1], padding='SAME') 45 | pre_activation = tf.nn.bias_add(conv, biases) 46 | conv1 = tf.nn.relu(pre_activation, name= scope.name) 47 | 48 | #pool1 and norm1 49 | with tf.variable_scope('pooling1_lrn') as scope: 50 | pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1],strides=[1,2,2,1], 51 | padding='SAME', name='pooling1') 52 | norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, 53 | beta=0.75,name='norm1') 54 | 55 | #conv2 56 | with tf.variable_scope('conv2') as scope: 57 | weights = tf.get_variable('weights', 58 | shape=[3,3,16,16], 59 | dtype=tf.float32, 60 | initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) 61 | biases = tf.get_variable('biases', 62 | shape=[16], 63 | dtype=tf.float32, 64 | initializer=tf.constant_initializer(0.1)) 65 | conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1],padding='SAME') 66 | pre_activation = tf.nn.bias_add(conv, biases) 67 | conv2 = tf.nn.relu(pre_activation, name='conv2') 68 | 69 | 70 | #pool2 and norm2 71 | with tf.variable_scope('pooling2_lrn') as scope: 72 | norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0, 73 | beta=0.75,name='norm2') 74 | pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1], 75 | padding='SAME',name='pooling2') 76 | 77 | 78 | #local3 79 | with tf.variable_scope('local3') as scope: 80 | reshape = tf.reshape(pool2, shape=[batch_size, -1]) 81 | dim = reshape.get_shape()[1].value 82 | weights = tf.get_variable('weights', 83 | shape=[dim,128], 84 | dtype=tf.float32, 85 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 86 | biases = tf.get_variable('biases', 87 | shape=[128], 88 | dtype=tf.float32, 89 | initializer=tf.constant_initializer(0.1)) 90 | local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name) 91 | 92 | #local4 93 | with tf.variable_scope('local4') as scope: 94 | weights = tf.get_variable('weights', 95 | shape=[128,128], 96 | dtype=tf.float32, 97 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 98 | biases = tf.get_variable('biases', 99 | shape=[128], 100 | dtype=tf.float32, 101 | initializer=tf.constant_initializer(0.1)) 102 | local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4') 103 | 104 | 105 | # softmax 106 | with tf.variable_scope('softmax_linear') as scope: 107 | weights = tf.get_variable('softmax_linear', 108 | shape=[128, n_classes], 109 | dtype=tf.float32, 110 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 111 | biases = tf.get_variable('biases', 112 | shape=[n_classes], 113 | dtype=tf.float32, 114 | initializer=tf.constant_initializer(0.1)) 115 | softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear') 116 | 117 | return softmax_linear 118 | 119 | #%% 120 | def losses(logits, labels): 121 | '''Compute loss from logits and labels 122 | Args: 123 | logits: logits tensor, float, [batch_size, n_classes] 124 | labels: label tensor, tf.int32, [batch_size] 125 | 126 | Returns: 127 | loss tensor of float type 128 | ''' 129 | with tf.variable_scope('loss') as scope: 130 | cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits\ 131 | (logits=logits, labels=labels, name='xentropy_per_example') 132 | loss = tf.reduce_mean(cross_entropy, name='loss') 133 | tf.summary.scalar(scope.name+'/loss', loss) 134 | return loss 135 | 136 | #%% 137 | def trainning(loss, learning_rate): 138 | '''Training ops, the Op returned by this function is what must be passed to 139 | 'sess.run()' call to cause the model to train. 140 | 141 | Args: 142 | loss: loss tensor, from losses() 143 | 144 | Returns: 145 | train_op: The op for trainning 146 | ''' 147 | with tf.name_scope('optimizer'): 148 | optimizer = tf.train.AdamOptimizer(learning_rate= learning_rate) 149 | global_step = tf.Variable(0, name='global_step', trainable=False) 150 | train_op = optimizer.minimize(loss, global_step= global_step) 151 | return train_op 152 | 153 | #%% 154 | def evaluation(logits, labels): 155 | """Evaluate the quality of the logits at predicting the label. 156 | Args: 157 | logits: Logits tensor, float - [batch_size, NUM_CLASSES]. 158 | labels: Labels tensor, int32 - [batch_size], with values in the 159 | range [0, NUM_CLASSES). 160 | Returns: 161 | A scalar int32 tensor with the number of examples (out of batch_size) 162 | that were predicted correctly. 163 | """ 164 | with tf.variable_scope('accuracy') as scope: 165 | correct = tf.nn.in_top_k(logits, labels, 1) 166 | correct = tf.cast(correct, tf.float16) 167 | accuracy = tf.reduce_mean(correct) 168 | tf.summary.scalar(scope.name+'/accuracy', accuracy) 169 | return accuracy 170 | 171 | #%% 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/images/101.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/new_version/images/101.png -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/images/102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/new_version/images/102.png -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/images/103.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/01 cats vs dogs/new_version/images/103.png -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/images/readme: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/input_train_val_split.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 4 | # 5 | #The aim of this project is to use TensorFlow to process our own data. 6 | # - input_data.py: read in data and generate batches 7 | # - model: build the model architecture 8 | # - training: train 9 | 10 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 11 | # With current settings, 10000 traing steps needed 50 minutes on my laptop. 12 | 13 | 14 | # data: cats vs. dogs from Kaggle 15 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 16 | # data size: ~540M 17 | 18 | # How to run? 19 | # 1. run the training.py once 20 | # 2. call the run_training() in the console to train the model. 21 | 22 | # Note: 23 | # it is suggested to restart your kenel to train the model multiple times 24 | #(in order to clear all the variables in the memory) 25 | # Otherwise errors may occur: conv1/weights/biases already exist...... 26 | 27 | 28 | #%% 29 | 30 | import tensorflow as tf 31 | import numpy as np 32 | import os 33 | import math 34 | 35 | #%% 36 | 37 | # you need to change this to your data directory 38 | train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 39 | 40 | def get_files(file_dir, ratio): 41 | ''' 42 | Args: 43 | file_dir: file directory 44 | Returns: 45 | list of images and labels 46 | ''' 47 | cats = [] 48 | label_cats = [] 49 | dogs = [] 50 | label_dogs = [] 51 | for file in os.listdir(file_dir): 52 | name = file.split(sep='.') 53 | if name[0]=='cat': 54 | cats.append(file_dir + file) 55 | label_cats.append(0) 56 | else: 57 | dogs.append(file_dir + file) 58 | label_dogs.append(1) 59 | print('There are %d cats\nThere are %d dogs' %(len(cats), len(dogs))) 60 | 61 | image_list = np.hstack((cats, dogs)) 62 | label_list = np.hstack((label_cats, label_dogs)) 63 | 64 | temp = np.array([image_list, label_list]) 65 | temp = temp.transpose() 66 | np.random.shuffle(temp) 67 | 68 | all_image_list = temp[:, 0] 69 | all_label_list = temp[:, 1] 70 | 71 | n_sample = len(all_label_list) 72 | n_val = math.ceil(n_sample*ratio) # number of validation samples 73 | n_train = n_sample - n_val # number of trainning samples 74 | 75 | tra_images = all_image_list[0:n_train] 76 | tra_labels = all_label_list[0:n_train] 77 | tra_labels = [int(float(i)) for i in tra_labels] 78 | val_images = all_image_list[n_train:-1] 79 | val_labels = all_label_list[n_train:-1] 80 | val_labels = [int(float(i)) for i in val_labels] 81 | 82 | 83 | 84 | return tra_images,tra_labels,val_images,val_labels 85 | 86 | 87 | #%% 88 | 89 | def get_batch(image, label, image_W, image_H, batch_size, capacity): 90 | ''' 91 | Args: 92 | image: list type 93 | label: list type 94 | image_W: image width 95 | image_H: image height 96 | batch_size: batch size 97 | capacity: the maximum elements in queue 98 | Returns: 99 | image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32 100 | label_batch: 1D tensor [batch_size], dtype=tf.int32 101 | ''' 102 | 103 | image = tf.cast(image, tf.string) 104 | label = tf.cast(label, tf.int32) 105 | 106 | # make an input queue 107 | input_queue = tf.train.slice_input_producer([image, label]) 108 | 109 | label = input_queue[1] 110 | image_contents = tf.read_file(input_queue[0]) 111 | image = tf.image.decode_jpeg(image_contents, channels=3) 112 | 113 | ###################################### 114 | # data argumentation should go to here 115 | ###################################### 116 | 117 | image = tf.image.resize_image_with_crop_or_pad(image, image_W, image_H) 118 | # if you want to test the generated batches of images, you might want to comment the following line. 119 | 120 | # 如果想看到正常的图片,请注释掉111行(标准化)和 130行(image_batch = tf.cast(image_batch, tf.float32)) 121 | # 训练时,不要注释掉! 122 | image = tf.image.per_image_standardization(image) 123 | 124 | image_batch, label_batch = tf.train.batch([image, label], 125 | batch_size= batch_size, 126 | num_threads= 64, 127 | capacity = capacity) 128 | 129 | label_batch = tf.reshape(label_batch, [batch_size]) 130 | image_batch = tf.cast(image_batch, tf.float32) 131 | 132 | return image_batch, label_batch 133 | 134 | 135 | 136 | #%% TEST 137 | # To test the generated batches of images 138 | # When training the model, DO comment the following codes 139 | 140 | 141 | 142 | 143 | #import matplotlib.pyplot as plt 144 | # 145 | #BATCH_SIZE = 2 146 | #CAPACITY = 256 147 | #IMG_W = 208 148 | #IMG_H = 208 149 | # 150 | #train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 151 | #ratio = 0.2 152 | #tra_images, tra_labels, val_images, val_labels = get_files(train_dir, ratio) 153 | #tra_image_batch, tra_label_batch = get_batch(tra_images, tra_labels, IMG_W, IMG_H, BATCH_SIZE, CAPACITY) 154 | # 155 | # 156 | # 157 | #with tf.Session() as sess: 158 | # i = 0 159 | # coord = tf.train.Coordinator() 160 | # threads = tf.train.start_queue_runners(coord=coord) 161 | # 162 | # try: 163 | # while not coord.should_stop() and i<1: 164 | # 165 | # img, label = sess.run([tra_image_batch, tra_label_batch]) 166 | # 167 | # # just test one batch 168 | # for j in np.arange(BATCH_SIZE): 169 | # print('label: %d' %label[j]) 170 | # plt.imshow(img[j,:,:,:]) 171 | # plt.show() 172 | # i+=1 173 | # 174 | # except tf.errors.OutOfRangeError: 175 | # print('done!') 176 | # finally: 177 | # coord.request_stop() 178 | # coord.join(threads) 179 | 180 | 181 | #%% 182 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/model.py: -------------------------------------------------------------------------------- 1 | 2 | #By @Kevin Xu 3 | #kevin28520@gmail.com 4 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 5 | # 6 | 7 | #The aim of this project is to use TensorFlow to process our own data. 8 | # - input_data.py: read in data and generate batches 9 | # - model: build the model architecture 10 | # - training: train 11 | 12 | # data: cats vs. dogs from Kaggle 13 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 14 | # data size: ~540M 15 | 16 | # How to run? 17 | # 1. run the training.py once 18 | # 2. call the run_training() in the console to train the model. 19 | 20 | 21 | #%% 22 | 23 | import tensorflow as tf 24 | 25 | #%% 26 | def inference(images, batch_size, n_classes): 27 | '''Build the model 28 | Args: 29 | images: image batch, 4D tensor, tf.float32, [batch_size, width, height, channels] 30 | Returns: 31 | output tensor with the computed logits, float, [batch_size, n_classes] 32 | ''' 33 | #conv1, shape = [kernel size, kernel size, channels, kernel numbers] 34 | 35 | with tf.variable_scope('conv1') as scope: 36 | weights = tf.get_variable('weights', 37 | shape = [3,3,3, 16], 38 | dtype = tf.float32, 39 | initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) 40 | biases = tf.get_variable('biases', 41 | shape=[16], 42 | dtype=tf.float32, 43 | initializer=tf.constant_initializer(0.1)) 44 | conv = tf.nn.conv2d(images, weights, strides=[1,1,1,1], padding='SAME') 45 | pre_activation = tf.nn.bias_add(conv, biases) 46 | conv1 = tf.nn.relu(pre_activation, name= scope.name) 47 | 48 | #pool1 and norm1 49 | with tf.variable_scope('pooling1_lrn') as scope: 50 | pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1],strides=[1,2,2,1], 51 | padding='SAME', name='pooling1') 52 | norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, 53 | beta=0.75,name='norm1') 54 | 55 | #conv2 56 | with tf.variable_scope('conv2') as scope: 57 | weights = tf.get_variable('weights', 58 | shape=[3,3,16,16], 59 | dtype=tf.float32, 60 | initializer=tf.truncated_normal_initializer(stddev=0.1,dtype=tf.float32)) 61 | biases = tf.get_variable('biases', 62 | shape=[16], 63 | dtype=tf.float32, 64 | initializer=tf.constant_initializer(0.1)) 65 | conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1],padding='SAME') 66 | pre_activation = tf.nn.bias_add(conv, biases) 67 | conv2 = tf.nn.relu(pre_activation, name='conv2') 68 | 69 | 70 | #pool2 and norm2 71 | with tf.variable_scope('pooling2_lrn') as scope: 72 | norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0, 73 | beta=0.75,name='norm2') 74 | pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1], 75 | padding='SAME',name='pooling2') 76 | 77 | 78 | #local3 79 | with tf.variable_scope('local3') as scope: 80 | reshape = tf.reshape(pool2, shape=[batch_size, -1]) 81 | dim = reshape.get_shape()[1].value 82 | weights = tf.get_variable('weights', 83 | shape=[dim,128], 84 | dtype=tf.float32, 85 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 86 | biases = tf.get_variable('biases', 87 | shape=[128], 88 | dtype=tf.float32, 89 | initializer=tf.constant_initializer(0.1)) 90 | local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name) 91 | 92 | #local4 93 | with tf.variable_scope('local4') as scope: 94 | weights = tf.get_variable('weights', 95 | shape=[128,128], 96 | dtype=tf.float32, 97 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 98 | biases = tf.get_variable('biases', 99 | shape=[128], 100 | dtype=tf.float32, 101 | initializer=tf.constant_initializer(0.1)) 102 | local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4') 103 | 104 | 105 | # softmax 106 | with tf.variable_scope('softmax_linear') as scope: 107 | weights = tf.get_variable('softmax_linear', 108 | shape=[128, n_classes], 109 | dtype=tf.float32, 110 | initializer=tf.truncated_normal_initializer(stddev=0.005,dtype=tf.float32)) 111 | biases = tf.get_variable('biases', 112 | shape=[n_classes], 113 | dtype=tf.float32, 114 | initializer=tf.constant_initializer(0.1)) 115 | softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear') 116 | 117 | return softmax_linear 118 | 119 | #%% 120 | def losses(logits, labels): 121 | '''Compute loss from logits and labels 122 | Args: 123 | logits: logits tensor, float, [batch_size, n_classes] 124 | labels: label tensor, tf.int32, [batch_size] 125 | 126 | Returns: 127 | loss tensor of float type 128 | ''' 129 | with tf.variable_scope('loss') as scope: 130 | cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits\ 131 | (logits=logits, labels=labels, name='xentropy_per_example') 132 | loss = tf.reduce_mean(cross_entropy, name='loss') 133 | tf.summary.scalar(scope.name+'/loss', loss) 134 | return loss 135 | 136 | #%% 137 | def trainning(loss, learning_rate): 138 | '''Training ops, the Op returned by this function is what must be passed to 139 | 'sess.run()' call to cause the model to train. 140 | 141 | Args: 142 | loss: loss tensor, from losses() 143 | 144 | Returns: 145 | train_op: The op for trainning 146 | ''' 147 | with tf.name_scope('optimizer'): 148 | optimizer = tf.train.AdamOptimizer(learning_rate= learning_rate) 149 | global_step = tf.Variable(0, name='global_step', trainable=False) 150 | train_op = optimizer.minimize(loss, global_step= global_step) 151 | return train_op 152 | 153 | #%% 154 | def evaluation(logits, labels): 155 | """Evaluate the quality of the logits at predicting the label. 156 | Args: 157 | logits: Logits tensor, float - [batch_size, NUM_CLASSES]. 158 | labels: Labels tensor, int32 - [batch_size], with values in the 159 | range [0, NUM_CLASSES). 160 | Returns: 161 | A scalar int32 tensor with the number of examples (out of batch_size) 162 | that were predicted correctly. 163 | """ 164 | with tf.variable_scope('accuracy') as scope: 165 | correct = tf.nn.in_top_k(logits, labels, 1) 166 | correct = tf.cast(correct, tf.float16) 167 | accuracy = tf.reduce_mean(correct) 168 | tf.summary.scalar(scope.name+'/accuracy', accuracy) 169 | return accuracy 170 | 171 | #%% 172 | 173 | 174 | 175 | 176 | -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/readme.md: -------------------------------------------------------------------------------- 1 | # We are going to solve two problems here 2 | 1. How to do training and validataion at the same time with Tensorflow? (more details about this question: [here](http://stackoverflow.com/questions/41162955/tensorflow-queues-switching-between-train-and-validation-data)) 3 | 2. How to plot training and validation curvers on Tensorboard? 4 | 5 | # The method (details are in the CODE) 6 | 1. generate train and validation batch with two queues 7 | 2. fetch the contents of each queue independently with sess.run() 8 | 3. during training, use feed_dict to select which one to be pushed into the computational graph 9 | 10 | # Screenshots: 11 |  ![training](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/new_version/images/101.png?raw=true) 12 | 13 | 14 | ![result1](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/new_version/images/103.png?raw=true) 15 | 16 | 17 | ![result2](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/new_version/images/102.png?raw=true) 18 | -------------------------------------------------------------------------------- /01 cats vs dogs/new_version/train_and_val.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 4 | # 5 | #The aim of this project is to use TensorFlow to process our own data. 6 | # - input_data.py: read in data and generate batches 7 | # - model: build the model architecture 8 | # - training: train 9 | 10 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 11 | # With current settings, 10000 traing steps needed 50 minutes on my laptop. 12 | 13 | # data: cats vs. dogs from Kaggle 14 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 15 | # data size: ~540M 16 | 17 | # How to run? 18 | # 1. run the training.py once 19 | # 2. call the run_training() in the console to train the model. 20 | 21 | # Note: 22 | # it is suggested to restart your kenel to train the model multiple times 23 | #(in order to clear all the variables in the memory) 24 | # Otherwise errors may occur: conv1/weights/biases already exist...... 25 | 26 | 27 | #%% 28 | 29 | import os 30 | import numpy as np 31 | import tensorflow as tf 32 | import input_train_val_split 33 | import model 34 | 35 | #%% 36 | 37 | N_CLASSES = 2 38 | IMG_W = 208 # resize the image, if the input image is too large, training will be very slow. 39 | IMG_H = 208 40 | RATIO = 0.2 # take 20% of dataset as validation data 41 | BATCH_SIZE = 64 42 | CAPACITY = 2000 43 | MAX_STEP = 6000 # with current parameters, it is suggested to use MAX_STEP>10k 44 | learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001 45 | 46 | 47 | #%% 48 | def run_training(): 49 | 50 | # you need to change the directories to yours. 51 | train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 52 | logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' 53 | logs_val_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/val/' 54 | 55 | train, train_label, val, val_label = input_train_val_split.get_files(train_dir, RATIO) 56 | train_batch, train_label_batch = input_train_val_split.get_batch(train, 57 | train_label, 58 | IMG_W, 59 | IMG_H, 60 | BATCH_SIZE, 61 | CAPACITY) 62 | val_batch, val_label_batch = input_train_val_split.get_batch(val, 63 | val_label, 64 | IMG_W, 65 | IMG_H, 66 | BATCH_SIZE, 67 | CAPACITY) 68 | 69 | 70 | 71 | x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3]) 72 | y_ = tf.placeholder(tf.int32, shape=[BATCH_SIZE]) 73 | 74 | logits = model.inference(x, BATCH_SIZE, N_CLASSES) 75 | loss = model.losses(logits, y_) 76 | acc = model.evaluation(logits, y_) 77 | train_op = model.trainning(loss, learning_rate) 78 | 79 | 80 | 81 | with tf.Session() as sess: 82 | saver = tf.train.Saver() 83 | sess.run(tf.global_variables_initializer()) 84 | coord = tf.train.Coordinator() 85 | threads = tf.train.start_queue_runners(sess= sess, coord=coord) 86 | 87 | summary_op = tf.summary.merge_all() 88 | train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) 89 | val_writer = tf.summary.FileWriter(logs_val_dir, sess.graph) 90 | 91 | try: 92 | for step in np.arange(MAX_STEP): 93 | if coord.should_stop(): 94 | break 95 | 96 | tra_images,tra_labels = sess.run([train_batch, train_label_batch]) 97 | _, tra_loss, tra_acc = sess.run([train_op, loss, acc], 98 | feed_dict={x:tra_images, y_:tra_labels}) 99 | if step % 50 == 0: 100 | print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) 101 | summary_str = sess.run(summary_op, feed_dict={x:tra_images, y_:tra_labels}) 102 | train_writer.add_summary(summary_str, step) 103 | 104 | if step % 200 == 0 or (step + 1) == MAX_STEP: 105 | val_images, val_labels = sess.run([val_batch, val_label_batch]) 106 | val_loss, val_acc = sess.run([loss, acc], 107 | feed_dict={x:val_images, y_:val_labels}) 108 | print('** Step %d, val loss = %.2f, val accuracy = %.2f%% **' %(step, val_loss, val_acc*100.0)) 109 | summary_str = sess.run(summary_op, feed_dict={x:tra_images, y_:tra_labels}) 110 | val_writer.add_summary(summary_str, step) 111 | 112 | if step % 2000 == 0 or (step + 1) == MAX_STEP: 113 | checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') 114 | saver.save(sess, checkpoint_path, global_step=step) 115 | 116 | except tf.errors.OutOfRangeError: 117 | print('Done training -- epoch limit reached') 118 | finally: 119 | coord.request_stop() 120 | coord.join(threads) 121 | 122 | 123 | #%% Evaluate one image 124 | # when training, comment the following codes. 125 | 126 | 127 | #from PIL import Image 128 | #import matplotlib.pyplot as plt 129 | # 130 | #def get_one_image(train): 131 | # '''Randomly pick one image from training data 132 | # Return: ndarray 133 | # ''' 134 | # n = len(train) 135 | # ind = np.random.randint(0, n) 136 | # img_dir = train[ind] 137 | # 138 | # image = Image.open(img_dir) 139 | # plt.imshow(image) 140 | # image = image.resize([208, 208]) 141 | # image = np.array(image) 142 | # return image 143 | # 144 | #def evaluate_one_image(): 145 | # '''Test one image against the saved models and parameters 146 | # ''' 147 | # 148 | # # you need to change the directories to yours. 149 | # train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 150 | # train, train_label = input_data.get_files(train_dir) 151 | # image_array = get_one_image(train) 152 | # 153 | # with tf.Graph().as_default(): 154 | # BATCH_SIZE = 1 155 | # N_CLASSES = 2 156 | # 157 | # image = tf.cast(image_array, tf.float32) 158 | # image = tf.image.per_image_standardization(image) 159 | # image = tf.reshape(image, [1, 208, 208, 3]) 160 | # 161 | # logit = model.inference(image, BATCH_SIZE, N_CLASSES) 162 | # 163 | # logit = tf.nn.softmax(logit) 164 | # 165 | # x = tf.placeholder(tf.float32, shape=[208, 208, 3]) 166 | # 167 | # # you need to change the directories to yours. 168 | # logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' 169 | # 170 | # saver = tf.train.Saver() 171 | # 172 | # with tf.Session() as sess: 173 | # 174 | # print("Reading checkpoints...") 175 | # ckpt = tf.train.get_checkpoint_state(logs_train_dir) 176 | # if ckpt and ckpt.model_checkpoint_path: 177 | # global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 178 | # saver.restore(sess, ckpt.model_checkpoint_path) 179 | # print('Loading success, global_step is %s' % global_step) 180 | # else: 181 | # print('No checkpoint file found') 182 | # 183 | # prediction = sess.run(logit, feed_dict={x: image_array}) 184 | # max_index = np.argmax(prediction) 185 | # if max_index==0: 186 | # print('This is a cat with possibility %.6f' %prediction[:, 0]) 187 | # else: 188 | # print('This is a dog with possibility %.6f' %prediction[:, 1]) 189 | 190 | 191 | #%% 192 | 193 | 194 | 195 | 196 | 197 | -------------------------------------------------------------------------------- /01 cats vs dogs/training.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 4 | # 5 | #The aim of this project is to use TensorFlow to process our own data. 6 | # - input_data.py: read in data and generate batches 7 | # - model: build the model architecture 8 | # - training: train 9 | 10 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 11 | # With current settings, 10000 traing steps needed 50 minutes on my laptop. 12 | 13 | # data: cats vs. dogs from Kaggle 14 | # Download link: https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition/data 15 | # data size: ~540M 16 | 17 | # How to run? 18 | # 1. run the training.py once 19 | # 2. call the run_training() in the console to train the model. 20 | 21 | # Note: 22 | # it is suggested to restart your kenel to train the model multiple times 23 | #(in order to clear all the variables in the memory) 24 | # Otherwise errors may occur: conv1/weights/biases already exist...... 25 | 26 | 27 | #%% 28 | 29 | import os 30 | import numpy as np 31 | import tensorflow as tf 32 | import input_data 33 | import model 34 | 35 | #%% 36 | 37 | N_CLASSES = 2 38 | IMG_W = 208 # resize the image, if the input image is too large, training will be very slow. 39 | IMG_H = 208 40 | BATCH_SIZE = 16 41 | CAPACITY = 2000 42 | MAX_STEP = 10000 # with current parameters, it is suggested to use MAX_STEP>10k 43 | learning_rate = 0.0001 # with current parameters, it is suggested to use learning rate<0.0001 44 | 45 | 46 | #%% 47 | def run_training(): 48 | 49 | # you need to change the directories to yours. 50 | train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 51 | logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' 52 | 53 | train, train_label = input_data.get_files(train_dir) 54 | 55 | train_batch, train_label_batch = input_data.get_batch(train, 56 | train_label, 57 | IMG_W, 58 | IMG_H, 59 | BATCH_SIZE, 60 | CAPACITY) 61 | train_logits = model.inference(train_batch, BATCH_SIZE, N_CLASSES) 62 | train_loss = model.losses(train_logits, train_label_batch) 63 | train_op = model.trainning(train_loss, learning_rate) 64 | train__acc = model.evaluation(train_logits, train_label_batch) 65 | 66 | summary_op = tf.summary.merge_all() 67 | sess = tf.Session() 68 | train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph) 69 | saver = tf.train.Saver() 70 | 71 | sess.run(tf.global_variables_initializer()) 72 | coord = tf.train.Coordinator() 73 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 74 | 75 | try: 76 | for step in np.arange(MAX_STEP): 77 | if coord.should_stop(): 78 | break 79 | _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc]) 80 | 81 | if step % 50 == 0: 82 | print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %(step, tra_loss, tra_acc*100.0)) 83 | summary_str = sess.run(summary_op) 84 | train_writer.add_summary(summary_str, step) 85 | 86 | if step % 2000 == 0 or (step + 1) == MAX_STEP: 87 | checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt') 88 | saver.save(sess, checkpoint_path, global_step=step) 89 | 90 | except tf.errors.OutOfRangeError: 91 | print('Done training -- epoch limit reached') 92 | finally: 93 | coord.request_stop() 94 | 95 | coord.join(threads) 96 | sess.close() 97 | 98 | 99 | #%% Evaluate one image 100 | # when training, comment the following codes. 101 | 102 | 103 | #from PIL import Image 104 | #import matplotlib.pyplot as plt 105 | # 106 | #def get_one_image(train): 107 | # '''Randomly pick one image from training data 108 | # Return: ndarray 109 | # ''' 110 | # n = len(train) 111 | # ind = np.random.randint(0, n) 112 | # img_dir = train[ind] 113 | # 114 | # image = Image.open(img_dir) 115 | # plt.imshow(image) 116 | # image = image.resize([208, 208]) 117 | # image = np.array(image) 118 | # return image 119 | # 120 | #def evaluate_one_image(): 121 | # '''Test one image against the saved models and parameters 122 | # ''' 123 | # 124 | # # you need to change the directories to yours. 125 | # train_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/train/' 126 | # train, train_label = input_data.get_files(train_dir) 127 | # image_array = get_one_image(train) 128 | # 129 | # with tf.Graph().as_default(): 130 | # BATCH_SIZE = 1 131 | # N_CLASSES = 2 132 | # 133 | # image = tf.cast(image_array, tf.float32) 134 | # image = tf.image.per_image_standardization(image) 135 | # image = tf.reshape(image, [1, 208, 208, 3]) 136 | # logit = model.inference(image, BATCH_SIZE, N_CLASSES) 137 | # 138 | # logit = tf.nn.softmax(logit) 139 | # 140 | # x = tf.placeholder(tf.float32, shape=[208, 208, 3]) 141 | # 142 | # # you need to change the directories to yours. 143 | # logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' 144 | # 145 | # saver = tf.train.Saver() 146 | # 147 | # with tf.Session() as sess: 148 | # 149 | # print("Reading checkpoints...") 150 | # ckpt = tf.train.get_checkpoint_state(logs_train_dir) 151 | # if ckpt and ckpt.model_checkpoint_path: 152 | # global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 153 | # saver.restore(sess, ckpt.model_checkpoint_path) 154 | # print('Loading success, global_step is %s' % global_step) 155 | # else: 156 | # print('No checkpoint file found') 157 | # 158 | # prediction = sess.run(logit, feed_dict={x: image_array}) 159 | # max_index = np.argmax(prediction) 160 | # if max_index==0: 161 | # print('This is a cat with possibility %.6f' %prediction[:, 0]) 162 | # else: 163 | # print('This is a dog with possibility %.6f' %prediction[:, 1]) 164 | # 165 | def evaluate_all_image(): 166 | ''' 167 | Test all image against the saved models and parameters. 168 | Return global accuracy of test_image_set 169 | ############################################## 170 | ##Notice that test image must has label to compare the prediction and real 171 | ############################################## 172 | ''' 173 | # you need to change the directories to yours. 174 | test_dir = '/home/kevin/tensorflow/cats_vs_dogs/data/test/' 175 | N_CLASSES = 2 176 | print('-------------------------') 177 | test, test_label = input_data.get_files(test_dir) 178 | BATCH_SIZE = len(test) 179 | print('There are %d test images totally..' % BATCH_SIZE) 180 | print('-------------------------') 181 | test_batch, test_label_batch = input_data.get_batch(test, 182 | test_label, 183 | IMG_W, 184 | IMG_H, 185 | BATCH_SIZE, 186 | CAPACITY) 187 | 188 | logits = model.inference(test_batch, BATCH_SIZE, N_CLASSES) 189 | testloss = model.losses(logits, test_label_batch) 190 | testacc = model.evaluation(logits, test_label_batch) 191 | 192 | logs_train_dir = '/home/kevin/tensorflow/cats_vs_dogs/logs/train/' 193 | saver = tf.train.Saver() 194 | 195 | with tf.Session() as sess: 196 | print("Reading checkpoints...") 197 | ckpt = tf.train.get_checkpoint_state(logs_train_dir) 198 | if ckpt and ckpt.model_checkpoint_path: 199 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 200 | saver.restore(sess, ckpt.model_checkpoint_path) 201 | print('Loading success, global_step is %s' % global_step) 202 | else: 203 | print('No checkpoint file found') 204 | print('-------------------------') 205 | coord = tf.train.Coordinator() 206 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 207 | test_loss,test_acc = sess.run([testloss,testacc]) 208 | print('The model\'s loss is %.2f' %test_loss) 209 | correct = int(BATCH_SIZE*test_acc) 210 | print('Correct : %d' % correct) 211 | print('Wrong : %d' % (BATCH_SIZE - correct)) 212 | print('The accuracy in test images are %.2f%%' %(test_acc*100.0)) 213 | coord.request_stop() 214 | coord.join(threads) 215 | sess.close() 216 | 217 | #%% 218 | 219 | 220 | 221 | 222 | 223 | -------------------------------------------------------------------------------- /02 CIFAR10/README.md: -------------------------------------------------------------------------------- 1 | # How to use TensorFlow to process our own data? 2 | - The CIFAR10 dataset (binary version) is used in this code. 3 | 4 | # The data 5 | - download: https://www.cs.toronto.edu/~kriz/cifar.html 6 | 7 | # The result: 8 | - I used 10k training steps, learning rate is 0.05, the performance on test dataset is 78.3%. While for the example on TensorFlow website, they used 100k training steps, got 83% on the test dataset. 9 | More can be done to improve this model. 10 | 11 | # Screenshot: 12 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/02%20CIFAR10/images/001.JPG) 13 | 14 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/02%20CIFAR10/images/002.JPG) 15 | 16 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/02%20CIFAR10/images/003.JPG) 17 | 18 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/02%20CIFAR10/images/004.JPG) 19 | 20 | 21 | -------------------------------------------------------------------------------- /02 CIFAR10/cifar10.py: -------------------------------------------------------------------------------- 1 | # 11.08 2017 更新 2 | # 最近入驻了网易云课堂讲师,我的第一门课《使用亚马逊云计算训练深度学习模型》。 3 | # 有兴趣的同学可以学习交流。 4 | # * 我的网易云课堂主页: http://study.163.com/provider/400000000275062/index.htm 5 | 6 | 7 | #Chinese weibo: http://bit.ly/2nAmOcO 8 | 9 | 10 | #The aim of this project is to use TensorFlow to process our own data. 11 | # - cifar10_input.py: read in data and generate batches 12 | # - cifar10.py: build the model architecture, train, evaluate 13 | 14 | 15 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 16 | # I didn't use data argumentation, I spent less than 30 mins with 10K steps. 17 | 18 | 19 | # data: cifar10 binary version 20 | # https://www.cs.toronto.edu/~kriz/cifar.html 21 | # data size: ~184M 22 | 23 | # How to run? 24 | # 0. you need to change the data directory 25 | # 1. run cifar10.py 26 | # 2. call train() in the console to train the model 27 | # 3. call evaluate() in the console to test on the test data 28 | 29 | # Note: 30 | # it is suggested to restart your kenel to train the model multiple times 31 | # (in order to clear all the variables in the memory) 32 | # Otherwise errors may occur: conv1/weights/biases already exist...... 33 | 34 | 35 | #%% 36 | 37 | import os 38 | import os.path 39 | import math 40 | 41 | import numpy as np 42 | import tensorflow as tf 43 | 44 | import cifar10_input 45 | 46 | #%% 47 | 48 | BATCH_SIZE = 128 49 | learning_rate = 0.05 50 | MAX_STEP = 10000 # with this setting, it took less than 30 mins on my laptop to train. 51 | 52 | 53 | #%% 54 | 55 | def inference(images): 56 | ''' 57 | Args: 58 | images: 4D tensor [batch_size, img_width, img_height, img_channel] 59 | Notes: 60 | In each conv layer, the kernel size is: 61 | [kernel_size, kernel_size, number of input channels, number of output channels]. 62 | number of input channels are from previuous layer, if previous layer is THE input 63 | layer, number of input channels should be image's channels. 64 | 65 | 66 | ''' 67 | #conv1, [5, 5, 3, 96], The first two dimensions are the patch size, 68 | #the next is the number of input channels, 69 | #the last is the number of output channels 70 | with tf.variable_scope('conv1') as scope: 71 | weights = tf.get_variable('weights', 72 | shape = [3, 3, 3, 96], 73 | dtype = tf.float32, 74 | initializer=tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32)) 75 | biases = tf.get_variable('biases', 76 | shape=[96], 77 | dtype=tf.float32, 78 | initializer=tf.constant_initializer(0.0)) 79 | conv = tf.nn.conv2d(images, weights, strides=[1,1,1,1], padding='SAME') 80 | pre_activation = tf.nn.bias_add(conv, biases) 81 | conv1 = tf.nn.relu(pre_activation, name= scope.name) 82 | 83 | 84 | #pool1 and norm1 85 | with tf.variable_scope('pooling1_lrn') as scope: 86 | pool1 = tf.nn.max_pool(conv1, ksize=[1,3,3,1],strides=[1,2,2,1], 87 | padding='SAME', name='pooling1') 88 | norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, 89 | beta=0.75, name='norm1') 90 | 91 | 92 | #conv2 93 | with tf.variable_scope('conv2') as scope: 94 | weights = tf.get_variable('weights', 95 | shape=[3,3,96, 64], 96 | dtype=tf.float32, 97 | initializer=tf.truncated_normal_initializer(stddev=0.05,dtype=tf.float32)) 98 | biases = tf.get_variable('biases', 99 | shape=[64], 100 | dtype=tf.float32, 101 | initializer=tf.constant_initializer(0.1)) 102 | conv = tf.nn.conv2d(norm1, weights, strides=[1,1,1,1],padding='SAME') 103 | pre_activation = tf.nn.bias_add(conv, biases) 104 | conv2 = tf.nn.relu(pre_activation, name='conv2') 105 | 106 | 107 | #pool2 and norm2 108 | with tf.variable_scope('pooling2_lrn') as scope: 109 | norm2 = tf.nn.lrn(conv2, depth_radius=4, bias=1.0, alpha=0.001/9.0, 110 | beta=0.75,name='norm2') 111 | pool2 = tf.nn.max_pool(norm2, ksize=[1,3,3,1], strides=[1,1,1,1], 112 | padding='SAME',name='pooling2') 113 | 114 | 115 | #local3 116 | with tf.variable_scope('local3') as scope: 117 | reshape = tf.reshape(pool2, shape=[BATCH_SIZE, -1]) 118 | dim = reshape.get_shape()[1].value 119 | weights = tf.get_variable('weights', 120 | shape=[dim,384], 121 | dtype=tf.float32, 122 | initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) 123 | biases = tf.get_variable('biases', 124 | shape=[384], 125 | dtype=tf.float32, 126 | initializer=tf.constant_initializer(0.1)) 127 | local3 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name=scope.name) 128 | 129 | 130 | #local4 131 | with tf.variable_scope('local4') as scope: 132 | weights = tf.get_variable('weights', 133 | shape=[384,192], 134 | dtype=tf.float32, 135 | initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) 136 | biases = tf.get_variable('biases', 137 | shape=[192], 138 | dtype=tf.float32, 139 | initializer=tf.constant_initializer(0.1)) 140 | local4 = tf.nn.relu(tf.matmul(local3, weights) + biases, name='local4') 141 | 142 | 143 | # softmax 144 | with tf.variable_scope('softmax_linear') as scope: 145 | weights = tf.get_variable('softmax_linear', 146 | shape=[192, 10], 147 | dtype=tf.float32, 148 | initializer=tf.truncated_normal_initializer(stddev=0.004,dtype=tf.float32)) 149 | biases = tf.get_variable('biases', 150 | shape=[10], 151 | dtype=tf.float32, 152 | initializer=tf.constant_initializer(0.1)) 153 | softmax_linear = tf.add(tf.matmul(local4, weights), biases, name='softmax_linear') 154 | 155 | return softmax_linear 156 | 157 | #%% 158 | 159 | def losses(logits, labels): 160 | with tf.variable_scope('loss') as scope: 161 | 162 | labels = tf.cast(labels, tf.int64) 163 | 164 | # to use this loss fuction, one-hot encoding is needed! 165 | cross_entropy = tf.nn.softmax_cross_entropy_with_logits\ 166 | (logits=logits, labels=labels, name='xentropy_per_example') 167 | 168 | 169 | # cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits\ 170 | # (logits=logits, labels=labels, name='xentropy_per_example') 171 | 172 | loss = tf.reduce_mean(cross_entropy, name='loss') 173 | tf.summary.scalar(scope.name+'/loss', loss) 174 | 175 | return loss 176 | 177 | 178 | #%% Train the model on the training data 179 | # you need to change the training data directory below 180 | 181 | 182 | 183 | def train(): 184 | 185 | my_global_step = tf.Variable(0, name='global_step', trainable=False) 186 | 187 | 188 | data_dir = '/home/kevin/tensorflow/CIFAR10/data/cifar-10-batches-bin/' 189 | log_dir = '/home/kevin/tensorflow/CIFAR10/logs234/' 190 | 191 | images, labels = cifar10_input.read_cifar10(data_dir=data_dir, 192 | is_train=True, 193 | batch_size= BATCH_SIZE, 194 | shuffle=True) 195 | logits = inference(images) 196 | 197 | loss = losses(logits, labels) 198 | 199 | 200 | optimizer = tf.train.GradientDescentOptimizer(learning_rate) 201 | train_op = optimizer.minimize(loss, global_step= my_global_step) 202 | 203 | saver = tf.train.Saver(tf.global_variables()) 204 | summary_op = tf.summary.merge_all() 205 | 206 | 207 | 208 | init = tf.global_variables_initializer() 209 | sess = tf.Session() 210 | sess.run(init) 211 | 212 | coord = tf.train.Coordinator() 213 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 214 | 215 | summary_writer = tf.summary.FileWriter(log_dir, sess.graph) 216 | 217 | try: 218 | for step in np.arange(MAX_STEP): 219 | if coord.should_stop(): 220 | break 221 | _, loss_value = sess.run([train_op, loss]) 222 | 223 | if step % 50 == 0: 224 | print ('Step: %d, loss: %.4f' % (step, loss_value)) 225 | 226 | if step % 100 == 0: 227 | summary_str = sess.run(summary_op) 228 | summary_writer.add_summary(summary_str, step) 229 | 230 | if step % 2000 == 0 or (step + 1) == MAX_STEP: 231 | checkpoint_path = os.path.join(log_dir, 'model.ckpt') 232 | saver.save(sess, checkpoint_path, global_step=step) 233 | 234 | except tf.errors.OutOfRangeError: 235 | print('Done training -- epoch limit reached') 236 | finally: 237 | coord.request_stop() 238 | 239 | coord.join(threads) 240 | sess.close() 241 | 242 | 243 | 244 | 245 | 246 | #%% To test the model on the test data 247 | 248 | 249 | 250 | 251 | def evaluate(): 252 | with tf.Graph().as_default(): 253 | 254 | log_dir = '/home/kevin/tensorflow/CIFAR10/logs10000/' 255 | test_dir = '/home/kevin/tensorflow/CIFAR10/data/cifar-10-batches-bin/' 256 | n_test = 10000 257 | 258 | 259 | # reading test data 260 | images, labels = cifar10_input.read_cifar10(data_dir=test_dir, 261 | is_train=False, 262 | batch_size= BATCH_SIZE, 263 | shuffle=False) 264 | 265 | logits = inference(images) 266 | top_k_op = tf.nn.in_top_k(logits, labels, 1) 267 | saver = tf.train.Saver(tf.global_variables()) 268 | 269 | with tf.Session() as sess: 270 | 271 | print("Reading checkpoints...") 272 | ckpt = tf.train.get_checkpoint_state(log_dir) 273 | if ckpt and ckpt.model_checkpoint_path: 274 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 275 | saver.restore(sess, ckpt.model_checkpoint_path) 276 | print('Loading success, global_step is %s' % global_step) 277 | else: 278 | print('No checkpoint file found') 279 | return 280 | 281 | coord = tf.train.Coordinator() 282 | threads = tf.train.start_queue_runners(sess = sess, coord = coord) 283 | 284 | try: 285 | num_iter = int(math.ceil(n_test / BATCH_SIZE)) 286 | true_count = 0 287 | total_sample_count = num_iter * BATCH_SIZE 288 | step = 0 289 | 290 | while step < num_iter and not coord.should_stop(): 291 | predictions = sess.run([top_k_op]) 292 | true_count += np.sum(predictions) 293 | step += 1 294 | precision = true_count / total_sample_count 295 | print('precision = %.3f' % precision) 296 | except Exception as e: 297 | coord.request_stop(e) 298 | finally: 299 | coord.request_stop() 300 | coord.join(threads) 301 | 302 | #%% 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | -------------------------------------------------------------------------------- /02 CIFAR10/cifar10_input.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | #Youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 4 | #Chinese weibo: http://bit.ly/2nAmOcO 5 | 6 | 7 | #The aim of this project is to use TensorFlow to process our own data. 8 | # - cifar10_input.py: read in data and generate batches 9 | # - cifar10.py: build the model architecture, train, evaluate 10 | 11 | 12 | # I used Ubuntu with Python 3.5, TensorFlow 1.0*, other OS should also be good. 13 | # I didn't use data argumentation, I spent less than 30 mins with 10K steps. 14 | 15 | 16 | # data: cifar10 binary version 17 | # https://www.cs.toronto.edu/~kriz/cifar.html 18 | # data size: ~184M 19 | 20 | # How to run? 21 | # 0. you need to change the data directory 22 | # 1. run cifar10.py 23 | # 2. call train() in the console to train the model 24 | # 3. call evaluate() in the console to test on the test data 25 | 26 | # Note: 27 | # it is suggested to restart your kenel to train the model multiple times 28 | # (in order to clear all the variables in the memory) 29 | # Otherwise errors may occur: conv1/weights/biases already exist...... 30 | 31 | #%% 32 | 33 | import tensorflow as tf 34 | import numpy as np 35 | import os 36 | 37 | #%% Reading data 38 | 39 | def read_cifar10(data_dir, is_train, batch_size, shuffle): 40 | """Read CIFAR10 41 | 42 | Args: 43 | data_dir: the directory of CIFAR10 44 | is_train: boolen 45 | batch_size: 46 | shuffle: 47 | Returns: 48 | label: 1D tensor, tf.int32 49 | image: 4D tensor, [batch_size, height, width, 3], tf.float32 50 | 51 | """ 52 | img_width = 32 53 | img_height = 32 54 | img_depth = 3 55 | label_bytes = 1 56 | image_bytes = img_width*img_height*img_depth 57 | 58 | 59 | with tf.name_scope('input'): 60 | 61 | if is_train: 62 | filenames = [os.path.join(data_dir, 'data_batch_%d.bin' %ii) 63 | for ii in np.arange(1, 6)] 64 | else: 65 | filenames = [os.path.join(data_dir, 'test_batch.bin')] 66 | 67 | filename_queue = tf.train.string_input_producer(filenames) 68 | 69 | reader = tf.FixedLengthRecordReader(label_bytes + image_bytes) 70 | 71 | key, value = reader.read(filename_queue) 72 | 73 | record_bytes = tf.decode_raw(value, tf.uint8) 74 | 75 | label = tf.slice(record_bytes, [0], [label_bytes]) 76 | label = tf.cast(label, tf.int32) 77 | 78 | image_raw = tf.slice(record_bytes, [label_bytes], [image_bytes]) 79 | image_raw = tf.reshape(image_raw, [img_depth, img_height, img_width]) 80 | image = tf.transpose(image_raw, (1,2,0)) # convert from D/H/W to H/W/D 81 | image = tf.cast(image, tf.float32) 82 | 83 | 84 | # # data argumentation 85 | 86 | # image = tf.random_crop(image, [24, 24, 3])# randomly crop the image size to 24 x 24 87 | # image = tf.image.random_flip_left_right(image) 88 | # image = tf.image.random_brightness(image, max_delta=63) 89 | # image = tf.image.random_contrast(image,lower=0.2,upper=1.8) 90 | 91 | 92 | 93 | image = tf.image.per_image_standardization(image) #substract off the mean and divide by the variance 94 | 95 | 96 | if shuffle: 97 | images, label_batch = tf.train.shuffle_batch( 98 | [image, label], 99 | batch_size = batch_size, 100 | num_threads= 16, 101 | capacity = 2000, 102 | min_after_dequeue = 1500) 103 | else: 104 | images, label_batch = tf.train.batch( 105 | [image, label], 106 | batch_size = batch_size, 107 | num_threads = 16, 108 | capacity= 2000) 109 | 110 | 111 | # return images, tf.reshape(label_batch, [batch_size]) 112 | 113 | 114 | 115 | 116 | 117 | 118 | ## ONE-HOT 119 | n_classes = 10 120 | label_batch = tf.one_hot(label_batch, depth= n_classes) 121 | 122 | 123 | return images, tf.reshape(label_batch, [batch_size, n_classes]) 124 | 125 | 126 | 127 | 128 | 129 | #%% TEST 130 | # To test the generated batches of images 131 | # When training the model, DO comment the following codes 132 | 133 | 134 | 135 | #import matplotlib.pyplot as plt 136 | # 137 | #data_dir = '/home/kevin/tensorflow/CIFAR10/data/cifar-10-batches-bin/' 138 | #BATCH_SIZE = 10 139 | #image_batch, label_batch = read_cifar10(data_dir, 140 | # is_train=True, 141 | # batch_size=BATCH_SIZE, 142 | # shuffle=True) 143 | # 144 | #with tf.Session() as sess: 145 | # i = 0 146 | # coord = tf.train.Coordinator() 147 | # threads = tf.train.start_queue_runners(coord=coord) 148 | # 149 | # try: 150 | # while not coord.should_stop() and i<1: 151 | # 152 | # img, label = sess.run([image_batch, label_batch]) 153 | # 154 | # # just test one batch 155 | # for j in np.arange(BATCH_SIZE): 156 | # print('label: %d' %label[j]) 157 | # plt.imshow(img[j,:,:,:]) 158 | # plt.show() 159 | # i+=1 160 | # 161 | # except tf.errors.OutOfRangeError: 162 | # print('done!') 163 | # finally: 164 | # coord.request_stop() 165 | # coord.join(threads) 166 | 167 | 168 | 169 | #%% 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /02 CIFAR10/data/put the data in this folder: -------------------------------------------------------------------------------- 1 | Put the data in this folder. 2 | -------------------------------------------------------------------------------- /02 CIFAR10/images/001.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/02 CIFAR10/images/001.JPG -------------------------------------------------------------------------------- /02 CIFAR10/images/002.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/02 CIFAR10/images/002.JPG -------------------------------------------------------------------------------- /02 CIFAR10/images/003.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/02 CIFAR10/images/003.JPG -------------------------------------------------------------------------------- /02 CIFAR10/images/004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/02 CIFAR10/images/004.JPG -------------------------------------------------------------------------------- /03 TFRecord/Data/README.md: -------------------------------------------------------------------------------- 1 | Data directory 2 | -------------------------------------------------------------------------------- /03 TFRecord/Images/001.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/001.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/002.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/002.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/003.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/003.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/004.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/005.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/005.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/006.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/03 TFRecord/Images/006.JPG -------------------------------------------------------------------------------- /03 TFRecord/Images/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /03 TFRecord/README.md: -------------------------------------------------------------------------------- 1 | # 11.08 2017 更新 2 | 最近入驻了网易云课堂讲师,我的第一门课《使用亚马逊云计算训练深度学习模型》。 3 | 有兴趣的同学可以学习交流。 4 | * 我的网易云课堂主页: http://study.163.com/provider/400000000275062/index.htm 5 | --- 6 | 7 | # How to transform our data into TFRecord? 8 | 1. transform data into TFRecord format 9 | 2. read TFRecord file 10 | 3. generate batch 11 | 12 | 13 | # Data 14 | - the dataset used here is notMNIST 15 | - download link: http://yaroslavvb.com/upload/notMNIST/ 16 | - data introduction: http://yaroslavvb.blogspot.ca/2011/09/notmnist-dataset.html 17 | 18 | # Screenshots 19 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/03%20TFRecord/Images/001.JPG) 20 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/03%20TFRecord/Images/002.JPG) 21 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/03%20TFRecord/Images/003.JPG) 22 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/03%20TFRecord/Images/004.JPG) 23 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/03%20TFRecord/Images/005.JPG) 24 | 25 | -------------------------------------------------------------------------------- /03 TFRecord/notMNIST_input.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | # My youtube: https://www.youtube.com/channel/UCVCSn4qQXTDAtGWpWAe4Plw 4 | # My Chinese weibo (微博): http://weibo.com/3983872447/profile 5 | # My Chinese youku (优酷): http://i.youku.com/deeplearning101 6 | # (深度学习QQ群): 153032765 (人满), 请加2群:462661267 7 | 8 | #The aim of this project is to use TensorFlow to transform our own data into TFRecord format. 9 | 10 | 11 | # I used Windows with Python 3.5, TensorFlow 1.0*, other OS should also be good. 12 | # I used the Spyder IDE. 13 | 14 | 15 | # data: notMNIST 16 | # http://yaroslavvb.blogspot.ca/2011/09/notmnist-dataset.html 17 | # http://yaroslavvb.com/upload/notMNIST/ 18 | 19 | 20 | 21 | 22 | #%% 23 | 24 | import tensorflow as tf 25 | import numpy as np 26 | import os 27 | import matplotlib.pyplot as plt 28 | import skimage.io as io 29 | 30 | 31 | #%% 32 | 33 | def get_file(file_dir): 34 | '''Get full image directory and corresponding labels 35 | Args: 36 | file_dir: file directory 37 | Returns: 38 | images: image directories, list, string 39 | labels: label, list, int 40 | ''' 41 | 42 | images = [] 43 | temp = [] 44 | for root, sub_folders, files in os.walk(file_dir): 45 | # image directories 46 | for name in files: 47 | images.append(os.path.join(root, name)) 48 | # get 10 sub-folder names 49 | for name in sub_folders: 50 | temp.append(os.path.join(root, name)) 51 | 52 | # assign 10 labels based on the folder names 53 | labels = [] 54 | for one_folder in temp: 55 | n_img = len(os.listdir(one_folder)) 56 | letter = one_folder.split('/')[-1] 57 | 58 | if letter=='A': 59 | labels = np.append(labels, n_img*[1]) 60 | elif letter=='B': 61 | labels = np.append(labels, n_img*[2]) 62 | elif letter=='C': 63 | labels = np.append(labels, n_img*[3]) 64 | elif letter=='D': 65 | labels = np.append(labels, n_img*[4]) 66 | elif letter=='E': 67 | labels = np.append(labels, n_img*[5]) 68 | elif letter=='F': 69 | labels = np.append(labels, n_img*[6]) 70 | elif letter=='G': 71 | labels = np.append(labels, n_img*[7]) 72 | elif letter=='H': 73 | labels = np.append(labels, n_img*[8]) 74 | elif letter=='I': 75 | labels = np.append(labels, n_img*[9]) 76 | else: 77 | labels = np.append(labels, n_img*[10]) 78 | 79 | # shuffle 80 | temp = np.array([images, labels]) 81 | temp = temp.transpose() 82 | np.random.shuffle(temp) 83 | 84 | image_list = list(temp[:, 0]) 85 | label_list = list(temp[:, 1]) 86 | label_list = [int(float(i)) for i in label_list] 87 | 88 | return image_list, label_list 89 | 90 | 91 | #%% 92 | 93 | def int64_feature(value): 94 | """Wrapper for inserting int64 features into Example proto.""" 95 | if not isinstance(value, list): 96 | value = [value] 97 | return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) 98 | 99 | def bytes_feature(value): 100 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 101 | 102 | #%% 103 | 104 | def convert_to_tfrecord(images, labels, save_dir, name): 105 | '''convert all images and labels to one tfrecord file. 106 | Args: 107 | images: list of image directories, string type 108 | labels: list of labels, int type 109 | save_dir: the directory to save tfrecord file, e.g.: '/home/folder1/' 110 | name: the name of tfrecord file, string type, e.g.: 'train' 111 | Return: 112 | no return 113 | Note: 114 | converting needs some time, be patient... 115 | ''' 116 | 117 | filename = os.path.join(save_dir, name + '.tfrecords') 118 | n_samples = len(labels) 119 | 120 | if np.shape(images)[0] != n_samples: 121 | raise ValueError('Images size %d does not match label size %d.' %(images.shape[0], n_samples)) 122 | 123 | 124 | 125 | # wait some time here, transforming need some time based on the size of your data. 126 | writer = tf.python_io.TFRecordWriter(filename) 127 | print('\nTransform start......') 128 | for i in np.arange(0, n_samples): 129 | try: 130 | image = io.imread(images[i]) # type(image) must be array! 131 | image_raw = image.tostring() 132 | label = int(labels[i]) 133 | example = tf.train.Example(features=tf.train.Features(feature={ 134 | 'label':int64_feature(label), 135 | 'image_raw': bytes_feature(image_raw)})) 136 | writer.write(example.SerializeToString()) 137 | except IOError as e: 138 | print('Could not read:', images[i]) 139 | print('error: %s' %e) 140 | print('Skip it!\n') 141 | writer.close() 142 | print('Transform done!') 143 | 144 | 145 | #%% 146 | 147 | def read_and_decode(tfrecords_file, batch_size): 148 | '''read and decode tfrecord file, generate (image, label) batches 149 | Args: 150 | tfrecords_file: the directory of tfrecord file 151 | batch_size: number of images in each batch 152 | Returns: 153 | image: 4D tensor - [batch_size, width, height, channel] 154 | label: 1D tensor - [batch_size] 155 | ''' 156 | # make an input queue from the tfrecord file 157 | filename_queue = tf.train.string_input_producer([tfrecords_file]) 158 | 159 | reader = tf.TFRecordReader() 160 | _, serialized_example = reader.read(filename_queue) 161 | img_features = tf.parse_single_example( 162 | serialized_example, 163 | features={ 164 | 'label': tf.FixedLenFeature([], tf.int64), 165 | 'image_raw': tf.FixedLenFeature([], tf.string), 166 | }) 167 | image = tf.decode_raw(img_features['image_raw'], tf.uint8) 168 | 169 | ########################################################## 170 | # you can put data augmentation here, I didn't use it 171 | ########################################################## 172 | # all the images of notMNIST are 28*28, you need to change the image size if you use other dataset. 173 | 174 | image = tf.reshape(image, [28, 28]) 175 | label = tf.cast(img_features['label'], tf.int32) 176 | image_batch, label_batch = tf.train.batch([image, label], 177 | batch_size= batch_size, 178 | num_threads= 64, 179 | capacity = 2000) 180 | return image_batch, tf.reshape(label_batch, [batch_size]) 181 | 182 | 183 | 184 | 185 | #%% Convert data to TFRecord 186 | 187 | test_dir = 'C://Users//Windows7//Documents//Python Scripts//notMNIST//notMNIST_small//' 188 | save_dir = 'C://Users//Windows7//Documents//Python Scripts//notMNIST//' 189 | BATCH_SIZE = 25 190 | 191 | 192 | #Convert test data: you just need to run it ONCE ! 193 | name_test = 'test' 194 | images, labels = get_file(test_dir) 195 | convert_to_tfrecord(images, labels, save_dir, name_test) 196 | 197 | 198 | #%% TO test train.tfrecord file 199 | 200 | def plot_images(images, labels): 201 | '''plot one batch size 202 | ''' 203 | for i in np.arange(0, BATCH_SIZE): 204 | plt.subplot(5, 5, i + 1) 205 | plt.axis('off') 206 | plt.title(chr(ord('A') + labels[i] - 1), fontsize = 14) 207 | plt.subplots_adjust(top=1.5) 208 | plt.imshow(images[i]) 209 | plt.show() 210 | 211 | 212 | tfrecords_file = 'C://Users//Windows7//Documents//Python Scripts//notMNIST//test.tfrecords' 213 | image_batch, label_batch = read_and_decode(tfrecords_file, batch_size=BATCH_SIZE) 214 | 215 | with tf.Session() as sess: 216 | 217 | i = 0 218 | coord = tf.train.Coordinator() 219 | threads = tf.train.start_queue_runners(coord=coord) 220 | 221 | try: 222 | while not coord.should_stop() and i<1: 223 | # just plot one batch size 224 | image, label = sess.run([image_batch, label_batch]) 225 | plot_images(image, label) 226 | i+=1 227 | 228 | except tf.errors.OutOfRangeError: 229 | print('done!') 230 | finally: 231 | coord.request_stop() 232 | coord.join(threads) 233 | 234 | 235 | #%% 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/VGG.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import tools 4 | 5 | #%% 6 | def VGG16(x, n_classes, is_pretrain=True): 7 | 8 | x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 9 | x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 10 | x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 11 | 12 | x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 13 | x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 14 | x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 15 | 16 | x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 17 | x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 18 | x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 19 | x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 20 | 21 | x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 22 | x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 23 | x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 24 | x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 25 | 26 | x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 27 | x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 28 | x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 29 | x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 30 | 31 | x = tools.FC_layer('fc6', x, out_nodes=4096) 32 | #x = tools.batch_norm(x) 33 | x = tools.FC_layer('fc7', x, out_nodes=4096) 34 | #x = tools.batch_norm(x) 35 | x = tools.FC_layer('fc8', x, out_nodes=n_classes) 36 | 37 | return x 38 | 39 | 40 | 41 | 42 | 43 | #%% TO get better tensorboard figures! 44 | 45 | def VGG16N(x, n_classes, is_pretrain=True): 46 | 47 | with tf.name_scope('VGG16'): 48 | 49 | x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 50 | x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 51 | with tf.name_scope('pool1'): 52 | x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 53 | 54 | x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 55 | x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 56 | with tf.name_scope('pool2'): 57 | x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 58 | 59 | 60 | 61 | x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 62 | x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 63 | x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 64 | with tf.name_scope('pool3'): 65 | x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 66 | 67 | 68 | x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 69 | x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 70 | x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 71 | with tf.name_scope('pool4'): 72 | x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 73 | 74 | 75 | x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 76 | x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 77 | x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) 78 | with tf.name_scope('pool5'): 79 | x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) 80 | 81 | 82 | x = tools.FC_layer('fc6', x, out_nodes=4096) 83 | #with tf.name_scope('batch_norm1'): 84 | #x = tools.batch_norm(x) 85 | x = tools.FC_layer('fc7', x, out_nodes=4096) 86 | #with tf.name_scope('batch_norm2'): 87 | #x = tools.batch_norm(x) 88 | x = tools.FC_layer('fc8', x, out_nodes=n_classes) 89 | 90 | return x 91 | 92 | 93 | 94 | #%% 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/000.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/000.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/001.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/001.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/002.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/002.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/007.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/007.png -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/008.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/008.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/009.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/009.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/images/010.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevin369ml/My-TensorFlow-tutorials/27163a33599e314cc15670dcabf6be2a0106f4a3/04 VGG Tensorflow/images/010.JPG -------------------------------------------------------------------------------- /04 VGG Tensorflow/input_data.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import numpy as np 4 | import os 5 | 6 | #%% Reading data 7 | 8 | def read_cifar10(data_dir, is_train, batch_size, shuffle): 9 | """Read CIFAR10 10 | 11 | Args: 12 | data_dir: the directory of CIFAR10 13 | is_train: boolen 14 | batch_size: 15 | shuffle: 16 | Returns: 17 | label: 1D tensor, tf.int32 18 | image: 4D tensor, [batch_size, height, width, 3], tf.float32 19 | 20 | """ 21 | img_width = 32 22 | img_height = 32 23 | img_depth = 3 24 | label_bytes = 1 25 | image_bytes = img_width*img_height*img_depth 26 | 27 | 28 | with tf.name_scope('input'): 29 | 30 | if is_train: 31 | filenames = [os.path.join(data_dir, 'data_batch_%d.bin' %ii) 32 | for ii in np.arange(1, 6)] 33 | else: 34 | filenames = [os.path.join(data_dir, 'test_batch.bin')] 35 | 36 | filename_queue = tf.train.string_input_producer(filenames) 37 | 38 | reader = tf.FixedLengthRecordReader(label_bytes + image_bytes) 39 | 40 | key, value = reader.read(filename_queue) 41 | 42 | record_bytes = tf.decode_raw(value, tf.uint8) 43 | 44 | label = tf.slice(record_bytes, [0], [label_bytes]) 45 | label = tf.cast(label, tf.int32) 46 | 47 | image_raw = tf.slice(record_bytes, [label_bytes], [image_bytes]) 48 | image_raw = tf.reshape(image_raw, [img_depth, img_height, img_width]) 49 | image = tf.transpose(image_raw, (1,2,0)) # convert from D/H/W to H/W/D 50 | image = tf.cast(image, tf.float32) 51 | 52 | 53 | # # data argumentation 54 | 55 | # image = tf.random_crop(image, [24, 24, 3])# randomly crop the image size to 24 x 24 56 | # image = tf.image.random_flip_left_right(image) 57 | # image = tf.image.random_brightness(image, max_delta=63) 58 | # image = tf.image.random_contrast(image,lower=0.2,upper=1.8) 59 | 60 | 61 | 62 | image = tf.image.per_image_standardization(image) #substract off the mean and divide by the variance 63 | 64 | 65 | if shuffle: 66 | images, label_batch = tf.train.shuffle_batch( 67 | [image, label], 68 | batch_size = batch_size, 69 | num_threads= 64, 70 | capacity = 20000, 71 | min_after_dequeue = 3000) 72 | else: 73 | images, label_batch = tf.train.batch( 74 | [image, label], 75 | batch_size = batch_size, 76 | num_threads = 64, 77 | capacity= 2000) 78 | ## ONE-HOT 79 | n_classes = 10 80 | label_batch = tf.one_hot(label_batch, depth= n_classes) 81 | label_batch = tf.cast(label_batch, dtype=tf.int32) 82 | label_batch = tf.reshape(label_batch, [batch_size, n_classes]) 83 | 84 | return images, label_batch 85 | #%% 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/readme.md: -------------------------------------------------------------------------------- 1 | # 11.08 2017 更新 2 | 最近入驻了网易云课堂讲师,我的第一门课《使用亚马逊云计算训练深度学习模型》。 3 | 有兴趣的同学可以学习交流。 4 | * 我的网易云课堂主页: http://study.163.com/provider/400000000275062/index.htm 5 | --- 6 | 7 | # VGG with Tensorflow 8 | 1. VGG paper review 9 | 10 | 2. Tensorflow implementation 11 | 12 | 3. Fine-tuning from parameters trained on ImageNet dataset 13 | # VGG Paper 14 | https://arxiv.org/abs/1409.1556 15 | 16 | # Paper keypoints 17 | 1. the effect of the convolutional network **depth** on its accuracy in the large-scale image recognition setting 18 | 19 | 2. using an architecture with very small (3 X 3) convolution filters, with stride 1 20 | 21 | 3. max-pooling is performed over a 2 × 2 pixel window, with stride 2 22 | 23 | 4. conv + 3 fully-connected layers (number of FC neurons: 4096 > 4096 > n_classes) 24 | 25 | 5. learning rate decay, parameter initializaiton from pre-trained models, etc. 26 | # My training 27 | 1. load pre-trained parameters (trained on ImageNet dataset, 1000 classes), you can download the parameter file (vgg16.npy, about 500M) here: 28 | https://mega.nz/#!YU1FWJrA!O1ywiCS2IiOlUCtCpI6HTJOMrneN-Qdv3ywQP5poecM 29 | 2. For Chinese users, I put the pre-trained parameter file (about 500M) on baidu: https://pan.baidu.com/s/1pLGzull 30 | 2. Remove the final layer, add one layer with 10 nodes to test the CIFAR10 dataset(binary version). 31 | https://www.cs.toronto.edu/~kriz/cifar.html 32 | 3. It took me around one hour to train with 15000 training steps and learning rate is 0.01. The testing accuracy on the CIFAR10 test dataset is about 85.69%. 33 | # Screenshots: 34 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/000.JPG?raw=true) 35 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/001.JPG?raw=true) 36 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/002.JPG?raw=true) 37 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/007.png?raw=true) 38 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/008.JPG?raw=true) 39 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/009.JPG?raw=true) 40 | ![alt_text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/04%20VGG%20Tensorflow/images/010.JPG?raw=true) 41 | # References 42 | 1. https://github.com/tensorflow/tensorflow/blob/129665119ea60640f7ed921f36db9b5c23455224/tensorflow/contrib/slim/python/slim/learning.py 43 | 2. https://github.com/huyng/tensorflow-vgg 44 | 3. https://hackernoon.com/learning-keras-by-implementing-vgg16-from-scratch-d036733f2d5 45 | 4. http://stackoverflow.com/questions/33783672/how-can-i-visualize-the-weightsvariables-in-cnn-in-tensorflow 46 | 5. http://r2rt.com/implementing-batch-normalization-in-tensorflow.html 47 | 6. https://github.com/boyw165/tensorflow-vgg 48 | 7. http://cs231n.github.io/ 49 | 8. etc. 50 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/tool_show_feature_map.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from PIL import Image 6 | import tools 7 | 8 | 9 | #%% 10 | def show_feature_map(): 11 | cat = plt.imread('cat.jpg') #unit8 12 | plt.imshow(cat) 13 | cat = tf.cast(cat, tf.float32) #[360, 300, 3] 14 | x = tf.reshape(cat, [1, 360, 300,3]) #[1, 360, 300, 3] 15 | 16 | out = 25 17 | 18 | with tf.variable_scope('conv1'): 19 | w = tools.weight([3,3,3,out], is_uniform=True) 20 | x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') 21 | b = tools.bias([out]) 22 | x_b = tf.nn.bias_add(x_w, b) 23 | x_relu = tf.nn.relu(x_b) 24 | 25 | n_feature = int(x_w.get_shape()[-1]) 26 | sess = tf.Session() 27 | sess.run(tf.global_variables_initializer()) 28 | feature_map = tf.reshape(x_w, [360,300,out]) 29 | images = tf.image.convert_image_dtype (feature_map, dtype=tf.uint8) 30 | images = sess.run(images) 31 | 32 | plt.figure(figsize=(10, 10)) 33 | for i in np.arange(0, n_feature): 34 | plt.subplot(5, 5, i + 1) 35 | plt.axis('off') 36 | plt.imshow(images[:,:,i]) 37 | plt.show() 38 | 39 | 40 | show_feature_map() 41 | 42 | 43 | 44 | #%% 45 | def show_rich_feature(): 46 | cat = plt.imread('cat.jpg') #unit8 47 | plt.imshow(cat) 48 | cat = tf.cast(cat, tf.float32) #[360, 300, 3] 49 | x = tf.reshape(cat, [1, 360, 300,3]) #[1, 360, 300, 3] 50 | 51 | with tf.variable_scope('conv1_1', reuse=True): 52 | w1 = tf.get_variable('weights', (3,3,3,64)) 53 | b1 = tf.get_variable('biases', (64)) 54 | 55 | x_w = tf.nn.conv2d(x, w1, strides=[1, 1, 1, 1], padding='SAME') 56 | x_b = tf.nn.bias_add(x_w, b1) 57 | x_relu = tf.nn.relu(x_b) 58 | 59 | out = 64 60 | 61 | n_feature = int(x_w.get_shape()[-1]) 62 | sess = tf.Session() 63 | sess.run(tf.global_variables_initializer()) 64 | feature_map = tf.reshape(x_relu, [360,300,out]) 65 | images = tf.image.convert_image_dtype (feature_map, dtype=tf.uint8) 66 | images = sess.run(images) 67 | 68 | plt.figure(figsize=(10, 10)) 69 | for i in np.arange(0, 25): 70 | plt.subplot(5, 5, i + 1) 71 | plt.axis('off') 72 | plt.imshow(images[:,:,i]) 73 | plt.show() 74 | #%% 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/tool_show_size.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import matplotlib.pyplot as plt 4 | import tools 5 | 6 | #%% 7 | cat = plt.imread('cat.jpg') #unit8 8 | plt.imshow(cat) 9 | cat = tf.cast(cat, tf.float32) #[360, 300, 3] 10 | x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] 11 | 12 | #%% 13 | 14 | # First conv 15 | with tf.variable_scope('conv1'): 16 | w = tools.weight([3,3,3,16], is_uniform=True) 17 | x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') 18 | 19 | b = tools.bias([16]) 20 | x_b = tf.nn.bias_add(x_w, b) 21 | 22 | x_relu = tf.nn.relu(x_b) 23 | 24 | x_pool = tools.pool('test1', x_relu, kernel=[1,2,2,1], stride=[1,2,2,1],is_max_pool=True) 25 | 26 | # Second conv 27 | with tf.variable_scope('conv2'): 28 | w2 = tools.weight([3,3,16,32], is_uniform=True) 29 | x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME') 30 | 31 | b2 = tools.bias([32]) 32 | x_b2 = tf.nn.bias_add(x_w2, b2) 33 | 34 | x_relu2 = tf.nn.relu(x_b2) 35 | 36 | x_pool2 = tools.pool('test2',x_relu2, kernel=[1,2,2,1],stride=[1,2,2,1], is_max_pool=False) 37 | 38 | x_BN = tools.batch_norm(x_pool2) 39 | 40 | #%% 41 | def shape(x): 42 | return str(x.get_shape()) 43 | 44 | ## First conv 45 | print('\n') 46 | print('** First conv: **\n') 47 | print('input size: ', shape(x)) 48 | print('w size:', shape(w)) 49 | print('x_w size: ', shape(x_w)) 50 | print('b size: ', shape(b)) 51 | print('x_b size: ', shape(x_b)) 52 | print('x_relu size: ', shape(x_relu)) 53 | print('x_pool size: ', shape(x_pool)) 54 | print('\n') 55 | 56 | ## Second conv 57 | print('** Second conv: **\n') 58 | print('input size: ', shape(x_pool)) 59 | print('w2 size:', shape(w2)) 60 | print('x_w2 size: ', shape(x_w2)) 61 | print('b2 size: ', shape(b2)) 62 | print('x_b2 size: ', shape(x_b2)) 63 | print('x_relu2 size: ', shape(x_relu2)) 64 | print('x_pool2 size: ', shape(x_pool2)) 65 | print('x_BN size: ', shape(x_BN)) 66 | print('\n') 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/tool_variable_name.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | #%% 4 | 5 | # First conv 6 | with tf.variable_scope('conv1'): 7 | w = tf.get_variable(name='weights', 8 | shape=[3,3,3,16], 9 | initializer=tf.contrib.layers.xavier_initializer(), trainable=False) 10 | b = tf.get_variable(name='biases', 11 | shape=[16], 12 | initializer=tf.constant_initializer(0.0), trainable=False) 13 | 14 | print('w name: ', w.name) 15 | print('b name:', b.name) 16 | 17 | with tf.name_scope('conv1'): 18 | w = tf.get_variable(name='weights', 19 | shape=[3,3,3,16], 20 | initializer=tf.contrib.layers.xavier_initializer()) 21 | b = tf.get_variable(name='biases', 22 | shape=[16], 23 | initializer=tf.constant_initializer(0.0)) 24 | 25 | print('w name: ', w.name) 26 | print('b name:', b.name) 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/tools.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | import numpy as np 4 | 5 | 6 | #%% 7 | def conv(layer_name, x, out_channels, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True): 8 | '''Convolution op wrapper, use RELU activation after convolution 9 | Args: 10 | layer_name: e.g. conv1, pool1... 11 | x: input tensor, [batch_size, height, width, channels] 12 | out_channels: number of output channels (or comvolutional kernels) 13 | kernel_size: the size of convolutional kernel, VGG paper used: [3,3] 14 | stride: A list of ints. 1-D of length 4. VGG paper used: [1, 1, 1, 1] 15 | is_pretrain: if load pretrained parameters, freeze all conv layers. 16 | Depending on different situations, you can just set part of conv layers to be freezed. 17 | the parameters of freezed layers will not change when training. 18 | Returns: 19 | 4D tensor 20 | ''' 21 | 22 | in_channels = x.get_shape()[-1] 23 | with tf.variable_scope(layer_name): 24 | w = tf.get_variable(name='weights', 25 | trainable=is_pretrain, 26 | shape=[kernel_size[0], kernel_size[1], in_channels, out_channels], 27 | initializer=tf.contrib.layers.xavier_initializer()) # default is uniform distribution initialization 28 | b = tf.get_variable(name='biases', 29 | trainable=is_pretrain, 30 | shape=[out_channels], 31 | initializer=tf.constant_initializer(0.0)) 32 | x = tf.nn.conv2d(x, w, stride, padding='SAME', name='conv') 33 | x = tf.nn.bias_add(x, b, name='bias_add') 34 | x = tf.nn.relu(x, name='relu') 35 | return x 36 | 37 | #%% 38 | def pool(layer_name, x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True): 39 | '''Pooling op 40 | Args: 41 | x: input tensor 42 | kernel: pooling kernel, VGG paper used [1,2,2,1], the size of kernel is 2X2 43 | stride: stride size, VGG paper used [1,2,2,1] 44 | padding: 45 | is_max_pool: boolen 46 | if True: use max pooling 47 | else: use avg pooling 48 | ''' 49 | if is_max_pool: 50 | x = tf.nn.max_pool(x, kernel, strides=stride, padding='SAME', name=layer_name) 51 | else: 52 | x = tf.nn.avg_pool(x, kernel, strides=stride, padding='SAME', name=layer_name) 53 | return x 54 | 55 | #%% 56 | def batch_norm(x): 57 | '''Batch normlization(I didn't include the offset and scale) 58 | ''' 59 | epsilon = 1e-3 60 | batch_mean, batch_var = tf.nn.moments(x, [0]) 61 | x = tf.nn.batch_normalization(x, 62 | mean=batch_mean, 63 | variance=batch_var, 64 | offset=None, 65 | scale=None, 66 | variance_epsilon=epsilon) 67 | return x 68 | 69 | #%% 70 | def FC_layer(layer_name, x, out_nodes): 71 | '''Wrapper for fully connected layers with RELU activation as default 72 | Args: 73 | layer_name: e.g. 'FC1', 'FC2' 74 | x: input feature map 75 | out_nodes: number of neurons for current FC layer 76 | ''' 77 | shape = x.get_shape() 78 | if len(shape) == 4: 79 | size = shape[1].value * shape[2].value * shape[3].value 80 | else: 81 | size = shape[-1].value 82 | 83 | with tf.variable_scope(layer_name): 84 | w = tf.get_variable('weights', 85 | shape=[size, out_nodes], 86 | initializer=tf.contrib.layers.xavier_initializer()) 87 | b = tf.get_variable('biases', 88 | shape=[out_nodes], 89 | initializer=tf.constant_initializer(0.0)) 90 | flat_x = tf.reshape(x, [-1, size]) # flatten into 1D 91 | 92 | x = tf.nn.bias_add(tf.matmul(flat_x, w), b) 93 | x = tf.nn.relu(x) 94 | return x 95 | 96 | #%% 97 | def loss(logits, labels): 98 | '''Compute loss 99 | Args: 100 | logits: logits tensor, [batch_size, n_classes] 101 | labels: one-hot labels 102 | ''' 103 | with tf.name_scope('loss') as scope: 104 | cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels,name='cross-entropy') 105 | loss = tf.reduce_mean(cross_entropy, name='loss') 106 | tf.summary.scalar(scope+'/loss', loss) 107 | return loss 108 | 109 | #%% 110 | def accuracy(logits, labels): 111 | """Evaluate the quality of the logits at predicting the label. 112 | Args: 113 | logits: Logits tensor, float - [batch_size, NUM_CLASSES]. 114 | labels: Labels tensor, 115 | """ 116 | with tf.name_scope('accuracy') as scope: 117 | correct = tf.equal(tf.arg_max(logits, 1), tf.arg_max(labels, 1)) 118 | correct = tf.cast(correct, tf.float32) 119 | accuracy = tf.reduce_mean(correct)*100.0 120 | tf.summary.scalar(scope+'/accuracy', accuracy) 121 | return accuracy 122 | 123 | 124 | 125 | #%% 126 | def num_correct_prediction(logits, labels): 127 | """Evaluate the quality of the logits at predicting the label. 128 | Return: 129 | the number of correct predictions 130 | """ 131 | correct = tf.equal(tf.arg_max(logits, 1), tf.arg_max(labels, 1)) 132 | correct = tf.cast(correct, tf.int32) 133 | n_correct = tf.reduce_sum(correct) 134 | return n_correct 135 | 136 | 137 | 138 | #%% 139 | def optimize(loss, learning_rate, global_step): 140 | '''optimization, use Gradient Descent as default 141 | ''' 142 | with tf.name_scope('optimizer'): 143 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) 144 | #optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 145 | train_op = optimizer.minimize(loss, global_step=global_step) 146 | return train_op 147 | 148 | 149 | 150 | 151 | #%% 152 | def load(data_path, session): 153 | data_dict = np.load(data_path, encoding='latin1').item() 154 | 155 | keys = sorted(data_dict.keys()) 156 | for key in keys: 157 | with tf.variable_scope(key, reuse=True): 158 | for subkey, data in zip(('weights', 'biases'), data_dict[key]): 159 | session.run(tf.get_variable(subkey).assign(data)) 160 | 161 | 162 | #%% 163 | def test_load(): 164 | data_path = './/vgg16_pretrain//vgg16.npy' 165 | 166 | data_dict = np.load(data_path, encoding='latin1').item() 167 | keys = sorted(data_dict.keys()) 168 | for key in keys: 169 | weights = data_dict[key][0] 170 | biases = data_dict[key][1] 171 | print('\n') 172 | print(key) 173 | print('weights shape: ', weights.shape) 174 | print('biases shape: ', biases.shape) 175 | 176 | 177 | #%% 178 | def load_with_skip(data_path, session, skip_layer): 179 | data_dict = np.load(data_path, encoding='latin1').item() 180 | for key in data_dict: 181 | if key not in skip_layer: 182 | with tf.variable_scope(key, reuse=True): 183 | for subkey, data in zip(('weights', 'biases'), data_dict[key]): 184 | session.run(tf.get_variable(subkey).assign(data)) 185 | 186 | 187 | #%% 188 | def print_all_variables(train_only=True): 189 | """Print all trainable and non-trainable variables 190 | without tl.layers.initialize_global_variables(sess) 191 | 192 | Parameters 193 | ---------- 194 | train_only : boolean 195 | If True, only print the trainable variables, otherwise, print all variables. 196 | """ 197 | # tvar = tf.trainable_variables() if train_only else tf.all_variables() 198 | if train_only: 199 | t_vars = tf.trainable_variables() 200 | print(" [*] printing trainable variables") 201 | else: 202 | try: # TF1.0 203 | t_vars = tf.global_variables() 204 | except: # TF0.12 205 | t_vars = tf.all_variables() 206 | print(" [*] printing global variables") 207 | for idx, v in enumerate(t_vars): 208 | print(" var {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name)) 209 | 210 | #%% 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | ##***** the followings are just for test the tensor size at diferent layers *********## 220 | 221 | #%% 222 | def weight(kernel_shape, is_uniform = True): 223 | ''' weight initializer 224 | Args: 225 | shape: the shape of weight 226 | is_uniform: boolen type. 227 | if True: use uniform distribution initializer 228 | if False: use normal distribution initizalizer 229 | Returns: 230 | weight tensor 231 | ''' 232 | w = tf.get_variable(name='weights', 233 | shape=kernel_shape, 234 | initializer=tf.contrib.layers.xavier_initializer()) 235 | return w 236 | 237 | #%% 238 | def bias(bias_shape): 239 | '''bias initializer 240 | ''' 241 | b = tf.get_variable(name='biases', 242 | shape=bias_shape, 243 | initializer=tf.constant_initializer(0.0)) 244 | return b 245 | 246 | #%% 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | -------------------------------------------------------------------------------- /04 VGG Tensorflow/training_and_val.py: -------------------------------------------------------------------------------- 1 | #By @Kevin Xu 2 | #kevin28520@gmail.com 3 | 4 | #My youtube: https://www.youtube.com/c/KevinXu 5 | #My Chinese weibo (微博): http://weibo.com/3983872447/profile 6 | #My Chinese youku (优酷): http://i.youku.com/deeplearning101 7 | # (深度学习QQ群): 153032765 (人满), 请加2群:462661267 8 | 9 | #%% 10 | #DATA: 11 | #1. cifar10(binary version):https://www.cs.toronto.edu/~kriz/cifar.html 12 | #2. pratrained weights (vgg16.npy):https://mega.nz/#!YU1FWJrA!O1ywiCS2IiOlUCtCpI6HTJOMrneN-Qdv3ywQP5poecM 13 | 14 | # TO Train and test: 15 | #0. get data ready, get paths ready !!! 16 | #1. run training_and_val.py and call train() in the console 17 | #2. call evaluate() in the console to test 18 | 19 | #%% 20 | 21 | import os 22 | import os.path 23 | 24 | import numpy as np 25 | import tensorflow as tf 26 | 27 | import input_data 28 | import VGG 29 | import tools 30 | 31 | #%% 32 | IMG_W = 32 33 | IMG_H = 32 34 | N_CLASSES = 10 35 | BATCH_SIZE = 32 36 | learning_rate = 0.01 37 | MAX_STEP = 15000 # it took me about one hour to complete the training. 38 | IS_PRETRAIN = True 39 | 40 | 41 | #%% Training 42 | def train(): 43 | 44 | pre_trained_weights = './/vgg16_pretrain//vgg16.npy' 45 | data_dir = './/data//cifar-10-batches-bin//' 46 | train_log_dir = './/logs//train//' 47 | val_log_dir = './/logs//val//' 48 | 49 | with tf.name_scope('input'): 50 | tra_image_batch, tra_label_batch = input_data.read_cifar10(data_dir=data_dir, 51 | is_train=True, 52 | batch_size= BATCH_SIZE, 53 | shuffle=True) 54 | val_image_batch, val_label_batch = input_data.read_cifar10(data_dir=data_dir, 55 | is_train=False, 56 | batch_size= BATCH_SIZE, 57 | shuffle=False) 58 | 59 | x = tf.placeholder(tf.float32, shape=[BATCH_SIZE, IMG_W, IMG_H, 3]) 60 | y_ = tf.placeholder(tf.int16, shape=[BATCH_SIZE, N_CLASSES]) 61 | 62 | logits = VGG.VGG16N(x, N_CLASSES, IS_PRETRAIN) 63 | loss = tools.loss(logits, y_) 64 | accuracy = tools.accuracy(logits, y_) 65 | 66 | my_global_step = tf.Variable(0, name='global_step', trainable=False) 67 | train_op = tools.optimize(loss, learning_rate, my_global_step) 68 | 69 | saver = tf.train.Saver(tf.global_variables()) 70 | summary_op = tf.summary.merge_all() 71 | 72 | init = tf.global_variables_initializer() 73 | sess = tf.Session() 74 | sess.run(init) 75 | 76 | # load the parameter file, assign the parameters, skip the specific layers 77 | tools.load_with_skip(pre_trained_weights, sess, ['fc6','fc7','fc8']) 78 | 79 | 80 | coord = tf.train.Coordinator() 81 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 82 | tra_summary_writer = tf.summary.FileWriter(train_log_dir, sess.graph) 83 | val_summary_writer = tf.summary.FileWriter(val_log_dir, sess.graph) 84 | 85 | try: 86 | for step in np.arange(MAX_STEP): 87 | if coord.should_stop(): 88 | break 89 | 90 | tra_images,tra_labels = sess.run([tra_image_batch, tra_label_batch]) 91 | _, tra_loss, tra_acc = sess.run([train_op, loss, accuracy], 92 | feed_dict={x:tra_images, y_:tra_labels}) 93 | if step % 50 == 0 or (step + 1) == MAX_STEP: 94 | print ('Step: %d, loss: %.4f, accuracy: %.4f%%' % (step, tra_loss, tra_acc)) 95 | summary_str = sess.run(summary_op, feed_dict={x:tra_images, y_:tra_labels}) 96 | tra_summary_writer.add_summary(summary_str, step) 97 | 98 | if step % 200 == 0 or (step + 1) == MAX_STEP: 99 | val_images, val_labels = sess.run([val_image_batch, val_label_batch]) 100 | val_loss, val_acc = sess.run([loss, accuracy], 101 | feed_dict={x:val_images,y_:val_labels}) 102 | print('** Step %d, val loss = %.2f, val accuracy = %.2f%% **' %(step, val_loss, val_acc)) 103 | 104 | summary_str = sess.run(summary_op, feed_dict={x:tra_images, y_:tra_labels}) 105 | val_summary_writer.add_summary(summary_str, step) 106 | 107 | if step % 2000 == 0 or (step + 1) == MAX_STEP: 108 | checkpoint_path = os.path.join(train_log_dir, 'model.ckpt') 109 | saver.save(sess, checkpoint_path, global_step=step) 110 | 111 | except tf.errors.OutOfRangeError: 112 | print('Done training -- epoch limit reached') 113 | finally: 114 | coord.request_stop() 115 | 116 | coord.join(threads) 117 | sess.close() 118 | 119 | 120 | 121 | 122 | 123 | 124 | #%% Test the accuracy on test dataset. got about 85.69% accuracy. 125 | import math 126 | def evaluate(): 127 | with tf.Graph().as_default(): 128 | 129 | # log_dir = 'C://Users//kevin//Documents//tensorflow//VGG//logsvgg//train//' 130 | log_dir = 'C:/Users/kevin/Documents/tensorflow/VGG/logs/train/' 131 | test_dir = './/data//cifar-10-batches-bin//' 132 | n_test = 10000 133 | 134 | images, labels = input_data.read_cifar10(data_dir=test_dir, 135 | is_train=False, 136 | batch_size= BATCH_SIZE, 137 | shuffle=False) 138 | 139 | logits = VGG.VGG16N(images, N_CLASSES, IS_PRETRAIN) 140 | correct = tools.num_correct_prediction(logits, labels) 141 | saver = tf.train.Saver(tf.global_variables()) 142 | 143 | with tf.Session() as sess: 144 | 145 | print("Reading checkpoints...") 146 | ckpt = tf.train.get_checkpoint_state(log_dir) 147 | if ckpt and ckpt.model_checkpoint_path: 148 | global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 149 | saver.restore(sess, ckpt.model_checkpoint_path) 150 | print('Loading success, global_step is %s' % global_step) 151 | else: 152 | print('No checkpoint file found') 153 | return 154 | 155 | coord = tf.train.Coordinator() 156 | threads = tf.train.start_queue_runners(sess = sess, coord = coord) 157 | 158 | try: 159 | print('\nEvaluating......') 160 | num_step = int(math.floor(n_test / BATCH_SIZE)) 161 | num_sample = num_step*BATCH_SIZE 162 | step = 0 163 | total_correct = 0 164 | while step < num_step and not coord.should_stop(): 165 | batch_correct = sess.run(correct) 166 | total_correct += np.sum(batch_correct) 167 | step += 1 168 | print('Total testing samples: %d' %num_sample) 169 | print('Total correct predictions: %d' %total_correct) 170 | print('Average accuracy: %.2f%%' %(100*total_correct/num_sample)) 171 | except Exception as e: 172 | coord.request_stop(e) 173 | finally: 174 | coord.request_stop() 175 | coord.join(threads) 176 | 177 | #%% 178 | 179 | 180 | 181 | -------------------------------------------------------------------------------- /05 object localization/README.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## (Note that this repo is NOT going to be updated anymore, tensorflow version used in this repo was very old. -- July 19th, 2018) 2 | 3 | # My-TensorFlow-tutorials 4 | This repo contains Tensorflow & deep learning projects. 5 | 1. Deep Learning (as well as traditional Machine Learning, Data Mining). 6 | 2. Tensorflow, Keras 7 | 8 | * Youtube: https://www.youtube.com/c/KevinXu 9 | 10 | 11 | 12 | 13 | # Have fun! 14 | ![alt text](https://github.com/kevin28520/My-TensorFlow-tutorials/blob/master/01%20cats%20vs%20dogs/images/starry%20night%20dd3.jpg) 15 | 16 | --------------------------------------------------------------------------------