def decode_libsvm(line): columns = tf.string_split([line], ' ') labels = tf.string_to_number(columns.values[0], out_type=tf.float32) splits = tf.string_split(columns.values[1:], ':') id_vals = tf.reshape(splits.values,splits.dense_shape) feat_ids, feat_vals = tf.split(id_vals,num_or_size_splits=2,axis=1) feat_ids = tf.string_to_number(feat_ids, out_type=tf.int32) feat_vals = tf.string_to_number(feat_vals, out_type=tf.float32) return {"feat_ids": feat_ids, "feat_vals": feat_vals}, labels
def decode_libsvm(line): #columns = tf.decode_csv(value, record_defaults=CSV_COLUMN_DEFAULTS) #features = dict(zip(CSV_COLUMNS, columns)) #labels = features.pop(LABEL_COLUMN) columns = tf.string_split([line], ' ') labels = tf.string_to_number(columns.values[0], out_type=tf.float32) splits = tf.string_split(columns.values[1:], ':') id_vals = tf.reshape(splits.values,splits.dense_shape) feat_ids, feat_vals = tf.split(id_vals,num_or_size_splits=2,axis=1) feat_ids = tf.string_to_number(feat_ids, out_type=tf.int32) feat_vals = tf.string_to_number(feat_vals, out_type=tf.float32) return {"feat_ids": feat_ids, "feat_vals": feat_vals}, labels
def _parse_line(line): """ _parse_line """ line_arr = tf.string_split([line], '\t').values #print(line_arr[2]) Tensor("strided_slice:0", shape=(), dtype=string) user = line_arr[0] label = tf.string_to_number(line_arr[1], out_type=tf.int32) #print(tf.string_split([line_arr[2]]).values) Tensor("StringSplit_1:1", shape=(?,), dtype=string) features = {} features["words"] = tf.string_to_number(tf.string_split([line_arr[2]], ",").values, tf.int32) features["id"] = user return features, label
def testToFloat(self): with self.test_session(): input_string = tf.placeholder(tf.string) output = tf.string_to_number( input_string, out_type=tf.float32) result = output.eval(feed_dict={ input_string: ["0", "3", "-1", "1.12", "0xF", " -10.5", "3.40282e+38", # The next two exceed maximum value for float, so we # expect +/-INF to be returned instead. "3.40283e+38", "-3.40283e+38", "NAN", "INF"] }) self.assertAllClose([0, 3, -1, 1.12, 0xF, -10.5, 3.40282e+38, float("INF"), float("-INF"), float("NAN"), float("INF")], result) with self.assertRaisesOpError(_ERROR_MESSAGE + "10foobar"): output.eval(feed_dict={input_string: ["10foobar"]})
def read_image_unlabeled(filename_queue, raw_img): class StatefarmRecord(object): pass result = StatefarmRecord() # Read a record, getting filenames from the filename_queue. result.key, _ = tf.decode_csv(filename_queue.dequeue(), [[""], [""]], " ") # Extract raw JPG data as a string # raw_contents = tf.read_file(result.key) # raw_contents = raw_img # Decode raw data as a PNG. Defaults to uint8 encoding. # result.uint8image = tf.image.decode_png(raw_contents) result.uint8image = raw_img.astype('uint8') # TENSORFLOW BUG: image shape not statically determined, so force # it to have correct CIFAR100 dimensions # result.uint8image.set_shape((32, 32, 3)) # Kind of hacky, but set a label so we can use the same structure # THIS SHOULD ALWAYS BE IGNORED DURING COMPUTATION, since we are # dealing with unlabaled data result.label = tf.cast(tf.string_to_number("0"), tf.int32) return result
def parse_record(record): columns = tf.decode_csv(record, record_defaults=commons.HEADER_DEFAULTS, field_delim='\t') features = columns[0] target = columns[1:] target = tf.cast(tf.string_to_number(target), dtype=tf.int32) target = tf.stack(target, axis=0) return {commons.FEATURE_COL: features}, target
def decode_libsvm(line): #columns = tf.decode_csv(value, record_defaults=CSV_COLUMN_DEFAULTS) #features = dict(zip(CSV_COLUMNS, columns)) #labels = features.pop(LABEL_COLUMN) columns = tf.string_split([line], ' ') labels = tf.string_to_number(columns.values[0], out_type=tf.float32) splits = tf.string_split(columns.values[1:], ':') id_vals = tf.reshape(splits.values,splits.dense_shape) feat_ids, feat_vals = tf.split(id_vals,num_or_size_splits=2,axis=1) feat_ids = tf.string_to_number(feat_ids, out_type=tf.int32) feat_vals = tf.string_to_number(feat_vals, out_type=tf.float32) #feat_ids = tf.reshape(feat_ids,shape=[-1,FLAGS.field_size]) #for i in range(splits.dense_shape.eval()[0]): # feat_ids.append(tf.string_to_number(splits.values[2*i], out_type=tf.int32)) # feat_vals.append(tf.string_to_number(splits.values[2*i+1])) #return tf.reshape(feat_ids,shape=[-1,field_size]), tf.reshape(feat_vals,shape=[-1,field_size]), labels return {"feat_ids": feat_ids, "feat_vals": feat_vals}, labels
def _create_model(self, dir_name): """Create a simple model that takes 'key', 'num1', 'text1', 'img_url1' input.""" def _decode_jpg(image): img_buf = BytesIO() Image.new('RGB', (16, 16)).save(img_buf, 'jpeg') default_image_string = base64.urlsafe_b64encode(img_buf.getvalue()) image = tf.where(tf.equal(image, ''), default_image_string, image) image = tf.decode_base64(image) image = tf.image.decode_jpeg(image, channels=3) image = tf.reshape(image, [-1]) image = tf.reduce_max(image) return image model_dir = tempfile.mkdtemp() with tf.Session(graph=tf.Graph()) as sess: record_defaults = [ tf.constant([0], dtype=tf.int64), tf.constant([0.0], dtype=tf.float32), tf.constant([''], dtype=tf.string), tf.constant([''], dtype=tf.string), ] placeholder = tf.placeholder(dtype=tf.string, shape=(None,), name='csv_input_placeholder') key_tensor, num_tensor, text_tensor, img_tensor = tf.decode_csv(placeholder, record_defaults) text_tensor = tf.string_to_number(text_tensor, tf.float32) img_tensor = tf.map_fn(_decode_jpg, img_tensor, back_prop=False, dtype=tf.uint8) img_tensor = tf.cast(img_tensor, tf.float32) stacked = tf.stack([num_tensor, text_tensor, img_tensor]) min_tensor = tf.reduce_min(stacked, axis=0) max_tensor = tf.reduce_max(stacked, axis=0) predict_input_tensor = tf.saved_model.utils.build_tensor_info(placeholder) predict_signature_inputs = {"input": predict_input_tensor} predict_output_tensor1 = tf.saved_model.utils.build_tensor_info(min_tensor) predict_output_tensor2 = tf.saved_model.utils.build_tensor_info(max_tensor) predict_key_tensor = tf.saved_model.utils.build_tensor_info(key_tensor) predict_signature_outputs = { 'key': predict_key_tensor, 'var1': predict_output_tensor1, 'var2': predict_output_tensor2 } predict_signature_def = ( tf.saved_model.signature_def_utils.build_signature_def( predict_signature_inputs, predict_signature_outputs, tf.saved_model.signature_constants.PREDICT_METHOD_NAME ) ) signature_def_map = { signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: predict_signature_def } model_dir = os.path.join(self._test_dir, dir_name) builder = tf.saved_model.builder.SavedModelBuilder(model_dir) builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map=signature_def_map) builder.save(False) return model_dir
def parse_fn(self, line, mode): image_path = line[0] label = tf.string_to_number(line[1], out_type=tf.int32) image = tf.read_file(image_path) image = tf.image.decode_image(image, self.config.image_channels) image.set_shape([ self.config.image_height, self.config.image_width, self.config.image_channels ]) image = tf.cast(image, dtype=tf.float32) return image, label
def input_fn(file_name, shuffle=False, repeat_count=1, batch_size=128): n_cpu = multiprocessing.cpu_count() data_set = tf.data.TextLineDataset(filenames=file_name).skip(1) if shuffle: data_set.shuffle(shuffle) data_set = data_set.map(lambda record: parse_record(record), num_parallel_calls=n_cpu).repeat(repeat_count).batch( batch_size=batch_size) iterator = data_set.make_one_shot_iterator() features, target = iterator.get_next() return features, tf.string_to_number(target, out_type=tf.int64)
def parse(line): """Parse a line from the colors dataset.""" # Each line of the dataset is comma-separated and formatted as # color_name, r, g, b # so `items` is a list [color_name, r, g, b]. items = tf.string_split([line], ",").values rgb = tf.string_to_number(items[1:], out_type=tf.float32) / 255. # Represent the color name as a one-hot encoded character sequence. color_name = items[0] chars = tf.one_hot(tf.decode_raw(color_name, tf.uint8), depth=256) # The sequence length is needed by our RNN. length = tf.cast(tf.shape(chars)[0], dtype=tf.int64) return rgb, chars, length
def input_fn(file_name, batch_size=16, shuffle=False, repeat_count=1): num_threads = multiprocessing.cpu_count() data_set = tf.data.TextLineDataset(filenames=file_name).skip(1) if shuffle: data_set = data_set.shuffle(buffer_size=1000) data_set = data_set.map(lambda row: parse_csv_row(row), num_parallel_calls=num_threads).batch(batch_size) \ .repeat(repeat_count).prefetch(1000) iterator = data_set.make_one_shot_iterator() features, target = iterator.get_next() return features, tf.string_to_number(target, out_type=tf.int64)
def testToInt32(self): with self.test_session(): input_string = tf.placeholder(tf.string) output = tf.string_to_number(input_string, out_type=tf.int32) result = output.eval(feed_dict={input_string: ["0", "3", "-1", " -10", "-2147483648", "2147483647"]}) self.assertAllEqual([0, 3, -1, -10, -2147483648, 2147483647], result) with self.assertRaisesOpError(_ERROR_MESSAGE + "2.9"): output.eval(feed_dict={input_string: ["2.9"]}) # The next two exceed maximum value of int32. for in_string in ["-2147483649", "2147483648"]: with self.assertRaisesOpError(_ERROR_MESSAGE + in_string): output.eval(feed_dict={input_string: [in_string]})
def read_image(filename_queue): """Reads and parses examples from CIFAR100 data files. Recommendation: if you want N-way read parallelism, call this function N times. This will give you N independent Readers reading different files & positions within those files, which will give better mixing of examples. Args: filename_queue: A queue of strings with the filenames to read from. Returns: An object representing a single example, with the following fields: height: number of rows in the result (32) width: number of columns in the result (32) depth: number of color channels in the result (3) key: a scalar string Tensor describing the filename & record number for this example. label: an int32 Tensor with the label in the range 0..9. uint8image: a [height, width, depth] uint8 Tensor with the image data """ class CIFAR10Record(object): pass result = CIFAR10Record() # Read a record, getting filenames from the filename_queue. No # header or footer in the CIFAR-10 format, so we leave header_bytes # and footer_bytes at their default of 0. result.key, label = tf.decode_csv(filename_queue.dequeue(), [[""], [""]], " ") # Extract raw PNG data as a string raw_contents = tf.read_file(result.key) # Decode raw data as a PNG. Defaults to uint8 encoding. result.uint8image = tf.image.decode_png(raw_contents) # TENSORFLOW BUG: image shape not statically determined, so force # it to have correct CIFAR100 dimensions result.uint8image.set_shape([32, 32, 3]) # The first bytes represent the label, which we convert from uint8->int32. result.label = tf.cast(tf.string_to_number(label), tf.int32) return result
def __read_imagenet(path, shuffle=True, save_file = 'imagenet_files.csv'): if not os.path.exists(save_file): def class_index(fn): class_id = re.search(r'(n\d+)', fn).group(1) return synset_map[class_id]['index'] file_list = glob.glob(path+'/*/*.JPEG') label_indexes = [] with open(save_file, 'wb') as csv_file: wr = csv.writer(csv_file, quoting=csv.QUOTE_NONE) for f in file_list: idx = class_index(f) label_indexes.append(idx) wr.writerow([f, idx]) with open(save_file, 'rb') as f: reader = csv.reader(f) file_list = list(reader) file_tuple, label_tuple = zip(*file_list) filename, labels = tf.train.slice_input_producer([list(file_tuple), list(label_tuple)], shuffle=shuffle) images = tf.image.decode_jpeg(tf.read_file(filename), channels=3) images = tf.div(tf.add(tf.to_float(images), -127), 128) return images, tf.string_to_number(labels, tf.int32)
def parse_csv_row(row): columns = tf.decode_csv(row, record_defaults=commons.HEADER_DEFAULTS, field_delim='\t') features = dict(zip(commons.HEADERS, columns)) target = features.pop(commons.LABEL_COL) return features, tf.string_to_number(target, out_type=tf.int32)
def MakeDataset(file_path): dataset = tf.data.TextLineDataset(file_path) dataset = dataset.map(lambda line: tf.string_split([line]).values) dataset = dataset.map(lambda str: tf.string_to_number(str, tf.int32)) dataset = dataset.map(lambda x:(x, tf.size(x))) return dataset
# coding: utf-8 import tensorflow as tf import numpy as np with tf.Session() as sess: a = tf.Variable('12') b = tf.string_to_number(a) sess.run(tf.initialize_all_variables()) print(b.eval())
def decode_func(value): return [tf.string_to_number(value, out_type=tf.int32)]
def _dataset_parser(value): """Parse data to a fixed dimension input image and learning targets. Args: value: A dictionary contains an image and groundtruth annotations. Returns: features: a dictionary that contains the image and auxiliary information. The following describes {key: value} pairs in the dictionary. image: Image tensor that is preproessed to have normalized value and fixed dimension [image_size, image_size, 3] image_info: image information that includes the original height and width, the scale of the proccessed image to the original image, and the scaled height and width. source_ids: Source image id. Default value -1 if the source id is empty in the groundtruth annotation. labels: a dictionary that contains auxiliary information plus (optional) labels. The following describes {key: value} pairs in the dictionary. `labels` is only for training. score_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors]. The height_l and width_l represent the dimension of objectiveness score at l-th level. box_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors * 4]. The height_l and width_l represent the dimension of bounding box regression output at l-th level. gt_boxes: Groundtruth bounding box annotations. The box is represented in [y1, x1, y2, x2] format. The tennsor is padded with -1 to the fixed dimension [self._max_num_instances, 4]. gt_classes: Groundtruth classes annotations. The tennsor is padded with -1 to the fixed dimension [self._max_num_instances]. cropped_gt_masks: groundtrugh masks cropped by the bounding box and resized to a fixed size determined by params['gt_mask_size'] """ with tf.name_scope('parser'): data = example_decoder.decode(value) image = data['image'] source_id = data['source_id'] source_id = tf.where(tf.equal(source_id, tf.constant('')), '-1', source_id) source_id = tf.string_to_number(source_id) if self._mode == tf.estimator.ModeKeys.PREDICT: input_processor = InstanceSegmentationInputProcessor( image, image_size) input_processor.normalize_image() input_processor.set_scale_factors_to_output_size() image = input_processor.resize_and_crop_image() if params['use_bfloat16']: image = tf.cast(image, dtype=tf.bfloat16) image_info = input_processor.get_image_info() return {'images': image, 'image_info': image_info, 'source_ids': source_id} elif self._mode == tf.estimator.ModeKeys.TRAIN: instance_masks = None if self._use_instance_mask: instance_masks = data['groundtruth_instance_masks'] boxes = data['groundtruth_boxes'] classes = data['groundtruth_classes'] classes = tf.reshape(tf.cast(classes, dtype=tf.float32), [-1, 1]) if not params['use_category']: classes = tf.cast(tf.greater(classes, 0), dtype=tf.float32) if (params['skip_crowd_during_training'] and self._mode == tf.estimator.ModeKeys.TRAIN): indices = tf.where(tf.logical_not(data['groundtruth_is_crowd'])) classes = tf.gather_nd(classes, indices) boxes = tf.gather_nd(boxes, indices) if self._use_instance_mask: instance_masks = tf.gather_nd(instance_masks, indices) input_processor = InstanceSegmentationInputProcessor( image, image_size, boxes, classes, instance_masks) input_processor.normalize_image() if params['input_rand_hflip']: input_processor.random_horizontal_flip() input_processor.set_training_random_scale_factors( params['train_scale_min'], params['train_scale_max']) image = input_processor.resize_and_crop_image() boxes, classes = input_processor.resize_and_crop_boxes() if self._use_instance_mask: instance_masks = input_processor.resize_and_crop_masks() cropped_gt_masks = input_processor.crop_gt_masks( instance_masks, boxes, params['gt_mask_size'], image_size) # Assign anchors. score_targets, box_targets = anchor_labeler.label_anchors( boxes, classes) # Pad groundtruth data. image_info = input_processor.get_image_info() boxes *= image_info[2] boxes = pad_to_fixed_size(boxes, -1, [self._max_num_instances, 4]) classes = pad_to_fixed_size(classes, -1, [self._max_num_instances, 1]) # Pads cropped_gt_masks. if self._use_instance_mask: cropped_gt_masks = tf.reshape( cropped_gt_masks, [self._max_num_instances, -1]) cropped_gt_masks = pad_to_fixed_size( cropped_gt_masks, -1, [self._max_num_instances, (params['gt_mask_size'] + 4) ** 2]) cropped_gt_masks = tf.reshape( cropped_gt_masks, [self._max_num_instances, params['gt_mask_size'] + 4, params['gt_mask_size'] + 4]) if params['use_bfloat16']: image = tf.cast(image, dtype=tf.bfloat16) features = {} features['images'] = image features['image_info'] = image_info features['source_ids'] = source_id labels = {} for level in range(params['min_level'], params['max_level'] + 1): labels['score_targets_%d' % level] = score_targets[level] labels['box_targets_%d' % level] = box_targets[level] labels['gt_boxes'] = boxes labels['gt_classes'] = classes if self._use_instance_mask: labels['cropped_gt_masks'] = cropped_gt_masks return (features, labels)
def _dataset_parser(value): """Parse data to a fixed dimension input image and learning targets. Args: value: A dictionary contains an image and groundtruth annotations. Returns: image: Image tensor that is preprocessed to have normalized value and fixed dimension [image_size, image_size, 3] cls_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors]. The height_l and width_l represent the dimension of class logits at l-th level. box_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors * 4]. The height_l and width_l represent the dimension of bounding box regression output at l-th level. num_positives: Number of positive anchors in the image. source_id: Source image id. Default value -1 if the source id is empty in the groundtruth annotation. image_scale: Scale of the processed image to the original image. boxes: Groundtruth bounding box annotations. The box is represented in [y1, x1, y2, x2] format. The tensor is padded with -1 to the fixed dimension [self._max_num_instances, 4]. is_crowds: Groundtruth annotations to indicate if an annotation represents a group of instances by value {0, 1}. The tensor is padded with 0 to the fixed dimension [self._max_num_instances]. areas: Groundtruth areas annotations. The tensor is padded with -1 to the fixed dimension [self._max_num_instances]. classes: Groundtruth classes annotations. The tensor is padded with -1 to the fixed dimension [self._max_num_instances]. """ data = example_decoder.decode(value) source_id = data['source_id'] image = data['image'] boxes = data['groundtruth_boxes'] classes = data['groundtruth_classes'] classes = tf.reshape(tf.cast(classes, dtype=tf.float32), [-1, 1]) areas = data['groundtruth_area'] is_crowds = data['groundtruth_is_crowd'] classes = tf.reshape(tf.cast(classes, dtype=tf.float32), [-1, 1]) input_processor = DetectionInputProcessor(image, params['image_size'], boxes, classes) input_processor.normalize_image() input_processor.set_scale_factors_to_output_size() image = input_processor.resize_and_crop_image() boxes, classes = input_processor.resize_and_crop_boxes() # Assign anchors. (cls_targets, box_targets, num_positives) = anchor_labeler.label_anchors(boxes, classes) source_id = tf.where(tf.equal(source_id, tf.constant('')), '-1', source_id) source_id = tf.string_to_number(source_id) # Pad groundtruth data for evaluation. image_scale = input_processor.image_scale_to_original boxes *= image_scale is_crowds = tf.cast(is_crowds, dtype=tf.float32) boxes = pad_to_fixed_size(boxes, -1, [MAX_NUM_INSTANCES, 4]) is_crowds = pad_to_fixed_size(is_crowds, 0, [MAX_NUM_INSTANCES, 1]) areas = pad_to_fixed_size(areas, -1, [MAX_NUM_INSTANCES, 1]) classes = pad_to_fixed_size(classes, -1, [MAX_NUM_INSTANCES, 1]) if params['use_bfloat16']: image = tf.cast(image, dtype=tf.bfloat16) return (image, cls_targets, box_targets, num_positives, source_id, image_scale, boxes, is_crowds, areas, classes)
def __convert2int(x, y): int_x = tf.to_int32(tf.string_to_number((tf.string_split([x]).values))) int_y = tf.to_int32(tf.string_to_number((tf.string_split([y]).values))) return int_x, int_y
def process_dataset(self, *row_parts): row_parts = list(row_parts) if self.indexed: index = tf.string_to_number(row_parts[0], tf.int32) row_parts = row_parts[1:] else: index = tf.constant(0) word = row_parts[0] # (, ) if not self.is_evaluating and self.config.RANDOM_CONTEXTS: all_contexts = tf.stack(row_parts[1:]) all_contexts_padded = tf.concat([all_contexts, [self.context_pad]], axis=-1) index_of_blank_context = tf.where( tf.equal(all_contexts_padded, self.context_pad)) num_contexts_per_example = tf.reduce_min(index_of_blank_context) # if there are less than self.max_contexts valid contexts, still sample self.max_contexts safe_limit = tf.cast( tf.maximum(num_contexts_per_example, self.config.MAX_CONTEXTS), tf.int32) rand_indices = tf.random_shuffle( tf.range(safe_limit))[:self.config.MAX_CONTEXTS] contexts = tf.gather(all_contexts, rand_indices) # (max_contexts,) else: contexts = row_parts[1:(self.config.MAX_CONTEXTS + 1)] # (max_contexts,) # contexts: (max_contexts, ) split_contexts = tf.string_split(contexts, delimiter=',', skip_empty=False) sparse_split_contexts = tf.sparse.SparseTensor( indices=split_contexts.indices, values=split_contexts.values, dense_shape=[self.config.MAX_CONTEXTS, 3]) dense_split_contexts = tf.reshape( tf.sparse.to_dense(sp_input=sparse_split_contexts, default_value=Common.PAD), shape=[ self.config.MAX_CONTEXTS, 3 ]) # (batch, max_contexts, 3) # should be (max_contexts, 3)??? # word -> target_word_labels ------------------------------------------------------------------------------- split_target_labels = tf.string_split(tf.expand_dims(word, -1), delimiter='|') target_dense_shape = [ 1, tf.maximum(tf.to_int64(self.config.MAX_TARGET_PARTS), split_target_labels.dense_shape[1] + 1) ] sparse_target_labels = tf.sparse.SparseTensor( indices=split_target_labels.indices, values=split_target_labels.values, dense_shape=target_dense_shape) dense_target_label = tf.reshape( tf.sparse.to_dense(sp_input=sparse_target_labels, default_value=Common.PAD), [-1]) index_of_blank = tf.where(tf.equal(dense_target_label, Common.PAD)) target_length = tf.reduce_min(index_of_blank) dense_target_label = dense_target_label[:self.config.MAX_TARGET_PARTS] clipped_target_lengths = tf.clip_by_value( target_length, clip_value_min=0, clip_value_max=self.config.MAX_TARGET_PARTS) target_word_labels = tf.concat( [self.target_table.lookup(dense_target_label), [0]], axis=-1) # (max_target_parts + 1) of int # ---------------------------------------------------------------------------------------------------------- path_source_strings = tf.slice( dense_split_contexts, [0, 0], [self.config.MAX_CONTEXTS, 1]) # (max_contexts, 1) flat_source_strings = tf.reshape(path_source_strings, [-1]) # (max_contexts) split_source = tf.string_split( flat_source_strings, delimiter='|', skip_empty=False) # (max_contexts, max_name_parts) sparse_split_source = tf.sparse.SparseTensor( indices=split_source.indices, values=split_source.values, dense_shape=[ self.config.MAX_CONTEXTS, tf.maximum(tf.to_int64(self.config.MAX_NAME_PARTS), split_source.dense_shape[1]) ]) dense_split_source = tf.sparse.to_dense( sp_input=sparse_split_source, default_value=Common.PAD) # (max_contexts, max_name_parts) dense_split_source = tf.slice(dense_split_source, [0, 0], [-1, self.config.MAX_NAME_PARTS]) path_source_indices = self.subtoken_table.lookup( dense_split_source) # (max_contexts, max_name_parts) path_source_lengths = tf.reduce_sum( tf.cast(tf.not_equal(dense_split_source, Common.PAD), tf.int32), -1) # (max_contexts) path_strings = tf.slice(dense_split_contexts, [0, 1], [self.config.MAX_CONTEXTS, 1]) flat_path_strings = tf.reshape(path_strings, [-1]) split_path = tf.string_split(flat_path_strings, delimiter='|', skip_empty=False) sparse_split_path = tf.sparse.SparseTensor( indices=split_path.indices, values=split_path.values, dense_shape=[ self.config.MAX_CONTEXTS, self.config.MAX_PATH_LENGTH ]) dense_split_path = tf.sparse.to_dense( sp_input=sparse_split_path, default_value=Common.PAD) # (batch, max_contexts, max_path_length) node_indices = self.node_table.lookup( dense_split_path) # (max_contexts, max_path_length) path_lengths = tf.reduce_sum( tf.cast(tf.not_equal(dense_split_path, Common.PAD), tf.int32), -1) # (max_contexts) path_target_strings = tf.slice( dense_split_contexts, [0, 2], [self.config.MAX_CONTEXTS, 1]) # (max_contexts, 1) flat_target_strings = tf.reshape(path_target_strings, [-1]) # (max_contexts) split_target = tf.string_split( flat_target_strings, delimiter='|', skip_empty=False) # (max_contexts, max_name_parts) sparse_split_target = tf.sparse.SparseTensor( indices=split_target.indices, values=split_target.values, dense_shape=[ self.config.MAX_CONTEXTS, tf.maximum(tf.to_int64(self.config.MAX_NAME_PARTS), split_target.dense_shape[1]) ]) dense_split_target = tf.sparse.to_dense( sp_input=sparse_split_target, default_value=Common.PAD) # (max_contexts, max_name_parts) dense_split_target = tf.slice(dense_split_target, [0, 0], [-1, self.config.MAX_NAME_PARTS]) path_target_indices = self.subtoken_table.lookup( dense_split_target) # (max_contexts, max_name_parts) path_target_lengths = tf.reduce_sum( tf.cast(tf.not_equal(dense_split_target, Common.PAD), tf.int32), -1) # (max_contexts) valid_contexts_mask = tf.to_float( tf.not_equal( tf.reduce_max(path_source_indices, -1) + tf.reduce_max(node_indices, -1) + tf.reduce_max(path_target_indices, -1), 0)) return { TARGET_STRING_KEY: word, TARGET_INDEX_KEY: target_word_labels, TARGET_LENGTH_KEY: clipped_target_lengths, PATH_SOURCE_INDICES_KEY: path_source_indices, NODE_INDICES_KEY: node_indices, PATH_TARGET_INDICES_KEY: path_target_indices, VALID_CONTEXT_MASK_KEY: valid_contexts_mask, PATH_SOURCE_LENGTHS_KEY: path_source_lengths, PATH_LENGTHS_KEY: path_lengths, PATH_TARGET_LENGTHS_KEY: path_target_lengths, PATH_SOURCE_STRINGS_KEY: path_source_strings, PATH_STRINGS_KEY: path_strings, PATH_TARGET_STRINGS_KEY: path_target_strings, INDEX_KEY: index }
def to_ids(raw, hashtable): raw_str = tf.string_split([raw]).values return tf.string_to_number(raw_str, tf.int32)
def get_final_training_input(filenames, params): with tf.device("/cpu:0"): text_dataset = tf.data.TextLineDataset(filenames[0]) aspect_dataset = tf.data.TextLineDataset(filenames[1]) polarity_dataset = tf.data.TextLineDataset(filenames[2]) attention_value_dataset = tf.data.TextLineDataset(filenames[3]) attention_mask_dataset = tf.data.TextLineDataset(filenames[4]) dataset = tf.data.Dataset.zip( (text_dataset, aspect_dataset, polarity_dataset, attention_value_dataset, attention_mask_dataset)) dataset = dataset.shuffle(params.buffer_size) dataset = dataset.repeat() dataset = dataset.map( lambda text, aspect, polarity, attention_value, attention_mask: (tf.string_split([text]).values, tf.string_split([aspect]).values, tf.string_split([polarity]).values, tf.string_split([attention_value]).values, tf.string_split([attention_mask]).values), num_parallel_calls=params.num_threads) dataset = dataset.map( lambda text, aspect, polarity, attention_value, attention_mask: { "text": text, "aspect": aspect, "polarity": tf.string_to_number(polarity), "attention_value": tf.string_to_number(attention_value), "attention_mask": tf.string_to_number(attention_mask), "text_length": tf.shape(text), "aspect_length": tf.shape(aspect) }, num_parallel_calls=params.num_threads) iterator = dataset.make_one_shot_iterator() features = iterator.get_next() table = tf.contrib.lookup.index_table_from_tensor( tf.constant(params.vocabulary), default_value=params.mapping[params.unk]) features["text"] = table.lookup(features["text"]) features["aspect"] = table.lookup(features["aspect"]) shard_multiplier = len(params.device_list) * params.update_cycle features = batch_examples(features, params.batch_size, params.max_length, params.mantissa_bits, shard_multiplier=shard_multiplier, length_multiplier=params.length_multiplier, constant=params.constant_batch_size, num_threads=params.num_threads) features["text"] = tf.to_int32(features["text"]) features["aspect"] = tf.to_int32(features["aspect"]) features["polarity"] = tf.to_int32(features["polarity"]) features["attention_value"] = tf.to_float(features["attention_value"]) features["attention_mask"] = tf.to_float(features["attention_mask"]) features["text_length"] = tf.to_int32(features["text_length"]) features["aspect_length"] = tf.to_int32(features["aspect_length"]) features["text_length"] = tf.squeeze(features["text_length"], 1) features["aspect_length"] = tf.squeeze(features["aspect_length"], 1) return features
def decode_libsvm(line): #columns = tf.decode_csv(value, record_defaults=CSV_COLUMN_DEFAULTS) #features = dict(zip(CSV_COLUMNS, columns)) #labels = features.pop(LABEL_COLUMN) # todo tf.string_split函数 # tf.string_split( # source, # delimiter=' ', # skip_empty=True # ) # ''' # @函数意义:将基于 delimiter 的 source 的元素拆分为 SparseTensor # @source:需要操作的对象,一般是[字符串或者多个字符串]构成的列表;---注意是列表哦!!! # @delimiter:分割符,默认空字符串 # @skip_empty:默认True,暂时没用到过 # ''' # demo # # 当对象是一个字符串 # a = 'we do it' # tf.string_split([a]) # # 返回值如下 # SparseTensorValue(indices=array([[0, 0],[0, 1],[0, 2]]), # values=array(['we', 'do', 'it'], dtype=object), # dense_shape=array([1, 3])) # # 当对象是多个字符串 # b = 'we can do it' # c = [a,b] # tf.string_split(c) # # 返回值如下 # SparseTensorValue(indices=array([[0, 0], # [0, 1], # [0, 2], # [1, 0], # [1, 1], # [1, 2], # [1, 3]], dtype=int64), values=array(['we', 'do', 'it', 'we', 'can', 'do', 'it'], dtype=object), dense_shape=array([2, 4], dtype=int64)) # 可以看到几个要点: # 1.传入的元素是字符串,但是必须是列表包括进去,不然会报格式错误! # 2.返回了稀疏矩阵(SparseTensorValue)的下标(indices),和值(value),以及类型,和输入数据的维度(dense_shape) # 3.到这一步已经很明显了,这个函数有split()的作用,可以从value获取我们要的东西。 # 返回值有三个参数,一个是indices,一个是values,一个是dense_shape. columns = tf.string_split([line], ' ') labels = tf.string_to_number(columns.values[0], out_type=tf.float32) splits = tf.string_split(columns.values[1:], ':') # todo 这里没有index就直接reshape成 id_vals = tf.reshape(splits.values,splits.dense_shape) # todo splits= SparseTensor(indices=Tensor("StringSplit_1:0", shape=(?, 2), dtype=int64), values=Tensor("StringSplit_1:1", shape=(?,), dtype=string), dense_shape=Tensor("StringSplit_1:2", shape=(2,), dtype=int64)) # Tensor("StringSplit_1:1", shape=(?,), dtype=string) # Tensor("StringSplit_1:2", shape=(2,), dtype=int64) # Tensor("Reshape:0", shape=(?, ?), dtype=string) # print("splits=",splits,splits.values,splits.dense_shape,id_vals) # todo tf.split(value,num_or_size_splits,axis=0,num=None,name='split') # https://blog.csdn.net/mls0311/article/details/82052472 # value:准备切分的张量 # num_or_size_splits:准备切成几份 # axis : 准备在第几个维度上进行切割 # 其中分割方式分为两种 # 1. 如果num_or_size_splits 传入的 是一个整数,那直接在axis=D这个维度上把张量平均切分成几个小张量 # 2. 如果num_or_size_splits 传入的是一个向量(这里向量各个元素的和要跟原本这个维度的数值相等)就根据这个向量有几个元素分为几项) feat_ids, feat_vals = tf.split(id_vals,num_or_size_splits=2,axis=1) feat_ids = tf.string_to_number(feat_ids, out_type=tf.int32) feat_vals = tf.string_to_number(feat_vals, out_type=tf.float32) #feat_ids = tf.reshape(feat_ids,shape=[-1,FLAGS.field_size]) #for i in range(splits.dense_shape.eval()[0]): # feat_ids.append(tf.string_to_number(splits.values[2*i], out_type=tf.int32)) # feat_vals.append(tf.string_to_number(splits.values[2*i+1])) #return tf.reshape(feat_ids,shape=[-1,field_size]), tf.reshape(feat_vals,shape=[-1,field_size]), labels return {"feat_ids": feat_ids, "feat_vals": feat_vals}, labels
result = sess.run(fetches=[a, c]) # result类型为 <class 'numpy.ndarray'> print(result) # 任务关闭 sess.close() # 会报错 关闭后不能再用了 # sess.run(c) # 用with模块来自动关闭 with tf.Session() as sese2: # result2 = sese2.run(c) # 可以传入fetches 参数 result2 = c.eval() # 这一句与上面一句有相同的作用,直接返回c的值 print(result2) # 对config中参数的设置 a = tf.constant('10', tf.string, name='a_const') b = tf.string_to_number(a, out_type=tf.float64, name='str_2_double') c = tf.to_double(5.0, name='to_double') d = tf.add(b, c, name='add') # 构建Session并执行图 # 1. 构建GPU 相关参数 gpu_options = tf.GPUOptions() # per_process_gpu_memory_fraction:给定对于每一个进程,分配多少的GPU内存,默认是1 # 设置为0.5 表示分配50%的GPU内存 gpu_options.per_process_gpu_memory_fraction = 0.5 # allow_growth: 设置为True表示在进行GPU内存分配的时候,采用动态分配方式,默认为False # 动态分配的意思是指,在启动之前,不分配全部内存,根据需要,后面动态的进行内存分配, # 在启动动态分配后,GPU内存不会自动释放(故:复杂、长时间运行的任务不建议设置为True), --False时会自动释放 gpu_options.allow_growth = True
def read_numpy_format_and_label(filename_queue): # Tricks to make the batch have # almost same caption length # We should group it by using dequeue_many and enqueue many # from sorted list. filename_and_label_tensor = filename_queue.dequeue_many( FLAGS.batch_size ) batch_filename, batch_context_length, batch_topic_length, batch_caption_length, \ batch_context, batch_topic, batch_caption = tf.decode_csv( filename_and_label_tensor, [[""], [""], [""], [""], [""], [""], [""]] ) batch_context_length = tf.minimum( tf.cast( tf.string_to_number(batch_context_length), tf.int32 )+1, FLAGS.max_context_length ) batch_caption_length = tf.minimum( tf.cast( tf.string_to_number(batch_caption_length), tf.int32 )+1, FLAGS.max_output_length ) batch_topic_length = tf.minimum( tf.cast( tf.string_to_number(batch_topic_length), tf.int32
def decode_record(record): src = tf.string_split([record]).values src = tf.string_to_number(src, out_type=tf.float32) return src, tf.constant([SOS], dtype=tf.int32)
def __init__(self, filenames_file, data_folder): self.ref_images = None self.current_images = None self.old_images = None self.old_to_current = None self.init_height = None self.init_width = None self.data_folder = None with tf.variable_scope("inputloader"): self.data_folder = tf.constant(data_folder) input_queue = tf.train.string_input_producer([filenames_file], shuffle = True) line_reader = tf.TextLineReader() _, line = line_reader.read(input_queue) split_line = tf.string_split([line]).values offset = 0 current_width = tf.string_to_number(split_line[offset], tf.int32) offset += 1 current_height = tf.string_to_number(split_line[offset], tf.int32) # Load the reference image offset += 1 ref_img = self.read_jpg(split_line[offset]) ref_img.set_shape([self.init_height, self.init_width, 3]) # Augment the reference image flip_vert = tf.random_uniform([], 0, 1) flip_horiz = tf.random_uniform([], 0, 1) rotate_img = tf.random_uniform([], 0, 1) crop_x = tf.random_uniform([], 0, current_width - crop, dtype=tf.int32) crop_y = tf.random_uniform([], 0, current_height - crop, dtype=tf.int32) asserts_ref = [ tf.assert_greater_equal(current_width, crop, message="Current width smaller than crop size"), tf.assert_greater_equal(current_height, crop, message="Current height smaller than crop size"), tf.assert_greater_equal(tf.shape(ref_img)[0], crop_y + crop, message="Reference height smaller than crop size"), tf.assert_greater_equal(tf.shape(ref_img)[1], crop_x + crop, message="Reference width smaller than crop size")] with tf.control_dependencies(asserts_ref): ref_img = tf.image.crop_to_bounding_box(ref_img, crop_y, crop_x, crop, crop) ref_img = augment_image(ref_img, flip_vert, flip_horiz, rotate_img) # Optionally: Load the optical flow between the two temporal frames offset += 1 flow_img = tf.zeros_like(ref_img) if use_temporal_loss: flow_img = self.read_flow(split_line[offset]) # Also augment the current-to-previous optical flow asserts_flow = [ tf.assert_greater_equal(tf.shape(flow_img)[0], crop_y + crop, message="Temporal flow height smaller than crop size"), tf.assert_greater_equal(tf.shape(flow_img)[1], crop_x + crop, message="Temporal flow width smaller than crop size")] with tf.control_dependencies(asserts_flow): flow_img = tf.image.crop_to_bounding_box(flow_img, crop_y, crop_x, crop, crop) flow_img = augment_flow(flow_img, flip_vert, flip_horiz, rotate_img) # Always load two temporal frames for path samples -- we'll ignore these # later during training if use_temporal_loss is False. num_path_samples = 2 frames_color = [] offset += 1 for j in range(num_path_samples): # Our training data has 5 input layers per path sample: # global_colors and local_layer_N_colors (where N=0...4) path_sample_offset = 5 * j # First populate the CNN inputs with data from the global mesh if use_global_mesh: global_color = self.read_jpg(split_line[offset + path_sample_offset + 0]) global_color.set_shape([self.init_height, self.init_width, 3]) # Make sure to apply the same data augmentation to the textured global mesh layer asserts_global = [ tf.assert_greater_equal(tf.shape(global_color)[0], crop_y + crop, message="Textured mesh height smaller than crop size"), tf.assert_greater_equal(tf.shape(global_color)[1], crop_x + crop, message="Textured mesh width smaller than crop size")] with tf.control_dependencies(asserts_global): global_color = tf.image.crop_to_bounding_box(global_color, crop_y, crop_x, crop, crop) global_color = augment_image(global_color, flip_vert, flip_horiz, rotate_img) frames_color.append([global_color]) else: frames_color.append([]) # Index where the image mosaic layers begin (+1 is for the textured mesh layer above) mosaic_layers_begin = offset + path_sample_offset + 1 for i in range(num_input_layers): colors = self.read_jpg(split_line[mosaic_layers_begin + i]) colors.set_shape([self.init_height, self.init_width, 3]) # Augment the image mosaics asserts_local = [ tf.assert_greater_equal(tf.shape(colors)[0], crop_y + crop, message="Mosaic height smaller than crop size"), tf.assert_greater_equal(tf.shape(colors)[1], crop_x + crop, message="Mosaic width smaller than crop size")] with tf.control_dependencies(asserts_local): colors = tf.image.crop_to_bounding_box(colors, crop_y, crop_x, crop, crop) colors = augment_image(colors, flip_vert, flip_horiz, rotate_img) frames_color[-1].append(colors) images_current = tf.concat(frames_color[0], axis=2) images_previous = tf.concat(frames_color[1], axis=2) dataloader_threads = 8 min_after_dequeue = 16 capacity = min_after_dequeue + 4 * batch_size if use_temporal_loss: self.ref_images, self.current_images, self.old_images, self.old_to_current = \ tf.train.shuffle_batch([ref_img, images_current, images_previous, flow_img], batch_size, capacity, min_after_dequeue, dataloader_threads) else: self.ref_images, self.current_images, self.old_images = \ tf.train.shuffle_batch([ref_img, images_current, images_previous], batch_size, capacity, min_after_dequeue, dataloader_threads)
cls = [] filename = "dataset.csv" filename2 = "test.csv" #filename = "dataset.csv" sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) #print((result1)) #print((result2)) with open(filename) as fl: for line in fl: i = i + 1 value, col2, col3, col4 = line.strip().split(",") col2 = tf.string_to_number(col2) col3 = tf.string_to_number(col3) col4 = tf.string_to_number(col4) batch_ys = np.array([[sess.run(col2), sess.run(col3), sess.run(col4)]]) batch_xs = np.ones((1, 1)) #print(batch_xs,batch_ys,value) sess.run(train, feed_dict={xs: batch_xs, ys: batch_ys}) print(sess.run(cross_entropy, feed_dict={ xs: batch_xs, ys: batch_ys }), "CROSS ENTROPY") if i % 20 == 0: # print((np.shape(batch_ys)))
def create_dataset(file): dataset = tf.data.TextLineDataset(pjoin(DATA_DIR, file)) string_split = dataset.map(lambda string: tf.string_split([string]).values) integer_dataset = string_split.map( lambda x: tf.string_to_number(x, out_type=tf.int32)) return integer_dataset
((src_input, src_len),(trg_label,trg_len)) = (src_tuple, trg_tuple) trg_input = tf.concat([[SOS_ID], trg_label[:-1]], axis=0) return ((src_input, src_len),(trg_input, trg_label, trg_len)) dataset = dataset.map(dup_target_input) # shape: [example, ((array[len1],1),(array[len2],array[len2],1))] dataset = dataset.shuffle(10000) padding_shape = ((tf.TensorShape([None]), tf.TensorShape([])), (tf.TensorShape([None]), tf.TensorShape([None]), tf.TensorShape([]))) # [example/batch, (([batch,len1],[batch]),([batch,len2],[batch,len2],[batch]))] dataset = dataset.padded_batch(batch_size, padding_shape) return dataset if __name__ == "__main__": a0 = tf.constant("1 2 3",dtype=tf.string) a1 = tf.string_split([a0]).values a2 = tf.string_to_number(a1,tf.int32) with tf.Session() as s: tf.global_variables_initializer().run() print(s.run([a0, a1, a2])) print("") # DATA_ROOT = "/Users/yxd/Downloads/en-zh/" DATA_ROOT = "E:/Download/en-zh/" src_path = DATA_ROOT + "train.code.en" trg_path = DATA_ROOT + "train.code.zh" batch_size = 3 ds = MakeSrcTrgDataset(src_path, trg_path, batch_size) iter = ds.make_one_shot_iterator() rec = iter.get_next() with tf.Session() as s:
def _str2id(self, line): line = tf.string_split([line]).values line = tf.string_to_number(line, tf.int32) return line
tf.inv(a)#取反 tf.square(a)#计算平方 tf.round(d)#舍入最接近的整数 tf.sqrt(a)#开方 tf.pow(a,b)#a的b次方 tf.exp(a)#e的a次方 tf.log(a)#一次输入是以e为底a的对数,两次输入是以第二个为底 tf.maximum(a,b)#返回最大值 tf.minimum(a,b)#返回最小值 tf.cos(a)#三角函数cos #数据类型转换 e = tf.constant("abcde") tf.string_to_number(e)#字符串转换为数字 tf.to_double(a) tf.cast(a)#转换为整数,比如1.8 = 1,2.2 = 2 #形状操作 tf.shape()#返回数据的shape tf.size()#返回数据的元素数量 tf.rank()#返回tensor的rank tf.reshape()#改变tensor的形状 tf.expand_dims()#插入维度1进入一个tensor #切片与合并 tf.slice()#切片操作 tf.split()#沿着某一维度将tensor分离 tf.concat()#沿着某一维度连接 tf.pack()#将一系列rank-R的tensor打包为一个rank-(R+1)的tensor
def load_examples(): if a.input_dir is None or not os.path.exists(a.input_dir): raise Exception("input_dir does not exist") input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg")) decode = tf.image.decode_jpeg if len(input_paths) == 0: input_paths = glob.glob(os.path.join(a.input_dir, "*.png")) decode = tf.image.decode_png if len(input_paths) == 0: raise Exception("input_dir contains no image files") def get_name(path): name, _ = os.path.splitext(os.path.basename(path)) return name # if the image names are numbers, sort by the value rather than asciibetically # having sorted inputs means that the outputs are sorted in test mode if all(get_name(path).isdigit() for path in input_paths): input_paths = sorted(input_paths, key=lambda path: int(get_name(path))) else: input_paths = sorted(input_paths) with tf.name_scope("load_images"): path_queue = tf.train.string_input_producer(input_paths, shuffle=a.mode == "train") reader = tf.WholeFileReader() paths, contents = reader.read(path_queue) #paths = tf.Print(paths, [paths], message="paths:") raw_input = decode(contents) raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32) img_path = tf.string_split([paths], delimiter='/').values[-1] classes = tf.string_to_number(tf.string_split([img_path], delimiter='_').values[0], out_type=tf.int32) # NOTE: may want to use one hots instead of numbers #classes = tf.one_hot(classes, NUM_CLASSES) # or NUM_CLASSES*2 if we want the full real/fake one hot #classes = tf.Print(classes, [img_path, classes], message="one hot", summarize=NUM_CLASSES) shape = classes.get_shape().dims #f.shape(classes) classes_real = classes classes_fake = tf.add(classes, tf.constant(NUM_CLASSES, shape=shape)) assertion = tf.assert_equal(tf.shape(raw_input)[2], 3, message="image does not have 3 channels") with tf.control_dependencies([assertion]): raw_input = tf.identity(raw_input) raw_input.set_shape([None, None, 3]) if a.lab_colorization: # load color and brightness from image, no B image exists here lab = rgb_to_lab(raw_input) L_chan, a_chan, b_chan = preprocess_lab(lab) a_images = tf.expand_dims(L_chan, axis=2) b_images = tf.stack([a_chan, b_chan], axis=2) else: # break apart image pair and move to range [-1, 1] width = tf.shape(raw_input)[1] # [height, width, channels] a_images = preprocess(raw_input[:, :width // 2, :]) b_images = preprocess(raw_input[:, width // 2:, :]) #print(raw_input.shape, len(classes)) if a.which_direction == "AtoB": inputs, targets = [a_images, b_images] elif a.which_direction == "BtoA": inputs, targets = [b_images, a_images] else: raise Exception("invalid direction") # synchronize seed for image operations so that we do the same operations to both # input and output images seed = random.randint(0, 2**31 - 1) def transform(image): r = image if a.flip: r = tf.image.random_flip_left_right(r, seed=seed) # area produces a nice downscaling, but does nearest neighbor for upscaling # assume we're going to be doing downscaling here r = tf.image.resize_images(r, [a.scale_size, a.scale_size], method=tf.image.ResizeMethod.AREA) offset = tf.cast(tf.floor( tf.random_uniform([2], 0, a.scale_size - CROP_SIZE + 1, seed=seed)), dtype=tf.int32) if a.scale_size > CROP_SIZE: r = tf.image.crop_to_bounding_box(r, offset[0], offset[1], CROP_SIZE, CROP_SIZE) elif a.scale_size < CROP_SIZE: raise Exception("scale size cannot be less than crop size") return r with tf.name_scope("input_images"): input_images = transform(inputs) with tf.name_scope("target_images"): target_images = transform(targets) paths_batch, inputs_batch, targets_batch, classes_real_batch, classes_fake_batch = tf.train.batch( [paths, input_images, target_images, classes_real, classes_fake], batch_size=a.batch_size) steps_per_epoch = int(math.ceil(len(input_paths) / a.batch_size)) return Examples( paths=paths_batch, inputs=inputs_batch, targets=targets_batch, classes_real=classes_real_batch, classes_fake=classes_fake_batch, count=len(input_paths), steps_per_epoch=steps_per_epoch, )
class CIFAR10Record(object): pass result = CIFAR10Record() result.height = 256 result.width = 320 result.depth = 3 with open('train.txt') as fid: content = fid.read() content = content.split('\n') content = content[:-1] #消除换行符\n的影响 valuequeue = tf.train.string_input_producer(content,shuffle=True) value = valuequeue.dequeue() dir, label1,label2,label3,label4 = tf.decode_csv(records=value, record_defaults=[['string'], [''],[''],[''],['']], field_delim=" ") label1 = tf.string_to_number(label1, tf.float32) label2 = tf.string_to_number(label2, tf.float32) label3 = tf.string_to_number(label3, tf.float32) label4 = tf.string_to_number(label4, tf.float32) result.label=tf.stack([label1,label2,label3,label4]) imagecontent = tf.read_file(dir) image = tf.image.decode_jpeg(imagecontent, channels=3) result.uint8image=image return result def generate_batch(image, label, min_queue_examples, batch_size): num_preprocess_threads = 8 #多线程读取 images=tf.placeholder(tf.float32) label_batch=tf.placeholder(tf.float32) images, label_batch = tf.train.batch( [image, label], batch_size=64, shapes=([256,320,3],[4]), num_threads=num_preprocess_threads, capacity=50000) return images, tf.reshape(label_batch, [batch_size,4]) def inference(images): # conv1 with tf.variable_scope('conv1') as scope: norm1=tf.placeholder("float",shape=[None,256,320,3]) conv1=tf.placeholder("float") conv=tf.placeholder("float") bias=tf.placeholder("float") norm1 = tf.nn.lrn(images, 4, bias=255.0, alpha=0.0, beta=1.0, name='norm1') norm1=norm1-0.5 tf.summary.histogram('norm1' + '/activations', norm1) kernel = tf.get_variable('weights', shape=[5, 5, 3, 24], initializer=tf.contrib.layers.xavier_initializer()) conv = tf.nn.conv2d(norm1, kernel, [1, 2, 2, 1], padding='VALID') biases = tf.get_variable('biases', shape=[24], initializer=tf.constant_initializer(0.1)) weight=tf.reduce_sum(kernel)/(5*5*3*24) biases_ave=tf.reduce_sum(biases)/24 bias = tf.nn.bias_add(conv, biases) conv1 = tf.nn.relu(bias) tf.summary.scalar('conv1' + '/weight', weight) tf.summary.scalar('conv1' + '/biases', biases_ave) tf.summary.histogram('conv1' + '/activations', conv1) # conv2 with tf.variable_scope('conv2') as scope: conv2=tf.placeholder("float") conv=tf.placeholder("float") bias=tf.placeholder("float") kernel = tf.get_variable('weights', shape=[5, 5, 24, 36], initializer=tf.contrib.layers.xavier_initializer()) conv = tf.nn.conv2d(conv1, kernel, [1, 2, 2, 1], padding='VALID') biases = tf.get_variable('biases', shape=[36], initializer=tf.constant_initializer(0.1)) weight=tf.reduce_sum(kernel)/(5*5*36*24) biases_ave=tf.reduce_sum(biases)/36 bias = tf.nn.bias_add(conv, biases) conv2 = tf.nn.relu(bias) tf.summary.scalar('conv2' + '/weight', weight) tf.summary.scalar('conv2' + '/biases', biases_ave) tf.summary.histogram('conv2' + '/activations', conv2) # conv3 with tf.variable_scope('conv3') as scope: conv3=tf.placeholder("float") conv=tf.placeholder("float") bias=tf.placeholder("float") kernel = tf.get_variable('weights', shape=[5, 5, 36, 48], initializer=tf.contrib.layers.xavier_initializer()) conv = tf.nn.conv2d(conv2, kernel, [1, 2, 2, 1], padding='VALID') biases = tf.get_variable('biases', shape=[48], initializer=tf.constant_initializer(0.1)) weight=tf.reduce_sum(kernel)/(5*5*36*48) biases_ave=tf.reduce_sum(biases)/48 bias = tf.nn.bias_add(conv, biases) conv3 = tf.nn.relu(bias) tf.summary.scalar('conv3' + '/weight', weight) tf.summary.scalar('conv3' + '/biases', biases_ave) tf.summary.histogram('conv3' + '/activations', conv3) # conv4 with tf.variable_scope('conv4') as scope: conv4=tf.placeholder("float") conv=tf.placeholder("float") bias=tf.placeholder("float") kernel = tf.get_variable('weights', shape=[3, 3, 48, 64], initializer=tf.contrib.layers.xavier_initializer()) conv = tf.nn.conv2d(conv3, kernel, [1, 1, 1, 1], padding='VALID') biases = tf.get_variable('biases', shape=[64], initializer=tf.constant_initializer(0.1)) weight=tf.reduce_sum(kernel)/(3*3*48*64) biases_ave=tf.reduce_sum(biases)/64 bias = tf.nn.bias_add(conv, biases) conv4 = tf.nn.relu(bias) tf.summary.scalar('conv4' + '/weight', weight) tf.summary.scalar('conv4' + '/biases', biases_ave) tf.summary.histogram('conv4' + '/activations', conv4) # conv5 with tf.variable_scope('conv5') as scope: conv5=tf.placeholder("float") conv=tf.placeholder("float") bias=tf.placeholder("float") kernel = tf.get_variable('weights', shape=[3, 3, 64, 128], initializer=tf.contrib.layers.xavier_initializer()) conv = tf.nn.conv2d(conv4, kernel, [1, 1, 1, 1], padding='VALID') biases = tf.get_variable('biases', shape=[128], initializer=tf.constant_initializer(0.1)) weight=tf.reduce_sum(kernel)/(3*3*64*64) biases_ave=tf.reduce_sum(biases)/128 bias = tf.nn.bias_add(conv, biases) conv5 = tf.nn.relu(bias) tf.summary.scalar('conv5' + '/weight', weight) tf.summary.scalar('conv5' + '/biases', biases_ave) tf.summary.histogram('conv5' + '/activations', conv5) # local3 with tf.variable_scope('local3') as scope: local3=tf.placeholder("float") dim=tf.placeholder(tf.int32) bias=tf.placeholder("float") weights=tf.placeholder("float") reshape = tf.reshape(conv5, [64,-1]) #-1代表缺省 dim = reshape.get_shape()[1].value weights = tf.get_variable('weights', shape=[dim, 500], initializer=tf.contrib.layers.xavier_initializer()) biases = tf.get_variable('biases', shape=[500], initializer=tf.constant_initializer(0.1)) bias = tf.matmul(reshape, weights)+biases local3=tf.nn.dropout(local3,0.5) local3=tf.nn.relu(bias) tf.summary.scalar('local3' + '/weight', tf.reduce_sum(weights)/(dim*100)) tf.summary.scalar('local3' + '/biases', tf.reduce_sum(biases)/100) tf.summary.histogram('local3' + '/activations', local3) # local4 with tf.variable_scope('local4') as scope: local4=tf.placeholder("float") weights=tf.placeholder("float") weights = tf.get_variable('weights', shape=[500, 100], initializer=tf.contrib.layers.xavier_initializer()) biases = tf.get_variable('biases', shape=[100], initializer=tf.constant_initializer(0.1)) local4 = tf.nn.relu(tf.matmul(local3, weights) + biases) tf.summary.scalar('local4' + '/weight', tf.reduce_sum(weights)/(500*100)) tf.summary.scalar('local4' + '/biases', tf.reduce_sum(biases)/100) tf.summary.histogram('local4' + '/activations', local4) #local5 with tf.variable_scope('local5') as scope: local5=tf.placeholder("float") weights=tf.placeholder("float") weights = tf.get_variable('weights', shape=[100, 20], initializer=tf.contrib.layers.xavier_initializer()) biases = tf.get_variable('biases', shape=[20], initializer=tf.constant_initializer(0.1)) local5 = tf.nn.relu(tf.matmul(local4, weights) + biases) tf.summary.scalar('local5' + '/weight', tf.reduce_sum(weights)/(20*100)) tf.summary.scalar('local5' + '/biases', tf.reduce_sum(biases)/20) tf.summary.histogram('local5' + '/activations', local5) with tf.variable_scope('local6') as scope: local6=tf.placeholder("float") weights=tf.placeholder("float") weights = tf.get_variable('weights', shape=[20, 4], initializer=tf.contrib.layers.xavier_initializer()) biases = tf.get_variable('biases', shape=[4], initializer=tf.constant_initializer(0.1)) local6 = tf.matmul(local5, weights) + biases tf.summary.scalar('local6' + '/weight', tf.reduce_sum(weights)/(20)) tf.summary.scalar('local6' + '/biases', tf.reduce_sum(biases)) #local6=local6[...,0] return local6 def compute_loss(logits, labels): loss=tf.placeholder("float") loss = tf.reduce_sum(tf.pow(tf.subtract(labels,logits), 2))/128 tf.summary.histogram('labels' + '/activations', labels) tf.summary.histogram('local6' + '/activations', logits) tf.summary.scalar('loss', loss) tf.summary.histogram('local6-labels' + '/activations', tf.subtract(logits,labels)) return loss def train(): """Train CIFAR-10 for a number of steps.""" with tf.Graph().as_default(): global_step = tf.Variable(0, trainable=False) # Get images and labels for CIFAR-10. images=tf.placeholder("float",shape=[None,256,320,3]) labels=tf.placeholder("float",shape=[None]) local6=tf.placeholder("float",shape=[None]) images, labels = inputs() logits = inference(images) loss = compute_loss(logits, labels) lr=0.001 tf.summary.scalar('learning_rate', lr) opt = tf.train.GradientDescentOptimizer(lr) grads = opt.compute_gradients(loss) apply_gradient_op = opt.apply_gradients(grads, global_step=global_step) train_op=apply_gradient_op # Create a saver. saver = tf.train.Saver(tf.global_variables()) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() # Build an initialization operation to run below. init = tf.global_variables_initializer() # Start running operations on the Graph. sess = tf.Session(config=tf.ConfigProto( log_device_placement=False)) sess.run(init) # Start the queue runners. tf.train.start_queue_runners(sess=sess) summary_writer = tf.summary.FileWriter('/home/fzyue/Desktop/caffeendtoend/1', sess.graph) for step in xrange(500000): start_time = time.time() _, loss_value = sess.run([train_op, loss]) duration = time.time() - start_time assert not np.isnan(loss_value), 'Model diverged with loss = NaN' if step % 10 == 0: num_examples_per_step = 64 examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print (format_str % (datetime.now(), step, loss_value, examples_per_sec, sec_per_batch)) #print(labels) #print (sess.run(logits)) if step % 100 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) # Save the model checkpoint periodically. if step % 1000 == 0 or (step + 1) == 500000: checkpoint_path = os.path.join('/home/fzyue/Desktop/caffeendtoend/1', 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step) def main(): if tf.gfile.Exists('/home/fzyue/Desktop/caffeendtoend/1'): tf.gfile.DeleteRecursively('/home/fzyue/Desktop/caffeendtoend/1') tf.gfile.MakeDirs('/home/fzyue/Desktop/caffeendtoend/1') train() main()
def text_to_index(text_label): return tf.string_to_number(text_label, out_type=tf.int32)
def __init__(self, opt): self.data_path = opt.data_dir self.opt = opt filenames_file = opt.train_file input_queue = tf.train.string_input_producer([filenames_file], shuffle=False) line_reader = tf.TextLineReader() _, line = line_reader.read(input_queue) split_line = tf.string_split([line]).values # we load only one image for test, except if we trained a stereo model left_image_path = tf.string_join([self.data_path, split_line[0]]) right_image_path = tf.string_join([self.data_path, split_line[1]]) next_left_image_path = tf.string_join([self.data_path, split_line[2]]) next_right_image_path = tf.string_join([self.data_path, split_line[3]]) cam_intrinsic_path = tf.string_join([self.data_path, split_line[4]]) left_image_o, orig_height, orig_width = self.read_image( left_image_path, get_shape=True) right_image_o = self.read_image(right_image_path) next_left_image_o = self.read_image(next_left_image_path) next_right_image_o = self.read_image(next_right_image_path) # randomly flip images do_flip = tf.random_uniform([], 0, 1) left_image = tf.cond(do_flip > 0.5, lambda: tf.image.flip_left_right(right_image_o), lambda: left_image_o) right_image = tf.cond(do_flip > 0.5, lambda: tf.image.flip_left_right(left_image_o), lambda: right_image_o) next_left_image = tf.cond( do_flip > 0.5, lambda: tf.image.flip_left_right(next_right_image_o), lambda: next_left_image_o) next_right_image = tf.cond( do_flip > 0.5, lambda: tf.image.flip_left_right(next_left_image_o), lambda: next_right_image_o) do_flip_fb = tf.random_uniform([], 0, 1) left_image, right_image, next_left_image, next_right_image = tf.cond( do_flip_fb > 0.5, lambda: (next_left_image, next_right_image, left_image, right_image), lambda: (left_image, right_image, next_left_image, next_right_image)) # randomly augment images # do_augment = tf.random_uniform([], 0, 0) # image_list = [left_image, right_image, next_left_image, next_right_image] # left_image, right_image, next_left_image, next_right_image = tf.cond(do_augment > 0.5, # lambda: self.augment_image_list(image_list), # lambda: image_list) left_image.set_shape([None, None, 3]) right_image.set_shape([None, None, 3]) next_left_image.set_shape([None, None, 3]) next_right_image.set_shape([None, None, 3]) raw_cam_contents = tf.read_file(cam_intrinsic_path) last_line = tf.string_split([raw_cam_contents], delimiter="\n").values[-1] raw_cam_vec = tf.string_to_number( tf.string_split([last_line]).values[1:]) raw_cam_mat = tf.reshape(raw_cam_vec, [3, 4]) raw_cam_mat = raw_cam_mat[0:3, 0:3] raw_cam_mat = rescale_intrinsics(raw_cam_mat, opt, orig_height, orig_width) # Scale and crop augmentation # im_batch = tf.concat([tf.expand_dims(left_image, 0), # tf.expand_dims(right_image, 0), # tf.expand_dims(next_left_image, 0), # tf.expand_dims(next_right_image, 0)], axis=3) # raw_cam_mat_batch = tf.expand_dims(raw_cam_mat, axis=0) # im_batch, raw_cam_mat_batch = data_augmentation(im_batch, raw_cam_mat_batch, self.opt.img_height, self.opt.img_width) # left_image, right_image, next_left_image, next_right_image = tf.split(im_batch[0,:,:,:], num_or_size_splits=4, axis=2) # raw_cam_mat = raw_cam_mat_batch[0,:,:] proj_cam2pix, proj_pix2cam = get_multi_scale_intrinsics( raw_cam_mat, opt.num_scales) # capacity = min_after_dequeue + (num_threads + a small safety margin) * batch_size min_after_dequeue = 2048 capacity = min_after_dequeue + 4 * opt.batch_size self.data_batch = tf.train.shuffle_batch([ left_image, right_image, next_left_image, next_right_image, proj_cam2pix, proj_pix2cam ], opt.batch_size, capacity, min_after_dequeue, 10)
def text_to_one_hot(text_label): int_label = tf.string_to_number(text_label, out_type=tf.int32) return tf.one_hot(int_label, 10)
def __init__(self, img_dir, list1, list2, batch_size, h, w, num_threads, seed=0): self.img_dir = img_dir self.list1 = list1 self.list2 = list2 self.target_h = h self.target_w = w self.simg_batch = None self.slabel_batch = None self.timg_batch = None self.tlabel_batch = None # Chong tf.set_random_seed(1) queue1 = tf.train.string_input_producer([self.list1], shuffle=True) queue2 = tf.train.string_input_producer([self.list2], shuffle=True) # Chong: must use two seperate TextLineReader!!! line_reader1 = tf.TextLineReader() line_reader2 = tf.TextLineReader() _, line1 = line_reader1.read(queue1) _, line2 = line_reader2.read(queue2) #pdb.set_trace() split_line1 = tf.string_split([line1], ' ').values split_line2 = tf.string_split([line2], ' ').values p1 = tf.string_join([self.img_dir, '/', split_line1[0]]) slabel = tf.string_to_number(split_line1[1], tf.int64) p2 = tf.string_join([self.img_dir, '/', split_line2[0]]) tlabel = tf.string_to_number(split_line2[1], tf.int64) simg = self.read_image(p1) timg = self.read_image(p2) simg = self.augment_image(simg) timg = self.augment_image(timg) # else: # simg=self.augment_image_when_test(simg) # timg=self.augment_image_when_test(timg) simg.set_shape([self.target_h, self.target_w, 3]) # slabel.set_shape([None, 1]) timg.set_shape([self.target_h, self.target_w, 3]) # tlabel.set_shape([None,1]) simg = self.pre_process(simg) timg = self.pre_process(timg) simg = simg[..., ::-1] timg = timg[..., ::-1] # capacity = min_after_dequeue + (num_threads + a small safety margin) * batch_size min_after_dequeue = 2048 capacity = min_after_dequeue + (4 + num_threads) * batch_size self.simg_batch, self.slabel_batch, self.timg_batch, self.tlabel_batch = tf.train.shuffle_batch( [simg, slabel, timg, tlabel], batch_size, capacity, min_after_dequeue, num_threads)
def test_StringToNumber(self): t = tf.string_to_number(list("0123456789")) self.check(t)
import tensorflow as tf import numpy as np labell_holder = tf.placeholder(dtype=tf.string, shape=[ None, ]) labelSplitter = tf.string_split(labell_holder, delimiter=",") tensor = tf.sparse_tensor_to_dense(labelSplitter, default_value="") tensor = tf.string_to_number(tensor) tensor = tf.transpose(tensor, perm=[1, 0]) x_ind = tf.cast(tensor[:, :1] * 7 / 448, dtype=tf.int64) y_ind = tf.cast(tensor[:, 1:2] * 7 / 448, dtype=tf.int64) labels = tf.zeros((7, 7, 25)) # xMinTensor = tensor[:, :1] # yMinTensor = tensor[:, 1:2] # xMaxTensor = tensor[:, 2:3] # yMaxTensor = tensor[:, 3:4] # labelTensor = tensor[:, 4:5] # label = np.zeros((7, 7, 25)) # x_ind = tf.cast(tensor[:, :1] * 7 / 224, dtype=tf.int64) # y_ind = tf.cast(tensor[:, 1:2] * 7 / 224, dtype=tf.int64) # shape = tensor.get_shape() # tensor =tensor[0] with tf.Session() as sess: sess.run(tf.global_variables_initializer())
def decode_txt(line, num_class): # decode line by line columns = tf.string_split([line], delimiter=' ') # convert line into tensor images = tf.string_to_number(columns.values[0:(-1*num_class)], out_type=tf.float32) labels = tf.string_to_number(columns.values[(-1*num_class):], out_type=tf.float32) return images, labels
def _dataset_parser(value): """Parse data to a fixed dimension input image and learning targets. Args: value: A dictionary contains an image and groundtruth annotations. Returns: features: a dictionary that contains the image and auxiliary information. The following describes {key: value} pairs in the dictionary. image: Image tensor that is preproessed to have normalized value and fixed dimension [image_size, image_size, 3] image_info: image information that includes the original height and width, the scale of the proccessed image to the original image, and the scaled height and width. source_ids: Source image id. Default value -1 if the source id is empty in the groundtruth annotation. labels: a dictionary that contains auxiliary information plus (optional) labels. The following describes {key: value} pairs in the dictionary. `labels` is only for training. score_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors]. The height_l and width_l represent the dimension of objectiveness score at l-th level. box_targets_dict: ordered dictionary with keys [min_level, min_level+1, ..., max_level]. The values are tensor with shape [height_l, width_l, num_anchors * 4]. The height_l and width_l represent the dimension of bounding box regression output at l-th level. gt_boxes: Groundtruth bounding box annotations. The box is represented in [y1, x1, y2, x2] format. The tennsor is padded with -1 to the fixed dimension [self._max_num_instances, 4]. gt_classes: Groundtruth classes annotations. The tennsor is padded with -1 to the fixed dimension [self._max_num_instances]. cropped_gt_masks: groundtrugh masks cropped by the bounding box and resized to a fixed size determined by params['gt_mask_size'] """ with tf.name_scope('parser'): data = example_decoder.decode(value) image = data['image'] image = tf.image.convert_image_dtype(image, dtype=tf.float32) source_id = data['source_id'] source_id = tf.where(tf.equal(source_id, tf.constant('')), '-1', source_id) source_id = tf.string_to_number(source_id) if self._mode == tf.estimator.ModeKeys.PREDICT: image = preprocess_ops.normalize_image(image) image, image_info, _, _ = preprocess_ops.resize_and_pad( image, params['image_size'], 2 ** params['max_level']) if params['use_bfloat16']: image = tf.cast(image, dtype=tf.bfloat16) features = { 'images': image, 'image_info': image_info, 'source_ids': source_id, } if params['include_groundtruth_in_features']: labels = _prepare_labels_for_eval( data, target_num_instances=self._max_num_instances, target_polygon_list_len=self._max_num_polygon_list_len, use_instance_mask=params['include_mask']) return {'features': features, 'labels': labels} else: return {'features': features} elif self._mode == tf.estimator.ModeKeys.TRAIN: instance_masks = None if self._use_instance_mask: instance_masks = data['groundtruth_instance_masks'] boxes = data['groundtruth_boxes'] classes = data['groundtruth_classes'] classes = tf.reshape(tf.cast(classes, dtype=tf.float32), [-1, 1]) if not params['use_category']: classes = tf.cast(tf.greater(classes, 0), dtype=tf.float32) if (params['skip_crowd_during_training'] and self._mode == tf.estimator.ModeKeys.TRAIN): indices = tf.where(tf.logical_not(data['groundtruth_is_crowd'])) classes = tf.gather_nd(classes, indices) boxes = tf.gather_nd(boxes, indices) if self._use_instance_mask: instance_masks = tf.gather_nd(instance_masks, indices) image = preprocess_ops.normalize_image(image) # Random flipping. if params['input_rand_hflip']: flipped_results = ( preprocess_ops.random_horizontal_flip( image, boxes=boxes, masks=instance_masks)) if self._use_instance_mask: image, boxes, instance_masks = flipped_results else: image, boxes = flipped_results # Scaling and padding. image, image_info, boxes, instance_masks = ( preprocess_ops.resize_and_pad( image, params['image_size'], 2 ** params['max_level'], boxes=boxes, masks=instance_masks)) padded_height, padded_width, _ = image.get_shape().as_list() padded_image_size = (padded_height, padded_width) if self._use_instance_mask: cropped_gt_masks = preprocess_ops.crop_gt_masks( instance_masks, boxes, params['gt_mask_size'], padded_image_size) input_anchors = anchors.Anchors( params['min_level'], params['max_level'], params['num_scales'], params['aspect_ratios'], params['anchor_scale'], padded_image_size) anchor_labeler = anchors.AnchorLabeler( input_anchors, params['num_classes'], params['rpn_positive_overlap'], params['rpn_negative_overlap'], params['rpn_batch_size_per_im'], params['rpn_fg_fraction']) # Assign anchors. score_targets, box_targets = anchor_labeler.label_anchors( boxes, classes) # Pad groundtruth data. boxes *= image_info[2] boxes = preprocess_ops.pad_to_fixed_size( boxes, -1, [self._max_num_instances, 4]) classes = preprocess_ops.pad_to_fixed_size( classes, -1, [self._max_num_instances, 1]) # Pads cropped_gt_masks. if self._use_instance_mask: cropped_gt_masks = tf.reshape( cropped_gt_masks, [self._max_num_instances, -1]) cropped_gt_masks = preprocess_ops.pad_to_fixed_size( cropped_gt_masks, -1, [self._max_num_instances, (params['gt_mask_size'] + 4) ** 2]) cropped_gt_masks = tf.reshape( cropped_gt_masks, [self._max_num_instances, params['gt_mask_size'] + 4, params['gt_mask_size'] + 4]) if params['use_bfloat16']: image = tf.cast(image, dtype=tf.bfloat16) features = { 'images': image, 'image_info': image_info, 'source_ids': source_id, } labels = {} for level in range(params['min_level'], params['max_level'] + 1): labels['score_targets_%d' % level] = score_targets[level] labels['box_targets_%d' % level] = box_targets[level] labels['gt_boxes'] = boxes labels['gt_classes'] = classes if self._use_instance_mask: labels['cropped_gt_masks'] = cropped_gt_masks return {'features': features, 'labels': labels}
@author: zxl """ import os import tensorflow as tf filename = os.path.join('data/train', 'list.txt') with open(filename) as fid: content = fid.read() content = content.split('\n') content = content[:-1] valuequeue = tf.train.string_input_producer(content, shuffle=True) value = valuequeue.dequeue() dir, labels = tf.decode_csv(records=value, record_defaults=[["string"], [""]], field_delim=" ") labels = tf.string_to_number(labels, tf.int32) imagecontent = tf.read_file(dir) image = tf.image.decode_png(imagecontent, channels=3, dtype=tf.uint8) image = tf.cast(image, tf.float32) #image = tf.image.resize_images(image,[100,100]) reshape = tf.reshape(tf.reduce_mean(image, [0, 1]), [1, 1, 3]) #result.uint8image = tf.transpose(image,[1,2,0]) image = image / reshape * 128 print "*******input done!*********" #image = tf.random_crop(image,[IMAGE_SIZE,IMAGE_SIZE,3]) #images,labels_batch = tf.train.shuffle_batch([image,labels],batch_size = batch_size,num_threads = 6,capacity = 3 * batch_size+3000,min_after_dequeue = 3000) #return images,labels_batch
def main(_): if not FLAGS.dataset_dir: raise ValueError('You must supply the dataset directory with --dataset_dir') os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpus if FLAGS.num_clones == -1: FLAGS.num_clones = len(FLAGS.gpus.split(',')) tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # tf.set_random_seed(42) tf.set_random_seed(0) ###################### # Config model_deploy# ###################### deploy_config = model_deploy.DeploymentConfig( num_clones=FLAGS.num_clones, clone_on_cpu=FLAGS.clone_on_cpu, replica_id=FLAGS.task, num_replicas=FLAGS.worker_replicas, num_ps_tasks=FLAGS.num_ps_tasks) # Create global_step with tf.device(deploy_config.variables_device()): global_step = slim.create_global_step() ###################### # Select the dataset # ###################### dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir.split(','), dataset_list_dir=FLAGS.dataset_list_dir, num_samples=FLAGS.frames_per_video, modality=FLAGS.modality, split_id=FLAGS.split_id) ###################### # Select the network # ###################### network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), batch_size=FLAGS.batch_size, weight_decay=FLAGS.weight_decay, is_training=True, dropout_keep_prob=(1.0-FLAGS.dropout), pooled_dropout_keep_prob=(1.0-FLAGS.pooled_dropout), batch_norm=FLAGS.netvlad_batch_norm) ##################################### # Select the preprocessing function # ##################################### preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=True) # in case of pooling images, # now preprocessing is done video-level ############################################################## # Create a dataset provider that loads data from the dataset # ############################################################## with tf.device(deploy_config.inputs_device()): provider = dataset_data_provider.DatasetDataProvider( dataset, num_readers=FLAGS.num_readers, common_queue_capacity=20 * FLAGS.batch_size, common_queue_min=10 * FLAGS.batch_size, bgr_flips=FLAGS.bgr_flip) [image, label] = provider.get(['image', 'label']) # now note that the above image might be a 23 channel image if you have # both RGB and flow streams. It will need to split later, but all the # preprocessing will be done consistently for all frames over all streams label = tf.string_to_number(label, tf.int32) label.set_shape(()) label -= FLAGS.labels_offset train_image_size = FLAGS.train_image_size or network_fn.default_image_size scale_ratios=[float(el) for el in FLAGS.scale_ratios.split(',')], image = image_preprocessing_fn(image, train_image_size, train_image_size, scale_ratios=scale_ratios, out_dim_scale=FLAGS.out_dim_scale, model_name=FLAGS.model_name) images, labels = tf.train.batch( [image, label], batch_size=FLAGS.batch_size, num_threads=FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) if FLAGS.debug: images = tf.Print(images, [labels], 'Read batch') labels = slim.one_hot_encoding( labels, dataset.num_classes - FLAGS.labels_offset) batch_queue = slim.prefetch_queue.prefetch_queue( [images, labels], capacity=2 * deploy_config.num_clones) summarize_images(images, provider.num_channels_stream) #################### # Define the model # #################### kwargs = {} if FLAGS.conv_endpoint is not None: kwargs['conv_endpoint'] = FLAGS.conv_endpoint def clone_fn(batch_queue): """Allows data parallelism by creating multiple clones of network_fn.""" images, labels = batch_queue.dequeue() logits, end_points = network_fn( images, pool_type=FLAGS.pooling, classifier_type=FLAGS.classifier_type, num_channels_stream=provider.num_channels_stream, netvlad_centers=FLAGS.netvlad_initCenters.split(','), stream_pool_type=FLAGS.stream_pool_type, **kwargs) ############################# # Specify the loss function # ############################# if 'AuxLogits' in end_points: slim.losses.softmax_cross_entropy( end_points['AuxLogits'], labels, label_smoothing=FLAGS.label_smoothing, weight=0.4, scope='aux_loss') slim.losses.softmax_cross_entropy( logits, labels, label_smoothing=FLAGS.label_smoothing, weight=1.0) return end_points # Gather initial summaries. summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue]) first_clone_scope = deploy_config.clone_scope(0) # Gather update_ops from the first clone. These contain, for example, # the updates for the batch_norm variables created by network_fn. update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) # Add summaries for end_points. global end_points_debug end_points = clones[0].outputs end_points_debug = dict(end_points) end_points_debug['images'] = images end_points_debug['labels'] = labels for end_point in end_points: x = end_points[end_point] summaries.add(tf.histogram_summary('activations/' + end_point, x)) summaries.add(tf.scalar_summary('sparsity/' + end_point, tf.nn.zero_fraction(x))) # Add summaries for losses. for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): summaries.add(tf.scalar_summary('losses/%s' % loss.op.name, loss)) # Add summaries for variables. for variable in slim.get_model_variables(): summaries.add(tf.histogram_summary(variable.op.name, variable)) ################################# # Configure the moving averages # ################################# if FLAGS.moving_average_decay: moving_average_variables = slim.get_model_variables() variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, global_step) else: moving_average_variables, variable_averages = None, None ######################################### # Configure the optimization procedure. # ######################################### with tf.device(deploy_config.optimizer_device()): learning_rate = _configure_learning_rate(dataset.num_samples, global_step) optimizer = _configure_optimizer(learning_rate) summaries.add(tf.scalar_summary('learning_rate', learning_rate, name='learning_rate')) if FLAGS.sync_replicas: # If sync_replicas is enabled, the averaging will be done in the chief # queue runner. optimizer = tf.train.SyncReplicasOptimizer( opt=optimizer, replicas_to_aggregate=FLAGS.replicas_to_aggregate, variable_averages=variable_averages, variables_to_average=moving_average_variables, replica_id=tf.constant(FLAGS.task, tf.int32, shape=()), total_num_replicas=FLAGS.worker_replicas) elif FLAGS.moving_average_decay: # Update ops executed locally by trainer. update_ops.append(variable_averages.apply(moving_average_variables)) # Variables to train. variables_to_train = _get_variables_to_train() logging.info('Training the following variables: %s' % ( ' '.join([el.name for el in variables_to_train]))) # and returns a train_tensor and summary_op total_loss, clones_gradients = model_deploy.optimize_clones( clones, optimizer, var_list=variables_to_train) # clip the gradients if needed if FLAGS.clip_gradients > 0: logging.info('Clipping gradients by %f' % FLAGS.clip_gradients) with tf.name_scope('clip_gradients'): clones_gradients = slim.learning.clip_gradient_norms( clones_gradients, FLAGS.clip_gradients) # Add total_loss to summary. summaries.add(tf.scalar_summary('total_loss', total_loss, name='total_loss')) # Create gradient updates. train_ops = {} if FLAGS.iter_size == 1: grad_updates = optimizer.apply_gradients(clones_gradients, global_step=global_step) update_ops.append(grad_updates) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') train_ops = train_tensor else: gvs = [(grad, var) for grad, var in clones_gradients] varnames = [var.name for grad, var in gvs] varname_to_var = {var.name: var for grad, var in gvs} varname_to_grad = {var.name: grad for grad, var in gvs} varname_to_ref_grad = {} for vn in varnames: grad = varname_to_grad[vn] print("accumulating ... ", (vn, grad.get_shape())) with tf.variable_scope("ref_grad"): with tf.device(deploy_config.variables_device()): ref_var = slim.local_variable( np.zeros(grad.get_shape(),dtype=np.float32), name=vn[:-2]) varname_to_ref_grad[vn] = ref_var all_assign_ref_op = [ref.assign(varname_to_grad[vn]) for vn, ref in varname_to_ref_grad.items()] all_assign_add_ref_op = [ref.assign_add(varname_to_grad[vn]) for vn, ref in varname_to_ref_grad.items()] assign_gradients_ref_op = tf.group(*all_assign_ref_op) accmulate_gradients_op = tf.group(*all_assign_add_ref_op) with tf.control_dependencies([accmulate_gradients_op]): final_gvs = [(varname_to_ref_grad[var.name] / float(FLAGS.iter_size), var) for grad, var in gvs] apply_gradients_op = optimizer.apply_gradients(final_gvs, global_step=global_step) update_ops.append(apply_gradients_op) update_op = tf.group(*update_ops) train_tensor = control_flow_ops.with_dependencies([update_op], total_loss, name='train_op') for i in range(FLAGS.iter_size): if i == 0: train_ops[i] = assign_gradients_ref_op elif i < FLAGS.iter_size - 1: # because apply_gradients also computes # (see control_dependency), so # no need of running an extra iteration train_ops[i] = accmulate_gradients_op else: train_ops[i] = train_tensor # Add the summaries from the first clone. These contain the summaries # created by model_fn and either optimize_clones() or _gather_clone_loss(). summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) # Merge all summaries together. summary_op = tf.merge_summary(list(summaries), name='summary_op') config = tf.ConfigProto() config.gpu_options.allow_growth = True config.intra_op_parallelism_threads = FLAGS.cpu_threads # config.allow_soft_placement = True # config.gpu_options.per_process_gpu_memory_fraction=0.7 ########################### # Kicks off the training. # ########################### logging.info('RUNNING ON SPLIT %d' % FLAGS.split_id) slim.learning.train( train_ops, train_step_fn=train_step, logdir=FLAGS.train_dir, master=FLAGS.master, is_chief=(FLAGS.task == 0), init_fn=_get_init_fn(), summary_op=summary_op, number_of_steps=FLAGS.max_number_of_steps, log_every_n_steps=FLAGS.log_every_n_steps, save_summaries_secs=FLAGS.save_summaries_secs, save_interval_secs=FLAGS.save_interval_secs, sync_optimizer=optimizer if FLAGS.sync_replicas else None, session_config=config)
def main(_): if not FLAGS.dataset_dir: raise ValueError('You must supply the dataset directory with --dataset_dir') if not os.path.isfile(FLAGS.checkpoint_path): FLAGS.eval_dir = os.path.join(FLAGS.checkpoint_path, 'eval') else: FLAGS.eval_dir = os.path.join( os.path.dirname(FLAGS.checkpoint_path), 'eval') try: os.makedirs(FLAGS.eval_dir) except OSError: pass tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): tf_global_step = slim.get_or_create_global_step() ###################### # Select the dataset # ###################### dataset = dataset_factory.get_dataset( FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir.split(','), FLAGS.dataset_list_dir, num_samples=FLAGS.frames_per_video, modality=FLAGS.modality, split_id=FLAGS.split_id) #################### # Select the model # #################### network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(dataset.num_classes - FLAGS.labels_offset), batch_size=FLAGS.batch_size, is_training=False) ############################################################## # Create a dataset provider that loads data from the dataset # ############################################################## provider = dataset_data_provider.DatasetDataProvider( dataset, shuffle=FLAGS.force_random_shuffle, common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size, bgr_flips=FLAGS.bgr_flip) [image, label] = provider.get(['image', 'label']) label = tf.cast(tf.string_to_number(label, tf.int32), tf.int64) label.set_shape(()) label -= FLAGS.labels_offset ##################################### # Select the preprocessing function # ##################################### preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) eval_image_size = FLAGS.eval_image_size or network_fn.default_image_size image = image_preprocessing_fn(image, eval_image_size, eval_image_size, model_name=FLAGS.model_name, ncrops=FLAGS.ncrops, out_dim_scale=FLAGS.out_dim_scale) images, labels = tf.train.batch( [image, label], batch_size=FLAGS.batch_size, num_threads=1 if FLAGS.store_feat is not None else FLAGS.num_preprocessing_threads, capacity=5 * FLAGS.batch_size) #################### # Define the model # #################### kwargs = {} if FLAGS.conv_endpoint is not None: kwargs['conv_endpoint'] = FLAGS.conv_endpoint logits, end_points = network_fn( images, pool_type=FLAGS.pooling, classifier_type=FLAGS.classifier_type, num_channels_stream=provider.num_channels_stream, netvlad_centers=FLAGS.netvlad_initCenters.split(','), stream_pool_type=FLAGS.stream_pool_type, **kwargs) end_points['images'] = images end_points['labels'] = labels if FLAGS.moving_average_decay: variable_averages = tf.train.ExponentialMovingAverage( FLAGS.moving_average_decay, tf_global_step) variables_to_restore = variable_averages.variables_to_restore( slim.get_model_variables()) variables_to_restore[tf_global_step.op.name] = tf_global_step else: variables_to_restore = slim.get_variables_to_restore() predictions = tf.argmax(logits, 1) # rgirdhar: Because of the following, can't use with batch_size=1 if FLAGS.batch_size > 1: labels = tf.squeeze(labels) # Define the metrics: names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({ 'Accuracy': slim.metrics.streaming_accuracy(predictions, labels), 'Recall@5': slim.metrics.streaming_recall_at_k( logits, labels, 5), }) # Print the summaries to screen. for name, value in names_to_values.iteritems(): summary_name = 'eval/%s' % name op = tf.scalar_summary(summary_name, value, collections=[]) op = tf.Print(op, [value], summary_name) tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # TODO(sguada) use num_epochs=1 if FLAGS.max_num_batches: num_batches = FLAGS.max_num_batches else: # This ensures that we make a single pass over all of the data. num_batches = int(math.ceil(dataset.num_samples / float(FLAGS.batch_size))) if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path tf.logging.info('Evaluating %s' % checkpoint_path) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True if FLAGS.store_feat is not None: assert(FLAGS.store_feat_path is not None) from tensorflow.python.training import supervisor from tensorflow.python.framework import ops import h5py saver = tf.train.Saver(variables_to_restore) sv = supervisor.Supervisor(graph=ops.get_default_graph(), logdir=None, summary_op=None, summary_writer=None, global_step=None, saver=None) ept_names_to_store = FLAGS.store_feat.split(',') try: ept_to_store = [end_points[el] for el in ept_names_to_store] except: logging.error('Endpoint not found') logging.error('Choose from %s' % ','.join(end_points.keys())) raise KeyError() res = dict([(epname, []) for epname in ept_names_to_store]) with sv.managed_session( FLAGS.master, start_standard_services=False, config=config) as sess: saver.restore(sess, checkpoint_path) sv.start_queue_runners(sess) for j in range(num_batches): if j % 10 == 0: logging.info('Doing batch %d/%d' % (j, num_batches)) feats = sess.run(ept_to_store) for eid, epname in enumerate(ept_names_to_store): res[epname].append(feats[eid]) logging.info('Writing out features to %s' % FLAGS.store_feat_path) with h5py.File(FLAGS.store_feat_path, 'w') as fout: for epname in res.keys(): fout.create_dataset(epname, data=np.concatenate(res[epname], axis=0), compression='gzip', compression_opts=FLAGS.feat_store_compression_opt) else: slim.evaluation.evaluate_once( master=FLAGS.master, checkpoint_path=checkpoint_path, logdir=FLAGS.eval_dir, num_evals=num_batches, eval_op=names_to_updates.values(), variables_to_restore=variables_to_restore, session_config=config)