20.03.2021 Views

Deep-Learning-with-PyTorch

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

474 CHAPTER 15 Deploying to production

method from the location given as an argument. There is a slight complication though:

apps’ data is provided by the supplier as assets that are not easily accessible from the

filesystem. For this reason, a utility method called assetFilePath (taken from the

PyTorch Android examples) copies the asset to a location in the filesystem. Finally, in

Java, we need to catch exceptions that our code throws, unless we want to (and are able

to) declare the method we are coding as throwing them in turn.

When we get an image from the camera app using Android’s Intent mechanism, we

need to run it through our model and display it. This happens in the onActivityResult

event handler.

Listing 15.19 MainActivity.java, part 2

Performs normalization, but the default is images in

the range of 0…1 so we do not need to transform:

that is, have 0 shift and a scaling divisor of 1.

@Override

protected void onActivityResult(int requestCode, int resultCode,

Intent data) {

This is executed when the

if (requestCode == REQUEST_IMAGE_CAPTURE && camera app takes a picture.

resultCode == RESULT_OK) {

Bitmap bitmap = (Bitmap) data.getExtras().get("data");

final float[] means = {0.0f, 0.0f, 0.0f};

final float[] stds = {1.0f, 1.0f, 1.0f};

Gets a tensor from a bitmap, combining

steps like TorchVision’s ToTensor

(converting to a float tensor with entries

between 0 and 1) and Normalize

}

}

final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(

bitmap, means, stds);

This looks almost like

final Tensor outputTensor = model.forward(

what we did in C++.

IValue.from(inputTensor)).toTensor();

Bitmap output_bitmap = tensorToBitmap(outputTensor, means, stds,

Bitmap.Config.RGB_565);

image_view.setImageBitmap(output_bitmap);

tensorToBitmap is

our own invention.

Converting the bitmap we get from Android to a tensor is handled by the Tensor-

ImageUtils.bitmapToFloat32Tensor function (static method), which takes two float

arrays, means and stds, in addition to bitmap. Here we specify the mean and standard

deviation of our input data(set), which will then be mapped to have zero mean and unit

standard deviation just like TorchVision’s Normalize transform. Android already gives

us the images in the 0..1 range that we need to feed into our model, so we specify mean

0 and standard deviation 1 to prevent the normalization from changing our image.

Around the actual call to model.forward, we then do the same IValue wrapping and

unwrapping dance that we did when using the JIT in C++, except that our forward takes

a single IValue rather than a vector of them. Finally, we need to get back to a bitmap.

Here PyTorch will not help us, so we need to define our own tensorToBitmap (and

submit the pull request to PyTorch). We spare you the details here, as they are tedious

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!