The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.
Example 1
def prepare_style(self, scale=1.0): """Called each phase of the optimization, process the style image according to the scale, then run it through the model to extract intermediate outputs (e.g. sem4_1) and turn them into patches. """ style_img = self.rescale_image(self.style_img_original, scale) self.style_img = self.model.prepare_image(style_img) style_map = self.rescale_image(self.style_map_original, scale) self.style_map = style_map.transpose((2, 0, 1))[np.newaxis].astype(np.float32) # Compile a function to run on the GPU to extract patches for all layers at once. layer_outputs = zip(self.style_layers, self.model.get_outputs('sem', self.style_layers)) extractor = self.compile([self.model.tensor_img, self.model.tensor_map], self.do_extract_patches(layer_outputs)) result = extractor(self.style_img, self.style_map) # Store all the style patches layer by layer, resized to match slice size and cast to 16-bit for size. self.style_data = {} for layer, *data in zip(self.style_layers, result[0::3], result[1::3], result[2::3]): patches = data[0] l = self.model.network['nn'+layer] l.num_filters = patches.shape[0] // args.slices self.style_data[layer] = [d[:l.num_filters*args.slices].astype(np.float16) for d in data]\ + [np.zeros((patches.shape[0],), dtype=np.float16)] print(' - Style layer {}: {} patches in {:,}kb.'.format(layer, patches.shape, patches.size//1000))
Example 2
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
Example 3
def test_tile_symmetry(self): ''' Make sure that tiles are symmetric ''' upload_file = open('data/Dixon2012-J1-NcoI-R1-filtered.100kb.multires.cool', 'rb') tileset = tm.Tileset.objects.create( datafile=dcfu.SimpleUploadedFile(upload_file.name, upload_file.read()), filetype='cooler', datatype='matrix', owner=self.user1, uuid='aa') ret = self.client.get('/api/v1/tiles/?d=aa.0.0.0') contents = json.loads(ret.content.decode('utf-8')) import base64 r = base64.decodestring(contents['aa.0.0.0']['dense'].encode('utf-8')) q = np.frombuffer(r, dtype=np.float16) q = q.reshape((256,256))
Example 4
def __init__(self, config, model_dir, ob_shape_list): self.model_dir = model_dir self.cnn_format = config.cnn_format self.memory_size = config.memory_size self.actions = np.empty(self.memory_size, dtype = np.uint8) self.rewards = np.empty(self.memory_size, dtype = np.integer) # print(self.memory_size, config.screen_height, config.screen_width) # self.screens = np.empty((self.memory_size, config.screen_height, config.screen_width), dtype = np.float16) self.screens = np.empty([self.memory_size] + ob_shape_list, dtype = np.float16) self.terminals = np.empty(self.memory_size, dtype = np.bool) self.history_length = config.history_length # self.dims = (config.screen_height, config.screen_width) self.dims = tuple(ob_shape_list) self.batch_size = config.batch_size self.count = 0 self.current = 0 # pre-allocate prestates and poststates for minibatch self.prestates = np.empty((self.batch_size, self.history_length) + self.dims, dtype = np.float16) self.poststates = np.empty((self.batch_size, self.history_length) + self.dims, dtype = np.float16) # self.prestates = np.empty((self.batch_size, self.history_length, self.dims), dtype = np.float16) # self.poststates = np.empty((self.batch_size, self.history_length, self.dims), dtype = np.float16)
Example 5
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.)
Example 6
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]
Example 7
def test_half_values(self): """Confirms a small number of known half values""" a = np.array([1.0, -1.0, 2.0, -2.0, 0.0999755859375, 0.333251953125, # 1/10, 1/3 65504, -65504, # Maximum magnitude 2.0**(-14), -2.0**(-14), # Minimum normal 2.0**(-24), -2.0**(-24), # Minimum subnormal 0, -1/1e1000, # Signed zeros np.inf, -np.inf]) b = np.array([0x3c00, 0xbc00, 0x4000, 0xc000, 0x2e66, 0x3555, 0x7bff, 0xfbff, 0x0400, 0x8400, 0x0001, 0x8001, 0x0000, 0x8000, 0x7c00, 0xfc00], dtype=uint16) b.dtype = float16 assert_equal(a, b)
Example 8
def test_half_ordering(self): """Make sure comparisons are working right""" # All non-NaN float16 values in reverse order a = self.nonan_f16[::-1].copy() # 32-bit float copy b = np.array(a, dtype=float32) # Should sort the same a.sort() b.sort() assert_equal(a, b) # Comparisons should work assert_((a[:-1] <= a[1:]).all()) assert_(not (a[:-1] > a[1:]).any()) assert_((a[1:] >= a[:-1]).all()) assert_(not (a[1:] < a[:-1]).any()) # All != except for +/-0 assert_equal(np.nonzero(a[:-1] < a[1:])[0].size, a.size-2) assert_equal(np.nonzero(a[1:] > a[:-1])[0].size, a.size-2)
Example 9
def test_half_coercion(self): """Test that half gets coerced properly with the other types""" a16 = np.array((1,), dtype=float16) a32 = np.array((1,), dtype=float32) b16 = float16(1) b32 = float32(1) assert_equal(np.power(a16, 2).dtype, float16) assert_equal(np.power(a16, 2.0).dtype, float16) assert_equal(np.power(a16, b16).dtype, float16) assert_equal(np.power(a16, b32).dtype, float16) assert_equal(np.power(a16, a16).dtype, float16) assert_equal(np.power(a16, a32).dtype, float32) assert_equal(np.power(b16, 2).dtype, float64) assert_equal(np.power(b16, 2.0).dtype, float64) assert_equal(np.power(b16, b16).dtype, float16) assert_equal(np.power(b16, b32).dtype, float32) assert_equal(np.power(b16, a16).dtype, float16) assert_equal(np.power(b16, a32).dtype, float32) assert_equal(np.power(a32, a16).dtype, float32) assert_equal(np.power(a32, b16).dtype, float32) assert_equal(np.power(b32, a16).dtype, float16) assert_equal(np.power(b32, b16).dtype, float32)
Example 10
def for_float_dtypes(name='dtype', no_float16=False): """Decorator that checks the fixture with all float dtypes. Args: name(str): Argument name to which specified dtypes are passed. no_float16(bool): If ``True``, ``numpy.float16`` is omitted from candidate dtypes. dtypes to be tested are ``numpy.float16`` (optional), ``numpy.float32``, and ``numpy.float64``. .. seealso:: :func:`cupy.testing.for_dtypes`, :func:`cupy.testing.for_all_dtypes` """ if no_float16: return for_dtypes(_regular_float_dtypes, name=name) else: return for_dtypes(_float_dtypes, name=name)
Example 11
def for_all_dtypes_combination(names=('dtyes',), no_float16=False, no_bool=False, full=None, no_complex=False): """Decorator that checks the fixture with a product set of all dtypes. Args: names(list of str): Argument names to which dtypes are passed. no_float16(bool): If ``True``, ``numpy.float16`` is omitted from candidate dtypes. no_bool(bool): If ``True``, ``numpy.bool_`` is omitted from candidate dtypes. full(bool): If ``True``, then all combinations of dtypes will be tested. Otherwise, the subset of combinations will be tested (see description in :func:`cupy.testing.for_dtypes_combination`). no_complex(bool): If, True, ``numpy.complex64`` and ``numpy.complex128`` are omitted from candidate dtypes. .. seealso:: :func:`cupy.testing.for_dtypes_combination` """ types = _make_all_dtypes(no_float16, no_bool, no_complex) return for_dtypes_combination(types, names, full)
Example 12
def convert_atoms(self, row): numbers = row.get('numbers') positions = row.get('positions').astype(self.floatX) pbc = row.get('pbc') cell = row.get('cell').astype(self.floatX) features = [numbers, positions, cell, pbc] for k in list(self.kvp.keys()): f = row[k] if np.isscalar(f): f = np.array([f]) if f.dtype in [np.float16, np.float32, np.float64]: f = f.astype(self.floatX) features.append(f) for k in list(self.data.keys()): f = np.array(row.data[k]) if np.isscalar(f): f = np.array([f]) if f.dtype in [np.float16, np.float32, np.float64]: f = f.astype(self.floatX) features.append(f) return features
Example 13
def numpy2bifrost(dtype): if dtype == np.int8: return _bf.BF_DTYPE_I8 elif dtype == np.int16: return _bf.BF_DTYPE_I16 elif dtype == np.int32: return _bf.BF_DTYPE_I32 elif dtype == np.uint8: return _bf.BF_DTYPE_U8 elif dtype == np.uint16: return _bf.BF_DTYPE_U16 elif dtype == np.uint32: return _bf.BF_DTYPE_U32 elif dtype == np.float16: return _bf.BF_DTYPE_F16 elif dtype == np.float32: return _bf.BF_DTYPE_F32 elif dtype == np.float64: return _bf.BF_DTYPE_F64 elif dtype == np.float128: return _bf.BF_DTYPE_F128 elif dtype == ci8: return _bf.BF_DTYPE_CI8 elif dtype == ci16: return _bf.BF_DTYPE_CI16 elif dtype == ci32: return _bf.BF_DTYPE_CI32 elif dtype == cf16: return _bf.BF_DTYPE_CF16 elif dtype == np.complex64: return _bf.BF_DTYPE_CF32 elif dtype == np.complex128: return _bf.BF_DTYPE_CF64 elif dtype == np.complex256: return _bf.BF_DTYPE_CF128 else: raise ValueError("Unsupported dtype: " + str(dtype))
Example 14
def numpy2string(dtype): if dtype == np.int8: return 'i8' elif dtype == np.int16: return 'i16' elif dtype == np.int32: return 'i32' elif dtype == np.int64: return 'i64' elif dtype == np.uint8: return 'u8' elif dtype == np.uint16: return 'u16' elif dtype == np.uint32: return 'u32' elif dtype == np.uint64: return 'u64' elif dtype == np.float16: return 'f16' elif dtype == np.float32: return 'f32' elif dtype == np.float64: return 'f64' elif dtype == np.float128: return 'f128' elif dtype == np.complex64: return 'cf32' elif dtype == np.complex128: return 'cf64' elif dtype == np.complex256: return 'cf128' else: raise TypeError("Unsupported dtype: " + str(dtype))
Example 15
def render_rgb(self, camera_idx): cameraPos = [(0.0, 0.75, 0.75), (0.75, 0.0, 0.75)][camera_idx] targetPos = (0, 0, 0.3) cameraUp = (0, 0, 1) nearVal, farVal = 1, 20 fov = 60 _w, _h, rgba, _depth, _objects = p.renderImage(self.render_width, self.render_height, cameraPos, targetPos, cameraUp, nearVal, farVal, fov) # convert from 1d uint8 array to (H,W,3) hacky hardcode whitened float16 array. # TODO: for storage concerns could just store this as uint8 (which it is) # and normalise 0->1 + whiten later. rgba_img = np.reshape(np.asarray(rgba, dtype=np.float16), (self.render_height, self.render_width, 4)) rgb_img = rgba_img[:,:,:3] # slice off alpha, always 1.0 rgb_img /= 255 return rgb_img
Example 16
def _typename(t): if t == np.float16: return 'float16' elif t == np.float32: return 'float32' elif t == np.float64: return 'float64' elif t == np.uint8: return 'uint8' elif t == np.uint16: return 'uint16' elif t == np.int16: return 'int16' elif t == np.int32: return 'int32' elif t == np.int64: return 'int64' else: raise TypeError('unknown type')
Example 17
def default(self, obj): # convert dates and numpy objects in a json serializable format if isinstance(obj, datetime): return obj.strftime('%Y-%m-%dT%H:%M:%SZ') elif isinstance(obj, date): return obj.strftime('%Y-%m-%d') elif type(obj) in (np.int_, np.intc, np.intp, np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64): return int(obj) elif type(obj) in (np.bool_,): return bool(obj) elif type(obj) in (np.float_, np.float16, np.float32, np.float64, np.complex_, np.complex64, np.complex128): return float(obj) # Let the base class default method raise the TypeError return json.JSONEncoder.default(self, obj)
Example 18
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.)
Example 19
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]
Example 20
def test_half_values(self): """Confirms a small number of known half values""" a = np.array([1.0, -1.0, 2.0, -2.0, 0.0999755859375, 0.333251953125, # 1/10, 1/3 65504, -65504, # Maximum magnitude 2.0**(-14), -2.0**(-14), # Minimum normal 2.0**(-24), -2.0**(-24), # Minimum subnormal 0, -1/1e1000, # Signed zeros np.inf, -np.inf]) b = np.array([0x3c00, 0xbc00, 0x4000, 0xc000, 0x2e66, 0x3555, 0x7bff, 0xfbff, 0x0400, 0x8400, 0x0001, 0x8001, 0x0000, 0x8000, 0x7c00, 0xfc00], dtype=uint16) b.dtype = float16 assert_equal(a, b)
Example 21
def test_half_ordering(self): """Make sure comparisons are working right""" # All non-NaN float16 values in reverse order a = self.nonan_f16[::-1].copy() # 32-bit float copy b = np.array(a, dtype=float32) # Should sort the same a.sort() b.sort() assert_equal(a, b) # Comparisons should work assert_((a[:-1] <= a[1:]).all()) assert_(not (a[:-1] > a[1:]).any()) assert_((a[1:] >= a[:-1]).all()) assert_(not (a[1:] < a[:-1]).any()) # All != except for +/-0 assert_equal(np.nonzero(a[:-1] < a[1:])[0].size, a.size-2) assert_equal(np.nonzero(a[1:] > a[:-1])[0].size, a.size-2)
Example 22
def test_half_coercion(self): """Test that half gets coerced properly with the other types""" a16 = np.array((1,), dtype=float16) a32 = np.array((1,), dtype=float32) b16 = float16(1) b32 = float32(1) assert_equal(np.power(a16, 2).dtype, float16) assert_equal(np.power(a16, 2.0).dtype, float16) assert_equal(np.power(a16, b16).dtype, float16) assert_equal(np.power(a16, b32).dtype, float16) assert_equal(np.power(a16, a16).dtype, float16) assert_equal(np.power(a16, a32).dtype, float32) assert_equal(np.power(b16, 2).dtype, float64) assert_equal(np.power(b16, 2.0).dtype, float64) assert_equal(np.power(b16, b16).dtype, float16) assert_equal(np.power(b16, b32).dtype, float32) assert_equal(np.power(b16, a16).dtype, float16) assert_equal(np.power(b16, a32).dtype, float32) assert_equal(np.power(a32, a16).dtype, float32) assert_equal(np.power(a32, b16).dtype, float32) assert_equal(np.power(b32, a16).dtype, float16) assert_equal(np.power(b32, b16).dtype, float32)
Example 23
def test_values_single(self): time_delay_sec = 0.005 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_sec=time_delay_sec) start_time = datetime.datetime.now() for _ in range(100): tdg.get_single() end_time = datetime.datetime.now() elapsed_timedelta = (end_time - start_time) assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53) tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_generator=ConstantValueGenerator(time_delay_sec, dtype=np.float16)) start_time = datetime.datetime.now() for _ in range(100): tdg.get_single() end_time = datetime.datetime.now() elapsed_timedelta = (end_time - start_time) assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)
Example 24
def test_values_batch(self): time_delay_sec = 0.0005 tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_sec=time_delay_sec) start_time = datetime.datetime.now() for _ in range(10): tdg.get_batch(100) end_time = datetime.datetime.now() elapsed_timedelta = (end_time - start_time) assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53) tdg = TimeDelayedGenerator(generator=ConstantValueGenerator(21, dtype=np.uint16), time_delay_generator=ConstantValueGenerator(time_delay_sec, dtype=np.float16)) start_time = datetime.datetime.now() for _ in range(10): tdg.get_batch(100) end_time = datetime.datetime.now() elapsed_timedelta = (end_time - start_time) assert datetime.timedelta(seconds=.47) <= elapsed_timedelta <= datetime.timedelta(seconds=.53)
Example 25
def _build_indicator(self, span, **kwds): """indicator???????????? Args: span: ?????????? """ def get_direction(val1, val2): if np.isnan(val1) or np.isnan(val1): return np.nan elif val1 < val2: return const.INDI_DIRECTION_UP # ??? elif val1 > val2: return const.INDI_DIRECTION_DOWN # ??? else: return const.INDI_DIRECTION_HR # ?? ma = MovingAverageIndicator(stock=self.stock, span=span) arr1 = ma.shifted(-1) # ??????????? arr2 = ma.data # ??????? return np.array([get_direction(a, b) for a, b in zip(arr1, arr2)], dtype=np.float16)
Example 26
def __init__(self, config, model_dir): self.model_dir = model_dir self.cnn_format = config.cnn_format self.memory_size = config.memory_size self.actions = np.empty(self.memory_size, dtype = np.uint8) self.rewards = np.empty(self.memory_size, dtype = np.integer) self.screens = np.empty((self.memory_size, config.screen_height, config.screen_width), dtype = np.float16) self.terminals = np.empty(self.memory_size, dtype = np.bool) self.history_length = config.history_length self.dims = (config.screen_height, config.screen_width) self.batch_size = config.batch_size self.count = 0 self.current = 0 # pre-allocate prestates and poststates for minibatch self.prestates = np.empty((self.batch_size, self.history_length) + self.dims, dtype = np.float16) self.poststates = np.empty((self.batch_size, self.history_length) + self.dims, dtype = np.float16)
Example 27
def for_float_dtypes(name='dtype', no_float16=False): """Decorator that checks the fixture with all float dtypes. Args: name(str): Argument name to which specified dtypes are passed. no_float16(bool): If, True, ``numpy.float16`` is omitted from candidate dtypes. dtypes to be tested are ``numpy.float16`` (optional), ``numpy.float32``, and ``numpy.float64``. .. seealso:: :func:`cupy.testing.for_dtypes`, :func:`cupy.testing.for_all_dtypes` """ if no_float16: return for_dtypes(_regular_float_dtypes, name=name) else: return for_dtypes(_float_dtypes, name=name)
Example 28
def for_all_dtypes_combination(names=['dtyes'], no_float16=False, no_bool=False, full=None): """Decorator that checks the fixture with a product set of all dtypes. Args: names(list of str): Argument names to which dtypes are passed. no_float16(bool): If ``True``, ``numpy.float16`` is omitted from candidate dtypes. no_bool(bool): If ``True``, ``numpy.bool_`` is omitted from candidate dtypes. full(bool): If ``True``, then all combinations of dtypes will be tested. Otherwise, the subset of combinations will be tested (see description in :func:`cupy.testing.for_dtypes_combination`). .. seealso:: :func:`cupy.testing.for_dtypes_combination` """ types = _make_all_dtypes(no_float16, no_bool) return for_dtypes_combination(types, names, full)
Example 29
def forward(self, x): xp = cuda.get_array_module(*x) if (xp != numpy and cuda.cudnn_enabled and self.use_cudnn and (_cudnn_version >= 3000 or x[0].dtype != numpy.float16)): oz_dtype = 'd' if x[0].dtype == 'd' else 'f' one = numpy.array(1, dtype=oz_dtype).ctypes zero = numpy.array(0, dtype=oz_dtype).ctypes handle = cudnn.get_handle() x_cube = x[0].reshape(x[0].shape[:2] + (-1, 1)) desc = cudnn.create_tensor_descriptor(x_cube) self.y = xp.empty_like(x[0]) libcudnn.softmaxForward( handle, _algorithm, _mode, one.data, desc.value, x_cube.data.ptr, zero.data, desc.value, self.y.data.ptr) else: self.y = x[0] - x[0].max(axis=1, keepdims=True) xp.exp(self.y, out=self.y) self.y /= self.y.sum(axis=1, keepdims=True) return self.y,
Example 30
def check_forward(self, op, x_data, gpu, positive): value = self.value if positive: value = numpy.abs(value) v = value if gpu: v = cuda.to_gpu(v) x = chainer.Variable(x_data) y = op(x, v) if self.dtype == numpy.float16: tol = 1e-3 else: tol = 1e-6 gradient_check.assert_allclose( op(self.x, value), y.data, atol=tol, rtol=tol)
Example 31
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}
Example 32
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}
Example 33
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}
Example 34
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}
Example 35
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}
Example 36
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
Example 37
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
Example 38
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')
Example 39
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
Example 40
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)
Example 41
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.)
Example 42
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]
Example 43
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
Example 44
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)
Example 45
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)
Example 46
def _normalize(self, image): return np.float16(image / 255.0)
Example 47
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
Example 48
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
Example 49
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)
Example 50
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)