Search is not available for this dataset
text
stringlengths
75
104k
def zero_state(self, batch_size, dtype=LayersConfig.tf_dtype): """Return zero-filled state tensor(s). Args: batch_size: int, float, or unit Tensor representing the batch size. Returns: tensor of shape '[batch_size x shape[0] x shape[1] x num_features] filled with zeros """ shape = self.shape num_features = self.num_features # TODO : TypeError: 'NoneType' object is not subscriptable zeros = tf.zeros([batch_size, shape[0], shape[1], num_features * 2], dtype=dtype) return zeros
def state_size(self): """State size of the LSTMStateTuple.""" return (LSTMStateTuple(self._num_units, self._num_units) if self._state_is_tuple else 2 * self._num_units)
def _to_bc_h_w(self, x, x_shape): """(b, h, w, c) -> (b*c, h, w)""" x = tf.transpose(x, [0, 3, 1, 2]) x = tf.reshape(x, (-1, x_shape[1], x_shape[2])) return x
def _to_b_h_w_n_c(self, x, x_shape): """(b*c, h, w, n) -> (b, h, w, n, c)""" x = tf.reshape(x, (-1, x_shape[4], x_shape[1], x_shape[2], x_shape[3])) x = tf.transpose(x, [0, 2, 3, 4, 1]) return x
def _tf_repeat(self, a, repeats): """Tensorflow version of np.repeat for 1D""" # https://github.com/tensorflow/tensorflow/issues/8521 if len(a.get_shape()) != 1: raise AssertionError("This is not a 1D Tensor") a = tf.expand_dims(a, -1) a = tf.tile(a, [1, repeats]) a = self.tf_flatten(a) return a
def _tf_batch_map_coordinates(self, inputs, coords): """Batch version of tf_map_coordinates Only supports 2D feature maps Parameters ---------- inputs : ``tf.Tensor`` shape = (b*c, h, w) coords : ``tf.Tensor`` shape = (b*c, h, w, n, 2) Returns ------- ``tf.Tensor`` A Tensor with the shape as (b*c, h, w, n) """ input_shape = inputs.get_shape() coords_shape = coords.get_shape() batch_channel = tf.shape(inputs)[0] input_h = int(input_shape[1]) input_w = int(input_shape[2]) kernel_n = int(coords_shape[3]) n_coords = input_h * input_w * kernel_n coords_lt = tf.cast(tf.floor(coords), 'int32') coords_rb = tf.cast(tf.ceil(coords), 'int32') coords_lb = tf.stack([coords_lt[:, :, :, :, 0], coords_rb[:, :, :, :, 1]], axis=-1) coords_rt = tf.stack([coords_rb[:, :, :, :, 0], coords_lt[:, :, :, :, 1]], axis=-1) idx = self._tf_repeat(tf.range(batch_channel), n_coords) vals_lt = self._get_vals_by_coords(inputs, coords_lt, idx, (batch_channel, input_h, input_w, kernel_n)) vals_rb = self._get_vals_by_coords(inputs, coords_rb, idx, (batch_channel, input_h, input_w, kernel_n)) vals_lb = self._get_vals_by_coords(inputs, coords_lb, idx, (batch_channel, input_h, input_w, kernel_n)) vals_rt = self._get_vals_by_coords(inputs, coords_rt, idx, (batch_channel, input_h, input_w, kernel_n)) coords_offset_lt = coords - tf.cast(coords_lt, 'float32') vals_t = vals_lt + (vals_rt - vals_lt) * coords_offset_lt[:, :, :, :, 0] vals_b = vals_lb + (vals_rb - vals_lb) * coords_offset_lt[:, :, :, :, 0] mapped_vals = vals_t + (vals_b - vals_t) * coords_offset_lt[:, :, :, :, 1] return mapped_vals
def _tf_batch_map_offsets(self, inputs, offsets, grid_offset): """Batch map offsets into input Parameters ------------ inputs : ``tf.Tensor`` shape = (b, h, w, c) offsets: ``tf.Tensor`` shape = (b, h, w, 2*n) grid_offset: `tf.Tensor`` Offset grids shape = (h, w, n, 2) Returns ------- ``tf.Tensor`` A Tensor with the shape as (b, h, w, c) """ input_shape = inputs.get_shape() batch_size = tf.shape(inputs)[0] kernel_n = int(int(offsets.get_shape()[3]) / 2) input_h = input_shape[1] input_w = input_shape[2] channel = input_shape[3] # inputs (b, h, w, c) --> (b*c, h, w) inputs = self._to_bc_h_w(inputs, input_shape) # offsets (b, h, w, 2*n) --> (b, h, w, n, 2) offsets = tf.reshape(offsets, (batch_size, input_h, input_w, kernel_n, 2)) # offsets (b, h, w, n, 2) --> (b*c, h, w, n, 2) # offsets = tf.tile(offsets, [channel, 1, 1, 1, 1]) coords = tf.expand_dims(grid_offset, 0) # grid_offset --> (1, h, w, n, 2) coords = tf.tile(coords, [batch_size, 1, 1, 1, 1]) + offsets # grid_offset --> (b, h, w, n, 2) # clip out of bound coords = tf.stack( [ tf.clip_by_value(coords[:, :, :, :, 0], 0.0, tf.cast(input_h - 1, 'float32')), tf.clip_by_value(coords[:, :, :, :, 1], 0.0, tf.cast(input_w - 1, 'float32')) ], axis=-1 ) coords = tf.tile(coords, [channel, 1, 1, 1, 1]) mapped_vals = self._tf_batch_map_coordinates(inputs, coords) # (b*c, h, w, n) --> (b, h, w, n, c) mapped_vals = self._to_b_h_w_n_c(mapped_vals, [batch_size, input_h, input_w, kernel_n, channel]) return mapped_vals
def minibatches(inputs=None, targets=None, batch_size=None, allow_dynamic_batch_size=False, shuffle=False): """Generate a generator that input a group of example in numpy.array and their labels, return the examples and labels by the given batch size. Parameters ---------- inputs : numpy.array The input features, every row is a example. targets : numpy.array The labels of inputs, every row is a example. batch_size : int The batch size. allow_dynamic_batch_size: boolean Allow the use of the last data batch in case the number of examples is not a multiple of batch_size, this may result in unexpected behaviour if other functions expect a fixed-sized batch-size. shuffle : boolean Indicating whether to use a shuffling queue, shuffle the dataset before return. Examples -------- >>> X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']]) >>> y = np.asarray([0,1,2,3,4,5]) >>> for batch in tl.iterate.minibatches(inputs=X, targets=y, batch_size=2, shuffle=False): >>> print(batch) (array([['a', 'a'], ['b', 'b']], dtype='<U1'), array([0, 1])) (array([['c', 'c'], ['d', 'd']], dtype='<U1'), array([2, 3])) (array([['e', 'e'], ['f', 'f']], dtype='<U1'), array([4, 5])) Notes ----- If you have two inputs and one label and want to shuffle them together, e.g. X1 (1000, 100), X2 (1000, 80) and Y (1000, 1), you can stack them together (`np.hstack((X1, X2))`) into (1000, 180) and feed to ``inputs``. After getting a batch, you can split it back into X1 and X2. """ if len(inputs) != len(targets): raise AssertionError("The length of inputs and targets should be equal") if shuffle: indices = np.arange(len(inputs)) np.random.shuffle(indices) # for start_idx in range(0, len(inputs) - batch_size + 1, batch_size): # chulei: handling the case where the number of samples is not a multiple of batch_size, avoiding wasting samples for start_idx in range(0, len(inputs), batch_size): end_idx = start_idx + batch_size if end_idx > len(inputs): if allow_dynamic_batch_size: end_idx = len(inputs) else: break if shuffle: excerpt = indices[start_idx:end_idx] else: excerpt = slice(start_idx, end_idx) if (isinstance(inputs, list) or isinstance(targets, list)) and (shuffle ==True): # zsdonghao: for list indexing when shuffle==True yield [inputs[i] for i in excerpt], [targets[i] for i in excerpt] else: yield inputs[excerpt], targets[excerpt]
def seq_minibatches(inputs, targets, batch_size, seq_length, stride=1): """Generate a generator that return a batch of sequence inputs and targets. If `batch_size=100` and `seq_length=5`, one return will have 500 rows (examples). Parameters ---------- inputs : numpy.array The input features, every row is a example. targets : numpy.array The labels of inputs, every element is a example. batch_size : int The batch size. seq_length : int The sequence length. stride : int The stride step, default is 1. Examples -------- Synced sequence input and output. >>> X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']]) >>> y = np.asarray([0, 1, 2, 3, 4, 5]) >>> for batch in tl.iterate.seq_minibatches(inputs=X, targets=y, batch_size=2, seq_length=2, stride=1): >>> print(batch) (array([['a', 'a'], ['b', 'b'], ['b', 'b'], ['c', 'c']], dtype='<U1'), array([0, 1, 1, 2])) (array([['c', 'c'], ['d', 'd'], ['d', 'd'], ['e', 'e']], dtype='<U1'), array([2, 3, 3, 4])) Many to One >>> return_last = True >>> num_steps = 2 >>> X = np.asarray([['a','a'], ['b','b'], ['c','c'], ['d','d'], ['e','e'], ['f','f']]) >>> Y = np.asarray([0,1,2,3,4,5]) >>> for batch in tl.iterate.seq_minibatches(inputs=X, targets=Y, batch_size=2, seq_length=num_steps, stride=1): >>> x, y = batch >>> if return_last: >>> tmp_y = y.reshape((-1, num_steps) + y.shape[1:]) >>> y = tmp_y[:, -1] >>> print(x, y) [['a' 'a'] ['b' 'b'] ['b' 'b'] ['c' 'c']] [1 2] [['c' 'c'] ['d' 'd'] ['d' 'd'] ['e' 'e']] [3 4] """ if len(inputs) != len(targets): raise AssertionError("The length of inputs and targets should be equal") n_loads = (batch_size * stride) + (seq_length - stride) for start_idx in range(0, len(inputs) - n_loads + 1, (batch_size * stride)): seq_inputs = np.zeros((batch_size, seq_length) + inputs.shape[1:], dtype=inputs.dtype) seq_targets = np.zeros((batch_size, seq_length) + targets.shape[1:], dtype=targets.dtype) for b_idx in xrange(batch_size): start_seq_idx = start_idx + (b_idx * stride) end_seq_idx = start_seq_idx + seq_length seq_inputs[b_idx] = inputs[start_seq_idx:end_seq_idx] seq_targets[b_idx] = targets[start_seq_idx:end_seq_idx] flatten_inputs = seq_inputs.reshape((-1, ) + inputs.shape[1:]) flatten_targets = seq_targets.reshape((-1, ) + targets.shape[1:]) yield flatten_inputs, flatten_targets
def seq_minibatches2(inputs, targets, batch_size, num_steps): """Generate a generator that iterates on two list of words. Yields (Returns) the source contexts and the target context by the given batch_size and num_steps (sequence_length). In TensorFlow's tutorial, this generates the `batch_size` pointers into the raw PTB data, and allows minibatch iteration along these pointers. Parameters ---------- inputs : list of data The context in list format; note that context usually be represented by splitting by space, and then convert to unique word IDs. targets : list of data The context in list format; note that context usually be represented by splitting by space, and then convert to unique word IDs. batch_size : int The batch size. num_steps : int The number of unrolls. i.e. sequence length Yields ------ Pairs of the batched data, each a matrix of shape [batch_size, num_steps]. Raises ------ ValueError : if batch_size or num_steps are too high. Examples -------- >>> X = [i for i in range(20)] >>> Y = [i for i in range(20,40)] >>> for batch in tl.iterate.seq_minibatches2(X, Y, batch_size=2, num_steps=3): ... x, y = batch ... print(x, y) [[ 0. 1. 2.] [ 10. 11. 12.]] [[ 20. 21. 22.] [ 30. 31. 32.]] [[ 3. 4. 5.] [ 13. 14. 15.]] [[ 23. 24. 25.] [ 33. 34. 35.]] [[ 6. 7. 8.] [ 16. 17. 18.]] [[ 26. 27. 28.] [ 36. 37. 38.]] Notes ----- - Hint, if the input data are images, you can modify the source code `data = np.zeros([batch_size, batch_len)` to `data = np.zeros([batch_size, batch_len, inputs.shape[1], inputs.shape[2], inputs.shape[3]])`. """ if len(inputs) != len(targets): raise AssertionError("The length of inputs and targets should be equal") data_len = len(inputs) batch_len = data_len // batch_size # data = np.zeros([batch_size, batch_len]) data = np.zeros((batch_size, batch_len) + inputs.shape[1:], dtype=inputs.dtype) data2 = np.zeros([batch_size, batch_len]) for i in range(batch_size): data[i] = inputs[batch_len * i:batch_len * (i + 1)] data2[i] = targets[batch_len * i:batch_len * (i + 1)] epoch_size = (batch_len - 1) // num_steps if epoch_size == 0: raise ValueError("epoch_size == 0, decrease batch_size or num_steps") for i in range(epoch_size): x = data[:, i * num_steps:(i + 1) * num_steps] x2 = data2[:, i * num_steps:(i + 1) * num_steps] yield (x, x2)
def ptb_iterator(raw_data, batch_size, num_steps): """Generate a generator that iterates on a list of words, see `PTB example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_ptb_lstm_state_is_tuple.py>`__. Yields the source contexts and the target context by the given batch_size and num_steps (sequence_length). In TensorFlow's tutorial, this generates `batch_size` pointers into the raw PTB data, and allows minibatch iteration along these pointers. Parameters ---------- raw_data : a list the context in list format; note that context usually be represented by splitting by space, and then convert to unique word IDs. batch_size : int the batch size. num_steps : int the number of unrolls. i.e. sequence_length Yields ------ Pairs of the batched data, each a matrix of shape [batch_size, num_steps]. The second element of the tuple is the same data time-shifted to the right by one. Raises ------ ValueError : if batch_size or num_steps are too high. Examples -------- >>> train_data = [i for i in range(20)] >>> for batch in tl.iterate.ptb_iterator(train_data, batch_size=2, num_steps=3): >>> x, y = batch >>> print(x, y) [[ 0 1 2] <---x 1st subset/ iteration [10 11 12]] [[ 1 2 3] <---y [11 12 13]] [[ 3 4 5] <--- 1st batch input 2nd subset/ iteration [13 14 15]] <--- 2nd batch input [[ 4 5 6] <--- 1st batch target [14 15 16]] <--- 2nd batch target [[ 6 7 8] 3rd subset/ iteration [16 17 18]] [[ 7 8 9] [17 18 19]] """ raw_data = np.array(raw_data, dtype=np.int32) data_len = len(raw_data) batch_len = data_len // batch_size data = np.zeros([batch_size, batch_len], dtype=np.int32) for i in range(batch_size): data[i] = raw_data[batch_len * i:batch_len * (i + 1)] epoch_size = (batch_len - 1) // num_steps if epoch_size == 0: raise ValueError("epoch_size == 0, decrease batch_size or num_steps") for i in range(epoch_size): x = data[:, i * num_steps:(i + 1) * num_steps] y = data[:, i * num_steps + 1:(i + 1) * num_steps + 1] yield (x, y)
def deconv2d_bilinear_upsampling_initializer(shape): """Returns the initializer that can be passed to DeConv2dLayer for initializing the weights in correspondence to channel-wise bilinear up-sampling. Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211) Parameters ---------- shape : tuple of int The shape of the filters, [height, width, output_channels, in_channels]. It must match the shape passed to DeConv2dLayer. Returns ------- ``tf.constant_initializer`` A constant initializer with weights set to correspond to per channel bilinear upsampling when passed as W_int in DeConv2dLayer Examples -------- - Upsampling by a factor of 2, ie e.g 100->200 >>> import tensorflow as tf >>> import tensorlayer as tl >>> rescale_factor = 2 >>> imsize = 128 >>> num_channels = 3 >>> filter_shape = (5, 5) >>> filter_size = (2 * rescale_factor - rescale_factor % 2) #Corresponding bilinear filter size >>> num_in_channels = 3 >>> num_out_channels = 3 >>> deconv_filter_shape = (filter_size, filter_size, num_out_channels, num_in_channels) >>> x = tf.placeholder(tf.float32, (1, imsize, imsize, num_channels)) >>> net = tl.layers.InputLayer(x, name='input_layer') >>> bilinear_init = deconv2d_bilinear_upsampling_initializer(shape=filter_shape) >>> net = tl.layers.DeConv2dLayer(net, ... shape=filter_shape, ... output_shape=(1, imsize*rescale_factor, imsize*rescale_factor, num_out_channels), ... strides=(1, rescale_factor, rescale_factor, 1), ... W_init=bilinear_init, ... padding='SAME', ... act=None, name='g/h1/decon2d') """ if shape[0] != shape[1]: raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes') if shape[3] < shape[2]: raise Exception( 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels ' ) filter_size = shape[0] num_out_channels = shape[2] num_in_channels = shape[3] # Create bilinear filter kernel as numpy array bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32) scale_factor = (filter_size + 1) // 2 if filter_size % 2 == 1: center = scale_factor - 1 else: center = scale_factor - 0.5 for x in range(filter_size): for y in range(filter_size): bilinear_kernel[x, y] = (1 - abs(x - center) / scale_factor) * (1 - abs(y - center) / scale_factor) weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels)) for i in range(num_out_channels): weights[:, :, i, i] = bilinear_kernel # assign numpy array to constant_initalizer and pass to get_variable return tf.constant_initializer(value=weights, dtype=LayersConfig.tf_dtype)
def save_model(self, network=None, model_name='model', **kwargs): """Save model architecture and parameters into database, timestamp will be added automatically. Parameters ---------- network : TensorLayer layer TensorLayer layer instance. model_name : str The name/key of model. kwargs : other events Other events, such as name, accuracy, loss, step number and etc (optinal). Examples --------- Save model architecture and parameters into database. >>> db.save_model(net, accuracy=0.8, loss=2.3, name='second_model') Load one model with parameters from database (run this in other script) >>> net = db.find_top_model(sess=sess, accuracy=0.8, loss=2.3) Find and load the latest model. >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.DESCENDING)]) >>> net = db.find_top_model(sess=sess, sort=[("time", -1)]) Find and load the oldest model. >>> net = db.find_top_model(sess=sess, sort=[("time", pymongo.ASCENDING)]) >>> net = db.find_top_model(sess=sess, sort=[("time", 1)]) Get model information >>> net._accuracy ... 0.8 Returns --------- boolean : True for success, False for fail. """ kwargs.update({'model_name': model_name}) self._fill_project_info(kwargs) # put project_name into kwargs params = network.get_all_params() s = time.time() kwargs.update({'architecture': network.all_graphs, 'time': datetime.utcnow()}) try: params_id = self.model_fs.put(self._serialization(params)) kwargs.update({'params_id': params_id, 'time': datetime.utcnow()}) self.db.Model.insert_one(kwargs) print("[Database] Save model: SUCCESS, took: {}s".format(round(time.time() - s, 2))) return True except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e)) print("[Database] Save model: FAIL") return False
def find_top_model(self, sess, sort=None, model_name='model', **kwargs): """Finds and returns a model architecture and its parameters from the database which matches the requirement. Parameters ---------- sess : Session TensorFlow session. sort : List of tuple PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details. model_name : str or None The name/key of model. kwargs : other events Other events, such as name, accuracy, loss, step number and etc (optinal). Examples --------- - see ``save_model``. Returns --------- network : TensorLayer layer Note that, the returned network contains all information of the document (record), e.g. if you saved accuracy in the document, you can get the accuracy by using ``net._accuracy``. """ # print(kwargs) # {} kwargs.update({'model_name': model_name}) self._fill_project_info(kwargs) s = time.time() d = self.db.Model.find_one(filter=kwargs, sort=sort) _temp_file_name = '_find_one_model_ztemp_file' if d is not None: params_id = d['params_id'] graphs = d['architecture'] _datetime = d['time'] exists_or_mkdir(_temp_file_name, False) with open(os.path.join(_temp_file_name, 'graph.pkl'), 'wb') as file: pickle.dump(graphs, file, protocol=pickle.HIGHEST_PROTOCOL) else: print("[Database] FAIL! Cannot find model: {}".format(kwargs)) return False try: params = self._deserialization(self.model_fs.get(params_id).read()) np.savez(os.path.join(_temp_file_name, 'params.npz'), params=params) network = load_graph_and_params(name=_temp_file_name, sess=sess) del_folder(_temp_file_name) pc = self.db.Model.find(kwargs) print( "[Database] Find one model SUCCESS. kwargs:{} sort:{} save time:{} took: {}s". format(kwargs, sort, _datetime, round(time.time() - s, 2)) ) # put all informations of model into the TL layer for key in d: network.__dict__.update({"_%s" % key: d[key]}) # check whether more parameters match the requirement params_id_list = pc.distinct('params_id') n_params = len(params_id_list) if n_params != 1: print(" Note that there are {} models match the kwargs".format(n_params)) return network except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e)) return False
def delete_model(self, **kwargs): """Delete model. Parameters ----------- kwargs : logging information Find items to delete, leave it empty to delete all log. """ self._fill_project_info(kwargs) self.db.Model.delete_many(kwargs) logging.info("[Database] Delete Model SUCCESS")
def save_dataset(self, dataset=None, dataset_name=None, **kwargs): """Saves one dataset into database, timestamp will be added automatically. Parameters ---------- dataset : any type The dataset you want to store. dataset_name : str The name of dataset. kwargs : other events Other events, such as description, author and etc (optinal). Examples ---------- Save dataset >>> db.save_dataset([X_train, y_train, X_test, y_test], 'mnist', description='this is a tutorial') Get dataset >>> dataset = db.find_top_dataset('mnist') Returns --------- boolean : Return True if save success, otherwise, return False. """ self._fill_project_info(kwargs) if dataset_name is None: raise Exception("dataset_name is None, please give a dataset name") kwargs.update({'dataset_name': dataset_name}) s = time.time() try: dataset_id = self.dataset_fs.put(self._serialization(dataset)) kwargs.update({'dataset_id': dataset_id, 'time': datetime.utcnow()}) self.db.Dataset.insert_one(kwargs) # print("[Database] Save params: {} SUCCESS, took: {}s".format(file_name, round(time.time()-s, 2))) print("[Database] Save dataset: SUCCESS, took: {}s".format(round(time.time() - s, 2))) return True except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e)) print("[Database] Save dataset: FAIL") return False
def find_top_dataset(self, dataset_name=None, sort=None, **kwargs): """Finds and returns a dataset from the database which matches the requirement. Parameters ---------- dataset_name : str The name of dataset. sort : List of tuple PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details. kwargs : other events Other events, such as description, author and etc (optinal). Examples --------- Save dataset >>> db.save_dataset([X_train, y_train, X_test, y_test], 'mnist', description='this is a tutorial') Get dataset >>> dataset = db.find_top_dataset('mnist') >>> datasets = db.find_datasets('mnist') Returns -------- dataset : the dataset or False Return False if nothing found. """ self._fill_project_info(kwargs) if dataset_name is None: raise Exception("dataset_name is None, please give a dataset name") kwargs.update({'dataset_name': dataset_name}) s = time.time() d = self.db.Dataset.find_one(filter=kwargs, sort=sort) if d is not None: dataset_id = d['dataset_id'] else: print("[Database] FAIL! Cannot find dataset: {}".format(kwargs)) return False try: dataset = self._deserialization(self.dataset_fs.get(dataset_id).read()) pc = self.db.Dataset.find(kwargs) print("[Database] Find one dataset SUCCESS, {} took: {}s".format(kwargs, round(time.time() - s, 2))) # check whether more datasets match the requirement dataset_id_list = pc.distinct('dataset_id') n_dataset = len(dataset_id_list) if n_dataset != 1: print(" Note that there are {} datasets match the requirement".format(n_dataset)) return dataset except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e)) return False
def find_datasets(self, dataset_name=None, **kwargs): """Finds and returns all datasets from the database which matches the requirement. In some case, the data in a dataset can be stored separately for better management. Parameters ---------- dataset_name : str The name/key of dataset. kwargs : other events Other events, such as description, author and etc (optional). Returns -------- params : the parameters, return False if nothing found. """ self._fill_project_info(kwargs) if dataset_name is None: raise Exception("dataset_name is None, please give a dataset name") kwargs.update({'dataset_name': dataset_name}) s = time.time() pc = self.db.Dataset.find(kwargs) if pc is not None: dataset_id_list = pc.distinct('dataset_id') dataset_list = [] for dataset_id in dataset_id_list: # you may have multiple Buckets files tmp = self.dataset_fs.get(dataset_id).read() dataset_list.append(self._deserialization(tmp)) else: print("[Database] FAIL! Cannot find any dataset: {}".format(kwargs)) return False print("[Database] Find {} datasets SUCCESS, took: {}s".format(len(dataset_list), round(time.time() - s, 2))) return dataset_list
def delete_datasets(self, **kwargs): """Delete datasets. Parameters ----------- kwargs : logging information Find items to delete, leave it empty to delete all log. """ self._fill_project_info(kwargs) self.db.Dataset.delete_many(kwargs) logging.info("[Database] Delete Dataset SUCCESS")
def save_training_log(self, **kwargs): """Saves the training log, timestamp will be added automatically. Parameters ----------- kwargs : logging information Events, such as accuracy, loss, step number and etc. Examples --------- >>> db.save_training_log(accuracy=0.33, loss=0.98) """ self._fill_project_info(kwargs) kwargs.update({'time': datetime.utcnow()}) _result = self.db.TrainLog.insert_one(kwargs) _log = self._print_dict(kwargs) logging.info("[Database] train log: " + _log)
def save_validation_log(self, **kwargs): """Saves the validation log, timestamp will be added automatically. Parameters ----------- kwargs : logging information Events, such as accuracy, loss, step number and etc. Examples --------- >>> db.save_validation_log(accuracy=0.33, loss=0.98) """ self._fill_project_info(kwargs) kwargs.update({'time': datetime.utcnow()}) _result = self.db.ValidLog.insert_one(kwargs) _log = self._print_dict(kwargs) logging.info("[Database] valid log: " + _log)
def delete_training_log(self, **kwargs): """Deletes training log. Parameters ----------- kwargs : logging information Find items to delete, leave it empty to delete all log. Examples --------- Save training log >>> db.save_training_log(accuracy=0.33) >>> db.save_training_log(accuracy=0.44) Delete logs that match the requirement >>> db.delete_training_log(accuracy=0.33) Delete all logs >>> db.delete_training_log() """ self._fill_project_info(kwargs) self.db.TrainLog.delete_many(kwargs) logging.info("[Database] Delete TrainLog SUCCESS")
def delete_validation_log(self, **kwargs): """Deletes validation log. Parameters ----------- kwargs : logging information Find items to delete, leave it empty to delete all log. Examples --------- - see ``save_training_log``. """ self._fill_project_info(kwargs) self.db.ValidLog.delete_many(kwargs) logging.info("[Database] Delete ValidLog SUCCESS")
def create_task(self, task_name=None, script=None, hyper_parameters=None, saved_result_keys=None, **kwargs): """Uploads a task to the database, timestamp will be added automatically. Parameters ----------- task_name : str The task name. script : str File name of the python script. hyper_parameters : dictionary The hyper parameters pass into the script. saved_result_keys : list of str The keys of the task results to keep in the database when the task finishes. kwargs : other parameters Users customized parameters such as description, version number. Examples ----------- Uploads a task >>> db.create_task(task_name='mnist', script='example/tutorial_mnist_simple.py', description='simple tutorial') Finds and runs the latest task >>> db.run_top_task(sess=sess, sort=[("time", pymongo.DESCENDING)]) >>> db.run_top_task(sess=sess, sort=[("time", -1)]) Finds and runs the oldest task >>> db.run_top_task(sess=sess, sort=[("time", pymongo.ASCENDING)]) >>> db.run_top_task(sess=sess, sort=[("time", 1)]) """ if not isinstance(task_name, str): # is None: raise Exception("task_name should be string") if not isinstance(script, str): # is None: raise Exception("script should be string") if hyper_parameters is None: hyper_parameters = {} if saved_result_keys is None: saved_result_keys = [] self._fill_project_info(kwargs) kwargs.update({'time': datetime.utcnow()}) kwargs.update({'hyper_parameters': hyper_parameters}) kwargs.update({'saved_result_keys': saved_result_keys}) _script = open(script, 'rb').read() kwargs.update({'status': 'pending', 'script': _script, 'result': {}}) self.db.Task.insert_one(kwargs) logging.info("[Database] Saved Task - task_name: {} script: {}".format(task_name, script))
def run_top_task(self, task_name=None, sort=None, **kwargs): """Finds and runs a pending task that in the first of the sorting list. Parameters ----------- task_name : str The task name. sort : List of tuple PyMongo sort comment, search "PyMongo find one sorting" and `collection level operations <http://api.mongodb.com/python/current/api/pymongo/collection.html>`__ for more details. kwargs : other parameters Users customized parameters such as description, version number. Examples --------- Monitors the database and pull tasks to run >>> while True: >>> print("waiting task from distributor") >>> db.run_top_task(task_name='mnist', sort=[("time", -1)]) >>> time.sleep(1) Returns -------- boolean : True for success, False for fail. """ if not isinstance(task_name, str): # is None: raise Exception("task_name should be string") self._fill_project_info(kwargs) kwargs.update({'status': 'pending'}) # find task and set status to running task = self.db.Task.find_one_and_update(kwargs, {'$set': {'status': 'running'}}, sort=sort) try: # get task info e.g. hyper parameters, python script if task is None: logging.info("[Database] Find Task FAIL: key: {} sort: {}".format(task_name, sort)) return False else: logging.info("[Database] Find Task SUCCESS: key: {} sort: {}".format(task_name, sort)) _datetime = task['time'] _script = task['script'] _id = task['_id'] _hyper_parameters = task['hyper_parameters'] _saved_result_keys = task['saved_result_keys'] logging.info(" hyper parameters:") for key in _hyper_parameters: globals()[key] = _hyper_parameters[key] logging.info(" {}: {}".format(key, _hyper_parameters[key])) # run task s = time.time() logging.info("[Database] Start Task: key: {} sort: {} push time: {}".format(task_name, sort, _datetime)) _script = _script.decode('utf-8') with tf.Graph().as_default(): # as graph: # clear all TF graphs exec(_script, globals()) # set status to finished _ = self.db.Task.find_one_and_update({'_id': _id}, {'$set': {'status': 'finished'}}) # return results __result = {} for _key in _saved_result_keys: logging.info(" result: {}={} {}".format(_key, globals()[_key], type(globals()[_key]))) __result.update({"%s" % _key: globals()[_key]}) _ = self.db.Task.find_one_and_update( { '_id': _id }, {'$set': { 'result': __result }}, return_document=pymongo.ReturnDocument.AFTER ) logging.info( "[Database] Finished Task: task_name - {} sort: {} push time: {} took: {}s". format(task_name, sort, _datetime, time.time() - s) ) return True except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logging.info("{} {} {} {} {}".format(exc_type, exc_obj, fname, exc_tb.tb_lineno, e)) logging.info("[Database] Fail to run task") # if fail, set status back to pending _ = self.db.Task.find_one_and_update({'_id': _id}, {'$set': {'status': 'pending'}}) return False
def delete_tasks(self, **kwargs): """Delete tasks. Parameters ----------- kwargs : logging information Find items to delete, leave it empty to delete all log. Examples --------- >>> db.delete_tasks() """ self._fill_project_info(kwargs) self.db.Task.delete_many(kwargs) logging.info("[Database] Delete Task SUCCESS")
def check_unfinished_task(self, task_name=None, **kwargs): """Finds and runs a pending task. Parameters ----------- task_name : str The task name. kwargs : other parameters Users customized parameters such as description, version number. Examples --------- Wait until all tasks finish in user's local console >>> while not db.check_unfinished_task(): >>> time.sleep(1) >>> print("all tasks finished") >>> sess = tf.InteractiveSession() >>> net = db.find_top_model(sess=sess, sort=[("test_accuracy", -1)]) >>> print("the best accuracy {} is from model {}".format(net._test_accuracy, net._name)) Returns -------- boolean : True for success, False for fail. """ if not isinstance(task_name, str): # is None: raise Exception("task_name should be string") self._fill_project_info(kwargs) kwargs.update({'$or': [{'status': 'pending'}, {'status': 'running'}]}) # ## find task # task = self.db.Task.find_one(kwargs) task = self.db.Task.find(kwargs) task_id_list = task.distinct('_id') n_task = len(task_id_list) if n_task == 0: logging.info("[Database] No unfinished task - task_name: {}".format(task_name)) return False else: logging.info("[Database] Find {} unfinished task - task_name: {}".format(n_task, task_name)) return True
def augment_with_ngrams(unigrams, unigram_vocab_size, n_buckets, n=2): """Augment unigram features with hashed n-gram features.""" def get_ngrams(n): return list(zip(*[unigrams[i:] for i in range(n)])) def hash_ngram(ngram): bytes_ = array.array('L', ngram).tobytes() hash_ = int(hashlib.sha256(bytes_).hexdigest(), 16) return unigram_vocab_size + hash_ % n_buckets return unigrams + [hash_ngram(ngram) for i in range(2, n + 1) for ngram in get_ngrams(i)]
def load_and_preprocess_imdb_data(n_gram=None): """Load IMDb data and augment with hashed n-gram features.""" X_train, y_train, X_test, y_test = tl.files.load_imdb_dataset(nb_words=VOCAB_SIZE) if n_gram is not None: X_train = np.array([augment_with_ngrams(x, VOCAB_SIZE, N_BUCKETS, n=n_gram) for x in X_train]) X_test = np.array([augment_with_ngrams(x, VOCAB_SIZE, N_BUCKETS, n=n_gram) for x in X_test]) return X_train, y_train, X_test, y_test
def read_image(image, path=''): """Read one image. Parameters ----------- image : str The image file name. path : str The image folder path. Returns ------- numpy.array The image. """ return imageio.imread(os.path.join(path, image))
def read_images(img_list, path='', n_threads=10, printable=True): """Returns all images in list by given path and name of each image file. Parameters ------------- img_list : list of str The image file names. path : str The image folder path. n_threads : int The number of threads to read image. printable : boolean Whether to print information when reading images. Returns ------- list of numpy.array The images. """ imgs = [] for idx in range(0, len(img_list), n_threads): b_imgs_list = img_list[idx:idx + n_threads] b_imgs = tl.prepro.threading_data(b_imgs_list, fn=read_image, path=path) # tl.logging.info(b_imgs.shape) imgs.extend(b_imgs) if printable: tl.logging.info('read %d from %s' % (len(imgs), path)) return imgs
def save_image(image, image_path='_temp.png'): """Save a image. Parameters ----------- image : numpy array [w, h, c] image_path : str path """ try: # RGB imageio.imwrite(image_path, image) except Exception: # Greyscale imageio.imwrite(image_path, image[:, :, 0])
def save_images(images, size, image_path='_temp.png'): """Save multiple images into one single image. Parameters ----------- images : numpy array (batch, w, h, c) size : list of 2 ints row and column number. number of images should be equal or less than size[0] * size[1] image_path : str save path Examples --------- >>> import numpy as np >>> import tensorlayer as tl >>> images = np.random.rand(64, 100, 100, 3) >>> tl.visualize.save_images(images, [8, 8], 'temp.png') """ if len(images.shape) == 3: # Greyscale [batch, h, w] --> [batch, h, w, 1] images = images[:, :, :, np.newaxis] def merge(images, size): h, w = images.shape[1], images.shape[2] img = np.zeros((h * size[0], w * size[1], 3), dtype=images.dtype) for idx, image in enumerate(images): i = idx % size[1] j = idx // size[1] img[j * h:j * h + h, i * w:i * w + w, :] = image return img def imsave(images, size, path): if np.max(images) <= 1 and (-1 <= np.min(images) < 0): images = ((images + 1) * 127.5).astype(np.uint8) elif np.max(images) <= 1 and np.min(images) >= 0: images = (images * 255).astype(np.uint8) return imageio.imwrite(path, merge(images, size)) if len(images) > size[0] * size[1]: raise AssertionError("number of images should be equal or less than size[0] * size[1] {}".format(len(images))) return imsave(images, size, image_path)
def draw_boxes_and_labels_to_image( image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None ): """Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of ``tl.prepro``. Parameters ----------- image : numpy.array The RGB image [height, width, channel]. classes : list of int A list of class ID (int). coords : list of int A list of list for coordinates. - Should be [x, y, x2, y2] (up-left and botton-right format) - If [x_center, y_center, w, h] (set is_center to True). scores : list of float A list of score (float). (Optional) classes_list : list of str for converting ID to string on image. is_center : boolean Whether the coordinates is [x_center, y_center, w, h] - If coordinates are [x_center, y_center, w, h], set it to True for converting it to [x, y, x2, y2] (up-left and botton-right) internally. - If coordinates are [x1, x2, y1, y2], set it to False. is_rescale : boolean Whether to rescale the coordinates from pixel-unit format to ratio format. - If True, the input coordinates are the portion of width and high, this API will scale the coordinates to pixel unit internally. - If False, feed the coordinates with pixel unit format. save_name : None or str The name of image file (i.e. image.png), if None, not to save image. Returns ------- numpy.array The saved image. References ----------- - OpenCV rectangle and putText. - `scikit-image <http://scikit-image.org/docs/dev/api/skimage.draw.html#skimage.draw.rectangle>`__. """ if len(coords) != len(classes): raise AssertionError("number of coordinates and classes are equal") if len(scores) > 0 and len(scores) != len(classes): raise AssertionError("number of scores and classes are equal") # don't change the original image, and avoid error https://stackoverflow.com/questions/30249053/python-opencv-drawing-errors-after-manipulating-array-with-numpy image = image.copy() imh, imw = image.shape[0:2] thick = int((imh + imw) // 430) for i, _v in enumerate(coords): if is_center: x, y, x2, y2 = tl.prepro.obj_box_coord_centroid_to_upleft_butright(coords[i]) else: x, y, x2, y2 = coords[i] if is_rescale: # scale back to pixel unit if the coords are the portion of width and high x, y, x2, y2 = tl.prepro.obj_box_coord_scale_to_pixelunit([x, y, x2, y2], (imh, imw)) cv2.rectangle( image, (int(x), int(y)), (int(x2), int(y2)), # up-left and botton-right [0, 255, 0], thick ) cv2.putText( image, classes_list[classes[i]] + ((" %.2f" % (scores[i])) if (len(scores) != 0) else " "), (int(x), int(y)), # button left 0, 1.5e-3 * imh, # bigger = larger font [0, 0, 256], # self.meta['colors'][max_indx], int(thick / 2) + 1 ) # bold if save_name is not None: # cv2.imwrite('_my.png', image) save_image(image, save_name) # if len(coords) == 0: # tl.logging.info("draw_boxes_and_labels_to_image: no bboxes exist, cannot draw !") return image
def draw_mpii_pose_to_image(image, poses, save_name='image.png'): """Draw people(s) into image using MPII dataset format as input, return or save the result image. This is an experimental API, can be changed in the future. Parameters ----------- image : numpy.array The RGB image [height, width, channel]. poses : list of dict The people(s) annotation in MPII format, see ``tl.files.load_mpii_pose_dataset``. save_name : None or str The name of image file (i.e. image.png), if None, not to save image. Returns -------- numpy.array The saved image. Examples -------- >>> import pprint >>> import tensorlayer as tl >>> img_train_list, ann_train_list, img_test_list, ann_test_list = tl.files.load_mpii_pose_dataset() >>> image = tl.vis.read_image(img_train_list[0]) >>> tl.vis.draw_mpii_pose_to_image(image, ann_train_list[0], 'image.png') >>> pprint.pprint(ann_train_list[0]) References ----------- - `MPII Keyponts and ID <http://human-pose.mpi-inf.mpg.de/#download>`__ """ # import skimage # don't change the original image, and avoid error https://stackoverflow.com/questions/30249053/python-opencv-drawing-errors-after-manipulating-array-with-numpy image = image.copy() imh, imw = image.shape[0:2] thick = int((imh + imw) // 430) # radius = int(image.shape[1] / 500) + 1 radius = int(thick * 1.5) if image.max() < 1: image = image * 255 for people in poses: # Pose Keyponts joint_pos = people['joint_pos'] # draw sketch # joint id (0 - r ankle, 1 - r knee, 2 - r hip, 3 - l hip, 4 - l knee, # 5 - l ankle, 6 - pelvis, 7 - thorax, 8 - upper neck, # 9 - head top, 10 - r wrist, 11 - r elbow, 12 - r shoulder, # 13 - l shoulder, 14 - l elbow, 15 - l wrist) # # 9 # 8 # 12 ** 7 ** 13 # * * * # 11 * 14 # * * * # 10 2 * 6 * 3 15 # * * # 1 4 # * * # 0 5 lines = [ [(0, 1), [100, 255, 100]], [(1, 2), [50, 255, 50]], [(2, 6), [0, 255, 0]], # right leg [(3, 4), [100, 100, 255]], [(4, 5), [50, 50, 255]], [(6, 3), [0, 0, 255]], # left leg [(6, 7), [255, 255, 100]], [(7, 8), [255, 150, 50]], # body [(8, 9), [255, 200, 100]], # head [(10, 11), [255, 100, 255]], [(11, 12), [255, 50, 255]], [(12, 8), [255, 0, 255]], # right hand [(8, 13), [0, 255, 255]], [(13, 14), [100, 255, 255]], [(14, 15), [200, 255, 255]] # left hand ] for line in lines: start, end = line[0] if (start in joint_pos) and (end in joint_pos): cv2.line( image, (int(joint_pos[start][0]), int(joint_pos[start][1])), (int(joint_pos[end][0]), int(joint_pos[end][1])), # up-left and botton-right line[1], thick ) # rr, cc, val = skimage.draw.line_aa(int(joint_pos[start][1]), int(joint_pos[start][0]), int(joint_pos[end][1]), int(joint_pos[end][0])) # image[rr, cc] = line[1] # draw circles for pos in joint_pos.items(): _, pos_loc = pos # pos_id, pos_loc pos_loc = (int(pos_loc[0]), int(pos_loc[1])) cv2.circle(image, center=pos_loc, radius=radius, color=(200, 200, 200), thickness=-1) # rr, cc = skimage.draw.circle(int(pos_loc[1]), int(pos_loc[0]), radius) # image[rr, cc] = [0, 255, 0] # Head head_rect = people['head_rect'] if head_rect: # if head exists cv2.rectangle( image, (int(head_rect[0]), int(head_rect[1])), (int(head_rect[2]), int(head_rect[3])), # up-left and botton-right [0, 180, 0], thick ) if save_name is not None: # cv2.imwrite(save_name, image) save_image(image, save_name) return image
def frame(I=None, second=5, saveable=True, name='frame', cmap=None, fig_idx=12836): """Display a frame. Make sure OpenAI Gym render() is disable before using it. Parameters ---------- I : numpy.array The image. second : int The display second(s) for the image(s), if saveable is False. saveable : boolean Save or plot the figure. name : str A name to save the image, if saveable is True. cmap : None or str 'gray' for greyscale, None for default, etc. fig_idx : int matplotlib figure index. Examples -------- >>> env = gym.make("Pong-v0") >>> observation = env.reset() >>> tl.visualize.frame(observation) """ import matplotlib.pyplot as plt if saveable is False: plt.ion() plt.figure(fig_idx) # show all feature images if len(I.shape) and I.shape[-1] == 1: # (10,10,1) --> (10,10) I = I[:, :, 0] plt.imshow(I, cmap) plt.title(name) # plt.gca().xaxis.set_major_locator(plt.NullLocator()) # distable tick # plt.gca().yaxis.set_major_locator(plt.NullLocator()) if saveable: plt.savefig(name + '.pdf', format='pdf') else: plt.draw() plt.pause(second)
def CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362): """Display a group of RGB or Greyscale CNN masks. Parameters ---------- CNN : numpy.array The image. e.g: 64 5x5 RGB images can be (5, 5, 3, 64). second : int The display second(s) for the image(s), if saveable is False. saveable : boolean Save or plot the figure. name : str A name to save the image, if saveable is True. fig_idx : int The matplotlib figure index. Examples -------- >>> tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_mnist', fig_idx=2012) """ import matplotlib.pyplot as plt # tl.logging.info(CNN.shape) # (5, 5, 3, 64) # exit() n_mask = CNN.shape[3] n_row = CNN.shape[0] n_col = CNN.shape[1] n_color = CNN.shape[2] row = int(np.sqrt(n_mask)) col = int(np.ceil(n_mask / row)) plt.ion() # active mode fig = plt.figure(fig_idx) count = 1 for _ir in range(1, row + 1): for _ic in range(1, col + 1): if count > n_mask: break fig.add_subplot(col, row, count) # tl.logging.info(CNN[:,:,:,count-1].shape, n_row, n_col) # (5, 1, 32) 5 5 # exit() # plt.imshow( # np.reshape(CNN[count-1,:,:,:], (n_row, n_col)), # cmap='gray', interpolation="nearest") # theano if n_color == 1: plt.imshow(np.reshape(CNN[:, :, :, count - 1], (n_row, n_col)), cmap='gray', interpolation="nearest") elif n_color == 3: plt.imshow( np.reshape(CNN[:, :, :, count - 1], (n_row, n_col, n_color)), cmap='gray', interpolation="nearest" ) else: raise Exception("Unknown n_color") plt.gca().xaxis.set_major_locator(plt.NullLocator()) # distable tick plt.gca().yaxis.set_major_locator(plt.NullLocator()) count = count + 1 if saveable: plt.savefig(name + '.pdf', format='pdf') else: plt.draw() plt.pause(second)
def tsne_embedding(embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='tsne', fig_idx=9862): """Visualize the embeddings by using t-SNE. Parameters ---------- embeddings : numpy.array The embedding matrix. reverse_dictionary : dictionary id_to_word, mapping id to unique word. plot_only : int The number of examples to plot, choice the most common words. second : int The display second(s) for the image(s), if saveable is False. saveable : boolean Save or plot the figure. name : str A name to save the image, if saveable is True. fig_idx : int matplotlib figure index. Examples -------- >>> see 'tutorial_word2vec_basic.py' >>> final_embeddings = normalized_embeddings.eval() >>> tl.visualize.tsne_embedding(final_embeddings, labels, reverse_dictionary, ... plot_only=500, second=5, saveable=False, name='tsne') """ import matplotlib.pyplot as plt def plot_with_labels(low_dim_embs, labels, figsize=(18, 18), second=5, saveable=True, name='tsne', fig_idx=9862): if low_dim_embs.shape[0] < len(labels): raise AssertionError("More labels than embeddings") if saveable is False: plt.ion() plt.figure(fig_idx) plt.figure(figsize=figsize) # in inches for i, label in enumerate(labels): x, y = low_dim_embs[i, :] plt.scatter(x, y) plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') if saveable: plt.savefig(name + '.pdf', format='pdf') else: plt.draw() plt.pause(second) try: from sklearn.manifold import TSNE from six.moves import xrange tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000) # plot_only = 500 low_dim_embs = tsne.fit_transform(embeddings[:plot_only, :]) labels = [reverse_dictionary[i] for i in xrange(plot_only)] plot_with_labels(low_dim_embs, labels, second=second, saveable=saveable, name=name, fig_idx=fig_idx) except ImportError: _err = "Please install sklearn and matplotlib to visualize embeddings." tl.logging.error(_err) raise ImportError(_err)
def draw_weights(W=None, second=10, saveable=True, shape=None, name='mnist', fig_idx=2396512): """Visualize every columns of the weight matrix to a group of Greyscale img. Parameters ---------- W : numpy.array The weight matrix second : int The display second(s) for the image(s), if saveable is False. saveable : boolean Save or plot the figure. shape : a list with 2 int or None The shape of feature image, MNIST is [28, 80]. name : a string A name to save the image, if saveable is True. fig_idx : int matplotlib figure index. Examples -------- >>> tl.visualize.draw_weights(network.all_params[0].eval(), second=10, saveable=True, name='weight_of_1st_layer', fig_idx=2012) """ if shape is None: shape = [28, 28] import matplotlib.pyplot as plt if saveable is False: plt.ion() fig = plt.figure(fig_idx) # show all feature images n_units = W.shape[1] num_r = int(np.sqrt(n_units)) # 每行显示的个数 若25个hidden unit -> 每行显示5个 num_c = int(np.ceil(n_units / num_r)) count = int(1) for _row in range(1, num_r + 1): for _col in range(1, num_c + 1): if count > n_units: break fig.add_subplot(num_r, num_c, count) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1],(28,28)), cmap='gray') # ------------------------------------------------------------ feature = W[:, count - 1] / np.sqrt((W[:, count - 1]**2).sum()) # feature[feature<0.0001] = 0 # value threshold # if count == 1 or count == 2: # print(np.mean(feature)) # if np.std(feature) < 0.03: # condition threshold # feature = np.zeros_like(feature) # if np.mean(feature) < -0.015: # condition threshold # feature = np.zeros_like(feature) plt.imshow( np.reshape(feature, (shape[0], shape[1])), cmap='gray', interpolation="nearest" ) # , vmin=np.min(feature), vmax=np.max(feature)) # plt.title(name) # ------------------------------------------------------------ # plt.imshow(np.reshape(W[:,count-1] ,(np.sqrt(size),np.sqrt(size))), cmap='gray', interpolation="nearest") plt.gca().xaxis.set_major_locator(plt.NullLocator()) # distable tick plt.gca().yaxis.set_major_locator(plt.NullLocator()) count = count + 1 if saveable: plt.savefig(name + '.pdf', format='pdf') else: plt.draw() plt.pause(second)
def data_to_tfrecord(images, labels, filename): """Save data into TFRecord.""" if os.path.isfile(filename): print("%s exists" % filename) return print("Converting data into %s ..." % filename) # cwd = os.getcwd() writer = tf.python_io.TFRecordWriter(filename) for index, img in enumerate(images): img_raw = img.tobytes() # Visualize a image # tl.visualize.frame(np.asarray(img, dtype=np.uint8), second=1, saveable=False, name='frame', fig_idx=1236) label = int(labels[index]) # print(label) # Convert the bytes back to image as follow: # image = Image.frombytes('RGB', (32, 32), img_raw) # image = np.fromstring(img_raw, np.float32) # image = image.reshape([32, 32, 3]) # tl.visualize.frame(np.asarray(image, dtype=np.uint8), second=1, saveable=False, name='frame', fig_idx=1236) example = tf.train.Example( features=tf.train.Features( feature={ "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[label])), 'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])), } ) ) writer.write(example.SerializeToString()) # Serialize To String writer.close()
def read_and_decode(filename, is_train=None): """Return tensor to read from TFRecord.""" filename_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), } ) # You can do more image distortion here for training data img = tf.decode_raw(features['img_raw'], tf.float32) img = tf.reshape(img, [32, 32, 3]) # img = tf.cast(img, tf.float32) #* (1. / 255) - 0.5 if is_train ==True: # 1. Randomly crop a [height, width] section of the image. img = tf.random_crop(img, [24, 24, 3]) # 2. Randomly flip the image horizontally. img = tf.image.random_flip_left_right(img) # 3. Randomly change brightness. img = tf.image.random_brightness(img, max_delta=63) # 4. Randomly change contrast. img = tf.image.random_contrast(img, lower=0.2, upper=1.8) # 5. Subtract off the mean and divide by the variance of the pixels. img = tf.image.per_image_standardization(img) elif is_train == False: # 1. Crop the central [height, width] of the image. img = tf.image.resize_image_with_crop_or_pad(img, 24, 24) # 2. Subtract off the mean and divide by the variance of the pixels. img = tf.image.per_image_standardization(img) elif is_train == None: img = img label = tf.cast(features['label'], tf.int32) return img, label
def print_params(self, details=True, session=None): """Print all info of parameters in the network""" for i, p in enumerate(self.all_params): if details: try: val = p.eval(session=session) logging.info( " param {:3}: {:20} {:15} {} (mean: {:<18}, median: {:<18}, std: {:<18}) ". format(i, p.name, str(val.shape), p.dtype.name, val.mean(), np.median(val), val.std()) ) except Exception as e: logging.info(str(e)) raise Exception( "Hint: print params details after tl.layers.initialize_global_variables(sess) " "or use network.print_params(False)." ) else: logging.info(" param {:3}: {:20} {:15} {}".format(i, p.name, str(p.get_shape()), p.dtype.name)) logging.info(" num of params: %d" % self.count_params())
def print_layers(self): """Print all info of layers in the network.""" for i, layer in enumerate(self.all_layers): # logging.info(" layer %d: %s" % (i, str(layer))) logging.info( " layer {:3}: {:20} {:15} {}".format(i, layer.name, str(layer.get_shape()), layer.dtype.name) )
def count_params(self): """Returns the number of parameters in the network.""" n_params = 0 for _i, p in enumerate(self.all_params): n = 1 # for s in p.eval().shape: for s in p.get_shape(): try: s = int(s) except Exception: s = 1 if s: n = n * s n_params = n_params + n return n_params
def get_all_params(self, session=None): """Return the parameters in a list of array.""" _params = [] for p in self.all_params: if session is None: _params.append(p.eval()) else: _params.append(session.run(p)) return _params
def _get_init_args(self, skip=4): """Get all arguments of current layer for saving the graph.""" stack = inspect.stack() if len(stack) < skip + 1: raise ValueError("The length of the inspection stack is shorter than the requested start position.") args, _, _, values = inspect.getargvalues(stack[skip][0]) params = {} for arg in args: # some args dont need to be saved into the graph. e.g. the input placeholder if values[arg] is not None and arg not in ['self', 'prev_layer', 'inputs']: val = values[arg] # change function (e.g. act) into dictionary of module path and function name if inspect.isfunction(val): params[arg] = {"module_path": val.__module__, "func_name": val.__name__} # ignore more args e.g. TF class elif arg.endswith('init'): continue # for other data type, save them directly else: params[arg] = val return params
def roi_pooling(input, rois, pool_height, pool_width): """ returns a tensorflow operation for computing the Region of Interest Pooling @arg input: feature maps on which to perform the pooling operation @arg rois: list of regions of interest in the format (feature map index, upper left, bottom right) @arg pool_width: size of the pooling sections """ # TODO(maciek): ops scope out = roi_pooling_module.roi_pooling(input, rois, pool_height=pool_height, pool_width=pool_width) output, argmax_output = out[0], out[1] return output
def _int64_feature(value): """Wrapper for inserting an int64 Feature into a SequenceExample proto, e.g, An integer label. """ return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value): """Wrapper for inserting a bytes Feature into a SequenceExample proto, e.g, an image in byte """ # return tf.train.Feature(bytes_list=tf.train.BytesList(value=[str(value)])) return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature_list(values): """Wrapper for inserting an int64 FeatureList into a SequenceExample proto, e.g, sentence in list of ints """ return tf.train.FeatureList(feature=[_int64_feature(v) for v in values])
def _bytes_feature_list(values): """Wrapper for inserting a bytes FeatureList into a SequenceExample proto, e.g, sentence in list of bytes """ return tf.train.FeatureList(feature=[_bytes_feature(v) for v in values])
def distort_image(image, thread_id): """Perform random distortions on an image. Args: image: A float32 Tensor of shape [height, width, 3] with values in [0, 1). thread_id: Preprocessing thread id used to select the ordering of color distortions. There should be a multiple of 2 preprocessing threads. Returns:```` distorted_image: A float32 Tensor of shape [height, width, 3] with values in [0, 1]. """ # Randomly flip horizontally. with tf.name_scope("flip_horizontal"): # , values=[image]): # DH MOdify # with tf.name_scope("flip_horizontal", values=[image]): image = tf.image.random_flip_left_right(image) # Randomly distort the colors based on thread id. color_ordering = thread_id % 2 with tf.name_scope("distort_color"): # , values=[image]): # DH MOdify # with tf.name_scope("distort_color", values=[image]): # DH MOdify if color_ordering == 0: image = tf.image.random_brightness(image, max_delta=32. / 255.) image = tf.image.random_saturation(image, lower=0.5, upper=1.5) image = tf.image.random_hue(image, max_delta=0.032) image = tf.image.random_contrast(image, lower=0.5, upper=1.5) elif color_ordering == 1: image = tf.image.random_brightness(image, max_delta=32. / 255.) image = tf.image.random_contrast(image, lower=0.5, upper=1.5) image = tf.image.random_saturation(image, lower=0.5, upper=1.5) image = tf.image.random_hue(image, max_delta=0.032) # The random_* ops do not necessarily clamp. image = tf.clip_by_value(image, 0.0, 1.0) return image
def prefetch_input_data( reader, file_pattern, is_training, batch_size, values_per_shard, input_queue_capacity_factor=16, num_reader_threads=1, shard_queue_name="filename_queue", value_queue_name="input_queue" ): """Prefetches string values from disk into an input queue. In training the capacity of the queue is important because a larger queue means better mixing of training examples between shards. The minimum number of values kept in the queue is values_per_shard * input_queue_capacity_factor, where input_queue_memory factor should be chosen to trade-off better mixing with memory usage. Args: reader: Instance of tf.ReaderBase. file_pattern: Comma-separated list of file patterns (e.g. /tmp/train_data-?????-of-00100). is_training: Boolean; whether prefetching for training or eval. batch_size: Model batch size used to determine queue capacity. values_per_shard: Approximate number of values per shard. input_queue_capacity_factor: Minimum number of values to keep in the queue in multiples of values_per_shard. See comments above. num_reader_threads: Number of reader threads to fill the queue. shard_queue_name: Name for the shards filename queue. value_queue_name: Name for the values input queue. Returns: A Queue containing prefetched string values. """ data_files = [] for pattern in file_pattern.split(","): data_files.extend(tf.gfile.Glob(pattern)) if not data_files: tl.logging.fatal("Found no input files matching %s", file_pattern) else: tl.logging.info("Prefetching values from %d files matching %s", len(data_files), file_pattern) if is_training: print(" is_training == True : RandomShuffleQueue") filename_queue = tf.train.string_input_producer(data_files, shuffle=True, capacity=16, name=shard_queue_name) min_queue_examples = values_per_shard * input_queue_capacity_factor capacity = min_queue_examples + 100 * batch_size values_queue = tf.RandomShuffleQueue( capacity=capacity, min_after_dequeue=min_queue_examples, dtypes=[tf.string], name="random_" + value_queue_name ) else: print(" is_training == False : FIFOQueue") filename_queue = tf.train.string_input_producer(data_files, shuffle=False, capacity=1, name=shard_queue_name) capacity = values_per_shard + 3 * batch_size values_queue = tf.FIFOQueue(capacity=capacity, dtypes=[tf.string], name="fifo_" + value_queue_name) enqueue_ops = [] for _ in range(num_reader_threads): _, value = reader.read(filename_queue) enqueue_ops.append(values_queue.enqueue([value])) tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner(values_queue, enqueue_ops)) tf.summary.scalar( "queue/%s/fraction_of_%d_full" % (values_queue.name, capacity), tf.cast(values_queue.size(), tf.float32) * (1. / capacity) ) return values_queue
def batch_with_dynamic_pad(images_and_captions, batch_size, queue_capacity, add_summaries=True): """Batches input images and captions. This function splits the caption into an input sequence and a target sequence, where the target sequence is the input sequence right-shifted by 1. Input and target sequences are batched and padded up to the maximum length of sequences in the batch. A mask is created to distinguish real words from padding words. Example: Actual captions in the batch ('-' denotes padded character): [ [ 1 2 5 4 5 ], [ 1 2 3 4 - ], [ 1 2 3 - - ], ] input_seqs: [ [ 1 2 3 4 ], [ 1 2 3 - ], [ 1 2 - - ], ] target_seqs: [ [ 2 3 4 5 ], [ 2 3 4 - ], [ 2 3 - - ], ] mask: [ [ 1 1 1 1 ], [ 1 1 1 0 ], [ 1 1 0 0 ], ] Args: images_and_captions: A list of pairs [image, caption], where image is a Tensor of shape [height, width, channels] and caption is a 1-D Tensor of any length. Each pair will be processed and added to the queue in a separate thread. batch_size: Batch size. queue_capacity: Queue capacity. add_summaries: If true, add caption length summaries. Returns: images: A Tensor of shape [batch_size, height, width, channels]. input_seqs: An int32 Tensor of shape [batch_size, padded_length]. target_seqs: An int32 Tensor of shape [batch_size, padded_length]. mask: An int32 0/1 Tensor of shape [batch_size, padded_length]. """ enqueue_list = [] for image, caption in images_and_captions: caption_length = tf.shape(caption)[0] input_length = tf.expand_dims(tf.subtract(caption_length, 1), 0) input_seq = tf.slice(caption, [0], input_length) target_seq = tf.slice(caption, [1], input_length) indicator = tf.ones(input_length, dtype=tf.int32) enqueue_list.append([image, input_seq, target_seq, indicator]) images, input_seqs, target_seqs, mask = tf.train.batch_join( enqueue_list, batch_size=batch_size, capacity=queue_capacity, dynamic_pad=True, name="batch_and_pad" ) if add_summaries: lengths = tf.add(tf.reduce_sum(mask, 1), 1) tf.summary.scalar("caption_length/batch_min", tf.reduce_min(lengths)) tf.summary.scalar("caption_length/batch_max", tf.reduce_max(lengths)) tf.summary.scalar("caption_length/batch_mean", tf.reduce_mean(lengths)) return images, input_seqs, target_seqs, mask
def _to_channel_first_bias(b): """Reshape [c] to [c, 1, 1].""" channel_size = int(b.shape[0]) new_shape = (channel_size, 1, 1) # new_shape = [-1, 1, 1] # doesn't work with tensorRT return tf.reshape(b, new_shape)
def _bias_scale(x, b, data_format): """The multiplication counter part of tf.nn.bias_add.""" if data_format == 'NHWC': return x * b elif data_format == 'NCHW': return x * _to_channel_first_bias(b) else: raise ValueError('invalid data_format: %s' % data_format)
def _bias_add(x, b, data_format): """Alternative implementation of tf.nn.bias_add which is compatiable with tensorRT.""" if data_format == 'NHWC': return tf.add(x, b) elif data_format == 'NCHW': return tf.add(x, _to_channel_first_bias(b)) else: raise ValueError('invalid data_format: %s' % data_format)
def batch_normalization(x, mean, variance, offset, scale, variance_epsilon, data_format, name=None): """Data Format aware version of tf.nn.batch_normalization.""" with ops.name_scope(name, 'batchnorm', [x, mean, variance, scale, offset]): inv = math_ops.rsqrt(variance + variance_epsilon) if scale is not None: inv *= scale a = math_ops.cast(inv, x.dtype) b = math_ops.cast(offset - mean * inv if offset is not None else -mean * inv, x.dtype) # Return a * x + b with customized data_format. # Currently TF doesn't have bias_scale, and tensorRT has bug in converting tf.nn.bias_add # So we reimplemted them to allow make the model work with tensorRT. # See https://github.com/tensorlayer/openpose-plus/issues/75 for more details. df = {'channels_first': 'NCHW', 'channels_last': 'NHWC'} return _bias_add(_bias_scale(x, a, df[data_format]), b, df[data_format])
def compute_alpha(x): """Computing the scale parameter.""" threshold = _compute_threshold(x) alpha1_temp1 = tf.where(tf.greater(x, threshold), x, tf.zeros_like(x, tf.float32)) alpha1_temp2 = tf.where(tf.less(x, -threshold), x, tf.zeros_like(x, tf.float32)) alpha_array = tf.add(alpha1_temp1, alpha1_temp2, name=None) alpha_array_abs = tf.abs(alpha_array) alpha_array_abs1 = tf.where( tf.greater(alpha_array_abs, 0), tf.ones_like(alpha_array_abs, tf.float32), tf.zeros_like(alpha_array_abs, tf.float32) ) alpha_sum = tf.reduce_sum(alpha_array_abs) n = tf.reduce_sum(alpha_array_abs1) alpha = tf.div(alpha_sum, n) return alpha
def flatten_reshape(variable, name='flatten'): """Reshapes a high-dimension vector input. [batch_size, mask_row, mask_col, n_mask] ---> [batch_size, mask_row x mask_col x n_mask] Parameters ---------- variable : TensorFlow variable or tensor The variable or tensor to be flatten. name : str A unique layer name. Returns ------- Tensor Flatten Tensor Examples -------- >>> import tensorflow as tf >>> import tensorlayer as tl >>> x = tf.placeholder(tf.float32, [None, 128, 128, 3]) >>> # Convolution Layer with 32 filters and a kernel size of 5 >>> network = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu) >>> # Max Pooling (down-sampling) with strides of 2 and kernel size of 2 >>> network = tf.layers.max_pooling2d(network, 2, 2) >>> print(network.get_shape()[:].as_list()) >>> [None, 62, 62, 32] >>> network = tl.layers.flatten_reshape(network) >>> print(network.get_shape()[:].as_list()[1:]) >>> [None, 123008] """ dim = 1 for d in variable.get_shape()[1:].as_list(): dim *= d return tf.reshape(variable, shape=[-1, dim], name=name)
def get_layers_with_name(net, name="", verbose=False): """Get a list of layers' output in a network by a given name scope. Parameters ----------- net : :class:`Layer` The last layer of the network. name : str Get the layers' output that contain this name. verbose : boolean If True, print information of all the layers' output Returns -------- list of Tensor A list of layers' output (TensorFlow tensor) Examples --------- >>> import tensorlayer as tl >>> layers = tl.layers.get_layers_with_name(net, "CNN", True) """ logging.info(" [*] geting layers with %s" % name) layers = [] i = 0 for layer in net.all_layers: # logging.info(type(layer.name)) if name in layer.name: layers.append(layer) if verbose: logging.info(" got {:3}: {:15} {}".format(i, layer.name, str(layer.get_shape()))) i = i + 1 return layers
def get_variables_with_name(name=None, train_only=True, verbose=False): """Get a list of TensorFlow variables by a given name scope. Parameters ---------- name : str Get the variables that contain this name. train_only : boolean If Ture, only get the trainable variables. verbose : boolean If True, print the information of all variables. Returns ------- list of Tensor A list of TensorFlow variables Examples -------- >>> import tensorlayer as tl >>> dense_vars = tl.layers.get_variables_with_name('dense', True, True) """ if name is None: raise Exception("please input a name") logging.info(" [*] geting variables with %s" % name) # tvar = tf.trainable_variables() if train_only else tf.all_variables() if train_only: t_vars = tf.trainable_variables() else: t_vars = tf.global_variables() d_vars = [var for var in t_vars if name in var.name] if verbose: for idx, v in enumerate(d_vars): logging.info(" got {:3}: {:15} {}".format(idx, v.name, str(v.get_shape()))) return d_vars
def initialize_rnn_state(state, feed_dict=None): """Returns the initialized RNN state. The inputs are `LSTMStateTuple` or `State` of `RNNCells`, and an optional `feed_dict`. Parameters ---------- state : RNN state. The TensorFlow's RNN state. feed_dict : dictionary Initial RNN state; if None, returns zero state. Returns ------- RNN state The TensorFlow's RNN state. """ if isinstance(state, LSTMStateTuple): c = state.c.eval(feed_dict=feed_dict) h = state.h.eval(feed_dict=feed_dict) return c, h else: new_state = state.eval(feed_dict=feed_dict) return new_state
def list_remove_repeat(x): """Remove the repeated items in a list, and return the processed list. You may need it to create merged layer like Concat, Elementwise and etc. Parameters ---------- x : list Input Returns ------- list A list that after removing it's repeated items Examples ------- >>> l = [2, 3, 4, 2, 3] >>> l = list_remove_repeat(l) [2, 3, 4] """ y = [] for i in x: if i not in y: y.append(i) return y
def merge_networks(layers=None): """Merge all parameters, layers and dropout probabilities to a :class:`Layer`. The output of return network is the first network in the list. Parameters ---------- layers : list of :class:`Layer` Merge all parameters, layers and dropout probabilities to the first layer in the list. Returns -------- :class:`Layer` The network after merging all parameters, layers and dropout probabilities to the first network in the list. Examples --------- >>> import tensorlayer as tl >>> n1 = ... >>> n2 = ... >>> n1 = tl.layers.merge_networks([n1, n2]) """ if layers is None: raise Exception("layers should be a list of TensorLayer's Layers.") layer = layers[0] all_params = [] all_layers = [] all_drop = {} for l in layers: all_params.extend(l.all_params) all_layers.extend(l.all_layers) all_drop.update(l.all_drop) layer.all_params = list(all_params) layer.all_layers = list(all_layers) layer.all_drop = dict(all_drop) layer.all_layers = list_remove_repeat(layer.all_layers) layer.all_params = list_remove_repeat(layer.all_params) return layer
def print_all_variables(train_only=False): """Print information of trainable or all variables, without ``tl.layers.initialize_global_variables(sess)``. Parameters ---------- train_only : boolean Whether print trainable variables only. - If True, print the trainable variables. - If False, print all variables. """ # tvar = tf.trainable_variables() if train_only else tf.all_variables() if train_only: t_vars = tf.trainable_variables() logging.info(" [*] printing trainable variables") else: t_vars = tf.global_variables() logging.info(" [*] printing global variables") for idx, v in enumerate(t_vars): logging.info(" var {:3}: {:15} {}".format(idx, str(v.get_shape()), v.name))
def ternary_operation(x): """Ternary operation use threshold computed with weights.""" g = tf.get_default_graph() with g.gradient_override_map({"Sign": "Identity"}): threshold = _compute_threshold(x) x = tf.sign(tf.add(tf.sign(tf.add(x, threshold)), tf.sign(tf.add(x, -threshold)))) return x
def _compute_threshold(x): """ ref: https://github.com/XJTUWYD/TWN Computing the threshold. """ x_sum = tf.reduce_sum(tf.abs(x), reduction_indices=None, keepdims=False, name=None) threshold = tf.div(x_sum, tf.cast(tf.size(x), tf.float32), name=None) threshold = tf.multiply(0.7, threshold, name=None) return threshold
def freeze_graph(graph_path, checkpoint_path, output_path, end_node_names, is_binary_graph): """Reimplementation of the TensorFlow official freeze_graph function to freeze the graph and checkpoint together: Parameters ----------- graph_path : string the path where your graph file save. checkpoint_output_path : string the path where your checkpoint save. output_path : string the path where you want to save the output proto buff end_node_names : string the name of the end node in your graph you want to get in your proto buff is_binary_graph : boolean declare your file whether is a binary graph References ---------- - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__ - `tensorflow freeze_graph <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py>` """ _freeze_graph( input_graph=graph_path, input_saver='', input_binary=is_binary_graph, input_checkpoint=checkpoint_path, output_graph=output_path, output_node_names=end_node_names, restore_op_name='save/restore_all', filename_tensor_name='save/Const:0', clear_devices=True, initializer_nodes=None )
def convert_model_to_onnx(frozen_graph_path, end_node_names, onnx_output_path): """Reimplementation of the TensorFlow-onnx official tutorial convert the proto buff to onnx file: Parameters ----------- frozen_graph_path : string the path where your frozen graph file save. end_node_names : string the name of the end node in your graph you want to get in your proto buff onnx_output_path : string the path where you want to save the onnx file. References ----------- - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>` """ with tf.gfile.GFile(frozen_graph_path, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) onnx_model = tensorflow_graph_to_onnx_model(graph_def, end_node_names, opset=6) file = open(onnx_output_path, "wb") file.write(onnx_model.SerializeToString()) file.close()
def convert_onnx_to_model(onnx_input_path): """Reimplementation of the TensorFlow-onnx official tutorial convert the onnx file to specific: model Parameters ----------- onnx_input_path : string the path where you save the onnx file. References ----------- - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__ """ model = onnx.load(onnx_input_path) tf_rep = prepare(model) # Image Path img = np.load("./assets/image.npz") output = tf_rep.run(img.reshape([1, 784])) print("The digit is classified as ", np.argmax(output))
def _add_deprecated_function_notice_to_docstring(doc, date, instructions): """Adds a deprecation notice to a docstring for deprecated functions.""" if instructions: deprecation_message = """ .. warning:: **THIS FUNCTION IS DEPRECATED:** It will be removed after %s. *Instructions for updating:* %s. """ % (('in a future version' if date is None else ('after %s' % date)), instructions) else: deprecation_message = """ .. warning:: **THIS FUNCTION IS DEPRECATED:** It will be removed after %s. """ % (('in a future version' if date is None else ('after %s' % date))) main_text = [deprecation_message] return _add_notice_to_docstring(doc, 'DEPRECATED FUNCTION', main_text)
def _add_notice_to_docstring(doc, no_doc_str, notice): """Adds a deprecation notice to a docstring.""" if not doc: lines = [no_doc_str] else: lines = _normalize_docstring(doc).splitlines() notice = [''] + notice if len(lines) > 1: # Make sure that we keep our distance from the main body if lines[1].strip(): notice.append('') lines[1:1] = notice else: lines += notice return '\n'.join(lines)
def alphas(shape, alpha_value, name=None): """Creates a tensor with all elements set to `alpha_value`. This operation returns a tensor of type `dtype` with shape `shape` and all elements set to alpha. Parameters ---------- shape: A list of integers, a tuple of integers, or a 1-D `Tensor` of type `int32`. The shape of the desired tensor alpha_value: `float32`, `float64`, `int8`, `uint8`, `int16`, `uint16`, int32`, `int64` The value used to fill the resulting `Tensor`. name: str A name for the operation (optional). Returns ------- A `Tensor` with all elements set to alpha. Examples -------- >>> tl.alphas([2, 3], tf.int32) # [[alpha, alpha, alpha], [alpha, alpha, alpha]] """ with ops.name_scope(name, "alphas", [shape]) as name: alpha_tensor = convert_to_tensor(alpha_value) alpha_dtype = dtypes.as_dtype(alpha_tensor.dtype).base_dtype if not isinstance(shape, ops.Tensor): try: shape = constant_op._tensor_shape_tensor_conversion_function(tensor_shape.TensorShape(shape)) except (TypeError, ValueError): shape = ops.convert_to_tensor(shape, dtype=dtypes.int32) if not shape._shape_tuple(): shape = reshape(shape, [-1]) # Ensure it's a vector try: output = constant(alpha_value, shape=shape, dtype=alpha_dtype, name=name) except (TypeError, ValueError): output = fill(shape, constant(alpha_value, dtype=alpha_dtype), name=name) if output.dtype.base_dtype != alpha_dtype: raise AssertionError("Dtypes do not corresponds: %s and %s" % (output.dtype.base_dtype, alpha_dtype)) return output
def alphas_like(tensor, alpha_value, name=None, optimize=True): """Creates a tensor with all elements set to `alpha_value`. Given a single tensor (`tensor`), this operation returns a tensor of the same type and shape as `tensor` with all elements set to `alpha_value`. Parameters ---------- tensor: tf.Tensor The Tensorflow Tensor that will be used as a template. alpha_value: `float32`, `float64`, `int8`, `uint8`, `int16`, `uint16`, int32`, `int64` The value used to fill the resulting `Tensor`. name: str A name for the operation (optional). optimize: bool if true, attempt to statically determine the shape of 'tensor' and encode it as a constant. Returns ------- A `Tensor` with all elements set to `alpha_value`. Examples -------- >>> tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) >>> tl.alphas_like(tensor, 0.5) # [[0.5, 0.5, 0.5], [0.5, 0.5, 0.5]] """ with ops.name_scope(name, "alphas_like", [tensor]) as name: tensor = ops.convert_to_tensor(tensor, name="tensor") if context.in_eager_mode(): # and dtype is not None and dtype != tensor.dtype: ret = alphas(shape_internal(tensor, optimize=optimize), alpha_value=alpha_value, name=name) else: # if context.in_graph_mode(): # For now, variant types must be created via zeros_like; as we need to # pass the input variant object to the proper zeros callback. if (optimize and tensor.shape.is_fully_defined()): # We can produce a zeros tensor independent of the value of 'tensor', # since the shape is known statically. ret = alphas(tensor.shape, alpha_value=alpha_value, name=name) # elif dtype is not None and dtype != tensor.dtype and dtype != dtypes.variant: else: ret = alphas(shape_internal(tensor, optimize=optimize), alpha_value=alpha_value, name=name) ret.set_shape(tensor.get_shape()) return ret
def example1(): """ Example 1: Applying transformation one-by-one is very SLOW ! """ st = time.time() for _ in range(100): # Try 100 times and compute the averaged speed xx = tl.prepro.rotation(image, rg=-20, is_random=False) xx = tl.prepro.flip_axis(xx, axis=1, is_random=False) xx = tl.prepro.shear2(xx, shear=(0., -0.2), is_random=False) xx = tl.prepro.zoom(xx, zoom_range=1 / 0.8) xx = tl.prepro.shift(xx, wrg=-0.1, hrg=0, is_random=False) print("apply transforms one-by-one took %fs for each image" % ((time.time() - st) / 100)) tl.vis.save_image(xx, '_result_slow.png')
def example2(): """ Example 2: Applying all transforms in one is very FAST ! """ st = time.time() for _ in range(100): # Repeat 100 times and compute the averaged speed transform_matrix = create_transformation_matrix() result = tl.prepro.affine_transform_cv2(image, transform_matrix) # Transform the image using a single operation print("apply all transforms once took %fs for each image" % ((time.time() - st) / 100)) # usually 50x faster tl.vis.save_image(result, '_result_fast.png')
def example3(): """ Example 3: Using TF dataset API to load and process image for training """ n_data = 100 imgs_file_list = ['tiger.jpeg'] * n_data train_targets = [np.ones(1)] * n_data def generator(): if len(imgs_file_list) != len(train_targets): raise RuntimeError('len(imgs_file_list) != len(train_targets)') for _input, _target in zip(imgs_file_list, train_targets): yield _input, _target def _data_aug_fn(image): transform_matrix = create_transformation_matrix() result = tl.prepro.affine_transform_cv2(image, transform_matrix) # Transform the image using a single operation return result def _map_fn(image_path, target): image = tf.read_file(image_path) image = tf.image.decode_jpeg(image, channels=3) # Get RGB with 0~1 image = tf.image.convert_image_dtype(image, dtype=tf.float32) image = tf.py_func(_data_aug_fn, [image], [tf.float32]) # image = tf.reshape(image, (h, w, 3)) target = tf.reshape(target, ()) return image, target n_epoch = 10 batch_size = 5 dataset = tf.data.Dataset().from_generator(generator, output_types=(tf.string, tf.int64)) dataset = dataset.shuffle(buffer_size=4096) # shuffle before loading images dataset = dataset.repeat(n_epoch) dataset = dataset.map(_map_fn, num_parallel_calls=multiprocessing.cpu_count()) dataset = dataset.batch(batch_size) # TODO: consider using tf.contrib.map_and_batch dataset = dataset.prefetch(1) # prefetch 1 batch iterator = dataset.make_one_shot_iterator() one_element = iterator.get_next() sess = tf.Session() # feed `one_element` into a network, for demo, we simply get the data as follows n_step = round(n_epoch * n_data / batch_size) st = time.time() for _ in range(n_step): _images, _targets = sess.run(one_element) print("dataset APIs took %fs for each image" % ((time.time() - st) / batch_size / n_step))
def example4(): """ Example 4: Transforming coordinates using affine matrix. """ transform_matrix = create_transformation_matrix() result = tl.prepro.affine_transform_cv2(image, transform_matrix) # 76 times faster # Transform keypoint coordinates coords = [[(50, 100), (100, 100), (100, 50), (200, 200)], [(250, 50), (200, 50), (200, 100)]] coords_result = tl.prepro.affine_transform_keypoints(coords, transform_matrix) def imwrite(image, coords_list, name): coords_list_ = [] for coords in coords_list: coords = np.array(coords, np.int32) coords = coords.reshape((-1, 1, 2)) coords_list_.append(coords) image = cv2.polylines(image, coords_list_, True, (0, 255, 255), 3) cv2.imwrite(name, image[..., ::-1]) imwrite(image, coords, '_with_keypoints_origin.png') imwrite(result, coords_result, '_with_keypoints_result.png')
def distort_fn(x, is_train=False): """ The images are processed as follows: .. They are cropped to 24 x 24 pixels, centrally for evaluation or randomly for training. .. They are approximately whitened to make the model insensitive to dynamic range. For training, we additionally apply a series of random distortions to artificially increase the data set size: .. Randomly flip the image from left to right. .. Randomly distort the image brightness. """ # print('begin',x.shape, np.min(x), np.max(x)) x = tl.prepro.crop(x, 24, 24, is_random=is_train) # print('after crop',x.shape, np.min(x), np.max(x)) if is_train: # x = tl.prepro.zoom(x, zoom_range=(0.9, 1.0), is_random=True) # print('after zoom', x.shape, np.min(x), np.max(x)) x = tl.prepro.flip_axis(x, axis=1, is_random=True) # print('after flip',x.shape, np.min(x), np.max(x)) x = tl.prepro.brightness(x, gamma=0.1, gain=1, is_random=True) # print('after brightness',x.shape, np.min(x), np.max(x)) # tmp = np.max(x) # x += np.random.uniform(-20, 20) # x /= tmp # normalize the image x = (x - np.mean(x)) / max(np.std(x), 1e-5) # avoid values divided by 0 # print('after norm', x.shape, np.min(x), np.max(x), np.mean(x)) return x
def fit( sess, network, train_op, cost, X_train, y_train, x, y_, acc=None, batch_size=100, n_epoch=100, print_freq=5, X_val=None, y_val=None, eval_train=True, tensorboard_dir=None, tensorboard_epoch_freq=5, tensorboard_weight_histograms=True, tensorboard_graph_vis=True ): """Training a given non time-series network by the given cost function, training data, batch_size, n_epoch etc. - MNIST example click `here <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mnist_simple.py>`_. - In order to control the training details, the authors HIGHLY recommend ``tl.iterate`` see two MNIST examples `1 <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mlp_dropout1.py>`_, `2 <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mlp_dropout1.py>`_. Parameters ---------- sess : Session TensorFlow Session. network : TensorLayer layer the network to be trained. train_op : TensorFlow optimizer The optimizer for training e.g. tf.train.AdamOptimizer. X_train : numpy.array The input of training data y_train : numpy.array The target of training data x : placeholder For inputs. y_ : placeholder For targets. acc : TensorFlow expression or None Metric for accuracy or others. If None, would not print the information. batch_size : int The batch size for training and evaluating. n_epoch : int The number of training epochs. print_freq : int Print the training information every ``print_freq`` epochs. X_val : numpy.array or None The input of validation data. If None, would not perform validation. y_val : numpy.array or None The target of validation data. If None, would not perform validation. eval_train : boolean Whether to evaluate the model during training. If X_val and y_val are not None, it reflects whether to evaluate the model on training data. tensorboard_dir : string path to log dir, if set, summary data will be stored to the tensorboard_dir/ directory for visualization with tensorboard. (default None) Also runs `tl.layers.initialize_global_variables(sess)` internally in fit() to setup the summary nodes. tensorboard_epoch_freq : int How many epochs between storing tensorboard checkpoint for visualization to log/ directory (default 5). tensorboard_weight_histograms : boolean If True updates tensorboard data in the logs/ directory for visualization of the weight histograms every tensorboard_epoch_freq epoch (default True). tensorboard_graph_vis : boolean If True stores the graph in the tensorboard summaries saved to log/ (default True). Examples -------- See `tutorial_mnist_simple.py <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mnist_simple.py>`_ >>> tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_, ... acc=acc, batch_size=500, n_epoch=200, print_freq=5, ... X_val=X_val, y_val=y_val, eval_train=False) >>> tl.utils.fit(sess, network, train_op, cost, X_train, y_train, x, y_, ... acc=acc, batch_size=500, n_epoch=200, print_freq=5, ... X_val=X_val, y_val=y_val, eval_train=False, ... tensorboard=True, tensorboard_weight_histograms=True, tensorboard_graph_vis=True) Notes -------- If tensorboard_dir not None, the `global_variables_initializer` will be run inside the fit function in order to initialize the automatically generated summary nodes used for tensorboard visualization, thus `tf.global_variables_initializer().run()` before the `fit()` call will be undefined. """ if X_train.shape[0] < batch_size: raise AssertionError("Number of training examples should be bigger than the batch size") if tensorboard_dir is not None: tl.logging.info("Setting up tensorboard ...") #Set up tensorboard summaries and saver tl.files.exists_or_mkdir(tensorboard_dir) #Only write summaries for more recent TensorFlow versions if hasattr(tf, 'summary') and hasattr(tf.summary, 'FileWriter'): if tensorboard_graph_vis: train_writer = tf.summary.FileWriter(tensorboard_dir + '/train', sess.graph) val_writer = tf.summary.FileWriter(tensorboard_dir + '/validation', sess.graph) else: train_writer = tf.summary.FileWriter(tensorboard_dir + '/train') val_writer = tf.summary.FileWriter(tensorboard_dir + '/validation') #Set up summary nodes if (tensorboard_weight_histograms): for param in network.all_params: if hasattr(tf, 'summary') and hasattr(tf.summary, 'histogram'): tl.logging.info('Param name %s' % param.name) tf.summary.histogram(param.name, param) if hasattr(tf, 'summary') and hasattr(tf.summary, 'histogram'): tf.summary.scalar('cost', cost) merged = tf.summary.merge_all() #Initalize all variables and summaries tl.layers.initialize_global_variables(sess) tl.logging.info("Finished! use `tensorboard --logdir=%s/` to start tensorboard" % tensorboard_dir) tl.logging.info("Start training the network ...") start_time_begin = time.time() tensorboard_train_index, tensorboard_val_index = 0, 0 for epoch in range(n_epoch): start_time = time.time() loss_ep = 0 n_step = 0 for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True): feed_dict = {x: X_train_a, y_: y_train_a} feed_dict.update(network.all_drop) # enable noise layers loss, _ = sess.run([cost, train_op], feed_dict=feed_dict) loss_ep += loss n_step += 1 loss_ep = loss_ep / n_step if tensorboard_dir is not None and hasattr(tf, 'summary'): if epoch + 1 == 1 or (epoch + 1) % tensorboard_epoch_freq == 0: for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True): dp_dict = dict_to_one(network.all_drop) # disable noise layers feed_dict = {x: X_train_a, y_: y_train_a} feed_dict.update(dp_dict) result = sess.run(merged, feed_dict=feed_dict) train_writer.add_summary(result, tensorboard_train_index) tensorboard_train_index += 1 if (X_val is not None) and (y_val is not None): for X_val_a, y_val_a in tl.iterate.minibatches(X_val, y_val, batch_size, shuffle=True): dp_dict = dict_to_one(network.all_drop) # disable noise layers feed_dict = {x: X_val_a, y_: y_val_a} feed_dict.update(dp_dict) result = sess.run(merged, feed_dict=feed_dict) val_writer.add_summary(result, tensorboard_val_index) tensorboard_val_index += 1 if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: if (X_val is not None) and (y_val is not None): tl.logging.info("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time)) if eval_train is True: train_loss, train_acc, n_batch = 0, 0, 0 for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True): dp_dict = dict_to_one(network.all_drop) # disable noise layers feed_dict = {x: X_train_a, y_: y_train_a} feed_dict.update(dp_dict) if acc is not None: err, ac = sess.run([cost, acc], feed_dict=feed_dict) train_acc += ac else: err = sess.run(cost, feed_dict=feed_dict) train_loss += err n_batch += 1 tl.logging.info(" train loss: %f" % (train_loss / n_batch)) if acc is not None: tl.logging.info(" train acc: %f" % (train_acc / n_batch)) val_loss, val_acc, n_batch = 0, 0, 0 for X_val_a, y_val_a in tl.iterate.minibatches(X_val, y_val, batch_size, shuffle=True): dp_dict = dict_to_one(network.all_drop) # disable noise layers feed_dict = {x: X_val_a, y_: y_val_a} feed_dict.update(dp_dict) if acc is not None: err, ac = sess.run([cost, acc], feed_dict=feed_dict) val_acc += ac else: err = sess.run(cost, feed_dict=feed_dict) val_loss += err n_batch += 1 tl.logging.info(" val loss: %f" % (val_loss / n_batch)) if acc is not None: tl.logging.info(" val acc: %f" % (val_acc / n_batch)) else: tl.logging.info( "Epoch %d of %d took %fs, loss %f" % (epoch + 1, n_epoch, time.time() - start_time, loss_ep) ) tl.logging.info("Total training time: %fs" % (time.time() - start_time_begin))
def predict(sess, network, X, x, y_op, batch_size=None): """ Return the predict results of given non time-series network. Parameters ---------- sess : Session TensorFlow Session. network : TensorLayer layer The network. X : numpy.array The inputs. x : placeholder For inputs. y_op : placeholder The argmax expression of softmax outputs. batch_size : int or None The batch size for prediction, when dataset is large, we should use minibatche for prediction; if dataset is small, we can set it to None. Examples -------- See `tutorial_mnist_simple.py <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_mnist_simple.py>`_ >>> y = network.outputs >>> y_op = tf.argmax(tf.nn.softmax(y), 1) >>> print(tl.utils.predict(sess, network, X_test, x, y_op)) """ if batch_size is None: dp_dict = dict_to_one(network.all_drop) # disable noise layers feed_dict = { x: X, } feed_dict.update(dp_dict) return sess.run(y_op, feed_dict=feed_dict) else: result = None for X_a, _ in tl.iterate.minibatches(X, X, batch_size, shuffle=False): dp_dict = dict_to_one(network.all_drop) feed_dict = { x: X_a, } feed_dict.update(dp_dict) result_a = sess.run(y_op, feed_dict=feed_dict) if result is None: result = result_a else: result = np.concatenate((result, result_a)) if result is None: if len(X) % batch_size != 0: dp_dict = dict_to_one(network.all_drop) feed_dict = { x: X[-(len(X) % batch_size):, :], } feed_dict.update(dp_dict) result_a = sess.run(y_op, feed_dict=feed_dict) result = result_a else: if len(X) != len(result) and len(X) % batch_size != 0: dp_dict = dict_to_one(network.all_drop) feed_dict = { x: X[-(len(X) % batch_size):, :], } feed_dict.update(dp_dict) result_a = sess.run(y_op, feed_dict=feed_dict) result = np.concatenate((result, result_a)) return result
def evaluation(y_test=None, y_predict=None, n_classes=None): """ Input the predicted results, targets results and the number of class, return the confusion matrix, F1-score of each class, accuracy and macro F1-score. Parameters ---------- y_test : list The target results y_predict : list The predicted results n_classes : int The number of classes Examples -------- >>> c_mat, f1, acc, f1_macro = tl.utils.evaluation(y_test, y_predict, n_classes) """ c_mat = confusion_matrix(y_test, y_predict, labels=[x for x in range(n_classes)]) f1 = f1_score(y_test, y_predict, average=None, labels=[x for x in range(n_classes)]) f1_macro = f1_score(y_test, y_predict, average='macro') acc = accuracy_score(y_test, y_predict) tl.logging.info('confusion matrix: \n%s' % c_mat) tl.logging.info('f1-score : %s' % f1) tl.logging.info('f1-score(macro) : %f' % f1_macro) # same output with > f1_score(y_true, y_pred, average='macro') tl.logging.info('accuracy-score : %f' % acc) return c_mat, f1, acc, f1_macro
def class_balancing_oversample(X_train=None, y_train=None, printable=True): """Input the features and labels, return the features and labels after oversampling. Parameters ---------- X_train : numpy.array The inputs. y_train : numpy.array The targets. Examples -------- One X >>> X_train, y_train = class_balancing_oversample(X_train, y_train, printable=True) Two X >>> X, y = tl.utils.class_balancing_oversample(X_train=np.hstack((X1, X2)), y_train=y, printable=False) >>> X1 = X[:, 0:5] >>> X2 = X[:, 5:] """ # ======== Classes balancing if printable: tl.logging.info("Classes balancing for training examples...") c = Counter(y_train) if printable: tl.logging.info('the occurrence number of each stage: %s' % c.most_common()) tl.logging.info('the least stage is Label %s have %s instances' % c.most_common()[-1]) tl.logging.info('the most stage is Label %s have %s instances' % c.most_common(1)[0]) most_num = c.most_common(1)[0][1] if printable: tl.logging.info('most num is %d, all classes tend to be this num' % most_num) locations = {} number = {} for lab, num in c.most_common(): # find the index from y_train number[lab] = num locations[lab] = np.where(np.array(y_train) == lab)[0] if printable: tl.logging.info('convert list(np.array) to dict format') X = {} # convert list to dict for lab, num in number.items(): X[lab] = X_train[locations[lab]] # oversampling if printable: tl.logging.info('start oversampling') for key in X: temp = X[key] while True: if len(X[key]) >= most_num: break X[key] = np.vstack((X[key], temp)) if printable: tl.logging.info('first features of label 0 > %d' % len(X[0][0])) tl.logging.info('the occurrence num of each stage after oversampling') for key in X: tl.logging.info("%s %d" % (key, len(X[key]))) if printable: tl.logging.info('make each stage have same num of instances') for key in X: X[key] = X[key][0:most_num, :] tl.logging.info("%s %d" % (key, len(X[key]))) # convert dict to list if printable: tl.logging.info('convert from dict to list format') y_train = [] X_train = np.empty(shape=(0, len(X[0][0]))) for key in X: X_train = np.vstack((X_train, X[key])) y_train.extend([key for i in range(len(X[key]))]) # tl.logging.info(len(X_train), len(y_train)) c = Counter(y_train) if printable: tl.logging.info('the occurrence number of each stage after oversampling: %s' % c.most_common()) # ================ End of Classes balancing return X_train, y_train
def get_random_int(min_v=0, max_v=10, number=5, seed=None): """Return a list of random integer by the given range and quantity. Parameters ----------- min_v : number The minimum value. max_v : number The maximum value. number : int Number of value. seed : int or None The seed for random. Examples --------- >>> r = get_random_int(min_v=0, max_v=10, number=5) [10, 2, 3, 3, 7] """ rnd = random.Random() if seed: rnd = random.Random(seed) # return [random.randint(min,max) for p in range(0, number)] return [rnd.randint(min_v, max_v) for p in range(0, number)]
def list_string_to_dict(string): """Inputs ``['a', 'b', 'c']``, returns ``{'a': 0, 'b': 1, 'c': 2}``.""" dictionary = {} for idx, c in enumerate(string): dictionary.update({c: idx}) return dictionary
def exit_tensorflow(sess=None, port=6006): """Close TensorFlow session, TensorBoard and Nvidia-process if available. Parameters ---------- sess : Session TensorFlow Session. tb_port : int TensorBoard port you want to close, `6006` as default. """ text = "[TL] Close tensorboard and nvidia-process if available" text2 = "[TL] Close tensorboard and nvidia-process not yet supported by this function (tl.ops.exit_tf) on " if sess is not None: sess.close() if _platform == "linux" or _platform == "linux2": tl.logging.info('linux: %s' % text) os.system('nvidia-smi') os.system('fuser ' + port + '/tcp -k') # kill tensorboard 6006 os.system("nvidia-smi | grep python |awk '{print $3}'|xargs kill") # kill all nvidia-smi python process _exit() elif _platform == "darwin": tl.logging.info('OS X: %s' % text) subprocess.Popen( "lsof -i tcp:" + str(port) + " | grep -v PID | awk '{print $2}' | xargs kill", shell=True ) # kill tensorboard elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: tl.logging.info(text2 + _platform)
def open_tensorboard(log_dir='/tmp/tensorflow', port=6006): """Open Tensorboard. Parameters ---------- log_dir : str Directory where your tensorboard logs are saved port : int TensorBoard port you want to open, 6006 is tensorboard default """ text = "[TL] Open tensorboard, go to localhost:" + str(port) + " to access" text2 = " not yet supported by this function (tl.ops.open_tb)" if not tl.files.exists_or_mkdir(log_dir, verbose=False): tl.logging.info("[TL] Log reportory was created at %s" % log_dir) if _platform == "linux" or _platform == "linux2": raise NotImplementedError() elif _platform == "darwin": tl.logging.info('OS X: %s' % text) subprocess.Popen( sys.prefix + " | python -m tensorflow.tensorboard --logdir=" + log_dir + " --port=" + str(port), shell=True ) # open tensorboard in localhost:6006/ or whatever port you chose elif _platform == "win32": raise NotImplementedError("this function is not supported on the Windows platform") else: tl.logging.info(_platform + text2)
def clear_all_placeholder_variables(printable=True): """Clears all the placeholder variables of keep prob, including keeping probabilities of all dropout, denoising, dropconnect etc. Parameters ---------- printable : boolean If True, print all deleted variables. """ tl.logging.info('clear all .....................................') gl = globals().copy() for var in gl: if var[0] == '_': continue if 'func' in str(globals()[var]): continue if 'module' in str(globals()[var]): continue if 'class' in str(globals()[var]): continue if printable: tl.logging.info(" clear_all ------- %s" % str(globals()[var])) del globals()[var]
def set_gpu_fraction(gpu_fraction=0.3): """Set the GPU memory fraction for the application. Parameters ---------- gpu_fraction : float Fraction of GPU memory, (0 ~ 1] References ---------- - `TensorFlow using GPU <https://www.tensorflow.org/versions/r0.9/how_tos/using_gpu/index.html>`__ """ tl.logging.info("[TL]: GPU MEM Fraction %f" % gpu_fraction) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) return sess
def generate_skip_gram_batch(data, batch_size, num_skips, skip_window, data_index=0): """Generate a training batch for the Skip-Gram model. See `Word2Vec example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_word2vec_basic.py>`__. Parameters ---------- data : list of data To present context, usually a list of integers. batch_size : int Batch size to return. num_skips : int How many times to reuse an input to generate a label. skip_window : int How many words to consider left and right. data_index : int Index of the context location. This code use `data_index` to instead of yield like ``tl.iterate``. Returns ------- batch : list of data Inputs. labels : list of data Labels data_index : int Index of the context location. Examples -------- Setting num_skips=2, skip_window=1, use the right and left words. In the same way, num_skips=4, skip_window=2 means use the nearby 4 words. >>> data = [1,2,3,4,5,6,7,8,9,10,11] >>> batch, labels, data_index = tl.nlp.generate_skip_gram_batch(data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0) >>> print(batch) [2 2 3 3 4 4 5 5] >>> print(labels) [[3] [1] [4] [2] [5] [3] [4] [6]] """ # global data_index # you can put data_index outside the function, then # modify the global data_index in the function without return it. # note: without using yield, this code use data_index to instead. if batch_size % num_skips != 0: raise Exception("batch_size should be able to be divided by num_skips.") if num_skips > 2 * skip_window: raise Exception("num_skips <= 2 * skip_window") batch = np.ndarray(shape=(batch_size), dtype=np.int32) labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32) span = 2 * skip_window + 1 # [ skip_window target skip_window ] buffer = collections.deque(maxlen=span) for _ in range(span): buffer.append(data[data_index]) data_index = (data_index + 1) % len(data) for i in range(batch_size // num_skips): target = skip_window # target label at the center of the buffer targets_to_avoid = [skip_window] for j in range(num_skips): while target in targets_to_avoid: target = random.randint(0, span - 1) targets_to_avoid.append(target) batch[i * num_skips + j] = buffer[skip_window] labels[i * num_skips + j, 0] = buffer[target] buffer.append(data[data_index]) data_index = (data_index + 1) % len(data) return batch, labels, data_index
def sample(a=None, temperature=1.0): """Sample an index from a probability array. Parameters ---------- a : list of float List of probabilities. temperature : float or None The higher the more uniform. When a = [0.1, 0.2, 0.7], - temperature = 0.7, the distribution will be sharpen [0.05048273, 0.13588945, 0.81362782] - temperature = 1.0, the distribution will be the same [0.1, 0.2, 0.7] - temperature = 1.5, the distribution will be filtered [0.16008435, 0.25411807, 0.58579758] - If None, it will be ``np.argmax(a)`` Notes ------ - No matter what is the temperature and input list, the sum of all probabilities will be one. Even if input list = [1, 100, 200], the sum of all probabilities will still be one. - For large vocabulary size, choice a higher temperature or ``tl.nlp.sample_top`` to avoid error. """ if a is None: raise Exception("a : list of float") b = np.copy(a) try: if temperature == 1: return np.argmax(np.random.multinomial(1, a, 1)) if temperature is None: return np.argmax(a) else: a = np.log(a) / temperature a = np.exp(a) / np.sum(np.exp(a)) return np.argmax(np.random.multinomial(1, a, 1)) except Exception: # np.set_printoptions(threshold=np.nan) # tl.logging.info(a) # tl.logging.info(np.sum(a)) # tl.logging.info(np.max(a)) # tl.logging.info(np.min(a)) # exit() message = "For large vocabulary_size, choice a higher temperature\ to avoid log error. Hint : use ``sample_top``. " warnings.warn(message, Warning) # tl.logging.info(a) # tl.logging.info(b) return np.argmax(np.random.multinomial(1, b, 1))
def sample_top(a=None, top_k=10): """Sample from ``top_k`` probabilities. Parameters ---------- a : list of float List of probabilities. top_k : int Number of candidates to be considered. """ if a is None: a = [] idx = np.argpartition(a, -top_k)[-top_k:] probs = a[idx] # tl.logging.info("new %f" % probs) probs = probs / np.sum(probs) choice = np.random.choice(idx, p=probs) return choice
def process_sentence(sentence, start_word="<S>", end_word="</S>"): """Seperate a sentence string into a list of string words, add start_word and end_word, see ``create_vocab()`` and ``tutorial_tfrecord3.py``. Parameters ---------- sentence : str A sentence. start_word : str or None The start word. If None, no start word will be appended. end_word : str or None The end word. If None, no end word will be appended. Returns --------- list of str A list of strings that separated into words. Examples ----------- >>> c = "how are you?" >>> c = tl.nlp.process_sentence(c) >>> print(c) ['<S>', 'how', 'are', 'you', '?', '</S>'] Notes ------- - You have to install the following package. - `Installing NLTK <http://www.nltk.org/install.html>`__ - `Installing NLTK data <http://www.nltk.org/data.html>`__ """ if start_word is not None: process_sentence = [start_word] else: process_sentence = [] process_sentence.extend(nltk.tokenize.word_tokenize(sentence.lower())) if end_word is not None: process_sentence.append(end_word) return process_sentence
def create_vocab(sentences, word_counts_output_file, min_word_count=1): """Creates the vocabulary of word to word_id. See ``tutorial_tfrecord3.py``. The vocabulary is saved to disk in a text file of word counts. The id of each word in the file is its corresponding 0-based line number. Parameters ------------ sentences : list of list of str All sentences for creating the vocabulary. word_counts_output_file : str The file name. min_word_count : int Minimum number of occurrences for a word. Returns -------- :class:`SimpleVocabulary` The simple vocabulary object, see :class:`Vocabulary` for more. Examples -------- Pre-process sentences >>> captions = ["one two , three", "four five five"] >>> processed_capts = [] >>> for c in captions: >>> c = tl.nlp.process_sentence(c, start_word="<S>", end_word="</S>") >>> processed_capts.append(c) >>> print(processed_capts) ...[['<S>', 'one', 'two', ',', 'three', '</S>'], ['<S>', 'four', 'five', 'five', '</S>']] Create vocabulary >>> tl.nlp.create_vocab(processed_capts, word_counts_output_file='vocab.txt', min_word_count=1) Creating vocabulary. Total words: 8 Words in vocabulary: 8 Wrote vocabulary file: vocab.txt Get vocabulary object >>> vocab = tl.nlp.Vocabulary('vocab.txt', start_word="<S>", end_word="</S>", unk_word="<UNK>") INFO:tensorflow:Initializing vocabulary from file: vocab.txt [TL] Vocabulary from vocab.txt : <S> </S> <UNK> vocabulary with 10 words (includes start_word, end_word, unk_word) start_id: 2 end_id: 3 unk_id: 9 pad_id: 0 """ tl.logging.info("Creating vocabulary.") counter = Counter() for c in sentences: counter.update(c) # tl.logging.info('c',c) tl.logging.info(" Total words: %d" % len(counter)) # Filter uncommon words and sort by descending count. word_counts = [x for x in counter.items() if x[1] >= min_word_count] word_counts.sort(key=lambda x: x[1], reverse=True) word_counts = [("<PAD>", 0)] + word_counts # 1st id should be reserved for padding # tl.logging.info(word_counts) tl.logging.info(" Words in vocabulary: %d" % len(word_counts)) # Write out the word counts file. with tf.gfile.FastGFile(word_counts_output_file, "w") as f: f.write("\n".join(["%s %d" % (w, c) for w, c in word_counts])) tl.logging.info(" Wrote vocabulary file: %s" % word_counts_output_file) # Create the vocabulary dictionary. reverse_vocab = [x[0] for x in word_counts] unk_id = len(reverse_vocab) vocab_dict = dict([(x, y) for (y, x) in enumerate(reverse_vocab)]) vocab = SimpleVocabulary(vocab_dict, unk_id) return vocab
def read_words(filename="nietzsche.txt", replace=None): """Read list format context from a file. For customized read_words method, see ``tutorial_generate_text.py``. Parameters ---------- filename : str a file path. replace : list of str replace original string by target string. Returns ------- list of str The context in a list (split using space). """ if replace is None: replace = ['\n', '<eos>'] with tf.gfile.GFile(filename, "r") as f: try: # python 3.4 or older context_list = f.read().replace(*replace).split() except Exception: # python 3.5 f.seek(0) replace = [x.encode('utf-8') for x in replace] context_list = f.read().replace(*replace).split() return context_list
def read_analogies_file(eval_file='questions-words.txt', word2id=None): """Reads through an analogy question file, return its id format. Parameters ---------- eval_file : str The file name. word2id : dictionary a dictionary that maps word to ID. Returns -------- numpy.array A ``[n_examples, 4]`` numpy array containing the analogy question's word IDs. Examples --------- The file should be in this format >>> : capital-common-countries >>> Athens Greece Baghdad Iraq >>> Athens Greece Bangkok Thailand >>> Athens Greece Beijing China >>> Athens Greece Berlin Germany >>> Athens Greece Bern Switzerland >>> Athens Greece Cairo Egypt >>> Athens Greece Canberra Australia >>> Athens Greece Hanoi Vietnam >>> Athens Greece Havana Cuba Get the tokenized analogy question data >>> words = tl.files.load_matt_mahoney_text8_dataset() >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True) >>> analogy_questions = tl.nlp.read_analogies_file(eval_file='questions-words.txt', word2id=dictionary) >>> print(analogy_questions) [[ 3068 1248 7161 1581] [ 3068 1248 28683 5642] [ 3068 1248 3878 486] ..., [ 1216 4309 19982 25506] [ 1216 4309 3194 8650] [ 1216 4309 140 312]] """ if word2id is None: word2id = {} questions = [] questions_skipped = 0 with open(eval_file, "rb") as analogy_f: for line in analogy_f: if line.startswith(b":"): # Skip comments. continue words = line.strip().lower().split(b" ") # lowercase ids = [word2id.get(w.strip()) for w in words] if None in ids or len(ids) != 4: questions_skipped += 1 else: questions.append(np.array(ids)) tl.logging.info("Eval analogy file: %s" % eval_file) tl.logging.info("Questions: %d", len(questions)) tl.logging.info("Skipped: %d", questions_skipped) analogy_questions = np.array(questions, dtype=np.int32) return analogy_questions
def build_reverse_dictionary(word_to_id): """Given a dictionary that maps word to integer id. Returns a reverse dictionary that maps a id to word. Parameters ---------- word_to_id : dictionary that maps word to ID. Returns -------- dictionary A dictionary that maps IDs to words. """ reverse_dictionary = dict(zip(word_to_id.values(), word_to_id.keys())) return reverse_dictionary
def build_words_dataset(words=None, vocabulary_size=50000, printable=True, unk_key='UNK'): """Build the words dictionary and replace rare words with 'UNK' token. The most common word has the smallest integer id. Parameters ---------- words : list of str or byte The context in list format. You may need to do preprocessing on the words, such as lower case, remove marks etc. vocabulary_size : int The maximum vocabulary size, limiting the vocabulary size. Then the script replaces rare words with 'UNK' token. printable : boolean Whether to print the read vocabulary size of the given words. unk_key : str Represent the unknown words. Returns -------- data : list of int The context in a list of ID. count : list of tuple and list Pair words and IDs. - count[0] is a list : the number of rare words - count[1:] are tuples : the number of occurrence of each word - e.g. [['UNK', 418391], (b'the', 1061396), (b'of', 593677), (b'and', 416629), (b'one', 411764)] dictionary : dictionary It is `word_to_id` that maps word to ID. reverse_dictionary : a dictionary It is `id_to_word` that maps ID to word. Examples -------- >>> words = tl.files.load_matt_mahoney_text8_dataset() >>> vocabulary_size = 50000 >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size) References ----------------- - `tensorflow/examples/tutorials/word2vec/word2vec_basic.py <https://github.com/tensorflow/tensorflow/blob/r0.7/tensorflow/examples/tutorials/word2vec/word2vec_basic.py>`__ """ if words is None: raise Exception("words : list of str or byte") count = [[unk_key, -1]] count.extend(collections.Counter(words).most_common(vocabulary_size - 1)) dictionary = dict() for word, _ in count: dictionary[word] = len(dictionary) data = list() unk_count = 0 for word in words: if word in dictionary: index = dictionary[word] else: index = 0 # dictionary['UNK'] unk_count += 1 data.append(index) count[0][1] = unk_count reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys())) if printable: tl.logging.info('Real vocabulary size %d' % len(collections.Counter(words).keys())) tl.logging.info('Limited vocabulary size {}'.format(vocabulary_size)) if len(collections.Counter(words).keys()) < vocabulary_size: raise Exception( "len(collections.Counter(words).keys()) >= vocabulary_size , the limited vocabulary_size must be less than or equal to the read vocabulary_size" ) return data, count, dictionary, reverse_dictionary
def words_to_word_ids(data=None, word_to_id=None, unk_key='UNK'): """Convert a list of string (words) to IDs. Parameters ---------- data : list of string or byte The context in list format word_to_id : a dictionary that maps word to ID. unk_key : str Represent the unknown words. Returns -------- list of int A list of IDs to represent the context. Examples -------- >>> words = tl.files.load_matt_mahoney_text8_dataset() >>> vocabulary_size = 50000 >>> data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True) >>> context = [b'hello', b'how', b'are', b'you'] >>> ids = tl.nlp.words_to_word_ids(words, dictionary) >>> context = tl.nlp.word_ids_to_words(ids, reverse_dictionary) >>> print(ids) [6434, 311, 26, 207] >>> print(context) [b'hello', b'how', b'are', b'you'] References --------------- - `tensorflow.models.rnn.ptb.reader <https://github.com/tensorflow/tensorflow/tree/master/tensorflow/models/rnn/ptb>`__ """ if data is None: raise Exception("data : list of string or byte") if word_to_id is None: raise Exception("word_to_id : a dictionary") # if isinstance(data[0], six.string_types): # tl.logging.info(type(data[0])) # # exit() # tl.logging.info(data[0]) # tl.logging.info(word_to_id) # return [word_to_id[str(word)] for word in data] # else: word_ids = [] for word in data: if word_to_id.get(word) is not None: word_ids.append(word_to_id[word]) else: word_ids.append(word_to_id[unk_key]) return word_ids