Python chainer.functions.resize_images() Examples

The following are 30 code examples of chainer.functions.resize_images(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.functions , or try the search function .
Example #1
Source File: test_arrays.py    From chainer with MIT License 6 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

            def __init__(self, ops, args, input_argname):
                super(Model, self).__init__()
                self.ops = ops
                self.args = args
                self.input_argname = input_argname

            def __call__(self, x):
                self.args[self.input_argname] = x
                return self.ops(**self.args)

        # (batch, channel, height, width) = (1, 1, 2, 2)
        self.x = np.array([[[[64, 32], [64, 32]]]], np.float32)

        # 2x upsampling
        args = {'output_shape': (4, 4)}
        self.model = Model(F.resize_images, args, 'x') 
Example #2
Source File: test_arrays.py    From onnx-chainer with MIT License 6 votes vote down vote up
def setUp(self):

        class Model(chainer.Chain):

            def __init__(self, ops, args, input_argname):
                super(Model, self).__init__()
                self.ops = ops
                self.args = args
                self.input_argname = input_argname

            def __call__(self, x):
                self.args[self.input_argname] = x
                return self.ops(**self.args)

        # (batch, channel, height, width) = (1, 1, 2, 2)
        self.x = np.array([[[[64, 32], [64, 32]]]], np.float32)

        # 2x upsampling
        args = {'output_shape': (4, 4)}
        self.model = Model(F.resize_images, args, 'x') 
Example #3
Source File: train_multi.py    From chainercv with MIT License 6 votes vote down vote up
def forward(self, imgs, labels):
        h_aux, h_main = self.model.extractor(imgs)
        h_aux = F.dropout(self.aux_conv1(h_aux), ratio=0.1)
        h_aux = self.aux_conv2(h_aux)
        h_aux = F.resize_images(h_aux, imgs.shape[2:])

        h_main = self.model.ppm(h_main)
        h_main = F.dropout(self.model.head_conv1(h_main), ratio=0.1)
        h_main = self.model.head_conv2(h_main)
        h_main = F.resize_images(h_main, imgs.shape[2:])

        aux_loss = F.softmax_cross_entropy(h_aux, labels)
        main_loss = F.softmax_cross_entropy(h_main, labels)
        loss = 0.4 * aux_loss + main_loss

        chainer.reporter.report({'loss': loss}, self)
        return loss 
Example #4
Source File: render.py    From mesh_reconstruction with MIT License 6 votes vote down vote up
def render(directory, elevation=30, distance=DISTANCE):
    for azimuth in range(0, 360, 15):
        filename = os.path.join(directory, 'e%03d_a%03d.png' % (elevation, azimuth))
        set_camera_location(elevation, azimuth, distance)
        bpy.context.scene.render.filepath = filename
        bpy.ops.render.render(write_still=True)

        if False:
            img = scipy.misc.imread(filename)[:, :, :].astype('float32') / 255.
            if False:
                img = (img[::2, ::2] + img[1::2, ::2] + img[::2, 1::2] + img[1::2, 1::2]) / 4.
            else:
                import chainer.functions as cf
                img = img.transpose((2, 0, 1))[None, :, :, :]
                img = cf.resize_images(img, (64, 64))
                img = img[0].data.transpose((1, 2, 0))

            img = (img * 255).clip(0., 255.).astype('uint8')
            scipy.misc.imsave(filename, img) 
Example #5
Source File: pspnet.py    From chainercv with MIT License 6 votes vote down vote up
def _multiscale_predict(predict_method, img, scales):
    orig_H, orig_W = img.shape[1:]
    scores = []
    orig_img = img
    for scale in scales:
        img = orig_img.copy()
        if scale != 1.0:
            img = transforms.resize(
                img, (int(orig_H * scale), int(orig_W * scale)))
        # This method should return scores
        y = predict_method(img)[None]
        assert y.shape[2:] == img.shape[1:]

        if scale != 1.0:
            y = F.resize_images(y, (orig_H, orig_W)).array
        scores.append(y)
    xp = chainer.backends.cuda.get_array_module(scores[0])
    scores = xp.stack(scores)
    return scores.mean(0)[0]  # (C, H, W) 
Example #6
Source File: evaluation.py    From chainer-gan-lib with MIT License 5 votes vote down vote up
def get_mean_cov(model, ims, batch_size=100):
    n, c, w, h = ims.shape
    n_batches = int(math.ceil(float(n) / float(batch_size)))

    xp = model.xp

    print('Batch size:', batch_size)
    print('Total number of images:', n)
    print('Total number of batches:', n_batches)

    ys = xp.empty((n, 2048), dtype=xp.float32)

    for i in range(n_batches):
        print('Running batch', i + 1, '/', n_batches, '...')
        batch_start = (i * batch_size)
        batch_end = min((i + 1) * batch_size, n)

        ims_batch = ims[batch_start:batch_end]
        ims_batch = xp.asarray(ims_batch)  # To GPU if using CuPy
        ims_batch = Variable(ims_batch)

        # Resize image to the shape expected by the inception module
        if (w, h) != (299, 299):
            ims_batch = F.resize_images(ims_batch, (299, 299))  # bilinear

        # Feed images to the inception module to get the features
        with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
            y = model(ims_batch, get_feature=True)
        ys[batch_start:batch_end] = y.data

    mean = chainer.cuda.to_cpu(xp.mean(ys, axis=0))
    # cov = F.cross_covariance(ys, ys, reduce="no").data.get()
    cov = np.cov(chainer.cuda.to_cpu(ys).T)

    return mean, cov 
Example #7
Source File: pspnet.py    From chainercv with MIT License 5 votes vote down vote up
def forward(self, x):
        ys = [x]
        H, W = x.shape[2:]
        for f, ksize in zip(self, self.ksizes):
            y = F.average_pooling_2d(x, ksize, ksize)
            y = f(y)
            y = F.resize_images(y, (H, W))
            ys.append(y)
        return F.concat(ys, axis=1) 
Example #8
Source File: pspnet.py    From chainercv with MIT License 5 votes vote down vote up
def forward(self, x):
        _, res5 = self.extractor(x)

        h = self.ppm(res5)
        h = self.head_conv1(h)
        h = self.head_conv2(h)
        h = F.resize_images(h, x.shape[2:])
        return h 
Example #9
Source File: deeplab_v3_plus.py    From chainercv with MIT License 5 votes vote down vote up
def _get_proba(self, img, scale, flip):
        if flip:
            img = img[:, :, ::-1]

        _, H, W = img.shape
        if scale == 1.0:
            h, w = H, W
        else:
            h, w = int(H * scale), int(W * scale)
            img = resize(img, (h, w))

        img = self.prepare(img)

        x = chainer.Variable(self.xp.asarray(img[np.newaxis]))
        x = self.forward(x)
        x = F.softmax(x, axis=1)
        score = F.resize_images(x, img.shape[1:])[0, :, :h, :w].array
        score = chainer.backends.cuda.to_cpu(score)

        if scale != 1.0:
            score = resize(score, (H, W))

        if flip:
            score = score[:, :, ::-1]

        return score 
Example #10
Source File: evaluation.py    From chainer-gan-lib with MIT License 5 votes vote down vote up
def get_mean_cov(model, ims, batch_size=100):
    n, c, w, h = ims.shape
    n_batches = int(math.ceil(float(n) / float(batch_size)))

    xp = model.xp

    print('Batch size:', batch_size)
    print('Total number of images:', n)
    print('Total number of batches:', n_batches)

    # Compute the softmax predicitions for for all images, split into batches
    # in order to fit in memory

    ys = xp.empty((n, 2048), dtype=xp.float32)  # Softmax container

    for i in range(n_batches):
        print('Running batch', i + 1, '/', n_batches, '...')
        batch_start = (i * batch_size)
        batch_end = min((i + 1) * batch_size, n)

        ims_batch = ims[batch_start:batch_end]
        ims_batch = xp.asarray(ims_batch)  # To GPU if using CuPy
        ims_batch = Variable(ims_batch)

        # Resize image to the shape expected by the inception module
        if (w, h) != (299, 299):
            ims_batch = F.resize_images(ims_batch, (299, 299))  # bilinear

        # Feed images to the inception module to get the softmax predictions
        with chainer.using_config('train', False), chainer.using_config('enable_backprop', False):
            y = model(ims_batch, get_feature=True)
        ys[batch_start:batch_end] = y.data

    mean = xp.mean(ys, axis=0).get()
    # cov = F.cross_covariance(ys, ys, reduce="no").data.get()
    cov = np.cov(ys.get().T)

    return mean, cov 
Example #11
Source File: ResizeImages.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.resize_images(x, (257, 513))
        return y1


# ====================================== 
Example #12
Source File: renderer.py    From mesh_reconstruction with MIT License 5 votes vote down vote up
def rasterize_silhouettes(
        faces,
        image_size=DEFAULT_IMAGE_SIZE,
        anti_aliasing=DEFAULT_ANTI_ALIASING,
        near=DEFAULT_NEAR,
        far=DEFAULT_FAR,
        eps=DEFAULT_EPS,
        background_color=DEFAULT_BACKGROUND_COLOR,
):
    if anti_aliasing:
        # 2x super-sampling
        faces = faces * (2 * image_size - 1) / (2 * image_size - 2)
        images = neural_renderer.Rasterize(
            image_size * 2, near, far, eps, background_color, return_rgb=False, return_alpha=True, return_depth=False)(
            faces)[1]
    else:
        images = neural_renderer.Rasterize(
            image_size, near, far, eps, background_color, return_rgb=False, return_alpha=True, return_depth=False)(
            faces)[1]

    # transpose & vertical flip
    images = images[:, ::-1, :]

    if anti_aliasing:
        # 0.5x down-sampling
        images = cf.resize_images(images[:, None, :, :], (image_size, image_size))[:, 0]

    return images 
Example #13
Source File: train.py    From deep_dream_3d with MIT License 5 votes vote down vote up
def save_image(directory, filename, scene, image_size, distance, azimuth=0):
    # set camera
    batch_size = scene.num_cameras
    azimuth_batch = cp.ones(batch_size, 'float32') * azimuth
    distance_batch = cp.ones(batch_size, 'float32') * distance
    scene.camera.set_eye(azimuth=azimuth_batch, distance=distance_batch)

    # rasterization & save
    images = scene.rasterize(image_size=image_size * 2, background_colors=1., fill_back=True).data.get()
    images = cf.resize_images(images, (image_size, image_size))
    image = images[0].transpose((1, 2, 0)).data
    image = (image * 255).clip(0., 255.).astype('uint8')
    scipy.misc.imsave(os.path.join(directory, filename), image) 
Example #14
Source File: utils.py    From Guided-Attention-Inference-Network with MIT License 5 votes vote down vote up
def VGGprepare_am_input(var):
	xp = get_array_module(var)

	# var = F.resize_images(var, size)
	var = F.transpose(var, (0, 2, 3, 1)) # [[W, H, C]]
	var = F.flip(var, 3)
	var -= xp.array([[103.939, 116.779, 123.68]], dtype=xp.float32)
	var = F.transpose(var, (0, 3, 1, 2))
	return var 
Example #15
Source File: GAIN.py    From Guided-Attention-Inference-Network with MIT License 5 votes vote down vote up
def get_gcam(self, end_output, activations, shape, label):
		self.cleargrads()
		class_id = self.set_init_grad(end_output, label)
		end_output.backward(retain_grad=True)
		grad = activations.grad_var
		grad = F.average_pooling_2d(grad, (grad.shape[-2], grad.shape[-1]), 1)
		grad = F.expand_dims(F.reshape(grad, (grad.shape[0]*grad.shape[1], grad.shape[2], grad.shape[3])), 0)
		weights = activations
		weights = F.expand_dims(F.reshape(weights, (weights.shape[0]*weights.shape[1], weights.shape[2], weights.shape[3])), 0)
		gcam = F.resize_images(F.relu(F.convolution_2d(weights, grad, None, 1, 0)), shape)
		return gcam, class_id 
Example #16
Source File: MultiScaleNetwork.py    From Video-frame-prediction-by-multi-scale-GAN with MIT License 5 votes vote down vote up
def __call__(self, seq_input, prev_output=None):
		"""

		:param seq_input: sequence of previous frames scaled down for the
					      current Generator
		:param prev output: The not yet scaled up output of the previous scale of Generator if any.

		:type seq_input: ndarray: [batch X Input feature Maps X Height X Width]
		:type prev output: ndarray: [batch X 3 X Height@prev scale X Width@prev scale

		return: Result of passing through Convolutions and activatons at this scale of Generator
		"""
		if not self.lowest_scale:
			# Concatenate scaled image from prev Generator Scale
			scaled_output = resize_images(prev_output, (int(seq_input.shape[2]), int(seq_input.shape[3])))
			seq_input = F.concat((seq_input, scaled_output), 1)

		# ReLu at laster to compute activations and hecne vrzw
		output = seq_input
		for i in range(len(self.net) - 1):
			output = getattr(self, self.net[i][0])(output)
			output = F.relu(output)

		output = getattr(self, self.net[-1][0])(output)
		output = F.tanh(output)

		# TODO: Train Network by allowing the addition of activations previous scales
		# if not self.lowest_scale:
		# 	return output + scaled_output
		return output 
Example #17
Source File: MultiScaleNetwork.py    From Video-frame-prediction-by-multi-scale-GAN with MIT License 5 votes vote down vote up
def predict(self, x, no_of_predictions=1, seq_len=4):
		"""

		:param x:
		:param no_of_predictions:
		:param seq_len:
		:return:
		"""
		# x shape = [n, 12, h, w]
		xp = cp.get_array_module(x)
		n, c, h, w = x.shape
		outputs = []

		for i in range(no_of_predictions):
			print("Predicting frame no : ",i+1)
			seq = resize_images(x, (int(h / 2 ** 3), int(w / 2 ** 3)))
			print((int(h / 2 ** 3), int(w / 2 ** 3)))
			output = None

			for j in range(1, 5):
				output = self.singleforward(j, seq, output)
				if j != 4:
					seq = resize_images(x, (int(h / 2 ** (3-j)), int(w / 2 ** (3-j))))

			outputs.append(output.data)
			x = xp.concatenate([x, output.data], 1)[:, -seq_len*3:, :, :]
			print("Predictions done for : ", i+1)
		return outputs 
Example #18
Source File: pspnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x, out_size):
        x = self.conv1(x)
        x = self.dropout(x)
        x = self.conv2(x)
        x = F.resize_images(x, output_shape=out_size)
        return x 
Example #19
Source File: deeplabv3.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        in_size = self.upscale_out_size if self.upscale_out_size is not None else x.shape[2:]
        x = self.pool(x)
        x = self.conv(x)
        x = F.resize_images(x, output_shape=in_size)
        return x 
Example #20
Source File: sinet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        out_size = self.out_size if (self.out_size is not None) else\
            (x.shape[2] * self.scale_factor, x.shape[3] * self.scale_factor)
        return F.resize_images(x, output_shape=out_size) 
Example #21
Source File: resattnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        return F.resize_images(x, output_shape=self.size) 
Example #22
Source File: airnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        input_shape = x.shape
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = F.resize_images(x, output_shape=input_shape[2:])
        x = self.conv3(x)
        x = F.sigmoid(x)
        return x 
Example #23
Source File: ntsnet_cub.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        raw_pre_features = self.backbone(x)

        rpn_score = self.navigator_unit(raw_pre_features)
        rpn_score.to_cpu()
        all_cdds = [np.concatenate((y.reshape(-1, 1), self.edge_anchors.copy()), axis=1)
                    for y in rpn_score.array]
        top_n_cdds = [hard_nms(y, top_n=self.top_n, iou_thresh=0.25) for y in all_cdds]
        top_n_cdds = np.array(top_n_cdds)
        top_n_index = top_n_cdds[:, :, -1].astype(np.int64)
        top_n_index = np.array(top_n_index, dtype=np.int64)
        top_n_prob = np.take_along_axis(rpn_score.array, top_n_index, axis=1)

        batch = x.shape[0]
        x_pad = F.pad(x, pad_width=self.pad_width, mode="constant", constant_values=0)
        part_imgs = []
        for i in range(batch):
            for j in range(self.top_n):
                y0, x0, y1, x1 = tuple(top_n_cdds[i][j, 1:5].astype(np.int64))
                x_res = F.resize_images(
                    x_pad[i:i + 1, :, y0:y1, x0:x1],
                    output_shape=(224, 224))
                part_imgs.append(x_res)
        part_imgs = F.concat(tuple(part_imgs), axis=0)
        part_features = self.backbone_tail(self.backbone(part_imgs))

        part_feature = part_features.reshape((batch, self.top_n, -1))
        part_feature = part_feature[:, :self.num_cat, :]
        part_feature = part_feature.reshape((batch, -1))

        raw_features = self.backbone_tail(raw_pre_features)

        concat_out = F.concat((part_feature, raw_features), axis=1)
        concat_logits = self.concat_net(concat_out)

        if self.aux:
            raw_logits = self.backbone_classifier(raw_features)
            part_logits = self.partcls_net(part_features).reshape((batch, self.top_n, -1))
            return concat_logits, raw_logits, part_logits, top_n_prob
        else:
            return concat_logits 
Example #24
Source File: deeplabv3.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x, out_size):
        x = self.conv1(x)
        x = self.dropout(x)
        x = self.conv2(x)
        x = F.resize_images(x, output_shape=out_size)
        return x 
Example #25
Source File: ResizeImages.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.resize_images(x, (257, 513))
        return y1


# ====================================== 
Example #26
Source File: pspnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        in_size = self.upscale_out_size if self.upscale_out_size is not None else x.shape[2:]
        x = self.pool(x)
        x = self.conv(x)
        x = F.resize_images(x, output_shape=in_size)
        return x 
Example #27
Source File: fcn8sd.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x, out_size):
        x = self.conv1(x)
        x = self.dropout(x)
        x = self.conv2(x)
        x = F.resize_images(x, output_shape=out_size)
        return x 
Example #28
Source File: test_resize_images.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, = inputs
        output_shape = self.in_shape[2:]
        y = functions.resize_images(
            x, output_shape,
            mode=self.mode, align_corners=self.align_corners)
        return y, 
Example #29
Source File: test_resize_images.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x, output_shape, gy):
        def f(x):
            return functions.resize_images(
                x, output_shape,
                mode=self.mode, align_corners=self.align_corners)

        gradient_check.check_backward(
            f, x, gy, dtype='d', atol=1e-2, rtol=1e-3, eps=1e-5) 
Example #30
Source File: test_resize_images.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, = inputs
        output_shape = self.output_shape[2:]
        y = functions.resize_images(
            x, output_shape,
            mode=self.mode, align_corners=self.align_corners)
        return y,