Ejemplo n.º 1
0
 def testForward(self):
     batch_size = 1
     height, width = 224, 224
     with self.test_session() as sess:
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = alexnet.alexnet_v2(inputs)
         sess.run(tf.global_variables_initializer())
         output = sess.run(logits)
         self.assertTrue(output.any())
Ejemplo n.º 2
0
def test_image():
    '''Test images against the saved models and parameters
    '''

    train, image_ids = get_files()
    with tf.Graph().as_default():
        train_batch, imageid_batch = get_batch(train, image_ids, IMG_W, IMG_H,
                                               BATCH_SIZE, CAPACITY)
        logit, _ = alexnet.alexnet_v2(train_batch, N_CLASSES)

        prediction = tf.argmax(logit, 1)

        # you need to change the directories to yours.
        logs_train_dir = './logs/train/'

        saver = tf.train.Saver()

        with tf.Session() as sess:

            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_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')

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            all_predictions = []
            try:
                for step in np.arange(MAX_STEP):
                    if coord.should_stop():
                        break
                    batch_predictions = sess.run([prediction])
                    print(batch_predictions)
                    all_predictions = np.concatenate(
                        [all_predictions, batch_predictions[0]])

            except tf.errors.OutOfRangeError:
                print('Done training -- epoch limit reached')
            finally:
                coord.request_stop()
            coord.join(threads)
    # pdb.set_trace()
    all_predictions.astype(np.int64)
    all_predictions = input_helper.get_real_label(all_predictions)
    predictions_human_readable = np.column_stack(
        (all_predictions, np.array(image_ids)))
    out_path = os.path.join(".", "prediction.csv")
    print("Saving evaluation to {0}".format(out_path))
    with open(out_path, 'w') as f:
        for line in predictions_human_readable:
            f.write(str(line[0]) + '\t' + str(line[1]) + '\n')
Ejemplo n.º 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')
Ejemplo n.º 4
0
 def testBuild(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = alexnet.alexnet_v2(inputs, num_classes)
         self.assertEquals(logits.op.name, 'alexnet_v2/fc8/squeezed')
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
Ejemplo n.º 5
0
 def testFullyConvolutional(self):
     batch_size = 1
     height, width = 300, 400
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = alexnet.alexnet_v2(inputs,
                                        num_classes,
                                        spatial_squeeze=False)
         self.assertEquals(logits.op.name, 'alexnet_v2/fc8/BiasAdd')
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, 4, 7, num_classes])
Ejemplo n.º 6
0
 def testEvaluation(self):
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         eval_inputs = tf.random_uniform((batch_size, height, width, 3))
         logits, _ = alexnet.alexnet_v2(eval_inputs, is_training=False)
         self.assertListEqual(logits.get_shape().as_list(),
                              [batch_size, num_classes])
         predictions = tf.argmax(logits, 1)
         self.assertListEqual(predictions.get_shape().as_list(),
                              [batch_size])
Ejemplo n.º 7
0
def main():
    args = parse_args()
    # Load the train data
    (train_images, train_labels,
     train_weights) = load_pascal(args.data_directory, split='train')

    # Avoid loading other data for now
    # (test_images, test_labels, test_weights) = load_pascal(args.data_directory, split='test')
    # (val_images, val_labels, val_weights) = load_pascal(args.data_directory, split='val')

    num_train_images = train_images.shape[0]
    batch_size = 10
    num_epochs = 5

    inputs = tf.placeholder(tf.float32, shape=(batch_size, 224, 224, 3))

    outputs, end_points = alexnet.alexnet_v2(inputs)

    # Log some dummy data to tensorboard
    summary_writer = tf.summary.FileWriter('tmp/tensorboard_example',
                                           graph=tf.get_default_graph())
    loss = 0.2
    tf.summary.scalar('Loss', loss)
    tf.summary.image('Batch Images', inputs)
    merged_summary = tf.summary.merge_all()

    # Setup a TensorFlow session
    with tf.Session() as sess:
        # Initialize global variables
        sess.run(tf.global_variables_initializer())
        # Epoch = a full cycle through all of the training images
        for epoch in range(num_epochs):
            # Randomly order the training images
            training_order = range(num_train_images)
            shuffle(training_order)

            # Split the training images into batches
            for batch_num in range(int(floor(num_train_images / batch_size))):
                # Extract the data for this batch
                batch_indices = training_order[batch_num *
                                               batch_size:(batch_num + 1) *
                                               batch_size]
                batch_images = np.take(train_images, batch_indices, axis=0)
                batch_labels = np.take(train_labels, batch_indices, axis=0)
                batch_weights = np.take(train_weights, batch_indices, axis=0)

                [output_logits,
                 summary] = sess.run([outputs, merged_summary],
                                     feed_dict={inputs: batch_images})
                summary_writer.add_summary(summary)
Ejemplo n.º 8
0
 def testEndPoints(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     with self.test_session():
         inputs = tf.random_uniform((batch_size, height, width, 3))
         _, end_points = alexnet.alexnet_v2(inputs, num_classes)
         expected_names = [
             'alexnet_v2/conv1', 'alexnet_v2/pool1', 'alexnet_v2/conv2',
             'alexnet_v2/pool2', 'alexnet_v2/conv3', 'alexnet_v2/conv4',
             'alexnet_v2/conv5', 'alexnet_v2/pool5', 'alexnet_v2/fc6',
             'alexnet_v2/fc7', 'alexnet_v2/fc8'
         ]
         self.assertSetEqual(set(end_points.keys()), set(expected_names))
Ejemplo n.º 9
0
 def testTrainEvalWithReuse(self):
     train_batch_size = 2
     eval_batch_size = 1
     train_height, train_width = 224, 224
     eval_height, eval_width = 300, 400
     num_classes = 1000
     with self.test_session():
         train_inputs = tf.random_uniform(
             (train_batch_size, train_height, train_width, 3))
         logits, _ = alexnet.alexnet_v2(train_inputs)
         self.assertListEqual(logits.get_shape().as_list(),
                              [train_batch_size, num_classes])
         tf.get_variable_scope().reuse_variables()
         eval_inputs = tf.random_uniform(
             (eval_batch_size, eval_height, eval_width, 3))
         logits, _ = alexnet.alexnet_v2(eval_inputs,
                                        is_training=False,
                                        spatial_squeeze=False)
         self.assertListEqual(logits.get_shape().as_list(),
                              [eval_batch_size, 4, 7, num_classes])
         logits = tf.reduce_mean(logits, [1, 2])
         predictions = tf.argmax(logits, 1)
         self.assertEquals(predictions.get_shape().as_list(),
                           [eval_batch_size])
Ejemplo n.º 10
0
def run_training():

    # you need to change the directories to yours.
    file = 'train_data.txt'
    logs_train_dir = './logs/train/'

    train, train_label = input_helper.get_files(file)

    train_batch, train_label_batch = input_helper.get_batch(
        train, train_label, IMG_W, IMG_H, BATCH_SIZE, CAPACITY)
    train_logits, _ = alexnet.alexnet_v2(train_batch, N_CLASSES)
    train_loss = alexnet.losses(train_logits, train_label_batch)
    train_op = alexnet.trainning(train_loss, learning_rate)
    train__acc = alexnet.evaluation(train_logits, train_label_batch)

    summary_op = tf.summary.merge_all()
    sess = tf.Session()
    train_writer = tf.summary.FileWriter(logs_train_dir, sess.graph)
    saver = tf.train.Saver()

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

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc = sess.run([train_op, train_loss, train__acc])

            if step % 50 == 0:
                print('Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                      (step, tra_loss, tra_acc * 100.0))
                summary_str = sess.run(summary_op)
                train_writer.add_summary(summary_str, step)

            if step % 200 == 0 or (step + 1) == MAX_STEP:
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()

    coord.join(threads)
    sess.close()
Ejemplo n.º 11
0
def evalalexnet():
    # Create model and obtain the predictions:
    imgs, labels = load_data.read_tfrecord(data_dir=DATA_DIR,
                                           image_size=IMAGE_SIZE,
                                           spilt='val',
                                           batch_size=BATCH_SIZE,
                                           num_classes=NUM_CLASS)

    X = tf.placeholder(dtype=tf.float32,
                       shape=[BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])
    Y = tf.placeholder(dtype=tf.int64, shape=[BATCH_SIZE, NUM_CLASS])

    logits, _ = alexnet.alexnet_v2(inputs=X, num_classes=5)
    predictions = tf.nn.softmax(logits)
    predictions = tf.cast(predictions, tf.int32)

    accuracy = slim.metrics.streaming_accuracy(predictions, Y),
    precision = slim.metrics.streaming_precision(predictions, Y),
    # recall = slim.metrics.streaming_recall_at_k(predictions, labels, 5),
    tf.summary.scalar('accuracy', accuracy)
    tf.summary.scalar('precision', precision)

    num_examples = 100
    num_batches = math.ceil(num_examples / float(BATCH_SIZE))

    summary_op = tf.summary.merge_all()
    ckptfile = '../train_log/alexnet/model.ckpt-4900'

    with tf.Session() as sess:
        summary_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'eval'),
                                               sess.graph)
        tf.train.Saver().restore(sess, ckptfile)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for batch_id in range(10):
            reaccuracy, reprecision = sess.run([accuracy, precision],
                                               feed_dict={
                                                   X: imgs,
                                                   Y: labels
                                               })
            # summary_writer.add_event(summary_str)

        coord.request_stop()
        coord.join(threads)
Ejemplo n.º 12
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
Ejemplo n.º 13
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                                                           
Ejemplo n.º 14
0
def inference(img):
    ref_dict = {}
    for img_file in glob("Dataset/Test/*.*"):
        img_name = img_file.split('/')[-1]
        img = cv2.imread(img_file)
        img = cv2.resize(img, (224, 224))
        ref_dict[img_name] = img
    images = np.asarray(list(ref_dict.values()))
    output = alexnet.alexnet_v2(X)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state("model")
        saver.restore(sess, "model/model-epoch500.ckpt")
        result = sess.run(output, feed_dict={X: images})
        result = tf.nn.softmax(result)
        inf_result = tf.argmax(result, 1)
        class_result = sess.run(result)
        out = sess.run(inf_result)
        for res in range(len(out)):
            if (out[res] == 0):
                print('It is NSFW!', list(ref_dict.keys())[res])
            else:
                print('It is SFW!', list(ref_dict.keys())[res])
Ejemplo n.º 15
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)
Ejemplo n.º 16
0
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)
            x = np.reshape(x, [-1, 28, 28, 1])
Ejemplo n.º 17
0
                            shape=(1, SIZE, SIZE, 3),
                            name='real_images')
    labels = tf.placeholder(tf.float32, shape=(1, 5), name='attributes')

    # clip logits between [0, 1] because that's the range of the labels
    if NETWORK == 'inception':
        print 'Using inception'
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logits, _ = inception_resnet_v2.inception_resnet_v2(
                images, num_classes=5, is_training=False)
    if NETWORK == 'alexnet':
        print 'Using alexnet'
        with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
            logits, _ = alexnet.alexnet_v2(images,
                                           num_classes=5,
                                           is_training=False)
    print 'Done.'

    saver = tf.train.Saver()
    init = tf.group(tf.global_variables_initializer(),
                    tf.local_variables_initializer())
    sess = tf.Session()
    sess.run(init)
    # restore previous model if there is one
    ckpt = tf.train.get_checkpoint_state(CHECKPOINT_DIR)
    if ckpt and ckpt.model_checkpoint_path:
        print 'Restoring previous model...'
        try:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print 'Model restored'
Ejemplo n.º 18
0
def train(data_dir, train_dir, max_steps, log_frequency):
    train_batch, train_label_batch = load_data.read_tfrecord(
        data_dir,
        image_size=IMAGE_SIZE,
        spilt='train',
        batch_size=BATCH_SIZE,
        num_classes=NUM_CLASS)

    val_batch, val_label_batch = load_data.read_tfrecord(data_dir,
                                                         image_size=IMAGE_SIZE,
                                                         spilt='val',
                                                         batch_size=BATCH_SIZE,
                                                         num_classes=NUM_CLASS)

    X = tf.placeholder(dtype=tf.float32,
                       shape=[BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])
    Y = tf.placeholder(dtype=tf.int64, shape=[BATCH_SIZE, NUM_CLASS])

    # 训练集
    logits, _ = alexnet.alexnet_v2(inputs=X,
                                   num_classes=NUM_CLASS,
                                   is_training=True)
    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits))
    train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
    correct_prediction = tf.equal(tf.argmax(Y, 1), tf.argmax(logits, 1))
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    tf.summary.scalar('train_loss', loss)
    tf.summary.scalar('train_acc', acc)
    summary_op = tf.summary.merge_all()
    saver = tf.train.Saver()
    start_time = now_time = time.time()

    with tf.Session() as sess:
        summary_train_writer = tf.summary.FileWriter(
            os.path.join(train_dir, 'trainsum'), sess.graph)
        summary_val_writer = tf.summary.FileWriter(
            os.path.join(train_dir, 'valsum'), 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 range(max_steps):
            if coord.should_stop():
                break
            train_images, train_labels = sess.run(
                [train_batch, train_label_batch])
            train_loss, _, train_acc, summary_str = sess.run(
                [loss, train_op, acc, summary_op],
                feed_dict={
                    X: train_images,
                    Y: train_labels
                })

            if step % log_frequency == 0:
                # 添加训练数据到tensorboard
                summary_train_writer.add_summary(summary_str, step)

                valdat_images, valdat_labels = sess.run(
                    [val_batch, val_label_batch])
                valdat_loss, valdat_acc, summary_str = sess.run(
                    [loss, acc, summary_op],
                    feed_dict={
                        X: valdat_images,
                        Y: valdat_labels
                    })
                # 添加验证集数据到tensorboard
                summary_val_writer.add_summary(summary_str, step)

                print(
                    'Step %d using %ds, loss %f, acc %.2f%% --- * val_loss %f, val_acc %.2f%%'
                    % (step, time.time() - now_time, train_loss,
                       train_acc * 100.0, valdat_loss, valdat_acc * 100.0))

            if step % (max_steps / 5) == 0 or (step + 1) == max_steps:
                checkpoint_path = os.path.join(train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)

        coord.request_stop()
        coord.join(threads)
        print('all using %d time:' % (time.time() - start_time))
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
def train():
    ps_hosts = FLAGS.ps_hosts.split(',')
    worker_hosts = FLAGS.worker_hosts.split(',')
    print('PS hosts are: %s' % ps_hosts)
    print('Worker hosts are: %s' % worker_hosts)
    server = tf.train.Server({
        'ps': ps_hosts,
        'worker': worker_hosts
    },
                             job_name=FLAGS.job_name,
                             task_index=FLAGS.task_id)
    if FLAGS.job_name == 'ps':
        server.join()
    is_chief = (FLAGS.task_id == 0)
    if is_chief:
        if tf.gfile.Exists(FLAGS.train_dir):
            tf.gfile.DeleteRecursively(FLAGS.train_dir)
        tf.gfile.MakeDirs(FLAGS.train_dir)

    device_setter = tf.train.replica_device_setter(ps_tasks=len(ps_hosts))
    with tf.device('/job:worker/task:%d' % FLAGS.task_id):
        partitioner = tf.fixed_size_partitioner(len(ps_hosts), axis=0)
        with tf.variable_scope('partitioned_space', partitioner=partitioner):
            with tf.device(device_setter):
                global_step = tf.Variable(0, trainable=False)
                decay_steps = 50000 * 350.0 / FLAGS.batch_size
                batch_size = tf.placeholder(dtype=tf.int32,
                                            shape=(),
                                            name='batch_size')
                images, labels = cifar10.distorted_inputs(batch_size)
                inputs = tf.reshape(images, [-1, _HEIGHT, _WIDTH, _DEPTH])
                labels = tf.one_hot(labels, 10, 1, 0)

                #  	    network_fn = nets_factory.get_network_fn('alexnet_v2',num_classes=10)
                # 	    (logits,_) = network_fn(inputs)
                #            with slim.arg_scope(alexnet.alexnet_v2_arg_scope(weight_decay=0.0)):
                (logits, _) = alexnet.alexnet_v2(inputs,
                                                 num_classes=10,
                                                 is_training=True)

                cross_entropy = tf.losses.softmax_cross_entropy(
                    logits=logits, onehot_labels=labels)
                loss = cross_entropy + _WEIGHT_DECAY * tf.add_n(
                    [tf.nn.l2_loss(v) for v in tf.trainable_variables()])

                # Decay the learning rate exponentially based on the number of steps.
                lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE *
                                                len(worker_hosts),
                                                global_step,
                                                decay_steps,
                                                LEARNING_RATE_DECAY_FACTOR,
                                                staircase=True)
                opt = tf.train.GradientDescentOptimizer(lr)
                # Track the moving averages of all trainable variables.
                exp_moving_averager = tf.train.ExponentialMovingAverage(
                    MOVING_AVERAGE_DECAY, global_step)
                variables_to_average = (tf.trainable_variables() +
                                        tf.moving_average_variables())
                opt = tf.train.SyncReplicasOptimizer(
                    opt,
                    replicas_to_aggregate=len(worker_hosts),
                    total_num_replicas=len(worker_hosts),
                    variable_averages=exp_moving_averager,
                    variables_to_average=variables_to_average)
                naive_grads = opt.compute_gradients(loss)
                grads = [(tf.scalar_mul(
                    tf.cast(batch_size / FLAGS.batch_size, tf.float32),
                    grad), var) for grad, var in naive_grads]
                apply_gradients_op = opt.apply_gradients(
                    grads, global_step=global_step)
                with tf.control_dependencies([apply_gradients_op]):
                    train_op = tf.identity(loss, name='train_op')

                chief_queue_runners = [opt.get_chief_queue_runner()]
                init_tokens_op = opt.get_init_tokens_op()
                saver = tf.train.Saver()
                sv = tf.train.Supervisor(is_chief=is_chief,
                                         logdir=FLAGS.train_dir,
                                         init_op=tf.group(
                                             tf.global_variables_initializer(),
                                             tf.local_variables_initializer()),
                                         summary_op=None,
                                         global_step=global_step,
                                         saver=saver,
                                         recovery_wait_secs=1,
                                         save_model_secs=60)

                tf.logging.info('%s Supervisor' % datetime.now())
                sess_config = tf.ConfigProto(
                    allow_soft_placement=True,
                    log_device_placement=FLAGS.log_device_placement)
                sess_config.gpu_options.allow_growth = True
                sess = sv.prepare_or_wait_for_session(server.target,
                                                      config=sess_config)
                queue_runners = tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS)
                sv.start_queue_runners(sess, queue_runners)

                sv.start_queue_runners(sess, chief_queue_runners)
                sess.run(init_tokens_op)
                """Train CIFAR-10 for a number of steps."""
                time0 = time.time()
                batch_size_num = FLAGS.batch_size
                for step in range(FLAGS.max_steps):
                    start_time = time.time()
                    run_options = tf.RunOptions(
                        trace_level=tf.RunOptions.FULL_TRACE)
                    run_metadata = tf.RunMetadata()
                    num_batches_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN / batch_size_num
                    decay_steps_num = int(num_batches_per_epoch *
                                          NUM_EPOCHS_PER_DECAY)
                    _, loss_value, gs = sess.run(
                        [train_op, loss, global_step],
                        feed_dict={batch_size: batch_size_num},
                        options=run_options,
                        run_metadata=run_metadata)
                    b = time.time()

                    if step % 1 == 0:
                        duration = time.time() - start_time
                        num_examples_per_step = batch_size_num
                        examples_per_sec = num_examples_per_step / duration
                        sec_per_batch = float(duration)
                        format_str = (
                            "time: " + str(time.time()) +
                            '; %s: step %d (global_step %d), loss = %.2f (%.1f examples/sec; %.3f sec/batch)'
                        )
                        tf.logging.info(format_str %
                                        (datetime.now(), step, gs, loss_value,
                                         examples_per_sec, sec_per_batch))
Ejemplo n.º 21
0
    LR = tf.placeholder(tf.float32, name='learning_rate')

    # clip logits between [0, 1] because that's the range of the labels
    if NETWORK == 'inception':
        print 'Using inception'
        with slim.arg_scope(
                inception_resnet_v2.inception_resnet_v2_arg_scope()):
            logits, _ = inception_resnet_v2.inception_resnet_v2(
                images, num_classes=37, is_training=True)
            logits = tf.minimum(tf.maximum(0.0, logits), 1.0)
    if NETWORK == 'alexnet':
        print 'Using alexnet'
        with slim.arg_scope(alexnet.alexnet_v2_arg_scope()):
            logits, _ = alexnet.alexnet_v2(images,
                                           num_classes=37,
                                           is_training=True)
            logits = tf.minimum(tf.maximum(0.0, logits), 1.0)
    print 'Done.'

    loss = tf.reduce_mean(tf.nn.l2_loss(logits - labels))

    tf.summary.scalar('loss', loss)
    summary_writer = tf.summary.FileWriter(CHECKPOINT_DIR + '/' + 'logs/',
                                           graph=tf.get_default_graph())
    merged_summary_op = tf.summary.merge_all()

    train_op = tf.train.AdamOptimizer(learning_rate=LR).minimize(
        loss, global_step=global_step)

    saver = tf.train.Saver(max_to_keep=1)
Ejemplo n.º 22
0
datagen = Data()
batch_generate = datagen.get_batch(imagefilepath)

batch_xs, batch_ys = next(batch_generate)
imshow(batch_xs[0])
print(batch_ys)

xs = tf.placeholder(tf.float32, [None, 512, 512, 3])
ys = tf.placeholder(tf.float32, [None, 6])
keep_prob_tensor = tf.placeholder(tf.float32)
learning_rate_tensor = tf.placeholder(tf.float32)
global_step = tf.Variable(0, name='global_step', trainable=False)
define_model = False
log_path = '/data3/Avery_Jupyter/log'

logit, _ = alexnet.alexnet_v2(xs, num_classes=6, spatial_squeeze=False)
#define loss
loss = slim.losses.softmax_cross_entropy(logit, ys)
#define optimizer for training
optimizer = tf.train.GradientDescentOptimizer(
    learning_rate=learning_rate_tensor)
#define train operator
train_op = slim.learning.create_train_op(loss, optimizer, global_step)

prediction = tf.arg_max(logit, 1)
accuracy = slim.metrics.accuracy(tf.arg_max(logit, 1), tf.arg_max(ys, 1))

#define summary for tensorboard
tf.summary.scalar('Loss', loss)
tf.summary.scalar('accuracy', accuracy)
summary_op = tf.summary.merge_all()
 def alexnet(self, images, is_training):
     return alexnet_v2(images, is_training=is_training)
                                           tf.FixedLenFeature([], tf.string)
                                       })
    img = tf.decode_raw(features["image"], tf.uint8)
    img = tf.reshape(img, [image_pixels, image_pixels, 3])
    img = tf.cast(img, tf.float32)
    label = tf.cast(features["label"], tf.int32)
    return img, label


images = tf.placeholder(tf.float32, [None, image_pixels, image_pixels, 3],
                        name="input/x_input")
labels = tf.placeholder(tf.int64, [None], name="input/y_input")

with slim.arg_scope(alexnet_v2_arg_scope()):
    logits, end_points = alexnet_v2(images,
                                    num_classes=classes,
                                    is_training=True)

exclude = ["alexnet_v2/fc8"]
variables_to_restore = slim.get_variables_to_restore(exclude=exclude)

one_hot_labels = slim.one_hot_encoding(labels, classes)
loss = tf.losses.softmax_cross_entropy(onehot_labels=one_hot_labels,
                                       logits=logits)
total_loss = tf.losses.get_total_loss()
learning_rate = tf.Variable(initial_value=0.0001,
                            trainable=False,
                            name="learning_rate",
                            dtype=tf.float32)
update_learning_rate = tf.assign(learning_rate, learning_rate * 0.8)
train_step = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(
Ejemplo n.º 25
0
 def alexnet(self, images, is_training):
     return alexnet_v2(images, num_classes=self.params.number_of_classes, is_training=is_training)
Ejemplo n.º 26
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)
Ejemplo n.º 27
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
Ejemplo n.º 28
0
def evaluate_image():
    '''Test one image against the saved models and parameters
    '''
    
    # you need to change the directories to yours.
    file = 'validation_data.txt'
    train, train_label = input_helper.get_files(file)    
    with tf.Graph().as_default() as g:
        train_batch, train_label_batch = input_helper.get_batch(train,
                                                          train_label,
                                                          IMG_W,
                                                          IMG_H,
                                                          BATCH_SIZE, 
                                                          CAPACITY)  
        logit, _ = alexnet.alexnet_v2(train_batch, N_CLASSES)
        
        top_k_op = tf.nn.in_top_k(logit, train_label_batch, 1)
        
        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()
      
        summary_writer = tf.summary.FileWriter('./logs/eval', g)
        # you need to change the directories to yours.
        logs_train_dir = './logs/train/' 
                       
        saver = tf.train.Saver()
        
        with tf.Session() as sess:
            
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_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')
            
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            total_sample_count = MAX_STEP * BATCH_SIZE
            true_count = 0  # Counts the number of correct predictions.
            try:
                for step in np.arange(MAX_STEP):
                    if coord.should_stop():
                        break
                    predictions  = sess.run([top_k_op])
                    true_count += np.sum(predictions)
                
                # Compute precision @ 1.
                precision = true_count / total_sample_count
                print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))
                
                summary = tf.Summary()
                summary.ParseFromString(sess.run(summary_op))
                summary.value.add(tag='Precision @ 1', simple_value=precision)
                summary_writer.add_summary(summary, global_step)
            except tf.errors.OutOfRangeError:
                print('Done training -- epoch limit reached')
            finally:
                coord.request_stop()    
            coord.join(threads)