【问题标题】:How to feed in images into the model Keras如何将图像输入模型 Keras
【发布时间】:2018-11-08 02:46:06
【问题描述】:

我有一个自动驾驶汽车的数据集。我的X 值是图像的名称。示例是

array([['img_2.png'],
       ['img_3.png'],
       ['img_4.png'],
       ...,
       ['img_6405.png'],
       ['img_6406.png'],
       ['img_6407.png']], dtype=object)

我发现如果我们有某种batch_generator,模型会表现得很好。我找到了那个模板代码。

def batch_generator(image_paths, steering_ang, batch_size, istraining):

  while True:
    batch_img = []
    batch_steering = []

    for i in range(batch_size):
      random_index = random.randint(0, len(image_paths) - 1)

      if istraining:
        im = random_augment(image_paths[random_index])
        steering = steering_ang[random_index]
      else:
        im = mpimg.imread(image_paths[random_index])
        steering = steering_ang[random_index]

      im = img_preprocess(im)
      batch_img.append(im)
      batch_steering.append(steering)
    yield (np.asarray(batch_img), np.asarray(batch_steering))  

我将此功能更改为供我使用,但是当我应用它时。

x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))

我收到以下错误TypeError: Object does not appear to be a 8-bit string path or a Python file-like object。我理解错误,图像不是数组而是字符串。如何将图像路径的字符串转换为数组

【问题讨论】:

    标签: python image-processing keras neural-network deep-learning


    【解决方案1】:

    这是因为在某些时候您将 X_trainy_train 转换为 numpy 数组而不是图像路径。

    这就是 python 抱怨的原因。您可能正在对需要转换整个训练数据集的代码执行其他操作,但现在您不需要这样做,因为您在 batch_generator 函数中有 imread()。我会回到前面的代码并重新创建 X_trainy_train 作为图像的文件路径,然后重新运行这部分代码。

    【讨论】:

      【解决方案2】:

      我不知道您在 img_preprocess() 函数中在做什么,但据我所知,有两个可能的问题:

      1. 您必须将图像的路径附加到图像名称:path_to_image = path_to_image_dir + '/' + image

      2. 您必须实际打开图像才能获得它的数组。您可以使用 Pillow 或 OpenCV: PIL.Image.open(path_to_image)cv2.imread(path_to_image)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-18
        • 2018-12-15
        • 2018-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-15
        相关资源
        最近更新 更多