def forward_network(self, input_img, reuse=False): with tf.compat.v1.variable_scope(self.name, reuse=reuse): tf_utils.print_activations(input_img, logger=None) inputs = self.conv2d_fixed_padding(inputs=input_img, filters=64, kernel_size=7, strides=2, name='conv1') inputs = tf_utils.max_pool(inputs, name='3x3_maxpool', ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], logger=None) inputs = self.block_layer(inputs=inputs, filters=64, block_fn=self.bottleneck_block, blocks=self.layers[0], strides=1, train_mode=False, name='block_layer1') inputs = self.block_layer(inputs=inputs, filters=128, block_fn=self.bottleneck_block, blocks=self.layers[1], strides=2, train_mode=False, name='block_layer2') inputs = self.block_layer(inputs=inputs, filters=256, block_fn=self.bottleneck_block, blocks=self.layers[2], strides=2, train_mode=False, name='block_layer3') inputs = self.block_layer(inputs=inputs, filters=512, block_fn=self.bottleneck_block, blocks=self.layers[3], strides=2, train_mode=False, name='block_layer4') inputs = tf_utils.relu(inputs, name='before_flatten_relu', logger=None) # _, h, w, _ = inputs.get_shape().as_list() # inputs = tf_utils.avg_pool(inputs, name='gap', ksize=[1, h, w, 1], strides=[1, 1, 1, 1], logger=self.logger) # Flatten & FC1 inputs = tf_utils.flatten(inputs, name='flatten', logger=None) inputs = tf_utils.linear(inputs, 512, name='FC1') inputs = tf_utils.relu(inputs, name='FC1_relu', logger=None) inputs = tf_utils.linear(inputs, 256, name='FC2') inputs = tf_utils.relu(inputs, name='FC2_relu', logger=None) logits = tf_utils.linear(inputs, self.num_attribute, name='Out') return logits
def __init__(self, input_dim=[32, 32, 3], output_dim=[128, 256, 512, 1000, 10], optimizer=None, use_dropout=True, lr=0.001, weight_decay=1e-4, random_seed=123, is_train=True, log_dir=None, name=None): self.name = name self.is_train = is_train self.log_dir = log_dir self.cur_lr = None self.logger, self.file_handler, self.stream_handler = utils.init_logger(log_dir=self.log_dir, name=self.name, is_train=self.is_train) with tf.variable_scope(self.name): # Placeholders for inputs self.X = tf.placeholder(dtype=tf.float32, shape=[None, *input_dim], name='X') self.y = tf.placeholder(dtype=tf.float32, shape=[None, output_dim[-1]], name='y') self.y_cls = tf.math.argmax(input=self.y, axis=1) self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') tf_utils.print_activations(self.X, logger=self.logger if self.is_train else None) # Placeholders for TensorBoard self.train_acc = tf.placeholder(tf.float32, name='train_acc') self.val_acc = tf.placeholder(tf.float32, name='val_acc') # Convolutional layers net = self.X if use_dropout: net = tf_utils.dropout(x=net, keep_prob=self.keep_prob, seed=random_seed, name='dropout_input', logger=self.logger if self.is_train else None) for idx in range(3): net = tf_utils.conv2d(x=net, output_dim=output_dim[idx], k_h=5, k_w=5, d_h=1, d_w=1, name='conv2d'+str(idx), logger=self.logger if self.is_train else None) net = tf_utils.max_pool(x=net, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], name='maxpool'+str(idx), logger=self.logger if self.is_train else None) net = tf_utils.relu(x=net, name='relu'+str(idx), is_print=True, logger=self.logger if self.is_train else None) # Fully conneted layers, flatten first net = tf_utils.flatten(x=net, name='fc2_flatten', logger=self.logger if self.is_train else None) net = tf_utils.linear(x=net, output_size=output_dim[-2], name='fc3', logger=self.logger if self.is_train else None) if use_dropout: net = tf_utils.dropout(x=net, keep_prob=self.keep_prob, seed=random_seed, name='dropout3', logger=self.logger if self.is_train else None) net = tf_utils.relu(x=net, name='relu3', logger=self.logger if self.is_train else None) # Last predict layer self.y_pred = tf_utils.linear(x=net, output_size=output_dim[-1], name='last_fc', logger=self.logger if self.is_train else None) # Loss = data loss + regularization term self.data_loss = tf.math.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=self.y_pred, labels=self.y)) self.reg_term = weight_decay * tf.reduce_sum( [tf.nn.l2_loss(weight) for weight in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)]) self.loss = self.data_loss + self.reg_term # Optimizer self.train_op, self.cur_lr = optimizer_fn(optimizer, lr=lr, loss=self.loss, name=self.name) # Accuracy etc self.y_pred_cls = tf.math.argmax(input=self.y_pred, axis=1) correct_prediction = tf.math.equal(self.y_pred_cls, self.y_cls) self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, dtype=tf.float32)) * 100. self._tensorboard() tf_utils.show_all_variables(logger=self.logger if self.is_train else None)
def __call__(self, x, is_train=True): with tf.variable_scope(self.name, reuse=self.reuse): tf_utils.print_activations(x) # (N, 120, 160, 1) -> (N, 60, 80, 64) h0_conv = tf_utils.conv2d( x, output_dim=self.dims[0], initializer='he', name='h0_conv', logger=self.logger if is_train is True else None) h0_lrelu = tf_utils.lrelu( h0_conv, name='h0_lrelu', logger=self.logger if is_train is True else None) # (N, 60, 80, 64) -> (N, 30, 40, 128) h1_conv = tf_utils.conv2d( h0_lrelu, output_dim=self.dims[1], initializer='he', name='h1_conv', logger=self.logger if is_train is True else None) h1_norm = tf_utils.norm( h1_conv, name='h1_batch', _type='batch', _ops=self._ops, is_train=is_train, logger=self.logger if is_train is True else None) h1_lrelu = tf_utils.lrelu( h1_norm, name='h1_lrelu', logger=self.logger if is_train is True else None) # (N, 30, 40, 128) -> (N, 15, 20, 256) h2_conv = tf_utils.conv2d( h1_lrelu, output_dim=self.dims[2], initializer='he', name='h2_conv', logger=self.logger if is_train is True else None) h2_norm = tf_utils.norm( h2_conv, name='h2_batch', _type='batch', _ops=self._ops, is_train=is_train, logger=self.logger if is_train is True else None) h2_lrelu = tf_utils.lrelu( h2_norm, name='h2_lrelu', logger=self.logger if is_train is True else None) # (N, 15, 20, 256) -> (N, 8, 10, 512) h3_conv = tf_utils.conv2d( h2_lrelu, output_dim=self.dims[3], initializer='he', name='h3_conv', logger=self.logger if is_train is True else None) h3_norm = tf_utils.norm( h3_conv, name='h3_batch', _type='batch', _ops=self._ops, is_train=is_train, logger=self.logger if is_train is True else None) h3_lrelu = tf_utils.lrelu( h3_norm, name='h3_lrelu', logger=self.logger if is_train is True else None) # (N, 8, 10, 512) -> (N, 4, 5, 1024) h4_conv = tf_utils.conv2d( h3_lrelu, output_dim=self.dims[4], initializer='he', name='h4_conv', logger=self.logger if is_train is True else None) h4_norm = tf_utils.norm( h4_conv, name='h4_batch', _type='batch', _ops=self._ops, is_train=is_train, logger=self.logger if is_train is True else None) h4_lrelu = tf_utils.lrelu( h4_norm, name='h4_lrelu', logger=self.logger if is_train is True else None) # (N, 4, 5, 1024) -> (N, 4*5*1024) h4_flatten = tf_utils.flatten( h4_lrelu, name='h4_flatten', logger=self.logger if is_train is True else None) # (N, 4*5*1024) -> (N, 1) output = tf_utils.linear( h4_flatten, output_size=self.dims[5], initializer='he', name='output', logger=self.logger if is_train is True else None) # Set reuse=True for next call self.reuse = True self.variables = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def forward_network(self, input_img, padding='SAME', reuse=False): with tf.compat.v1.variable_scope(self.name, reuse=reuse): tf_utils.print_activations(input_img, logger=self.logger) inputs = self.conv2d_fixed_padding(inputs=input_img, filters=64, kernel_size=7, strides=2, name='conv1') inputs = tf_utils.max_pool(inputs, name='3x3_maxpool', ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], logger=self.logger) inputs = self.block_layer(inputs=inputs, filters=64, block_fn=self.bottleneck_block, blocks=self.layers[0], strides=1, train_mode=self.train_mode, name='block_layer1') inputs = self.block_layer(inputs=inputs, filters=128, block_fn=self.bottleneck_block, blocks=self.layers[1], strides=2, train_mode=self.train_mode, name='block_layer2') inputs = self.block_layer(inputs=inputs, filters=256, block_fn=self.bottleneck_block, blocks=self.layers[2], strides=2, train_mode=self.train_mode, name='block_layer3') inputs = self.block_layer(inputs=inputs, filters=512, block_fn=self.bottleneck_block, blocks=self.layers[3], strides=2, train_mode=self.train_mode, name='block_layer4') inputs = tf_utils.norm(inputs, name='before_gap_batch_norm', _type='batch', _ops=self._ops, is_train=self.train_mode, logger=self.logger) inputs = tf_utils.relu(inputs, name='before_gap_relu', logger=self.logger) _, h, w, _ = inputs.get_shape().as_list() inputs = tf_utils.avg_pool(inputs, name='gap', ksize=[1, h, w, 1], strides=[1, 1, 1, 1], logger=self.logger) inputs = tf_utils.flatten(inputs, name='flatten', logger=self.logger) logits = tf_utils.linear(inputs, self.num_classes, name='logits') return logits