本文整理汇总了Python中numpy.asarray方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.asarray方法的具体用法?Python numpy.asarray怎么用?Python numpy.asarray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.asarray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: transform
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def transform(self, sample):
if not self.model:
if not self.architecture.startswith("@"):
_, self.preprocess_input, self.model = \
get_imagenet_architecture(self.architecture, self.variant, self.size, self.alpha, self.output_layer)
else:
self.model = get_custom_architecture(self.architecture, self.trainings_dir, self.output_layer)
self.preprocess_input = generic_preprocess_input
x = sample.x
x = x.convert('RGB')
x = resize_image(x, self.image_size, self.image_size, 'antialias', 'aspect-fill')
#x = x.resize((self.image_size, self.image_size))
x = np.asarray(x)
x = np.expand_dims(x, axis=0)
x = self.preprocess_input(x)
features = self.model.predict(x)
features = features.flatten()
sample.x = features
sample.y = None
return sample
示例2: variable
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def variable(value, dtype=None, name=None):
'''Instantiates a variable and returns it.
# Arguments
value: Numpy array, initial value of the tensor.
dtype: Tensor type.
name: Optional name string for the tensor.
# Returns
A variable instance (with Keras metadata included).
'''
if dtype is None:
dtype = floatx()
if hasattr(value, 'tocoo'):
_assert_sparse_module()
variable = th_sparse_module.as_sparse_variable(value)
else:
value = np.asarray(value, dtype=dtype)
variable = theano.shared(value=value, name=name, strict=False)
variable._keras_shape = value.shape
variable._uses_learning_phase = False
return variable
示例3: cast_to_floatx
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def cast_to_floatx(x):
'''Cast a Numpy array to the default Keras float type.
# Arguments
x: Numpy array.
# Returns
The same Numpy array, cast to its new type.
# Example
```python
>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> arr = numpy.array([1.0, 2.0], dtype='float64')
>>> arr.dtype
dtype('float64')
>>> new_arr = K.cast_to_floatx(arr)
>>> new_arr
array([ 1., 2.], dtype=float32)
>>> new_arr.dtype
dtype('float32')
```
'''
return np.asarray(x, dtype=_FLOATX)
示例4: loadW2V
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def loadW2V(self,emb_path, type="bin"):
print("Loading W2V data...")
num_keys = 0
if type=="textgz":
# this seems faster than gensim non-binary load
for line in gzip.open(emb_path):
l = line.strip().split()
st=l[0].lower()
self.pre_emb[st]=np.asarray(l[1:])
num_keys=len(self.pre_emb)
if type=="text":
# this seems faster than gensim non-binary load
for line in open(emb_path):
l = line.strip().split()
st=l[0].lower()
self.pre_emb[st]=np.asarray(l[1:])
num_keys=len(self.pre_emb)
else:
self.pre_emb = Word2Vec.load_word2vec_format(emb_path,binary=True)
self.pre_emb.init_sims(replace=True)
num_keys=len(self.pre_emb.vocab)
print("loaded word2vec len ", num_keys)
gc.collect()
示例5: getTsvData
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def getTsvData(self, filepath):
print("Loading training data from "+filepath)
x1=[]
x2=[]
y=[]
# positive samples from file
for line in open(filepath):
l=line.strip().split("\t")
if len(l)<2:
continue
if random() > 0.5:
x1.append(l[0].lower())
x2.append(l[1].lower())
else:
x1.append(l[1].lower())
x2.append(l[0].lower())
y.append(int(l[2]))
return np.asarray(x1),np.asarray(x2),np.asarray(y)
示例6: batch_iter
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def batch_iter(self, data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
data = np.asarray(data)
print(data)
print(data.shape)
data_size = len(data)
num_batches_per_epoch = int(len(data)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
示例7: create_celeba
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def create_celeba(tfrecord_dir, celeba_dir, cx=89, cy=121):
print('Loading CelebA from "%s"' % celeba_dir)
glob_pattern = os.path.join(celeba_dir, 'img_align_celeba_png', '*.png')
image_filenames = sorted(glob.glob(glob_pattern))
expected_images = 202599
if len(image_filenames) != expected_images:
error('Expected to find %d images' % expected_images)
with TFRecordExporter(tfrecord_dir, len(image_filenames)) as tfr:
order = tfr.choose_shuffled_order()
for idx in range(order.size):
img = np.asarray(PIL.Image.open(image_filenames[order[idx]]))
assert img.shape == (218, 178, 3)
img = img[cy - 64 : cy + 64, cx - 64 : cx + 64]
img = img.transpose(2, 0, 1) # HWC => CHW
tfr.add_image(img)
#----------------------------------------------------------------------------
示例8: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def __init__(self, dataset, oversample_thr):
self.dataset = dataset
self.oversample_thr = oversample_thr
self.CLASSES = dataset.CLASSES
repeat_factors = self._get_repeat_factors(dataset, oversample_thr)
repeat_indices = []
for dataset_index, repeat_factor in enumerate(repeat_factors):
repeat_indices.extend([dataset_index] * math.ceil(repeat_factor))
self.repeat_indices = repeat_indices
flags = []
if hasattr(self.dataset, 'flag'):
for flag, repeat_factor in zip(self.dataset.flag, repeat_factors):
flags.extend([flag] * int(math.ceil(repeat_factor)))
assert len(flags) == len(repeat_indices)
self.flag = np.asarray(flags, dtype=np.uint8)
示例9: areas
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def areas(self):
"""Compute areas of masks.
This func is modified from
https://github.com/facebookresearch/detectron2/blob/ffff8acc35ea88ad1cb1806ab0f00b4c1c5dbfd9/detectron2/structures/masks.py#L387
Only works with Polygons, using the shoelace formula
Return:
ndarray: areas of each instance
""" # noqa: W501
area = []
for polygons_per_obj in self.masks:
area_per_obj = 0
for p in polygons_per_obj:
area_per_obj += self._polygon_area(p[0::2], p[1::2])
area.append(area_per_obj)
return np.asarray(area)
示例10: predict
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def predict(limit):
_limit = limit if limit > 0 else 5
td = TrainingData(LABEL_FILE, img_root=IMAGES_ROOT, mean_image_file=MEAN_IMAGE_FILE, image_property=IMAGE_PROP)
label_def = LabelingMachine.read_label_def(LABEL_DEF_FILE)
model = alex.Alex(len(label_def))
serializers.load_npz(MODEL_FILE, model)
i = 0
for arr, im in td.generate():
x = np.ndarray((1,) + arr.shape, arr.dtype)
x[0] = arr
x = chainer.Variable(np.asarray(x), volatile="on")
y = model.predict(x)
p = np.argmax(y.data)
print("predict {0}, actual {1}".format(label_def[p], label_def[im.label]))
im.image.show()
i += 1
if i >= _limit:
break
示例11: build_example
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def build_example(line):
parts = line.split(' ')
label = int(parts[0])
if label > 1:
label = 1
indice_list = []
items = parts[1:]
for item in items:
index = int(item.split(':')[0])
if index >= input_dim:
continue
indice_list += [[0, index]]
value_list = [1 for i in range(len(indice_list))]
shape_list = [1, input_dim]
indice_list = numpy.asarray(indice_list)
value_list = numpy.asarray(value_list)
shape_list = numpy.asarray(shape_list)
return indice_list, value_list, shape_list, label
# 一定要放在 with 里,不然 导出的 graph 不带变量和参数
示例12: color_overlap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def color_overlap(color1, *args):
'''
color_overlap(color1, color2...) yields the rgba value associated with overlaying color2 on top
of color1 followed by any additional colors (overlaid left to right). This respects alpha
values when calculating the results.
Note that colors may be lists of colors, in which case a matrix of RGBA values is yielded.
'''
args = list(args)
args.insert(0, color1)
rgba = np.asarray([0.5,0.5,0.5,0])
for c in args:
c = to_rgba(c)
a = c[...,3]
a0 = rgba[...,3]
if np.isclose(a0, 0).all(): rgba = np.ones(rgba.shape) * c
elif np.isclose(a, 0).all(): continue
else: rgba = times(a, c) + times(1-a, rgba)
return rgba
示例13: apply_cmap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def apply_cmap(zs, cmap, vmin=None, vmax=None, unit=None, logrescale=False):
'''
apply_cmap(z, cmap) applies the given cmap to the values in z; if vmin and/or vmax are passed,
they are used to scale z.
Note that this function can automatically rescale data into log-space if the colormap is a
neuropythy log-space colormap such as log_eccentricity. To enable this behaviour use the
optional argument logrescale=True.
'''
zs = pimms.mag(zs) if unit is None else pimms.mag(zs, unit)
zs = np.asarray(zs, dtype='float')
if pimms.is_str(cmap): cmap = matplotlib.cm.get_cmap(cmap)
if logrescale:
if vmin is None: vmin = np.log(np.nanmin(zs))
if vmax is None: vmax = np.log(np.nanmax(zs))
mn = np.exp(vmin)
u = zdivide(nanlog(zs + mn) - vmin, vmax - vmin, null=np.nan)
else:
if vmin is None: vmin = np.nanmin(zs)
if vmax is None: vmax = np.nanmax(zs)
u = zdivide(zs - vmin, vmax - vmin, null=np.nan)
u[np.isnan(u)] = -np.inf
return cmap(u)
示例14: images_from_filemap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def images_from_filemap(fmap):
'''
images_from_filemap(fmap) yields a persistent map of MRImages tracked by the given subject with
the given name and path; in freesurfer subjects these are renamed and converted from their
typical freesurfer filenames (such as 'ribbon') to forms that conform to the neuropythy naming
conventions (such as 'gray_mask'). To access data by their original names, use the filemap.
'''
imgmap = fmap.data_tree.image
def img_loader(k): return lambda:imgmap[k]
imgs = {k:img_loader(k) for k in six.iterkeys(imgmap)}
def _make_mask(val, eq=True):
rib = imgmap['ribbon']
img = np.asarray(rib.dataobj)
arr = (img == val) if eq else (img != val)
arr.setflags(write=False)
return type(rib)(arr, rib.affine, rib.header)
imgs['lh_gray_mask'] = lambda:_make_mask(3)
imgs['lh_white_mask'] = lambda:_make_mask(2)
imgs['rh_gray_mask'] = lambda:_make_mask(42)
imgs['rh_white_mask'] = lambda:_make_mask(41)
imgs['brain_mask'] = lambda:_make_mask(0, False)
# merge in with the typical images
return pimms.merge(fmap.data_tree.image, pimms.lazy_map(imgs))
示例15: image_dimensions
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import asarray [as 别名]
def image_dimensions(images):
'''
sub.image_dimensions is a tuple of the default size of an anatomical image for the given
subject.
'''
if images is None or len(images) == 0: return None
if pimms.is_lazy_map(images):
# look for an image that isn't lazy...
key = next((k for k in images.iterkeys() if not images.is_lazy(k)), None)
if key is None: key = next(images.iterkeys(), None)
else:
key = next(images.iterkeys(), None)
img = images[key]
if img is None: return None
if is_image(img): img = img.dataobj
return np.asarray(img).shape