1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| from __future__ import print_function, division import tensorflow as tf import numpy as np import cv2 import scipy.io import commentjson as json conf = json.load(open("./config.json")) logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(filename)s %(funcName)s(): %(lineno)i: %(levelname)s: %(message)s", ) logger = logging.getLogger(__name__)
def get_variable(weights, name=None): init = tf.constant_initializer(weights, dtype=tf.float32) return tf.get_variable(name=name, initializer=init, shape=weights.shape)
def weights_variable(shape, stddev=0.02, name=None): init = tf.truncated_normal(shape, stddev=stddev) return tf.Varialbe(init) if name is None else tf.get_variable( name, initializer=init)
def bias_variable(shape, name=None): init = tf.constant(0.0, shape=shape) return tf.Varialbe(init) if name is None else tf.get_variable( name, initializer=init)
def conv2d_basic(x, W, bias): conv = tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME") return tf.nn.bias_add(conv, bias)
def conv2d_transpose_stride(x, W, b, output_shape=None, stride=2): if output_shape is None: output_shape = x.get_shape().as_list() output_shape[1] *= 2 output_shape[2] *= 2 output_shape[3] *= W.get_shape().as_list()[2] conv = tf.nn.conv2d_transpose(x, W, output_shape, strides=[1, stride, stride, 1], padding="SAME") return tf.nn.bias_add(conv, b)
def avg_pool_2x2(x): return tf.nn.avg_pool(x, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME")
def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding="SAME")
def vgg(weights, image): layers = ( 'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1', 'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2', 'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3', 'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4', 'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3', 'relu5_3', 'conv5_4', 'relu5_4', 'pool5', ) net = {} current = image for idx, name in enumerate(layers): kind = name[:4] if kind == "conv": kernels, bias = weights[idx][0][0][0][0] kernels = get_variable(np.transpose(kernels, (1, 0, 2, 3)), name=name+'_w') bias = get_variable(bias.reshape(-1), name=name+'_b') current = conv2d_basic(current, kernels, bias) elif kind == 'relu': current = tf.nn.relu(current, name=name) elif kind == "pool": current = avg_pool_2x2(current) net[name] = current return net
def inference(image, keep_prob): image = image-tf.constant([123.68, 116.779, 103.939]) modelpath = conf["vgg"] model_data = scipy.io.loadmat(modelpath) weights = np.squeeze(model_data['layers']) with tf.variable_scope("inference"): image_net = vgg(weights, image) conv_final_layer = image_net["conv5_3"] pool5 = max_pool_2x2(conv_final_layer) W6 = weights_variable([7, 7, 512, 4096], name="W6") b6 = bias_variable([4096], name="b6") conv6 = conv2d_basic(pool5, W6, b6) relu6 = tf.nn.relu(conv6, name="relu6") relu_dropout6 = tf.nn. dropout(relu6, keep_prob=keep_prob) W7 = weights_variable([1, 1, 4096, 4096], name="W7") b7 = bias_variable([4096], name="b7") conv7 = conv2d_basic(relu_dropout6, W7, b7) relu7 = tf.nn.relu(conv7, name="relu7") relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob) W8 = weights_variable([1, 1, 4096, conf["num_of_classes"]], name="W8") b8 = bias_variable([conf["num_of_classes"]], name="b8") conv8 = conv2d_basic(relu_dropout7, W8, b8)
deconv_shape1 = image_net["pool4"].get_shape() W_t1 = weights_variable([4, 4, deconv_shape1[3].value, conf["num_of_classes"]], name="W_t1") b_t1 = bias_variable([deconv_shape1[3].value], name="b_t1") conv_t1 = conv2d_transpose_stride(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"])) fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1")
deconv_shape2 = image_net["pool3"].get_shape() W_t2 = weights_variable([4, 4, deconv_shape2[3].value, deconv_shape1[3].value], name="W_t2") b_t2 = bias_variable([deconv_shape2[3].value], name="b_t2") conv_t2 = conv2d_transpose_stride(fuse_1, W_t2, b_t2, output_shape=tf.shape(image_net["pool3"])) fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2")
shape = tf.shape(image) deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], conf["num_of_classes"]]) W_t3 = weights_variable([16, 16, conf["num_of_classes"], deconv_shape2[3].value], name="W_t3") b_t3 = bias_variable([conf["num_of_classes"]], name="b_t3") conv_t3 = conv2d_transpose_stride(fuse_2, W_t3, b_t3, output_shape=deconv_shape3, stride=8)
pred = tf.argmax(conv_t3, dimension=3, name="prediction") return tf.expand_dims(pred, dim=3), conv_t3
|