Мне нужна помощь, чтобы завершить мой код, который классифицирует изображения рака молочной железы с помощью CNN.
Мне нужна модель для прогнозирования на основе случайных изображений, но она продолжает давать один класс прогнозирования (инвазивный) для всех изображений, которые Всего 306 штук. Кроме того, мне нужно рассчитать матрицу точности и неточности. Я ценю помощь. Некоторые части кода ниже:
s=100
X_train = []
y_train = []
for folder in os.listdir(trainpath +'TrainingSet') :
files = gb.glob(pathname= str( trainpath +'TrainingSet//' + folder + '/*.png'))
for file in files:
image = cv2.imread(file)
image_array = cv2.resize(image , (s,s))
X_train.append(list(image_array))
y_train.append(code[folder])
X_pred = []
s=100
files = gb.glob(pathname= str(testpath + 'ValidationSet/*.png'))
for file in files:
image = cv2.imread(file)
image_array = cv2.resize(image , (s,s))
X_pred.append(list(image_array))
X_train = np.array(X_train)
y_train = np.array(y_train)
X_pred_array = np.array(X_pred)
print(f'X_train shape is {X_train.shape}')
print(f'y_train shape is {y_train.shape}')
print(f'X_pred shape is {X_pred_array.shape}')
KerasModel = keras.models.Sequential([
keras.layers.Conv2D(200,kernel_size=(3,3),activation='relu',input_shape=(s,s,3)),
keras.layers.Conv2D(150,kernel_size=(3,3),activation='relu'),
keras.layers.MaxPool2D(4,4),
keras.layers.Conv2D(120,kernel_size=(3,3),activation='relu'),
keras.layers.Conv2D(80,kernel_size=(3,3),activation='relu'),
keras.layers.Conv2D(50,kernel_size=(3,3),activation='relu'),
keras.layers.MaxPool2D(4,4),
keras.layers.Flatten() ,
keras.layers.Dense(120,activation='relu') ,
keras.layers.Dense(100,activation='relu') ,
keras.layers.Dense(50,activation='relu') ,
keras.layers.Dropout(rate=0.5) ,
keras.layers.Dense(3,activation='softmax') ,
])
KerasModel.compile(optimizer ='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
print('Model Details are : ')
print(KerasModel.summary())
epochs = 15
ThisModel = KerasModel.fit(X_train, y_train, epochs=epochs,batch_size=16,verbose=1, callbacks=[learn_control, checkpoint])
ModelLoss, ModelAccuracy = KerasModel.evaluate(X_train, y_train)
print('Total Loss is {}'.format(ModelLoss))
print('Total Accuracy is {}'.format(ModelAccuracy ))
y_result = KerasModel.predict(X_pred_array)
print('Prediction Shape is {}'.format(y_result.shape))
import warnings
warnings.filterwarnings('always')
warnings.filterwarnings('ignore')
import fnmatch
import os
from sklearn.metrics import confusion_matrix
image_path = 'Data\ValidationSet'
for filename in os.listdir(image_path):
filename = os.path.basename(filename)
Actual_list = []
Predicted_list = []
if any(map(filename.startswith, 'InSitu_')):
Actual='Insitu'
if any(map(filename.startswith, 'SOB_B_')):
Actual='Benign'
if any(map(filename.startswith, 'SOB_M_')):
Actual='Invasive'
plt.figure(figsize=(20,40))
for n , i in enumerate(list(np.random.randint(0,len(X_pred),306))) :
plt.subplot(34,9,n+1)
plt.imshow(X_pred[i])
plt.axis('off')
Predicted= getcode(np.argmax(y_result[i]))
plt.title('Predicted: ' + Predicted +'\n' + 'Actual:' + Actual)
Actual_list.append(Actual)
Predicted_list.append(Predicted)
Результат тренировочного процесса:
Epoch 1/15
1231/1231 [===] - 284s 230ms/step - loss: 0.6721 - accuracy: 0.7295
Epoch 2/15
1231/1231 [===] - 279s 227ms/step - loss: 0.6698 - accuracy: 0.7295
Epoch 3/15
1231/1231 [===] - 259s 211ms/step - loss: 0.6784 - accuracy: 0.7295
Epoch 4/15
1231/1231 [===] - 263s 214ms/step - loss: 0.6751 - accuracy: 0.7295
Epoch 5/15
1231/1231 [===] - 255s 207ms/step - loss: 0.6715 - accuracy: 0.7295
Epoch 6/15
1231/1231 [===] - 255s 207ms/step - loss: 0.6695 - accuracy: 0.7295
Epoch 7/15
1231/1231 [===] - 253s 205ms/step - loss: 0.6737 - accuracy: 0.7295
Epoch 8/15
1231/1231 [===] - 256s 208ms/step - loss: 0.6730 - accuracy: 0.7295
Epoch 9/15
1231/1231 [===] - 263s 214ms/step - loss: 0.6733 - accuracy: 0.7295
Epoch 10/15
1231/1231 [===] - 277s 225ms/step - loss: 0.6741 - accuracy: 0.7295
Epoch 11/15
1231/1231 [===] - 279s 227ms/step - loss: 0.6698 - accuracy: 0.7295
Epoch 12/15
1231/1231 [===] - 254s 206ms/step - loss: 0.6743 - accuracy: 0.7295
Epoch 13/15
1231/1231 [===] - 255s 207ms/step - loss: 0.6758 - accuracy: 0.7295
Epoch 14/15
1231/1231 [===] - 254s 206ms/step - loss: 0.6722 - accuracy: 0.7295
Epoch 15/15
1231/1231 [===] - 257s 209ms/step - loss: 0.6706 - accuracy: 0.7295
Общая потеря и точность:
1231/1231 [= =============================] - 114 с 93 мс / шаг Общая потеря 0,667820717773236 Общая точность 0,7294881939888