def main(args):
    dset = dataset.Dataset()
    dset.get_datalist(args.dataset_dir,['png', 'jpg'])
    image_set, points_set = dset.gether_data(is_bbox_aug=True)
    print('image_set len %d' %len(image_set))

    with tf.Graph().as_default():
        with tf.Session() as sess:
            model_tool.load_model(sess, args.model)
            image_batch, points_batch = read_tfrecord.convert_from_tfrecord('/home/public/nfs132_1/hanfy/align/ibugs/validationset_bbox_auged.record', 64, 1, is_preprocess=False)

            # image_input = tf.get_default_graph().get_tensor_by_name('IteratorGetNext:0')
            image_input = tf.get_default_graph().get_tensor_by_name('image_input:0')
            training_placeholder = tf.get_default_graph().get_tensor_by_name('is_training:0')
            pts_pred = tf.get_default_graph().get_tensor_by_name('alexnet_v2/fc8/squeezed:0')

            start_time = time.time()
            for i in range(len(image_set)):
                image = image_set[i]
                pts = points_set[i]
                pts = np.reshape(pts, [68, 2])
                image_in = np.reshape(image, [1, 224, 224, 3])
                res = sess.run(pts_pred, feed_dict={image_input:image_in, training_placeholder:False})
                print(np.shape(res))
                print(res)
                res = np.reshape(res, [68, 2])
                # error = np.mean(np.sqrt(np.sum(np.subtract(res, pts))))
                error = np.mean(np.sqrt(np.sum((pts - res)**2, axis=1)))
                visualize.show_points(image, res, dim=2)
                visualize.show_points(image, pts, dim=2, color=(0,0, 255))
                cv2.putText(image_set[i], str(error), (40, 20), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0))
                visualize.show_image(image, 'test', 0)
            duration = time.time() - start_time
            print('%d images total cost %f, average cost %f' %(len(image_set), duration, duration/len(image_set)))
Exemple #2
0
def main(args):
    dset = dataset.Dataset()
    dset.get_datalist(args.dataset_dir,['png', 'jpg'])
    image_set, points_set = dset.gether_data(is_bbox_aug=False)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            # saver = tf.train.Saver(max_to_keep=10)
            model_tool.load_model(sess, args.model)
            # model_tool.show_op_name()

            image_placeholder = tf.get_default_graph().get_tensor_by_name('Placeholder_1:0')
            training_placeholder = tf.get_default_graph().get_tensor_by_name('is_training:0')
            pts_pred = tf.get_default_graph().get_tensor_by_name('alexnet_v2/fc8/squeezed:0')
            print(len(image_set))
            for img in image_set:
                image = []
                image.append(img)
                start_time = time.time()
                result = sess.run(pts_pred, feed_dict={image_placeholder:image, training_placeholder:False})
                duration = time.time() - start_time
                print('%d images total cost %f, average cost %f' %(len(image_set), duration, duration/len(image_set)))

                visualize.show_points(image[0], result[0], dim=1)
                visualize.show_image(image[0], 'img')
Exemple #3
0
def main(args):
    dset = dataset.Dataset()
    dset.get_datalist(args.dataset_dir, ['png', 'jpg'])
    image_set, points_set, _ = dset.gether_data(is_bbox_aug=True)
    print('image_set len %d' % len(image_set))

    shape_mean = np.loadtxt(
        '/home/hanfy/workspace/DL/alignment/align_untouch/shape_mean.txt',
        delimiter=' ')
    shape_std = np.loadtxt(
        '/home/hanfy/workspace/DL/alignment/align_untouch/shape_std.txt',
        delimiter=' ')
    print('mean', shape_mean)
    print('std', shape_std)

    with tf.Graph().as_default():
        with tf.Session() as sess:
            model_tool.load_model(sess, args.model)
            # model_tool.show_op_name()
            # exit(0)
            image_batch, points_batch = read_tfrecord.convert_from_tfrecord(
                '/home/public/nfs132_1/hanfy/align/ibugs/validationset_bbox_auged.record',
                64,
                1,
                is_preprocess=False)

            # image_input = tf.get_default_graph().get_tensor_by_name('IteratorGetNext:0')
            image_input = tf.get_default_graph().get_tensor_by_name(
                'image_input:0')
            training_placeholder = tf.get_default_graph().get_tensor_by_name(
                'is_training:0')
            pts_pred = tf.get_default_graph().get_tensor_by_name(
                'alexnet_v2/fc8/squeezed:0')

            start_time = time.time()
            image_set, points_set = sess.run([image_batch, points_batch])
            results = sess.run([pts_pred],
                               feed_dict={
                                   image_input: image_set,
                                   training_placeholder: False
                               })
            duration = time.time() - start_time
            print('%d images total cost %f, average cost %f' %
                  (len(image_set), duration, duration / len(image_set)))

            points_set = np.reshape(points_set, [-1, 68, 2])
            results = np.reshape(results, [-1, 68, 2])
            # results = np.multiply(np.sum(np.multiply(results, model_tool.std), model_tool.mean), 224)

            # norm_errors, errors = landmark_eval.landmark_error(points_set, results, show_results=True)
            # norm_errors, errors = landmark_eval.landmark_error(points_set, points_set, show_results=True)
            # landmark_eval.auc_error(norm_errors, 0.2, showCurve=True)

            for i in range(len(image_set)):
                print('pts: ', len(results[i]))
                pred_shape = np.multiply(
                    np.add(np.multiply(results[i], shape_std), shape_mean) +
                    0.5, 224.0)

                # visualize.show_points(image_set[i], results[i], dim=2)
                visualize.show_points(image_set[i], pred_shape, dim=2)
                # visualize.show_points(image_set[i], points_set[i], dim=2)
                visualize.show_points(image_set[i],
                                      points_set[i],
                                      dim=2,
                                      color=(0, 0, 255))
                # cv2.putText(image_set[i], str(errors[i]), (40, 20), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0))
                visualize.show_image(image_set[i], 'test', 0)
Exemple #4
0
def main(args):
    os.makedirs(args.result_dir, exist_ok=True)
    img_result_dir = os.path.join(args.result_dir, 'img_result')
    os.makedirs(img_result_dir, exist_ok=True)

    dset = dataset.Dataset()
    # dset.get_datalist(args.dataset_dir,['png', 'jpg'])
    # image_set, points_set, _ = dset.gether_data(is_bbox_aug=False)
    # image_set, points_set = dset.read_hdf5(args.test_file)

    # image_set, points_set = dset.read_hdf5('/home/public/nfs132_1/hanfy/align/ibugs/trainset.hdf5')
    # points_set = points_set.reshape(len(points_set), 68*2)

    tmp_dir = '/home/public/nfs132_1/hanfy/align_data/ibugs/testset'
    dset = dataset.Dataset()
    dset.get_datalist(tmp_dir, ['png', 'jpg'])
    image_set, points_set, _ = dset.gether_data()

    with tf.Graph().as_default():
        with tf.Session() as sess:
            model_tool.load_model(sess, args.model)
            # model_tool.show_op_name()

            image_input = tf.get_default_graph().get_tensor_by_name(
                'image_ph:0')
            training_placeholder = tf.get_default_graph().get_tensor_by_name(
                'is_training:0')
            pts_pred = tf.get_default_graph().get_tensor_by_name(
                'alexnet_v2/fc8/squeezed:0')
            print(len(image_set))

            errors = []
            for i in range(len(image_set)):
                image = []
                img = image_set[i]
                image.append(img)
                start_time = time.time()
                result = sess.run(pts_pred,
                                  feed_dict={
                                      image_input: image,
                                      training_placeholder: False
                                  })
                duration = time.time() - start_time
                print('%d images total cost %f, average cost %f' %
                      (len(image_set), duration, duration / len(image_set)))
                res = np.reshape(result, [68, 2])
                pts = points_set[i]
                pts = np.reshape(pts, [68, 2])
                # error = np.mean(np.sum(np.sqrt(np.subtract(res, pts))))
                error = np.mean(np.sqrt(np.sum((pts - res)**2, axis=1)))
                errors.append(error)
                visualize.show_points(img, res, dim=2, color=(0, 0, 255))
                visualize.show_points(img, pts, dim=2)
                cv2.putText(img, str(error), (40, 20),
                            cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0))
                # visualize.show_image(img, 'test', 30)
                # cv2.imwrite(args.result_dir + '/' + str(i) + '.jpg', img)
                cv2.imwrite(os.path.join(img_result_dir, str(i) + '.jpg'), img)

            auc, failure_rate = eval_tools.auc_error(errors,
                                                     args.failure_threshold,
                                                     save_path=os.path.join(
                                                         args.result_dir,
                                                         'auc.jpg'))
            mean_error = np.mean(errors)
            print('mean_error', mean_error)
            with open(args.result, 'a+') as fp:
                fp.write("mean error: {0}".format(mean_error) + '\n')
                fp.write("AUC @ {0}: {1}".format(args.failure_threshold, auc) +
                         '\n')
                fp.write("Failure rate: {0}".format(failure_rate) + '\n')
Exemple #5
0
def main(args):

    os.makedirs(args.model_dir, exist_ok=True)
    os.makedirs(args.log_dir, exist_ok=True)

    with tf.Graph().as_default():
        val_image, val_pts = read_tfrecord.convert_from_tfrecord(
            args.val_file,
            batch_size=448,
            is_preprocess=False,
            is_shuffle=False)

        image_batch, points_batch = read_tfrecord.convert_from_tfrecord(
            args.input_file, args.batch_size, args.num_epochs)
        image_batch = tf.identity(image_batch, 'image_input')
        points_batch = tf.identity(points_batch, 'pts_input')
        # placeholders
        is_training = tf.placeholder(tf.bool, name='is_training')

        # construct loss
        inference, _ = net.inference(image_batch, args.num_landmarks * 2,
                                     is_training, args.dropout_keep_prob)
        # loss = tf.reduce_mean(loss_func.NormRmse(gtLandmarks=points_batch, predLandmarks=inference, num_points=args.num_landmarks))
        loss = loss_func.wing_loss(gtLandmarks=points_batch,
                                   predLandmarks=inference,
                                   num_points=args.num_landmarks)
        tf.summary.scalar('wing_loss', loss)
        optimizer = tf.train.AdamOptimizer(args.learning_rate).minimize(
            loss,
            var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                       'alexnet_v2'))

        Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=10)
        merged = tf.summary.merge_all()
        Writer = tf.summary.FileWriter(args.log_dir, tf.get_default_graph())
        with tf.Session() as sess:
            # test
            img_val, pts_val = sess.run([val_image, val_pts])
            # img_batch, pts_batch = sess.run([image_batch, points_batch])
            # for i in range(len(img_batch)):
            #     visualize.show_points(img_batch[i], pts_batch[i], dim=1)
            #     visualize.show_image(img_batch[i], 'img', 0)
            #     visualize.show_points(img_val[i], pts_val[i], dim=1)
            #     visualize.show_image(img_val[i], 'val', 0)

            sess.run(tf.global_variables_initializer())
            sess.run(tf.local_variables_initializer())

            step = 0
            if args.pretrained_model_dir is not None:
                step = int(
                    model_tool.load_model(sess,
                                          model_dir=args.pretrained_model_dir))

            try:
                while True:
                    start_time = time.time()
                    _, lm_loss, summary = sess.run(
                        [optimizer, loss, merged],
                        feed_dict={is_training: args.is_training})
                    duration = time.time() - start_time
                    print('step: [%d]\tTime %.3f\tLoss %2.3f' %
                          (step, duration, lm_loss))
                    Writer.add_summary(summary, step)
                    step += 1
                    if step % 1000 == 0:
                        pred_pts = sess.run([inference],
                                            feed_dict={
                                                image_batch: img_val,
                                                is_training: False
                                            })
                        pred_pts = np.reshape(pred_pts, [len(pts_val), 68, 2])
                        pts_val = np.reshape(pts_val, [len(pts_val), 68, 2])
                        for i in range(20):
                            img = img_val[i]
                            # print(np.shape(img))
                            # print(np.shape(pred_pts))
                            # print(np.shape(pts_val))
                            visualize.show_points(img,
                                                  pred_pts[i],
                                                  color=(0, 0, 255))
                            visualize.show_points(img,
                                                  pts_val[i],
                                                  color=(0, 255, 0))
                            visualize.show_image(img, name='val', waitkey=100)
                        cv2.destroyWindow('val')
                        eval_tools.landmark_error(pts_val, pred_pts)

                        Saver.save(sess,
                                   args.model_dir + '/model',
                                   global_step=step)
            except tf.errors.OutOfRangeError:
                print('Done training for %d epochs, %d steps.' %
                      (args.num_epochs, step))
            Writer.close()
Exemple #6
0
def main(args):
    os.makedirs(args.model_dir, exist_ok=True)
    os.makedirs(args.log_dir, exist_ok=True)
    os.makedirs(args.mid_result_dir, exist_ok=True)
    dset = dataset.Dataset()
    image_set, points_set = dset.read_hdf5(args.input_file)
    points_set = points_set.reshape((len(points_set), 68 * 2))

    imgs_val, shapes_val = dset.read_hdf5(args.test_file)
    shapes_val = shapes_val.reshape(len(shapes_val), 68 * 2)

    with tf.Graph().as_default():
        global_steps = tf.Variable(0, trainable=False)

        # placeholders
        lr_ph = tf.placeholder(tf.float32, name='learning_rate_ph')
        images = tf.placeholder(tf.float32, [None, 224, 224, 3],
                                name='image_ph')
        points_gt = tf.placeholder(tf.float32, [None, args.num_landmarks * 2],
                                   name='points_gt_ph')
        is_training = tf.placeholder(tf.bool, name='is_training')

        # construct loss
        inference, _ = net.inference(images, args.num_landmarks * 2,
                                     is_training, args.dropout_keep_prob)
        with tf.variable_scope('squeezenet'):
            loss = tf.reduce_mean(
                loss_func.NormRmse(gtLandmarks=points_gt,
                                   predLandmarks=inference,
                                   num_points=args.num_landmarks))
            # loss = tf.reduce_mean(loss_func.l1_loss(gtLandmarks=points_gt, predLandmarks=inference))
            # loss = tf.reduce_mean(loss_func.smooth_l1_loss(gtLandmarks=points_gt, predLandmarks=inference, num_points=args.num_landmarks))
            tf.summary.scalar('landmark_loss', loss)
        with tf.control_dependencies(
                tf.get_collection(tf.GraphKeys.UPDATE_OPS, 'squeezenet')):
            optimizer = tf.train.AdamOptimizer(0.001).minimize(
                loss,
                var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                           'alexnet_v2'))

        Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=1)
        merged = tf.summary.merge_all()
        Writer = tf.summary.FileWriter(args.log_dir, tf.get_default_graph())

        mid_result_path = os.path.join(args.mid_result_dir, 'result.txt')
        print(mid_result_path)
        min_error = float('inf')
        with open(mid_result_path, 'a+') as mid_result:
            results = np.loadtxt(mid_result_path)
            if len(results) != 0:
                min_error = np.min(results[:, 1])

            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                step = 0
                if args.pretrained_model_dir is not None:
                    step = int(
                        model_tool.load_model(
                            sess, model_dir=args.pretrained_model_dir))

                print('min_error', min_error)
                for epoch in range(args.epochs):
                    batch_id = 0
                    while batch_id < args.epoch_size:
                        RandomIdx = np.random.choice(image_set.shape[0],
                                                     args.batch_size, False)
                        start_time = time.time()
                        summary, _, lm_loss = sess.run(
                            [merged, optimizer, loss],
                            feed_dict={
                                images: image_set[RandomIdx],
                                points_gt: points_set[RandomIdx],
                                is_training: args.is_training
                            })
                        Writer.add_summary(summary, step)
                        duration = time.time() - start_time
                        print('Epoch: [%d][%d/%d]\tTime %.3f\tLoss %2.3f' %
                              (epoch, batch_id + 1, args.epoch_size, duration,
                               lm_loss))

                        batch_id += 1
                        step += 1
                        if batch_id % 100 == 0:
                            pred_shapes = sess.run([inference],
                                                   feed_dict={
                                                       images: imgs_val,
                                                       is_training: False
                                                   })
                            pred_shapes = np.reshape(pred_shapes,
                                                     [len(shapes_val), 68, 2])
                            pts_val = np.reshape(shapes_val,
                                                 [len(shapes_val), 68, 2])
                            norm_errors, errors = eval_tools.landmark_error(
                                pts_val, pred_shapes)
                            mean_error = np.mean(errors)
                            mid_result.write(
                                "{0} {1}".format(step, str(mean_error)) + '\n')
                            if mean_error < min_error:
                                min_error = mean_error
                                print('saving model...')
                                Saver.save(sess,
                                           args.model_dir + '/model',
                                           global_step=step)
                Writer.close()
Exemple #7
0
def main(args):
    trainset = args.trainset
    testset = args.testset
    img_size = args.img_size

    meanShape = np.genfromtxt('../../meanshape_untouch.txt')
    meanShape = np.reshape(meanShape, [164]).astype(np.float32) * img_size

    with tf.Graph().as_default():
        step = 0
        # if args.pretrained_model_dir is not None:
        #     step = int(model_tool.load_model(tf.Session(), model_dir=args.pretrained_model_dir))
        # model_tool.show_var_name()
        # print("********************************************")

        global_steps = tf.Variable(step, trainable=False)
        train_queue = tf.train.string_input_producer([trainset])
        test_queue = tf.train.string_input_producer([testset], num_epochs=1)
        images_train, points_train = load_tfrecord(
            train_queue,
            pts_num=82,
            img_shape=[img_size, img_size, 1],
            batch_size=args.batch_size,
            is_shuffle=True)
        images_test, points_test = load_tfrecord(
            test_queue,
            pts_num=82,
            img_shape=[img_size, img_size, 1],
            batch_size=128,
            is_shuffle=False)

        lr_ph = tf.placeholder(tf.float32, name='learning_rate_ph')
        imgs_ph = tf.placeholder(tf.float32, [None, img_size, img_size, 1],
                                 'images_ph')
        pts_ph = tf.placeholder(tf.float32, [None, 164], 'points_ph')
        is_train_ph = tf.placeholder(tf.bool, name='is_train')

        learning_rate = tf.train.exponential_decay(
            lr_ph,
            global_step=global_steps,
            decay_steps=args.learning_rate_decay_steps,
            decay_rate=args.learning_rate_decay_rate,
            staircase=True)

        pts_pre = inference(imgs_ph,
                            meanshape=meanShape,
                            pts_num=82,
                            is_training=is_train_ph)
        model_tool.show_var_name()
        print("********************************************")

        # loss = tf.reduce_mean(loss_func.NormRmse(pts_ph, pts_pre, 82))
        loss = tf.reduce_mean(loss_func.smooth_l1_loss(pts_ph, pts_pre, 82))

        error_test = tf.reduce_mean(loss_func.NormRmse(pts_ph, pts_pre, 82))
        opt = tf.train.AdamOptimizer(learning_rate=learning_rate)
        optimizer = opt.minimize(loss, global_step=global_steps)
        opt.compute_gradients(loss)

        Saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=10)
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())

        tf.summary.scalar('loss', loss)
        merged = tf.summary.merge_all()
        Writer = tf.summary.FileWriter(args.log_dir, tf.get_default_graph())

        mid_result_path = os.path.join(args.mid_result_dir, 'train_result.txt')
        min_error = float('inf')

        with open(mid_result_path, 'a+') as mid_result:
            results = np.loadtxt(mid_result_path)
            if len(results) != 0:
                min_error = np.min(results[:, 1])
            print('min_error', min_error)

            with tf.Session() as sess:
                sess.run(init_op)
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(coord=coord)

                # load pre_models
                if args.pretrained_model_dir is not None:
                    step = int(
                        model_tool.load_model(
                            sess, model_dir=args.pretrained_model_dir))
                model_tool.show_var_name()
                print("********************************************")
                # exit(0)
                imgs_test, pts_test = sess.run([images_test, points_test])
                while True:
                    step = sess.run(global_steps, feed_dict=None)
                    if step % 100 == 0:
                        summary, error = sess.run(
                            [merged, error_test],
                            feed_dict={
                                imgs_ph: imgs_test,
                                pts_ph: pts_test,
                                is_train_ph: False
                            })
                        mid_result.write("{0} {1}".format(step, str(error)) +
                                         '\n')
                        print('evaluate on testset -> step: %d, loss: %2.4f' %
                              (step, error))
                        if error < min_error:
                            min_error = error
                            print('saving model...')
                            Saver.save(sess,
                                       args.model_dir + '/model',
                                       global_step=step)

                    imgs_train, pts_train = sess.run(
                        [images_train, points_train])
                    summary, step, _, error, lr = sess.run(
                        [merged, global_steps, optimizer, loss, learning_rate],
                        feed_dict={
                            imgs_ph: imgs_train,
                            pts_ph: pts_train,
                            is_train_ph: True,
                            lr_ph: args.learning_rate
                        })
                    Writer.add_summary(summary, step)
                    print('step: %d, loss: %2.4f, lr: %2.7f' %
                          (step, error, lr))
                coord.request_stop()
                coord.join(threads)
    sess = tf.Session()

    image_size = 64
    embedding_size = 128
    network = importlib.import_module('network.squeezenet_v11')
    image_placeholder = tf.placeholder(dtype=tf.float32,
                                       shape=[None, image_size, image_size, 1],
                                       name='input_image')

    prelogits, _ = network.inference(image_placeholder,
                                     keep_probability=1.0,
                                     phase_train=False,
                                     bottleneck_layer_size=embedding_size,
                                     weight_decay=0.0)
    logits = slim.fully_connected(
        prelogits,
        36,
        activation_fn=None,
        weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
        weights_regularizer=slim.l2_regularizer(0.0),
        scope='Logits',
        reuse=False)
    logits = tf.identity(logits, 'squeezenet/output_logits_stu')
    output = tf.nn.softmax(logits=logits, name="squeezenet/output_softmax_stu")
    # embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='student_embeddings')
    saver = model_tool.load_model(sess, model_dir)
    saver.save(
        sess,
        '/home/public/nfs72/hanfy/models/hand/fine_model/hand_36_no_fused_squeezenet-20180621-115258/renamed/hand_36_no_fused_squeezenet-20180621-115258'
    )