本文整理汇总了Python中numpy.frombuffer方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.frombuffer方法的具体用法?Python numpy.frombuffer怎么用?Python numpy.frombuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.frombuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_mnist
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def create_mnist(tfrecord_dir, mnist_dir):
print('Loading MNIST from "%s"' % mnist_dir)
import gzip
with gzip.open(os.path.join(mnist_dir, 'train-images-idx3-ubyte.gz'), 'rb') as file:
images = np.frombuffer(file.read(), np.uint8, offset=16)
with gzip.open(os.path.join(mnist_dir, 'train-labels-idx1-ubyte.gz'), 'rb') as file:
labels = np.frombuffer(file.read(), np.uint8, offset=8)
images = images.reshape(-1, 1, 28, 28)
images = np.pad(images, [(0,0), (0,0), (2,2), (2,2)], 'constant', constant_values=0)
assert images.shape == (60000, 1, 32, 32) and images.dtype == np.uint8
assert labels.shape == (60000,) and labels.dtype == np.uint8
assert np.min(images) == 0 and np.max(images) == 255
assert np.min(labels) == 0 and np.max(labels) == 9
onehot = np.zeros((labels.size, np.max(labels) + 1), dtype=np.float32)
onehot[np.arange(labels.size), labels] = 1.0
with TFRecordExporter(tfrecord_dir, images.shape[0]) as tfr:
order = tfr.choose_shuffled_order()
for idx in range(order.size):
tfr.add_image(images[order[idx]])
tfr.add_labels(onehot[order])
#----------------------------------------------------------------------------
示例2: create_mnistrgb
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def create_mnistrgb(tfrecord_dir, mnist_dir, num_images=1000000, random_seed=123):
print('Loading MNIST from "%s"' % mnist_dir)
import gzip
with gzip.open(os.path.join(mnist_dir, 'train-images-idx3-ubyte.gz'), 'rb') as file:
images = np.frombuffer(file.read(), np.uint8, offset=16)
images = images.reshape(-1, 28, 28)
images = np.pad(images, [(0,0), (2,2), (2,2)], 'constant', constant_values=0)
assert images.shape == (60000, 32, 32) and images.dtype == np.uint8
assert np.min(images) == 0 and np.max(images) == 255
with TFRecordExporter(tfrecord_dir, num_images) as tfr:
rnd = np.random.RandomState(random_seed)
for idx in range(num_images):
tfr.add_image(images[rnd.randint(images.shape[0], size=3)])
#----------------------------------------------------------------------------
示例3: ctypes2numpy_shared
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def ctypes2numpy_shared(cptr, shape):
"""Convert a ctypes pointer to a numpy array.
The resulting NumPy array shares the memory with the pointer.
Parameters
----------
cptr : ctypes.POINTER(mx_float)
pointer to the memory region
shape : tuple
Shape of target `NDArray`.
Returns
-------
out : numpy_array
A numpy array : numpy array.
"""
if not isinstance(cptr, ctypes.POINTER(mx_float)):
raise RuntimeError('expected float pointer')
size = 1
for s in shape:
size *= s
dbuffer = (mx_float * size).from_address(ctypes.addressof(cptr.contents))
return np.frombuffer(dbuffer, dtype=np.float32).reshape(shape)
示例4: _get_data
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def _get_data(self):
if self._train:
data, label = self._train_data, self._train_label
else:
data, label = self._test_data, self._test_label
namespace = 'gluon/dataset/'+self._namespace
data_file = download(_get_repo_file_url(namespace, data[0]),
path=self._root,
sha1_hash=data[1])
label_file = download(_get_repo_file_url(namespace, label[0]),
path=self._root,
sha1_hash=label[1])
with gzip.open(label_file, 'rb') as fin:
struct.unpack(">II", fin.read(8))
label = np.frombuffer(fin.read(), dtype=np.uint8).astype(np.int32)
with gzip.open(data_file, 'rb') as fin:
struct.unpack(">IIII", fin.read(16))
data = np.frombuffer(fin.read(), dtype=np.uint8)
data = data.reshape(len(label), 28, 28, 1)
self._data = nd.array(data, dtype=data.dtype)
self._label = label
示例5: _extract_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
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
示例6: _extract_labels
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
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
示例7: extract_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print('Extracting', filename)
with gzip.open(filename) 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
示例8: msgpackext_decode
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def msgpackext_decode(obj: Any) -> Any:
"""
Decodes a msgpack objects from a dictionary representation.
Parameters
----------
obj : Any
An encoded object, likely a dictionary.
Returns
-------
Any
The decoded form of the object.
"""
if b"_nd_" in obj:
arr = np.frombuffer(obj[b"data"], dtype=obj[b"dtype"])
if b"shape" in obj:
arr.shape = obj[b"shape"]
return arr
return obj
示例9: _extract_mnist_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def _extract_mnist_images(filename, num_images):
"""Extract images from an MNIST file 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].
"""
with gzip.open(filename) as bytestream:
bytestream.read(16)
buf = bytestream.read(_MNIST_IMAGE_SIZE * _MNIST_IMAGE_SIZE * num_images)
data = np.frombuffer(buf, dtype=np.uint8)
data = data.reshape(num_images, _MNIST_IMAGE_SIZE, _MNIST_IMAGE_SIZE, 1)
return data
示例10: unserialize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def unserialize(cls, data, block_size, back_window):
uncompressed = lz4.block.decompress(data)
nb_points = (
len(uncompressed) // cls._SERIALIZATION_TIMESTAMP_VALUE_LEN
)
try:
timestamps = numpy.frombuffer(uncompressed, dtype='<Q',
count=nb_points)
values = numpy.frombuffer(
uncompressed, dtype='<d',
offset=nb_points * cls._SERIALIZATION_TIMESTAMP_LEN)
except ValueError:
raise InvalidData
return cls.from_data(
numpy.cumsum(timestamps),
values,
block_size=block_size,
back_window=back_window)
示例11: extract_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
print('Extracting %s' % filename)
with gzip.open(filename) 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
示例12: extract_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def extract_images(filename):
"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
with gzip.open(filename) 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)
data = data.astype('float32') / 255.0
return data
示例13: set_shared_params
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def set_shared_params(a, b):
"""Set shared params (and persistent values) to a link.
Args:
a (chainer.Link): link whose params are to be replaced
b (dict): dict that consists of (param_name, multiprocessing.Array)
"""
assert isinstance(a, chainer.Link)
remaining_keys = set(b.keys())
for param_name, param in a.namedparams():
if param_name in b:
shared_param = b[param_name]
param.array = np.frombuffer(
shared_param, dtype=param.dtype).reshape(param.shape)
remaining_keys.remove(param_name)
for persistent_name, _ in chainerrl.misc.namedpersistent(a):
if persistent_name in b:
_set_persistent_values_recursively(
a, persistent_name, b[persistent_name])
remaining_keys.remove(persistent_name)
assert not remaining_keys
示例14: bytesToArray
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def bytesToArray(data, dt, shape):
#print(f"bytesToArray({len(data)}, {dt}, {shape}")
nelements = getNumElements(shape)
if not isVlen(dt):
# regular numpy from string
arr = np.frombuffer(data, dtype=dt)
else:
arr = np.zeros((nelements,), dtype=dt)
offset = 0
for index in range(nelements):
offset = readElement(data, offset, arr, index, dt)
arr = arr.reshape(shape)
# check that we can update the array if needed
# Note: this seems to have been required starting with numpuy v 1.17
# Setting the flag directly is not recommended. cf: https://github.com/numpy/numpy/issues/9440
if not arr.flags['WRITEABLE']:
arr_copy = arr.copy()
arr = arr_copy
return arr
示例15: read_vec_int
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import frombuffer [as 别名]
def read_vec_int(file_or_fd):
""" [int-vec] = read_vec_int(file_or_fd)
Read kaldi integer vector, ascii or binary input,
"""
fd = open_or_fd(file_or_fd)
binary = fd.read(2).decode()
if binary == '\0B': # binary flag
assert(fd.read(1).decode() == '\4'); # int-size
vec_size = np.frombuffer(fd.read(4), dtype='int32', count=1)[0] # vector dim
# Elements from int32 vector are sored in tuples: (sizeof(int32), value),
vec = np.frombuffer(fd.read(vec_size*5), dtype=[('size','int8'),('value','int32')], count=vec_size)
assert(vec[0]['size'] == 4) # int32 size,
ans = vec[:]['value'] # values are in 2nd column,
else: # ascii,
arr = (binary + fd.readline().decode()).strip().split()
try:
arr.remove('['); arr.remove(']') # optionally
except ValueError:
pass
ans = np.array(arr, dtype=int)
if fd is not file_or_fd : fd.close() # cleanup
return ans
# Writing,