def main(_): # if len(sys.argv) < 2 or sys.argv[-1].startswith('-'): # print('Usage: mnist_export.py [--training_iteration=x] ' # '[--model_version=y] export_dir') # sys.exit(-1) if FLAGS.training_iteration <= 0: print('Please specify a positive value for training iteration.') sys.exit(-1) if FLAGS.model_version <= 0: print('Please specify a positive value for version number.') sys.exit(-1) print('Training...') mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) sess = tf.InteractiveSession() serialized_tf_example = tf.placeholder(tf.string, name='tf_example') feature_configs = {'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32),} # 28*28 =784 tf_example = tf.parse_example(serialized_tf_example, feature_configs) x = tf.identity(tf_example['x'], name='x') y_ = tf.placeholder('float', shape=[None, 10]) W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) x_image = tf.reshape(x, [-1, 28, 28, 1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2 y = tf.nn.softmax(y_conv, name='y') cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) values, indices = tf.nn.top_k(y_conv, 10) # 从小到大排名每个分数以及相应的index prediction_classes = tf.contrib.lookup.index_to_string( tf.to_int64(indices), mapping=tf.constant([str(i) for i in range(10)])) sess.run(tf.global_variables_initializer()) for _ in range(FLAGS.training_iteration): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print('training accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})) print('training is finished!') export_path_base = FLAGS.export_path #"export_base" #sys.argv[-1] export_path = os.path.join(compat.as_bytes(export_path_base),compat.as_bytes(str(FLAGS.model_version))) print('Exporting trained model to', export_path) if os.path.exists(export_path_base): shutil.rmtree(export_path_base,ignore_errors=True) builder = saved_model_builder.SavedModelBuilder(export_path) # 分类的签名 classification_inputs = utils.build_tensor_info(serialized_tf_example) classification_outputs_classes = utils.build_tensor_info(prediction_classes) # 预测的top 10的类别 classification_outputs_scores = utils.build_tensor_info(values) # 预测的top 10的分数 classification_signature = signature_def_utils.build_signature_def( inputs={signature_constants.CLASSIFY_INPUTS:classification_inputs}, # 注意:此处的输出有两个值 outputs={ signature_constants.CLASSIFY_OUTPUT_CLASSES: classification_outputs_classes, signature_constants.CLASSIFY_OUTPUT_SCORES: classification_outputs_scores }, method_name=signature_constants.CLASSIFY_METHOD_NAME ) # 预测的签名 tensor_info_x = utils.build_tensor_info(x) tensor_info_y = utils.build_tensor_info(y_conv) #预测时不需要再计算softmax,直接比较就行 prediction_signature = signature_def_utils.build_signature_def( inputs={'images': tensor_info_x}, outputs={'scores': tensor_info_y}, # 预测成为不同label的分数 method_name=signature_constants.PREDICT_METHOD_NAME) legacy_init_op = tf.group(tf.initialize_all_tables(), name='legacy_init_op') builder.add_meta_graph_and_variables( sess, [tag_constants.SERVING], signature_def_map={ 'predict_images': prediction_signature, "class_signature": classification_signature, #signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: classification_signature, # serving_default }, legacy_init_op=legacy_init_op) builder.save() print('new model exported!')
def parse_example_proto(example_serialized): """Parses an Example proto containing a training example of an image. The output of the build_image_data.py image preprocessing script is a dataset containing serialized Example protocol buffers. Each Example proto contains the following fields: image/height: 462 image/width: 581 image/colorspace: 'RGB' image/channels: 3 image/class/label: 615 image/class/synset: 'n03623198' image/class/text: 'knee pad' image/object/bbox/xmin: 0.1 image/object/bbox/xmax: 0.9 image/object/bbox/ymin: 0.2 image/object/bbox/ymax: 0.6 image/object/bbox/label: 615 image/format: 'JPEG' image/filename: 'ILSVRC2012_val_00041207.JPEG' image/encoded: <JPEG encoded string> Args: example_serialized: scalar Tensor tf.string containing a serialized Example protocol buffer. Returns: image_buffer: Tensor tf.string containing the contents of a JPEG file. label: Tensor tf.int32 containing the label. bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] where each coordinate is [0, 1) and the coordinates are arranged as [ymin, xmin, ymax, xmax]. text: Tensor tf.string containing the human-readable label. """ # Dense features in Example proto. feature_map = { 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/class/label': tf.FixedLenFeature([1], dtype=tf.int64, default_value=-1), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), } sparse_float32 = tf.VarLenFeature(dtype=tf.float32) # Sparse features in Example proto. feature_map.update( {k: sparse_float32 for k in ['image/object/bbox/xmin', 'image/object/bbox/ymin', 'image/object/bbox/xmax', 'image/object/bbox/ymax']}) features = tf.parse_single_example(example_serialized, feature_map) label = tf.cast(features['image/class/label'], dtype=tf.int32) xmin = tf.expand_dims(features['image/object/bbox/xmin'].values, 0) ymin = tf.expand_dims(features['image/object/bbox/ymin'].values, 0) xmax = tf.expand_dims(features['image/object/bbox/xmax'].values, 0) ymax = tf.expand_dims(features['image/object/bbox/ymax'].values, 0) # Note that we impose an ordering of (y, x) just to make life difficult. bbox = tf.concat(axis=0, values=[ymin, xmin, ymax, xmax]) # Force the variable number of bounding boxes into the shape # [1, num_boxes, coords]. bbox = tf.expand_dims(bbox, 0) bbox = tf.transpose(bbox, [0, 2, 1]) return features['image/encoded'], label, bbox, features['image/class/text']
# -*- coding: utf-8 -*- # @Author : JaeZheng # @Time : 2019/9/4 15:09 # @File : FileQueue_read.py import tensorflow as tf # tf.train.match_filenames_once获取文件列表 files = tf.train.match_filenames_once("./data/tfrecords-*") # tf.train.string_input_producer创建输入文件的列表,shuffle设置是否打乱 file_queue = tf.train.string_input_producer(files, shuffle=False) reader = tf.TFRecordReader() _, serialized_example = reader.read(file_queue) features = tf.parse_single_example(serialized_example, features={ 'i': tf.FixedLenFeature([], tf.int64), 'j': tf.FixedLenFeature([], tf.int64), }) with tf.Session() as sess: tf.local_variables_initializer().run() print(sess.run(files)) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(6): print(sess.run([features['i'], features['j']])) coord.request_stop() coord.join(threads)
def get_dataset(dataset_name, split_name, dataset_dir): """Gets an instance of slim Dataset. Args: dataset_name: Dataset name. split_name: A train/val Split name. dataset_dir: The directory of the dataset sources. Returns: An instance of slim Dataset. Raises: ValueError: if the dataset_name or split_name is not recognized. """ if dataset_name not in _DATASETS_INFORMATION: raise ValueError('The specified dataset is not supported yet.') splits_to_sizes = _DATASETS_INFORMATION[dataset_name].splits_to_sizes if split_name not in splits_to_sizes: raise ValueError('data split name %s not recognized' % split_name) # Prepare the variables for different datasets. num_classes = _DATASETS_INFORMATION[dataset_name].num_classes ignore_label = _DATASETS_INFORMATION[dataset_name].ignore_label file_pattern = _FILE_PATTERN file_pattern = os.path.join(dataset_dir, file_pattern % split_name) # Specify how the TF-Examples are decoded. keys_to_features = { 'image/encoded': tf.FixedLenFeature( (), tf.string, default_value=''), 'image/filename': tf.FixedLenFeature( (), tf.string, default_value=''), 'image/format': tf.FixedLenFeature( (), tf.string, default_value='jpeg'), 'image/height': tf.FixedLenFeature( (), tf.int64, default_value=0), 'image/width': tf.FixedLenFeature( (), tf.int64, default_value=0), 'image/segmentation/class/encoded': tf.FixedLenFeature( (), tf.string, default_value=''), 'image/segmentation/class/format': tf.FixedLenFeature( (), tf.string, default_value='png'), } items_to_handlers = { 'image': tfexample_decoder.Image( image_key='image/encoded', format_key='image/format', channels=3), 'image_name': tfexample_decoder.Tensor('image/filename'), 'height': tfexample_decoder.Tensor('image/height'), 'width': tfexample_decoder.Tensor('image/width'), 'labels_class': tfexample_decoder.Image( image_key='image/segmentation/class/encoded', format_key='image/segmentation/class/format', channels=1), } decoder = tfexample_decoder.TFExampleDecoder( keys_to_features, items_to_handlers) return dataset.Dataset( data_sources=file_pattern, reader=tf.TFRecordReader, decoder=decoder, num_samples=splits_to_sizes[split_name], items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, ignore_label=ignore_label, num_classes=num_classes, name=dataset_name, multi_label=True)
def arbitrary_style_image_inputs(style_dataset_file, batch_size=None, image_size=None, center_crop=True, shuffle=True, augment_style_images=False, random_style_image_size=False, min_rand_image_size=128, max_rand_image_size=300): """Loads a batch of random style image given the path of tfrecord dataset. This method does not return pre-compute Gram matrices for the images like style_image_inputs. But it can provide data augmentation. If augment_style_images is equal to True, then style images will randomly modified (eg. changes in brightness, hue or saturation) for data augmentation. If random_style_image_size is set to True then all images in one batch will be resized to a random size. Args: style_dataset_file: str, path to the tfrecord dataset of style files. batch_size: int. If provided, batches style images. Defaults to None. image_size: int. The images will be resized bilinearly so that the smallest side has size image_size. Defaults to None. center_crop: bool. If True, center-crops to [image_size, image_size]. Defaults to False. shuffle: bool, whether to shuffle style files at random. Defaults to False. augment_style_images: bool. Wheather to augment style images or not. random_style_image_size: bool. If this value is True, then all the style images in one batch will be resized to a random size between min_rand_image_size and max_rand_image_size. min_rand_image_size: int. If random_style_image_size is True, this value specifies the minimum image size. max_rand_image_size: int. If random_style_image_size is True, this value specifies the maximum image size. Returns: 4-D tensor of shape [1, ?, ?, 3] with values in [0, 1] for the style image (with random changes for data augmentation if augment_style_image_size is set to true), and 0-D tensor for the style label, 4-D tensor of shape [1, ?, ?, 3] with values in [0, 1] for the style image without random changes for data augmentation. Raises: ValueError: if center cropping is requested but no image size is provided, or if batch size is specified but center-cropping or augment-style-images is not requested, or if both augment-style-images and center-cropping are requested. """ if center_crop and image_size is None: raise ValueError('center-cropping requires specifying the image size.') if center_crop and augment_style_images: raise ValueError( 'When augment_style_images is true images will be randomly cropped.' ) if batch_size is not None and not center_crop and not augment_style_images: raise ValueError( 'batching requires same image sizes (Set center-cropping or ' 'augment_style_images to true)') with tf.name_scope('style_image_processing'): # Force all input processing onto CPU in order to reserve the GPU for the # forward inference and back-propagation. with tf.device('/cpu:0'): filename_queue = tf.train.string_input_producer( [style_dataset_file], shuffle=False, capacity=1, name='filename_queue') if shuffle: examples_queue = tf.RandomShuffleQueue( capacity=64, min_after_dequeue=32, dtypes=[tf.string], name='random_examples_queue') else: examples_queue = tf.FIFOQueue(capacity=64, dtypes=[tf.string], name='fifo_examples_queue') reader = tf.TFRecordReader() _, value = reader.read(filename_queue) enqueue_ops = [examples_queue.enqueue([value])] tf.train.queue_runner.add_queue_runner( tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops)) example_serialized = examples_queue.dequeue() features = tf.parse_single_example( example_serialized, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string) }) image = tf.image.decode_jpeg(features['image_raw']) image.set_shape([None, None, 3]) label = features['label'] if image_size is not None: image_channels = image.shape[2].value if augment_style_images: image_orig = image image = tf.image.random_brightness(image, max_delta=0.8) image = tf.image.random_saturation(image, lower=0.5, upper=1.5) image = tf.image.random_hue(image, max_delta=0.2) image = tf.image.random_flip_left_right(image) image = tf.image.random_flip_up_down(image) random_larger_image_size = random_ops.random_uniform( [], minval=image_size + 2, maxval=image_size + 200, dtype=dtypes.int32) image = _aspect_preserving_resize( image, random_larger_image_size) image = tf.random_crop( image, size=[image_size, image_size, image_channels]) image.set_shape([image_size, image_size, image_channels]) image_orig = _aspect_preserving_resize( image_orig, image_size + 2) image_orig = _central_crop([image_orig], image_size, image_size)[0] image_orig.set_shape([image_size, image_size, 3]) elif center_crop: image = _aspect_preserving_resize(image, image_size + 2) image = _central_crop([image], image_size, image_size)[0] image.set_shape([image_size, image_size, image_channels]) image_orig = image else: image = _aspect_preserving_resize(image, image_size) image_orig = image image = tf.to_float(image) / 255.0 image_orig = tf.to_float(image_orig) / 255.0 if batch_size is None: image = tf.expand_dims(image, 0) else: [image, image_orig, label] = tf.train.batch([image, image_orig, label], batch_size=batch_size) if random_style_image_size: # Selects a random size for the style images and resizes all the images # in the batch to that size. image = _aspect_preserving_resize( image, random_ops.random_uniform([], minval=min_rand_image_size, maxval=max_rand_image_size, dtype=dtypes.int32)) return image, label, image_orig
print(tf.__version__) print("Finish parameter setting") filename = "/planes_scannet_val.tfrecords" filename_queue = tf.train.string_input_producer(['/planes_scannet_val.tfrecords'], num_epochs=1) reader = tf.TFRecordReader() print(filename_queue) _, serialized_example = reader.read(filename_queue) print(serialized_example) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ #'height': tf.FixedLenFeature([], tf.int64), #'width': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), 'image_path': tf.FixedLenFeature([], tf.string), 'num_planes': tf.FixedLenFeature([], tf.int64), 'plane': tf.FixedLenFeature([NUM_PLANES * 3], tf.float32), #'plane_relation': tf.FixedLenFeature([NUM_PLANES * NUM_PLANES], tf.float32), 'segmentation_raw': tf.FixedLenFeature([], tf.string), 'depth': tf.FixedLenFeature([HEIGHT * WIDTH], tf.float32), 'normal': tf.FixedLenFeature([HEIGHT * WIDTH * 3], tf.float32), 'semantics_raw': tf.FixedLenFeature([], tf.string), 'boundary_raw': tf.FixedLenFeature([], tf.string), 'info': tf.FixedLenFeature([4 * 4 + 4], tf.float32), }) sess=tf.Session() print("stuck after this:1") sess.run(tf.global_variables_initializer())
def load_examples(): if a.mode == 'train': filename_queue = tf.train.string_input_producer([a.train_tfrecord]) elif a.mode == 'test': filename_queue = tf.train.string_input_producer([a.test_tfrecord]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'im_mul_raw': tf.FixedLenFeature([], tf.string), 'im_blur_raw': tf.FixedLenFeature([], tf.string), 'im_lr_raw': tf.FixedLenFeature([], tf.string), 'im_ndvi_raw': tf.FixedLenFeature([], tf.string), 'im_ndwi_raw': tf.FixedLenFeature([], tf.string), 'im_pan_raw': tf.FixedLenFeature([], tf.string), }) im_mul_1_raw = tf.decode_raw(features['im_mul_raw'], tf.uint8) im_mul_1_raw = tf.reshape(im_mul_1_raw, [128, 128, 4]) im_mul_1_raw = tf.cast(im_mul_1_raw, tf.float32) im_lr_1_raw = tf.decode_raw(features['im_lr_raw'], tf.uint8) im_lr_1_raw = tf.reshape(im_lr_1_raw, [32, 32, 4]) im_lr_1_raw = tf.cast(im_lr_1_raw, tf.float32) im_ndvi_raw = tf.decode_raw(features['im_ndvi_raw'], tf.uint8) im_ndvi_raw = tf.reshape(im_ndvi_raw, [128, 128, 1]) im_ndvi_raw = tf.cast(im_ndvi_raw, tf.float32) im_ndwi_raw = tf.decode_raw(features['im_ndwi_raw'], tf.uint8) im_ndwi_raw = tf.reshape(im_ndwi_raw, [128, 128, 1]) im_ndwi_raw = tf.cast(im_ndwi_raw, tf.float32) im_pan_raw = tf.decode_raw(features['im_pan_raw'], tf.uint8) im_pan_raw = tf.reshape(im_pan_raw, [128, 128, 1]) im_pan_raw = tf.cast(im_pan_raw, tf.float32) im_blur_1_raw = tf.decode_raw(features['im_blur_raw'], tf.uint8) im_blur_1_raw = tf.reshape(im_blur_1_raw, [128, 128, 4]) im_blur_1_raw = tf.cast(im_blur_1_raw, tf.float32) if a.mode == 'train': inputs1_batch, inputs2_batch, inputs3_batch, inputs4_batch, targets_batch, inputs5_batch = tf.train.shuffle_batch( [im_blur_1_raw, im_ndvi_raw, im_ndwi_raw, im_pan_raw, im_mul_1_raw, im_lr_1_raw], batch_size=a.batch_size, capacity=200, min_after_dequeue=100) steps_per_epoch = int(a.train_count / a.batch_size) elif a.mode == 'test': inputs1_batch, inputs2_batch, inputs3_batch, inputs4_batch, targets_batch, inputs5_batch = tf.train.batch( [im_blur_1_raw, im_ndvi_raw, im_ndwi_raw, im_pan_raw, im_mul_1_raw, im_lr_1_raw], batch_size=a.batch_size, capacity=200) steps_per_epoch = int(a.test_count / a.batch_size) return Examples( inputs1=inputs1_batch, inputs2=inputs2_batch, inputs3=inputs3_batch, inputs4=inputs4_batch, inputs5=inputs5_batch, targets=targets_batch, steps_per_epoch=steps_per_epoch, )
import tensorflow as tf reader = tf.TFRecordReader() filename_queue = tf.train.string_input_producer(["./records/output.tfrecords"]) # 从文件中读出一个样例 _, serialized_example = reader.read(filename_queue) # 解析读入的一个样例 features = tf.parse_single_example( serialized_example, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'pixels': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.int64), }) images = tf.decode_raw(features['image_raw'], tf.uint8) labels = tf.cast(features['label'], tf.int32) pixels = tf.cast(features['pixels'], tf.int32) sess = tf.Session() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) for i in range(10): inage, label, pixel = sess.run([images, labels, pixels])
def parser(record): # preprocess "inp_perm" and "tgt_perm" def _process_perm_feature(example, prefix): for b in range(len(bin_sizes)): cnt = example.pop("{}_cnt_{}".format(prefix, b))[0] tup = example.pop("{}_tup_{}".format(prefix, b)) tup = tf.reshape(tf.sparse_tensor_to_dense(tup), shape=[cnt, 2]) # tf.float32 perm = tf.sparse_to_dense( sparse_indices=tup, output_shape=[tgt_len, bin_sizes[b]], sparse_values=1.0, default_value=0.0) example["{}_perm_{}".format(prefix, b)] = perm # whether allow the last batch with a potentially shorter length if use_tpu: record_spec = { "inputs": tf.FixedLenFeature([tgt_len], tf.int64), "labels": tf.FixedLenFeature([tgt_len], tf.int64), } else: record_spec = { "inputs": tf.VarLenFeature(tf.int64), "labels": tf.VarLenFeature(tf.int64), } # permutation related features if bin_sizes and use_tpu: # tf.float32 record_spec["inp_mask"] = tf.FixedLenFeature([tgt_len], tf.float32) record_spec["tgt_mask"] = tf.FixedLenFeature([tgt_len], tf.float32) record_spec["head_labels"] = tf.FixedLenFeature([tgt_len], tf.int64) for b in range(len(bin_sizes)): record_spec["inp_cnt_{}".format(b)] = tf.FixedLenFeature( [1], tf.int64) record_spec["inp_tup_{}".format(b)] = tf.VarLenFeature( tf.int64) record_spec["tgt_cnt_{}".format(b)] = tf.FixedLenFeature( [1], tf.int64) record_spec["tgt_tup_{}".format(b)] = tf.VarLenFeature( tf.int64) # retrieve serialized example example = tf.parse_single_example(serialized=record, features=record_spec) # transform permutation tuples to permutation matrices if bin_sizes and use_tpu: _process_perm_feature(example, "inp") _process_perm_feature(example, "tgt") # cast int64 into int32 # cast sparse to dense for key in list(example.keys()): val = example[key] if tf.keras.backend.is_sparse(val): val = tf.sparse.to_dense(val) if val.dtype == tf.int64: val = tf.to_int32(val) example[key] = val if use_tpu: return example else: return example["inputs"], example["labels"]
def _multi_read_and_decode(filename_queue): """Reads a multi record and converts it to a dictionary of tensors. Args: filename_queue: Tensor Queue, list of input files. Returns: Dictionary of tensors containing the information from a multi record. Image height. """ reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # Defaults are not specified since both keys are required. features={ 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), 'label_1': tf.FixedLenFeature([], tf.int64), 'label_2': tf.FixedLenFeature([], tf.int64), 'image_raw_1': tf.FixedLenFeature([], tf.string), 'image_raw_2': tf.FixedLenFeature([], tf.string), 'merged_raw': tf.FixedLenFeature([], tf.string), }) # Decode 3 images features['image_raw_1'] = tf.decode_raw(features['image_raw_1'], tf.uint8) features['image_raw_2'] = tf.decode_raw(features['image_raw_2'], tf.uint8) features['merged_raw'] = tf.decode_raw(features['merged_raw'], tf.uint8) image_pixels = 36 * 36 features['image_raw_1'].set_shape([image_pixels]) features['image_raw_2'].set_shape([image_pixels]) features['merged_raw'].set_shape([image_pixels]) # Convert from [0, 255] -> [-0.5, 0.5] floats. features['image_raw_1'] = tf.cast(features['image_raw_1'], tf.float32) * (1. / 255) features['image_raw_2'] = tf.cast(features['image_raw_2'], tf.float32) * (1. / 255) features['merged_raw'] = tf.cast(features['merged_raw'], tf.float32) * (1. / 255) # Convert label from a scalar uint8 tensor to an int32 scalar. features['label_1'] = tf.cast(features['label_1'], tf.int32) features['label_2'] = tf.cast(features['label_2'], tf.int32) # Convert the dictionary to the format used in the code. res_features = {} res_features['height'] = features['height'] res_features['depth'] = features['depth'] res_features['images'] = features['merged_raw'] res_features['labels'] = tf.one_hot(features['label_1'], 10) + tf.one_hot( features['label_2'], 10) res_features['recons_label'] = features['label_1'] res_features['recons_image'] = features['image_raw_1'] res_features['spare_label'] = features['label_2'] res_features['spare_image'] = features['image_raw_2'] return res_features, 36
def _read_and_decode(filename_queue, image_dim=28, distort=False, split='train', evaluate=False): """Reads a single record and converts it to a tensor. Args: filename_queue: Tensor Queue, list of input files. image_dim: Scalar, the height (and width) of the image in pixels. distort: Boolean, whether to distort the input or not. split: String, the split of the data (test or train) to read from. evaluate: If set, avoid distorting the image. Returns: Dictionary of the (Image, label) and the image height. """ reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64) }) # Convert from a scalar string tensor (whose single string has # length image_pixel*image_pixel) to a uint8 tensor with shape # [image_pixel, image_pixel, 1]. image = tf.decode_raw(features['image_raw'], tf.uint8) image = tf.reshape(image, [image_dim, image_dim, 1]) image.set_shape([image_dim, image_dim, 1]) # Convert from [0, 255] -> [-0.5, 0.5] floats. image = tf.cast(image, tf.float32) * (1. / 255) if distort: cropped_dim = image_dim - 4 if not evaluate: image = tf.reshape(image, [image_dim, image_dim]) image = tf.random_crop(image, [cropped_dim, cropped_dim]) # 0.26179938779 is 15 degress in radians image = tf.contrib.image.rotate( image, random.uniform(-0.26179938779, 0.26179938779)) image = tf.reshape(image, [cropped_dim, cropped_dim, 1]) image.set_shape([cropped_dim, cropped_dim, 1]) else: fraction = cropped_dim / image_dim image = tf.image.central_crop(image, central_fraction=fraction) image.set_shape([cropped_dim, cropped_dim, 1]) image_dim = cropped_dim # Convert label from a scalar uint8 tensor to an int32 scalar. label = tf.cast(features['label'], tf.int32) features = { 'images': image, 'labels': tf.one_hot(label, 10), 'recons_image': image, 'recons_label': label, } return features, image_dim
import tensorflow as tf import glob data_path = '../datasets/coco/2017_training/tfrecords/' # address to save the hdf5 file # data_path = '/data/cvg/lukas/datasets/coco/2017_test/version/v2/final/' data_path = '/data/cvg/lukas/datasets/coco/2017_training/version/v6/tmp/' data_path = '/data/cvg/lukas/datasets/coco/2017_training/version/v6/final/' data_path = '/data/cvg/lukas/datasets/coco/2017_test/version/v3/final/' data_path = '/home/lz01a008/git/yad2k/YAD2K/voc_conversion_scripts/VOCdevkit/tfrecords/train/' name_image_feature = 'image/encoded' name_image_feature = 'encoded' with tf.Session() as sess: feature = {name_image_feature: tf.FixedLenFeature([], tf.string)} all_files = glob.glob(data_path + '*') all_files = all_files if len(all_files) > 0 else [data_path] print('counting in %s files...' % (len(all_files))) # Create a list of filenames and pass it to a queue filename_queue = tf.train.string_input_producer(all_files, num_epochs=1) # Define a reader and read the next record reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # Decode the record read by the reader features = tf.parse_single_example(serialized_example, features=feature) #image = tf.decode_raw(features['image/encoded'], tf.uint8) print(features) image = features[name_image_feature]
def main(_): assert FLAGS.tfrecord, '`tfrecord` is missing.' filename = [FLAGS.tfrecord] number = number_of_jpeg(filename) filename_queue = tf.train.string_input_producer(filename) #Read tfrecord reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/object/bbox/xmin': tf.VarLenFeature(tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(tf.float32), 'image/object/class/label': tf.VarLenFeature(tf.int64), 'image/encoded': tf.FixedLenFeature([], tf.string), }) image = tf.image.decode_jpeg(features['image/encoded']) #label = tf.cast(features['image/object/class/label'], tf.int32) if not os.path.exists(FLAGS.output_filepath): os.makedirs(FLAGS.output_filepath) if FLAGS.pdf: pp = PdfPages(FLAGS.output_filepath + '/out.pdf') category_index = None if FLAGS.pbtxt: category_index = label_map_util.create_category_index_from_labelmap( FLAGS.pbtxt) print(category_index) with tf.Session() as sess: init_op = tf.initialize_all_variables() sess.run(init_op) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) t_h = tf.cast(features['image/height'], tf.int32) t_w = tf.cast(features['image/width'], tf.int32) #single_shape = tf.parallel_stack([1]) d_label = tf.sparse_tensor_to_dense( features['image/object/class/label'], default_value=0) d_xm = tf.sparse_tensor_to_dense(features['image/object/bbox/xmin'], default_value=0) d_xM = tf.sparse_tensor_to_dense(features['image/object/bbox/xmax'], default_value=0) d_ym = tf.sparse_tensor_to_dense(features['image/object/bbox/ymin'], default_value=0) d_yM = tf.sparse_tensor_to_dense(features['image/object/bbox/ymax'], default_value=0) #t_xm = tf.cast(features['image/object/bbox/xmin'], [tf.float32]) #t_xM = tf.cast(features['image/object/bbox/xmax'], [tf.float32]) #t_ym = tf.cast(features['image/object/bbox/ymin'], [tf.float32]) #t_yM = tf.cast(features['image/object/bbox/ymax'], [tf.float32]) t_l = tf.reshape(d_label, [-1]) t_xm = tf.reshape(d_xm, [-1]) t_xM = tf.reshape(d_xM, [-1]) t_ym = tf.reshape(d_ym, [-1]) t_yM = tf.reshape(d_yM, [-1]) boxid = 0 #axarr.autoscale(True) for i in range(number): #image = tf.reshape(image, [w, h, 3]) example, ls, w, h, xm, xM, ym, yM = sess.run( [image, d_label, t_w, t_h, t_xm, t_xM, t_ym, t_yM]) img_data_jpg = tf.image.convert_image_dtype(example, dtype=tf.uint8) encode_image_jpg = tf.image.encode_jpeg(img_data_jpg) folder = FLAGS.output_filepath bx = [] bl = [] label = [] for idx, l in enumerate(ls): #if l not in label: #label.append(str(l)) bx.append([ym[idx], xm[idx], yM[idx], xM[idx]]) #bl.append([str(l)]) labelname = str(l) if category_index: labeldic = category_index[int(l)] labelname = labeldic['name'] bl.append([labelname]) if labelname not in label: label.append(labelname) newname=folder+"/"+str(boxid)+'['+str(w)+'x'+str(h)+']'+'Label_'+labelname+'@[' \ +str(int(round(xm[idx]*w)))+','+str(int(round(ym[idx]*h)))+']to[' \ +str(int(round(xM[idx]*w)))+','+str(int(round(yM[idx]*h)))+'].jpg' with tf.gfile.GFile(newname, 'wb') as f: f.write(encode_image_jpg.eval()) boxid += 1 if FLAGS.pdf: num = len(label) with Image.open(newname) as im: shape = len(np.array(im).shape) #print(shape) for idx, l in enumerate(bl): lid = label.index(l[0]) if shape == 3: gb = int(lid * 255.0 / num) c = (255, 255 - gb, gb) elif shape == 2: c = int(255 - (lid * 128.0 / num)) #print(bx[idx]) #print(bl[idx]) #print(c) vis_util.draw_bounding_boxes_on_image( im, np.array([bx[idx]]), color=c, display_str_list_list=[bl[idx]]) plt.imshow(im) pp.savefig() print(str(bl[idx]) + ":" + str(w) + "x" + str(h)) #input('press return to continue') if FLAGS.pdf: pp.close() coord.request_stop() coord.join(threads)
def parse_tfrecord_tf(self, record): features = tf.parse_single_example(record, features={ 'shape': tf.FixedLenFeature([3], tf.int64), 'data': tf.FixedLenFeature([], tf.string)}) data = tf.decode_raw(features['data'], tf.as_dtype(self.dtype)) return tf.reshape(data, features['shape'])
def main(_): with tf.device( tf.train.replica_device_setter(worker_device="/job:%s/task:%d" % (FLAGS.job_name, FLAGS.task_index), ps_device="/job:ps/cpu:0", cluster=cluster)): # # 1. read training data # # image - 784 (=28 x 28) elements of grey-scaled integer value [0, 1] # label - digit (0, 1, ..., 9) train_queue = tf.train.string_input_producer( [FLAGS.train_file], num_epochs=200) # when all data is read, it raises OutOfRange train_reader = tf.TFRecordReader() _, train_serialized_exam = train_reader.read(train_queue) train_exam = tf.parse_single_example(train_serialized_exam, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64) }) train_image = tf.decode_raw(train_exam['image_raw'], tf.uint8) train_image.set_shape([784]) train_image = tf.cast(train_image, tf.float32) * (1. / 255) train_label = tf.cast(train_exam['label'], tf.int32) train_batch_image, train_batch_label = tf.train.batch( [train_image, train_label], batch_size=batch_size) # # for debugging... (check input) # with tf.Session() as sess: # sess.run(tf.global_variables_initializer()) # tf.train.start_queue_runners(sess=sess) # for i in range(2): # debug_image, debug_label = sess.run([train_batch_image, train_batch_label]) # tf.summary.image('images', debug_image) # print(debug_label) # # 2. define graph # # define input plchd_image = tf.placeholder( dtype=tf.float32, shape=(batch_size, 784) ) # here we use fixed dimension with batch_size. (Please use undefined dimension with None in production.) plchd_label = tf.placeholder( dtype=tf.int32, shape=(batch_size) ) # here we use fixed dimension with batch_size. (Please use undefined dimension with None in production.) # define network and inference # (simple 2 fully connected hidden layer : 784->128->64->10) with tf.name_scope('hidden1'): weights = tf.Variable(tf.truncated_normal([784, 128], stddev=1.0 / math.sqrt(float(784))), name='weights') biases = tf.Variable(tf.zeros([128]), name='biases') hidden1 = tf.nn.relu(tf.matmul(plchd_image, weights) + biases) with tf.name_scope('hidden2'): weights = tf.Variable(tf.truncated_normal([128, 64], stddev=1.0 / math.sqrt(float(128))), name='weights') biases = tf.Variable(tf.zeros([64]), name='biases') hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases) with tf.name_scope('softmax_linear'): weights = tf.Variable(tf.truncated_normal([64, 10], stddev=1.0 / math.sqrt(float(64))), name='weights') biases = tf.Variable(tf.zeros([10]), name='biases') logits = tf.matmul(hidden2, weights) + biases global_step = tf.train.get_or_create_global_step() # define optimization (training) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.07) loss = tf.losses.sparse_softmax_cross_entropy(labels=plchd_label, logits=logits) train_op = optimizer.minimize(loss=loss, global_step=global_step) # # 3. run session # with tf.train.MonitoredTrainingSession( master=server.target, checkpoint_dir=FLAGS.out_dir, is_chief=is_chief, ) as sess: # use tf.train.MonitoredTrainingSession for more advanced features ... # train !!! local_step_value = 0 array_image, array_label = sess.run( [train_batch_image, train_batch_label]) while not sess.should_stop(): feed_dict = { plchd_image: array_image, plchd_label: array_label } _, global_step_value, loss_value, array_image, array_label = sess.run( [ train_op, global_step, loss, train_batch_image, train_batch_label ], feed_dict=feed_dict) local_step_value += 1 if local_step_value % 100 == 0: print( "Worker: Local Step %d, Global Step %d (Loss: %.2f)" % (local_step_value, global_step_value, loss_value)) print('Training done !')
def slim_get_split(file_pattern='{}_????'): # Features in Pascal VOC TFRecords. keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 'image/image_id': tf.FixedLenFeature([1], tf.int64), 'image/height': tf.FixedLenFeature([1], tf.int64), 'image/width': tf.FixedLenFeature([1], tf.int64), 'image/channels': tf.FixedLenFeature([1], tf.int64), 'image/shape': tf.FixedLenFeature([3], tf.int64), 'image/object/bbox/xmin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/label': tf.VarLenFeature(dtype=tf.int64), } items_to_handlers = { 'image': slim.tfexample_decoder.Image('image/encoded', 'image/format'), 'image_id': slim.tfexample_decoder.Tensor('image/image_id'), 'shape': slim.tfexample_decoder.Tensor('image/shape'), 'object/bbox': slim.tfexample_decoder.BoundingBox(['ymin', 'xmin', 'ymax', 'xmax'], 'image/object/bbox/'), 'object/label': slim.tfexample_decoder.Tensor('image/object/bbox/label'), } decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) dataset = slim.dataset.Dataset(data_sources=file_pattern, reader=tf.TFRecordReader, decoder=decoder, num_samples=100, items_to_descriptions=None, num_classes=81, labels_to_names=None) with tf.name_scope('dataset_data_provider'): provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=2, common_queue_capacity=32, common_queue_min=8, shuffle=True, num_epochs=1) [org_image, image_id, shape, glabels_raw, gbboxes_raw] = provider.get( ['image', 'image_id', 'shape', 'object/label', 'object/bbox']) image, glabels, gbboxes = ssd_preprocessing.preprocess_image( org_image, glabels_raw, gbboxes_raw, [304, 304], is_training=True, data_format='channels_last', output_rgb=False) # image = tf.transpose(image, perm=(1, 2, 0)) save_image_op = tf.py_func(save_image_with_bbox, [ ssd_preprocessing.unwhiten_image(image), tf.clip_by_value(glabels, 0, tf.int64.max), tf.ones_like(glabels), gbboxes ], tf.int64, stateful=True) # out_shape = [304, 304] # image = tf.to_float(org_image) # image = tf.image.resize_images(image, out_shape, method=tf.image.ResizeMethod.BILINEAR, align_corners=False) # image.set_shape(out_shape + [3]) # save_image_op = tf.py_func(save_image_with_bbox, [image, glabels_raw, tf.ones_like(glabels_raw), gbboxes_raw], tf.int64, stateful=True) """ ============before ssd_preprocessing===============: image: Tensor("dataset_data_provider/case/cond/Merge:0", shape=(?, ?, 3), dtype=uint8) glabels_raw: Tensor("dataset_data_provider/SparseToDense:0", shape=(?,), dtype=int64) gbboxes_raw: Tensor("dataset_data_provider/transpose:0", shape=(?, 4), dtype=float32) ============after ssd_preprocessing================: image: Tensor("ssd_preprocessing_train/merge_bgr:0", shape=(304, 304, 3), dtype=float32) glabels_raw: Tensor("ssd_preprocessing_train/ssd_random_sample_patch_wrapper/cond/Merge_1:0", shape=(?,), dtype=int64) gbboxes_raw: Tensor("ssd_preprocessing_train/random_flip_left_right/cond_1/Merge:0", shape=(?, 4), dtype=float32) """ return save_image_op
def _parse_function(self, example_proto): """Function to parse the example proto. Args: example_proto: Proto in the format of tf.Example. Returns: A dictionary with parsed image, label, height, width and image name. Raises: ValueError: Label is of wrong shape. """ # Currently only supports jpeg and png. # Need to use this logic because the shape is not known for # tf.image.decode_image and we rely on this info to # extend label if necessary. def _decode_image(content, channels): return tf.cond(tf.image.is_jpeg(content), lambda: tf.image.decode_jpeg(content, channels), lambda: tf.image.decode_png(content, channels)) features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/filename': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 'image/height': tf.FixedLenFeature((), tf.int64, default_value=0), 'image/width': tf.FixedLenFeature((), tf.int64, default_value=0), 'image/segmentation/class/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/segmentation/class/format': tf.FixedLenFeature((), tf.string, default_value='png'), } parsed_features = tf.parse_single_example(example_proto, features) image = _decode_image(parsed_features['image/encoded'], channels=3) label = None if self.split_name != common.TEST_SET: label = _decode_image( parsed_features['image/segmentation/class/encoded'], channels=1) image_name = parsed_features['image/filename'] if image_name is None: image_name = tf.constant('') sample = { common.IMAGE: image, common.IMAGE_NAME: image_name, common.HEIGHT: parsed_features['image/height'], common.WIDTH: parsed_features['image/width'], } if label is not None: if label.get_shape().ndims == 2: label = tf.expand_dims(label, 2) elif label.get_shape().ndims == 3 and label.shape.dims[2] == 1: pass else: raise ValueError( 'Input label shape must be [height, width], or ' '[height, width, 1].') label.set_shape([None, None, 1]) sample[common.LABELS_CLASS] = label return sample
def file_based_input_fn_builder(input_file, seq_length, is_training, drop_remainder): name_to_features = { "input_ids": tf.FixedLenFeature([sea_length], tf.int64), "input_mask": tf.FixedLenFeature([seq_length], tf.int64), "aux_data": tf.FixedLenFeature([30], tf.int64), "segment_ids": tf.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.FixedLenFeature([], tf.int64), "is_real_example": tf.FixedLenFeature([], tf.int64), } #again using 30 directly in above, although would be better to input in flags def _decode_record(record, name_to_features): example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.to_int32(t) example[name] = t return example def input_fn(params): batch_size = params["batch_size"] # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. d = None if is_training: gap_file = os.path.join(input_file, "*.tsv.tf_record") gap = tf.data.Dataset.list_files(gap_file)\ .flat_map(tf.data.TFRecordDataset)\ .take(64) if len(tf.gfile.ListDirectory(input_file)) > 3: pseudo_file = os.path.join(input_file, "*-tsv.tf_record") pseudo = tf.data.Dataset.list_files(pseudo_file)\ .flat_map(tf.data.TFRecordDataset)\ .shuffle(buffer_size=1000)\ .take(128) d = gap.concatenate(pseudo).shuffle(buffer_size=128) else: d = tf.data.Dataset.list_files(gap_file)\ .flat_map(tf.data.TFRecordDataset) d = d.repeat() else: d = tf.data.TFRecordDataset(input_file) d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn
import tensorflow as tf import matplotlib.pyplot as plt import numpy as np path = "/media/hlee/Work/set/output.tfrecords" files = tf.train.match_filenames_once(path) #获取所有符合正则表达式的文件,返回文件列表 filename_queue = tf.train.string_input_producer( [path], shuffle=False) # create a queue reader = tf.TFRecordReader() _, serialized_example = reader.read( filename_queue) # return file_name and file features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'img': tf.FixedLenFeature([], tf.string), 'width': tf.FixedLenFeature([], tf.int64), 'height': tf.FixedLenFeature([], tf.int64), 'channels': tf.FixedLenFeature([], tf.int64) }) # return image and label # img = test_tf.image.convert_image_dtype(img, dtype=test_tf.float32) # img = test_tf.reshape(img, [512, 80, 3]) # reshape image to 512*80*3 # img = test_tf.cast(img, test_tf.float32) * (1. / 255) - 0.5 # throw img tensor label = tf.cast(features['label'], tf.int32) # throw label tensor
def getSplit(self, split_name): ############################################################### # # Obtains the training/validation split # ############################################################### #Check whether the split_name is train or validation if split_name not in ['train', 'validation']: raise ValueError( 'The split_name %s is not recognized. Please input either train or validation as the split_name' % (split_name)) #Create the full path for a general FilePattern to locate the tfrecord_files FilePattern_path = os.path.join( self.confs["Classifier"]["DatasetDir"], self.confs["Classifier"]["FilePattern"] % (split_name)) #Count the total number of examples in all of these shard num_samples = 0 FilePattern_for_counting = '200label_' + split_name tfrecords_to_count = [ os.path.join(self.confs["Classifier"]["DatasetDir"], file) for file in os.listdir(self.confs["Classifier"]["DatasetDir"]) if file.startswith(FilePattern_for_counting) ] #print(tfrecords_to_count) for tfrecord_file in tfrecords_to_count: for record in tf.python_io.tf_record_iterator(tfrecord_file): num_samples += 1 #Create a reader, which must be a TFRecord reader in this case reader = tf.TFRecordReader #Create the keys_to_features dictionary for the decoder keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpg'), 'image/class/label': tf.FixedLenFeature([], tf.int64, default_value=tf.zeros([], dtype=tf.int64)), } #Create the items_to_handlers dictionary for the decoder. items_to_handlers = { 'image': slim.tfexample_decoder.Image(), 'label': slim.tfexample_decoder.Tensor('image/class/label'), } #Start to create the decoder decoder = slim.tfexample_decoder.TFExampleDecoder( keys_to_features, items_to_handlers) #Create the labels_to_name file labels_to_name_dict = self.labelsToName #Actually create the dataset dataset = slim.dataset.Dataset( data_sources=FilePattern_path, decoder=decoder, reader=reader, num_readers=4, num_samples=num_samples, num_classes=self.confs["Classifier"]["NumClasses"], labels_to_name=labels_to_name_dict, items_to_descriptions=self.items_to_descriptions) return dataset
def file_based_input_fn_builder(input_file, seq_length, is_training, drop_remainder): """Creates an `input_fn` closure to be passed to TPUEstimator.""" name_to_features = { "input_ids": tf.FixedLenFeature([seq_length], tf.int64), "input_mask": tf.FixedLenFeature([seq_length], tf.float32), "segment_ids": tf.FixedLenFeature([seq_length], tf.int64), "label_ids": tf.FixedLenFeature([], tf.int64), "is_real_example": tf.FixedLenFeature([], tf.int64), } if FLAGS.is_regression: name_to_features["label_ids"] = tf.FixedLenFeature([], tf.float32) tf.logging.info("Input tfrecord file {}".format(input_file)) def _decode_record(record, name_to_features): """Decodes a record to a TensorFlow example.""" example = tf.parse_single_example(record, name_to_features) # tf.Example only supports tf.int64, but the TPU only supports tf.int32. # So cast all int64 to int32. for name in list(example.keys()): t = example[name] if t.dtype == tf.int64: t = tf.cast(t, tf.int32) example[name] = t return example def input_fn(params, input_context=None): """The actual input function.""" if FLAGS.use_tpu: batch_size = params["batch_size"] elif is_training: batch_size = FLAGS.train_batch_size elif FLAGS.do_eval: batch_size = FLAGS.eval_batch_size else: batch_size = FLAGS.predict_batch_size d = tf.data.TFRecordDataset(input_file) # Shard the dataset to difference devices if input_context is not None: tf.logging.info("Input pipeline id %d out of %d", input_context.input_pipeline_id, input_context.num_replicas_in_sync) d = d.shard(input_context.num_input_pipelines, input_context.input_pipeline_id) # For training, we want a lot of parallel reading and shuffling. # For eval, we want no shuffling and parallel reading doesn't matter. if is_training: d = d.shuffle(buffer_size=FLAGS.shuffle_buffer) d = d.repeat() d = d.apply( tf.contrib.data.map_and_batch( lambda record: _decode_record(record, name_to_features), batch_size=batch_size, drop_remainder=drop_remainder)) return d return input_fn
def get_split(split_name, dataset_dir, file_pattern, reader, split_to_sizes, items_to_descriptions, num_classes): """Gets a dataset tuple with instructions for reading Pascal VOC dataset. Args: split_name: A train/test split name. dataset_dir: The base directory of the dataset sources. file_pattern: The file pattern to use when matching the dataset sources. It is assumed that the pattern contains a '%s' string so that the split name can be inserted. reader: The TensorFlow reader type. Returns: A `Dataset` namedtuple. Raises: ValueError: if `split_name` is not a valid train/test split. """ if split_name not in split_to_sizes: raise ValueError('split name %s was not recognized.' % split_name) file_pattern = os.path.join(dataset_dir, file_pattern % split_name) # Allowing None in the signature so that dataset_factory can use the default. if reader is None: reader = tf.TFRecordReader # Features in Pascal VOC TFRecords. keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 'image/height': tf.FixedLenFeature([1], tf.int64), 'image/width': tf.FixedLenFeature([1], tf.int64), 'image/channels': tf.FixedLenFeature([1], tf.int64), 'image/shape': tf.FixedLenFeature([3], tf.int64), 'image/object/bbox/xmin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/label': tf.VarLenFeature(dtype=tf.int64), 'image/object/bbox/difficult': tf.VarLenFeature(dtype=tf.int64), 'image/object/bbox/truncated': tf.VarLenFeature(dtype=tf.int64), } items_to_handlers = { 'image': slim.tfexample_decoder.Image('image/encoded', 'image/format'), 'shape': slim.tfexample_decoder.Tensor('image/shape'), 'object/bbox': slim.tfexample_decoder.BoundingBox(['ymin', 'xmin', 'ymax', 'xmax'], 'image/object/bbox/'), 'object/label': slim.tfexample_decoder.Tensor('image/object/bbox/label'), 'object/difficult': slim.tfexample_decoder.Tensor('image/object/bbox/difficult'), 'object/truncated': slim.tfexample_decoder.Tensor('image/object/bbox/truncated'), } decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) labels_to_names = None if dataset_utils.has_labels(dataset_dir): labels_to_names = dataset_utils.read_label_file(dataset_dir) # else: # labels_to_names = create_readable_names_for_imagenet_labels() # dataset_utils.write_label_file(labels_to_names, dataset_dir) return slim.dataset.Dataset(data_sources=file_pattern, reader=reader, decoder=decoder, num_samples=split_to_sizes[split_name], items_to_descriptions=items_to_descriptions, num_classes=num_classes, labels_to_names=labels_to_names)
def style_image_inputs(style_dataset_file, batch_size=None, image_size=None, square_crop=False, shuffle=True): """Loads a batch of random style image given the path of tfrecord dataset. Args: style_dataset_file: str, path to the tfrecord dataset of style files. The dataset is produced via the create_style_dataset.py script and is made of Example protobufs with the following features: * 'image_raw': byte encoding of the JPEG string of the style image. * 'label': integer identifier of the style image in [0, N - 1], where N is the number of examples in the dataset. * 'vgg_16/<LAYER_NAME>': Gram matrix at layer <LAYER_NAME> of the VGG-16 network (<LAYER_NAME> in {conv,pool}{1,2,3,4,5}) for the style image. batch_size: int. If provided, batches style images. Defaults to None. image_size: int. The images will be resized bilinearly so that the smallest side has size image_size. Defaults to None. square_crop: bool. If True, square-crops to [image_size, image_size]. Defaults to False. shuffle: bool, whether to shuffle style files at random. Defaults to True. Returns: If batch_size is defined, a 4-D tensor of shape [batch_size, ?, ?, 3] with values in [0, 1] for the style image, and 1-D tensor for the style label. Raises: ValueError: if center cropping is requested but no image size is provided, or if batch size is specified but center-cropping is not requested. """ vgg_layers = [ 'vgg_16/conv1', 'vgg_16/pool1', 'vgg_16/conv2', 'vgg_16/pool2', 'vgg_16/conv3', 'vgg_16/pool3', 'vgg_16/conv4', 'vgg_16/pool4', 'vgg_16/conv5', 'vgg_16/pool5' ] if square_crop and image_size is None: raise ValueError('center-cropping requires specifying the image size.') if batch_size is not None and not square_crop: raise ValueError('batching requires center-cropping.') with tf.name_scope('style_image_processing'): filename_queue = tf.train.string_input_producer([style_dataset_file], shuffle=False, capacity=1, name='filename_queue') if shuffle: examples_queue = tf.RandomShuffleQueue( capacity=64, min_after_dequeue=32, dtypes=[tf.string], name='random_examples_queue') else: examples_queue = tf.FIFOQueue(capacity=64, dtypes=[tf.string], name='fifo_examples_queue') reader = tf.TFRecordReader() _, value = reader.read(filename_queue) enqueue_ops = [examples_queue.enqueue([value])] tf.train.queue_runner.add_queue_runner( tf.train.queue_runner.QueueRunner(examples_queue, enqueue_ops)) example_serialized = examples_queue.dequeue() features = tf.parse_single_example( example_serialized, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), 'vgg_16/conv1': tf.FixedLenFeature([64, 64], tf.float32), 'vgg_16/pool1': tf.FixedLenFeature([64, 64], tf.float32), 'vgg_16/conv2': tf.FixedLenFeature([128, 128], tf.float32), 'vgg_16/pool2': tf.FixedLenFeature([128, 128], tf.float32), 'vgg_16/conv3': tf.FixedLenFeature([256, 256], tf.float32), 'vgg_16/pool3': tf.FixedLenFeature([256, 256], tf.float32), 'vgg_16/conv4': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/pool4': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/conv5': tf.FixedLenFeature([512, 512], tf.float32), 'vgg_16/pool5': tf.FixedLenFeature([512, 512], tf.float32) }) image = tf.image.decode_jpeg(features['image_raw']) label = features['label'] gram_matrices = [features[vgg_layer] for vgg_layer in vgg_layers] image.set_shape([None, None, 3]) if image_size: if square_crop: image = _aspect_preserving_resize(image, image_size + 2) image = _central_crop([image], image_size, image_size)[0] image.set_shape([image_size, image_size, 3]) else: image = _aspect_preserving_resize(image, image_size) image = tf.to_float(image) / 255.0 if batch_size is None: image = tf.expand_dims(image, 0) else: image_label_gram_matrices = tf.train.batch([image, label] + gram_matrices, batch_size=batch_size) image, label = image_label_gram_matrices[:2] gram_matrices = image_label_gram_matrices[2:] gram_matrices = dict([ (vgg_layer, gram_matrix) for vgg_layer, gram_matrix in zip(vgg_layers, gram_matrices) ]) return image, label, gram_matrices
def get_split(split_name, dataset_dir, file_pattern=None, reader=None): """Gets a dataset tuple with instructions for reading ImageNet. Args: split_name: A train/test split name. dataset_dir: The base directory of the dataset sources. file_pattern: The file pattern to use when matching the dataset sources. It is assumed that the pattern contains a '%s' string so that the split name can be inserted. reader: The TensorFlow reader type. Returns: A `Dataset` namedtuple. Raises: ValueError: if `split_name` is not a valid train/test split. """ if split_name not in _SPLITS_TO_SIZES: raise ValueError('split name %s was not recognized.' % split_name) if not file_pattern: file_pattern = _FILE_PATTERN file_pattern = os.path.join(dataset_dir, file_pattern % split_name) # Allowing None in the signature so that dataset_factory can use the default. if reader is None: reader = tf.TFRecordReader keys_to_features = { 'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''), 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), 'image/class/label': tf.FixedLenFeature([], dtype=tf.int64, default_value=-1), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/object/bbox/xmin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymin': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/xmax': tf.VarLenFeature(dtype=tf.float32), 'image/object/bbox/ymax': tf.VarLenFeature(dtype=tf.float32), 'image/object/class/label': tf.VarLenFeature(dtype=tf.int64), } items_to_handlers = { 'image': slim.tfexample_decoder.Image('image/encoded', 'image/format'), 'label': slim.tfexample_decoder.Tensor('image/class/label'), 'label_text': slim.tfexample_decoder.Tensor('image/class/text'), 'object/bbox': slim.tfexample_decoder.BoundingBox(['ymin', 'xmin', 'ymax', 'xmax'], 'image/object/bbox/'), 'object/label': slim.tfexample_decoder.Tensor('image/object/class/label'), } decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers) labels_to_names = None if LOAD_READABLE_NAMES: if dataset_utils.has_labels(dataset_dir): labels_to_names = dataset_utils.read_label_file(dataset_dir) else: labels_to_names = create_readable_names_for_imagenet_labels() dataset_utils.write_label_file(labels_to_names, dataset_dir) return slim.dataset.Dataset(data_sources=file_pattern, reader=reader, decoder=decoder, num_samples=_SPLITS_TO_SIZES[split_name], items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, num_classes=_NUM_CLASSES, labels_to_names=labels_to_names)
import cv2 MOVING_AVERAGE_DECAY = 0.99 EVAL_INTERVAL_SECS = 10 image_size = 128 batch_size = 200 files = tf.train.match_filenames_once("train.tfrecords") filename_queue = tf.train.string_input_producer(files, shuffle=True) # filename_queue = tf.train.string_input_producer(["dog_train.tfrecords0"]) #读入流中 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # 返回文件名和文件 features = tf.parse_single_example(serialized_example, features={ 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'label': tf.FixedLenFeature([], tf.int64), 'img_raw': tf.FixedLenFeature([], tf.string), 'channels': tf.FixedLenFeature([], tf.int64), }) # 取出包含image和label的feature对象 image, label = features['img_raw'], features['label'] height, width = features['height'], features['width'] height = tf.cast(height, tf.uint8) width = tf.cast(width, tf.float32) channels = features['channels'] decoded_image = tf.decode_raw(image, tf.uint8)
import tensorflow_transform as tft from string import ascii_lowercase from apache_beam.io import tfrecordio from tensorflow_transform import coders from tensorflow_transform.beam import impl as beam_impl from tensorflow_transform.beam.tft_beam_io import transform_fn_io from tensorflow_transform.tf_metadata import dataset_metadata, metadata_io,dataset_schema DELIMITERS_WORDS=' ' MAX_WORD_LEN=15 MAX_SENTENCE_LEN=25 # IT IS EXTREMELY COSTFUL TO HAVE TO LONG WORDS/SENTENCES TRAIN_DATA_SCHEMA = dataset_schema.from_feature_spec({ 'id': tf.FixedLenFeature(shape=[], dtype=tf.float32), 'text': tf.FixedLenFeature(shape=[], dtype=tf.string), 'labels': tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN], dtype=tf.int64), 'label': tf.FixedLenFeature(shape=[], dtype=tf.string), "chars" : tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN,MAX_WORD_LEN], dtype=tf.int64), 'label_length': tf.FixedLenFeature(shape=[], dtype=tf.int64), 'sentence_length': tf.FixedLenFeature(shape=[], dtype=tf.int64), 'chars_in_word': tf.FixedLenFeature(shape=[MAX_SENTENCE_LEN], dtype=tf.int64), }) train_metadata = dataset_metadata.DatasetMetadata(schema=TRAIN_DATA_SCHEMA) OUTPUT_FILE_TRAIN="train_records.tfrecords" MAPPING = {a:index for index,a in enumerate(ascii_lowercase + ascii_lowercase.upper())} UNKONWN = len(MAPPING) PADD_VALUE_CHAR = UNKONWN+1
def main(_): if len(sys.argv) < 2 or sys.argv[-1].startswith('-'): print( 'Usage: mnist_export.py [--training_iteration=x] ' '[--model_version=y] export_dir') sys.exit(-1) if FLAGS.training_iteration <= 0: print 'Please specify a positive value for training iteration.' sys.exit(-1) if FLAGS.model_version <= 0: print 'Please specify a positive value for version number.' sys.exit(-1) # Train model print 'Training model...' mnist = mnist_input_data.read_data_sets(FLAGS.work_dir, one_hot=True) sess = tf.InteractiveSession() serialized_tf_example = tf.placeholder(tf.string, name='tf_example') feature_configs = { 'x': tf.FixedLenFeature(shape=[784], dtype=tf.float32), } tf_example = tf.parse_example(serialized_tf_example, feature_configs) x = tf.identity(tf_example['x'], name='x') # use tf.identity() to assign name y_ = tf.placeholder('float', shape=[None, 10]) w = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) sess.run(tf.global_variables_initializer()) y = tf.nn.softmax(tf.matmul(x, w) + b, name='y') cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) train_step = tf.train.GradientDescentOptimizer(0.01).minimize( cross_entropy) values, indices = tf.nn.top_k(y, 10) table = tf.contrib.lookup.index_to_string_table_from_tensor( tf.constant([str(i) for i in xrange(10)])) prediction_classes = table.lookup(tf.to_int64(indices)) for _ in range(FLAGS.training_iteration): batch = mnist.train.next_batch(50) train_step.run(feed_dict={x: batch[0], y_: batch[1]}) correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) print 'training accuracy %g' % sess.run(accuracy, feed_dict={ x: mnist.test.images, y_: mnist.test.labels }) print 'Done training!' # Export model # WARNING(break-tutorial-inline-code): The following code snippet is # in-lined in tutorials, please update tutorial documents accordingly # whenever code changes. export_path_base = sys.argv[-1] export_path = os.path.join(tf.compat.as_bytes(export_path_base), tf.compat.as_bytes(str(FLAGS.model_version))) print 'Exporting trained model to', export_path builder = tf.saved_model.builder.SavedModelBuilder(export_path) # Build the signature_def_map. classification_inputs = tf.saved_model.utils.build_tensor_info( serialized_tf_example) classification_outputs_classes = tf.saved_model.utils.build_tensor_info( prediction_classes) classification_outputs_scores = tf.saved_model.utils.build_tensor_info( values) classification_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={ tf.saved_model.signature_constants.CLASSIFY_INPUTS: classification_inputs }, outputs={ tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES: classification_outputs_classes, tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES: classification_outputs_scores }, method_name=tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME )) tensor_info_x = tf.saved_model.utils.build_tensor_info(x) tensor_info_y = tf.saved_model.utils.build_tensor_info(y) prediction_signature = ( tf.saved_model.signature_def_utils.build_signature_def( inputs={'images': tensor_info_x}, outputs={'scores': tensor_info_y}, method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME) ) legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op') builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ 'predict_images': prediction_signature, tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: classification_signature, }, legacy_init_op=legacy_init_op) builder.save() print 'Done exporting!'
image_width = 32 image_channel = 3 class_numbers = 10 #model storage model_checkpoint_path = join( checkpoint_path, 'model.ckpt') train_summary_path = join( checkpoint_path, 'train' ) valid_summary_path = join( checkpoint_path, 'valid' ) ## data loading root_path = '/home/rachakra/few_shot_learning/prepare_data/output' train_tfrecords = join( root_path, 'train_siamese_pair_{0}.tfrecords'.format( sample_per_class ) ) valid_tfrecords = join( root_path, 'siamese_pair_valid.tfrecords' ) ## information for parsing the tfrecord features={'image_one':tf.FixedLenFeature([], tf.string), 'image_two':tf.FixedLenFeature([], tf.string),\ 'label': tf.FixedLenFeature([], tf.int64 )} train_parser = Parser( features, image_height, image_width, True ) valid_parser = Parser( features, image_height, image_width, False ) ##test files training_list_file = 'train_raw_list_{0}.txt'.format( sample_per_class ) class_valid_list = join( root_path, 'class_valid.pickle' )
def create_sprite_image(examples): """Returns an encoded sprite image for use in Facets Dive. Args: examples: A list of serialized example protos to get images for. Returns: An encoded PNG. """ def generate_image_from_thubnails(thumbnails, thumbnail_dims): """Generates a sprite atlas image from a set of thumbnails.""" num_thumbnails = tf.shape(thumbnails)[0].eval() images_per_row = int(math.ceil(math.sqrt(num_thumbnails))) thumb_height = thumbnail_dims[0] thumb_width = thumbnail_dims[1] master_height = images_per_row * thumb_height master_width = images_per_row * thumb_width num_channels = 3 master = np.zeros([master_height, master_width, num_channels]) for idx, image in enumerate(thumbnails.eval()): left_idx = idx % images_per_row top_idx = int(math.floor(idx / images_per_row)) left_start = left_idx * thumb_width left_end = left_start + thumb_width top_start = top_idx * thumb_height top_end = top_start + thumb_height master[top_start:top_end, left_start:left_end, :] = image return tf.image.encode_png(master) image_feature_name = 'image/encoded' sprite_thumbnail_dim_px = 32 with tf.compat.v1.Session(): keys_to_features = { image_feature_name: tf.FixedLenFeature((), tf.string, default_value=''), } parsed = tf.parse_example(examples, keys_to_features) images = tf.zeros([1, 1, 1, 1], tf.float32) i = tf.constant(0) thumbnail_dims = (sprite_thumbnail_dim_px, sprite_thumbnail_dim_px) num_examples = tf.constant(len(examples)) encoded_images = parsed[image_feature_name] # Loop over all examples, decoding the image feature value, resizing # and appending to a list of all images. def loop_body(i, encoded_images, images): encoded_image = encoded_images[i] image = tf.image.decode_jpeg(encoded_image, channels=3) resized_image = tf.image.resize(image, thumbnail_dims) expanded_image = tf.expand_dims(resized_image, 0) images = tf.cond(tf.equal(i, 0), lambda: expanded_image, lambda: tf.concat([images, expanded_image], 0)) return i + 1, encoded_images, images loop_out = tf.while_loop( lambda i, encoded_images, images: tf.less(i, num_examples), loop_body, [i, encoded_images, images], shape_invariants=[ i.get_shape(), encoded_images.get_shape(), tf.TensorShape(None) ]) # Create the single sprite atlas image from these thumbnails. sprite = generate_image_from_thubnails(loop_out[2], thumbnail_dims) return sprite.eval()
def Train(graph, Batch_size, Iteration): filenames = [("train_1.tfrecords")] print(filenames) filenames_cross = ["train_2.tfrecords"] filename_queue = tf.train.string_input_producer(filenames) # Tensorflow需要队列流输入数据 reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) # tf.parse_single_example为解析器 features = tf.parse_single_example(serialized_example, features={ "Mat": tf.FixedLenFeature([], tf.string), "label": tf.FixedLenFeature([], tf.int64), }) Mat_32 = tf.decode_raw(features["Mat"], tf.float64) Mat_32 = tf.reshape(Mat_32, [32, 32, 1]) Label_32 = tf.cast(features["label"], tf.float32) # Mat_batch,Label64_batch,Label32_batch,Label32_Batch = tf.train.batch\ # ([Mat_64, Label_64,Label_32,Label_32],batch_size= Batch_size,capacity=300,num_threads= 1) # tf.train.shuffle_batch()这个函数对读取到的数据进行了batch处理,这样更有利于后续的训练。 Mat_batch, Label32_batch = tf.train.shuffle_batch \ ([Mat_32, Label_32], batch_size=Batch_size, capacity=3 * Batch_size, min_after_dequeue=5, num_threads=32) print("\n--------- finish reading train data-----------") filename_queue_cross = tf.train.string_input_producer(filenames_cross) reader_cross = tf.TFRecordReader() _, serialized_example_cross = reader_cross.read(filename_queue_cross) features_cross = tf.parse_single_example(serialized_example_cross, features={ "Mat": tf.FixedLenFeature([], tf.string), "label": tf.FixedLenFeature([], tf.int64), }) Mat_32_cross = tf.decode_raw(features_cross["Mat"], tf.float64) Mat_32_cross = tf.reshape(Mat_32_cross, [32, 32, 1]) Label_32_cross = tf.cast(features_cross["label"], tf.float32) Mat_batch_cross, Label32_batch_cross = tf.train.shuffle_batch \ ([Mat_32_cross, Label_32_cross], batch_size=Batch_size, capacity=Batch_size * 3, min_after_dequeue=5, num_threads=32) # saver = tf.train.Saver() # print("Mat_Batttt",Mat_Batch) with tf.Session() as sess: # 开启协调器 coord = tf.train.Coordinator() # 使用start_queue_runners 启动队列填充 # 一定要开启队列,要不然是空的 threads = tf.train.start_queue_runners(sess, coord) initglo = tf.global_variables_initializer() initloc = tf.local_variables_initializer() sess.run(initglo) sess.run(initloc) tf.local_variables_initializer().run() epoch = 9 learning_rate = 0.01 for j in range(epoch): if j % 3 == 0: learning_rate = learning_rate * 0.1 print(learning_rate) acc = 0 loss = 0 acc_cross = 0 loss_cross = 0 for i in range(Iteration): # 这句话的意思是从队列里拿出指定批次的数据 is_training = True mat_32, label_32 = sess.run([Mat_batch, Label32_batch]) mat_32 = mat_32.reshape(Batch_size, 32, 32, 1) # print("Mat_64 is ",Mat_64) # print("Label_64 is ",Label_64) mat_32 = mat_32.astype('float32') label_32 = label_32.astype("float32") label_32 = label_32.reshape(Batch_size, 1) for b in range(Batch_size): mat_32[b] = LoadData.Normalize(mat_32[b]) # LOSS_64, acc_rate64, Pre_lable_64, _ = sess.run( # [graph["LOSS"], graph["acc"], graph["Pre"], graph["Train"]], # feed_dict={ # graph["inputs_Mat"]: mat_64, # graph["ys_label"]: label_64, # graph['is_training']: is_training, # graph["is_64"]: 1, # graph["is_32"]: 0, # graph["kp"]: 0.5, # graph["ln"]:learning_rate, # }) # LOSS_32, acc_rate32, Pre_lable_32, _ = sess.run( # [graph["LOSS"], graph["acc"], graph["Pre"], graph["Train"]], # feed_dict={ # graph["inputs_Mat"]: mat_32, # graph["ys_label"]: label_32, # graph['is_training']: is_training, # graph["is_64"]: 0, # graph["is_32"]: 1, # graph["kp"]: 0.5, # graph["ln"]: learning_rate, # }) # print(label_32) LOSS_32, acc_rate32, Pre_lable_32, _ = sess.run( [ graph["LOSS"], graph["acc"], graph["Pre"], graph["Train"] ], feed_dict={ graph["inputs_Mat"]: mat_32, graph["ys_label"]: label_32, graph['is_training']: is_training, graph["kp"]: 0.5, graph["ln"]: learning_rate, }) if i % 100 == 0: is_training = False print("\n------------- Iteration %d --------------" % (i)) # # print("LOSS64 is ", LOSS_64) # # print("LOSS32 is ", LOSS_32) # print("LOSS16 is ", LOSS_16) # # print("acc_rate64 is : ", acc_rate64) # # print("acc_rate32 is : ", acc_rate32) # print("acc_rate16 is : ", acc_rate16) # # 把输出写入到文件里 # stdout_backup = sys.stdout # log_file = open(YUV + "_RunDetail.log", "a") # sys.stdout = log_file # print("--------------- Iteration %d ---------------" % (i)) # # print("LOSS64 is ", LOSS_64) # print("LOSS32 is ", LOSS_32) # print("LOSS16 is ", LOSS_16) # print("acc_rate64 is : ", acc_rate64) # print("acc_rate32 is : ", acc_rate32) # print("acc_rate16 is : ", acc_rate16) mat_cross32, label_cross32 = sess.run( [Mat_batch_cross, Label32_batch_cross]) mat_cross32 = mat_cross32.reshape(Batch_size, 32, 32, 1) mat_cross32 = mat_cross32.astype("float32") for b in range(Batch_size): mat_cross32[b] = LoadData.Normalize(mat_cross32[b]) label_cross32 = label_cross32.astype("float32") label_cross32 = label_cross32.reshape(Batch_size, 1) LOSS_ceoss32, acc_rate_cross32, Pre_lable_cross32 = sess.run( [graph["LOSS"], graph["acc"], graph["Pre"]], feed_dict={ graph["inputs_Mat"]: mat_cross32, graph["ys_label"]: label_cross32, graph['is_training']: is_training, graph["kp"]: 1.0, }) acc_cross = acc_cross + acc_rate_cross32 loss_cross = loss_cross + LOSS_ceoss32 loss = loss + LOSS_32 acc = acc + acc_rate32 print("*************train*************") print("acc:" + str(acc / 60)) print("loss:" + str(loss / 60)) print("*************cross*************") print("acc:" + str(acc_cross / 60)) print("loss:" + str(loss_cross / 60)) # sum16 = 0 # a16 = 0 # for e in range(Batch_size): # if label_cross16[e][0] == 1: # sum16 += 1 # if Pre_lable_cross16[e][0] == 1: # a16 += 1 # if sum16 != 0: # print("reCall 16 is : ", a16 / sum16) # sum16 = 0 # a16 = 0 # for e in range(Batch_size): # if label_cross16[e][0] == 0: # sum16 += 1 # if Pre_lable_cross16[e][0] == 0: # a16 += 1 # if sum16 != 0: # print("Call 16 is : ", a16 / sum16) if j == 8: print("save the model finally:" + str(i)) var_list = [ var for var in tf.global_variables() if "moving" in var.name ] var_list += tf.trainable_variables() saver = tf.train.Saver(var_list=var_list, max_to_keep=20) Write_List = ['X', 'is_train', 'kp', 'y', 'acc', 'output'] constant_graph = graph_util.convert_variables_to_constants \ (sess, sess.graph_def, Write_List) with tf.gfile.FastGFile(pb_file_path, mode='wb') as f: f.write(constant_graph.SerializeToString()) saver.save(sess, "model/Demo-model") # saver.save(sess, "./model/Demo-model-one") print("save the cross model") coord.request_stop() print("program ending") coord.join(threads)