当前位置:   article > 正文

InceptionV3详细代码,带准确率和loss分析,以及ROC曲线(python)_python inception loss计算

python inception loss计算
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import time
start_time = time.time()
import numpy as np
import matplotlib.pyplot as plt
from keras.callbacks import Callback,ModelCheckpoint
from keras.models import Model
from keras.layers import Dense, Input,Conv2D, MaxPooling2D, Dropout, Flatten, Activation, BatchNormalization,GlobalAveragePooling2D
import keras as ks
from sklearn.model_selection import train_test_split
from generator import s2_generator
from keras.utils.vis_utils import plot_model
from sklearn.metrics import recall_score,accuracy_score
from sklearn.metrics import precision_score,f1_score
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from itertools import cycle
from scipy import interp
from sklearn.preprocessing import label_binarize
import Adapt_categorical_crossentropy as ACC
from keras.callbacks import Callback,ModelCheckpoint
from keras.utils import multi_gpu_model
from keras.optimizers import Adam,SGD,sgd
from LRFinder import LRFinder
from clr_callbackR import CyclicLR
from step_decay import step_decay_schedule
from keras.applications.vgg19 import VGG19
from keras.applications.vgg16 import VGG16
from keras.applications.xception import Xception
from keras.applications.inception_v3 import InceptionV3
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.mobilenet import MobileNet
from keras.applications.densenet import DenseNet121,DenseNet169,DenseNet201
from keras.applications.resnet50 import ResNet50
from keras.applications.nasnet import NASNetLarge
from keras.applications.nasnet import NASNetMobile
from keras.layers.core import Reshape
from keras.layers import multiply

def build_model(nb_classes, input_shape=(299,299,3)):
    inputs_dim = Input(input_shape)
    x = InceptionV3(include_top=False,
                weights='imagenet',
                input_tensor=None,
                input_shape=(299, 299, 3),
                pooling=max)(inputs_dim)
    print(x.shape)
    # max_pooling = MaxPooling2D(10,10)(x)
    squeeze = GlobalAveragePooling2D()(x)

    excitation = Dense(units=2048 // 16)(squeeze)
    excitation = Activation('relu')(excitation)
    excitation = Dense(units=2048)(excitation)
    excitation = Activation('sigmoid')(excitation)
    excitation = Reshape((1, 1, 2048))(excitation)


    scale = multiply([x, excitation])

    x = GlobalAveragePooling2D()(scale)
    dp_1 = Dropout(0.3)(x)
    fc2 = Dense(nb_classes)(dp_1)
    fc2 = Activation('sigmoid')(fc2) #此处注意,为sigmoid函数
    model = Model(inputs=inputs_dim, outputs=fc2)
    return model

class ParallelModelCheckpoint(ModelCheckpoint):
    def __init__(self, model, filepath, monitor='val_loss', verbose=0,
                 save_best_only=True, save_weights_only=False,
                 mode='auto', period=1):
        self.single_model = model
        super(ParallelModelCheckpoint, self).__init__(filepath, monitor, verbose, save_best_only, save_weights_only,
                                                      mode, period)

    def set_model(self, model):
        super(ParallelModelCheckpoint, self).set_model(self.single_model)



if __name__ == '__main__':
    im_size1 = 299
    im_size2 = 299
    channels = 3
    nb_classes = 5
    epochs = 150
    min_lr = 1e-7
    max_lr = 1e-4

    X_train = np.load('../Divide_test/x_train_5_right.npy')
    Y_train = np.load('../Divide_test/y_train_5_right.npy')
    print("全部数据形状")
    print(X_train.shape)
    print(Y_train.shape)

    X_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=0.1, random_state=666)
    # X_train, X_test,  Y_train, Y_test  = train_test_split(X_train, Y_train, test_size=0.04, random_state=66)
    X_test = np.load('../Divide_test/x_test_5_right.npy')
    Y_test = np.load('../Divide_test/y_test_5_right.npy')
    print("训练集形状")
    print(X_train.shape)
    print(Y_train.shape)
    print("验证集形状")
    print(X_valid.shape)
    print(Y_valid.shape)
    print("测试集形状")
    print(X_test.shape)
    print(Y_test.shape)

    Y_train = np.asarray(Y_train,np.uint8)
    Y_valid = np.asarray(Y_valid,np.uint8)
    Y_test  = np.asarray(Y_test,np.uint8)

    X_valid = np.asarray(X_valid, np.float32) / 255.
    X_test  = np.asarray(X_test,np.float32)  / 255.



    # This is the model we will train
    # single_model = NASNetMobile(weights=None, include_top=True, input_shape=(im_size1, im_size2, channels),
    #                            classes=nb_classes)
    single_model = build_model(nb_classes, input_shape=(im_size1, im_size2, channels))
    plot_model(single_model, to_file="../images/images/SE-InceptionV3_model.png", show_shapes=True, show_layer_names=True)
    model = multi_gpu_model(single_model, 2)
    early_stopping = ks.callbacks.EarlyStopping(monitor='val_acc', patience=5)

    filepath = './model/SE-InceptionV3_model.h5'
    saveBestModel = ParallelModelCheckpoint(single_model,'./model/SE-InceptionV3_model.h5',monitor='val_acc',
                                                 verbose=1, save_best_only=True, mode='auto')

    tensorboard = ks.callbacks.TensorBoard(log_dir=r'./RESNET_ray/SE-InceptionV3_model')
    opt = Adam(lr=1e-4)
    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])# binary_crossentropy,categorical_crossentropy
    model.summary()

    batch_size=100
    a = X_train.shape[0]
    '''
        学习率设置:lr_finder:找寻最优学习速率的系统化方法
              reduce_lr:一般设置学习率方法,验证准确率不提高就减少准确率
                 clr_triangular:周期性学习率 //效果不是太好,可能参数不对
                  step_decay : 学习速率退火的最流行方式是「步衰减」(Step Decay),类似于reduce_lr
                 
    '''
    # lr_finder = LRFinder(min_lr=1e-7, max_lr=1e-4, steps_per_epoch=np.ceil(a // batch_size),
    #                      epochs=10)
    reduce_lr = ks.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.3, verbose=1, patience=2,
                                               min_lr=1e-7)
    # clr_triangular = CyclicLR(base_lr=1e-6,max_lr=1e-4,mode='triangular',step_size=10*np.ceil(a // batch_size))
    #
    # step_decay = step_decay_schedule(initial_lr=1e-4, decay_factor=0.75, step_size=2)

    history = model.fit_generator(generator=s2_generator(X_train, Y_train, batch_size,a),
                        steps_per_epoch=int(X_train.shape[0] / batch_size)+ 1,
                        epochs=epochs, validation_data=(X_valid, Y_valid), verbose=1,
                        callbacks=[early_stopping, saveBestModel, tensorboard,reduce_lr])
    # lr_finder.plot_loss()
    # lr_finder.plot_lr()

# 訓練集和驗證機——損失函數和準確率變化曲線
    val_loss = history.history['val_loss']
    val_acc = history.history['val_acc']
    train_loss = history.history['loss']
    train_acc = history.history['acc']

# 損失函數曲線
    plt.figure(figsize=(10, 4))
    plt.ylim(0, 1.5)
    plt.plot(train_loss, 'b', label='Training loss')
    plt.plot(val_loss, 'r', label='Validation loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    plt.savefig("../images/ROC/SE-InceptionV3_model_5分类_loss.png")
    # plt.show()

# 準確率變化曲線
    plt.figure(figsize=(10, 4))
    plt.ylim(0, 1)
    plt.plot(train_acc, 'b', label='Training Accuracy')
    plt.plot(val_acc, 'r', label='Validation Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.savefig("../images/ROC/SE-InceptionV3_model_5分类_Accuracy.png")
    # plt.show()


    print("Predicting")
    Y_pred = model.predict(X_test)
    Y_pred = [np.argmax(y) for y in Y_pred]  # 取出y中元素最大值所对应的索引
    Y_test = [np.argmax(y) for y in Y_test]

    # Binarize the output
    Y_test = label_binarize(Y_test, classes=[i for i in range(nb_classes)])
    Y_pred = label_binarize(Y_pred, classes=[i for i in range(nb_classes)])

    # micro:多分类  
    # weighted:不均衡数量的类来说,计算二分类metrics的平均
    # macro:计算二分类metrics的均值,为每个类给出相同权重的分值。
    precision = precision_score(Y_test, Y_pred, average='micro')
    recall = recall_score(Y_test, Y_pred, average='micro')
    f1_score = f1_score(Y_test, Y_pred, average='micro')
    accuracy_score = accuracy_score(Y_test, Y_pred)
    print("Precision_score:", precision)
    print("Recall_score:", recall)
    print("F1_score:", f1_score)
    print("Accuracy_score:", accuracy_score)

    # roc_curve:真正率(True Positive Rate , TPR)或灵敏度(sensitivity)
    # 横坐标:假正率(False Positive Rate , FPR)

    # Compute ROC curve and ROC area for each class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(nb_classes):
        fpr[i], tpr[i], _ = roc_curve(Y_test[:, i], Y_pred[:, i])
        roc_auc[i] = auc(fpr[i], tpr[i])

    # Compute micro-average ROC curve and ROC area
    fpr["micro"], tpr["micro"], _ = roc_curve(Y_test.ravel(), Y_pred.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

    # Compute macro-average ROC curve and ROC area

    # First aggregate all false positive rates
    all_fpr = np.unique(np.concatenate([fpr[i] for i in range(nb_classes)]))

    # Then interpolate all ROC curves at this points
    mean_tpr = np.zeros_like(all_fpr)
    for i in range(nb_classes):
        mean_tpr += interp(all_fpr, fpr[i], tpr[i])

    # Finally average it and compute AUC
    mean_tpr /= nb_classes

    fpr["macro"] = all_fpr
    tpr["macro"] = mean_tpr
    roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])

    # Plot all ROC curves
    lw = 2
    plt.figure()
    plt.plot(fpr["micro"], tpr["micro"],
             label='micro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["micro"]),
             color='deeppink', linestyle=':', linewidth=4)

    plt.plot(fpr["macro"], tpr["macro"],
             label='macro-average ROC curve (area = {0:0.2f})'
                   ''.format(roc_auc["macro"]),
             color='navy', linestyle=':', linewidth=4)

    colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
    for i, color in zip(range(nb_classes), colors):
        plt.plot(fpr[i], tpr[i], color=color, lw=lw,
                 label='ROC curve of class {0} (area = {1:0.2f})'
                       ''.format(i, roc_auc[i]))

    plt.plot([0, 1], [0, 1], 'k--', lw=lw)
    plt.xlim([0.0, 1.0])
    plt.ylim([0.0, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title('Some extension of Receiver operating characteristic to multi-class')
    plt.legend(loc="lower right")
    plt.savefig("../images/ROC/SE-InceptionV3_model_5分类.png")
    # plt.show()



    # plt.figure()
    # plt.plot(clr_triangular.history['iterations'], clr_triangular.history['lr'])
    # plt.xlabel('Training Iterations')
    # plt.ylabel('Learning Rate')
    # plt.title("CLR - 'exp_range' Policy")
    # plt.savefig("../images/images/CLR-'exp_range' Policy.png")
    # plt.show()



    # 二分类
    # roc_curve:真正率(True Positive Rate , TPR)或灵敏度(sensitivity)
    # 横坐标:假正率(False Positive Rate , FPR)
    # fpr, tpr, thresholds_keras = roc_curve(Y_test, Y_pred)
    # auc = auc(fpr, tpr)
    # print("AUC : ", auc)
    # plt.figure()
    # plt.plot([0, 1], [0, 1], 'k--')
    # plt.plot(fpr, tpr, label='Keras (area = {:.3f})'.format(auc))
    # plt.xlabel('False positive rate')
    # plt.ylabel('True positive rate')
    # plt.title('ROC curve')
    # plt.legend(loc='best')
    # plt.savefig("../images/ROC/SE-InceptionV3_model.png")
    # plt.show()

    print("--- %s seconds ---" % (time.time() - start_time))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号