def testModelVariables(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) vgg.vgg_16(inputs, num_classes) expected_names = [ 'vgg_16/conv1/conv1_1/weights', 'vgg_16/conv1/conv1_1/biases', 'vgg_16/conv1/conv1_2/weights', 'vgg_16/conv1/conv1_2/biases', 'vgg_16/conv2/conv2_1/weights', 'vgg_16/conv2/conv2_1/biases', 'vgg_16/conv2/conv2_2/weights', 'vgg_16/conv2/conv2_2/biases', 'vgg_16/conv3/conv3_1/weights', 'vgg_16/conv3/conv3_1/biases', 'vgg_16/conv3/conv3_2/weights', 'vgg_16/conv3/conv3_2/biases', 'vgg_16/conv3/conv3_3/weights', 'vgg_16/conv3/conv3_3/biases', 'vgg_16/conv4/conv4_1/weights', 'vgg_16/conv4/conv4_1/biases', 'vgg_16/conv4/conv4_2/weights', 'vgg_16/conv4/conv4_2/biases', 'vgg_16/conv4/conv4_3/weights', 'vgg_16/conv4/conv4_3/biases', 'vgg_16/conv5/conv5_1/weights', 'vgg_16/conv5/conv5_1/biases', 'vgg_16/conv5/conv5_2/weights', 'vgg_16/conv5/conv5_2/biases', 'vgg_16/conv5/conv5_3/weights', 'vgg_16/conv5/conv5_3/biases', 'vgg_16/fc6/weights', 'vgg_16/fc6/biases', 'vgg_16/fc7/weights', 'vgg_16/fc7/biases', 'vgg_16/fc8/weights', 'vgg_16/fc8/biases', ] model_variables = [v.op.name for v in slim.get_model_variables()] self.assertSetEqual(set(model_variables), set(expected_names))
def testFullyConvolutional(self): batch_size = 1 height, width = 256, 256 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(inputs, num_classes, spatial_squeeze=False) self.assertEquals(logits.op.name, 'vgg_16/fc8/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, 2, 2, num_classes])
def total_loss(inputs, stylized_inputs, style_gram_matrices, content_weights, style_weights, reuse=False): """Computes the total loss function. The total loss function is composed of a content, a style and a total variation term. Args: inputs: Tensor. The input images. stylized_inputs: Tensor. The stylized input images. style_gram_matrices: dict mapping layer names to their corresponding Gram matrices. content_weights: dict mapping layer names to their associated content loss weight. Keys that are missing from the dict won't have their content loss computed. style_weights: dict mapping layer names to their associated style loss weight. Keys that are missing from the dict won't have their style loss computed. reuse: bool. Whether to reuse model parameters. Defaults to False. Returns: Tensor for the total loss, dict mapping loss names to losses. """ # Propagate the input and its stylized version through VGG16. end_points = vgg.vgg_16(inputs, reuse=reuse) stylized_end_points = vgg.vgg_16(stylized_inputs, reuse=True) # Compute the content loss total_content_loss, content_loss_dict = content_loss( end_points, stylized_end_points, content_weights) # Compute the style loss total_style_loss, style_loss_dict = style_loss( style_gram_matrices, stylized_end_points, style_weights) # Compute the total loss loss = total_content_loss + total_style_loss loss_dict = {'total_loss': loss} loss_dict.update(content_loss_dict) loss_dict.update(style_loss_dict) return loss, loss_dict
def tower_loss(data_tensor, label_tensor, num_classes, train_mode): #vgg = tf.contrib.slim.nets.vgg with slim.arg_scope(vgg.vgg_arg_scope(weight_decay=args.weight_decay)): logits, endpoints_dict = vgg.vgg_16(data_tensor, num_classes=num_classes, is_training=train_mode,dropout_keep_prob=args.dropout_keep_prob) loss=tf.losses.sparse_softmax_cross_entropy(labels=label_tensor, logits=logits) return loss, logits
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(inputs, num_classes) self.assertEquals(logits.op.name, 'vgg_16/fc8/squeezed') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = vgg.vgg_16(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(logits, 1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
def CRAFT_net(inputs, is_trianing=True, reuse=None, weight_decay=0.9): with slim.arg_scope(vgg_arg_scope()): vgg_res, end_points = vgg_16(inputs) with tf.variable_scope('vgg_16', [end_points.values]): batch_norm_params = { 'decay': 0.997, 'epsilon': 1e-5, 'scale': True, 'is_training': is_trianing } with slim.arg_scope( [slim.conv2d], activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params, weights_regularizer=slim.l2_regularizer(weight_decay)): f = [ end_points['vgg_16/conv2/conv2_2'], end_points['vgg_16/conv3/conv3_3'], end_points['vgg_16/conv4/conv4_3'], end_points['vgg_16/conv5/conv5_3'] ] net = f[3] # VGG end net = slim.max_pool2d(net, [3, 3], stride=1, padding='SAME', scope='pool5') # w/16 512 net = arous_conv(net, 3, 3, 1024, 6, name='arous_conv') # w/16 1024 net = slim.conv2d(net, 1024, [1, 1], padding='SAME', scope='conv6') # w/16 1024 # U-net start net = tf.concat([net, f[3]], axis=3) # w/16 1024 + 512 net = upconvBlock(net, 512, 256) # w/16 256 net = upsample(net, (64, 64)) net = tf.concat([net, f[2]], axis=3) # w/8 256 + 512 net = upconvBlock(net, 256, 128) # w/8 128 net = upsample(net, (128, 128)) net = tf.concat([net, f[1]], axis=3) # w/4 128 + 256 net = upconvBlock(net, 128, 64) # w/4 64 net = upsample(net, (256, 256)) net = tf.concat([net, f[0]], axis=3) # w/2 64 + 128 net = upconvBlock(net, 64, 32) # w/2 32 # U-net end net = slim.repeat(net, 2, slim.conv2d, 32, [3, 3]) # w/2 32 net = slim.conv2d(net, 16, [3, 3], padding='SAME') # w/2 16 net = slim.conv2d(net, 16, [1, 1], padding='SAME') # w/2 16 net = slim.conv2d(net, 2, [1, 1], padding='SAME') # w/2 2 return net, end_points
def build_model(self): # get content_img, style_img and define gen_img if content_input is not None: self.content_path = content_input if style_input is not None: self.style_path = style_input content_img, content_shape = utils.load_content_img(self.content_path) style_img = utils.load_style_img(self.style_path, content_shape) content_img_shape = content_img.shape gen_img = tf.Variable(tf.random_normal(content_img_shape) * 0.256) with slim.arg_scope(vgg.vgg_arg_scope()): f1, f2, f3, f4, exclude = vgg.vgg_16( tf.concat([gen_img, content_img, style_img], axis=0)) # calculate content_loss and style_loss content_loss = utils.cal_content_loss(f3) style_loss = utils.cal_style_loss(f1, f2, f3, f4) # load vgg model vgg_model_path = VGG_MODEL_PATH vgg_vars = slim.get_variables_to_restore(include=['vgg_16'], exclude=exclude) init_fn = slim.assign_from_checkpoint_fn(vgg_model_path, vgg_vars) init_fn(self.sess) print('vgg_16 weights load done') self.gen_img = gen_img self.global_step = tf.Variable(0, name='global_step', trainable=False) self.content_loss = content_loss self.style_loss = style_loss * W_STYLE # the total loss self.loss = self.content_loss + self.style_loss # starter_learning_rate = 1e1 # global_step = tf.train.get_global_step() # learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, decay_steps=500, # decay_rate=0.98, # staircase=True) self.opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize( self.loss, global_step=self.global_step, var_list=gen_img) all_var = tf.global_variables() init_var = [v for v in all_var if 'vgg_16' not in v.name] init = tf.variables_initializer(var_list=init_var) self.sess.run(init) self.save = tf.train.Saver()
def vgg_16(inputs, no_fc=False): with slim.arg_scope(vgg.vgg_arg_scope()): net, end_points = vgg.vgg_16(inputs, None, is_training=False, spatial_squeeze=False, fc_conv_padding='SAME', no_fc=no_fc) if no_fc: return end_points['vgg_16/pool5'], end_points[ 'vgg_16/pool4'], end_points['vgg_16/pool3'] else: return net, end_points['vgg_16/pool4'], end_points['vgg_16/pool3']
def _fc7_flat(eval_dataset): inputs = eval_dataset.get_next("inputs") logits, end_points = vgg.vgg_16( inputs, num_classes=1000, is_training=False, # this time, we don't train the network. dropout_keep_prob=0.5, # doesn't matter since "is_training=False." spatial_squeeze=True, # squeeze spatial dimensions in the final output scope='vgg_16', fc_conv_padding= 'VALID', # this code uses conv instead of fc (they are equivalent!) global_pool=False) fc7_features = end_points['vgg_16/fc7'] return tf.layers.flatten(fc7_features, name="fc7_flat")
def vgg_encoding(self, processed_images, is_training, reuse=False): with slim.arg_scope(vgg.vgg_arg_scope()): fc7 = vgg.vgg_16(processed_images, num_classes=self.no_classes, is_training=is_training, spatial_squeeze=False, fc_conv_padding='VALID', reuse=reuse, return_fc7=True, fc7_size=self.fc7_size) return fc7
def arch_vgg16(X, num_classes, dropout_keep_prob=0.8, is_train=False): arg_scope = vgg_arg_scope() with slim.arg_scope(arg_scope): net, end_points = vgg_16(X, is_training=is_train) with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'): with tf.variable_scope('Logits_out'): net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='fc8') net = tf.squeeze(net, [1, 2], name='fc8/squeezed') return net
def testTrainEvalWithReuse(self): train_batch_size = 2 eval_batch_size = 1 train_height, train_width = 224, 224 eval_height, eval_width = 256, 256 num_classes = 1000 with self.test_session(): train_inputs = tf.random_uniform( (train_batch_size, train_height, train_width, 3)) logits, _ = vgg.vgg_16(train_inputs) self.assertListEqual(logits.get_shape().as_list(), [train_batch_size, num_classes]) tf.get_variable_scope().reuse_variables() eval_inputs = tf.random_uniform( (eval_batch_size, eval_height, eval_width, 3)) logits, _ = vgg.vgg_16(eval_inputs, is_training=False, spatial_squeeze=False) self.assertListEqual(logits.get_shape().as_list(), [eval_batch_size, 2, 2, num_classes]) logits = tf.reduce_mean(logits, [1, 2]) predictions = tf.argmax(logits, 1) self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def net_feats(ims, num_classes=None, is_training=True, reuse=False, scope=''): if net_type == 'resnet': feats = resnet_v1.resnet_v1_50(ims, num_classes=num_classes, reuse=reuse, is_training=is_training, scope='%s_resnet_v1_50' % scope)[0] feats = slim.flatten(feats) elif net_type == 'vgg': feats = vgg.vgg_16(ims, num_classes=None, scope='%s_vgg_16' % scope, reuse=reuse, is_training=is_training)[0] print 'CNN feature shape:', shape(feats) return feats
def build_model(self): # get content_img, style_img and define gen_img if content_input is not None: self.content_path = content_input if style_input is not None: self.style_path = style_input content_img, content_shape = utils.load_content_img(self.content_path) style_img = utils.load_style_img(self.style_path, content_shape) content_img_shape = content_img.shape gen_img = tf.Variable(tf.random_normal(content_img_shape) * 0.256) with slim.arg_scope(vgg.vgg_arg_scope()): f1, f2, f3, f4, exclude = vgg.vgg_16(tf.concat([gen_img, content_img, style_img], axis=0)) # calculate content_loss and style_loss content_loss = utils.cal_content_loss(f3) style_loss = utils.cal_style_loss(f1, f2, f3, f4) # load vgg model vgg_model_path = VGG_MODEL_PATH vgg_vars = slim.get_variables_to_restore(include=['vgg_16'], exclude=exclude) init_fn = slim.assign_from_checkpoint_fn(vgg_model_path, vgg_vars) init_fn(self.sess) print('vgg_16 weights load done') self.gen_img = gen_img self.global_step = tf.Variable(0, name='global_step', trainable=False) self.content_loss = content_loss self.style_loss = style_loss * W_STYLE # the total loss self.loss = self.content_loss + self.style_loss # starter_learning_rate = 1e1 # global_step = tf.train.get_global_step() # learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step, decay_steps=500, # decay_rate=0.98, # staircase=True) self.opt = tf.train.AdamOptimizer(LEARNING_RATE).minimize(self.loss, global_step=self.global_step, var_list=gen_img) all_var = tf.global_variables() init_var = [v for v in all_var if 'vgg_16' not in v.name] init = tf.variables_initializer(var_list=init_var) self.sess.run(init) self.save = tf.train.Saver()
def precompute_gram_matrices(image, final_endpoint='fc8'): """Pre-computes the Gram matrices on a given image. Args: image: 4-D tensor. Input (batch of) image(s). final_endpoint: str, name of the final layer to compute Gram matrices for. Defaults to 'fc8'. Returns: dict mapping layer names to their corresponding Gram matrices. """ with tf.Session() as session: end_points = vgg.vgg_16(image, final_endpoint=final_endpoint) tf.train.Saver(slim.get_variables('vgg_16')).restore( session, vgg.checkpoint_file()) return dict((key, gram_matrix(value).eval()) for key, value in end_points.items())
def _buildGraph(self): x_in = tf.placeholder( tf.float32, shape=[ None, # enables variable batch size self.input_dim[0] ], name="x") x_in_reshape = tf.reshape( x_in, [-1, self.input_dim[1], self.input_dim[2], 3]) dropout = tf.placeholder_with_default(1., shape=[], name="dropout") y_in = tf.placeholder(dtype=tf.int8, name="y") onehot_labels = tf.one_hot(indices=tf.cast(y_in, tf.int32), depth=2) is_train = tf.placeholder_with_default(True, shape=[], name="is_train") logits, nett, ww = vgg.vgg_16(x_in_reshape, num_classes=2, is_training=is_train, dropout_keep_prob=dropout, spatial_squeeze=True, scope='vgg16') pred = tf.nn.softmax(logits, name="prediction") global_step = tf.Variable(0, trainable=False) pred_cost = tf.losses.softmax_cross_entropy( onehot_labels=onehot_labels, logits=logits) tf.summary.scalar("InceptionV3_cost", pred_cost) train_op = tf.contrib.layers.optimize_loss( loss=pred_cost, learning_rate=self.learning_rate, global_step=global_step, optimizer="Adam") merged_summary = tf.summary.merge_all() return (x_in, dropout, is_train, y_in, logits, nett, ww, pred, pred_cost, global_step, train_op, merged_summary)
def test_with_origin(): image=cv2.imread('./imgdata/cat.18.jpg') print(image.shape) res=cv2.resize(image,(224,224)) res_image=np.expand_dims(res,0) print(res_image.shape,type(res_image)) graph = tf.Graph inputs = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3], name='inputs') net, end_points = vgg.vgg_16(inputs, num_classes=1000) print(end_points) saver=tf.train.Saver() with tf.Session() as sess: saver.restore(sess,'./models/vgg_16.ckpt') input=sess.graph.get_tensor_by_name('inputs:0') output=sess.graph.get_tensor_by_name('vgg_16/fc8/squeezed:0') pred=sess.run(output,feed_dict={input:res_image}) print(np.argmax(pred,1))
def testEndPoints(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = vgg.vgg_16(inputs, num_classes) expected_names = [ 'vgg_16/conv1/conv1_1', 'vgg_16/conv1/conv1_2', 'vgg_16/pool1', 'vgg_16/conv2/conv2_1', 'vgg_16/conv2/conv2_2', 'vgg_16/pool2', 'vgg_16/conv3/conv3_1', 'vgg_16/conv3/conv3_2', 'vgg_16/conv3/conv3_3', 'vgg_16/pool3', 'vgg_16/conv4/conv4_1', 'vgg_16/conv4/conv4_2', 'vgg_16/conv4/conv4_3', 'vgg_16/pool4', 'vgg_16/conv5/conv5_1', 'vgg_16/conv5/conv5_2', 'vgg_16/conv5/conv5_3', 'vgg_16/pool5', 'vgg_16/fc6', 'vgg_16/fc7', 'vgg_16/fc8' ] self.assertSetEqual(set(end_points.keys()), set(expected_names))
def vgg16_fcn_net(image_tensor, number_of_classes, is_training=True, upsample_factor=8): # tf.reset_default_graph() # Define the model that we want to use -- specify to use only two classes at the last layer with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16(image_tensor, num_classes=number_of_classes, is_training=is_training, spatial_squeeze=False, fc_conv_padding='SAME') downsampled_logits_shape = tf.shape(logits) img_shape = tf.shape(image_tensor) # Calculate the ouput size of the upsampled tensor # The shape should be batch_size X width X height X num_classes upsampled_logits_shape = tf.stack([ downsampled_logits_shape[0], img_shape[1], img_shape[2], downsampled_logits_shape[3] ]) # Perform the upsampling x2 upsampled_logits = upsample(logits, 'vgg_16/fc8/t_conv_x2', 2, end_points['vgg_16/pool4'], 'conv_pool4', number_of_classes) # Perform the upsampling x2 upsampled_logits = upsample(upsampled_logits, 'vgg_16/fc8/t_conv_x2_x2', 2, end_points['vgg_16/pool3'], 'conv_pool3', number_of_classes) # Perform the upsampling x8 upsample_filter_tensor_x8 = bilinear_upsample_weights( upsample_factor, number_of_classes, 'vgg_16/fc8/t_conv_x8') upsampled_logits = tf.nn.conv2d_transpose( upsampled_logits, upsample_filter_tensor_x8, output_shape=upsampled_logits_shape, strides=[1, upsample_factor, upsample_factor, 1], padding='SAME') return upsampled_logits
def __init__(self, learning_rate, num_classes=1000, is_training=True): self.global_step = tf.Variable(0, trainable=False, name='global_step') self.batch_imgs = tf.placeholder(tf.float32, (None, None, None, 3)) self.batch_lbls = tf.placeholder(tf.int32, (None, 1)) _, end_points = vgg_16(self.batch_imgs, num_classes, is_training) self.loss = tf.losses.sparse_softmax_cross_entropy( labels=self.batch_lbls, logits=end_points['vgg_16/fc8']) gradients = tf.gradients(self.loss, tf.trainable_variables()) self.update = tf.train.GradientDescentOptimizer( learning_rate).apply_gradients(zip(gradients, tf.trainable_variables()), global_step=self.global_step) self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=99999999)
def vgg16_fcn8_model(images, num_classes, is_training=False, raw_image_shape=(520 - 170, 800), decoder='fcn8'): train_image_shape = (224 * 2, 224 * 3) if decoder == 'fcn8': decoder_fn = mobilenet_v1_fcn_decoder elif decoder == 'fcn8_upsample': decoder_fn = mobilenet_v1_fcn8_upsample_decoder else: raise ValueError("the decoder should be either fcn8 or fcn8_upsample") if images.dtype != tf.uint8: raise ValueError("the image should be uint8") images = tf.image.resize_images(images, size=train_image_shape) tf.summary.image('input_image_after_rescale_and_resize', tf.expand_dims(images[0], 0)) processed_images = tf.map_fn(vgg_preprocessing.vgg_image_rescale, images, dtype=tf.float32) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(vgg.vgg_arg_scope()): # 1000 classes instead of 1001. logits, end_points = vgg.vgg_16(processed_images, num_classes=1000, is_training=is_training, spatial_squeeze=False) layer4 = end_points['vgg_16/pool3'] layer6 = end_points['vgg_16/pool4'] layer13 = end_points['vgg_16/pool5'] last_layer = decoder_fn(layer13, layer4, layer6, num_classes) last_layer = post_process_logits(end_points, last_layer, raw_image_shape, train_image_shape) return last_layer, end_points
def finetune(): image=cv2.imread('./imgdata/cat.18.jpg') print(image.shape) res_image=cv2.resize(image,(224,224),interpolation=cv2.INTER_CUBIC) print(res_image.shape) res_image=np.expand_dims(res_image,axis=0) print(res_image.shape) labels=[[1,0]] graph=tf.get_default_graph() input=tf.placeholder(dtype=tf.float32,shape=[None,224,224,3],name='inputs') y_=tf.placeholder(dtype=tf.float32,shape=[None,2],name='labels') net,end_points=vgg.vgg_16(input,num_classes=2) print(net,end_points) y=tf.nn.softmax(net) cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1])) output_vars=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,scope='vgg_16/fc8') print(output_vars) train_op=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy,var_list=output_vars) var=tf.global_variables() print(var) var_to_restore=[val for val in var if 'fc8' not in val.name] print(var_to_restore) saver=tf.train.Saver(var_to_restore) with tf.Session() as sess: saver.restore(sess,'./models/vgg_16.ckpt') var_to_init=[val for val in var if 'fc8' in val.name] sess.run(tf.variables_initializer(var_to_init)) w1=sess.graph.get_tensor_by_name('vgg_16/conv1/conv1_1/weights:0') print(sess.run(w1,feed_dict={input:res_image})) w8=sess.graph.get_tensor_by_name('vgg_16/fc8/weights:0') print('w8',sess.run(w8,feed_dict={input:res_image})) sess.run(train_op,feed_dict={input:res_image,y_:labels})
def build_network(self): """Builds the image model subgraph and generates image feature maps. """ # dimension convention: depth x height x width x channels inputs = self.images # with tf.variable_scope('model', [inputs]) as sc: # if self.mode == "validation": # sc.reuse_variables() net, end_points = vgg_16(inputs, num_classes=self.num_classes, is_training=self.is_training()) # net = imagenet_resnet_v2(50, self.num_classes)(inputs, self.is_training()) print("self.num_classes") print(self.num_classes) print('complete network build.') return net
def look_model_params(): graph=tf.Graph inputs=tf.placeholder(dtype=tf.float32,shape=[None,224,224,3],name='inputs') net,end_points=vgg.vgg_16(inputs,num_classes=1000) saver=tf.train.Saver() with tf.Session() as sess: saver.restore(sess,'models/vgg_16.ckpt') tvs=[v for v in tf.trainable_variables()] print('获得所有可训练变量的权重:') for v in tvs: print(v.name) print(sess.run(v)) gv=[v for v in tf.global_variables()] print('获得所有变量:') for v in gv: print(v.name,'\n') ops=[o for o in sess.graph.get_operations()] print('获得所有operations相关的tensor:') for o in ops: print(o.name,'\n')
def build_model(self): train_imgs = tools.load_train_img(TRAIN_DATA_DIR, self.batch_size, self.img_size) style_imgs = tools.load_style_img(STYLE_IMAGE_PATH) with slim.arg_scope(model.arg_scope()): gen_img, variables = model.inference(train_imgs, reuse=False, name='transform') with slim.arg_scope(vgg.vgg_arg_scope()): gen_img_processed = [tf.image.per_image_standardization(image) for image in tf.unstack(gen_img, axis=0, num=self.batch_size)] f1, f2, f3, f4, exclude = vgg.vgg_16(tf.concat([gen_img_processed, train_imgs, style_imgs], axis=0)) gen_f, img_f, _ = tf.split(f4, 3, 0) content_loss = tf.nn.l2_loss(gen_f - img_f) / tf.to_float(tf.size(gen_f)) style_loss = model.styleloss(f1, f2, f3, f4) vgg_model_path = VGG_MODEL_PATH vgg_vars = slim.get_variables_to_restore(include=['vgg_16'], exclude=exclude) init_fn = slim.assign_from_checkpoint_fn(vgg_model_path, vgg_vars) init_fn(self.sess) print("vgg's weights load done") self.gen_img = gen_img self.global_step = tf.Variable(0, name="global_step", trainable=False) self.content_loss = content_loss self.style_loss = style_loss * self.style_w self.loss = self.content_loss + self.style_loss self.learn_rate = tf.train.exponential_decay(self.learn_rate_base, self.global_step, 1, self.learn_rate_decay, staircase=True) self.opt = tf.train.AdamOptimizer(self.learn_rate).minimize(self.loss, global_step=self.global_step, var_list=variables) all_var = tf.global_variables() init_var = [v for v in all_var if 'vgg_16' not in v.name] init = tf.variables_initializer(var_list=init_var) self.sess.run(init) self.save = tf.train.Saver(var_list=variables)
log_folder = os.path.join(FLAGS.output_dir, 'train') vgg_checkpoint_path = FLAGS.checkpoint_path # Creates a variable to hold the global_step. global_step = tf.Variable(0, trainable=False, name='global_step', dtype=tf.int64) # Define the model that we want to use -- specify to use only two classes at the last layer with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16(image_tensor, num_classes=number_of_classes, is_training=is_training_placeholder, spatial_squeeze=False, fc_conv_padding='SAME') downsampled_logits_shape = tf.shape(logits) img_shape = tf.shape(image_tensor) # Calculate the ouput size of the upsampled tensor # The shape should be batch_size X width X height X num_classes upsampled_logits_shape = tf.stack([ downsampled_logits_shape[0], img_shape[1], img_shape[2], downsampled_logits_shape[3] ]) #取出pool4的结果
def init_model(self): slim = tf.contrib.slim # 初始化 tf.reset_default_graph() # 输入图片 image_tensor = tf.placeholder(tf.float32, shape=(1, None, None, 3), name='image_tensor') orig_img_tensor = tf.placeholder(tf.uint8, shape=(1, None, None, 3), name='orig_img_tensor') # 初始化模型 with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16(image_tensor, num_classes=self.number_of_classes, is_training=False, spatial_squeeze=False, fc_conv_padding='SAME') downsampled_logits_shape = tf.shape(logits) img_shape = tf.shape(image_tensor) # Calculate the ouput size of the upsampled tensor # The shape should be batch_size X width X height X num_classes upsampled_logits_shape = tf.stack([ downsampled_logits_shape[0], img_shape[1], img_shape[2], downsampled_logits_shape[3] ]) pool4_feature = end_points['vgg_16/pool4'] with tf.variable_scope('vgg_16/fc8'): aux_logits_16s = slim.conv2d( pool4_feature, self.number_of_classes, [1, 1], activation_fn=None, weights_initializer=tf.zeros_initializer, scope='conv_pool4') # Perform the upsampling upsample_filter_np_x2 = bilinear_upsample_weights( 2, # upsample_factor, self.number_of_classes) upsample_filter_tensor_x2 = tf.Variable(upsample_filter_np_x2, name='vgg_16/fc8/t_conv_x2') upsampled_logits = tf.nn.conv2d_transpose( logits, upsample_filter_tensor_x2, output_shape=tf.shape(aux_logits_16s), strides=[1, 2, 2, 1], padding='SAME') upsampled_logits = upsampled_logits + aux_logits_16s upsample_filter_np_x16 = bilinear_upsample_weights( self.upsample_factor, self.number_of_classes) upsample_filter_tensor_x16 = tf.Variable(upsample_filter_np_x16, name='vgg_16/fc8/t_conv_x16') upsampled_logits = tf.nn.conv2d_transpose( upsampled_logits, upsample_filter_tensor_x16, output_shape=upsampled_logits_shape, strides=[1, self.upsample_factor, self.upsample_factor, 1], padding='SAME') # Tensor to get the final prediction for each pixel -- pay # attention that we don't need softmax in this case because # we only need the final decision. If we also need the respective # probabilities we will have to apply softmax. pred = tf.argmax(upsampled_logits, axis=3, name='predictions') probabilities = tf.nn.softmax(upsampled_logits, name='probabilities') # 恢复模型,从训练好的模型中恢复参数 checkpoint_path = tf.train.latest_checkpoint(self.ckpt) assert checkpoint_path, "no checkpoint exist, cant perform predict." variables_to_restore = slim.get_model_variables() sess_config = tf.ConfigProto() sess_config.gpu_options.allow_growth = True sess = tf.Session(config=sess_config) init_op = tf.global_variables_initializer() init_local_op = tf.local_variables_initializer() saver = tf.train.Saver(max_to_keep=1) # Run the initializers. sess.run(init_op) sess.run(init_local_op) saver.restore(sess, checkpoint_path) logging.debug('checkpoint restored from [{0}]'.format(checkpoint_path)) return sess, pred, orig_img_tensor, probabilities
log_folder = os.path.join(FLAGS.output_dir, 'train') vgg_checkpoint_path = FLAGS.checkpoint_path # Creates a variable to hold the global_step. global_step = tf.Variable(0, trainable=False, name='global_step', dtype=tf.int64) # Define the model that we want to use -- specify to use only two classes at the last layer with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16( image_tensor, num_classes=number_of_classes, is_training=is_training_placeholder, spatial_squeeze=False, #包含空间信息 所以不做squeezn fc_conv_padding='SAME' ) #wangluo suojian wei 1/32 224*224 --- 7*7*num_classes downsampled_logits_shape = tf.shape(logits) img_shape = tf.shape(image_tensor) # Calculate the ouput size of the upsampled tensor # The shape should be batch_size X width X height X num_classes ###dui logits jinxing shangcaiyang upsampled_logits_shape = tf.stack([ downsampled_logits_shape[0], # img_shape[1], img_shape[2],
raise ValueError('Only have dataset: hand, standard, casia') if not args.one_shot: x_train = np.concatenate((x_train, x_test), axis=1) t_train = np.concatenate((t_train, t_test), axis=1) x_train = x_train.reshape([-1, n_xl, n_xl, n_channels]).astype(np.float32) x_test = x_test.reshape([-1, n_xl, n_xl, n_channels]).astype(np.float32) t_train = t_train.reshape([-1, n_y]) t_test = t_test.reshape([-1, n_y]) #is_training = tf.placeholder(tf.bool, shape=[]) x = tf.placeholder(tf.float32, shape=[None, n_xl, n_xl, n_channels]) y = tf.placeholder(tf.float32, shape=[None, n_y]) optimizer = tf.train.AdamOptimizer(lr, beta1=0.5) net = vgg.vgg_16(x, n_y) y_ = net.outputs net.print_paras() net.print_layers() loss = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_) #infer = optimizer.minimize(loss) grads = optimizer.compute_gradients(loss) infer = optimizer.apply_gradients(grads) saver = tf.train.Saver(max_to_keep=10) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) ckpt_file = tf.train.latest_checkpoint(result_path) begin_epoch = 1 if ckpt_file is not None:
#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf import vgg inputs = tf.placeholder(tf.float32, (None, 224, 224, 3), name='inputs') r, g, b = tf.split(axis=3, num_or_size_splits=3, value=inputs * 255.0) VGG_MEAN = [103.939, 116.779, 123.68] bgr = tf.concat(values=[b - VGG_MEAN[0], g - VGG_MEAN[1], r - VGG_MEAN[2]], axis=3) with tf.contrib.slim.arg_scope(vgg.vgg_arg_scope()): fc8, endpoints = vgg.vgg_16(bgr, is_training=False) for k, v in endpoints.iteritems(): print(k, v)
upsample_factor = 16 number_of_classes = 21 log_folder = os.path.join(FLAGS.output_dir, 'train') vgg_checkpoint_path = FLAGS.checkpoint_path # Creates a variable to hold the global_step. global_step = tf.Variable(0, trainable=False, name='global_step', dtype=tf.int64) # Define the model that we want to use -- specify to use only two classes at the last layer with slim.arg_scope(vgg.vgg_arg_scope()): logits, end_points = vgg.vgg_16(image_tensor, num_classes=number_of_classes, is_training=is_training_placeholder, spatial_squeeze=False, fc_conv_padding='SAME') downsampled_logits_shape = tf.shape(logits) img_shape = tf.shape(image_tensor) # Calculate the ouput size of the upsampled tensor # The shape should be batch_size X width X height X num_classes upsampled_logits_shape = tf.stack([ downsampled_logits_shape[0], img_shape[1], img_shape[2], downsampled_logits_shape[3] ])