def main(unused_argv): # if platform.system() == 'Linux': # dataset_dir = "/home/anurag/Desktop/tensorflow_data" # else: # dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" dataset_dir = disk_storage_props.PATCHES_TRAIN_DATA_DIR tf_record_dir = disk_storage_props.PATCHES_TF_RECORD_DIR dataset = get_split('train', tf_record_dir) dataset_provider = dataset_data_provider.DatasetDataProvider( dataset, num_readers=12, shuffle=False) with tf.Session() as sess: #dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" tf.train.start_queue_runners() [image_raw, label] = dataset_provider.get(['image', 'label']) #image_decoded = tf.image.decode_jpeg(image_raw) image_raw, label = sess.run([image_raw, label]) print(label) print(image_raw.shape) #Image.fromarray(np.array(image_raw)).show() #print(image_decoded.shape) #print(image_decoded) #print("\n\n\n\n",image_raw) print(dataset_provider.num_samples()) #images, labels = dataset_provider.get(['image', 'label']) for i in range(dataset_provider.num_samples()): [image_raw, label] = dataset_provider.get(['image', 'label']) image_raw, label = sess.run([image_raw, label]) if label == 0: Image.fromarray(np.array(image_raw)).show()
def get(dataset_dir, dataset_name, split_name, shuffle=True, num_readers=1, common_queue_capacity=64, common_queue_min=50): """Provides input data for a specified dataset and split.""" dataset_to_kwargs = { 'shapenet_chair': { 'file_pattern': '03001627_%s.tfrecords' % split_name, 'num_views': 24, 'image_size': 64, 'vox_size': 32, }, 'shapenet_all': { 'file_pattern': '*_%s.tfrecords' % split_name, 'num_views': 24, 'image_size': 64, 'vox_size': 32, }, } split_sizes = { 'shapenet_chair': { 'train': 4744, 'val': 678, 'test': 1356, }, 'shapenet_all': { 'train': 30643, 'val': 4378, 'test': 8762, } } kwargs = dataset_to_kwargs[dataset_name] kwargs['file_pattern'] = os.path.join(dataset_dir, kwargs['file_pattern']) kwargs['num_samples'] = split_sizes[dataset_name][split_name] dataset_split = _get_split(**kwargs) data_provider = dataset_data_provider.DatasetDataProvider( dataset_split, num_readers=num_readers, common_queue_capacity=common_queue_capacity, common_queue_min=common_queue_min, shuffle=shuffle) inputs = { 'num_samples': dataset_split.num_samples, } [image, mask, vox] = data_provider.get(['image', 'mask', 'vox']) inputs['image'] = image inputs['mask'] = mask inputs['voxel'] = vox return inputs
def testConflictingRecordKeyItem(self): dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(), 'tfrecord_dataset')) with self.cached_session(): with self.assertRaises(ValueError): dataset_data_provider.DatasetDataProvider( _create_tfrecord_dataset(dataset_dir), record_key='image')
def provide_data(dataset_dir, split_name, batch_size, split_type, image_size, preprocess_options=None, grayscale=None, shuffle_data=True, num_preprocessing_threads=4, num_readers=10, add_summary=False): """TODO(vrama): Add documentation.""" # Check that the options are all valid. if preprocess_options != CENTER: raise ValueError("Must preprocess celeba images to be -1 to 1.") dataset_celeba, metadata = celeba_dataset.get_split( split_name=split_name, split_type=split_type, dataset_dir=dataset_dir, image_length=image_size[0]) attribute_names = label_map.LabelMap(metadata['label_map_json']).attributes # Plug it into a data provider. provider = dataset_data_provider.DatasetDataProvider( dataset_celeba, num_readers=num_readers, shuffle=shuffle_data, common_queue_capacity=4 * batch_size, common_queue_min=2 * batch_size) # Extract labels from the data provider. # Note that the celeba dataset is configured in a way that we extract the # center 64x64 pixels from a 148 x 148 center crop of the original celeba # image. # TODO(vrama): Also add blur as done in the vae-gan paper. [image, labels] = provider.get(['image', 'labels']) image = tf.to_float(image) if preprocess_options == CENTER: image = tf.subtract(image, 128.0) image = tf.div(image, 128.0) batch_image, batch_labels = tf.train.batch( [image, labels], batch_size=batch_size, num_threads=num_preprocessing_threads, capacity=2 * batch_size) batch_label_list = tf.unstack(batch_labels, axis=-1, name='unstack_labels') # Add image summary. if add_summary: tf.summary.image("input_images", batch_image) return batch_image, batch_label_list, dataset_celeba.num_samples, metadata[ 'num_classes_per_attribute'], attribute_names
def parseTfTodataset(): """ 将tfrecors文件的数据读出转化为slim需要的datasets :return: """ dataset = flowers.get_split("train", flowersDataPath) dataProvider = provider.DatasetDataProvider(dataset=dataset, common_queue_capacity=32, common_queue_min=1) image, label = dataProvider.get(["image", "label"]) return image, label
def get_batch_preprocess(self, dataset, batch_size): provider = dataset_data_provider.DatasetDataProvider(dataset) image, label, xmin, xmax, ymin, ymax, size = provider.get( ['image','label', 'xmin','xmax','ymin','ymax','size']) #label = array_ops.squeeze(label, [2]) image, label = self.process_image_and_label(image, label) image_b, label_b, xmin_b, xmax_b, ymin_b, ymax_b, size_b = tf.train.batch( [image, label, xmin, xmax, ymin, ymax, size], batch_size = batch_size, num_threads = 4, capacity = 1024) return image_b, label_b, xmin_b, xmax_b, ymin_b, ymax_b, size_b
def load_batch(dataset, batch_size=4, height=299, width=299, is_training=True): import tensorflow.contrib.slim.python.slim.data.dataset_data_provider as provider data_provider = provider.DatasetDataProvider(dataset, common_queue_capacity=8, common_queue_min=1) image_raw, label = data_provider.get(['image', 'label']) image_raw = tf.image.resize_images(image_raw, [height, width]) image_raw = tf.image.convert_image_dtype(image_raw, tf.float32) image_raw, labels = tf.train.batch([image_raw, label], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) return image_raw, labels
def get_examples(config): """Get batched tensor of training data. Args: config: an instance of ads_examples_pb2.AdsExamples. Returns: tensor_dict: a dictionary mapping data names to tensors. 'video_id': tf.string, [batch] 'n_frames': tf.int64, [batch] 'sentiment': tf.int64, [batch] 'sentiment_list': tf.int64, [batch, 30] 'frame_features': tf.float32, [batch, max_frame_len, feature_dims] Raises: ValueError: if config is not an instance of ads_examples_pb2.AdsExamples. """ if not isinstance(config, ads_examples_pb2.AdsExamples): raise ValueError('config has to be an instance of AdsExamples.') num_epochs = None if config.HasField('num_epochs'): num_epochs = config.num_epochs dataset = _create_tfrecord_dataset(config) provider = dataset_data_provider.DatasetDataProvider( dataset, shuffle=True, num_epochs=num_epochs, num_readers=config.data_provider_num_readers, common_queue_capacity=config.data_provider_common_queue_capacity, common_queue_min=config.data_provider_common_queue_min) # Resize image, seperate question and answer. items = filter(lambda x: x != 'record_key', provider.list_items()) tensor_dict = {} data = provider.get(items) for i, item in enumerate(items): tensor_dict[item] = data[i] # Batch. tensor_dict = tf.train.batch(tensor_dict, config.batch_size, capacity=config.batch_op_capacity, num_threads=config.batch_op_num_threads, enqueue_many=False, dynamic_pad=True) return tensor_dict
def testTFRecordDataset(self): dataset_dir = tempfile.mkdtemp( prefix=os.path.join(self.get_temp_dir(), 'tfrecord_dataset')) height = 300 width = 280 with self.test_session(): provider = dataset_data_provider.DatasetDataProvider( _create_tfrecord_dataset(dataset_dir)) image, label = provider.get(['image', 'label']) image = _resize_image(image, height, width) with session.Session('') as sess: with queues.QueueRunners(sess): image, label = sess.run([image, label]) self.assertListEqual([height, width, 3], list(image.shape)) self.assertListEqual([1], list(label.shape))
def main(unused_argv): if platform.system() == 'Linux': dataset_dir = "/home/anurag/Desktop/tensorflow_data" else: dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" dataset = get_split('train', dataset_dir) dataset_provider = dataset_data_provider.DatasetDataProvider( dataset, num_readers=12, shuffle=False) with tf.Session() as sess: #dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" tf.train.start_queue_runners() [image_raw, label] = dataset_provider.get(['image', 'label']) #image_decoded = tf.image.decode_jpeg(image_raw) image_raw, label = sess.run([image_raw, label]) print(label) print(image_raw.shape) Image.fromarray(np.array(image_raw)).show()
def main(unused_argv): # if platform.system() == 'Linux': # dataset_dir = "/home/anurag/Desktop/tensorflow_data" # else: # dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" # dataset_dir = disk_storage_props.RAW_PATCHES_DIR_TO_GET_HEATMAPS # tf_record_dir = disk_storage_props.RAW_PATCHES_TF_RECORD_DIR_TO_GET_HEATMAPS wsi_name = "Normal_001" dataset_dir = disk_storage_props.WSI_RAW_PATCHES_PARENT_DIR_TO_GET_HEATMAPS.replace( "WSI_NAME", wsi_name) tf_record_dir = disk_storage_props.WSI_RAW_PATCHES_TF_RECORD_DIR_TO_GET_HEATMAPS.replace( "WSI_NAME", wsi_name) patches_count = read_patches_count_file(wsi_name=wsi_name) dataset = get_split('eval', tf_record_dir, patches_count) dataset_provider = dataset_data_provider.DatasetDataProvider( dataset, num_readers=12, shuffle=False) with tf.Session() as sess: #dataset_dir = "/Users/anuragverma/Desktop/tensorflow_data" tf.train.start_queue_runners() [image_raw, label, filename] = dataset_provider.get(['image', 'label', 'filename']) #image_decoded = tf.image.decode_jpeg(image_raw) image_raw, label, filename = sess.run([image_raw, label, filename]) print(label) print(image_raw.shape) print(filename) #Image.fromarray(np.array(image_raw)).show() #print(image_decoded.shape) #print(image_decoded) #print("\n\n\n\n",image_raw) print(dataset_provider.num_samples()) #images, labels = dataset_provider.get(['image', 'label']) for i in range(dataset_provider.num_samples()): [image_raw, label, filename] = dataset_provider.get(['image', 'label', 'filename']) image_raw, label, filename = sess.run([image_raw, label, filename]) if label == 0: Image.fromarray(np.array(image_raw)).show()
def loadBatch(dataset, batchSize=4, height=299, width=299): """ 批量加载数据 :param dataset: :param batchSize: :param height: :param width: :return: """ dataProvider = provider.DatasetDataProvider(dataset=dataset, common_queue_capacity=8, common_queue_min=1) imageRaw, label = dataProvider.get(["image", "label"]) imageRaw = tf.image.resize_images(images=imageRaw, size=[height, width]) imageRaw = tf.image.convert_image_dtype(image=imageRaw, dtype=tf.float32) imageRaw, labels = tf.train.batch(tensors=[imageRaw, label], batch_size=batchSize, num_threads=1, capacity=2 * batchSize) return imageRaw, labels
def testTFRecordDataset(self): dataset_dir = tempfile.mkdtemp(prefix=os.path.join(self.get_temp_dir(), 'tfrecord_dataset')) height = 300 width = 280 with self.cached_session(): test_dataset = _create_tfrecord_dataset(dataset_dir) provider = dataset_data_provider.DatasetDataProvider(test_dataset) key, image, label = provider.get(['record_key', 'image', 'label']) image = _resize_image(image, height, width) with session.Session('') as sess: with queues.QueueRunners(sess): key, image, label = sess.run([key, image, label]) split_key = key.decode('utf-8').split(':') self.assertEqual(2, len(split_key)) self.assertEqual(test_dataset.data_sources[0], split_key[0]) self.assertTrue(split_key[1].isdigit()) self.assertListEqual([height, width, 3], list(image.shape)) self.assertListEqual([1], list(label.shape))
def loadBatch(dataset, batchSize=32, height=217, width=217, isTraining=True): """ 从dataset中取出图片数据并进行重构,重新按批量生成数据集 :param dataset: :param batchSize: :param height: :param width: :param isTraining: :return: """ dataProvider = provider.DatasetDataProvider(dataset=dataset, common_queue_capacity=32, common_queue_min=1) imageRaw, label = dataProvider.get(["image", "label"]) imageRaw = tf.image.resize_images(images=imageRaw, size=[height, width]) imageRaw = tf.image.convert_image_dtype(image=imageRaw, dtype=tf.float32) imageRaw, labels = tf.train.batch(tensors=[imageRaw, label], batch_size=batchSize, num_threads=1, capacity=2 * batchSize) return imageRaw, labels
def showFlowers(): """ 展示数据集中的图片 :return: """ with tf.Graph().as_default(): dataset = flowers.get_split("train", flowersDataPath) dataProvider = provider.DatasetDataProvider(dataset=dataset, common_queue_capacity=32, common_queue_min=1) image, label = dataProvider.get(["image", "label"]) with tf.Session() as sess: with slim.queues.QueueRunners(sess): for i in range(5): npImage, npLabel = sess.run([image, label]) height, width, channel = npImage.shape className = name = dataset.labels_to_names[npLabel] plt.figure() plt.imshow(npImage) plt.title("%s,%d x %d" % (name, height, width)) plt.axis("off") plt.show()
def run_adaptive_training(): with tf.Graph().as_default(): with tf.Session() as sess: tf.logging.info('Setup') tf_global_step = tf.train.get_or_create_global_step() p_images = tf.placeholder(tf.float32, shape=(None, image_size[FLAGS.dataset_name], image_size[FLAGS.dataset_name], channels[FLAGS.dataset_name])) p_labels = tf.placeholder(tf.int64, shape=(None, num_classes[FLAGS.dataset_name])) p_emb_idx = tf.placeholder(tf.int32, shape=(FLAGS.batch_size / 2, )) p_assign_idx = tf.placeholder(tf.int32) # Data for eval. Currently hardcoded for cifar eval_data_provider = dataset_data_provider.DatasetDataProvider( cifar10.get_split('test', '.'), common_queue_capacity=2 * FLAGS.batch_size, common_queue_min=FLAGS.batch_size) e_image, e_label = eval_data_provider.get(['image', 'label']) e_image = tf.to_float(e_image) # TODO this is a hack eval_images, eval_labels = tf.train.batch( [e_image, e_label], batch_size=FLAGS.batch_size, num_threads=1, capacity=5 * FLAGS.batch_size, allow_smaller_final_batch=True) with tf.device('/cpu:0'): dataloader = adamb_data_loader.adamb_data_loader( FLAGS.dataset_name, decay=FLAGS.decay, loss_scaling=FLAGS.loss_scaling) if FLAGS.model == 'resnet': with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, end_points = resnet_v2.resnet_v2_50( p_images, num_classes[FLAGS.dataset_name], is_training=True, global_pool=True) embeddings = end_points[ 'global_pool'] # size (BATCH,1,1,2048) # this doesn't work tf1.4. using batch predictions = end_points['predictions'] predictions = tf.argmax(predictions, 1) if FLAGS.model == 'inception': with slim.arg_scope(inception.inception_v1_arg_scope()): print(num_classes) logits, end_points = inception.inception_v1( p_images, num_classes[FLAGS.dataset_name], is_training=True, global_pool=True) embeddings = end_points['global_pool'] predictions = tf.argmax(logits, 1) embeddings = tf.squeeze(embeddings, [1, 2]) tf.logging.debug("embeddings size: ", embeddings.shape) sample_losses = tf.losses.softmax_cross_entropy( logits=logits, onehot_labels=p_labels) # sample_losses = tf.losses.sparse_softmax_cross_entropy(logits=logits, # labels=p_labels, # loss_collection=None) # tf.losses.sparse_softmax_cross_entropy( # labels=p_labels, logits=logits, weights=1.0) # Try total loss with sample loss commented out. also something is # happening here that is making softmax super slow... # total_loss = tf.losses.get_total_loss() optimizer = _get_optimizer(FLAGS.opt) # train_op = optimizer.minimize(tf.reduce_mean(sample_losses), global_step=tf_global_step) # sample_losses) train_op = optimizer.minimize( sample_losses, global_step=tf_global_step) # sample_losses) # train_op = slim.learning.create_train_op(total_loss, optimizer) tf.logging.info('Model + training setup') embedding_list = tf.get_variable( 'EmbeddingList', shape=[num_train_samples[FLAGS.dataset_name], 2048], initializer=tf.random_normal_initializer(mean=0.3, stddev=0.6), trainable=False) b = tf.gather(embedding_list, p_emb_idx, axis=0) c = tf.matmul( b, embedding_list, transpose_b=True) # this transpose could be backwards squared_euclid = tf.transpose( tf.transpose( tf.reduce_sum(tf.square(embedding_list), axis=1) - 2 * c) + tf.reduce_sum(tf.square(b), axis=1) ) # TODO check this, last term may be incorrect if FLAGS.pot_func == 'sq_recip': recip_squared_euclid = tf.reciprocal( squared_euclid + FLAGS.recip_scale) # hyperparam fix infs potential = recip_squared_euclid else: neg_exp_euclid = tf.exp(-FLAGS.recip_scale * squared_euclid / 1000) potential = neg_exp_euclid m, n = potential.get_shape().as_list() class_starts = dataloader.class_starts def get_mask(class_starts, labels, batch_size): labels_mask = np.ones(shape=(batch_size, 50000)) # fix these hard codes static_range = 5000 # fix these hard codes # class_starts = np.asarray(class_starts) # Possibly relevant mins = class_starts[labels] mask_idxs = mins[..., None] + np.arange(static_range) labels_mask[np.expand_dims(np.arange(batch_size), 1), mask_idxs] = 0.0 return labels_mask labels_mask = tf.py_func(get_mask, [ class_starts, tf.argmax(p_labels, axis=1), tf.cast(FLAGS.batch_size / 2, tf.int32) ], tf.double) #tf.int32) # TODO last term may be wrong diverse_dist = tf.multiply(potential, tf.cast(labels_mask, tf.float32)) cumm_array = tf.cumsum(diverse_dist, axis=1) max_cumm_array = tf.reduce_max(cumm_array, axis=1) bin_min = tf.cumsum(max_cumm_array + 1, exclusive=True) cumm_array = tf.expand_dims(bin_min, 1) + cumm_array scaled_seed = bin_min + tf.multiply( tf.random_uniform([ tf.cast(FLAGS.batch_size / 2, tf.int32), ]), max_cumm_array) scaled_seed_idx = tf.py_func( searchsortedtf, [tf.reshape(cumm_array, [-1]), scaled_seed], tf.int64) pair_idx_tensor = tf.cast(scaled_seed_idx, tf.int32) - tf.range(m) * n # Embedding update emb_update_op = tf.scatter_nd_update( embedding_list, tf.expand_dims(p_assign_idx, 1), embeddings) # predictions = tf.squeeze(end_points['predictions'], [1, 2], name='SpatialSqueeze') # predictions = tf.argmax(predictions, 1) accuracy_op = tf.reduce_mean( tf.to_float(tf.equal(predictions, tf.argmax(p_labels, 1)))) tf.summary.scalar('Train_Accuracy', accuracy_op) tf.summary.scalar('Total_Loss', sample_losses) #total_loss) tf.summary.image('input', p_images) slim.summaries.add_histogram_summaries(slim.get_model_variables()) # slim.summaries.add_histogram_summaries() summary_writer = tf.summary.FileWriter(FLAGS.train_log_dir, sess.graph) merged_summary_op = tf.summary.merge_all() saver = tf.train.Saver() tf.logging.info('Savers') sess.run(tf.global_variables_initializer()) # Training loop for step in range(FLAGS.max_steps): # TODO Still need to modify this to do more than just half-batches # TODO should be split up into functions that return images/labels from idx and a separate function that finds those idxs, not one in the same if FLAGS.method == 'pairwise': images, _, labels, sample_idxs = dataloader.load_batch( batch_size=int(FLAGS.batch_size / 2), method=FLAGS.method) # print('images', images.shape, 'labels', labels.shape, 'sample_idxs', sample_idxs.shape) pair_idxs = sess.run(pair_idx_tensor, feed_dict={ p_emb_idx: sample_idxs, p_labels: labels }) if FLAGS.debug: np_diverse_dist = sess.run(diverse_dist, feed_dict={ p_emb_idx: sample_idxs, p_labels: labels }) print('np_diverse_dist: ', np_diverse_dist) image_pairs, label_pairs = dataloader.get_data_from_idx( pair_idxs) images = np.concatenate((images, image_pairs), axis=0) labels = np.concatenate((labels, label_pairs), axis=0) sample_idxs = np.append(sample_idxs, pair_idxs) _, losses, acc, batch_embeddings, _, summary = sess.run( [ train_op, sample_losses, accuracy_op, embeddings, emb_update_op, merged_summary_op ], feed_dict={ p_images: images, p_labels: labels, p_assign_idx: sample_idxs }) # _, losses, batch_embeddings, summary = sess.run([train_op, sample_losses, embeddings, merged_summary_op], # feed_dict={p_images: images, p_labels: labels, p_assign_idx: sample_idxs}) else: images, _, labels, sample_idxs = dataloader.load_batch( batch_size=FLAGS.batch_size, method=FLAGS.method) # print('images', images.shape, 'labels', labels.shape, 'sample_idxs', sample_idxs.shape) _, losses, acc, summary = sess.run([ train_op, sample_losses, accuracy_op, merged_summary_op ], feed_dict={ p_images: images, p_labels: labels }) tf.logging.debug('loss: ' + str(losses.mean())) if FLAGS.method == 'singleton': dataloader.update(FLAGS.method, sample_idxs, metrics={'losses': losses}) if FLAGS.method == 'pairwise': dataloader.update(FLAGS.method, sample_idxs, metrics={ 'losses': losses, 'batch_embeddings': batch_embeddings }) if ((step + 1) % FLAGS.save_summary_iters == 0 or (step + 1) == FLAGS.max_steps): tf.logging.info('Iteration %d complete', step) summary_writer.add_summary(summary, step) label_names = np.argmax(labels, 1) # print(type(label_names)) # print(cifar_classes[label_names]) print(acc) summary_writer.flush() tf.logging.info('loss: ' + str(losses.mean())) tf.logging.debug('Summary Saved') if ((step + 1) % FLAGS.save_model_iters == 0 or (step + 1) == FLAGS.max_steps): checkpoint_file = join(FLAGS.train_log_dir, 'model.ckpt') saver.save(sess, checkpoint_file, global_step=tf_global_step) tf.logging.debug('Model Saved')
def provide_cifarnet_data(dataset_name, split_name, batch_size, dataset_dir=None, num_epochs=None): """Provides batches of CIFAR images for cifarnet. Args: dataset_name: Eiether 'cifar10' or 'cifar100'. split_name: Either 'train' or 'test'. batch_size: The number of images in each batch. dataset_dir: The directory where the MNIST data can be found. num_epochs: The number of times each data source is read. If left as None, the data will be cycled through indefinitely. Returns: images: A `Tensor` of size [batch_size, 32, 32, 1] one_hot_labels: A `Tensor` of size [batch_size, NUM_CLASSES], where each row has a single element set to one and the rest set to zeros. num_samples: The number of total samples in the dataset. num_classes: The number of total classes in the dataset. Raises: ValueError: If `split_name` is not either 'train' or 'test'. """ dataset = get_dataset(dataset_name, split_name, dataset_dir=dataset_dir) # num_epochs = 1 if split_name == 'test' else None provider = dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * batch_size, common_queue_min=batch_size, shuffle=(split_name == 'train'), num_epochs=num_epochs) if dataset_name == 'cifar100': [image, label] = provider.get(['image', 'fine_label']) else: [image, label] = provider.get(['image', 'label']) image_size = 32 image = tf.to_float(image) # preprocess the images. if split_name == 'train': padding = image_size / 4 image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]]) image = tf.random_crop(image, [image_size, image_size, 3]) image = tf.image.random_flip_left_right(image) image = tf.image.per_image_standardization(image) else: image = tf.image.resize_image_with_crop_or_pad(image, image_size, image_size) image = tf.image.per_image_standardization(image) # Creates a QueueRunner for the pre-fetching operation. images, labels = tf.train.batch([image, label], batch_size=batch_size, num_threads=1, capacity=5 * batch_size, allow_smaller_final_batch=True) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) one_hot_labels = tf.squeeze(one_hot_labels, 1) return images, one_hot_labels, dataset.num_samples, dataset.num_classes
def provide_data(split_name, batch_size, split_type='comp', grayscale=True, preprocess_options=CENTER, dataset_dir=None, shuffle_data=None): """Provides batches of Grayscale shapes dataset. Args: split_name: Name of the split of data, one of the keys in grayscale_shapes_dataset._SPLITS_TO_SIZES batch_size: The number of images in each batch. split_type: 'comp' or 'iid', comp loads a dataset with non-intersecting label sets during train and test. IID just loads a dataset that is split IID. grayscale: Boolean, True indicates that the images are grayscale, False indicates images are RGB. preprocess_options: 'binarize' or 'center', options for preprocessing images. 'binarize' converts the image into a float tensor with 0 and 1 values, while 'center' does mean subtraction and scaling to make the input between -1 and 1. dataset_dir: The directory where the deepmind grayscale shapes data can be found. Returns: images: A `Tensor` of size [batch_size, 64, 64, 1] if grayscale is True. A `Tensor` of size [batch_size, 64, 64, 3] if grayscale is False. batch_label_list: A list of `Tensor` of size [batch_size], where each element has a value denoting the class label associated with the four kind of labels (shape, size, orientation, location). Thus, in this case, the list will be of length 4. latents: A `Tensor` of size [batch_size, _NUM_LATENTS], which is the true number of latent values underlying the model. These are available for grayscale_shapes_dataset and grayscale_shapes_iid_dataset. num_samples: The number of total samples in the dataset. num_classes_per_attribute: List, with number of labels for the classfication problem corresponding to each attribute. shuffle_data: Boolean, indicates whether to use a RandomShuffleQueue or a FIFO queue for loading data. Raises: ValueError: if the split_name is not either 'train' or 'val' or 'test' """ if not shuffle_data: if split_name == 'train': shuffle_data = True else: shuffle_data = False # The retrieval split is a different split of the data, from # IID or compositional. The retrieval split is an IID split, still though. if split_type == 'retrieval': raise NotImplementedError # If we are not requesting the retrieval split, then look for whether we want # IID or compositional split. elif split_type == 'comp': dataset, metadata = affine_mnist_dataset_comp.get_split( split_name, dataset_dir) elif split_type == 'iid': dataset, metadata = affine_mnist_dataset_iid.get_split( split_name, dataset_dir) else: raise ValueError('Invalid %s split type.', split_type) if split_name != 'retrieval': assert metadata['split_type'] == split_type, ( 'Called the wrong dataset' ' specification file.') provider = dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * batch_size, common_queue_min=batch_size, shuffle=shuffle_data) [image, labels, latents] = provider.get(['image', 'labels', 'latents']) image = tf.to_float(image) # Preprocess the images. if preprocess_options == CENTER: image = tf.subtract(image, 128.0) image = tf.div(image, 128.0) elif preprocess_options == BINARIZE: image = tf.div(image, 255.0) else: raise ValueError('Invalid argument for preprocess_options %s' % (preprocess_options)) # Creates a QueueRunner for the pre-fetching operation. images, batch_labels, batch_latents = tf.train.batch( [image, labels, latents], batch_size=batch_size, num_threads=1, capacity=5 * batch_size) if grayscale is True: images = tf.slice(images, [0, 0, 0, 0], [batch_size, 64, 64, 1], name='slice_image') batch_label_list = tf.unstack(batch_labels, axis=-1, name='unstack_labels') return images, batch_label_list, batch_latents, dataset.num_samples, metadata[ 'num_classes_per_attribute']
from datasets import flowers import tensorflow as tf import matplotlib.pyplot as plt #import global_variable import tensorflow.contrib.slim.python.slim as slim flowers_data_dir = "./data/flower_photos" with tf.Graph().as_default(): dataset = flowers.get_split("train", flowers_data_dir) import tensorflow.contrib.slim.python.slim.data.dataset_data_provider as provider data_provider = provider.DatasetDataProvider(dataset, common_queue_capacity=32, common_queue_min=1) image, label = data_provider.get(['image', 'label']) with tf.Session() as sess: with slim.queues.QueueRunners(sess): for i in range(5): np_image, np_label = sess.run([image, label]) height, width, _ = np_image.shape class_name = dataset.labels_to_names[np_label] plt.figure() plt.imshow(np_image) plt.title("%s, %d x %d" % (class_name, height, width)) plt.axis("off") plt.show()
def provide_resnet_data(dataset_name, split_name, batch_size, dataset_dir=None, num_epochs=None): """Provides batches of CIFAR images for resnet. Args: dataset_name: Eiether 'cifar10' or 'cifar100'. split_name: Either 'train' or 'test'. batch_size: The number of images in each batch. dataset_dir: The directory where the MNIST data can be found. num_epochs: The number of times each data source is read. If left as None, the data will be cycled through indefinitely. Returns: images: A `Tensor` of size [batch_size, 32, 32, 1] one_hot_labels: A `Tensor` of size [batch_size, NUM_CLASSES], where each row has a single element set to one and the rest set to zeros. num_samples: The number of total samples in the dataset. num_classes: The number of total classes in the dataset. Raises: ValueError: If `split_name` is not either 'train' or 'test'. """ dataset = _get_dataset(dataset_name, split_name, dataset_dir=dataset_dir) provider = dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=2 * batch_size, common_queue_min=batch_size, shuffle=(split_name == 'train'), num_epochs=num_epochs) [image, label] = provider.get(['image', 'label']) image = tf.to_float(image) image_size = 32 if split_name == 'train': image = tf.image.resize_image_with_crop_or_pad(image, image_size + 4, image_size + 4) image = tf.random_crop(image, [image_size, image_size, 3]) image = tf.image.random_flip_left_right(image) image /= 255 # pylint: disable=unnecessary-lambda image = _apply_with_random_selector( image, lambda x, ordering: distort_color(x, ordering), num_cases=2) image = 2 * (image - 0.5) else: image = tf.image.resize_image_with_crop_or_pad(image, image_size, image_size) image = (image - 127.5) / 127.5 # Creates a QueueRunner for the pre-fetching operation. images, labels = tf.train.batch([image, label], batch_size=batch_size, num_threads=1, capacity=5 * batch_size, allow_smaller_final_batch=True) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) one_hot_labels = tf.squeeze(one_hot_labels, 1) return images, one_hot_labels, dataset.num_samples, dataset.num_classes