def setUp(self):
if self.shape is None:
if self.dtype == numpy.float16:
self.x = numpy.array([[-5, 1]], dtype=self.dtype)
else:
self.x = numpy.array([[-1000, 1]], dtype=self.dtype)
self.t = numpy.array([0], dtype=numpy.int32)
else:
self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
out_shape = (self.shape[0],) + self.shape[2:]
self.t = numpy.random.randint(0, 3, out_shape).astype(numpy.int32)
if (self.ignore_index is not None and
len(self.ignore_index) <= self.t.ndim):
self.t[self.ignore_index] = -1
self.check_forward_options = {}
self.check_backward_options = {'atol': 1e-4, 'rtol': 1e-3}
if self.dtype == numpy.float16:
self.check_forward_options = {'atol': 1e-4, 'rtol': 1e-3}
self.check_backward_options = {'atol': 5e-2, 'rtol': 1e-1}
python类float16()的实例源码
def setUp(self):
self.N = 2
self.n_channels = 3
inh, inw = 2, 1
self.x = numpy.arange(
self.N * self.n_channels * inh * inw,
dtype=self.dtype).reshape(self.N, self.n_channels, inh, inw)
numpy.random.shuffle(self.x)
self.x = 2 * self.x / self.x.size - 1
self.ksize = 2
outh, outw = self.outsize or self.expected_outsize
self.gy = numpy.random.uniform(
-1, 1, (self.N, self.n_channels, outh, outw)).astype(self.dtype)
self.check_backward_options = {}
if self.dtype == numpy.float16:
self.check_backward_options = {'atol': 0.05, 'rtol': 0.05}
def setUp(self):
self.expander = (None, Ellipsis) + (None,) * self.ndim
self.aggr_axes = (0,) + tuple(six.moves.range(2, self.ndim + 2))
self.eps = 1e-5
self.gamma = numpy.random.uniform(.5, 1, (3,)).astype(self.dtype)
self.beta = numpy.random.uniform(-1, 1, (3,)).astype(self.dtype)
shape = (7, 3) + (2,) * self.ndim
self.x = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
self.gy = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
self.args = [self.x, self.gamma, self.beta]
self.mean = self.x.mean(axis=self.aggr_axes)
self.var = self.x.var(axis=self.aggr_axes) + self.eps
self.check_forward_optionss = {'atol': 1e-4, 'rtol': 1e-3}
self.check_backward_optionss = {'atol': 1e-4, 'rtol': 1e-3}
if self.dtype == numpy.float16:
self.check_forward_optionss = {'atol': 1e-3, 'rtol': 1e-2}
self.check_backward_optionss = {'atol': 5e-1, 'rtol': 1e-1}
def setUp(self):
self.gamma = numpy.random.uniform(.5, 1, (3,)).astype(self.dtype)
self.beta = numpy.random.uniform(-1, 1, (3,)).astype(self.dtype)
self.expander = (None, Ellipsis) + (None,) * self.ndim
shape = (7, 3) + (2,) * self.ndim
self.x = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
self.gy = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
self.eps = 1e-5
self.aggr_axes = (0,) + tuple(six.moves.range(2, self.ndim + 2))
self.mean = numpy.random.uniform(-1, 1, (3,)).astype(self.dtype)
self.var = numpy.random.uniform(
0.5, 1, (3,)).astype(self.dtype)
self.args = [self.x, self.gamma, self.beta, self.mean, self.var]
self.check_forward_optionss = {'atol': 1e-4, 'rtol': 1e-3}
self.check_backward_optionss = {'atol': 1e-4, 'rtol': 1e-3}
if self.dtype == numpy.float16:
self.check_forward_optionss = {'atol': 1e-3, 'rtol': 1e-2}
self.check_backward_optionss = {'atol': 5e-2, 'rtol': 1e-1}
def setUp(self):
self.c_prev1 = numpy.random.uniform(-1,
1, (3, 2, 4)).astype(self.dtype)
self.c_prev2 = numpy.random.uniform(-1,
1, (3, 2, 4)).astype(self.dtype)
self.x1 = numpy.random.uniform(-1, 1, (3, 8, 4)).astype(self.dtype)
self.x2 = numpy.random.uniform(-1, 1, (3, 8, 4)).astype(self.dtype)
self.gc = numpy.random.uniform(-1, 1, (3, 2, 4)).astype(self.dtype)
self.gh = numpy.random.uniform(-1, 1, (3, 2, 4)).astype(self.dtype)
self.check_forward_options = {}
self.check_backward_options = {'eps': 1e-2}
if self.dtype == numpy.float16:
self.check_forward_options = {'atol': 5e-4, 'rtol': 5e-3}
self.check_backward_options = {
'eps': 2 ** -4, 'atol': 5e-3, 'rtol': 5e-2}
def load_test_data(phone, dped_dir, IMAGE_SIZE):
test_directory_phone = dped_dir + str(phone) + '/test_data/patches/' + str(phone) + '/'
test_directory_dslr = dped_dir + str(phone) + '/test_data/patches/canon/'
NUM_TEST_IMAGES = len([name for name in os.listdir(test_directory_phone)
if os.path.isfile(os.path.join(test_directory_phone, name))])
test_data = np.zeros((NUM_TEST_IMAGES, IMAGE_SIZE))
test_answ = np.zeros((NUM_TEST_IMAGES, IMAGE_SIZE))
for i in range(0, NUM_TEST_IMAGES):
I = np.asarray(misc.imread(test_directory_phone + str(i) + '.jpg'))
I = np.float16(np.reshape(I, [1, IMAGE_SIZE]))/255
test_data[i, :] = I
I = np.asarray(misc.imread(test_directory_dslr + str(i) + '.jpg'))
I = np.float16(np.reshape(I, [1, IMAGE_SIZE]))/255
test_answ[i, :] = I
if i % 100 == 0:
print(str(round(i * 100 / NUM_TEST_IMAGES)) + "% done", end="\r")
return test_data, test_answ
def add_data_args(parser):
data = parser.add_argument_group('Data', 'the input images')
data.add_argument('--data-train', type=str, help='the training data')
data.add_argument('--data-val', type=str, help='the validation data')
data.add_argument('--rgb-mean', type=str, default='123.68,116.779,103.939',
help='a tuple of size 3 for the mean rgb')
data.add_argument('--pad-size', type=int, default=0,
help='padding the input image')
data.add_argument('--image-shape', type=str,
help='the image shape feed into the network, e.g. (3,224,224)')
data.add_argument('--num-classes', type=int, help='the number of classes')
data.add_argument('--num-examples', type=int, help='the number of training examples')
data.add_argument('--data-nthreads', type=int, default=4,
help='number of threads for data decoding')
data.add_argument('--benchmark', type=int, default=0,
help='if 1, then feed the network with synthetic data')
data.add_argument('--dtype', type=str, default='float32',
help='data type: float32 or float16')
return data
def json_encode(obj):
try:
serial = obj.to_json()
serial['classname'] = obj.__class__.__qualname__
return serial
except AttributeError:
pass
# Convert numpy types:
if type(obj) in [np.int8, np.int16, np.int32, np.int64]:
return int(obj)
elif type(obj) in [np.float16, np.float32, np.float64, np.float128]:
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, datetime.datetime):
return obj.isoformat()
raise TypeError('Type not serialisable')
def preprocess_image(self, im_data: np.ndarray) -> np.ndarray:
# mean = np.mean(im_data, axis=(0, 1))
# std = np.std(im_data, axis=(0, 1))
std = np.array([
62.00827863, 46.65453694, 24.7612776, 54.50255552,
13.48645938, 24.76103598, 46.52145521, 62.36207267,
61.54443128, 59.2848377, 85.72930307, 68.62678882,
448.43441827, 634.79572682, 567.21509273, 523.10079804,
530.42441592, 461.8304455, 486.95994727, 478.63768386],
dtype=np.float32)
mean = np.array([
413.62140162, 459.99189475, 325.6722122, 502.57730746,
294.6884949, 325.82117752, 460.0356966, 482.39001004,
413.79388678, 527.57681818, 678.22878001, 529.64198655,
4243.25847972, 4473.47956815, 4178.84648439, 3708.16482918,
2887.49330138, 2589.61786722, 2525.53347208, 2417.23798598],
dtype=np.float32)
scaled = ((im_data - mean) / std).astype(np.float16)
return scaled.transpose([2, 0, 1]) # torch order
def inside_triangle(point,triangles):
v0 = triangles[:,2]-triangles[:,0]
v1 = triangles[:,1]-triangles[:,0]
v2 = point-triangles[:,0]
dot00 = np.einsum('ij,ij->i',v0,v0)
dot01 = np.einsum('ij,ij->i',v0,v1)
dot02 = np.einsum('ij,ij->i',v0,v2)
dot11 = np.einsum('ij,ij->i',v1,v1)
dot12 = np.einsum('ij,ij->i',v1,v2)
invDenom = 1./(dot00 * dot11-dot01*dot01)
u = np.float16((dot11 * dot02 - dot01 * dot12)*invDenom)
v = np.float16((dot00 * dot12 - dot01 * dot02)*invDenom)
return (u>=0) & (v>=0) & (u+v<=1)
test_ufunc.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def test_sum(self):
for dt in (np.int, np.float16, np.float32, np.float64, np.longdouble):
for v in (0, 1, 2, 7, 8, 9, 15, 16, 19, 127,
128, 1024, 1235):
tgt = dt(v * (v + 1) / 2)
d = np.arange(1, v + 1, dtype=dt)
assert_almost_equal(np.sum(d), tgt)
assert_almost_equal(np.sum(d[::-1]), tgt)
d = np.ones(500, dtype=dt)
assert_almost_equal(np.sum(d[::2]), 250.)
assert_almost_equal(np.sum(d[1::2]), 250.)
assert_almost_equal(np.sum(d[::3]), 167.)
assert_almost_equal(np.sum(d[1::3]), 167.)
assert_almost_equal(np.sum(d[::-2]), 250.)
assert_almost_equal(np.sum(d[-1::-2]), 250.)
assert_almost_equal(np.sum(d[::-3]), 167.)
assert_almost_equal(np.sum(d[-1::-3]), 167.)
# sum with first reduction entry != 0
d = np.ones((1,), dtype=dt)
d += d
assert_almost_equal(d, 2.)
test_half.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def setUp(self):
# An array of all possible float16 values
self.all_f16 = np.arange(0x10000, dtype=uint16)
self.all_f16.dtype = float16
self.all_f32 = np.array(self.all_f16, dtype=float32)
self.all_f64 = np.array(self.all_f16, dtype=float64)
# An array of all non-NaN float16 values, in sorted order
self.nonan_f16 = np.concatenate(
(np.arange(0xfc00, 0x7fff, -1, dtype=uint16),
np.arange(0x0000, 0x7c01, 1, dtype=uint16)))
self.nonan_f16.dtype = float16
self.nonan_f32 = np.array(self.nonan_f16, dtype=float32)
self.nonan_f64 = np.array(self.nonan_f16, dtype=float64)
# An array of all finite float16 values, in sorted order
self.finite_f16 = self.nonan_f16[1:-1]
self.finite_f32 = self.nonan_f32[1:-1]
self.finite_f64 = self.nonan_f64[1:-1]
def _is_class_a_primitive(cls):
'''
Check if class is a number or string including numpy numbers
:param cls: any class
:return: True if class is a primitive class, else False
'''
primitives = [
np.float16, np.float32, np.float64, np.float128,
np.int8, np.int16, np.int32, np.int64,
bool, str, np.uint8, np.uint16, np.uint32, np.uint64,
int, float
]
return cls in primitives
def floatX(x):
"""
Convert `x` to the numpy type specified in `theano.config.floatX`.
"""
if theano.config.floatX == 'float16':
return numpy.float16(x)
elif theano.config.floatX == 'float32':
return numpy.float32(x)
else: # Theano's default float type is float64
print "Warning: lib.floatX using float64"
return numpy.float64(x)
def check_tile(self, z, x, y):
returned = json.loads(
self.client.get(
'/api/v1/tiles/?d={uuid}.{z}.{x}.{y}'.format(
uuid=self.cooler.uuid, x=x, y=y, z=z
)
).content.decode('utf-8')
)
r = base64.decodestring(returned[list(returned.keys())[0]]['dense'].encode('utf-8'))
q = np.frombuffer(r, dtype=np.float16)
with h5py.File(self.cooler.datafile.url) as f:
tileset_info = cch.get_info(self.cooler.datafile.url)
tileset_file = f
mat = [tileset_file, tileset_info]
zoom_level = z
BINS_PER_TILE = 256
hdf_for_resolution = tileset_file[str(zoom_level)]
resolution = (tileset_info['max_width'] / 2**zoom_level) / BINS_PER_TILE
t = tt.make_tiles(hdf_for_resolution, resolution, x, y)[(x,y)]
# test the base64 encoding
self.assertTrue(np.isclose(sum(q), sum(t), rtol=1e-3))
# make sure we're returning actual data
self.assertGreater(sum(q), 0)
def _normalize(self, image):
return np.float16(image / 255.0)
def csv_to_array(csv_filedir):
"""
save array to scv
"""
csv_table = pd.read_csv(csv_filedir)
sorted_table = csv_table.sort_values(by='VideoId')
sorted_table = sorted_table.reset_index(drop=True)
label_array = np.zeros((len(sorted_table), 4716), dtype=np.float16)
label_pair = sorted_table['LabelConfidencePairs']
videoid = sorted_table['VideoId']
for i in range(len(label_pair)):
example = label_pair[i].split(' ')
for j in range(0, len(example), 2):
label_array[i, int(example[j])] = float(example[j + 1])
return label_array, videoid
def test_scalar_format():
"""Test the str.format method with NumPy scalar types"""
tests = [('{0}', True, np.bool_),
('{0}', False, np.bool_),
('{0:d}', 130, np.uint8),
('{0:d}', 50000, np.uint16),
('{0:d}', 3000000000, np.uint32),
('{0:d}', 15000000000000000000, np.uint64),
('{0:d}', -120, np.int8),
('{0:d}', -30000, np.int16),
('{0:d}', -2000000000, np.int32),
('{0:d}', -7000000000000000000, np.int64),
('{0:g}', 1.5, np.float16),
('{0:g}', 1.5, np.float32),
('{0:g}', 1.5, np.float64),
('{0:g}', 1.5, np.longdouble)]
# Python 2.6 doesn't implement complex.__format__
if sys.version_info[:2] > (2, 6):
tests += [('{0:g}', 1.5+0.5j, np.complex64),
('{0:g}', 1.5+0.5j, np.complex128),
('{0:g}', 1.5+0.5j, np.clongdouble)]
for (fmat, val, valtype) in tests:
try:
assert_equal(fmat.format(val), fmat.format(valtype(val)),
"failed with val %s, type %s" % (val, valtype))
except ValueError as e:
assert_(False,
"format raised exception (fmt='%s', val=%s, type=%s, exc='%s')" %
(fmat, repr(val), repr(valtype), str(e)))
# Locale tests: scalar types formatting should be independent of the locale
def test_half_conversions(self):
"""Checks that all 16-bit values survive conversion
to/from 32-bit and 64-bit float"""
# Because the underlying routines preserve the NaN bits, every
# value is preserved when converting to/from other floats.
# Convert from float32 back to float16
b = np.array(self.all_f32, dtype=float16)
assert_equal(self.all_f16.view(dtype=uint16),
b.view(dtype=uint16))
# Convert from float64 back to float16
b = np.array(self.all_f64, dtype=float16)
assert_equal(self.all_f16.view(dtype=uint16),
b.view(dtype=uint16))
# Convert float16 to longdouble and back
# This doesn't necessarily preserve the extra NaN bits,
# so exclude NaNs.
a_ld = np.array(self.nonan_f16, dtype=np.longdouble)
b = np.array(a_ld, dtype=float16)
assert_equal(self.nonan_f16.view(dtype=uint16),
b.view(dtype=uint16))
# Check the range for which all integers can be represented
i_int = np.arange(-2048, 2049)
i_f16 = np.array(i_int, dtype=float16)
j = np.array(i_f16, dtype=np.int)
assert_equal(i_int, j)
def test_half_rounding(self):
"""Checks that rounding when converting to half is correct"""
a = np.array([2.0**-25 + 2.0**-35, # Rounds to minimum subnormal
2.0**-25, # Underflows to zero (nearest even mode)
2.0**-26, # Underflows to zero
1.0+2.0**-11 + 2.0**-16, # rounds to 1.0+2**(-10)
1.0+2.0**-11, # rounds to 1.0 (nearest even mode)
1.0+2.0**-12, # rounds to 1.0
65519, # rounds to 65504
65520], # rounds to inf
dtype=float64)
rounded = [2.0**-24,
0.0,
0.0,
1.0+2.0**(-10),
1.0,
1.0,
65504,
np.inf]
# Check float64->float16 rounding
b = np.array(a, dtype=float16)
assert_equal(b, rounded)
# Check float32->float16 rounding
a = np.array(a, dtype=float32)
b = np.array(a, dtype=float16)
assert_equal(b, rounded)