iT邦幫忙

0

Python_open.cv-tensorflow training model 問題

IMAGEPATH = "images"
dirs = os.listdir(IMAGEPATH)
X=[]
Y=[]
print(dirs);
w=400 # 224
h=300 # 224
i=0
for name in dirs:
    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)

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.01)
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(x_train.shape)
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
                            rotation_range=25 ,
                            width_shift_range=[-3,3],
                            height_shift_range=[-3,3] ,
                            zoom_range=0.3 ,
							data_format='channels_last')

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3),
                 padding="same",
                 activation='relu',
                 input_shape=(w,h,3)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(50, activation='relu'))
model.add(tf.keras.layers.Dense(units=category,
    activation=tf.nn.softmax ))


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

https://ithelp.ithome.com.tw/upload/images/20210205/201192388IELj0BSPN.pnghttps://ithelp.ithome.com.tw/upload/images/20210205/20119238aRF4slB5dJ.pnghttps://ithelp.ithome.com.tw/upload/images/20210205/20119238FmsNk99oNL.png
可以詢問一下
我共有八種類型圖像分類
各1-36張
2-23張
3-8張
4-1張
5-24張
6-9張
7-9張
8-7張
共117張
為什麼X與Y出來只有115張呢
另外
ValueError: Shapes (3840000, 50) and (32768, 50) are incompatible
這是指我哪兩個不相容呢?

麻煩大神幫幫新手小弟
感謝感謝

我猜圖片檔名可能有中文或非法字元
錯誤的部分可能要看一下 model.fit 的程式

1 個回答

0
hokou
iT邦新手 3 級 ‧ 2021-02-08 08:48:48
最佳解答

延續之前的討論
https://ithelp.ithome.com.tw/questions/10202022

你的 test_size = 0.01
117 x 0.01 = 1.17 => 2張在 test,115張在 train

另外錯誤都是有跡可循,建議從錯誤的行數開始查問題

我自己會先用設定的跑看看,再逐一調整

model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

參考資料
https://stackoverflow.com/questions/61742556/valueerror-shapes-none-1-and-none-2-are-incompatible
https://www.tensorflow.org/tutorials/images/cnn

godman08 iT邦新手 5 級 ‧ 2021-02-08 22:38:24 檢舉

score = model.evaluate(x_test, y_test2, batch_size=16)

print("score:",score)

predict = model.predict(x_test)
print("Ans:",np.argmax(predict[0]),np.argmax(predict[1]),np.argmax(predict[2]),np.argmax(predict[3]))

predict2 = model.predict_classes(x_test)
print("predict_classes:",predict2)
print("y_test",y_test[:])

for t1 in predict2:
print(dirs[t1])

#--------------------OK
img=x_test[0]
img=img.reshape(w,h,3)
img=img*255
img = img.astype('uint8')
img = cv2.resize(img, (400, 300), 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()
目前程式已經可以正常跑動
但是這行BUG我還是不太懂呢
我的是multi-class classification 所以我是用softmax 而非sigmoid這樣不對嗎?
改成sigmoid也是如此出現bug
Please use instead:* np.argmax(model.predict(x), axis=-1), if your model does multi-class classification (e.g. if it uses a softmax last-layer activation).* (model.predict(x) > 0.5).astype("int32"), if your model does binary classification (e.g. if it uses a sigmoid last-layer activation).

我要發表回答

立即登入回答