iT邦幫忙

0

tensorflow_影像預測問題

訓練影像程式碼
dirs = os.listdir(IMAGEPATH)
X=[]
Y=[]
print(dirs);
w=200
h=200
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);

category=len(dirs)
dim=X.shape[1]
x_train , x_test , y_train , y_test = train_test_split(X,Y,test_size=0.1)
#print(x_train.shape)
#print(y_train.shape)

y_train2 = tf.keras.utils.to_categorical(y_train, category)
y_test2 = tf.keras.utils.to_categorical(y_test, category)
print(y_train2)
print(y_test2)

想問一下影像圖片已經都規格化後也訓練好模型了
但要丟入新圖片進行預測卻有問題
影像格式不一致的狀況
ValueError: Shapes (5760000, 50) and (480000, 50) are incompatible

以下為預測程式碼:
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('2.bmp')

w=200
h=200

img=x_test[0]
img=img.reshape(w,h,3)
img=img*255
img = img.astype('uint8')
img = cv2.resize(img, (1000, 750), interpolation=cv2.INTER_AREA)
im_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
i = np.argmax(predict[0])
str1 = dirs[i] + " " + str(predict[0][i])
print(str1);

im_bgr = cv2.putText(im_bgr, str1, (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0, 0), 1, cv2.LINE_AA)
cv2.imshow('image', im_bgr)
cv2.waitKey(0)
cv2.destroyAllWindows()

再麻煩幫忙了
謝謝

你的訓練資料是1600×1200像素,待預測的卻是800×600像素。要馬放大預測資料,不然就reshape。而且不知道你的變數predict怎麼來的,不知道有沒有One Hot過。還有img=img*255的意義為何?
godman08 iT邦新手 5 級 ‧ 2021-04-21 09:08:55 檢舉
可是我訓練資料都改為200X200了不是嗎?
待預測也改成200X200了? 是我哪裡有沒注意到嗎?
訓練資料有One Hot過,因電腦不是很好不想讓他跑不動
img=img*255因為在訓練的時候有/255,想說要用imshow時要乘回來圖片才能顯示?
謝謝酸哥幫忙
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
beebus
iT邦見習生 ‧ 2021-04-20 16:47:14
【**此則訊息已被站方移除**】

尚未有邦友回答

立即登入回答