Skip to content

Commit

Permalink
Refactor TensorImage using state pattern
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 322284753
  • Loading branch information
lu-wang-g authored and tflite-support-robot committed Jul 22, 2020
1 parent 59cfb05 commit 55ffefb
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow.lite.support.image;

import static org.tensorflow.lite.support.common.SupportPreconditions.checkArgument;
import static org.tensorflow.lite.support.common.SupportPreconditions.checkNotNull;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

/** Holds a {@link Bitmap} and converts it to other image formats as needed. */
final class BitmapContainer implements ImageContainer {

private final Bitmap bitmap;

// Only supports Bitmap with ARGB_8888.
static BitmapContainer create(Bitmap bitmap) {
return new BitmapContainer(bitmap);
}

private BitmapContainer(Bitmap bitmap) {
checkNotNull(bitmap, "Cannot load null bitmap.");
checkArgument(
bitmap.getConfig().equals(Config.ARGB_8888), "Only supports loading ARGB_8888 bitmaps.");
this.bitmap = bitmap;
}

@Override
public BitmapContainer clone() {
return create(bitmap.copy(bitmap.getConfig(), bitmap.isMutable()));
}

@Override
public Bitmap getBitmap() {
// Not making a defensive copy for performance considerations. During image processing,
// users may need to set and get the bitmap for many times.
return bitmap;
}

@Override
public TensorBuffer getTensorBuffer(DataType dataType) {
TensorBuffer buffer = TensorBuffer.createDynamic(dataType);
ImageConversions.convertBitmapToTensorBuffer(bitmap, buffer);
return buffer;
}

@Override
public int getWidth() {
return bitmap.getWidth();
}

@Override
public int getHeight() {
return bitmap.getHeight();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow.lite.support.image;

import static org.tensorflow.lite.support.common.SupportPreconditions.checkState;

import android.graphics.Bitmap;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

/** */
final class EmptyContainer implements ImageContainer {

static EmptyContainer create() {
return new EmptyContainer();
}

private EmptyContainer() {}

@Override
public EmptyContainer clone() {
return new EmptyContainer();
}

@Override
public Bitmap getBitmap() {
checkState(false, "No image has been loaded yet.");
return null;
}

@Override
public TensorBuffer getTensorBuffer(DataType dataType) {
checkState(false, "No image has been loaded yet.");
return null;
}

@Override
public int getWidth() {
checkState(false, "No image has been loaded yet.");
return 0;
}

@Override
public int getHeight() {
checkState(false, "No image has been loaded yet.");
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow.lite.support.image;

import android.graphics.Bitmap;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

/**
* Handles image conversion across different image types.
*
* <p>An {@link ImageContainer} should support the conversion between the underlying image format to
* the following image types:
*
* <ul>
* <li>{@link Bitmap}
* <li>{@link TensorBuffer} of the specified date type.
* </ul>
*/
interface ImageContainer {

/** Performs deep copy of the {@link ImageContainer}. */
ImageContainer clone();

/** Returns the width of the image. */
int getWidth();

/** Returns the height of the image. */
int getHeight();

/** Gets the {@link Bitmap} representation of the underlying image format. */
Bitmap getBitmap();

/**
* Gets the {@link TensorBuffer} representation with the specific {@code dataType} of the
* underlying image format.
*/
TensorBuffer getTensorBuffer(DataType dataType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void convertTensorBufferToBitmap(TensorBuffer buffer, Bitmap bitmap) {
buffer.getDataType()));
}
int[] shape = buffer.getShape();
TensorImage.checkImageTensorShape(shape);
TensorBufferContainer.checkImageTensorShape(shape);
int h = shape[shape.length - 3];
int w = shape[shape.length - 2];
if (bitmap.getWidth() != w || bitmap.getHeight() != h) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow.lite.support.image;

import static org.tensorflow.lite.support.common.SupportPreconditions.checkArgument;
import static org.tensorflow.lite.support.common.SupportPreconditions.checkState;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import org.tensorflow.lite.DataType;
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

/** Holds a {@link TensorBuffer} and converts it to other image formats as needed. */
final class TensorBufferContainer implements ImageContainer {

private final TensorBuffer buffer;

static TensorBufferContainer create(TensorBuffer buffer) {
return new TensorBufferContainer(buffer);
}

private TensorBufferContainer(TensorBuffer buffer) {
checkImageTensorShape(buffer.getShape());
this.buffer = buffer;
}

@Override
public TensorBufferContainer clone() {
return create(TensorBuffer.createFrom(buffer, buffer.getDataType()));
}

@Override
public Bitmap getBitmap() {
checkState(
buffer.getDataType() == DataType.UINT8,
"TensorImage is holding a float-value image which is not able to convert to a Bitmap.");

int[] shape = buffer.getShape();
int h = shape[shape.length - 3];
int w = shape[shape.length - 2];
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
ImageConversions.convertTensorBufferToBitmap(buffer, bitmap);
return bitmap;
}

@Override
public TensorBuffer getTensorBuffer(DataType dataType) {
// If the data type of buffer is desired, return it directly. Not making a defensive copy for
// performance considerations. During image processing, users may need to set and get the
// TensorBuffer for many times.
// Otherwise, create another one with the expected data type.
return buffer.getDataType() == dataType ? buffer : TensorBuffer.createFrom(buffer, dataType);
}

@Override
public int getWidth() {
return getBufferDimensionSize(-2);
}

@Override
public int getHeight() {
return getBufferDimensionSize(-3);
}

// Internal helper method to get the size of one dimension in the shape of the buffer.
private int getBufferDimensionSize(int dim) {
int[] shape = buffer.getShape();
// The defensive check is needed, because buffer might be invalidly changed by users
// (a.k.a internal data is corrupted)
checkImageTensorShape(shape);
dim = dim % shape.length;
if (dim < 0) {
dim += shape.length;
}
return shape[dim];
}

// Verifies if the tensor shape is [h, w, 3] or [1, h, w, 3].
static void checkImageTensorShape(int[] shape) {
checkArgument(
(shape.length == 3 || (shape.length == 4 && shape[0] == 1))
&& shape[shape.length - 3] > 0
&& shape[shape.length - 2] > 0
&& shape[shape.length - 1] == 3,
"Only supports image shape in (h, w, c) or (1, h, w, c), and channels representing R, G, B"
+ " in order.");
}
}
Loading

0 comments on commit 55ffefb

Please sign in to comment.