Example #1
0
 def arch_multi_alexnet_v2(self,
                           X1,
                           X2,
                           X3,
                           num_classes,
                           dropout_keep_prob=0.8,
                           is_train=False):
     arg_scope = alexnet_v2_arg_scope()
     with slim.arg_scope(arg_scope):
         with tf.variable_scope('arch_alexnet_v2_1'):
             net_vis1, end_points1 = alexnet_v2(X1, is_training=is_train)
         with tf.variable_scope('arch_alexnet_v2_2'):
             net_vis2, end_points2 = alexnet_v2(X2, is_training=is_train)
         with tf.variable_scope('arch_alexnet_v2_3'):
             net_vis3, end_points3 = alexnet_v2(X2, is_training=is_train)
         # net_vis3, end_points3 = alexnet_v2(X3, 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_vis = tf.concat([net_vis1, net_vis2, net_vis3], 3)
             net = slim.conv2d(net_vis,
                               num_classes, [1, 1],
                               activation_fn=None,
                               normalizer_fn=None,
                               scope='fc8')
             net = tf.squeeze(net, [1, 2], name='fc8/squeezed')
     return net, net_vis
Example #2
0
 def arch_alexnet_v2(self, X, num_classes, dropout_keep_prob=0.8, is_train=False):
     arg_scope = alexnet_v2_arg_scope()
     with slim.arg_scope(arg_scope):
         net_vis, end_points = alexnet_v2(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, num_classes, [1, 1],activation_fn=None,normalizer_fn=None,scope='fc8')
             net = tf.squeeze(net,[1,2], name='fc8/squeezed')
     return net, net_vis
Example #3
0
def main(_):
    """
    Configuration Part.
    """
    assert FLAGS.image_folder, "--picture_folder necessary"
    assert FLAGS.logs_dir, "--logs_dir necessary"
    assert FLAGS.label_list_path, "--label_list_path necessary"
    logs_path = os.path.join('logs', FLAGS.logs_dir)
    pattern_extension = range(2)
    num_classes = 2
    label_list = []
    with open(FLAGS.label_list_path) as f:
        for line in f:
            label_list.append(line.strip().split())

    image_size = [256, 256]

    image1_placeholder = tf.placeholder(tf.uint8, [None, image_size[0], image_size[1]], name='image1_input')
    image2_placeholder = tf.placeholder(tf.uint8, [None, image_size[0], image_size[1]], name='image2_input')

    merged_image = tf.stack((image1_placeholder, image2_placeholder), -1)
    float_input_tensor = tf.to_float(merged_image)
    # Define the network
    # with slim.arg_scope(mobilenet_v2.training_scope(is_training=False)):
    #     logits, _ = mobilenet_v2.mobilenet(tf.to_float(image_tensor), num_classes=num_classes)
    with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
        logits, end_points = alexnet.alexnet_v2(float_input_tensor, num_classes=num_classes, is_training=False)

    predictions = tf.argmax(logits, 1, name='output_argmax')
    # Setup the global step.
    tf.train.get_or_create_global_step()
    session_config = tf.ConfigProto()
    session_config.gpu_options.allow_growth = True
    tf.logging.set_verbosity(tf.logging.INFO)
    saver = tf.train.Saver()

    # Launch the graph
    with tf.Session(config=session_config) as sess:
        start_time = time.time()
        prev_model = tf.train.get_checkpoint_state(logs_path)
        if prev_model:
            saver.restore(sess, prev_model.model_checkpoint_path)
            elapsed_time = time.time() - start_time
            print('Checkpoint found, {}'.format(prev_model))
            print('restore elapsed time: {}'.format(elapsed_time))
            for image_pair in label_list:
                image_array = read_image_array(image_pair, FLAGS.image_folder)
                start_time = time.time()
                predict_array = sess.run(predictions, feed_dict={image1_placeholder: [image_array[0]],
                                                                 image2_placeholder: [image_array[1]]})
                elapsed_time = time.time() - start_time
                print("Prediction: {}, shape: {}".format(predict_array, predict_array.shape))
                print('inference elapsed time: {}'.format(elapsed_time))

        else:
            print('No checkpoint found')
Example #4
0
 def predict(self,preprocessed_inputs):
     """Predict prediction tensors from inputs tensor.
     
     Outputs of this function can be passed to loss or postprocess functions.
     
     Args:
         preprocessed_inputs: A float32 tensor with shape [batch_size,
             height, width, num_channels] representing a batch of images.
         
     Returns:
         prediction_dict: A dictionary holding prediction tensors to be
             passed to the Loss or Postprocess functions.
     """
     with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
         net, endpoints = alexnet.alexnet_v2(inputs=preprocessed_inputs,num_classes=None,
                                             is_training=self._is_training,global_pool=True)
         net = tf.squeeze(net,axis=[1,2])
         logits = slim.fully_connected(net, num_outputs=self.num_classes,
                                   activation_fn=None, scope='Predict')
         prediction_dict = {'logits': logits}
     return prediction_dict                                                           
Example #5
0
def main(_):
    """
    Configuration Part.
    """
    assert FLAGS.logs_dir, '`logs_dir` is missing.'
    logs_path = os.path.join('logs', FLAGS.logs_dir)
    data_dir = 'data'
    tfrecord_train = ['2_image_compare_train.tfrecords']
    load_checkpoint = True
    train_tf_path = []
    for record in tfrecord_train:
        train_tf_path.append(os.path.join(data_dir, record))

    crop_size = [256, 256]
    # Learning params
    learning_rate = 0.01
    num_epochs = 500
    batch_size = 128
    num_examples = get_record_number(train_tf_path)
    num_batches = math.ceil(num_examples / float(batch_size))
    total_steps = num_batches * num_epochs
    print('batch number: {}, total steps: {}'.format(num_batches, total_steps))

    pattern_extension = range(2)
    num_classes = 2
    num_ng_sample = 3760
    num_ok_sample = 4929
    class_ratio = num_ng_sample / (num_ng_sample + num_ok_sample)

    # Launch the graph
    with tf.Graph().as_default():

        tf.logging.set_verbosity(tf.logging.INFO)
        tf.summary.scalar('batch_size', batch_size)

        # Load the data
        train_image_batch, train_label_batch = get_data_batch(
            train_tf_path, pattern_extension, crop_size, batch_size, is_training=True, one_hot=False)
        # convert to float batch
        float_image_batch = tf.to_float(train_image_batch)

        # with slim.arg_scope(mobilenet_v2.training_scope(is_training=True)):
        #     logits, end_points = mobilenet_v2.mobilenet(float_image_batch, num_classes=num_classes)
        with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
            logits, end_points = alexnet.alexnet_v2(float_image_batch, num_classes=num_classes, is_training=True)

        # make summaries of every operation in the node
        for layer_name, layer_op in end_points.items():
            tf.summary.histogram(layer_name, layer_op)

        class_weight = tf.constant([[class_ratio, 1 - class_ratio]])
        # weighted_logits = tf.multiply(logits, class_weight)

        # Specify the loss function (outside the model!)
        one_hot_label = tf.one_hot(indices=train_label_batch, depth=num_classes)
        weight_per_label = tf.transpose(tf.matmul(one_hot_label, tf.transpose(class_weight)))
        loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=one_hot_label, name='softmax')
        weight_loss = tf.multiply(weight_per_label, loss)
        total_loss = tf.reduce_mean(weight_loss)

        # slim.losses.softmax_cross_entropy(logits, one_hot_label)
        # total_loss = slim.losses.get_total_loss()

        # Create some summaries to visualize the training process:
        tf.summary.scalar('losses/Total Loss', total_loss)

        # Specify the optimizer and create the train op:
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
        # optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        train_op = slim.learning.create_train_op(total_loss, optimizer)

        # Track accuracy and recall
        predictions = tf.argmax(logits, 1)

        # Define the metrics:
        # Recall@5 would make no sense, because we have only 5 classes here
        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
            'eval/Accuracy': slim.metrics.streaming_accuracy(predictions, train_label_batch),
            'eval/Recall': slim.metrics.streaming_recall(predictions, train_label_batch),
            'eval/Precision': slim.metrics.streaming_precision(predictions, train_label_batch)
        })
        for name, tensor in names_to_updates.items():
            tf.summary.scalar(name, tensor)
        saver = tf.train.Saver()
        session_config = tf.ConfigProto()
        session_config.gpu_options.allow_growth = True
        session_config.log_device_placement = True
        session = tf.Session(config=session_config)
        prev_model = tf.train.get_checkpoint_state(logs_path)
        if load_checkpoint:
            if prev_model:
                saver.restore(session, prev_model.model_checkpoint_path)
                print('Checkpoint found, {}'.format(prev_model))
            else:
                print('No checkpoint found')
    # Run the training:
        final_loss = slim.learning.train(
            train_op,
            logdir=logs_path,
            number_of_steps=num_epochs * num_batches,
            session_config=session_config,
            save_summaries_secs=20,
            save_interval_secs=300
        )

        print('Finished training. Final batch loss %d' % final_loss)
Example #6
0
X_test = mnist.test.images
X_test=np.reshape(X_test,[-1,28,28,1])
X_images=[]
for image in X_test:
    X_images.append(pad_image(image,img_h,img_w))
X_test=np.asarray(X_images)
y_test=mnist.test.labels

# -------------------------------#

X = tf.placeholder('float', [None, img_h,img_w,1])
Y = tf.placeholder('float')
is_training=tf.placeholder(tf.bool)


with slim.arg_scope(alexnet_v2_arg_scope()):
    predict, _ = alexnet_v2(X, n_output_layer, is_training, 0.8)

cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predict, labels=Y))
optimizer = tf.train.AdamOptimizer().minimize(cost_func)  # learning rate 默认 0.001
correct = tf.equal(tf.argmax(predict, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))


epochs = 1
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    epoch_loss = 0
    for epoch in range(epochs):
        for step in range(mnist.train.num_examples // batch_size):
            x, y = mnist.train.next_batch(batch_size)
Example #7
0
def main(_):
    """
    Configuration Part.
    """
    assert FLAGS.logs_dir, '`logs_dir` is missing.'
    logs_path = os.path.join('logs', FLAGS.logs_dir)
    data_dir = 'data'
    tfrecord_test = ['2_image_compare_test.tfrecords']
    test_tf_path = []
    for record in tfrecord_test:
        test_tf_path.append(os.path.join(data_dir, record))
    crop_size = [256, 256]
    num_classes = 2
    pattern_extension = range(2)

    num_examples = get_record_number(test_tf_path)
    batch_size = 64
    num_batches = math.ceil(num_examples / float(batch_size))
    # Load the data
    test_image_batch, test_label_batch = get_data_batch(test_tf_path,
                                                        pattern_extension,
                                                        crop_size,
                                                        batch_size,
                                                        is_training=False,
                                                        one_hot=False)
    # convert to float batch
    float_image_batch = tf.to_float(test_image_batch)
    # Define the network
    # with slim.arg_scope(mobilenet_v2.training_scope(is_training=False)):
    #     logits, _ = mobilenet_v2.mobilenet(float_image_batch, num_classes=num_classes)
    with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
        logits, end_points = alexnet.alexnet_v2(float_image_batch,
                                                num_classes=num_classes,
                                                is_training=False)

    predictions = tf.argmax(logits, 1)

    # Choose the metrics to compute:
    # names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
    #     'accuracy': slim.metrics.accuracy(predictions, test_label_batch),
    # })
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
        'test/Accuracy':
        slim.metrics.streaming_accuracy(predictions, test_label_batch),
        'test/mse':
        slim.metrics.streaming_mean_squared_error(predictions,
                                                  test_label_batch),
        'test/Recall':
        slim.metrics.streaming_recall(predictions, test_label_batch),
        'test/Precision':
        slim.metrics.streaming_precision(predictions, test_label_batch)
        # 'test/Recall@5': slim.metrics.streaming_recall_at_k(logits, test_label, 5),
    })
    for name, tensor in names_to_updates.items():
        tf.summary.scalar(name, tensor)
    # Create the summary ops such that they also print out to std output:
    summary_ops = []
    for metric_name, metric_value in names_to_values.items():
        op = tf.summary.scalar(metric_name, metric_value)
        op = tf.Print(op, [metric_value], metric_name)
        summary_ops.append(op)

    # Setup the global step.
    slim.get_or_create_global_step()
    session_config = tf.ConfigProto()
    session_config.gpu_options.allow_growth = True
    session = tf.Session(config=session_config)
    tf.logging.set_verbosity(tf.logging.INFO)
    eval_interval_secs = 10  # How often to run the evaluation.
    slim.evaluation.evaluation_loop('',
                                    logs_path,
                                    logs_path,
                                    num_evals=num_batches,
                                    eval_op=list(names_to_updates.values()),
                                    summary_op=tf.summary.merge(summary_ops),
                                    eval_interval_secs=eval_interval_secs)
Example #8
0
def FullyConvolutional_net(X, num_classes, dropout_keep_prob=0.25):
    with alexnet.slim.arg_scope(alexnet.alexnet_v2_arg_scope(
    )):  # Dimension of inputs : [batch_size, height, width, 3]
        logits, _ = alexnet.alexnet_v2(
            X, num_classes)  # Dimension of Logits : [batch_size, num_classes]
        return logits
Example #9
0
def train():

    input_model = Inputs(FLAGS.data_dir,
                         FLAGS.batch_size,
                         one_hot=FLAGS.one_hot)

    with tf.name_scope('input'):
        x = tf.placeholder(tf.float32, [None, 28 * 28 * 1], 'x')
        y_ = tf.placeholder(tf.float32, [None, 10], 'y_')
        keep = tf.placeholder(tf.float32)
        is_training = tf.placeholder(tf.bool, name='MODE')

    image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])  # shape [n,28,28,1]
    # alexnet要求的数据shape是 224x224
    image_shaped_input = tf.image.resize_image_with_crop_or_pad(
        image_shaped_input, 224, 224)  # shape[n,224,224,1]

    # with slim.arg_scope(cifarnet_arg_scope()):
    # with slim.arg_scope(inception_resnet_v2_arg_scope()):
    #     y, _ = cifarnet(images=image_shaped_input,num_classes=10,is_training=is_training,dropout_keep_prob=keep)

    # 上面的修改成
    with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
        y, _ = alexnet.alexnet_v2(inputs=image_shaped_input,
                                  num_classes=10,
                                  is_training=is_training,
                                  dropout_keep_prob=keep)

    model = Conv_model(y_, FLAGS.learning_rate)
    cross_entropy = model.loss(y, MSE_error=False, one_hot=FLAGS.one_hot)
    train_op = model.train(cross_entropy)
    accuracy = model.evaluate(y, one_hot=FLAGS.one_hot)
    init = tf.group(tf.global_variables_initializer(),
                    tf.local_variables_initializer())

    with tf.Session() as sess:
        sess.run(init)
        for step in range(FLAGS.num_steps):
            batch_xs, batch_ys = input_model.inputs()
            train_op.run({
                x: batch_xs,
                y_: batch_ys,
                keep: 0.7,
                is_training: True
            })

            if step % FLAGS.disp_step == 0:
                acc = accuracy.eval({
                    x: batch_xs,
                    y_: batch_ys,
                    keep: 1.,
                    is_training: False
                })
                print(
                    "step", step, 'acc', acc, 'loss',
                    cross_entropy.eval({
                        x: batch_xs,
                        y_: batch_ys,
                        keep: 1.,
                        is_training: False
                    }))
        # test acc
        test_x, test_y = input_model.test_inputs()
        acc = accuracy.eval({
            x: test_x,
            y_: test_y,
            keep: 1.,
            is_training: False
        })
        print('test acc', acc)