Example #1
0
    def deeplabv3_plus(image):
        with tf.variable_scope('', reuse=tf.AUTO_REUSE):
            with slim.arg_scope(resnet_v1.resnet_arg_scope()):
                net, end_points = resnet_v1.resnet_v1_50(image,
                                                         num_classes=None,
                                                         is_training=None,
                                                         global_pool=False,
                                                         output_stride=16,
                                                         spatial_squeeze=False)
            # ASPP
            aspp = atrous_spp16(net)
            with tf.variable_scope('decoder'):
                # Low level
                low_level_features = end_points[
                    'resnet_v1_50/block1/unit_2/bottleneck_v1']
                low_level_features = slim.conv2d(
                    low_level_features,
                    48, [1, 1],
                    scope='low_level_feature_conv_1x1')
                low_level_features_shape = low_level_features.get_shape(
                ).as_list()[1:3]

                # Upsample
                net = tf.image.resize_images(aspp, low_level_features_shape)
                net = tf.concat([net, low_level_features], axis=3)
                net = slim.conv2d(net, 256, [3, 3], scope='conv_3x3_1')
                net = slim.conv2d(net, 256, [3, 3], scope='conv_3x3_2')

        return net
Example #2
0
def resnet_50_network(img_ph,
                      background=None,
                      is_training=False,
                      reuse=False,
                      scope=None):
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        _, end_points = resnet_v1.resnet_v1_50(img_ph,
                                               reuse=reuse,
                                               is_training=is_training)
    x = end_points['resnet_v1_50/block3/unit_5/bottleneck_v1']
    return x
def model(images, weight_decay=1e-5, is_training=True):
    # 图片减去均值
    images = mean_image_subtraction(images)

    # 101层残差
    with slim.arg_scope(resnet_v1.resnet_arg_scope(weight_decay=weight_decay)):
        logits, end_points = resnet_v1.resnet_v1_101(images,
                                                     is_training=is_training,
                                                     scope='resnet_v1_101')

    with tf.variable_scope('feature_fusion', values=[end_points.values]):
        batch_norm_params = {
            'decay': 0.997,
            'epsilon': 1e-5,
            'scale': True,
            'is_training': is_training
        }
        with slim.arg_scope(
            [slim.conv2d],
                activation_fn=tf.nn.relu,
                normalizer_fn=slim.batch_norm,
                normalizer_params=batch_norm_params,
                weights_regularizer=slim.l2_regularizer(weight_decay)):
            f = [
                end_points['pool5'], end_points['pool4'], end_points['pool3'],
                end_points['pool2']
            ]
            for i in range(4):
                print('Shape of f_{} {}'.format(i, f[i].shape))

            g = [None, None, None, None]
            h = [None, None, None, None]

            for i in range(4):
                h[i] = slim.conv2d(f[i], 256, 1)
            for i in range(4):
                print('Shape of h_{} {}'.format(i, h[i].shape))

            g[0] = RefineBlock(h[0])
            g[1] = RefineBlock(g[0], h[1])
            g[2] = RefineBlock(g[1], h[2])
            g[3] = RefineBlock(g[2], h[3])

            g[3] = unpool(g[3], scale=4)
            f_score = slim.conv2d(g[3],
                                  21,
                                  1,
                                  activation_fn=tf.nn.relu,
                                  normalizer_fn=None)

    return f_score
    def resnet(self, image):
        with slim.arg_scope(resnet_v1.resnet_arg_scope()):
            logits, end_points = resnet_v1.resnet_v1_50(image, is_training=True, scope="resnet_v1_50")

        with tf.variable_scope('feature_fusion', values=[end_points.values]):
            batch_norm_params = {
                'decay': 0.997,
                'epsilon': 1e-5,
                'scale': True,
                'is_training': True
            }
            with slim.arg_scope([slim.conv2d],
                                activation_fn=tf.nn.relu,
                                normalizer_fn=slim.batch_norm,
                                normalizer_params=batch_norm_params,
                                weights_regularizer=slim.l2_regularizer(1e-4)):
                f = [end_points['pool5'], end_points['pool4'],
                     end_points['pool3'], end_points['pool2'],
                     end_points['pool1'], end_points['pool0']]
                global_pool = tf.reduce_mean(f[0], [1, 2], name='pool5', keep_dims=True)
                for i in range(6):
                    print('Shape of f_{} {}'.format(i, f[i].shape))
                g = [None, None, None, None, None, None]
                h = [None, None, None, None, None, None]
                num_outputs = [128, 128, 64, 32, 32, 16]
                for i in range(4):
                    if i == 0:
                        global_pool = self.unpool(global_pool, size=[tf.shape(f[i])[1], tf.shape(f[i])[2]])
                        h[i] = tf.concat([f[i], global_pool],
                                         axis=-1)
                        h[i] = slim.conv2d(h[i], num_outputs[i], 3)
                        # h[i] = f[i]
                    else:
                        c1_1 = slim.conv2d(tf.concat([g[i - 1], f[i]], axis=-1), num_outputs[i], 1)
                        h[i] = slim.conv2d(c1_1, num_outputs[i], [3, 3])

                    if i < 3:
                        g[i] = self.unpool(h[i], size=[tf.shape(f[i + 1])[1], tf.shape(f[i + 1])[2]])
                    if i == 3:
                        # c1_1 = slim.conv2d(tf.concat([g[i], end_points['pool1']], axis=-1), num_outputs[i], 1)
                        g[i] = self.unpool(h[i])
                        g[i] = slim.conv2d(g[i], num_outputs[i], 3, padding="SAME")
                    print('Shape of h_{} {}, g_{} {}'.format(i, h[i].shape, i, g[i].shape))

                mask = self.unpool(g[3])
                mask = slim.conv2d(mask, num_outputs[3], 3, padding="SAME")
                output_map = slim.conv2d(mask, 2, 1, activation_fn=None, normalizer_fn=None)
                output_contour = slim.conv2d(mask, 2, 1, activation_fn=None, normalizer_fn=None)

        return output_map, output_contour
Example #5
0
def resnet_18_network(img_ph,
                      background=None,
                      is_training=False,
                      reuse=False,
                      scope=None):
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        _, end_points = resnet_v1.resnet_v1_18(img_ph,
                                               reuse=reuse,
                                               is_training=is_training)
    if scope is None:
        x = end_points['resnet_v1_18/block3/unit_1/bottleneck_v1']
    else:
        x = end_points['%s/resnet_v1_18/block3/unit_1/bottleneck_v1' % scope]
    return x
import tensorflow as tf
slim = tf.contrib.slim
from resnet import resnet_v1
import numpy as np
from scipy.misc import imread, imresize

checkpoint_file = './data/resnet-50/resnet_v1_50.ckpt'
sample_images = ['dog.jpg', 'panda.jpg']
#Load the model
sess = tf.Session()
inputs = tf.placeholder(tf.float32, [None, 160, 160, 3])
background = tf.placeholder(tf.float32, [None, 160, 160, 3])
with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    net, end_points = resnet_v1.resnet_v1_50(inputs,
                                             background,
                                             1000,
                                             is_training=False)
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)

# import pdb; pdb.set_trace()
# def get_candidate(result, topn=5):
# 	with open('./imagenet.txt') as f:
# 		dictionary = eval(f.read())
# 	idxes = np.argsort(result[0])[-topn:][::-1]
# 	results = []
# 	for idx in idxes:
# 		results.append(dictionary[idx])
# 	return results

# imgs = ['./cat.jpg', './car.jpeg', './house.jpeg']