簡體   English   中英

從已保存的 .h5 cnn 保存模型加載 val_acc 和 val_loss

[英]Loading val_acc and val_loss from a saved .h5 cnn saved model

我使用 Keras 構建了一個 CNN 分類器,繪制了超過 3 個時期的驗證准確性和驗證損失的歷史記錄,然后使用分類器.save("name.h5:) 保存了模型。

稍后我已經成功地使用 .load() 命令加載了分類器。 但是,我無法重新加載驗證准確性和驗證損失。 有什么辦法嗎?

我試過評估()函數但沒有用。

from keras.models import Sequential
from keras.layers import Conv2D,Activation,MaxPooling2D,Dense,Flatten,Dropout
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from IPython.display import display
import matplotlib.pyplot as plt
from PIL import Image
from keras.models import load_model
from sklearn.metrics import classification_report, confusion_matrix

classifier = Sequential()
classifier.add(Conv2D(32,(3,3),input_shape=(64,64,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(32,(3,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Conv2D(64,(3,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size =(2,2)))
classifier.add(Flatten())
classifier.add(Dense(64))
classifier.add(Activation('relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(2))
classifier.add(Activation('softmax'))
classifier.summary()
classifier.compile(optimizer ='rmsprop',
                   loss ='categorical_crossentropy',
                   metrics =['accuracy'])
train_datagen = ImageDataGenerator(rescale =1./255,
                                   shear_range =0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip =True)
test_datagen = ImageDataGenerator(rescale = 1./255)

batchsize=60
training_set = train_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/dogs-vs-cats/train/',
                                                target_size=(64,64),
                                                batch_size= batchsize,
                                                class_mode='categorical')

test_set = test_datagen.flow_from_directory('/home/osboxes/Downloads/Downloads/dogs-vs-cats/test/',
                                           target_size = (64,64),
                                           batch_size = batchsize,
                       shuffle=False,
                                           class_mode ='categorical')
history=classifier.fit_generator(training_set,
                        steps_per_epoch =9000 // batchsize,
                        epochs = 3,
                        validation_data =test_set,
                        validation_steps = 4500 // batchsize)

classifier.save('my_model3.h5')
Y_pred = classifier.predict_generator(test_set, steps=4500 // batchsize)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(test_set.classes, y_pred))
print('Classification Report')
target_names = test_set.classes
class_labels = list(test_set.class_indices.keys()) 
target_names = ['cats', 'dogs'] 
report = classification_report(test_set.classes, y_pred, target_names=class_labels)
print(report) 

# summarize history for accuracy
#plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

恐怕不是,History 是一個作為 fit() 函數的產品返回的對象。 模型本身不保留此信息,因此不會保存。

您可以恢復歷史記錄的唯一方法是您是否特別保存了它。

否則,如果您在第一次訓練模型時設置了一個隨機種子,您也可能會得到相同的結果(歷史)。 然后您可以使用相同的種子重復該過程並獲得相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM