def read_images_from_disk(input_queue): """Consumes a single filename and label as a ' '-delimited string. Args: filenames for data and tensor Returns: Two tensors: the decoded image, and the string label. """ img_filename = input_queue[0] label_filename = input_queue[1] img_data = tf.read_file(img_filename, name='read_image') img_tensor = tf.image.decode_png(img_data, channels=1) img_tensor = tf.reshape(img_tensor, [IMG_HEIGHT, IMG_WIDTH, 1]) # transform to a float image img_tensor = tf.cast(img_tensor, tf.float32) # img_tensor = tf.zeros([IMG_HEIGHT, IMG_WIDTH, 1], dtype=tf.float32, name=None) label_data = tf.read_file(label_filename, name='read_label') label_tensor = tf.image.decode_png(label_data, channels=1) label_tensor = tf.reshape(label_tensor, [IMG_HEIGHT, IMG_WIDTH, 1]) label_tensor = tf.cast(label_tensor, tf.float32) # label_tensor = tf.zeros([IMG_HEIGHT, IMG_WIDTH, 1], dtype=tf.float32, name=None) return img_tensor, label_tensor
def _produce_one_sample(self): dirname = os.path.dirname(self.path) if not check_dir(dirname): raise ValueError("Invalid data path.") with open(self.path, 'r') as fid: flist = [l.strip() for l in fid.xreadlines()] if self.shuffle: random.shuffle(flist) input_files = [os.path.join(dirname, 'input', f) for f in flist] output_files = [os.path.join(dirname, 'output', f) for f in flist] self.nsamples = len(input_files) input_queue, output_queue = tf.train.slice_input_producer( [input_files, output_files], shuffle=self.shuffle, seed=0123, num_epochs=self.num_epochs) if '16-bit' in magic.from_file(input_files[0]): input_dtype = tf.uint16 input_wl = 65535.0 else: input_wl = 255.0 input_dtype = tf.uint8 if '16-bit' in magic.from_file(output_files[0]): output_dtype = tf.uint16 output_wl = 65535.0 else: output_wl = 255.0 output_dtype = tf.uint8 input_file = tf.read_file(input_queue) output_file = tf.read_file(output_queue) if os.path.splitext(input_files[0])[-1] == '.jpg': im_input = tf.image.decode_jpeg(input_file, channels=3) else: im_input = tf.image.decode_png(input_file, dtype=input_dtype, channels=3) if os.path.splitext(output_files[0])[-1] == '.jpg': im_output = tf.image.decode_jpeg(output_file, channels=3) else: im_output = tf.image.decode_png(output_file, dtype=output_dtype, channels=3) # normalize input/output sample = {} with tf.name_scope('normalize_images'): im_input = tf.to_float(im_input)/input_wl im_output = tf.to_float(im_output)/output_wl inout = tf.concat([im_input, im_output], 2) fullres, inout = self._augment_data(inout, 6) sample['lowres_input'] = inout[:, :, :3] sample['lowres_output'] = inout[:, :, 3:] sample['image_input'] = fullres[:, :, :3] sample['image_output'] = fullres[:, :, 3:] return sample
def _read_images(paths): with tf.name_scope("read_images"): path1, path2 = tf.decode_csv(paths, [[""], [""]], field_delim=" ") file_content1 = tf.read_file(path1) file_content2 = tf.read_file(path2) image1 = tf.cast(tf.image.decode_png(file_content1, channels=3, dtype=tf.uint8), tf.float32) image2 = tf.cast(tf.image.decode_png(file_content2, channels=3, dtype=tf.uint8), tf.float32) image1.set_shape(IMAGE_SHAPE) image2.set_shape(IMAGE_SHAPE) return image1, image2
def read_and_decode(self): image_name = tf.read_file(self.filename_queue[0]) image = tf.image.decode_jpeg(image_name, channels = 3) image = tf.image.resize_images(image, 320, 480) image /= 255. label_name = tf.read_file(self.filename_queue[1]) label = tf.image.decode_png(label_name, channels = 1) label = tf.image.resize_images(label, 320, 480) label = tf.to_int64(label > 0) return image, label
def _produce_one_sample(self): # TODO: check dir structure with open(os.path.join(self.path, 'filelist.txt'), 'r') as fid: flist = [l.strip() for l in fid.xreadlines()] with open(os.path.join(self.path, 'targets.txt'), 'r') as fid: tlist = [l.strip() for l in fid.xreadlines()] input_files = [] model_files = [] output_files = [] for f in flist: for t in tlist: input_files.append(os.path.join(self.path, 'input', f)) model_files.append(os.path.join(self.path, 'input', t+'.png')) output_files.append(os.path.join(self.path, 'output', t, f)) self.nsamples = len(input_files) input_queue, model_queue, output_queue = tf.train.slice_input_producer( [input_files, model_files, output_files], capacity=1000, shuffle=self.shuffle, num_epochs=self.num_epochs, seed=1234) input_wl = 255.0 input_dtype = tf.uint8 input_file = tf.read_file(input_queue) model_file = tf.read_file(model_queue) output_file = tf.read_file(output_queue) im_input = tf.image.decode_png(input_file, dtype=input_dtype, channels=3) im_model = tf.image.decode_png(model_file, dtype=input_dtype, channels=3) im_output = tf.image.decode_png(output_file, dtype=input_dtype, channels=3) # normalize input/output with tf.name_scope('normalize_images'): im_input = tf.to_float(im_input)/input_wl im_model = tf.to_float(im_model)/input_wl im_output = tf.to_float(im_output)/input_wl inout = tf.concat([im_input, im_output], 2) fullres, inout = self._augment_data(inout, 6) mdl = tf.image.resize_images(im_model, tf.shape(inout)[:2]) sample = {} sample['lowres_input'] = tf.concat([inout[:, :, :3], mdl], 2) sample['lowres_output'] = inout[:, :, 3:] fullres_mdl = tf.image.resize_images(im_model, tf.shape(fullres)[:2]) sample['image_input'] = tf.concat([fullres[:, :, :3], fullres_mdl], 2) sample['image_output'] = fullres[:, :, 3:] return sample
def CamVid_reader(filename_queue): image_filename = filename_queue[0] label_filename = filename_queue[1] imageValue = tf.read_file(image_filename) labelValue = tf.read_file(label_filename) image_bytes = tf.image.decode_png(imageValue) label_bytes = tf.image.decode_png(labelValue) image = tf.reshape(image_bytes, (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_DEPTH)) label = tf.reshape(label_bytes, (IMAGE_HEIGHT, IMAGE_WIDTH, 1)) return image, label
def build_prepro_graph(inception_path): global input_layer, output_layer with open(inception_path, 'rb') as f: fileContent = f.read() graph_def = tf.GraphDef() graph_def.ParseFromString(fileContent) tf.import_graph_def(graph_def) graph = tf.get_default_graph() input_layer = graph.get_tensor_by_name("import/InputImage:0") output_layer = graph.get_tensor_by_name( "import/InceptionV4/Logits/AvgPool_1a/AvgPool:0") input_file = tf.placeholder(dtype=tf.string, name="InputFile") image_file = tf.read_file(input_file) jpg = tf.image.decode_jpeg(image_file, channels=3) png = tf.image.decode_png(image_file, channels=3) output_jpg = tf.image.resize_images(jpg, [299, 299]) / 255.0 output_jpg = tf.reshape( output_jpg, [ 1, 299, 299, 3], name="Preprocessed_JPG") output_png = tf.image.resize_images(png, [299, 299]) / 255.0 output_png = tf.reshape( output_png, [ 1, 299, 299, 3], name="Preprocessed_PNG") return input_file, output_jpg, output_png
def convertDataset(image_dir): num_labels = len(LABELS_DICT) label = np.eye(num_labels) # Convert labels to one-hot-vector i = 0 session = tf.Session() init = tf.initialize_all_variables() session.run(init) log.info("Start processing images (Dataset.py) ") start = timer() for dirName in os.listdir(image_dir): label_i = label[i] print("ONE_HOT_ROW = ", label_i) i += 1 # log.info("Execution time of convLabels function = %.4f sec" % (end1-start1)) path = os.path.join(image_dir, dirName) for img in os.listdir(path): img_path = os.path.join(path, img) if os.path.isfile(img_path) and (img.endswith('jpeg') or (img.endswith('jpg'))): img_bytes = tf.read_file(img_path) img_u8 = tf.image.decode_jpeg(img_bytes, channels=3) img_u8_eval = session.run(img_u8) image = tf.image.convert_image_dtype(img_u8_eval, tf.float32) img_padded_or_cropped = tf.image.resize_image_with_crop_or_pad(image, IMG_SIZE, IMG_SIZE) img_padded_or_cropped = tf.reshape(img_padded_or_cropped, shape=[IMG_SIZE * IMG_SIZE, 3]) yield img_padded_or_cropped.eval(session=session), label_i end = timer() log.info("End processing images (Dataset.py) - Time = %.2f sec" % (end-start))
def _parse_resize_inception_function(filename): image_string = tf.read_file(filename) image_decoded = tf.image.decode_png(image_string,channels=3) # Cannot use decode_image but decode_png decodes both jpeg as well as png # https://github.com/tensorflow/tensorflow/issues/8551 image_scaled = tf.image.resize_images(image_decoded, [299, 299]) return image_scaled, filename
def read_one_image(fname, **kwargs): """Reads one image given a filepath Parameters ----------- fname : str path to a JPEG file img_shape : tuple (kwarg) shape of the eventual image. Default is (224, 224, 3) is_training : bool (kwarg) boolean to tell the loader function if the graph is in training mode or testing. Default is True Returns ------- preprocessed image """ img_shape = kwargs.pop("image_shape", (224, 224, 3)) is_training = kwargs.pop("is_training", False) # read the image file content = tf.read_file(fname) # decode buffer as jpeg img_raw = tf.image.decode_jpeg(content, channels=img_shape[-1]) return preprocess_image(img_raw, img_shape[0], img_shape[1], is_training=is_training)
def read_frame(self, t, format, crop, resize): value = tf.read_file(t) if format == 'jpg': img = tf.image.decode_jpeg(value, channels=channels) elif format == 'png': img = tf.image.decode_png(value, channels=channels) else: print("[loader] Failed to load format", format) img = tf.cast(img, tf.float32) # Image processing for evaluation. # Crop the central [height, width] of the image. if crop: resized_image = hypergan.inputs.resize_image_patch.resize_image_with_crop_or_pad(img, height, width, dynamic_shape=True) elif resize: resized_image = tf.image.resize_images(img, [height, width], 1) else: resized_image = img tf.Tensor.set_shape(resized_image, [height,width,channels]) # This moves the image to a range of -1 to 1. float_image = resized_image / 127.5 - 1. return float_image
def distorted_inputs(): data = load_data(FLAGS.data_dir) filenames = [ d['filename'] for d in data ] label_indexes = [ d['label_index'] for d in data ] filename, label_index = tf.train.slice_input_producer([filenames, label_indexes], shuffle=True) num_preprocess_threads = 4 images_and_labels = [] for thread_id in range(num_preprocess_threads): image_buffer = tf.read_file(filename) bbox = [] train = True image = image_preprocessing(image_buffer, bbox, train, thread_id) images_and_labels.append([image, label_index]) images, label_index_batch = tf.train.batch_join( images_and_labels, batch_size=FLAGS.batch_size, capacity=2 * num_preprocess_threads * FLAGS.batch_size) height = FLAGS.input_size width = FLAGS.input_size depth = 3 images = tf.cast(images, tf.float32) images = tf.reshape(images, shape=[FLAGS.batch_size, height, width, depth]) return images, tf.reshape(label_index_batch, [FLAGS.batch_size])
def blend_images(data_folder1, data_folder2, out_folder, alpha=.5): filename_queue = tf.placeholder(dtype=tf.string) label = tf.placeholder(dtype=tf.int32) tensor_image = tf.read_file(filename_queue) image = tf.image.decode_jpeg(tensor_image, channels=3) multiplier = tf.div(tf.constant(224, tf.float32), tf.cast(tf.maximum(tf.shape(image)[0], tf.shape(image)[1]), tf.float32)) x = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[0], tf.float32), multiplier)), tf.int32) y = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[1], tf.float32), multiplier)), tf.int32) image = tf.image.resize_images(image, [x, y]) image = tf.image.rot90(image, k=label) image = tf.image.resize_image_with_crop_or_pad(image, 224, 224) sess = tf.Session() sess.run(tf.local_variables_initializer()) for root, folders, files in os.walk(data_folder1): for each in files: if each.find('.jpg') >= 0: img1 = Image.open(os.path.join(root, each)) img2_path = os.path.join(root.replace(data_folder1, data_folder2), each.split("-")[-1]) rotation = int(each.split("-")[1]) img2 = sess.run(image, feed_dict={filename_queue: img2_path, label: rotation}) imsave(os.path.join(os.getcwd(), "temp", "temp.jpg"), img2) img2 = Image.open(os.path.join(os.getcwd(), "temp", "temp.jpg")) out_image = Image.blend(img1, img2, alpha) outfile = os.path.join(root.replace(data_folder1, out_folder), each) if not os.path.exists(os.path.split(outfile)[0]): os.makedirs(os.path.split(outfile)[0]) out_image.save(outfile) else: print(each) sess.close()
def read_jpeg(filename): value = tf.read_file(filename) decoded_image = tf.image.decode_jpeg(value, channels=FLAGS.depth) resized_image = tf.image.resize_images(decoded_image, FLAGS.raw_height, FLAGS.raw_width) resized_image = tf.cast(resized_image, tf.uint8) return resized_image
def decode(self, filename, distort_data = False, whiten_data = False): """ distort_data and whiten_data are not used """ bin_file = tf.read_file(filename) bin_tensor = tf.decode_raw(bin_file, self.dtype) bin_tensor = tf.to_float(bin_tensor) bin_tensor = tf.reshape(bin_tensor, self.shape) return bin_tensor
def preprocess_image(file_name, output_height=224, output_width=224, num_channels=3): """Run standard ImageNet preprocessing on the passed image file. Args: file_name: string, path to file containing a JPEG image output_height: int, final height of image output_width: int, final width of image num_channels: int, depth of input image Returns: Float array representing processed image with shape [output_height, output_width, num_channels] Raises: ValueError: if image is not a JPEG. """ if imghdr.what(file_name) != "jpeg": raise ValueError("At this time, only JPEG images are supported. " "Please try another image.") image_buffer = tf.read_file(file_name) normalized = imagenet_preprocessing.preprocess_image( image_buffer=image_buffer, bbox=None, output_height=output_height, output_width=output_width, num_channels=num_channels, is_training=False) with tf.Session(config=get_gpu_config()) as sess: result = sess.run([normalized]) return result[0]
def read(filename_queue): value = filename_queue.dequeue() fpath, label = tf.decode_csv( value, record_defaults=[[''], ['']], field_delim=' ') image_buffer = tf.read_file(fpath) return [image_buffer, label]
def get_input(input_file, batch_size, im_size=224): input = DATA_DIR + 'SegNet/SiftFlow/' + input_file filenames = [] with open(input, 'r') as f: for line in f: filenames.append('{}/{}'.format( DATA_DIR, line.strip())) # filenames.append('{}/{}.jpg {}'.format( # DATA_DIR, line.strip(), # line.strip())) filename_queue = tf.train.string_input_producer(filenames) filename, label_dir = tf.decode_csv(filename_queue.dequeue(), [[""], [""]], " ") label = label_dir; file_contents = tf.read_file(filename) im = tf.image.decode_jpeg(file_contents) im = tf.image.resize_images(im, im_size, im_size, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) im = tf.reshape(im, [im_size, im_size, 3]) im = tf.to_float(im) im_mean = tf.constant([122.67892, 116.66877, 104.00699], dtype=tf.float32) im = tf.sub(im, im_mean) # im = tf.image.per_image_whitening(im) # im = tf.image.per_image_whitening(im) min_queue_examples = int(10000 * 0.4) example_batch, lbl_batch = tf.train.batch([im, label], num_threads=1, batch_size=batch_size, capacity=min_queue_examples + 3 * batch_size) return example_batch, lbl_batch
def read_image(filename, label): image_string = tf.read_file(filename) image_decoded = tf.image.decode_png(image_string, channels=3) image_resized = tf.image.resize_images(image_decoded, [28, 28]) image_resized = tf.cast(image_resized, tf.float32) image_resized = image_resized / 255.0 return image_resized, label
def _read_image_and_label(self, image_file, label): image = tf.read_file(image_file) image = tf.image.decode_jpeg(image) image = tf.image.resize_images(image, [224, 224]) image = tf.reshape(image, (1, 224, 224, 3)) # for resnet50 return image, label
def main(_): path_to_image_file = FLAGS.image path_to_restore_checkpoint_file = FLAGS.restore_checkpoint image = tf.image.decode_jpeg(tf.read_file(path_to_image_file), channels=3) image = tf.reshape(image, [64, 64, 3]) image = tf.image.convert_image_dtype(image, dtype=tf.float32) image = tf.multiply(tf.subtract(image, 0.5), 2) image = tf.image.resize_images(image, [54, 54]) images = tf.reshape(image, [1, 54, 54, 3]) length_logits, digits_logits = Model.inference(images, drop_rate=0.0) length_predictions = tf.argmax(length_logits, axis=1) digits_predictions = tf.argmax(digits_logits, axis=2) digits_predictions_string = tf.reduce_join(tf.as_string(digits_predictions), axis=1) with tf.Session() as sess: restorer = tf.train.Saver() restorer.restore(sess, path_to_restore_checkpoint_file) length_predictions_val, digits_predictions_string_val = sess.run([length_predictions, digits_predictions_string]) length_prediction_val = length_predictions_val[0] digits_prediction_string_val = digits_predictions_string_val[0] print 'length: %d' % length_prediction_val print 'digits: %s' % digits_prediction_string_val
def input_images(self): num_images = (self._sqlen + self._args.lookback_length) * self._bsize images = tf.map_fn(lambda x: tf.image.decode_jpeg(tf.read_file(x)), tf.reshape(self._imfiles, shape=[num_images]), dtype=tf.uint8) images.set_shape([None, ds.HEIGHT, ds.WIDTH, ds.CHANNELS]) return images
def _read_from_disk_temporal( fpath, nframes, num_samples=25, optical_flow_frames=10, start_frame=0, file_prefix='', file_zero_padding=4, file_index=1, dataset_dir='', step=None): duration = nframes if step is None: if num_samples == 1: step = tf.random_uniform([1], 0, nframes-optical_flow_frames-1, dtype='int32')[0] else: step = tf.cast((duration-tf.constant(optical_flow_frames)) / (tf.constant(num_samples)), 'int32') allimgs = [] with tf.variable_scope('read_flow_video'): for i in range(num_samples): if num_samples == 1: i = 1 # so that the random step value can be used with tf.variable_scope('read_flow_image'): flow_img = [] for j in range(optical_flow_frames): with tf.variable_scope('read_flow_channels'): for dr in ['x', 'y']: prefix = file_prefix + '_' if file_prefix else '' impath = tf.string_join([ tf.constant(dataset_dir + '/'), fpath, tf.constant('/'), prefix, '%s_' % dr, tf.as_string(start_frame + i * step + file_index + j, width=file_zero_padding, fill='0'), tf.constant('.jpg')]) img_str = tf.read_file(impath) flow_img.append(img_str) allimgs.append(flow_img) return allimgs
def get_batch(image,label,image_W,image_H,batch_size,capacity): ''' Args: image: list type label: list type image_W: image width image_H: image height batch_size: batch size capacity: the maximum elements in queue Returns: image_batch: 4D tensor [batch_size, width, height, 3], dtype=tf.float32 label_batch: 1D tensor [batch_size], dtype=tf.int32 ''' image = tf.cast(image,tf.string) label = tf.cast(label,tf.int32) input_queue = tf.train.slice_input_producer([image,label]) label = input_queue[1] image_contents = tf.read_file(input_queue[0]) image = tf.image.decode_jpeg(image_contents,channels=3) image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H) image = tf.image.per_image_standardization(image) image_batch,label_batch = tf.train.batch([image,label], batch_size = batch_size, num_threads = 64, capacity = capacity) label_batch = tf.reshape(label_batch,[batch_size]) image_batch = tf.cast(image_batch,tf.float32) return image_batch,label_batch
def test_inputs(self, csv, batch_size): print("input csv file path: %s, batch size: %d" % (csv, batch_size)) filename_queue = tf.train.string_input_producer([csv], shuffle=False) reader = tf.TextLineReader() _, serialized_example = reader.read(filename_queue) filename, label = tf.decode_csv(serialized_example, [["path"], [0]]) label = tf.cast(label, tf.int32) jpg = tf.read_file(filename) image = tf.image.decode_jpeg(jpg, channels=3) image = tf.cast(image, tf.float32) print "original image shape:" print image.get_shape() # resize to distort dist = tf.image.resize_images(image, FLAGS.scale_h, FLAGS.scale_w) # random crop dist = tf.image.resize_image_with_crop_or_pad(dist, FLAGS.input_h, FLAGS.input_w) min_fraction_of_examples_in_queue = 0.4 min_queue_examples = int(FLAGS.num_examples_per_epoch_for_train * min_fraction_of_examples_in_queue) print ( 'filling queue with %d train images before starting to train. This will take a few minutes.' % min_queue_examples) return self._generate_image_and_label_batch(dist, label, min_queue_examples, batch_size, shuffle=False)
def _read_from_disk_spatial(fpath, nframes, num_samples=25, start_frame=0, file_prefix='', file_zero_padding=4, file_index=1, dataset_dir='', step=None): duration = nframes if step is None: if num_samples == 1: step = tf.random_uniform([1], 0, nframes, dtype='int32')[0] else: step = tf.cast((duration-tf.constant(1)) / (tf.constant(num_samples-1)), 'int32') allimgs = [] with tf.variable_scope('read_rgb_video'): for i in range(num_samples): if num_samples == 1: i = 1 # so that the random step value can be used with tf.variable_scope('read_rgb_image'): prefix = file_prefix + '_' if file_prefix else '' impath = tf.string_join([ tf.constant(dataset_dir + '/'), fpath, tf.constant('/'), prefix, tf.as_string(start_frame + i * step + file_index, width=file_zero_padding, fill='0'), tf.constant('.jpg')]) img_str = tf.read_file(impath) allimgs.append(img_str) return allimgs
def image_processing(self, filename): x = tf.read_file(filename) x_decode = tf.image.decode_jpeg(x, channels=self.channels) img = tf.image.resize_images(x_decode, [self.load_size, self.load_size]) img = tf.cast(img, tf.float32) / 127.5 - 1 return img
def read_tensor_from_image_file(file_name): input_name = "file_reader" output_name = "normalized" width = input_size height = input_size num_channels = 3 file_reader = tf.read_file(file_name, input_name) if file_name.endswith(".png"): image_reader = tf.image.decode_png(file_reader, channels = 3, name='png_reader') elif file_name.endswith(".gif"): image_reader = tf.squeeze(tf.image.decode_gif(file_reader, name='gif_reader')) elif file_name.endswith(".bmp"): image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader') else: image_reader = tf.image.decode_jpeg(file_reader, channels = 3, name='jpeg_reader') float_caster = tf.cast(image_reader, tf.float32) dims_expander = tf.expand_dims(float_caster, 0); # resized = tf.image.resize_bilinear(dims_expander, [input_size, input_size]) normalized = tf.divide(tf.subtract(dims_expander, [input_mean]), [input_std]) patches = tf.extract_image_patches(normalized, ksizes=[1, patch_height, patch_width, 1], strides=[1, patch_height/4, patch_width/4, 1], rates=[1,1,1,1], padding="VALID") patches_shape = tf.shape(patches) patches = tf.reshape(patches, [-1, patch_height, patch_width, num_channels]) patches = tf.image.resize_images(patches, [height, width]) patches = tf.reshape(patches, [-1, height, width, num_channels]) sess = tf.Session() return sess.run([patches, patches_shape])
def read_tensor_from_image_file(self, file_name, input_height=299, input_width=299, input_mean=0, input_std=255): input_name = "file_reader" output_name = "normalized" file_reader = tf.read_file(file_name, input_name) if file_name.endswith(".png"): image_reader = tf.image.decode_png(file_reader, channels=3, name='png_reader') elif file_name.endswith(".gif"): image_reader = tf.squeeze(tf.image.decode_gif(file_reader, name='gif_reader')) elif file_name.endswith(".bmp"): image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader') else: image_reader = tf.image.decode_jpeg(file_reader, channels=3, name='jpeg_reader') float_caster = tf.cast(image_reader, tf.float32) dims_expander = tf.expand_dims(float_caster, 0); resized = tf.image.resize_bilinear(dims_expander, [self.input_height, self.input_width]) normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std]) sess = tf.Session() result = sess.run(normalized) sess.close() return result
def CamVid_reader_seq(filename_queue, seq_length): image_seq_filenames = tf.split(axis=0, num_or_size_splits=seq_length, value=filename_queue[0]) label_seq_filenames = tf.split(axis=0, num_or_size_splits=seq_length, value=filename_queue[1]) image_seq = [] label_seq = [] for im ,la in zip(image_seq_filenames, label_seq_filenames): imageValue = tf.read_file(tf.squeeze(im)) labelValue = tf.read_file(tf.squeeze(la)) image_bytes = tf.image.decode_png(imageValue) label_bytes = tf.image.decode_png(labelValue) image = tf.cast(tf.reshape(image_bytes, (IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_DEPTH)), tf.float32) label = tf.cast(tf.reshape(label_bytes, (IMAGE_HEIGHT, IMAGE_WIDTH, 1)), tf.int64) image_seq.append(image) label_seq.append(label) return image_seq, label_seq
def _parse_test_img(img_path): with tf.device('/cpu:0'): img_buffer = tf.read_file(img_path) image_decoded = tf.image.decode_jpeg(img_buffer) return image_decoded
def read_img(path): return tf.image.decode_image(tf.read_file(path))
def load_tensor(file_name): sict=tf.read_file(file_name) # serialised ict=tf.parse_tensor(sict,numpy.float32) return ict
def input_map_fn(img_path, label): # do some process to label one_hot = tf.one_hot(label, num_classes) img_f = tf.read_file(img_path) img_decodes = tf.image.decode_image(img_f, channels=3) return img_decodes, one_hot
import tensorflow as tf tf.enable_eager_execution() d = tf.read_file(r"D:\download\Downloads\facades\train\1.jpg") file = tf.data.Dataset.list_files( "D:/download/Downloads/facades/train/*.jpg").make_one_shot_iterator() print(file.get_next()) print(file.get_next()) #tf.Tensor(b'D:\\download\\Downloads\\facades\\train\\389.jpg', shape=(), dtype=string) #tf.Tensor(b'D:\\download\\Downloads\\facades\\train\\315.jpg', shape=(), dtype=string) train_dataset = tf.data.Dataset.list_files(PATH + 'train/*.jpg') train_dataset = train_dataset.shuffle(BUFFER_SIZE) train_dataset = train_dataset.map(lambda x: load_image(x, True)) #train_dataset是一个可以迭代的东西,map将train_dataset中的每一个元素应用于一个函数 train_dataset = train_dataset.batch(1)
def main(args): network = importlib.import_module(args.model_def) subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir) if not os.path.isdir(log_dir): os.makedirs(log_dir) model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir) if not os.path.isdir(model_dir): os.makedirs(model_dir) np.random.seed(seed=args.seed) random.seed(args.seed) train_set = facenet.get_dataset(args.data_dir) if args.filter_filename: train_set = filter_dataset(train_set, os.path.expanduser(args.filter_filename), args.filter_percentile, args.filter_min_nrof_images_per_class) nrof_classes = len(train_set) print('Model directory: %s' % model_dir) print('Log directory: %s' % log_dir) pretrained_model = None if args.pretrained_model: pretrained_model = os.path.expanduser(args.pretrained_model) print('Pre-trained model: %s' % pretrained_model) if args.lfw_dir: print('LFW directory: %s' % args.lfw_dir) pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs)) lfw_paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) with tf.Graph().as_default(): tf.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) image_list, label_list = facenet.get_image_paths_and_labels(train_set) assert len(image_list) > 0, 'The dataset should not be empty' labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many( args.batch_size * args.epoch_size, 'index_dequeue') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths') labels_placeholder = tf.placeholder(tf.int64, shape=(None, 1), name='labels') input_queue = data_flow_ops.FIFOQueue(capacity=256000, dtypes=[tf.string, tf.int64], shapes=[(1, ), (1, )], shared_name=None, name=None) enqueue_op = input_queue.enqueue_many( [image_paths_placeholder, labels_placeholder], name='enqueue_op') nrof_preprocess_threads = 4 images_and_labels = [] for _ in range(nrof_preprocess_threads): filenames, label = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): file_contents = tf.read_file(filename) image = tf.cast( tf.image.decode_image(file_contents, channels=3), tf.float32) # if args.random_crop: # image = tf.random_crop(image, [args.image_size, args.image_size, 3]) # #image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) # else: # image = tf.image.resize_image_with_crop_or_pad(image, args.image_size, args.image_size) if args.random_flip: image = tf.image.random_flip_left_right(image) #image = tf.image.random_brightness(image,max_delta=30) #image = tf.image.random_contrast(image,lower=0.8,upper=1.2) #image = tf.image.random_saturation(image,lower=0.8,upper=1.2) image.set_shape((112, 96, 3)) images.append(tf.subtract(image, 127.5) * 0.0078125) images_and_labels.append([images, label]) image_batch, label_batch = tf.train.batch_join( images_and_labels, batch_size=batch_size_placeholder, shapes=[(112, 96, 3), ()], enqueue_many=True, capacity=4 * nrof_preprocess_threads * args.batch_size, allow_smaller_final_batch=True) image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label_batch') print('Total number of classes: %d' % nrof_classes) print('Total number of examples: %d' % len(image_list)) print('Building training graph') # Build the inference graph prelogits, _ = network.inference( image_batch, args.keep_probability, phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') AM_logits = AM_logits_compute(embeddings, label_batch, args, nrof_classes) #AM_logits = Arc_logits(embeddings, label_batch, args, nrof_classes) learning_rate = tf.train.exponential_decay( learning_rate_placeholder, global_step, args.learning_rate_decay_epochs * args.epoch_size, args.learning_rate_decay_factor, staircase=True) tf.summary.scalar('learning_rate', learning_rate) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=AM_logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') #print('test',tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) for weights in slim.get_variables_by_name('kernel'): kernel_regularization = tf.contrib.layers.l2_regularizer( args.weight_decay)(weights) print(weights) tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, kernel_regularization) regularization_losses = tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES) if args.weight_decay == 0: total_loss = tf.add_n([cross_entropy_mean], name='total_loss') else: total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') tf.add_to_collection('losses', total_loss) #define two saver in case under 'finetuning on different dataset' situation saver_load = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) saver_save = tf.train.Saver(tf.trainable_variables(), max_to_keep=1) #train_op = facenet.train(total_loss, global_step, args.optimizer, # learning_rate, args.moving_average_decay, tf.trainable_variables(), args.log_histograms) #train_op = tf.train.AdamOptimizer(learning_rate).minimize(total_loss,global_step = global_step,var_list=tf.trainable_variables()) train_op = tf.train.MomentumOptimizer( learning_rate, momentum=0.9).minimize(total_loss, global_step=global_step, var_list=tf.trainable_variables()) summary_op = tf.summary.merge_all() # Start running operations on the Graph. gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=args.gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if pretrained_model: print('Restoring pretrained model: %s' % pretrained_model) saver_load.restore(sess, pretrained_model) print('Running training') epoch = 0 best_accuracy = 0.0 while epoch < args.max_nrof_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file) print('validation running...') if args.lfw_dir: #best_accuracy = evaluate_double(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, # label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer,best_accuracy, saver_save,model_dir,subdir,image_batch,args) best_accuracy = evaluate( sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer, best_accuracy, saver_save, model_dir, subdir) return model_dir
def _read_images_from_disk(input_queue): label = input_queue[1] file_contents = tf.read_file(input_queue[0]) example = tf.image.decode_jpeg(file_contents, channels=3) return example, label
def _read_image(path): image = tf.read_file(path) image = tf.image.decode_png(image, channels=3) return image
def model_fn(features, labels, mode, params): # 加载词到id的映射 with tf.name_scope('vocab'): vocab_table = tf.contrib.lookup.index_table_from_tensor( mapping=tf.convert_to_tensor(params['vocabs']), num_oov_buckets=0, default_value=params['vocab_size'] - 1) # 单词未定义时,默认指向词向量表的最后一个单词下标 # 定义隐藏层, 词汇扩展1个用来存储未知单词 with tf.name_scope('hidden'): embeddings = tf.get_variable( 'embeddings', shape=[params['vocab_size'], params['embedding_size']], initializer=tf.random_uniform_initializer(minval=-1.0, maxval=1.0)) # 预测相似词 if mode == tf.estimator.ModeKeys.PREDICT: # 将相似id转为词 sparse_index_tensor = tf.string_split( [tf.read_file(params['vocab_file'])], delimiter='\n') index_tensor = tf.squeeze( tf.sparse_to_dense(sparse_index_tensor.indices, [1, params['vocab_size']], sparse_index_tensor.values, default_value='unknown')) # L2正则化,泛化,防止过拟合 normalized_embeddings = tf.nn.l2_normalize(embeddings, axis=1) discret_features = vocab_table.lookup(features) valid_embeddings = tf.nn.embedding_lookup(normalized_embeddings, tf.squeeze(discret_features)) # 用向量内积表示余弦值: 内积越大,夹角越小,余弦值越大,向量越相似 similarity = tf.matmul(valid_embeddings, normalized_embeddings, transpose_b=True) values, preds = tf.nn.top_k(similarity, sorted=True, k=params['pred_top']) # 计算top predictions = {"prob": tf.gather(index_tensor, preds)} export_outputs = { tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: tf.estimator.export.PredictOutput(predictions) } return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs) discret_labels = vocab_table.lookup(labels) discret_features = vocab_table.lookup(features) discret_features_embeddings = tf.nn.embedding_lookup( embeddings, discret_features) #定义输出层权重 with tf.name_scope('weights'): nce_weights = tf.get_variable( 'nce_weights', shape=[params['vocab_size'], params['embedding_size']], initializer=tf.truncated_normal_initializer( stddev=1.0 / math.sqrt(params['embedding_size']))) # 定义输出层偏置 with tf.name_scope('biases'): nce_biases = tf.get_variable('nce_biases', shape=[params['vocab_size']], initializer=tf.zeros_initializer) # 定义损失函数, 采用nce with tf.name_scope('loss'): loss = tf.reduce_mean( tf.nn.nce_loss(weights=nce_weights, biases=nce_biases, labels=discret_labels, inputs=discret_features_embeddings, num_sampled=params['num_neg_samples'], num_classes=params['vocab_size'])) # 训练,采用随机梯度下降优化 with tf.name_scope('optimizer'): optimizer = (tf.train.GradientDescentOptimizer( params['learning_rate']).minimize( loss, global_step=tf.train.get_global_step())) assert mode == tf.estimator.ModeKeys.TRAIN or mode == tf.estimator.ModeKeys.EVAL return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=optimizer)
from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf import numpy as np import pickle import os tf.logging.set_verbosity(tf.logging.INFO) #BG = BatchGenerator(batch_size, num_steps, windows_size, vocab_size) sess = tf.Session() input_file = tf.read_file(os.path.join("", 'input.p')) input_file_data = pickle.loads(sess.run(input_file)) int_to_key = input_file_data['itk'] key_to_int = input_file_data['kti'] all_songs = input_file_data['ixx'] feature_file = tf.read_file(os.path.join("", 'songsf.p')) all_features = pickle.loads(sess.run(feature_file)) int_to_features = np.empty((len(all_features) + 1, 240)) int_to_features[0] = np.zeros((240)) for key, feature in all_features.items(): int_to_features[key_to_int[key]] = feature hidden_size = 240 vocab_size = len(all_features) + 1 batch_size = 1 #num_steps = 31
image_height = 299 image_width = 299 train_batch_size = 32 # batch size test_batch_size = 16 num_out = 3 # number of output result # train data load train_queue = tf.train.string_input_producer([train_csv_dir]) train_reader = tf.TextLineReader() _, train_csv_value = train_reader.read(train_queue) train_img_dir, train_label, train_gender = tf.decode_csv(train_csv_value, record_defaults=[[""], [-1], [-1] ]) train_img_value = tf.read_file(train_img_dir) train_img = tf.reshape(tf.cast(tf.image.decode_jpeg(train_img_value, channels=3), dtype=tf.float32), shape=[image_height, image_width, 3]) train_label = tf.reshape(tf.one_hot(train_label, depth=num_out, on_value=1.0, off_value=0.0), shape=[num_out]) train_gender = tf.reshape(train_gender, shape=[1]) # test data load test_queue = tf.train.string_input_producer([test_csv_dir], shuffle=False) test_reader = tf.TextLineReader() _, test_csv_value = test_reader.read(test_queue)
def InitNetwork(self): self.graph_search = tf.Graph() with self.graph_search.as_default(): with tf.variable_scope('resnet', reuse=False): with tf.name_scope('network') as name_scope: #using default values for batch_norm_decay=0.997, batch_norm_epsilon=1e-5 model = ResNetColorizer(is_training=False, data_format='channels_last') #Exemplar placeholders for data and labels #[BATCH, NUM_OF_SAMPLES, H, W, CH] #Input of grayscaled images #Placehholders self.search_ph = { "pos_x_ph": tf.placeholder(tf.float64), "pos_y_ph": tf.placeholder(tf.float64), "x_sz0_ph": tf.placeholder(tf.float64), "x_sz1_ph": tf.placeholder(tf.float64), "x_sz2_ph": tf.placeholder(tf.float64), "filename_ph": tf.placeholder(tf.string, [], name='filename') } #Open the image image_file = tf.read_file(self.search_ph['filename_ph']) # Decode the image as a JPEG file, this will turn it into a Tensor image = tf.image.decode_jpeg(image_file, channels=1) #Get the shape frame_sz = tf.shape(image) # pad with if necessary frame_padded_x, npad_x = self.pad_frame( image, frame_sz, self.search_ph['pos_x_ph'], self.search_ph['pos_y_ph'], self.search_ph['x_sz2_ph']) frame_padded_x = tf.cast(frame_padded_x, tf.float32) # extract tensor of x_crops (3 scales) x_crops = self.extract_crops_x( frame_padded_x, npad_x, self.search_ph['pos_x_ph'], self.search_ph['pos_y_ph'], self.search_ph['x_sz0_ph'], self.search_ph['x_sz1_ph'], self.search_ph['x_sz2_ph'], self.search_sz) x_crops = tf.expand_dims(x_crops, 0) features_x = model.forward(x_crops) self.graph_exemplar = tf.Graph() with self.graph_exemplar.as_default(): with tf.variable_scope('resnet', reuse=False): with tf.name_scope('network') as name_scope: # using default values for batch_norm_decay=0.997, batch_norm_epsilon=1e-5 model = ResNetColorizer(is_training=False, data_format='channels_last') #Template placeholders for data and labels #Input of grayscaled images self.exemplar_ph = { 'pos_x_ph': tf.placeholder(tf.float64), 'pos_y_ph': tf.placeholder(tf.float64), 'z_sz_ph': tf.placeholder(tf.float64), 'filename_ph': tf.placeholder(tf.string, [], name='filename') } #Open the image image_file = tf.read_file(self.exemplar_ph['filename_ph']) # Decode the image as a JPEG file, this will turn it into a Tensor image = tf.image.decode_jpeg(image_file, channels=1) #Get the shape frame_sz = tf.shape(image) #pad with if necessary frame_padded_z, npad_z = self.pad_frame( image, frame_sz, self.exemplar_ph['pos_x_ph'], self.exemplar_ph['pos_y_ph'], self.exemplar_ph['z_sz_ph']) frame_padded_z = tf.cast(frame_padded_z, tf.float32) # extract tensor of z_crops z_crops = self.extract_crops_z( frame_padded_z, npad_z, self.exemplar_ph['pos_x_ph'], self.exemplar_ph['pos_y_ph'], self.exemplar_ph['z_sz_ph'], self.exemplar_sz) #Feed to the model z_crops = tf.expand_dims(z_crops, 0) features_z = model.forward(z_crops) features_z = tf.squeeze(features_z) features_z = tf.stack([features_z, features_z, features_z]) self.graph_match = tf.Graph() with self.graph_match.as_default(): self.features_x_ph = tf.placeholder(tf.float32, (3, 32, 32, 64), 'feautes_x_input') self.features_z_ph = tf.placeholder(tf.float32, (3, 16, 16, 64), 'features_z_input') #match the templates # z, x are [B, H, W, C] net_z = tf.transpose(self.features_z_ph, perm=[1, 2, 0, 3]) net_x = tf.transpose(self.features_x_ph, perm=[1, 2, 0, 3]) # z, x are [H, W, B, C] Hz, Wz, B, C = tf.unstack(tf.shape(net_z)) Hx, Wx, Bx, Cx = tf.unstack(tf.shape(net_x)) # assert B==Bx, ('Z and X should have same Batch size') # assert C==Cx, ('Z and X should have same Channels number') net_z = tf.reshape(net_z, (Hz, Wz, B * C, 1)) net_x = tf.reshape(net_x, (1, Hx, Wx, B * C)) net_final = tf.nn.depthwise_conv2d(net_x, net_z, strides=[1, 1, 1, 1], padding='VALID') # final is [1, Hf, Wf, BC] net_final = tf.concat(tf.split(net_final, 3, axis=3), axis=0) # final is [B, Hf, Wf, C] scores = tf.expand_dims(tf.reduce_sum(net_final, axis=3), axis=3) scores_up = tf.image.resize_images( scores, [self.final_score_sz, self.final_score_sz], method=tf.image.ResizeMethod.BICUBIC, align_corners=True) return features_x, features_z, scores_up, z_crops, x_crops
#3、找出破损的图像(暂时手动删除) import os import tensorflow as tf def get_files(file_dir): image_list, label_list = [], [] for f in os.listdir(file_dir): image_list.append(os.path.join(file_dir,f)) print('There are %d data' % (len(image_list))) return image_list if __name__=="__main__": train_dir = "D:/project/ShuffleNet/pa/dogsunset" img_list = get_files(train_dir) filename = tf.placeholder(tf.string, [], name='filename') image_file = tf.read_file(filename) # Decode the image as a JPEG file, this will turn it into a Tensor image = tf.image.decode_jpeg(image_file) # 图像解码成矩阵 sess=tf.Session() for i in range(len(img_list)): print(img_list[i], '{}/{}'.format(i,len(img_list))) img=sess.run(image, feed_dict={filename:img_list[i]})
def read_image_from_disk(filename_to_label_tuple): label = filename_to_label_tuple[1] file_contents = tf.read_file(filename_to_label_tuple[0]) img = tf.image.decode_jpeg(file_contents, channels=3) return img, label
def get_image(image_path): file = tf.read_file(image_path) image = tf.image.decode_image(file, channels=channels) sess = tf.get_default_session() file, image = sess.run([file, image]) return image
import util file_listing = pd.read_csv("test.csv") graph = tf.Graph() with tf.Session(graph=graph) as sess: tf.keras.backend.set_session(sess) hairnet_def = tf.contrib.saved_model.load_keras_model("./saved_models/1559294377") index = 120 image_in = cv2.resize(cv2.imread(file_listing.iloc[index]["input"]), (224, 224)) image_la = tf.read_file(file_listing.iloc[index]["output"]) image_la = tf.image.decode_bmp(image_la, channels=3) image_la = tf.image.resize_images(image_la, (224, 224)) # image_la = tf.cast(image_la, tf.float32) image_la = sess.run(image_la[:, :, 0:2]) gtruth = np.zeros_like(image_in) gtruth[:, :, 0:2] = image_la input_layer = graph.get_tensor_by_name("input:0") output_layer = graph.get_tensor_by_name("output/Relu:0") print(output_layer) start = time.time() result = sess.run(output_layer, feed_dict={"input:0": [image_in]}) print(f"Processed in {time.time() - start} s") output_hair = result[0, :, :, 0]
user_processed_images = [] # image_names = [] image_files = [] # GT_dict = {} GT_num = {} f = open("tmp\\captcha\\test2.txt", "r") while(1): data = f.readline() if not data: break image_files.append(data[:-1]) # image_files = random.sample(image_names, sample_num) for i in image_files: # GT_dict[i] = i.split('/')[3] GT_num[i] = i.split('\\')[3] image_input = tf.read_file(i) image = tf.image.decode_jpeg(image_input, channels=3) user_images.append(image) processed_image = densenet_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) user_processed_images.append(processed_image) processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(densenet.densenet_arg_scope()): logits, _ = densenet.densenet121(user_processed_images, num_classes=5, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'model.ckpt-21031'), slim.get_model_variables('densenet121'))
def run(): with tf.Graph().as_default() as graph: tf.logging.set_verbosity(tf.logging.INFO) #===================TEST BRANCH======================= #Load the files into one input queue images = tf.convert_to_tensor(image_files) annotations = tf.convert_to_tensor(annotation_files) input_queue = tf.train.slice_input_producer([images, annotations]) #Decode the image and annotation raw content image = tf.read_file(input_queue[0]) image = tf.image.decode_image(image, channels=3) annotation = tf.read_file(input_queue[1]) annotation = tf.image.decode_image(annotation) #preprocess and batch up the image and annotation preprocessed_image, preprocessed_annotation = preprocess( image, annotation, image_height, image_width) images, annotations = tf.train.batch( [preprocessed_image, preprocessed_annotation], batch_size=batch_size, allow_smaller_final_batch=True) #Create the model inference with slim.arg_scope(ENet_arg_scope()): logits, probabilities = ENet(images, num_classes, batch_size=batch_size, is_training=True, reuse=None, num_initial_blocks=num_initial_blocks, stage_two_repeat=stage_two_repeat, skip_connections=skip_connections) # Set up the variables to restore and restoring function from a saver. exclude = [] variables_to_restore = slim.get_variables_to_restore(exclude=exclude) saver = tf.train.Saver(variables_to_restore) def restore_fn(sess): return saver.restore(sess, checkpoint_file) #perform one-hot-encoding on the ground truth annotation to get same shape as the logits annotations = tf.reshape(annotations, shape=[batch_size, image_height, image_width]) annotations_ohe = tf.one_hot(annotations, num_classes, axis=-1) annotations = tf.cast(annotations, tf.int64) #State the metrics that you want to predict. We get a predictions that is not one_hot_encoded. predictions = tf.argmax(probabilities, -1) accuracy, accuracy_update = tf.contrib.metrics.streaming_accuracy( predictions, annotations) mean_IOU, mean_IOU_update = tf.contrib.metrics.streaming_mean_iou( predictions=predictions, labels=annotations, num_classes=num_classes) per_class_accuracy, per_class_accuracy_update = tf.metrics.mean_per_class_accuracy( labels=annotations, predictions=predictions, num_classes=num_classes) metrics_op = tf.group(accuracy_update, mean_IOU_update, per_class_accuracy_update) #Create the global step and an increment op for monitoring global_step = get_or_create_global_step() global_step_op = tf.assign( global_step, global_step + 1 ) #no apply_gradient method so manually increasing the global_step #Create a evaluation step function def eval_step(sess, metrics_op, global_step): ''' Simply takes in a session, runs the metrics op and some logging information. ''' start_time = time.time() _, global_step_count, accuracy_value, mean_IOU_value, per_class_accuracy_value = sess.run( [ metrics_op, global_step_op, accuracy, mean_IOU, per_class_accuracy ]) time_elapsed = time.time() - start_time #Log some information logging.info( 'Global Step %s: Streaming Accuracy: %.4f Streaming Mean IOU: %.4f Per-class Accuracy: %.4f (%.2f sec/step)', global_step_count, accuracy_value, mean_IOU_value, per_class_accuracy_value, time_elapsed) return accuracy_value, mean_IOU_value, per_class_accuracy_value #Create your summaries tf.summary.scalar('Monitor/test_accuracy', accuracy) tf.summary.scalar('Monitor/test_mean_per_class_accuracy', per_class_accuracy) tf.summary.scalar('Monitor/test_mean_IOU', mean_IOU) my_summary_op = tf.summary.merge_all() #Define your supervisor for running a managed session. Do not run the summary_op automatically or else it will consume too much memory sv = tf.train.Supervisor(logdir=logdir, summary_op=None, init_fn=restore_fn) #Run the managed session with sv.managed_session() as sess: for step in range(int(num_steps_per_epoch * num_epochs)): #print vital information every start of the epoch as always if step % num_batches_per_epoch == 0: accuracy_value, mean_IOU_value = sess.run( [accuracy, mean_IOU]) logging.info('Epoch: %s/%s', step / num_batches_per_epoch + 1, num_epochs) logging.info('Current Streaming Accuracy: %.4f', accuracy_value) logging.info('Current Streaming Mean IOU: %.4f', mean_IOU_value) #Compute summaries every 10 steps and continue evaluating if step % 10 == 0: test_accuracy, test_mean_IOU, test_per_class_accuracy = eval_step( sess, metrics_op=metrics_op, global_step=sv.global_step) summaries = sess.run(my_summary_op) sv.summary_computed(sess, summaries) #Otherwise just run as per normal else: test_accuracy, test_mean_IOU, test_per_class_accuracy = eval_step( sess, metrics_op=metrics_op, global_step=sv.global_step) #At the end of all the evaluation, show the final accuracy logging.info('Final Streaming Accuracy: %.4f', test_accuracy) logging.info('Final Mean IOU: %.4f', test_mean_IOU) logging.info('Final Per Class Accuracy %.4f', test_per_class_accuracy) #Show end of evaluation logging.info('Finished evaluating!') #Save the images if save_images: if not os.path.exists(photo_dir): os.mkdir(photo_dir) #Save the image visualizations for the first 10 images. logging.info('Saving the images now...') predictions_val, annotations_val = sess.run( [predictions, annotations]) for i in xrange(1): predicted_annotation = predictions_val[i] annotation = annotations_val[i] plt.subplot(1, 2, 1) plt.imshow(predicted_annotation) print(type(predicted_annotation)) print(predicted_annotation.shape) plt.subplot(1, 2, 2) plt.imshow(annotation) plt.savefig(photo_dir + "/image_" + str(i))
def _pre_imgs(imgs_filenames): imgs = tf.read_file(imgs_filenames) imgs = tf.image.decode_png(imgs, channels=3) imgs = tf.image.resize_images(imgs, [256, 256]) imgs = tf.image.per_image_standardization(imgs) return imgs
'Directory to put the training data.') data = __import__(FLAGS.data_name) model = __import__(FLAGS.model_name) if data.resize == model.upsample: print("Config Error") quit() with tf.Graph().as_default(): with open(FLAGS.hr_flist) as f: hr_filename_list = f.read().splitlines() with open(FLAGS.lr_flist) as f: lr_filename_list = f.read().splitlines() filename_queue = tf.train.slice_input_producer( [hr_filename_list, lr_filename_list], num_epochs=2, shuffle=False) hr_image_file = tf.read_file(filename_queue[0]) lr_image_file = tf.read_file(filename_queue[1]) hr_image = tf.image.decode_image(hr_image_file, channels=3) lr_image = tf.image.decode_image(lr_image_file, channels=3) hr_image = tf.image.convert_image_dtype(hr_image, tf.float32) lr_image = tf.image.convert_image_dtype(lr_image, tf.float32) hr_image = tf.expand_dims(hr_image, 0) lr_image = tf.expand_dims(lr_image, 0) lr_image_shape = tf.shape(lr_image)[1:3] hr_image_shape = tf.shape(hr_image)[1:3] if data.resize: lr_image = util.resize_func(lr_image, hr_image_shape) lr_image = tf.reshape(lr_image, [1, hr_image_shape[0], hr_image_shape[1], 3]) else: lr_image = tf.reshape(lr_image,
def create_train_data(self, train_dir, savename, save_type): """ Create training data :param train_dir: String, path to images folder :return: array of Tensors or Numpy.ndarrays, depending on `save_type` parameter """ # TODO: add image contortions to increase dataset size and variance if save_type not in ("np", "tf"): raise ValueError("Invalid argument for 'save_type' parameter.\nValid options are 'np' or 'tf'.") training_data = [] labels = [] prog_bar = tqdm(os.listdir(train_dir)) # Use tqdm for image loading progress meter for img_name in prog_bar: prog_bar.set_description("Loading training set") path = os.path.join(train_dir, img_name) if save_type == "np": if TadpoleConvNet.GRAYSCALE: img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE) else: img_data = cv2.imread(path) if img_data is None: print("Could not load image {}".format(path)) continue img_data = cv2.resize(img_data, (self.IMG_SIZE, self.IMG_SIZE)) img_data = img_data[:, :, np.newaxis] # img dimensions must match input tensor dims # TODO: normalize the images?? # image = image.astype(np.float32) # image = np.multiply(image, 1.0 / 255.0) else: if ".jpg" in path: img = tf.read_file(path) img_data = tf.image.decode_jpeg(img, channels=TadpoleConvNet.NUM_CHANNELS) elif ".png" in path: img = tf.read_file(path) img_data = tf.image.decode_png(img, channels=TadpoleConvNet.NUM_CHANNELS) else: print("Could not open image {}".format(path)) continue # if <<check if Tensor is empty>> # print("Could not load image {}".format(path)) # continue img_data = tf.image.resize_images(img_data, [TadpoleConvNet.IMG_SIZE, TadpoleConvNet.IMG_SIZE]) if TadpoleConvNet.GRAYSCALE: img_data = tf.image.rgb_to_grayscale(img_data) # img_data = tf.matmul(img_data, [1/255.0]) training_data.append(img_data) labels.append(self.create_label(img_name, save_type=save_type)) if self.save_data and save_type == "np": # np.save(savename, (training_data, labels)) np.savez(savename, training_data, labels) else: pass # figure out saving list of Tensors to file -- TFRecords format? return (training_data, labels)
def process_img(path, shape=None, crop=False): img = tf.read_file(path) img = tf.image.decode_jpeg(img, channels=3) if shape is not None: img.set_shape(shape) return tf.to_float(img)
def main(args): network = importlib.import_module(args.model_def) subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S') log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir) if not os.path.isdir( log_dir): # Create the log directory if it doesn't exist os.makedirs(log_dir) model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir) if not os.path.isdir( model_dir): # Create the model directory if it doesn't exist os.makedirs(model_dir) # Write arguments to a text file facenet.write_arguments_to_file(args, os.path.join(log_dir, 'arguments.txt')) # Store some git revision info in a text file in the log directory src_path, _ = os.path.split(os.path.realpath(__file__)) facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv)) np.random.seed(seed=args.seed) random.seed(args.seed) train_set = facenet.get_dataset(args.data_dir) if args.filter_filename: train_set = filter_dataset(train_set, os.path.expanduser(args.filter_filename), args.filter_percentile, args.filter_min_nrof_images_per_class) nrof_classes = len(train_set) print('Model directory: %s' % model_dir) print('Log directory: %s' % log_dir) pretrained_model = None if args.pretrained_model: pretrained_model = os.path.expanduser(args.pretrained_model) print('Pre-trained model: %s' % pretrained_model) if args.lfw_dir: print('LFW directory: %s' % args.lfw_dir) # Read the file containing the pairs used for testing pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs)) # Get the paths for the corresponding images lfw_paths, actual_issame = lfw.get_paths( os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext) with tf.Graph().as_default(): tf.set_random_seed(args.seed) global_step = tf.Variable(0, trainable=False) # Get a list of image paths and their labels image_list, label_list = facenet.get_image_paths_and_labels(train_set) assert len(image_list) > 0, 'The dataset should not be empty' # Create a queue that produces indices into the image_list and label_list labels = ops.convert_to_tensor(label_list, dtype=tf.int32) range_size = array_ops.shape(labels)[0] index_queue = tf.train.range_input_producer(range_size, num_epochs=None, shuffle=True, seed=None, capacity=32) index_dequeue_op = index_queue.dequeue_many( args.batch_size * args.epoch_size, 'index_dequeue') learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate') batch_size_placeholder = tf.placeholder(tf.int32, name='batch_size') phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train') image_paths_placeholder = tf.placeholder(tf.string, shape=(None, 1), name='image_paths') labels_placeholder = tf.placeholder(tf.int64, shape=(None, 1), name='labels') input_queue = data_flow_ops.FIFOQueue(capacity=100000, dtypes=[tf.string, tf.int64], shapes=[(1, ), (1, )], shared_name=None, name=None) enqueue_op = input_queue.enqueue_many( [image_paths_placeholder, labels_placeholder], name='enqueue_op') nrof_preprocess_threads = 4 images_and_labels = [] for _ in range(nrof_preprocess_threads): filenames, label = input_queue.dequeue() images = [] for filename in tf.unstack(filenames): file_contents = tf.read_file(filename) image = tf.image.decode_image(file_contents, channels=3) if args.random_rotate: image = tf.py_func(facenet.random_rotate_image, [image], tf.uint8) if args.random_crop: image = tf.random_crop( image, [args.image_size, args.image_size, 3]) else: image = tf.image.resize_image_with_crop_or_pad( image, args.image_size, args.image_size) if args.random_flip: image = tf.image.random_flip_left_right(image) #pylint: disable=no-member image.set_shape((args.image_size, args.image_size, 3)) images.append(tf.image.per_image_standardization(image)) images_and_labels.append([images, label]) image_batch, label_batch = tf.train.batch_join( images_and_labels, batch_size=batch_size_placeholder, shapes=[(args.image_size, args.image_size, 3), ()], enqueue_many=True, capacity=4 * nrof_preprocess_threads * args.batch_size, allow_smaller_final_batch=True) image_batch = tf.identity(image_batch, 'image_batch') image_batch = tf.identity(image_batch, 'input') label_batch = tf.identity(label_batch, 'label_batch') print('Total number of classes: %d' % nrof_classes) print('Total number of examples: %d' % len(image_list)) print('Building training graph') # Build the inference graph prelogits, _ = network.inference( image_batch, args.keep_probability, phase_train=phase_train_placeholder, bottleneck_layer_size=args.embedding_size, weight_decay=args.weight_decay) logits = slim.fully_connected( prelogits, len(train_set), activation_fn=None, weights_initializer=tf.truncated_normal_initializer(stddev=0.1), weights_regularizer=slim.l2_regularizer(args.weight_decay), scope='Logits', reuse=False) embeddings = tf.nn.l2_normalize(prelogits, 1, 1e-10, name='embeddings') # Add center loss if args.center_loss_factor > 0.0: prelogits_center_loss, _ = facenet.center_loss( prelogits, label_batch, args.center_loss_alfa, nrof_classes) tf.add_to_collection( tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor) learning_rate = tf.train.exponential_decay( learning_rate_placeholder, global_step, args.learning_rate_decay_epochs * args.epoch_size, args.learning_rate_decay_factor, staircase=True) tf.summary.scalar('learning_rate', learning_rate) # Calculate the average cross entropy loss across the batch cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( labels=label_batch, logits=logits, name='cross_entropy_per_example') cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy') tf.add_to_collection('losses', cross_entropy_mean) # Calculate the total losses regularization_losses = tf.get_collection( tf.GraphKeys.REGULARIZATION_LOSSES) total_loss = tf.add_n([cross_entropy_mean] + regularization_losses, name='total_loss') # Build a Graph that trains the model with one batch of examples and updates the model parameters train_op = facenet.train(total_loss, global_step, args.optimizer, learning_rate, args.moving_average_decay, tf.global_variables(), args.log_histograms) # Create a saver saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() # Start running operations on the Graph. gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=args.gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) summary_writer = tf.summary.FileWriter(log_dir, sess.graph) coord = tf.train.Coordinator() tf.train.start_queue_runners(coord=coord, sess=sess) with sess.as_default(): if pretrained_model: print('Restoring pretrained model: %s' % pretrained_model) saver.restore(sess, pretrained_model) # Training and validation loop print('Running training') epoch = 0 while epoch < args.max_nrof_epochs: step = sess.run(global_step, feed_dict=None) epoch = step // args.epoch_size # Train for one epoch train(args, sess, epoch, image_list, label_list, index_dequeue_op, enqueue_op, image_paths_placeholder, labels_placeholder, learning_rate_placeholder, phase_train_placeholder, batch_size_placeholder, global_step, total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file) # Save variables and the metagraph if it doesn't exist already save_variables_and_metagraph(sess, saver, summary_writer, model_dir, subdir, step) # Evaluate on LFW if args.lfw_dir: evaluate(sess, enqueue_op, image_paths_placeholder, labels_placeholder, phase_train_placeholder, batch_size_placeholder, embeddings, label_batch, lfw_paths, actual_issame, args.lfw_batch_size, args.lfw_nrof_folds, log_dir, step, summary_writer) return model_dir
def create_test_data(self, test_dir, savename, save_type): testing_data = [] labels = [] prog_bar = tqdm(os.listdir(test_dir)) # Use tqdm for image loading progress meter for img_name in prog_bar: prog_bar.set_description("Loading testing set") path = os.path.join(test_dir, img_name) if save_type == "np": if self.GRAYSCALE: img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE) else: img_data = cv2.imread(path) if img_data is None: print("Could not load image {}".format(path)) continue img_data = cv2.resize(img_data, (self.IMG_SIZE, self.IMG_SIZE)) img_data = img_data[:, :, np.newaxis] # img dimensions must match input tensor dims # image = image.astype(np.float32) # image = np.multiply(image, 1.0 / 255.0) else: if ".jpg" in path: img = tf.read_file(path) img_data = tf.image.decode_jpeg(img, channels=self.NUM_CHANNELS) elif ".png" in path: img = tf.read_file(path) img_data = tf.image.decode_png(img, channels=self.NUM_CHANNELS) else: print("Could not open image {}".format(path)) continue # if <<check if Tensor is empty>> # print("Could not load image {}".format(path)) # continue img_data = tf.image.resize_images(img_data, [self.IMG_SIZE, self.IMG_SIZE]) if self.GRAYSCALE: img_data = tf.image.rgb_to_grayscale(img_data) # img_data = tf.matmul(img_data, [1/255.0]) testing_data.append(img_data) labels.append(self.create_label(img_name, save_type=save_type)) if self.save_data and save_type == "np": # np.save(savename, (testing_data, labels)) np.savez(savename, testing_data, labels) # with open(savename, "wb") as handle: # pickle.dump((testing_data, labels), handle) else: pass return testing_data, labels
def _read_processed(filepath, label): """Read function for reading processed images.""" image_string = tf.read_file(filepath) image_decoded_bw = tf.image.decode_jpeg(image_string, channels=1) return image_decoded_bw, label
def load_img(path): img_raw = tf.read_file(path) img = tf.image.decode_jpeg(img_raw, channels=3) return img
tf.reduce_prod() tf.reduce_sum() tf.reduced_shape() tf.random_crop() tf.random_gamma() tf.random_normal() tf.random_poisson() tf.random_poisson_v2() tf.random_shuffle() tf.random_uniform() tf.where() tf.while_loop() tf.write_file() tf.read_file() tf.record_input() tf.reshape() tf.restore_v2() tf.reverse() tf.ordered_map_clear() tf.ordered_map_incomplete_size() tf.ordered_map_peek() tf.ordered_map_size() tf.ordered_map_stage() tf.ordered_map_unstage() tf.ordered_map_unstage_no_key() tf.matrix_diag()
def _parse_function(filename, label): image_string = tf.read_file(filename) image_decoded = tf.image.decode_image(image_string) return image_decoded, label
def data_loader(FLAGS): with tf.device('/cpu:0'): # Define the returned data batches Data = collections.namedtuple( 'Data', 'paths_LR, paths_HR, inputs, targets, image_count, steps_per_epoch' ) #Check the input directory if (FLAGS.input_dir_LR == 'None') or (FLAGS.input_dir_HR == 'None'): raise ValueError('Input directory is not provided') if (not os.path.exists(FLAGS.input_dir_LR)) or (not os.path.exists( FLAGS.input_dir_HR)): raise ValueError('Input directory not found') image_list_LR = os.listdir(FLAGS.input_dir_LR) image_list_LR = [_ for _ in image_list_LR if _.endswith('.png')] if len(image_list_LR) == 0: raise Exception('No png files in the input directory') image_list_LR_temp = sorted(image_list_LR) image_list_LR = [ os.path.join(FLAGS.input_dir_LR, _) for _ in image_list_LR_temp ] image_list_HR = [ os.path.join(FLAGS.input_dir_HR, _) for _ in image_list_LR_temp ] image_list_LR_tensor = tf.convert_to_tensor(image_list_LR, dtype=tf.string) image_list_HR_tensor = tf.convert_to_tensor(image_list_HR, dtype=tf.string) with tf.variable_scope('load_image'): # define the image list queue # image_list_LR_queue = tf.train.string_input_producer(image_list_LR, shuffle=False, capacity=FLAGS.name_queue_capacity) # image_list_HR_queue = tf.train.string_input_producer(image_list_HR, shuffle=False, capacity=FLAGS.name_queue_capacity) #print('[Queue] image list queue use shuffle: %s'%(FLAGS.mode == 'Train')) output = tf.train.slice_input_producer( [image_list_LR_tensor, image_list_HR_tensor], shuffle=False, capacity=FLAGS.name_queue_capacity) # Reading and decode the images reader = tf.WholeFileReader(name='image_reader') image_LR = tf.read_file(output[0]) image_HR = tf.read_file(output[1]) input_image_LR = tf.image.decode_png(image_LR, channels=3) input_image_HR = tf.image.decode_png(image_HR, channels=3) input_image_LR = tf.image.convert_image_dtype(input_image_LR, dtype=tf.float32) input_image_HR = tf.image.convert_image_dtype(input_image_HR, dtype=tf.float32) assertion = tf.assert_equal( tf.shape(input_image_LR)[2], 3, message="image does not have 3 channels") with tf.control_dependencies([assertion]): input_image_LR = tf.identity(input_image_LR) input_image_HR = tf.identity(input_image_HR) # Normalize the low resolution image to [0, 1], high resolution to [-1, 1] a_image = preprocessLR(input_image_LR) b_image = preprocess(input_image_HR) inputs, targets = [a_image, b_image] # The data augmentation part with tf.name_scope('data_preprocessing'): with tf.name_scope('random_crop'): # Check whether perform crop if (FLAGS.random_crop is True) and FLAGS.mode == 'train': print('[Config] Use random crop') # Set the shape of the input image. the target will have 4X size input_size = tf.shape(inputs) target_size = tf.shape(targets) offset_w = tf.cast(tf.floor( tf.random_uniform([], 0, tf.cast(input_size[1], tf.float32) - FLAGS.crop_size)), dtype=tf.int32) offset_h = tf.cast(tf.floor( tf.random_uniform([], 0, tf.cast(input_size[0], tf.float32) - FLAGS.crop_size)), dtype=tf.int32) if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet' or FLAGS.task == 'MAD_SRGAN': inputs = tf.image.crop_to_bounding_box( inputs, offset_h, offset_w, FLAGS.crop_size, FLAGS.crop_size) targets = tf.image.crop_to_bounding_box( targets, offset_h * 4, offset_w * 4, FLAGS.crop_size * 4, FLAGS.crop_size * 4) elif FLAGS.task == 'denoise': inputs = tf.image.crop_to_bounding_box( inputs, offset_h, offset_w, FLAGS.crop_size, FLAGS.crop_size) targets = tf.image.crop_to_bounding_box( targets, offset_h, offset_w, FLAGS.crop_size, FLAGS.crop_size) # Do not perform crop else: inputs = tf.identity(inputs) targets = tf.identity(targets) with tf.variable_scope('random_flip'): # Check for random flip: if (FLAGS.flip is True) and (FLAGS.mode == 'train'): print('[Config] Use random flip') # Produce the decision of random flip decision = tf.random_uniform([], 0, 1, dtype=tf.float32) input_images = random_flip(inputs, decision) target_images = random_flip(targets, decision) else: input_images = tf.identity(inputs) target_images = tf.identity(targets) if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet' or FLAGS.task == 'MAD_SRGAN': input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3]) target_images.set_shape( [FLAGS.crop_size * 4, FLAGS.crop_size * 4, 3]) elif FLAGS.task == 'denoise': input_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3]) target_images.set_shape([FLAGS.crop_size, FLAGS.crop_size, 3]) if FLAGS.mode == 'train': paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.shuffle_batch( [output[0], output[1], input_images, target_images], batch_size=FLAGS.batch_size, capacity=FLAGS.image_queue_capacity + 4 * FLAGS.batch_size, min_after_dequeue=FLAGS.image_queue_capacity, num_threads=FLAGS.queue_thread) else: paths_LR_batch, paths_HR_batch, inputs_batch, targets_batch = tf.train.batch( [output[0], output[1], input_images, target_images], batch_size=FLAGS.batch_size, num_threads=FLAGS.queue_thread, allow_smaller_final_batch=True) steps_per_epoch = int(math.ceil(len(image_list_LR) / FLAGS.batch_size)) if FLAGS.task == 'SRGAN' or FLAGS.task == 'SRResnet' or FLAGS.task == 'MAD_SRGAN': inputs_batch.set_shape( [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3]) targets_batch.set_shape([ FLAGS.batch_size, FLAGS.crop_size * 4, FLAGS.crop_size * 4, 3 ]) elif FLAGS.task == 'denoise': inputs_batch.set_shape( [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3]) targets_batch.set_shape( [FLAGS.batch_size, FLAGS.crop_size, FLAGS.crop_size, 3]) return Data(paths_LR=paths_LR_batch, paths_HR=paths_HR_batch, inputs=inputs_batch, targets=targets_batch, image_count=len(image_list_LR), steps_per_epoch=steps_per_epoch)
def custom_map(image_path): image_data = tf.read_file(image_path) image = tf.image.decode_jpeg(tf.reshape(image_data, shape=[]), 3) image = tf.image.resize_bicubic([image], [224, 224]) image = tf.cast(tf.clip_by_value(image, 0, 255), tf.uint8)[0] return image