def decode_from_tfrecords(filename,num_epoch=None):
filename_queue=tf.train.string_input_producer([filename],num_epochs=num_epoch)#???????????????????????????????????????
reader=tf.TFRecordReader()
_,serialized=reader.read(filename_queue)
example=tf.parse_single_example(serialized,features={
'height':tf.FixedLenFeature([],tf.int64),
'width':tf.FixedLenFeature([],tf.int64),
'nchannel':tf.FixedLenFeature([],tf.int64),
'image':tf.FixedLenFeature([],tf.string),
'label':tf.FixedLenFeature([],tf.int64)
})
label=tf.cast(example['label'], tf.int32)
image=tf.decode_raw(example['image'],tf.uint8)
image=tf.reshape(image,tf.pack([
tf.cast(example['height'], tf.int32),
tf.cast(example['width'], tf.int32),
tf.cast(example['nchannel'], tf.int32)]))
return image,label
python类uint8()的实例源码
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)
image = tf.image.decode_png(self._png_data, channels=3)
self._png_to_jpeg = tf.image.encode_jpeg(image, format='rgb', quality=100)
# Initializes function that decodes RGB JPEG data.
self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
self._decode_jpeg = tf.image.decode_jpeg(self._decode_jpeg_data, channels=3)
# Resize
self._resize = tf.expand_dims(self._decode_jpeg, 0)
self._resize = tf.image.resize_bilinear(self._resize, [FLAGS.new_height, FLAGS.new_width])
self._resize = tf.squeeze(self._resize)
self._resize = tf.cast(self._resize, tf.uint8)
self._new_jpeg = tf.image.encode_jpeg(self._resize, format='rgb', quality=FLAGS.jpeg_q,
progressive=False, optimize_size=True, chroma_downsampling=True)
def preprocess(self, inputs):
"""Perform preprocess.
Args:
inputs: raw input to the model.
Returns:
preprocessed input data.
"""
preprocess_fn = self.get_preprocess_fn()
assert inputs.ndim == 3 or inputs.ndim == 4, "invalid image format for preprocessing"
if inputs.ndim == 3:
inputs = np.expand_dims(inputs, axis=0)
with tf.Graph().as_default() as cur_g:
input_tensor = tf.convert_to_tensor(inputs, dtype=tf.uint8)
all_inputs = tf.unstack(input_tensor)
processed_inputs = []
for cur_input in all_inputs:
new_input = preprocess_fn(cur_input, self.net_params.input_img_height,
self.net_params.input_img_width)
processed_inputs.append(new_input)
new_inputs = tf.stack(processed_inputs)
with tf.Session(graph=cur_g) as sess:
processed_inputs = sess.run(new_inputs)
return processed_inputs
def read_and_decode(filename, batch_size):
# ???????????
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue) # ????????
features = tf.parse_single_example(
serialized_example,
features={
'label': tf.FixedLenFeature([], tf.int64),
'img_raw': tf.FixedLenFeature([], tf.string),
}
)
img = tf.decode_raw(features['img_raw'], tf.uint8)
print('xxxx: ', img.get_shape())
img = tf.reshape(img, [512, 144, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
image_batch, label_batch = tf.train.batch([img, label],
batch_size=batch_size,
num_threads=64,
capacity=2000)
return image_batch, tf.reshape(label_batch, [batch_size])
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
ML_Final_Project.py 文件源码
项目:apparent-age-gender-classification
作者: danielyou0230
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def read_and_decode(filename, img_size=128, depth=1):
if not filename.endswith('.tfrecords'):
print "Invalid file \"{:s}\"".format(filename)
return [], []
else:
data_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(data_queue)
features = tf.parse_single_example(serialized_example,
features={
'label' : tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [img_size, img_size, depth])
# Normalize the image
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
label_onehot = tf.stack(tf.one_hot(label, n_classes))
return img, label_onehot
#read_and_decode('test.tfrecords')
ML_Final_Project_LBP.py 文件源码
项目:apparent-age-gender-classification
作者: danielyou0230
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def read_and_decode(filename, img_size=128, depth=1):
if not filename.endswith('.tfrecords'):
print "Invalid file \"{:s}\"".format(filename)
return [], []
else:
data_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(data_queue)
features = tf.parse_single_example(serialized_example,
features={
'label' : tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [img_size, img_size, depth])
# Normalize the image
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
label_onehot = tf.stack(tf.one_hot(label, n_classes))
return img, label_onehot
#read_and_decode('test.tfrecords')
def read_and_decode(filename, img_size=128, depth=1):
if not filename.endswith('.tfrecords'):
print "Invalid file \"{:s}\"".format(filename)
return [], []
else:
data_queue = tf.train.string_input_producer([filename])
reader = tf.TFRecordReader()
_, serialized_example = reader.read(data_queue)
features = tf.parse_single_example(serialized_example,
features={
'label' : tf.FixedLenFeature([], tf.int64),
'img_raw' : tf.FixedLenFeature([], tf.string),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [img_size, img_size, depth])
# Normalize the image
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
label_onehot = tf.stack(tf.one_hot(label, n_classes))
return img, label_onehot
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 __init__(self):
self.history = StateProcessorSetting.history_length
self.dims = StateProcessorSetting.observation_dims
pass
#get current,prev frame, set by env
with tf.variable_scope('input', reuse =True):
self.cur_frame = tf.get_variable('cur_frame',dtype = tf.uint8)
self.prev_frame = tf.get_variable('prev_frame',dtype = tf.uint8)
with tf.variable_scope('input'):
maxOf2 = tf.maximum(tf.to_float(self.cur_frame), tf.to_float(self.prev_frame))
toGray = tf.expand_dims(tf.image.rgb_to_grayscale(maxOf2), 0)
resize = tf.image.resize_bilinear(toGray, self.dims, align_corners=None, name='observation')
self.observe = tf.div(tf.squeeze(resize), 255.0)
self.state = tf.get_variable(name = 'state', shape = [self.dims[0],self.dims[1],self.history], dtype = tf.float32,initializer = tf.constant_initializer(0.0),trainable = False)
self.to_stack = tf.expand_dims(self.observe, 2)
self.f3, self.f2, self.f1, _ = tf.split(2, self.history, self.state) # each is 84x84x1
self.concat = tf.concat(2, [self.to_stack, self.f3, self.f2, self.f1], name='concat')
self.updateState = self.state.assign(self.concat)
def get_data(img_folder, label_folder, train_fraction, img_size,
train_timesteps=4, test_timesteps=4, batch_size=1, sample_objects=False, n_threads=3,
in_memory=False, which_seqs=None, truncated_threshold=2., occluded_threshold=3., depth_folder=None,
storage_dtype=tf.uint8, mirror=False, reverse=False, bbox_scale=.5):
kitti = KittiTrackingParser(img_folder, label_folder, presence=True, id=False, cls=False,
truncated_threshold=truncated_threshold, occluded_threshold=occluded_threshold)
train, test = split_sequence_dict(kitti.data_dict, train_fraction)
def make_store(name, d, timesteps, n_threads, mirror=False, reverse=False):
s = KittiStore(d, timesteps, img_size, batch_size,
sample_objects=sample_objects, which_seqs=which_seqs, n_threads=n_threads,
in_memory=in_memory, depth_folder=depth_folder, storage_dtype=storage_dtype,
mirror=mirror, reverse=reverse, bbox_scale=bbox_scale, name=name)
return s
train_store = make_store('train', train, train_timesteps, n_threads, mirror, reverse)
test_store = make_store('test', test, test_timesteps, (n_threads // 2) + 1)
return train_store, train_store.get_minibatch(), test_store, test_store.get_minibatch()
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 read_and_augment_data(image_list, label_list, image_size, batch_size, max_nrof_epochs,
random_crop, random_flip, random_rotate, nrof_preprocess_threads, shuffle=True):
images = ops.convert_to_tensor(image_list, dtype=tf.string)
labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
num_epochs=max_nrof_epochs, shuffle=shuffle)
images_and_labels = []
for _ in range(nrof_preprocess_threads):
image, label = read_images_from_disk(input_queue)
if random_rotate:
image = tf.py_func(random_rotate_image, [image], tf.uint8)
if random_crop:
image = tf.random_crop(image, [image_size, image_size, 3])
else:
image = tf.image.resize_image_with_crop_or_pad(image, image_size, image_size)
if random_flip:
image = tf.image.random_flip_left_right(image)
#pylint: disable=no-member
image.set_shape((image_size, image_size, 3))
image = tf.image.per_image_standardization(image)
images_and_labels.append([image, label])
image_batch, label_batch = tf.train.batch_join(
images_and_labels, batch_size=batch_size,
capacity=4 * nrof_preprocess_threads * batch_size,
allow_smaller_final_batch=True)
return image_batch, label_batch
def to_rgb(img):
w, h = img.shape
ret = np.empty((w, h, 3), dtype=np.uint8)
ret[:, :, 0] = ret[:, :, 1] = ret[:, :, 2] = img
return ret
def get_video_matrix(self,
features,
feature_size,
max_frames,
max_quantized_value,
min_quantized_value):
"""Decodes features from an input string and quantizes it.
Args:
features: raw feature values
feature_size: length of each frame feature vector
max_frames: number of frames (rows) in the output feature_matrix
max_quantized_value: the maximum of the quantized value.
min_quantized_value: the minimum of the quantized value.
Returns:
feature_matrix: matrix of all frame-features
num_frames: number of frames in the sequence
"""
decoded_features = tf.reshape(
tf.cast(tf.decode_raw(features, tf.uint8), tf.float32),
[-1, feature_size])
num_frames = tf.minimum(tf.shape(decoded_features)[0], max_frames)
feature_matrix = utils.Dequantize(decoded_features,
max_quantized_value,
min_quantized_value)
feature_matrix = resize_axis(feature_matrix, 0, max_frames)
return feature_matrix, num_frames
def get_video_matrix(self,
features,
feature_size,
max_frames,
max_quantized_value,
min_quantized_value):
"""Decodes features from an input string and quantizes it.
Args:
features: raw feature values
feature_size: length of each frame feature vector
max_frames: number of frames (rows) in the output feature_matrix
max_quantized_value: the maximum of the quantized value.
min_quantized_value: the minimum of the quantized value.
Returns:
feature_matrix: matrix of all frame-features
num_frames: number of frames in the sequence
"""
decoded_features = tf.reshape(
tf.cast(tf.decode_raw(features, tf.uint8), tf.float32),
[-1, feature_size])
num_frames = tf.minimum(tf.shape(decoded_features)[0], max_frames)
feature_matrix = utils.Dequantize(decoded_features,
max_quantized_value,
min_quantized_value)
feature_matrix = resize_axis(feature_matrix, 0, max_frames)
return feature_matrix, num_frames
def get_video_matrix(self,
features,
feature_size,
max_frames,
max_quantized_value,
min_quantized_value):
"""Decodes features from an input string and quantizes it.
Args:
features: raw feature values
feature_size: length of each frame feature vector
max_frames: number of frames (rows) in the output feature_matrix
max_quantized_value: the maximum of the quantized value.
min_quantized_value: the minimum of the quantized value.
Returns:
feature_matrix: matrix of all frame-features
num_frames: number of frames in the sequence
"""
decoded_features = tf.reshape(
tf.cast(tf.decode_raw(features, tf.uint8), tf.float32),
[-1, feature_size])
num_frames = tf.minimum(tf.shape(decoded_features)[0], max_frames)
feature_matrix = utils.Dequantize(decoded_features,
max_quantized_value,
min_quantized_value)
feature_matrix = resize_axis(feature_matrix, 0, max_frames)
return feature_matrix, num_frames
def get_video_matrix(self,
features,
feature_size,
max_frames,
max_quantized_value,
min_quantized_value):
"""Decodes features from an input string and quantizes it.
Args:
features: raw feature values
feature_size: length of each frame feature vector
max_frames: number of frames (rows) in the output feature_matrix
max_quantized_value: the maximum of the quantized value.
min_quantized_value: the minimum of the quantized value.
Returns:
feature_matrix: matrix of all frame-features
num_frames: number of frames in the sequence
"""
decoded_features = tf.reshape(
tf.cast(tf.decode_raw(features, tf.uint8), tf.float32),
[-1, feature_size])
num_frames = tf.minimum(tf.shape(decoded_features)[0], max_frames)
feature_matrix = utils.Dequantize(decoded_features,
max_quantized_value,
min_quantized_value)
feature_matrix = resize_axis(feature_matrix, 0, max_frames)
return feature_matrix, num_frames