Exemple #1
0
def model(x, n_outputs):
    """
    Function that builds the graph for the neural network.
    :param x: Input data
    :param n_outputs: Number of output neurons
    :return: Output vector of network, keep probability, and weight matrices in the network
    """
    # First convolutional layer
    x_image = tf.reshape(x, [-1, 250, 250, 3])
    w_conv1 = weight_variable([10, 10, 3, 6], name="w_conv1")
    b_conv1 = bias_variable([6], name="b_conv1")
    h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)

    # Second convolutional layer
    w_conv2 = weight_variable([5, 5, 6, 16], name="w_conv2")
    b_conv2 = bias_variable([16], name="b_conv2")
    h_conv2 = tf.nn.relu(conv2d(h_conv1, w_conv2) + b_conv2)

    # Fully connected layer
    w_fc1 = weight_variable([237 * 237 * 16, 80], name="w_fc1")
    b_fc1 = bias_variable([80], name="b_fc1")
    h_conv2_flat = tf.reshape(h_conv2, [-1, 237 * 237 * 16])
    h_fc1 = tf.nn.relu(tf.matmul(h_conv2_flat, w_fc1) + b_fc1)

    # Dropout on the fully connected layer
    dropout = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, rate=dropout)

    # Output layer
    w_fc2 = weight_variable([80, n_outputs], name="w_fc2")
    b_fc2 = bias_variable([n_outputs], name="b_fc2")
    y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2

    # Returns the prediction and the dropout probability placeholder
    return y_conv, dropout, w_conv1, w_conv2, w_fc1, w_fc2
Exemple #2
0
    def create_UGRNN_variable(self):
        with tf.variable_scope(self._Unique_ID + "EncodingNN") as scope:
            contextual_features = tf.get_variable(
                "contextual_features", [
                    config.max_seq_len * config.max_seq_len * 4,
                    self.encoding_nn_output_size
                ],
                dtype=tf.float32,
                initializer=tf.constant_initializer(0),
                trainable=False)

            with tf.variable_scope('hidden1') as scope:
                weights = nn_utils.weight_variable(
                    [
                        self.encoding_nn_input_size,
                        self.encoding_nn_hidden_size
                    ],
                    initializer=self.initializer_fun)
                biases = nn_utils.bias_variable([self.encoding_nn_hidden_size])
                self.trainable_variables.append(weights)
                self.trainable_variables.append(biases)

            with tf.variable_scope('output') as scope:
                weights = nn_utils.weight_variable(
                    [
                        self.encoding_nn_hidden_size,
                        self.encoding_nn_output_size
                    ],
                    initializer=self.initializer_fun)

                biases = nn_utils.bias_variable([self.encoding_nn_output_size])
                self.trainable_variables.append(weights)
                self.trainable_variables.append(biases)

        with tf.variable_scope(self._Unique_ID + "OutputNN") as scope:
            with tf.variable_scope('hidden1') as scope:
                weights = nn_utils.weight_variable(
                    [self.output_nn_input_size, self.output_nn_hidden_size],
                    self.initializer_fun, 'weights_decay')

                biases = nn_utils.bias_variable([self.output_nn_hidden_size])
                self.trainable_variables.append(weights)
                self.trainable_variables.append(biases)

            with tf.variable_scope('output') as scope:
                weights = nn_utils.weight_variable(
                    [self.output_nn_hidden_size, 1], self.initializer_fun,
                    'weights_decay')
                self.trainable_variables.append(weights)
    def build_graph_forLwF(self, sess):
        with tf.variable_scope("AlexNet_ImageNet"):
            train_x = np.zeros((1, 227, 227, 3)).astype(float32)
            train_y = np.zeros((1, 1000))
            xdim = train_x.shape[1:]
            ydim = train_y.shape[1]

            net_data = load("bvlc_alexnet.npy").item()

            def conv(input,
                     kernel,
                     biases,
                     k_h,
                     k_w,
                     c_o,
                     s_h,
                     s_w,
                     padding="VALID",
                     group=1):
                '''From https://github.com/ethereon/caffe-tensorflow
			    '''
                c_i = input.get_shape()[-1]
                assert c_i % group == 0
                assert c_o % group == 0
                convolve = lambda i, k: tf.nn.conv2d(
                    i, k, [1, s_h, s_w, 1], padding=padding)

                if group == 1:
                    conv = convolve(input, kernel)
                else:
                    input_groups = tf.split(3, group, input)
                    kernel_groups = tf.split(3, group, kernel)
                    output_groups = [
                        convolve(i, k)
                        for i, k in zip(input_groups, kernel_groups)
                    ]
                    conv = tf.concat(3, output_groups)
                return tf.reshape(tf.nn.bias_add(conv, biases),
                                  [-1] + conv.get_shape().as_list()[1:])

            x = tf.placeholder(tf.float32, (None, ) + xdim, name='imgTensor')

            #conv1
            #conv(11, 11, 96, 4, 4, padding='VALID', name='conv1')
            k_h = 11
            k_w = 11
            c_o = 96
            s_h = 4
            s_w = 4
            conv1W = tf.Variable(net_data["conv1"][0])
            conv1b = tf.Variable(net_data["conv1"][1])
            conv1_in = conv(x,
                            conv1W,
                            conv1b,
                            k_h,
                            k_w,
                            c_o,
                            s_h,
                            s_w,
                            padding="SAME",
                            group=1)
            conv1 = tf.nn.relu(conv1_in)

            #lrn1
            #lrn(2, 2e-05, 0.75, name='norm1')
            radius = 2
            alpha = 2e-05
            beta = 0.75
            bias = 1.0
            lrn1 = tf.nn.local_response_normalization(conv1,
                                                      depth_radius=radius,
                                                      alpha=alpha,
                                                      beta=beta,
                                                      bias=bias)

            #maxpool1
            #max_pool(3, 3, 2, 2, padding='VALID', name='pool1')
            k_h = 3
            k_w = 3
            s_h = 2
            s_w = 2
            padding = 'VALID'
            maxpool1 = tf.nn.max_pool(lrn1,
                                      ksize=[1, k_h, k_w, 1],
                                      strides=[1, s_h, s_w, 1],
                                      padding=padding)

            #conv2
            #conv(5, 5, 256, 1, 1, group=2, name='conv2')
            k_h = 5
            k_w = 5
            c_o = 256
            s_h = 1
            s_w = 1
            group = 2
            conv2W = tf.Variable(net_data["conv2"][0])
            conv2b = tf.Variable(net_data["conv2"][1])
            conv2_in = conv(maxpool1,
                            conv2W,
                            conv2b,
                            k_h,
                            k_w,
                            c_o,
                            s_h,
                            s_w,
                            padding="SAME",
                            group=group)
            conv2 = tf.nn.relu(conv2_in)

            #lrn2
            #lrn(2, 2e-05, 0.75, name='norm2')
            radius = 2
            alpha = 2e-05
            beta = 0.75
            bias = 1.0
            lrn2 = tf.nn.local_response_normalization(conv2,
                                                      depth_radius=radius,
                                                      alpha=alpha,
                                                      beta=beta,
                                                      bias=bias)

            #maxpool2
            #max_pool(3, 3, 2, 2, padding='VALID', name='pool2')
            k_h = 3
            k_w = 3
            s_h = 2
            s_w = 2
            padding = 'VALID'
            maxpool2 = tf.nn.max_pool(lrn2,
                                      ksize=[1, k_h, k_w, 1],
                                      strides=[1, s_h, s_w, 1],
                                      padding=padding)

            #conv3
            #conv(3, 3, 384, 1, 1, name='conv3')
            k_h = 3
            k_w = 3
            c_o = 384
            s_h = 1
            s_w = 1
            group = 1
            conv3W = tf.Variable(net_data["conv3"][0])
            conv3b = tf.Variable(net_data["conv3"][1])
            conv3_in = conv(maxpool2,
                            conv3W,
                            conv3b,
                            k_h,
                            k_w,
                            c_o,
                            s_h,
                            s_w,
                            padding="SAME",
                            group=group)
            conv3 = tf.nn.relu(conv3_in)

            #conv4
            #conv(3, 3, 384, 1, 1, group=2, name='conv4')
            k_h = 3
            k_w = 3
            c_o = 384
            s_h = 1
            s_w = 1
            group = 2
            conv4W = tf.Variable(net_data["conv4"][0])
            conv4b = tf.Variable(net_data["conv4"][1])
            conv4_in = conv(conv3,
                            conv4W,
                            conv4b,
                            k_h,
                            k_w,
                            c_o,
                            s_h,
                            s_w,
                            padding="SAME",
                            group=group)
            conv4 = tf.nn.relu(conv4_in)

            #conv5
            #conv(3, 3, 256, 1, 1, group=2, name='conv5')
            k_h = 3
            k_w = 3
            c_o = 256
            s_h = 1
            s_w = 1
            group = 2
            conv5W = tf.Variable(net_data["conv5"][0])
            conv5b = tf.Variable(net_data["conv5"][1])
            conv5_in = conv(conv4,
                            conv5W,
                            conv5b,
                            k_h,
                            k_w,
                            c_o,
                            s_h,
                            s_w,
                            padding="SAME",
                            group=group)
            conv5 = tf.nn.relu(conv5_in)

            #maxpool5
            #max_pool(3, 3, 2, 2, padding='VALID', name='pool5')
            k_h = 3
            k_w = 3
            s_h = 2
            s_w = 2
            padding = 'VALID'
            maxpool5 = tf.nn.max_pool(conv5,
                                      ksize=[1, k_h, k_w, 1],
                                      strides=[1, s_h, s_w, 1],
                                      padding=padding)

            # maxpool5 = tf.stop_gradient(maxpool5)

            #fc6
            #fc(4096, name='fc6')
            fc6W = tf.Variable(net_data["fc6"][0])
            fc6b = tf.Variable(net_data["fc6"][1])
            fc6 = tf.nn.relu_layer(tf.reshape(
                maxpool5, [-1, int(prod(maxpool5.get_shape()[1:]))]),
                                   fc6W,
                                   fc6b,
                                   name='fc6')

            #fc7
            #fc(4096, name='fc7')
            fc7W = tf.Variable(net_data["fc7"][0], name='fc7W')
            fc7b = tf.Variable(net_data["fc7"][1], name='fc7b')
            fc7 = tf.nn.relu_layer(fc6, fc7W, fc7b, name='fc7')

            fc7 = tf.stop_gradient(fc7)

            W_fc2 = weight_variable([4096, self.class_count])
            b_fc2 = bias_variable([self.class_count])
            scores = tf.matmul(fc7, W_fc2) + b_fc2

            object_or_not = tf.placeholder("float", [None])
            labelTensor = tf.placeholder("float", [None, self.class_count])

        return object_or_not, labelTensor, x, scores, fc7
    def build_basic_graph(self, sess):
        # W_conv1 = weight_variable([8, 8, 3, 32])
        # b_conv1 = bias_variable([32])

        # W_conv2 = weight_variable([4, 4, 32, 64])
        # b_conv2 = bias_variable([64])

        # W_conv3 = weight_variable([3, 3, 64, 64])
        # b_conv3 = bias_variable([64])

        # W_fc1 = weight_variable([6400, 512])
        # b_fc1 = bias_variable([512])

        # W_fc2 = weight_variable([512, self.class_count])
        # b_fc2 = bias_variable([self.class_count])

        # imgTensor = tf.placeholder("float", [None, 80, 80, 3])
        # labelTensor = tf.placeholder("float", [None, self.class_count])
        # object_or_not = tf.placeholder("float", [None])

        #    # hidden layers
        # conv1 = tf.nn.relu(conv2d(imgTensor, W_conv1, 4) + b_conv1)

        # conv2 = tf.nn.relu(conv2d(conv1, W_conv2, 2) + b_conv2)

        # conv3 = tf.nn.relu(conv2d(conv2, W_conv3, 1) + b_conv3)

        # conv3_flat = tf.reshape(conv3, [-1, 6400])

        # h_fc1 = tf.nn.relu(tf.matmul(conv3_flat, W_fc1) + b_fc1)

        # scores = tf.matmul(h_fc1, W_fc2) + b_fc2

        # newSaver = tf.train.import_meta_graph(ALEXNET_META)
        # newSaver.restore(sess, ALEXNET_CKPT)

        # # print [n.name for n in tf.get_default_graph().as_graph_def().node]

        # h_fc1 = sess.graph.get_tensor_by_name('fc7:0')
        # print h_fc1

        # object_or_not = tf.placeholder("float", [None])
        # labelTensor = tf.placeholder(tf.float32, [None, self.class_count])
        # imgTensor = sess.graph.get_tensor_by_name('imgTensor:0')
        # print imgTensor
        # W_fc2 = weight_variable([4096, self.class_count])
        # b_fc2 = bias_variable([self.class_count])

        # scores = tf.matmul(h_fc1, W_fc2) + b_fc2

        train_x = np.zeros((1, 227, 227, 3)).astype(float32)
        train_y = np.zeros((1, 1000))
        xdim = train_x.shape[1:]
        ydim = train_y.shape[1]

        net_data = load("bvlc_alexnet.npy").item()

        def conv(input,
                 kernel,
                 biases,
                 k_h,
                 k_w,
                 c_o,
                 s_h,
                 s_w,
                 padding="VALID",
                 group=1):
            '''From https://github.com/ethereon/caffe-tensorflow
		    '''
            c_i = input.get_shape()[-1]
            assert c_i % group == 0
            assert c_o % group == 0
            convolve = lambda i, k: tf.nn.conv2d(
                i, k, [1, s_h, s_w, 1], padding=padding)

            if group == 1:
                conv = convolve(input, kernel)
            else:
                input_groups = tf.split(3, group, input)
                kernel_groups = tf.split(3, group, kernel)
                output_groups = [
                    convolve(i, k) for i, k in zip(input_groups, kernel_groups)
                ]
                conv = tf.concat(3, output_groups)
            return tf.reshape(tf.nn.bias_add(conv, biases),
                              [-1] + conv.get_shape().as_list()[1:])

        x = tf.placeholder(tf.float32, (None, ) + xdim, name='imgTensor')

        #conv1
        #conv(11, 11, 96, 4, 4, padding='VALID', name='conv1')
        k_h = 11
        k_w = 11
        c_o = 96
        s_h = 4
        s_w = 4
        conv1W = tf.Variable(net_data["conv1"][0])
        conv1b = tf.Variable(net_data["conv1"][1])
        conv1_in = conv(x,
                        conv1W,
                        conv1b,
                        k_h,
                        k_w,
                        c_o,
                        s_h,
                        s_w,
                        padding="SAME",
                        group=1)
        conv1 = tf.nn.relu(conv1_in)

        #lrn1
        #lrn(2, 2e-05, 0.75, name='norm1')
        radius = 2
        alpha = 2e-05
        beta = 0.75
        bias = 1.0
        lrn1 = tf.nn.local_response_normalization(conv1,
                                                  depth_radius=radius,
                                                  alpha=alpha,
                                                  beta=beta,
                                                  bias=bias)

        #maxpool1
        #max_pool(3, 3, 2, 2, padding='VALID', name='pool1')
        k_h = 3
        k_w = 3
        s_h = 2
        s_w = 2
        padding = 'VALID'
        maxpool1 = tf.nn.max_pool(lrn1,
                                  ksize=[1, k_h, k_w, 1],
                                  strides=[1, s_h, s_w, 1],
                                  padding=padding)

        #conv2
        #conv(5, 5, 256, 1, 1, group=2, name='conv2')
        k_h = 5
        k_w = 5
        c_o = 256
        s_h = 1
        s_w = 1
        group = 2
        conv2W = tf.Variable(net_data["conv2"][0])
        conv2b = tf.Variable(net_data["conv2"][1])
        conv2_in = conv(maxpool1,
                        conv2W,
                        conv2b,
                        k_h,
                        k_w,
                        c_o,
                        s_h,
                        s_w,
                        padding="SAME",
                        group=group)
        conv2 = tf.nn.relu(conv2_in)

        #lrn2
        #lrn(2, 2e-05, 0.75, name='norm2')
        radius = 2
        alpha = 2e-05
        beta = 0.75
        bias = 1.0
        lrn2 = tf.nn.local_response_normalization(conv2,
                                                  depth_radius=radius,
                                                  alpha=alpha,
                                                  beta=beta,
                                                  bias=bias)

        #maxpool2
        #max_pool(3, 3, 2, 2, padding='VALID', name='pool2')
        k_h = 3
        k_w = 3
        s_h = 2
        s_w = 2
        padding = 'VALID'
        maxpool2 = tf.nn.max_pool(lrn2,
                                  ksize=[1, k_h, k_w, 1],
                                  strides=[1, s_h, s_w, 1],
                                  padding=padding)

        #conv3
        #conv(3, 3, 384, 1, 1, name='conv3')
        k_h = 3
        k_w = 3
        c_o = 384
        s_h = 1
        s_w = 1
        group = 1
        conv3W = tf.Variable(net_data["conv3"][0])
        conv3b = tf.Variable(net_data["conv3"][1])
        conv3_in = conv(maxpool2,
                        conv3W,
                        conv3b,
                        k_h,
                        k_w,
                        c_o,
                        s_h,
                        s_w,
                        padding="SAME",
                        group=group)
        conv3 = tf.nn.relu(conv3_in)

        #conv4
        #conv(3, 3, 384, 1, 1, group=2, name='conv4')
        k_h = 3
        k_w = 3
        c_o = 384
        s_h = 1
        s_w = 1
        group = 2
        conv4W = tf.Variable(net_data["conv4"][0])
        conv4b = tf.Variable(net_data["conv4"][1])
        conv4_in = conv(conv3,
                        conv4W,
                        conv4b,
                        k_h,
                        k_w,
                        c_o,
                        s_h,
                        s_w,
                        padding="SAME",
                        group=group)
        conv4 = tf.nn.relu(conv4_in)

        conv4 = tf.stop_gradient(conv4)

        #conv5
        #conv(3, 3, 256, 1, 1, group=2, name='conv5')
        k_h = 3
        k_w = 3
        c_o = 256
        s_h = 1
        s_w = 1
        group = 2
        conv5W = tf.Variable(net_data["conv5"][0])
        conv5b = tf.Variable(net_data["conv5"][1])
        conv5_in = conv(conv4,
                        conv5W,
                        conv5b,
                        k_h,
                        k_w,
                        c_o,
                        s_h,
                        s_w,
                        padding="SAME",
                        group=group)
        conv5 = tf.nn.relu(conv5_in)

        #maxpool5
        #max_pool(3, 3, 2, 2, padding='VALID', name='pool5')
        k_h = 3
        k_w = 3
        s_h = 2
        s_w = 2
        padding = 'VALID'
        maxpool5 = tf.nn.max_pool(conv5,
                                  ksize=[1, k_h, k_w, 1],
                                  strides=[1, s_h, s_w, 1],
                                  padding=padding)

        # maxpool5 = tf.stop_gradient(maxpool5)

        #fc6
        #fc(4096, name='fc6')
        fc6W = tf.Variable(net_data["fc6"][0])
        fc6b = tf.Variable(net_data["fc6"][1])
        fc6 = tf.nn.relu_layer(tf.reshape(
            maxpool5, [-1, int(prod(maxpool5.get_shape()[1:]))]),
                               fc6W,
                               fc6b,
                               name='fc6')

        #fc7
        #fc(4096, name='fc7')
        fc7W = tf.Variable(net_data["fc7"][0], name='fc7W')
        fc7b = tf.Variable(net_data["fc7"][1], name='fc7b')
        fc7 = tf.nn.relu_layer(fc6, fc7W, fc7b, name='fc7')

        W_fc2 = weight_variable([4096, self.class_count])
        b_fc2 = bias_variable([self.class_count])
        scores = tf.matmul(fc7, W_fc2) + b_fc2

        object_or_not = tf.placeholder("float", [None])
        labelTensor = tf.placeholder("float", [None, self.class_count])

        # init = tf.initialize_all_variables()
        # sess = tf.Session()
        # sess.run(init)
        return object_or_not, labelTensor, x, scores, fc7