示例#1
0
	def __init__(self,is_training):
		
		self.input_image = tf.placeholder(dtype=tf.float32,shape=[None,224,224,3],name='input_image')

		logits, end_points = resnet_v2.resnet_v2_50(self.input_image, num_classes=100, is_training=True)

		self.feature = logits

		self.output = tf.sigmoid(logits)
示例#2
0
 def arch_resnet_v2_50(self, X, num_classes, dropout_keep_prob=0.8, is_train=False):
     arg_scope = resnet_arg_scope()
     with slim.arg_scope(arg_scope):
         net_vis, end_points = resnet_v2_50(X, is_training=is_train)
     with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'):
         with tf.variable_scope('Logits_out'):
             net = slim.conv2d(net_vis, 1000, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out0')
             net = slim.dropout(net, dropout_keep_prob, scope='Dropout_1b_out0')
             net = slim.conv2d(net, 200, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out1')
             net = slim.dropout(net, dropout_keep_prob, scope='Dropout_1b_out1')
             net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, normalizer_fn=None, scope='Logits_out2')
             net = tf.squeeze(net,[1,2], name='SpatialSqueeze')
     return net, net_vis
示例#3
0
    def __init__(self, is_training):

        self.input_image = tf.placeholder(dtype=tf.float32,
                                          shape=[None, 224, 224, 3],
                                          name='input_image')

        logits, end_points = resnet_v2.resnet_v2_50(self.input_image,
                                                    num_classes=100,
                                                    is_training=True)

        self.feature = logits

        self.output = tf.sigmoid(logits)
示例#4
0
def build_deeplabv3(inputs, num_classes, preset_model='DeepLabV3-Res50', weight_decay=1e-5, is_training=True, pretrained_dir="models"):
    """
    Builds the DeepLabV3 model.

    Arguments:
      inputs: The input tensor=
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction
      num_classes: Number of classes

    Returns:
      DeepLabV3 model
    """

    if preset_model == 'DeepLabV3-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope='resnet_v2_50'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'DeepLabV3-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope='resnet_v2_101'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'DeepLabV3-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope='resnet_v2_152'
            # DeepLabV3 requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model))


    label_size = tf.shape(inputs)[1:3]

    net = AtrousSpatialPyramidPoolingModule(end_points['pool4'])

    net = Upsampling(net, label_size)

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
示例#5
0
    def arch_resnet_v2_50_rnn_attention(self,
                                        X,
                                        num_classes,
                                        dropout_keep_prob=0.8,
                                        is_train=False):
        rnn_size = 256
        num_layers = 2
        attention_size = 64
        arg_scope = resnet_arg_scope()
        with slim.arg_scope(arg_scope):
            net_vis, end_points = resnet_v2_50(X, is_training=is_train)
        with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                            stride=1,
                            padding='SAME'):
            with tf.variable_scope('Logits_out'):
                orig_shape = net_vis.get_shape().as_list()
                net = tf.reshape(
                    net_vis,
                    [-1, orig_shape[1] * orig_shape[2], orig_shape[3]])

                def gru_cell():
                    return tf.contrib.rnn.GRUCell(run_size)

                def lstm_cell():
                    return tf.contrib.rnn.LSTMCell(rnn_size)

                def attn_cell():
                    return tf.contrib.rnn.DropoutWrapper(
                        lstm_cell(), output_keep_prob=dropout_keep_prob)

                stack = tf.contrib.rnn.MultiRNNCell(
                    [lstm_cell() for _ in range(0, num_layers)],
                    state_is_tuple=True)
                net, _ = tf.nn.dynamic_rnn(stack, net, dtype=tf.float32)
                net = tf.transpose(net, (1, 0, 2))

                net = attention(net, attention_size, True)
                #
                #net = slim.fully_connected(net[-1], 256, activation_fn=tf.nn.relu, scope='Logits_out0')
                net = slim.fully_connected(net,
                                           num_classes,
                                           activation_fn=None,
                                           scope='Logits_out1')
        return net, net_vis
示例#6
0
	def __init__(self,is_training):
		
		self.input_image = tf.placeholder(dtype=tf.float32,shape=[None,224,224,3],name='input_image')
		
		self.input_label = tf.placeholder(dtype=tf.float32,shape=[None,100],name='input_label')

		logits, end_points = resnet_v2.resnet_v2_50(self.input_image, num_classes=100, is_training=True)

		self.ce_loss = tf.reduce_mean(tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.input_label,logits=logits),1))

		tf.summary.scalar('ce_loss',self.ce_loss)

		slim.losses.add_loss(self.ce_loss)		

		self.l2_loss = tf.add_n(slim.losses.get_regularization_losses())

		tf.summary.scalar('l2_loss',self.l2_loss)

		self.total_loss = slim.losses.get_total_loss()

		tf.summary.scalar('total_loss',self.total_loss)

		self.output = tf.sigmoid(logits)
示例#7
0
def build_encoder(input_shape,
                  encoder_name,
                  encoder_weights=None,
                  weight_decay=1e-4,
                  kernel_initializer="he_normal",
                  bn_epsilon=1e-3,
                  bn_momentum=0.99):
    """ the main api to build a encoder.
    :param input_shape: tuple, i.e., (height, width. channel).
    :param encoder_name: string, name of the encoder, refer to 'scope_table' above.
    :param encoder_weights: string, path of the weight, default None.
    :param weight_decay: float, default 1e-4.
    :param kernel_initializer: string, default "he_normal".
    :param bn_epsilon: float, default 1e-3.
    :param bn_momentum: float, default 0.99.

    :return: a Keras Model instance.
    """
    encoder_name = encoder_name.lower()

    if encoder_name=="resnet_v2_50":
        encoder = resnet_v2_50(input_shape, weight_decay=weight_decay, kernel_initializer=kernel_initializer, bn_epsilon=bn_epsilon, bn_momentum=bn_momentum)
    elif encoder_name=="resnet_v2_50_nosam":
        encoder = resnet_v2_50_nosam(input_shape, weight_decay=weight_decay, kernel_initializer=kernel_initializer,bn_epsilon=bn_epsilon, bn_momentum=bn_momentum)

    elif encoder_name == "xception_41":
        encoder = xception_41(input_shape, weight_decay=weight_decay, kernel_initializer=kernel_initializer, bn_epsilon=bn_epsilon, bn_momentum=bn_momentum)
    elif encoder_name == "xception_41_nosam":
        encoder = xception_41_nosam(input_shape, weight_decay=weight_decay, kernel_initializer=kernel_initializer, bn_epsilon=bn_epsilon, bn_momentum=bn_momentum)

    else:
        raise ValueError("Invalid encoder name")

    if encoder_weights is not None and os.path.exists(encoder_weights):
        encoder.load_weights(encoder_weights)

    return encoder
示例#8
0
def generate_features(predict_flag, gallery_flag):
    print('generate_features(%s, %s)' % (predict_flag, gallery_flag))

    with tf.Graph().as_default():
        # define placeholder
        train_mode = tf.placeholder(tf.bool, name='train_mode')
        input_batch = tf.placeholder(tf.float32,
                                     [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3],
                                     'input_batch')

        # define model
        resnet_reid = ResnetReid()
        with slim.arg_scope(resnet_arg_scope()):
            # input_batch: [batch, height, width, 3] values scaled [0.0, 1.0], dtype = tf.float32
            resnet_avg_pool, end_points = resnet_v2_50(input_batch,
                                                       is_training=False,
                                                       global_pool=True)
        resnet_reid.build(resnet_avg_pool, train_mode)

        # define sess
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())

            # load checkpoint
            saver = tf.train.Saver()
            ckpt = tf.train.get_checkpoint_state(
                train_flags.output_check_point_path)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('load checkpoint')

            # define parameter
            if gallery_flag and predict_flag:
                features_num = train_flags.predict_gallery_num
                features_npy_name = 'predict_gallery_features.npy'
                get_image_batch = resnet_reid.get_predict_image_batch
                file_title = 'predict'
            elif gallery_flag and (not predict_flag):
                features_num = train_flags.train_200_gallery_num
                features_npy_name = 'train_200_gallery_features.npy'
                get_image_batch = resnet_reid.get_test_image_batch
                file_title = 'train_200'
            elif (not gallery_flag) and predict_flag:
                features_num = train_flags.predict_probe_num
                features_npy_name = 'predict_probe_features.npy'
                get_image_batch = resnet_reid.get_predict_image_batch
                file_title = 'predict'
            else:
                features_num = train_flags.train_200_probe_num
                features_npy_name = 'train_200_probe_features.npy'
                get_image_batch = resnet_reid.get_test_image_batch
                file_title = 'train_200'

            print('start generate features')
            # get feature batch
            features = np.zeros((features_num, train_flags.output_feature_dim),
                                dtype=float)
            batch_len = train_flags.test_batch_size
            end_len = features_num % batch_len
            for batch_index in range(0, features_num - end_len, batch_len):
                batch = get_image_batch(batch_index, batch_len,
                                        train_flags.id_image_path, file_title,
                                        gallery_flag)
                batch_feature = sess.run(resnet_reid.output,
                                         feed_dict={
                                             input_batch: batch,
                                             train_mode: False
                                         })
                features[batch_index:batch_index +
                         batch_len, :] = batch_feature
                print('batch_index: ' + str(batch_index) + '-' +
                      str(batch_index + batch_len - 1))
            if end_len != 0:
                batch_index += batch_len
                batch = get_image_batch(batch_index, batch_len,
                                        train_flags.id_image_path, file_title,
                                        gallery_flag)
                batch_feature = sess.run(resnet_reid.output,
                                         feed_dict={
                                             input_batch: batch,
                                             train_mode: False
                                         })
                features[batch_index:batch_index +
                         end_len, :] = batch_feature[:end_len]
                print('batch_index: ' + str(batch_index) + '-' +
                      str(batch_index + end_len - 1))

            # save feature
            features_npy_path = os.path.join(
                train_flags.output_test_features_path, features_npy_name)
            np.save(features_npy_path, features)
    def build_model(self):
        """Builds the model.

		Inputs:
			self.images
			self.target_labels
			self.model_config.train_base_network
			self.model_config.mode

		Outputs:
			self.sigmoid_result (inference)
			self.cross_entropy (train, eval)
			self.total_loss
		"""

        # build resnet_50
        arg_scope = self.resnet_arg_scope()
        is_base_network_training = self.model_config.train_base_network and (
            self.model_config.mode == "train")
        with slim.arg_scope(arg_scope):
            net, end_points = resnet_v2_50(
                self.images,
                is_training=is_base_network_training,
                global_pool=False)

        with tf.variable_scope('my_sub_network'):
            if self.model_config.use_regularizer:
                weights_regularizer = slim.l2_regularizer(
                    self.model_config.weight_decay)
            else:
                weights_regularizer = None

            net = slim.conv2d(net,
                              self.model_config.label_count, [1, 1],
                              weights_regularizer=weights_regularizer,
                              scope="my_conv_1")

            net = slim.conv2d(net,
                              self.model_config.label_count, [1, 1],
                              weights_regularizer=weights_regularizer,
                              scope="my_conv_2")

            net = slim.conv2d(net,
                              self.model_config.label_count, [7, 7],
                              weights_regularizer=weights_regularizer,
                              padding="VALID",
                              activation_fn=None,
                              scope="my_conv_3")

            logits = tf.squeeze(net, [1, 2], name="logits")

        self.sigmoid_result = tf.sigmoid(logits, name="sigmoid_result")

        if self.model_config.mode == "inference":
            return

        tf.logging.info(
            "Before:--- GraphKeys.LOSSES len: {}; GraphKeys.REGULARIZATION_LOSSES len: {}"
            .format(len(tf.losses.get_losses()),
                    len(tf.losses.get_regularization_losses())))

        # By default, all the losses in 'tf.losses' are collected into the GraphKeys.LOSSES collection.
        sigmoid_cross_entropy_loss = tf.losses.sigmoid_cross_entropy(
            multi_class_labels=self.target_labels,
            logits=logits,
            scope="sigmoid_cross_entropy")

        # get_total_loss get both GraphKeys.LOSSES and GraphKeys.REGULARIZATION_LOSSES.
        total_loss = tf.losses.get_total_loss()

        tf.logging.info(
            "After:--- GraphKeys.LOSSES len: {}; GraphKeys.REGULARIZATION_LOSSES len: {}"
            .format(len(tf.losses.get_losses()),
                    len(tf.losses.get_regularization_losses())))

        # Add summaries.
        tf.summary.scalar("losses/sigmoid_cross_entropy_loss",
                          sigmoid_cross_entropy_loss)
        tf.summary.scalar("losses/total_loss", total_loss)

        self.sigmoid_cross_entropy_loss = sigmoid_cross_entropy_loss
        self.total_loss = total_loss
示例#10
0
def train():
    tf.logging.set_verbosity(
        tf.logging.INFO)  # Set the verbosity to INFO level

    initial_learning_rate = 0.0002
    learning_rate_decay_factor = 0.7
    decay_steps = 40000

    checkpoint_file = tf.train.latest_checkpoint(log_root)
    print(checkpoint_file)

    images, one_hot_labels = get_input(20, FLAGS.batch_size, hand=FLAGS.hand)

    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        logits, end_points = resnet_v2_50(images, 20)
        print logits, end_points

    variables_to_restore = slim.get_variables_to_restore()

    loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                           logits=logits)
    total_loss = tf.losses.get_total_loss(
    )  # obtain the regularization losses as well

    # Create the global step for monitoring the learning_rate and training.
    global_step_1 = tf.contrib.framework.get_or_create_global_step()

    # Define your exponentially decaying learning rate
    lr = tf.train.exponential_decay(learning_rate=initial_learning_rate,
                                    global_step=global_step_1,
                                    decay_steps=decay_steps,
                                    decay_rate=learning_rate_decay_factor,
                                    staircase=True)

    # Now we can define the optimizer that takes on the learning rate
    optimizer = tf.train.AdamOptimizer(learning_rate=lr)
    #variables_to_train = [v for v in variables_to_restore if 'resnet_v2_50/logits' in v.name]
    #print variables_to_train
    #raw_input()

    # Create the train_op.
    train_op = slim.learning.create_train_op(
        total_loss,
        optimizer,
    )  # variables_to_train = variables_to_train)

    # State the metrics that you want to predict. We get a predictions that is not one_hot_encoded.
    predictions = tf.argmax(end_points['predictions'], 1)
    probabilities = end_points['predictions']
    labels = tf.argmax(one_hot_labels, 1)
    precision = tf.reduce_mean(tf.to_float(tf.equal(predictions, labels)))

    summary_writer = tf.summary.FileWriter(train_dir)
    summary_op = tf.summary.merge([
        tf.summary.scalar('costs/cost', total_loss),
        tf.summary.scalar('Precision', precision),
        tf.summary.scalar('learning_rate', lr)
    ])

    saver = tf.train.Saver(max_to_keep=0)
    sess = tf.Session()
    saver.restore(sess, tf.train.latest_checkpoint(log_root))

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)

    var = [
        v for v in tf.model_variables()
        if "50/conv1" in v.name or "logits" in v.name
    ]
    var_list = sess.run(var)
    for v1, v in zip(var, var_list):
        print v1.name, v.shape, np.min(v), np.max(v), np.mean(v), np.std(v)

    saver.save(sess,
               os.path.join(log_root, "model.ckpt"),
               global_step=global_step_1)
    checkpoint_time = time.time()

    while True:
        _, summary, g_step, loss, accuracy, l_rate = sess.run(
            [train_op, summary_op, global_step_1, total_loss, precision, lr])

        if g_step % 10 == 0:
            tf.logging.info(
                'Global_step_1: %d, loss: %f, precision: %.2f, lr: %f' %
                (g_step, loss, accuracy, l_rate))

        if time.time() - checkpoint_time > save_checkpoint_secs:
            saver.save(sess,
                       os.path.join(log_root, "model.ckpt"),
                       global_step=global_step_1)
            checkpoint_time = time.time()

        if g_step % save_summaries_steps == 0:
            summary_writer.add_summary(summary, g_step)
            summary_writer.flush()
示例#11
0
def evaluate():
    """Eval loop."""
    images, one_hot_labels = get_input(20, FLAGS.batch_size, FLAGS.mode,
                                       FLAGS.hand)
    gesture_list = os.listdir(
        "/s/red/a/nobackup/cwc/hands/rgb_hands_new_frames/s_01")
    gesture_list.sort()
    print gesture_list

    with slim.arg_scope(resnet_utils.resnet_arg_scope()):
        logits_tensor, end_points = resnet_v2_50(images, 20, False)

    saver = tf.train.Saver()
    summary_writer = tf.summary.FileWriter(eval_dir)

    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    tf.train.start_queue_runners(sess)

    best_precision = 0.0

    predictions_tensor = tf.argmax(end_points['predictions'], 1)
    probabilities_tensor = end_points['predictions']

    labels = tf.argmax(one_hot_labels, 1)
    #precision = tf.reduce_mean(tf.to_float(tf.equal(predictions_tensor, labels)))

    checkpoint_list = os.listdir(log_root)
    checkpoint_list = [
        c.replace(".index", "") for c in checkpoint_list if ".index" in c
    ]
    print checkpoint_list

    precision_list = []

    #while True:
    for c in checkpoint_list:
        try:
            ckpt_state = tf.train.get_checkpoint_state(log_root)
        except tf.errors.OutOfRangeError as e:
            tf.logging.error('Cannot restore checkpoint: %s', e)
            continue
        if not (ckpt_state and ckpt_state.model_checkpoint_path):
            tf.logging.info('No model to eval yet at %s', log_root)
            continue
        ckpt_state.model_checkpoint_path = os.path.join(log_root, c)
        tf.logging.info('Loading checkpoint %s',
                        ckpt_state.model_checkpoint_path)
        saver.restore(sess, ckpt_state.model_checkpoint_path)
        tf.logging.info("Checkpoint restoration done")
        train_step = int(ckpt_state.model_checkpoint_path.split("-")[-1])

        total_prediction, correct_prediction = 0, 0

        gt_list = []
        pred_list = []

        for sample in range(212):
            (predictions,
             truth) = sess.run([probabilities_tensor, one_hot_labels])

            gt_list.append(truth)
            pred_list.append(predictions)

            truth = np.argmax(truth, axis=1)
            predictions = np.argmax(predictions, axis=1)
            correct_prediction += np.sum(truth == predictions)
            total_prediction += predictions.shape[0]

            if sample % 40 == 0:
                print sample, correct_prediction, total_prediction

        gt = np.squeeze(np.vstack(gt_list))
        pred = np.squeeze(np.vstack(pred_list))

        precision = np.mean(np.argmax(gt, axis=1) == np.argmax(pred, axis=1))
        precision_list.append(precision)
        best_precision = max(precision, best_precision)
        print gt.shape, pred.shape, precision, best_precision
        gt = np.argmax(gt, axis=1)
        pred = np.argmax(pred, axis=1)
        '''cm = np.float(confusion_matrix(gt,pred))
	
	cm /= np.sum(cm,axis=0)
	plt.imshow(cm,interpolation='nearest')
	plt.xticks(range(20),gesture_list,rotation=90)
	plt.yticks(range(20),gesture_list)
	plt.show()
        precision_summ = tf.Summary()
        precision_summ.value.add(tag='Precision', simple_value=precision)
        summary_writer.add_summary(precision_summ, train_step)
        best_precision_summ = tf.Summary()
        best_precision_summ.value.add(tag='Best Precision', simple_value=best_precision)
        summary_writer.add_summary(best_precision_summ, train_step)'''

        tf.logging.info('precision: %.3f, best precision: %.3f' %
                        (precision, best_precision))
        summary_writer.flush()

        #time.sleep(60)
    print "Max Precision : ", np.max(
        precision_list), " Ckpt: ", checkpoint_list[np.argmax(precision_list)]
示例#12
0
def build_pspnet(inputs,
                 label_size,
                 num_classes,
                 preset_model='PSPNet-Res50',
                 pooling_type="MAX",
                 weight_decay=1e-5,
                 upscaling_method="conv",
                 is_training=True,
                 pretrained_dir="models"):
    """
    Builds the PSPNet model. 

    Arguments:
      inputs: The input tensor
      label_size: Size of the final label tensor. We need to know this for proper upscaling 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes
      pooling_type: Max or Average pooling

    Returns:
      PSPNet model
    """

    if preset_model == 'PSPNet-Res50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope = 'resnet_v2_50'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'PSPNet-Res101':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope = 'resnet_v2_101'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'PSPNet-Res152':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope = 'resnet_v2_152'
            # PSPNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError(
            "Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152"
            % (preset_model))

    feature_map_shape = [int(x / 8.0) for x in label_size]
    print(feature_map_shape)
    psp = PyramidPoolingModule(end_points['pool3'],
                               feature_map_shape=feature_map_shape,
                               pooling_type=pooling_type)

    net = slim.conv2d(psp, 512, [3, 3], activation_fn=None)
    net = slim.batch_norm(net, fused=True)
    net = tf.nn.relu(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, label_size)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
示例#13
0
def build_gcn(inputs, num_classes, preset_model='GCN-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"):
    """
    Builds the GCN model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      GCN model
    """

    inputs = mean_image_subtraction(inputs)

    if preset_model == 'GCN-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            resnet_scope = 'resnet_v2_50'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'GCN-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            resnet_scope = 'resnet_v2_101'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'GCN-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            resnet_scope = 'resnet_v2_152'
            # GCN requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
    	raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model))

    


    res = [end_points['pool5'], end_points['pool4'],
         end_points['pool3'], end_points['pool2']]

    down_5 = GlobalConvBlock(res[0], n_filters=21, size=3)
    down_5 = BoundaryRefinementBlock(down_5, n_filters=21, kernel_size=[3, 3])
    down_5 = ConvUpscaleBlock(down_5, n_filters=21, kernel_size=[3, 3], scale=2)

    down_4 = GlobalConvBlock(res[1], n_filters=21, size=3)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = tf.add(down_4, down_5)
    down_4 = BoundaryRefinementBlock(down_4, n_filters=21, kernel_size=[3, 3])
    down_4 = ConvUpscaleBlock(down_4, n_filters=21, kernel_size=[3, 3], scale=2)

    down_3 = GlobalConvBlock(res[2], n_filters=21, size=3)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = tf.add(down_3, down_4)
    down_3 = BoundaryRefinementBlock(down_3, n_filters=21, kernel_size=[3, 3])
    down_3 = ConvUpscaleBlock(down_3, n_filters=21, kernel_size=[3, 3], scale=2)

    down_2 = GlobalConvBlock(res[3], n_filters=21, size=3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = tf.add(down_2, down_3)
    down_2 = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    down_2 = ConvUpscaleBlock(down_2, n_filters=21, kernel_size=[3, 3], scale=2)

    net = BoundaryRefinementBlock(down_2, n_filters=21, kernel_size=[3, 3])
    net = ConvUpscaleBlock(net, n_filters=21, kernel_size=[3, 3], scale=2)
    net = BoundaryRefinementBlock(net, n_filters=21, kernel_size=[3, 3])

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
示例#14
0
                                   num_classes=num_classes,
                                   shuffle=False)

    # create an reinitializable iterator given the dataset structure
    iterator_test = Iterator.from_structure(test_data.data.output_types,
                                            test_data.data.output_shapes)

    images_batch_test, labels_batch_test = iterator_test.get_next()

# Ops for initializing the two different iterators
test_init_op = iterator_test.make_initializer(test_data.data)

images_batch = tf.concat([images_batch_test, images_batch_test], axis=0)
y = tf.concat([labels_batch_test, labels_batch_test], axis=0)
with slim.arg_scope(resnet.resnet_arg_scope()):
    Gap5, CONV5, net, _ = resnet.resnet_v2_50(images_batch, is_training=False)
net = tf.nn.dropout(net, 1.0)
net = slim.conv2d(net,
                  num_classes, [1, 1],
                  activation_fn=None,
                  normalizer_fn=None,
                  scope='logits')
score = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
SCORE = tf.reshape(score, [2 * batch_size, -1])

# Evaluation op: Accuracy of the model
with tf.name_scope("accuracy"):
    correct_pred = tf.equal(
        tf.argmax(tf.slice(SCORE, [0, 0], [batch_size, num_classes]), 1),
        tf.argmax(tf.slice(y, [0, 0], [batch_size, num_classes]), 1))
    accuracy = tf.reduce_sum(tf.cast(correct_pred, tf.float32))
示例#15
0
def test_resnet(device, num_classes, num_layers, dataset, normalization, checkpoint):
    """
    Computes accuracy for the test dataset
    Inputs: device - gpu device
            num_classes - number of output classes 
            num_layers - number of layers selected for ResNet 
            dataset - dataset selected, options are val and test
            normalization - normalization used options are standard z-score normalization or unet normalization
            checkpoint - file where graph model weights are stored
    Output: None
    """
    print dataset
    os.environ['CUDA_VISIBLE_DEVICES'] = str(device) # use nvidia-smi to see available options '0' means first gpu
    config = GleasonConfig() # loads pathology configuration 

    if dataset == 'val':  
        print "Using val..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/val.tfrecords')
    elif dataset == 'test':
        print "Using test..." 
        config.test_fn = os.path.join(config.main_dir, 'tfrecords/test.tfrecords')
    else:
        config.test_fn = None

    config.test_checkpoint = checkpoint
    print "Loading checkpoint: {0}".format(checkpoint)

    if int(num_classes) == 2:
        print "Converting to Output Shape to Binary..."
        config.output_shape = 2
    elif int(num_classes) == 4:
        config.output_shape = 4
    else:
        raise Exception('Invalid number of classes!')

    batch_size = 128
    # loading test data
    test_meta = np.load(tfrecord2metafilename(config.test_fn))
    print 'Using {0} tfrecords: {1} | {2} images'.format(dataset, config.test_fn, len(test_meta['labels']))
    test_filename_queue = tf.train.string_input_producer([config.test_fn] , num_epochs=1) # 1 epoch, passing through the
                                                                                            # the dataset once

    test_img, test_t_l, test_f_p  = read_and_decode(filename_queue = test_filename_queue,
                                           img_dims = config.input_image_size,
                                           model_dims = config.model_image_size,
                                           size_of_batch = batch_size,
                                           augmentations_dic = config.val_augmentations_dic,
                                           num_of_threads = 4,
                                           shuffle = False)
    if int(num_classes) == 2:
        print "Converting labels to Binary..."
        test_t_l = tf.clip_by_value(test_t_l, 0, 1)

    if num_layers == "50":
        print "Loading Resnet 50..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_50(inputs = test_img, 
                                                       num_classes = config.output_shape,
                                                       is_training = False) 
    elif num_layers == "101":
        print "Loading Resnet 101..."
        if normalization == "standard":
            print "Using standard normalization..."
            test_img = normalize(test_img)
        elif normalization == "unet":
            print "Using unet normalization..."
            test_img,_ = unet_preprocess.unet(test_img,
                                           is_training = False,
                                           is_batch_norm = False,
                                           num_channels = 1)
        else:
            raise Exception('Not known normalization! Options are: standard and unet.')
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay = config.l2_reg)):
            test_target_logits, _ = resnet_v2.resnet_v2_101(inputs = test_img, 
                                                           num_classes = config.output_shape,
                                                           is_training = False)
    else:
        raise Expection("Wrong number of layers! allowed numbers are 50 and 101.")
        
    target_prob = tf.nn.softmax(test_target_logits)
    prob_and_label_files = [target_prob,  test_t_l, test_f_p]
    restorer = tf.train.Saver()
    print "Variables stored in checkpoint:"
    print_tensors_in_checkpoint_file(file_name=config.test_checkpoint, tensor_name='', all_tensors='')
    
    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
      
        sess.run(tf.group(tf.global_variables_initializer(),tf.local_variables_initializer()))

        restorer.restore(sess, config.test_checkpoint)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        all_predictions_target = []
        all_predictions_t_n = []
        all_labels = []
        all_files = []

        batch_num = 1
        try:
            print "Total number of batch iterations needed: {0}".format(int(math.ceil(len(test_meta['labels'])/batch_size)))
            while not coord.should_stop():
               
                np_prob_and_label_files = sess.run(prob_and_label_files)

                target_probs = np_prob_and_label_files[0]
                labels = np_prob_and_label_files[1] 
                files = np_prob_and_label_files[2]
                
                all_labels += list(labels) 
                all_files += list(files)
                all_predictions_target += list(np.argmax(target_probs, axis=1)) 
                print "evaluating current batch number: {0}".format(batch_num)
                batch_num +=1
         
        except tf.errors.OutOfRangeError:
            print "{0} accuracy: {1:.2f}".format(dataset, (metrics.accuracy_score(all_labels, all_predictions_target)*100))
            if int(num_classes) == 2:
                print "{0} precision: {1:.2f}".format(dataset, (metrics.precision_score(all_labels, all_predictions_target)*100))
                print "{0} recall: {1:.2f}".format(dataset, (metrics.recall_score(all_labels, all_predictions_target)*100))
            print 
        finally:
            coord.request_stop()  
        coord.join(threads) 
def build_refinenet(inputs,
                    num_classes,
                    preset_model='RefineNet-Res101',
                    weight_decay=1e-5,
                    is_training=True,
                    upscaling_method="bilinear",
                    pretrained_dir="models"):
    """
    Builds the RefineNet model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      RefineNet model
    """

    inputs = mean_image_subtraction(inputs)

    if preset_model == 'RefineNet-Res50':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(
                inputs, is_training=is_training, scope='resnet_v2_50')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'),
                slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'RefineNet-Res101':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(
                inputs, is_training=is_training, scope='resnet_v2_101')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'),
                slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'RefineNet-Res152':
        with slim.arg_scope(
                resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(
                inputs, is_training=is_training, scope='resnet_v2_152')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(
                os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'),
                slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError(
            "Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152"
            % (preset_model))

    f = [
        end_points['pool5'], end_points['pool4'], end_points['pool3'],
        end_points['pool2']
    ]

    g = [None, None, None, None]
    h = [None, None, None, None]

    for i in range(4):
        h[i] = slim.conv2d(f[i], 256, 1)

    g[0] = RefineBlock(high_inputs=None, low_inputs=h[0])
    g[1] = RefineBlock(g[0], h[1])
    g[2] = RefineBlock(g[1], h[2])
    g[3] = RefineBlock(g[2], h[3])

    # g[3]=Upsampling(g[3],scale=4)

    net = g[3]

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 256, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 256)
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, scale=4)

    net = slim.conv2d(net,
                      num_classes, [1, 1],
                      activation_fn=None,
                      scope='logits')

    return net, init_fn
示例#17
0
def train(retain_flag=True, start_step=0):
    print('train(retain_flag=%s, start_step=%s)' % (retain_flag, start_step))

    with tf.Graph().as_default():
        # define placeholder
        train_mode = tf.placeholder(tf.bool, name='train_mode')
        input_batch = tf.placeholder(tf.float32,
                                     [None, IMAGE_HEIGHT, IMAGE_WIDTH, 3],
                                     'input_batch')

        # define model
        resnet_reid = ResnetReid(train_flags.dropout)
        input_batch = tf.cond(
            train_mode, lambda: resnet_reid.process_image(
                input_batch, train_flags.train_batch_size),
            lambda: input_batch, 'process_image')
        with slim.arg_scope(resnet_arg_scope()):
            # input_batch: [batch, height, width, 3] values scaled [0.0, 1.0], dtype = tf.float32
            resnet_avg_pool, end_points = resnet_v2_50(input_batch,
                                                       is_training=False,
                                                       global_pool=True)
        # Define the scopes that you want to exclude for restoration
        variables_to_restore = slim.get_variables_to_restore(
            exclude=['beta2_power'])
        resnet_reid.build(resnet_avg_pool, train_mode)

        # define loss
        loss = tf.cond(train_mode, lambda: _model_loss(resnet_reid),
                       lambda: tf.constant([0.0], dtype=tf.float32),
                       'chose_loss')

        # Create an optimizer that performs gradient descent.
        global_step = get_or_create_global_step()
        lr = tf.train.exponential_decay(train_flags.initial_learning_rate,
                                        global_step,
                                        train_flags.decay_steps,
                                        train_flags.decay_rate,
                                        staircase=True)
        vars_to_optimize = [v for v in tf.trainable_variables()]
        # vars_to_optimize = [v for v in tf.trainable_variables() if ('add' in v.name)]
        print('\nvariables to optimize')
        for v in vars_to_optimize:
            print(v.name, v.get_shape().as_list())
            tf.summary.histogram(v.name, v)
        opt = tf.train.AdamOptimizer(lr)

        grads = opt.compute_gradients(loss, var_list=vars_to_optimize)
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
        train_op = tf.group(apply_gradient_op)

        # define sess
        sess = tf.Session()
        sess.run(tf.global_variables_initializer())

        # define summary and saver
        summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(train_flags.output_summary_path,
                                               graph=sess.graph)
        saver = tf.train.Saver(max_to_keep=8)

        # retrain or continue
        if retain_flag:
            saver_resnet = tf.train.Saver(variables_to_restore)
            saver_resnet.restore(sess, train_flags.resnet_checkpoint_file)
        else:
            # load checkpoint
            ckpt = tf.train.get_checkpoint_state(
                train_flags.output_check_point_path)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('load checkpoint')

        print('start training')
        for step in range(start_step, train_flags.max_step):
            # input image
            batch = resnet_reid.get_train_image_batch_direct(
                train_flags.id_path_train_path, train_flags.return_id_num,
                train_flags.image_num_every_id)
            # if step == 0: # load mode
            #     change_file_flag = True
            # else:
            #     change_file_flag = False
            # batch = resnet_reid.get_train_image_batch(train_flags.id_image_path, train_flags.id_image_train_num,
            #                                           train_flags.return_id_num, train_flags.image_num_every_id,
            #                                           change_file=change_file_flag)

            # start run
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss],
                                     feed_dict={
                                         input_batch: batch,
                                         train_mode: True
                                     })
            # _, loss_value, f = sess.run([train_op, loss, resnet_reid.output],
            #                             feed_dict={input_batch: batch, train_mode: True})
            # print('feature abs mean: %s' % (np.mean(np.abs(f))))
            duration = time.time() - start_time
            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step % 10 == 0:
                examples_per_sec = train_flags.train_batch_size / float(
                    duration)
                format_str = '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f sec/batch)'
                print(format_str % (datetime.now(), step, loss_value,
                                    examples_per_sec, duration))

            if step % 100 == 0:
                summary_str, feature = sess.run(
                    [summary_op, resnet_reid.output],
                    feed_dict={
                        input_batch: batch,
                        train_mode: True
                    })
                print('feature abs mean: %s' % (np.mean(np.abs(feature))))
                summary_str = sess.run(summary_op,
                                       feed_dict={
                                           input_batch: batch,
                                           train_mode: True
                                       })
                summary_writer.add_summary(summary_str, step)

            if step % 5000 == 0 or (step + 1) == train_flags.max_step:
                # Save the model checkpoint periodically.
                checkpoint_path = os.path.join(
                    train_flags.output_check_point_path,
                    train_flags.checkpoint_name)
                saver.save(sess, checkpoint_path, global_step=step)

                # do test: send every image in valid_gallery.csv and valid_probe.csv, then get feature vector
                # save all feature vector in npy
                def valid(gallery_flag):
                    # get feature batch
                    features_num = train_flags.valid_gallery_num if gallery_flag else train_flags.valid_probe_num
                    features = np.zeros(
                        (features_num, train_flags.output_feature_dim),
                        dtype=float)
                    batch_len = train_flags.test_batch_size
                    batch_name = 'valid_gallery_batch_index: ' if gallery_flag else 'valid_probe_batch_index: '
                    end_len = features_num % batch_len
                    for batch_index in range(0, features_num - end_len,
                                             batch_len):
                        batch = resnet_reid.get_test_image_batch(
                            batch_index, batch_len, train_flags.id_image_path,
                            'valid', gallery_flag)
                        batch_feature = sess.run(resnet_reid.output,
                                                 feed_dict={
                                                     input_batch: batch,
                                                     train_mode: False
                                                 })
                        features[batch_index:batch_index +
                                 batch_len, :] = batch_feature
                        print(batch_name + str(batch_index) + '-' +
                              str(batch_index + batch_len - 1))
                    if end_len != 0:
                        batch_index += batch_len
                        batch = resnet_reid.get_test_image_batch(
                            batch_index, batch_len, train_flags.id_image_path,
                            'valid', gallery_flag)
                        batch_feature = sess.run(resnet_reid.output,
                                                 feed_dict={
                                                     input_batch: batch,
                                                     train_mode: False
                                                 })
                        features[batch_index:batch_index +
                                 end_len, :] = batch_feature[:end_len]
                        print(batch_name + str(batch_index) + '-' +
                              str(batch_index + end_len - 1))

                    # save feature
                    features_npy_name = 'valid_gallery_features_step-%d.npy' if gallery_flag else 'valid_probe_features_step-%d.npy'
                    features_npy_path = os.path.join(
                        train_flags.output_test_features_path,
                        features_npy_name % step)
                    np.save(features_npy_path, features)

                valid(False)
                valid(True)
        sess.close()
def model_fn(features, labels, mode, params):
    """
    This is a function for creating a computational tensorflow graph.
    The function is in format required by tf.estimator.
    """
    global INIT_FLAG
    is_training = mode == tf.estimator.ModeKeys.TRAIN
    logits = resnet_v2_50(features['images'],
                          num_classes=params['num_classes'],
                          is_training=is_training
                          #depth_multiplier=params['depth_multiplier']
                          )
    predictions = {
        'probabilities': tf.nn.softmax(logits, axis=1),
        'classes': tf.argmax(logits, axis=1, output_type=tf.int32)
    }

    if mode == tf.estimator.ModeKeys.PREDICT:
        export_outputs = tf.estimator.export.PredictOutput({
            name: tf.identity(tensor, name)
            for name, tensor in predictions.items()
        })
        return tf.estimator.EstimatorSpec(
            mode,
            predictions=predictions,
            export_outputs={'outputs': export_outputs})
    init_fn = None
    if INIT_FLAG:
        exclude = [
            'global_step:0'
        ]  #,'classifier/biases:0','classifier/weights:0'] # ['ShuffleNetV2/Conv1/weights:0','global_step:0','classifier/biases:0','classifier/weights:0'
        #]
        all_variables = tf.contrib.slim.get_variables_to_restore()
        varialbes_to_use = []
        num_params = 0
        for v in all_variables:
            shape = v.get_shape()
            num_params += reduce(mul, [dim.value for dim in shape], 1)
            if v.name not in exclude:
                varialbes_to_use.append(v)
        init_fn = tf.contrib.framework.assign_from_checkpoint_fn(
            tf.train.latest_checkpoint('models/jiu-huan_0.33_9967'),
            varialbes_to_use,
            ignore_missing_vars=True)
        print('***********************params: ', num_params)

    with tf.name_scope('weight_decay'):
        add_weight_decay(params['weight_decay'])
        regularization_loss = tf.losses.get_regularization_loss()

    with tf.name_scope('cross_entropy'):
        losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=labels['labels'], logits=logits)
        loss = tf.reduce_mean(losses, axis=0)
        tf.losses.add_loss(loss)

    total_loss = tf.losses.get_total_loss(add_regularization_losses=True)
    tf.summary.scalar('cross_entropy_loss', loss)
    tf.summary.scalar('regularization_loss', regularization_loss)

    if mode == tf.estimator.ModeKeys.EVAL:
        eval_metric_ops = {
            'accuracy':
            tf.metrics.accuracy(labels['labels'], predictions['classes']),
            'top5_accuracy':
            tf.metrics.mean(
                tf.to_float(
                    tf.nn.in_top_k(predictions=predictions['probabilities'],
                                   targets=labels['labels'],
                                   k=5)))
        }
        return tf.estimator.EstimatorSpec(mode,
                                          loss=total_loss,
                                          eval_metric_ops=eval_metric_ops)

    assert mode == tf.estimator.ModeKeys.TRAIN
    with tf.variable_scope('learning_rate'):
        global_step = tf.train.get_global_step()
        learning_rate = tf.train.polynomial_decay(
            params['initial_learning_rate'],
            global_step,
            params['decay_steps'],
            params['end_learning_rate'],
            power=1.0)  # linear decay
        tf.summary.scalar('learning_rate', learning_rate)

    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops), tf.variable_scope('optimizer'):
        optimizer = tf.train.MomentumOptimizer(learning_rate,
                                               momentum=MOMENTUM,
                                               use_nesterov=USE_NESTEROV)
        grads_and_vars = optimizer.compute_gradients(total_loss)
        train_op = optimizer.apply_gradients(grads_and_vars, global_step)

    for g, v in grads_and_vars:
        tf.summary.histogram(v.name[:-2] + '_hist', v)
        tf.summary.histogram(v.name[:-2] + '_grad_hist', g)

    with tf.name_scope('evaluation_ops'):
        train_accuracy = tf.reduce_mean(tf.to_float(
            tf.equal(labels['labels'], predictions['classes'])),
                                        axis=0)
        train_top5_accuracy = tf.reduce_mean(tf.to_float(
            tf.nn.in_top_k(predictions=predictions['probabilities'],
                           targets=labels['labels'],
                           k=5)),
                                             axis=0)
    tf.summary.scalar('train_accuracy', train_accuracy)
    tf.summary.scalar('train_top5_accuracy', train_top5_accuracy)

    with tf.control_dependencies([train_op]), tf.name_scope('ema'):
        ema = tf.train.ExponentialMovingAverage(decay=MOVING_AVERAGE_DECAY,
                                                num_updates=0)
        train_op = ema.apply(tf.trainable_variables())
    if INIT_FLAG:
        INIT_FLAG = False
        return tf.estimator.EstimatorSpec(
            mode,
            loss=total_loss,
            train_op=train_op,
            training_hooks=[RestoreHook(init_fn)])
    else:
        return tf.estimator.EstimatorSpec(mode,
                                          loss=total_loss,
                                          train_op=train_op)
def build_refinenet(inputs, num_classes, preset_model='RefineNet-Res101', weight_decay=1e-5, is_training=True, upscaling_method="bilinear", pretrained_dir="models"):
    """
    Builds the RefineNet model. 

    Arguments:
      inputs: The input tensor
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes

    Returns:
      RefineNet model
    """

    if preset_model == 'RefineNet-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_50(inputs, is_training=is_training, scope='resnet_v2_50')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'RefineNet-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_101(inputs, is_training=is_training, scope='resnet_v2_101')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'RefineNet-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits, end_points = resnet_v2.resnet_v2_152(inputs, is_training=is_training, scope='resnet_v2_152')
            # RefineNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
    	raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 101 and ResNet 152" % (preset_model))

    


    high = [end_points['pool5'], end_points['pool4'],
         end_points['pool3'], end_points['pool2']]

    low = [None, None, None, None]

    # Get the feature maps to the proper size with bottleneck
    high[0]=slim.conv2d(high[0], 512, 1)
    high[1]=slim.conv2d(high[1], 256, 1)
    high[2]=slim.conv2d(high[2], 256, 1)
    high[3]=slim.conv2d(high[3], 256, 1)

    # RefineNet
    low[0]=RefineBlock(high_inputs=high[0],low_inputs=None) # Only input ResNet 1/32
    low[1]=RefineBlock(high[1],low[0]) # High input = ResNet 1/16, Low input = Previous 1/16
    low[2]=RefineBlock(high[2],low[1]) # High input = ResNet 1/8, Low input = Previous 1/8
    low[3]=RefineBlock(high[3],low[2]) # High input = ResNet 1/4, Low input = Previous 1/4

    # g[3]=Upsampling(g[3],scale=4)

    net = low[3]

    net = ResidualConvUnit(net)
    net = ResidualConvUnit(net)

    if upscaling_method.lower() == "conv":
        net = ConvUpscaleBlock(net, 128, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 128)
        net = ConvUpscaleBlock(net, 64, kernel_size=[3, 3], scale=2)
        net = ConvBlock(net, 64)
    elif upscaling_method.lower() == "bilinear":
        net = Upsampling(net, scale=4)

    net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, scope='logits')

    return net, init_fn
示例#20
0
                                  shape=[1, size, size, 3],
                                  dtype=tf.float32)
        input_image = tf.placeholder(shape=[None, 28, 28, 1], dtype=tf.float32)
        channel_image = tf.concat([input_image, input_image, input_image],
                                  axis=-1)
        rgb_image = tf.pad(tf.concat([input_image, input_image, input_image],
                                     axis=-1),
                           paddings=tf.constant([[0, 0], [136, 135],
                                                 [136, 135], [0, 0]]))

        adv_image = tf.nn.tanh(tf.multiply(weights, mask)) + rgb_image

        labels = tf.placeholder(tf.float32, shape=[None, 10])

        logits, _ = resnet_v2_50(adv_image,
                                 num_classes=1001,
                                 is_training=False)

        output_mapping_tensor = tf.constant(output_mapping, dtype=tf.float32)
        new_logits = tf.matmul(logits, output_mapping_tensor)

        loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(
                logits=new_logits,
                labels=labels)) + reg_lambda * tf.nn.l2_loss(weights)

        learning_rate = tf.train.exponential_decay(starter_learning_rate,
                                                   global_step,
                                                   decay_steps,
                                                   decay_rate,
                                                   staircase=True)
示例#21
0
def build_icnet(inputs, label_size, num_classes, preset_model='ICNet-Res50', pooling_type = "MAX",
    weight_decay=1e-5, is_training=True, pretrained_dir="models"):
    """
    Builds the ICNet model. 

    Arguments:
      inputs: The input tensor
      label_size: Size of the final label tensor. We need to know this for proper upscaling 
      preset_model: Which model you want to use. Select which ResNet model to use for feature extraction 
      num_classes: Number of classes
      pooling_type: Max or Average pooling

    Returns:
      ICNet model
    """

    inputs_4 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*4,  tf.shape(inputs)[2]*4])   
    inputs_2 = tf.image.resize_bilinear(inputs, size=[tf.shape(inputs)[1]*2,  tf.shape(inputs)[2]*2])
    inputs_1 = inputs

    if preset_model == 'ICNet-Res50':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_50(inputs_4, is_training=is_training, scope='resnet_v2_50')
            logits_16, end_points_16 = resnet_v2.resnet_v2_50(inputs_2, is_training=is_training, scope='resnet_v2_50')
            logits_8, end_points_8 = resnet_v2.resnet_v2_50(inputs_1, is_training=is_training, scope='resnet_v2_50')
            resnet_scope='resnet_v2_50'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_50.ckpt'), slim.get_model_variables('resnet_v2_50'))
    elif preset_model == 'ICNet-Res101':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_101(inputs_4, is_training=is_training, scope='resnet_v2_101')
            logits_16, end_points_16 = resnet_v2.resnet_v2_101(inputs_2, is_training=is_training, scope='resnet_v2_101')
            logits_8, end_points_8 = resnet_v2.resnet_v2_101(inputs_1, is_training=is_training, scope='resnet_v2_101')
            resnet_scope='resnet_v2_101'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_101.ckpt'), slim.get_model_variables('resnet_v2_101'))
    elif preset_model == 'ICNet-Res152':
        with slim.arg_scope(resnet_v2.resnet_arg_scope(weight_decay=weight_decay)):
            logits_32, end_points_32 = resnet_v2.resnet_v2_152(inputs_4, is_training=is_training, scope='resnet_v2_152')
            logits_16, end_points_16 = resnet_v2.resnet_v2_152(inputs_2, is_training=is_training, scope='resnet_v2_152')
            logits_8, end_points_8 = resnet_v2.resnet_v2_152(inputs_1, is_training=is_training, scope='resnet_v2_152')
            resnet_scope='resnet_v2_152'
            # ICNet requires pre-trained ResNet weights
            init_fn = slim.assign_from_checkpoint_fn(os.path.join(pretrained_dir, 'resnet_v2_152.ckpt'), slim.get_model_variables('resnet_v2_152'))
    else:
        raise ValueError("Unsupported ResNet model '%s'. This function only supports ResNet 50, ResNet 101, and ResNet 152" % (preset_model))



    feature_map_shape = [int(x / 32.0) for x in label_size]
    block_32 = PyramidPoolingModule(end_points_32['pool3'], feature_map_shape=feature_map_shape, pooling_type=pooling_type)

    out_16, block_16 = CFFBlock(psp_32, end_points_16['pool3'])
    out_8, block_8 = CFFBlock(block_16, end_points_8['pool3'])
    out_4 = Upsampling_by_scale(out_8, scale=2)
    out_4 = slim.conv2d(out_4, num_classes, [1, 1], activation_fn=None) 

    out_full = Upsampling_by_scale(out_4, scale=2)
    
    out_full = slim.conv2d(out_full, num_classes, [1, 1], activation_fn=None, scope='logits')

    net = tf.concat([out_16, out_8, out_4, out_final])

    return net, init_fn
def run_training():

    train_dir = 'G:/kaggel-cancer-detection/train_jpg/'
    logs_train_dir = 'G:/kaggel-cancer-detection-code/SaveNet-ResNet/'

    # 获取图片和标签集
    train, train_label = input_data.get_files(train_dir)
    # 生成批次
    train_batch, train_label_batch = input_data.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    global_step = tf.Variable(0, trainable=False)
    learning_rate = tf.train.exponential_decay(
        learning_rate=starter_learning_rate,
        global_step=global_step,
        decay_steps=steps_per_decay,
        decay_rate=decay_factor,
        staircase=True,  #If `True` decay the learning rate at discrete intervals
        #staircase = False,change learning rate at every step
    )
    print("Enter Model!")
    train_logits, end_points = resnet_v2.resnet_v2_50(inputs=train_batch,
                                                      num_classes=N_CLASSES)
    print("Get loss!")
    train_loss = losses(train_logits, train_label_batch)
    print("Start training!")
    train_op = trainning(train_loss, learning_rate, global_step)
    print("Get Acc!")
    train__acc = evaluation(train_logits, train_label_batch)
    summary_op = tf.summary.merge_all()
    #sess = tf.Session()
    print("Save summary")

    saver = tf.train.Saver()

    with tf.Session() as sess:
        train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                print("break!!!")
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss,
                                             train__acc])  #训练
            if sess.run(global_step) % 50 == 0:
                print(
                    'Step %d, train loss = %.2f, learning rate = %.10f,train accuracy = %.2f%%'
                    % (sess.run(global_step), tra_loss,
                       sess.run(learning_rate), tra_acc * 100.0))
                #print('global_step:',sess.run(global_step))
                #print('learning rate:',sess.run(learning_rate))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)
            if sess.run(global_step) % 2000 == 0 or (
                    sess.run(global_step)) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess,
                           checkpoint_path,
                           global_step=sess.run(global_step))
        coord.join(threads)
    sess.close()