iT邦幫忙

0

python辨識影像預測問題矩陣資料不相同

  • 分享至 

  • xImage

json_file = open('model_ImageDataGenerator_myImage.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = tf.keras.models.model_from_json(loaded_model_json)
model.load_weights("model_ImageDataGenerator_myImage.h5")
model.compile(loss=tf.keras.losses.categorical_crossentropy,
optimizer=tf.keras.optimizers.Adadelta(),
metrics=['accuracy'])

model.summary()

opt1 = tf.keras.optimizers.Adam(lr=0.001)
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])

img = cv2.imread('F:/1100317_rotate/glue/IMG_4871.bmp')

w=200
h=200
X=[]

img = cv2.resize(img, (w,h), interpolation=cv2.INTER_AREA)

img=img.reshape(w,h,3)
img = img.astype('uint8')
img = cv2.resize(img, (200, 200), interpolation=cv2.INTER_AREA)
im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
X.append(im_rgb)
X = np.asarray(X)
X = X.astype('float32')
X=X/255
X=X.reshape(X.shape[0],w,h,3);
predict = model.predict(X)

想詢問一下
ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 960000 but received input with shape [None, 1280000]

不太懂哪邊資料錯誤
模組使用訓練圖片之預處理為

X=[]
Y=[]
print(dirs);
w=200 # 1000
h=200 # 1000
i=0
for name in dirs:
#file_paths = glob.glob(path.join(IMAGEPATH+"/"+name, '.'))
file_paths = glob.glob(path.join(IMAGEPATH,name))
file_name = os.listdir(file_paths[0])
for path3 in file_name :
print(path.join(file_paths[0], path3))
img = cv2.imread(path3)
img = cv2.imread(path.join(file_paths[0], path3))
if img is not None:
img = cv2.resize(img, (w,h), interpolation=cv2.INTER_AREA)
im_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
X.append(im_rgb)
Y.append(i)
i=i+1
print(path3)
X = np.asarray(X)
Y = np.asarray(Y)
print(X)
print(Y)
print("+++++++++Done reading!!+++++++++")

X = X.astype('float32')
X=X/255
X=X.reshape(X.shape[0],w,h,3);

是以相同影像處理方式做預處理
為何無法判別呢

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
I code so I am
iT邦高手 1 級 ‧ 2021-04-26 07:33:50
最佳解答
  1. 錯誤訊息顯示餵入模型的最後一個維度應為960000,但你餵入 [None, 1280000],可使用model.summary() 看第一層應輸入的維度。
  2. resize 2 次。
  3. 通常CNN預測時,應該是 (筆數, 寬, 高, 色彩通道),第一個參數怪怪的。
X=X.reshape(X.shape[0],w,h,3)

我想啟另一個話題:
Hi, I code so I am. 我剛去看過你寫的文章,你是神人等級了。
(我拍拍手表示認同)

/images/emoticon/emoticon37.gif

godman08 iT邦新手 5 級 ‧ 2021-04-28 20:51:30 檢舉

Model: "sequential"

Layer (type) Output Shape Param #
conv2d (Conv2D) (None, 200, 150, 32) 896


已改成原本第一層維度的條件下

w=200
h=150

img = cv2.resize(img, (w,h), interpolation=cv2.INTER_AREA)

img = img.reshape((1,w,h,32))
predict = model.predict(img)
cv2.imshow('image', predict)
cv2.waitKey(0)
cv2.destroyAllWindows()

i = np.argmax(img[0])
str1 = dirs[i] + " " + str(im_bgr[0][i])
print(str1);
img = cv2.putText(im_bgr,str1, (30,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0, 0), 1, cv2.LINE_AA)
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

想問一下改成這樣但卻執行程式毫無反應
預想為會顯示出圖形並上字幕為預測%數

我要發表回答

立即登入回答