为什么这个CNN的nan丢失并且精度为0?显示完整代码

我正在尝试创建一个可以识别字母(26个类)的NN。对于冗长的帖子,我深表歉意,但是我已经包括了所有相关代码,以使其尽可能清晰。最后,我解释了这个问题。

下面的块是我正确命名路径,标准化/标准化图像并准备进行训练的地方。

X = [] # Image data
y = [] # Labels

#datagen = ImageDataGenerator(samplewise_center=True)

# Loops through imagepaths to load images and labels into arrays
for path in imagepaths:
  img = cv2.imread(path) # Reads image and returns np.array
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Converts into the corret colorspace (GRAY) #find rgb 
  img = cv2.resize(img, (75, 75)) # Reduce image size so training can be faster
  img = image.img_to_array(img)
  #img = datagen.standardize(img)
  X.append(img)


  # Processing label in image path
  category = path.split("\\")[1]
  #print(category)
  split = (category.split("_"))     
  if int(split[0]) == 0:
    label = int(split[1])
  else:
    label = int(split[0])
  y.append(label)

# Turn X and y into np.array to speed up train_test_split

X = np.array(X, dtype="float") #ORIGINAL uint8
X = X.astype(np.float) / 255.

X = X.reshape(len(imagepaths), 75, 75, 1) # Needed to reshape so CNN knows it's different images, 1 for bw change to 3 for rgb
y = np.array(y)

创建测试集。

ts = 0.3 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=ts, random_state=42)

创建模型。密度为26,每个字母一个输出,大小为200,200,1以匹配输入:

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(200, 200, 1))) 
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu')) 
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(26, activation='softmax'))

模型编译器和拟合:

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 
model.fit(X_train, y_train, epochs=1, batch_size=64, verbose=1, validation_data=(X_test, y_test))

问题出在这里,我的model.fit的输出是:

Train on 54600 samples, validate on 23400 samples
Epoch 1/1
54600/54600 [==============================] - 23s 419us/step - loss: nan - accuracy: 1.8315e-05 - val_loss: nan - val_accuracy: 0.0000e+00

我知道这可能不是很高的准确性,也可能一无所获,但是为什么损失会减少呢?我在其他地方发布了信息,并首先被告知要对我的数据进行规范化(为此问题我对此进行了修复)。然后我被告知,我的数据集可能已损坏或泄漏-情况并非如此,因为当我一次不写26个字母时,它就可以正常工作。 (意思是我使用字母A-J,密度= 10等测试了代码),并获得了约95%的高精度。

感谢您的任何帮助,我已经在这里抓了几个小时了!