如何改善Inceptionv3模型?

时间:2019-12-25 12:09:35

标签: deep-learning predict transfer-learning model-fitting

我正在使用来自InceptionV3的预训练模型进行糖尿病性视网膜病变的检测,从现在开始我对模型进行训练,但是准确性如此之低,范围从0.16到0.20,因此,如果我想提高模型的准确性,应该怎么做?干嘛?

我的代码

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tqdm import tqdm
from keras.applications.inception_v3 import preprocess_input
from keras.models import Model
from keras.layers import  MaxPooling2D,Dense, Convolution2D, BatchNormalization, Activation,AveragePooling2D, Input, Flatten ,Dropout
from skimage import exposure, color
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint, Callback
from keras import regularizers
from keras_preprocessing.image import ImageDataGenerator
from sklearn.utils import class_weight
from collections import Counter
from sklearn.metrics import classification_report, confusion_matrix
from keras import backend as K
import itertools
import warnings
import scikitplot as skplt
from keras import models
from keras import layers
from keras import optimizers


config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth=True
session = tf.compat.v1.Session(config=config)
warnings.filterwarnings('ignore', category=UserWarning)

# Histogram equalization
def HE(img):
    img_eq = exposure.equalize_hist(img)
    return img_eq



def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')


def plotImages(images_arr):
    fig, axes = plt.subplots(1, 5, figsize=(20,20))
    axes = axes.flatten()
    for img, ax in zip( images_arr, axes):
        ax.imshow(img)
        ax.axis('off')
    plt.tight_layout()
    plt.show()
#Training parameter
batch_size =1
Epoch = 100
num_of_train_samples = 6000
num_of_validate_samples = 1000

train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    rotation_range=180,
    shear_range=0.2,
    zoom_range=0.4,
    horizontal_flip=True,
    fill_mode='constant',
    vertical_flip=True,
    preprocessing_function =preprocess_input

)

validation_datagen = ImageDataGenerator(
    rescale=1./255
)
test_datagen = ImageDataGenerator(
    rescale=1./255
)

#get image and label with augmentation
train = train_datagen.flow_from_directory(
'train/train_deep/',
target_size=(224,224),
class_mode='categorical',
shuffle=True,
color_mode="rgb",
batch_size = batch_size
)

test = test_datagen.flow_from_directory(
    'test_deep/',
    batch_size=1,
    class_mode=None,
    shuffle=False,
    color_mode="rgb",
    target_size = (224,224),

)

val = validation_datagen.flow_from_directory(
    'train/validate_deep/',
    target_size=(224,224),
    batch_size = batch_size,
    color_mode="rgb",
    shuffle=False,
    class_mode='categorical'
)
res_conv = tf.compat.v2.keras.applications.InceptionV3(weights='inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
                  include_top=False,
                  input_shape=(224, 224, 3),
                  pooling ='max'
                  )
res_conv.summary()
#Training
train_features = np.zeros(shape=(6000, 5, 5, 2048))
train_labels = np.zeros(shape=(6000,5))
#validate
validation_features = np.zeros(shape=(1000, 5, 5,2048))
validation_labels = np.zeros(shape=(1000,5))

class_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']   
class_weight = {0: 0.32903757,
                1: 1.6,
                2: 1.08597285,
                3: 3.93442623,
                4: 6.21761658}

#model
model = tf.compat.v2.keras.models.Sequential()
model.add(tf.compat.v2.keras.layers.Flatten())
model.add(tf.compat.v2.keras.layers.Dense(1024, activation='relu', input_dim=5 * 5 * 2048))
model.add(tf.compat.v2.keras.layers.Dropout(0.4))
model.add(tf.compat.v2.keras.layers.Dense(1024, activation='relu'))
model.add(tf.compat.v2.keras.layers.Dropout(0.2))
model.add(tf.compat.v2.keras.layers.Dense(5, activation='softmax'))

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              loss='categorical_crossentropy',
              metrics=['categorical_accuracy','acc'])
#training
i = 0
for inputs_batch, labels_batch in tqdm(train):
    print(labels_batch.shape)
    print(inputs_batch.shape)
    features_batch = res_conv.predict(inputs_batch)
    train_features[i * batch_size: (i + 1) * batch_size] = features_batch
    train_labels[i * batch_size: (i + 1) * batch_size] = labels_batch
    i += 1
    if i * batch_size >= num_of_train_samples:
        break

train_features = np.reshape(train_features, (num_of_train_samples, 5 * 5 * 2048))
#validate
i = 0
for inputs_batch, labels_batch in tqdm(val):
    features_batch = res_conv.predict(inputs_batch)
    validation_features[i * batch_size : (i + 1) * batch_size] = features_batch
    validation_labels[i * batch_size : (i + 1) * batch_size] = labels_batch
    i += 1
    if i * batch_size >= num_of_validate_samples:
        break
validation_features = np.reshape(validation_features, (num_of_validate_samples, 5 * 5 * 2048))
filepath="weight/densenet-{epoch:02d}-{val_categorical_accuracy:.2f}.hdf5"
checkpointer = tf.compat.v2.keras.callbacks.ModelCheckpoint(filepath,monitor='val_categorical_accuracy',mode='max', verbose=1, save_best_only=True,save_weights_only=True)
earlystopping = tf.compat.v2.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=50, min_delta=0.0001)
lr_reduction =  tf.compat.v2.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.1,cooldown=1,min_lr=0.000001)
callbacks_list = [checkpointer,lr_reduction,earlystopping]
#Validation
X_val , y_val = next(val)

history = model.fit(train_features,
                    train_labels,
                    epochs=Epoch,
                    batch_size=20,
                    validation_data=(validation_features,validation_labels),
                    callbacks=callbacks_list,
                    class_weight=class_weight,
                    shuffle=True
                    )


ground_truth = val.classes
label2index = val.class_indices
print(label2index)
idx2label = dict((v,k) for k,v in label2index.items())
predictions = model.predict_classes(validation_features)
prob = model.predict(validation_features)
y_prob = model.predict_proba(validation_features)
prob = np.argmax(prob,axis=1)
errors = np.where(predictions != ground_truth)[0]
print("No of errors = {}/{}".format(len(errors),1000))

print('Confusion Matrix')
cnf_matrix = confusion_matrix(val.classes, prob)
plt.figure(figsize=(15,8))
plot_confusion_matrix(cnf_matrix, classes=class_names,
                      title='Confusion matrix, without normalization')
print(cnf_matrix)
print('Classification Report')
target_names = ['No DR', 'Mild', 'Moderate', 'Severe', 'Proliferative DR']
print(classification_report(val.classes, prob, target_names=target_names))

plt.figure()
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['categorical_accuracy'])
plt.plot(history.history['val_categorical_accuracy'])
plt.title('model4 accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model4 loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

skplt.metrics.plot_roc_curve(ground_truth,y_prob)
plt.show()

我尝试了以下步骤:

  1. 更改过滤器大小。
  2. 添加更多隐藏层

但似乎什么也没发生。

0 个答案:

没有答案