簡體   English   中英

如何將圖像輸入模型Keras

[英]How to feed in images into the model Keras

我有一個自動駕駛汽車的數據集。 我的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 我了解錯誤,圖片不是數組而是字符串。 我如何將圖像路徑的字符串轉換為數組

這是因為有時將X_trainy_train轉換為numpy數組,而不是圖像路徑。

這就是python抱怨的原因。 您可能正在使用需要轉換整個訓練數據集的代碼做其他事情,但是現在您不需要了,因為在batch_generator函數中有imread() 我將回到代碼的前面,並重新創建X_trainy_train作為圖像的文件路徑,然后重新運行此部分代碼。

我不知道您在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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM