【Kares】train_lossval_losstrain_accval_acc可视化第⼀种:使⽤ validation_split=0.2
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
training_vis(H)
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
del_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
from keras.utils.vis_utils import plot_model
sys.path.append('..')
from net.lenet import LeNet
# from lenet import LeNet
def args_parse():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-dtest", "--dataset_test", required=True,
help="path to input dataset_test")
ap.add_argument("-dtrain", "--dataset_train", required=True,
help="path to input dataset_train")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
return args
# initialize the number of epochs to train for, initial learning rate,
# and batch size
EPOCHS = 35
INIT_LR = 1e-3
BS = 32
CLASS_NUM = 2 #61
norm_size = 64 #32
# define the function
def training_vis(hist):
loss = hist.history['loss']
val_loss = hist.history['val_loss']
acc = hist.history['acc']
val_acc = hist.history['val_acc']
val_acc = hist.history['val_acc']
# make a figure
fig = plt.figure(figsize=(8,4))
# subplot loss
ax1 = fig.add_subplot(121)
ax1.plot(loss, label='train_loss')
ax1.plot(val_loss, label='val_loss')
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Loss')
ax1.set_title('Loss on Training and Validation Data')
ax1.legend()
# subplot acc
ax2 = fig.add_subplot(122)
ax2.plot(acc,label='train_acc')
ax2.plot(val_acc,label='val_acc')
ax2.set_xlabel('Epochs')
ax2.set_ylabel('Accuracy')
ax2.set_title('Accuracy on Training and Validation Data')
ax2.legend()
plt.tight_layout()
plt.savefig('plot_nogenerator.png')
#plt.show() 因为使⽤的是Agg,所以⽆法使⽤
def load_data(path):
print("[INFO] ")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input images
for imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = size(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
label = int(imagePath.split(os.path.sep)[1][0])
labels.append(label)
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# convert the labels from integers to vectors
labels = to_categorical(labels, num_classes=CLASS_NUM)
return data, labels
def train(aug, trainX, trainY, testX, testY):
# initialize the model
print("[INFO] ")
model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
metrics=["accuracy"])
# train the network
print("[INFO] ")
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
training_vis(H)
# save the model to disk
print("[INFO] ")
#model.save(args["model"])
model.save('live_del')
#python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model del
if __name__=='__main__':
# args = args_parse()
# train_file_path = args["dataset_train"]
# test_file_path = args["dataset_test"]
trainX,trainY = load_data('E:/github/My/LearnKeras/livedetection/dataset/train/') #train_file_path
testX,testY = load_data('E:/github/My/LearnKeras/livedetection/dataset/test/')
# construct the image generator for data augmentation
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")
# train(aug,trainX,trainY,testX,testY,args)
train(aug, trainX, trainY, testX, testY)
第⼆种:使⽤ validation_data (不推荐)
⾸先:没找到多输⼊时model.fit中设置validation_data的例⼦,⽽且⽤validation_split也⼀样是错的,但是验证集⼜是必须⽤的,所以后来如果是多输⼊的话,那就把多
参考链接:
⾸先Keras的fit函数中,传⼊的validation data并不⽤于更新权重,只是⽤是来检测loss和accuracy等指标的。但是!作者说了,即使模
型没有直接在validation data上训练,这也会导致信息泄露,模型会对validation data逐渐熟悉。所以这⾥我简单总结⼀下⽐较⽅便的
data split⽅法。
第⼀步:调参
1、⽤sklearn的train_test_split来把数据分割为training data和test data.(根本没有validation data注意了)
2、⽤keras的model.fit()时,不要使⽤validation_data这个参数,⽽是直接使⽤validation_split这个参数,把training data中的⼀部分⽤
来作为validation data就⾏了。
必须在validation data上进⾏验证,输出loss,观察变化,调参,包括:更改layer,unit,加dropout,使⽤L2正则化,添加新feature等
等。
第⼆步:训练
等调参结束后,拿着我们满意的参数,再⼀次在整个training data上进⾏训练,这⼀次就不⽤validation_split了。因为我们已经调好了参数,不需要观察输出的loss。
训练完之后,⽤model.evaluate()在test data上进⾏预测
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
del_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
#显⽰模型结构图的包
from keras.utils.vis_utils import plot_model
sys.path.append('..')
from net.lenet import LeNet
from LossHistory import LossHistory
def args_parse():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-dtest", "--dataset_test", required=True,
help="path to input dataset_test")
ap.add_argument("-dtrain", "--dataset_train", required=True,
help="path to input dataset_train")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
return args
# initialize the number of epochs to train for, initial learning rate,
# and batch size
EPOCHS = 35
INIT_LR = 1e-3
BS = 32
CLASS_NUM = 2 #61
norm_size = 64 #32
def load_data(path):
print("[INFO] ")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input images
# loop over the input images
for imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = size(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
label = int(imagePath.split(os.path.sep)[1][0])
labels.append(label)
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# convert the labels from integers to vectors
labels = to_categorical(labels, num_classes=CLASS_NUM)
return data, labels
def train(aug, trainX, trainY, testX, testY):
# initialize the model
print("[INFO] ")
model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
metrics=["accuracy"])
# train the network
print("[INFO] ")
# H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
# training_vis(H)
#创建⼀个实例LossHistory
history = LossHistory()
model.fit(trainX, trainY,
batch_size=BS, nb_epoch=EPOCHS,
verbose=1,
validation_data=(testX, testY),
callbacks=[history]) # callbacks回调,将数据传给history
# save the model to disk
print("[INFO] ")
model.save('live_dete_nogenerator_del')
# 模型评估
score = model.evaluate(testX, testY, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
# 绘制acc-loss曲线
history.loss_plot('epoch')
#python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model del if __name__=='__main__':
# args = args_parse()
# train_file_path = args["dataset_train"]
# test_file_path = args["dataset_test"]
trainX,trainY = load_data('E:/github/My/LearnKeras/livedetection/dataset/train/') #train_file_path
testX,testY = load_data('E:/github/My/LearnKeras/livedetection/dataset/test/')
# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
【Kares】train_lossval_losstrain_accval_acc可视化第⼀种:使⽤ validation_split=0.2
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
training_vis(H)
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
del_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
from keras.utils.vis_utils import plot_model
sys.path.append('..')
from net.lenet import LeNet
# from lenet import LeNet
def args_parse():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-dtest", "--dataset_test", required=True,
help="path to input dataset_test")
ap.add_argument("-dtrain", "--dataset_train", required=True,
help="path to input dataset_train")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
return args
# initialize the number of epochs to train for, initial learning rate,
# and batch size
EPOCHS = 35
INIT_LR = 1e-3
BS = 32
CLASS_NUM = 2 #61
norm_size = 64 #32
# define the function
def training_vis(hist):
loss = hist.history['loss']
val_loss = hist.history['val_loss']
acc = hist.history['acc']
val_acc = hist.history['val_acc']
val_acc = hist.history['val_acc']
# make a figure
fig = plt.figure(figsize=(8,4))
# subplot loss
ax1 = fig.add_subplot(121)
ax1.plot(loss, label='train_loss')
ax1.plot(val_loss, label='val_loss')
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Loss')
ax1.set_title('Loss on Training and Validation Data')
ax1.legend()
# subplot acc
ax2 = fig.add_subplot(122)
ax2.plot(acc,label='train_acc')
ax2.plot(val_acc,label='val_acc')
ax2.set_xlabel('Epochs')
ax2.set_ylabel('Accuracy')
ax2.set_title('Accuracy on Training and Validation Data')
ax2.legend()
plt.tight_layout()
plt.savefig('plot_nogenerator.png')
#plt.show() 因为使⽤的是Agg,所以⽆法使⽤
def load_data(path):
print("[INFO] ")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input images
for imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = size(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
label = int(imagePath.split(os.path.sep)[1][0])
labels.append(label)
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# convert the labels from integers to vectors
labels = to_categorical(labels, num_classes=CLASS_NUM)
return data, labels
def train(aug, trainX, trainY, testX, testY):
# initialize the model
print("[INFO] ")
model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
metrics=["accuracy"])
# train the network
print("[INFO] ")
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
training_vis(H)
# save the model to disk
print("[INFO] ")
#model.save(args["model"])
model.save('live_del')
#python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model del
if __name__=='__main__':
# args = args_parse()
# train_file_path = args["dataset_train"]
# test_file_path = args["dataset_test"]
trainX,trainY = load_data('E:/github/My/LearnKeras/livedetection/dataset/train/') #train_file_path
testX,testY = load_data('E:/github/My/LearnKeras/livedetection/dataset/test/')
# construct the image generator for data augmentation
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")
# train(aug,trainX,trainY,testX,testY,args)
train(aug, trainX, trainY, testX, testY)
第⼆种:使⽤ validation_data (不推荐)
⾸先:没找到多输⼊时model.fit中设置validation_data的例⼦,⽽且⽤validation_split也⼀样是错的,但是验证集⼜是必须⽤的,所以后来如果是多输⼊的话,那就把多
参考链接:
⾸先Keras的fit函数中,传⼊的validation data并不⽤于更新权重,只是⽤是来检测loss和accuracy等指标的。但是!作者说了,即使模
型没有直接在validation data上训练,这也会导致信息泄露,模型会对validation data逐渐熟悉。所以这⾥我简单总结⼀下⽐较⽅便的
data split⽅法。
第⼀步:调参
1、⽤sklearn的train_test_split来把数据分割为training data和test data.(根本没有validation data注意了)
2、⽤keras的model.fit()时,不要使⽤validation_data这个参数,⽽是直接使⽤validation_split这个参数,把training data中的⼀部分⽤
来作为validation data就⾏了。
必须在validation data上进⾏验证,输出loss,观察变化,调参,包括:更改layer,unit,加dropout,使⽤L2正则化,添加新feature等
等。
第⼆步:训练
等调参结束后,拿着我们满意的参数,再⼀次在整个training data上进⾏训练,这⼀次就不⽤validation_split了。因为我们已经调好了参数,不需要观察输出的loss。
训练完之后,⽤model.evaluate()在test data上进⾏预测
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
del_selection import train_test_split
from keras.preprocessing.image import img_to_array
from keras.utils import to_categorical
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import cv2
import os
import sys
#显⽰模型结构图的包
from keras.utils.vis_utils import plot_model
sys.path.append('..')
from net.lenet import LeNet
from LossHistory import LossHistory
def args_parse():
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-dtest", "--dataset_test", required=True,
help="path to input dataset_test")
ap.add_argument("-dtrain", "--dataset_train", required=True,
help="path to input dataset_train")
ap.add_argument("-m", "--model", required=True,
help="path to output model")
ap.add_argument("-p", "--plot", type=str, default="plot.png",
help="path to output accuracy/loss plot")
args = vars(ap.parse_args())
return args
# initialize the number of epochs to train for, initial learning rate,
# and batch size
EPOCHS = 35
INIT_LR = 1e-3
BS = 32
CLASS_NUM = 2 #61
norm_size = 64 #32
def load_data(path):
print("[INFO] ")
data = []
labels = []
# grab the image paths and randomly shuffle them
imagePaths = sorted(list(paths.list_images(path)))
random.seed(42)
random.shuffle(imagePaths)
# loop over the input images
# loop over the input images
for imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = size(image, (norm_size, norm_size))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
label = int(imagePath.split(os.path.sep)[1][0])
labels.append(label)
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# convert the labels from integers to vectors
labels = to_categorical(labels, num_classes=CLASS_NUM)
return data, labels
def train(aug, trainX, trainY, testX, testY):
# initialize the model
print("[INFO] ")
model = LeNet.build(width=norm_size, height=norm_size, depth=3, classes=CLASS_NUM)
opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
metrics=["accuracy"])
# train the network
print("[INFO] ")
# H = model.fit(trainX, trainY, batch_size=BS, epochs=EPOCHS, validation_split=0.2, shuffle=True)
# plot the training loss and accuracy
# training_vis(H)
#创建⼀个实例LossHistory
history = LossHistory()
model.fit(trainX, trainY,
batch_size=BS, nb_epoch=EPOCHS,
verbose=1,
validation_data=(testX, testY),
callbacks=[history]) # callbacks回调,将数据传给history
# save the model to disk
print("[INFO] ")
model.save('live_dete_nogenerator_del')
# 模型评估
score = model.evaluate(testX, testY, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
# 绘制acc-loss曲线
history.loss_plot('epoch')
#python train.py --dataset_train ../../traffic-sign/train --dataset_test ../../traffic-sign/test --model del if __name__=='__main__':
# args = args_parse()
# train_file_path = args["dataset_train"]
# test_file_path = args["dataset_test"]
trainX,trainY = load_data('E:/github/My/LearnKeras/livedetection/dataset/train/') #train_file_path
testX,testY = load_data('E:/github/My/LearnKeras/livedetection/dataset/test/')
# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,