博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用CIFAR-10样本数据集测试卷积神经网络(ConvolutionalNeuralNetwork,CNN)
阅读量:5092 次
发布时间:2019-06-13

本文共 5761 字,大约阅读时间需要 19 分钟。

第一次将例程跑起来了,有些兴趣。

参考的是如下URL:

http://www.yidianzixun.com/article/0KNz7OX1

本来是比较Keras和Tensorflow的,我现在的水平,只能是跑通一个算一个啦。

因为要比较CPU和GPU的性能,两个DOCKER如下:

tensorflow/tensorflow:1.12.0-gpu-py3

tensorflow/tensorflow:1.12.0-py3

CIFAR-10的数据自已从网上下载,所以出现如下错误时,要自己更改成一个内网URL地址:

Traceback (most recent call last):  File "train_network_tf.py", line 26, in 
split = tf.keras.datasets.cifar10.load_data() File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/datasets/cifar10.py", line 40, in load_data path = get_file(dirname, origin=origin, untar=True) File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/utils/data_utils.py", line 251, in get_file raise Exception(error_msg.format(origin, e.errno, e.reason))Exception: URL fetch failure on https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz: None -- [Errno -3] Temporary failure in name resolution
/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/datasets/cifar10.py这个文件40行是一个网址,更改一下就OK了。 一,modle层实现代码 pyimagesearch/minivggnettf.py
# import the necessary packagesimport tensorflow as tfclass MiniVGGNetTF:    @staticmethod    def build(width, height, depth, classes):        # initialize the input shape and channel dimension, assuming        # TensorFlow/channels-last ordering        inputShape = (height, width, depth)        chanDim = -1        # define the model input        inputs = tf.keras.layers.Input(shape=inputShape)        # first (CONV => RELU) * 2 => POOL layer set        x = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(inputs)        x = tf.keras.layers.Activation("relu")(x)        x = tf.keras.layers.BatchNormalization(axis=chanDim)(x)        x = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(x)        x = tf.keras.layers.Lambda(lambda t: tf.nn.crelu(x))(x)        x = tf.keras.layers.BatchNormalization(axis=chanDim)(x)        x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)        x = tf.keras.layers.Dropout(0.25)(x)        # second (CONV => RELU) * 2 => POOL layer set        x = tf.keras.layers.Conv2D(64, (3, 3), padding="same")(x)        x = tf.keras.layers.Lambda(lambda t: tf.nn.crelu(x))(x)        x = tf.keras.layers.BatchNormalization(axis=chanDim)(x)        x = tf.keras.layers.Conv2D(64, (3, 3), padding="same")(x)        x = tf.keras.layers.Lambda(lambda t: tf.nn.crelu(x))(x)        x = tf.keras.layers.BatchNormalization(axis=chanDim)(x)        x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x)        x = tf.keras.layers.Dropout(0.25)(x)        # first (and only) set of FC => RELU layers        x = tf.keras.layers.Flatten()(x)        x = tf.keras.layers.Dense(512)(x)        x = tf.keras.layers.Lambda(lambda t: tf.nn.crelu(x))(x)        x = tf.keras.layers.BatchNormalization()(x)        x = tf.keras.layers.Dropout(0.5)(x)        # softmax classifier        x = tf.keras.layers.Dense(classes)(x)        x = tf.keras.layers.Activation("softmax")(x)        # create the model        model = tf.keras.models.Model(inputs, x, name="minivggnet_tf")        # return the constructed network architecture        return model

二,数据训练

train_network_tf.py

# USAGE# python train_network_tf.py# set the matplotlib backend so figures can be saved in the backgroundimport matplotlibmatplotlib.use("Agg")# import the necessary packagesfrom pyimagesearch.minivggnettf import MiniVGGNetTFfrom sklearn.preprocessing import LabelBinarizerfrom sklearn.metrics import classification_reportimport matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npimport argparse# construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument("-p", "--plot", type=str, default="plot_tf.png",    help="path to output loss/accuracy plot")args = vars(ap.parse_args())# load the training and testing data, then scale it into the# range [0, 1]print("[INFO] loading CIFAR-10 data...")split = tf.keras.datasets.cifar10.load_data()((trainX, trainY), (testX, testY)) = splittrainX = trainX.astype("float") / 255.0testX = testX.astype("float") / 255.0# convert the labels from integers to vectorslb = LabelBinarizer()trainY = lb.fit_transform(trainY)testY = lb.transform(testY)# initialize the label names for the CIFAR-10 datasetlabelNames = ["airplane", "automobile", "bird", "cat", "deer",    "dog", "frog", "horse", "ship", "truck"]# initialize the initial learning rate, total number of epochs to# train for, and batch sizeINIT_LR = 0.01EPOCHS = 5BS = 32# initialize the optimizer and modelprint("[INFO] compiling model...")opt = tf.keras.optimizers.SGD(lr=INIT_LR, decay=INIT_LR / EPOCHS)model = MiniVGGNetTF.build(width=32, height=32, depth=3,    classes=len(labelNames))model.compile(loss="categorical_crossentropy", optimizer=opt,    metrics=["accuracy"])# train the networkprint("[INFO] training network for {} epochs...".format(EPOCHS))H = model.fit(trainX, trainY, validation_data=(testX, testY),    batch_size=BS, epochs=EPOCHS, verbose=1)# evaluate the networkprint("[INFO] evaluating network...")predictions = model.predict(testX, batch_size=32)print(classification_report(testY.argmax(axis=1),    predictions.argmax(axis=1), target_names=labelNames))# plot the training loss and accuracyplt.style.use("ggplot")plt.figure()plt.plot(np.arange(0, EPOCHS), H.history["loss"], label="train_loss")plt.plot(np.arange(0, EPOCHS), H.history["val_loss"], label="val_loss")plt.plot(np.arange(0, EPOCHS), H.history["acc"], label="train_acc")plt.plot(np.arange(0, EPOCHS), H.history["val_acc"], label="val_acc")plt.title("Training Loss and Accuracy on Dataset")plt.xlabel("Epoch #")plt.ylabel("Loss/Accuracy")plt.legend(loc="lower left")plt.savefig(args["plot"])

三,结果对比:

当使用GPU吧,一个批次完成需要30秒上下。

而只使用CPU的话,一个批次完成则需要330秒以上。

效率提高10倍以上啊。

 

转载于:https://www.cnblogs.com/aguncn/p/10949579.html

你可能感兴趣的文章
npm install 报node-sass错误
查看>>
软件常用问题
查看>>
上传文件(ajax结合form表单)
查看>>
selenium python grid
查看>>
nc(NetCat)命令
查看>>
CNN卷积神经网络-tensorflow
查看>>
JS性能优化
查看>>
P3930 SAC E#1 - 一道大水题 Knight
查看>>
Linux中tar命令
查看>>
Vue 中watch和computed 的用法及区别
查看>>
设计模式:第二章--抽象工厂模式
查看>>
Redis分布式锁
查看>>
yum 崩溃的解决方法
查看>>
Entity Framework之问题收集
查看>>
iOS 设计模式
查看>>
渗透小助手——几个密码收集工具
查看>>
MapReduce入门(二)合并小文件
查看>>
String基本操作
查看>>
通过反射访问private的属性或方法
查看>>
算法分析
查看>>