def _get_latent_code(self, x): x = tf.to_float(x)/255. fc1 = nn.fc(x, 500, nl=tf.nn.softplus, scope='e_fc1') fc2 = nn.fc(fc1, 500, nl=tf.nn.softplus, scope='e_fc2') mu = nn.fc(fc2, self.z_dims, scope='e_fc_mu') log_sigma_sq = nn.fc(fc2, self.z_dims, scope='e_fc_sigma') return mu, log_sigma_sq
def demo(): mymodel = nn.model() mymodel.name = "test" mymodel.add_prop([nn.fc(2, 100)]) mymodel.add_prop([nn.node(100)]) mymodel.add_prop([nn.fc(100, 2)]) mymodel.add_prop([nn.node_out(2)]) mymodel.train(pat_train(), pat_eval(), 10)
def network(self, input, keep_prob=0.5, reuse=None): with tf.variable_scope('network', reuse=reuse): pool_ = lambda x: nn.max_pool(x, 2, 2) max_out_ = lambda x: nn.max_out(x, 16) conv_ = lambda x, output_depth, name, trainable=True: nn.conv( x, 3, output_depth, 1, self.weight_decay, name=name, trainable=trainable) fc_ = lambda x, features, name, relu=True: nn.fc( x, features, self.weight_decay, name, relu=relu) VGG_MEAN = [103.939, 116.779, 123.68] # Convert RGB to BGR and subtract mean # red, green, blue = tf.split(input, 3, axis=3) input = tf.concat([ input - 24, input - 24, input - 24, ], axis=3) conv_1_1 = conv_(input, 64, 'conv1_1', trainable=False) conv_1_2 = conv_(conv_1_1, 64, 'conv1_2', trainable=False) pool_1 = pool_(conv_1_2) conv_2_1 = conv_(pool_1, 128, 'conv2_1', trainable=False) conv_2_2 = conv_(conv_2_1, 128, 'conv2_2', trainable=False) pool_2 = pool_(conv_2_2) conv_3_1 = conv_(pool_2, 256, 'conv3_1') conv_3_2 = conv_(conv_3_1, 256, 'conv3_2') conv_3_3 = conv_(conv_3_2, 256, 'conv3_3') pool_3 = pool_(conv_3_3) conv_4_1 = conv_(pool_3, 512, 'conv4_1') conv_4_2 = conv_(conv_4_1, 512, 'conv4_2') conv_4_3 = conv_(conv_4_2, 512, 'conv4_3') pool_4 = pool_(conv_4_3) conv_5_1 = conv_(pool_4, 512, 'conv5_1') conv_5_2 = conv_(conv_5_1, 512, 'conv5_2') conv_5_3 = conv_(conv_5_2, 512, 'conv5_3') pool_5 = pool_(conv_5_3) if self.maxout: max_5 = max_out_(pool_5) flattened = tf.contrib.layers.flatten(max_5) else: flattened = tf.contrib.layers.flatten(pool_5) fc_6 = nn.dropout(fc_(flattened, 4096, 'fc6'), keep_prob) fc_7 = nn.dropout(fc_(fc_6, 4096, 'fc7'), keep_prob) fc_8 = fc_(fc_7, self.label_dim, 'fc8', relu=False) return fc_8
def siamese(self, input1, input2, keep_prob = 0.5, weight_decay = \ weight_decay_factor, reuse = None): fc_ = lambda x, features, name, relu = True: nn.fc(x, features, weight_decay, name, relu = relu) feat1, _ = self.vgg(input1, keep_prob, True) feat2, _ = self.vgg(input2, keep_prob, True) with tf.variable_scope('network', reuse = reuse): fc_combined = tf.concat((feat1,feat2),1) fc_8 = nn.dropout(fc_(fc_combined, 4096, 'fc8'), keep_prob) fc_9 = fc_(fc_8, 2, 'fc9', relu = False) return fc_9
def rnn_cell(mem, x, name='rnn_cell', reuse=False): with tf.variable_scope(name, reuse=reuse): with tf.variable_scope('thinging_1'): for i in range(0, len(mem)): mem[i] = nn.afc(mem[i], name='afc_' + str(i)) with tf.variable_scope('recall'): for i in range(0, len(mem) - 1): mem[i] = nn.layer_add(nn.afc(mem[i + 1], mem[i].get_shape().as_list()[1:], name='afc_' + str(i)), mem[i], name='layer_add_' + str(i)) with tf.variable_scope('thinging_2'): for i in range(0, len(mem)): mem[i] = nn.afc(mem[i], name='afc_' + str(i)) with tf.variable_scope('in_flow'): x = nn.afc(x, name='afc_1') x = nn.afc(x, name='afc_2') x = nn.afc(x, name='afc_3') mem[0] = nn.layer_add(nn.fc(x, mem[0].get_shape().as_list()[1:]), mem[0]) with tf.variable_scope('out_flow'): x = nn.layer_add(nn.fc(mem[0], O_SHAPE, name='fc_mem'), nn.fc(x, O_SHAPE, name='fc_x')) x = nn.afc(x, name='afc_1') x = nn.afc(x, name='afc_2') x = nn.afc(x, name='afc_3') with tf.variable_scope('thinging_3'): for i in range(0, len(mem)): mem[i] = nn.afc(mem[i], name='afc_' + str(i)) with tf.variable_scope('remember'): for i in range(len(mem) - 1, 0, -1): mem[i] = nn.layer_add(nn.afc(mem[i - 1], mem[i].get_shape().as_list()[1:], name='afc_' + str(i)), mem[i], name='layer_add_' + str(i)) with tf.variable_scope('thinging_4'): for i in range(0, len(mem)): mem[i] = nn.afc(mem[i], name='afc_' + str(i)) return [mem, x]
def _build_model(self, triplets): ''' :param triplets: batches of triplets, [3*N, 64, 64, 3] :return: a model dict containing all Tensors ''' model = {} if self._data_format == "NCHW": images = tf.transpose(triplets, [0, 3, 1, 2]) shape_dict = {} shape_dict['conv1'] = [8, 8, 3, 16] with tf.variable_scope('conv1'): model['conv1'] = nn.conv_layer( self._data_format, triplets, 1, 'VALID', shape_dict['conv1']) # [3N,57,57,16] model['pool1'] = nn.max_pool2d(self._data_format, model['conv1'], 2, 'VALID') # outsize [3N, 28, 28, 16] shape_dict['conv2'] = [5, 5, 16, 7] with tf.variable_scope('conv2'): model['conv2'] = nn.conv_layer(self._data_format, model['pool1'], 1, 'VALID', shape_dict['conv2']) # [3N,24,24,7] model['pool2'] = nn.max_pool2d(self._data_format, model['conv2'], 2, 'SAME') # [3N, 12, 12, 7] shape_dict['fc1'] = 256 with tf.variable_scope('fc1'): model['fc1'] = nn.fc(model['pool2'], shape_dict['fc1']) # [3N, 256] shape_dict['fc2'] = 16 with tf.variable_scope('fc2'): model['fc2'] = nn.fc(model['fc1'], shape_dict['fc2']) # [3N, 16] return model
def _reconstruct(self, z): fc1 = nn.fc(z, 500, nl=tf.nn.softplus, scope='d_fc1') fc2 = nn.fc(fc1, 500, nl=tf.nn.softplus, scope='d_fc2') _x = nn.fc(fc2, self.x_dims, nl=tf.nn.sigmoid, scope='d_fc3') return _x
def __init__( self, config, output_dir="./output", use_rnn=False, testing=False, use_best=False, ): self.config = config self.output_dir = output_dir self.checkpoint_path = os.path.join(self.output_dir, "checkpoint") self.best_ckpt_path = os.path.join(self.output_dir, "best_ckpt") self.weights_path = os.path.join(self.output_dir, "weights") self.log_dir = os.path.join(self.output_dir, "log") self.use_rnn = use_rnn # Placeholder with tf.variable_scope("placeholders") as scope: self.signals = tf.placeholder(dtype=tf.float32, shape=(None, self.config["input_size"], 1, 1), name='signals') self.labels = tf.placeholder(dtype=tf.int32, shape=(None, ), name='labels') self.is_training = tf.placeholder(dtype=tf.bool, shape=(), name='is_training') if self.use_rnn: self.loss_weights = tf.placeholder(dtype=tf.float32, shape=(None, ), name='loss_weights') self.seq_lengths = tf.placeholder(dtype=tf.int32, shape=(None, ), name='seq_lengths') # Monitor global step update self.global_step = tf.Variable(0, trainable=False, name='global_step') # Monitor the number of epochs passed self.global_epoch = tf.Variable(0, trainable=False, name='global_epoch') # Build a network that receives inputs from placeholders net = self.build_cnn() if self.use_rnn: # Check whether the corresponding config is given if "n_rnn_layers" not in self.config: raise Exception("Invalid config.") # Append the RNN if needed net = self.append_rnn(net) # Softmax linear net = nn.fc("softmax_linear", net, self.config["n_classes"], bias=0.0) # Outputs self.logits = net self.preds = tf.argmax(self.logits, axis=1) # Cross-entropy loss self.loss_per_sample = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=self.labels, logits=self.logits, name="loss_ce_per_sample") with tf.name_scope("loss_ce_mean") as scope: if self.use_rnn: # Weight by sequence loss_w_seq = tf.multiply(self.loss_weights, self.loss_per_sample) # Weight by class sample_weights = tf.reduce_sum( tf.multiply( tf.one_hot(indices=self.labels, depth=self.config["n_classes"]), np.asarray(self.config["class_weights"], dtype=np.float32)), 1) loss_w_class = tf.multiply(loss_w_seq, sample_weights) # Computer average loss scaled with the sequence length self.loss_ce = tf.reduce_sum(loss_w_class) / tf.reduce_sum( self.loss_weights) else: self.loss_ce = tf.reduce_mean(self.loss_per_sample) # Regularization loss self.reg_losses = self.regularization_loss() # Total loss self.loss = self.loss_ce + self.reg_losses # Metrics (used when we want to compute a metric from the output from minibatches) with tf.variable_scope("stream_metrics") as scope: self.metric_value_op, self.metric_update_op = contrib_metrics.aggregate_metric_map( { "loss": tf.metrics.mean(values=self.loss), "accuracy": tf.metrics.accuracy(labels=self.labels, predictions=self.preds), "precision": tf.metrics.precision(labels=self.labels, predictions=self.preds), "recall": tf.metrics.recall(labels=self.labels, predictions=self.preds), }) # Manually create reset operations of local vars metric_vars = contrib_slim.get_local_variables(scope=scope.name) self.metric_init_op = tf.variables_initializer(metric_vars) # Training outputs self.train_outputs = { "global_step": self.global_step, "train/loss": self.loss, "train/preds": self.preds, "train/stream_metrics": self.metric_update_op, } if self.use_rnn: self.train_outputs.update({ "train/init_state": self.init_state, "train/final_state": self.final_state, }) # Test outputs self.test_outputs = { "global_step": self.global_step, "test/loss": self.loss, "test/preds": self.preds, } if self.use_rnn: self.test_outputs.update({ "test/init_state": self.init_state, "test/final_state": self.final_state, }) # Tensoflow config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) if not testing: self.train_writer = tf.summary.FileWriter( os.path.join(self.log_dir, "train")) self.train_writer.add_graph(self.sess.graph) logger.info("Saved tensorboard graph to {}".format( self.train_writer.get_logdir())) # Optimizer if not testing: # self.lr = tf.train.exponential_decay( # learning_rate=self.config["learning_rate_decay"], # global_step=self.global_step, # decay_steps=self.config["decay_steps"], # decay_rate=self.config["decay_rate"], # staircase=False, # name="learning_rate" # ) self.lr = tf.constant(self.config["learning_rate"], dtype=tf.float32) with tf.variable_scope("optimizer") as scope: update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): # Pretraining if not self.use_rnn: self.train_step_op, self.grad_op = nn.adam_optimizer( loss=self.loss, training_variables=tf.trainable_variables(), global_step=self.global_step, # learning_rate=self.config["learning_rate"], learning_rate=self.lr, beta1=self.config["adam_beta_1"], beta2=self.config["adam_beta_2"], epsilon=self.config["adam_epsilon"], ) # Fine-tuning else: # Use different learning rates for CNN and RNN self.train_step_op, self.grad_op = nn.adam_optimizer_clip( loss=self.loss, training_variables=tf.trainable_variables(), global_step=self.global_step, # learning_rate=self.config["learning_rate"], learning_rate=self.lr, beta1=self.config["adam_beta_1"], beta2=self.config["adam_beta_2"], epsilon=self.config["adam_epsilon"], clip_value=self.config["clip_grad_value"], ) # Initializer with tf.variable_scope("initializer") as scope: # tf.trainable_variables() or tf.global_variables() self.init_global_op = tf.variables_initializer( tf.global_variables()) self.init_local_op = tf.variables_initializer(tf.local_variables()) # Saver for storing variables self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=1) self.best_saver = tf.train.Saver(tf.global_variables(), max_to_keep=1) # Initialize variables self.run([self.init_global_op, self.init_local_op]) # Restore variables (if possible) is_restore = False if use_best: if os.path.exists(self.best_ckpt_path): if os.path.isfile( os.path.join(self.best_ckpt_path, "checkpoint")): # Restore the last checkpoint latest_checkpoint = tf.train.latest_checkpoint( self.best_ckpt_path) self.saver.restore(self.sess, latest_checkpoint) logger.info("Best model restored from {}".format( latest_checkpoint)) is_restore = True else: if os.path.exists(self.checkpoint_path): if os.path.isfile( os.path.join(self.checkpoint_path, "checkpoint")): # Restore the last checkpoint latest_checkpoint = tf.train.latest_checkpoint( self.checkpoint_path) self.saver.restore(self.sess, latest_checkpoint) logger.info( "Model restored from {}".format(latest_checkpoint)) is_restore = True if not is_restore: logger.info("Model started from random weights")