def __init__(self, config): """ GANTrainer expects a ModelDesc in config which sets the following attribute after :meth:`_build_graph`: g_loss, d_loss, g_vars, d_vars. """ input = QueueInput(config.dataflow) model = config.model cbs = input.setup(model.get_inputs_desc()) config.callbacks.extend(cbs) with TowerContext('', is_training=True): model.build_graph(input) opt = model.get_optimizer() # by default, run one d_min after one g_min with tf.name_scope('optimize'): g_min = opt.minimize(model.g_loss, var_list=model.g_vars, name='g_op') with tf.control_dependencies([g_min]): d_min = opt.minimize(model.d_loss, var_list=model.d_vars, name='d_op') self.train_op = d_min super(GANTrainer, self).__init__(config)
def minutiae_whole_image(model_path, sample_path, imgs, output_name='reconstruction/gen:0'): imgs = glob.glob('/Data/Rolled/NISTSD14/Image2/*.bmp') imgs.sort() with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name( 'QueueInput/input_deque:0') # is_training minutiae_cylinder_placeholder = tf.get_default_graph( ).get_tensor_by_name(output_name) for n, file in enumerate(imgs): img0 = cv2.imread(file, cv2.IMREAD_GRAYSCALE) img = img0 / 128.0 - 1 img = np.expand_dims(img, axis=2) img = np.expand_dims(img, axis=0) feed_dict = {images_placeholder: img} minutiae_cylinder = sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.squeeze(minutiae_cylinder, axis=0) minutiae = prepare_data.get_minutiae_from_cylinder( minutiae_cylinder, thr=0.25) minutiae = prepare_data.refine_minutiae(minutiae, dist_thr=10, ori_dist=np.pi / 4) prepare_data.show_minutiae(img0, minutiae) print n
def load(self, session, model_path): x_input = tf.placeholder(tf.float32, shape=(None, ) + self.x_shape) x_input = tf.transpose(x_input, [0, 3, 1, 2]) with TowerContext('', is_training=False): with tf.variable_scope('', reuse=tf.AUTO_REUSE): _ = self.model.get_logits(x_input) get_model_loader(model_path).init(session)
def __init__(self, config, d_period=1, g_period=1): """ Args: d_period(int): period of each d_opt run g_period(int): period of each g_opt run """ self._d_period = int(d_period) self._g_period = int(g_period) assert min(d_period, g_period) == 1 input = QueueInput(config.dataflow) model = config.model cbs = input.setup(model.get_inputs_desc()) config.callbacks.extend(cbs) with TowerContext('', is_training=True): model.build_graph(input) opt = model.get_optimizer() with tf.name_scope('optimize'): self.d_min = opt.minimize(model.d_loss, var_list=model.d_vars, name='d_min') self.g_min = opt.minimize(model.g_loss, var_list=model.g_vars, name='g_min') super(SeparateGANTrainer, self).__init__(config)
def enhancement_whole_image(model_path, sample_path, imgs, output_name='reconstruction/gen:0'): imgs = glob.glob('/media/kaicao/Data/Data/Rolled/NISTSD4/Image_Aligned' + '/*.jpeg') #imgs = glob.glob('/home/kaicao/Dropbox/Research/Data/Latent/NISTSD27/image/'+'*.bmp') #imgs = glob.glob('/research/prip-kaicao/Data/Latent/DB/NIST27/image/' + '*.bmp') #imgs = glob.glob('/research/prip-kaicao/Data/Rolled/NIST4/Image/'+'*.bmp') #imgs = glob.glob('/future/Data/Rolled/NSITSD14/Image2_jpeg/*.jpeg') imgs = glob.glob( '/home/kaicao/Dropbox/Research/Data/Latent/NISTSD27/image/*.bmp') imgs.sort() sample_path = '/home/kaicao/Research/AutomatedLatentRecognition/enhanced_latents_2/' weight = get_weights(opt.SHAPE, opt.SHAPE, 1) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name( 'sub:0') #is_training minutiae_cylinder_placeholder = tf.get_default_graph( ).get_tensor_by_name(output_name) for k, file in enumerate(imgs): img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) img = Image.fromarray(img) img = ImageEnhance.Contrast(img) img = np.asarray(img) img = img / 128.0 - 1 #img = img[0:512,0:512] h, w = img.shape img = np.expand_dims(img, axis=2) img = np.expand_dims(img, axis=0) feed_dict = {images_placeholder: img} minutiae_cylinder = sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) #minutiae_cylinder = np.zeros((h, w, 1)) #minutiae_cylinder_array[:,-10:,:,:] = 0 #minutiae_cylinder_array[:, :10, :, :] = 0 #minutiae_cylinder_array[:, :, -10:, :] = 0 #minutiae_cylinder_array[:, :, 10, :] = 0 #for i in range(n): # minutiae_cylinder[y[i]:y[i]+opt.SHAPE,x[i]:x[i]+opt.SHAPE,:] =minutiae_cylinder[y[i]:y[i]+opt.SHAPE,x[i]:x[i]+opt.SHAPE,:] + minutiae_cylinder_array[i]*weight #print minutiae_cylinder #minutiae = prepare_data.get_minutiae_from_cylinder(minutiae_cylinder,thr=0.25) minutiae_cylinder = np.squeeze(minutiae_cylinder, axis=0) minutiae_cylinder = np.squeeze(minutiae_cylinder, axis=2) minV = np.min(minutiae_cylinder) maxV = np.max(minutiae_cylinder) minutiae_cylinder = (minutiae_cylinder - minV) / (maxV - minV) * 255 cv2.imwrite((sample_path + 'test_%03d.jpeg' % (k + 1)), minutiae_cylinder) #cv2.imwrite('test_{}.jpeg'.format(k), minutiae_cylinder) print h, w
def minutiae_extraction3(model_path, sample_path, imgs, output_name='reconstruction/gen:0', block=True): imgs = glob.glob('/research/prip-kaicao/Data/Rolled/NIST4/Image/' + '*.bmp') imgs.sort() import os if not os.path.isdir(sample_path): os.makedirs(sample_path) weight = get_weights(opt.SHAPE, opt.SHAPE, 12) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name('sub:0') minutiae_cylinder_placeholder = tf.get_default_graph().get_tensor_by_name(output_name) for k, file in enumerate(imgs): img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) h, w = img.shape x = [] y = [] nrof_samples = len(range(0, h, opt.SHAPE // 2)) * len(range(0, w, opt.SHAPE // 2)) patches = np.zeros((nrof_samples, opt.SHAPE, opt.SHAPE, 1)) n = 0 for i in range(0, h - opt.SHAPE + 1, opt.SHAPE // 2): for j in range(0, w - opt.SHAPE + 1, opt.SHAPE // 2): print j patch = img[i:i + opt.SHAPE, j:j + opt.SHAPE, np.newaxis] x.append(j) y.append(i) patches[n, :, :, :] = patch n = n + 1 # print x[-1] feed_dict = {images_placeholder: patches} minutiae_cylinder_array = sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.zeros((h, w, 12)) minutiae_cylinder_array[:, -10:, :, :] = 0 minutiae_cylinder_array[:, :10, :, :] = 0 minutiae_cylinder_array[:, :, -10:, :] = 0 minutiae_cylinder_array[:, :, 10, :] = 0 for i in range(n): minutiae_cylinder[y[i]:y[i] + opt.SHAPE, x[i]:x[i] + opt.SHAPE, :] = minutiae_cylinder[y[i]:y[i] + opt.SHAPE, x[i]:x[i] + opt.SHAPE, :] + minutiae_cylinder_array[i] * weight minutiae = prepare_data.get_minutiae_from_cylinder(minutiae_cylinder, thr=0.1) minutiae = prepare_data.refine_minutiae(minutiae, dist_thr=10, ori_dist=np.pi / 4) minutiae_sets = [] minutiae_sets.append(minutiae) fname = sample_path + os.path.basename(file)[:-4] + 'nms' + '.jpeg' prepare_data.show_minutiae_sets(img, minutiae_sets, ROI=None, fname=fname, block=block) print(n)
def logits_and_labels(self, xs_ph): xs_ph = xs_ph * 2.0 - 1.0 xs_ph = xs_ph[:, :, :, ::-1] xs_ph = tf.transpose(xs_ph, [0, 3, 1, 2]) with TowerContext('', is_training=False): with tf.variable_scope('', reuse=tf.AUTO_REUSE): logits = self.model.get_logits(xs_ph) predicts = tf.nn.softmax(logits) predicted_labels = tf.argmax(predicts, 1) return logits, predicted_labels
def enhancement2(model_path, sample_path, imgs, output_name='reconstruction/gen:0'): imgs = glob.glob('/Data/Data/Rolled/NISTSD4/Image_Aligned' + '/*.jpeg') imgs = glob.glob('/Data/Latent/NISTSD27/image/*.bmp') imgs.sort() sample_path = '/AutomatedLatentRecognition/enhanced_latents_3/' weight = get_weights(opt.SHAPE, opt.SHAPE, 1) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name( 'sub:0') minutiae_cylinder_placeholder = tf.get_default_graph( ).get_tensor_by_name(output_name) for k, file in enumerate(imgs): img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) u, texture = LP.FastCartoonTexture(img) img = texture / 128.0 - 1 h, w = img.shape x = [] y = [] nrof_samples = len(range(0, h, opt.SHAPE // 2)) * len( range(0, w, opt.SHAPE // 2)) patches = np.zeros((nrof_samples, opt.SHAPE, opt.SHAPE, 1)) n = 0 for i in range(0, h - opt.SHAPE + 1, opt.SHAPE // 2): for j in range(0, w - opt.SHAPE + 1, opt.SHAPE // 2): patch = img[i:i + opt.SHAPE, j:j + opt.SHAPE, np.newaxis] x.append(j) y.append(i) patches[n, :, :, :] = patch n = n + 1 feed_dict = {images_placeholder: patches} minutiae_cylinder_array = sess.run( minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.zeros((h, w, 1)) for i in range(n): minutiae_cylinder[ y[i]:y[i] + opt.SHAPE, x[i]:x[i] + opt.SHAPE, :] = minutiae_cylinder[ y[i]:y[i] + opt.SHAPE, x[i]:x[i] + opt. SHAPE, :] + minutiae_cylinder_array[i] * weight minV = np.min(minutiae_cylinder) maxV = np.max(minutiae_cylinder) minutiae_cylinder = (minutiae_cylinder - minV) / (maxV - minV) * 255 cv2.imwrite((sample_path + 'test_%03d.jpeg' % (k + 1)), minutiae_cylinder)
def _setup(self): super(SeparateGANTrainer, self)._setup() with TowerContext('', is_training=True): self.model.build_graph(self._input_source) opt = self.model.get_optimizer() self.d_min = opt.minimize( self.model.d_loss, var_list=self.model.d_vars, name='d_min') self.g_min = opt.minimize( self.model.g_loss, var_list=self.model.g_vars, name='g_min') self._cnt = 1
def _setup(self): super(GANTrainer, self)._setup() with TowerContext('', is_training=True): self.model.build_graph(self._input_source) opt = self.model.get_optimizer() # by default, run one d_min after one g_min self.g_min = opt.minimize(self.model.g_loss, var_list=self.model.g_vars, name='g_op') with tf.control_dependencies([self.g_min]): self.d_min = opt.minimize(self.model.d_loss, var_list=self.model.d_vars, name='d_op') self.train_op = self.d_min
def _setup(self): super(GANTrainer, self)._setup() with TowerContext(''): actual_inputs = self._get_input_tensors() self.model.build_graph(actual_inputs) self.g_min = self.config.optimizer.minimize(self.model.g_loss, var_list=self.model.g_vars, name='g_op') self.d_min = self.config.optimizer.minimize(self.model.d_loss, var_list=self.model.d_vars, name='d_op') self.gs_incr = tf.assign_add(get_global_step_var(), 1, name='global_step_incr') self.summary_op = summary_moving_average() self.d_min = tf.group(self.d_min, self.summary_op, self.gs_incr)
def enhancement_whole_image(model_path, sample_path, imgs, output_name='reconstruction/gen:0'): imgs = glob.glob('/Data/Rolled/NISTSD4/Image_Aligned' + '/*.jpeg') imgs = glob.glob('/Data/Latent/NISTSD27/image/*.bmp') imgs.sort() sample_path = '/AutomatedLatentRecognition/enhanced_latents_2/' weight = get_weights(opt.SHAPE, opt.SHAPE, 1) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name( 'sub:0') #is_training minutiae_cylinder_placeholder = tf.get_default_graph( ).get_tensor_by_name(output_name) for k, file in enumerate(imgs): img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) img = Image.fromarray(img) img = ImageEnhance.Contrast(img) img = np.asarray(img) img = img / 128.0 - 1 h, w = img.shape img = np.expand_dims(img, axis=2) img = np.expand_dims(img, axis=0) feed_dict = {images_placeholder: img} minutiae_cylinder = sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.squeeze(minutiae_cylinder, axis=0) minutiae_cylinder = np.squeeze(minutiae_cylinder, axis=2) minV = np.min(minutiae_cylinder) maxV = np.max(minutiae_cylinder) minutiae_cylinder = (minutiae_cylinder - minV) / (maxV - minV) * 255 cv2.imwrite((sample_path + 'test_%03d.jpeg' % (k + 1)), minutiae_cylinder) print h, w
def load(self, **kwargs): session = kwargs["session"] assert isinstance(session, tf.Session) x_input = tf.placeholder(tf.float32, shape=(None, ) + self.x_shape) x_input = tf.transpose(x_input, [0, 3, 1, 2]) with TowerContext('', is_training=False): with tf.variable_scope('', reuse=tf.AUTO_REUSE): logits = self.model.get_logits(x_input) model_path = get_model_path('R152-Denoise.npz') url = 'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz' if not os.path.exists(model_path): if not os.path.exists(os.path.dirname(model_path)): os.makedirs(os.path.dirname(model_path)) from six.moves import urllib urllib.request.urlretrieve(url, model_path, show_progress) get_model_loader(model_path).init(session)
def calc_flops(model): # manually build the graph with batch=1 input_desc = [ InputDesc(tf.float32, [1, 224, 224, 3], 'input'), InputDesc(tf.int32, [1], 'label') ] input = PlaceholderInput() input.setup(input_desc) with TowerContext('', is_training=False): model.build_graph(*input.get_input_tensors()) model_utils.describe_trainable_vars() tf.profiler.profile( tf.get_default_graph(), cmd='op', options=tf.profiler.ProfileOptionBuilder.float_operation()) logger.info( "Note that TensorFlow counts flops in a different way from the paper.") logger.info( "TensorFlow counts multiply+add as two flops, however the paper counts them " "as 1 flop because it can be executed in one instruction.")
def minutiae_whole_image(model_path,sample_path, imgs, output_name='reconstruction/gen:0'): #imgs = glob.glob('/media/kaicao/Data/Data/Rolled/NISTSD4/Image_Aligned'+'/*.jpeg') #imgs = glob.glob('/home/kaicao/Dropbox/Research/Data/Latent/NISTSD27/image/'+'*.bmp') imgs = glob.glob('/future/Data/Rolled/NISTSD14/Image2/*.bmp') #imgs = glob.glob('/research/prip-kaicao/Data/Latent/DB/NIST27/image/' + '*.bmp') #imgs = glob.glob('/research/prip-kaicao/Data/Rolled/NIST4/Image/'+'*.bmp') imgs.sort() weight = get_weights(opt.SHAPE, opt.SHAPE, 12) import os #if not os.path.isdir(sample_path): # os.makedirs(sample_path) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training= get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name('QueueInput/input_deque:0') #is_training minutiae_cylinder_placeholder = tf.get_default_graph().get_tensor_by_name(output_name) for n, file in enumerate(imgs): img0 = cv2.imread(file,cv2.IMREAD_GRAYSCALE) img = img0/128.0-1 img = np.expand_dims(img,axis=2) img = np.expand_dims(img,axis=0) feed_dict = {images_placeholder: img} minutiae_cylinder= sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.squeeze(minutiae_cylinder,axis=0) minutiae = prepare_data.get_minutiae_from_cylinder(minutiae_cylinder,thr=0.25) #cv2.imwrite('test_0.jpeg', (minutiae_cylinder[:, :, 0:3]) * 255) #cv2.imwrite('test_1.jpeg', (minutiae_cylinder[:, :, 3:6]) * 255) #cv2.imwrite('test_2.jpeg', (minutiae_cylinder[:, :, 6:9]) * 255) #cv2.imwrite('test_3.jpeg', (minutiae_cylinder[:, :, 9:12]) * 255) #prepare_data.show_features(img, minutiae, fname=os.path.basename(file)[:-4] +'.jpeg') minutiae = prepare_data.refine_minutiae(minutiae, dist_thr=10, ori_dist=np.pi / 4) prepare_data.show_minutiae(img0, minutiae) print n
def create(): # fetch weights weights_path = zoo.fetch_weights( 'https://github.com/facebookresearch/ImageNet-Adversarial-Training/releases/download/v0.1/R152-Denoise.npz', unzip=False) # model constructer expects an ArgumentParser as argument parser = argparse.ArgumentParser() parser.add_argument('-d', '--depth', help='ResNet depth', type=int, default=152, choices=[50, 101, 152]) parser.add_argument('--arch', help='Name of architectures defined in nets.py', default='ResNetDenoise') args = parser.parse_args([]) model = getattr(nets, args.arch + 'Model')(args) image = tf.placeholder(tf.float32, shape=(None, 3, 224, 224)) with TowerContext(tower_name='', is_training=False): logits = model.get_logits(image) config = tf.ConfigProto() config.gpu_options.allow_growth = True session = tf.Session(config=config) with session.as_default(): model = get_model_loader(weights_path).init(session) fmodel = TensorFlowModel(image, logits, channel_axis=1, bounds=[0, 255.], preprocessing=(127.5, 127.5)) return fmodel
def _setup(self): super(GANTrainer, self)._setup() with TowerContext(''): actual_inputs = self._get_input_tensors() self.model.build_graph(actual_inputs) # optimize G grads = self.config.optimizer.compute_gradients( self.model.g_loss, var_list=self.model.g_vars) grads = apply_grad_processors( grads, self.model.get_gradient_processor_g()) self.g_min = self.config.optimizer.apply_gradients(grads, name='g_op') # optimize D with tf.control_dependencies([self.g_min]): grads = self.config.optimizer.compute_gradients( self.model.d_loss, var_list=self.model.d_vars) grads = apply_grad_processors( grads, self.model.get_gradient_processor_d()) self.d_min = self.config.optimizer.apply_gradients(grads, name='d_op') self.train_op = self.d_min
def minutiae_extraction_latent(model_path, sample_path, imgs, output_name='reconstruction/gen:0', block=True): imgs = glob.glob('/Data/Latent/DB/NIST27/image/' + '*.bmp') minu_files = glob.glob('/Data/Latent/DB/ManualInformation/NIST27/ManMinu/*.txt') minu_files.sort() imgs.sort() import os if not os.path.isdir(sample_path): os.makedirs(sample_path) weight = get_weights(opt.SHAPE, opt.SHAPE, 12) with tf.Graph().as_default(): with TowerContext('', is_training=False): with tf.Session() as sess: is_training = get_current_tower_context().is_training load_model(model_path) images_placeholder = tf.get_default_graph().get_tensor_by_name('QueueInput/input_deque:0') # sub:0 minutiae_cylinder_placeholder = tf.get_default_graph().get_tensor_by_name(output_name) for k, file in enumerate(imgs): print file img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) u, texture = LP.FastCartoonTexture(img) img = texture / 128.0 - 1 h, w = img.shape x = [] y = [] nrof_samples = len(range(0, h, opt.SHAPE // 2)) * len(range(0, w, opt.SHAPE // 2)) patches = np.zeros((nrof_samples, opt.SHAPE, opt.SHAPE, 1)) n = 0 for i in range(0, h - opt.SHAPE + 1, opt.SHAPE // 2): for j in range(0, w - opt.SHAPE + 1, opt.SHAPE // 2): print j patch = img[i:i + opt.SHAPE, j:j + opt.SHAPE, np.newaxis] x.append(j) y.append(i) patches[n, :, :, :] = patch n = n + 1 # print x[-1] feed_dict = {images_placeholder: patches} minutiae_cylinder_array = sess.run(minutiae_cylinder_placeholder, feed_dict=feed_dict) minutiae_cylinder = np.zeros((h, w, 12)) minutiae_cylinder_array[:, -10:, :, :] = 0 minutiae_cylinder_array[:, :10, :, :] = 0 minutiae_cylinder_array[:, :, -10:, :] = 0 minutiae_cylinder_array[:, :, 10, :] = 0 for i in range(n): minutiae_cylinder[y[i]:y[i] + opt.SHAPE, x[i]:x[i] + opt.SHAPE, :] = minutiae_cylinder[y[i] :y[i] + opt.SHAPE, x[i]:x[i] + opt.SHAPE, :] + minutiae_cylinder_array[i] * weight minutiae = prepare_data.get_minutiae_from_cylinder(minutiae_cylinder, thr=0.05) minutiae = prepare_data.refine_minutiae(minutiae, dist_thr=10, ori_dist=np.pi / 4) minutiae_sets = [] minutiae_sets.append(minutiae) manu_minutiae = np.loadtxt(minu_files[k]) manu_minutiae[:, 2] = manu_minutiae[:, 2] / 180 * np.pi minutiae_sets.append(manu_minutiae) fname = sample_path + os.path.basename(file)[:-4] + '.jpeg' prepare_data.show_minutiae_sets(img, minutiae_sets, ROI=None, fname=fname, block=block) fname = sample_path + os.path.basename(file)[:-4] + '.txt' np.savetxt(fname, minutiae_sets[0]) print(n)
model = Model() if args.eval: batch = 128 # something that can run on one gpu ds = get_data('val', batch) eval_on_ILSVRC12(model, get_model_loader(args.load), ds) elif args.flops: # manually build the graph with batch=1 input_desc = [ InputDesc(tf.float32, [1, 224, 224, 3], 'input'), InputDesc(tf.int32, [1], 'label') ] input = PlaceholderInput() input.setup(input_desc) with TowerContext('', is_training=True): model.build_graph(*input.get_input_tensors()) tf.profiler.profile( tf.get_default_graph(), cmd='op', options=tf.profiler.ProfileOptionBuilder.float_operation()) else: logger.set_logger_dir(os.path.join('train_log', 'shufflenet')) nr_tower = max(get_nr_gpu(), 1) config = get_config(model, nr_tower) if args.load: config.session_init = get_model_loader(args.load) launch_train_with_config(config, SyncMultiGPUTrainerParameterServer(nr_tower))
def graph(x, target_class_input, true_label_input, i, x_max, x_min, grad): eps = 2.0 * FLAGS.max_epsilon / 255.0 alpha = eps / FLAGS.iterations momentum = FLAGS.momentum num_classes = 1001 x_div = input_diversity(x) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens3_adv_v3, end_points_ens3_adv_v3 = inception_v3.inception_v3( x_div, num_classes=num_classes, is_training=False, scope='Ens3AdvInceptionV3') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_ens4_adv_v3, end_points_ens4_adv_v3 = inception_v3.inception_v3( x_div, num_classes=num_classes, is_training=False, scope='Ens4AdvInceptionV3') with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_ensadv_res_v2, end_points_ensadv_res_v2 = inception_resnet_v2.inception_resnet_v2( x_div, num_classes=num_classes, is_training=False, scope='EnsAdvInceptionResnetV2') with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): logits_adv_res_v2, end_points_adv_res_v2 = inception_resnet_v2.inception_resnet_v2( x_div, num_classes=num_classes, is_training=False, scope='AdvInceptionResnetV2') with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_inc_v3, end_points_inc_v3 = inception_v3.inception_v3( x_div, num_classes=num_classes, is_training=False) with slim.arg_scope(inception_v3.inception_v3_arg_scope()): logits_adv_v3, end_points_adv_v3 = inception_v3.inception_v3( x_div, num_classes=num_classes, is_training=False, scope='AdvInceptionV3') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_res_v2, end_points_res_v2 = resnet_v2.resnet_v2_50( x_div, num_classes=num_classes, is_training=False, scope='resnet_v2_50') with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits_res_v2_101, end_points_res_v2_101 = resnet_v2.resnet_v2_101( x_div, num_classes=num_classes, is_training=False, scope='resnet_v2_101') with TowerContext(tower_name='eval', is_training=False): logits_advresnext_101 = resnext.ResNeXtDenoiseAllModel().get_logits(preprocess_for_model(x_div)) one_hot_target_class = tf.one_hot(target_class_input, num_classes) one_hot_true_label = tf.one_hot(true_label_input, num_classes) one_hot_target_class_resnext = tf.one_hot(target_class_input-1, num_classes-1) one_hot_true_label_resnext = tf.one_hot(true_label_input-1, num_classes-1) logits = (logits_inc_v3 + logits_ens3_adv_v3 + logits_ens4_adv_v3 + logits_ensadv_res_v2 + logits_adv_res_v2 +logits_adv_v3+logits_res_v2+logits_res_v2_101) / 8 auxlogits = (end_points_inc_v3['AuxLogits'] + end_points_ens3_adv_v3['AuxLogits'] + end_points_ens4_adv_v3['AuxLogits'] + end_points_adv_res_v2['AuxLogits'] + end_points_ensadv_res_v2['AuxLogits'] +end_points_adv_v3['AuxLogits']+logits_res_v2+logits_res_v2_101) / 8 logits_resnext = logits_advresnext_101 cross_entropy = -tf.losses.softmax_cross_entropy(one_hot_true_label, logits, label_smoothing=0.0, weights=1.0) cross_entropy -= tf.losses.softmax_cross_entropy(one_hot_true_label, auxlogits, label_smoothing=0.0, weights=0.4) cross_entropy2 = tf.losses.softmax_cross_entropy(one_hot_true_label_resnext, logits_resnext, label_smoothing=0.0, weights=1.0) '''cross_entropy -= tf.losses.softmax_cross_entropy(one_hot_true_label, logits_ensadv_res_v2, label_smoothing=0.0, weights=1.0) cross_entropy -= tf.losses.softmax_cross_entropy(one_hot_true_label, end_points_ensadv_res_v2['AuxLogits'], label_smoothing=0.0, weights=0.4)''' rnd = i%2 cross_entropy = tf.cond(tf.greater(rnd,0),lambda:cross_entropy,lambda:cross_entropy2) noise = tf.gradients(cross_entropy, x)[0] kernel = gkern(7, FLAGS.sig).astype(np.float32) #kernel = gkern(15, 4).astype(np.float32) stack_kernel = np.stack([kernel, kernel, kernel]).swapaxes(2, 0) stack_kernel = np.expand_dims(stack_kernel, 3) noise = tf.nn.depthwise_conv2d(noise, stack_kernel, strides=[1, 1, 1, 1], padding='SAME') noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) noise = momentum * grad + noise noise = noise / tf.reshape(tf.contrib.keras.backend.std(tf.reshape(noise, [FLAGS.batch_size, -1]), axis=1), [FLAGS.batch_size, 1, 1, 1]) x = x - alpha * tf.clip_by_value(tf.round(noise), -2, 2) x = tf.clip_by_value(x, x_min, x_max) i = tf.add(i, 1) return x, target_class_input, true_label_input, i, x_max, x_min, noise
parser = argparse.ArgumentParser() parser.add_argument('--config', help='config file') parser.add_argument('--meta', help='metagraph file') parser.add_argument(dest='model') parser.add_argument(dest='output') args = parser.parse_args() assert args.config or args.meta, "Either config or metagraph must be present!" with tf.Graph().as_default() as G: if args.config: logger.warn( "Using a config script is not reliable. Please use metagraph.") MODEL = imp.load_source('config_script', args.config).Model M = MODEL() with TowerContext('', is_training=False): input = PlaceholderInput() input.setup(M.get_inputs_desc()) M.build_graph(input) else: tf.train.import_meta_graph(args.meta) # loading... init = get_model_loader(args.model) sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) init.init(sess) # dump ... with sess.as_default():
def _add_forward_graph(self): """NN architecture.""" mc = self.mc if mc.LOAD_PRETRAINED_MODEL: assert tf.gfile.Exists(mc.PRETRAINED_MODEL_PATH), \ 'Cannot find pretrained model at the given path:' \ ' {}'.format(mc.PRETRAINED_MODEL_PATH) self.caffemodel_weight = joblib.load(mc.PRETRAINED_MODEL_PATH) def gaussian_noise_layer(input_layer, std): noise = tf.random_normal(shape=tf.shape(input_layer), mean=0.0, stddev=std, dtype=tf.float32) return input_layer + noise # @layer_register(log_shape=True) # def DepthConv(x, out_channel, kernel_shape, padding='SAME', stride=1, # W_init=None, nl=tf.identity): # in_shape = x.get_shape().as_list() # in_channel = in_shape[3] # in_shape[1] # assert out_channel % in_channel == 0 # channel_mult = out_channel // in_channel # # if W_init is None: # W_init = tf.contrib.layers.variance_scaling_initializer() # kernel_shape = [kernel_shape, kernel_shape] # filter_shape = kernel_shape + [in_channel, channel_mult] # # W = tf.get_variable('W', filter_shape, initializer=W_init) # conv = tf.nn.depthwise_conv2d(x, W, [1, stride, stride, 1], padding=padding, data_format='NHWC') # return nl(conv, name='output') # # @under_name_scope() # def channel_shuffle(l, group): # # l = tf.transpose(l, [0,3,1,2]) # in_shape = l.get_shape().as_list() # in_channel = in_shape[3] # in_shape[1] # l = tf.reshape(l, [-1, in_shape[1], in_shape[2], group, in_channel // group]) # l = tf.transpose(l, [0, 1, 2, 4, 3]) # l = tf.reshape(l, [-1, in_shape[1], in_shape[2], in_channel]) # # l = tf.transpose(l, [0, 2, 3, 1]) #NHWC # # return l # # def BN(x, name): # return BatchNorm('bn', x) # # def shufflenet_unit(l, out_channel, group, stride): # in_shape = l.get_shape().as_list() # in_channel = in_shape[3] # in_shape[1] # shortcut = l # # # We do not apply group convolution on the first pointwise layer # # because the number of input channels is relatively small. # first_split = group if in_channel != 16 else 1 # l = Conv2D('conv1', l, out_channel // 4, 1, split=first_split, nl=BNReLU) # l = channel_shuffle(l, group) # l = DepthConv('dconv', l, out_channel // 4, 3, nl=BN, stride=stride) # l = Conv2D('conv2', l, # out_channel if stride == 1 else out_channel - in_channel, # 1, split=group, nl=BN) # if stride == 1: # unit (b) # output = tf.nn.relu(shortcut + l) # else: # unit (c) # shortcut = AvgPooling('avgpool', shortcut, 3, 2, padding='SAME') # output = tf.concat([shortcut, tf.nn.relu(l)], axis=3) # axis=1) # return output # def group_conv(net, output, stride, group, relu=True, scope="GConv"): # assert 0 == output % group, "Output channels must be a multiple of groups" # num_channels_in_group = output // group # with tf.variable_scope(scope): # net = tf.split(net, group, axis=3, name="split") # for i in range(group): # net[i] = slim.conv2d(net[i], # num_channels_in_group, # [1, 1], # stride=stride, # activation_fn=tf.nn.relu if relu else None, # normalizer_fn=slim.batch_norm, # normalizer_params={'is_training': True}) # net = tf.concat(net, axis=3, name="concat") # # return net # # def channel_shuffle(net, output, group, scope="ChannelShuffle"): # assert 0 == output % group, "Output channels must be a multiple of groups" # num_channels_in_group = output // group # with tf.variable_scope(scope): # net = tf.split(net, output, axis=3, name="split") # chs = [] # for i in range(group): # for j in range(num_channels_in_group): # chs.append(net[i + j * group]) # net = tf.concat(chs, axis=3, name="concat") # # return net # # def shuffle_bottleneck(net, output, stride, group=1, scope="Unit"): # if 1 != stride: # _b, _h, _w, _c = net.get_shape().as_list() # output = output - _c # # assert 0 == output % group, "Output channels must be a multiple of groups" # # with tf.variable_scope(scope): # if 1 != stride: # net_skip = slim.avg_pool2d(net, [3, 3], stride, padding="SAME", scope='3x3AVGPool') # else: # net_skip = net # # net = group_conv(net, output, 1, group, relu=True, scope="1x1ConvIn") # # # net = channel_shuffle(net, output, group, scope="ChannelShuffle") # # with tf.variable_scope("3x3DWConv"): # depthwise_filter = tf.get_variable("depth_conv_w", # [3, 3, output, 1], # initializer=tf.truncated_normal_initializer()) # net = tf.nn.depthwise_conv2d(net, depthwise_filter, [1, stride, stride, 1], 'SAME', name="DWConv") # # net = group_conv(net, output, 1, group, relu=True, scope="1x1ConvOut") # # if 1 != stride: # net = tf.concat([net, net_skip], axis=3) # else: # net = net + net_skip # # return net # # def _depthwise_separable_conv(inputs, # num_pwc_filters, # width_multiplier, # sc, # downsample=False): # """ Helper function to build the depth-wise separable convolution layer. # """ # num_pwc_filters = round(num_pwc_filters * width_multiplier) # _stride = 2 if downsample else 1 # # # skip pointwise by setting num_outputs=None # depthwise_conv = slim.separable_convolution2d(inputs, # num_outputs=None, # stride=_stride, # depth_multiplier=1, # kernel_size=[3, 3], # scope=sc + '/depthwise_conv') # # bn = slim.batch_norm(depthwise_conv, scope=sc + '/dw_batch_norm') # pointwise_conv = slim.convolution2d(bn, # num_pwc_filters, # kernel_size=[1, 1], # scope=sc + '/pointwise_conv') # bn = slim.batch_norm(pointwise_conv, scope=sc + '/pw_batch_norm') # return bn # noise = gaussian_noise_layer(self.image_input, .5) def resnet_shortcut(l, n_out, stride, nl=tf.identity): #data_format = get_arg_scope()['Conv2D']['data_format'] n_in = l.get_shape().as_list()[3] if n_in != n_out: # change dimension when channel is not the same return Conv2D('convshortcut', l, n_out, 1, stride=stride, nl=nl) else: return l def get_bn(zero_init=False): """ Zero init gamma is good for resnet. See https://arxiv.org/abs/1706.02677. """ if zero_init: return lambda x, name: BatchNorm( 'bn', x, gamma_init=tf.zeros_initializer()) else: return lambda x, name: BatchNorm('bn', x) def se_resnet_bottleneck(l, ch_out, stride): shortcut = l l = Conv2D('conv1', l, ch_out, 1, nl=BNReLU) print(l.get_shape()) l = Conv2D('conv2', l, ch_out, 3, stride=stride, nl=BNReLU) print(l.get_shape()) l = Conv2D('conv3', l, ch_out * 4, 1, nl=get_bn(zero_init=True)) print(l.get_shape()) squeeze = GlobalAvgPooling('gap', l) squeeze = FullyConnected('fc1', squeeze, ch_out // 4, nl=tf.nn.relu) squeeze = FullyConnected('fc2', squeeze, ch_out * 4, nl=tf.nn.sigmoid) #data_format = get_arg_scope()['Conv2D']['data_format'] ch_ax = 3 shape = [-1, 1, 1, 1] shape[ch_ax] = ch_out * 4 l = l * tf.reshape(squeeze, shape) return l + resnet_shortcut( shortcut, ch_out * 4, stride, nl=get_bn(zero_init=False)) conv1 = self._conv_layer('conv1', self.image_input, filters=64, size=3, stride=2, padding='SAME', freeze=True) pool1 = self._pooling_layer('pool1', conv1, size=3, stride=2, padding='SAME') fire2 = self._fire_layer('fire2', pool1, s1x1=16, e1x1=64, e3x3=64, freeze=False) fire3 = self._fire_layer('fire3', fire2, s1x1=16, e1x1=64, e3x3=64, freeze=False) pool3 = self._pooling_layer('pool3', fire3, size=3, stride=2, padding='SAME') fire4 = self._fire_layer('fire4', pool3, s1x1=32, e1x1=128, e3x3=128, freeze=False) fire5 = self._fire_layer('fire5', fire4, s1x1=32, e1x1=128, e3x3=128, freeze=False) pool5 = self._pooling_layer('pool5', fire5, size=3, stride=2, padding='SAME') fire6 = self._fire_layer('fire6', pool5, s1x1=48, e1x1=192, e3x3=192, freeze=False) fire7 = self._fire_layer('fire7', fire6, s1x1=48, e1x1=192, e3x3=192, freeze=False) fire8 = self._fire_layer('fire8', fire7, s1x1=64, e1x1=256, e3x3=256, freeze=False) net = self._fire_layer('fire9', fire8, s1x1=64, e1x1=256, e3x3=256, freeze=False) # Two extra fire modules that are not trained before # fire10 = self._fire_layer( # 'fire10', fire9, s1x1=96, e1x1=384, e3x3=384, freeze=False) # fire11 = self._fire_layer( # 'fire11', fire10, s1x1=96, e1x1=384, e3x3=384, freeze=False) # dropout11 = tf.nn.dropout(fire11, self.keep_prob, name='drop11') # # print(dropout11.get_shape()) # with tf.variable_scope('ExtraNet') as sc: # end_points_collection = sc.name + '_end_points' # with slim.arg_scope([slim.convolution2d, slim.separable_convolution2d], # activation_fn=None, # outputs_collections=[end_points_collection]): # with slim.arg_scope([slim.batch_norm], # is_training=True, # activation_fn=tf.nn.relu): # width_multiplier = 1 # fire10 = self._fire_layer('fire10', fire9, s1x1=96, e1x1=384, e3x3=384, freeze=False) # net = _depthwise_separable_conv(fire10, 768, width_multiplier, sc='conv_ds_1') # fire11 = self._fire_layer('fire11', net, s1x1=96, e1x1=384, e3x3=384, freeze=False) # net = _depthwise_separable_conv(fire11, 768, width_multiplier, sc='conv_ds_2') # print(net.get_shape()) # fire10 = self._fire_layer('fire10', fire9, s1x1=96, e1x1=384, e3x3=384, freeze=False) # net = group_conv(fire10, 768, 1, 3, relu=True, scope="conv_g_1") # # fire11 = self._fire_layer('fire11', net, s1x1=96, e1x1=384, e3x3=384, freeze=False) # net = group_conv(net, 768, 1, 3, relu=True, scope="conv_g_2") # print(net.get_shape()) # net = self._fire_layer('fire10', fire9, s1x1=96, e1x1=384, e3x3=384, freeze=False) with TowerContext('', is_training=True): for i in range(2): with tf.variable_scope('block{}'.format(i)): net = se_resnet_bottleneck(net, 192, 1) print(net.get_shape()) # with argscope([Conv2D, MaxPooling, AvgPooling, GlobalAvgPooling, BatchNorm], data_format='NHWC'), \ # argscope(Conv2D, use_bias=False): # group = 3 # channels = [768] # # with TowerContext('', is_training=True): # # with varreplace.freeze_variables(): # with tf.variable_scope('group1'): # for i in range(2): # with tf.variable_scope('block{}'.format(i)): # net = shufflenet_unit(net, channels[0], group, 1) # net = shuffle_bottleneck(fire11, 768, 1, 3, scope='Unit-1') # net = shuffle_bottleneck(net, 768, 1, 3, scope='Unit-2') # print(net.get_shape()) net = tf.nn.dropout(net, self.keep_prob, name='drop11') num_output = mc.ANCHOR_PER_GRID * (mc.CLASSES + 1 + 4 + mc.POINTS + 2 + 2) self.preds = self._conv_layer('conv12', net, filters=num_output, size=3, stride=1, padding='SAME', xavier=False, relu=False, stddev=0.0001) # variables_to_restore = slim.get_variables_to_restore() # print(variables_to_restore) modelparams1 = self.model_params for varres in tf.trainable_variables(): #variables_to_restore: if varres not in modelparams1: if 'iou' not in varres.name: self.model_params += [varres] print(self.model_params) total_parameters = 0 #iterating over all variables for variable in tf.trainable_variables(): local_parameters = 1 shape = variable.get_shape() #getting shape of a variable for i in shape: local_parameters *= i.value #mutiplying dimension values print(variable.name, local_parameters) total_parameters += local_parameters print(total_parameters)