コード例 #1
0
ファイル: deconv.py プロジェクト: Thrasi/thesis-project
    def model(data, train=False, prefix=""):
        """The Model definition."""
        # 2D convolution, with 'SAME' padding (i.e. the output feature map has
        # the same size as the input). Note that {strides} is a 4D array whose
        # shape matches the data layout: [image index, y, x, depth].

        # 28x28
        conv = helpers.conv2d(
            data, name=prefix + "conv1", kernel_width=5, num_filters=32, transfer=tf.nn.relu, decay_rate=DECAY_RATE
        )
        print("After first conv: " + str(conv.get_shape()))
        # Max pooling. The kernel size spec {ksize} also follows the layout of
        # the data. Here we have a pooling window of 2, and a stride of 2.
        pool = helpers.pool(conv, name=prefix + "pool1", kernel_width=2)
        print("After first pool: " + str(pool.get_shape()))
        #    print("After first conv: "+str(relu.get_shape())
        # 14x14
        conv = helpers.conv2d(
            pool, name=prefix + "conv2", kernel_width=5, num_filters=64, transfer=tf.nn.relu, decay_rate=DECAY_RATE
        )
        print("After second conv: " + str(conv.get_shape()))
        # pool = helpers.pool(conv,
        #                     name=prefix+"pool2",
        #                     kernel_width=2)
        # print("After second pool: "+str(pool.get_shape()))
        # # 7x7
        # conv = helpers.conv2d(pool,
        #                       name=prefix+"conv3",
        #                       kernel_width=3,
        #                       num_filters=128,
        #                       transfer=tf.nn.relu,
        #                       padding='SAME',
        #                       decay_rate=DECAY_RATE)
        # print("After third conv: "+str(conv.get_shape()))
        # conv = helpers.conv2d(conv,
        #                       name=prefix+"1x1",
        #                       kernel_width=1,
        #                       num_filters=512,
        #                       transfer=tf.nn.relu,
        #                       decay_rate=DECAY_RATE)
        if train:
            conv = tf.nn.dropout(conv, 0.5, seed=SEED)
        # 5x5
        # print("After 1x1 conv: "+str(conv.get_shape()))
        # size = tf.constant([7, 7])
        # unpool = tf.image.resize_nearest_neighbor(conv,
        #                                   size,
        #                                   align_corners=None,
        #                                   name=prefix+"unpool1")
        # print("After first unpool: "+str(unpool.get_shape()))
        # # 7x7
        # conv = helpers.conv2d(unpool,
        #                       name=prefix+"deconv1",
        #                       kernel_width=3,
        #                       num_filters=64,
        #                       transfer=tf.nn.relu,
        #                       decay_rate=DECAY_RATE)
        # print("After first deconv: "+str(conv.get_shape()))
        # # 7x7
        # size = tf.constant([14, 14])
        # unpool = tf.image.resize_nearest_neighbor(conv,
        #                                   size,
        #                                   align_corners=None,
        #                                   name=prefix+"unpool2")
        # print("After second unpool: "+str(unpool.get_shape()))
        # # 14x14
        # conv = helpers.conv2d(unpool,
        #                       name=prefix+"deconv2",
        #                       kernel_width=3,
        #                       num_filters=32,
        #                       transfer=tf.nn.relu,
        #                       decay_rate=DECAY_RATE)
        # print("After second deconv: "+str(conv.get_shape()))
        size = tf.constant([28, 28])
        unpool = tf.image.resize_nearest_neighbor(conv, size, align_corners=None, name=prefix + "unpool3")
        print("After third unpool: " + str(unpool.get_shape()))
        conv = helpers.conv2d(
            unpool, name=prefix + "deconv3", kernel_width=3, num_filters=2, transfer=tf.nn.relu, decay_rate=DECAY_RATE
        )
        print("After third deconv: " + str(conv.get_shape()))
        conv_shape = conv.get_shape().as_list()
        #    reshape = tf.reshape(conv,
        #                     [conv_shape[0],conv_shape[1]*conv_shape[2]*conv_shape[3]])
        reshape = tf.reshape(conv, [conv_shape[0] * conv_shape[1] * conv_shape[2], conv_shape[3]])
        return reshape
コード例 #2
0
ファイル: cifar10.py プロジェクト: Thrasi/thesis-project
def inference(images):
  """Build the CIFAR-10 model.

  Args:
    images: Images returned from distorted_inputs() or inputs().

  Returns:
    Logits.
  """
  # We instantiate all variables using tf.get_variable() instead of
  # tf.Variable() in order to share variables across multiple GPU training runs.
  # If we only ran this model on a single GPU, we could simplify this function
  # by replacing all instances of tf.get_variable() with tf.Variable().
  #
  # conv1
  print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
  print(images.get_shape())
  nr_params = 0
  shape = images.get_shape().as_list()
  SIZE1 = tf.constant(shape[1:3])
  conv1, nrp = helpers.conv2d(images,
                         name="conv1",
                         kernel_width=5,
                         num_filters=64,
                         transfer=tf.nn.relu,
                         decay_rate=DECAY_RATE)
  print("conv1: "+str(conv1.get_shape()))
  nr_params += nrp
  # pool1
  pool1 = helpers.pool(conv1, name="pool1", kernel_width=2, stride=2)
  print("pool1: "+str(pool1.get_shape()))
  # norm1
  norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
                    name='norm1')

  shape = norm1.get_shape().as_list()
  SIZE2 = tf.constant(shape[1:3])
  # conv2
  conv2, nrp = helpers.conv2d(norm1,
                         name="conv2",
                         kernel_width=5,
                         num_filters=64,
                         transfer=tf.nn.relu,
                         decay_rate=DECAY_RATE)
  nr_params += nrp
  print("conv2: "+str(conv2.get_shape()))
  # norm2
  norm2 = tf.nn.lrn(conv2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
                    name='norm2')
  pool2 = helpers.pool(norm2, name="pool2", kernel_width=3, stride=2)
  print("pool2: "+str(pool2.get_shape()))
  deconv_shape = pool2.get_shape()
  # conv3
  conv3, nrp = helpers.conv2d(pool2,
                        name="conv3",
                        kernel_width=6,
                        num_filters=128,
                        transfer=tf.nn.relu,
                        padding="VALID",
                        decay_rate=DECAY_RATE)
  print("conv3: "+str(conv3.get_shape()))
  nr_params += nrp
  # conv 4 1x1
  conv4, nrp = helpers.conv2d(conv3,
                        name="conv4-1x1",
                        kernel_width=1,
                        num_filters=128,
                        transfer=tf.nn.relu,
                        decay_rate=DECAY_RATE)
  print("conv4: "+str(conv4.get_shape()))
  nr_params += nrp
  # deconv
  deconv1, nrp = helpers.conv2d_transpose(conv4,
                          name="deconv1",
                          kernel_width=6,
                          num_filters=64,
                          transfer=tf.nn.relu,
                          padding="VALID",
                          output_shape=deconv_shape,
                          decay_rate=DECAY_RATE)
  print("deconv1: "+str(deconv1.get_shape()))
  nr_params += nrp
  unpool1 = tf.image.resize_nearest_neighbor(deconv1,
                                             SIZE2,
                                             align_corners=None,
                                             name="unpool1")
  print("unpool1: "+str(unpool1.get_shape()))
  # conv5
  conv5, nrp = helpers.conv2d(unpool1,
                        name="conv5",
                        kernel_width=5,
                        num_filters=64,
                        transfer=tf.nn.relu,
                        decay_rate=DECAY_RATE)
  print("conv5: "+str(conv5.get_shape()))
  nr_params += nrp
  unpool2 = tf.image.resize_nearest_neighbor(conv5,
                                              SIZE1,
                                              align_corners=None,
                                              name="unpool2")
  print("unpool2: "+str(unpool2.get_shape()))
  # conv6
  conv6, nrp = helpers.conv2d(unpool2,
                        name="conv6",
                        kernel_width=5,
                        num_filters=NUM_CLASSES,
                        transfer=tf.nn.relu,
                        decay_rate=DECAY_RATE)
  print("conv6  : "+str(conv6.get_shape()))
  nr_params += nrp
  
  
  return conv6, nr_params
コード例 #3
0
wc1 = np.load('../../Predictor/server_net/models/model_16/weights/weights1.npy')
wc2 = np.load('../../Predictor/server_net/models/model_16/weights/weights2.npy')
wf1 = np.load('../../Predictor/server_net/models/model_16/weights/weights3.npy')
otw = np.load('../../Predictor/server_net/models/model_16/weights/weights4.npy')

bc1 = np.load('../../Predictor/server_net/models/model_16/biases/bc1.npy')
bc2 = np.load('../../Predictor/server_net/models/model_16/biases/bc2.npy') 
bf1 = np.load('../../Predictor/server_net/models/model_16/biases/bd1.npy')
otb = np.load('../../Predictor/server_net/models/model_16/biases/bout.npy')

test_im = np.reshape(test_im,(1,28,28,1))


#conv2d takes (1,28,28,1) image and (5,5,1,5) kernel and gives (1,28,28,5) output
con1 = conv2d(test_im,wc1)
# np.save('con1.npy',con1)
print "conv1"
print con1
#add_bias takes the 5 (1,28,28) inputs and 5 biases, and adds the bias to every pixel in the 5 inputs
con1 = add_bias(con1,bc1)
# np.save('con1bias.npy',con1)
print "conv1addbias"
print con1
#activ_fun takes (1,28,28,5) input and squares each and every element in the input
con1 = activ_fun(con1)
# np.save('con1act.npy',con1)
print "conv1act"
print con1
#meanpool2 takes (1,28,28,5) input and performs meanpooling on each of the 5 28x28 matrices seperately
#and gives a (1,14,14,5) output
コード例 #4
0
def inference():
    x = tf.placeholder(tf.float32, shape=[None, 784], name='input')
    image = tf.reshape(x, [-1, 28, 28, 1])

    with tf.name_scope('conv_layer_1'):
        W_conv1 = helpers.weight_variable([5, 5, 1, 32], 'W_conv1')
        b_conv1 = helpers.bias_variable([32], 'bias_conv1')
        alphas_conv1 = helpers.bias_variable([32], 'alpha_conv1')
        layer_conv_1 = helpers.prelu(
            helpers.conv2d(image, W_conv1) + b_conv1, alphas_conv1)

        W_conv1_b = helpers.weight_variable([5, 5, 32, 32], 'W_conv1_b')
        b_conv1_b = helpers.bias_variable([32], 'bias_conv1_b')
        alphas_conv1_b = helpers.bias_variable([32], 'alpha_conv1_b')

        layer_conv_1_b = helpers.prelu(
            helpers.conv2d(layer_conv_1, W_conv1_b) + b_conv1_b,
            alphas_conv1_b)

        stage_1_pool = helpers.max_pool_2x2(layer_conv_1_b)

    with tf.name_scope('conv_layer_2'):
        W_conv2 = helpers.weight_variable([5, 5, 32, 64], "W_conv2")
        b_conv2 = helpers.bias_variable([64], 'bias_conv2')
        alphas_conv2 = helpers.bias_variable([64], 'alpha_conv2')
        layer_conv_2 = helpers.prelu(
            helpers.conv2d(stage_1_pool, W_conv2) + b_conv2, alphas_conv2)

        W_conv2_b = helpers.weight_variable([5, 5, 64, 64], "W_conv2_b")
        b_conv2_b = helpers.bias_variable([64], 'bias_conv2_b')
        alphas_conv2_b = helpers.bias_variable([64], 'alpha_conv2_b')
        layer_conv_2_b = helpers.prelu(
            helpers.conv2d(layer_conv_2, W_conv2_b) + b_conv2_b,
            alphas_conv2_b)

        stage_2_pool = helpers.max_pool_2x2(layer_conv_2_b)
        # stage_2_pool_flat = tf.reshape(stage_2_pool, [-1, 7 * 7 * 64])

    with tf.name_scope('conv_layer_3'):
        W_conv3 = helpers.weight_variable([5, 5, 64, 128], "W_conv3")
        b_conv3 = helpers.bias_variable([128], 'bias_conv3')
        alphas_conv3 = helpers.bias_variable([128], 'alpha_conv3')
        layer_conv_3 = helpers.prelu(
            helpers.conv2d(stage_2_pool, W_conv3) + b_conv3, alphas_conv3)

        # stage_3_pool = helpers.max_pool_2x2(layer_conv_3)
        # stage_3_pool_flat = tf.reshape(stage_3_pool, [-1, 4 * 4 * 256])

        W_conv3_b = helpers.weight_variable([5, 5, 128, 128], "W_conv3_b")
        b_conv3_b = helpers.bias_variable([128], 'bias_conv3_b')
        alphas_conv3_b = helpers.bias_variable([128], 'alpha_conv3_b')
        layer_conv_3_b = helpers.prelu(
            helpers.conv2d(layer_conv_3, W_conv3_b) + b_conv3_b,
            alphas_conv3_b)

        stage_3_pool = helpers.max_pool_2x2(layer_conv_3_b)
        stage_3_pool_flat = tf.reshape(stage_3_pool, [-1, 4 * 4 * 128])

    with tf.name_scope('fc_layer_1'):
        W_fc1 = helpers.weight_variable([4 * 4 * 128, 2], "W_fc1")
        # W_fc1 = helpers.weight_variable([7 * 7 * 64, 2], "W_fc1")
        b_fc1 = helpers.bias_variable([2], 'bias_fc1')
        alphas_fc1 = helpers.bias_variable([2], 'alpha_conv3')
        output = helpers.prelu(
            tf.matmul(stage_3_pool_flat, W_fc1) + b_fc1, alphas_fc1)

    # with tf.name_scope('fc_output'):
    #     W_output = helpers.weight_variable([500, 10], "W_putput")
    #     b_output = helpers.bias_variable([10], 'bias_output')
    #     output = tf.nn.relu(tf.matmul(h_fc1, W_output) + b_output)

    # with tf.name_scope('output'):
    #     W_output = helpers.weight_variable([2, 10], "W_output")
    #     b_output = helpers.bias_variable([10])
    #     output = tf.nn.relu(tf.matmul(h_fc2, W_output) + b_output)

    return x, output