这里我们使用keras定义简单的神经网络全连接层训练MNIST数据集和cifar10数据集:
keras_mnist.py
from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from keras.models import Sequential from keras.layers.core import Dense from keras.optimizers import SGD from sklearn import datasets import matplotlib.pyplot as plt import numpy as np import argparse # 命令行参数运行 ap = argparse.ArgumentParser() ap.add_argument("-o", "--output", required=True, help="path to the output loss/accuracy plot") args =vars(ap.parse_args()) # 加载数据MNIST,然后归一化到【0,1】,同时使用75%做训练,25%做测试 print("[INFO] loading MNIST (full) dataset") dataset = datasets.fetch_mldata("MNIST Original", data_home="/home/king/test/python/train/pyimagesearch/nn/data/") data = dataset.data.astype("float") / 255.0 (trainX, testX, trainY, testY) = train_test_split(data, dataset.target, test_size=0.25) # 将label进行one-hot编码 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # keras定义网络结构784--256--128--10 model = Sequential() model.add(Dense(256, input_shape=(784,), activation="relu")) model.add(Dense(128, activation="relu")) model.add(Dense(10, activation="softmax")) # 开始训练 print("[INFO] training network...") # 0.01的学习率 sgd = SGD(0.01) # 交叉验证 model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=['accuracy']) H = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=100, batch_size=128) # 测试模型和评估 print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=128) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=[str(x) for x in lb.classes_])) # 保存可视化训练结果 plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 100), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 100), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 100), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 100), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("# Epoch") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["output"])
使用relu做激活函数:
使用sigmoid做激活函数:
接着我们自己定义一些modules去实现一个简单的卷基层去训练cifar10数据集:
imagetoarraypreprocessor.py
''' 该函数主要是实现keras的一个细节转换,因为训练的图像时RGB三颜色通道,读取进来的数据是有depth的,keras为了兼容一些后台,默认是按照(height, width, depth)读取,但有时候就要改变成(depth, height, width) ''' from keras.preprocessing.image import img_to_array class ImageToArrayPreprocessor: def __init__(self, dataFormat=None): self.dataFormat = dataFormat def preprocess(self, image): return img_to_array(image, data_format=self.dataFormat)
shallownet.py
''' 定义一个简单的卷基层: input->conv->Relu->FC ''' from keras.models import Sequential from keras.layers.convolutional import Conv2D from keras.layers.core import Activation, Flatten, Dense from keras import backend as K class ShallowNet: @staticmethod def build(width, height, depth, classes): model = Sequential() inputShape = (height, width, depth) if K.image_data_format() == "channels_first": inputShape = (depth, height, width) model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShape)) model.add(Activation("relu")) model.add(Flatten()) model.add(Dense(classes)) model.add(Activation("softmax")) return model
然后就是训练代码:
keras_cifar10.py
from sklearn.preprocessing import LabelBinarizer from sklearn.metrics import classification_report from shallownet import ShallowNet from keras.optimizers import SGD from keras.datasets import cifar10 import matplotlib.pyplot as plt import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-o", "--output", required=True, help="path to the output loss/accuracy plot") args = vars(ap.parse_args()) print("[INFO] loading CIFAR-10 dataset") ((trainX, trainY), (testX, testY)) = cifar10.load_data() trainX = trainX.astype("float") / 255.0 testX = testX.astype("float") / 255.0 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # 标签0-9代表的类别string labelNames = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] print("[INFO] compiling model...") opt = SGD(lr=0.0001) model = ShallowNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=1000, verbose=1) print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames)) # 保存可视化训练结果 plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 1000), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 1000), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 1000), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 1000), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("# Epoch") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["output"])
代码中可以对训练的learning rate进行微调,大概可以接近60%的准确率。
然后修改下代码可以保存训练模型:
from sklearn.preprocessing import LabelBinarizer from sklearn.metrics import classification_report from shallownet import ShallowNet from keras.optimizers import SGD from keras.datasets import cifar10 import matplotlib.pyplot as plt import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-o", "--output", required=True, help="path to the output loss/accuracy plot") ap.add_argument("-m", "--model", required=True, help="path to save train model") args = vars(ap.parse_args()) print("[INFO] loading CIFAR-10 dataset") ((trainX, trainY), (testX, testY)) = cifar10.load_data() trainX = trainX.astype("float") / 255.0 testX = testX.astype("float") / 255.0 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) # 标签0-9代表的类别string labelNames = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] print("[INFO] compiling model...") opt = SGD(lr=0.005) model = ShallowNet.build(width=32, height=32, depth=3, classes=10) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print("[INFO] training network...") H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=50, verbose=1) model.save(args["model"]) print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32) print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=labelNames)) # 保存可视化训练结果 plt.style.use("ggplot") plt.figure() plt.plot(np.arange(0, 5), H.history["loss"], label="train_loss") plt.plot(np.arange(0, 5), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, 5), H.history["acc"], label="train_acc") plt.plot(np.arange(0, 5), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("# Epoch") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["output"])
命令行运行:
我们使用另一个程序来加载上一次训练保存的模型,然后进行测试:
test.py
from sklearn.preprocessing import LabelBinarizer from sklearn.metrics import classification_report from shallownet import ShallowNet from keras.optimizers import SGD from keras.datasets import cifar10 from keras.models import load_model import matplotlib.pyplot as plt import numpy as np import argparse ap = argparse.ArgumentParser() ap.add_argument("-m", "--model", required=True, help="path to save train model") args = vars(ap.parse_args()) # 标签0-9代表的类别string labelNames = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] print("[INFO] loading CIFAR-10 dataset") ((trainX, trainY), (testX, testY)) = cifar10.load_data() idxs = np.random.randint(0, len(testX), size=(10,)) testX = testX[idxs] testY = testY[idxs] trainX = trainX.astype("float") / 255.0 testX = testX.astype("float") / 255.0 lb = LabelBinarizer() trainY = lb.fit_transform(trainY) testY = lb.transform(testY) print("[INFO] loading pre-trained network...") model = load_model(args["model"]) print("[INFO] evaluating network...") predictions = model.predict(testX, batch_size=32).argmax(axis=1) print("predictions\n", predictions) for i in range(len(testY)): print("label:{}".format(labelNames[predictions[i]])) trueLabel = [] for i in range(len(testY)): for j in range(len(testY[i])): if testY[i][j] != 0: trueLabel.append(j) print(trueLabel) print("ground truth testY:") for i in range(len(trueLabel)): print("label:{}".format(labelNames[trueLabel[i]])) print("TestY\n", testY)
以上这篇keras训练浅层卷积网络并保存和加载模型实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
keras,训练,浅层,卷积网络
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】