def test_specified_mixed16_dtype(self): with chainer.using_config('dtype', numpy.float64): self.assertEqual(chainer.get_dtype(chainer.mixed16), numpy.dtype(numpy.float16)) self.assertEqual( chainer.get_dtype(chainer.mixed16, map_mixed16=numpy.float32), numpy.dtype(numpy.float32))
def __init__(self, gt_file, image_size, root='.', dtype=None, transform_probability=0, image_mode='L', keep_aspect_ratio=False, resize_after_load=True): _check_pillow_availability() assert isinstance(gt_file, six.string_types), "paths must be a file name!" assert os.path.splitext(gt_file)[ -1] == ".json", "You have to supply gt information as json file!" if not isinstance(image_size, Size): image_size = Size(*image_size) with open(gt_file) as handle: self.gt_data = json.load(handle) self.root = root self.dtype = chainer.get_dtype(dtype) self.image_size = image_size self.transform_probability = transform_probability self.keep_aspect_ratio = keep_aspect_ratio self.resize_after_load = resize_after_load self.image_mode = image_mode self.augmentations = self.init_augmentations()
def __init__(self, size, decay=0.9, eps=2e-5, dtype=None, valid_test=False): super(ConditionalInstanceNormalization, self).__init__() self.valid_test = valid_test self.dtype = chainer.get_dtype(dtype) self.decay = decay self.eps = eps with self.init_scope(): self.instance_norm = InstanceNormalization(size, use_gamma=False, use_beta=False, decay=self.decay, eps=self.eps, dtype=self.dtype) # class 0 initial_gamma0 = initializers._get_initializer(1) initial_gamma0.dtype = self.dtype self.gamma0 = variable.Parameter(initial_gamma0, (1, size, 1, 1)) initial_beta0 = initializers._get_initializer(0) initial_beta0.dtype = self.dtype self.beta0 = variable.Parameter(initial_beta0, (1, size, 1, 1)) # class 1 initial_gamma1 = initializers._get_initializer(1) initial_gamma1.dtype = self.dtype self.gamma1 = variable.Parameter(initial_gamma1, (1, size, 1, 1)) initial_beta1 = initializers._get_initializer(0) initial_beta1.dtype = self.dtype self.beta1 = variable.Parameter(initial_beta1, (1, size, 1, 1))
def prepare(self, image): """Converts the given image to the np array for Inception-v3 Note that you have to call this method before ``forward`` because the pre-trained vgg model requires to resize the given image, covert the RGB to the BGR, and normalize to -1~1 range. Args: image (PIL.Image or np.ndarray): Input image. If an input is ``np.ndarray``, its shape must be ``(height, width)``, ``(height, width, channels)``, or ``(channels, height, width)``, and the order of the channels must be RGB. size (pair of ints): Size of converted images. If ``None``, the given image is not resized. Returns: np.ndarray: The converted output array. """ if isinstance(image, np.ndarray): if image.ndim == 3: if image.shape[0] == 1: image = image[0, :, :] elif image.shape[0] == 3: image = image.transpose((1, 2, 0)) image = Image.fromarray(image.astype(np.uint8)) image = image.convert('RGB') ratio = float(299) / min(image.size) image = image.resize( (ceil(ratio * image.size[0]), ceil(ratio * image.size[1]))) image = np.asarray(image, dtype=chainer.get_dtype()) image = center_crop(image, 299) image = preprocess_input(image) image = image[:, :, ::-1] image = image.transpose((2, 0, 1)) return image
def dali_converter(inputs, device=None): """Convert DALI arrays to Numpy/CuPy arrays""" outputs = [] for i in range(len(inputs)): x = inputs[i].as_tensor() if (isinstance(x, dali.backend_impl.TensorCPU)): x = np.array(x) if x.ndim == 2 and x.shape[1] == 1: x = x.squeeze(axis=1) if device is not None and device >= 0: x = cuda.to_gpu(x, device) elif (isinstance(x, dali.backend_impl.TensorGPU)): x_cupy = cuda.cupy.empty(shape=x.shape(), dtype=x.dtype()) # Synchronization is necessary here to avoid data corruption # because DALI and CuPy will use different CUDA streams. cuda.cupy.cuda.runtime.deviceSynchronize() # copy data from DALI array to CuPy array x.copy_to_external(ctypes.c_void_p(x_cupy.data.ptr)) cuda.cupy.cuda.runtime.deviceSynchronize() x = x_cupy.astype(chainer.get_dtype()) if device is not None and device < 0: x = cuda.to_cpu(x) else: raise ValueError('Unexpected object') outputs.append(x) return tuple(outputs)
def __init__(self, zipfilename, dtype=None): self._zipfilename = zipfilename self._zf = zipfile.ZipFile(zipfilename) self._zf_pid = os.getpid() self._dtype = chainer.get_dtype(dtype) self._paths = [x for x in self._zf.namelist() if not x.endswith('/')] self._lock = threading.Lock()
def __init__(self, size, decay=0.9, eps=2e-5, dtype=None, valid_test=False, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None): super(InstanceNormalization, self).__init__() self.valid_test = valid_test self.avg_mean = numpy.zeros(size, dtype=dtype) self.avg_var = numpy.zeros(size, dtype=dtype) self.N = 0 self.register_persistent('avg_mean') self.register_persistent('avg_var') self.register_persistent('N') self.decay = decay self.eps = eps self.dtype = chainer.get_dtype(dtype) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = self.dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = self.dtype self.beta = variable.Parameter(initial_beta, size)
def generate_array(initializer, shape, xp, dtype=None): """Return initialized array. The algorithms used to make the new values depend on the concrete derived classes. If the initializer has the ``dtype`` attribute, it is used to construct the array. Otherwise, ``chainer.config.dtype`` is used instead. See :ref:`configuration` for the dtype config. Args: initializer: A callable object that takes :ref:`ndarray` and edits its value. shape (tuple): Shape of a return array. xp (module): :mod:`cupy` or :mod:`numpy`. dtype: Dtype specifier. If omitted, ``initializer.dtype`` is used. Returns: :ref:`ndarray`: An initialized array. """ dtype_attr = getattr(initializer, 'dtype', None) if dtype is not None and dtype_attr is not None \ and numpy.dtype(dtype) != numpy.dtype(dtype_attr): raise ValueError('dtype mismatch: {} != {}'.format(dtype, dtype_attr)) if dtype is None: dtype = dtype_attr dtype = chainer.get_dtype(dtype) array = xp.empty(shape, dtype=dtype) initializer(array) return array
def build_iou_labels(self, shape, num_word_indicator, xp): base_array = xp.empty(shape, dtype=chainer.get_dtype()) if xp == numpy: labels = self.fill_iou_array_cpu(base_array, num_word_indicator) else: labels = self.fill_iou_array_gpu(base_array, num_word_indicator) return xp.ravel(labels)
def __init__(self, in_size, out_size, pool_size, initialW=None, initial_bias=0): super(Maxout, self).__init__() linear_out_size = out_size * pool_size if initialW is not None: initialW = initialW.reshape(linear_out_size, in_size) if initial_bias is not None: if numpy.isscalar(initial_bias): dtype = chainer.get_dtype() initial_bias = numpy.full((linear_out_size, ), initial_bias, dtype=dtype) elif isinstance(initial_bias, (numpy.ndarray, cuda.ndarray)): initial_bias = initial_bias.reshape(linear_out_size) else: raise ValueError( 'initial bias must be float, ndarray, or None') with self.init_scope(): self.linear = linear.Linear(in_size, linear_out_size, nobias=initial_bias is None, initialW=initialW, initial_bias=initial_bias) self.out_size = out_size self.pool_size = pool_size
def __call__(self, inputs, device=None): """Convert DALI arrays to Numpy/CuPy arrays""" xp = cuda.get_array_module(self.perturbation) if xp is not cuda.cupy: self.perturbation = cuda.to_gpu(self.perturbation, device) outputs = [] for i in range(len(inputs)): x = inputs[i].as_tensor() if (isinstance(x, dali.backend_impl.TensorCPU)): x = np.array(x) if x.ndim == 2 and x.shape[1] == 1: x = x.squeeze(axis=1) if device is not None and device >= 0: x = cuda.to_gpu(x, device) elif (isinstance(x, dali.backend_impl.TensorGPU)): x_cupy = cuda.cupy.empty(shape=x.shape(), dtype=x.dtype()) # Synchronization is necessary here to avoid data corruption # because DALI and CuPy will use different CUDA streams. cuda.cupy.cuda.runtime.deviceSynchronize() # copy data from DALI array to CuPy array x.copy_to_external(ctypes.c_void_p(x_cupy.data.ptr)) cuda.cupy.cuda.runtime.deviceSynchronize() x = x_cupy.astype(chainer.get_dtype()) if self.perturbation is not None: x = x - self.perturbation if device is not None and device < 0: x = cuda.to_cpu(x) else: raise ValueError('Unexpected object') outputs.append(x) return tuple(outputs)
def generate_array(initializer, shape, xp, dtype=None): """Return initialized array. The algorithms used to make the new values depend on the concrete derived classes. If the initializer has the ``dtype`` attribute, it is used to construct the array. Otherwise, ``chainer.config.dtype`` is used instead. See :ref:`configuration` for the dtype config. Args: initializer: A callable object that takes :class:`numpy.ndarray` or :class:`cupy.ndarray` and edits its value. shape (tuple): Shape of a return array. xp (module): :mod:`cupy` or :mod:`numpy`. dtype: Dtype specifier. If omitted, ``initializer.dtype`` is used. Returns: numpy.ndarray or cupy.ndarray: An initialized array. """ dtype_attr = getattr(initializer, 'dtype', None) if dtype is not None and dtype_attr is not None \ and numpy.dtype(dtype) != numpy.dtype(dtype_attr): raise ValueError( 'dtype mismatch: {} != {}'.format(dtype, dtype_attr)) if dtype is None: dtype = dtype_attr dtype = chainer.get_dtype(dtype) array = xp.empty(shape, dtype=dtype) initializer(array) return array
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'): """Update a Q-function with given samples and a target Q-function.""" dtype = chainer.get_dtype() xp = Q.xp obs = xp.asarray([sample[0] for sample in samples], dtype=dtype) action = xp.asarray([sample[1] for sample in samples], dtype=np.int32) reward = xp.asarray([sample[2] for sample in samples], dtype=dtype) done = xp.asarray([sample[3] for sample in samples], dtype=dtype) obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype) # Predicted values: Q(s,a) y = F.select_item(Q(obs), action) # Target values: r + gamma * max_b Q(s',b) with chainer.no_backprop_mode(): if target_type == 'dqn': next_q = F.max(target_Q(obs_next), axis=1) elif target_type == 'double_dqn': next_q = F.select_item(target_Q(obs_next), F.argmax(Q(obs_next), axis=1)) else: raise ValueError('Unsupported target_type: {}'.format(target_type)) target = reward + gamma * (1 - done) * next_q loss = mean_clipped_loss(y, target) Q.cleargrads() loss.backward() opt.update()
def update(Q, target_Q, policy, target_policy, opt_Q, opt_policy, samples, gamma=0.99): """Update a Q-function and a policy.""" dtype = chainer.get_dtype() xp = Q.xp obs = xp.asarray([sample[0] for sample in samples], dtype=dtype) action = xp.asarray([sample[1] for sample in samples], dtype=dtype) reward = xp.asarray([sample[2] for sample in samples], dtype=dtype) done = xp.asarray([sample[3] for sample in samples], dtype=dtype) obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype) def update_Q(): # Predicted values: Q(s,a) y = F.squeeze(Q(obs, action), axis=1) # Target values: r + gamma * Q(s,policy(s)) with chainer.no_backprop_mode(): next_q = F.squeeze(target_Q(obs_next, target_policy(obs_next)), axis=1) target = reward + gamma * (1 - done) * next_q loss = F.mean_squared_error(y, target) Q.cleargrads() loss.backward() opt_Q.update() def update_policy(): # Maximize Q(s,policy(s)) q = Q(obs, policy(obs)) q = q[:] # Avoid https://github.com/chainer/chainer/issues/2744 loss = - F.mean(q) policy.cleargrads() loss.backward() opt_policy.update() update_Q() update_policy()
def get_greedy_action(Q, obs): """Get a greedy action wrt a given Q-function.""" dtype = chainer.get_dtype() obs = Q.xp.asarray(obs[None], dtype=dtype) with chainer.no_backprop_mode(): q = Q(obs).array[0] return int(q.argmax())
def get_action(policy, obs): """Get an action by evaluating a given policy.""" dtype = chainer.get_dtype() obs = policy.xp.asarray(obs[None], dtype=dtype) with chainer.no_backprop_mode(): action = policy(obs).array[0] return chainer.backends.cuda.to_cpu(action)
def setup_data(self): dtype = chainer.get_dtype() self.x = numpy.random.uniform( -1, 1, (10, self.in_channels, 5, 5)).astype(dtype) self.l = links.InceptionBN(self.in_channels, self.out1, self.proj3, self.out3, self.proj33, self.out33, self.pooltype, self.proj_pool, self.stride)
def __init__(self, paths, root='.', dtype=None): _check_pillow_availability() if isinstance(paths, six.string_types): with open(paths) as paths_file: paths = [path.strip() for path in paths_file] self._paths = paths self._root = root self._dtype = chainer.get_dtype(dtype)
def __call__(self, img): dtype = get_dtype(None) img = img.astype(dtype) img *= 1.0 / 255.0 img -= self.mean img /= self.std return img
def __init__(self, n_latent): super(Prior, self).__init__() dtype = chainer.get_dtype() self.loc = np.zeros(n_latent, dtype) self.scale = np.ones(n_latent, dtype) self.register_persistent('loc') self.register_persistent('scale')
def generate_params(self): highprec_dtype = chainer.get_dtype(self.dtype, map_mixed16=numpy.float32) initial_gamma = numpy.random.uniform( -1, 1, (self.shape[1], )).astype(highprec_dtype) initial_beta = numpy.random.uniform( -1, 1, (self.shape[1], )).astype(highprec_dtype) return initial_gamma, initial_beta
def get_mnist(withlabel=True, ndim=1, scale=1., dtype=None, label_dtype=numpy.int32, rgb_format=False, data=None): """Gets the MNIST dataset. `MNIST <http://yann.lecun.com/exdb/mnist/>`_ is a set of hand-written digits represented by grey-scale 28x28 images. In the original images, each pixel is represented by one-byte unsigned integer. This function scales the pixels to floating point values in the interval ``[0, scale]``. This function returns the training set and the test set of the official MNIST dataset. If ``withlabel`` is ``True``, each dataset consists of tuples of images and labels, otherwise it only consists of images. Args: withlabel (bool): If ``True``, it returns datasets with labels. In this case, each example is a tuple of an image and a label. Otherwise, the datasets only contain images. ndim (int): Number of dimensions of each image. The shape of each image is determined depending on ``ndim`` as follows: - ``ndim == 1``: the shape is ``(784,)`` - ``ndim == 2``: the shape is ``(28, 28)`` - ``ndim == 3``: the shape is ``(1, 28, 28)`` scale (float): Pixel value scale. If it is 1 (default), pixels are scaled to the interval ``[0, 1]``. dtype: Data type of resulting image arrays. ``chainer.config.dtype`` is used by default (see :ref:`configuration`). label_dtype: Data type of the labels. rgb_format (bool): if ``ndim == 3`` and ``rgb_format`` is ``True``, the image will be converted to rgb format by duplicating the channels so the image shape is (3, 28, 28). Default is ``False``. Returns: A tuple of two datasets. If ``withlabel`` is ``True``, both datasets are :class:`~chainer.datasets.TupleDataset` instances. Otherwise, both datasets are arrays of images. """ dtype = chainer.get_dtype(dtype) train_raw = data.astype(numpy.float64) mean_value = numpy.mean(train_raw, axis=1) std_value = numpy.std(train_raw, axis=1) train_raw = numpy.divide(train_raw.T - mean_value, std_value) train_raw = train_raw.T #train_raw *= scale / max_value #train_raw[train_raw < 0] = 0.0 size_data = len(train_raw[:, 1]) i = range(math.ceil(size_data / 4)) test_raw = train_raw[i, :] return train_raw, test_raw
def forward(self, *inputs): batch = len(inputs) // 6 lefts = inputs[0:batch] rights = inputs[batch:batch * 2] dests = inputs[batch * 2:batch * 3] labels = inputs[batch * 3:batch * 4] sequences = inputs[batch * 4:batch * 5] leaf_labels = inputs[batch * 5:batch * 6] inds = numpy.argsort([-len(l) for l in lefts]) # Sort all arrays in descending order and transpose them lefts = F.transpose_sequence([lefts[i] for i in inds]) rights = F.transpose_sequence([rights[i] for i in inds]) dests = F.transpose_sequence([dests[i] for i in inds]) labels = F.transpose_sequence([labels[i] for i in inds]) sequences = F.transpose_sequence([sequences[i] for i in inds]) leaf_labels = F.transpose_sequence([leaf_labels[i] for i in inds]) batch = len(inds) maxlen = len(sequences) loss = 0 count = 0 correct = 0 dtype = chainer.get_dtype() stack = self.xp.zeros((batch, maxlen * 2, self.n_units), dtype) for i, (word, label) in enumerate(zip(sequences, leaf_labels)): batch = word.shape[0] es = self.leaf(word) ds = self.xp.full((batch, ), i, self.xp.int32) y = self.label(es) loss += F.softmax_cross_entropy(y, label, normalize=False) * batch count += batch predict = self.xp.argmax(y.array, axis=1) correct += (predict == label.array).sum() stack = thin_stack.thin_stack_set(stack, ds, es) for left, right, dest, label in zip(lefts, rights, dests, labels): l, stack = thin_stack.thin_stack_get(stack, left) r, stack = thin_stack.thin_stack_get(stack, right) o = self.node(l, r) y = self.label(o) batch = l.shape[0] loss += F.softmax_cross_entropy(y, label, normalize=False) * batch count += batch predict = self.xp.argmax(y.array, axis=1) correct += (predict == label.array).sum() stack = thin_stack.thin_stack_set(stack, dest, o) loss /= count reporter.report({'loss': loss}, self) reporter.report({'total': count}, self) reporter.report({'correct': correct}, self) return loss
def setup_data(self): dtype = chainer.get_dtype() self.x = numpy.random.uniform( -1, 1, (10, self.in_channels, 5, 5) ).astype(dtype) self.l = links.InceptionBN( self.in_channels, self.out1, self.proj3, self.out3, self.proj33, self.out33, self.pooltype, self.proj_pool, self.stride)
def __init__(self, in_size, tree, dtype=None): # This function object is copied on every forward computation. super(BinaryHierarchicalSoftmax, self).__init__() dtype = chainer.get_dtype(dtype) self._func = BinaryHierarchicalSoftmaxFunction(tree, dtype) with self.init_scope(): self.W = variable.Parameter(uniform.Uniform(1), (self._func.parser_size, in_size))
def __init__(self, mean, crop_size): self.mean = mean self.crop_size = crop_size ch_mean = np.average(mean, axis=(1, 2)) perturbation = (mean - ch_mean.reshape(3, 1, 1)) / 255.0 perturbation = perturbation[:3, :crop_size, :crop_size].astype( chainer.get_dtype()) self.perturbation = perturbation.reshape(1, 3, crop_size, crop_size)
def _preprocess(self, img): dtype = chainer.get_dtype(None) img = img.transpose(2, 0, 1) img = img.astype(dtype) img *= 1.0 / 255.0 img -= self.mean img /= self.std return img
def build_dataset(self, args): return PredictionDataset( args.eval_gt, args.image_size, root=os.path.dirname(args.eval_gt), dtype=chainer.get_dtype(), image_mode=args.image_mode, max_size=2000, )
def __init__(self, lr=_default_hyperparam.lr, momentum=_default_hyperparam.momentum, rule=None): super(LpMomentumSGD, self).__init__(lr=lr, momentum=momentum) # NOTE: you should be in float16 to use this optimizer. assert chainer.get_dtype() == np.float16 # this will distinguish deterministic and stochastic rules. self._rule = rule
def _prepare(self, img): img = img.astype(np.float32) img = transforms.resize(img, (self.insize, self.insize)) img -= self.mean # NOTE: chainer.get_dtype will return float16 if the # global_config.dtype is chainer.mixed16 img = img.astype(chainer.get_dtype()) return img
def prepare_image(self, image: ImageClass) -> numpy.ndarray: image = image.convert('L').convert('RGB') if not self.needs_patches: image = image.resize(self.prediction_helper.image_size) image = numpy.array(image, dtype=chainer.get_dtype()).transpose(2, 0, 1) if self.needs_patches: image = self.prediction_helper.resize_image(image) image /= 255 return image
def test_image(base_dir, file_name): try: with Image.open(os.path.join(base_dir, file_name)) as the_image: the_image.convert('L').convert("RGB") the_image = the_image.resize((200, 64), Image.LANCZOS) image = np.array(the_image, chainer.get_dtype()) assert image.max() > 0 return True except Exception: return False
def __call__(self, in_data): # There are five data augmentation steps # 1. Color augmentation # 2. Random expansion # 3. Random cropping # 4. Resizing with random interpolation # 5. Random horizontal flipping img, bbox, label = in_data # 1. Color augmentation img = random_distort(img) # 2. Random expansion if np.random.randint(2): img, param = transforms.random_expand(img, fill=self.mean, return_param=True) bbox = transforms.translate_bbox(bbox, y_offset=param['y_offset'], x_offset=param['x_offset']) # 3. Random cropping img, param = random_crop_with_bbox_constraints(img, bbox, return_param=True) bbox, param = transforms.crop_bbox(bbox, y_slice=param['y_slice'], x_slice=param['x_slice'], allow_outside_center=False, return_param=True) label = label[param['index']] # 4. Resizing with random interpolatation _, H, W = img.shape img = resize_with_random_interpolation(img, (self.size, self.size)) bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size)) # 5. Random horizontal flipping img, params = transforms.random_flip(img, x_random=True, return_param=True) bbox = transforms.flip_bbox(bbox, (self.size, self.size), x_flip=params['x_flip']) # Preparation for SSD network img -= self.mean mb_loc, mb_label = self.coder.encode(bbox, label) dtype = chainer.get_dtype(self.dtype) if img.dtype != dtype: img = img.astype(dtype) return img, mb_loc, mb_label
def generate_array(initializer, shape, xp, dtype=None, device=None): # type: (types.AbstractInitializer, types.ShapeSpec, types.Xp, types.DTypeSpec, types.DeviceSpec) -> types.NdArray # NOQA """Return initialized array. The algorithms used to make the new values depend on the concrete derived classes. If the initializer has the ``dtype`` attribute, it is used to construct the array. Otherwise, ``chainer.config.dtype`` is used instead. See :ref:`configuration` for the dtype config. Args: initializer: A callable object that takes :ref:`ndarray` and edits its value. shape (int or tuple of int): Shape of the initialized array. xp (module): :mod:`cupy`, :mod:`numpy`, or :mod:`chainerx`. dtype: Dtype specifier. If omitted, ``initializer.dtype`` is used. device: Target device specifier. If omitted, the current device is used for :mod:`cupy`, and the default device is used for :mod:`chainerx`. Returns: :ref:`ndarray`: An initialized array. """ dtype_attr = getattr(initializer, 'dtype', None) if dtype is not None and dtype_attr is not None \ and numpy.dtype(dtype) != numpy.dtype(dtype_attr): raise ValueError('dtype mismatch: {} != {}'.format(dtype, dtype_attr)) if dtype is None: dtype = dtype_attr dtype = chainer.get_dtype(dtype) if device is None: backend_device = backend._guess_device_from_array_module(xp) else: backend_device = chainer.get_device(device) if xp != backend_device.xp: raise ValueError('xp and device arguments are inconsistent.') if xp is chainerx: # Initialize with NumPy/CuPy array that shares memory with the # ChainerX array. # TODO(sonots): Directly use initializer after ChainerX # supports random. chx_device = backend_device.device array = chainerx.empty(shape, dtype=dtype, device=chx_device) fallback_device = backend_device.fallback_device with chainer.using_device(fallback_device): initializer(fallback_device.send(array)) return array with chainer.using_device(backend_device): array = xp.empty(shape, dtype=dtype) initializer(array) return array
def __init__(self, in_size, counts, sample_size, power=0.75, dtype=None): super(NegativeSampling, self).__init__() dtype = chainer.get_dtype(dtype) vocab_size = len(counts) self.sample_size = sample_size power = dtype.type(power) p = numpy.array(counts, dtype) numpy.power(p, power, p) self.sampler = walker_alias.WalkerAlias(p) with self.init_scope(): self.W = variable.Parameter(0, (vocab_size, in_size))
def check_forward(self, x_data): with chainer.using_config('train', not self.test): x = chainer.Variable(x_data) y = self.link(x) self.assertEqual(y.data.dtype, chainer.get_dtype(self.dtype)) y_expect = _batch_normalization( self.expander, self.gamma, self.beta, self.x, self.mean, self.var, self.link.eps, self.test) testing.assert_allclose( y_expect, y.data, **self.check_forward_optionss)
def forward(self, x): if self.h is None: n_batch = x.shape[0] dtype = chainer.get_dtype() h_data = self.xp.zeros( (n_batch, self._state_size), dtype=dtype) h = chainer.Variable(h_data) else: h = self.h self.h = self._call_mgu(h, x) return self.h
def get_svhn(withlabel=True, scale=1., dtype=None, label_dtype=numpy.int32, add_extra=False): """Gets the SVHN dataset. `The Street View House Numbers (SVHN) dataset <http://ufldl.stanford.edu/housenumbers/>`_ is a dataset similar to MNIST but composed of cropped images of house numbers. The functionality of this function is identical to the counterpart for the MNIST dataset (:func:`~chainer.datasets.get_mnist`), with the exception that there is no ``ndim`` argument. .. note:: `SciPy <https://www.scipy.org/>`_ is required to use this feature. Args: withlabel (bool): If ``True``, it returns datasets with labels. In this case, each example is a tuple of an image and a label. Otherwise, the datasets only contain images. scale (float): Pixel value scale. If it is 1 (default), pixels are scaled to the interval ``[0, 1]``. dtype: Data type of resulting image arrays. ``chainer.config.dtype`` is used by default (see :ref:`configuration`). label_dtype: Data type of the labels. add_extra: Use extra training set. Returns: If ``add_extra`` is ``False``, a tuple of two datasets (train and test). Otherwise, a tuple of three datasets (train, test, and extra). If ``withlabel`` is ``True``, all datasets are :class:`~chainer.datasets.TupleDataset` instances. Otherwise, both datasets are arrays of images. """ if not _scipy_available: raise RuntimeError('SciPy is not available: %s' % _error) train_raw = _retrieve_svhn_training() dtype = chainer.get_dtype(dtype) train = _preprocess_svhn(train_raw, withlabel, scale, dtype, label_dtype) test_raw = _retrieve_svhn_test() test = _preprocess_svhn(test_raw, withlabel, scale, dtype, label_dtype) if add_extra: extra_raw = _retrieve_svhn_extra() extra = _preprocess_svhn(extra_raw, withlabel, scale, dtype, label_dtype) return train, test, extra else: return train, test
def __init__(self, in_channels, out1, proj3, out3, proj33, out33, pooltype, proj_pool=None, stride=1, conv_init=None, dtype=None): super(InceptionBN, self).__init__() self.out1 = out1 self.proj_pool = proj_pool self.stride = stride self.pooltype = pooltype if pooltype != 'max' and pooltype != 'avg': raise NotImplementedError() dtype = chainer.get_dtype(dtype) with self.init_scope(): self.proj3 = convolution_2d.Convolution2D( in_channels, proj3, 1, nobias=True, initialW=conv_init) self.conv3 = convolution_2d.Convolution2D( proj3, out3, 3, pad=1, stride=stride, nobias=True, initialW=conv_init) self.proj33 = convolution_2d.Convolution2D( in_channels, proj33, 1, nobias=True, initialW=conv_init) self.conv33a = convolution_2d.Convolution2D( proj33, out33, 3, pad=1, nobias=True, initialW=conv_init) self.conv33b = convolution_2d.Convolution2D( out33, out33, 3, pad=1, stride=stride, nobias=True, initialW=conv_init) self.proj3n = batch_normalization.BatchNormalization( proj3, dtype=dtype) self.conv3n = batch_normalization.BatchNormalization( out3, dtype=dtype) self.proj33n = batch_normalization.BatchNormalization( proj33, dtype=dtype) self.conv33an = batch_normalization.BatchNormalization( out33, dtype=dtype) self.conv33bn = batch_normalization.BatchNormalization( out33, dtype=dtype) if out1 > 0: assert stride == 1 assert proj_pool is not None self.conv1 = convolution_2d.Convolution2D( in_channels, out1, 1, stride=stride, nobias=True, initialW=conv_init) self.conv1n = batch_normalization.BatchNormalization( out1, dtype=dtype) if proj_pool is not None: self.poolp = convolution_2d.Convolution2D( in_channels, proj_pool, 1, nobias=True, initialW=conv_init) self.poolpn = batch_normalization.BatchNormalization( proj_pool, dtype=dtype)
def __init__(self, sr, n_fft, hop_length, n_mels, top_db, length, quantize, dtype=None): self.sr = sr self.n_fft = n_fft self.hop_length = hop_length self.n_mels = n_mels self.top_db = top_db self.mu_law = MuLaw(quantize) self.quantize = quantize if length is None: self.length = None else: self.length = length + 1 self.dtype = chainer.get_dtype(dtype)
def get_fashion_mnist(withlabel=True, ndim=1, scale=1., dtype=None, label_dtype=numpy.int32, rgb_format=False): """Gets the Fashion-MNIST dataset. `Fashion-MNIST <https://github.com/zalandoresearch/fashion-mnist/>`_ is a set of fashion articles represented by grey-scale 28x28 images. In the original images, each pixel is represented by one-byte unsigned integer. This function scales the pixels to floating point values in the interval ``[0, scale]``. This function returns the training set and the test set of the official Fashion-MNIST dataset. If ``withlabel`` is ``True``, each dataset consists of tuples of images and labels, otherwise it only consists of images. Args: withlabel (bool): If ``True``, it returns datasets with labels. In this case, each example is a tuple of an image and a label. Otherwise, the datasets only contain images. ndim (int): Number of dimensions of each image. The shape of each image is determined depending on ``ndim`` as follows: - ``ndim == 1``: the shape is ``(784,)`` - ``ndim == 2``: the shape is ``(28, 28)`` - ``ndim == 3``: the shape is ``(1, 28, 28)`` scale (float): Pixel value scale. If it is 1 (default), pixels are scaled to the interval ``[0, 1]``. dtype: Data type of resulting image arrays. ``chainer.config.dtype`` is used by default (see :ref:`configuration`). label_dtype: Data type of the labels. rgb_format (bool): if ``ndim == 3`` and ``rgb_format`` is ``True``, the image will be converted to rgb format by duplicating the channels so the image shape is (3, 28, 28). Default is ``False``. Returns: A tuple of two datasets. If ``withlabel`` is ``True``, both datasets are :class:`~chainer.datasets.TupleDataset` instances. Otherwise, both datasets are arrays of images. """ train_raw = _retrieve_fashion_mnist_training() dtype = chainer.get_dtype(dtype) train = preprocess_mnist(train_raw, withlabel, ndim, scale, dtype, label_dtype, rgb_format) test_raw = _retrieve_fashion_mnist_test() test = preprocess_mnist(test_raw, withlabel, ndim, scale, dtype, label_dtype, rgb_format) return train, test
def _preprocess_cifar(images, labels, withlabel, ndim, scale, dtype): if ndim == 1: images = images.reshape(-1, 3072) elif ndim == 3: images = images.reshape(-1, 3, 32, 32) else: raise ValueError('invalid ndim for CIFAR dataset') dtype = chainer.get_dtype(dtype) images = images.astype(dtype) images *= scale / 255. if withlabel: labels = labels.astype(numpy.int32) return tuple_dataset.TupleDataset(images, labels) else: return images
def prepare(image, size=(224, 224)): """Converts the given image to the numpy array for ResNets. Note that you have to call this method before ``forward`` because the pre-trained resnet model requires to resize the given image, covert the RGB to the BGR, subtract the mean, and permute the dimensions before calling. Args: image (PIL.Image or numpy.ndarray): Input image. If an input is ``numpy.ndarray``, its shape must be ``(height, width)``, ``(height, width, channels)``, or ``(channels, height, width)``, and the order of the channels must be RGB. size (pair of ints): Size of converted images. If ``None``, the given image is not resized. Returns: numpy.ndarray: The converted output array. """ if not available: raise ImportError('PIL cannot be loaded. Install Pillow!\n' 'The actual import error is as follows:\n' + str(_import_error)) dtype = chainer.get_dtype() if isinstance(image, numpy.ndarray): if image.ndim == 3: if image.shape[0] == 1: image = image[0, :, :] elif image.shape[0] == 3: image = image.transpose((1, 2, 0)) image = Image.fromarray(image.astype(numpy.uint8)) image = image.convert('RGB') if size: image = image.resize(size) image = numpy.asarray(image, dtype=dtype) image = image[:, :, ::-1] # NOTE: in the original paper they subtract a fixed mean image, # however, in order to support arbitrary size we instead use the # mean pixel (rather than mean image) as with VGG team. The mean # value used in ResNet is slightly different from that of VGG16. image -= numpy.array( [103.063, 115.903, 123.152], dtype=dtype) image = image.transpose((2, 0, 1)) return image
def __init__(self, pairs, root='.', dtype=None, label_dtype=numpy.int32): _check_pillow_availability() if isinstance(pairs, six.string_types): pairs_path = pairs with open(pairs_path) as pairs_file: pairs = [] for i, line in enumerate(pairs_file): pair = line.strip().split() if len(pair) != 2: raise ValueError( 'invalid format at line {} in file {}'.format( i, pairs_path)) pairs.append((pair[0], int(pair[1]))) self._pairs = pairs self._root = root self._dtype = chainer.get_dtype(dtype) self._label_dtype = label_dtype
def prepare(image, size=(224, 224)): """Converts the given image to the numpy array for VGG models. Note that you have to call this method before ``forward`` because the pre-trained vgg model requires to resize the given image, covert the RGB to the BGR, subtract the mean, and permute the dimensions before calling. Args: image (PIL.Image or numpy.ndarray): Input image. If an input is ``numpy.ndarray``, its shape must be ``(height, width)``, ``(height, width, channels)``, or ``(channels, height, width)``, and the order of the channels must be RGB. size (pair of ints): Size of converted images. If ``None``, the given image is not resized. Returns: numpy.ndarray: The converted output array. """ if not available: raise ImportError('PIL cannot be loaded. Install Pillow!\n' 'The actual import error is as follows:\n' + str(_import_error)) dtype = chainer.get_dtype() if isinstance(image, numpy.ndarray): if image.ndim == 3: if image.shape[0] == 1: image = image[0, :, :] elif image.shape[0] == 3: image = image.transpose((1, 2, 0)) image = Image.fromarray(image.astype(numpy.uint8)) image = image.convert('RGB') if size: image = image.resize(size) image = numpy.asarray(image, dtype=dtype) image = image[:, :, ::-1] image -= numpy.array( [103.939, 116.779, 123.68], dtype=dtype) image = image.transpose((2, 0, 1)) return image
def generate_array(initializer, shape, xp): """Return initialized array. The algorithms used to make the new values depend on the concrete derived classes. If the initializer has the ``dtype`` attribute, it is used to construct the array. Otherwise, ``chainer.config.dtype`` is used instead. See :ref:`configuration` for the dtype config. Args: initializer: A callable object that takes :class:`numpy.ndarray` or :class:`cupy.ndarray` and edits its value. shape (tuple): Shape of a return array. xp (module): :mod:`cupy` or :mod:`numpy`. Returns: numpy.ndarray or cupy.ndarray: An initialized array. """ dtype = chainer.get_dtype(getattr(initializer, 'dtype', None)) array = xp.empty(shape, dtype=dtype) initializer(array) return array
def __init__(self, size=None, decay=0.9, eps=2e-5, dtype=None, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, axis=None, initial_avg_mean=None, initial_avg_var=None): super(BatchNormalization, self).__init__() if size is None and axis is None: raise RuntimeError('size or axis is required') self._initial_avg_mean = initial_avg_mean self._initial_avg_var = initial_avg_var self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps if isinstance(axis, int): axis = (axis,) self.axis = axis self._highprec_dtype = chainer.get_dtype( dtype, map_mixed16=numpy.float32) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 gamma_initializer = \ initializers._get_initializer(initial_gamma) gamma_initializer.dtype = self._highprec_dtype self.gamma = variable.Parameter(gamma_initializer) if use_beta: if initial_beta is None: initial_beta = 0 beta_initializer = initializers._get_initializer(initial_beta) beta_initializer.dtype = self._highprec_dtype self.beta = variable.Parameter(beta_initializer) if size is not None: self._initialize_params(size)
def __init__(self, size, comm, decay=0.9, eps=2e-5, dtype=None, use_gamma=True, use_beta=True, initial_gamma=None, initial_beta=None, communication_backend='auto'): chainer.utils.experimental( 'chainermn.links.MultiNodeBatchNormalization') super(MultiNodeBatchNormalization, self).__init__() self._highprec_dtype = chainer.get_dtype( dtype, map_mixed16=numpy.float32) self.comm = comm self.avg_mean = numpy.zeros(size, dtype=self._highprec_dtype) self.register_persistent('avg_mean') self.avg_var = numpy.zeros(size, dtype=self._highprec_dtype) self.register_persistent('avg_var') self.N = 0 self.register_persistent('N') self.decay = decay self.eps = eps self._communication_backend = \ get_communication_backend(comm, communication_backend) with self.init_scope(): if use_gamma: if initial_gamma is None: initial_gamma = 1 initial_gamma = initializers._get_initializer(initial_gamma) initial_gamma.dtype = self._highprec_dtype self.gamma = variable.Parameter(initial_gamma, size) if use_beta: if initial_beta is None: initial_beta = 0 initial_beta = initializers._get_initializer(initial_beta) initial_beta.dtype = self._highprec_dtype self.beta = variable.Parameter(initial_beta, size)
def make_hidden(self, batchsize): dtype = chainer.get_dtype() return numpy.random.uniform(-1, 1, (batchsize, self.n_hidden, 1, 1))\ .astype(dtype)
def generate_array(initializer, shape, xp, dtype=None, device=None): # type: (types.AbstractInitializer, types.ShapeSpec, types.Xp, types.DTypeSpec, types.DeviceSpec) -> types.NdArray # NOQA """Return initialized array. The algorithms used to make the new values depend on the concrete derived classes. If the initializer has the ``dtype`` attribute, it is used to construct the array. Otherwise, ``chainer.config.dtype`` is used instead. See :ref:`configuration` for the dtype config. Args: initializer: A callable object that takes :ref:`ndarray` and edits its value. shape (tuple): Shape of a return array. xp (module): :mod:`cupy`, :mod:`numpy`, or :mod:`chainerx`. dtype: Dtype specifier. If omitted, ``initializer.dtype`` is used. device: Target device specifier. If omitted, the current device is used for :mod:`cupy`, and the default device is used for :mod:`chainerx`. Returns: :ref:`ndarray`: An initialized array. """ dtype_attr = getattr(initializer, 'dtype', None) if dtype is not None and dtype_attr is not None \ and numpy.dtype(dtype) != numpy.dtype(dtype_attr): raise ValueError( 'dtype mismatch: {} != {}'.format(dtype, dtype_attr)) if dtype is None: dtype = dtype_attr dtype = chainer.get_dtype(dtype) if device is None: backend_device = backend._guess_device_from_array_module(xp) else: backend_device = chainer.get_device(device) if xp != backend_device.xp: raise ValueError('xp and device arguments are inconsistent.') if xp is chainerx: # Initialize with NumPy/CuPy array that shares memory with the # ChainerX array. # TODO(sonots): Directly use initializer after ChainerX # supports random. chx_device = backend_device.device # type: ignore # TODO(okapies): remove 'type: ignore' when chainerx implements sequence support for empty() # NOQA array = chainerx.empty(shape, dtype=dtype, device=chx_device) # type: ignore # NOQA if chx_device.backend.name == 'native': temp_array = _cpu._to_cpu(array) temp_device = cuda.DummyDevice # type: cuda.Device elif chx_device.backend.name == 'cuda': temp_array = cuda.to_gpu(array, chx_device.index) temp_device = cuda.Device(chx_device.index) else: raise RuntimeError('ChainerX backend: {} is not supported.'.format( chx_device.backend.name)) with temp_device: initializer(temp_array) return array with chainer.using_device(backend_device): array = xp.empty(shape, dtype=dtype) initializer(array) return array
def __init__(self, path, root, mean, crop_size, random=True): self.base = chainer.datasets.LabeledImageDataset(path, root) self.mean = mean.astype(chainer.get_dtype()) self.crop_size = crop_size self.random = random