class LandmarkDataLayer(caffe.Layer): def setup(self, bottom, top): param = eval(self.param_str) self.batch = int(param['batch']) self.img_size = config.IMG_SIZE self.batch_loader = BatchLoader(param, "train") self.ohem_batch_loader = BatchLoader(param, 'ohem') self.train_ratio = 1. self.ohem_ratio = 0 if self.ohem_batch_loader.is_loaded(): self.train_ratio = 7 / 8. self.ohem_ratio = 1. - self.train_ratio top[0].reshape(self.batch, 3, self.img_size, self.img_size) # data top[1].reshape(self.batch, config.LANDMARK_SIZE * 2) # landmark top[2].reshape(self.batch, 1) # landmark def reshape(self, bottom, top): pass def forward(self, bottom, top): batch_data = self.batch_loader.next_batch( self.batch * self.train_ratio, '') batch_data += self.ohem_batch_loader.next_batch( self.batch * self.ohem_ratio, '') for i, datum in enumerate(batch_data): img, pts, eye_dist = datum top[0].data[i, ...] = img top[1].data[i, ...] = pts top[2].data[i, ...] = eye_dist def backward(self, bottom, top): pass
class LandmarkDataLayer(caffe.Layer): def setup(self, bottom, top): param = eval(self.param_str) self.batch = int(param['batch']) self.batch_loader = BatchLoader(param, "train") self.ohem_batch_loader = BatchLoader(param, 'ohem') self.train_ratio = 1. self.ohem_ratio = 0 if self.ohem_batch_loader.is_loaded(): self.train_ratio = 7 / 8. self.ohem_ratio = 1. - self.train_ratio top[0].reshape(self.batch, 3, self.img_size, self.img_size) # data top[1].reshape(self.batch, 72) # landmark def reshape(self, bottom, top): pass def forward(self, bottom, top): batch_data = self.batch_loader.next_batch( self.batch * self.train_ratio, '') batch_data += self.ohem_batch_loader.next_batch( self.batch * self.ohem_ratio, '') random.shuffle(batch_data) for i, datum in enumerate(batch_data): img, label, bbox, landm5 = datum top[0].data[i, ...] = img top[1].data[i, ...] = label top[2].data[i, ...] = bbox if self.net != 'pnet': top[3].data[i, ...] = landm5 def backward(self, bottom, top): pass
class GanNetTrain(caffe.Layer): """Data layer for training""" def setup(self, bottom, top): layer_params = yaml.load(self.param_str) self.batch_size = layer_params['batch_size'] self.image_file = layer_params['image_file'] self.batch_loader = BatchLoader(self.image_file, self.batch_size) def forward(self, bottom, top): # assign output top[0].data[...] = self.images_a top[1].data[...] = self.images_b top[2].data[...] = self.label_true top[3].data[...] = self.label_false def backward(self, top, propagate_down, bottom): """This layer does not propagate gradients.""" pass def reshape(self, bottom, top): images_A, images_B = self.batch_loader.next_batch() images_A = np.array(images_A) images_B = np.array(images_B) images_A = images_A.transpose((0, 3, 1, 2)) images_B = images_B.transpose((0, 3, 1, 2)) self.images_a = images_A self.images_b = images_B self.label_true = np.ones((self.batch_size, 1), dtype='float32') self.label_false = np.zeros((self.batch_size, 1), dtype='float32') top[0].reshape(*self.images_a.shape) top[1].reshape(*self.images_b.shape) top[2].reshape(*self.label_true.shape) top[3].reshape(*self.label_false.shape)
class DataLayer(caffe.Layer): def setup(self, bottom, top): param = eval(self.param_str) self.batch = int(param['batch']) self.net = param['net'] self.img_size = config.NET_IMG_SIZES[self.net] self.batch_loader = BatchLoader(param, "train") self.ohem_batch_loader = BatchLoader(param, 'ohem') self.train_ratio = 1. self.ohem_ratio = 0 if self.ohem_batch_loader.is_loaded(): self.train_ratio = 7 / 8. self.ohem_ratio = 1 / 8. top[0].reshape(self.batch, 3, self.img_size, self.img_size) # data top[1].reshape(self.batch, 1) # label top[2].reshape(self.batch, 4) # bbox if self.net != 'pnet': top[3].reshape(self.batch, config.LANDMARK_SIZE * 2) def reshape(self, bottom, top): pass def forward(self, bottom, top): task = random.choice(config.TRAIN_TASKS[self.net]) batch_data = self.batch_loader.next_batch( self.batch * self.train_ratio, '') batch_data += self.ohem_batch_loader.next_batch( self.batch * self.ohem_ratio, '') random.shuffle(batch_data) for i, datum in enumerate(batch_data): img, label, bbox, landm5 = datum top[0].data[i, ...] = img top[1].data[i, ...] = label top[2].data[i, ...] = bbox if self.net != 'pnet': top[3].data[i, ...] = landm5 def backward(self, bottom, top): pass
class LSTMTDNN(Model): """Time-delayed Nueral Network (cf. http://arxiv.org/abs/1508.06615v4) """ def __init__(self, sess, batch_size=100, rnn_size=650, layer_depth=2, word_embed_dim=650, char_embed_dim=15, feature_maps=[50, 100, 150, 200, 200, 200, 200], kernels=[1, 2, 3, 4, 5, 6, 7], seq_length=35, max_word_length=65, use_word=False, use_char=True, hsm=0, max_grad_norm=5, highway_layers=2, dropout_prob=0.5, use_batch_norm=True, checkpoint_dir="checkpoint", forward_only=False, data_dir="data", dataset_name="pdb", use_progressbar=False): """Initialize the parameters for LSTM TDNN Args: rnn_size: the dimensionality of hidden layers layer_depth: # of depth in LSTM layers batch_size: size of batch per epoch word_embed_dim: the dimensionality of word embeddings char_embed_dim: the dimensionality of character embeddings feature_maps: list of feature maps (for each kernel width) kernels: list of kernel widths seq_length: max length of a word use_word: whether to use word embeddings or not use_char: whether to use character embeddings or not highway_layers: # of highway layers to use dropout_prob: the probability of dropout use_batch_norm: whether to use batch normalization or not hsm: whether to use hierarchical softmax """ self.sess = sess self.batch_size = batch_size self.seq_length = seq_length # RNN self.rnn_size = rnn_size self.layer_depth = layer_depth # CNN self.use_word = use_word self.use_char = use_char self.word_embed_dim = word_embed_dim self.char_embed_dim = char_embed_dim self.feature_maps = feature_maps self.kernels = kernels # General self.highway_layers = highway_layers self.dropout_prob = dropout_prob self.use_batch_norm = use_batch_norm # Training self.max_grad_norm = max_grad_norm self.max_word_length = max_word_length self.hsm = hsm self.data_dir = data_dir self.dataset_name = dataset_name self.checkpoint_dir = checkpoint_dir self.forward_only = forward_only self.use_progressbar = use_progressbar self.loader = BatchLoader(self.data_dir, self.dataset_name, self.batch_size, self.seq_length, self.max_word_length) print('Word vocab size: %d, Char vocab size: %d, Max word length (incl. padding): %d' % \ (len(self.loader.idx2word), len(self.loader.idx2char), self.loader.max_word_length)) self.max_word_length = self.loader.max_word_length self.char_vocab_size = len(self.loader.idx2char) self.word_vocab_size = len(self.loader.idx2word) # build LSTMTDNN model self.prepare_model() # load checkpoints if self.forward_only == True: if self.load(self.checkpoint_dir, self.dataset_name): print(" [*] SUCCESS to load model for %s." % self.dataset_name) else: print(" [!] Failed to load model for %s." % self.dataset_name) sys.exit(1) def prepare_model(self): with tf.variable_scope("LSTMTDNN"): self.char_inputs = [] self.word_inputs = [] self.cnn_outputs = [] if self.use_char: char_W = tf.get_variable( "char_embed", [self.char_vocab_size, self.char_embed_dim]) else: word_W = tf.get_variable( "word_embed", [self.word_vocab_size, self.word_embed_dim]) with tf.variable_scope("CNN") as scope: self.char_inputs = tf.placeholder( tf.int32, [self.batch_size, self.seq_length, self.max_word_length]) self.word_inputs = tf.placeholder( tf.int32, [self.batch_size, self.seq_length]) char_indices = tf.split(1, self.seq_length, self.char_inputs) word_indices = tf.split(1, self.seq_length, tf.expand_dims(self.word_inputs, -1)) for idx in xrange(self.seq_length): char_index = tf.reshape(char_indices[idx], [-1, self.max_word_length]) word_index = tf.reshape(word_indices[idx], [-1, 1]) if idx != 0: scope.reuse_variables() if self.use_char: # [batch_size x word_max_length, char_embed] char_embed = tf.nn.embedding_lookup(char_W, char_index) char_cnn = TDNN(char_embed, self.char_embed_dim, self.feature_maps, self.kernels) if self.use_word: word_embed = tf.nn.embedding_lookup( word_W, word_index) cnn_output = tf.concat(1, char_cnn.output, word_embed) else: cnn_output = char_cnn.output else: cnn_output = tf.squeeze( tf.nn.embedding_lookup(word_W, word_index)) if self.use_batch_norm: bn = batch_norm() norm_output = bn( tf.expand_dims(tf.expand_dims(cnn_output, 1), 1)) cnn_output = tf.squeeze(norm_output) if highway: #cnn_output = highway(input_, input_dim_length, self.highway_layers, 0) cnn_output = highway(cnn_output, cnn_output.get_shape()[1], self.highway_layers, 0) self.cnn_outputs.append(cnn_output) with tf.variable_scope("LSTM") as scope: self.cell = rnn_cell.BasicLSTMCell(self.rnn_size) self.stacked_cell = rnn_cell.MultiRNNCell([self.cell] * self.layer_depth) outputs, _ = rnn.rnn(self.stacked_cell, self.cnn_outputs, dtype=tf.float32) self.lstm_outputs = [] self.true_outputs = tf.placeholder( tf.float32, [self.batch_size, self.seq_length, self.word_vocab_size]) loss = 0 true_outputs = tf.split(1, self.seq_length, self.true_outputs) for idx, (top_h, true_output) in enumerate(zip(outputs, true_outputs)): if self.dropout_prob > 0: top_h = tf.nn.dropout(top_h, self.dropout_prob) if self.hsm > 0: self.lstm_outputs.append(top_h) else: if idx != 0: scope.reuse_variables() proj = rnn_cell.linear(top_h, self.word_vocab_size, 0) log_softmax = tf.log(tf.nn.softmax(proj)) self.lstm_outputs.append(log_softmax) loss += tf.nn.softmax_cross_entropy_with_logits( self.lstm_outputs[idx], tf.squeeze(true_output)) self.loss = tf.reduce_mean(loss) / self.seq_length tf.scalar_summary("loss", self.loss) tf.scalar_summary("perplexity", tf.exp(self.loss)) def train(self, epoch): cost = 0 target = np.zeros( [self.batch_size, self.seq_length, self.word_vocab_size]) N = self.loader.sizes[0] for idx in xrange(N): target.fill(0) x, y, x_char = self.loader.next_batch(0) for b in xrange(self.batch_size): for t, w in enumerate(y[b]): target[b][t][w] = 1 feed_dict = { self.word_inputs: x, self.char_inputs: x_char, self.true_outputs: target, } _, loss, step, summary_str = self.sess.run( [self.optim, self.loss, self.global_step, self.merged_summary], feed_dict=feed_dict) self.writer.add_summary(summary_str, step) if idx % 50 == 0: if self.use_progressbar: progress( idx / N, "epoch: [%2d] [%4d/%4d] loss: %2.6f" % (epoch, idx, N, loss)) else: print("epoch: [%2d] [%4d/%4d] loss: %2.6f" % (epoch, idx, N, loss)) cost += loss return cost / N def test(self, split_idx, max_batches=None): if split_idx == 1: set_name = 'Valid' else: set_name = 'Test' N = self.loader.sizes[split_idx] if max_batches != None: N = min(max_batches, N) self.loader.reset_batch_pointer(split_idx) target = np.zeros( [self.batch_size, self.seq_length, self.word_vocab_size]) cost = 0 for idx in xrange(N): target.fill(0) x, y, x_char = self.loader.next_batch(split_idx) for b in xrange(self.batch_size): for t, w in enumerate(y[b]): target[b][t][w] = 1 feed_dict = { self.word_inputs: x, self.char_inputs: x_char, self.true_outputs: target, } loss = self.sess.run(self.loss, feed_dict=feed_dict) if idx % 50 == 0: if self.use_progressbar: progress( idx / N, "> %s: loss: %2.6f, perplexity: %2.6f" % (set_name, loss, np.exp(loss))) else: print(" > %s: loss: %2.6f, perplexity: %2.6f" % (set_name, loss, np.exp(loss))) cost += loss cost = cost / N return cost def run(self, epoch=25, learning_rate=1, learning_rate_decay=0.5): self.current_lr = learning_rate self.lr = tf.Variable(learning_rate, trainable=False) self.opt = tf.train.GradientDescentOptimizer(self.lr) #self.opt = tf.train.AdamOptimizer(learning_rate, beta1=0.5).minimize(self.loss) # clip gradients params = tf.trainable_variables() grads = [] for grad in tf.gradients(self.loss, params): if grad: grads.append(tf.clip_by_norm(grad, self.max_grad_norm)) else: grads.append(grad) self.global_step = tf.Variable(0, name="global_step", trainable=False) self.optim = self.opt.apply_gradients(zip(grads, params), global_step=self.global_step) # ready for train tf.initialize_all_variables().run() if self.load(self.checkpoint_dir, self.dataset_name): print(" [*] SUCCESS to load model for %s." % self.dataset_name) else: print(" [!] Failed to load model for %s." % self.dataset_name) self.saver = tf.train.Saver() self.merged_summary = tf.merge_all_summaries() self.writer = tf.train.SummaryWriter("./logs", self.sess.graph_def) self.log_loss = [] self.log_perp = [] if not self.forward_only: for idx in xrange(epoch): train_loss = self.train(idx) valid_loss = self.test(1) # Logging self.log_loss.append([train_loss, valid_loss]) self.log_perp.append([np.exp(train_loss), np.exp(valid_loss)]) state = { 'perplexity': np.exp(train_loss), 'epoch': idx, 'learning_rate': self.current_lr, 'valid_perplexity': np.exp(valid_loss) } print(state) # Learning rate annealing if len(self.log_loss) > 1 and self.log_loss[idx][ 1] > self.log_loss[idx - 1][1] * 0.9999: self.current_lr = self.current_lr * learning_rate_decay self.lr.assign(self.current_lr).eval() if self.current_lr < 1e-5: break if idx % 2 == 0: self.save(self.checkpoint_dir, self.dataset_name) test_loss = self.test(2) print(" [*] Test loss: %2.6f, perplexity: %2.6f" % (test_loss, np.exp(test_loss)))
summary_op = tf.summary.merge_all() # In[12]: sess = tf.Session() sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('/tmp/mnist_log', sess.graph) # ## Train # In[14]: saver = tf.train.Saver() step = sess.run(global_step) while step <= 80000: batch_images, batch_labels = train_batch_loader.next_batch() # print batch_images.shape # print batch_labels.shape _, summary_str, train_acc, Center_loss, Softmax_loss = sess.run( [train_op, summary_op, accuracy, center_loss, softmax_loss], feed_dict={ input_images: (batch_images - 127.5) * 0.0078125, # - mean_data, labels: batch_labels, }) step += 1 if step % 100 == 0: print "********* Step %s: ***********" % str(step) print "center loss: %s" % str(Center_loss) print "softmax_loss: %s" % str(Softmax_loss) print "train_acc: %s" % str(train_acc) print "*******************************"
class LSTMTDNN(Model): """ Time-delayed Neural Network (cf. http://arxiv.org/abs/1508.06615v4) """ def __init__(self, sess, batch_size=100, rnn_size=650, layer_depth=2, word_embed_dim=650, char_embed_dim=15, feature_maps=[50, 100, 150, 200, 200, 200, 200], kernels=[1,2,3,4,5,6,7], seq_length=35, max_word_length=65, use_word=False, use_char=True, hsm=0, max_grad_norm=5, highway_layers=2, dropout_prob=0.5, use_batch_norm=True, checkpoint_dir="checkpoint", forward_only=False, data_dir="data", dataset_name="pdb", use_progressbar=False): """ Initialize the parameters for LSTM TDNN Args: rnn_size: the dimensionality of hidden layers layer_depth: # of depth in LSTM layers batch_size: size of batch per epoch word_embed_dim: the dimensionality of word embeddings char_embed_dim: the dimensionality of character embeddings feature_maps: list of feature maps (for each kernel width) kernels: list of kernel widths seq_length: max length of a word use_word: whether to use word embeddings or not use_char: whether to use character embeddings or not highway_layers: # of highway layers to use dropout_prob: the probability of dropout use_batch_norm: whether to use batch normalization or not hsm: whether to use hierarchical softmax """ self.sess = sess self.batch_size = batch_size self.seq_length = seq_length # RNN self.rnn_size = rnn_size self.layer_depth = layer_depth # CNN self.use_word = use_word self.use_char = use_char self.word_embed_dim = word_embed_dim self.char_embed_dim = char_embed_dim self.feature_maps = feature_maps self.kernels = kernels # General self.highway_layers = highway_layers self.dropout_prob = dropout_prob self.use_batch_norm = use_batch_norm # Training self.max_grad_norm = max_grad_norm self.max_word_length = max_word_length self.hsm = hsm self.data_dir = data_dir self.dataset_name = dataset_name self.checkpoint_dir = checkpoint_dir self.forward_only = forward_only self.use_progressbar = use_progressbar self.loader = BatchLoader(self.data_dir, self.dataset_name, self.batch_size, self.seq_length, self.max_word_length) print('Word vocab size: %d, Char vocab size: %d, Max word length (incl. padding): %d' % \ (len(self.loader.idx2word), len(self.loader.idx2char), self.loader.max_word_length)) self.max_word_length = self.loader.max_word_length self.char_vocab_size = len(self.loader.idx2char) self.word_vocab_size = len(self.loader.idx2word) # build LSTMTDNN model self.prepare_model() # load checkpoints if self.forward_only == True: if self.load(self.checkpoint_dir, self.dataset_name): print("[*] SUCCESS to load model for %s." % self.dataset_name) else: print("[!] Failed to load model for %s." % self.dataset_name) sys.exit(1) def prepare_model(self): with tf.variable_scope("LSTMTDNN"): self.char_inputs = [] self.word_inputs = [] self.cnn_outputs = [] if self.use_char: char_W = tf.get_variable("char_embed", [self.char_vocab_size, self.char_embed_dim]) if self.use_word: word_W = tf.get_variable("word_embed", [self.word_vocab_size, self.word_embed_dim]) with tf.variable_scope("CNN") as scope: self.char_inputs = tf.placeholder(tf.int32, [self.batch_size, self.seq_length, self.max_word_length]) self.word_inputs = tf.placeholder(tf.int32, [self.batch_size, self.seq_length]) char_indices = tf.split(1, self.seq_length, self.char_inputs) word_indices = tf.split(1, self.seq_length, tf.expand_dims(self.word_inputs, -1)) for idx in xrange(self.seq_length): char_index = tf.reshape(char_indices[idx], [-1, self.max_word_length]) word_index = tf.reshape(word_indices[idx], [-1, 1]) if idx != 0: scope.reuse_variables() if self.use_char: # [batch_size x word_max_length, char_embed] char_embed = tf.nn.embedding_lookup(char_W, char_index) char_cnn = TDNN(char_embed, self.char_embed_dim, self.feature_maps, self.kernels) if self.use_word: word_embed = tf.nn.embedding_lookup(word_W, word_index) cnn_output = tf.concat(1, [char_cnn.output, tf.squeeze(word_embed, [1])]) else: cnn_output = char_cnn.output else: cnn_output = tf.squeeze(tf.nn.embedding_lookup(word_W, word_index)) if self.use_batch_norm: bn = batch_norm() norm_output = bn(tf.expand_dims(tf.expand_dims(cnn_output, 1), 1)) cnn_output = tf.squeeze(norm_output) if highway: #cnn_output = highway(input_, input_dim_length, self.highway_layers, 0) cnn_output = highway(cnn_output, cnn_output.get_shape()[1], self.highway_layers, 0) self.cnn_outputs.append(cnn_output) with tf.variable_scope("LSTM") as scope: self.cell = tf.nn.rnn_cell.BasicLSTMCell(self.rnn_size) self.stacked_cell = tf.nn.rnn_cell.MultiRNNCell([self.cell] * self.layer_depth) outputs, _ = tf.nn.rnn(self.stacked_cell, self.cnn_outputs, dtype=tf.float32) self.lstm_outputs = [] self.true_outputs = tf.placeholder(tf.int64, [self.batch_size, self.seq_length]) loss = 0 true_outputs = tf.split(1, self.seq_length, self.true_outputs) for idx, (top_h, true_output) in enumerate(zip(outputs, true_outputs)): if self.dropout_prob > 0: top_h = tf.nn.dropout(top_h, self.dropout_prob) if self.hsm > 0: self.lstm_outputs.append(top_h) else: if idx != 0: scope.reuse_variables() proj = tf.nn.rnn_cell._linear(top_h, self.word_vocab_size, 0) self.lstm_outputs.append(proj) loss += tf.nn.sparse_softmax_cross_entropy_with_logits(self.lstm_outputs[idx], tf.squeeze(true_output)) self.loss = tf.reduce_mean(loss) / self.seq_length tf.scalar_summary("loss", self.loss) tf.scalar_summary("perplexity", tf.exp(self.loss)) def train(self, epoch): cost = 0 target = np.zeros([self.batch_size, self.seq_length]) N = self.loader.sizes[0] for idx in xrange(N): target.fill(0) x, y, x_char = self.loader.next_batch(0) for b in xrange(self.batch_size): for t, w in enumerate(y[b]): target[b][t] = w feed_dict = { self.word_inputs: x, self.char_inputs: x_char, self.true_outputs: target, } _, loss, step, summary_str = self.sess.run( [self.optim, self.loss, self.global_step, self.merged_summary], feed_dict=feed_dict) self.writer.add_summary(summary_str, step) if idx % 50 == 0: if self.use_progressbar: progress(idx/N, "epoch: [%2d] [%4d/%4d] loss: %2.6f" % (epoch, idx, N, loss)) else: print("epoch: [%2d] [%4d/%4d] loss: %2.6f" % (epoch, idx, N, loss)) cost += loss return cost / N def test(self, split_idx, max_batches=None): if split_idx == 1: set_name = 'Valid' else: set_name = 'Test' N = self.loader.sizes[split_idx] if max_batches != None: N = min(max_batches, N) self.loader.reset_batch_pointer(split_idx) target = np.zeros([self.batch_size, self.seq_length]) cost = 0 for idx in xrange(N): target.fill(0) x, y, x_char = self.loader.next_batch(split_idx) for b in xrange(self.batch_size): for t, w in enumerate(y[b]): target[b][t] = w feed_dict = { self.word_inputs: x, self.char_inputs: x_char, self.true_outputs: target, } loss = self.sess.run(self.loss, feed_dict=feed_dict) if idx % 50 == 0: if self.use_progressbar: progress(idx/N, "> %s: loss: %2.6f, perplexity: %2.6f" % (set_name, loss, np.exp(loss))) else: print(" > %s: loss: %2.6f, perplexity: %2.6f" % (set_name, loss, np.exp(loss))) cost += loss cost = cost / N return cost def run(self, epoch=25, learning_rate=1, learning_rate_decay=0.5): self.current_lr = learning_rate self.lr = tf.Variable(learning_rate, trainable=False) self.opt = tf.train.GradientDescentOptimizer(self.lr) #self.opt = tf.train.AdamOptimizer(learning_rate, beta1=0.5).minimize(self.loss) # clip gradients params = tf.trainable_variables() grads = [] for grad in tf.gradients(self.loss, params): if grad is not None: grads.append(tf.clip_by_norm(grad, self.max_grad_norm)) else: grads.append(grad) self.global_step = tf.Variable(0, name="global_step", trainable=False) self.optim = self.opt.apply_gradients(zip(grads, params), global_step=self.global_step) # ready for train tf.initialize_all_variables().run() if self.load(self.checkpoint_dir, self.dataset_name): print("[*] SUCCESS to load model for %s." % self.dataset_name) else: print("[!] Failed to load model for %s." % self.dataset_name) self.saver = tf.train.Saver() self.merged_summary = tf.merge_all_summaries() self.writer = tf.train.SummaryWriter("./logs", self.sess.graph_def) self.log_loss = [] self.log_perp = [] if not self.forward_only: for idx in xrange(epoch): train_loss = self.train(idx) valid_loss = self.test(1) # Logging self.log_loss.append([train_loss, valid_loss]) self.log_perp.append([np.exp(train_loss), np.exp(valid_loss)]) state = { 'perplexity': np.exp(train_loss), 'epoch': idx, 'learning_rate': self.current_lr, 'valid_perplexity': np.exp(valid_loss) } print(state) # Learning rate annealing if len(self.log_loss) > 1 and self.log_loss[idx][1] > self.log_loss[idx-1][1] * 0.9999: self.current_lr = self.current_lr * learning_rate_decay self.lr.assign(self.current_lr).eval() if self.current_lr < 1e-5: break if idx % 2 == 0: self.save(self.checkpoint_dir, self.dataset_name) test_loss = self.test(2) print("[*] Test loss: %2.6f, perplexity: %2.6f" % (test_loss, np.exp(test_loss)))
def main(): LAMBDA = 0.0 num_class = 526 checkpoint_dir = "../model/" with tf.name_scope('input'): input_images = tf.placeholder(tf.float32, shape=(None, 100, 100, 3), name='input_images') labels = tf.placeholder(tf.int64, shape=(None), name='labels') logits, features, total_loss, accuracy, centers_update_op, center_loss, softmax_loss = build_network( input_images, labels, num_class, ratio=LAMBDA) global_step = tf.Variable(0, trainable=False, name='global_step') train_batch_loader = BatchLoader("../data/facescrub_train.list", 16) test_batch_loader = BatchLoader("../data/facescrub_val.list", 16) optimizer = tf.train.AdamOptimizer(0.001) with tf.control_dependencies([centers_update_op]): train_op = optimizer.minimize(total_loss, global_step=global_step) summary_op = tf.summary.merge_all() with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('../tmp/face_log', sess.graph) saver = tf.train.Saver() step = sess.run(global_step) while step <= 80000: batch_images, batch_labels = train_batch_loader.next_batch() # print batch_images.shape # print batch_labels.shape _, summary_str, train_acc, Center_loss, Softmax_loss = sess.run( [train_op, summary_op, accuracy, center_loss, softmax_loss], feed_dict={ input_images: (batch_images - 127.5) * 0.0078125, # - mean_data, labels: batch_labels, }) step += 1 print("step", step) if step % 100 == 0: print("********* Step %s: ***********" % str(step)) print("center loss: %s" % str(Center_loss)) print("softmax_loss: %s" % str(Softmax_loss)) print("train_acc: %s" % str(train_acc)) print("*******************************") if step % 10000 == 0: saver.save(sess, checkpoint_dir + 'model.ckpt', global_step=step) writer.add_summary(summary_str, global_step=step) if step % 2000 == 0: batch_images, batch_labels = test_batch_loader.next_batch() vali_image = (batch_images - 127.5) * 0.0078125 vali_acc = sess.run(accuracy, feed_dict={ input_images: vali_image, labels: batch_labels }) print(("step: {}, train_acc:{:.4f}, vali_acc:{:.4f}".format( step, train_acc, vali_acc))) sess.close()
def main(): params = args() model_prefix = params.model_prefix load_epoch = params.load_epoch batch_size = params.batch_size img1_path = params.image1 img2_path = params.image2 img_shape = params.image_shape img_dir = params.img_dir txt_path = params.file_txt # in out files file_ = open(txt_path, 'r') lines_ = file_.readlines() result_ = open("result.txt", 'w') # if Test_Img == 'True': img1 = cv2.imread(img1_path) img2 = cv2.imread(img2_path) img1 = cv2.resize(img1, (img_shape, img_shape)) img2 = cv2.resize(img2, (img_shape, img_shape)) img1 = np.expand_dims(img1, 0) img2 = np.expand_dims(img2, 0) test_batch_loader = BatchLoader("../data/facescrub_val.list", batch_size, img_shape) tf.reset_default_graph() with tf.name_scope('input'): input_images = tf.placeholder(tf.float32, shape=(batch_size, img_shape, img_shape, 3), name='input_images') labels = tf.placeholder(tf.int32, shape=(batch_size), name='labels') features, accuracy, pred_class, res1 = build_network( input_images, labels, 526, 'test') check_ckpt(model_prefix + '-' + str(load_epoch)) #detect detector = face_dect() # with tf.Session() as sess: restore_model = tf.train.Saver() #restore_model = tf.train.import_meta_graph(model_prefix+'-'+str(load_epoch) +'.meta') restore_model.restore(sess, model_prefix + '-' + str(load_epoch)) print("face model restore over") ''' all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) key_list = [] var_dic = dict() for v_name in tf.global_variables(): i=0 print("name : ",v_name.name[:-2],v_name.shape) print("shape",all_vars[i]) key_list.append(v_name.name[:-2]) i+=1 #print(tf.get_variable_scope()) #all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) vas= sess.run([all_vars]) print(len(vas)) for i in range(len(vas)): cur_name = key_list[i] cur_var = vas[i] print("name ,shape : ",cur_name,np.shape(cur_var)) var_dic[cur_name] = cur_var ''' #restore_model = tf.train.import_meta_graph(model_prefix+'-'+str(load_epoch)+'.meta') #restore_model.restore(sess,model_prefix+'-'+str(load_epoch)) if Test_Img == 'False': iter_num = 0 accuracy_sum = 0 while iter_num < test_batch_loader.batch_num: batch_images, batch_labels = test_batch_loader.next_batch() images_in = (batch_images - 127.5) * 0.0078125 feat, batch_accuracy = sess.run([features, accuracy], feed_dict={ input_images: images_in, labels: batch_labels }) accuracy_sum += batch_accuracy iter_num += 1 if iter_num % 10 == 0: print("step ", iter_num, batch_accuracy) print("image num: ", test_batch_loader.data_num, "the test accuracy: ", accuracy_sum / (test_batch_loader.batch_num)) elif Test_Img == 'True': with tf.name_scope('valdata'): label_t = np.zeros([1]) feat1 = sess.run([features], feed_dict={ input_images: img1, labels: label_t }) feat2 = sess.run([features], feed_dict={ input_images: img2, labels: label_t }) distance = L2_distance(feat1, feat2, 512) print("the 2 image dis: ", distance) for rot, fdir, fname in os.walk(img_dir): if len(fname) != 0: break img_list = [] print(fname) for i in range(len(fname)): org_img = cv2.imread(os.path.join(rot, fname[i])) img_org = cv2.resize(org_img, (img_shape, img_shape)) img_org = np.expand_dims(img_org, 0) img_list.append(img_org) for i in range(len(fname)): img1 = img_list[i] feat1 = sess.run([features], feed_dict={ input_images: img1, labels: label_t }) j = i + 1 while j < len(fname): img2 = img_list[j] t1 = time.time() feat2 = sess.run([features], feed_dict={ input_images: img2, labels: label_t }) t2 = time.time() print("one image time ", t2 - t1) distance = L2_distance(feat1, feat2, 512) t3 = time.time() print("compere time ", t3 - t2) print(i, j, distance) j += 1 elif Test_Img == 'File': label_t = np.ones([1]) for i in range(len(lines_)): feat_vec = [] feat1_fg = 0 feat2_fg = 0 line_1 = lines_[i] line_1 = string.strip(line_1) line_s = line_1.split(',') dir_path_save = line_s[0][:-4] dir_path_save = "../cropface/" + dir_path_save mkdir_(dir_path_save) for j in range(len(line_s)): feat_vec2 = [] if j == 0: #print("line ",line_s) img1_pic = line_s[0] img1_path = os.path.join(img_dir, img1_pic) img1 = cv2.imread(img1_path) bboxes_1 = detector.get_face(img1) if bboxes_1 is not None: for k in range(bboxes_1.shape[0]): crop_img1 = get_img(img1, bboxes_1[k], img_shape) if k == 0 and SV_IMG: img_save_path = dir_path_save + '/' + line_s[ 0][:-4] + ".jpg" save_image(img_save_path, crop_img1) crop_img1 = (crop_img1 - 127.5) * 0.0078125 crop_img1 = np.expand_dims(crop_img1, 0) feat1 = sess.run([features], feed_dict={ input_images: crop_img1, labels: label_t }) print("a feature shape ", np.shape(feat1)) feat_vec.append(feat1) feat_fg = 1 else: print("a no face detect ") break else: img2_pic = line_s[j] img2_path = os.path.join(img_dir, img2_pic) img2 = cv2.imread(img2_path) bboxes_2 = detector.get_face(img2) if bboxes_2 is not None: for k in range(bboxes_2.shape[0]): crop_img2 = get_img(img2, bboxes_2[k], img_shape) if SV_IMG: img_save_path = dir_path_save + '/' + line_s[ j][:-4] + "-" + str(k) + ".jpg" save_image(img_save_path, crop_img2) crop_img2 = (crop_img2 - 127.5) * 0.0078125 crop_img2 = np.expand_dims(crop_img2, 0) feat2 = sess.run([features], feed_dict={ input_images: crop_img2, labels: label_t }) print("b feature shape ", np.shape(feat2)) feat_vec2.append(feat2) feat2_fg = 1 else: print("b no face detect ") continue if j > 0: t2 = time.time() distance = L2_distance(feat_vec[0], feat_vec2[0], 512) print("distance is ", distance) t3 = time.time() print("compere time ", t3 - t2) result_.write("{} ".format(distance)) result_.write("\n") print(feat2) file_.close() result_.close()
def main(): LAMBDA = 1e-8 center_alpha = 0.9 num_class = 10000 embd_size = 512 args = argument() checkpoint_dir = args.save_model_name lr = args.lr batch_size = args.batch_size epoch_num = args.epoch_num sta = args.sta img_shape = args.img_size train_file = args.train_file val_file = args.val_file train_batch_loader = BatchLoader(train_file, batch_size, img_shape) test_batch_loader = BatchLoader(val_file, batch_size, img_shape) #(Height,Width) = (train_batch_loader.height,train_batch_loader.width) #train_batch_loader = mnist_data(batch_size) print("img shape", img_shape) with tf.name_scope('input'): input_images = tf.placeholder(tf.float32, shape=(batch_size, img_shape[0], img_shape[1], 3), name='input_images') labels = tf.placeholder(tf.int32, shape=(batch_size), name='labels') learn_rate = tf.placeholder(tf.float32, shape=(None), name='learn_rate') with tf.name_scope('var'): global_step = tf.Variable(0, trainable=False, name='global_step') loss_op = CenterLoss(center_alpha, num_class, embd_size) #with tf.device('/gpu:0'): total_loss, accuracy, centers_update_op, center_loss, softmax_loss, pred_class, res1 = build_network( input_images, labels, num_class, sta, loss_op, ratio=LAMBDA) optimizer = tf.train.AdamOptimizer(learn_rate) #optimizer = tf.train.GradientDescentOptimizer(learn_rate) with tf.control_dependencies([centers_update_op]): train_op = optimizer.minimize(total_loss, global_step=global_step) #train_op = optimizer.minimize(total_loss, global_step=global_step) summary_op = tf.summary.merge_all() with tf.Session(config=tf.ConfigProto(log_device_placement=False)) as sess: #sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('../tmp/face_log', sess.graph) saver = tf.train.Saver() if args.pretrained is not None: model_path = args.save_model_name + '-' + str(args.pretrained) #saver.restore(sess,'./face_model/high_score-60') saver.restore(sess, model_path) else: init = tf.global_variables_initializer() sess.run(init) step = sess.run(global_step) epoch_idx = 0 graph_step = 0 while epoch_idx <= epoch_num: step = 0 ckpt_fg = 'True' ps_loss = 0.0 pc_loss = 0.0 acc_sum = 0.0 while step < train_batch_loader.batch_num: batch_images, batch_labels = train_batch_loader.next_batch() #batch_images, batch_labels = train_batch_loader.get_batchdata() in_imgs = (batch_images - 127.5) * 0.0078125 #print("data in ",in_img[0,:2,:2,0]) _, summary_str, train_acc, Center_loss, Softmax_loss, Pred_class, res1_o = sess.run( [ train_op, summary_op, accuracy, center_loss, softmax_loss, pred_class, res1 ], feed_dict={ input_images: in_imgs, labels: batch_labels, learn_rate: lr }) step += 1 #print("step",step, str(Softmax_loss),str(Center_loss)) #print("step label",step, str(batch_labels)) graph_step += 1 if step % 100 == 0: writer.add_summary(summary_str, global_step=graph_step) pc_loss += Center_loss ps_loss += Softmax_loss acc_sum += train_acc if step % 1000 == 0: #lr = lr*0.1 #c_loss+=c_loss #s_loss+=s_loss print("****** Epoch {} Step {}: ***********".format( str(epoch_idx), str(step))) print("center loss: {}".format(pc_loss / 1000.0)) print("softmax_loss: {}".format(ps_loss / 1000.0)) print("train_acc: {}".format(acc_sum / 1000.0)) print("centers", res1_o[0, :5]) print("*******************************") #if (acc_sum/100.0) >= 0.98 and (pc_loss/100.0)<40 and (ps_loss/100.0) <0.1 and ckpt_fg=='True': if ckpt_fg == 'True': print( "******************************************************************************" ) saver.save(sess, checkpoint_dir, global_step=epoch_idx) ckpt_fg = 'False' ps_loss = 0.0 pc_loss = 0.0 acc_sum = 0.0 epoch_idx += 1 if epoch_idx % 10 == 0: print( "******************************************************************************" ) saver.save(sess, checkpoint_dir, global_step=epoch_idx) #writer.add_summary(summary_str, global_step=step) if epoch_idx % 5 == 0: lr = lr * 0.5 if epoch_idx: batch_images, batch_labels = test_batch_loader.next_batch() #batch_images,batch_labels = train_batch_loader.get_valdata() vali_image = (batch_images - 127.5) * 0.0078125 vali_acc = sess.run(accuracy, feed_dict={ input_images: vali_image, labels: batch_labels }) print(("epoch: {}, train_acc:{:.4f}, vali_acc:{:.4f}".format( epoch_idx, train_acc, vali_acc))) sess.close()
from batch_loader import BatchLoader import config import random if __name__ == '__main__': param = {"net": "pnet", "batch": 64} batch_loader = BatchLoader(param) task = random.choice(config.TRAIN_TASKS[param['net']]) data = batch_loader.next_batch(64, task) for datum in data: print(datum)