示例#1
0
def create_context_path(input_im):

    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        last_layer, end_points = resnet_v2.resnet_v2_101(input_im,
                                                         is_training=True,
                                                         scope='resnet_v2_101',
                                                         global_pool=False)
        frontend_scope = 'resnet_v2_101'
        init_fn = slim.assign_from_checkpoint_fn(
            model_path=os.path.join('models', 'resnet_v2_101.ckpt'),
            var_list=slim.get_model_variables('resnet_v2_101'),
            ignore_missing_vars=True)

        layer_reduced16 = end_points[frontend_scope + '/block2']
        layer_reduced32 = last_layer
        layer_arm16 = arm_module(layer_reduced16, n_filter_maps=512)
        layer_arm32 = arm_module(layer_reduced32, n_filter_maps=2048)
        layer_global_context = tf.reduce_mean(last_layer,
                                              axis=[1, 2],
                                              keepdims=True,
                                              name='global_context')

        ## Combining Context Features
        layer_context1 = tf.math.multiply(layer_arm32, layer_global_context)
        layer_context1 = layers.UpSampling2D(
            size=4, interpolation='bilinear')(layer_context1)
        layer_context2 = layers.UpSampling2D(
            size=2, interpolation='bilinear')(layer_arm16)
        context_output = tf.concat([layer_context1, layer_context2], axis=-1)

        return context_output, init_fn
示例#2
0
def Deeplab_v2(inputs , num_classes, is_training):
	'''A TensorFlow implementation of Deeplab_v2 model based on 
	   http://liangchiehchen.com/projects/DeepLabv2_resnet.html		
	
	Args:
		inputs: A 4-D tensor with dimensions [batch_size, height, width, channels]
		num_classes: Integer, the total number of categories in the dataset
		is_training : Bool, whether to updates the running means and variances during the training.
	Returns:
		A score map with dimensions [batch_size, 1/8*height, 1/8*width, num_classes]

	'''
	with slim.arg_scope(resnet_utils.resnet_arg_scope()) as sc:
		net, _ = resnet_v2.resnet_v2_101(inputs,
					                    num_classes=None,
					                    is_training=is_training,
					                    global_pool=False,
					                    output_stride=8,	
					                    reuse=None,
					                    scope='resnet_v2_101')
	
	# ASPP module without BN layers
	with tf.variable_scope('Deeplab_v2'):
		pool6 = slim.conv2d(net, num_classes, [3,3], rate=6, activation_fn=None, scope='pool6')
		pool12 = slim.conv2d(net, num_classes, [3,3], rate=12, activation_fn=None, scope='pool12')
		pool18 = slim.conv2d(net, num_classes, [3,3], rate=18, activation_fn=None, scope='pool18')
		pool24 = slim.conv2d(net, num_classes, [3,3], rate=24, activation_fn=None, scope='pool24')
		logits = tf.add_n([pool6, pool12, pool18, pool24], name='logits')
		return logits
def PSPNet(inputs , num_classes, is_training):
	'''A TensorFlow implementation of PSPNet model based on 
	   https://github.com/hszhao/PSPNet/tree/master/evaluation/prototxt	   	
	
	Args:
		inputs: A 4-D tensor with dimensions [batch_size, height, width, channels]
		num_classes: Integer, the total number of categories in the dataset
		is_training : Bool, whether to updates the running means and variances during the training.
	Returns:
		A score map with dimensions [batch_size, 1/8*height, 1/8*width, num_classes]

	'''
	with slim.arg_scope(resnet_utils.resnet_arg_scope()):
		net, end_points = resnet_v2.resnet_v2_101(inputs,
							                      num_classes=None,
							                      is_training=is_training,
							                      global_pool=False,
							                      output_stride=8,
							                      reuse=None,
							                      scope='resnet_v2_101')
	with tf.variable_scope("PSPNet") as sc:    	
		shape = tf.shape(net)[1:3]
		net = PyramidPoolingModule(net, shape = shape)
		net = slim.conv2d(net, 512, [3, 3], activation_fn=None, scope='conv5')
		net = slim.batch_norm(net, fused=True, scope='conv5_bn')
		net = tf.nn.relu(net, name='conv5_bn_relu')
		logits = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')
		return logits
示例#4
0
def extract_features(inputs, contexts, is_training):

    # TODO - Add skip connections between conv-deconv layers
    with slim.arg_scope(resnet_utils.resnet_arg_scope(is_training=is_training)):

        conv1 = tf.layers.conv2d(inputs, filters=128, kernel_size=3, strides=1, padding='same')

        context_layer = expand_context(contexts, height, width)

        contexted_batch = tf.concat([conv1, context_layer], axis=3)

        net, end_points = resnet_v2.resnet_v2_101(contexted_batch,
                                                None,
                                                global_pool=False,
                                                output_stride=16)

        deconv1 = deconv_block(net,num_inputs=2048, num_outputs=1024,
                               is_training=is_training, scope='deconv1')

        deconv2 = deconv_block(deconv1, num_inputs=1024, num_outputs=512,
                               stride=2, is_training=is_training, scope='deconv2')

        deconv3 = deconv_block(deconv2, num_inputs=512, num_outputs=256,
                               stride=2, is_training=is_training, scope='deconv3')

        deconv4 = deconv_block(deconv3, num_inputs=256,num_outputs=128,
                               stride=2, is_training=is_training, scope='deconv4')

        deconv5 = deconv_block(deconv4, num_inputs=128,num_outputs=64,
                               stride=2, is_training=is_training, scope='deconv5')

    return deconv5, net
示例#5
0
def featureExtractor(input,
                     feature_norm,
                     model='101',
                     reuse=False,
                     scope='resnet_v2_101'):
    with tf.variable_scope(scope, reuse=reuse) as scope:
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            input = scale_RGB(input)
            if '50' in model:
                net, end_points = resnet_v2.resnet_v2_50(input,
                                                         global_pool=True,
                                                         is_training=False,
                                                         reuse=reuse)
            elif '101' in model:
                _, end_points = resnet_v2.resnet_v2_101(input,
                                                        global_pool=True,
                                                        is_training=False,
                                                        reuse=reuse)
                #f = end_points['stabNet/pathFinder/featureExtractor/resnet_v2_101/block3/unit_23/bottleneck_v2/conv1'] #(18 X 32) X (18 X 32)
                f = end_points[
                    '{}/resnet_v2_101/block4/unit_2/bottleneck_v2/conv1'.
                    format(scope.name)]  #(9 X 16) X (9 X 16)
        if feature_norm:
            f = featureL2Norm(f)

    return f
示例#6
0
 def graph_backbone_resnet_101(self, inputs):
     with tf.variable_scope('backbone'):
         feature, end_point = resnet_v2.resnet_v2_101(
             inputs,
             num_classes=None,
             global_pool=False,
             is_training=self.is_training,
             reuse=self.reuse,
             scope="resnet_v2_101")
         with tf.variable_scope('up_sample'):
             feature = slim.conv2d_transpose(feature,
                                             512,
                                             3,
                                             2,
                                             activation_fn=None,
                                             reuse=self.reuse,
                                             scope="transpose_conv1")
             feature = slim.batch_norm(inputs=feature,
                                       activation_fn=tf.nn.relu,
                                       is_training=self.is_training,
                                       scope='transpose_conv1/bn',
                                       reuse=self.reuse,
                                       scale=True)
             feature = slim.conv2d_transpose(feature,
                                             512,
                                             3,
                                             2,
                                             activation_fn=None,
                                             reuse=self.reuse,
                                             scope="transpose_conv2")
             feature = slim.batch_norm(inputs=feature,
                                       activation_fn=tf.nn.relu,
                                       is_training=self.is_training,
                                       scope='transpose_conv2/bn',
                                       reuse=self.reuse,
                                       scale=True)
             feature = slim.conv2d_transpose(feature,
                                             512,
                                             3,
                                             2,
                                             activation_fn=None,
                                             reuse=self.reuse,
                                             scope="transpose_conv3")
             feature = slim.batch_norm(inputs=feature,
                                       activation_fn=tf.nn.relu,
                                       is_training=self.is_training,
                                       scope='transpose_conv3/bn',
                                       reuse=self.reuse,
                                       scale=True)
             feature = slim.conv2d(feature,
                                   self.residual_dim[0],
                                   3,
                                   1,
                                   activation_fn=None,
                                   normalizer_fn=None,
                                   reuse=self.reuse,
                                   scope="conv1")
     return [feature]
示例#7
0
def resnet(bsize=None):
    from tensorflow.contrib.slim.nets import resnet_v2
    x = tf.placeholder(tf.float32, shape=(bsize, 224, 224, 3))
    y = tf.placeholder(tf.float32, shape=(bsize, 1000))
    output, _ = resnet_v2.resnet_v2_101(x, 1000)
    output = tf.contrib.slim.flatten(output)
    loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)
    optimizer = tf.train.AdamOptimizer(learning_rate=0.2).minimize(tf.reduce_sum(loss))
    return optimizer
示例#8
0
def resnet101(inputs, num_classes, is_training, global_pool = True):
  with slim.arg_scope(resnet_v2.resnet_arg_scope()):
    logits, end_points = resnet_v2.resnet_v2_101(inputs,
                                                num_classes,
                                                global_pool = global_pool,
                                                reuse=tf.AUTO_REUSE)
    predictions = {
      "classes": tf.argmax(logits, axis=1),
      "probabilities": end_points["predictions"]
    }

    return logits, predictions
示例#9
0
 def __call__(self, x_input):
     """Constructs model and return probabilities for given input."""
     reuse = True if self.built else None
     with slim.arg_scope(resnet_v2.resnet_arg_scope()):
         _, end_points = resnet_v2.resnet_v2_101(
                 x_input, num_classes=self.num_classes, is_training=False,
                 reuse=reuse)
         self.built = True
     output = end_points['predictions']
     # Strip off the extra reshape op at the output
     probs = output.op.inputs[0]
     return probs
示例#10
0
 def _get_logits_by_slim_model(self, inputs):
     ctx = get_current_tower_context()
     with tf.contrib.slim.arg_scope(
             resnet_v2.resnet_arg_scope(batch_norm_decay=0.9997)):
         logits, end_points = resnet_v2.resnet_v2_101(
             inputs,
             num_classes=None,
             is_training=ctx.is_training,
             global_pool=False,
             output_stride=16)
     net = end_points['resnet_v2_101/block4']
     return net
示例#11
0
 def side(self, input):
     net1, end_points1 = resnet_v2.resnet_v2_101(input,
                                                 None,
                                                 is_training=True,
                                                 global_pool=False,
                                                 output_stride=16)
     arranged = tf.reshape(net1,
                           shape=[-1, 1, 1, 12 * 15 * 2048],
                           name="arrange_for_fcl")
     self.fc1 = self.fcl(arranged, 256, "fc1")  # 1024
     self.fc2 = self.fcl(self.fc1, 512, "fc2")  # 2048
     self.fc3 = self.fcl(self.fc2, 128, "fc3")  # 512
     return self.fc3
def denseASPP(inputs,
              is_training,
              output_stride,
              pre_trained_model,
              classes,
              keep_prob=1.0):

    with tf.contrib.slim.arg_scope(
            resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):
        logits, end_points = resnet_v2.resnet_v2_101(
            inputs,
            num_classes=None,
            is_training=is_training,
            global_pool=False,
            output_stride=output_stride)

    if is_training:
        exclude = ['resnet_v2_101' + '/logits', 'global_step']
        variables_to_restore = tf.contrib.slim.get_variables_to_restore(
            exclude=exclude)
        tf.train.init_from_checkpoint(
            pre_trained_model,
            {v.name.split(':')[0]: v
             for v in variables_to_restore})

    net = end_points['resnet_v2_101' + '/block4']

    with tf.name_scope("denseASPP"):

        input = denseASPP_block(net, is_training, keep_prob)

    with tf.contrib.slim.arg_scope(
            resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):

        with tf.name_scope("segmentation"):

            input_shape = input.get_shape().as_list()
            input = tf.nn.dropout(input, keep_prob=keep_prob)
            weight_1 = weight_variable([1, 1, input_shape[-1], classes])
            bias = bias_variable([classes])
            input = tf.nn.conv2d(input, weight_1, [1, 1, 1, 1],
                                 padding='SAME') + bias

        with tf.name_scope("upsamling"):
            input_shape = input.get_shape().as_list()
            input = tf.image.resize_bilinear(input, tf.shape(inputs)[1:3])

    output = input
    return output
示例#13
0
def get_network():
    images_placeholder = tf.placeholder(tf.float32, shape=(None, 512, 512, 3))
    preprocessed_batch = tf.map_fn(
        lambda img: preprocess_image(img,
                                     output_height=512,
                                     output_width=512,
                                     is_training=False,
                                     resize_side_min=512,
                                     resize_side_max=512), images_placeholder)
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        net, all_layers = resnet_v2.resnet_v2_101(preprocessed_batch,
                                                  21,
                                                  is_training=False,
                                                  global_pool=False,
                                                  output_stride=16)
    return images_placeholder, net, all_layers
示例#14
0
def PSPNet(inputs, is_training, output_stride, pre_trained_model, classes):
    with tf.contrib.slim.arg_scope(resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):
        logits, end_points = resnet_v2.resnet_v2_101(inputs, num_classes=None, is_training=is_training,
                                                     global_pool=False, output_stride=output_stride)

    if is_training:
        exclude = ['resnet_v2_101' + '/logits', 'global_step']
        variables_to_restore = tf.contrib.slim.get_variables_to_restore(exclude=exclude)
        tf.train.init_from_checkpoint(pre_trained_model, {v.name.split(':')[0]: v for v in variables_to_restore})

    net = end_points['resnet_v2_101' + '/block4']
    encoder_output = pyramid_pooling(net, is_training)

    with tf.contrib.slim.arg_scope(resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):
        with tf.name_scope("auli_logits"):

            key = 'resnet_v2_101/block3'
            auxi_logits = end_points[key]

            auli_shape = auxi_logits.get_shape().as_list()
            weight_3 = weight_variable([3, 3, auli_shape[-1], auli_shape[-1] // 4])
            auxi_logits = tf.nn.conv2d(auxi_logits, weight_3, [1, 1, 1, 1], padding='SAME')
            auxi_logits = batch_norm(auxi_logits, is_training)
            auxi_logits = tf.nn.relu(auxi_logits)

            weight_1 = weight_variable([1, 1, auli_shape[-1] // 4, classes])
            bias = bias_variable([classes])
            auxi_logits = tf.nn.conv2d(auxi_logits, weight_1, [1, 1, 1, 1], padding='SAME') + bias

            auxi_logits = tf.image.resize_bilinear(auxi_logits, tf.shape(inputs)[1:3])

        with tf.name_scope("segmentation"):
            encoder_output_shape = encoder_output.get_shape().as_list()
            weight_3 = weight_variable([3, 3, encoder_output_shape[-1], encoder_output_shape[-1] // 4])
            net = tf.nn.conv2d(encoder_output, weight_3, [1, 1, 1, 1], padding='SAME')
            net = batch_norm(net, is_training)
            net = tf.nn.relu(net)

            weight_1 = weight_variable([1, 1, encoder_output_shape[-1] // 4, classes])
            bias = bias_variable([classes])
            net = tf.nn.conv2d(net, weight_1, [1, 1, 1, 1], padding='SAME') + bias

            logits = tf.image.resize_bilinear(net, tf.shape(inputs)[1:3])



    return auxi_logits, logits
def evaluate_one_image():
    '''Test one image against the saved models and parameters
   '''

    # you need to change the directories to yours.
    model_dir = '/home/jiangtao/model/5_class_resnet/'
    img_dir = '/home/jiangtao/datasets/DANdata/test/2_test/Audi0001.jpg'
    image = Image.open(img_dir)
    plt.imshow(image)
    image = image.resize([224, 224])
    image = np.array(image)
    print(image.shape)
    image_array = image.reshape([224, 224, 3])
    image_tensor = tf.constant(image_array)
    image_tensor_std = tf.image.per_image_standardization(image_tensor)
    with tf.Session() as sess:
        image_array = image_tensor_std.eval()
    image_array = image_array.reshape([1, 224, 224, 3])

    with tf.Graph().as_default():

        x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
        logit, _ = resnet_v2.resnet_v2_101(x, num_classes=5, is_training=False)
        logit = tf.nn.softmax(logit)

        saver = tf.train.Saver()

        with tf.Session() as sess:

            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(model_dir)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')

            prediction = sess.run(logit, feed_dict={x: image_array})
            max_index = np.argmax(prediction)

            print(max_index)
示例#16
0
def deeplab_v3_plus(inputs, is_training, output_stride, pre_trained_model):

    with tf.contrib.slim.arg_scope(resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):
        logits, end_points = resnet_v2.resnet_v2_101(inputs, num_classes=None, is_training=is_training, global_pool=False, output_stride=output_stride)

    if is_training:
        exclude = ['resnet_v2_101' + '/logits', 'global_step']
        variables_to_restore = tf.contrib.slim.get_variables_to_restore(exclude=exclude)
        tf.train.init_from_checkpoint(pre_trained_model, {v.name.split(':')[0]: v for v in variables_to_restore})

    net = end_points['resnet_v2_101' + '/block4']
    encoder_output = aspp(net, output_stride, is_training)

    with tf.contrib.slim.arg_scope(resnet_v2.resnet_arg_scope(batch_norm_decay=_BATCH_NORM_DECAY)):
        with tf.name_scope('low_level_features'):
            low_level_features = end_points['resnet_v2_101' + '/block1/unit_3/bottleneck_v2/conv1']
            in_channels = low_level_features.get_shape().as_list()[-1]
            low_level_shape = tf.shape(low_level_features)
            weight_1x1_low_level = weight_variable([1, 1, in_channels, 48], name='weight_1x1_low_level')
            conv_1x1_low_level = tf.nn.conv2d(low_level_features, weight_1x1_low_level, [1, 1, 1, 1], padding='SAME', name='conv_1x1_low_level')
            conv_1x1_low_level = tf.nn.relu(batch_norm(conv_1x1_low_level, is_training), name='relu_1x1_low_level')

            low_level_features_size = low_level_shape[1:3]

        with tf.name_scope("upsamling_logits"):
            net = tf.image.resize_bilinear(encoder_output, low_level_features_size, name='upsample_1')

            net = tf.concat([net, low_level_features], axis=-1, name='concat')
            weight_3x3_upsamle_1 = weight_variable([3, 3, net.get_shape().as_list()[-1], 256], name='weight_3x3_upsamle_1')
            weight_3x3_upsamle_2 = weight_variable([3, 3, 256, 256], name='weight_3x3_upsamle_2')
            weight_3x3_upsamle_3 = weight_variable([1, 1, 256, CLASSES], name='weight_3x3_upsamle_3')  # => weight_1x1_upsamle_3
            bias = bias_variable([CLASSES], name='softmax_bias')

            net = tf.nn.conv2d(net, weight_3x3_upsamle_1, [1, 1, 1, 1], padding='SAME', name='conv_3x3_upsamle_1')
            net = tf.nn.relu(batch_norm(net, is_training), name='conv_3x3_relu_1')
            net = tf.nn.conv2d(net, weight_3x3_upsamle_2, [1, 1, 1, 1], padding='SAME', name='conv_3x3_upsamle_2')
            net = tf.nn.relu(batch_norm(net, is_training), name='conv_3x3_relu_2')
            net = tf.nn.conv2d(net, weight_3x3_upsamle_3, [1, 1, 1, 1], padding='SAME', name='conv_1x1_upsamle_3') + bias

            logits = tf.image.resize_bilinear(net, tf.shape(inputs)[1:3], name='upsample_2')


    return logits
示例#17
0
 def get_backbone(self,features):
     if self.flags.model_variant.startswith('xception'):
         assert False,'not implement'
     elif self.flags.model_variant=='resnet_v2_50':
         # inputs has shape [batch, 513, 513, 3]
         with slim.arg_scope(resnet_v2.resnet_arg_scope()):
             net, end_points = resnet_v2.resnet_v2_50(features,
                                             self.num_classes,
                                             is_training=False,
                                             global_pool=False,
                                             output_stride=self.output_stride)
     elif self.flags.model_variant=='resnet_v1_50':
         # The key difference of the full preactivation 'v2' variant compared to the
         # 'v1' variant in [1] is the use of batch normalization before every weight layer.
         with slim.arg_scope(resnet_v1.resnet_arg_scope()):
             net, end_points = resnet_v1.resnet_v1_50(features,
                                             self.num_classes,
                                             is_training=False,
                                             global_pool=False,
                                             output_stride=self.output_stride)
     elif self.flags.model_variant=='resnet_v2_101':
         # inputs has shape [batch, 513, 513, 3]
         with slim.arg_scope(resnet_v2.resnet_arg_scope()):
             net, end_points = resnet_v2.resnet_v2_101(features,
                                             self.num_classes,
                                             is_training=False,
                                             global_pool=False,
                                             output_stride=self.output_stride)
     elif self.flags.model_variant=='resnet_v1_101':
         # The key difference of the full preactivation 'v2' variant compared to the
         # 'v1' variant in [1] is the use of batch normalization before every weight layer.
         with slim.arg_scope(resnet_v1.resnet_arg_scope()):
             net, end_points = resnet_v1.resnet_v1_101(features,
                                             self.num_classes,
                                             is_training=False,
                                             global_pool=False,
                                             output_stride=self.output_stride)
     else:
         assert False,'not implement'
         
     print(end_points.keys())
     print(net)
示例#18
0
def main(unused_argv):
    mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
    if FLAGS.download_only:
        sys.exit(0)

    if FLAGS.job_name is None or FLAGS.job_name == "":
        raise ValueError("Must specify an explicit `job_name`")
    if FLAGS.task_index is None or FLAGS.task_index == "":
        raise ValueError("Must specify an explicit `task_index`")

    print("job name = %s" % FLAGS.job_name)
    print("task index = %d" % FLAGS.task_index)

    #Construct the cluster and start the server
    ps_spec = FLAGS.ps_hosts.split(",")
    worker_spec = FLAGS.worker_hosts.split(",")

    # Get the number of workers.
    num_workers = len(worker_spec)

    cluster = tf.train.ClusterSpec({"ps": ps_spec, "worker": worker_spec})

    if not FLAGS.existing_servers:
        # Not using existing servers. Create an in-process server.
        server = tf.train.Server(cluster,
                                 job_name=FLAGS.job_name,
                                 task_index=FLAGS.task_index)
        if FLAGS.job_name == "ps":
            server.join()

    is_chief = (FLAGS.task_index == 0)
    if FLAGS.num_gpus > 0:
        # Avoid gpu allocation conflict: now allocate task_num -> #gpu
        # for each worker in the corresponding machine
        gpu = (FLAGS.task_index % FLAGS.num_gpus)
        worker_device = "/job:worker/task:%d/gpu:%d" % (FLAGS.task_index, gpu)
    elif FLAGS.num_gpus == 0:
        # Just allocate the CPU to worker server
        cpu = 0
        worker_device = "/job:worker/task:%d/cpu:%d" % (FLAGS.task_index, cpu)
    # The device setter will automatically place Variables ops on separate
    # parameter servers (ps). The non-Variable ops will be placed on the workers.
    # The ps use CPU and workers use corresponding GPU
    with tf.device(
            tf.train.replica_device_setter(worker_device=worker_device,
                                           ps_device="/job:ps/cpu:0",
                                           cluster=cluster)):
        global_step = tf.Variable(0, name="global_step", trainable=False)

        # Here we construct a simple training model.
        # Ops: located on the worker specified with FLAGS.task_index
        x = tf.placeholder(tf.float32, [None, IMAGE_PIXELS * IMAGE_PIXELS])
        y_ = tf.placeholder(tf.float32, [None, 10])

        _x = tf.reshape(x, (-1, IMAGE_PIXELS, IMAGE_PIXELS))
        x_ = tf.stack((_x, _x, _x), axis=3)

        if FLAGS.cnn_model == "resnet_v2":
            x_ = tf.image.resize_images(x_, (224, 224))
            y, _ = resnet_v2.resnet_v2_101(x_,
                                           num_classes=10,
                                           is_training=True)

            cross_entropy = -tf.reduce_sum(
                y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
            opt = tf.train.AdamOptimizer(FLAGS.learning_rate)
        else:
            # default model is inception_v3
            x_ = tf.image.resize_images(x_, (299, 299))
            y, _ = inception.inception_v3(x_, num_classes=10, is_training=True)

            cross_entropy = -tf.reduce_sum(
                y_ * tf.log(tf.clip_by_value(y, 1e-10, 1.0)))
            opt = tf.train.AdamOptimizer(FLAGS.learning_rate)

        if FLAGS.sync_replicas:
            if FLAGS.replicas_to_aggregate is None:
                replicas_to_aggregate = num_workers
            else:
                replicas_to_aggregate = FLAGS.replicas_to_aggregate

            opt = tf.train.SyncReplicasOptimizer(
                opt,
                replicas_to_aggregate=replicas_to_aggregate,
                total_num_replicas=num_workers,
                name="mnist_sync_replicas")

        train_step = opt.minimize(cross_entropy, global_step=global_step)

        if FLAGS.sync_replicas:
            local_init_op = opt.local_step_init_op
            if is_chief:
                local_init_op = opt.chief_init_op

            ready_for_local_init_op = opt.ready_for_local_init_op

            # Initial token and chief queue runners required by the sync_replicas mode
            chief_queue_runner = opt.get_chief_queue_runner()
            sync_init_op = opt.get_init_tokens_op()

        init_op = tf.global_variables_initializer()
        train_dir = tempfile.mkdtemp()

        if FLAGS.sync_replicas:
            sv = tf.train.Supervisor(
                is_chief=is_chief,
                logdir=train_dir,
                init_op=init_op,
                local_init_op=local_init_op,
                ready_for_local_init_op=ready_for_local_init_op,
                recovery_wait_secs=1,
                global_step=global_step)
        else:
            sv = tf.train.Supervisor(is_chief=is_chief,
                                     logdir=train_dir,
                                     init_op=init_op,
                                     recovery_wait_secs=1,
                                     global_step=global_step)

        sess_config = tf.ConfigProto(allow_soft_placement=True,
                                     log_device_placement=False,
                                     device_filters=[
                                         "/job:ps",
                                         "/job:worker/task:%d" %
                                         FLAGS.task_index
                                     ])

        # The chief worker (task_index==0) session will prepare the session,
        # while the remaining workers will wait for the preparation to complete.
        if is_chief:
            print("Worker %d: Initializing session..." % FLAGS.task_index)
        else:
            print("Worker %d: Waiting for session to be initialized..." %
                  FLAGS.task_index)

        if FLAGS.existing_servers:
            server_grpc_url = "grpc://" + worker_spec[FLAGS.task_index]
            print("Using existing server at: %s" % server_grpc_url)

            sess = sv.prepare_or_wait_for_session(server_grpc_url,
                                                  config=sess_config)
        else:
            sess = sv.prepare_or_wait_for_session(server.target,
                                                  config=sess_config)

        print("Worker %d: Session initialization complete." % FLAGS.task_index)

        if FLAGS.sync_replicas and is_chief:
            # Chief worker will start the chief queue runner and call the init op.
            sess.run(sync_init_op)
            sv.start_queue_runners(sess, [chief_queue_runner])

        # Perform training
        time_begin = time.time()
        print("Training begins @ %f" % time_begin)

        local_step = 0
        while True:
            # Training feed
            batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size)
            train_feed = {x: batch_xs, y_: batch_ys}

            _, step = sess.run([train_step, global_step], feed_dict=train_feed)
            local_step += 1

            now = time.time()
            print("%f: Worker %d: training step %d done (global step: %d)" %
                  (now, FLAGS.task_index, local_step, step))

            if step >= FLAGS.train_steps:
                break

        time_end = time.time()
        print("Training ends @ %f" % time_end)
        training_time = time_end - time_begin
        print("Training elapsed time: %f s" % training_time)

        # Validation feed
        val_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
        val_xent = sess.run(cross_entropy, feed_dict=val_feed)
        print("After %d training step(s), validation cross entropy = %g" %
              (FLAGS.train_steps, val_xent))
示例#19
0
def _iterative_fast_gradient_attack(ensemble,
                                    eps,
                                    alpha,
                                    num_iter,
                                    tic=time.time()):
    """ Constructs the tensorflow graph that implements this attack. 
      Note this does not execute the attack; that is for the caller to do.
  """
    # hardcode since we are in a hurry...
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001

    # Network inputs
    x_input = tf.placeholder(tf.float32, shape=batch_shape)
    x_max = tf.clip_by_value(x_input + eps, -1.0, 1.0)
    x_min = tf.clip_by_value(x_input - eps, -1.0, 1.0)

    target_class_input = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    one_hot_target_class = tf.one_hot(
        target_class_input,
        num_classes)  # note: labels are smoothed in loss function

    # initially x_adv is the same as the input
    x_adv = x_input

    # initialize networks
    if 'InceptionV3' in ensemble:
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='InceptionV3')

    if 'resnet_v2_101' in ensemble:
        with slim.arg_scope(resnet_v2.resnet_arg_scope()):
            resnet_v2.resnet_v2_101(x_input,
                                    num_classes=num_classes,
                                    is_training=False,
                                    scope='resnet_v2_101')

    if 'InceptionResnetV2' in ensemble:
        with slim.arg_scope(ir.inception_resnet_v2_arg_scope()):
            ir.inception_resnet_v2(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='InceptionResnetV2')

    if 'Adv-InceptionV3' in ensemble:
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            inception.inception_v3(x_input,
                                   num_classes=num_classes,
                                   is_training=False,
                                   scope='Adv-InceptionV3')

    print('[INFO]: initial setup; net runtime: %0.2f seconds\n' %
          (time.time() - tic))
    print('[INFO]: ensemble contents:', ensemble)

    for ts in range(num_iter):
        # TODO: this code is gross; clean up if time permits...

        #--------------------------------------------------
        # contribution from InceptionV3
        #--------------------------------------------------
        if 'InceptionV3' in ensemble:
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                logits, end_points = inception.inception_v3(
                    x_adv,
                    num_classes=num_classes,
                    is_training=False,
                    reuse=True,
                    scope='InceptionV3')

            cross_entropy = tf.losses.softmax_cross_entropy(
                one_hot_target_class, logits, label_smoothing=0.1, weights=1.0)
            cross_entropy += tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                end_points['AuxLogits'],
                label_smoothing=0.1,
                weights=0.4)
        else:
            cross_entropy = 0

        #--------------------------------------------------
        # contribution from resnet_v2
        #--------------------------------------------------
        if 'resnet_v2_101' in ensemble:
            with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                logits_2, _ = resnet_v2.resnet_v2_101(x_adv,
                                                      num_classes=num_classes,
                                                      is_training=False,
                                                      reuse=True,
                                                      scope='resnet_v2_101')

                # Note: ResnetV2 logits has shape (BATCH_SIZE, 1, 1, N_CLASSES)
                #       Hence the squeeze below.
                cross_entropy_2 = tf.losses.softmax_cross_entropy(
                    one_hot_target_class,
                    tf.squeeze(logits_2),
                    label_smoothing=0.1,
                    weights=1.0)
        else:
            cross_entropy_2 = 0

        #--------------------------------------------------
        # contribution from ensemble {resnet_v2, inception_v3}
        #--------------------------------------------------
        if 'InceptionResnetV2' in ensemble:
            with slim.arg_scope(ir.inception_resnet_v2_arg_scope()):
                logits_3, _ = ir.inception_resnet_v2(x_adv,
                                                     num_classes=num_classes,
                                                     is_training=False,
                                                     reuse=True,
                                                     scope='InceptionResnetV2')

            cross_entropy_3 = tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                logits_3,
                label_smoothing=0.1,
                weights=1.0)
        else:
            cross_entropy_3 = 0

        #--------------------------------------------------
        # contribution from InceptionV3-adv
        #--------------------------------------------------
        if 'Adv-InceptionV3' in ensemble:
            with slim.arg_scope(inception.inception_v3_arg_scope()):
                logits_4, end_points_4 = inception.inception_v3(
                    x_adv,
                    num_classes=num_classes,
                    is_training=False,
                    reuse=True,
                    scope='Adv-InceptionV3')

            cross_entropy_4 = tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                logits_4,
                label_smoothing=0.1,
                weights=1.0)
            cross_entropy_4 += tf.losses.softmax_cross_entropy(
                one_hot_target_class,
                end_points_4['AuxLogits'],
                label_smoothing=0.1,
                weights=0.4)
        else:
            cross_entropy_4 = 0

        print(
            '[INFO]: added %d models for timestep %d/%d; net runtime: %0.2f seconds'
            % (len(ensemble), ts + 1, num_iter, time.time() - tic))

        #--------------------------------------------------
        # gradient step
        #--------------------------------------------------
        cross_entropy_avg = (cross_entropy + cross_entropy_2 +
                             cross_entropy_3 + cross_entropy_4) / len(ensemble)

        nabla_x = tf.gradients(cross_entropy_avg, x_adv)[0]

        # EXPERIMENT: add some random noise to gradient (avoid "overfitting"?)
        #if False:
        #  print('[WARNING]: using experimental noisy gradient!')
        #  nabla_x = nabla_x + tf.random_normal(tf.shape(nabla_x), mean=0, stddev=1e-2)

        # EXPERIMENT: avoid moving in directions corresponding to small values of gradient
        #if False:
        #  print('[WARNING]: using experimental gradient clipping!')
        #  # 1e-2 too large of a threshold
        #  nabla_x = tf.where(tf.abs(nabla_x) < 1e-3, tf.zeros(tf.shape(nabla_x)), nabla_x)

        x_next = x_adv - alpha * tf.sign(nabla_x)

        # Always clip at the end
        x_next = tf.clip_by_value(x_next, x_min, x_max)
        x_adv = x_next

    #--------------------------------------------------
    # set up model weight loading
    #--------------------------------------------------
    savers = []
    if 'InceptionV3' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='InceptionV3')),
             './Weights/inception_v3.ckpt'))
    if 'resnet_v2_101' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='resnet_v2_101')),
             './Weights/resnet_v2_101.ckpt'))
    if 'InceptionResnetV2' in ensemble:
        savers.append((tf.train.Saver(
            slim.get_model_variables(scope='InceptionResnetV2')),
                       './Weights/ens_adv_inception_resnet_v2.ckpt'))
    if 'Adv-InceptionV3' in ensemble:
        savers.append(
            (tf.train.Saver(slim.get_model_variables(scope='Adv-InceptionV3')),
             './Weights/InceptionV3-adv.ckpt'))

    print('[INFO]: FG(S)M setup complete; took %0.2f seconds\n' %
          (time.time() - tic))

    return x_adv, [x_input, target_class_input], savers
def _resnet_v2(x):
    with tf.name_scope("resnet_v2"):
        x_ = tf.image.resize_images(x, (224, 224))
        y, _ = resnet_v2.resnet_v2_101(x_, num_classes=10, is_training=True)
        return y
示例#21
0
文件: dssd_513.py 项目: gzn91/DSSD
    def ssd_network(self, image, *args, **kwargs):
        is_training, reuse, scope, use_bn = args
        ncls, fmap_layer, anchor_scales, anchor_ratios, multibox_bn = kwargs.values(
        )
        x = image

        # Down
        with tf.variable_scope('', reuse=reuse):
            with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                net, end_points = resnet_v2.resnet_v2_101(image,
                                                          num_classes=None,
                                                          is_training=False,
                                                          global_pool=False,
                                                          output_stride=None,
                                                          reuse=reuse)

            x_65 = end_points['resnet_v2_101/block2']
            x = end_points['resnet_v2_101/block4']
            print('shape after block2', x_65.get_shape().as_list())

            # Dilation block
            # Batch norm and leaky relu
            with tf.variable_scope('block5_a'):
                x = conv2d(x,
                           1024,
                           3,
                           dilations=6,
                           name='dil1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_33 = conv2d(x,
                              1024,
                              1,
                              dilations=1,
                              name='conv1',
                              padding='SAME',
                              use_bn=True,
                              training=is_training)
                print('shape after block5_a', x_33.get_shape().as_list())

            # cba renaming all the blocks
            with tf.variable_scope('block5_b'):
                x = conv2d(x_33,
                           256,
                           1,
                           name='conv1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_17 = conv2d(x,
                              512,
                              3,
                              strides=2,
                              name='conv2',
                              padding='SAME',
                              use_bn=True,
                              training=is_training)
                print('shape after block5_b', x_17.get_shape().as_list())

            # SSD Blocks
            # 9 x 9
            with tf.variable_scope('block6'):
                x = conv2d(x_17,
                           128,
                           1,
                           name='conv1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_9 = conv2d(x,
                             256,
                             3,
                             strides=2,
                             name='conv2',
                             padding='SAME',
                             use_bn=True,
                             training=is_training)
                print('shape after block6', x_9.get_shape().as_list())

            # 5 x 5
            with tf.variable_scope('block7'):
                x = conv2d(x_9,
                           128,
                           1,
                           name='conv1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_5 = conv2d(x,
                             256,
                             3,
                             strides=2,
                             name='conv2',
                             padding='SAME',
                             use_bn=True,
                             training=is_training)
                print('shape after block7', x_5.get_shape().as_list())

            # 3 x 3
            with tf.variable_scope('block8'):
                x = conv2d(x_5,
                           128,
                           1,
                           name='conv1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_3 = conv2d(x,
                             256,
                             3,
                             strides=2,
                             name='conv2',
                             padding='SAME',
                             use_bn=True,
                             training=is_training)
                print('shape after block8', x_3.get_shape().as_list())

            # 1 x 1
            with tf.variable_scope('block9'):
                x = conv2d(x_3,
                           128,
                           1,
                           name='conv1',
                           padding='SAME',
                           use_bn=True,
                           training=is_training)
                x_1 = conv2d(x,
                             256,
                             3,
                             strides=1,
                             name='conv2',
                             padding='VALID',
                             use_bn=True,
                             training=is_training)
                end_points['block9'] = x_1
                print('shape after block9', x_1.get_shape().as_list())

            # 3 x 3
            with tf.variable_scope('block10'):
                y_3 = self.deconv_module(x_1,
                                         x_3,
                                         is_training,
                                         kernel_size=3,
                                         scope='up1')
                print('shape after block10', y_3.get_shape().as_list())
                end_points['block10'] = y_3

            with tf.variable_scope('block11'):
                y_4 = self.deconv_module(y_3, x_5, is_training, scope='up2')
                print('shape after block11', y_4.get_shape().as_list())
                end_points['block11'] = y_4

            with tf.variable_scope('block12'):
                y_8 = self.deconv_module(y_4, x_9, is_training, scope='up3')
                print('shape after block12', y_8.get_shape().as_list())
                end_points['block12'] = y_8

            with tf.variable_scope('block13'):
                y_17 = self.deconv_module(y_8, x_17, is_training, scope='up4')
                print('shape after block13', y_17.get_shape().as_list())
                end_points['block13'] = y_17

            with tf.variable_scope('block14'):
                y_33 = self.deconv_module(y_17, x_33, is_training, scope='up5')
                print('shape after block14', y_33.get_shape().as_list())
                end_points['block14'] = y_33

            with tf.variable_scope('block15'):
                y_66 = self.deconv_module(y_33, x_65, is_training, scope='up6')
                print('shape after block15', y_66.get_shape().as_list())
                end_points['block15'] = y_66

            self.filters = 64

            predictions = []
            logits = []
            localizations = []
            """From each feature map in VGG send it to ssd multibox layer to predict class and location of objects"""
            for i, layer in enumerate(fmap_layer):
                with tf.variable_scope(layer + '_box', reuse=reuse):
                    prediction, localization = self.multibox_layer(
                        end_points[layer], ncls, anchor_scales[i],
                        anchor_ratios[i], multibox_bn[i], is_training)
                predictions.append(tf.nn.softmax(prediction))
                logits.append(prediction)
                localizations.append(localization)

            return predictions, localizations, logits, end_points
示例#22
0
def main(args):
    image_list_file = os.path.join(
        *[args.dataset_dir, 'image_lists', args.image_list_file])
    print('image_list_file =', image_list_file)
    with open(image_list_file) as handle:
        rows = handle.read().splitlines()
        rows = [row.split(',') for row in rows]
        image_files = [row[1] for row in rows]
        if len(rows[0]) > 2:
            crops = [row[2:] for row in rows]
        else:
            crops = [None] * len(image_files)
    print('len(image_files) =', len(image_files))

    output_file = os.path.join(
        *[args.dataset_dir, "resnet_fcn_features", args.output_file])
    output_file_handle = h5py.File(output_file, 'w')
    dataset_name = re.sub('.txt', '', args.image_list_file.split('/')[-1])
    hpy5_dataset = output_file_handle.create_dataset(dataset_name,
                                                     shape=(len(image_files),
                                                            32, 32, 2048),
                                                     dtype='f')

    images_placeholder = tf.placeholder(tf.float32, shape=(None, 512, 512, 3))
    preprocessed_batch = tf.map_fn(
        lambda img: preprocess_image(img,
                                     output_height=512,
                                     output_width=512,
                                     is_training=False,
                                     resize_side_min=512,
                                     resize_side_max=512), images_placeholder)
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        net, all_layers = resnet_v2.resnet_v2_101(preprocessed_batch,
                                                  21,
                                                  is_training=False,
                                                  global_pool=False,
                                                  output_stride=16)
    init_fn = get_init_fn(args.ckpt_path)

    # This is to prevent a CuDNN error - https://github.com/tensorflow/tensorflow/issues/24828
    config = tf.ConfigProto()
    # config.gpu_options.allow_growth = True

    with tf.Session(config=config) as sess:
        sess.run(tf.global_variables_initializer())
        init_fn(sess)

        num_images_in_batch = 0
        num_batches_done = 0
        batch_images = list()
        num_images_processed = 0

        if args.max_images_to_process is not None:
            max_images_to_process = args.max_images_to_process
        else:
            # Set it to a number that won't be hit
            max_images_to_process = len(image_files) + 1
        print('max_images_to_process =', max_images_to_process)

        inputs = zip(image_files, crops)
        for image_file, crop in inputs:
            pillow_image = Image.open(image_file)
            if crop is not None:
                pillow_image = pillow_image.crop(crop)
            pillow_image = pillow_image.resize((512, 512))
            np_image = get_numpy_array(pillow_image)
            np_image = np.expand_dims(np_image, 0)
            batch_images.append(np_image)

            num_images_in_batch += 1

            if num_images_in_batch >= args.batch_size or num_images_processed >= max_images_to_process:
                batch_images = np.concatenate(batch_images, 0)
                feed_dict = {images_placeholder: batch_images}
                output, activations = sess.run([net, all_layers],
                                               feed_dict=feed_dict)
                features = activations['resnet_v2_101/block4']
                batch_start_idx = num_batches_done * args.batch_size
                batch_end_idx = batch_start_idx + features.shape[0]
                print("batch_start_idx =", batch_start_idx)
                print("batch_end_idx =", batch_end_idx)
                print("features.shape =", features.shape)
                hpy5_dataset[batch_start_idx:batch_end_idx, :] = features

                print("Completed batch", num_batches_done)
                num_batches_done += 1
                num_images_processed += num_images_in_batch
                num_images_in_batch = 0
                print('num_images_processed =', num_images_processed)

                batch_images = list()
                if num_images_processed >= max_images_to_process:
                    print('Breaking loop: num_images_processed =',
                          num_images_processed, ', max_images_to_process =',
                          max_images_to_process)
                    break

        if len(batch_images) > 0:
            print('Completing remaining', len(batch_images), 'images')
            batch_images = np.concatenate(batch_images, 0)
            feed_dict = {images_placeholder: batch_images}
            output, activations = sess.run([net, all_layers],
                                           feed_dict=feed_dict)
            features = activations['resnet_v2_101/block4']
            batch_start_idx = num_batches_done * args.batch_size
            batch_end_idx = batch_start_idx + features.shape[0] - 1
            hpy5_dataset[batch_start_idx:batch_end_idx, :] = features
            num_batches_done += 1

    output_file_handle.close()
示例#23
0
def model_fn(model_name, batch_size):
    if model_name=='vgg19':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1000))
        output, _ = vgg.vgg_19(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='resnet200':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1,1, 1000))
        output, _ = resnet_v2.resnet_v2_200(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='resnet101':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1,1, 1000))
        output, _ = resnet_v2.resnet_v2_101(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='resnet152':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1,1, 1000))
        output, _ = resnet_v2.resnet_v2_152(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='nasnet_cifar':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1000))
        output, _ = nasnet.build_nasnet_cifar(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='mobile_net':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size,1000))
        output, _ = mobilenet_v2.mobilenet(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='inceptionv3':
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        output, _ = inception.inception_v3(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name=='transformer':
        dm = DatasetManager('wmt14')
        dm.maybe_download_data_files()
        dm.load_vocab()
        transformer = transf.Transformer(
            num_heads=8,
            d_model=512,
            d_ff=2048,
            model_name=model_name,
            tf_sess_config=dict(allow_soft_placement=True)
        )
        train_params = dict(
            learning_rate=1e-4,
            batch_size=batch_size,
            seq_len=10,
            max_steps=300000,
        )
        transformer.build_model('wmt14', dm.source_id2word, dm.target_id2word, 0,**train_params)
        loss = transformer._loss

    elif model_name=='bert':
        #bert_config = modeling.BertConfig.from_json_file('bert/bert_large/bert_config.json')
        bert_large_config_path = 'bert/pre-trained/large/cased_L-24_H-1024_A-16/bert_config.json'
        bert_config = modeling.BertConfig.from_json_file(bert_large_config_path)
        model = new_model_fn_builder(bert_config)
        features = {}
        features['input_ids']= tf.cast(100*tf.placeholder(tf.float32,shape=(batch_size,128)),tf.int32)
        features['input_mask'] = tf.cast(100*tf.placeholder(tf.float32,shape=(batch_size,128)),tf.int32)
        features['segment_ids']=tf.cast(100*tf.placeholder(tf.float32,shape=(batch_size,128)),tf.int32)
        features['start_positions'] = tf.cast(100*tf.placeholder(tf.float32,shape=(batch_size,)),tf.int32)
        features['end_positions'] =tf.cast(100*tf.placeholder(tf.float32,shape=(batch_size,)),tf.int32)
        loss = model(features)

    elif model_name == 'small':
        slim = tf.contrib.slim
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        v= tf.get_variable(name='large_variable',shape=(3000,224, 224, 3),trainable=True)
        x = tf.slice(v,[0,0,0,0],tf.shape(x),name='large_slice')
        net = slim.max_pool2d(x, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5],trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5],trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5],trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.flatten(net)
        net = slim.fully_connected(net, 1024, activation_fn=tf.nn.sigmoid,trainable=False)
        net = slim.fully_connected(net, 1000, activation_fn=None,trainable=False)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=net)

    optimizer = tf.train.AdamOptimizer(learning_rate=0.2,
                            beta1=0.9, beta2=0.98, epsilon=1e-9).minimize(
                                                        tf.reduce_sum(loss))
    # TODO: Make lr, beta, epsilon value of parameter
    """
    if opt == 'Adam':
        optimizer = tf.train.AdamOptimizer(learning_rate=0.2,
                            beta1=0.9, beta2=0.98, epsilon=1e-9).minimize(
                                                        tf.reduce_sum(loss))
    elif opt == 'GradientDescent':
        optimizer = tf.train.GradientDescentOptimizer(
                                learning_rate=0.2).minimize(tf.reduce_sum(loss))
    """
    return optimizer
slim = tf.contrib.slim

# Load images
filenames = list(glob.glob('./images/*.jpg'))
images = np.zeros((len(filenames), 224, 224, 3), dtype=np.float32)
for i, imageName in enumerate(filenames):
    print i, imageName
    img = skimage.io.imread(imageName)
    if len(img.shape) == 2:
        # we have a 2D, black and white image but  vgg16 needs 3 channels
        img = np.expand_dims(img, 2)
        img = np.repeat(img, 3, axis=2)
    img = scipy.misc.imresize(img, (224, 224))
    images[i, :, :, :] = img

in_images = tf.placeholder(tf.float32, images.shape)

with slim.arg_scope(resnet_v2.resnet_arg_scope()):
    model, intermed = resnet_v2.resnet_v2_101(in_images,
                                              1001,
                                              is_training=False)
    restored_variables = tf.contrib.framework.get_variables_to_restore()
    restorer = tf.train.Saver(restored_variables)

    with tf.Session() as sess:
        img_net_path = '/home/dan/tensorflow_tests/models/resnet_v2_101.ckpt'
        restorer.restore(sess, img_net_path)
        features = sess.run(model, feed_dict={in_images: images})

        embed()
示例#25
0
    for idx in range(c):
        kernel = units[0,:,:,idx]
        i = idx // (c_sqrt)
        j = idx % c_sqrt
        img[i*w:(i+1)*w, j*h:(j+1)*h] = kernel
    return img

def getActivations(layer, inputs):
    units = sess.run(layer, feed_dict={x: inputs})
    return plotNNFilter(units)

with tf.Graph().as_default():
    x = tf.placeholder(tf.float32, [1, 224, 224, 3])
    
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        output, layers = resnet_v2.resnet_v2_101(x, is_training=False)
        cnn_layers = []
        pattern = re.compile(".*conv*")
        for k in layers:
            if pattern.match(k):
                cnn_layers.append(k) 
    init_fn = slim.assign_from_checkpoint_fn(
        os.path.join('./resnet_v2_101.ckpt'),
        slim.get_model_variables('resnet_v2_101'))
    
    with tf.Session() as sess:
        init_fn(sess)
        for img_path in ['simple.png', 'clutter.png', 'outdoor.png']:
            img = cv2.imread(img_path)
            img = cv2.resize(img, (224, 224))
            for layer in cnn_layers:
    def build_model(self):
        with tf.name_scope('input'):
            input = self.input_dict['X']

            targets = self.input_dict['Y_softmax']
            targets_one_hot = tf.one_hot(targets, self.nb_classes + 1)

        # with slim.arg_scope(inception.inception_v3_arg_scope()):
        #     net, endpoints = inception.inception_v3(input, is_training=self.is_train)
        #     zzz = 0
        with slim.arg_scope(
                resnet_utils.resnet_arg_scope(is_training=self.is_train)):
            net, end_points = resnet_v2.resnet_v2_101(input,
                                                      num_classes=None,
                                                      global_pool=False,
                                                      output_stride=16)
            zzz = 0

        with tf.name_scope('upsamplig'):
            # upsampling

            # first, upsample x2
            net = slim.conv2d_transpose(net,
                                        256, [4, 4],
                                        stride=2,
                                        scope='upsample_1')
            block1_scored = slim.conv2d(end_points['resnet_v2_101/block1'],
                                        256, [1, 1],
                                        scope='upsample_1_scored',
                                        activation_fn=None)
            net = net + block1_scored

            # second, upsample x2
            net = slim.conv2d_transpose(net,
                                        128, [4, 4],
                                        stride=2,
                                        scope='upsample_2')
            block2_scored = slim.conv2d(
                end_points['resnet_v2_101/block1/unit_1/bottleneck_v2'],
                128, [1, 1],
                scope='upsample_2_scored',
                activation_fn=None)
            net = net + block2_scored

            # finally, upsample x4
            net = slim.conv2d_transpose(net,
                                        64, [16, 16],
                                        stride=4,
                                        scope='upsample_3')

            # add a few conv layers as the output
            net = slim.conv2d(net, 32, [3, 3], scope='conv8_1')
            net = slim.conv2d(net, 32, [3, 3], scope='conv8_2')

        ########
        # Logits
        with slim.arg_scope([
                slim.conv2d,
        ],
                            normalizer_fn=slim.batch_norm,
                            weights_regularizer=slim.l2_regularizer(
                                self.regularization)):
            logits = slim.conv2d(net,
                                 self.nb_classes + 1, [1, 1],
                                 scope='conv_final_classes',
                                 activation_fn=None)

        with tf.name_scope('prediction'):
            classes_probs = tf.nn.softmax(logits)

            self.op_predict = classes_probs

        with tf.name_scope('loss'):
            cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
                labels=targets_one_hot, logits=logits)
            loss_ce = tf.reduce_mean(tf.reduce_sum(cross_entropy, axis=[1, 2]))

            tf.losses.add_loss(loss_ce)

            total_loss = tf.losses.get_total_loss(
                add_regularization_losses=True)

            update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
            if update_ops:
                updates = tf.group(*update_ops)
                with tf.control_dependencies([updates]):
                    total_loss = tf.identity(total_loss)

            self.op_loss = total_loss
def model_fn(model_name, batch_size):
    if model_name == "vgg19":
        from tensorflow.contrib.slim.nets import vgg
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        output, _ = vgg.vgg_19(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "resnet200":
        from tensorflow.contrib.slim.nets import resnet_v2
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1, 1, 1000))
        output, _ = resnet_v2.resnet_v2_200(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "resnet101":
        from tensorflow.contrib.slim.nets import resnet_v2
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1, 1, 1000))
        output, _ = resnet_v2.resnet_v2_101(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "resnet152":
        from tensorflow.contrib.slim.nets import resnet_v2
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1, 1, 1000))
        output, _ = resnet_v2.resnet_v2_152(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "nasnet_cifar":
        from tensorflow.contrib.slim.nets import nasnet
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        output, _ = nasnet.build_nasnet_cifar(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)
    elif model_name == "mobile_net":
        from tensorflow.contrib.slim.nets import mobilenet_v2
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        output, _ = mobilenet_v2.mobilenet(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "inceptionv3":
        from tensorflow.contrib.slim.nets import inception_v3
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        output, _ = inception_v3.inception_v3(x, 1000)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=output)

    elif model_name == "transformer":
        import modeltransformer.transformer as transf
        from modeltransformer.data import DatasetManager
        dm = DatasetManager("wmt14")
        dm.maybe_download_data_files()
        dm.load_vocab()
        transformer = transf.Transformer(
            num_heads=8,
            d_model=512,
            d_ff=2048,
            model_name=model_name,
            tf_sess_config=dict(allow_soft_placement=True))
        train_params = dict(
            learning_rate=1e-4,
            batch_size=batch_size,
            seq_len=10,
            max_steps=300000,
        )
        transformer.build_model("wmt14", dm.source_id2word, dm.target_id2word,
                                0, **train_params)
        loss = transformer._loss

    elif model_name == "bert":
        from bert.runsquad import new_model_fn_builder
        import modeling
        bert_config = modeling.BertConfig.from_json_file(
            "bert/bert_large/bert_config.json")
        model = new_model_fn_builder(bert_config)
        features = {}
        features["input_ids"] = tf.cast(
            100 * tf.placeholder(tf.float32, shape=(batch_size, 128)),
            tf.int32)
        features["input_mask"] = tf.cast(
            100 * tf.placeholder(tf.float32, shape=(batch_size, 128)),
            tf.int32)
        features["segment_ids"] = tf.cast(
            100 * tf.placeholder(tf.float32, shape=(batch_size, 128)),
            tf.int32)
        features["start_positions"] = tf.cast(
            100 * tf.placeholder(tf.float32, shape=(batch_size, )), tf.int32)
        features["end_positions"] = tf.cast(
            100 * tf.placeholder(tf.float32, shape=(batch_size, )), tf.int32)
        loss = model(features)
    elif model_name == "small":
        slim = tf.contrib.slim
        x = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))
        y = tf.placeholder(tf.float32, shape=(batch_size, 1000))
        v = tf.get_variable(name="large_variable",
                            shape=(3000, 224, 224, 3),
                            trainable=True)
        x = tf.slice(v, [0, 0, 0, 0], tf.shape(x), name="large_slice")
        net = slim.max_pool2d(x, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5], trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5], trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.conv2d(net, 128, [5, 5], trainable=False)
        net = slim.max_pool2d(net, [2, 2], 2)
        net = slim.flatten(net)
        net = slim.fully_connected(net,
                                   1024,
                                   activation_fn=tf.nn.sigmoid,
                                   trainable=False)
        net = slim.fully_connected(net,
                                   1000,
                                   activation_fn=None,
                                   trainable=False)
        loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=net)
    optimizer = tf.train.AdamOptimizer(learning_rate=0.2,
                                       beta1=0.9,
                                       beta2=0.98,
                                       epsilon=1e-9).minimize(
                                           tf.reduce_sum(loss))
    return optimizer
示例#28
0
def cnn_model_fn():
    x = tf.placeholder(tf.string, name='x')

    input_layer = _parse_function(x)
    input_layer = tf.reshape(input_layer, [-1, 224, 224, 1])
    net, _ = resnet_v2.resnet_v2_101(input_layer)

    y1 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])
    y2 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])
    y3 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])
    y4 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])
    y5 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])
    y6 = tf.placeholder(tf.float32, shape=[None, RESNET_CLASSES])

    net = tf.squeeze(net, axis=[1, 2])
    letter1 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train')
    letter2 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train1')
    letter3 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train2')
    letter4 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train3')
    letter5 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train4')
    letter6 = slim.fully_connected(net,
                                   num_outputs=33,
                                   activation_fn=None,
                                   scope='train5')

    letter1_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y1, logits=letter1))
    letter2_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y2, logits=letter2))
    letter3_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y3, logits=letter3))
    letter4_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y4, logits=letter4))
    letter5_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y5, logits=letter5))
    letter6_cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y6, logits=letter6))
    loss = letter1_cross_entropy + letter2_cross_entropy + letter3_cross_entropy + letter4_cross_entropy + letter5_cross_entropy + letter6_cross_entropy

    optimizer = tf.train.AdamOptimizer(learning_rate=0.0001)
    train_op = slim.learning.create_train_op(loss,
                                             optimizer,
                                             summarize_gradients=True)
    tf.summary.scalar('loss', loss)

    predict_concat = tf.stack([
        tf.argmax(letter1, 1),
        tf.argmax(letter2, 1),
        tf.argmax(letter3, 1),
        tf.argmax(letter4, 1),
        tf.argmax(letter5, 1),
        tf.argmax(letter6, 1)
    ], 1)
    y_concat = tf.stack([
        tf.argmax(y1, 1),
        tf.argmax(y2, 1),
        tf.argmax(y3, 1),
        tf.argmax(y4, 1),
        tf.argmax(y5, 1),
        tf.argmax(y6, 1)
    ], 1)

    accuracy_internal = tf.cast(tf.equal(predict_concat, y_concat),
                                tf.float32),
    accuracy = tf.reduce_mean(tf.reduce_min(accuracy_internal, 2))
    tf.summary.scalar("accuracy", accuracy)
    merged = tf.summary.merge_all()
    accuracy_letter = tf.reduce_mean(tf.reshape(accuracy_internal, [-1]))

    length = tf.constant([1, 1, 1, 1, 1, 1], tf.int64)
    predicts = tf.map_fn(lambda pos: tf.substr(pickup, pos, length),
                         predict_concat, tf.string)
    predict_join = tf.reduce_join(predicts, axis=1)
    # tf.print(predict_join)

    initer = tf.global_variables_initializer()
    saver = tf.train.Saver()

    sess = tf.Session()
    sess.run(initer)
    ckpt = tf.train.get_checkpoint_state(model_path_path)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)

    train_writer = tf.summary.FileWriter(user_home + '/101logs')
    # saver.restore(sess, model_path)

    images = datasets.images  #get_train_dataset(dataset_path)
    labels = datasets.labels
    test_images = datasets.test_images
    test_labels = datasets.test_labels
    n = 1
    start_time = time.mktime(time.localtime())
    ret_status = False
    for j in range(15000):
        if ret_status:
            break
        for i in range(len(images)):
            if ret_status:
                break
            image = images[i]
            label = labels[i]
            batch_y_1 = [identity[pickup.find(label[0])]]
            batch_y_2 = [identity[pickup.find(label[1])]]
            batch_y_3 = [identity[pickup.find(label[2])]]
            batch_y_4 = [identity[pickup.find(label[3])]]
            batch_y_5 = [identity[pickup.find(label[4])]]
            batch_y_6 = [identity[pickup.find(label[5])]]

            sess.run(train_op,
                     feed_dict={
                         x: image,
                         y1: batch_y_1,
                         y2: batch_y_2,
                         y3: batch_y_3,
                         y4: batch_y_4,
                         y5: batch_y_5,
                         y6: batch_y_6
                     })

            n = n + 1

            if n % 100 == 0:
                saver.save(sess, model_path, global_step=n)
                # if i % 10 == 0:
                summary = sess.run(merged,
                                   feed_dict={
                                       x: image,
                                       y1: batch_y_1,
                                       y2: batch_y_2,
                                       y3: batch_y_3,
                                       y4: batch_y_4,
                                       y5: batch_y_5,
                                       y6: batch_y_6
                                   })
                train_writer.add_summary(summary, n)
                count = 0
                for k in range(len(test_images)):
                    image = test_images[k]
                    label = test_labels[k]
                    batch_y_1 = [identity[pickup.find(label[0])]]
                    batch_y_2 = [identity[pickup.find(label[1])]]
                    batch_y_3 = [identity[pickup.find(label[2])]]
                    batch_y_4 = [identity[pickup.find(label[3])]]
                    batch_y_5 = [identity[pickup.find(label[4])]]
                    batch_y_6 = [identity[pickup.find(label[5])]]

                    accuracy_letter_, accuracy_, predict_ = sess.run(
                        [accuracy_letter, accuracy, predict_join],
                        feed_dict={
                            x: image,
                            y1: batch_y_1,
                            y2: batch_y_2,
                            y3: batch_y_3,
                            y4: batch_y_4,
                            y5: batch_y_5,
                            y6: batch_y_6
                        })
                    if accuracy_letter_ == 0:
                        continue
                    print(image)
                    print(label)
                    print(accuracy_letter_)
                    print("accuracy is ====>%f" % accuracy_)
                    print(predict_)
                    if accuracy_ == 1.00:
                        count += 1

                accuracy_ = count / len(test_labels)
                print(accuracy_)
                if accuracy_ > 0.9:
                    ret_status = True

                print(j, n)
                end_time = time.mktime(time.localtime())
                print(end_time - start_time)
                start_time = end_time
                print("-" * 20)

    prediction_signature = tf.saved_model.signature_def_utils.predict_signature_def(
        {'image_path': x}, {'label': predict_join})

    legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')

    builder = tf.saved_model.builder.SavedModelBuilder(user_home + '/101model')
    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            prediction_signature,
        },
        legacy_init_op=legacy_init_op)
    builder.save()
示例#29
0
def main(_):
    batch_shape = [FLAGS.batch_size, FLAGS.image_height, FLAGS.image_width, 3]
    num_classes = 1001
    ensemble_type = FLAGS.ensemble_type

    tf.logging.set_verbosity(tf.logging.INFO)

    checkpoint_path_list = [
        FLAGS.checkpoint_path_inception_v1, FLAGS.checkpoint_path_inception_v2,
        FLAGS.checkpoint_path_inception_v3, FLAGS.checkpoint_path_inception_v4,
        FLAGS.checkpoint_path_inception_resnet_v2,
        FLAGS.checkpoint_path_resnet_v1_101,
        FLAGS.checkpoint_path_resnet_v1_152,
        FLAGS.checkpoint_path_resnet_v2_101,
        FLAGS.checkpoint_path_resnet_v2_152, FLAGS.checkpoint_path_vgg_16,
        FLAGS.checkpoint_path_vgg_19
    ]
    normalization_method = [
        'default', 'default', 'default', 'default', 'global', 'caffe_rgb',
        'caffe_rgb', 'default', 'default', 'caffe_rgb', 'caffe_rgb'
    ]
    pred_list = []
    for idx, checkpoint_path in enumerate(checkpoint_path_list, 1):
        with tf.Graph().as_default():
            if int(FLAGS.test_idx) == 20 and idx in [3]:
                continue
            if int(FLAGS.test_idx) in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
                                       ] and int(FLAGS.test_idx) != idx:
                continue
            # Prepare graph
            if idx in [1, 2, 6, 7, 10, 11]:
                _x_input = tf.placeholder(tf.float32, shape=batch_shape)
                x_input = tf.image.resize_images(_x_input, [224, 224])
            else:
                _x_input = tf.placeholder(tf.float32, shape=batch_shape)
                x_input = _x_input

            x_input = image_normalize(x_input, normalization_method[idx - 1])

            if idx == 1:
                with slim.arg_scope(inception.inception_v1_arg_scope()):
                    _, end_points = inception.inception_v1(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 2:
                with slim.arg_scope(inception.inception_v2_arg_scope()):
                    _, end_points = inception.inception_v2(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 3:
                with slim.arg_scope(inception.inception_v3_arg_scope()):
                    _, end_points = inception.inception_v3(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 4:
                with slim.arg_scope(inception.inception_v4_arg_scope()):
                    _, end_points = inception.inception_v4(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 5:
                with slim.arg_scope(inception.inception_resnet_v2_arg_scope()):
                    _, end_points = inception.inception_resnet_v2(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 6:
                with slim.arg_scope(resnet_v1.resnet_arg_scope()):
                    _, end_points = resnet_v1.resnet_v1_101(x_input,
                                                            num_classes=1000,
                                                            is_training=False)
            elif idx == 7:
                with slim.arg_scope(resnet_v1.resnet_arg_scope()):
                    _, end_points = resnet_v1.resnet_v1_152(x_input,
                                                            num_classes=1000,
                                                            is_training=False)
            elif idx == 8:
                with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                    _, end_points = resnet_v2.resnet_v2_101(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 9:
                with slim.arg_scope(resnet_v2.resnet_arg_scope()):
                    _, end_points = resnet_v2.resnet_v2_152(
                        x_input, num_classes=num_classes, is_training=False)
            elif idx == 10:
                with slim.arg_scope(vgg.vgg_arg_scope()):
                    _, end_points = vgg.vgg_16(x_input,
                                               num_classes=1000,
                                               is_training=False)
                    end_points['predictions'] = tf.nn.softmax(
                        end_points['vgg_16/fc8'])
            elif idx == 11:
                with slim.arg_scope(vgg.vgg_arg_scope()):
                    _, end_points = vgg.vgg_19(x_input,
                                               num_classes=1000,
                                               is_training=False)
                    end_points['predictions'] = tf.nn.softmax(
                        end_points['vgg_19/fc8'])

            #end_points = tf.reduce_mean([end_points1['Predictions'], end_points2['Predictions'], end_points3['Predictions'], end_points4['Predictions']], axis=0)

            #predicted_labels = tf.argmax(end_points, 1)

            # Run computation
            saver = tf.train.Saver(slim.get_model_variables())
            session_creator = tf.train.ChiefSessionCreator(
                scaffold=tf.train.Scaffold(saver=saver),
                checkpoint_filename_with_path=checkpoint_path,
                master=FLAGS.master)

            pred_in = []
            filenames_list = []
            with tf.train.MonitoredSession(
                    session_creator=session_creator) as sess:
                for filenames, images in load_images(FLAGS.input_dir,
                                                     batch_shape):
                    #if idx in [1,2,6,7,10,11]:
                    #  # 16x299x299x3
                    #  images = zoom(images, (1, 0.7491638795986622, 0.7491638795986622, 1), order=2)
                    filenames_list.extend(filenames)
                    end_points_dict = sess.run(end_points,
                                               feed_dict={_x_input: images})
                    if idx in [6, 7, 10, 11]:
                        end_points_dict['predictions'] = \
                                      np.concatenate([np.zeros([FLAGS.batch_size, 1]),
                                                      np.array(end_points_dict['predictions'].reshape(-1, 1000))],
                                                      axis=1)
                    try:
                        pred_in.extend(end_points_dict['Predictions'].reshape(
                            -1, num_classes))
                    except KeyError:
                        pred_in.extend(end_points_dict['predictions'].reshape(
                            -1, num_classes))
            pred_list.append(pred_in)

    if ensemble_type == 'mean':
        pred = np.mean(pred_list, axis=0)
        labels = np.argmax(
            pred, axis=1
        )  # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch
    elif ensemble_type == 'vote':
        pred = np.argmax(
            pred_list, axis=2
        )  # model_num X batch X class_num ==(np.mean)==> batch X class_num ==(np.argmax)==> batch
        labels = np.median(pred, axis=0)
    with tf.gfile.Open(FLAGS.output_file, 'w') as out_file:
        for filename, label in zip(filenames_list, labels):
            out_file.write('{0},{1}\n'.format(filename, label))
def GCN(inputs, num_classes, is_training):
    '''A TensorFlow implementation of GCN model based on 
	   "Large Kernel Matters -- Improve Semantic Segmentation by Global Convolutional Network"
	
	Args:
		inputs: A 4-D tensor with dimensions [batch_size, height, width, channels]
		num_classes: Integer, the total number of categories in the dataset
		is_training : Bool, whether to updates the running means and variances during the training.
	Returns:
		A score map with dimensions [batch_size, height, width, num_classes]

	'''
    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        net, end_points = resnet_v2.resnet_v2_101(inputs,
                                                  num_classes=None,
                                                  is_training=is_training,
                                                  global_pool=False,
                                                  output_stride=None,
                                                  reuse=None,
                                                  scope='resnet_v2_101')
    block1 = end_points[
        "resnet_v2_101/block1/unit_2/bottleneck_v2"]  # (56, 56, 256)
    block2 = end_points[
        "resnet_v2_101/block2/unit_3/bottleneck_v2"]  # (28, 28, 512)
    block3 = end_points[
        "resnet_v2_101/block3/unit_22/bottleneck_v2"]  # (14, 14, 1024)
    block4 = net  # (7, 7, 2048)

    with tf.variable_scope("gcn") as sc:
        down5 = GlobalConvBlock(block4, num_classes=num_classes)
        down5 = BoundaryRefinementBlock(down5,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down5 = slim.conv2d_transpose(down5,
                                      num_classes,
                                      kernel_size=[4, 4],
                                      stride=2,
                                      activation_fn=None)  # (14, 14, 21)

        down4 = GlobalConvBlock(block3, num_classes=num_classes)
        down4 = BoundaryRefinementBlock(down4,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down4 = tf.add(down4, down5)
        down4 = BoundaryRefinementBlock(down4,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down4 = slim.conv2d_transpose(down4,
                                      num_classes,
                                      kernel_size=[4, 4],
                                      stride=2,
                                      activation_fn=None)  # (28, 28, 21)

        down3 = GlobalConvBlock(block2, num_classes=num_classes)
        down3 = BoundaryRefinementBlock(down3,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down3 = tf.add(down3, down4)
        down3 = BoundaryRefinementBlock(down3,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down3 = slim.conv2d_transpose(down3,
                                      num_classes,
                                      kernel_size=[4, 4],
                                      stride=2,
                                      activation_fn=None)  # (56, 56, 21)

        down2 = GlobalConvBlock(block1, num_classes=num_classes)
        down2 = BoundaryRefinementBlock(down2,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down2 = tf.add(down2, down3)
        down2 = BoundaryRefinementBlock(down2,
                                        num_classes=num_classes,
                                        kernel_size=[3, 3])
        down2 = slim.conv2d_transpose(down2,
                                      num_classes,
                                      kernel_size=[4, 4],
                                      stride=2,
                                      activation_fn=None)  # (112, 112, 21)

        output = BoundaryRefinementBlock(down2,
                                         num_classes=num_classes,
                                         kernel_size=[3, 3])
        output = slim.conv2d_transpose(output,
                                       num_classes,
                                       kernel_size=[4, 4],
                                       stride=2,
                                       activation_fn=None)  # (224, 224, 21)
        output = BoundaryRefinementBlock(output,
                                         num_classes=num_classes,
                                         kernel_size=[3, 3])
        return output