Keras模型无法解决多类分类问题

时间:2019-05-02 18:51:34

标签: python keras

我想了解有关机器学习/深度学习的更多信息,因此我一直在尝试解决Kaggle Diabetic Retinopathy竞赛作为一种学习体验。 但是,我的Keras模型的准确性和损失函数似乎没有改善。

我下载了糖尿病视网膜病变数据集。平衡类并创建均匀分布的批次,每批次100张图像。我尝试了许多参数化组合,例如更多的时期,不同的学习率,更复杂的模型等等。它们似乎都没有作用。这是我的代码。

我的进口商品:

from tqdm import tqdm
import os
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten
from keras import optimizers
from keras.callbacks import ModelCheckpoint
from keras import backend as K
K.tensorflow_backend._get_available_gpus()

我的参数:

HEIGHT = 512
WIDTH = 512
DEPTH = 3
inputShape = (HEIGHT, WIDTH, DEPTH)
NUM_CLASSES = 5
EPOCHS = 15
INIT_LR = 0.001
BS = 1

我检查给定目录中的批次:

''' read batches '''
train_dir = '/DATA/npy_data/train_dir/'
batch_path_list = []
for batch in tqdm(os.listdir(train_dir)):
    batch_full_path = os.path.join(os.path.sep, train_dir, batch)
    batch_path_list.append(str(batch_full_path))

AMOUNT_OF_BATCHES = len(batch_path_list)

if AMOUNT_OF_BATCHES == 0:
    print('We found no batches. Either no data or wrong directory...')

if AMOUNT_OF_BATCHES != 0:
    print('We found ' + str(AMOUNT_OF_BATCHES) + ' batches.')

我阅读了CSV文件以获得标签

''' read csv labels '''
csv_dir = '/DATA/data/trainLabels_normalised.csv'
dataframe = pd.read_csv(csv_dir, sep=',')
patientIDList = []
for index, row in dataframe.iterrows():
    patientID = row[0] + ''
    patientID = patientID.replace('_right', '')
    patientID = patientID.replace('_left', '')
    dataframe.at[index, 'PatientID'] = patientID
    patientIDList.append(patientID)

我创建并编译我的模型

model = Sequential(name='test')

model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=inputShape))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(activation='softmax', units=5))

opt = optimizers.SGD(decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy", "mse"])

checkpointer = ModelCheckpoint(filepath="/home/user/Desktop/code/model/best_weights.hdf5",
                               verbose=1,
                               save_best_only=True)

我加载了一个批次,并将标签与该批次中的100张图像连接在一起。

''' load batches '''
for item in batch_path_list:
    batch_data = np.load(item).tolist()
    df1 = dataframe[dataframe['image'].isin(batch_data)]
    imageNameArr = []
    dataArr = []

    for index, row in df1.iterrows():
        key = str(row[0])
        if key in batch_data:
            imageNameArr.append(key)
            dataArr.append(batch_data[key])

    df2 = pd.DataFrame({'image': imageNameArr, 'data': dataArr})
    for idx in range(0, len(df1)):
        if (df1.loc[df1.index[idx], 'image'] != df2.loc[df2.index[idx], 'image']):
            print("Error " + df1.loc[df1.index[idx], 'image'] + "==" + df2.loc[df2.index[idx], 'image'])

    merged_files = pd.merge(df2, df1, left_on='image', right_on='image', how='outer')

我生成拆分

    train_ids, valid_ids = train_test_split(patientIDList, test_size=0.25, random_state=10)
    traindf = merged_files[merged_files.PatientID.isin(train_ids)] #data (data) image (img name) level (fase)
    valSet = merged_files[merged_files.PatientID.isin(valid_ids)]
    trainX = traindf['data']
    trainY = traindf['level']
    valX = valSet['data']
    valY = valSet['level']
    trainY = to_categorical(trainY, num_classes=NUM_CLASSES)
    valY = to_categorical(valY, num_classes=NUM_CLASSES)
    Xtrain = np.zeros([trainX.shape[0], HEIGHT, WIDTH, DEPTH])
    Xval = np.zeros([valX.shape[0], HEIGHT, WIDTH, DEPTH])

我使用生成器并调用fit函数。

    aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
                              height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                              horizontal_flip=True, fill_mode="nearest")


    model.fit_generator(aug.flow(Xtrain, trainY,
                        batch_size=BS),
                        validation_data=(Xval, valY),
                        steps_per_epoch=(len(trainX) // BS),
                        epochs=EPOCHS,
                        verbose=1,
                        callbacks=[checkpointer])

但是,这导致非常低的精度,并且在29个批次中似乎没有改善。

结果:

  

54/87 [================> ............]-ETA:0s-损失:1.6092-acc:0.2037 -均方误差:0.1599   56/87 [================== ..............]-ETA:0s-损失:1.6089-acc:0.2143-mean_squared_error: 0.1598   58/87 [==================> ..........]-ETA:0s-损失:1.6169-acc:0.2069-mean_squared_error: 0.1605   60/87 [==================> ..........]-ETA:0s-损失:1.6146-acc:0.2167-mean_squared_error: 0.1602   62/87 [===================> .........]-ETA:0秒-损失:1.6172-acc:0.2097-mean_squared_error: 0.1605   64/87 [====================> ........]-ETA:0s-损失:1.6196-acc:0.2031-mean_squared_error: 0.1607   66/87 [====================> ........]-ETA:0s-损失:1.6180-acc:0.2121-mean_squared_error: 0.1605   68/87 [=====================> .......]-ETA:0s-损失:1.6164-acc:0.2206-mean_squared_error: 0.1604   70/87 [=====================> ......]-ETA:0s-损失:1.6144-acc:0.2286-mean_squared_error: 0.1602   72/87 [======================> ......]-ETA:0s-损失:1.6163-acc:0.2222-mean_squared_error: 0.1604   74/87 [======================> .....]-ETA:0秒-损失:1.6134-acc:0.2297-mean_squared_error: 0.1601   76/87 [=======================> ....]-ETA:0s-损失:1.6102-acc:0.2368-mean_squared_error: 0.1598   78/87 [=======================> ....]-ETA:0s-损失:1.6119-acc:0.2308-mean_squared_error: 0.1600   80/87 [========================== ......]-ETA:0s-损失:1.6159-acc:0.2250-mean_squared_error: 0.1604   82/87 [=========================== ..]-ETA:0秒-损失:1.6150-acc:0.2195-mean_squared_error: 0.1603   84/87 [==========================> ..]-ETA:0秒-损失:1.6206-acc:0.2143-mean_squared_error: 0.1608   86/87 [===========================>。]-ETA:0s-损失:1.6230-acc:0.2093-mean_squared_error: 0.1610   87/87 [==============================]-3s 31ms / step-损失:1.6234-acc:0.2069-mean_squared_error :0.1610-val_loss:1.6435-val_acc:0.1282-val_mean_squared_error:0.1629   纪元00015:val_loss从1.57533没有改善

我们非常感谢您提出的建议和反馈,以改进我的模型!

0 个答案:

没有答案