【问题标题】:Implementation of image pre-processing methods on androidandroid上图像预处理方法的实现
【发布时间】:2017-09-05 14:58:13
【问题描述】:

您能否推荐一个库(或 sn-p)来在 Android API 18 上实现预处理 Python 方法(例如 numpy.expand_dims()img_to_array)(以部署基于 TensorFlow Mobile 的应用程序)? Java 上有与 Python 类似的库(例如 ND4J),但它们需要运行 API 级别 21 或更高级别的设备或模拟器。

from keras.preprocessing.image import img_to_array
import numpy as np

    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image /= 255.

【问题讨论】:

  • 函数文档页面有源代码链接。

标签: python tensorflow keras dl4j nd4j


【解决方案1】:

我是一个交互式会话:

In [168]: np.source(np.expand_dims)
In file: /usr/local/lib/python3.5/dist-packages/numpy/lib/shape_base.py

def expand_dims(a, axis):
    """
    .... docs
    """
    a = asarray(a)
    shape = a.shape
    if axis > a.ndim or axis < -a.ndim - 1:
        # 2017-05-17, 1.13.0
        warnings.warn("Both axis > a.ndim and axis < -a.ndim - 1 are "
                      "deprecated and will raise an AxisError in the future.",
                      DeprecationWarning, stacklevel=2)
    # When the deprecation period expires, delete this if block,
    if axis < 0:
        axis = axis + a.ndim + 1
    # and uncomment the following line.
    # axis = normalize_axis_index(axis, a.ndim + 1)
    return a.reshape(shape[:axis] + (1,) + shape[axis:])

所以基本上它使用reshape,其形状在正确的位置包含1 尺寸。这也可以使用[:,:,np.newaxis,...] 语法来完成。

其中任何一个是否可移植到 Java 取决于该语言中的数组实现。

【讨论】:

  • 非常感谢您的回答。我看到了源代码。我的问题与此完全相关:Whether any of that is portable to Java depends on the array implementation in that language. 我之前在 Java 中编写过空行,将我的 Python 代码移植到 Java 是满足我的图像预处理需求的唯一方法。
  • 这种重塑在numpy 中很容易,因为数据实际上存储在 1d 缓冲区中,并且形状(维度)存储为元组。形状影响数据的解释和迭代,但不影响存储。但是,如果将多维数组存储为数组数组,就像 Python 列表一样,任何类型的重塑都需要更多的工作。
猜你喜欢
  • 1970-01-01
  • 2012-04-01
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 2017-05-03
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多