def ndlstm_base_dynamic(inputs, noutput, sequence_length, scope=None, reverse=False): with tf.variable_scope(scope, "SeqLstm", [inputs]): # TODO(tmb) make batch size, sequence_length dynamic # example: sequence_length = tf.shape(inputs)[0] _, batch_size, _ = tf.unstack(tf.shape(inputs)) #_shape(inputs) lstm_cell = tf.contrib.rnn.BasicLSTMCell(noutput, state_is_tuple=True) state = lstm_cell.zero_state(batch_size, dtype=tf.float32) #state = array_ops.zeros([batch_size, lstm_cell.state_size]) #sequence_length = int(inputs.get_shape()[0]) sequence_lengths = tf.to_int64(tf.fill([batch_size], sequence_length)) if reverse: inputs = tf.reverse_v2(inputs, [0]) outputs, _ = rnn.dynamic_rnn(lstm_cell, inputs, sequence_lengths, state, time_major=True) #print(outputs) if reverse: outputs = tf.reverse_v2(outputs, [0]) return outputs
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with tf.variable_scope(scope, "SeqLstm", [inputs]): # TODO(tmb) make batch size, sequence_length dynamic # example: sequence_length = tf.shape(inputs)[0] _, batch_size, _ = _shape(inputs) lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(noutput, state_is_tuple=False) state = tf.zeros([batch_size, lstm_cell.state_size]) sequence_length = int(inputs.get_shape()[0]) sequence_lengths = tf.to_int64(tf.fill([batch_size], sequence_length)) if reverse: inputs = tf.reverse_v2(inputs, [0]) outputs, _ = tf.nn.dynamic_rnn(lstm_cell, inputs, sequence_lengths, state, time_major=True) if reverse: outputs = tf.reverse_v2(outputs, [0]) return outputs
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with tf.variable_scope(scope, "SeqLstm", [inputs]): # TODO(tmb) make batch size, sequence_length dynamic # example: sequence_length = tf.shape(inputs)[0] _, batch_size, _ = _shape(inputs) lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(noutput, state_is_tuple=False) state = tf.zeros([batch_size, lstm_cell.state_size]) sequence_length = int(inputs.get_shape()[0]) sequence_lengths = tf.to_int64(tf.fill([batch_size], sequence_length)) if reverse: inputs = tf.reverse_v2(inputs, [0]) outputs, _ = tf.nn.dynamic_rnn(lstm_cell, inputs, sequence_lengths, state, time_major=True) if reverse: outputs = tf.reverse_v2(outputs, [0]) return outputs
def predict_labels_multi_scale(images, model_options, eval_scales=(1.0, ), add_flipped_images=False): outputs_to_predictions = { output: [] for output in model_options.outputs_to_num_classes } for i, image_scale in enumerate(eval_scales): with tf.variable_scope(tf.get_variable_scope(), reuse=True if i else None): outputs_to_scales_to_logits = multi_scale_logits( images, model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) if add_flipped_images: with tf.variable_scope(tf.get_variable_scope(), reuse=True): outputs_to_scales_to_logits_reversed = multi_scale_logits( tf.reverse_v2(images, [2]), model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) for output in sorted(outputs_to_scales_to_logits): scales_to_logits = outputs_to_scales_to_logits[output] logits = tf.image.resize_bilinear( scales_to_logits[MERGED_LOGITS_SCOPE], tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits), 4)) if add_flipped_images: scales_to_logits_reversed = ( outputs_to_scales_to_logits_reversed[output]) logits_reversed = tf.image.resize_bilinear( tf.reverse_v2( scales_to_logits_reversed[MERGED_LOGITS_SCOPE], [2]), tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits_reversed), 4)) for output in sorted(outputs_to_predictions): predictions = outputs_to_predictions[output] # Compute average prediction across different scales and flipped images. predictions = tf.reduce_mean(tf.concat(predictions, 4), axis=4) outputs_to_predictions[output] = tf.argmax(predictions, 3) return outputs_to_predictions
def get_batch(dataset_dir, num_readers=2, batch_size=3, net=None, FLAGS=None, file_pattern='*.tfrecord', is_training=True, shuffle=False): filename = glob.glob(os.path.join(dataset_dir, "*train*" + file_pattern))[0] print(filename) filename_queue = tf.train.string_input_producer([filename], num_epochs=50) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'image/encoded': tf.FixedLenFeature([], tf.string, default_value=''), 'label/encoded': tf.FixedLenFeature([], tf.string, default_value=''), }) # return image and label with tf.name_scope('cmc/label'): label = tf.decode_raw(features['label/encoded'], tf.float32) label = tf.transpose(tf.reshape(label, [3, 1, 240, 240]), (0, 2, 3, 1)) with tf.name_scope('cmc/image'): image = tf.decode_raw(features['image/encoded'], tf.float32) image = tf.transpose(tf.reshape(image, [3, 4, 240, 240]), (0, 2, 3, 1)) #image = tf.reshape(image, [32, 100, 3]) image = tf.cast(image, tf.float32) label = tf.cast(label, tf.int32) print("image", image.get_shape(), label.get_shape()) """ if (np.random.choice([0, 1]) == 1): image = tf.image.flip_left_right(image) label = tf.image.flip_left_right(label) """ do_a_crop_flip = tf.random_uniform([], seed=None) do_a_crop_flip = tf.greater(do_a_crop_flip, 0.5) image = tf.cond(do_a_crop_flip, lambda: tf.reverse_v2(image, [2]), lambda: image) label = tf.cond(do_a_crop_flip, lambda: tf.reverse_v2(label, [2]), lambda: label) sh_images, sh_labels = tf.train.shuffle_batch([image, label], batch_size=batch_size, num_threads=1, capacity=100 * batch_size, min_after_dequeue=batch_size) return sh_images, sh_labels
def _rotate_pp(data): data["label"] = tf.constant([0, 1, 2, 3]) # We use our own instead of tf.image.rot90 because that one broke # internally shortly before deadline... data["image"] = tf.stack([ data["image"], tf.transpose(tf.reverse_v2(data["image"], [1]), [1, 0, 2]), tf.reverse_v2(data["image"], [0, 1]), tf.reverse_v2(tf.transpose(data["image"], [1, 0, 2]), [1]), ]) return data
def predict_labels_multi_scale(images, model_options, eval_scales=(1.0,), add_flipped_images=False): """预测分割标签 :param images: tensor,[batch, height, width, channels] :param model_options: 卷积网络模型选择来配置模型 :param eval_scales: 用于调整图像大小以进行评估的比例 :param add_flipped_images:是否添加翻转图像用于评估 :return:具有指定output_type(例如,语义预测)的键的字典和存储表示预测的张量的值(通道上的argmax). 每个预测的大小[批次,高度,宽度]。 """ outputs_to_predictions = { output: [] for output in model_options.outputs_to_num_classes } for i, image_scale in enumerate(eval_scales): with tf.variable_scope(tf.get_variable_scope(), reuse=True if i else None): outputs_to_scales_to_logits = multi_scale_logits(images, model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) if add_flipped_images: with tf.variable_scope(tf.get_variable_scope(), reuse=True): outputs_to_scales_to_logits_reversed = multi_scale_logits( tf.reverse_v2(images, [2]), model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) for output in sorted(outputs_to_scales_to_logits): scales_to_logits = outputs_to_scales_to_logits[output] logits = tf.image.resize_bilinear(scales_to_logits[MERGED_LOGITS_SCOPE], tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits), 4)) if add_flipped_images: scales_to_logits_reversed = (outputs_to_scales_to_logits_reversed[output]) logits_reversed = tf.image.resize_bilinear( tf.reverse_v2(scales_to_logits_reversed[MERGED_LOGITS_SCOPE], [2]), tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits_reversed), 4)) for output in sorted(outputs_to_predictions): predictions = outputs_to_predictions[output] #计算不同尺度和翻转图像的平均预测。 predictions = tf.reduce_mean(tf.concat(predictions, 4), axis=4) outputs_to_predictions[output] = tf.argmax(predictions, 3) return outputs_to_predictions
def random_flip_lr(image, bboxes, seed=None): """Random flip left-right of an image and its bounding boxes.""" def flip_bboxes(bboxes): """Flip bounding boxes coordinates. Args: bboxes: [xmin, ymin, xmax, ymax] """ bboxes = tf.stack([1 - bboxes[:, 2], bboxes[:, 1], 1 - bboxes[:, 0], bboxes[:, 3]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('random_flip_left_right'): image = tf.convert_to_tensor(image, name='image') uniform_random = tf.random_uniform([], 0, 1.0, seed=seed) mirror_cond = tf.less(uniform_random, .5) # Flip image. result = tf.cond(mirror_cond, lambda: tf.reverse_v2(image, axis=[1]), lambda: image) # Flip bboxes. bboxes = tf.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) return fix_image_flip_shape(image, result), bboxes
def flip(): flipped = [] for tensor in tensor_list: if dim < 0 or dim >= len(tensor.get_shape().as_list()): raise ValueError('dim must represent a valid dimension.') flipped.append(tf.reverse_v2(tensor, [dim])) return flipped
def init_U(d): translations = tf.cast(tf.reverse_v2(d, [0]), dtype=tf.float32) shifted_original2 = tf.contrib.image.translate(original2, translations) m = tf.minimum(original1, shifted_original2) / tf.maximum(original1, shifted_original2) # if w and v are zero, we get nan, but we should get 0. m = tf.where(tf.is_nan(m), tf.zeros_like(m), m) return tf.reshape(m, tf.shape(m)[1:])
def flip_dim(tensor, prob=0.5, dim=1): random_value = tf.random_uniform([]) is_flipped = tf.less_equal(random_value, prob) flipped = tf.cond(is_flipped, lambda: tf.reverse_v2(tensor, [dim]), lambda: tensor) return flipped
def propagate_pooled(self, equal1, equal2, S, d): translations = tf.cast(tf.reverse_v2(d, [0]), dtype=tf.float32) shifted_equal2 = tf.contrib.image.translate(equal2, translations) m = equal1 * shifted_equal2 return S * m
def get_em_acc_op(self, bucket_id): """Create a em_acc_op.""" with tf.name_scope("EMAcc_%d" % bucket_id): # [sequence_length, batch_size] input_ph = tf.placeholder(tf.int64, shape=(None, None)) # [sequence_length, batch_size, vocab_size] output_ph = tf.placeholder(tf.float32, shape=(None, None, self.target_vocab_size)) input_op = tf.reverse_v2(input_ph, axis=[0]) output_op = tf.argmax(output_ph, axis=2) def replace_eos_with_pad(in_seq): """Replace all tokens after EOS in sequence with PAD.""" out_seq = in_seq.copy() for idx in range(in_seq.shape[-1]): eos_list = in_seq[:, idx].reshape(in_seq.shape[0]).tolist() eos_idx = eos_list.index( EOS_ID) if EOS_ID in eos_list else -1 out_seq[eos_idx:, idx] = PAD_ID return out_seq eos_op = tf.py_func(replace_eos_with_pad, [output_op], tf.int64) equal_op = tf.equal( tf.reduce_sum(tf.abs(input_op - eos_op), axis=0), 0) em_acc_op = tf.reduce_mean(tf.cast(equal_op, tf.float32), axis=0) summary_op = tf.summary.scalar("EMAccSummary", em_acc_op) return input_ph, output_ph, em_acc_op, summary_op
def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None): """Sort boxes and associated fields according to a scalar value. In-place operation. `boxlist` must have that field which stores a 1-D numeric tensor. Args: boxlist: a BoxList holding `n` boxes. field: string scalar, the field by which boxes are sorted. Must be 1-D. order: int scalar, indicating descend or ascend. Defaults to descend. scope: string scalar, name scope. Returns: sorted_boxlist: a BoxList holding `n` boxes where boxes are ordered by the values in the field `field` in the specified order. """ with tf.name_scope(scope, 'SortByField'): if order != SortOrder.descend and order != SortOrder.ascend: raise ValueError('Invalid sort order.') field_to_sort = boxlist.get_field(field) num_boxes = boxlist.num_boxes() _, sorted_indices = tf.nn.top_k(field_to_sort, num_boxes, sorted=True) if order == SortOrder.ascend: sorted_indices = tf.reverse_v2(sorted_indices, [0]) return gather(boxlist, sorted_indices)
def to_image_tf(tensor_in, colormap=None): """Reverse the second dimension and swap the second dimension and the third dimension""" if colormap is None: colormap = get_colormap() shape = tf.stack([-1, tensor_in.get_shape()[1], tensor_in.get_shape()[2], 3]) recolored = tf.reshape(tf.matmul(tf.reshape(tensor_in, [-1, 5]), colormap), shape) return tf.transpose(tf.reverse_v2(recolored, axis=[2]), [0, 2, 1, 3])
def flip(): flipped = [] for tensor in tensor_list: if dim < 0 or dim >= len(tensor.get_shape().as_list()): raise ValueError('dim must represent a valid dimension.') flipped.append(tf.reverse_v2(tensor, [dim])) # 反转张量的特定维度dim return flipped
def estimate_age_and_gender(aligned_images, model_path): with tf.Graph().as_default(): session = tf.Session() images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image') images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]), images_pl) #BGR TO RGB images_norm = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), images) train_mode = tf.placeholder(tf.bool) age_logits, gender_logits, _ = inception_resnet_v1.inference( images_norm, keep_probability=0.8, phase_train=train_mode, weight_decay=1e-5) gender = tf.argmax(tf.nn.softmax(gender_logits), 1) age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32) age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) session.run(init_op) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(session, ckpt.model_checkpoint_path) else: pass return session.run([age, gender], feed_dict={ images_pl: aligned_images, train_mode: False })
def _encoder(self): with tf.variable_scope("encoder"): with tf.variable_scope("embedding-layer"): if not self.pretrained_wordvec: embedding_encoder = tf.get_variable( name="decoder-embedding", shape=[self.vocab_size_x, self.embed_size], dtype=tf.float32) else: embedding_encoder = np.load("glove.npy") tf.summary.histogram(name="embedding-encoder", values=embedding_encoder) embeded_x = tf.nn.embedding_lookup( embedding_encoder, self.input_x) # [None, sequence_len, embed_size] with tf.variable_scope("backward-rnn"): # backward rnn embeded_x_back = tf.reverse_v2(embeded_x, axis=[1]) self.encoder_rnn_cell = self._get_cell(self.num_units, self.rnn_type) init_state = self.encoder_rnn_cell.zero_state(self.batch_size, dtype=tf.float32) encoder_output, encoder_state = tf.nn.dynamic_rnn( cell=self.encoder_rnn_cell, inputs=embeded_x_back, initial_state=init_state) return encoder_state # [None, sequence_len, num_units], [None, num_units]
def random_flip_left_right(image, bboxes, seed=None): """Random flip left-right of an image and its bounding boxes. 随机翻转图像及其边界框的左右两侧。 """ def flip_bboxes(bboxes): """Flip bounding boxes coordinates. """ bboxes = tf.stack( [bboxes[:, 0], 1 - bboxes[:, 3], bboxes[:, 2], 1 - bboxes[:, 1]], axis=-1) return bboxes # Random flip. Tensorflow implementation. with tf.name_scope('random_flip_left_right'): image = tf.convert_to_tensor(image, name='image') _check3dimage(image, require_static=False) uniform_random = tf.random_uniform([], 0, 1.0, seed=seed) mirror_cond = tf.less(uniform_random, .5) # Flip image. result = tf.cond(mirror_cond, lambda: tf.reverse_v2(image, [1]), lambda: image) # Flip bboxes. bboxes = tf.cond(mirror_cond, lambda: flip_bboxes(bboxes), lambda: bboxes) image_shape = image.get_shape() result.set_shape(image_shape) return result, bboxes
def image_mirroring(img, label): """ Randomly mirrors the images. Args: img: Training image to mirror. label: Segmentation mask to mirror. """ distort_left_right_random = tf.random_uniform([1], 0, 1.0, dtype=tf.float32)[0] mirror = tf.less(tf.stack([1.0, distort_left_right_random, 1.0]), 0.5) img = tf.reverse_v2(img, mirror) label = tf.reverse_v2(label, mirror) return img, label
def to_image_tf(tensor_in, colormap=None): """Reverse the second dimension and swap the second dimension and the third dimension""" if colormap is None: colormap = get_colormap() shape = tf.stack([-1, tensor_in.get_shape()[1], tensor_in.get_shape()[2], 3]) recolored = tf.reshape(tf.matmul(tf.reshape(tensor_in, [-1, 2]), colormap), shape) ##recolored = tf.reshape(tf.matmul(tf.reshape(tensor_in, [-1, 6]), colormap), shape) return tf.transpose(tf.reverse_v2(recolored, axis=[2]), [0, 2, 1, 3])
def propagate_convolved(self, original1, original2, S, d): translations = tf.cast(tf.reverse_v2(d, [0]), dtype=tf.float32) shifted_original2 = tf.contrib.image.translate(original2, translations) m = tf.minimum(original1, shifted_original2) / tf.maximum(original1, shifted_original2) # if w and v are zero, we get nan, but we should get 0. m = tf.where(tf.is_nan(m), tf.zeros_like(m), m) return S * m
def flip(): image_tensor_reversed = tf.reverse_v2(image_tensor, [1]) bbox_x_coords = 1.0 - bbox_tensor[:, 0] bbox_y_coords = bbox_tensor[:, 1] pts_x_coords = 1.0 - points_tensor[:, 0] pts_y_coords = points_tensor[:, 1] bbox_ret = tf.stack([bbox_x_coords, bbox_y_coords], axis=1) pts_ret = tf.stack([pts_x_coords, pts_y_coords], axis=1) return image_tensor_reversed, bbox_ret, pts_ret
def flip(): image_tensor_reversed = tf.reverse_v2(image_tensor, [0]) x_coords = points_tensor[:, 0] y_coords = image_height - points_tensor[:, 1] tmp = tf.stack([x_coords, y_coords], axis=1) p1 = tmp[0, :] p2 = tmp[1, :] p3 = tmp[2, :] p4 = tmp[3, :] points_reversed = tf.stack([p3, p4, p1, p2]) return image_tensor_reversed, points_reversed
def gru_backward(self, input_x, hidden_size, name_variable): """ GRU backward :param input_x:shape:[batch_size*num_sentence, sequence_length, embedding_size] :param hidden_size: gru output hidden size :param name_variable: name of gru variable :return:GRU backward outputs and every time step state """ with tf.variable_scope(name_variable): input_x = tf.reverse_v2(input_x, axis=[1]) gru_cell = self.create_gru_unit(hidden_size, 'gru_backward') # init unit state zero_state_length = tf.shape(input_x)[0] gru_init_state = gru_cell.zero_state(zero_state_length, dtype=tf.float32) # run GRU backward outputs, state = tf.nn.dynamic_rnn(gru_cell, inputs=input_x, initial_state=gru_init_state) outputs = tf.reverse_v2(outputs, axis=[1]) return outputs, state
def load_session(model_path=MODEL_PATH): global SESSION global AGE global GENDER global IMAGES_PL global TRAIN_MODE global tf_config graph = tf.Graph().as_default() sess = tf.Session(config=tf_config) images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image') images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]), images_pl) #BGR TO RGB images_norm = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), images) train_mode = tf.placeholder(tf.bool) age_logits, gender_logits, _ = inception_resnet_v1.inference( images_norm, keep_probability=1.0, #0.8 phase_train=train_mode, weight_decay=1e-5) gender = tf.argmax(tf.nn.softmax(gender_logits), 1) age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32) age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1) GENDER = gender AGE = age IMAGES_PL = images_pl TRAIN_MODE = train_mode init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init_op) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print("restore and continue training! : {}".format( ckpt.model_checkpoint_path)) else: sys.exit("Age-Gender Model not found") SESSION = sess
def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None): """Sort boxes and associated fields according to a scalar field. A common use case is reordering the boxes according to descending scores. Args: boxlist: BoxList holding N boxes. field: A BoxList field for sorting and reordering the BoxList. order: (Optional) descend or ascend. Default is descend. scope: name scope. Returns: sorted_boxlist: A sorted BoxList with the field in the specified order. Raises: ValueError: if specified field does not exist ValueError: if the order is not either descend or ascend """ with tf.name_scope(scope, 'SortByField'): if order != SortOrder.descend and order != SortOrder.ascend: raise ValueError('Invalid sort order') field_to_sort = boxlist.get_field(field) if len(field_to_sort.shape.as_list()) != 1: raise ValueError('Field should have rank 1') num_boxes = boxlist.num_boxes() num_entries = tf.size(field_to_sort) length_assert = tf.Assert(tf.equal(num_boxes, num_entries), [ 'Incorrect field size: actual vs expected.', num_entries, num_boxes ]) with tf.control_dependencies([length_assert]): # TODO(derekjchow): Remove with tf.device when top_k operation runs # correctly on GPU. with tf.device('/cpu:0'): _, sorted_indices = tf.nn.top_k(field_to_sort, num_boxes, sorted=True) if order == SortOrder.ascend: sorted_indices = tf.reverse_v2(sorted_indices, [0]) return gather(boxlist, sorted_indices)
def _flip_patches(self, tensor, name): with tf.variable_scope('flip-' + name): batch_size, seq_len, out_size = tensor.get_shape().as_list() out_h, out_w = self._output_shape(out_size, self.input_factor) output_number = self._output_number(seq_len) # prepare for flipping tmp = tf.reshape(tensor, [batch_size, output_number, -1, out_h, out_w]) tmp = tf.transpose(tmp, [0, 1, 3, 2, 4]) tmp = tf.reshape( tmp, [batch_size, out_h * output_number, out_w * output_number]) # flip tmp = tf.reverse_v2(tmp, [1]) # transform back tmp = tf.reshape(tmp, [batch_size, output_number, out_h, -1, out_w]) tmp = tf.transpose(tmp, [0, 1, 3, 2, 4]) return tf.reshape(tmp, [batch_size, seq_len, out_size])
def polyconv(self, M): """ compute the coefficients of the product of polynomials with individual coefs as the rows of M. Fixed width based on DXmax """ w = 4 * DXmax + 1 N = tf.concat( [tf.zeros([self.latent_input_dim, 4]), tf.cast(M, tf.float32)], axis=1) P = tf.foldl( lambda a, x: tf.reshape( tf.nn.convolution( tf.reshape(a, [1, w, 1]), tf.reshape(tf.reverse_v2(x, axis=[0]), [w, 1, 1]), 'SAME'), [w]), N) Q = tf.slice(P, [4], [-1]) # Q=tf.Print(Q,[M,Q,tf.shape(Q)],summarize=100,message='q') return tf.cast(Q, tf.float64)
def __init__(self): self.model_path = "./models" self.shape_detector = "shape_predictor_68_face_landmarks.dat" cuda = True font_scale = 1 thickness = 1 #log('1') print('1') with tf.Graph().as_default(): config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.4 # session = tf.Session(config=config, ...) self.sess = tf.Session(config=config) self.images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image') images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]), self.images_pl) #BGR TO RGB images_norm = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), images) self.train_mode = tf.placeholder(tf.bool) age_logits, gender_logits, _ = inception_resnet_v1.inference( images_norm, keep_probability=0.8, phase_train=self.train_mode, weight_decay=1e-5) self.gender = tf.argmax(tf.nn.softmax(gender_logits), 1) age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32) self.age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self.sess.run(init_op) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(self.model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(self.sess, ckpt.model_checkpoint_path) print("restore and continue training!") else: pass
def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None): """Sort boxes and associated fields according to a scalar field. A common use case is reordering the boxes according to descending scores. Args: boxlist: BoxList holding N boxes. field: A BoxList field for sorting and reordering the BoxList. order: (Optional) descend or ascend. Default is descend. scope: name scope. Returns: sorted_boxlist: A sorted BoxList with the field in the specified order. Raises: ValueError: if specified field does not exist ValueError: if the order is not either descend or ascend """ with tf.name_scope(scope, 'SortByField'): if order != SortOrder.descend and order != SortOrder.ascend: raise ValueError('Invalid sort order') field_to_sort = boxlist.get_field(field) if len(field_to_sort.shape.as_list()) != 1: raise ValueError('Field should have rank 1') num_boxes = boxlist.num_boxes() num_entries = tf.size(field_to_sort) length_assert = tf.Assert( tf.equal(num_boxes, num_entries), ['Incorrect field size: actual vs expected.', num_entries, num_boxes]) with tf.control_dependencies([length_assert]): # TODO: Remove with tf.device when top_k operation runs # correctly on GPU. with tf.device('/cpu:0'): _, sorted_indices = tf.nn.top_k(field_to_sort, num_boxes, sorted=True) if order == SortOrder.ascend: sorted_indices = tf.reverse_v2(sorted_indices, [0]) return gather(boxlist, sorted_indices)
def _create_session(self): logger.debug("Creating session") with tf.Graph().as_default(): self.session = tf.Session() images_pl = tf.placeholder(tf.float32, shape=[None, 160, 160, 3], name='input_image') images = tf.map_fn(lambda frame: tf.reverse_v2(frame, [-1]), images_pl) #BGR TO RGB images_norm = tf.map_fn( lambda frame: tf.image.per_image_standardization(frame), images) train_mode = tf.placeholder(tf.bool) age_logits, gender_logits, _ = resnet.inference( images_norm, keep_probability=0.8, phase_train=train_mode, weight_decay=1e-5) gender = tf.argmax(tf.nn.softmax(gender_logits), 1) age_ = tf.cast(tf.constant([i for i in range(0, 101)]), tf.float32) age = tf.reduce_sum(tf.multiply(tf.nn.softmax(age_logits), age_), axis=1) init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) self.session.run(init_op) saver = tf.train.Saver() ckpt = tf.train.get_checkpoint_state(self.model_path) if ckpt and ckpt.model_checkpoint_path: saver.restore(self.session, ckpt.model_checkpoint_path) self._age = age self._gender = gender self._images_pl = images_pl self._train_mode = train_mode logger.debug("Session restorted") else: logger.debug("Could not create session") profiler.tick("Created session")
def __init__(self, k=50, filter_width=5, pooling_size=4, axis_num=3, thread_num=4, param_scope_name='MotionBasisLearner', save_params_path='./save_variables/params', summary_dir='./summaries/', accumulated_steps=0): self.k = k self.filter_width = filter_width self.pooling_size = pooling_size self.axis_num = axis_num self.save_params_path = save_params_path self.summary_dir = summary_dir self.accumulated_steps = accumulated_steps self.param_scope_name = param_scope_name assert thread_num >= 1 self.thread_num = thread_num # number of thread to run on. with tf.variable_scope(param_scope_name) as crbm_scope: self.w = tf.get_variable( 'weights', shape=(axis_num, filter_width, 1, k), dtype=tf.float32, initializer=tf.random_normal_initializer()) self.w_r = tf.reverse_v2(self.w, [0, 1]) self.hb = tf.get_variable( 'hidden_biase', shape=(k, ), dtype=tf.float32, initializer=tf.random_normal_initializer()) self.vb = tf.get_variable( 'visible_biase', shape=(1, ), dtype=tf.float32, initializer=tf.random_normal_initializer()) self.sess = tf.Session() # initialize parameters self.sess.run(tf.global_variables_initializer())
def read_and_decode(filename_queue): reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'age': tf.FixedLenFeature([], tf.int64), 'gender': tf.FixedLenFeature([], tf.int64), 'file_name': tf.FixedLenFeature([], tf.string) }) # Convert from a scalar string tensor (whose single string has # length mnist.IMAGE_PIXELS) to a uint8 tensor with shape # [mnist.IMAGE_PIXELS]. # image = tf.image.decode_jpeg(features['image_raw'], channels=3) # image = tf.image.resize_images(image, [64, 64]) # image = tf.cast(image, tf.uint8) # image.set_shape([mnist.IMAGE_PIXELS]) # OPTIONAL: Could reshape into a 28x28 image and apply distortions # here. Since we are not applying any distortions in this # example, and the next step expects the image to be flattened # into a vector, we don't bother. # Convert from [0, 255] -> [-0.5, 0.5] floats. # image = image * (1. / 255) - 0.5 image = tf.decode_raw(features['image_raw'], tf.uint8) image.set_shape([160 * 160 * 3]) image = tf.reshape(image, [160, 160, 3]) image = tf.reverse_v2(image, [-1]) image = tf.image.per_image_standardization(image) # image = tf.cast(image,tf.float32) * (1. / 255) - 0.5 # Convert label from a scalar uint8 tensor to an int32 scalar. age = features['age'] gender = features['gender'] file_path = features['file_name'] return image, age, gender, file_path
def predict_labels_multi_scale(images, model_options, eval_scales=(1.0,), add_flipped_images=False): """Predicts segmentation labels. Args: images: A tensor of size [batch, height, width, channels]. model_options: A ModelOptions instance to configure models. eval_scales: The scales to resize images for evaluation. add_flipped_images: Add flipped images for evaluation or not. Returns: A dictionary with keys specifying the output_type (e.g., semantic prediction) and values storing Tensors representing predictions (argmax over channels). Each prediction has size [batch, height, width]. """ outputs_to_predictions = { output: [] for output in model_options.outputs_to_num_classes } for i, image_scale in enumerate(eval_scales): with tf.variable_scope(tf.get_variable_scope(), reuse=True if i else None): outputs_to_scales_to_logits = multi_scale_logits( images, model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) if add_flipped_images: with tf.variable_scope(tf.get_variable_scope(), reuse=True): outputs_to_scales_to_logits_reversed = multi_scale_logits( tf.reverse_v2(images, [2]), model_options=model_options, image_pyramid=[image_scale], is_training=False, fine_tune_batch_norm=False) for output in sorted(outputs_to_scales_to_logits): scales_to_logits = outputs_to_scales_to_logits[output] logits = tf.image.resize_bilinear( scales_to_logits[MERGED_LOGITS_SCOPE], tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits), 4)) if add_flipped_images: scales_to_logits_reversed = ( outputs_to_scales_to_logits_reversed[output]) logits_reversed = tf.image.resize_bilinear( tf.reverse_v2(scales_to_logits_reversed[MERGED_LOGITS_SCOPE], [2]), tf.shape(images)[1:3], align_corners=True) outputs_to_predictions[output].append( tf.expand_dims(tf.nn.softmax(logits_reversed), 4)) for output in sorted(outputs_to_predictions): predictions = outputs_to_predictions[output] # Compute average prediction across different scales and flipped images. predictions = tf.reduce_mean(tf.concat(predictions, 4), axis=4) outputs_to_predictions[output] = tf.argmax(predictions, 3) return outputs_to_predictions