def __init__(self, sess): self.num_epoch = 25 self.batch_size = 64 self.output_size = 32 self.sample_size = 64 self.image_size = 108 self.sess = sess self.g_bn0 = batch_norm(name='g_bn0') self.g_bn1 = batch_norm(name='g_bn1') self.g_bn2 = batch_norm(name='g_bn2') self.g_bn3 = batch_norm(name='g_bn3') self.d_bn1 = batch_norm(name='d_bn1') self.d_bn2 = batch_norm(name='d_bn2') self.d_bn3 = batch_norm(name='d_bn3') self.alpha = 20000 self.beta = 1 self.proj_dir = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) self.sample_dir = os.path.join(self.proj_dir, 'sample') self.data_dir = os.path.join(self.proj_dir, 'data') self.build_net()
def apply(self, inputs, mask, training): with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE) as scope: # [K, T, 7] tensordot [7, units] = [K, T, units] pointwise = tf.nn.relu(batch_norm(self.dense.apply(inputs), phase_train=training, name=self.name)) #n [K, 1, units] aggregated = tf.reduce_max(pointwise, axis=1, keep_dims=True) # [K, T, units] repeated = tf.tile(aggregated, [1, cfg.VOXEL_POINT_COUNT, 1]) # [K, T, 2 * units] concatenated = tf.concat([pointwise, repeated], axis=2) mask = tf.tile(mask, [1, 1, 2 * self.units]) concatenated = tf.multiply(concatenated, tf.cast(mask, tf.float32)) return concatenated
def ConvMD(M, Cin, Cout, k, s, p, input, training, activation=True, bn=True, name='conv'): temp_p = np.array(p) temp_p = np.lib.pad(temp_p, (1, 1), 'constant', constant_values=(0, 0)) with tf.variable_scope(name) as scope: if (M == 2): paddings = (np.array(temp_p)).repeat(2).reshape(4, 2) pad = tf.pad(input, paddings, "CONSTANT") temp_conv = tf.layers.conv2d( pad, Cout, k, strides=s, padding="valid", reuse=tf.AUTO_REUSE, name=scope, kernel_initializer=tf.keras.initializers.he_normal(seed=None), bias_initializer=tf.zeros_initializer()) if (M == 3): paddings = (np.array(temp_p)).repeat(2).reshape(5, 2) pad = tf.pad(input, paddings, "CONSTANT") temp_conv = tf.layers.conv3d( pad, Cout, k, strides=s, padding="valid", reuse=tf.AUTO_REUSE, name=scope, kernel_initializer=tf.keras.initializers.he_normal(seed=None), bias_initializer=tf.zeros_initializer()) if bn: # temp_conv = tf.layers.batch_normalization( # temp_conv, axis=-1, fused=True, training=training, reuse=tf.AUTO_REUSE, name=scope) temp_conv = batch_norm(temp_conv, phase_train=training, name=name) if activation: return tf.nn.relu(temp_conv) else: return temp_conv
def __init__(self, training, batch_size, name=''): super(FeatureNet, self).__init__() self.training = training self.name = name # scalar self.batch_size = batch_size # [ΣK, 35/45, 7] self.feature = tf.placeholder( tf.float32, [None, cfg.VOXEL_POINT_COUNT, 7], name='feature') # [ΣK] self.number = tf.placeholder(tf.int64, [None], name='number') # [ΣK, 4], each row stores (batch, d, h, w) self.coordinate = tf.placeholder( tf.int64, [None, 4], name='coordinate') with tf.variable_scope(name, reuse=tf.AUTO_REUSE) as scope: self.vfe1 = VFELayer(32, 'VFE-1') self.vfe2 = VFELayer(128, 'VFE-2') # YUN: Add this line to coincide with original paper and official code self.dense = tf.layers.Dense( 128, None, name='dense', _reuse=tf.AUTO_REUSE, _scope=scope, kernel_initializer=tf.keras.initializers.he_normal(seed=None), bias_initializer=tf.zeros_initializer()) # YUN: Add this line to coincide with original paper and official code # self.batch_norm = tf.layers.BatchNormalization( # name='batch_norm', fused=True, _reuse=tf.AUTO_REUSE, _scope=scope) # boolean mask [K, T, 2 * units] mask = tf.not_equal(tf.reduce_max( self.feature, axis=2, keep_dims=True), 0) x = self.vfe1.apply(self.feature, mask, self.training) x = self.vfe2.apply(x, mask, self.training) # YUN: Add this line to coincide with original paper and official code #FCN x = self.dense.apply(x) # YUN: Add this line to coincide with original paper and official code # x = self.batch_norm.apply(x, self.training) x = batch_norm(x, phase_train=self.training, name=name) x = tf.nn.relu(x) # [ΣK, 128] voxelwise = tf.reduce_max(x, axis=1) # car: [N * 10 * 400 * 352 * 128] # pedestrian/cyclist: [N * 10 * 200 * 240 * 128] self.outputs = tf.scatter_nd( self.coordinate, voxelwise, [self.batch_size, 10, cfg.INPUT_HEIGHT, cfg.INPUT_WIDTH, 128])
def Deconv2D(Cin, Cout, k, s, p, input, training, bn=True, name='deconv'): temp_p = np.array(p) temp_p = np.lib.pad(temp_p, (1, 1), 'constant', constant_values=(0, 0)) paddings = (np.array(temp_p)).repeat(2).reshape(4, 2) pad = tf.pad(input, paddings, "CONSTANT") with tf.variable_scope(name) as scope: temp_conv = tf.layers.conv2d_transpose( pad, Cout, k, strides=s, padding="SAME", reuse=tf.AUTO_REUSE, name=scope, kernel_initializer=tf.keras.initializers.he_normal(seed=None), bias_initializer=tf.zeros_initializer()) if bn: # temp_conv = tf.layers.batch_normalization( # temp_conv, axis=-1, fused=True, training=training, reuse=tf.AUTO_REUSE, name=scope) temp_conv = batch_norm(temp_conv, phase_train=training, name=name) return tf.nn.relu(temp_conv)
def conv2d_block(inputs, n_channels, kernel_size=(3, 3), strides=(2, 2), mode='SAME', use_batch_norm=True, activation='relu', is_training=True, data_format='NHWC', conv2d_hparams=None, batch_norm_hparams=None, name='conv2d'): if not isinstance(conv2d_hparams, tf.contrib.training.HParams): raise ValueError( "The paramater `conv2d_hparams` is not of type `HParams`") if not isinstance(batch_norm_hparams, tf.contrib.training.HParams) and use_batch_norm: raise ValueError( "The paramater `conv2d_hparams` is not of type `HParams`") with tf.variable_scope(name): if mode != 'SAME_RESNET': net = layers.conv2d( inputs, n_channels=n_channels, kernel_size=kernel_size, strides=strides, padding=mode, data_format=data_format, use_bias=not use_batch_norm, trainable=is_training, kernel_initializer=conv2d_hparams.kernel_initializer, bias_initializer=conv2d_hparams.bias_initializer, ) else: # Special padding mode for ResNet models if strides == (1, 1): net = layers.conv2d( inputs, n_channels=n_channels, kernel_size=kernel_size, strides=strides, padding='SAME', data_format=data_format, use_bias=not use_batch_norm, trainable=is_training, kernel_initializer=conv2d_hparams.kernel_initializer, bias_initializer=conv2d_hparams.bias_initializer, ) else: rate = 1 # Unused (for 'a trous' convolutions) kernel_height_effective = kernel_size[0] + (kernel_size[0] - 1) * (rate - 1) pad_h_beg = (kernel_height_effective - 1) // 2 pad_h_end = kernel_height_effective - 1 - pad_h_beg kernel_width_effective = kernel_size[1] + (kernel_size[1] - 1) * (rate - 1) pad_w_beg = (kernel_width_effective - 1) // 2 pad_w_end = kernel_width_effective - 1 - pad_w_beg padding = [[0, 0], [pad_h_beg, pad_h_end], [pad_w_beg, pad_w_end], [0, 0]] if data_format == 'NCHW': padding = [padding[0], padding[3], padding[1], padding[2]] padded_inputs = tf.pad(inputs, padding) net = layers.conv2d( padded_inputs, # inputs, n_channels=n_channels, kernel_size=kernel_size, strides=strides, padding='VALID', data_format=data_format, use_bias=not use_batch_norm, trainable=is_training, kernel_initializer=conv2d_hparams.kernel_initializer, bias_initializer=conv2d_hparams.bias_initializer, ) if use_batch_norm: net = layers.batch_norm( net, decay=batch_norm_hparams.decay, epsilon=batch_norm_hparams.epsilon, scale=batch_norm_hparams.scale, center=batch_norm_hparams.center, is_training=is_training, data_format=data_format, param_initializers=batch_norm_hparams.param_initializers) if activation == 'relu': net = layers.relu(net, name='relu') elif activation == 'tanh': net = layers.tanh(net, name='tanh') elif activation != 'linear' and activation is not None: raise KeyError('Invalid activation type: `%s`' % activation) return net
def conv2d_block( inputs, n_channels, kernel_size=(3, 3), strides=(2, 2), mode='SAME', use_batch_norm=True, activation='relu', is_training=True, data_format='NHWC', conv2d_hparams=None, batch_norm_hparams=None, name='conv2d', cardinality=1, ): if not isinstance(conv2d_hparams, tf.contrib.training.HParams): raise ValueError( "The paramater `conv2d_hparams` is not of type `HParams`") if not isinstance(batch_norm_hparams, tf.contrib.training.HParams) and use_batch_norm: raise ValueError( "The paramater `conv2d_hparams` is not of type `HParams`") with tf.variable_scope(name): if cardinality == 1: net = layers.conv2d( inputs, n_channels=n_channels, kernel_size=kernel_size, strides=strides, padding=mode, data_format=data_format, use_bias=not use_batch_norm, trainable=is_training, kernel_initializer=conv2d_hparams.kernel_initializer, bias_initializer=conv2d_hparams.bias_initializer) else: group_filter = tf.get_variable( name=name + 'group_filter', shape=[3, 3, n_channels // cardinality, n_channels], trainable=is_training, dtype=tf.float32) net = tf.nn.conv2d(inputs, group_filter, strides=strides, padding='SAME', data_format=data_format) if use_batch_norm: net = layers.batch_norm( net, decay=batch_norm_hparams.decay, epsilon=batch_norm_hparams.epsilon, scale=batch_norm_hparams.scale, center=batch_norm_hparams.center, is_training=is_training, data_format=data_format, param_initializers=batch_norm_hparams.param_initializers) if activation == 'relu': net = layers.relu(net, name='relu') elif activation == 'tanh': net = layers.tanh(net, name='tanh') elif activation != 'linear' and activation is not None: raise KeyError('Invalid activation type: `%s`' % activation) return net