예제 #1
0
def inference(image, keep_prob):
    """ fcn_8s """
    # load data
    vgg16_object = vgg16()
    print("setting up vgg initialized conv layers ...")
    model_data = utils.get_model_data(FLAGS.model_dir, MODEL_URL)
    mean = model_data['normalization'][0][0][0]
    mean_pixel = np.mean(mean, axis=(0, 1))
    weights = model_data['layers'][0]

    # preprocess
    processed_image = utils.process_image(image, mean_pixel)

    # deconvolution
    with tf.variable_scope("inference"):
        image_net = vgg16_object._vgg16_modified(processed_image, weights)

        pool4, pool3 = image_net["pool4"], image_net["pool3"]

        conv_final_layer = image_net["conv5_3"]
        """ pool5 """
        pool5 = vgg16_object._max_pool(conv_final_layer, 2, 2, 'pool5')
        """ fc6 """
        W6 = utils.weight_variable([7, 7, 512, 4096], name="W6")
        b6 = utils.bias_variable([4096], name="b6")
        fc6 = vgg16_object._conv(pool5, W6, b6, 'fc6')
        relu6 = vgg16_object._relu(fc6, 'relu6')
        fc6 = tf.nn.dropout(relu6, keep_prob)
        """ fc7 """
        W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7")
        b7 = utils.bias_variable([4096], name="b7")
        fc7 = vgg16_object._conv(fc6, W7, b7, 'fc7')
        relu7 = vgg16_object._relu(fc7, 'relu7')
        fc7 = tf.nn.dropout(relu7, keep_prob)
        """ fc8 """
        W8 = utils.weight_variable([1, 1, 4096, 21], name="W8")
        b8 = utils.bias_variable([21], name="b8")
        fc8 = vgg16_object._conv(fc7, W8, b8, 'fc8')

        # upsample and add with pool4(pool4 should pass through a score layer)
        score_w1 = utils.weight_variable([1, 1, 512, 21], name="score_w1")
        score_b1 = utils.weight_variable([21], name="score_b1")
        score1 = vgg16_object._conv(pool4, score_w1, score_b1, 'score1')
        W_t1 = tf.Variable(initial_value=bilinear_init(), name="W_t1")
        b_t1 = tf.Variable(initial_value=tf.constant(0.0, shape=[21]),
                           name="b_t1")
        conv_t1 = vgg16_object.conv2d_transpose_strided(
            fc8, W_t1, b_t1, output_shape=tf.shape(score1))
        fuse_1 = tf.add(conv_t1, score1, name="fuse_1")

        # upsample and add with pool3(pool3 should pass through a score layer)
        score_w2 = utils.weight_variable([1, 1, 256, 21], name="score_w2")
        score_b2 = utils.weight_variable([21], name="score_b2")
        score2 = vgg16_object._conv(pool3, score_w2, score_b2, 'score2')
        W_t2 = tf.Variable(initial_value=bilinear_init(), name="W_t2")
        b_t2 = tf.Variable(initial_value=tf.constant(0.0, shape=[21]),
                           name="b_t2")
        conv_t2 = vgg16_object.conv2d_transpose_strided(
            fuse_1, W_t2, b_t2, output_shape=tf.shape(score2))
        fuse_2 = tf.add(conv_t2, score2, name="fuse_2")

        shape = tf.shape(processed_image)
        deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], 21])
        W_t3 = tf.Variable(initial_value=bilinear_init(scale=8), name="W_t3")
        b_t3 = tf.Variable(initial_value=tf.constant(0.0, shape=[21]),
                           name="b_t3")
        conv_t3 = vgg16_object.conv2d_transpose_strided(
            fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8)

        prediction = tf.argmax(conv_t3, dimension=3, name="prediction")
        return prediction, conv_t3, pool4
예제 #2
0
                    filenames_test,
                    batch_size=batch_size,
                    num_epochs=num_epochs,
                    read_threads=read_threads,
                    shuffle=True)

                images = tf.placeholder_with_default(images_batch_train,
                                                     shape=[None, 224, 224, 3])
                points = tf.placeholder_with_default(points_batch_train,
                                                     shape=[None, 3006])
                fl = tf.placeholder_with_default(fl_batch_train, shape=None)
#                images = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])
#                points = tf.placeholder(tf.float32, shape=[None, 3006])
#                fl = tf.placeholder(tf.float32, shape=None)

            model = vgg16(images)

            #
            model.add_histogram_summary()

            with tf.name_scope("costs"):

                def rmse(a, b):
                    return tf.sqrt(tf.reduce_mean(tf.square(tf.sub(a, b))))

                #reg_losses = tf.nn.l2_loss(model.fc1w)+tf.nn.l2_loss(model.fc3w) #+tf.nn.l2_loss(model.fc2w)
#                cost_train_reg = 0.5*rmse(points, model.rnn1)+0.5*rmse(points, model.rnn2)
                cost_train_reg = rmse(points, model.pred)
                cost_train = rmse(points, model.pred)
                cost_test = rmse(points, model.pred)
                #                cost_train_reg = rmse(tf.multiply(points, 227), tf.multiply(model.pred, 227))
예제 #3
0
    #    weights_path = 'weights/weights_latest.npz'#'weights/weights_fc_1491233872.99.npz'
    test_weights_path = 'weights/weights_latest_main_11_04_2.npz'
    testpath = '/home/arvardaz/SFT_with_CNN/temp2/'

    with tf.Graph().as_default():
        _, filenames_test = getFileList(testpath)
        images_batch_test, points_batch_test = input_pipeline(testpath,
                                                              filenames_test,
                                                              batch_size=1,
                                                              num_epochs=1,
                                                              read_threads=1)

        x = tf.placeholder(tf.float32, [None, 224, 224, 3])
        y = tf.placeholder(tf.float32, [None, 3006])
        keep_prob = tf.placeholder(tf.float32)
        vgg = vgg16(x, keep_prob)
        config = tf.ConfigProto()
        config.gpu_options.allocator_type = 'BFC'

        with tf.Session(config=config) as sess:
            sess.run(
                tf.group(tf.initialize_all_variables(),
                         tf.initialize_local_variables()))

            vgg.load_retrained_weights(test_weights_path, sess)
            cost_test = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y, vgg.pred))))
            runTest(data=[images_batch_test, points_batch_test],
                    cost=cost_test,
                    sess=sess,
                    print_step=10,
                    saveData=False)
예제 #4
0
    edges = np.genfromtxt("edges.csv", dtype=np.int32)
    real_gt_dist = np.genfromtxt("dist_real_gt.csv")

    real_edge_len = np.mean(real_gt_dist)

    obj_size = 3.68  #mean side length

    with tf.Graph().as_default():

        with tf.Session() as sess:
            imgs = tf.placeholder(tf.float32, [None, 224, 224, 3])
            #            keep_prob = tf.placeholder_with_default(1.0, [])

            #            vgg = vgg16(imgs, keep_prob, 'weights/weights_latest_main_11_04_2.npz', sess)
            vgg = vgg16(imgs, 'weights/weights_bg_best_1.56.npz', sess)
            #            vgg = vgg16(imgs, keep_prob, 'weights/weights_fc_conv3_best_all_weights.npz', sess)

            img_file, gt_file = getFileList(dr)

            cumul_loss = 0
            cumul_iso_loss = 0
            for i in range(3):  #len(img_file)):
                img0 = imread(img_file[i], mode='RGB')
                #                img0 = imrotate(img0, -90)

                img = imresize(img0, (224, 224))

                #                tf.image.per_image_whitening(img)
                img = sess.run(tf.image.per_image_standardization(img))
                #                gt = np.genfromtxt(gt_file[i])
예제 #5
0
        with tf.Graph().as_default():
            with tf.device('/cpu:0'):
                _, filenames_test = getFileList(testpath)
                images_batch_test, points_batch_test = input_pipeline(
                    testpath,
                    filenames_test,
                    batch_size=1,
                    num_epochs=1,
                    read_threads=1)

            x = tf.placeholder(tf.float32, [None, 224, 224, 3])
            y = tf.placeholder(tf.float32, [None, 3006])
            #            keep_prob = tf.placeholder(tf.float32)
            #            vgg = vgg16(x, keep_prob)
            vgg = vgg16(x)
            config = tf.ConfigProto()
            config.gpu_options.allocator_type = 'BFC'

            with tf.Session(config=config) as sess:
                sess.run(
                    tf.group(tf.initialize_all_variables(),
                             tf.initialize_local_variables()))

                vgg.load_retrained_weights(test_weights_path, sess)
                cost_test = tf.sqrt(
                    tf.reduce_mean(tf.square(tf.sub(y, vgg.pred))))
                mean, std = runTest(
                    data=[images_batch_test, points_batch_test],
                    cost=cost_test,
                    sess=sess,
예제 #6
0
#testpath = "/home/isit/armine/Dropbox/datasets/fl/rig_rt+fl/test/"
#testpath = "/home/isit/armine/Dropbox/datasets/fl/test/"
#testpath = "/home/isit/armine/Dropbox/datasets/fl/def_rt+fl+l+bg/test/"
testpath = "/home/isit/armine/Dropbox/datasets/fl/rig_rt+fl+l+bg/test/"

_, filenames_test = getFileList(testpath)

print_step = 1

with tf.Graph().as_default():

    x = tf.placeholder(tf.float32, [None, 224, 224, 3])
    y = tf.placeholder(tf.float32, [None, 3006])
    fl = tf.placeholder(tf.float32, shape=None)

    model = vgg16(x)
    config = tf.ConfigProto()
    config.gpu_options.allocator_type = 'BFC'

    with tf.Session(config=config) as sess:
        sess.run(
            tf.group(tf.initialize_all_variables(),
                     tf.initialize_local_variables()))

        model.load_retrained_weights(test_weights_path, sess)
        cost_test = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y, model.pred))))
        cost_test_fl = tf.reduce_mean(tf.divide(tf.abs(fl - model.pred_fl),
                                                fl))

        losses = []
        losses_fl = []