Exemple #1
0
def conv_layer(inputT, shape, train_phase, activation=True, Before_ReLu=True, name=None):
    width = shape[0]
    hight = shape[1]
    in_channel = shape[2]
    out_channel = shape[3]
    Input_channel = inputT.get_shape().as_list()[3]

    def ReLu(inputs, train_phase, name):
        x = tf.nn.relu(batch_norm_layer(inputs, train_phase, name))
        return x 

    with tf.variable_scope(name) as scope:
      if Before_ReLu is True:
        biases_Input = _variable_on_cpu('biases_Input', [Input_channel], tf.constant_initializer(0.0))
        biass = tf.nn.bias_add(inputT, biases_Input)
        inputT = ReLu(biass, train_phase, scope.name +'_BeforeRelu')
        
      kernel = _variable_with_weight_decay('ort_weights', [width, hight, in_channel, out_channel], initializer=orthogonal_initializer(), wd=None)
      conv = tf.nn.conv2d(inputT, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [out_channel], tf.constant_initializer(0.0))
      bias = tf.nn.bias_add(conv, biases)
      if activation is True:
        conv_out = ReLu(bias, train_phase, scope.name)
      else:
        conv_out = batch_norm_layer(bias, train_phase, scope.name)
    return conv_out
Exemple #2
0
def fully_connected(inputT, name, out_channel, activation_fn=tf.nn.relu):
    in_channel = inputT.get_shape().as_list()[-1]
    with tf.variable_scope(name):
        kernel = _variable_with_weight_decay('weights', shape = [in_channel, out_channel], initializer=orthogonal_initializer(),wd=0.0005)  #tf.contrib.layers.xavier_initializer() or msra_initializer(3, in_channel)
        biases = _variable_on_cpu('biases', [out_channel], tf.constant_initializer(0.0)) 
        out = tf.nn.bias_add(tf.matmul(inputT, kernel), biases)
        out = activation_fn(out)
        _activation_summary(out)
    return out
Exemple #3
0
def conv_layer_with_bn(inputT, shape, train_phase, activation=True, name=None):
    in_channel = shape[2]
    out_channel = shape[3]
    k_size = shape[0]
    with tf.variable_scope(name) as scope:
      kernel = _variable_with_weight_decay('ort_weights', shape=shape, initializer=orthogonal_initializer(), wd=None)
      conv = tf.nn.conv2d(inputT, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [out_channel], tf.constant_initializer(0.0))
      bias = tf.nn.bias_add(conv, biases)
      if activation is True:
        conv_out = tf.nn.relu(batch_norm_layer(bias, train_phase, scope.name))
      else:
        conv_out = batch_norm_layer(bias, train_phase, scope.name)
    return conv_out
Exemple #4
0
def conv_layer_with_bn(inputT, shape, train_phase, name=None, activation_fn=tf.nn.relu):
    #shape = [k_size, k_size, in_channel, out_channel]
    out_channel = shape[3]

    with tf.variable_scope(name) as scope:
      kernel = _variable_with_weight_decay('weights', shape=shape, initializer=orthogonal_initializer(), wd=None)   #orthogonal_initializer() or xaviar initialization
      conv = tf.nn.conv2d(inputT, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [out_channel], tf.constant_initializer(0.0))
      bias = tf.nn.bias_add(conv, biases)
      
      #batch_norm = batch_norm_layer(bias, train_phase, scope.name)
      #conv_out = tf.nn.relu(batch_norm)
      conv_out = tf.nn.relu(bias)
      
      _activation_summary(inputT)

    return conv_out
def inference(images, labels, batch_size, phase_train):
    # norm1
    norm1 = tf.nn.lrn(images, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75,
                name='norm1')
    # conv1
    conv1 = conv_layer_with_bn(norm1, [3, 3, images.get_shape().as_list()[3], 32], phase_train, name="conv1")
    # pool1
    pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                           padding='SAME', name='pool1')
    # conv2
    conv2 = conv_layer_with_bn(pool1, [3, 3, 32, 32], phase_train, name="conv2")

    # pool2
    pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1], padding='SAME', name='pool2')

    """ End of encoder """
    """ start upsample """
    # upsample2
    # upsample2 = upsample_with_pool_indices(conv_decode3, pool2_indices, conv_decode3.get_shape(), scale=2, name='upsample2')
    upsample2= deconv_layer(pool2, [2, 2, 32, 32], [batch_size, 45, 60, 32], 2, "up2")
    # decode 2
    conv_decode2 = conv_layer_with_bn(upsample2, [3, 3, 32, 32], phase_train, False, name="conv_decode2")

    # upsample1
    # upsample1 = upsample_with_pool_indices(conv_decode2, pool1_indices, conv_decode2.get_shape(), scale=2, name='upsample1')
    upsample1= deconv_layer(conv_decode2, [2, 2, 32, 32], [batch_size, 90, 120, 32], 2, "up1")
    # decode4
    conv_decode1 = conv_layer_with_bn(upsample1, [3, 3, 32, 32], phase_train, False, name="conv_decode1")
    """ end of Decode """
    """ Start Classify """
    # output predicted class number (6)
    with tf.variable_scope('conv_classifier') as scope:
      kernel = _variable_with_weight_decay('weights',
                                           shape=[1, 1, 32, NUM_CLASSES],
                                           initializer=msra_initializer(1, 32),
                                           wd=0.0005)
      conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.0))
      conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    logit = conv_classifier
    loss = cal_loss(conv_classifier, labels)

    return loss, logit
def inference_with_endpoints(images, labels, batch_size, phase_train):
    end_points = {}
    # norm1
    norm1 = tf.nn.lrn(images,
                      depth_radius=5,
                      bias=1.0,
                      alpha=0.0001,
                      beta=0.75,
                      name='norm1')
    # conv1
    conv1 = conv_layer_with_bn(
        norm1, [7, 7, images.get_shape().as_list()[3], 64],
        phase_train,
        name="conv1")
    end_points['segnet/conv1'] = conv1
    # pool1
    #    pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
    #                           padding='SAME', name='pool1')

    pool1 = tf.nn.max_pool(conv1,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool1')

    #    tf.nn.max_pool(value, ksize,  strides,   padding,   data_format='NHWC',   name=None)

    # conv2
    conv2 = conv_layer_with_bn(pool1, [7, 7, 64, 64],
                               phase_train,
                               name="conv2")
    end_points['segnet/conv2'] = conv2
    # pool2
    #    pool2, pool2_indices = tf.nn.max_pool_with_argmax(conv2, ksize=[1, 2, 2, 1],
    #                           strides=[1, 2, 2, 1], padding='SAME', name='pool2')
    pool2 = tf.nn.max_pool(conv2,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool2')
    # conv3
    conv3 = conv_layer_with_bn(pool2, [7, 7, 64, 64],
                               phase_train,
                               name="conv3")
    end_points['segnet/conv3'] = conv3

    # pool3
    #    pool3, pool3_indices = tf.nn.max_pool_with_argmax(conv3, ksize=[1, 2, 2, 1],
    #                           strides=[1, 2, 2, 1], padding='SAME', name='pool3')
    pool3 = tf.nn.max_pool(conv3,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool3')
    # conv4
    conv4 = conv_layer_with_bn(pool3, [7, 7, 64, 64],
                               phase_train,
                               name="conv4")
    end_points['segnet/conv4'] = conv4

    # pool4
    #    pool4, pool4_indices = tf.nn.max_pool_with_argmax(conv4, ksize=[1, 2, 2, 1],
    #                           strides=[1, 2, 2, 1], padding='SAME', name='pool4')
    pool4 = tf.nn.max_pool(conv4,
                           ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1],
                           padding='SAME',
                           name='pool4')
    """ End of encoder """
    """ start upsample """
    # upsample4
    # Need to change when using different dataset out_w, out_h
    # upsample4 = upsample_with_pool_indices(pool4, pool4_indices, pool4.get_shape(), out_w=45, out_h=60, scale=2, name='upsample4')
    upsample4 = deconv_layer(pool4, [2, 2, 64, 64], [batch_size, 64, 64, 64],
                             2, "up4")
    # decode 4
    conv_decode4 = conv_layer_with_bn(upsample4, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode4")
    end_points['segnet/conv_decode4'] = conv_decode4
    # upsample 3
    # upsample3 = upsample_with_pool_indices(conv_decode4, pool3_indices, conv_decode4.get_shape(), scale=2, name='upsample3')
    upsample3 = deconv_layer(conv_decode4, [2, 2, 64, 64],
                             [batch_size, 128, 128, 64], 2, "up3")
    # decode 3
    conv_decode3 = conv_layer_with_bn(upsample3, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode3")
    end_points['segnet/conv_decode3'] = conv_decode3
    # upsample2
    # upsample2 = upsample_with_pool_indices(conv_decode3, pool2_indices, conv_decode3.get_shape(), scale=2, name='upsample2')
    upsample2 = deconv_layer(conv_decode3, [2, 2, 64, 64],
                             [batch_size, 256, 256, 64], 2, "up2")

    # decode 2
    conv_decode2 = conv_layer_with_bn(upsample2, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode2")
    end_points['segnet/conv_decode2'] = conv_decode2
    # upsample1
    # upsample1 = upsample_with_pool_indices(conv_decode2, pool1_indices, conv_decode2.get_shape(), scale=2, name='upsample1')
    upsample1 = deconv_layer(conv_decode2, [2, 2, 64, 64],
                             [batch_size, 512, 512, 64], 2, "up1")
    # decode4
    conv_decode1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode1")
    end_points['segnet/conv_decode1'] = conv_decode1
    """ end of Decode """
    """ Start Classify """
    # output predicted class number (6)
    with tf.variable_scope('conv_classifier') as scope:
        kernel = _variable_with_weight_decay('weights',
                                             shape=[1, 1, 64, NUM_CLASSES],
                                             initializer=msra_initializer(
                                                 1, 64),
                                             wd=0.0005)
        conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
        end_points['segnet/conv_classifier'] = conv
        biases = _variable_on_cpu('biases', [NUM_CLASSES],
                                  tf.constant_initializer(0.0))
        conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    logit = conv_classifier
    end_points['segnet/logits'] = logit
    #    loss = cal_loss(conv_classifier, labels)

    return logit, end_points
Exemple #7
0
def inference(images, labels, batch_size, phase_train):
    # norm1
    norm1 = tf.nn.lrn(images, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75,
                name='norm1')
    # conv1
    conv1 = conv_layer_with_bn(norm1, [7, 7, images.get_shape().as_list()[3], 64], phase_train, name="conv1")
    # pool1
    pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
                           padding='SAME', name='pool1')
    # conv2
    conv2 = conv_layer_with_bn(pool1, [7, 7, 64, 64], phase_train, name="conv2")

    # pool2
    pool2, pool2_indices = tf.nn.max_pool_with_argmax(conv2, ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1], padding='SAME', name='pool2')
    # conv3
    conv3 = conv_layer_with_bn(pool2, [7, 7, 64, 64], phase_train, name="conv3")

    # pool3
    pool3, pool3_indices = tf.nn.max_pool_with_argmax(conv3, ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1], padding='SAME', name='pool3')
    # conv4
    conv4 = conv_layer_with_bn(pool3, [7, 7, 64, 64], phase_train, name="conv4")

    # pool4
    pool4, pool4_indices = tf.nn.max_pool_with_argmax(conv4, ksize=[1, 2, 2, 1],
                           strides=[1, 2, 2, 1], padding='SAME', name='pool4')

    """ End of encoder """
    """ start upsample """
    # upsample4
    # Need to change when using different dataset out_w, out_h
    # upsample4 = upsample_with_pool_indices(pool4, pool4_indices, pool4.get_shape(), out_w=45, out_h=60, scale=2, name='upsample4')
    upsample4 = deconv_layer(pool4, [2, 2, 64, 64], [batch_size, 45, 60, 64], 2, "up4")
    # decode 4
    conv_decode4 = conv_layer_with_bn(upsample4, [7, 7, 64, 64], phase_train, False, name="conv_decode4")

    # upsample 3
    # upsample3 = upsample_with_pool_indices(conv_decode4, pool3_indices, conv_decode4.get_shape(), scale=2, name='upsample3')
    upsample3= deconv_layer(conv_decode4, [2, 2, 64, 64], [batch_size, 90, 120, 64], 2, "up3")
    # decode 3
    conv_decode3 = conv_layer_with_bn(upsample3, [7, 7, 64, 64], phase_train, False, name="conv_decode3")

    # upsample2
    # upsample2 = upsample_with_pool_indices(conv_decode3, pool2_indices, conv_decode3.get_shape(), scale=2, name='upsample2')
    upsample2= deconv_layer(conv_decode3, [2, 2, 64, 64], [batch_size, 180, 240, 64], 2, "up2")
    # decode 2
    conv_decode2 = conv_layer_with_bn(upsample2, [7, 7, 64, 64], phase_train, False, name="conv_decode2")

    # upsample1
    # upsample1 = upsample_with_pool_indices(conv_decode2, pool1_indices, conv_decode2.get_shape(), scale=2, name='upsample1')
    upsample1= deconv_layer(conv_decode2, [2, 2, 64, 64], [batch_size, 360, 480, 64], 2, "up1")
    # decode4
    conv_decode1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64], phase_train, False, name="conv_decode1")
    """ end of Decode """
    """ Start Classify """
    # output predicted class number (6)
    with tf.variable_scope('conv_classifier') as scope:
      kernel = _variable_with_weight_decay('weights',
                                           shape=[1, 1, 64, NUM_CLASSES],
                                           initializer=msra_initializer(1, 64),
                                           wd=0.0005)
      conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.0))
      conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    logit = conv_classifier
    loss = cal_loss(conv_classifier, labels)

    return loss, logit
Exemple #8
0
def inference(images, labels, batch_size, phase_train):
    # norm1
    with tf.name_scope(name="inference"):
         with tf.variable_scope("norm-1"):
              norm1 = tf.nn.lrn(images, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75,
                name='norm1')
              tf.summary.histogram('/norm-1', norm1)  
    # conv1
         with tf.variable_scope("conv-1"):
              conv1 = conv_layer_with_bn(norm1, [7, 7, images.get_shape().as_list()[3], 64], phase_train, name="conv1")
              tf.summary.histogram('/conv-1', conv1)  
##    print(conv1.shape)
    # pool1
##    pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],
##                           padding='SAME', name='pool1')
         with tf.variable_scope("pool-1"):
              pool1, pool1_indices = max_pool_2x2(conv1, name="pool1")
              tf.summary.histogram('/pool-1', pool1) 
##    print(pool1.shape)
    # conv2
         with tf.variable_scope("conv-2"):
              conv2 = conv_layer_with_bn(pool1, [7, 7, 64, 64], phase_train, name="conv2")
              tf.summary.histogram('/conv-2', conv2) 

    # pool2
##    pool2, pool2_indices = tf.nn.max_pool_with_argmax(conv2, ksize=[1, 2, 2, 1],
##                           strides=[1, 2, 2, 1], padding='SAME', name='pool2')
         with tf.variable_scope("pool-2"):
              pool2, pool2_indices = max_pool_2x2(conv2, name="pool2")
              tf.summary.histogram('/pool-2', pool2) 
##    print(pool2.shape)
    # conv3
         with tf.variable_scope("conv-3"):
              conv3 = conv_layer_with_bn(pool2, [7, 7, 64, 64], phase_train, name="conv3")
              tf.summary.histogram('/conv-3', conv3) 

    # pool3
##    pool3, pool3_indices = tf.nn.max_pool_with_argmax(conv3, ksize=[1, 2, 2, 1],
##                           strides=[1, 2, 2, 1], padding='SAME', name='pool3')
         with tf.variable_scope("pool-3"):
              pool3, pool3_indices = max_pool_2x2(conv3, name="pool3")
              tf.summary.histogram('/pool-3', pool3) 
##    print(pool3.shape)
    # conv4
         with tf.variable_scope("conv-4"):
              conv4 = conv_layer_with_bn(pool3, [7, 7, 64, 64], phase_train, name="conv4")
              tf.summary.histogram('/conv-4', conv4) 

    # pool4
##    pool4, pool4_indices = tf.nn.max_pool_with_argmax(conv4, ksize=[1, 2, 2, 1],
##                           strides=[1, 2, 2, 1], padding='SAME', name='pool4')
         with tf.variable_scope("pool-4"):
              pool4, pool4_indices = max_pool_2x2(conv4, name="pool4")
              tf.summary.histogram('/pool-4', pool4) 
              print('pool4 = ',pool4.get_shape())
              
         """ End of encoder """


         """ Pyramid Pooling Module"""
          
          
         with tf.variable_scope('PyP', dtype=tf.float32):
              rcu01 = conv_layer(images, [3, 3, images.get_shape().as_list()[3], 64], phase_train, activation=True, Before_ReLu=True, name='RCU01')
              rcu11 = tf.add(rcu01, conv_layer(rcu01, [3, 3, rcu01.get_shape().as_list()[3], 64], phase_train, name='RCU11'))
              print('pool11 =' , images.get_shape() , ' and rcu11 =', rcu11.get_shape())
                
              rcu02 = conv_layer(pool1, [3, 3, pool1.get_shape().as_list()[3], 32], phase_train, activation=True, Before_ReLu=True, name='RCU02')
              rcu12 = tf.add(pool1, conv_layer(rcu02, [3, 3, rcu02.get_shape().as_list()[3], 64], phase_train, name='RCU12'))

              rcu03 = conv_layer(pool2, [3, 3, pool2.get_shape().as_list()[3], 32], phase_train, activation=True, Before_ReLu=True, name='RCU03')
              rcu13 = tf.add(pool2, conv_layer(rcu03, [3, 3, rcu03.get_shape().as_list()[3], 64], phase_train, name='RCU13'))
              
              rcu04 = conv_layer(pool3, [3, 3, pool3.get_shape().as_list()[3], 32], phase_train, activation=True, Before_ReLu=True, name='RCU04')
              rcu14 = tf.add(pool3, conv_layer(rcu04, [3, 3, rcu04.get_shape().as_list()[3], 64], phase_train, name='RCU14'))
              
              rcu05 = conv_layer(pool4, [3, 3, pool4.get_shape().as_list()[3], 32], phase_train, activation=True, Before_ReLu=True, name='RCU05')
              rcu15 = tf.add(pool4, conv_layer(rcu05, [3, 3, rcu05.get_shape().as_list()[3], 64], phase_train, name='RCU15'))

              
              pypool1 = tf.image.resize_bilinear(rcu11, [120, 160])
              pypool2 = tf.image.resize_bilinear(rcu12, [120, 160])
              pypool3 = tf.image.resize_bilinear(rcu13, [120, 160])
              pypool4 = tf.image.resize_bilinear(rcu14, [120, 160])
              pypool5 = tf.image.resize_bilinear(rcu15, [120, 160])
              print("PYpool4" , pypool4.get_shape())
              pyp1 = tf.multiply(pypool1, pypool2, name='pyp1')
              pyp2 = tf.multiply(pyp1, pypool3, name='pyp2')
              pyp3 = tf.multiply(pyp2, pypool4, name='pyp3')
              pyp4 = tf.multiply(pyp3, pypool5, name='pyp4')
##              pyp = tf.stack(tf.reduce_mean([pypool1, pypool1, pypool1, pypool1, pypool1], 0))
              print("pyp.shape = " , pyp1.get_shape())
              print("pyp4.shape = " , pyp4.get_shape())
              pyp_conv = conv_layer_with_bn(pyp4, [7, 7, pyp4.get_shape().as_list()[3], 64], phase_train, name='pyp_conv')
              crp = tf.nn.relu(pyp_conv , name='crp')

         pool_add_rcu = tf.add(tf.image.resize_bilinear(crp,[8, 10]), pool4)
         print("pool_add_rcuF = " , pool_add_rcu.get_shape())



         
         """ start upsample """

    # upsample4
    # Need to change when using different dataset out_w, out_h

    
         with tf.variable_scope("upsample-4"):
              upsample4 = deconv_layer(pool_add_rcu, [2, 2, 64, 64], [batch_size, 15, 20, 64], 2, "up4")
              tf.summary.histogram('/upsample4', upsample4) 
    # decode 4
         with tf.variable_scope("decode-4"):
              conv_decode4 = conv_layer_with_bn(upsample4, [7, 7, 64, 64], phase_train, False, name="conv_decode4")
              tf.summary.histogram('/conv_decode4', conv_decode4) 

    # upsample 3

         with tf.variable_scope("upsample-3"):
              upsample3= deconv_layer(conv_decode4, [2, 2, 64, 64], [batch_size, 30, 40, 64], 2, "up3")
              tf.summary.histogram('/upsample3', upsample3) 
    # decode 3
         with tf.variable_scope("decode-3"):
              conv_decode3 = conv_layer_with_bn(upsample3, [7, 7, 64, 64], phase_train, False, name="conv_decode3")
              tf.summary.histogram('/conv_decode3', conv_decode3) 

    # upsample2

         with tf.variable_scope("updample-2"):
              upsample2= deconv_layer(conv_decode3, [2, 2, 64, 64], [batch_size, 60, 80, 64], 2, "up2")
              tf.summary.histogram('/upsample2', upsample2) 
    # decode 2
         with tf.variable_scope("decode-2"):
              conv_decode2 = conv_layer_with_bn(upsample2, [7, 7, 64, 64], phase_train, False, name="conv_decode2")
              tf.summary.histogram('/conv_decode2', conv_decode2) 

    # upsample1

         with tf.variable_scope("upsample-1"):
              upsample1= deconv_layer(conv_decode2, [2, 2, 64, 64], [batch_size, 120, 160, 64], 2, "up1")
              tf.summary.histogram('/upsample1', upsample1) 
    # decode1
         with tf.variable_scope("decode-1"):
              print('upsample = ', upsample1.get_shape())
              conv_decode1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64], phase_train, False, name="conv_decode1")
              tf.summary.histogram('/conv_decode1', conv_decode1) 
         """ end of Decode """

         """ Start Classify """
    # output predicted class number (6)
         with tf.variable_scope('conv_classifier') as scope:
              kernel = _variable_with_weight_decay('weights',
                                                    shape=[1, 1, 64, NUM_CLASSES],
                                                    initializer=msra_initializer(1, 64),
                                                    wd=0.0005)
              conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
              biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.0))
              conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)
         logit = conv_classifier
         loss = cal_loss(conv_classifier, labels)
         tf.summary.histogram(scope.name, loss)     
    return loss, logit
Exemple #9
0
def inference(images, labels, phase_train):
	global num_classes, batch_size

	#local normalization on input images
	norm1 = tf.nn.lrn(images, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75,name='norm1')

	#convolve input layer
	conv1 = conv_layer_with_bn(norm1, [5, 5, images.get_shape().as_list()[3], 64], phase_train, name="conv1")
	print conv1.shape
	#residual set 1
	conv2 = residual_block(conv1, "conv2", phase_train, "1")
	conv3 = residual_block(conv2, "conv3", phase_train, "2")
	conv4 = residual_block(conv3, "conv4", phase_train, "3")

	pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1')
	print pool1.shape
	#residual block 2
	conv5 = residual_block(pool1, "conv5", phase_train, "4")
	conv6 = residual_block(conv5, "conv6", phase_train, "5")
	conv7 = residual_block(conv6, "conv7", phase_train, "6")

	pool2, pool2_indices = tf.nn.max_pool_with_argmax(conv7, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool2')
	print pool2.shape
	#residual block 3

	conv8 = residual_block(pool2, "conv8", phase_train, "7")
	conv9 = residual_block(conv8, "conv9", phase_train, "8")
	conv10 = residual_block(conv9, "conv10", phase_train, "9")

	pool3, pool3_indices = tf.nn.max_pool_with_argmax(conv9, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3')
	print pool3.shape
	#residual block 4

	conv11 = residual_block(pool3, "conv11", phase_train, "10")
	conv12 = residual_block(conv11, "conv12", phase_train, "11")
	conv13 = residual_block(conv12, "conv13", phase_train, "12")

	pool4, pool4_indices = tf.nn.max_pool_with_argmax(conv13, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool4')
	print pool4.shape
	#end of encoder - start of decoder
	
	#residual block 5
	upsample4 = deconv_layer(pool4, [2, 2, 64, 64], [batch_size, 30, 40, 64], 2, "up4")
	conv_decode14 = residual_block_2(upsample4, "conv_decode14", phase_train, "13")
	conv_decode15 = residual_block_2(conv_decode14, "conv_decode15", phase_train, "14")
	conv_decode16 = residual_block_2(conv_decode15, "conv_decode16", phase_train, "15")

	#residual block 6
	upsample3 = deconv_layer(conv_decode16, [2, 2, 64, 64], [batch_size, 60, 80, 64], 2, "up3")
	conv_decode17 = residual_block_2(upsample3, "conv_decode17", phase_train, "16")
	conv_decode18 = residual_block_2(conv_decode17, "conv_decode18", phase_train, "17")
	conv_decode19 = residual_block_2(conv_decode18, "conv_decode19", phase_train, "18")
	
	#residual block 7
	upsample2 = deconv_layer(conv_decode19, [2, 2, 64, 64], [batch_size, 120, 160, 64], 2, "up2")
	conv_decode20 = residual_block_2(upsample2, "conv_decode20", phase_train, "19")
	conv_decode21 = residual_block_2(conv_decode20, "conv_decode20", phase_train, "20")
	conv_decode22 = residual_block_2(conv_decode21, "conv_decode21", phase_train, "21")

	#residual block 8
	upsample1 = deconv_layer(conv_decode22, [2, 2, 64, 64], [batch_size, 240, 320, 64], 2, "up1")
	conv_decode23 = residual_block_2(upsample1, "conv_decode23", phase_train, "22")
	conv_decode24 = residual_block_2(conv_decode23, "conv_decode24", phase_train, "23")
	conv_decode25 = residual_block_2(conv_decode24, "conv_decode25", phase_train, "24")
	print conv_decode25.shape
	#conv_decode26 = conv_layer_with_bn(conv_decode25, [5, 5, 64, images.get_shape().as_list()[3]], phase_train, name="conv26")
	#print conv_decode26.shape
	#start classification
	with tf.variable_scope('conv_classifier') as scope:
		kernel = _variable_with_weight_decay('weights', shape=[1, 1, 64, num_classes], initializer=msra_initializer(1, 64),wd=0.0005)
		print kernel.shape
		conv = tf.nn.conv2d(conv_decode25, kernel, [1, 1, 1, 1], padding='SAME')
		print "final" + str(conv.shape)
		biases = _variable_on_cpu('biases', [num_classes], tf.constant_initializer(0.0))
		conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

	logit = conv_classifier
	loss = cal_loss(conv_classifier, labels)
	return loss, logit
Exemple #10
0
def inference(FLAGS, images, labels, batch_size, phase_train):
    # norm1
    norm1 = tf.nn.lrn(images,
                      depth_radius=5,
                      bias=1.0,
                      alpha=0.0001,
                      beta=0.75,
                      name='norm1')
    # conv1
    conv1 = conv_layer_with_bn(
        norm1, [7, 7, images.get_shape().as_list()[3], 64],
        phase_train,
        name="conv1")
    # pool1
    pool1, pool1_indices = tf.nn.max_pool_with_argmax(conv1,
                                                      ksize=[1, 2, 2, 1],
                                                      strides=[1, 2, 2, 1],
                                                      padding='SAME',
                                                      name='pool1')
    # conv2
    conv2 = conv_layer_with_bn(pool1, [7, 7, 64, 64],
                               phase_train,
                               name="conv2")

    # pool2
    pool2, pool2_indices = tf.nn.max_pool_with_argmax(conv2,
                                                      ksize=[1, 2, 2, 1],
                                                      strides=[1, 2, 2, 1],
                                                      padding='SAME',
                                                      name='pool2')

    # conv3
    conv3 = conv_layer_with_bn(pool2, [7, 7, 64, 64],
                               phase_train,
                               name="conv3")

    # pool3
    pool3, pool3_indices = tf.nn.max_pool_with_argmax(conv3,
                                                      ksize=[1, 2, 2, 1],
                                                      strides=[1, 2, 2, 1],
                                                      padding='SAME',
                                                      name='pool3')

    # conv4
    conv4 = conv_layer_with_bn(pool3, [7, 7, 64, 64],
                               phase_train,
                               name="conv4")

    # pool4
    pool4, pool4_indices = tf.nn.max_pool_with_argmax(conv4,
                                                      ksize=[1, 2, 2, 1],
                                                      strides=[1, 2, 2, 1],
                                                      padding='SAME',
                                                      name='pool4')
    """ End of encoder """
    """ start upsample """
    # upsample4
    # Need to change when using different dataset out_w, out_h
    # upsample4 = upsample_with_pool_indices(
    #     pool4, pool4_indices, pool4.get_shape(), out_w=45, out_h=60, scale=2, name='upsample4'
    # )
    upsample4 = deconv_layer(pool4, [2, 2, 64, 64], [batch_size, 45, 60, 64],
                             2, "up4")
    # decode 4
    conv_decode4 = conv_layer_with_bn(upsample4, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode4")

    # upsample 3
    # upsample3 = upsample_with_pool_indices(
    #     conv_decode4, pool3_indices, conv_decode4.get_shape(), scale=2, name='upsample3'
    # )
    upsample3 = deconv_layer(conv_decode4, [2, 2, 64, 64],
                             [batch_size, 90, 120, 64], 2, "up3")
    # decode 3
    conv_decode3 = conv_layer_with_bn(upsample3, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode3")

    # upsample2
    # upsample2 = upsample_with_pool_indices(
    #     conv_decode3, pool2_indices, conv_decode3.get_shape(), scale=2, name='upsample2'
    # )
    upsample2 = deconv_layer(conv_decode3, [2, 2, 64, 64],
                             [batch_size, 180, 240, 64], 2, "up2")
    # decode 2
    conv_decode2 = conv_layer_with_bn(upsample2, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode2")

    # upsample1
    # upsample1 = upsample_with_pool_indices(
    #     conv_decode2, pool1_indices, conv_decode2.get_shape(), scale=2, name='upsample1'
    # )
    upsample1 = deconv_layer(conv_decode2, [2, 2, 64, 64],
                             [batch_size, 360, 480, 64], 2, "up1")
    # decode4
    conv_decode1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64],
                                      phase_train,
                                      False,
                                      name="conv_decode1")
    """ end of Decode """
    """ Start Classify """
    # output predicted class number (6)
    with tf.variable_scope('conv_classifier') as scope:
        kernel = _variable_with_weight_decay(
            'weights',
            shape=[1, 1, 64, FLAGS.num_classes],
            initializer=msra_initializer(1, 64),
            wd=0.0005)
        conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
        biases = _variable_on_cpu('biases', [FLAGS.num_classes],
                                  tf.constant_initializer(0.0))
        conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    logit = conv_classifier
    loss = cal_loss(FLAGS, conv_classifier, labels)

    return loss, logit
Exemple #11
0
def prediction_myModel(images, batch_size=1, phase_train=False):
    # norm1
    norm1 = tf.nn.lrn(images, depth_radius=5, bias=1.0, alpha=0.0001, beta=0.75,
                name='norm1')
    # conv1
    conv1_1 = conv_layer_with_bn(norm1, [3, 3, images.get_shape().as_list()[3], 64], phase_train, name="conv1_1")
    conv1_2 = conv_layer_with_bn(conv1_1, [3, 3, 64, 64], phase_train, name="conv1_2")
    # pool1
    pool1 = tf.nn.max_pool(conv1_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1],padding='SAME', name='pool1')
    # conv2
    conv2_1 = conv_layer_with_bn(pool1, [3, 3, 64, 128], phase_train, name="conv2_1")
    conv2_2 = conv_layer_with_bn(conv2_1, [3, 3, 128, 128], phase_train, name="conv2_2")
    # pool2
    pool2 = tf.nn.max_pool(conv2_2, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME', name='pool2')
    # conv3
    conv3_1 = conv_layer_with_bn(pool2, [3, 3, 128, 256], phase_train, name="conv3_1")
    conv3_2 = conv_layer_with_bn(conv3_1, [3, 3, 256, 256], phase_train, name="conv3_2")
    conv3_3 = conv_layer_with_bn(conv3_2, [3, 3, 256, 256], phase_train, name="conv3_3")

    # pool3
    pool3 = tf.nn.max_pool(conv3_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3')
    # conv4
    conv4_1 = conv_layer_with_bn(pool3, [3, 3, 256, 512], phase_train, name="conv4_1")
    conv4_2 = conv_layer_with_bn(conv4_1, [3, 3, 512, 512], phase_train, name="conv4_2")
    conv4_3 = conv_layer_with_bn(conv4_2, [3, 3, 512, 512], phase_train, name="conv4_3")
    # pool4
    pool4 = tf.nn.max_pool(conv4_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool4')
    # conv5
    conv5_1 = conv_layer_with_bn(pool4, [3, 3, 512, 512], phase_train, name="conv5_1")
    conv5_2 = conv_layer_with_bn(conv5_1, [3, 3, 512, 512], phase_train, name="conv5_2")
    conv5_3 = conv_layer_with_bn(conv5_2, [3, 3, 512, 512], phase_train, name="conv5_3")
    # pool5
    pool5 = tf.nn.max_pool(conv5_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool5')
    """ End of encoder """
    """ start upsample """
    # upsample5
    upsample5 = deconv_layer(pool5, [2, 2, 512, 512], [batch_size, 23, 30, 512], 2, "up5")
    # decode 4
    conv_decode5_3 = conv_layer_with_bn(upsample5, [3, 3, 512, 512], phase_train, False, name="conv_decode5_3")
    conv_decode5_2 = conv_layer_with_bn(conv_decode5_3, [3, 3, 512, 512], phase_train, False, name="conv_decode5_2")
    conv_decode5_1 = conv_layer_with_bn(conv_decode5_2, [3, 3, 512, 512], phase_train, False, name="conv_decode5_1")
    # upsample4
    # Need to change when using different dataset out_w, out_h
    # upsample4 = upsample_with_pool_indices(pool4, pool4_indices, pool4.get_shape(), out_w=45, out_h=60, scale=2, name='upsample4')
    upsample4 = deconv_layer(conv_decode5_1, [2, 2, 512, 512], [batch_size, 45, 60, 512], 2, "up4")
    # decode 4
    conv_decode4_3 = conv_layer_with_bn(upsample4, [3, 3, 512, 512], phase_train, False, name="conv_decode4_3")
    conv_decode4_2 = conv_layer_with_bn(conv_decode4_3, [3, 3, 512, 512], phase_train, False, name="conv_decode4_2")
    conv_decode4_1 = conv_layer_with_bn(conv_decode4_2, [3, 3, 512, 256], phase_train, False, name="conv_decode4_1")

    # upsample 3
    # upsample3 = upsample_with_pool_indices(conv_decode4, pool3_indices, conv_decode4.get_shape(), scale=2, name='upsample3')
    upsample3= deconv_layer(conv_decode4_1, [2, 2, 256, 256], [batch_size, 90, 120, 256], 2, "up3")
    # decode 3
    conv_decode3_3 = conv_layer_with_bn(upsample3, [3, 3, 256, 256], phase_train, False, name="conv_decode3_3")
    conv_decode3_2 = conv_layer_with_bn(conv_decode3_3, [3, 3, 256, 256], phase_train, False, name="conv_decode3_2")
    conv_decode3_1 = conv_layer_with_bn(conv_decode3_2, [3, 3, 256, 128], phase_train, False, name="conv_decode3_1")
    # upsample2
    # upsample2 = upsample_with_pool_indices(conv_decode3, pool2_indices, conv_decode3.get_shape(), scale=2, name='upsample2')
    upsample2= deconv_layer(conv_decode3_1, [2, 2, 128, 128], [batch_size, 180, 240, 128], 2, "up2")
    # decode 2
    conv_decode2_2 = conv_layer_with_bn(upsample2, [3, 3, 128, 128], phase_train, False, name="conv_decode2_2")
    conv_decode2_1 = conv_layer_with_bn(conv_decode2_2, [3, 3, 128, 64], phase_train, False, name="conv_decode2_1")
    # upsample1
    # upsample1 = upsample_with_pool_indices(conv_decode2, pool1_indices, conv_decode2.get_shape(), scale=2, name='upsample1')
    upsample1= deconv_layer(conv_decode2_1, [2, 2, 64, 64], [batch_size, 360, 480, 64], 2, "up1")
    # decode4
    conv_decode1_2 = conv_layer_with_bn(upsample1, [3, 3, 64, 64], phase_train, False, name="conv_decode1_2")
    conv_decode1 = conv_layer_with_bn(conv_decode1_2, [3, 3, 64, 64], phase_train, False, name="conv_decode1")
    """ end of Decode """
    """ Start Classify """
    # output predicted class number (6)
    with tf.variable_scope('conv_classifier') as scope:
      kernel = _variable_with_weight_decay('weights',
                                           shape=[1, 1, 64, NUM_CLASSES],
                                           initializer=msra_initializer(1, 64),
                                           wd=0.0005)
      conv = tf.nn.conv2d(conv_decode1, kernel, [1, 1, 1, 1], padding='SAME')
      biases = _variable_on_cpu('biases', [NUM_CLASSES], tf.constant_initializer(0.0))
      conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name)

    logit = conv_classifier

    return logit