我试图使用Keras构建图像分类神经网络来识别国际象棋棋盘上的方格是否包含黑色或白色的棋子。我通过翻转和旋转单一棋套中的所有棋子,创建了256张大小为45 x 45的黑白棋子图片。由于训练样本数量相对较少,并且我是Keras的新手,因此我在创建模型时遇到了困难。
图像文件夹的结构如下所示:
-数据
---训练数据
--------黑色
--------白色
---验证数据
--------黑色
--------白色
链接压缩文件在这里(仅1.78 MB)
我尝试的代码基于这个,可以在这里看到:
输出结果似乎并没有表明任何“学习”已经完成,因为验证数据的准确性只有0.5000,即使它成功地以99%的准确率获得了20个测试图像(correct),但它的情况不确定。
图像文件夹的结构如下所示:
-数据
---训练数据
--------黑色
--------白色
---验证数据
--------黑色
--------白色
链接压缩文件在这里(仅1.78 MB)
我尝试的代码基于这个,可以在这里看到:
# Imports components from Keras
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import layers
import numpy as np
from PIL import Image
from tensorflow.python.ops.gen_dataset_ops import prefetch_dataset
import matplotlib.pyplot as plt
import glob
# Initializes a sequential model
model = Sequential()
# First layer
model.add(Dense(10, activation='relu', input_shape=(45*45*3,)))
# Second layer
model.add(Dense(10, activation='relu'))
# Output layer
model.add(Dense(2, activation='softmax'))
# Compile the model
model.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])
#open training data as np array
filelist = glob.glob('Data/Training Data/black/*.png')
train_dataBlack = np.array([np.array(Image.open(fname)) for fname in filelist])
filelist = glob.glob('Data/Training Data/white/*.png')
train_dataWhite = np.array([np.array(Image.open(fname)) for fname in filelist])
train_data = np.append(train_dataBlack,train_dataWhite)
#open validation data as np array
filelist = glob.glob('Data/Validation Data/black/*.png')
test_dataBlack = np.array([np.array(Image.open(fname)) for fname in filelist])
filelist = glob.glob('Data/Validation Data/white/*.png')
test_dataWhite = np.array([np.array(Image.open(fname)) for fname in filelist])
test_data = np.append(test_dataBlack,test_dataWhite)
test_labels = np.zeros(shape=(256,2))
#initializing training labels numpy array
train_labels = np.zeros(shape=(256,2))
i = 0
while(i < 256):
if(i < 128):
train_labels[i] = np.array([1,0])
else:
train_labels[i] = np.array([0,1])
i+=1
#initializing validation labels numpy array
i = 0
while(i < 256):
if(i < 128):
test_labels[i] = np.array([1,0])
else:
test_labels[i] = np.array([0,1])
i+=1
#shuffling the training data and training labels in the same way
rng_state = np.random.get_state()
np.random.shuffle(train_data)
np.random.set_state(rng_state)
np.random.shuffle(train_labels)
# Reshape the data to two-dimensional array
train_data = train_data.reshape(256, 45*45*3)
# Fit the model
model.fit(train_data, train_labels, epochs=10,validation_split=0.2)
#save/open model
model.save_weights('model_saved.h5')
model.load_weights('model_saved.h5')
# Reshape test data
test_data = test_data.reshape(256, 45*45*3)
# Evaluate the model
model.evaluate(test_data, test_labels)
#testing output for a single image
img = test_data[20]
img = img.reshape(1,45*45*3)
predictions = model.predict(img)
print(test_labels[20])
print(predictions*100)
输出结果似乎并没有表明任何“学习”已经完成,因为验证数据的准确性只有0.5000,即使它成功地以99%的准确率获得了20个测试图像(correct),但它的情况不确定。
Epoch 1/10
7/7 [==============================] - 0s 22ms/step - loss: 76.1521 - accuracy: 0.4804 - val_loss: 34.4301 - val_accuracy: 0.6346
Epoch 2/10
7/7 [==============================] - 0s 3ms/step - loss: 38.9190 - accuracy: 0.4559 - val_loss: 19.3758 - val_accuracy: 0.3846
Epoch 3/10
7/7 [==============================] - 0s 3ms/step - loss: 18.7589 - accuracy: 0.5049 - val_loss: 35.1795 - val_accuracy: 0.3654
Epoch 4/10
7/7 [==============================] - 0s 3ms/step - loss: 18.5703 - accuracy: 0.5000 - val_loss: 4.7349 - val_accuracy: 0.5962
Epoch 5/10
7/7 [==============================] - 0s 3ms/step - loss: 6.5564 - accuracy: 0.5539 - val_loss: 10.1864 - val_accuracy: 0.4423
Epoch 6/10
7/7 [==============================] - 0s 3ms/step - loss: 6.8870 - accuracy: 0.5833 - val_loss: 11.2020 - val_accuracy: 0.4038
Epoch 7/10
7/7 [==============================] - 0s 3ms/step - loss: 7.3905 - accuracy: 0.5343 - val_loss: 17.9842 - val_accuracy: 0.3846
Epoch 8/10
7/7 [==============================] - 0s 3ms/step - loss: 6.3737 - accuracy: 0.6029 - val_loss: 13.0180 - val_accuracy: 0.4038
Epoch 9/10
7/7 [==============================] - 0s 3ms/step - loss: 6.2868 - accuracy: 0.5980 - val_loss: 14.8001 - val_accuracy: 0.3846
Epoch 10/10
7/7 [==============================] - 0s 3ms/step - loss: 5.0725 - accuracy: 0.6618 - val_loss: 18.7289 - val_accuracy: 0.3846
8/8 [==============================] - 0s 1ms/step - loss: 21.6894 - accuracy: 0.5000
[1. 0.]
[[99 1]]
我对几乎所有的东西都感到困惑:
- 层数
- 每层神经元数量
- 各层类型
- 每个epoch的步数
- epoch的数量
我尝试过各种变量的实验,但似乎没有什么帮助。
提前感谢您的回复!