【问题标题】:I am trying to translate the numpy.ndarray to tensor input feed to my model我正在尝试将 numpy.ndarray 转换为张量输入到我的模型
【发布时间】:2021-10-15 05:01:02
【问题描述】:

模型输入提要:

图像numpy数组:

{'test': array([[[[ 31,  24,  14],
         [ 24,  20,  11],
         [ 21,  21,  19],
         ...,
         [ 12,  23,  29],
         [ 14,  25,  31],
         [ 17,  28,  34]],

        [[ 12,  23,  27],
         [ 10,  21,  23],
         [ 20,  32,  32],
         ...,
         [ 23,  45,  56],
         [ 16,  40,  50],
         [  2,  31,  39]],

        [[  6,  33,  42],
         [  0,  21,  29],
         [  5,  25,  34],
         ...,
         [ 28,  47,  64],
         [ 13,  30,  48],
         [  0,  15,  34]],

        ...,

        [[ 29,  46,  56],
         [ 50,  68,  78],
         [ 29,  46,  56],
         ...,
         [ 84, 104, 111],
         [ 91, 111, 118],
         [ 69,  89,  96]],

        [[ 90, 110, 119],
         [ 96, 116, 125],
         [ 95, 115, 124],
         ...,
         [ 70,  85,  92],
         [ 81,  98, 106],
         [ 86, 103, 111]],

        [[100, 118, 128],
         [ 71,  89,  99],
         [ 62,  80,  90],
         ...,
         [  7,  44,  71],
         [ 14,  51,  77],
         [  7,  43,  65]]],


       [[[  6,  37,  57],
         [ 23,  49,  64],
         [ 20,  42,  53],
         ...,
         [ 41,  40,  36],
         [ 17,   8,   3],
         [ 24,   0,   0]],

        [[ 28,  29,  24],
         [ 19,  21,  18],
         [ 20,  22,  21],
         ...,
         [ 33,  75,  91],
         [ 34,  86, 110],
         [ 21,  84, 119]],

        [[ 12,  81, 120],
         [  5,  77, 117],
         [ 16,  85, 124],
         ...,
         [ 74,  96, 117],
         [ 74,  99, 119],
         [ 51,  78,  97]],

        ...,

        [[ 14,  22,  33],
         [ 27,  36,  45],
         [ 11,  20,  29],
         ...,
         [ 56,  63,  69],
         [ 74,  81,  87],
         [ 50,  59,  64]],

        [[ 40,  51,  55],
         [ 52,  63,  67],
         [ 26,  40,  41],
         ...,
         [ 13,  33,  44],
         [  7,  25,  37],
         [ 34,  50,  63]],

        [[ 10,  26,  39],
         [ 10,  28,  38],
         [ 39,  59,  68],
         ...,
         [ 87, 110, 126],
         [ 64,  87, 103],
         [ 63,  86, 102]]]], dtype=uint8)}

所需输出:

{'test': array([[[[0.12156863, 0.09411765, 0.05490196],
         [0.11372549, 0.09019608, 0.05098039],
         [0.09803922, 0.08235294, 0.04313725],
         ...,
         [0.1372549 , 0.03137255, 0.01960784],
         [0.18823529, 0.03529412, 0.03921569],
         [0.21568627, 0.03921569, 0.05098039]],

        [[0.12156863, 0.09803922, 0.05882353],
         [0.10980392, 0.09019608, 0.05490196],
         [0.09411765, 0.08235294, 0.04705882],
         ...,
         [0.13333333, 0.03529412, 0.02352941],
         [0.18823529, 0.04313725, 0.04705882],
         [0.21960784, 0.05098039, 0.05882353]],

        [[0.11764706, 0.10196078, 0.06666667],
         [0.10588235, 0.09411765, 0.05882353],
         [0.09019608, 0.07843137, 0.05098039],
         ...,
         [0.1254902 , 0.03921569, 0.02745098],
         [0.18823529, 0.05490196, 0.05490196],
         [0.22352941, 0.06666667, 0.07058824]],

        ...,

        [[0.06666667, 0.07058824, 0.05098039],
         [0.06666667, 0.07058824, 0.05490196],
         [0.06666667, 0.06666667, 0.05882353],
         ...,
         [0.10196078, 0.03921569, 0.02745098],
         [0.14117647, 0.04705882, 0.04705882],
         [0.16470588, 0.05098039, 0.05882353]],

        [[0.04313725, 0.04705882, 0.02745098],
         [0.04313725, 0.04705882, 0.02745098],
         [0.04313725, 0.04705882, 0.03137255],
         ...,
         [0.10588235, 0.03921569, 0.03137255],
         [0.14509804, 0.04705882, 0.04705882],
         [0.16862745, 0.05098039, 0.05882353]],

        [[0.02745098, 0.03137255, 0.01176471],
         [0.02745098, 0.03137255, 0.01176471],
         [0.02745098, 0.03137255, 0.01176471],
         ...,
         [0.10588235, 0.03921569, 0.03137255],
         [0.14509804, 0.04705882, 0.04705882],
         [0.16862745, 0.05098039, 0.05882353]]]])}

我不知道该怎么做,实际上,我正在尝试查找图像中存在的文本对象。有一些预处理技术可以实现这一点。

任何帮助将不胜感激。

编辑:

图片附上here

我的推理代码

from tensorflow.contrib import predictor
from PIL import Image
import numpy as np

a = predictor.from_saved_model('my_model')  # this is a tensorflow saved model not a frozenmodel
image_np = np.array(Image.open("car_1.jpg"))
image_resized = np.resize(image_np, (2,70,130,3))
a({'test':image_resized})

【问题讨论】:

  • 能否提供所需数据的形状
  • 我不太明白。 {'test': array([[[[ 31, 24, 14], [ 24, 20, 11]..... 部分中是否有某种文本?
  • @AbhishekPrajapat 这是一个图像数组
  • @Nagakiran 所需的数据形状 ---> (2, 70, 130, 3)
  • 这意味着您需要 2 张高度:70、体重:130 和 3 个通道的图像。另外,您输入的当前形状是什么?

标签: python numpy tensorflow machine-learning deep-learning


【解决方案1】:

一张图片有 3 个组件 -> 高度、宽度和通道。在您的情况下,所需的高度为 70,宽度为 130。现在对于通道,您无法控制它们。然后保持原样。在你的情况下是 3。

来到[2,70,130,3] 中的第一个值2 代表batch_size 或者说图像的数量。在您的情况下,由于您只有 1 图像,因此您无法在此处获得 2。如果你有 2 图像,那么你就会得到它。

from PIL import Image
import requests
import numpy as np
import cv2

# reading your image
img = Image.open(requests.get('https://i.stack.imgur.com/qf5RE.jpg', stream=True).raw)

# You need to do this
new_img = cv2.resize(np.array(img), (70,130))
new_img = new_img / 255

new_img 是所需的输出。

更新:

假设您有一个图像列表。那你就可以这样了

def reshape_images(images):
    new_images = []
    for image in images:
        new_img = cv2.resize(np.array(img), (70,130))
        new_images.append(new_img)
    new_images = np.stack(new_images, axis=0)
    return new_images

new_images = reshape_images([img, img])

【讨论】:

  • 差不多了,如果我也有两张图片,我怎么联系他们来获取形状,我需要调用哪个函数?
  • 你的回答是对的,我发错了问题,谢谢你的时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2023-01-11
  • 2021-10-10
  • 2016-10-04
  • 1970-01-01
  • 2021-03-14
  • 2021-03-06
相关资源
最近更新 更多