def res_block_bottleneck(x, ksize_list, strides, is_training, data_format='NHWC', w_project=None, no_activation=False): """ Computes y = x + F(x), where F(x) is the residual block function. At downsample layers, applies a downsample function on x as well. """ if w_project is not None: x_ = tf.conv2d(x, w_project, strides, padding='SAME', data_format=data_format) else: x_ = x return x_ + _bottleneck_residual(x, ksize_list, strides, 'SAME', is_training, data_format=data_format, no_activation=no_activation)
def tf_detect_glassframe(rgb_img, seg_img): # input range [0,1] dX = seg_img[:, :, 1:] - seg_img[:, :, :-1] dX = tf.pad(dX, ((0, 0), (0, 0), (0, 1)), "constant") dY = seg_img[:, 1:, :] - seg_img[:, :-1, :] dY = tf.pad(dY, ((0, 0), (0, 1), (0, 0)), "constant") G = tf.sqrt(tf.square(dX) + tf.square(dY)) G = tf.where(tf.greater(G, 0.1), tf.ones_like(G), tf.zeros_like(G)) G = tf.expand_dims(G, axis=3) k = 10 kernel = np.ones((k, k), np.float32) / (k * k) kernel = tf.reshape(kernel, [k, k, 1, 1]) G_list = tf.split(G, G_list.get_shape().as_list()[-1], axis=-1) G_out_list = [] for g_channel in G_list: g_out_channel = tf.conv2d(g_channel, kernel, strides=[1, 1, 1, 1], padding='SAME') G_out_list.append(g_out_channel) mask = tf.concat(G_out_list, axis=-1) mask = tf.where(tf.greater(mask, 0.01), tf.ones_like(mask), tf.zeros_like(mask)) mask = tf.squeeze(mask) # convert rgb to hsv hsv_img = tf_rgb_to_hsv(rgb_img) v_img = hsv_img[:, :, :, 2] v_mask = tf.where(tf.less(v_img, 0.6), tf.ones_like(v_img), tf.zeros_like(v_img)) glassframe = v_mask * mask * seg_img return glassframe
def conv2d_op(x,name,shape,strides,pams): with tf.name_scope(name) as scope: W_conv=tf.get_variable(scope+'w',shape=shape,dtype=tf.float32, initializer=tf.contrib.layers.xavier_initializer()) b_conv=tf.Variable(tf.constant(0.0,shape=[shape[-1]],dtype=tf.float32),trainable=True,name='b') conv=tf.conv2d(x,W_conv,strides=strides,padding='SAME') h_conv=tf.nn.relu(tf.bias_add(conv,b_conv),name=scope) pams+=[W_conv,b_conv] return h_conv
def buildLayer(self, layers, layerDescription): layerType = layerDescription["type"] layerName = layerDescription["name"] output = None if layerType == "fully-connected": inputs = self.getSimpleInputs(layers, layerDescription) layerSize = int(layerDescription["size"]) output = tf.nn.fully_connected(inputs, layerSize) else if layerType == "relu": inputs = self.getSimpleInputs(layers, layerDescription) output = tf.nn.relu(inputs) else if layerType == "tanh": inputs = self.getSimpleInputs(layers, layerDescription) output = tf.nn.tanh(inputs) else if layerType == "sigmoid": inputs = self.getSimpleInputs(layers, layerDescription) output = tf.nn.sigmoid(inputs) else if layerType == "mul": inputs = self.getBinaryInputs(layers, layerDescription) output = tf.mul(inputs[0], inputs[1]) else if layerType == "add": inputs = self.getBinaryInputs(layers, layerDescription) output = tf.add(inputs[0], inputs[1]) else if layerType == "ones": layerSize = int(layerDescription["size"]) output = tf.ones(layerSize) else if layerType == "zeros": layerSize = int(layerDescription["size"]) output = tf.zeros(layerSize) else if layerType == "conv2d": layerSize = int(layerDescription["size"]) layerStride = int(layerDescription["stride"]) layerFilterSize = int(layerDescription["filter"]) inputs, padding = self.getSimple2DInputsAndPadding(layers, layerDescription, layerFilterSize) self.fixInputsForConv2d(inputs) filterWeights = self.makeConv2dFilterWeights(inputs, layerSize, layerStride, layerFilterSize) output = tf.conv2d(inputs, filterWeights, self.getConv2dStrides(layerStride), padding) layer[layerName] = output
def neural_network(): with tf.device('/cpu:0'), tf.name_scope("embedding"): embedding_size = 128 W = tf.Variable( tf.random_uniform([input_size, embedding_size], -1.0, 1.0)) embedding_chars = tf.embedding_lookup(W, X) embedded_chars_expanded = tf.expand_dims(embedding_chars, -1) num_filter = 128 filter_size = [3, 4, 5] pooled_output = [] for i, filter_size in enumerate(filter_sizes): with tf.name_scope("conv_maxpool_%s" % filter_size): filter_shape = [filter_size, embedding_size, 1, num_filter] W = tf.Variable(tf.truncated_normal(filter_shape, stdev=0.1)) b = tf.Variable(tf.constant(0.1, shape=[num_filter])) conv = tf.conv2d(embedded_chars_expanded, W, strides=[1, 1, 1, 1], padding="VALID") h = tf.nn.relu(tf.nn.bias_add(conv, b)) pooled = tf.nn.max_pool(h, ksize=[1, input_size - file_size, 1, 1], strides=[1, 1, 1, 1], padding="VALID") pooled_output.append(pooled) num_filters_total = num_filters * len(filter_sizes) h_pool = tf.concat(3, pooled_outputs) h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) # dropout with tf.name_scope("dropout"): h_drop = tf.nn.dropout(h_pool_flat, dropout_keep_prob) # output with tf.name_scope("output"): W = tf.get_variable("W", shape=[num_filters_total, num_classes], initializer=tf.contrib.layers.xavier_initializer()) b = tf.Variable(tf.constant(0.1, shape=[num_classes])) output = tf.nn.xw_plus_b(h_drop, W, b) return output
def forward(self, input_image): input_image = tf.reshape(input_image, [-1, 32, 32, 3]) activ = tf.nn.conv2d(input_image, filters=self.convW1, strides=[1, 4, 4, 1], padding='VALID') activ = tf.nn.conv2d(activ, filters=self.convW2, strides=[1, 2, 2, 1], padding='VALID') activ = tf.nn.conv2d(activ, filters=self.convW3, strides=[1, 2, 2, 1], padding='VALID') activ = tf.conv2d(activ, filters=self.conW4, strides=[1, 1, 1, 1], padding='VALID') activ = tf.slim.flatten(activ) activ = tf.matmul(activ, self.W1) + self.b1 activ = tf.matmul(activ, self.W2) + self.b2 return activ
def sparse_res_block_bottleneck(x, ksize_list, indices, block_params, strides, is_training, data_format='NHWC', w_project=None, no_activation=False, use_var=False): """ Computes y = x + F(x), where F(x) is the residual block function. At downsample layers, applies a downsample function on x as well. :param x: [Tensor] [N, H, W, C]. Input activation tensor, dtype float32. :param ksize_list: [list] List of list of 4 int. Kernel size for each convolution layer in the residual block. :param indices: [tuple] Non-sparse locations returned by reduce_mask. :param block_params: [tuple] BlockParam namedtuple. :param :return """ transpose = True if data_format == 'NCHW' else False p = sbnet_module.sparse_gather(x, indices.bin_counts, indices.active_block_indices, bsize=block_params.bsize, boffset=block_params.boffset, bstride=block_params.bstrides, transpose=transpose) if w_project is not None: x = tf.conv2d(x, w_project, strides, padding='SAME') # Set shape for BN in the residual function. if transpose: p.set_shape([ None, x.get_shape()[3], block_params.bsize[0], block_params.bsize[1] ]) else: p.set_shape([ None, block_params.bsize[0], block_params.bsize[1], x.get_shape()[3] ]) q = _bottleneck_residual(p, ksize_list, strides, 'VALID', is_training, data_format=data_format, no_activation=no_activation) if use_var: y = sbnet_module.sparse_scatter_var(q, indices.bin_counts, indices.active_block_indices, x, bsize=block_params.bsize_out, boffset=[0, 0], bstride=block_params.bsize_out, add=True, transpose=transpose) else: y = sbnet_module.sparse_scatter(q, indices.bin_counts, indices.active_block_indices, x, bsize=block_params.bsize_out, boffset=[0, 0], bstride=block_params.bsize_out, add=True, transpose=transpose) return y
def conv2d(x, W, strides, padding='SAME'): return tf.conv2d(x, W, strides=strides, padding=padding)
def inference(): # zero mean input mean = tf.constant(VGG_MEAN, dtype=tf.float32, shape=[1,1,1,3], name='img_mean') imgs = self.imgs - mean # color transform with tf.name_scope('color_space') as scope: kernel = tf.Variable(tf.truncated_normal([1,1,1,3]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[3],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv = tf.nn.relu(out, name=scope) with tf.name_scope('conv1_1') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,3,64]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[64],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv1_1 = tf.nn.relu(out, name=scope) with tf.name_scope('conv1_2') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,64,64]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[64],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv1_2 = tf.nn.relu(out, name=scope) with tf.name_scope('pool_1') as scope: pool_1 = tf.nn.max_pool(conv1_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool1') with tf.name_scope('conv2_1') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,64,128]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[128],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_1 = tf.nn.relu(out, name=scope) with tf.name_scope('conv2_2') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,128,128]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[128],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_2 = tf.nn.relu(out, name=scope) with tf.name_scope('pool_2') as scope: pool_2 = tf.nn.max_pool(conv2_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool2') with tf.name_scope('conv3_1') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,128,256]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_1 = tf.nn.relu(out, name=scope) with tf.name_scope('conv3_2') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,256,256]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv3_2 = tf.nn.relu(out, name=scope) with tf.name_scope('conv3_3') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,256,256]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[256],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv3_3 = tf.nn.relu(out, name=scope) with tf.name_scope('pool_3') as scope: pool_4 = tf.nn.max_pool(conv3_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool3') with tf.name_scope('conv4_1') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,256,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_1 = tf.nn.relu(out, name=scope) with tf.name_scope('conv4_2') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,512,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_2 = tf.nn.relu(out, name=scope) with tf.name_scope('conv4_3') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,512,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv2_1 = tf.nn.relu(out, name=scope) with tf.name_scope('pool_4') as scope: pool_4 = tf.nn.max_pool(conv4_3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool4') with tf.name_scope('conv5_1') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,512,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv5_1 = tf.nn.relu(out, name=scope) with tf.name_scope('conv5_2') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,512,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv5_2 = tf.nn.relu(out, name=scope) with tf.name_scope('conv5_3') as scope: kernel = tf.Variable(tf.truncated_normal([3,3,512,512]), dtype=tf.float32, stddev=1e-1, name='weights') conv = tf.conv2d(imgs, kernel, [1,1,1,1], padding='SAME') biases = tf.Variable(tf.constant(0.0,shape=[512],dtype=tf.float32), trainable=True,name='biases') out = tf.nn.bias_add(conv, biases) conv5_3 = tf.nn.relu(out, name=scope) with tf.name_scope('pool_5') as scope: pool_5 = tf.nn.max_pool(conv2_2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME', name='pool5') return pool5
import tensorflow as tf import cv2 # Create a 3x3 Gabor filter params = { 'ksize': (3, 3), 'sigma': 1.0, 'theta': 0, 'lambd': 15.0, 'gamma': 0.02 } filter = cv2.getGaborKernel(**params) # make the filter to have 4 dimensions. filter = tf.expand_dims(filter, 2) filter = tf.expand_dims(filter, 3) # Apply the filter on `image` answer = tf.conv2d(image, filter, strides=[1, 1, 1, 1], padding='SAME')
def convLayer(self, input, x): # conv and 최적화 featureMap = tf.conv2d(input (x, x)) return featureMap