def build_transformer_placeholders(input_dim=40, gpu_nums=0): group_input_seq_len = [] group_output_seq_len = [] batch_size = tf.Dimension(None) input_dim = tf.Dimension(input_dim) #returns group_input_seq = [] group_input_len = [] group_input_mask = [] group_output_seq = [] group_output_mask = [] for i in range(gpu_nums): group_input_seq_len.append(tf.Dimension(None)) group_input_seq.append( tf.placeholder( tf.float32, shape=[group_input_seq_len[i], batch_size, input_dim], name='input_seq' + str(i))) group_input_len.append( tf.placeholder(tf.int32, [batch_size], name='input_len' + str(i))) group_input_mask.append( tf.placeholder(tf.int32, shape=[group_input_seq_len[i], batch_size], name='input_mask' + str(i))) group_output_seq_len.append(tf.Dimension(None)) group_output_seq.append( tf.placeholder(tf.int32, shape=[group_output_seq_len[i], batch_size], name='output_seq' + str(i))) group_output_mask.append( tf.placeholder(tf.int32, shape=[group_output_seq_len[i], batch_size], name='output_mask' + str(i))) return TransformerPlaceholders(group_input_len, group_input_seq, group_input_mask, group_output_mask, group_output_seq)
def build(self, input_layer): """ Construct the layer in tensorflow """ # Determine the size of the input when flattened input_layer_shape = input_layer.get_shape()[1:].dims flattened_dimension = reduce(lambda x, y: x * y, input_layer_shape, tf.Dimension(1)) # Create the ReLU layer self.layer = tf.reshape(input_layer, [-1, flattened_dimension.value], name=self.name) return self.layer, 0
def generate_iterator_ops(filenames, train=True, reuse=False): dataset = tf.data.TFRecordDataset(filenames) dataset = dataset.map(_parse_function, num_parallel_calls=8) if train: dataset = dataset.shuffle(buffer_size=2 * FLAGS.batch_size) dataset = dataset.padded_batch( FLAGS.batch_size, ([tf.Dimension(None), tf.Dimension(301)], [tf.Dimension(None)], [], [ tf.Dimension(None) ], [tf.Dimension(None)], [tf.Dimension(None)], [tf.Dimension(None)], [tf.Dimension(None)], [tf.Dimension(None)], [tf.Dimension(None)])) data_iterator = dataset.make_initializable_iterator() # grab a batch of data next_x, next_y, next_l, next_pos, next_dep, next_path, next_lp, next_cp, next_id, next_c = data_iterator.get_next( ) ops = annotation_func_train(next_x, next_y, next_l, next_pos, next_dep, next_path, next_lp, next_cp, next_id, next_c, train=train, reuse=reuse) ops = list(ops) ops.append(next_y) ops.append(next_l) return data_iterator, ops
def _set_waveforms_shape(self, waveforms): """ Sets the final dimension of a batch of waveforms. When we receive a batch of waveforms its final dimension is `None`, even though we know that the dimension is the sliced waveform length. We set the dimension since if we don't and the model includes at least one convolutional layer, then the `Classifier.train` method raises an exception. """ dims = list(waveforms.shape.dims) dims[-1] = tf.Dimension(self.waveform_length) shape = tf.TensorShape(dims) waveforms.set_shape(shape)
def soft_attn(self, top_recur): """""" reuse = (self.moving_params is not None) or None input_size = top_recur.get_shape().as_list()[-1] with tf.variable_scope('MLP', reuse=reuse): head_mlp, dep_mlp = self.MLP(top_recur, self.info_mlp_size, func=self.info_func, keep_prob=self.info_keep_prob, n_splits=2) with tf.variable_scope('Arcs', reuse=reuse): arc_logits = self.bilinear_classifier( dep_mlp, head_mlp, keep_prob=self.info_keep_prob) arc_prob = self.softmax(arc_logits) head_lin = tf.batch_matmul(arc_prob, top_recur) top_recur = tf.concat(2, [top_recur, head_lin]) top_recur.set_shape([ tf.Dimension(None), tf.Dimension(None), tf.Dimension(4 * self.recur_size) ]) return top_recur
def flatten(self, keep_prob=1): """ :param keep_prob: int. set to 1 for no dropout """ self.count['flat'] += 1 scope = 'flat_' + str(self.count['flat']) with tf.variable_scope(scope): input_nodes = tf.Dimension(self.input.get_shape()[1] * self.input.get_shape()[2] * self.input.get_shape()[3]) output_shape = tf.pack([-1, input_nodes]) self.input = tf.reshape(self.input, output_shape) if keep_prob != 1: self.input = tf.nn.dropout(self.input, keep_prob=keep_prob) self.print_log(scope + ' output: ' + str(self.input.get_shape()))
def linear(self, inputs, output_size, n_splits=1, add_bias=False): """""" n_dims = len(inputs.get_shape().as_list()) batch_size = tf.shape(inputs)[0] bucket_size = tf.shape(inputs)[1] input_size = inputs.get_shape().as_list()[-1] output_shape = tf.pack([batch_size] + [bucket_size] * (n_dims - 2) + [output_size]) shape_to_set = [tf.Dimension(None)] * (n_dims - 1) + [ tf.Dimension(output_size) ] if self.moving_params is None: keep_prob = self.info_keep_prob else: keep_prob = 1 if keep_prob < 1: noise_shape = tf.pack([batch_size] + [1] * (n_dims - 2) + [input_size]) inputs = tf.nn.dropout(inputs, keep_prob, noise_shape=noise_shape) lin = linalg.linear(inputs, output_size, n_splits=n_splits, add_bias=add_bias, moving_params=self.moving_params) if n_splits == 1: lin = [lin] for i, split in enumerate(lin): split.set_shape(shape_to_set) if n_splits == 1: return lin[0] else: return lin
def input_fn(self, mode, batch_size, bucket_boundaries=None, bow=False): """Returns an instance of tf.data.Dataset. Args: mode: One of 'train' or 'test'. batch_size: Size of a batch. bucket_boundaries: List of boundaries for bucketing. bow: True to process the input as a bag-of-words. """ if mode not in ('train', 'test'): raise ValueError('Unsupported mode type %s' % mode) dataset = self._dataset[mode] # Transform into a bag-of-words input if applicable. def bag_of_words(tokens, label): indices = tf.expand_dims(tokens, axis=-1) updates = tf.ones([tf.shape(indices)[0]]) shape = tf.constant([self.tokenizer.vocab_size], dtype=indices.dtype) scatter = tf.scatter_nd(indices, updates, shape) return scatter, label if bow: dataset = dataset.map(bag_of_words, num_parallel_calls=12) # Shuffle the data. if self.max_length: dataset = dataset.filter(lambda f, l: tf.shape(f)[0] < self.max_length) dataset = dataset.shuffle( buffer_size=FLAGS.shuffle_buffer_size, reshuffle_each_iteration=True) # Create batches of examples and pad. if mode == 'train' and bucket_boundaries: bucket_batch_sizes = [batch_size] * (len(bucket_boundaries) + 1) dataset = dataset.apply( tf.data.experimental.bucket_by_sequence_length( lambda feature, label: tf.shape(feature)[0], bucket_boundaries=bucket_boundaries, bucket_batch_sizes=bucket_batch_sizes, padded_shapes=dataset.output_shapes)) else: output_shapes = dataset.output_shapes if self.max_length: output_shapes = (tf.TensorShape([tf.Dimension(sefl.max_length)]), tf.TensorShape([])) dataset = dataset.padded_batch(batch_size, output_shapes) dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) return dataset
def __init__(self, meta_file, checkpoint_file, dest_nodes, input_shape=None, in_nodes=None, input_format="NCHW".encode()): graph_def = None self.weights = None self.inputs = in_nodes self.outputs = dest_nodes sess = tf.Session() if meta_file is None: raise Exception("meta_file must be provided") new_saver = tf.train.import_meta_graph(meta_file) if checkpoint_file is not None: self.weights = dict() new_saver.restore(sess, tf.train.latest_checkpoint(checkpoint_file)) for var in tf.global_variables(): value = var.eval(sess) self.weights[var.name.split(':')[0]] = value self.infer = ModelInfer(sess) graph_def, ver = tf.get_default_graph()._as_graph_def(add_shapes=True) if in_nodes is not None and input_shape is not None: graph_def = strip_unused_lib.strip_unused( input_graph_def=graph_def, input_node_names=in_nodes, output_node_names=dest_nodes, placeholder_type_enum=dtypes.float32.as_datatype_enum) for node in graph_def.node: if node.name in in_nodes: index = in_nodes.index(node.name) shape = [tf.Dimension(x) for x in input_shape[index]] shape_proto = tf.TensorShape(shape).as_proto() node.attr['_output_shapes'].list.shape.pop() node.attr['_output_shapes'].list.shape.extend( [shape_proto]) self.infer.gen_sample_data(node.name, input_shape[index]) self.tf_graph = TensorflowGraph(graph_def) else: raise Exception('in_nodes and output_nodes need be provided') self.tf_graph.build(input_format)
def sample_code(self, code_distrib, n_samples=1): """ :param code_distrib: :param n_samples: :return: """ d = [tf.Dimension(n_samples), code_distrib.batch_shape[0]] if len(code_distrib.batch_shape) > 0 else [n_samples, 1] random_seed = self._latent_prior.sample( d) # actually sampling from a N(0,1). Just using _latent_prior since it's N(0,1) sampled_encoding = code_distrib.mean() + random_seed * code_distrib.stddev() sampled_encoding = tf.reshape(sampled_encoding, [-1] + list(sampled_encoding.shape[2:])) return sampled_encoding
def build(self, input_layer, trainable=True): """ Construct the layer in tensorflow """ with tf.variable_scope(self.name): # Determine the size of the input when flattened input_layer_shape = input_layer.get_shape()[1:].dims flattened_dimension = reduce(lambda x, y: x * y, input_layer_shape, tf.Dimension(1)) # Create the layer self.layer = tf.reshape(input_layer, [-1, flattened_dimension.value]) return self.layer, None, None
def repeat_2d(x, reps, axis): assert(axis == 0 or axis == 1) if axis == 1: x = tf.transpose(x) static_shape = list(x.get_shape()) dyn_shape = tf.shape(x) x_repeat = tf.reshape(tf.tile(x, [1, reps]), (dyn_shape[0] * reps, dyn_shape[1])) if static_shape[0].value is not None: static_shape[0] = tf.Dimension(static_shape[0].value *reps) x_repeat.set_shape(static_shape) if axis == 1: x_repeat = tf.transpose(x_repeat) return x_repeat
def batched_data(self, tfrecord_filename, single_example_parser, batch_size, padded_shapes, num_epochs=1, buffer_size=1000): padded_shapes = tf.Dimension(None) tfrecord_filenames = [ "%s-%.5d-of-%.5d" % (tfrecord_filename, shard, 10) for shard in range(10) ] dataset = tf.data.TFRecordDataset(tfrecord_filenames).map( single_example_parser).padded_batch( batch_size, padded_shapes=padded_shapes).shuffle( buffer_size).repeat(num_epochs) return dataset.make_one_shot_iterator().get_next()
def __init__(self, meta_file, checkpoint_file, dest_nodes, inputShape = None, in_nodes = None): super(TensorflowParser, self).__init__() # load model files into TensorFlow graph if meta_file: model = TensorflowParser._load_meta(meta_file) if checkpoint_file: self.ckpt_data = TensorflowParser._load_weights(checkpoint_file) self.weight_loaded = True # extract subgraph using in_nodes and dest_nodes if in_nodes != None and inputShape != None: from tensorflow.python.tools import strip_unused_lib from tensorflow.python.framework import dtypes from tensorflow.python.platform import gfile input_node_names = in_nodes.split(',') output_node_names = dest_nodes.split(',') model = strip_unused_lib.strip_unused( input_graph_def = model, input_node_names = input_node_names, output_node_names = output_node_names, placeholder_type_enum = dtypes.float32.as_datatype_enum) input_list = [None] for i in range(len(inputShape)): input_list.append(tensorflow.Dimension(inputShape[i])) tensor_input = tensorflow.TensorShape(input_list) # Build network graph self.tf_graph = TensorflowGraph(model) for node in self.tf_graph.model.node: if node.name in input_node_names: node.attr['shape'].list.shape.extend([tensor_input.as_proto()]) node.attr['_output_shapes'].list.shape.pop() #unknown_rank pop node.attr['_output_shapes'].list.shape.extend([tensor_input.as_proto()]) # extract subgraph using dest_nodes elif dest_nodes != None: from tensorflow.python.framework.graph_util import extract_sub_graph model = extract_sub_graph(model, dest_nodes.split(',')) self.tf_graph = TensorflowGraph(model) else: self.tf_graph = TensorflowGraph(model) self.tf_graph.build()
def unravel_argmax(argmax, shape): print("argmax") print(argmax.get_shape().as_list()) argmax_shape = argmax.get_shape() print(argmax_shape[0]) #new_1dim_shape = tf.shape(tf.constant(0, shape=[tf.Dimension(4), argmax_shape[0]*argmax_shape[1]*argmax_shape[2]*argmax_shape[3]])) new_1dim_shape = tf.shape(tf.constant(0, shape=[tf.Dimension(4), argmax_shape[1] * argmax_shape[2] * argmax_shape[3]])) batch_shape = tf.constant(0, dtype=tf.int64, shape=[argmax_shape[0], 1, 1, 1]).get_shape() b = tf.multiply(tf.ones_like(argmax), tf.reshape(tf.range(shape[0]), batch_shape)) y = argmax // (shape[2] * shape[3]) x = argmax % (shape[2] * shape[3]) // shape[3] c = tf.ones_like(argmax) * tf.range(shape[3]) pack = tf.stack([b, y, x, c]) pack = tf.reshape(pack, new_1dim_shape) pack = tf.transpose(pack) return pack
def _init_inception(): global softmax, prefix if not os.path.exists(MODEL_DIR): os.makedirs(MODEL_DIR) filename = DATA_URL.split('/')[-1] filepath = os.path.join(MODEL_DIR, filename) if not os.path.exists(filepath): def _progress(count, block_size, total_size): sys.stdout.write('\r>> Downloading %s %.1f%%' % ( filename, float(count * block_size) / float(total_size) * 100.0)) sys.stdout.flush() filepath, _ = urllib.request.urlretrieve(DATA_URL, filepath, _progress) print() statinfo = os.stat(filepath) print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') tarfile.open(filepath, 'r:gz').extractall(MODEL_DIR) with tf.gfile.FastGFile(os.path.join( MODEL_DIR, 'classify_image_graph_def.pb'), 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) for node in graph_def.node: node.device = "/gpu:2" _ = tf.import_graph_def(graph_def, name=prefix) if prefix[-1] != '/': prefix += '/' # Works with an arbitrary minibatch size. with tf.Session(config=config) as sess: pool3 = sess.graph.get_tensor_by_name(prefix + 'pool_3:0') ops = pool3.graph.get_operations() for op_idx, op in enumerate(ops): for o in op.outputs: shape = o.get_shape() shape = [s.value for s in shape] # new_shape = [] for j, s in enumerate(shape): if s == 1 and j == 0: o.shape.dims[0] = tf.Dimension(None) # new_shape.append(None) # else: # new_shape.append(s) # o.set_shape(tf.TensorShape(new_shape)) w = sess.graph.get_operation_by_name(prefix + "softmax/logits/MatMul").inputs[1] logits = tf.matmul(tf.squeeze(pool3, (1, 2)), w) softmax = tf.nn.softmax(logits)
def test_graph_lookup(): embeddings = ff_tf.ff_embeddings() init = ff_tf.initialize_ff_embeddings(embeddings, "testdata/test.fifu", False) ber = ff_tf.ff_lookup(embeddings, "Berlin", mask_empty_string=False, mask_failed_lookup=False, embedding_len=100) assert ber.shape == (100, ) ber_list = ff_tf.ff_lookup(embeddings, ["Berlin"], mask_empty_string=False, mask_failed_lookup=False, embedding_len=100) assert ber_list.shape == (1, 100) ber_tensor = ff_tf.ff_lookup(embeddings, [["Berlin"]], mask_empty_string=False, mask_failed_lookup=False, embedding_len=100) assert ber_tensor.shape == (1, 1, 100) ber_no_shape = ff_tf.ff_lookup(embeddings, "Berlin", mask_empty_string=False, mask_failed_lookup=False) assert ber_no_shape.shape.rank == 1 assert ber_no_shape.shape[0].value is None ber_list_no_shape = ff_tf.ff_lookup(embeddings, ["Berlin"], mask_empty_string=False, mask_failed_lookup=False) assert ber_list_no_shape.shape.rank == 2 assert ber_list_no_shape.shape[0].value == tf.Dimension(1) assert ber_list_no_shape.shape[1].value is None with tf.Session() as sess: sess.run([init]) res = sess.run([ber, ber_list, ber_tensor]) assert res[0].shape == (100, ) assert res[1].shape == (1, 100) assert res[2].shape == (1, 1, 100) sess.run([ff_tf.close_ff_embeddings(embeddings)])
def RNN(self, inputs): """""" input_size = inputs.get_shape().as_list()[-1] cell = self.recur_cell(self._config, input_size=input_size, moving_params=self.moving_params) lengths = tf.reshape(tf.to_int64(self.sequence_lengths), [-1]) if self.moving_params is None: ff_keep_prob = self.ff_keep_prob recur_keep_prob = self.recur_keep_prob else: ff_keep_prob = 1 recur_keep_prob = 1 if self.recur_bidir: top_recur, fw_recur, bw_recur = rnn.dynamic_bidirectional_rnn( cell, cell, inputs, lengths, ff_keep_prob=ff_keep_prob, recur_keep_prob=recur_keep_prob, dtype=tf.float32) fw_cell, fw_out = tf.split(axis=1, num_or_size_splits=2, value=fw_recur) bw_cell, bw_out = tf.split(axis=1, num_or_size_splits=2, value=bw_recur) end_recur = tf.concat(axis=1, values=[fw_out, bw_out]) top_recur.set_shape([ tf.Dimension(None), tf.Dimension(None), tf.Dimension(2 * self.recur_size) ]) else: top_recur, end_recur = rnn.dynamic_rnn( cell, inputs, lengths, ff_keep_prob=ff_keep_prob, recur_keep_prob=recur_keep_prob, dtype=tf.float32) top_recur.set_shape([ tf.Dimension(None), tf.Dimension(None), tf.Dimension(self.recur_size) ]) return top_recur, end_recur
def get_inference_input(params): eval_dataset = tf.data.Dataset.from_generator(generator_infer,{'input_img': tf.float32, 'lens': tf.int32, 'cnts': tf.float32, 'care': tf.int32}, {'input_img': ( tf.Dimension(None), tf.Dimension(None), tf.Dimension(None), 3), 'lens': (tf.Dimension(None),), 'cnts': ( tf.Dimension(None), tf.Dimension(None), tf.Dimension(None), tf.Dimension(None)), 'care':(tf.Dimension(None),)} ) iterator = eval_dataset.make_one_shot_iterator() features = iterator.get_next() return features
def __init__(self, target_id, data_dir, subset, num_examples, pad_shape, sparse_labels): """Initializer. Extends DataProvider. Args: data_dir (str): Directory containing (sharded) tfrecord files. subset (str): One of {'train', 'dev', 'test'}. num_examples (int): Number of examples in subset. pad_shape (tuple): Shape (height, width) of padded images. """ self.data_dir = data_dir self.subset = subset self.num_examples = num_examples self.pad_shape = pad_shape self.sparse_labels = sparse_labels super(TFDataSet, self).__init__(target_id) channels = tf.Dimension(1) # Converting to grayscale in _preproc self._padding = { "image/data": tf.TensorShape([tf.Dimension(self.pad_shape[0]), tf.Dimension(self.pad_shape[1]), channels]), "image/height": tf.TensorShape([]), "image/width": tf.TensorShape([]), "image/char/count": tf.TensorShape([]), "image/line/count": tf.TensorShape([]), "image/mask/char": tf.TensorShape([ tf.Dimension(self.pad_shape[0]), tf.Dimension(self.pad_shape[1]), 1 ]), "image/mask/line": tf.TensorShape([ tf.Dimension(self.pad_shape[0]), tf.Dimension(self.pad_shape[1]), 1 ]), "image/seq/char/id": tf.TensorShape([None]), "image/seq/char/id_sparse": tf.TensorShape([None]), "image/seq/char/bbox": tf.TensorShape([None, 4]), "image/seq/line/bbox": tf.TensorShape([None, 4]), }
def __init__(self, num_cells, input_dim, seq_length, name, activation=tf.nn.tanh, dynamic=False, bidirectional=False): """ Parameters ---------- num_cells : int Number of neurons in the layer. input_dim : int Dimensionality of the input vectors, e.t. number of features. Dimensionality example: [batch_size, seq_length, num_features(this is input_dim in this case)]. seq_length : int Max length of the input sequences. activation : tensorflow function Activation function of the layer. dynamic : boolean Influences whether the layer will be working as dynamic RNN or static. The difference between static and dynamic is that in case of static TensorFlow builds static graph and the RNN will always go through each time step in the sequence. In case of dynamic TensorFlow will be creating RNN `in a while loop`, that is to say that using dynamic RNN you can pass sequences of variable length, but you have to provide list of sequences' lengthes. Currently API for using dynamic RNNs is not provided. WARNING! THIS PARAMETER DOESN'T PLAY ANY ROLE IF YOU'RE GONNA STACK RNN LAYERS. bidirectional : boolean Influences whether the layer will be bidirectional. WARNING! THIS PARAMETER DOES NOT PLAY ANY ROLE IF YOU ARE GOING TO STACK RNN LAYERS. """ self._num_cells = num_cells self._input_dim = input_dim self._f = activation cell = GRUCell(num_units=num_cells, activation=activation, dtype=tf.float32) cell.build(input_shape=[None, tf.Dimension(self._input_dim)]) params = cell.variables param_common_name = name + f'_{num_cells}_{input_dim}_{seq_length}' named_params_dict = {(param_common_name + '_' + str(i)): param for i, param in enumerate(params)} super().__init__( cells=cell, params=params, named_params_dict=named_params_dict, name=name, seq_length=seq_length, dynamic=dynamic, bidirectional=bidirectional )
def flatten(self, keep_prob=1): """ Flattens 4D Tensor (from Conv Layer) into 2D Tensor (to FC Layer) :param keep_prob: int. set to 1 for no dropout """ self.count['flat'] += 1 scope = 'flat_' + str(self.count['flat']) with tf.variable_scope(scope): # Reshape function input_nodes = tf.Dimension(self.input.get_shape()[1] * self.input.get_shape()[2] * self.input.get_shape()[3]) output_shape = tf.stack([-1, input_nodes]) self.input = tf.reshape(self.input, output_shape) # Dropout function if keep_prob != 1: self.input = tf.nn.dropout(self.input, keep_prob=keep_prob) self.print_log(scope + ' output: ' + str(self.input.get_shape()))
def get_inference_input(inputs, params): if params.generate_samples: batch_size = params.sample_batch_size else: batch_size = params.decode_batch_size with tf.device("/cpu:0"): dataset = tf.data.Dataset.from_tensor_slices( tf.constant(inputs) ) # Split string dataset = dataset.map(lambda x: tf.string_split([x]).values, num_parallel_calls=params.num_threads) # Append <eos> dataset = dataset.map( lambda x: tf.concat([x, [tf.constant(params.eos)]], axis=0), num_parallel_calls=params.num_threads ) # Convert tuple to dictionary dataset = dataset.map( lambda x: {"source": x, "source_length": tf.shape(x)[0]}, num_parallel_calls=params.num_threads ) dataset = dataset.padded_batch( batch_size * len(params.device_list), {"source": [tf.Dimension(None)], "source_length": []}, {"source": params.pad, "source_length": 0} ) iterator = dataset.make_one_shot_iterator() features = iterator.get_next() src_table = tf.contrib.lookup.index_table_from_tensor( tf.constant(params.vocabulary["source"]), default_value=params.mapping["source"][params.unk] ) features["source"] = src_table.lookup(features["source"]) return features
def build(self, input_shape): super(KWinners2d, self).build(input_shape=input_shape) self.channels = input_shape[self.channel_axis] duty_cycles_shape = [tf.Dimension(1)] * 4 duty_cycles_shape[self.channel_axis] = self.channels self.duty_cycles = self.add_variable( name="duty_cycles", shape=duty_cycles_shape, initializer=tf.zeros_initializer, trainable=False, ) self.n = int(np.prod(input_shape[1:])) self.k = int(round(self.n * self.percent_on)) self.k_inference = int(round(self.k * self.k_inference_factor)) shape = input_shape.as_list() del shape[self.channel_axis] # Remove channel dim del shape[0] # Remove batch dim self.scale_factor = float(np.prod(shape))
def _init_inception(self, model_dir, data_url): if not os.path.exists(model_dir): os.makedirs(model_dir) filename = data_url.split('/')[-1] filepath = os.path.join(model_dir, filename) if not os.path.exists(filepath): def _progress(count, block_size, total_size): sys.stdout.write('\r>> Downloading %s %.1f%%' % ( filename, float(count * block_size) / float(total_size) * 100.0)) sys.stdout.flush() filepath, _ = urllib.request.urlretrieve(data_url, filepath, _progress) print() statinfo = os.stat(filepath) print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.') tarfile.open(filepath, 'r:gz').extractall(model_dir) with tf.gfile.FastGFile(os.path.join( model_dir, 'classify_image_graph_def.pb'), 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') time.sleep(1.) # Works with an arbitrary minibatch size. with tf.Session() as sess: pool3 = sess.graph.get_tensor_by_name('pool_3:0') ops = pool3.graph.get_operations() for op_idx, op in enumerate(ops): for o in op.outputs: shape = o.get_shape() shape = [s.value for s in shape] for j, s in enumerate(shape): if s == 1 and j == 0: o.shape.dims[0] = tf.Dimension(None) w = sess.graph.get_operation_by_name("softmax/logits/MatMul").inputs[1] print('w shape: ', w.get_shape().as_list()) logits = tf.matmul(tf.squeeze(pool3, axis=[1, 2]), w) self.softmax = tf.nn.softmax(logits)
def get_inference_input(inputs, vocabulary, decode_batch_size=32, num_cpu_threads=2, eos="<EOS>", unkId=1): dataset = tf.data.Dataset.from_tensor_slices(tf.constant(inputs)) # Split string dataset = dataset.map(lambda x: tf.string_split([x]).values, num_parallel_calls=num_cpu_threads) # Append <EOS> dataset = dataset.map(lambda x: tf.concat([x, [tf.constant(eos)]], axis=0), num_parallel_calls=num_cpu_threads) # Convert tuple to dictionary dataset = dataset.map(lambda x: { "source": x, "source_length": tf.shape(x)[0] }, num_parallel_calls=num_cpu_threads) dataset = dataset.padded_batch(decode_batch_size, { "source": [tf.Dimension(None)], "source_length": [] }, { "source": eos, "source_length": 0 }) iterator = dataset.make_one_shot_iterator() features = iterator.get_next() src_table = tf.contrib.lookup.index_table_from_tensor( tf.constant(vocabulary), default_value=unkId) features["source"] = src_table.lookup(features["source"]) return features
def train_input_fn(): sql = text( 'select captions.image_id, vectors.vec from captions inner join vectors on captions.text_id=vectors.id' ) query = session.query(Caption.image_id, Vector.vec).from_statement(sql) def generate_example_pair(): for example, vec in query: example = str(images_path / str(example)) vec = np.frombuffer(vec, dtype=np.float32) yield example, vec string_shape = tf.TensorShape([]) vector_shape = tf.TensorShape(tf.Dimension(VEC_SPACE_DIMENSIONS)) dataset = tf.data.Dataset.from_generator(generate_example_pair, (tf.string, tf.float32), (string_shape, vector_shape)) def decode_and_resize(fd, vecs): out = tf.read_file(fd) # In TensorFlow, "decode_jpeg" can decode PNG files too. # "decode_image" will not work at all, because it doesn't return the # tensor's shape. out = tf.image.decode_jpeg(out, channels=3, try_recover_truncated=True) out = tf.image.resize_images(out, [height, width], align_corners=True) return {'x': out}, vecs dataset = dataset.map(decode_and_resize) dataset = dataset.batch(IMG_BATCH_SIZE) dataset = dataset.repeat(IMG_EPOCHS) features, labels = dataset.make_one_shot_iterator().get_next() return features, labels
def tokenize_op(x): def tokenize(y): # Split the string into single-character lists (using the 'list' constructor) # and call the texts_to_matrix method. matrix = self.tokenizer.texts_to_matrix( list(y.decode('utf-8'))) # Convert to float type return matrix.astype(np.float32) # Wrap a call to the tokenize function with a float32 result out = tf.py_func( tokenize, # Target function [x], # Arguments ( tf.float32 ), # Return type (must be specified in advance because the function is called on demand) False # Whether this operation is stateful ) # Add some shape information on the output for the tensorflow shape inference engine out.set_shape([tf.Dimension(None), vocab_size]) return out
def infer_dataset_fn(src_vocab_path, max_len, batch_size): tf_vocab_src = get_vocab(src_vocab_path) input_src = tf.placeholder(shape=(1, None), dtype=tf.string) dataset_src = tf.data.Dataset.from_tensor_slices(input_src) dataset_src = dataset_src.map(lambda src: tf.concat([src, ["</s>"]], axis=0)) dataset_src = dataset_src.map(lambda src: tf_vocab_src.lookup(src)) dataset_src = dataset_src.map(lambda src: tf.to_int32(src)) dataset_src = dataset_src.map(lambda src: {"src_input_idx": src, "src_len": tf.shape(src)[0]}) dataset_src = dataset_src.padded_batch( batch_size, { "src_input_idx": [tf.Dimension(None)], "src_len": [] }, { "src_input_idx": 0, "src_len": 0 } ) return dataset_src, input_src
def fc_tensor(x, c, name='fc_tensor'): inshape = list(x.get_shape()[1:]) num_units_out = c['fc_units_out'] inshape.append(tf.Dimension(num_units_out)) #weights_initializer = tf.truncated_normal_initializer( #stddev=FC_WEIGHT_STDDEV) weights_initializer = tf.contrib.layers.xavier_initializer(uniform=True) weights = _get_variable('weights', shape=inshape, initializer=weights_initializer, weight_decay=FC_WEIGHT_STDDEV) biases = _get_variable('biases', shape=[num_units_out], initializer=tf.zeros_initializer) axes_a = list(np.arange(1, len(inshape))) axes_b = list(np.arange(0, len(inshape) - 1)) x = tf.add(tf.tensordot(x, weights, axes=[axes_a, axes_b]), biases, name=name) return x