def parse_example(serialized_example):
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'shape': tf.FixedLenFeature([], tf.string),
'img_raw': tf.FixedLenFeature([], tf.string),
'gt_raw': tf.FixedLenFeature([], tf.string),
'example_name': tf.FixedLenFeature([], tf.string)
})
with tf.variable_scope('decoder'):
shape = tf.decode_raw(features['shape'], tf.int32)
image = tf.decode_raw(features['img_raw'], tf.float32)
ground_truth = tf.decode_raw(features['gt_raw'], tf.uint8)
example_name = features['example_name']
with tf.variable_scope('image'):
# reshape and add 0 dimension (would be batch dimension)
image = tf.expand_dims(tf.reshape(image, shape), 0)
with tf.variable_scope('ground_truth'):
# reshape
ground_truth = tf.cast(tf.reshape(ground_truth, shape[:-1]), tf.float32)
return image, ground_truth, example_name
python类uint8()的实例源码
def preprocess(self, image_buffer, bbox, batch_position):
"""Preprocessing image_buffer as a function of its batch position."""
if self.train:
image = train_image(image_buffer, self.height, self.width, bbox,
batch_position, self.resize_method, self.distortions,
None, summary_verbosity=self.summary_verbosity,
distort_color_in_yiq=self.distort_color_in_yiq,
fuse_decode_and_crop=self.fuse_decode_and_crop)
else:
image = tf.image.decode_jpeg(
image_buffer, channels=3, dct_method='INTEGER_FAST')
image = eval_image(image, self.height, self.width, batch_position,
self.resize_method,
summary_verbosity=self.summary_verbosity)
# Note: image is now float32 [height,width,3] with range [0, 255]
# image = tf.cast(image, tf.uint8) # HACK TESTING
return image
def make_png_thumbnail(x, n):
'''
Input:
`x`: Tensor, value range=[-1, 1), shape=[n*n, h, w, c]
`n`: sqrt of the number of images
Return:
`tf.string` (bytes) of the PNG.
(write these binary directly into a file)
'''
with tf.name_scope('MakeThumbnail'):
_, h, w, c = x.get_shape().as_list()
x = tf.reshape(x, [n, n, h, w, c])
x = tf.transpose(x, [0, 2, 1, 3, 4])
x = tf.reshape(x, [n * h, n * w, c])
x = x / 2. + .5
x = tf.image.convert_image_dtype(x, tf.uint8, saturate=True)
x = tf.image.encode_png(x)
return x
def make_png_jet_thumbnail(x, n):
'''
Input:
`x`: Tensor, value range=[-1, 1), shape=[n*n, h, w, c]
`n`: sqrt of the number of images
Return:
`tf.string` (bytes) of the PNG.
(write these binary directly into a file)
'''
with tf.name_scope('MakeThumbnail'):
_, h, w, c = x.get_shape().as_list()
x = tf.reshape(x, [n, n, h, w, c])
x = tf.transpose(x, [0, 2, 1, 3, 4])
x = tf.reshape(x, [n * h, n * w, c])
x = x / 2. + .5
x = gray2jet(x)
x = tf.image.convert_image_dtype(x, tf.uint8, saturate=True)
x = tf.image.encode_png(x)
return x
def get_label_queue(self,batch_size):
tf_labels = tf.convert_to_tensor(self.attr.values, dtype=tf.uint8)#0,1
with tf.name_scope('label_queue'):
uint_label=tf.train.slice_input_producer([tf_labels])[0]
label=tf.to_float(uint_label)
#All labels, not just those in causal_model
dict_data={sl:tl for sl,tl in
zip(self.label_names,tf.split(label,len(self.label_names)))}
num_preprocess_threads = max(self.num_worker-3,1)
data_batch = tf.train.shuffle_batch(
dict_data,
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=self.min_queue_examples + 3 * batch_size,
min_after_dequeue=self.min_queue_examples,
)
return data_batch
def write_tfrecord(self, img_list, label_list, record_path):
# write a single tfrecord
if os.path.exists(record_path):
print ("%s exists!"%record_path)
return
self._check_list()
print ("write %s"%record_path)
self._write_info()
writer = tf.python_io.TFRecordWriter(record_path)
c = 0
for imgname,label in zip(img_list,label_list):
img = Image.open(imgname).resize((self.flags.width, self.flags.height))
data = np.array(img).astype(np.uint8)
img,data = self._check_color(img,data)
example = self._get_example(data,label)
writer.write(example.SerializeToString())
c+=1
if c%LOG_EVERY == 0:
print ("%d images written to tfrecord"%c)
writer.close()
print("writing %s done"%record_path)
def get_shapes_and_dtypes(data):
shapes = {}
dtypes = {}
for k in data.keys():
if isinstance(data[k][0], str):
shapes[k] = []
dtypes[k] = tf.string
elif isinstance(data[k][0], np.ndarray):
shapes[k] = data[k][0].shape
dtypes[k] = tf.uint8
elif isinstance(data[k][0], np.bool_):
shapes[k] = []
dtypes[k] = tf.string
else:
raise TypeError('Unknown data type', type(data[k][0]))
return shapes, dtypes
def __init__(self):
# Create a single Session to run all image coding calls.
self._sess = tf.Session()
# Initializes function that decodes video
self._video_path = tf.placeholder(dtype=tf.string)
self._decode_video = decode_video(self._video_path)
# Initialize function that resizes a frame
self._resize_video_data = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3])
# Initialize function to JPEG-encode a frame
self._raw_frame = tf.placeholder(dtype=tf.uint8, shape=[None, None, 3])
self._raw_mask = tf.placeholder(dtype=tf.uint8, shape=[None, None, 1])
self._encode_frame = tf.image.encode_jpeg(self._raw_frame, quality=100)
self._encode_mask = tf.image.encode_png(self._raw_mask)
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print('Extracting', filename)
with tf.gfile.Open(filename, 'rb') as f, gzip.GzipFile(fileobj=f) as bytestream:
magic = _read32(bytestream)
if magic != 2051:
raise ValueError(
'Invalid magic number %d in MNIST image file: %s' %
(magic, filename))
num_images = _read32(bytestream)
rows = _read32(bytestream)
cols = _read32(bytestream)
buf = bytestream.read(rows * cols * num_images)
data = numpy.frombuffer(buf, dtype=numpy.uint8)
data = data.reshape(num_images, rows, cols, 1)
return data
def parse_mnist_tfrec(tfrecord, features_shape):
tfrecord_features = tf.parse_single_example(
tfrecord,
features={
'features': tf.FixedLenFeature([], tf.string),
'targets': tf.FixedLenFeature([], tf.string)
}
)
features = tf.decode_raw(tfrecord_features['features'], tf.uint8)
features = tf.reshape(features, features_shape)
features = tf.cast(features, tf.float32)
targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8)
targets = tf.reshape(targets, [])
targets = tf.one_hot(indices=targets, depth=10, on_value=1, off_value=0)
targets = tf.cast(targets, tf.float32)
return features, targets
def parse_mnist_tfrec(tfrecord, name, features_shape, scalar_targs=False):
tfrecord_features = tf.parse_single_example(
tfrecord,
features={
'features': tf.FixedLenFeature([], tf.string),
'targets': tf.FixedLenFeature([], tf.string)
},
name=name+'_data'
)
with tf.variable_scope('features'):
features = tf.decode_raw(
tfrecord_features['features'], tf.uint8
)
features = tf.reshape(features, features_shape)
features = tf.cast(features, tf.float32)
with tf.variable_scope('targets'):
targets = tf.decode_raw(tfrecord_features['targets'], tf.uint8)
if scalar_targs:
targets = tf.reshape(targets, [])
targets = tf.one_hot(
indices=targets, depth=10, on_value=1, off_value=0
)
targets = tf.cast(targets, tf.float32)
return features, targets
def read_and_decode(filename_queue, batch_size):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
feature = features()
feature = tf.parse_single_example(
serialized_example,
features = feature,
)
hr_image = tf.decode_raw(feature['hr_image'], tf.uint8)
height = tf.cast(feature['height'], tf.int32)
width = tf.cast(feature['width'], tf.int32)
print(height)
image_shape = tf.stack([128, 128,3 ])
hr_image = tf.reshape(hr_image, image_shape)
hr_image = tf.image.random_flip_left_right(hr_image)
hr_image = tf.image.random_contrast(hr_image, 0.5, 1.3)
hr_images = tf.train.shuffle_batch([hr_image], batch_size = batch_size, capacity = 30,
num_threads = 2,
min_after_dequeue = 10)
return hr_images
def _convert_string_dtype(dtype):
if dtype == 'float16':
return tf.float16
if dtype == 'float32':
return tf.float32
elif dtype == 'float64':
return tf.float64
elif dtype == 'int16':
return tf.int16
elif dtype == 'int32':
return tf.int32
elif dtype == 'int64':
return tf.int64
elif dtype == 'uint8':
return tf.int8
elif dtype == 'uint16':
return tf.uint16
else:
raise ValueError('Unsupported dtype:', dtype)
def process_state(self, state):
"""
Processing of state
State placeholders are tf.uint8 for fast transfer to GPU
Need to cast it to float32 for the rest of the tf graph.
Args:
state: node of tf graph of shape = (batch_size, height, width, nchannels)
of type tf.uint8.
if , values are between 0 and 255 -> 0 and 1
"""
state = tf.cast(state, tf.float32)
state /= self.config.high
return state
def save(tensor, name="noise.png"):
"""
Save an image Tensor to a file.
:param Tensor tensor: Image tensor
:param str name: Filename, ending with .png or .jpg
:return: None
"""
tensor = tf.image.convert_image_dtype(tensor, tf.uint8, saturate=True)
if name.endswith(".png"):
data = tf.image.encode_png(tensor).eval()
elif name.endswith(".jpg"):
data = tf.image.encode_jpeg(tensor).eval()
else:
raise ValueError("Filename should end with .png or .jpg")
with open(name, "wb") as fh:
fh.write(data)
def jpeg_decimate(tensor, shape, iterations=25):
"""
JPEG decimation with conv2d feedback loop
:param Tensor tensor:
:return: Tensor
"""
jpegged = tensor
for i in range(iterations):
jpegged = tf.image.convert_image_dtype(jpegged, tf.uint8)
data = tf.image.encode_jpeg(jpegged, quality=random.randint(5, 50), x_density=random.randint(50, 500), y_density=random.randint(50, 500))
jpegged = tf.image.decode_jpeg(data)
jpegged = tf.image.convert_image_dtype(jpegged, tf.float32, saturate=True)
return jpegged
def _maybe_download_and_extract(self):
"""Download and extract the MNIST dataset"""
data_sets = mnist.read_data_sets(
self._data_dir,
dtype=tf.uint8,
reshape=False,
validation_size=self._num_examples_per_epoch_for_eval)
# Convert to Examples and write the result to TFRecords.
if not tf.gfile.Exists(os.path.join(self._data_dir, 'train.tfrecords')):
convert_to_tfrecords(data_sets.train, 'train', self._data_dir)
if not tf.gfile.Exists(
os.path.join(self._data_dir, 'validation.tfrecords')):
convert_to_tfrecords(data_sets.validation, 'validation',
self._data_dir)
if not tf.gfile.Exists(os.path.join(self._data_dir, 'test.tfrecords')):
convert_to_tfrecords(data_sets.test, 'test', self._data_dir)
def read_and_decode(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
features={
'image_raw': tf.FixedLenFeature([], tf.string),
'label_raw': tf.FixedLenFeature([], tf.string),
})
image = tf.decode_raw(features['image_raw'], tf.int16)
image.set_shape([IMAGE_HEIGHT * IMAGE_WIDTH])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5
reshape_image = tf.reshape(image, [IMAGE_HEIGHT, IMAGE_WIDTH, 1])
label = tf.decode_raw(features['label_raw'], tf.uint8)
label.set_shape([CHARS_NUM * CLASSES_NUM])
reshape_label = tf.reshape(label, [CHARS_NUM, CLASSES_NUM])
return tf.cast(reshape_image, tf.float32), tf.cast(reshape_label, tf.float32)
def read_and_decode_embedding(filename_queue):
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(
serialized_example,
# Defaults are not specified since both keys are required.
features={
'label': tf.FixedLenFeature(
[], tf.int64),
'sequence_raw': tf.FixedLenFeature(
[], tf.string),
})
sequence = features['sequence_raw']
# preprocess
s_decode = tf.decode_raw(sequence, tf.int32)
s_decode.set_shape([FLAGS.embed_length])
# Convert label from a scalar uint8 tensor to an int32 scalar.
label = tf.cast(features['label'], tf.int32)
return s_decode, label
def process_state(self, state):
"""
Processing of state
State placeholders are tf.uint8 for fast transfer to GPU
Need to cast it to float32 for the rest of the tf graph.
Args:
state: node of tf graph of shape = (batch_size, height, width, nchannels)
of type tf.uint8.
if , values are between 0 and 255 -> 0 and 1
"""
state = tf.cast(state, tf.float32)
state /= self.config.high
return state
def _extract_images(filename, num_images):
"""Extract the images into a numpy array.
Args:
filename: The path to an MNIST images file.
num_images: The number of images in the file.
Returns:
A numpy array of shape [number_of_images, height, width, channels].
"""
print('Extracting images from: ', filename)
with gzip.open(filename) as bytestream:
bytestream.read(16)
buf = bytestream.read(
_IMAGE_SIZE * _IMAGE_SIZE * num_images * _NUM_CHANNELS)
data = np.frombuffer(buf, dtype=np.uint8)
data = data.reshape(num_images, _IMAGE_SIZE, _IMAGE_SIZE, _NUM_CHANNELS)
return data
def _extract_labels(filename, num_labels):
"""Extract the labels into a vector of int64 label IDs.
Args:
filename: The path to an MNIST labels file.
num_labels: The number of labels in the file.
Returns:
A numpy array of shape [number_of_labels]
"""
print('Extracting labels from: ', filename)
with gzip.open(filename) as bytestream:
bytestream.read(8)
buf = bytestream.read(1 * num_labels)
labels = np.frombuffer(buf, dtype=np.uint8).astype(np.int64)
return labels
def ternary_encoder(input_data):
"""Encoding and compressing the signs """
a = tf.sign(input_data) # -1, 0, 1
a = tf.add(a,1) # shift -1,0,1 to 0,1,2 (2'b00,2'b01,2'b10)
a = tf.reshape(a,[-1])
pad_size = 4 - tf.mod(tf.size(a), 4)
pad = tf.range(0.0, pad_size)
a = tf.concat([a, pad], 0)
a_split1, a_split2, a_split3, a_split4 = tf.split(a,4) # assume the size is dividable by 4
# encode 4 grads into 1 Byte
sum_1 = tf.add(a_split1, a_split2*4)
sum_2 = tf.add(a_split3*16, a_split4*64)
sum_all = tf.add(sum_1, sum_2)
encoded = tf.cast(sum_all, tf.uint8)
return encoded
def decode_raw(image_buffer, orig_height, orig_width, scope=None):
"""Decode a RAW string into one 3-D float image Tensor.
Args:
image_buffer: scalar string Tensor.
[orig_height, orig_width]: the size of original image
scope: Optional scope for op_scope.
Returns:
3-D float Tensor with values ranging from [0, 1).
"""
with tf.op_scope([image_buffer], scope, 'decode_raw'):
# Decode the string as an raw RGB.
image = tf.decode_raw(image_buffer, tf.uint8)
image = tf.reshape(image, tf.concat([orig_height,orig_width,[3]],0))
# After this point, all image pixels reside in [0,1)
# The various adjust_* ops all require this range for dtype float.
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
return image
def ternary_encoder(input_data):
"""Encoding and compressing the signs """
a = tf.sign(input_data) # -1, 0, 1
a = tf.add(a,1) # shift -1,0,1 to 0,1,2 (2'b00,2'b01,2'b10)
a = tf.reshape(a,[-1])
pad_size = 4 - tf.mod(tf.size(a), 4)
pad = tf.range(0.0, pad_size)
a = tf.concat([a, pad], 0)
a_split1, a_split2, a_split3, a_split4 = tf.split(a,4) # assume the size is dividable by 4
# encode 4 grads into 1 Byte
sum_1 = tf.add(a_split1, a_split2*4)
sum_2 = tf.add(a_split3*16, a_split4*64)
sum_all = tf.add(sum_1, sum_2)
encoded = tf.cast(sum_all, tf.uint8)
return encoded
def tf_random_aspect_resize(image, label, low_val=1.0, upper_val=1.5):
shape = tf.shape(image)
height = shape[0]
width = shape[1]
# 1~1.5
which_side = tf.to_float(tf.random_uniform([1]))[0]
multi_val = tf.to_float(tf.random_uniform([1]))[0] * (upper_val - low_val) + low_val
new_height = tf.cond(which_side > 0.5, lambda: tf.to_float(height), lambda: tf.to_float(height) * multi_val)
new_width = tf.cond(which_side <= 0.5, lambda: tf.to_float(width), lambda: tf.to_float(width) * multi_val)
new_height = tf.to_int32(new_height)
new_width = tf.to_int32(new_width)
image = tf.expand_dims(image, 0)
label = tf.expand_dims(label, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width], align_corners=False)
resized_image = tf.cast(resized_image, tf.uint8)
resized_label = tf.image.resize_nearest_neighbor(label, [new_height, new_width], align_corners=False)
resized_label = tf.cast(resized_label, tf.uint8)
resized_image = tf.squeeze(resized_image, 0)
resized_label = tf.squeeze(resized_label, 0)
return resized_image, resized_label
def tf_aspect_preserving_resize(image, label, smallest_side):
smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32)
shape = tf.shape(image)
height = shape[0]
width = shape[1]
new_height, new_width = _smallest_size_at_least(height, width, smallest_side)
new_height = tf.maximum(new_height, smallest_side)
new_width = tf.maximum(new_width, smallest_side)
image = tf.expand_dims(image, 0)
label = tf.expand_dims(label, 0)
resized_image = tf.image.resize_bilinear(image, [new_height, new_width], align_corners=False)
resized_image = tf.cast(resized_image, tf.uint8)
resized_image = tf.squeeze(resized_image, 0)
resized_label = tf.image.resize_nearest_neighbor(label, [new_height, new_width], align_corners=False)
resized_label = tf.cast(resized_label, tf.uint8)
resized_label = tf.squeeze(resized_label, 0)
return resized_image, resized_label
def _read_pngs_from(path):
"""Reads directory of images.
Args:
path: path to the directory
Returns:
A list of all images in the directory in the TF format (You need to call sess.run() or .eval() to get the value).
"""
images = []
png_files_path = glob.glob(os.path.join(path, '*.[pP][nN][gG]'))
for filename in png_files_path:
im = Image.open(filename)
im = np.asarray(im, np.uint8)
# get only images name, not path
image_name = filename.split('/')[-1].split('.')[0]
images.append([int(image_name), im])
images = sorted(images, key=lambda image: image[0])
images_only = [np.asarray(image[1], np.uint8) for image in images] # Use unint8 or you will be !!!
images_only = np.array(images_only)
#print(images_only.shape)
return images_only
def __init__(self):
# Create a single Session to run all image coding calls.
self._sess = tf.Session()
# Initializes function that converts PNG to JPEG data.
self._png_data = tf.placeholder(dtype=tf.string)
self._decode_png = tf.image.decode_png(self._png_data, channels=3)
# Initializes function that decodes RGB JPEG data.
self._jpeg_data = tf.placeholder(dtype=tf.string)
self._decode_jpeg = tf.image.decode_jpeg(self._jpeg_data, channels=3)
# Initializes function that encode RGB JPEG/PNG data.
self._image = tf.placeholder(dtype=tf.uint8)
self._encoded_png = tf.image.encode_png(self._image)
self._encoded_jpeg = tf.image.encode_jpeg(self._image)
tensorflow_backend.py 文件源码
项目:deep-learning-keras-projects
作者: jasmeetsb
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def _convert_string_dtype(dtype):
if dtype == 'float16':
return tf.float16
if dtype == 'float32':
return tf.float32
elif dtype == 'float64':
return tf.float64
elif dtype == 'int16':
return tf.int16
elif dtype == 'int32':
return tf.int32
elif dtype == 'int64':
return tf.int64
elif dtype == 'uint8':
return tf.int8
elif dtype == 'uint16':
return tf.uint16
else:
raise ValueError('Unsupported dtype:', dtype)