├── GANs_inference.py ├── Gan.png ├── README.md ├── ckpt_pb.py ├── config.py ├── g_tfrecords.py ├── gender ├── 0male │ ├── 0(1).jpeg │ ├── 0(1).jpg │ ├── 0(2).jpeg │ ├── 0(2).jpg │ ├── 0(3).jpeg │ └── 0(3).jpg ├── 1female │ ├── 1(1).jpg │ ├── 1(2).jpg │ ├── 1(3).jpg │ ├── 1(4).jpg │ ├── 1(5).jpg │ └── 1(6).jpg ├── 2many │ ├── 0_Parade_marchingband_1_12.jpg │ ├── 0_Parade_marchingband_1_13.jpg │ ├── 0_Parade_marchingband_1_17.jpg │ ├── 0_Parade_marchingband_1_5.jpg │ ├── 0_Parade_marchingband_1_6.jpg │ └── 0_Parade_marchingband_1_8.jpg └── 3other │ ├── 6(2).jpg │ ├── 6(3).jpg │ ├── 6(4).jpg │ ├── 6(5).jpg │ ├── 6(6).jpg │ └── 6(9).jpg ├── lib ├── .DS_Store ├── data_aug │ ├── .DS_Store │ └── data_aug.py ├── data_load │ ├── .DS_Store │ ├── data_load.py │ └── data_load_from_txt_mullabel.py ├── loss │ ├── .DS_Store │ └── loss.py ├── model │ ├── .DS_Store │ ├── build_net │ │ ├── .DS_Store │ │ ├── arch_net.py │ │ └── networks.py │ ├── discriminator │ │ ├── .DS_Store │ │ └── build_discriminator │ │ │ ├── __pycache__ │ │ │ └── build_discriminator.cpython-35.pyc │ │ │ └── build_discriminator.py │ ├── generator │ │ ├── .DS_Store │ │ ├── alexnet │ │ │ ├── __pycache__ │ │ │ │ └── alexnet.cpython-35.pyc │ │ │ └── alexnet.py │ │ ├── attention │ │ │ ├── .DS_Store │ │ │ ├── __pycache__ │ │ │ │ └── attention.cpython-35.pyc │ │ │ └── attention.py │ │ ├── build_generator │ │ │ ├── .DS_Store │ │ │ ├── __pycache__ │ │ │ │ ├── build_generator.cpython-35.pyc │ │ │ │ └── build_net.cpython-35.pyc │ │ │ └── build_generator.py │ │ ├── cifarnet │ │ │ └── cifarnet.py │ │ ├── inception_resnet_v2 │ │ │ └── inception_resnet_v2.py │ │ ├── inception_v4 │ │ │ ├── .DS_Store │ │ │ ├── __pycache__ │ │ │ │ ├── inception_utils.cpython-35.pyc │ │ │ │ └── inception_v4.cpython-35.pyc │ │ │ ├── inception_utils.py │ │ │ └── inception_v4.py │ │ ├── lp_net │ │ │ ├── __pycache__ │ │ │ │ └── lp_net.cpython-35.pyc │ │ │ └── lp_net.py │ │ ├── resnet_v2 │ │ │ ├── .DS_Store │ │ │ ├── __pycache__ │ │ │ │ ├── resnet_utils.cpython-35.pyc │ │ │ │ └── resnet_v2.cpython-35.pyc │ │ │ ├── resnet_utils.py │ │ │ └── resnet_v2.py │ │ └── vgg │ │ │ ├── .DS_Store │ │ │ ├── __pycache__ │ │ │ └── vgg.cpython-35.pyc │ │ │ └── vgg.py │ └── pix2pix │ │ ├── pix2pix.py │ │ └── pix2pix_test.py ├── optimizer │ ├── .DS_Store │ ├── optimizer.py │ └── optimizer_minimize.py ├── train │ ├── .DS_Store │ └── train_GANs.py └── utils │ ├── .DS_Store │ └── GANs_utils.py ├── lp.jpg ├── main.py ├── model ├── .DS_Store └── README.md ├── pretrain ├── .DS_Store └── README.md ├── test_accuracy.py ├── test_tfRecords.py └── train.txt /GANs_inference.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on 2017 10.17 5 | @author: liupeng 6 | wechat: lp9628 7 | blog: http://blog.csdn.net/u014365862/article/details/78422372 8 | """ 9 | 10 | import numpy as np 11 | from scipy import misc 12 | import tensorflow as tf 13 | from threading import Lock 14 | import os 15 | import cv2 16 | import sys 17 | from lib.utils.GANs_utils import sample_noise 18 | import matplotlib.pyplot as plt 19 | import matplotlib.gridspec as gridspec 20 | import config 21 | 22 | model_class = 1 23 | dim = 64 24 | num_gen = 128 25 | 26 | def GPU_config(rate=0.99): 27 | 28 | os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" 29 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 30 | gpuConfig = tf.ConfigProto() 31 | gpuConfig.allow_soft_placement = False 32 | gpuConfig.gpu_options.allow_growth = True 33 | gpuConfig.gpu_options.per_process_gpu_memory_fraction = rate 34 | 35 | return gpuConfig 36 | 37 | def prewhiten(self, img): 38 | mean = np.mean(img) 39 | std = np.std(img) 40 | std_adj = np.maximum(std, 1.0/np.sqrt(img.size)) 41 | ret = np.multiply(np.subtract(img, mean), 1/std_adj) 42 | return ret 43 | def to_rgb(self,img): 44 | w, h = img.shape 45 | ret = np.empty((w, h, 3), dtype=np.uint8) 46 | ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img 47 | return ret 48 | def img_crop(img, box): 49 | # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40 50 | x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) 51 | img = img[y1:y2, x1:x2] 52 | return img 53 | def data_norm(img): 54 | img = img / 255.0 55 | img = img - 0.5 56 | img = img * 2 57 | return img 58 | def dec_data_norm(img): 59 | img = img / 2. 60 | img = img + 0.5 61 | img = img * 255. 62 | return img 63 | 64 | class LPAlg_unconditional(object): 65 | 66 | # default model path of .pb 67 | PB_PATH_1 = os.path.join(os.getcwd(), "model", "body_pose_model.pb") 68 | PB_PATH = [PB_PATH_1] 69 | 70 | CLASS_NUMBER = model_class 71 | 72 | def __init__(self, pb_path_1=None, gpu_config=GPU_config()): 73 | def get_path(path,default_path): 74 | return (path, default_path)[path is None] 75 | 76 | def load_graph(frozen_graph_filename): 77 | # We load the protobuf file from the disk and parse it to retrieve the 78 | # unserialized graph_def 79 | with tf.gfile.GFile(frozen_graph_filename, "rb") as f: 80 | graph_def = tf.GraphDef() 81 | graph_def.ParseFromString(f.read()) 82 | 83 | # Then, we can use again a convenient built-in function to import a graph_def into the 84 | # current default Graph 85 | with tf.Graph().as_default() as graph: 86 | tf.import_graph_def( 87 | graph_def, 88 | input_map=None, 89 | return_elements=None, 90 | name="prefix", 91 | op_dict=None, 92 | producer_op_list=None 93 | ) 94 | return graph 95 | 96 | # model 97 | def sess_def(pb_path): 98 | print (pb_path) 99 | graph = load_graph(pb_path) 100 | pred = graph.get_tensor_by_name('prefix/predictions:0') 101 | batch_size = tf.placeholder(tf.float32, [None, 1]) 102 | label_indices = tf.placeholder(tf.float32, [None, 2]) 103 | x1 = graph.get_tensor_by_name('prefix/inputs_placeholder:0') 104 | # x2 = graph.get_tensor_by_name('prefix/inputs_placeholder2:0') 105 | # x3 = graph.get_tensor_by_name('prefix/inputs_placeholder3:0') 106 | sess = tf.Session(graph=graph,config=gpu_config) 107 | return [sess,x1,batch_size,label_indices,pred] 108 | 109 | # multiple models 110 | def multi_model_def(pb_path_1): 111 | model_1 = sess_def(pb_path_1) 112 | return [model_1] 113 | 114 | path_1 = get_path(pb_path_1, LPAlg_unconditional.PB_PATH[0]) 115 | self._pb_path = [path_1] 116 | 117 | self.model = multi_model_def(self._pb_path[0]) 118 | 119 | def _close(self): 120 | self.model[0][0].close() 121 | 122 | def _run(self, images_path=None): 123 | idx = 0 # model index 124 | # print (imgs) 125 | sess = tf.Session() 126 | generator_x = sess.run(sample_noise(num_gen, dim)) 127 | predict = self.model[idx][0].run( 128 | self.model[idx][4], 129 | feed_dict={self.model[idx][1]: generator_x 130 | # self.model[idx][2]: imgs2, 131 | # self.model[idx][3]: imgs3 132 | } 133 | ) 134 | print ('predict:', predict) 135 | 136 | return predict.tolist() 137 | 138 | class LPAlg_conditional(object): 139 | 140 | # default model path of .pb 141 | PB_PATH_1 = os.path.join(os.getcwd(), "model", "body_pose_model.pb") 142 | PB_PATH = [PB_PATH_1] 143 | 144 | CLASS_NUMBER = model_class 145 | 146 | def __init__(self, pb_path_1=None, gpu_config=GPU_config()): 147 | def get_path(path,default_path): 148 | return (path, default_path)[path is None] 149 | 150 | def load_graph(frozen_graph_filename): 151 | # We load the protobuf file from the disk and parse it to retrieve the 152 | # unserialized graph_def 153 | with tf.gfile.GFile(frozen_graph_filename, "rb") as f: 154 | graph_def = tf.GraphDef() 155 | graph_def.ParseFromString(f.read()) 156 | 157 | # Then, we can use again a convenient built-in function to import a graph_def into the 158 | # current default Graph 159 | with tf.Graph().as_default() as graph: 160 | tf.import_graph_def( 161 | graph_def, 162 | input_map=None, 163 | return_elements=None, 164 | name="prefix", 165 | op_dict=None, 166 | producer_op_list=None 167 | ) 168 | return graph 169 | 170 | # model 171 | def sess_def(pb_path): 172 | print (pb_path) 173 | graph = load_graph(pb_path) 174 | pred = graph.get_tensor_by_name('prefix/predictions:0') 175 | batch_size = tf.placeholder(tf.float32, [None, 1]) 176 | label_indices = tf.placeholder(tf.float32, [None, 2]) 177 | x1 = graph.get_tensor_by_name('prefix/inputs_placeholder0:0') 178 | x2 = graph.get_tensor_by_name('prefix/inputs_placeholder1:0') 179 | # x2 = graph.get_tensor_by_name('prefix/inputs_placeholder2:0') 180 | # x3 = graph.get_tensor_by_name('prefix/inputs_placeholder3:0') 181 | sess = tf.Session(graph=graph,config=gpu_config) 182 | return [sess,x1,x2,batch_size,label_indices,pred] 183 | 184 | # multiple models 185 | def multi_model_def(pb_path_1): 186 | model_1 = sess_def(pb_path_1) 187 | return [model_1] 188 | 189 | path_1 = get_path(pb_path_1, LPAlg_conditional.PB_PATH[0]) 190 | self._pb_path = [path_1] 191 | 192 | self.model = multi_model_def(self._pb_path[0]) 193 | 194 | def _close(self): 195 | self.model[0][0].close() 196 | 197 | def _run(self, images_path=None): 198 | idx = 0 # model index 199 | # print (imgs) 200 | sess = tf.Session() 201 | generator_x = sess.run(sample_noise(num_gen, dim)) 202 | num_classes = config.num_classes 203 | def sample_label(): 204 | num = num_gen 205 | label_vector = np.zeros((num , num_classes), dtype=np.float) 206 | for i in range(0 , num): 207 | label_vector[i , i%4] = 1.0 208 | return label_vector 209 | label = sample_label() 210 | predict = self.model[idx][0].run( 211 | self.model[idx][5], 212 | feed_dict={self.model[idx][1]: generator_x, 213 | self.model[idx][2]: label 214 | # self.model[idx][3]: imgs3 215 | } 216 | ) 217 | print ('predict:', predict) 218 | 219 | return predict.tolist() 220 | 221 | def ProjectInterface(image_path_list, proxy=None): 222 | images_path = image_path_list.keys() 223 | predict = proxy._run(images_path) 224 | return predict 225 | 226 | 227 | 228 | def show_images(images): 229 | # images = np.reshape(images, [images.shape[0], -1]) # images reshape to (batch_size, D) 230 | # sqrtn = int(np.ceil(np.sqrt(images.shape[0]))) 231 | # sqrtimg = int(np.ceil(np.sqrt(images.shape[1]))) 232 | 233 | fig = plt.figure(figsize=(36, 36)) 234 | gs = gridspec.GridSpec(36, 36) 235 | gs.update(wspace=0.05, hspace=0.05) 236 | 237 | for i, img in enumerate(images): 238 | img = np.asarray(img) 239 | img = dec_data_norm(img) 240 | cv2.imwrite('face0.jpg', img) 241 | img = cv2.imread('face0.jpg') 242 | img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 243 | ax = plt.subplot(gs[i]) 244 | plt.axis('off') 245 | ax.set_xticklabels([]) 246 | ax.set_yticklabels([]) 247 | ax.set_aspect('equal') 248 | # plt.imshow(img.reshape([sqrtimg,sqrtimg])) 249 | plt.imshow(img.reshape([32,32,3])) 250 | 251 | 252 | if __name__ == "__main__": 253 | # python predict.py lp.jpg (带标签输出逻辑) 254 | import argparse 255 | parser = argparse.ArgumentParser() 256 | parser.add_argument('image', type=str, help='Assign the image path.', default="") 257 | args = parser.parse_args() 258 | arch_model = config.arch_model 259 | if arch_model == "arch_dcgan_unconditional": 260 | alg_core = LPAlg_unconditional(pb_path_1="model/body_pose_model.pb") 261 | elif arch_model == "arch_dcgan_conditional": 262 | alg_core = LPAlg_conditional(pb_path_1="model/body_pose_model.pb") 263 | else: 264 | print ('{} is error!', arch_model) 265 | result_dict = ProjectInterface({args.image: args.image}, proxy=alg_core) 266 | result_dict_img = ((np.asarray(result_dict) / 2. + 0.5) *255) #.reshape([32,32,3]) 267 | print(result_dict_img) 268 | cv2.imwrite('face.jpg', result_dict_img[0]) 269 | result_dict = np.asarray(result_dict) 270 | show_images(result_dict) 271 | plt.show() 272 | -------------------------------------------------------------------------------- /Gan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/Gan.png -------------------------------------------------------------------------------- /ckpt_pb.py: -------------------------------------------------------------------------------- 1 | # coding = utf-8 2 | 3 | import tensorflow as tf 4 | from tensorflow.python.framework import graph_util 5 | # from lib.utils.utils import input_placeholder, g_parameter, build_net 6 | from lib.utils.GANs_utils import build_generator 7 | import cv2 8 | import numpy as np 9 | import os 10 | import sys 11 | import config 12 | MODEL_DIR = "model/" 13 | MODEL_NAME = "body_pose_model.pb" 14 | if not tf.gfile.Exists(MODEL_DIR): #创建目录 15 | tf.gfile.MakeDirs(MODEL_DIR) 16 | 17 | height, width = config.height, config.width 18 | num_classes = config.num_classes 19 | arch_model = config.arch_model 20 | dim = 64 21 | 22 | if arch_model == "arch_dcgan_unconditional": 23 | X = tf.placeholder(tf.float32, [None, dim], name = "inputs_placeholder") 24 | with tf.variable_scope("Generator_dcgan", reuse=None) as scope: 25 | net, net_vis = build_generator(X, num_classes, 1.0, False, arch_model) 26 | elif arch_model == "arch_dcgan_conditional": 27 | X = tf.placeholder(tf.float32, [None, dim], name = "inputs_placeholder0") 28 | Y = tf.placeholder(tf.float32, [None, num_classes], name = "inputs_placeholder1") 29 | with tf.variable_scope("Generator_dcgan", reuse=None) as scope: 30 | net, net_vis = build_generator([X,Y], num_classes, 1.0, False, arch_model) 31 | else: 32 | print ('{} is error!', arch_model) 33 | # net = tf.nn.softmax(net) 34 | # predict = tf.reshape(net, [-1, num_classes], name='predictions') 35 | predict = tf.reshape(net, [-1, height, width, 3], name='predictions') 36 | 37 | 38 | def freeze_graph(model_folder): 39 | #checkpoint = tf.train.get_checkpoint_state(model_folder) #检查目录下ckpt文件状态是否可用 40 | #input_checkpoint = checkpoint.model_checkpoint_path #得ckpt文件路径 41 | input_checkpoint = model_folder 42 | output_graph = os.path.join(MODEL_DIR, MODEL_NAME) #PB模型保存路径 43 | 44 | output_node_names = "predictions" #原模型输出操作节点的名字 45 | #saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=True) #得到图、clear_devices :Whether or not to clear the device field for an `Operation` or `Tensor` during import. 46 | saver = tf.train.Saver() 47 | 48 | graph = tf.get_default_graph() #获得默认的图 49 | input_graph_def = graph.as_graph_def() #返回一个序列化的图代表当前的图 50 | 51 | with tf.Session() as sess: 52 | sess.run(tf.initialize_all_variables()) 53 | saver.restore(sess, input_checkpoint) #恢复图并得到数据 54 | 55 | #print "predictions : ", sess.run("predictions:0", feed_dict={"input_holder:0": [10.0]}) # 测试读出来的模型是否正确,注意这里传入的是输出 和输入 节点的 tensor的名字,不是操作节点的名字 56 | 57 | output_graph_def = graph_util.convert_variables_to_constants( #模型持久化,将变量值固定 58 | sess, 59 | input_graph_def, 60 | output_node_names.split(",") #如果有多个输出节点,以逗号隔开 61 | ) 62 | with tf.gfile.GFile(output_graph, "wb") as f: #保存模型 63 | f.write(output_graph_def.SerializeToString()) #序列化输出 64 | print("%d ops in the final graph." % len(output_graph_def.node)) #得到当前图有几个操作节点 65 | 66 | for op in graph.get_operations(): 67 | #print(op.name, op.values()) 68 | print("name:",op.name) 69 | print ("success!") 70 | 71 | 72 | #下面是用于测试, 读取pd模型,答应每个变量的名字。 73 | graph = load_graph("model/body_pose_model.pb") 74 | for op in graph.get_operations(): 75 | #print(op.name, op.values()) 76 | print("name111111111111:",op.name) 77 | if arch_model == "arch_dcgan_unconditional": 78 | pred = graph.get_tensor_by_name('prefix/inputs_placeholder:0') 79 | print (pred) 80 | elif arch_model == "arch_dcgan_conditional": 81 | pred = graph.get_tensor_by_name('prefix/inputs_placeholder0:0') 82 | pred = graph.get_tensor_by_name('prefix/inputs_placeholder1:0') 83 | print (pred) 84 | else: 85 | print ('{} is error!', arch_model) 86 | temp = graph.get_tensor_by_name('prefix/predictions:0') 87 | print (temp) 88 | 89 | def load_graph(frozen_graph_filename): 90 | # We load the protobuf file from the disk and parse it to retrieve the 91 | # unserialized graph_def 92 | with tf.gfile.GFile(frozen_graph_filename, "rb") as f: 93 | graph_def = tf.GraphDef() 94 | graph_def.ParseFromString(f.read()) 95 | 96 | # Then, we can use again a convenient built-in function to import a graph_def into the 97 | # current default Graph 98 | with tf.Graph().as_default() as graph: 99 | tf.import_graph_def( 100 | graph_def, 101 | input_map=None, 102 | return_elements=None, 103 | name="prefix", 104 | op_dict=None, 105 | producer_op_list=None 106 | ) 107 | return graph 108 | 109 | 110 | if __name__ == '__main__': 111 | train_dir = 'model' 112 | latest = tf.train.latest_checkpoint(train_dir) 113 | if not latest: 114 | print ("No checkpoint to continue from in", train_dir) 115 | sys.exit(1) 116 | print ("resume", latest) 117 | # saver2.restore(sess, latest) 118 | # model_folder = './model/model.ckpt-0' 119 | model_folder = latest 120 | freeze_graph(model_folder) 121 | 122 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | # 训练文件夹 4 | # sample_dir = "/Users/liupeng/Desktop/anaconda/data_padding/img_body_pose_train_crop_up" # 可以为txt, 但是需要该代码 5 | sample_dir = "gender" 6 | tfRecord = False 7 | # 需要分类的类别数量 8 | num_classes = 5 9 | # 最小批训练的大小 10 | batch_size = 1 11 | # 选择使用的模型 12 | # arch_model="arch_dcgan_conditional" 13 | arch_model = "arch_dcgan_unconditional" 14 | # 选择训练的网络层 15 | checkpoint_exclude_scopes = "Logits_out" 16 | # dropout的大小 17 | dropout_prob = 0.8 18 | # 选择训练样本的比例 19 | train_rate = 0.9 20 | # 整个训练集上进行多少次迭代 21 | epoch = 2000 22 | # 是否使用提前终止训练 23 | early_stop = True 24 | EARLY_STOP_PATIENCE = 1000 25 | # 是否使用learning_rate 26 | ''' 27 | 'unconditional': (1e-4, 1e-3), 28 | 'conditional': (1e-4, 1e-3), 29 | ''' 30 | learning_r_decay = True 31 | learning_rate_base = 0.001 32 | decay_rate = 0.95 33 | height, width = 32,32 #224, 224 34 | # 模型保存的路径 35 | train_dir = 'model' 36 | # 是否进行fine-tune。 选择fine-tune的的参数 37 | fine_tune = False 38 | # 是否训练所有层的参数 39 | train_all_layers = True 40 | # 迁移学习模型参数, 下载训练好模型:https://github.com/MachineLP/models/tree/master/research/slim 41 | # checkpoint_path="pretrain/inception_v4/inception_v4.ckpt"; 42 | # checkpoint_path="pretrain/resnet_v2_50/resnet_v2_50.ckpt" 43 | checkpoint_path = 'pretrain/inception_v4/inception_v4.ckpt' 44 | -------------------------------------------------------------------------------- /g_tfrecords.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import os 11 | import tensorflow as tf 12 | slim = tf.contrib.slim 13 | from lib.data_load.data_load_from_txt_mullabel import data_load_from_txt_mullabel 14 | from lib.model.build_model.build_net import net_arch 15 | from lib.utils.multi_label_utils import g_parameter 16 | from lib.utils.multi_label_utils import to_one_hot 17 | from PIL import Image 18 | # from lib.train.train3 import train3 as train 19 | from keras.utils import np_utils 20 | import config 21 | os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" 22 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 23 | 24 | sample_dir = config.sample_dir 25 | num_classes = config.num_classes 26 | batch_size = config.batch_size 27 | arch_model = config.arch_model 28 | checkpoint_exclude_scopes = config.checkpoint_exclude_scopes 29 | dropout_prob = config.dropout_prob 30 | train_rate = config.train_rate 31 | epoch = config.epoch 32 | # 是否使用提前终止训练 33 | early_stop = config.early_stop 34 | EARLY_STOP_PATIENCE = config.EARLY_STOP_PATIENCE 35 | # 是否使用learning_rate 36 | learning_r_decay = config.learning_r_decay 37 | learning_rate_base = config.learning_rate_base 38 | decay_rate = config.decay_rate 39 | height, width = config.height, config.width 40 | # 模型保存的路径 41 | train_dir = config.train_dir 42 | # 是否进行fine-tune。 选择fine-tune的的参数 43 | fine_tune = config.fine_tune 44 | # 训练所有层的参数 45 | train_all_layers = config.train_all_layers 46 | # 迁移学习的网络模型 47 | checkpoint_path = config.checkpoint_path 48 | 49 | 50 | from lib.train.train_multi_label import train_multi_label as train 51 | train_data, train_label, valid_data, valid_label, train_n, valid_n, note_label = data_load_from_txt_mullabel(sample_dir, train_rate).gen_train_valid() 52 | 53 | def arr_to_list(train_label): 54 | train_labels = [] 55 | for label_i in train_label: 56 | tmp_label = [] 57 | for label_j in label_i: 58 | tmp_label.append(label_j) 59 | train_labels.append(tmp_label) 60 | return train_labels 61 | 62 | print ('note_label', note_label) 63 | print (train_data) 64 | print (train_label) 65 | if arch_model!='arch_seg_vgg16_conv' and arch_model!='arch_vgg16_ocr': 66 | # train_label = np_utils.to_categorical(train_label, num_classes) 67 | # valid_label = np_utils.to_categorical(valid_label, num_classes) 68 | train_label = to_one_hot(train_label, num_classes) 69 | valid_label = to_one_hot(valid_label, num_classes) 70 | 71 | train_label = arr_to_list(train_label) 72 | valid_label = arr_to_list(valid_label) 73 | print (train_label) 74 | 75 | 76 | if not os.path.isdir(train_dir): 77 | os.makedirs(train_dir) 78 | 79 | class ImageReader(object): 80 | """Helper class that provides TensorFlow image coding utilities.""" 81 | 82 | def __init__(self): 83 | # Initializes function that decodes RGB JPEG data. 84 | self._decode_jpeg_data = tf.placeholder(dtype=tf.string) 85 | self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3) 86 | 87 | def read_image_dims(self, sess, image_data): 88 | image = self.decode_jpeg(sess, image_data) 89 | return image.shape[0], image.shape[1] 90 | 91 | def decode_jpeg(self, sess, image_data): 92 | image = sess.run(self._decode_jpeg, 93 | feed_dict={self._decode_jpeg_data: image_data}) 94 | assert len(image.shape) == 3 95 | assert image.shape[2] == 3 96 | return image 97 | def int64_feature(value): 98 | """Wrapper for inserting int64 features into Example proto. 99 | """ 100 | if not isinstance(value, list): 101 | value = [value] 102 | return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) 103 | 104 | 105 | def float_feature(value): 106 | """Wrapper for inserting float features into Example proto. 107 | """ 108 | if not isinstance(value, list): 109 | value = [value] 110 | return tf.train.Feature(float_list=tf.train.FloatList(value=value)) 111 | 112 | 113 | def bytes_feature(value): 114 | """Wrapper for inserting bytes features into Example proto. 115 | """ 116 | if not isinstance(value, list): 117 | value = [value] 118 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=value)) 119 | 120 | TFwriter_train = tf.python_io.TFRecordWriter("img_train.tfrecords") 121 | TFwriter_test = tf.python_io.TFRecordWriter("img_test.tfrecords") 122 | 123 | image_reader = ImageReader() 124 | sess = tf.Session() 125 | for i in range(len(train_data)): 126 | #image_data = tf.gfile.FastGFile(img_path, 'rb').read() 127 | #height, width = image_reader.read_image_dims(sess, image_data) 128 | img = Image.open(train_data[i]) 129 | img = img.resize((height, width)) 130 | image_data = img.tobytes() 131 | height, width = height, width 132 | label = train_label[i] 133 | example = tf.train.Example(features=tf.train.Features(feature={ 134 | "label":float_feature(label), 135 | "img":bytes_feature(image_data), 136 | "height":int64_feature(height), 137 | "width":int64_feature(width) 138 | }) ) 139 | TFwriter_train.write(example.SerializeToString()) 140 | for i in range(len(valid_data)): 141 | #image_data = tf.gfile.FastGFile(img_path, 'rb').read() 142 | #height, width = image_reader.read_image_dims(sess, image_data) 143 | img = Image.open(valid_data[i]) 144 | img = img.resize((height, width)) 145 | image_data = img.tobytes() 146 | height, width = height, width 147 | label = valid_label[i] 148 | example = tf.train.Example(features=tf.train.Features(feature={ 149 | "label":float_feature(label), 150 | "img":bytes_feature(image_data), 151 | "height":int64_feature(height), 152 | "width":int64_feature(width) 153 | }) ) 154 | TFwriter_test.write(example.SerializeToString()) 155 | TFwriter_train.close() 156 | TFwriter_test.close() 157 | -------------------------------------------------------------------------------- /gender/0male/0(1).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(1).jpeg -------------------------------------------------------------------------------- /gender/0male/0(1).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(1).jpg -------------------------------------------------------------------------------- /gender/0male/0(2).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(2).jpeg -------------------------------------------------------------------------------- /gender/0male/0(2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(2).jpg -------------------------------------------------------------------------------- /gender/0male/0(3).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(3).jpeg -------------------------------------------------------------------------------- /gender/0male/0(3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/0male/0(3).jpg -------------------------------------------------------------------------------- /gender/1female/1(1).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(1).jpg -------------------------------------------------------------------------------- /gender/1female/1(2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(2).jpg -------------------------------------------------------------------------------- /gender/1female/1(3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(3).jpg -------------------------------------------------------------------------------- /gender/1female/1(4).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(4).jpg -------------------------------------------------------------------------------- /gender/1female/1(5).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(5).jpg -------------------------------------------------------------------------------- /gender/1female/1(6).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/1female/1(6).jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_12.jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_13.jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_17.jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_5.jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_6.jpg -------------------------------------------------------------------------------- /gender/2many/0_Parade_marchingband_1_8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/2many/0_Parade_marchingband_1_8.jpg -------------------------------------------------------------------------------- /gender/3other/6(2).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(2).jpg -------------------------------------------------------------------------------- /gender/3other/6(3).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(3).jpg -------------------------------------------------------------------------------- /gender/3other/6(4).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(4).jpg -------------------------------------------------------------------------------- /gender/3other/6(5).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(5).jpg -------------------------------------------------------------------------------- /gender/3other/6(6).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(6).jpg -------------------------------------------------------------------------------- /gender/3other/6(9).jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/gender/3other/6(9).jpg -------------------------------------------------------------------------------- /lib/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/.DS_Store -------------------------------------------------------------------------------- /lib/data_aug/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/data_aug/.DS_Store -------------------------------------------------------------------------------- /lib/data_aug/data_aug.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import cv2 11 | from skimage import exposure 12 | 13 | class DataAugmenters(): 14 | 15 | def __init__(self, image): 16 | self.img = image 17 | 18 | def _random_fliplr(self, random_fliplr=True): 19 | if random_fliplr and np.random.choice([True, False]): 20 | self.img = np.fliplr(self.img) 21 | 22 | def _random_flipud(self, random_flipud=True): 23 | if random_flipud and np.random.choice([True, False]): 24 | self.img = np.fliplr(self.img) 25 | 26 | def _random_rotation(self, random_rotation=True): 27 | if random_rotation and np.random.choice([True, False]): 28 | w,h = self.img.shape[1], self.img.shape[0] 29 | angle = np.random.randint(0,360) 30 | rotate_matrix = cv2.getRotationMatrix2D(center=(self.img.shape[1]/2, self.img.shape[0]/2), angle=angle, scale=0.7) 31 | self.img = cv2.warpAffine(self.img, rotate_matrix, (w,h), borderMode=cv2.BORDER_REPLICATE) 32 | 33 | def _random_exposure(self, random_exposure=True): 34 | if random_exposure and np.random.choice([True, False]): 35 | e_rate = np.random.uniform(0.5,1.5) 36 | self.img = exposure.adjust_gamma(self.img, e_rate) 37 | 38 | # 裁剪 39 | def _random_crop(self, crop_size = 299, random_crop = True): 40 | if random_crop and np.random.choice([True, False]): 41 | if self.img.shape[1] > crop_size: 42 | sz1 = self.img.shape[1] // 2 43 | sz2 = crop_size // 2 44 | diff = sz1 - sz2 45 | (h, v) = (np.random.randint(0, diff + 1), np.random.randint(0, diff + 1)) 46 | self.img = self.img[v:(v + crop_size), h:(h + crop_size), :] 47 | 48 | def run(self): 49 | data_aug_list = [self._random_fliplr, self._random_flipud, self._random_rotation, self._random_exposure, self._random_crop] 50 | data_aug_func = np.random.choice(data_aug_list, 2) 51 | for func in data_aug_func: 52 | func() 53 | return self.img 54 | -------------------------------------------------------------------------------- /lib/data_load/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/data_load/.DS_Store -------------------------------------------------------------------------------- /lib/data_load/data_load.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import sys 10 | import tensorflow as tf 11 | import numpy as np 12 | import os 13 | import cv2 14 | from skimage import exposure 15 | from lib.utils.GANs_utils import shuffle_train_data 16 | 17 | class load_image(object): 18 | 19 | def __init__(self, img_dir, train_rate): 20 | self.img_dir = img_dir 21 | self.train_rate = train_rate 22 | self.train_imgs = [] 23 | self.train_labels = [] 24 | self.note_label = [] 25 | 26 | def _load_img_path(self, img_sub_dir, img_label): 27 | img_all_path = os.listdir(os.path.join(self.img_dir, img_sub_dir)) 28 | img_num = len(img_all_path) 29 | data = [] 30 | label = [] 31 | for i in range (img_num): 32 | img_path = os.path.join(self.img_dir, img_sub_dir, img_all_path[i]) 33 | #if cv2.imread(img_path) is not None: 34 | data.append(img_path) 35 | # print (img_path) 36 | label.append(int(img_label)) 37 | return data, label 38 | 39 | def _load_database_path(self): 40 | file_path = os.listdir(self.img_dir) 41 | for i, path in enumerate(file_path): 42 | if os.path.isfile(os.path.join(self.img_dir, path)): 43 | continue 44 | data, label = self._load_img_path(path, i) 45 | self.train_imgs.extend(data) 46 | self.train_labels.extend(label) 47 | self.note_label.append([path, i]) 48 | # print (path, i) 49 | self.train_imgs, self.train_labels = shuffle_train_data(self.train_imgs, self.train_labels) 50 | 51 | def gen_train_valid(self): 52 | self._load_database_path() 53 | image_n = len(self.train_imgs) 54 | train_n = int(image_n*self.train_rate) 55 | valid_n = int(image_n*(1-self.train_rate)) 56 | train_data, train_label = self.train_imgs[0:train_n], self.train_labels[0:train_n] 57 | valid_data, valid_label = self.train_imgs[train_n:image_n], self.train_labels[train_n:image_n] 58 | return train_data, train_label, valid_data, valid_label, train_n, valid_n, self.note_label 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /lib/data_load/data_load_from_txt_mullabel.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from lib.utils.GANs_utils import shuffle_train_data 10 | 11 | class data_load_from_txt_mullabel(object): 12 | 13 | def __init__(self, img_dir, train_rate): 14 | self.img_dir = img_dir 15 | self.train_imgs = [] 16 | self.train_labels = [] 17 | self.train_rate = train_rate 18 | self.note_label = [] 19 | 20 | def _gen_img_path(self): 21 | data_lines = open(self.img_dir, 'r').readlines() 22 | for line in data_lines: 23 | img_path = line.split(' ')[0] 24 | img_label = line.split(' ')[1] 25 | img_label = img_label.split('\n')[0] 26 | img_label = img_label.split(',') 27 | k = [] 28 | for label in img_label: 29 | k += [int(label)] 30 | self.train_imgs.append(img_path) 31 | self.train_labels.append(k) 32 | self.train_imgs, self.train_labels = shuffle_train_data(self.train_imgs, self.train_labels) 33 | 34 | def gen_train_valid(self): 35 | self._gen_img_path() 36 | image_n = len(self.train_imgs) 37 | train_n = int(image_n*self.train_rate) 38 | valid_n = int(image_n*(1-self.train_rate)) 39 | train_data, train_label = self.train_imgs[0:train_n], self.train_labels[0:train_n] 40 | valid_data, valid_label = self.train_imgs[train_n:image_n], self.train_labels[train_n:image_n] 41 | 42 | return train_data, train_label, valid_data, valid_label, train_n, valid_n, self.note_label 43 | 44 | # 以下测试用 45 | if __name__ == '__main__': 46 | data = data_load_from_txt('train.txt', 0.9) 47 | train_data, train_label, valid_data, valid_label, train_n, valid_n = data.gen_train_valid() 48 | print (train_data) 49 | print (train_label) 50 | -------------------------------------------------------------------------------- /lib/loss/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/loss/.DS_Store -------------------------------------------------------------------------------- /lib/loss/loss.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import tensorflow as tf 10 | import numpy as np 11 | 12 | 13 | # quared loss 14 | def squared_loss(label, logit): 15 | loss = tf.reduce_mean(tf.reduce_sum(tf.square(label - logit), 1)) 16 | return loss 17 | 18 | # sigmoid loss 19 | def sigmoid_loss(label, logit): 20 | loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = label, logits = logit)) 21 | return loss 22 | 23 | # softmax loss 24 | def softmax_loss(label, logit): 25 | loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = label, logits = logit)) 26 | return loss 27 | 28 | # triplet loss 29 | def triplet_loss(anchor, positive, negative, alpha): 30 | # 理论可以看这里: https://blog.csdn.net/tangwei2014/article/details/46788025 31 | # facenet理解: http://www.mamicode.com/info-detail-2096766.html 32 | """Calculate the triplet loss according to the FaceNet paper 33 | 34 | Args: 35 | anchor: the embeddings for the anchor images. 36 | positive: the embeddings for the positive images. 37 | negative: the embeddings for the negative images. 38 | 39 | Returns: 40 | the triplet loss according to the FaceNet paper as a float tensor. 41 | """ 42 | with tf.variable_scope('triplet_loss'): 43 | pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1) 44 | neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1) 45 | 46 | basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha) 47 | loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0) 48 | 49 | return loss 50 | 51 | 52 | # center loss 53 | def center_loss(features, label, alfa, nrof_classes): 54 | """Center loss based on the paper "A Discriminative Feature Learning Approach for Deep Face Recognition" 55 | (http://ydwen.github.io/papers/WenECCV16.pdf) 56 | """ 57 | """获取center loss及center的更新op 58 | 还可参考博客: https://blog.csdn.net/u014365862/article/details/79184966 59 | Arguments: 60 | features: Tensor,表征样本特征,一般使用某个fc层的输出,shape应该为[batch_size, feature_length]. 61 | labels: Tensor,表征样本label,非one-hot编码,shape应为[batch_size]. 62 | alpha: 0-1之间的数字,控制样本类别中心的学习率,细节参考原文. 63 | num_classes: 整数,表明总共有多少个类别,网络分类输出有多少个神经元这里就取多少. 64 | 65 | Return: 66 | loss: Tensor,可与softmax loss相加作为总的loss进行优化. 67 | centers: Tensor,存储样本中心值的Tensor,仅查看样本中心存储的具体数值时有用. 68 | centers_update_op: op,用于更新样本中心的op,在训练时需要同时运行该op,否则样本中心不会更新 69 | """ 70 | nrof_features = features.get_shape()[1] 71 | centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32, 72 | initializer=tf.constant_initializer(0), trainable=False) 73 | label = tf.reshape(label, [-1]) 74 | centers_batch = tf.gather(centers, label) 75 | diff = (1 - alfa) * (centers_batch - features) 76 | centers = tf.scatter_sub(centers, label, diff) 77 | loss = tf.reduce_mean(tf.square(features - centers_batch)) 78 | return loss, centers 79 | 80 | 81 | def get_center_loss(features, labels, alpha, num_classes): 82 | """获取center loss及center的更新op 83 | 84 | Arguments: 85 | features: Tensor,表征样本特征,一般使用某个fc层的输出,shape应该为[batch_size, feature_length]. 86 | labels: Tensor,表征样本label,非one-hot编码,shape应为[batch_size]. 87 | alpha: 0-1之间的数字,控制样本类别中心的学习率,细节参考原文. 88 | num_classes: 整数,表明总共有多少个类别,网络分类输出有多少个神经元这里就取多少. 89 | 90 | Return: 91 | loss: Tensor,可与softmax loss相加作为总的loss进行优化. 92 | centers: Tensor,存储样本中心值的Tensor,仅查看样本中心存储的具体数值时有用. 93 | centers_update_op: op,用于更新样本中心的op,在训练时需要同时运行该op,否则样本中心不会更新 94 | """ 95 | # 获取特征的维数,例如256维 96 | len_features = features.get_shape()[1] 97 | # 建立一个Variable,shape为[num_classes, len_features],用于存储整个网络的样本中心, 98 | # 设置trainable=False是因为样本中心不是由梯度进行更新的 99 | centers = tf.get_variable('centers', [num_classes, len_features], dtype=tf.float32, 100 | initializer=tf.constant_initializer(0), trainable=False) 101 | # 将label展开为一维的,输入如果已经是一维的,则该动作其实无必要 102 | # labels = tf.reshape(labels, [-1]) 103 | labels = tf.argmax(labels, 1) 104 | 105 | # 根据样本label,获取mini-batch中每一个样本对应的中心值 106 | centers_batch = tf.gather(centers, labels) 107 | # 计算loss 108 | loss = tf.nn.l2_loss(features - centers_batch) 109 | 110 | # 当前mini-batch的特征值与它们对应的中心值之间的差 111 | diff = centers_batch - features 112 | 113 | # 获取mini-batch中同一类别样本出现的次数,了解原理请参考原文公式(4) 114 | unique_label, unique_idx, unique_count = tf.unique_with_counts(labels) 115 | appear_times = tf.gather(unique_count, unique_idx) 116 | appear_times = tf.reshape(appear_times, [-1, 1]) 117 | 118 | diff = diff / tf.cast((1 + appear_times), tf.float32) 119 | diff = alpha * diff 120 | 121 | centers_update_op = tf.scatter_sub(centers, labels, diff) 122 | 123 | return loss, centers, centers_update_op 124 | 125 | 126 | # 加入L2正则化的loss 127 | def add_l2(loss, weight_decay): 128 | l2_losses = [weight_decay * tf.nn.l2_loss(v) for v in tf.trainable_variables() if 'weights' in v.name] 129 | reduced_loss = tf.reduce_mean(loss) + tf.add_n(l2_losses) 130 | return reduced_loss 131 | 132 | -------------------------------------------------------------------------------- /lib/model/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/.DS_Store -------------------------------------------------------------------------------- /lib/model/build_net/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/build_net/.DS_Store -------------------------------------------------------------------------------- /lib/model/build_net/arch_net.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | from math import log 14 | 15 | from six.moves import xrange 16 | import tensorflow as tf 17 | slim = tf.contrib.slim 18 | 19 | def dcgan_arg_scope(weight_decay=0.00004, 20 | use_batch_norm=True, 21 | batch_norm_decay=0.9997, 22 | batch_norm_epsilon=0.001): 23 | """Defines the default arg scope for inception models. 24 | Args: 25 | weight_decay: The weight decay to use for regularizing the model. 26 | use_batch_norm: "If `True`, batch_norm is applied after each convolution. 27 | batch_norm_decay: Decay for batch norm moving average. 28 | batch_norm_epsilon: Small float added to variance to avoid dividing by zero 29 | in batch norm. 30 | Returns: 31 | An `arg_scope` to use for the inception models. 32 | """ 33 | batch_norm_params = { 34 | # Decay for the moving averages. 35 | 'decay': batch_norm_decay, 36 | # epsilon to prevent 0s in variance. 37 | 'epsilon': batch_norm_epsilon, 38 | # collection containing update_ops. 39 | 'updates_collections': tf.GraphKeys.UPDATE_OPS, 40 | } 41 | if use_batch_norm: 42 | normalizer_fn = slim.batch_norm 43 | normalizer_params = batch_norm_params 44 | else: 45 | normalizer_fn = None 46 | normalizer_params = {} 47 | # Set weight_decay for weights in Conv and FC layers. 48 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 49 | weights_regularizer=slim.l2_regularizer(weight_decay)): 50 | with slim.arg_scope( 51 | [slim.conv2d], 52 | weights_initializer=slim.variance_scaling_initializer(), 53 | activation_fn=tf.nn.relu, 54 | normalizer_fn=normalizer_fn, 55 | normalizer_params=normalizer_params) as sc: 56 | return sc 57 | 58 | 59 | def _validate_image_inputs(inputs): 60 | inputs.get_shape().assert_has_rank(4) 61 | inputs.get_shape()[1:3].assert_is_fully_defined() 62 | if inputs.get_shape()[1] != inputs.get_shape()[2]: 63 | raise ValueError('Input tensor does not have equal width and height: ', 64 | inputs.get_shape()[1:3]) 65 | width = inputs.get_shape().as_list()[1] 66 | if log(width, 2) != int(log(width, 2)): 67 | raise ValueError('Input tensor `width` is not a power of 2: ', width) 68 | 69 | 70 | # TODO(joelshor): Use fused batch norm by default. Investigate why some GAN 71 | # setups need the gradient of gradient FusedBatchNormGrad. 72 | def discriminator(inputs, 73 | depth=64, 74 | is_training=True, 75 | reuse=None, 76 | scope='Discriminator', 77 | fused_batch_norm=False, 78 | conditional = False): 79 | """Discriminator network for DCGAN. 80 | Construct discriminator network from inputs to the final endpoint. 81 | Args: 82 | inputs: A tensor of size [batch_size, height, width, channels]. Must be 83 | floating point. 84 | depth: Number of channels in first convolution layer. 85 | is_training: Whether the network is for training or not. 86 | reuse: Whether or not the network variables should be reused. `scope` 87 | must be given to be reused. 88 | scope: Optional variable_scope. 89 | fused_batch_norm: If `True`, use a faster, fused implementation of 90 | batch norm. 91 | Returns: 92 | logits: The pre-softmax activations, a tensor of size [batch_size, 1] 93 | end_points: a dictionary from components of the network to their activation. 94 | Raises: 95 | ValueError: If the input image shape is not 4-dimensional, if the spatial 96 | dimensions aren't defined at graph construction time, if the spatial 97 | dimensions aren't square, or if the spatial dimensions aren't a power of 98 | two. 99 | """ 100 | 101 | normalizer_fn = slim.batch_norm 102 | normalizer_fn_args = { 103 | 'is_training': is_training, 104 | 'zero_debias_moving_mean': True, 105 | 'fused': fused_batch_norm, 106 | } 107 | 108 | _validate_image_inputs(inputs) 109 | inp_shape = inputs.get_shape().as_list()[1] 110 | 111 | end_points = {} 112 | with tf.variable_scope(scope, values=[inputs], reuse=reuse) as scope: 113 | with slim.arg_scope([normalizer_fn], **normalizer_fn_args): 114 | with slim.arg_scope([slim.conv2d], 115 | stride=2, 116 | kernel_size=4, 117 | activation_fn=tf.nn.leaky_relu): 118 | net = inputs 119 | for i in xrange(int(log(inp_shape, 2))): 120 | scope = 'conv%i' % (i + 1) 121 | current_depth = depth * 2**i 122 | normalizer_fn_ = None if i == 0 else None #normalizer_fn 123 | net = slim.conv2d( net, current_depth, normalizer_fn=normalizer_fn_, scope=scope) 124 | end_points[scope] = net 125 | 126 | logits = slim.conv2d(net, 1, kernel_size=1, stride=1, padding='VALID', normalizer_fn=None, activation_fn=None) 127 | logits = tf.reshape(logits, [-1, 1]) 128 | if conditional: 129 | logits = net 130 | end_points['logits'] = logits 131 | 132 | return logits, end_points 133 | 134 | 135 | # TODO(joelshor): Use fused batch norm by default. Investigate why some GAN 136 | # setups need the gradient of gradient FusedBatchNormGrad. 137 | def generator(inputs, 138 | depth=64, 139 | final_size=32, 140 | num_outputs=3, 141 | is_training=True, 142 | reuse=None, 143 | scope='Generator', 144 | fused_batch_norm=False): 145 | """Generator network for DCGAN. 146 | Construct generator network from inputs to the final endpoint. 147 | Args: 148 | inputs: A tensor with any size N. [batch_size, N] 149 | depth: Number of channels in last deconvolution layer. 150 | final_size: The shape of the final output. 151 | num_outputs: Number of output features. For images, this is the number of 152 | channels. 153 | is_training: whether is training or not. 154 | reuse: Whether or not the network has its variables should be reused. scope 155 | must be given to be reused. 156 | scope: Optional variable_scope. 157 | fused_batch_norm: If `True`, use a faster, fused implementation of 158 | batch norm. 159 | Returns: 160 | logits: the pre-softmax activations, a tensor of size 161 | [batch_size, 32, 32, channels] 162 | end_points: a dictionary from components of the network to their activation. 163 | Raises: 164 | ValueError: If `inputs` is not 2-dimensional. 165 | ValueError: If `final_size` isn't a power of 2 or is less than 8. 166 | """ 167 | normalizer_fn = slim.batch_norm 168 | normalizer_fn_args = { 169 | 'is_training': is_training, 170 | 'zero_debias_moving_mean': True, 171 | 'fused': fused_batch_norm, 172 | } 173 | 174 | inputs.get_shape().assert_has_rank(2) 175 | if log(final_size, 2) != int(log(final_size, 2)): 176 | raise ValueError('`final_size` (%i) must be a power of 2.' % final_size) 177 | if final_size < 8: 178 | raise ValueError('`final_size` (%i) must be greater than 8.' % final_size) 179 | 180 | end_points = {} 181 | num_layers = int(log(final_size, 2)) - 1 182 | with tf.variable_scope(scope, values=[inputs], reuse=reuse) as scope: 183 | with slim.arg_scope([normalizer_fn], **normalizer_fn_args): 184 | with slim.arg_scope([slim.conv2d_transpose], 185 | #normalizer_fn=normalizer_fn, 186 | stride=2, 187 | kernel_size=4): 188 | net = tf.expand_dims(tf.expand_dims(inputs, 1), 1) 189 | 190 | # First upscaling is different because it takes the input vector. 191 | current_depth = depth * 2 ** (num_layers - 1) 192 | scope = 'deconv1' 193 | net = slim.conv2d_transpose( net, current_depth, stride=1, padding='VALID', scope=scope) 194 | end_points[scope] = net 195 | 196 | for i in xrange(2, num_layers): 197 | scope = 'deconv%i' % (i) 198 | current_depth = depth * 2 ** (num_layers - i) 199 | net = slim.conv2d_transpose(net, current_depth, scope=scope) 200 | end_points[scope] = net 201 | 202 | # Last layer has different normalizer and activation. 203 | scope = 'deconv%i' % (num_layers) 204 | net = slim.conv2d_transpose( net, depth, normalizer_fn=None, activation_fn=None, scope=scope) 205 | end_points[scope] = net 206 | 207 | # Convert to proper channels. 208 | scope = 'logits' 209 | logits = slim.conv2d( 210 | net, 211 | num_outputs, 212 | normalizer_fn=None, 213 | activation_fn=None, 214 | kernel_size=1, 215 | stride=1, 216 | padding='VALID', 217 | scope=scope) 218 | end_points[scope] = logits 219 | 220 | logits.get_shape().assert_has_rank(4) 221 | logits.get_shape().assert_is_compatible_with( 222 | [None, final_size, final_size, num_outputs]) 223 | return logits, end_points 224 | -------------------------------------------------------------------------------- /lib/model/build_net/networks.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | from lib.model.build_net import arch_net as dcgan 16 | from lib.model.build_net.arch_net import dcgan_arg_scope 17 | 18 | tfgan = tf.contrib.gan 19 | slim = tf.contrib.slim 20 | 21 | 22 | def _last_conv_layer(end_points): 23 | """"Returns the last convolutional layer from an endpoints dictionary.""" 24 | conv_list = [k if k[:4] == 'conv' else None for k in end_points.keys()] 25 | # conv_list.sort() 26 | return end_points[conv_list[-1]] 27 | 28 | 29 | def generator(noise, is_training=True): 30 | """Generator to produce CIFAR images. 31 | Args: 32 | noise: A 2D Tensor of shape [batch size, noise dim]. Since this example 33 | does not use conditioning, this Tensor represents a noise vector of some 34 | kind that will be reshaped by the generator into CIFAR examples. 35 | Returns: 36 | A single Tensor with a batch of generated CIFAR images. 37 | """ 38 | # arg_scope = dcgan_arg_scope() 39 | # with slim.arg_scope(arg_scope): 40 | images, _ = dcgan.generator(noise, is_training=is_training) 41 | 42 | # Make sure output lies between [-1, 1]. 43 | return tf.tanh(images), _ 44 | 45 | 46 | def conditional_generator(inputs, is_training=True): 47 | """Generator to produce CIFAR images. 48 | Args: 49 | inputs: A 2-tuple of Tensors (noise, one_hot_labels) and creates a 50 | conditional generator. 51 | Returns: 52 | A single Tensor with a batch of generated CIFAR images. 53 | """ 54 | noise, one_hot_labels = inputs 55 | noise = tfgan.features.condition_tensor_from_onehot(noise, one_hot_labels) 56 | 57 | images, _ = dcgan.generator(noise, is_training=is_training) 58 | 59 | # Make sure output lies between [-1, 1]. 60 | return tf.tanh(images), _ 61 | 62 | 63 | def discriminator(img, is_training=True): 64 | """Discriminator for CIFAR images. 65 | Args: 66 | img: A Tensor of shape [batch size, width, height, channels], that can be 67 | either real or generated. It is the discriminator's goal to distinguish 68 | between the two. 69 | unused_conditioning: The TFGAN API can help with conditional GANs, which 70 | would require extra `condition` information to both the generator and the 71 | discriminator. Since this example is not conditional, we do not use this 72 | argument. 73 | Returns: 74 | A 1D Tensor of shape [batch size] representing the confidence that the 75 | images are real. The output can lie in [-inf, inf], with positive values 76 | indicating high confidence that the images are real. 77 | """ 78 | logits, _ = dcgan.discriminator(img, is_training=is_training) 79 | return logits , _ 80 | 81 | 82 | # (joelshor): This discriminator creates variables that aren't used, and 83 | # causes logging warnings. Improve `dcgan` nets to accept a target end layer, 84 | # so extraneous variables aren't created. 85 | def conditional_discriminator(img, conditioning, is_training=True): 86 | """Discriminator for CIFAR images. 87 | Args: 88 | img: A Tensor of shape [batch size, width, height, channels], that can be 89 | either real or generated. It is the discriminator's goal to distinguish 90 | between the two. 91 | conditioning: A 2-tuple of Tensors representing (noise, one_hot_labels). 92 | Returns: 93 | A 1D Tensor of shape [batch size] representing the confidence that the 94 | images are real. The output can lie in [-inf, inf], with positive values 95 | indicating high confidence that the images are real. 96 | """ 97 | logits, end_points = dcgan.discriminator(img, is_training=is_training, conditional=True) 98 | # Condition the last convolution layer. 99 | _, one_hot_labels = conditioning 100 | # print (end_points.keys) 101 | # net = _last_conv_layer(end_points) 102 | net = logits 103 | net = tfgan.features.condition_tensor_from_onehot( 104 | tf.contrib.layers.flatten(net), one_hot_labels) 105 | logits = tf.contrib.layers.linear(net, 1) 106 | 107 | return logits, _ 108 | -------------------------------------------------------------------------------- /lib/model/discriminator/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/discriminator/.DS_Store -------------------------------------------------------------------------------- /lib/model/discriminator/build_discriminator/__pycache__/build_discriminator.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/discriminator/build_discriminator/__pycache__/build_discriminator.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/discriminator/build_discriminator/build_discriminator.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import tensorflow as tf 11 | slim = tf.contrib.slim 12 | import numpy as np 13 | import argparse 14 | import os 15 | from PIL import Image 16 | from datetime import datetime 17 | import math 18 | import time 19 | import cv2 20 | # from lib.utils.gans_utils import leaky_relu 21 | 22 | def fully_connected(prev_layer, num_units, is_training): 23 | 24 | layer = tf.layers.dense(prev_layer, num_units, use_bias=False, activation=None) 25 | layer = tf.layers.batch_normalization(layer, training=is_training) 26 | layer = tf.nn.relu(layer) 27 | return layer 28 | 29 | 30 | def conv_layer(prev_layer, layer_depth, is_training): 31 | 32 | strides = 2 if layer_depth % 3 == 0 else 1 33 | conv_layer = tf.layers.conv2d(prev_layer, layer_depth*16, 3, strides, 'same', use_bias=True, activation=None) 34 | conv_layer = tf.layers.batch_normalization(conv_layer, training=is_training) 35 | conv_layer = tf.nn.relu(conv_layer) 36 | 37 | return conv_layer 38 | 39 | def unpool(inputs,scale): 40 | return tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*scale, tf.shape(inputs)[2]*scale]) 41 | 42 | def ResidualConvUnit(inputs,features=256,kernel_size=3): 43 | net=tf.nn.relu(inputs) 44 | net=slim.conv2d(net, features, kernel_size) 45 | net=tf.nn.relu(net) 46 | net=slim.conv2d(net,features,kernel_size) 47 | net=tf.add(net,inputs) 48 | return net 49 | def MultiResolutionFusion(high_inputs=None,low_inputs=None,features=256): 50 | 51 | if high_inputs is None:#refineNet block 4 52 | rcu_low_1 = low_inputs[0] 53 | rcu_low_2 = low_inputs[1] 54 | 55 | rcu_low_1 = slim.conv2d(rcu_low_1, features, 3) 56 | rcu_low_2 = slim.conv2d(rcu_low_2, features, 3) 57 | 58 | return tf.add(rcu_low_1,rcu_low_2) 59 | 60 | class discriminator_arch(object): 61 | def __inint__(self): 62 | pass 63 | 64 | def discriminator(self, x): 65 | """Compute discriminator score for a batch of input images. 66 | 67 | Inputs: 68 | - x: TensorFlow Tensor of flattened input images, shape [batch_size, 784] 69 | 70 | Returns: 71 | TensorFlow Tensor with shape [batch_size, 1], containing the score 72 | for an image being real for each input image. 73 | """ 74 | with tf.variable_scope("discriminator"): 75 | # TODO: implement architecture 76 | dense_1 = tf.layers.dense(inputs=x,units=256,use_bias=True) 77 | # relu_1=leaky_relu(dense_1, alpha=0.01) 78 | relu_1=tf.nn.relu(dense_1) 79 | dense_2 = tf.layers.dense(inputs=relu_1,units=256,use_bias=True) 80 | # relu_2=leaky_relu(dense_2, alpha=0.01) 81 | relu_2 = tf.nn.relu(dense_2) 82 | dense_3 = tf.layers.dense(inputs=relu_2,units=1,use_bias=True) 83 | 84 | logits=dense_3 85 | return logits, dense_3 86 | 87 | def discriminator_conv(self, x): 88 | """Compute discriminator score for a batch of input images. 89 | 90 | Inputs: 91 | - x: TensorFlow Tensor of flattened input images, shape [batch_size, 784] 92 | 93 | Returns: 94 | TensorFlow Tensor with shape [batch_size, 1], containing the score 95 | for an image being real for each input image. 96 | """ 97 | with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): 98 | with tf.variable_scope("discriminator"): 99 | for layer_i in [1,2,4]: 100 | for n in range(1): 101 | layer = conv_layer(layer, layer_i, is_training) 102 | layer = tf.nn.max_pool(layer, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') 103 | 104 | net = unpool(layer, 2) 105 | net = tf.layers.conv2d(net, 256, 3, 1, 'SAME', use_bias=True, activation=None) 106 | net = ResidualConvUnit(net, 256) 107 | net=tf.nn.relu(net) 108 | 109 | net = unpool(layer, 2) 110 | net = tf.layers.conv2d(net, 128, 3, 1, 'SAME', use_bias=True, activation=None) 111 | net = ResidualConvUnit(net, 128) 112 | net=tf.nn.relu(net) 113 | 114 | net = unpool(layer, 2) 115 | net = tf.layers.conv2d(net, 64, 3, 1, 'SAME', use_bias=True, activation=None) 116 | net = ResidualConvUnit(net, 64) 117 | net=tf.nn.relu(net) 118 | 119 | net = tf.layers.conv2d(net, num_classes, 3, 1, 'SAME', use_bias=True, activation=None) 120 | 121 | return net, layer 122 | 123 | 124 | -------------------------------------------------------------------------------- /lib/model/generator/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/alexnet/__pycache__/alexnet.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/alexnet/__pycache__/alexnet.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/alexnet/alexnet.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | slim = tf.contrib.slim 16 | trunc_normal = lambda stddev: tf.truncated_normal_initializer(0.0, stddev) 17 | 18 | 19 | def alexnet_v2_arg_scope(weight_decay=0.0005): 20 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 21 | activation_fn=tf.nn.relu, 22 | biases_initializer=tf.constant_initializer(0.1), 23 | weights_regularizer=slim.l2_regularizer(weight_decay)): 24 | with slim.arg_scope([slim.conv2d], padding='SAME'): 25 | with slim.arg_scope([slim.max_pool2d], padding='VALID') as arg_sc: 26 | return arg_sc 27 | 28 | 29 | def alexnet_v2(inputs, 30 | num_classes=None, 31 | is_training=True, 32 | dropout_keep_prob=0.5, 33 | spatial_squeeze=True, 34 | scope='alexnet_v2'): 35 | """AlexNet version 2. 36 | 37 | Described in: http://arxiv.org/pdf/1404.5997v2.pdf 38 | Parameters from: 39 | github.com/akrizhevsky/cuda-convnet2/blob/master/layers/ 40 | layers-imagenet-1gpu.cfg 41 | 42 | Note: All the fully_connected layers have been transformed to conv2d layers. 43 | To use in classification mode, resize input to 224x224. To use in fully 44 | convolutional mode, set spatial_squeeze to false. 45 | The LRN layers have been removed and change the initializers from 46 | random_normal_initializer to xavier_initializer. 47 | 48 | Args: 49 | inputs: a tensor of size [batch_size, height, width, channels]. 50 | num_classes: number of predicted classes. 51 | is_training: whether or not the model is being trained. 52 | dropout_keep_prob: the probability that activations are kept in the dropout 53 | layers during training. 54 | spatial_squeeze: whether or not should squeeze the spatial dimensions of the 55 | outputs. Useful to remove unnecessary dimensions for classification. 56 | scope: Optional scope for the variables. 57 | 58 | Returns: 59 | the last op containing the log predictions and end_points dict. 60 | """ 61 | with tf.variable_scope(scope, 'alexnet_v2', [inputs]) as sc: 62 | end_points_collection = sc.name + '_end_points' 63 | # Collect outputs for conv2d, fully_connected and max_pool2d. 64 | with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], 65 | outputs_collections=[end_points_collection]): 66 | net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID', 67 | scope='conv1') 68 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool1') 69 | net = slim.conv2d(net, 192, [5, 5], scope='conv2') 70 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool2') 71 | net = slim.conv2d(net, 384, [3, 3], scope='conv3') 72 | net = slim.conv2d(net, 384, [3, 3], scope='conv4') 73 | net = slim.conv2d(net, 256, [3, 3], scope='conv5') 74 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool5') 75 | 76 | # Use conv2d instead of fully_connected layers. 77 | with slim.arg_scope([slim.conv2d], 78 | weights_initializer=trunc_normal(0.005), 79 | biases_initializer=tf.constant_initializer(0.1)): 80 | net = slim.conv2d(net, 4096, [5, 5], padding='VALID', 81 | scope='fc6') 82 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 83 | scope='dropout6') 84 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 85 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 86 | scope='dropout7') 87 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 88 | if num_classes is not None: 89 | net = slim.conv2d(net, num_classes, [1, 1], 90 | activation_fn=None, 91 | normalizer_fn=None, 92 | biases_initializer=tf.zeros_initializer(), 93 | scope='fc8') 94 | 95 | # Convert end_points_collection into a end_point dict. 96 | if spatial_squeeze: 97 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 98 | end_points[sc.name + '/fc8'] = net 99 | else: 100 | net = net 101 | end_points = end_points 102 | return net, end_points 103 | alexnet_v2.default_image_size = 224 104 | -------------------------------------------------------------------------------- /lib/model/generator/attention/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/attention/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/attention/__pycache__/attention.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/attention/__pycache__/attention.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/attention/attention.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | 3 | import tensorflow as tf 4 | 5 | def attention(inputs, attention_size, time_major=False): 6 | if isinstance(inputs, tuple): 7 | # In case of Bi-RNN, concatenate the forward and the backward RNN outputs. 8 | inputs = tf.concat(inputs, 2) 9 | 10 | if time_major: 11 | # (T,B,D) => (B,T,D) 12 | inputs = tf.transpose(inputs, [1, 0, 2]) 13 | 14 | inputs_shape = inputs.shape 15 | sequence_length = inputs_shape[1].value # the length of sequences processed in the antecedent RNN layer 16 | hidden_size = inputs_shape[2].value # hidden size of the RNN layer 17 | 18 | # Attention mechanism 19 | W_omega = tf.Variable(tf.random_normal([hidden_size, attention_size], stddev=0.1)) 20 | b_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) 21 | u_omega = tf.Variable(tf.random_normal([attention_size], stddev=0.1)) 22 | 23 | v = tf.tanh(tf.matmul(tf.reshape(inputs, [-1, hidden_size]), W_omega) + tf.reshape(b_omega, [1, -1])) 24 | vu = tf.matmul(v, tf.reshape(u_omega, [-1, 1])) 25 | exps = tf.reshape(tf.exp(vu), [-1, sequence_length]) 26 | alphas = exps / tf.reshape(tf.reduce_sum(exps, 1), [-1, 1]) 27 | 28 | # Output of Bi-RNN is reduced with attention vector 29 | output = tf.reduce_sum(inputs * tf.reshape(alphas, [-1, sequence_length, 1]), 1) 30 | 31 | return output 32 | -------------------------------------------------------------------------------- /lib/model/generator/build_generator/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/build_generator/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/build_generator/__pycache__/build_generator.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/build_generator/__pycache__/build_generator.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/build_generator/__pycache__/build_net.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/build_generator/__pycache__/build_net.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/build_generator/build_generator.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import tensorflow as tf 11 | slim = tf.contrib.slim 12 | import numpy as np 13 | import argparse 14 | import os 15 | from PIL import Image 16 | from datetime import datetime 17 | import math 18 | import time 19 | import cv2 20 | from keras.utils import np_utils 21 | 22 | # inception_v4 23 | try: 24 | from inception_v4 import inception_v4_arg_scope, inception_v4 25 | except: 26 | from lib.model.generator.inception_v4.inception_v4 import inception_v4_arg_scope, inception_v4 27 | # resnet_v2_50, resnet_v2_101, resnet_v2_152 28 | try: 29 | from resnet_v2 import resnet_arg_scope, resnet_v2_50 30 | except: 31 | from lib.model.generator.resnet_v2.resnet_v2 import resnet_arg_scope, resnet_v2_50 32 | # vgg16, vgg19 33 | try: 34 | from vgg import vgg_arg_scope, vgg_16, vgg_16_conv 35 | except: 36 | from lib.model.generator.vgg.vgg import vgg_arg_scope, vgg_16, vgg_16_conv 37 | 38 | try: 39 | from alexnet import alexnet_v2_arg_scope, alexnet_v2 40 | except: 41 | from lib.model.generator.alexnet.alexnet import alexnet_v2_arg_scope, alexnet_v2 42 | 43 | try: 44 | from lp_net import lp_net, lp_net_arg_scope 45 | except: 46 | from lib.model.generator.lp_net.lp_net import lp_net, lp_net_arg_scope 47 | 48 | try: 49 | from attention import attention 50 | except: 51 | from lib.model.generator.attention.attention import attention 52 | 53 | 54 | class generator_arch(object): 55 | 56 | def __init__(self): 57 | pass 58 | 59 | def generator(self, z): 60 | """Generate images from a random noise vector. 61 | 62 | Inputs: 63 | - z: TensorFlow Tensor of random noise with shape [batch_size, noise_dim] 64 | 65 | Returns: 66 | TensorFlow Tensor of generated images, with shape [batch_size, 784]. 67 | """ 68 | with tf.variable_scope("generator"): 69 | # TODO: implement architecture 70 | dense_1 = tf.layers.dense(inputs=z,units=1024,activation=tf.nn.relu,use_bias=True) 71 | dense_2 = tf.layers.dense(inputs=dense_1,units=1024,activation=tf.nn.relu,use_bias=True) 72 | dense_3 = tf.layers.dense(inputs=dense_2,units=784,use_bias=True) 73 | img=tf.tanh(dense_3) 74 | return img, dense_3 75 | 76 | def generator_conv(self, z): 77 | """Generate images from a random noise vector. 78 | 79 | Inputs: 80 | - z: TensorFlow Tensor of random noise with shape [batch_size, noise_dim] 81 | 82 | Returns: 83 | TensorFlow Tensor of generated images, with shape [batch_size, 784]. 84 | """ 85 | with tf.variable_scope("generator"): 86 | # TODO: implement architecture 87 | net = tf.layers.dense(inputs=z,units=1024,activation=tf.nn.relu,use_bias=True) 88 | net = tf.reshape(net, [-1, 16, 16, 4]) 89 | 90 | net = tf.layers.conv2d(net, 16, 3, 1, 'same', use_bias=True, activation=None) 91 | net = tf.layers.batch_normalization(net, training=is_training) 92 | net = tf.nn.relu(net) 93 | 94 | net = tf.layers.conv2d(net, 16, 3, 1, 'same', use_bias=True, activation=None) 95 | net = tf.layers.batch_normalization(net, training=is_training) 96 | net = tf.nn.relu(net) 97 | 98 | net = tf.layers.conv2d(net, 3, 3, 1, 'same', use_bias=True, activation=None) 99 | net = tf.layers.batch_normalization(net, training=is_training) 100 | net = tf.nn.relu(net) 101 | 102 | img=tf.tanh(net) 103 | return img, dense_3 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /lib/model/generator/cifarnet/cifarnet.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | slim = tf.contrib.slim 16 | 17 | trunc_normal = lambda stddev: tf.truncated_normal_initializer(stddev=stddev) 18 | 19 | 20 | def cifarnet(images, num_classes=10, is_training=False, 21 | dropout_keep_prob=0.5, 22 | prediction_fn=slim.softmax, 23 | scope='CifarNet'): 24 | """Creates a variant of the CifarNet model. 25 | 26 | Note that since the output is a set of 'logits', the values fall in the 27 | interval of (-infinity, infinity). Consequently, to convert the outputs to a 28 | probability distribution over the characters, one will need to convert them 29 | using the softmax function: 30 | 31 | logits = cifarnet.cifarnet(images, is_training=False) 32 | probabilities = tf.nn.softmax(logits) 33 | predictions = tf.argmax(logits, 1) 34 | 35 | Args: 36 | images: A batch of `Tensors` of size [batch_size, height, width, channels]. 37 | num_classes: the number of classes in the dataset. 38 | is_training: specifies whether or not we're currently training the model. 39 | This variable will determine the behaviour of the dropout layer. 40 | dropout_keep_prob: the percentage of activation values that are retained. 41 | prediction_fn: a function to get predictions out of logits. 42 | scope: Optional variable_scope. 43 | 44 | Returns: 45 | logits: the pre-softmax activations, a tensor of size 46 | [batch_size, `num_classes`] 47 | end_points: a dictionary from components of the network to the corresponding 48 | activation. 49 | """ 50 | end_points = {} 51 | 52 | with tf.variable_scope(scope, 'CifarNet', [images, num_classes]): 53 | net = slim.conv2d(images, 64, [5, 5], scope='conv1') 54 | end_points['conv1'] = net 55 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool1') 56 | end_points['pool1'] = net 57 | net = tf.nn.lrn(net, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1') 58 | net = slim.conv2d(net, 64, [5, 5], scope='conv2') 59 | end_points['conv2'] = net 60 | net = tf.nn.lrn(net, 4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm2') 61 | net = slim.max_pool2d(net, [2, 2], 2, scope='pool2') 62 | end_points['pool2'] = net 63 | net = slim.flatten(net) 64 | end_points['Flatten'] = net 65 | net = slim.fully_connected(net, 384, scope='fc3') 66 | end_points['fc3'] = net 67 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 68 | scope='dropout3') 69 | net = slim.fully_connected(net, 192, scope='fc4') 70 | end_points['fc4'] = net 71 | logits = slim.fully_connected(net, num_classes, 72 | biases_initializer=tf.zeros_initializer(), 73 | weights_initializer=trunc_normal(1/192.0), 74 | weights_regularizer=None, 75 | activation_fn=None, 76 | scope='logits') 77 | 78 | end_points['Logits'] = logits 79 | end_points['Predictions'] = prediction_fn(logits, scope='Predictions') 80 | 81 | return logits, end_points 82 | cifarnet.default_image_size = 32 83 | 84 | 85 | def cifarnet_arg_scope(weight_decay=0.004): 86 | """Defines the default cifarnet argument scope. 87 | 88 | Args: 89 | weight_decay: The weight decay to use for regularizing the model. 90 | 91 | Returns: 92 | An `arg_scope` to use for the inception v3 model. 93 | """ 94 | with slim.arg_scope( 95 | [slim.conv2d], 96 | weights_initializer=tf.truncated_normal_initializer(stddev=5e-2), 97 | activation_fn=tf.nn.relu): 98 | with slim.arg_scope( 99 | [slim.fully_connected], 100 | biases_initializer=tf.constant_initializer(0.1), 101 | weights_initializer=trunc_normal(0.04), 102 | weights_regularizer=slim.l2_regularizer(weight_decay), 103 | activation_fn=tf.nn.relu) as sc: 104 | return sc 105 | -------------------------------------------------------------------------------- /lib/model/generator/inception_resnet_v2/inception_resnet_v2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | """ 6 | 7 | from __future__ import absolute_import 8 | from __future__ import division 9 | from __future__ import print_function 10 | 11 | 12 | import tensorflow as tf 13 | 14 | slim = tf.contrib.slim 15 | 16 | 17 | def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): 18 | """Builds the 35x35 resnet block.""" 19 | with tf.variable_scope(scope, 'Block35', [net], reuse=reuse): 20 | with tf.variable_scope('Branch_0'): 21 | tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1') 22 | with tf.variable_scope('Branch_1'): 23 | tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') 24 | tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3') 25 | with tf.variable_scope('Branch_2'): 26 | tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1') 27 | tower_conv2_1 = slim.conv2d(tower_conv2_0, 48, 3, scope='Conv2d_0b_3x3') 28 | tower_conv2_2 = slim.conv2d(tower_conv2_1, 64, 3, scope='Conv2d_0c_3x3') 29 | mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2]) 30 | up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, 31 | activation_fn=None, scope='Conv2d_1x1') 32 | net += scale * up 33 | if activation_fn: 34 | net = activation_fn(net) 35 | return net 36 | 37 | 38 | def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): 39 | """Builds the 17x17 resnet block.""" 40 | with tf.variable_scope(scope, 'Block17', [net], reuse=reuse): 41 | with tf.variable_scope('Branch_0'): 42 | tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1') 43 | with tf.variable_scope('Branch_1'): 44 | tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1') 45 | tower_conv1_1 = slim.conv2d(tower_conv1_0, 160, [1, 7], 46 | scope='Conv2d_0b_1x7') 47 | tower_conv1_2 = slim.conv2d(tower_conv1_1, 192, [7, 1], 48 | scope='Conv2d_0c_7x1') 49 | mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2]) 50 | up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, 51 | activation_fn=None, scope='Conv2d_1x1') 52 | net += scale * up 53 | if activation_fn: 54 | net = activation_fn(net) 55 | return net 56 | 57 | 58 | def block8(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None): 59 | """Builds the 8x8 resnet block.""" 60 | with tf.variable_scope(scope, 'Block8', [net], reuse=reuse): 61 | with tf.variable_scope('Branch_0'): 62 | tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1') 63 | with tf.variable_scope('Branch_1'): 64 | tower_conv1_0 = slim.conv2d(net, 192, 1, scope='Conv2d_0a_1x1') 65 | tower_conv1_1 = slim.conv2d(tower_conv1_0, 224, [1, 3], 66 | scope='Conv2d_0b_1x3') 67 | tower_conv1_2 = slim.conv2d(tower_conv1_1, 256, [3, 1], 68 | scope='Conv2d_0c_3x1') 69 | mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2]) 70 | up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None, 71 | activation_fn=None, scope='Conv2d_1x1') 72 | net += scale * up 73 | if activation_fn: 74 | net = activation_fn(net) 75 | return net 76 | 77 | 78 | def inception_resnet_v2_base(inputs, 79 | final_endpoint='Conv2d_7b_1x1', 80 | output_stride=16, 81 | align_feature_maps=False, 82 | scope=None): 83 | """Inception model from http://arxiv.org/abs/1602.07261. 84 | 85 | Constructs an Inception Resnet v2 network from inputs to the given final 86 | endpoint. This method can construct the network up to the final inception 87 | block Conv2d_7b_1x1. 88 | 89 | Args: 90 | inputs: a tensor of size [batch_size, height, width, channels]. 91 | final_endpoint: specifies the endpoint to construct the network up to. It 92 | can be one of ['Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 93 | 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3', 94 | 'Mixed_5b', 'Mixed_6a', 'PreAuxLogits', 'Mixed_7a', 'Conv2d_7b_1x1'] 95 | output_stride: A scalar that specifies the requested ratio of input to 96 | output spatial resolution. Only supports 8 and 16. 97 | align_feature_maps: When true, changes all the VALID paddings in the network 98 | to SAME padding so that the feature maps are aligned. 99 | scope: Optional variable_scope. 100 | 101 | Returns: 102 | tensor_out: output tensor corresponding to the final_endpoint. 103 | end_points: a set of activations for external use, for example summaries or 104 | losses. 105 | 106 | Raises: 107 | ValueError: if final_endpoint is not set to one of the predefined values, 108 | or if the output_stride is not 8 or 16, or if the output_stride is 8 and 109 | we request an end point after 'PreAuxLogits'. 110 | """ 111 | if output_stride != 8 and output_stride != 16: 112 | raise ValueError('output_stride must be 8 or 16.') 113 | 114 | padding = 'SAME' if align_feature_maps else 'VALID' 115 | 116 | end_points = {} 117 | 118 | def add_and_check_final(name, net): 119 | end_points[name] = net 120 | return name == final_endpoint 121 | 122 | with tf.variable_scope(scope, 'InceptionResnetV2', [inputs]): 123 | with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], 124 | stride=1, padding='SAME'): 125 | # 149 x 149 x 32 126 | net = slim.conv2d(inputs, 32, 3, stride=2, padding=padding, 127 | scope='Conv2d_1a_3x3') 128 | if add_and_check_final('Conv2d_1a_3x3', net): return net, end_points 129 | 130 | # 147 x 147 x 32 131 | net = slim.conv2d(net, 32, 3, padding=padding, 132 | scope='Conv2d_2a_3x3') 133 | if add_and_check_final('Conv2d_2a_3x3', net): return net, end_points 134 | # 147 x 147 x 64 135 | net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3') 136 | if add_and_check_final('Conv2d_2b_3x3', net): return net, end_points 137 | # 73 x 73 x 64 138 | net = slim.max_pool2d(net, 3, stride=2, padding=padding, 139 | scope='MaxPool_3a_3x3') 140 | if add_and_check_final('MaxPool_3a_3x3', net): return net, end_points 141 | # 73 x 73 x 80 142 | net = slim.conv2d(net, 80, 1, padding=padding, 143 | scope='Conv2d_3b_1x1') 144 | if add_and_check_final('Conv2d_3b_1x1', net): return net, end_points 145 | # 71 x 71 x 192 146 | net = slim.conv2d(net, 192, 3, padding=padding, 147 | scope='Conv2d_4a_3x3') 148 | if add_and_check_final('Conv2d_4a_3x3', net): return net, end_points 149 | # 35 x 35 x 192 150 | net = slim.max_pool2d(net, 3, stride=2, padding=padding, 151 | scope='MaxPool_5a_3x3') 152 | if add_and_check_final('MaxPool_5a_3x3', net): return net, end_points 153 | 154 | # 35 x 35 x 320 155 | with tf.variable_scope('Mixed_5b'): 156 | with tf.variable_scope('Branch_0'): 157 | tower_conv = slim.conv2d(net, 96, 1, scope='Conv2d_1x1') 158 | with tf.variable_scope('Branch_1'): 159 | tower_conv1_0 = slim.conv2d(net, 48, 1, scope='Conv2d_0a_1x1') 160 | tower_conv1_1 = slim.conv2d(tower_conv1_0, 64, 5, 161 | scope='Conv2d_0b_5x5') 162 | with tf.variable_scope('Branch_2'): 163 | tower_conv2_0 = slim.conv2d(net, 64, 1, scope='Conv2d_0a_1x1') 164 | tower_conv2_1 = slim.conv2d(tower_conv2_0, 96, 3, 165 | scope='Conv2d_0b_3x3') 166 | tower_conv2_2 = slim.conv2d(tower_conv2_1, 96, 3, 167 | scope='Conv2d_0c_3x3') 168 | with tf.variable_scope('Branch_3'): 169 | tower_pool = slim.avg_pool2d(net, 3, stride=1, padding='SAME', 170 | scope='AvgPool_0a_3x3') 171 | tower_pool_1 = slim.conv2d(tower_pool, 64, 1, 172 | scope='Conv2d_0b_1x1') 173 | net = tf.concat( 174 | [tower_conv, tower_conv1_1, tower_conv2_2, tower_pool_1], 3) 175 | 176 | if add_and_check_final('Mixed_5b', net): return net, end_points 177 | # TODO(alemi): Register intermediate endpoints 178 | net = slim.repeat(net, 10, block35, scale=0.17) 179 | 180 | # 17 x 17 x 1088 if output_stride == 8, 181 | # 33 x 33 x 1088 if output_stride == 16 182 | use_atrous = output_stride == 8 183 | 184 | with tf.variable_scope('Mixed_6a'): 185 | with tf.variable_scope('Branch_0'): 186 | tower_conv = slim.conv2d(net, 384, 3, stride=1 if use_atrous else 2, 187 | padding=padding, 188 | scope='Conv2d_1a_3x3') 189 | with tf.variable_scope('Branch_1'): 190 | tower_conv1_0 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') 191 | tower_conv1_1 = slim.conv2d(tower_conv1_0, 256, 3, 192 | scope='Conv2d_0b_3x3') 193 | tower_conv1_2 = slim.conv2d(tower_conv1_1, 384, 3, 194 | stride=1 if use_atrous else 2, 195 | padding=padding, 196 | scope='Conv2d_1a_3x3') 197 | with tf.variable_scope('Branch_2'): 198 | tower_pool = slim.max_pool2d(net, 3, stride=1 if use_atrous else 2, 199 | padding=padding, 200 | scope='MaxPool_1a_3x3') 201 | net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3) 202 | 203 | if add_and_check_final('Mixed_6a', net): return net, end_points 204 | 205 | # TODO(alemi): register intermediate endpoints 206 | with slim.arg_scope([slim.conv2d], rate=2 if use_atrous else 1): 207 | net = slim.repeat(net, 20, block17, scale=0.10) 208 | if add_and_check_final('PreAuxLogits', net): return net, end_points 209 | 210 | if output_stride == 8: 211 | # TODO(gpapan): Properly support output_stride for the rest of the net. 212 | raise ValueError('output_stride==8 is only supported up to the ' 213 | 'PreAuxlogits end_point for now.') 214 | 215 | # 8 x 8 x 2080 216 | with tf.variable_scope('Mixed_7a'): 217 | with tf.variable_scope('Branch_0'): 218 | tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') 219 | tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=2, 220 | padding=padding, 221 | scope='Conv2d_1a_3x3') 222 | with tf.variable_scope('Branch_1'): 223 | tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') 224 | tower_conv1_1 = slim.conv2d(tower_conv1, 288, 3, stride=2, 225 | padding=padding, 226 | scope='Conv2d_1a_3x3') 227 | with tf.variable_scope('Branch_2'): 228 | tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1') 229 | tower_conv2_1 = slim.conv2d(tower_conv2, 288, 3, 230 | scope='Conv2d_0b_3x3') 231 | tower_conv2_2 = slim.conv2d(tower_conv2_1, 320, 3, stride=2, 232 | padding=padding, 233 | scope='Conv2d_1a_3x3') 234 | with tf.variable_scope('Branch_3'): 235 | tower_pool = slim.max_pool2d(net, 3, stride=2, 236 | padding=padding, 237 | scope='MaxPool_1a_3x3') 238 | net = tf.concat( 239 | [tower_conv_1, tower_conv1_1, tower_conv2_2, tower_pool], 3) 240 | 241 | if add_and_check_final('Mixed_7a', net): return net, end_points 242 | 243 | # TODO(alemi): register intermediate endpoints 244 | net = slim.repeat(net, 9, block8, scale=0.20) 245 | net = block8(net, activation_fn=None) 246 | 247 | # 8 x 8 x 1536 248 | net = slim.conv2d(net, 1536, 1, scope='Conv2d_7b_1x1') 249 | if add_and_check_final('Conv2d_7b_1x1', net): return net, end_points 250 | 251 | raise ValueError('final_endpoint (%s) not recognized', final_endpoint) 252 | 253 | 254 | def inception_resnet_v2(inputs, num_classes=1001, is_training=True, 255 | dropout_keep_prob=0.8, 256 | reuse=None, 257 | scope='InceptionResnetV2', 258 | create_aux_logits=True): 259 | """Creates the Inception Resnet V2 model. 260 | 261 | Args: 262 | inputs: a 4-D tensor of size [batch_size, height, width, 3]. 263 | num_classes: number of predicted classes. 264 | is_training: whether is training or not. 265 | dropout_keep_prob: float, the fraction to keep before final layer. 266 | reuse: whether or not the network and its variables should be reused. To be 267 | able to reuse 'scope' must be given. 268 | scope: Optional variable_scope. 269 | create_aux_logits: Whether to include the auxilliary logits. 270 | 271 | Returns: 272 | logits: the logits outputs of the model. 273 | end_points: the set of end_points from the inception model. 274 | """ 275 | end_points = {} 276 | 277 | with tf.variable_scope(scope, 'InceptionResnetV2', [inputs, num_classes], 278 | reuse=reuse) as scope: 279 | with slim.arg_scope([slim.batch_norm, slim.dropout], 280 | is_training=is_training): 281 | 282 | net, end_points = inception_resnet_v2_base(inputs, scope=scope) 283 | 284 | if create_aux_logits: 285 | with tf.variable_scope('AuxLogits'): 286 | aux = end_points['PreAuxLogits'] 287 | aux = slim.avg_pool2d(aux, 5, stride=3, padding='VALID', 288 | scope='Conv2d_1a_3x3') 289 | aux = slim.conv2d(aux, 128, 1, scope='Conv2d_1b_1x1') 290 | aux = slim.conv2d(aux, 768, aux.get_shape()[1:3], 291 | padding='VALID', scope='Conv2d_2a_5x5') 292 | aux = slim.flatten(aux) 293 | aux = slim.fully_connected(aux, num_classes, activation_fn=None, 294 | scope='Logits') 295 | end_points['AuxLogits'] = aux 296 | 297 | with tf.variable_scope('Logits'): 298 | net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID', 299 | scope='AvgPool_1a_8x8') 300 | net = slim.flatten(net) 301 | 302 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 303 | scope='Dropout') 304 | 305 | end_points['PreLogitsFlatten'] = net 306 | logits = slim.fully_connected(net, num_classes, activation_fn=None, 307 | scope='Logits') 308 | end_points['Logits'] = logits 309 | end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions') 310 | 311 | return logits, end_points 312 | inception_resnet_v2.default_image_size = 299 313 | 314 | 315 | def inception_resnet_v2_arg_scope(weight_decay=0.00004, 316 | batch_norm_decay=0.9997, 317 | batch_norm_epsilon=0.001): 318 | """Returns the scope with the default parameters for inception_resnet_v2. 319 | 320 | Args: 321 | weight_decay: the weight decay for weights variables. 322 | batch_norm_decay: decay for the moving average of batch_norm momentums. 323 | batch_norm_epsilon: small float added to variance to avoid dividing by zero. 324 | 325 | Returns: 326 | a arg_scope with the parameters needed for inception_resnet_v2. 327 | """ 328 | # Set weight_decay for weights in conv2d and fully_connected layers. 329 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 330 | weights_regularizer=slim.l2_regularizer(weight_decay), 331 | biases_regularizer=slim.l2_regularizer(weight_decay)): 332 | 333 | batch_norm_params = { 334 | 'decay': batch_norm_decay, 335 | 'epsilon': batch_norm_epsilon, 336 | } 337 | # Set activation_fn and parameters for batch_norm. 338 | with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu, 339 | normalizer_fn=slim.batch_norm, 340 | normalizer_params=batch_norm_params) as scope: 341 | return scope 342 | -------------------------------------------------------------------------------- /lib/model/generator/inception_v4/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/inception_v4/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/inception_v4/__pycache__/inception_utils.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/inception_v4/__pycache__/inception_utils.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/inception_v4/__pycache__/inception_v4.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/inception_v4/__pycache__/inception_v4.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/inception_v4/inception_utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | slim = tf.contrib.slim 16 | 17 | 18 | def inception_arg_scope(weight_decay=0.00004, 19 | use_batch_norm=True, 20 | batch_norm_decay=0.9997, 21 | batch_norm_epsilon=0.001): 22 | """Defines the default arg scope for inception models. 23 | Args: 24 | weight_decay: The weight decay to use for regularizing the model. 25 | use_batch_norm: "If `True`, batch_norm is applied after each convolution. 26 | batch_norm_decay: Decay for batch norm moving average. 27 | batch_norm_epsilon: Small float added to variance to avoid dividing by zero 28 | in batch norm. 29 | Returns: 30 | An `arg_scope` to use for the inception models. 31 | """ 32 | batch_norm_params = { 33 | # Decay for the moving averages. 34 | 'decay': batch_norm_decay, 35 | # epsilon to prevent 0s in variance. 36 | 'epsilon': batch_norm_epsilon, 37 | # collection containing update_ops. 38 | 'updates_collections': tf.GraphKeys.UPDATE_OPS, 39 | } 40 | if use_batch_norm: 41 | normalizer_fn = slim.batch_norm 42 | normalizer_params = batch_norm_params 43 | else: 44 | normalizer_fn = None 45 | normalizer_params = {} 46 | # Set weight_decay for weights in Conv and FC layers. 47 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 48 | weights_regularizer=slim.l2_regularizer(weight_decay)): 49 | with slim.arg_scope( 50 | [slim.conv2d], 51 | weights_initializer=slim.variance_scaling_initializer(), 52 | activation_fn=tf.nn.relu, 53 | normalizer_fn=normalizer_fn, 54 | normalizer_params=normalizer_params) as sc: 55 | return sc 56 | -------------------------------------------------------------------------------- /lib/model/generator/inception_v4/inception_v4.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | try: 15 | import inception_utils 16 | except: 17 | from lib.model.generator.inception_v4 import inception_utils 18 | 19 | slim = tf.contrib.slim 20 | 21 | 22 | def block_inception_a(inputs, scope=None, reuse=None): 23 | """Builds Inception-A block for Inception v4 network.""" 24 | # By default use stride=1 and SAME padding 25 | with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d], 26 | stride=1, padding='SAME'): 27 | with tf.variable_scope(scope, 'BlockInceptionA', [inputs], reuse=reuse): 28 | with tf.variable_scope('Branch_0'): 29 | branch_0 = slim.conv2d(inputs, 96, [1, 1], scope='Conv2d_0a_1x1') 30 | with tf.variable_scope('Branch_1'): 31 | branch_1 = slim.conv2d(inputs, 64, [1, 1], scope='Conv2d_0a_1x1') 32 | branch_1 = slim.conv2d(branch_1, 96, [3, 3], scope='Conv2d_0b_3x3') 33 | with tf.variable_scope('Branch_2'): 34 | branch_2 = slim.conv2d(inputs, 64, [1, 1], scope='Conv2d_0a_1x1') 35 | branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='Conv2d_0b_3x3') 36 | branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='Conv2d_0c_3x3') 37 | with tf.variable_scope('Branch_3'): 38 | branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3') 39 | branch_3 = slim.conv2d(branch_3, 96, [1, 1], scope='Conv2d_0b_1x1') 40 | return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3]) 41 | 42 | 43 | def block_reduction_a(inputs, scope=None, reuse=None): 44 | """Builds Reduction-A block for Inception v4 network.""" 45 | # By default use stride=1 and SAME padding 46 | with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d], 47 | stride=1, padding='SAME'): 48 | with tf.variable_scope(scope, 'BlockReductionA', [inputs], reuse=reuse): 49 | with tf.variable_scope('Branch_0'): 50 | branch_0 = slim.conv2d(inputs, 384, [3, 3], stride=2, padding='VALID', 51 | scope='Conv2d_1a_3x3') 52 | with tf.variable_scope('Branch_1'): 53 | branch_1 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1') 54 | branch_1 = slim.conv2d(branch_1, 224, [3, 3], scope='Conv2d_0b_3x3') 55 | branch_1 = slim.conv2d(branch_1, 256, [3, 3], stride=2, 56 | padding='VALID', scope='Conv2d_1a_3x3') 57 | with tf.variable_scope('Branch_2'): 58 | branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID', 59 | scope='MaxPool_1a_3x3') 60 | return tf.concat(axis=3, values=[branch_0, branch_1, branch_2]) 61 | 62 | 63 | def block_inception_b(inputs, scope=None, reuse=None): 64 | """Builds Inception-B block for Inception v4 network.""" 65 | # By default use stride=1 and SAME padding 66 | with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d], 67 | stride=1, padding='SAME'): 68 | with tf.variable_scope(scope, 'BlockInceptionB', [inputs], reuse=reuse): 69 | with tf.variable_scope('Branch_0'): 70 | branch_0 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1') 71 | with tf.variable_scope('Branch_1'): 72 | branch_1 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1') 73 | branch_1 = slim.conv2d(branch_1, 224, [1, 7], scope='Conv2d_0b_1x7') 74 | branch_1 = slim.conv2d(branch_1, 256, [7, 1], scope='Conv2d_0c_7x1') 75 | with tf.variable_scope('Branch_2'): 76 | branch_2 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1') 77 | branch_2 = slim.conv2d(branch_2, 192, [7, 1], scope='Conv2d_0b_7x1') 78 | branch_2 = slim.conv2d(branch_2, 224, [1, 7], scope='Conv2d_0c_1x7') 79 | branch_2 = slim.conv2d(branch_2, 224, [7, 1], scope='Conv2d_0d_7x1') 80 | branch_2 = slim.conv2d(branch_2, 256, [1, 7], scope='Conv2d_0e_1x7') 81 | with tf.variable_scope('Branch_3'): 82 | branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3') 83 | branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1') 84 | return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3]) 85 | 86 | 87 | def block_reduction_b(inputs, scope=None, reuse=None): 88 | """Builds Reduction-B block for Inception v4 network.""" 89 | # By default use stride=1 and SAME padding 90 | with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d], 91 | stride=1, padding='SAME'): 92 | with tf.variable_scope(scope, 'BlockReductionB', [inputs], reuse=reuse): 93 | with tf.variable_scope('Branch_0'): 94 | branch_0 = slim.conv2d(inputs, 192, [1, 1], scope='Conv2d_0a_1x1') 95 | branch_0 = slim.conv2d(branch_0, 192, [3, 3], stride=2, 96 | padding='VALID', scope='Conv2d_1a_3x3') 97 | with tf.variable_scope('Branch_1'): 98 | branch_1 = slim.conv2d(inputs, 256, [1, 1], scope='Conv2d_0a_1x1') 99 | branch_1 = slim.conv2d(branch_1, 256, [1, 7], scope='Conv2d_0b_1x7') 100 | branch_1 = slim.conv2d(branch_1, 320, [7, 1], scope='Conv2d_0c_7x1') 101 | branch_1 = slim.conv2d(branch_1, 320, [3, 3], stride=2, 102 | padding='VALID', scope='Conv2d_1a_3x3') 103 | with tf.variable_scope('Branch_2'): 104 | branch_2 = slim.max_pool2d(inputs, [3, 3], stride=2, padding='VALID', 105 | scope='MaxPool_1a_3x3') 106 | return tf.concat(axis=3, values=[branch_0, branch_1, branch_2]) 107 | 108 | 109 | def block_inception_c(inputs, scope=None, reuse=None): 110 | """Builds Inception-C block for Inception v4 network.""" 111 | # By default use stride=1 and SAME padding 112 | with slim.arg_scope([slim.conv2d, slim.avg_pool2d, slim.max_pool2d], 113 | stride=1, padding='SAME'): 114 | with tf.variable_scope(scope, 'BlockInceptionC', [inputs], reuse=reuse): 115 | with tf.variable_scope('Branch_0'): 116 | branch_0 = slim.conv2d(inputs, 256, [1, 1], scope='Conv2d_0a_1x1') 117 | with tf.variable_scope('Branch_1'): 118 | branch_1 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1') 119 | branch_1 = tf.concat(axis=3, values=[ 120 | slim.conv2d(branch_1, 256, [1, 3], scope='Conv2d_0b_1x3'), 121 | slim.conv2d(branch_1, 256, [3, 1], scope='Conv2d_0c_3x1')]) 122 | with tf.variable_scope('Branch_2'): 123 | branch_2 = slim.conv2d(inputs, 384, [1, 1], scope='Conv2d_0a_1x1') 124 | branch_2 = slim.conv2d(branch_2, 448, [3, 1], scope='Conv2d_0b_3x1') 125 | branch_2 = slim.conv2d(branch_2, 512, [1, 3], scope='Conv2d_0c_1x3') 126 | branch_2 = tf.concat(axis=3, values=[ 127 | slim.conv2d(branch_2, 256, [1, 3], scope='Conv2d_0d_1x3'), 128 | slim.conv2d(branch_2, 256, [3, 1], scope='Conv2d_0e_3x1')]) 129 | with tf.variable_scope('Branch_3'): 130 | branch_3 = slim.avg_pool2d(inputs, [3, 3], scope='AvgPool_0a_3x3') 131 | branch_3 = slim.conv2d(branch_3, 256, [1, 1], scope='Conv2d_0b_1x1') 132 | return tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3]) 133 | 134 | 135 | def inception_v4_base(inputs, final_endpoint='Mixed_7d', scope=None): 136 | """Creates the Inception V4 network up to the given final endpoint. 137 | Args: 138 | inputs: a 4-D tensor of size [batch_size, height, width, 3]. 139 | final_endpoint: specifies the endpoint to construct the network up to. 140 | It can be one of [ 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 141 | 'Mixed_3a', 'Mixed_4a', 'Mixed_5a', 'Mixed_5b', 'Mixed_5c', 'Mixed_5d', 142 | 'Mixed_5e', 'Mixed_6a', 'Mixed_6b', 'Mixed_6c', 'Mixed_6d', 'Mixed_6e', 143 | 'Mixed_6f', 'Mixed_6g', 'Mixed_6h', 'Mixed_7a', 'Mixed_7b', 'Mixed_7c', 144 | 'Mixed_7d'] 145 | scope: Optional variable_scope. 146 | Returns: 147 | logits: the logits outputs of the model. 148 | end_points: the set of end_points from the inception model. 149 | Raises: 150 | ValueError: if final_endpoint is not set to one of the predefined values, 151 | """ 152 | end_points = {} 153 | 154 | def add_and_check_final(name, net): 155 | end_points[name] = net 156 | return name == final_endpoint 157 | 158 | with tf.variable_scope(scope, 'InceptionV4', [inputs]): 159 | with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], 160 | stride=1, padding='SAME'): 161 | # 299 x 299 x 3 162 | net = slim.conv2d(inputs, 32, [3, 3], stride=2, 163 | padding='VALID', scope='Conv2d_1a_3x3') 164 | if add_and_check_final('Conv2d_1a_3x3', net): return net, end_points 165 | # 149 x 149 x 32 166 | net = slim.conv2d(net, 32, [3, 3], padding='VALID', 167 | scope='Conv2d_2a_3x3') 168 | if add_and_check_final('Conv2d_2a_3x3', net): return net, end_points 169 | # 147 x 147 x 32 170 | net = slim.conv2d(net, 64, [3, 3], scope='Conv2d_2b_3x3') 171 | if add_and_check_final('Conv2d_2b_3x3', net): return net, end_points 172 | # 147 x 147 x 64 173 | with tf.variable_scope('Mixed_3a'): 174 | with tf.variable_scope('Branch_0'): 175 | branch_0 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', 176 | scope='MaxPool_0a_3x3') 177 | with tf.variable_scope('Branch_1'): 178 | branch_1 = slim.conv2d(net, 96, [3, 3], stride=2, padding='VALID', 179 | scope='Conv2d_0a_3x3') 180 | net = tf.concat(axis=3, values=[branch_0, branch_1]) 181 | if add_and_check_final('Mixed_3a', net): return net, end_points 182 | 183 | # 73 x 73 x 160 184 | with tf.variable_scope('Mixed_4a'): 185 | with tf.variable_scope('Branch_0'): 186 | branch_0 = slim.conv2d(net, 64, [1, 1], scope='Conv2d_0a_1x1') 187 | branch_0 = slim.conv2d(branch_0, 96, [3, 3], padding='VALID', 188 | scope='Conv2d_1a_3x3') 189 | with tf.variable_scope('Branch_1'): 190 | branch_1 = slim.conv2d(net, 64, [1, 1], scope='Conv2d_0a_1x1') 191 | branch_1 = slim.conv2d(branch_1, 64, [1, 7], scope='Conv2d_0b_1x7') 192 | branch_1 = slim.conv2d(branch_1, 64, [7, 1], scope='Conv2d_0c_7x1') 193 | branch_1 = slim.conv2d(branch_1, 96, [3, 3], padding='VALID', 194 | scope='Conv2d_1a_3x3') 195 | net = tf.concat(axis=3, values=[branch_0, branch_1]) 196 | if add_and_check_final('Mixed_4a', net): return net, end_points 197 | 198 | # 71 x 71 x 192 199 | with tf.variable_scope('Mixed_5a'): 200 | with tf.variable_scope('Branch_0'): 201 | branch_0 = slim.conv2d(net, 192, [3, 3], stride=2, padding='VALID', 202 | scope='Conv2d_1a_3x3') 203 | with tf.variable_scope('Branch_1'): 204 | branch_1 = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', 205 | scope='MaxPool_1a_3x3') 206 | net = tf.concat(axis=3, values=[branch_0, branch_1]) 207 | if add_and_check_final('Mixed_5a', net): return net, end_points 208 | 209 | # 35 x 35 x 384 210 | # 4 x Inception-A blocks 211 | for idx in range(4): 212 | block_scope = 'Mixed_5' + chr(ord('b') + idx) 213 | net = block_inception_a(net, block_scope) 214 | if add_and_check_final(block_scope, net): return net, end_points 215 | 216 | # 35 x 35 x 384 217 | # Reduction-A block 218 | net = block_reduction_a(net, 'Mixed_6a') 219 | if add_and_check_final('Mixed_6a', net): return net, end_points 220 | 221 | # 17 x 17 x 1024 222 | # 7 x Inception-B blocks 223 | for idx in range(7): 224 | block_scope = 'Mixed_6' + chr(ord('b') + idx) 225 | net = block_inception_b(net, block_scope) 226 | if add_and_check_final(block_scope, net): return net, end_points 227 | 228 | # 17 x 17 x 1024 229 | # Reduction-B block 230 | net = block_reduction_b(net, 'Mixed_7a') 231 | if add_and_check_final('Mixed_7a', net): return net, end_points 232 | 233 | # 8 x 8 x 1536 234 | # 3 x Inception-C blocks 235 | for idx in range(3): 236 | block_scope = 'Mixed_7' + chr(ord('b') + idx) 237 | net = block_inception_c(net, block_scope) 238 | if add_and_check_final(block_scope, net): return net, end_points 239 | raise ValueError('Unknown final endpoint %s' % final_endpoint) 240 | 241 | 242 | # num_classes=1001 243 | def inception_v4(inputs, num_classes=None, is_training=True, 244 | dropout_keep_prob=0.8, 245 | reuse=None, 246 | scope='InceptionV4', 247 | create_aux_logits=True): 248 | """Creates the Inception V4 model. 249 | Args: 250 | inputs: a 4-D tensor of size [batch_size, height, width, 3]. 251 | num_classes: number of predicted classes. 252 | is_training: whether is training or not. 253 | dropout_keep_prob: float, the fraction to keep before final layer. 254 | reuse: whether or not the network and its variables should be reused. To be 255 | able to reuse 'scope' must be given. 256 | scope: Optional variable_scope. 257 | create_aux_logits: Whether to include the auxiliary logits. 258 | Returns: 259 | logits: the logits outputs of the model. 260 | end_points: the set of end_points from the inception model. 261 | """ 262 | end_points = {} 263 | with tf.variable_scope(scope, 'InceptionV4', [inputs], reuse=reuse) as scope: 264 | with slim.arg_scope([slim.batch_norm, slim.dropout], 265 | is_training=is_training): 266 | net, end_points = inception_v4_base(inputs, scope=scope) 267 | if num_classes is not None: 268 | with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], 269 | stride=1, padding='SAME'): 270 | # Auxiliary Head logits 271 | if create_aux_logits: 272 | with tf.variable_scope('AuxLogits'): 273 | # 17 x 17 x 1024 274 | aux_logits = end_points['Mixed_6h'] 275 | aux_logits = slim.avg_pool2d(aux_logits, [5, 5], stride=3, 276 | padding='VALID', 277 | scope='AvgPool_1a_5x5') 278 | aux_logits = slim.conv2d(aux_logits, 128, [1, 1], 279 | scope='Conv2d_1b_1x1') 280 | aux_logits = slim.conv2d(aux_logits, 768, 281 | aux_logits.get_shape()[1:3], 282 | padding='VALID', scope='Conv2d_2a') 283 | aux_logits = slim.flatten(aux_logits) 284 | aux_logits = slim.fully_connected(aux_logits, num_classes, 285 | activation_fn=None, 286 | scope='Aux_logits') 287 | end_points['AuxLogits'] = aux_logits 288 | 289 | # Final pooling and prediction 290 | with tf.variable_scope('Logits'): 291 | # 8 x 8 x 1536 292 | net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID', 293 | scope='AvgPool_1a') 294 | # 1 x 1 x 1536 295 | net = slim.dropout(net, dropout_keep_prob, scope='Dropout_1b') 296 | net = slim.flatten(net, scope='PreLogitsFlatten') 297 | end_points['PreLogitsFlatten'] = net 298 | # 1536 299 | logits = slim.fully_connected(net, num_classes, activation_fn=None, 300 | scope='Logits') 301 | end_points['Logits'] = logits 302 | end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions') 303 | else: 304 | logits = net 305 | end_points = end_points 306 | return logits, end_points 307 | inception_v4.default_image_size = 299 308 | 309 | 310 | inception_v4_arg_scope = inception_utils.inception_arg_scope 311 | -------------------------------------------------------------------------------- /lib/model/generator/lp_net/__pycache__/lp_net.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/lp_net/__pycache__/lp_net.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/lp_net/lp_net.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | slim = tf.contrib.slim 16 | 17 | def lp_net_arg_scope(weight_decay=0.00004, 18 | use_batch_norm=True, 19 | batch_norm_decay=0.9997, 20 | batch_norm_epsilon=0.001): 21 | batch_norm_params = {'decay': batch_norm_decay,'epsilon': batch_norm_epsilon,'updates_collections': tf.GraphKeys.UPDATE_OPS,} 22 | if use_batch_norm: 23 | normalizer_fn = slim.batch_norm 24 | normalizer_params = batch_norm_params 25 | else: 26 | normalizer_fn = None 27 | normalizer_params = {} 28 | with slim.arg_scope([slim.conv2d, slim.fully_connected], weights_regularizer=slim.l2_regularizer(weight_decay)): 29 | with slim.arg_scope( 30 | [slim.conv2d], 31 | weights_initializer=slim.variance_scaling_initializer(), 32 | activation_fn=tf.nn.relu, 33 | normalizer_fn=normalizer_fn, 34 | normalizer_params=normalizer_params) as sc: 35 | return sc 36 | 37 | def lp_net(inputs, 38 | num_classes=None, 39 | is_training=True, 40 | dropout_keep_prob=0.5, 41 | spatial_squeeze=True, 42 | scope='lp_net'): 43 | with tf.variable_scope(scope, 'lp_net', [inputs]) as sc: 44 | end_points_collection = sc.name + '_end_points' 45 | # Collect outputs for conv2d, fully_connected and max_pool2d. 46 | with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], 47 | outputs_collections=[end_points_collection]): 48 | net = slim.conv2d(inputs, 64, [11, 11], 4, padding='VALID',scope='conv1') 49 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool1') 50 | net = slim.conv2d(net, 192, [5, 5], scope='conv2') 51 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool2') 52 | net = slim.conv2d(net, 384, [3, 3], scope='conv3') 53 | net = slim.conv2d(net, 384, [3, 3], scope='conv4') 54 | net = slim.conv2d(net, 256, [3, 3], scope='conv5') 55 | net = slim.max_pool2d(net, [3, 3], 2, scope='pool5') 56 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 57 | if num_classes is not None: 58 | with slim.arg_scope([slim.conv2d],weights_initializer=trunc_normal(0.005),biases_initializer=tf.constant_initializer(0.1)): 59 | # net = slim.conv2d(net, 4096, [5, 5], padding='VALID',scope='fc6') 60 | net = slim.conv2d(net, 4096, net.get_shape()[1:3], padding='VALID',scope='fc6') 61 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training,scope='dropout6') 62 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 63 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training,scope='dropout7') 64 | net = slim.conv2d(net, num_classes, [1, 1],activation_fn=None,normalizer_fn=None,biases_initializer=tf.zeros_initializer(),scope='fc8') 65 | if spatial_squeeze: 66 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 67 | end_points[sc.name + '/fc8'] = net 68 | else: 69 | net = net 70 | end_points = end_points 71 | return net, end_points 72 | 73 | -------------------------------------------------------------------------------- /lib/model/generator/resnet_v2/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/resnet_v2/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/resnet_v2/__pycache__/resnet_utils.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/resnet_v2/__pycache__/resnet_utils.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/resnet_v2/__pycache__/resnet_v2.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/resnet_v2/__pycache__/resnet_v2.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/resnet_v2/resnet_utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import collections 14 | import tensorflow as tf 15 | 16 | slim = tf.contrib.slim 17 | 18 | 19 | class Block(collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])): 20 | """A named tuple describing a ResNet block. 21 | 22 | Its parts are: 23 | scope: The scope of the `Block`. 24 | unit_fn: The ResNet unit function which takes as input a `Tensor` and 25 | returns another `Tensor` with the output of the ResNet unit. 26 | args: A list of length equal to the number of units in the `Block`. The list 27 | contains one (depth, depth_bottleneck, stride) tuple for each unit in the 28 | block to serve as argument to unit_fn. 29 | """ 30 | 31 | 32 | def subsample(inputs, factor, scope=None): 33 | """Subsamples the input along the spatial dimensions. 34 | 35 | Args: 36 | inputs: A `Tensor` of size [batch, height_in, width_in, channels]. 37 | factor: The subsampling factor. 38 | scope: Optional variable_scope. 39 | 40 | Returns: 41 | output: A `Tensor` of size [batch, height_out, width_out, channels] with the 42 | input, either intact (if factor == 1) or subsampled (if factor > 1). 43 | """ 44 | if factor == 1: 45 | return inputs 46 | else: 47 | return slim.max_pool2d(inputs, [1, 1], stride=factor, scope=scope) 48 | 49 | 50 | def conv2d_same(inputs, num_outputs, kernel_size, stride, rate=1, scope=None): 51 | """Strided 2-D convolution with 'SAME' padding. 52 | 53 | When stride > 1, then we do explicit zero-padding, followed by conv2d with 54 | 'VALID' padding. 55 | 56 | Note that 57 | 58 | net = conv2d_same(inputs, num_outputs, 3, stride=stride) 59 | 60 | is equivalent to 61 | 62 | net = slim.conv2d(inputs, num_outputs, 3, stride=1, padding='SAME') 63 | net = subsample(net, factor=stride) 64 | 65 | whereas 66 | 67 | net = slim.conv2d(inputs, num_outputs, 3, stride=stride, padding='SAME') 68 | 69 | is different when the input's height or width is even, which is why we add the 70 | current function. For more details, see ResnetUtilsTest.testConv2DSameEven(). 71 | 72 | Args: 73 | inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. 74 | num_outputs: An integer, the number of output filters. 75 | kernel_size: An int with the kernel_size of the filters. 76 | stride: An integer, the output stride. 77 | rate: An integer, rate for atrous convolution. 78 | scope: Scope. 79 | 80 | Returns: 81 | output: A 4-D tensor of size [batch, height_out, width_out, channels] with 82 | the convolution output. 83 | """ 84 | if stride == 1: 85 | return slim.conv2d(inputs, num_outputs, kernel_size, stride=1, rate=rate, 86 | padding='SAME', scope=scope) 87 | else: 88 | kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) 89 | pad_total = kernel_size_effective - 1 90 | pad_beg = pad_total // 2 91 | pad_end = pad_total - pad_beg 92 | inputs = tf.pad(inputs, 93 | [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) 94 | return slim.conv2d(inputs, num_outputs, kernel_size, stride=stride, 95 | rate=rate, padding='VALID', scope=scope) 96 | 97 | 98 | @slim.add_arg_scope 99 | def stack_blocks_dense(net, blocks, output_stride=None, 100 | outputs_collections=None): 101 | """Stacks ResNet `Blocks` and controls output feature density. 102 | 103 | First, this function creates scopes for the ResNet in the form of 104 | 'block_name/unit_1', 'block_name/unit_2', etc. 105 | 106 | Second, this function allows the user to explicitly control the ResNet 107 | output_stride, which is the ratio of the input to output spatial resolution. 108 | This is useful for dense prediction tasks such as semantic segmentation or 109 | object detection. 110 | 111 | Most ResNets consist of 4 ResNet blocks and subsample the activations by a 112 | factor of 2 when transitioning between consecutive ResNet blocks. This results 113 | to a nominal ResNet output_stride equal to 8. If we set the output_stride to 114 | half the nominal network stride (e.g., output_stride=4), then we compute 115 | responses twice. 116 | 117 | Control of the output feature density is implemented by atrous convolution. 118 | 119 | Args: 120 | net: A `Tensor` of size [batch, height, width, channels]. 121 | blocks: A list of length equal to the number of ResNet `Blocks`. Each 122 | element is a ResNet `Block` object describing the units in the `Block`. 123 | output_stride: If `None`, then the output will be computed at the nominal 124 | network stride. If output_stride is not `None`, it specifies the requested 125 | ratio of input to output spatial resolution, which needs to be equal to 126 | the product of unit strides from the start up to some level of the ResNet. 127 | For example, if the ResNet employs units with strides 1, 2, 1, 3, 4, 1, 128 | then valid values for the output_stride are 1, 2, 6, 24 or None (which 129 | is equivalent to output_stride=24). 130 | outputs_collections: Collection to add the ResNet block outputs. 131 | 132 | Returns: 133 | net: Output tensor with stride equal to the specified output_stride. 134 | 135 | Raises: 136 | ValueError: If the target output_stride is not valid. 137 | """ 138 | # The current_stride variable keeps track of the effective stride of the 139 | # activations. This allows us to invoke atrous convolution whenever applying 140 | # the next residual unit would result in the activations having stride larger 141 | # than the target output_stride. 142 | current_stride = 1 143 | 144 | # The atrous convolution rate parameter. 145 | rate = 1 146 | 147 | for block in blocks: 148 | with tf.variable_scope(block.scope, 'block', [net]) as sc: 149 | for i, unit in enumerate(block.args): 150 | if output_stride is not None and current_stride > output_stride: 151 | raise ValueError('The target output_stride cannot be reached.') 152 | 153 | with tf.variable_scope('unit_%d' % (i + 1), values=[net]): 154 | # If we have reached the target output_stride, then we need to employ 155 | # atrous convolution with stride=1 and multiply the atrous rate by the 156 | # current unit's stride for use in subsequent layers. 157 | if output_stride is not None and current_stride == output_stride: 158 | net = block.unit_fn(net, rate=rate, **dict(unit, stride=1)) 159 | rate *= unit.get('stride', 1) 160 | 161 | else: 162 | net = block.unit_fn(net, rate=1, **unit) 163 | current_stride *= unit.get('stride', 1) 164 | net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net) 165 | 166 | if output_stride is not None and current_stride != output_stride: 167 | raise ValueError('The target output_stride cannot be reached.') 168 | 169 | return net 170 | 171 | 172 | def resnet_arg_scope(weight_decay=0.0001, 173 | batch_norm_decay=0.997, 174 | batch_norm_epsilon=1e-5, 175 | batch_norm_scale=True, 176 | activation_fn=tf.nn.relu, 177 | use_batch_norm=True): 178 | """Defines the default ResNet arg scope. 179 | 180 | TODO(gpapan): The batch-normalization related default values above are 181 | appropriate for use in conjunction with the reference ResNet models 182 | released at https://github.com/KaimingHe/deep-residual-networks. When 183 | training ResNets from scratch, they might need to be tuned. 184 | 185 | Args: 186 | weight_decay: The weight decay to use for regularizing the model. 187 | batch_norm_decay: The moving average decay when estimating layer activation 188 | statistics in batch normalization. 189 | batch_norm_epsilon: Small constant to prevent division by zero when 190 | normalizing activations by their variance in batch normalization. 191 | batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the 192 | activations in the batch normalization layer. 193 | activation_fn: The activation function which is used in ResNet. 194 | use_batch_norm: Whether or not to use batch normalization. 195 | 196 | Returns: 197 | An `arg_scope` to use for the resnet models. 198 | """ 199 | batch_norm_params = { 200 | 'decay': batch_norm_decay, 201 | 'epsilon': batch_norm_epsilon, 202 | 'scale': batch_norm_scale, 203 | 'updates_collections': tf.GraphKeys.UPDATE_OPS, 204 | } 205 | 206 | with slim.arg_scope( 207 | [slim.conv2d], 208 | weights_regularizer=slim.l2_regularizer(weight_decay), 209 | weights_initializer=slim.variance_scaling_initializer(), 210 | activation_fn=activation_fn, 211 | normalizer_fn=slim.batch_norm if use_batch_norm else None, 212 | normalizer_params=batch_norm_params): 213 | with slim.arg_scope([slim.batch_norm], **batch_norm_params): 214 | # The following implies padding='SAME' for pool1, which makes feature 215 | # alignment easier for dense prediction tasks. This is also used in 216 | # https://github.com/facebook/fb.resnet.torch. However the accompanying 217 | # code of 'Deep Residual Learning for Image Recognition' uses 218 | # padding='VALID' for pool1. You can switch to that choice by setting 219 | # slim.arg_scope([slim.max_pool2d], padding='VALID'). 220 | with slim.arg_scope([slim.max_pool2d], padding='SAME') as arg_sc: 221 | return arg_sc 222 | -------------------------------------------------------------------------------- /lib/model/generator/resnet_v2/resnet_v2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | try: 16 | import resnet_utils 17 | except: 18 | from lib.model.generator.resnet_v2 import resnet_utils 19 | 20 | slim = tf.contrib.slim 21 | resnet_arg_scope = resnet_utils.resnet_arg_scope 22 | 23 | 24 | @slim.add_arg_scope 25 | def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1, 26 | outputs_collections=None, scope=None): 27 | """Bottleneck residual unit variant with BN before convolutions. 28 | 29 | This is the full preactivation residual unit variant proposed in [2]. See 30 | Fig. 1(b) of [2] for its definition. Note that we use here the bottleneck 31 | variant which has an extra bottleneck layer. 32 | 33 | When putting together two consecutive ResNet blocks that use this unit, one 34 | should use stride = 2 in the last unit of the first block. 35 | 36 | Args: 37 | inputs: A tensor of size [batch, height, width, channels]. 38 | depth: The depth of the ResNet unit output. 39 | depth_bottleneck: The depth of the bottleneck layers. 40 | stride: The ResNet unit's stride. Determines the amount of downsampling of 41 | the units output compared to its input. 42 | rate: An integer, rate for atrous convolution. 43 | outputs_collections: Collection to add the ResNet unit output. 44 | scope: Optional variable_scope. 45 | 46 | Returns: 47 | The ResNet unit's output. 48 | """ 49 | with tf.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc: 50 | depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4) 51 | preact = slim.batch_norm(inputs, activation_fn=tf.nn.relu, scope='preact') 52 | if depth == depth_in: 53 | shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') 54 | else: 55 | shortcut = slim.conv2d(preact, depth, [1, 1], stride=stride, 56 | normalizer_fn=None, activation_fn=None, 57 | scope='shortcut') 58 | 59 | residual = slim.conv2d(preact, depth_bottleneck, [1, 1], stride=1, 60 | scope='conv1') 61 | residual = resnet_utils.conv2d_same(residual, depth_bottleneck, 3, stride, 62 | rate=rate, scope='conv2') 63 | residual = slim.conv2d(residual, depth, [1, 1], stride=1, 64 | normalizer_fn=None, activation_fn=None, 65 | scope='conv3') 66 | 67 | output = shortcut + residual 68 | 69 | return slim.utils.collect_named_outputs(outputs_collections, 70 | sc.original_name_scope, 71 | output) 72 | 73 | 74 | def resnet_v2(inputs, 75 | blocks, 76 | num_classes=None, 77 | is_training=True, 78 | global_pool=True, 79 | output_stride=None, 80 | include_root_block=True, 81 | spatial_squeeze=True, 82 | reuse=None, 83 | scope=None): 84 | """Generator for v2 (preactivation) ResNet models. 85 | 86 | This function generates a family of ResNet v2 models. See the resnet_v2_*() 87 | methods for specific model instantiations, obtained by selecting different 88 | block instantiations that produce ResNets of various depths. 89 | 90 | Training for image classification on Imagenet is usually done with [224, 224] 91 | inputs, resulting in [7, 7] feature maps at the output of the last ResNet 92 | block for the ResNets defined in [1] that have nominal stride equal to 32. 93 | However, for dense prediction tasks we advise that one uses inputs with 94 | spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In 95 | this case the feature maps at the ResNet output will have spatial shape 96 | [(height - 1) / output_stride + 1, (width - 1) / output_stride + 1] 97 | and corners exactly aligned with the input image corners, which greatly 98 | facilitates alignment of the features to the image. Using as input [225, 225] 99 | images results in [8, 8] feature maps at the output of the last ResNet block. 100 | 101 | For dense prediction tasks, the ResNet needs to run in fully-convolutional 102 | (FCN) mode and global_pool needs to be set to False. The ResNets in [1, 2] all 103 | have nominal stride equal to 32 and a good choice in FCN mode is to use 104 | output_stride=16 in order to increase the density of the computed features at 105 | small computational and memory overhead, cf. http://arxiv.org/abs/1606.00915. 106 | 107 | Args: 108 | inputs: A tensor of size [batch, height_in, width_in, channels]. 109 | blocks: A list of length equal to the number of ResNet blocks. Each element 110 | is a resnet_utils.Block object describing the units in the block. 111 | num_classes: Number of predicted classes for classification tasks. If None 112 | we return the features before the logit layer. 113 | is_training: whether is training or not. 114 | global_pool: If True, we perform global average pooling before computing the 115 | logits. Set to True for image classification, False for dense prediction. 116 | output_stride: If None, then the output will be computed at the nominal 117 | network stride. If output_stride is not None, it specifies the requested 118 | ratio of input to output spatial resolution. 119 | include_root_block: If True, include the initial convolution followed by 120 | max-pooling, if False excludes it. If excluded, `inputs` should be the 121 | results of an activation-less convolution. 122 | spatial_squeeze: if True, logits is of shape [B, C], if false logits is 123 | of shape [B, 1, 1, C], where B is batch_size and C is number of classes. 124 | To use this parameter, the input images must be smaller than 300x300 125 | pixels, in which case the output logit layer does not contain spatial 126 | information and can be removed. 127 | reuse: whether or not the network and its variables should be reused. To be 128 | able to reuse 'scope' must be given. 129 | scope: Optional variable_scope. 130 | 131 | 132 | Returns: 133 | net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. 134 | If global_pool is False, then height_out and width_out are reduced by a 135 | factor of output_stride compared to the respective height_in and width_in, 136 | else both height_out and width_out equal one. If num_classes is None, then 137 | net is the output of the last ResNet block, potentially after global 138 | average pooling. If num_classes is not None, net contains the pre-softmax 139 | activations. 140 | end_points: A dictionary from components of the network to the corresponding 141 | activation. 142 | 143 | Raises: 144 | ValueError: If the target output_stride is not valid. 145 | """ 146 | with tf.variable_scope(scope, 'resnet_v2', [inputs], reuse=reuse) as sc: 147 | end_points_collection = sc.name + '_end_points' 148 | with slim.arg_scope([slim.conv2d, bottleneck, 149 | resnet_utils.stack_blocks_dense], 150 | outputs_collections=end_points_collection): 151 | with slim.arg_scope([slim.batch_norm], is_training=is_training): 152 | net = inputs 153 | if include_root_block: 154 | if output_stride is not None: 155 | if output_stride % 4 != 0: 156 | raise ValueError('The output_stride needs to be a multiple of 4.') 157 | output_stride /= 4 158 | # We do not include batch normalization or activation functions in 159 | # conv1 because the first ResNet unit will perform these. Cf. 160 | # Appendix of [2]. 161 | with slim.arg_scope([slim.conv2d], 162 | activation_fn=None, normalizer_fn=None): 163 | net = resnet_utils.conv2d_same(net, 64, 7, stride=2, scope='conv1') 164 | net = slim.max_pool2d(net, [3, 3], stride=2, scope='pool1') 165 | net = resnet_utils.stack_blocks_dense(net, blocks, output_stride) 166 | # This is needed because the pre-activation variant does not have batch 167 | # normalization or activation functions in the residual unit output. See 168 | # Appendix of [2]. 169 | net = slim.batch_norm(net, activation_fn=tf.nn.relu, scope='postnorm') 170 | if global_pool: 171 | # Global average pooling. 172 | net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True) 173 | if num_classes is not None: 174 | net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, 175 | normalizer_fn=None, scope='logits') 176 | if spatial_squeeze: 177 | net = tf.squeeze(net, [1, 2], name='SpatialSqueeze') 178 | # Convert end_points_collection into a dictionary of end_points. 179 | end_points = slim.utils.convert_collection_to_dict( 180 | end_points_collection) 181 | if num_classes is not None: 182 | end_points['predictions'] = slim.softmax(net, scope='predictions') 183 | return net, end_points 184 | resnet_v2.default_image_size = 224 185 | 186 | 187 | def resnet_v2_block(scope, base_depth, num_units, stride): 188 | """Helper function for creating a resnet_v2 bottleneck block. 189 | 190 | Args: 191 | scope: The scope of the block. 192 | base_depth: The depth of the bottleneck layer for each unit. 193 | num_units: The number of units in the block. 194 | stride: The stride of the block, implemented as a stride in the last unit. 195 | All other units have stride=1. 196 | 197 | Returns: 198 | A resnet_v2 bottleneck block. 199 | """ 200 | return resnet_utils.Block(scope, bottleneck, [{ 201 | 'depth': base_depth * 4, 202 | 'depth_bottleneck': base_depth, 203 | 'stride': 1 204 | }] * (num_units - 1) + [{ 205 | 'depth': base_depth * 4, 206 | 'depth_bottleneck': base_depth, 207 | 'stride': stride 208 | }]) 209 | resnet_v2.default_image_size = 224 210 | 211 | 212 | def resnet_v2_50(inputs, 213 | num_classes=None, 214 | is_training=True, 215 | global_pool=True, 216 | output_stride=None, 217 | spatial_squeeze=True, 218 | reuse=None, 219 | scope='resnet_v2_50'): 220 | """ResNet-50 model of [1]. See resnet_v2() for arg and return description.""" 221 | blocks = [ 222 | resnet_v2_block('block1', base_depth=64, num_units=3, stride=2), 223 | resnet_v2_block('block2', base_depth=128, num_units=4, stride=2), 224 | resnet_v2_block('block3', base_depth=256, num_units=6, stride=2), 225 | resnet_v2_block('block4', base_depth=512, num_units=3, stride=1), 226 | ] 227 | return resnet_v2(inputs, blocks, num_classes, is_training=is_training, 228 | global_pool=global_pool, output_stride=output_stride, 229 | include_root_block=True, spatial_squeeze=spatial_squeeze, 230 | reuse=reuse, scope=scope) 231 | resnet_v2_50.default_image_size = resnet_v2.default_image_size 232 | 233 | 234 | def resnet_v2_101(inputs, 235 | num_classes=None, 236 | is_training=True, 237 | global_pool=True, 238 | output_stride=None, 239 | spatial_squeeze=True, 240 | reuse=None, 241 | scope='resnet_v2_101'): 242 | """ResNet-101 model of [1]. See resnet_v2() for arg and return description.""" 243 | blocks = [ 244 | resnet_v2_block('block1', base_depth=64, num_units=3, stride=2), 245 | resnet_v2_block('block2', base_depth=128, num_units=4, stride=2), 246 | resnet_v2_block('block3', base_depth=256, num_units=23, stride=2), 247 | resnet_v2_block('block4', base_depth=512, num_units=3, stride=1), 248 | ] 249 | return resnet_v2(inputs, blocks, num_classes, is_training=is_training, 250 | global_pool=global_pool, output_stride=output_stride, 251 | include_root_block=True, spatial_squeeze=spatial_squeeze, 252 | reuse=reuse, scope=scope) 253 | resnet_v2_101.default_image_size = resnet_v2.default_image_size 254 | 255 | 256 | def resnet_v2_152(inputs, 257 | num_classes=None, 258 | is_training=True, 259 | global_pool=True, 260 | output_stride=None, 261 | spatial_squeeze=True, 262 | reuse=None, 263 | scope='resnet_v2_152'): 264 | """ResNet-152 model of [1]. See resnet_v2() for arg and return description.""" 265 | blocks = [ 266 | resnet_v2_block('block1', base_depth=64, num_units=3, stride=2), 267 | resnet_v2_block('block2', base_depth=128, num_units=8, stride=2), 268 | resnet_v2_block('block3', base_depth=256, num_units=36, stride=2), 269 | resnet_v2_block('block4', base_depth=512, num_units=3, stride=1), 270 | ] 271 | return resnet_v2(inputs, blocks, num_classes, is_training=is_training, 272 | global_pool=global_pool, output_stride=output_stride, 273 | include_root_block=True, spatial_squeeze=spatial_squeeze, 274 | reuse=reuse, scope=scope) 275 | resnet_v2_152.default_image_size = resnet_v2.default_image_size 276 | 277 | 278 | def resnet_v2_200(inputs, 279 | num_classes=None, 280 | is_training=True, 281 | global_pool=True, 282 | output_stride=None, 283 | spatial_squeeze=True, 284 | reuse=None, 285 | scope='resnet_v2_200'): 286 | """ResNet-200 model of [2]. See resnet_v2() for arg and return description.""" 287 | blocks = [ 288 | resnet_v2_block('block1', base_depth=64, num_units=3, stride=2), 289 | resnet_v2_block('block2', base_depth=128, num_units=24, stride=2), 290 | resnet_v2_block('block3', base_depth=256, num_units=36, stride=2), 291 | resnet_v2_block('block4', base_depth=512, num_units=3, stride=1), 292 | ] 293 | return resnet_v2(inputs, blocks, num_classes, is_training=is_training, 294 | global_pool=global_pool, output_stride=output_stride, 295 | include_root_block=True, spatial_squeeze=spatial_squeeze, 296 | reuse=reuse, scope=scope) 297 | resnet_v2_200.default_image_size = resnet_v2.default_image_size 298 | -------------------------------------------------------------------------------- /lib/model/generator/vgg/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/vgg/.DS_Store -------------------------------------------------------------------------------- /lib/model/generator/vgg/__pycache__/vgg.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/model/generator/vgg/__pycache__/vgg.cpython-35.pyc -------------------------------------------------------------------------------- /lib/model/generator/vgg/vgg.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import division 11 | from __future__ import print_function 12 | 13 | import tensorflow as tf 14 | 15 | slim = tf.contrib.slim 16 | 17 | 18 | def vgg_arg_scope(weight_decay=0.0005): 19 | """Defines the VGG arg scope. 20 | 21 | Args: 22 | weight_decay: The l2 regularization coefficient. 23 | 24 | Returns: 25 | An arg_scope. 26 | """ 27 | with slim.arg_scope([slim.conv2d, slim.fully_connected], 28 | activation_fn=tf.nn.relu, 29 | weights_regularizer=slim.l2_regularizer(weight_decay), 30 | biases_initializer=tf.zeros_initializer()): 31 | with slim.arg_scope([slim.conv2d], padding='SAME') as arg_sc: 32 | return arg_sc 33 | 34 | 35 | def vgg_a(inputs, 36 | num_classes=1000, 37 | is_training=True, 38 | dropout_keep_prob=0.5, 39 | spatial_squeeze=True, 40 | scope='vgg_a', 41 | fc_conv_padding='VALID'): 42 | """Oxford Net VGG 11-Layers version A Example. 43 | 44 | Note: All the fully_connected layers have been transformed to conv2d layers. 45 | To use in classification mode, resize input to 224x224. 46 | 47 | Args: 48 | inputs: a tensor of size [batch_size, height, width, channels]. 49 | num_classes: number of predicted classes. 50 | is_training: whether or not the model is being trained. 51 | dropout_keep_prob: the probability that activations are kept in the dropout 52 | layers during training. 53 | spatial_squeeze: whether or not should squeeze the spatial dimensions of the 54 | outputs. Useful to remove unnecessary dimensions for classification. 55 | scope: Optional scope for the variables. 56 | fc_conv_padding: the type of padding to use for the fully connected layer 57 | that is implemented as a convolutional layer. Use 'SAME' padding if you 58 | are applying the network in a fully convolutional manner and want to 59 | get a prediction map downsampled by a factor of 32 as an output. 60 | Otherwise, the output prediction map will be (input / 32) - 6 in case of 61 | 'VALID' padding. 62 | 63 | Returns: 64 | the last op containing the log predictions and end_points dict. 65 | """ 66 | with tf.variable_scope(scope, 'vgg_a', [inputs]) as sc: 67 | end_points_collection = sc.name + '_end_points' 68 | # Collect outputs for conv2d, fully_connected and max_pool2d. 69 | with slim.arg_scope([slim.conv2d, slim.max_pool2d], 70 | outputs_collections=end_points_collection): 71 | net = slim.repeat(inputs, 1, slim.conv2d, 64, [3, 3], scope='conv1') 72 | net = slim.max_pool2d(net, [2, 2], scope='pool1') 73 | net = slim.repeat(net, 1, slim.conv2d, 128, [3, 3], scope='conv2') 74 | net = slim.max_pool2d(net, [2, 2], scope='pool2') 75 | net = slim.repeat(net, 2, slim.conv2d, 256, [3, 3], scope='conv3') 76 | net = slim.max_pool2d(net, [2, 2], scope='pool3') 77 | net = slim.repeat(net, 2, slim.conv2d, 512, [3, 3], scope='conv4') 78 | net = slim.max_pool2d(net, [2, 2], scope='pool4') 79 | net = slim.repeat(net, 2, slim.conv2d, 512, [3, 3], scope='conv5') 80 | net = slim.max_pool2d(net, [2, 2], scope='pool5') 81 | # Use conv2d instead of fully_connected layers. 82 | net = slim.conv2d(net, 4096, [7, 7], padding=fc_conv_padding, scope='fc6') 83 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 84 | scope='dropout6') 85 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 86 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 87 | scope='dropout7') 88 | net = slim.conv2d(net, num_classes, [1, 1], 89 | activation_fn=None, 90 | normalizer_fn=None, 91 | scope='fc8') 92 | # Convert end_points_collection into a end_point dict. 93 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 94 | if spatial_squeeze: 95 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 96 | end_points[sc.name + '/fc8'] = net 97 | return net, end_points 98 | vgg_a.default_image_size = 224 99 | 100 | 101 | def vgg_16(inputs, 102 | num_classes=None, 103 | is_training=True, 104 | dropout_keep_prob=0.5, 105 | spatial_squeeze=True, 106 | scope='vgg_16', 107 | fc_conv_padding='VALID'): 108 | """Oxford Net VGG 16-Layers version D Example. 109 | 110 | Note: All the fully_connected layers have been transformed to conv2d layers. 111 | To use in classification mode, resize input to 224x224. 112 | 113 | Args: 114 | inputs: a tensor of size [batch_size, height, width, channels]. 115 | num_classes: number of predicted classes. 116 | is_training: whether or not the model is being trained. 117 | dropout_keep_prob: the probability that activations are kept in the dropout 118 | layers during training. 119 | spatial_squeeze: whether or not should squeeze the spatial dimensions of the 120 | outputs. Useful to remove unnecessary dimensions for classification. 121 | scope: Optional scope for the variables. 122 | fc_conv_padding: the type of padding to use for the fully connected layer 123 | that is implemented as a convolutional layer. Use 'SAME' padding if you 124 | are applying the network in a fully convolutional manner and want to 125 | get a prediction map downsampled by a factor of 32 as an output. 126 | Otherwise, the output prediction map will be (input / 32) - 6 in case of 127 | 'VALID' padding. 128 | 129 | Returns: 130 | the last op containing the log predictions and end_points dict. 131 | """ 132 | with tf.variable_scope(scope, 'vgg_16', [inputs]) as sc: 133 | end_points_collection = sc.name + '_end_points' 134 | # Collect outputs for conv2d, fully_connected and max_pool2d. 135 | with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], 136 | outputs_collections=end_points_collection): 137 | net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') 138 | net = slim.max_pool2d(net, [2, 2], scope='pool1') 139 | net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') 140 | net = slim.max_pool2d(net, [2, 2], scope='pool2') 141 | net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3') 142 | net = slim.max_pool2d(net, [2, 2], scope='pool3') 143 | net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4') 144 | net = slim.max_pool2d(net, [2, 2], scope='pool4') 145 | net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5') 146 | net = slim.max_pool2d(net, [2, 2], scope='pool5') 147 | # Use conv2d instead of fully_connected layers. 148 | net = slim.conv2d(net, 4096, [7, 7], padding=fc_conv_padding, scope='fc6') 149 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 150 | scope='dropout6') 151 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 152 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 153 | scope='dropout7') 154 | # Convert end_points_collection into a end_point dict. 155 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 156 | if num_classes is not None: 157 | net = slim.conv2d(net, num_classes, [1, 1], 158 | activation_fn=None, 159 | normalizer_fn=None, 160 | scope='fc8') 161 | if spatial_squeeze: 162 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 163 | end_points[sc.name + '/fc8'] = net 164 | else: 165 | net = net 166 | end_points = end_points 167 | return net, end_points 168 | vgg_16.default_image_size = 224 169 | 170 | 171 | def vgg_16_conv(inputs, 172 | num_classes=None, 173 | is_training=True, 174 | dropout_keep_prob=0.5, 175 | spatial_squeeze=True, 176 | scope='vgg_16', 177 | fc_conv_padding='VALID'): 178 | """ 179 | """ 180 | with tf.variable_scope(scope, 'vgg_16', [inputs]) as sc: 181 | end_points_collection = sc.name + '_end_points' 182 | # Collect outputs for conv2d, fully_connected and max_pool2d. 183 | with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], 184 | outputs_collections=end_points_collection): 185 | net_c = [] 186 | net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') 187 | net_c.append(net) 188 | net = slim.max_pool2d(net, [2, 2], scope='pool1') 189 | net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') 190 | net_c.append(net) 191 | net = slim.max_pool2d(net, [2, 2], scope='pool2') 192 | net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3], scope='conv3') 193 | net_c.append(net) 194 | net = slim.max_pool2d(net, [2, 2], scope='pool3') 195 | net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv4') 196 | net_c.append(net) 197 | net = slim.max_pool2d(net, [2, 2], scope='pool4') 198 | net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3], scope='conv5') 199 | net_c.append(net) 200 | net = slim.max_pool2d(net, [2, 2], scope='pool5') 201 | 202 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 203 | if num_classes is not None: 204 | # Use conv2d instead of fully_connected layers. 205 | net = slim.conv2d(net, 4096, [7, 7], padding=fc_conv_padding, scope='fc6') 206 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout6') 207 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 208 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, scope='dropout7') 209 | # Convert end_points_collection into a end_point dict. 210 | net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='fc8') 211 | if spatial_squeeze: 212 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 213 | end_points[sc.name + '/fc8'] = net 214 | else: 215 | net = net 216 | end_points = end_points 217 | return net, end_points, net_c 218 | vgg_16_conv.default_image_size = 224 219 | 220 | 221 | def vgg_19(inputs, 222 | num_classes=None, 223 | is_training=True, 224 | dropout_keep_prob=0.5, 225 | spatial_squeeze=True, 226 | scope='vgg_19', 227 | fc_conv_padding='VALID'): 228 | """Oxford Net VGG 19-Layers version E Example. 229 | 230 | Note: All the fully_connected layers have been transformed to conv2d layers. 231 | To use in classification mode, resize input to 224x224. 232 | 233 | Args: 234 | inputs: a tensor of size [batch_size, height, width, channels]. 235 | num_classes: number of predicted classes. 236 | is_training: whether or not the model is being trained. 237 | dropout_keep_prob: the probability that activations are kept in the dropout 238 | layers during training. 239 | spatial_squeeze: whether or not should squeeze the spatial dimensions of the 240 | outputs. Useful to remove unnecessary dimensions for classification. 241 | scope: Optional scope for the variables. 242 | fc_conv_padding: the type of padding to use for the fully connected layer 243 | that is implemented as a convolutional layer. Use 'SAME' padding if you 244 | are applying the network in a fully convolutional manner and want to 245 | get a prediction map downsampled by a factor of 32 as an output. 246 | Otherwise, the output prediction map will be (input / 32) - 6 in case of 247 | 'VALID' padding. 248 | 249 | 250 | Returns: 251 | the last op containing the log predictions and end_points dict. 252 | """ 253 | with tf.variable_scope(scope, 'vgg_19', [inputs]) as sc: 254 | end_points_collection = sc.name + '_end_points' 255 | # Collect outputs for conv2d, fully_connected and max_pool2d. 256 | with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d], 257 | outputs_collections=end_points_collection): 258 | net = slim.repeat(inputs, 2, slim.conv2d, 64, [3, 3], scope='conv1') 259 | net = slim.max_pool2d(net, [2, 2], scope='pool1') 260 | net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3], scope='conv2') 261 | net = slim.max_pool2d(net, [2, 2], scope='pool2') 262 | net = slim.repeat(net, 4, slim.conv2d, 256, [3, 3], scope='conv3') 263 | net = slim.max_pool2d(net, [2, 2], scope='pool3') 264 | net = slim.repeat(net, 4, slim.conv2d, 512, [3, 3], scope='conv4') 265 | net = slim.max_pool2d(net, [2, 2], scope='pool4') 266 | net = slim.repeat(net, 4, slim.conv2d, 512, [3, 3], scope='conv5') 267 | net = slim.max_pool2d(net, [2, 2], scope='pool5') 268 | # Use conv2d instead of fully_connected layers. 269 | net = slim.conv2d(net, 4096, [7, 7], padding=fc_conv_padding, scope='fc6') 270 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 271 | scope='dropout6') 272 | net = slim.conv2d(net, 4096, [1, 1], scope='fc7') 273 | net = slim.dropout(net, dropout_keep_prob, is_training=is_training, 274 | scope='dropout7') 275 | end_points = slim.utils.convert_collection_to_dict(end_points_collection) 276 | if num_classes is not None: 277 | net = slim.conv2d(net, num_classes, [1, 1], 278 | activation_fn=None, 279 | normalizer_fn=None, 280 | scope='fc8') 281 | if spatial_squeeze: 282 | net = tf.squeeze(net, [1, 2], name='fc8/squeezed') 283 | end_points[sc.name + '/fc8'] = net 284 | else: 285 | net = net 286 | end_points = end_points 287 | return net, end_pointss 288 | vgg_19.default_image_size = 224 289 | 290 | # Alias 291 | vgg_d = vgg_16 292 | vgg_e = vgg_19 293 | -------------------------------------------------------------------------------- /lib/model/pix2pix/pix2pix.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | import collections 8 | import functools 9 | 10 | import tensorflow as tf 11 | 12 | layers = tf.contrib.layers 13 | 14 | 15 | def pix2pix_arg_scope(): 16 | """Returns a default argument scope for isola_net. 17 | Returns: 18 | An arg scope. 19 | """ 20 | # These parameters come from the online port, which don't necessarily match 21 | # those in the paper. 22 | # TODO(nsilberman): confirm these values with Philip. 23 | instance_norm_params = { 24 | 'center': True, 25 | 'scale': True, 26 | 'epsilon': 0.00001, 27 | } 28 | 29 | with tf.contrib.framework.arg_scope( 30 | [layers.conv2d, layers.conv2d_transpose], 31 | normalizer_fn=layers.instance_norm, 32 | normalizer_params=instance_norm_params, 33 | weights_initializer=tf.random_normal_initializer(0, 0.02)) as sc: 34 | return sc 35 | 36 | 37 | def upsample(net, num_outputs, kernel_size, method='nn_upsample_conv'): 38 | """Upsamples the given inputs. 39 | Args: 40 | net: A `Tensor` of size [batch_size, height, width, filters]. 41 | num_outputs: The number of output filters. 42 | kernel_size: A list of 2 scalars or a 1x2 `Tensor` indicating the scale, 43 | relative to the inputs, of the output dimensions. For example, if kernel 44 | size is [2, 3], then the output height and width will be twice and three 45 | times the input size. 46 | method: The upsampling method. 47 | Returns: 48 | An `Tensor` which was upsampled using the specified method. 49 | Raises: 50 | ValueError: if `method` is not recognized. 51 | """ 52 | net_shape = tf.shape(net) 53 | height = net_shape[1] 54 | width = net_shape[2] 55 | 56 | if method == 'nn_upsample_conv': 57 | net = tf.image.resize_nearest_neighbor( 58 | net, [kernel_size[0] * height, kernel_size[1] * width]) 59 | net = layers.conv2d(net, num_outputs, [4, 4], activation_fn=None) 60 | elif method == 'conv2d_transpose': 61 | net = layers.conv2d_transpose( 62 | net, num_outputs, [4, 4], stride=kernel_size, activation_fn=None) 63 | else: 64 | raise ValueError('Unknown method: [%s]', method) 65 | 66 | return net 67 | 68 | 69 | class Block( 70 | collections.namedtuple('Block', ['num_filters', 'decoder_keep_prob'])): 71 | """Represents a single block of encoder and decoder processing. 72 | The Image-to-Image translation paper works a bit differently than the original 73 | U-Net model. In particular, each block represents a single operation in the 74 | encoder which is concatenated with the corresponding decoder representation. 75 | A dropout layer follows the concatenation and convolution of the concatenated 76 | features. 77 | """ 78 | pass 79 | 80 | 81 | def _default_generator_blocks(): 82 | """Returns the default generator block definitions. 83 | Returns: 84 | A list of generator blocks. 85 | """ 86 | return [ 87 | Block(64, 0.5), 88 | Block(128, 0.5), 89 | Block(256, 0.5), 90 | Block(512, 0), 91 | Block(512, 0), 92 | Block(512, 0), 93 | Block(512, 0), 94 | ] 95 | 96 | 97 | def pix2pix_generator(net, 98 | num_outputs, 99 | blocks=None, 100 | upsample_method='nn_upsample_conv', 101 | is_training=False): # pylint: disable=unused-argument 102 | """Defines the network architecture. 103 | Args: 104 | net: A `Tensor` of size [batch, height, width, channels]. Note that the 105 | generator currently requires square inputs (e.g. height=width). 106 | num_outputs: The number of (per-pixel) outputs. 107 | blocks: A list of generator blocks or `None` to use the default generator 108 | definition. 109 | upsample_method: The method of upsampling images, one of 'nn_upsample_conv' 110 | or 'conv2d_transpose' 111 | is_training: Whether or not we're in training or testing mode. 112 | Returns: 113 | A `Tensor` representing the model output and a dictionary of model end 114 | points. 115 | Raises: 116 | ValueError: if the input heights do not match their widths. 117 | """ 118 | end_points = {} 119 | 120 | blocks = blocks or _default_generator_blocks() 121 | 122 | input_size = net.get_shape().as_list() 123 | height, width = input_size[1], input_size[2] 124 | if height != width: 125 | raise ValueError('The input height must match the input width.') 126 | 127 | input_size[3] = num_outputs 128 | 129 | upsample_fn = functools.partial(upsample, method=upsample_method) 130 | 131 | encoder_activations = [] 132 | 133 | ########### 134 | # Encoder # 135 | ########### 136 | with tf.variable_scope('encoder'): 137 | with tf.contrib.framework.arg_scope( 138 | [layers.conv2d], 139 | kernel_size=[4, 4], 140 | stride=2, 141 | activation_fn=tf.nn.leaky_relu): 142 | 143 | for block_id, block in enumerate(blocks): 144 | # No normalizer for the first encoder layers as per 'Image-to-Image', 145 | # Section 5.1.1 146 | if block_id == 0: 147 | # First layer doesn't use normalizer_fn 148 | net = layers.conv2d(net, block.num_filters, normalizer_fn=None) 149 | elif block_id < len(blocks) - 1: 150 | net = layers.conv2d(net, block.num_filters) 151 | else: 152 | # Last layer doesn't use activation_fn nor normalizer_fn 153 | net = layers.conv2d( 154 | net, block.num_filters, activation_fn=None, normalizer_fn=None) 155 | 156 | encoder_activations.append(net) 157 | end_points['encoder%d' % block_id] = net 158 | 159 | ########### 160 | # Decoder # 161 | ########### 162 | reversed_blocks = list(blocks) 163 | reversed_blocks.reverse() 164 | 165 | with tf.variable_scope('decoder'): 166 | # Dropout is used at both train and test time as per 'Image-to-Image', 167 | # Section 2.1 (last paragraph). 168 | with tf.contrib.framework.arg_scope([layers.dropout], is_training=True): 169 | 170 | for block_id, block in enumerate(reversed_blocks): 171 | if block_id > 0: 172 | net = tf.concat([net, encoder_activations[-block_id - 1]], axis=3) 173 | 174 | # The Relu comes BEFORE the upsample op: 175 | net = tf.nn.relu(net) 176 | net = upsample_fn(net, block.num_filters, [2, 2]) 177 | if block.decoder_keep_prob > 0: 178 | net = layers.dropout(net, keep_prob=block.decoder_keep_prob) 179 | end_points['decoder%d' % block_id] = net 180 | 181 | with tf.variable_scope('output'): 182 | logits = layers.conv2d(net, num_outputs, [4, 4], activation_fn=None) 183 | logits = tf.reshape(logits, input_size) 184 | 185 | end_points['logits'] = logits 186 | end_points['predictions'] = tf.tanh(logits) 187 | 188 | return logits, end_points 189 | 190 | 191 | def pix2pix_discriminator(net, num_filters, padding=2, is_training=False): 192 | """Creates the Image2Image Translation Discriminator. 193 | Args: 194 | net: A `Tensor` of size [batch_size, height, width, channels] representing 195 | the input. 196 | num_filters: A list of the filters in the discriminator. The length of the 197 | list determines the number of layers in the discriminator. 198 | padding: Amount of reflection padding applied before each convolution. 199 | is_training: Whether or not the model is training or testing. 200 | Returns: 201 | A logits `Tensor` of size [batch_size, N, N, 1] where N is the number of 202 | 'patches' we're attempting to discriminate and a dictionary of model end 203 | points. 204 | """ 205 | del is_training 206 | end_points = {} 207 | 208 | num_layers = len(num_filters) 209 | 210 | def padded(net, scope): 211 | if padding: 212 | with tf.variable_scope(scope): 213 | spatial_pad = tf.constant( 214 | [[0, 0], [padding, padding], [padding, padding], [0, 0]], 215 | dtype=tf.int32) 216 | return tf.pad(net, spatial_pad, 'REFLECT') 217 | else: 218 | return net 219 | 220 | with tf.contrib.framework.arg_scope( 221 | [layers.conv2d], 222 | kernel_size=[4, 4], 223 | stride=2, 224 | padding='valid', 225 | activation_fn=tf.nn.leaky_relu): 226 | 227 | # No normalization on the input layer. 228 | net = layers.conv2d( 229 | padded(net, 'conv0'), num_filters[0], normalizer_fn=None, scope='conv0') 230 | 231 | end_points['conv0'] = net 232 | 233 | for i in range(1, num_layers - 1): 234 | net = layers.conv2d( 235 | padded(net, 'conv%d' % i), num_filters[i], scope='conv%d' % i) 236 | end_points['conv%d' % i] = net 237 | 238 | # Stride 1 on the last layer. 239 | net = layers.conv2d( 240 | padded(net, 'conv%d' % (num_layers - 1)), 241 | num_filters[-1], 242 | stride=1, 243 | scope='conv%d' % (num_layers - 1)) 244 | end_points['conv%d' % (num_layers - 1)] = net 245 | 246 | # 1-dim logits, stride 1, no activation, no normalization. 247 | logits = layers.conv2d( 248 | padded(net, 'conv%d' % num_layers), 249 | 1, 250 | stride=1, 251 | activation_fn=None, 252 | normalizer_fn=None, 253 | scope='conv%d' % num_layers) 254 | end_points['logits'] = logits 255 | end_points['predictions'] = tf.sigmoid(logits) 256 | return logits, end_points -------------------------------------------------------------------------------- /lib/model/pix2pix/pix2pix_test.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | import tensorflow as tf 8 | from nets import pix2pix 9 | 10 | 11 | class GeneratorTest(tf.test.TestCase): 12 | 13 | def test_nonsquare_inputs_raise_exception(self): 14 | batch_size = 2 15 | height, width = 240, 320 16 | num_outputs = 4 17 | 18 | images = tf.ones((batch_size, height, width, 3)) 19 | 20 | with self.assertRaises(ValueError): 21 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 22 | pix2pix.pix2pix_generator( 23 | images, num_outputs, upsample_method='nn_upsample_conv') 24 | 25 | def _reduced_default_blocks(self): 26 | """Returns the default blocks, scaled down to make test run faster.""" 27 | return [pix2pix.Block(b.num_filters // 32, b.decoder_keep_prob) 28 | for b in pix2pix._default_generator_blocks()] 29 | 30 | def test_output_size_nn_upsample_conv(self): 31 | batch_size = 2 32 | height, width = 256, 256 33 | num_outputs = 4 34 | 35 | images = tf.ones((batch_size, height, width, 3)) 36 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 37 | logits, _ = pix2pix.pix2pix_generator( 38 | images, num_outputs, blocks=self._reduced_default_blocks(), 39 | upsample_method='nn_upsample_conv') 40 | 41 | with self.test_session() as session: 42 | session.run(tf.global_variables_initializer()) 43 | np_outputs = session.run(logits) 44 | self.assertListEqual([batch_size, height, width, num_outputs], 45 | list(np_outputs.shape)) 46 | 47 | def test_output_size_conv2d_transpose(self): 48 | batch_size = 2 49 | height, width = 256, 256 50 | num_outputs = 4 51 | 52 | images = tf.ones((batch_size, height, width, 3)) 53 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 54 | logits, _ = pix2pix.pix2pix_generator( 55 | images, num_outputs, blocks=self._reduced_default_blocks(), 56 | upsample_method='conv2d_transpose') 57 | 58 | with self.test_session() as session: 59 | session.run(tf.global_variables_initializer()) 60 | np_outputs = session.run(logits) 61 | self.assertListEqual([batch_size, height, width, num_outputs], 62 | list(np_outputs.shape)) 63 | 64 | def test_block_number_dictates_number_of_layers(self): 65 | batch_size = 2 66 | height, width = 256, 256 67 | num_outputs = 4 68 | 69 | images = tf.ones((batch_size, height, width, 3)) 70 | blocks = [ 71 | pix2pix.Block(64, 0.5), 72 | pix2pix.Block(128, 0), 73 | ] 74 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 75 | _, end_points = pix2pix.pix2pix_generator( 76 | images, num_outputs, blocks) 77 | 78 | num_encoder_layers = 0 79 | num_decoder_layers = 0 80 | for end_point in end_points: 81 | if end_point.startswith('encoder'): 82 | num_encoder_layers += 1 83 | elif end_point.startswith('decoder'): 84 | num_decoder_layers += 1 85 | 86 | self.assertEqual(num_encoder_layers, len(blocks)) 87 | self.assertEqual(num_decoder_layers, len(blocks)) 88 | 89 | 90 | class DiscriminatorTest(tf.test.TestCase): 91 | 92 | def _layer_output_size(self, input_size, kernel_size=4, stride=2, pad=2): 93 | return (input_size + pad * 2 - kernel_size) // stride + 1 94 | 95 | def test_four_layers(self): 96 | batch_size = 2 97 | input_size = 256 98 | 99 | output_size = self._layer_output_size(input_size) 100 | output_size = self._layer_output_size(output_size) 101 | output_size = self._layer_output_size(output_size) 102 | output_size = self._layer_output_size(output_size, stride=1) 103 | output_size = self._layer_output_size(output_size, stride=1) 104 | 105 | images = tf.ones((batch_size, input_size, input_size, 3)) 106 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 107 | logits, end_points = pix2pix.pix2pix_discriminator( 108 | images, num_filters=[64, 128, 256, 512]) 109 | self.assertListEqual([batch_size, output_size, output_size, 1], 110 | logits.shape.as_list()) 111 | self.assertListEqual([batch_size, output_size, output_size, 1], 112 | end_points['predictions'].shape.as_list()) 113 | 114 | def test_four_layers_no_padding(self): 115 | batch_size = 2 116 | input_size = 256 117 | 118 | output_size = self._layer_output_size(input_size, pad=0) 119 | output_size = self._layer_output_size(output_size, pad=0) 120 | output_size = self._layer_output_size(output_size, pad=0) 121 | output_size = self._layer_output_size(output_size, stride=1, pad=0) 122 | output_size = self._layer_output_size(output_size, stride=1, pad=0) 123 | 124 | images = tf.ones((batch_size, input_size, input_size, 3)) 125 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 126 | logits, end_points = pix2pix.pix2pix_discriminator( 127 | images, num_filters=[64, 128, 256, 512], padding=0) 128 | self.assertListEqual([batch_size, output_size, output_size, 1], 129 | logits.shape.as_list()) 130 | self.assertListEqual([batch_size, output_size, output_size, 1], 131 | end_points['predictions'].shape.as_list()) 132 | 133 | def test_four_layers_wrog_paddig(self): 134 | batch_size = 2 135 | input_size = 256 136 | 137 | images = tf.ones((batch_size, input_size, input_size, 3)) 138 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 139 | with self.assertRaises(TypeError): 140 | pix2pix.pix2pix_discriminator( 141 | images, num_filters=[64, 128, 256, 512], padding=1.5) 142 | 143 | def test_four_layers_negative_padding(self): 144 | batch_size = 2 145 | input_size = 256 146 | 147 | images = tf.ones((batch_size, input_size, input_size, 3)) 148 | with tf.contrib.framework.arg_scope(pix2pix.pix2pix_arg_scope()): 149 | with self.assertRaises(ValueError): 150 | pix2pix.pix2pix_discriminator( 151 | images, num_filters=[64, 128, 256, 512], padding=-1) 152 | 153 | if __name__ == '__main__': 154 | tf.test.main() -------------------------------------------------------------------------------- /lib/optimizer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/optimizer/.DS_Store -------------------------------------------------------------------------------- /lib/optimizer/optimizer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import tensorflow as tf 10 | import numpy as np 11 | 12 | def adam_optimizer(learning_rate, adam_beta1=0.5, adam_beta2=0.999, opt_epsilon=1e-08): 13 | optimizer = tf.train.AdamOptimizer(learning_rate, beta1=adam_beta1, beta2=adam_beta2, epsilon=opt_epsilon) 14 | return optimizer 15 | 16 | def adadelta_optimizer(learning_rate, adadelta_rho=0.95, opt_epsilon=1e-08): 17 | optimizer = tf.train.AdadeltaOptimizer(learning_rate, rho=adadelta_rho, epsilon=opt_epsilon) 18 | return optimizer 19 | 20 | def adagrad_optimizer(learning_rate, adagrad_initial_accumulator_value=0.1): 21 | optimizer = tf.train.AdagradOptimizer(learning_rate, initial_accumulator_value=adagrad_initial_accumulator_value) 22 | return optimizer 23 | 24 | def ftrl_optimizer(learning_rate, ftrl_learning_rate_power=-0.5, ftrl_initial_accumulator_value=0.1, ftrl_l1=0.0, ftrl_l2=0.0): 25 | optimizer = tf.train.FtrlOptimizer(learning_rate, learning_rate_power=ftrl_learning_rate_power, initial_accumulator_value=ftrl_initial_accumulator_value, l1_regularization_strength=ftrl_l1, l2_regularization_strength=ftrl_l2) 26 | return optimizer 27 | 28 | def momentum_optimizer(learning_rate, momentum): 29 | optimizer = tf.train.MomentumOptimizer(learning_rate, momentum=momentum, name='Momentum') 30 | return optimizer 31 | 32 | def rmsprop_optimizer(learning_rate, rmsprop_decay=0.9, rmsprop_momentum=0.0, opt_epsilon=1e-10): 33 | optimizer = tf.train.RMSPropOptimizer(learning_rate, decay=rmsprop_decay, momentum=rmsprop_momentum, epsilon=opt_epsilon) 34 | return optimizer 35 | 36 | def sgd_optimizer(learning_rate): 37 | optimizer = tf.train.GradientDescentOptimizer(learning_rate) 38 | return optimizer 39 | 40 | 41 | 42 | def _configure_optimizer(learning_rate): 43 | """Configures the optimizer used for training. 44 | Args: 45 | learning_rate: A scalar or `Tensor` learning rate. 46 | Returns: 47 | An instance of an optimizer. 48 | Raises: 49 | ValueError: if FLAGS.optimizer is not recognized. 50 | """ 51 | if FLAGS.optimizer == 'adadelta': 52 | optimizer = tf.train.AdadeltaOptimizer( 53 | learning_rate, 54 | rho=FLAGS.adadelta_rho, 55 | epsilon=FLAGS.opt_epsilon) 56 | elif FLAGS.optimizer == 'adagrad': 57 | optimizer = tf.train.AdagradOptimizer( 58 | learning_rate, 59 | initial_accumulator_value=FLAGS.adagrad_initial_accumulator_value) 60 | elif FLAGS.optimizer == 'adam': 61 | optimizer = tf.train.AdamOptimizer( 62 | learning_rate, 63 | beta1=FLAGS.adam_beta1, 64 | beta2=FLAGS.adam_beta2, 65 | epsilon=FLAGS.opt_epsilon) 66 | elif FLAGS.optimizer == 'ftrl': 67 | optimizer = tf.train.FtrlOptimizer( 68 | learning_rate, 69 | learning_rate_power=FLAGS.ftrl_learning_rate_power, 70 | initial_accumulator_value=FLAGS.ftrl_initial_accumulator_value, 71 | l1_regularization_strength=FLAGS.ftrl_l1, 72 | l2_regularization_strength=FLAGS.ftrl_l2) 73 | elif FLAGS.optimizer == 'momentum': 74 | optimizer = tf.train.MomentumOptimizer( 75 | learning_rate, 76 | momentum=FLAGS.momentum, 77 | name='Momentum') 78 | elif FLAGS.optimizer == 'rmsprop': 79 | optimizer = tf.train.RMSPropOptimizer( 80 | learning_rate, 81 | decay=FLAGS.rmsprop_decay, 82 | momentum=FLAGS.rmsprop_momentum, 83 | epsilon=FLAGS.opt_epsilon) 84 | elif FLAGS.optimizer == 'sgd': 85 | optimizer = tf.train.GradientDescentOptimizer(learning_rate) 86 | else: 87 | raise ValueError('Optimizer [%s] was not recognized', FLAGS.optimizer) 88 | return optimizer 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /lib/optimizer/optimizer_minimize.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import tensorflow as tf 10 | import numpy as np 11 | 12 | 13 | def optimizer_minimize(optimizer, loss, global_step=tf.train.get_global_step(), var_list=tf.all_variables()): 14 | opt_op = optimizer.minimize(loss, var_list=var_list) 15 | return opt_op 16 | 17 | 18 | def optimizer_apply_gradients(optimizer, loss, global_step=tf.train.get_global_step(), var_list=tf.all_variables()): 19 | gradients = tf.gradients(loss, var_list) 20 | opt_op = optimizer.apply_gradients(zip(gradients, var_list), global_step=global_step) 21 | return opt_op 22 | -------------------------------------------------------------------------------- /lib/train/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/train/.DS_Store -------------------------------------------------------------------------------- /lib/train/train_GANs.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import tensorflow as tf 11 | slim = tf.contrib.slim 12 | from lib.utils.GANs_utils import build_generator, build_discriminator, cost, train_op, get_next_batch_from_path, gan_loss, preprocess_img 13 | from lib.utils.GANs_utils import sample_noise, generator_input_placeholder, discriminator_input_placeholder,shuffle_train_data 14 | import os 15 | import sys 16 | import matplotlib.pyplot as plt 17 | import matplotlib.gridspec as gridspec 18 | 19 | from tensorflow.examples.tutorials.mnist import input_data 20 | # mnist = input_data.read_data_sets('./datasets/MNIST_data', one_hot=False) 21 | 22 | # %matplotlib inline 23 | plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots 24 | plt.rcParams['image.interpolation'] = 'nearest' 25 | plt.rcParams['image.cmap'] = 'gray' 26 | 27 | # A bunch of utility functions 28 | 29 | def show_images(images): 30 | # images = np.reshape(images, [images.shape[0], -1]) # images reshape to (batch_size, D) 31 | # sqrtn = int(np.ceil(np.sqrt(images.shape[0]))) 32 | # sqrtimg = int(np.ceil(np.sqrt(images.shape[1]))) 33 | 34 | fig = plt.figure(figsize=(32, 32)) 35 | gs = gridspec.GridSpec(32, 32) 36 | gs.update(wspace=0.05, hspace=0.05) 37 | 38 | for i, img in enumerate(images): 39 | ax = plt.subplot(gs[i]) 40 | plt.axis('off') 41 | ax.set_xticklabels([]) 42 | ax.set_yticklabels([]) 43 | ax.set_aspect('equal') 44 | # plt.imshow(img.reshape([sqrtimg,sqrtimg])) 45 | plt.imshow(img.reshape([32,32,3])) 46 | return 47 | 48 | # GPU 参数配置 49 | def GPU_config(rate=0.8): 50 | 51 | os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # 按照PCI_BUS_ID顺序从0开始排列GPU设备 52 | os.environ["CUDA_VISIBLE_DEVICES"] = "1" # 设置当前使用的GPU设备仅为0号设备 53 | gpuConfig = tf.ConfigProto() 54 | gpuConfig.allow_soft_placement = False #设置为True,当GPU不存在或者程序中出现GPU不能运行的代码时,自动切换到CPU运行 55 | gpuConfig.gpu_options.allow_growth = True #设置为True,程序运行时,会根据程序所需GPU显存情况,分配最小的资源 56 | gpuConfig.gpu_options.per_process_gpu_memory_fraction = rate #程序运行的时,所需的GPU显存资源最大不允许超过rate的设定值 57 | 58 | return gpuConfig 59 | 60 | def train_GANs(train_data,train_label,valid_data,valid_label,train_dir,num_classes,batch_size,arch_model,learning_r_decay,learning_rate_base,decay_rate,dropout_prob,epoch,height,width,checkpoint_exclude_scopes,early_stop,EARLY_STOP_PATIENCE,fine_tune,train_all_layers,checkpoint_path,train_n,valid_n,g_parameter,dim = 64): 61 | # ---------------------------------------------------------------------------------# 62 | G_X, G_Y, G_is_train, G_keep_prob_fc = generator_input_placeholder(dim, num_classes) 63 | if arch_model == "arch_dcgan_unconditional": 64 | with tf.variable_scope("Generator_dcgan", reuse=None) as scope: 65 | G_net, _ = build_generator(G_X, num_classes, G_keep_prob_fc, G_is_train,arch_model) 66 | elif arch_model == "arch_dcgan_conditional": 67 | with tf.variable_scope("Generator_dcgan", reuse=None) as scope: 68 | G_net, _ = build_generator([G_X, G_Y], num_classes, G_keep_prob_fc, G_is_train,arch_model) 69 | else: 70 | print ('{} is error!', arch_model) 71 | 72 | D_X, D_Y, D_is_train, D_keep_prob_fc = discriminator_input_placeholder(height, width, num_classes) 73 | if arch_model == "arch_dcgan_unconditional": 74 | with tf.variable_scope("Discriminator_dcgan", reuse=tf.AUTO_REUSE) as scope: 75 | logits_real, _ = build_discriminator(D_X, num_classes, D_keep_prob_fc, D_is_train,arch_model) 76 | # scope.reuse_variables() 77 | logits_fake, _ = build_discriminator(G_net, num_classes, D_keep_prob_fc, D_is_train,arch_model) 78 | elif arch_model == "arch_dcgan_conditional": 79 | with tf.variable_scope("Discriminator_dcgan", reuse=tf.AUTO_REUSE) as scope: 80 | logits_real, _ = build_discriminator([D_X, D_Y], num_classes, D_keep_prob_fc, D_is_train,arch_model) 81 | # scope.reuse_variables() 82 | logits_fake, _ = build_discriminator([G_net,G_Y] , num_classes, D_keep_prob_fc, D_is_train,arch_model) 83 | else: 84 | print ('{} is error!', arch_model) 85 | 86 | # tf.GraphKeys.TRAINABLE_VARIABLES, tf.GraphKeys.GLOBAL_VARIABLES 87 | G_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'Generator_dcgan') 88 | # print (G_vars) 89 | D_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'Discriminator_dcgan') 90 | # print (G_vars) 91 | 92 | D_loss, G_loss = gan_loss(logits_real, logits_fake) 93 | # G_loss = cost(logits_fake) 94 | # D_loss = cost(logits_real) + cost(logits_fake) 95 | 96 | global_step = tf.Variable(0, trainable=False) 97 | if learning_r_decay: 98 | learning_rate = tf.train.exponential_decay( 99 | learning_rate_base, 100 | global_step * batch_size, 101 | train_n, 102 | decay_rate, 103 | staircase=True) 104 | else: 105 | learning_rate = learning_rate_base 106 | if arch_model == "arch_dcgan_unconditional": 107 | G_optimizer = train_op(learning_rate, G_loss, G_vars, global_step) 108 | D_optimizer = train_op(learning_rate*0.1, D_loss, D_vars, global_step) 109 | elif arch_model == "arch_dcgan_conditional": 110 | G_optimizer = train_op(learning_rate, G_loss, G_vars, global_step) 111 | D_optimizer = train_op(learning_rate*0.1, D_loss, D_vars, global_step) 112 | else: 113 | print ('{} is error!', arch_model) 114 | 115 | #------------------------------------------------------------------------------------# 116 | sess = tf.Session(config=GPU_config()) 117 | init = tf.global_variables_initializer() 118 | sess.run(init) 119 | saver2 = tf.train.Saver(G_vars) 120 | if not train_all_layers: 121 | saver_net = tf.train.Saver(G_vars) 122 | saver_net.restore(sess, checkpoint_path) 123 | 124 | if fine_tune: 125 | # saver2.restore(sess, fine_tune_dir) 126 | latest = tf.train.latest_checkpoint(train_dir) 127 | if not latest: 128 | print ("No checkpoint to continue from in", train_dir) 129 | sys.exit(1) 130 | print ("resume", latest) 131 | saver2.restore(sess, latest) 132 | 133 | # early stopping 134 | best_valid = np.inf 135 | best_valid_epoch = 0 136 | 137 | for epoch_i in range(epoch): 138 | for batch_i in range(int(train_n/batch_size)): 139 | dim = dim 140 | generator_x = sess.run(sample_noise(batch_size, dim)) 141 | # images,_ = mnist.train.next_batch(batch_size) 142 | # images = preprocess_img(images) 143 | images, labels = get_next_batch_from_path(train_data, train_label, batch_i, height, width, batch_size=batch_size, training=True) 144 | if arch_model == "arch_dcgan_unconditional": 145 | D_los, _ = sess.run([D_loss,D_optimizer], feed_dict={G_X: generator_x, G_is_train:True, G_keep_prob_fc:dropout_prob,D_X: images,D_is_train:True, D_keep_prob_fc:dropout_prob}) 146 | G_los, _ = sess.run([G_loss,G_optimizer], feed_dict={G_X: generator_x,G_is_train:True, G_keep_prob_fc:dropout_prob,D_X: images,D_is_train:True, D_keep_prob_fc:dropout_prob}) 147 | elif arch_model == "arch_dcgan_conditional": 148 | D_los, _ = sess.run([D_loss,D_optimizer], feed_dict={G_X: generator_x,G_Y:labels,G_is_train:True,G_keep_prob_fc:dropout_prob,D_X: images,D_Y:labels,D_is_train:True, D_keep_prob_fc:dropout_prob}) 149 | G_los, _ = sess.run([G_loss,G_optimizer], feed_dict={G_X: generator_x,G_Y:labels,G_is_train:True, G_keep_prob_fc:dropout_prob,D_X: images,D_is_train:True, D_keep_prob_fc:dropout_prob}) 150 | else: 151 | print ('{} is error!', arch_model) 152 | print ('D_los:', D_los) 153 | print ('G_los:', G_los) 154 | checkpoint_path = os.path.join(train_dir, 'model.ckpt') 155 | saver2.save(sess, checkpoint_path, global_step=batch_i, write_meta_graph=False) 156 | if batch_i%20==0: 157 | if arch_model == "arch_dcgan_unconditional": 158 | D_loss_ = sess.run(D_loss, feed_dict={G_X: generator_x, G_is_train:True, G_keep_prob_fc:1.0,D_X: images, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 159 | G_loss_ = sess.run(G_loss, feed_dict={G_X: generator_x, G_is_train:True, G_keep_prob_fc:1.0, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 160 | elif arch_model == "arch_dcgan_conditional": 161 | D_loss_ = sess.run(D_loss, feed_dict={G_X: generator_x,G_Y:labels,G_is_train:True,G_keep_prob_fc:1.0,D_X: images,D_Y:labels,D_is_train:False, D_keep_prob_fc:1.0}) 162 | G_loss_ = sess.run(G_loss, feed_dict={G_X: generator_x,G_Y:labels,G_is_train:True, G_keep_prob_fc:1.0, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 163 | else: 164 | print ('{} is error!', arch_model) 165 | 166 | print('Batch: {:>2}: D_training loss: {:>3.5f}'.format(batch_i, D_loss_)) 167 | print('Batch: {:>2}: G_training loss: {:>3.5f}'.format(batch_i, G_loss_)) 168 | 169 | if batch_i%100==0: 170 | generator_x = sess.run(sample_noise(batch_size, dim)) 171 | # images,_ = mnist.train.next_batch(batch_size) 172 | # images = preprocess_img(images) 173 | images, labels = get_next_batch_from_path(valid_data, valid_label, batch_i%(int(valid_n/batch_size)), height, width, batch_size=batch_size, training=False) 174 | if arch_model == "arch_dcgan_unconditional": 175 | D_ls = sess.run(D_loss, feed_dict={G_X: generator_x, G_is_train:False, G_keep_prob_fc:1.0, D_X: images, D_is_train:False, D_keep_prob_fc:1.0}) 176 | G_ls = sess.run(G_loss, feed_dict={G_X: generator_x, G_is_train:False, G_keep_prob_fc:1.0, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 177 | elif arch_model == "arch_dcgan_conditional": 178 | D_ls = sess.run(D_loss, feed_dict={G_X: generator_x,G_Y:labels,G_is_train:False,G_keep_prob_fc:1.0,D_X: images,D_Y:labels,D_is_train:False, D_keep_prob_fc:1.0}) 179 | G_ls = sess.run(G_loss, feed_dict={G_X: generator_x,G_Y:labels,G_is_train:False, G_keep_prob_fc:1.0, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 180 | else: 181 | print ('{} is error!', arch_model) 182 | 183 | print('Batch: {:>2}: D_validation loss: {:>3.5f}'.format(batch_i, D_ls)) 184 | print('Batch: {:>2}: G_validation loss: {:>3.5f}'.format(batch_i, G_ls)) 185 | 186 | 187 | print('Epoch===================================>: {:>2}'.format(epoch_i)) 188 | G_valid_ls = 0 189 | G_samples = [] 190 | for batch_i in range(int(valid_n/batch_size)): 191 | generator_x = sess.run(sample_noise(batch_size, dim)) 192 | if arch_model == "arch_dcgan_unconditional": 193 | images, labels = get_next_batch_from_path(valid_data, valid_label, batch_i%(int(valid_n/batch_size)), height, width, batch_size=batch_size, training=False) 194 | G_epoch_ls, G_samples = sess.run([G_loss, G_net], feed_dict={G_X: generator_x, G_keep_prob_fc:1.0, G_is_train:False, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 195 | elif arch_model == "arch_dcgan_conditional": 196 | images, labels = get_next_batch_from_path(valid_data, valid_label, batch_i%(int(valid_n/batch_size)), height, width, batch_size=batch_size, training=False) 197 | G_epoch_ls, G_samples = sess.run([G_loss, G_net], feed_dict={G_X: generator_x, G_Y:labels, G_keep_prob_fc:1.0, G_is_train:False, D_X: images,D_is_train:False, D_keep_prob_fc:1.0}) 198 | 199 | G_valid_ls = G_valid_ls + G_epoch_ls 200 | fig = show_images(G_samples[:16]) 201 | plt.show() 202 | print('Epoch: {:>2}: G_validation loss: {:>3.5f}'.format(epoch_i, G_valid_ls/int(valid_n/batch_size))) 203 | # ---------------------------------------------------------------------------------# 204 | if early_stop: 205 | loss_valid = G_valid_ls/int(valid_n/batch_size) 206 | if loss_valid < best_valid: 207 | best_valid = loss_valid 208 | best_valid_epoch = epoch_i 209 | elif best_valid_epoch + EARLY_STOP_PATIENCE < epoch_i: 210 | print("Early stopping.") 211 | print("Best valid loss was {:.6f} at epoch {}.".format(best_valid, best_valid_epoch)) 212 | break 213 | train_data, train_label = shuffle_train_data(train_data, train_label) 214 | sess.close() 215 | -------------------------------------------------------------------------------- /lib/utils/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lib/utils/.DS_Store -------------------------------------------------------------------------------- /lib/utils/GANs_utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import tensorflow as tf 10 | slim = tf.contrib.slim 11 | import numpy as np 12 | import sklearn.metrics 13 | import cv2 14 | import os 15 | # from data_aug.data_aug import DataAugmenters 16 | from lib.model.generator.build_generator.build_generator import generator_arch 17 | from lib.model.discriminator.build_discriminator.build_discriminator import discriminator_arch 18 | from lib.model.build_net import networks 19 | import tensorflow as tf 20 | from lib.data_aug.data_aug import DataAugmenters 21 | from lib.loss.loss import softmax_loss, sigmoid_loss, squared_loss, add_l2 22 | from lib.optimizer.optimizer import adam_optimizer,sgd_optimizer, rmsprop_optimizer 23 | from lib.optimizer.optimizer_minimize import optimizer_minimize, optimizer_apply_gradients 24 | 25 | def g_parameter(checkpoint_exclude_scopes): 26 | exclusions = [] 27 | if checkpoint_exclude_scopes: 28 | exclusions = [scope.strip() for scope in checkpoint_exclude_scopes.split(',')] 29 | variables_to_restore = [] 30 | variables_to_train = [] 31 | for var in slim.get_model_variables(): 32 | print (var) 33 | excluded = False 34 | for exclusion in exclusions: 35 | if var.op.name.startswith(exclusion): 36 | excluded = True 37 | variables_to_train.append(var) 38 | print (var) 39 | break 40 | if not excluded: 41 | variables_to_restore.append(var) 42 | return variables_to_restore,variables_to_train 43 | 44 | def shuffle_train_data(train_imgs, train_labels): 45 | index = [i for i in range(len(train_imgs))] 46 | np.random.shuffle(index) 47 | train_imgs = np.asarray(train_imgs) 48 | train_labels = np.asarray(train_labels) 49 | train_imgs = train_imgs[index] 50 | train_labels = train_labels[index] 51 | return train_imgs, train_labels 52 | 53 | def preprocess_img(x): 54 | return 2 * x - 1.0 55 | def data_norm(img): 56 | img = img / 255.0 57 | img = img - 0.5 58 | img = img * 2 59 | return img 60 | def data_aug(img): 61 | return DataAugmenters(img).run() 62 | 63 | def get_next_batch_from_path(image_path, image_labels, pointer, height, width, batch_size=64, training=True): 64 | batch_x = np.zeros([batch_size, height, width,3]) 65 | # batch_x = np.zeros([batch_size, height*width]) 66 | num_classes = len(image_labels[0]) 67 | batch_y = np.zeros([batch_size, num_classes]) 68 | for i in range(batch_size): 69 | image = cv2.imread(image_path[i+pointer*batch_size]) 70 | # image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 71 | image = cv2.resize(image, (height, width)) 72 | #if training: 73 | # image = data_aug([image])[0] 74 | # image = data_aug(image) 75 | image = data_norm(image) 76 | # batch_x[i,:,:,:] = image 77 | batch_x[i] = image #.reshape(-1) 78 | batch_y[i] = image_labels[i+pointer*batch_size] 79 | return [batch_x, batch_y] 80 | 81 | def img_crop(img, box): 82 | # y1, x1, y2, x2 = box[1]-20, box[0]-20, box[1]+box[3]+40, box[0]+box[2]+40 83 | x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3]) 84 | img = img[y1:y2, x1:x2] 85 | return img 86 | 87 | 88 | ####### 89 | def generator_input_placeholder(dim, num_classes): 90 | X = tf.placeholder(tf.float32, [None, dim]) 91 | Y = tf.placeholder(tf.float32, [None, num_classes]) 92 | is_train = tf.placeholder(tf.bool) 93 | keep_prob_fc = tf.placeholder(tf.float32) 94 | return X, Y, is_train, keep_prob_fc 95 | 96 | 97 | def discriminator_input_placeholder(height, width, num_classes): 98 | # X = tf.placeholder(tf.float32, [None, height, width, 3]) 99 | X = tf.placeholder(tf.float32, [None, height, width, 3]) 100 | Y = tf.placeholder(tf.float32, [None, num_classes]) 101 | is_train = tf.placeholder(tf.bool) 102 | keep_prob_fc = tf.placeholder(tf.float32) 103 | return X, Y, is_train, keep_prob_fc 104 | 105 | 106 | 107 | def build_generator(X, num_classes, keep_prob_fc, is_train, arch_model): 108 | arch = networks 109 | if arch_model == "arch_inception_v4_multi_label": 110 | net, net_vis = arch.arch_inception_v4(X, num_classes, keep_prob_fc, is_train) 111 | elif arch_model == "arch_inception_v4_rnn_multi_label": 112 | net, net_vis = arch.arch_inception_v4_rnn(X, num_classes, keep_prob_fc, is_train) 113 | elif arch_model == "arch_inception_v4_rnn_attention_multi_label": 114 | net, net_vis = arch.arch_inception_v4_rnn_attention(X, num_classes, keep_prob_fc, is_train) 115 | elif arch_model == "arch_alexnet_v2_multi_label": 116 | net, net_vis = arch.arch_alexnet_v2(X, num_classes, keep_prob_fc, is_train) 117 | elif arch_model == "arch_lp_net_multi_label": 118 | net, net_vis = arch.arch_lp_net(X, num_classes, keep_prob_fc, is_train) 119 | elif arch_model == "arch_vgg16_multi_label": 120 | net, net_vis = arch.arch_vgg16(X, num_classes, keep_prob_fc, is_train) 121 | elif arch_model == "arch_dcgan_unconditional": 122 | net, net_vis = arch.generator(X, is_training=is_train) 123 | elif arch_model == "arch_dcgan_conditional": 124 | net, net_vis = arch.conditional_generator(X, is_training=is_train) 125 | else: 126 | print ('{} is error!', arch_model) 127 | return net, net_vis 128 | 129 | 130 | def build_discriminator(X, num_classes, keep_prob_fc, is_train, arch_model): 131 | arch = networks 132 | if arch_model == "arch_inception_v4_multi_label": 133 | net, net_vis = arch.arch_inception_v4(X, num_classes, keep_prob_fc, is_train) 134 | elif arch_model == "arch_inception_v4_rnn_multi_label": 135 | net, net_vis = arch.arch_inception_v4_rnn(X, num_classes, keep_prob_fc, is_train) 136 | elif arch_model == "arch_inception_v4_rnn_attention_multi_label": 137 | net, net_vis = arch.arch_inception_v4_rnn_attention(X, num_classes, keep_prob_fc, is_train) 138 | elif arch_model == "arch_alexnet_v2_multi_label": 139 | net, net_vis = arch.arch_alexnet_v2(X, num_classes, keep_prob_fc, is_train) 140 | elif arch_model == "arch_lp_net_multi_label": 141 | net, net_vis = arch.arch_lp_net(X, num_classes, keep_prob_fc, is_train) 142 | elif arch_model == "arch_vgg16_multi_label": 143 | net, net_vis = arch.arch_vgg16(X, num_classes, keep_prob_fc, is_train) 144 | elif arch_model == "arch_dcgan_unconditional": 145 | net, net_vis = arch.discriminator(X, is_training=is_train) 146 | elif arch_model == "arch_dcgan_conditional": 147 | net, net_vis = arch.conditional_discriminator(X[0], [X[0],X[1]], is_training=is_train) 148 | else: 149 | print ('{} is error!', arch_model) 150 | return net, net_vis 151 | 152 | def cost(logit): 153 | # loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = label, logits = logit)) 154 | label = tf.ones_like(logit) 155 | loss = sigmoid_loss(label = label, logit = logit) 156 | return loss 157 | 158 | def gan_loss_no_log(logits_real, logits_fake): 159 | D_real_loss= tf.square(logits_real-1) 160 | D_fake_loss= tf.square(logits_fake) 161 | D_loss=tf.reduce_mean(D_real_loss+D_fake_loss) 162 | 163 | G_loss= tf.square(logits_fake-1) 164 | G_loss=tf.reduce_mean(G_loss) 165 | 166 | return D_loss, G_loss 167 | 168 | def gan_loss(logits_real, logits_fake): 169 | # TODO: compute D_loss and G_loss 170 | 171 | D_real_loss=tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(logits_real),logits=logits_real) 172 | D_fake_loss=tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.zeros_like(logits_fake),logits=logits_fake) 173 | D_loss=tf.reduce_mean(D_real_loss+D_fake_loss) 174 | 175 | G_loss=tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.ones_like(logits_fake),logits=logits_fake) 176 | G_loss=tf.reduce_mean(G_loss) 177 | 178 | return D_loss, G_loss 179 | 180 | ''' 181 | generator_opt = tf.train.RMSPropOptimizer(gen_lr, decay=.9, momentum=0.1) 182 | discriminator_opt = tf.train.RMSPropOptimizer(dis_lr, decay=.95, momentum=0.1) 183 | ''' 184 | def train_op(learning_rate, loss, variables_to_train, global_step): 185 | update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 186 | with tf.control_dependencies(update_ops): 187 | if variables_to_train == []: 188 | opt_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, global_step=global_step) 189 | # optimizer = adam_optimizer(learning_rate) 190 | # opt_op = optimizer_minimize(optimizer, loss, global_step) 191 | else: 192 | #opt_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss, var_list = variables_to_train, global_step=global_step) 193 | # optimizer = rmsprop_optimizer(learning_rate) 194 | optimizer = adam_optimizer(learning_rate) 195 | opt_op = optimizer_minimize(optimizer, loss, global_step, var_list = variables_to_train) 196 | return opt_op 197 | 198 | def model_accuracy(net, Y, num_classes): 199 | predict = tf.reshape(net, [-1, num_classes]) 200 | max_idx_p = tf.argmax(predict, 1) 201 | max_idx_l = tf.argmax(Y, 1) 202 | correct_pred = tf.equal(max_idx_p, max_idx_l) 203 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 204 | return accuracy 205 | 206 | def model_accuracy_seg(net, Y, num_classes): 207 | predict = tf.reshape(net, [-1, num_classes]) 208 | max_idx_p = tf.argmax(predict, 1) 209 | max_idx_l = tf.argmax(Y, 1) 210 | correct_pred = tf.equal(max_idx_p, max_idx_l) 211 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 212 | return accuracy 213 | 214 | def compute_map(net, Y): 215 | 216 | nclasses = Y.shape[1] 217 | all_ap = [] 218 | for cid in range(nclasses): 219 | gt_cls = Y[:, cid].astype('float32') 220 | pred_cls = net[:, cid].astype('float32') 221 | # 某个人标签没有属性值,continue; 222 | if np.sum(gt_cls) == 0: 223 | continue 224 | # As per PhilK. code: 225 | # https://github.com/philkr/voc-classification/blob/master/src/train_cls.py 226 | pred_cls -= 1e-5 * gt_cls 227 | ap = sklearn.metrics.average_precision_score( 228 | gt_cls, pred_cls, average=None) 229 | all_ap.append(ap) 230 | mAP = np.mean(all_ap).astype('float32') 231 | return mAP 232 | 233 | def model_mAP(net, Y): 234 | mAP = tf.py_func(compute_map,[net, Y], tf.float32) 235 | return mAP 236 | 237 | # 同样也可用于多标签 238 | def to_one_hot(labels, num_classes): 239 | labels_onehot = [] 240 | for i in range(len(labels)): 241 | label = np.zeros([num_classes], np.float32) # 标签容器,注意大小---------------------------change------------------------------------ 242 | for j in list([labels[i]]): 243 | try: 244 | label[j] = 1. # 若图像标签为j,label[j] = 1 245 | except(): 246 | continue 247 | labels_onehot += [label] 248 | return labels_onehot 249 | 250 | def leaky_relu(x, alpha=0.01): 251 | return tf.maximum(alpha*x,x) 252 | 253 | def sample_noise(batch_size, dim): 254 | return tf.random_uniform([batch_size,dim], minval=-1, maxval=1, dtype=tf.float32) 255 | 256 | 257 | 258 | -------------------------------------------------------------------------------- /lp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/lp.jpg -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | """ 6 | 7 | import numpy as np 8 | import os 9 | import tensorflow as tf 10 | slim = tf.contrib.slim 11 | from lib.data_load.data_load_from_txt_mullabel import data_load_from_txt_mullabel 12 | from lib.data_load.data_load import load_image 13 | from lib.utils.GANs_utils import g_parameter 14 | from lib.utils.GANs_utils import to_one_hot 15 | # from lib.train.train3 import train3 as train 16 | from keras.utils import np_utils 17 | import config 18 | os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" 19 | os.environ["CUDA_VISIBLE_DEVICES"] = "0" 20 | 21 | sample_dir = config.sample_dir 22 | tfRecord = config.tfRecord 23 | num_classes = config.num_classes 24 | batch_size = config.batch_size 25 | arch_model = config.arch_model 26 | checkpoint_exclude_scopes = config.checkpoint_exclude_scopes 27 | dropout_prob = config.dropout_prob 28 | train_rate = config.train_rate 29 | epoch = config.epoch 30 | # 是否使用提前终止训练 31 | early_stop = config.early_stop 32 | EARLY_STOP_PATIENCE = config.EARLY_STOP_PATIENCE 33 | # 是否使用learning_rate 34 | learning_r_decay = config.learning_r_decay 35 | learning_rate_base = config.learning_rate_base 36 | decay_rate = config.decay_rate 37 | height, width = config.height, config.width 38 | # 模型保存的路径 39 | train_dir = config.train_dir 40 | # 是否进行fine-tune。 选择fine-tune的的参数 41 | fine_tune = config.fine_tune 42 | # 训练所有层的参数 43 | train_all_layers = config.train_all_layers 44 | # 迁移学习的网络模型 45 | checkpoint_path = config.checkpoint_path 46 | 47 | 48 | from lib.train.train_GANs import train_GANs as train 49 | # train_data, train_label, valid_data, valid_label, train_n, valid_n, note_label = data_load_from_txt_mullabel(sample_dir, train_rate).gen_train_valid() 50 | 51 | train_data, train_label, valid_data, valid_label, train_n, valid_n, note_label = load_image(sample_dir, train_rate).gen_train_valid() 52 | 53 | print ('note_label', note_label) 54 | print (train_data) 55 | print (train_label) 56 | 57 | 58 | train_label = to_one_hot(train_label, num_classes) 59 | valid_label = to_one_hot(valid_label, num_classes) 60 | 61 | print (train_label) 62 | if not os.path.isdir(train_dir): 63 | os.makedirs(train_dir) 64 | train(train_data,train_label,valid_data,valid_label,train_dir,num_classes,batch_size,arch_model,learning_r_decay,learning_rate_base,decay_rate,dropout_prob,epoch,height,width,checkpoint_exclude_scopes,early_stop,EARLY_STOP_PATIENCE,fine_tune,train_all_layers,checkpoint_path,train_n,valid_n,g_parameter) 65 | 66 | -------------------------------------------------------------------------------- /model/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/model/.DS_Store -------------------------------------------------------------------------------- /model/README.md: -------------------------------------------------------------------------------- 1 | 2 | # 训练的模型。 3 | -------------------------------------------------------------------------------- /pretrain/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MachineLP/train_cnn_GANs/66e8d917b91bd45337bcc0d4e603133fba67ad89/pretrain/.DS_Store -------------------------------------------------------------------------------- /pretrain/README.md: -------------------------------------------------------------------------------- 1 | 2 | # 预训练好的模型放在这里。 3 | -------------------------------------------------------------------------------- /test_accuracy.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import tensorflow as tf 10 | from tensorflow.python.framework import graph_util 11 | from lib.data_load.data_load import load_image 12 | from lib.utils.utils import input_placeholder3, g_parameter, data_norm, model_accuracy 13 | from lib.utils.utils import get_next_batch_from_path3, shuffle_train_data, cost, build_net3 14 | from keras.utils import np_utils 15 | import cv2 16 | import numpy as np 17 | import os 18 | import sys 19 | import config 20 | 21 | num_classes = config.num_classes 22 | height, width = config.height, config.width 23 | arch_model = config.arch_model 24 | batch_size = config.batch_size 25 | sample_dir = 'gender' 26 | train_rate = 1.0 27 | test_data, test_label, valid_data, valid_label, test_n, valid_n, note_label = load_image(sample_dir, train_rate).gen_train_valid() 28 | print ('test_n', test_n) 29 | print ('valid_n', valid_n) 30 | test_label = np_utils.to_categorical(test_label, num_classes) 31 | valid_label = np_utils.to_categorical(valid_label, num_classes) 32 | 33 | 34 | X1,X2,X3, Y, is_train, keep_prob_fc = input_placeholder3(height, width, num_classes) 35 | net, net_vis = build_net3(X1,X2,X3, num_classes, keep_prob_fc, is_train,arch_model) 36 | loss = cost(Y, net) 37 | accuracy = model_accuracy(net, Y, num_classes) 38 | # predict = tf.reshape(net, [-1, num_classes], name='predictions') 39 | 40 | 41 | 42 | if __name__ == '__main__': 43 | train_dir = 'model' 44 | latest = tf.train.latest_checkpoint(train_dir) 45 | if not latest: 46 | print ("No checkpoint to continue from in", train_dir) 47 | sys.exit(1) 48 | print ("resume", latest) 49 | sess = tf.Session() 50 | saver = tf.train.Saver(tf.global_variables()) 51 | saver.restore(sess, latest) 52 | test_ls = 0 53 | test_acc = 0 54 | for batch_i in range(int(test_n/batch_size)): 55 | images_test1, images_test2, images_test3, labels_test = get_next_batch_from_path3(test_data, test_label, batch_i, height, width, batch_size=batch_size, training=False) 56 | epoch_ls, epoch_acc = sess.run([loss, accuracy], feed_dict={X1: images_test1,X2: images_test2,X3: images_test3, Y: labels_test, keep_prob_fc:1.0, is_train:False}) 57 | print (epoch_acc) 58 | test_ls = test_ls + epoch_ls 59 | test_acc = test_acc + epoch_acc 60 | print('Epoch: {:>2}: Validation loss: {:>3.5f}, Validation accuracy: {:>3.5f}'.format(0, test_ls/int(test_n/batch_size), test_acc/int(test_n/batch_size))) 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /test_tfRecords.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on 2017 10.17 4 | @author: liupeng 5 | wechat: lp9628 6 | blog: http://blog.csdn.net/u014365862/article/details/78422372 7 | """ 8 | 9 | import numpy as np 10 | import tensorflow as tf 11 | import argparse 12 | import os 13 | 14 | from PIL import Image 15 | 16 | FLAGS = None 17 | 18 | batch_size = 64 19 | 20 | def read_image(file_queue, num_classes): 21 | reader = tf.TFRecordReader() 22 | key, value = reader.read(file_queue) 23 | _, serialized_example = reader.read(file_queue) 24 | features = tf.parse_single_example( 25 | serialized_example, 26 | features={ 27 | 'img': tf.FixedLenFeature([], tf.string), 28 | 'label': tf.FixedLenFeature([20], tf.float32), 29 | #'height': tf.FixedLenFeature([], tf.string), 30 | #'width': tf.FixedLenFeature([], tf.string), 31 | }) 32 | image = tf.decode_raw(features['img'], tf.uint8) 33 | #h = tf.decode_raw(features['height'], tf.int64) 34 | #w = tf.decode_raw(features['width'], tf.int64) 35 | # image_shape = tf.stack([h, w, 3]) 36 | image = tf.reshape(image, [299,299,3]) 37 | # image = tf.image.resize_images(image, (299, 299), method=0) 38 | # image = tf.cast(image, tf.float32) 39 | 40 | label = features['label'] 41 | print (label) 42 | # label = tf.cast(features['label'], tf.string) 43 | # label = tf.reshape(label, [num_classes]) 44 | # label = tf.cast(label, tf.float32) 45 | return image, label 46 | 47 | def read_image_batch(file_queue, num_classes, batchsize): 48 | img, label = read_image(file_queue, num_classes) 49 | min_after_dequeue = 2 50 | capacity = min_after_dequeue + 3 * batchsize 51 | # image_batch, label_batch = tf.train.batch([img, label], batch_size=batchsize, capacity=capacity) 52 | image_batch, label_batch = tf.train.shuffle_batch([img, label], batch_size=batchsize, capacity=capacity, min_after_dequeue=min_after_dequeue) 53 | label_batch = tf.to_float(label_batch) 54 | return image_batch, label_batch 55 | 56 | 57 | def main(_): 58 | 59 | train_file_path = "img_train.tfrecords" 60 | test_file_path = "img_test.tfrecords" 61 | model_path = "cnn_jisuanshi_model" 62 | 63 | train_image_filename_queue = tf.train.string_input_producer(tf.train.match_filenames_once(train_file_path)) 64 | img, label = read_image(train_image_filename_queue, 20) 65 | train_images, train_labels = read_image_batch(train_image_filename_queue, 20,batch_size) 66 | 67 | sess = tf.InteractiveSession() 68 | tf.local_variables_initializer().run() 69 | tf.global_variables_initializer().run() 70 | 71 | # start queue runner 72 | coord = tf.train.Coordinator() 73 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 74 | 75 | for i in range(2): 76 | images, labels = sess.run([img, label]) 77 | print (images, labels) 78 | images = tf.reshape(images, [299,299, 3]) 79 | images = tf.image.convert_image_dtype(images, dtype=tf.uint8) 80 | images = tf.image.encode_jpeg(images) 81 | # print(sess.run(labels)) 82 | with tf.gfile.GFile('pic_%d.jpg' % i, 'wb') as f: 83 | f.write(sess.run(images)) 84 | #img=Image.fromarray(images, 'RGB')#这里Image是之前提到的 85 | #img.save(str(i)+'_''Label_'+str(i)+'.jpg')#存下图片 86 | 87 | coord.request_stop() 88 | coord.join(threads) 89 | sess.close() 90 | 91 | if __name__ == '__main__': 92 | tf.app.run(main=main) 93 | 94 | 95 | --------------------------------------------------------------------------------