示例#1
0
    def call(self, inputs, is_testing=False):
        conv1_layer = tf.nn.conv2d(inputs, self.conv_1_filter, [1, 2, 2, 1],
                                   "SAME")
        conv1_out = tf.nn.bias_add(conv1_layer, self.conv_1_bias)
        m, v = tf.nn.moments(conv1_out, [0])
        conv1_norm = tf.nn.batch_normalization(conv1_out, m, v, None, None,
                                               0.001)
        conv1 = tf.nn.relu(conv1_norm)

        max_pool1 = tf.nn.max_pool(conv1, [3, 3], [1, 2, 2, 1], "SAME")

        conv2_layer = tf.nn.conv2d(max_pool1, self.conv_2_filter, [1, 1, 1, 1],
                                   "SAME")
        conv2_out = tf.nn.bias_add(conv2_layer, self.conv_2_bias)
        m, v = tf.nn.moments(conv2_out, [0])
        conv2_norm = tf.nn.batch_normalization(conv2_out, m, v, None, None,
                                               0.001)
        conv2 = tf.nn.relu(conv2_norm)

        max_pool2 = tf.nn.max_pool(conv2, [2, 2], [1, 2, 2, 1], "SAME")

        if is_testing:
            conv3_layer = conv2d(max_pool2, self.conv_3_filter, [1, 1, 1, 1],
                                 "SAME")
        else:
            conv3_layer = tf.nn.conv2d(max_pool2, self.conv_3_filter,
                                       [1, 1, 1, 1], "SAME")
        conv3_out = tf.nn.bias_add(conv3_layer, self.conv_3_bias)
        m, v = tf.nn.moments(conv3_out, [0])
        conv3_norm = tf.nn.batch_normalization(conv3_out, m, v, None, None,
                                               0.001)
        conv3 = tf.nn.relu(conv3_norm)

        input_size = conv3.get_shape().as_list()
        size2 = input_size[-1] * input_size[-2] * input_size[-3]
        result = tf.reshape(conv3, [-1, size2])

        result1 = tf.matmul(result, self.dense_w_1) + self.dense_b_1
        result1 = tf.nn.dropout(result1, 0.3)
        result2 = tf.matmul(result1, self.dense_w_2) + self.dense_b_2
        result2 = tf.nn.dropout(result2, 0.3)
        result3 = tf.matmul(result2, self.dense_w_3) + self.dense_b_3
        return result3
示例#2
0
    def call(self, inputs, is_testing=False):
        """
        Runs a forward pass on an input batch of images.
        :param inputs: images, shape of (num_inputs, 32, 32, 3); during training, the shape is (batch_size, 32, 32, 3)
        :param is_testing: a boolean that should be set to True only when you're doing Part 2 of the assignment and this function is being called during testing
        :return: logits - a matrix of shape (num_inputs, num_classes); during training, it would be (batch_size, 2)
        """
        # Remember that
        # shape of input = (num_inputs (or batch_size), in_height, in_width, in_channels)
        # shape of filter = (filter_height, filter_width, in_channels, out_channels)
        # shape of strides = (batch_stride, height_stride, width_stride, channels_stride)

        x = tf.nn.conv2d(inputs, self.C1, strides=[2, 2], padding='SAME')
        mean, variance = tf.nn.moments(x, [0, 1, 2])
        x = tf.nn.batch_normalization(x,
                                      mean=mean,
                                      variance=variance,
                                      variance_epsilon=1e-5,
                                      offset=None,
                                      scale=None)
        x = tf.nn.relu(x)
        x = tf.nn.max_pool(x, ksize=[3, 3], strides=[2, 2], padding='SAME')
        # print(x.shape)

        x = tf.nn.conv2d(x, self.C2, strides=[2, 2], padding='SAME')
        mean, variance = tf.nn.moments(x, [0, 1, 2])
        x = tf.nn.batch_normalization(x,
                                      mean=mean,
                                      variance=variance,
                                      variance_epsilon=1e-5,
                                      offset=None,
                                      scale=None)
        x = tf.nn.relu(x)
        x = tf.nn.max_pool(x, ksize=[2, 2], strides=[2, 2], padding='SAME')
        # print(x.shape)

        if is_testing is True:
            x = conv2d(x, self.C3, strides=[1, 1, 1, 1], padding='SAME')
        else:
            x = tf.nn.conv2d(x, self.C3, strides=[1, 1], padding='SAME')

        mean, variance = tf.nn.moments(x, [0, 1, 2])
        x = tf.nn.batch_normalization(x,
                                      mean=mean,
                                      variance=variance,
                                      variance_epsilon=1e-5,
                                      offset=None,
                                      scale=None)
        x = tf.nn.relu(x)

        # print(x.shape)

        x = tf.reshape(x, [self.batch_size, -1])
        # print(x.shape)

        #dense layer 1
        x = x @ self.W1 + self.b1
        x = tf.nn.dropout(x, rate=0.3)
        # #dense layer 2
        x = x @ self.W2 + self.b2
        x = tf.nn.dropout(x, rate=0.3)
        #dense layer 3
        x = x @ self.W3 + self.b3

        x = tf.nn.softmax(x)

        return x
# Inputs and truth placeholder
x, t = cnv.get_placeholders(784,10)

# ------------------------------------------ #
# Construct network...
# ------------------------------------------ #

# First layer
W_conv1 = cnv.weight_variable([5, 5, 1, 32])
b_conv1 = cnv.bias_variable([32])

# Apply filter
x_image = tf.reshape(x, [-1, 28, 28, 1])

# Convolve layer with filter
h_conv1 = tf.nn.relu(cnv.conv2d(x_image, W_conv1, 1) + b_conv1)
h_pool1 = cnv.max_pool_2x2(h_conv1)

# ------------------------------------------ #
# Second layer
# ------------------------------------------ #

# Weight & Bias
W_conv2 = cnv.weight_variable([5, 5, 32, 64])
b_conv2 = cnv.bias_variable([64])

# ReLU activation function & Max-pooling layer
h_conv2 = tf.nn.relu(cnv.conv2d(h_pool1, W_conv2, 1) + b_conv2)
h_pool2 = cnv.max_pool_2x2(h_conv2)

# ------------------------------------------ #
    def call(self, inputs, is_testing=False):
        """
		Runs a forward pass on an input batch of images.
		:param inputs: images, shape of (num_inputs, 32, 32, 3); during training, the shape is (batch_size, 32, 32, 3)
		:param is_testing: a boolean that should be set to True only when you're doing Part 2 of the assignment
							and this function is being called during testing
		:return: logits - a matrix of shape (num_inputs, num_classes); during training, it would be (batch_size, 2)
		"""
        # Remember that
        # shape of input = (num_inputs (or batch_size), in_height, in_width, in_channels)
        # shape of filter = (filter_height, filter_width, in_channels, out_channels)
        # shape of strides = (batch_stride, height_stride, width_stride, channels_stride)

        # layer1
        layer1_conv = tf.nn.conv2d(inputs, self.filter1, self.stride1, 'SAME')
        mean1, variance1 = tf.nn.moments(layer1_conv, axes=[0, 1, 2])
        layer1_norm = tf.nn.batch_normalization(layer1_conv,
                                                mean1,
                                                variance1,
                                                offset=None,
                                                scale=None,
                                                variance_epsilon=1e-3)
        layer1_relu = tf.nn.relu(layer1_norm)
        layer1_pool = tf.nn.max_pool(layer1_relu, self.layer1_pool_ksize,
                                     self.layer1_pool_stride, 'SAME')

        # layer2
        layer2_conv = tf.nn.conv2d(layer1_pool, self.filter2, self.stride2,
                                   'SAME')
        mean2, variance2 = tf.nn.moments(layer2_conv, axes=[0, 1, 2])
        layer2_norm = tf.nn.batch_normalization(layer2_conv,
                                                mean2,
                                                variance2,
                                                offset=None,
                                                scale=None,
                                                variance_epsilon=1e-3)
        layer2_relu = tf.nn.relu(layer2_norm)
        layer2_pool = tf.nn.max_pool(layer2_relu, self.layer2_pool_ksize,
                                     self.layer2_pool_stride, 'SAME')

        # layer3
        layer3_conv = tf.nn.conv2d(layer2_pool, self.filter3, self.stride3,
                                   'SAME')
        mean3, variance3 = tf.nn.moments(layer3_conv, axes=[0, 1, 2])
        layer3_norm = tf.nn.batch_normalization(layer3_conv,
                                                mean3,
                                                variance3,
                                                offset=None,
                                                scale=None,
                                                variance_epsilon=1e-3)
        layer3_relu = tf.nn.relu(layer3_norm)
        layer3_pool = tf.nn.max_pool(layer3_relu, self.layer3_pool_ksize,
                                     self.layer3_pool_stride, 'SAME')

        # layer4
        if is_testing is True:
            layer4_conv = conv2d(layer3_pool, self.filter4, self.stride4,
                                 'SAME')
        else:
            layer4_conv = tf.nn.conv2d(layer3_pool, self.filter4, self.stride4,
                                       'SAME')
        mean4, variance4 = tf.nn.moments(layer4_conv, axes=[0, 1, 2])
        layer4_norm = tf.nn.batch_normalization(layer4_conv,
                                                mean4,
                                                variance4,
                                                offset=None,
                                                scale=None,
                                                variance_epsilon=1e-3)
        layer4_relu = tf.nn.relu(layer4_norm)
        layer4_pool = tf.nn.max_pool(layer4_relu, self.layer4_pool_ksize,
                                     self.layer4_pool_stride, 'SAME')

        # flatten
        dense_input = tf.reshape(layer4_pool, [-1, self.flatten_width])

        # fully connected layer1
        dense_layer1 = tf.nn.relu(tf.matmul(dense_input, self.w1) + self.b1)
        dense_layer1 = tf.nn.dropout(dense_layer1, rate=0.3)

        # fully connected layer2
        dense_layer2 = tf.nn.relu(tf.matmul(dense_layer1, self.w2) + self.b2)
        dense_layer2 = tf.nn.dropout(dense_layer2, rate=0.3)

        # fully connected layer3
        dense_layer3 = tf.nn.relu(tf.matmul(dense_layer2, self.w3) + self.b3)
        dense_layer3 = tf.nn.dropout(dense_layer3, rate=0.3)

        # fully connected layer4
        dense_layer4 = tf.nn.relu(tf.matmul(dense_layer3, self.w4) + self.b4)

        logits = dense_layer4
        return logits
示例#5
0
                    y[i, j] += kernal[a, b] * img[i + a, j + b]
    return y


img = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) * 1.0
k = np.array([[1, 2], [2, 3]]) * 1.0

# img = np.random.randn(150, 150)
# k = np.random.randn(9, 7)

t = time.time()
_conv2d(img, k)
print 'python:', time.time() - t

t = time.time()
convolution.conv2d(img, k)
print 'cython:', time.time() - t

t = time.time()
convolution.deconv2d(img, k)
print 'deconv:', time.time() - t

t = time.time()
convolve2d(img, np.rot90(k, 2), mode='valid')
print 'scipy:', time.time() - t

c = convolution.conv2d(img, k)
print c
print convolve2d(img, np.rot90(k, 2), mode='valid')
print convolution.deconv2d(c, k)
print k
示例#6
0
    def call(self, inputs, is_testing=False):
        """
		Runs a forward pass on an input batch of images.
		:param inputs: images, shape of (num_inputs, 32, 32, 3); during training, the shape is (batch_size, 32, 32, 3)
		:param is_testing: a boolean that should be set to True only when you're doing Part 2 of the assignment and this function is being called during testing
		:return: logits - a matrix of shape (num_inputs, num_classes); during training, it would be (batch_size, 2)
		"""
        # shape of input = (num_inputs (or batch_size), in_height, in_width, in_channels)
        # shape of filter = (filter_height, filter_width, in_channels, out_channels)
        # shape of strides = (batch_stride, height_stride, width_stride, channels_stride)
        conv_1 = tf.nn.conv2d(inputs,
                              self.W["conv_1"], [1, 1, 1, 1],
                              padding="SAME")
        conv_1_with_bias = tf.nn.bias_add(conv_1, self.B["conv_1"])

        mean_1, var_1 = tf.nn.moments(conv_1_with_bias, [0, 1, 2])
        norm_1 = tf.nn.batch_normalization(conv_1_with_bias, mean_1, var_1,
                                           None, None, self.learning_rate)

        relu_1 = tf.nn.relu(norm_1)

        pool_1 = tf.nn.max_pool(relu_1, 3, [1, 2, 2, 1], padding="SAME")

        conv_2 = tf.nn.conv2d(pool_1,
                              self.W["conv_2"], [1, 1, 1, 1],
                              padding="SAME")
        conv_2_with_bias = tf.nn.bias_add(conv_2, self.B["conv_2"])

        mean_2, var_2 = tf.nn.moments(conv_2_with_bias, [0, 1, 2])
        norm_2 = tf.nn.batch_normalization(conv_2_with_bias, mean_2, var_2,
                                           None, None, self.learning_rate)

        relu_2 = tf.nn.relu(norm_2)

        pool_2 = tf.nn.max_pool(relu_2, 3, [1, 2, 2, 1], padding="SAME")

        #used for testing sutom cov2D function
        if is_testing:
            print("My Conv2d Begins..")
            conv_3 = conv2d(pool_2,
                            self.W["conv_3"], [1, 1, 1, 1],
                            padding="SAME")
        else:
            conv_3 = tf.nn.conv2d(pool_2,
                                  self.W["conv_3"], [1, 1, 1, 1],
                                  padding="SAME")

        conv_3_with_bias = tf.nn.bias_add(conv_3, self.B["conv_3"])

        mean_3, var_3 = tf.nn.moments(conv_3_with_bias, [0, 1, 2])
        norm_3 = tf.nn.batch_normalization(conv_3_with_bias, mean_3, var_3,
                                           None, None, self.learning_rate)

        relu_3 = tf.nn.relu(norm_3)

        reshaped_layer = self.layer_reshape(relu_3)

        dense_1 = tf.nn.relu(
            tf.matmul(reshaped_layer, self.W["dense_1"]) + self.B["dense_1"])

        drop_1 = tf.nn.dropout(dense_1, self.drop_rate)

        dense_2 = tf.nn.relu(
            tf.matmul(drop_1, self.W["dense_2"]) + self.B["dense_2"])

        drop_2 = tf.nn.dropout(dense_2, self.drop_rate)

        logits = tf.nn.relu(
            tf.matmul(drop_2, self.W["dense_3"]) + self.B["dense_3"])

        return logits