Beispiel #1
0
def main():
    parser = argparse.ArgumentParser(description='VGG Fine Tune')
    parser.add_argument('--batch-size',
                        type=int,
                        default=20,
                        help='input batch size for training')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.0001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1, help='random seed')
    parser.add_argument('--log-interval',
                        type=int,
                        default=60,
                        help='how many batches to wait before'
                        ' logging training status')
    parser.add_argument('--eval-interval',
                        type=int,
                        default=60,
                        help='how many batches to wait before'
                        ' evaluate the model')
    parser.add_argument('--log-dir',
                        type=str,
                        default='tb',
                        help='path for logging directory')
    parser.add_argument('--data-dir',
                        type=str,
                        default='./data/VOCdevkit/VOC2007',
                        help='Path to PASCAL data storage')
    args = parser.parse_args()
    util.set_random_seed(args.seed)
    sess = util.set_session()

    train_images, train_labels, train_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='trainval')
    test_images, test_labels, test_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='test')

    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels, train_weights))
    train_dataset = train_dataset.map(augment_train_data)
    train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)

    test_dataset = tf.data.Dataset.from_tensor_slices(
        (test_images, test_labels, test_weights))
    test_dataset = test_dataset.map(center_crop_test_data)
    test_dataset = test_dataset.batch(args.batch_size)

    model = VGG(num_classes=len(CLASS_NAMES))

    logdir = os.path.join(args.log_dir,
                          datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    if os.path.exists(logdir):
        shutil.rmtree(logdir)
    os.makedirs(logdir)
    writer = tf.contrib.summary.create_file_writer(logdir)
    writer.set_as_default()

    tf.contrib.summary.initialize()

    global_step = tf.train.get_or_create_global_step()

    train_log = {'iter': [], 'loss': [], 'accuracy': []}
    test_log = {'iter': [], 'loss': [], 'accuracy': []}

    ckpt_dir = 'pascal_vgg_ft'
    ckpt_prefix = os.path.join(ckpt_dir, 'ckpt')
    if not os.path.exists(ckpt_dir):
        os.makedirs(ckpt_dir)

    # Build model first to load weights
    input_shape = tf.TensorShape([None, 224, 224, 3])
    model.build(input_shape)

    model.load_weights('vgg16_weights_tf_dim_ordering_tf_kernels.h5',
                       by_name=True)

    # Print layer names in saved weights
    # f = h5py.File('vgg16_weights_tf_dim_ordering_tf_kernels.h5', 'r')

    # # Get the data
    # for i in list(f.keys()):
    # print(i)

    decayed_lr = tf.train.exponential_decay(args.lr,
                                            global_step,
                                            1000,
                                            0.5,
                                            staircase=True)
    optimizer = tf.train.MomentumOptimizer(learning_rate=decayed_lr(),
                                           momentum=0.9)

    root = tf.train.Checkpoint(optimizer=optimizer, model=model)

    for ep in range(args.epochs):
        epoch_loss_avg = tfe.metrics.Mean()
        for batch, (images, labels, weights) in enumerate(train_dataset):
            loss_value, grads = util.cal_grad(
                model,
                loss_func=tf.losses.sigmoid_cross_entropy,
                inputs=images,
                targets=labels,
                weights=weights)

            grads_and_vars = zip(grads, model.trainable_variables)
            optimizer.apply_gradients(grads_and_vars, global_step)

            epoch_loss_avg(loss_value)
            if global_step.numpy() % args.log_interval == 0:
                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Training Loss:{3:.4f}'
                    .format(ep, args.epochs, global_step.numpy(),
                            epoch_loss_avg.result()))
                train_log['iter'].append(global_step.numpy())
                train_log['loss'].append(epoch_loss_avg.result())

                with tf.contrib.summary.always_record_summaries():
                    tf.contrib.summary.scalar('Training Loss', loss_value)
                    tf.contrib.summary.image('RGB', images)
                    tf.contrib.summary.scalar('LR', decayed_lr())

                    for i, variable in enumerate(model.trainable_variables):
                        tf.contrib.summary.histogram("grad_" + variable.name,
                                                     grads[i])

            if global_step.numpy() % args.eval_interval == 0:
                test_AP, test_mAP = util.eval_dataset_map(model, test_dataset)
                test_loss = test(model, test_dataset)
                print("mAP: ", test_mAP)
                print("Test Loss: ", test_loss)
                # print("Loss: %.4f, Acc: %.4f, mAP: %.4f", test_lotest_mAP)
                with tf.contrib.summary.always_record_summaries():
                    tf.contrib.summary.scalar('Test mAP', test_mAP)
                    tf.contrib.summary.scalar('Test Loss', test_loss)

        if ep % 2 == 0:
            root.save(ckpt_prefix)

    root.save(ckpt_prefix)
    model.summary()

    AP, mAP = util.eval_dataset_map(model, test_dataset)
    rand_AP = util.compute_ap(test_labels,
                              np.random.random(test_labels.shape),
                              test_weights,
                              average=None)
    print('Random AP: {} mAP'.format(np.mean(rand_AP)))
    gt_AP = util.compute_ap(test_labels,
                            test_labels,
                            test_weights,
                            average=None)
    print('GT AP: {} mAP'.format(np.mean(gt_AP)))
    print('Obtained {} mAP'.format(mAP))
    print('Per class:')
    for cid, cname in enumerate(CLASS_NAMES):
        print('{}: {}'.format(cname, util.get_el(AP, cid)))
def main():
    parser = argparse.ArgumentParser(
        description='TensorFlow Fashion MNIST Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=100,
                        help='input batch size for training')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1, help='random seed')
    parser.add_argument('--log-interval',
                        type=int,
                        default=5,
                        help='how many batches to wait before'
                        ' logging training status')
    parser.add_argument('--eval-interval',
                        type=int,
                        default=100,
                        help='how many batches to wait before'
                        ' evaluate the model')
    parser.add_argument('--log-dir',
                        type=str,
                        default='tb',
                        help='path for logging directory')
    parser.add_argument('--ckpt-dir',
                        type=str,
                        default='ckpt',
                        help='path for saving model')

    args = parser.parse_args()
    start_time = time.time()
    util.set_random_seed(args.seed)
    sess = util.set_session()

    fashion_mnist = keras.datasets.fashion_mnist
    class_names = [
        'T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal',
        'Shirt', 'Sneaker', 'Bag', 'Ankle boot'
    ]
    (train_images, train_labels), (test_images,
                                   test_labels) = fashion_mnist.load_data()

    train_images, train_labels = preprocess_data(train_images, train_labels)
    test_images, test_labels = preprocess_data(test_images, test_labels)

    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels))
    train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)
    test_dataset = tf.data.Dataset.from_tensor_slices(
        (test_images, test_labels))
    test_dataset = test_dataset.shuffle(10000).batch(args.batch_size)
    train_dataset_mix = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels))
    train_dataset_mix = train_dataset.shuffle(10000).batch(args.batch_size)

    model = SimpleCNN(num_classes=len(class_names))

    global_step = tf.train.get_or_create_global_step()
    optimizer = tf.train.AdamOptimizer(learning_rate=args.lr)
    train_log = {'iter': [], 'loss': [], 'accuracy': []}
    test_log = {'iter': [], 'loss': [], 'accuracy': []}
    for ep in range(args.epochs):
        epoch_loss_avg = tfe.metrics.Mean()
        epoch_accuracy = tfe.metrics.Accuracy()
        for batch, ((images, labels), (images_mix, labels_mix)) in enumerate(
                zip(train_dataset, train_dataset_mix)):
            lamb = np.random.beta(2., 2., args.batch_size)
            images = images * lamb[:, np.newaxis, np.newaxis,
                                   np.newaxis] + images_mix * (
                                       1 - lamb)[:, np.newaxis, np.newaxis,
                                                 np.newaxis]
            labels = labels * lamb + labels_mix * (1. - lamb)
            loss_value, grads = util.cal_grad(
                model,
                loss_func=tf.losses.sparse_softmax_cross_entropy,
                inputs=images,
                targets=labels)
            optimizer.apply_gradients(zip(grads, model.trainable_variables),
                                      global_step)
            epoch_loss_avg(loss_value)
            epoch_accuracy(
                tf.argmax(model(images), axis=1, output_type=tf.int32), labels)
            if global_step.numpy() % args.log_interval == 0:
                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Training Loss:{3:.4f}  '
                    'Training Accuracy:{4:.4f}'.format(
                        ep, args.epochs, global_step.numpy(),
                        epoch_loss_avg.result(), epoch_accuracy.result()))
                train_log['iter'].append(global_step.numpy())
                train_log['loss'].append(epoch_loss_avg.result())
                train_log['accuracy'].append(epoch_accuracy.result())
            if global_step.numpy() % args.eval_interval == 0:
                test_loss, test_acc = test(model, test_dataset)
                test_log['iter'].append(global_step.numpy())
                test_log['loss'].append(test_loss)
                test_log['accuracy'].append(test_acc)

    model.summary()
    end_time = time.time()
    print('Elapsed time: {0:.3f}s'.format(end_time - start_time))
    predict(model, test_images[:5], class_names)
    fig = plt.figure()
    plt.plot(train_log['iter'], train_log['loss'], 'r', label='Training')
    plt.plot(test_log['iter'], test_log['loss'], 'b', label='Testing')
    plt.title('Loss')
    plt.legend()
    plt.savefig('dynamic_loss_10.png')
    fig = plt.figure()
    plt.plot(train_log['iter'], train_log['accuracy'], 'r', label='Training')
    plt.plot(test_log['iter'], test_log['accuracy'], 'b', label='Testing')
    plt.title('Accuracy')
    plt.legend()
    plt.savefig('dynamic_accuracy_10.png')
    plt.show()
def main():
    parser = argparse.ArgumentParser(description='TensorFlow Pascal Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=20,
                        help='input batch size for training')
    parser.add_argument('--epochs',
                        type=int,
                        default=10,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.0001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1, help='random seed')
    parser.add_argument('--log-interval',
                        type=int,
                        default=10,
                        help='how many batches to wait before'
                        ' logging training status')
    parser.add_argument('--eval-interval',
                        type=int,
                        default=60,
                        help='how many batches to wait before'
                        ' evaluate the model')
    parser.add_argument('--log-dir',
                        type=str,
                        default='tb/05',
                        help='path for logging directory')
    parser.add_argument('--data-dir',
                        type=str,
                        default='./VOCdevkit/VOC2007',
                        help='Path to PASCAL data storage')
    parser.add_argument('--checkpoint-dir',
                        type=str,
                        default='./checkpoints/06',
                        help='Path to checkpoints storage')
    parser.add_argument(
        '--save-interval',
        type=int,
        default=2,
        help='How many batch to wait before storing checkpoints')
    parser.add_argument(
        '--pretrain-dir',
        type=str,
        default=
        './pre_trained_model/vgg16_weights_tf_dim_ordering_tf_kernels.h5',
        help='path the pretrained model')
    parser.add_argument('--scratch-dir',
                        type=str,
                        default='./checkpoints/04/ckpt.h5',
                        help='path the scratched model')
    args = parser.parse_args()
    util.set_random_seed(args.seed)
    sess = util.set_session()

    model = SimpleCNN(pretrain_dir=args.pretrain_dir,
                      scratch_dir=args.scratch_dir,
                      num_classes=len(CLASS_NAMES))

    train_images, train_labels, train_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='trainval')
    test_images, test_labels, test_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='test')
    # np.random.seed(1)
    # images_mix = train_images
    # np.random.shuffle(images_mix)

    # np.random.seed(1)
    # labels_mix = train_labels
    # np.random.shuffle(labels_mix)

    # np.random.seed(1)
    # weights_mix = train_weights
    # np.random.shuffle(weights_mix)

    # lamb = np.random.beta(2., 2.)

    # train_images=train_images * lamb + images_mix * (1-lamb)
    # train_labels=train_labels * lamb + labels_mix * (1-lamb)
    # train_weights=train_weights * lamb + weights_mix * (1-lamb)

    ## TODO modify the following code to apply data augmentation here
    print('start_loading!')
    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels, train_weights))
    train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)

    test_dataset = tf.data.Dataset.from_tensor_slices(
        (test_images, test_labels, test_weights))
    test_dataset = test_dataset.batch(50)

    train_dataset_mix = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels, train_weights))
    train_dataset_mix = train_dataset_mix.shuffle(10000).batch(args.batch_size)

    logdir = os.path.join(args.log_dir,
                          datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    if os.path.exists(logdir):
        shutil.rmtree(logdir)
    os.makedirs(logdir)
    writer = tf.contrib.summary.create_file_writer(logdir)
    writer.set_as_default()

    ## TODO write the training and testing code for multi-label classification
    global_step = tf.train.get_or_create_global_step()
    learning_rate_decay = tf.train.exponential_decay(args.lr, global_step,
                                                     1000, 0.5)
    optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate_decay,
                                           momentum=0.9)

    train_log = {'iter': [], 'loss': []}
    test_log = {'iter': [], 'loss': [], 'accuracy': []}
    print('start training!')

    for ep in range(args.epochs):
        epoch_loss_avg = tfe.metrics.Mean()
        # epoch_accuracy = tfe.metrics.Accuracy()
        for batch, ((images, labels, weights),
                    (images_mix, labels_mix, weights_mix)) in enumerate(
                        zip(train_dataset, train_dataset_mix)):
            # print(labels - labels_mix)

            labels = tf.cast(labels, tf.float32)

            labels_mix = tf.cast(labels_mix, tf.float32)

            weights = tf.cast(weights, tf.float32)

            weights_mix = tf.cast(weights_mix, tf.float32)

            lamb_size = images.shape[0]

            lamb = np.random.beta(0.2, 0.2, lamb_size)

            # print(lamb)

            images = images * lamb[:, np.newaxis, np.newaxis,
                                   np.newaxis] + images_mix * (
                                       1 - lamb)[:, np.newaxis, np.newaxis,
                                                 np.newaxis]
            # print(images.shape)
            weights = weights * lamb[:, np.newaxis] + weights_mix * (
                1. - lamb)[:, np.newaxis]

            labels = labels * lamb[:, np.newaxis] + labels_mix * (
                1. - lamb)[:, np.newaxis]

            # print(labels * lamb[:, np.newaxis])

            # print(labels.dtype)

            images, labels, weights = mean_normalization(
                images, labels, weights)
            images, labels, weights = randomly_crop(images, labels, weights)
            images, labels, weights = randomly_flip(images, labels, weights)

            # print(images[0])
            # print(labels)
            # print(weights.shape)

            with tf.contrib.summary.record_summaries_every_n_global_steps(100):
                tf.contrib.summary.image("sample_image", images, max_images=3)

            loss_value, grads = util.cal_grad(
                model,
                loss_func=tf.losses.sigmoid_cross_entropy,
                inputs=images,
                targets=labels,
                weights=weights)
            optimizer.apply_gradients(zip(grads, model.trainable_variables),
                                      global_step)
            learning_rate_decay = tf.train.exponential_decay(
                args.lr, global_step, 1000, 0.5)
            with tf.contrib.summary.record_summaries_every_n_global_steps(1):
                tf.contrib.summary.scalar('learning_rate',
                                          learning_rate_decay())

            with tf.contrib.summary.record_summaries_every_n_global_steps(10):
                for grad, var in zip(grads, model.trainable_variables):
                    tf.contrib.summary.histogram(
                        "{}/grad_histogram".format(var.name), grad)

            with tf.contrib.summary.record_summaries_every_n_global_steps(1):
                tf.contrib.summary.scalar('training_loss', loss_value)

            epoch_loss_avg(loss_value)
            if global_step.numpy() % args.log_interval == 0:
                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Training Loss:{3:.4f}'
                    .format(ep, args.epochs, global_step.numpy(),
                            epoch_loss_avg.result()))
                train_log['iter'].append(global_step.numpy())
                train_log['loss'].append(epoch_loss_avg.result())
                # tf.contrib.summary.scalar('training_loss', epoch_loss_avg.result())
                # train_log['accuracy'].append(epoch_accuracy.result())
            if global_step.numpy() % args.eval_interval == 0:
                test_loss, test_acc = test(model, test_dataset)
                with tf.contrib.summary.record_summaries_every_n_global_steps(
                        args.eval_interval):
                    tf.contrib.summary.scalar('testing_acc', test_acc)
                test_log['iter'].append(global_step.numpy())
                test_log['loss'].append(test_loss)
                test_log['accuracy'].append(test_acc)
                # tf.contrib.summary.scalar('testing_loss', test_loss)
                # tf.contrib.summary.scalar('testing_loss', test_acc)
                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Testing Loss:{3:.4f} Testing Accuracy:{4:.4f}'
                    .format(ep, args.epochs, global_step.numpy(), test_loss,
                            test_acc))
        # if global_step.numpy() % args.save_epoch == 0:
        #     checkpoint = tfe.Checkpoint(optimizer=optimizer,
        #                         model=model,
        #                         optimizer_step=tf.train.get_or_create_global_step())
        #     checkpoint_prefix = os.path.join(args.checkpoint_dir, "ckpt")
        #     checkpoint.save(file_prefix=checkpoint_prefix)

    AP, mAP = util.eval_dataset_map(model, test_dataset)
    rand_AP = util.compute_ap(test_labels,
                              np.random.random(test_labels.shape),
                              test_weights,
                              average=None)

    # checkpoint = tfe.Checkpoint(optimizer=optimizer,
    #                             model=model,
    #                             optimizer_step=tf.train.get_or_create_global_step())
    # checkpoint_prefix = os.path.join(args.checkpoint_dir, "ckpt")
    # checkpoint.save(file_prefix=checkpoint_prefix)
    checkpoint_prefix = os.path.join(args.checkpoint_dir, "ckpt.h5")
    model.save_weights(checkpoint_prefix)

    print('Random AP: {} mAP'.format(np.mean(rand_AP)))
    gt_AP = util.compute_ap(test_labels,
                            test_labels,
                            test_weights,
                            average=None)
    print('GT AP: {} mAP'.format(np.mean(gt_AP)))
    print('Obtained {} mAP'.format(mAP))
    print('Per class:')
    for cid, cname in enumerate(CLASS_NAMES):
        print('{}: {}'.format(cname, util.get_el(AP, cid)))
    writer.close()
Beispiel #4
0
def main():
    parser = argparse.ArgumentParser(description='TensorFlow Pascal Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=20,
                        help='input batch size for training')
    parser.add_argument('--epochs',
                        type=int,
                        default=5,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1, help='random seed')
    parser.add_argument('--log-interval',
                        type=int,
                        default=10,
                        help='how many batches to wait before'
                        ' logging training status')
    parser.add_argument('--eval-interval',
                        type=int,
                        default=20,
                        help='how many batches to wait before'
                        ' evaluate the model')
    parser.add_argument('--log-dir',
                        type=str,
                        default='tb',
                        help='path for logging directory')
    parser.add_argument('--data-dir',
                        type=str,
                        default='./VOCdevkit/VOC2007',
                        help='Path to PASCAL data storage')
    args = parser.parse_args()
    util.set_random_seed(args.seed)
    sess = util.set_session()
    img_save_interval = 200

    train_images, train_labels, train_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='trainval')
    test_images, test_labels, test_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='test')

    ## TODO modify the following code to apply data augmentation here
    ori_h = train_images.shape[1]
    ori_w = train_images.shape[2]
    crop_h = 224
    crop_w = 224
    central_fraction = 0.7

    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels, train_weights))
    test_dataset = tf.data.Dataset.from_tensor_slices(
        (test_images, test_labels, test_weights))

    train_dataset_aug_flip = train_dataset.map(
        lambda img, l, w: (tf.image.random_flip_left_right(img), l, w))
    train_dataset_aug_crop = train_dataset_aug_flip.map(
        lambda img, l, w: (tf.random_crop(img, [crop_h, crop_w, 3]), l, w))

    train_dataset.concatenate(train_dataset_aug_flip)

    test_dataset_aug = test_dataset.map(
        lambda img, l, w: (tf.image.central_crop(img, central_fraction), l, w))
    test_dataset_aug = test_dataset_aug.map(
        lambda img, l, w: (tf.image.resize_images(img, (ori_h, ori_w)), l, w))

    test_dataset.concatenate(test_dataset_aug)

    train_dataset = train_dataset.map(lambda img, l, w:
                                      (img_mean_substract(img), l, w))
    test_dataset = test_dataset.map(lambda img, l, w:
                                    (img_mean_substract(img), l, w))

    train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)
    test_dataset = test_dataset.batch(args.batch_size)

    model = SimpleCNN(num_classes=len(CLASS_NAMES))

    logdir = os.path.join(args.log_dir,
                          datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))

    checkpoint_dir = os.path.join(logdir, "ckpt")

    if os.path.exists(logdir):
        shutil.rmtree(logdir)
    os.makedirs(logdir)
    writer = tf.contrib.summary.create_file_writer(logdir)
    writer.set_as_default()

    ## TODO write the training and testing code for multi-label classification
    global_step = tf.train.get_or_create_global_step()
    learning_rate = tf.train.exponential_decay(args.lr,
                                               global_step,
                                               5000,
                                               0.5,
                                               staircase=True)
    optimizer = tf.train.MomentumOptimizer(learning_rate=learning_rate,
                                           momentum=0.9)
    checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model)
    train_log = {'iter': [], 'loss': [], 'accuracy': []}
    test_log = {'iter': [], 'loss': [], 'accuracy': []}

    for ep in range(args.epochs):
        epoch_loss_avg = tfe.metrics.Mean()

        for batch, (images, labels, weights) in enumerate(train_dataset):

            loss_value, grads = util.cal_grad(
                model,
                loss_func=tf.losses.sigmoid_cross_entropy,
                inputs=images,
                weights=weights,
                targets=labels)
            optimizer.apply_gradients(zip(grads, model.trainable_variables),
                                      global_step)
            epoch_loss_avg(loss_value)

            if global_step.numpy() % args.log_interval == 0:

                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Training Loss:{3:.4f}  '
                    .format(ep, args.epochs, global_step.numpy(),
                            epoch_loss_avg.result()))
                train_log['iter'].append(global_step.numpy())
                train_log['loss'].append(epoch_loss_avg.result())

                # Tensorboard Visualization
                with tf.contrib.summary.always_record_summaries():
                    tf.contrib.summary.scalar('training_loss',
                                              epoch_loss_avg.result())
                    #tf.contrib.summary.scalar('learning_rate', learning_rate())
                    # for grad,var in zip(grads,model.trainable_variables):
                    #     tf.contrib.summary.histogram("gradients_{0}".format(var.name), grad)

            if global_step.numpy() % args.eval_interval == 0:
                with tf.contrib.summary.always_record_summaries():
                    test_AP, test_mAP = util.eval_dataset_map(
                        model, test_dataset)
                    tf.contrib.summary.scalar('test_map', test_mAP)
                    #test_loss = test(test_dataset,model)
                    #tf.contrib.summary.scalar('testing_loss', test_loss)

            # if global_step.numpy() % img_save_interval == 0:
            #     with tf.contrib.summary.always_record_summaries():
            #         tf.contrib.summary.image('training_img', images)

        # Save checkpoints
        checkpoint.save(file_prefix=checkpoint_dir)

    AP, mAP = util.eval_dataset_map(model, test_dataset)
    # For visualization

    rand_AP = util.compute_ap(test_labels,
                              np.random.random(test_labels.shape),
                              test_weights,
                              average=None)
    print('Random AP: {} mAP'.format(np.mean(rand_AP)))
    gt_AP = util.compute_ap(test_labels,
                            test_labels,
                            test_weights,
                            average=None)
    print('GT AP: {} mAP'.format(np.mean(gt_AP)))
    print('Obtained {} mAP'.format(mAP))
    print('Per class:')
    for cid, cname in enumerate(CLASS_NAMES):
        print('{}: {}'.format(cname, util.get_el(AP, cid)))
Beispiel #5
0
def main():
    parser = argparse.ArgumentParser(description='TensorFlow Pascal Example')
    parser.add_argument('--batch-size',
                        type=int,
                        default=20,
                        help='input batch size for training')
    parser.add_argument('--epochs',
                        type=int,
                        default=5,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1, help='random seed')
    parser.add_argument('--log-interval',
                        type=int,
                        default=10,
                        help='how many batches to wait before'
                        ' logging training status')
    parser.add_argument('--eval-interval',
                        type=int,
                        default=50,
                        help='how many batches to wait before'
                        ' evaluate the model')
    parser.add_argument('--log-dir',
                        type=str,
                        default='tb',
                        help='path for logging directory')
    parser.add_argument('--data-dir',
                        type=str,
                        default='./data/VOCdevkit/VOC2007',
                        help='Path to PASCAL data storage')
    args = parser.parse_args()
    util.set_random_seed(args.seed)
    sess = util.set_session()

    train_images, train_labels, train_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='trainval')
    test_images, test_labels, test_weights = util.load_pascal(
        args.data_dir, class_names=CLASS_NAMES, split='test')

    train_dataset = tf.data.Dataset.from_tensor_slices(
        (train_images, train_labels, train_weights))
    train_dataset = train_dataset.map(augment_train_data)
    train_dataset = train_dataset.shuffle(10000).batch(args.batch_size)

    test_dataset = tf.data.Dataset.from_tensor_slices(
        (test_images, test_labels, test_weights))
    test_dataset = test_dataset.map(center_crop_test_data)
    test_dataset = test_dataset.batch(args.batch_size)

    model = SimpleCNN(num_classes=len(CLASS_NAMES))

    logdir = os.path.join(args.log_dir,
                          datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    if os.path.exists(logdir):
        shutil.rmtree(logdir)
    os.makedirs(logdir)
    writer = tf.contrib.summary.create_file_writer(logdir)
    writer.set_as_default()

    tf.contrib.summary.initialize()

    global_step = tf.train.get_or_create_global_step()
    optimizer = tf.train.AdamOptimizer(learning_rate=args.lr)
    train_log = {'iter': [], 'loss': [], 'accuracy': []}
    test_log = {'iter': [], 'loss': [], 'accuracy': []}
    for ep in range(args.epochs):
        epoch_loss_avg = tfe.metrics.Mean()
        for batch, (images, labels, weights) in enumerate(train_dataset):
            loss_value, grads = util.cal_grad(
                model,
                loss_func=tf.losses.sigmoid_cross_entropy,
                inputs=images,
                targets=labels,
                weights=weights)
            optimizer.apply_gradients(zip(grads, model.trainable_variables),
                                      global_step)
            epoch_loss_avg(loss_value)

            with tf.contrib.summary.always_record_summaries():
                tf.contrib.summary.scalar('Training Loss', loss_value)
            if global_step.numpy() % args.log_interval == 0:
                print(
                    'Epoch: {0:d}/{1:d} Iteration:{2:d}  Training Loss:{3:.4f}'
                    .format(ep, args.epochs, global_step.numpy(),
                            epoch_loss_avg.result()))
                train_log['iter'].append(global_step.numpy())
                train_log['loss'].append(epoch_loss_avg.result())
            if global_step.numpy() % args.eval_interval == 0:
                test_AP, test_mAP = util.eval_dataset_map(model, test_dataset)
                print("mAP: ", test_mAP)
                with tf.contrib.summary.always_record_summaries():
                    tf.contrib.summary.scalar('Test mAP', test_mAP)

    model.summary()

    # fig = plt.figure()
    # plt.plot(train_log['iter'], train_log['loss'], 'r', label='Training')
    # plt.plot(test_log['iter'], test_log['loss'], 'b', label='Testing')
    # plt.title('Loss')
    # plt.legend()
    # fig = plt.figure()
    # plt.plot(train_log['iter'], train_log['accuracy'], 'r', label='Training')
    # plt.plot(test_log['iter'], test_log['accuracy'], 'b', label='Testing')
    # plt.title('Accuracy')
    # plt.legend()
    # plt.show()

    AP, mAP = util.eval_dataset_map(model, test_dataset)
    rand_AP = util.compute_ap(test_labels,
                              np.random.random(test_labels.shape),
                              test_weights,
                              average=None)
    print('Random AP: {} mAP'.format(np.mean(rand_AP)))
    gt_AP = util.compute_ap(test_labels,
                            test_labels,
                            test_weights,
                            average=None)
    print('GT AP: {} mAP'.format(np.mean(gt_AP)))
    print('Obtained {} mAP'.format(mAP))
    print('Per class:')
    for cid, cname in enumerate(CLASS_NAMES):
        print('{}: {}'.format(cname, util.get_el(AP, cid)))