亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

NameError:未定義名稱“read_image”

NameError:未定義名稱“read_image”

Helenr 2022-06-02 15:12:23
我想用圖像測試我的圖像分類模型。但我得到一些錯誤: --> path = 'E:/My Work Elements/Thesis Related/Trash Classification/Trash New Code/test/cardboard/cardboard42.jpg'    test_single_image(path)        Traceback (most recent call last):          File "<ipython-input-15-4a6021aada0c>", line 2, in <module>            test_single_image(path)          File "<ipython-input-14-1654f8f7a46b>", line 3, in test_single_image            images = read_image(path)        NameError: name 'read_image' is not defined我使用這段代碼:def test_single_image(path):  Garbage = ['cardboard','Glass','Metal','paper','plastic','Trash']  images = read_image(path)  time.sleep(.5)  bt_prediction = vgg16.predict(images)   preds = model.predict_proba(bt_prediction)  for idx, Garbage, x in zip(range(0,6), Garbage , preds[0]):   print("ID: {}, Label: {} {}%".format(idx, Garbage, round(x*100,2) ))  print('Final Decision:')  time.sleep(.5)  for x in range(3):   print('.'*(x+1))   time.sleep(.2)  class_predicted = model.predict_classes(bt_prediction)  class_dictionary = generator_top.class_indices  print(class_dictionary)  inv_map = {v: k for k, v in class_dictionary.items()}   print("Class: {}, prediction Result: {}".format(class_predicted[0], inv_map[class_predicted[0]]))   return load_img(path, target_size=(224,224))path = 'E:/My Work Elements/Thesis Related/Trash Classification/Trash New Code/test/cardboard/cardboard42.jpg'test_single_image(path)如何修復代碼?
查看完整描述

2 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您可以使用 keras VGG16 API 輕松獲取 VGG16 模型


from keras.applications.vgg16 import VGG16

from keras.preprocessing import image

from keras.applications.vgg16 import preprocess_input

import numpy as np


model = VGG16(weights='imagenet', include_top=False)


img_path = 'elephant.jpg'

img = image.load_img(img_path, target_size=(224, 224))

x = image.img_to_array(img)

x = np.expand_dims(x, axis=0)

x = preprocess_input(x)


features = model.predict(x)

你read_image方法改變load_image方法。keras API 將調整圖像大小以滿足 vgg16 模型請求。


查看完整回答
反對 回復 2022-06-02
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

如果您使用 opencv 讀取圖像,它應該是 cv2.imread()。就我使用 vgg16 而言,應該有一個 read_image 函數之前定義,它接收任何形狀的圖像,將其調整為標準形狀(224 * 224 * 3)并進行所需的任何其他類型的預處理。


如果您使用的是 keras 預處理,它有一個預定義的 load_img 函數,可以為您進行預處理。


例子 :


from keras.preprocessing.image import load_img

load_image(path,target_size= (224*224*3))

我的樣本加載功能


from keras.preprocessing import image 

from keras.applications.imagenet_utils import decode_predictions, preprocess_input

def load_image(path): 

  img = image.load_img(path,target_size=model.input_shape[1:3]) 

  x = image.img_to_array(img) 

  x = np.expand_dims(x,axis=0) 

  x = preprocess_input(x) 

  return img,x


查看完整回答
反對 回復 2022-06-02
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號