keras构建卷积神经网络_在Keras中构建,加载和保存卷积神经网络
keras構建卷積神經網絡
This article is aimed at people who want to learn or review how to build a basic Convolutional Neural Network in Keras. The dataset in which this article is based on is the Fashion-Mnist dataset.
本文針對想要學習或復習如何在Keras中構建基本卷積神經網絡的人們。 本文所基于的數據集是Fashion-Mnist數據集 。
Along with this article, we will explain how:
與本文一起,我們將解釋如何:
數據集描述 (Dataset description)
Fashion-MNIST is a dataset of Zalando’s article images — consisting of a training set of 60,000 examples and a test set of 10,000 examples. Each example is a 28x28 grayscale image, associated with a label from 10 classes. We intend Fashion-MNIST to serve as a direct drop-in replacement for the original MNIST dataset for benchmarking machine learning algorithms. It shares the same image size and structure of training and testing splits.
Fashion-MNIST是Zalando文章圖片的數據集-包含60,000個示例的訓練集和10,000個示例的測試集。 每個示例都是一個28x28灰度圖像,與來自10個類別的標簽相關聯。 我們打算將Fashion-MNIST用作直接替代MNIST原始數據集的基準機器學習算法。 它具有相同的圖像大小以及訓練和測試分割的結構。
加載數據 (Loading the data)
In order to run the code displayed below, it is necessary to download the following files. Once downloaded the data, you can load it using the following code.
為了運行下面顯示的代碼,必須下載以下文件 。 下載數據后,您可以使用以下代碼加載數據。
數據標準化 (Data normalization)
Then, we rescale the images from 0–255 to 0–1 by dividing the data by 255.
然后,通過將數據除以255,我們將圖像從0-255縮放到0-1。
前處理 (Pre-processing)
Before loading the data inside the neural network, it is necessary to reshape the images to the correct format that Keras requires. When using a 2D convolution as the first layer in a model, the default shape is (batch_size, height, width, channels); (no_data, 128, 128, 3) for 128x128 RGB pictures.
在將數據加載到神經網絡內部之前,有必要將圖像重塑為Keras所需的正確格式。 當使用2D卷積作為模型的第一層時,默認形狀為(batch_size,height,width,channels); ( no_data,128,128,3 )用于128x128 RGB圖片。
The images of our dataset are grayscale images, in which the value of each pixel is a single sample representing only an amount of light. Therefore, the shape of the training data has to be (no_data, 28, 28, 1).
我們的數據集的圖像是灰度圖像,其中每個像素的值是一個僅代表光量的單個樣本。 因此,訓練數據的形狀必須為( no_data,28,28,1 )。
建立神經網絡 (Building the neural network)
For this article, I built a neural network using two 2D convolutions layers and then two fully connected layers. When declaring the 2D convolutional layers, it is possible/necessary to indicate some parameters. Also, remember to recheck the input_shape. Most errors come from not declaring it right.
對于本文,我使用兩個2D卷積層以及兩個完全連接的層構建了一個神經網絡。 在聲明2D卷積層時,有可能/有必要指出一些參數。 另外,請記住要重新檢查input_shape。 大多數錯誤來自未正確聲明。
Arguments:
參數:
filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the convolution).
過濾器 :整數,輸出空間的維數(即卷積中輸出過濾器的數量)。
kernel_size: An integer or tuple/list of 2 integers, specifying the height and width of the 2D convolution window. Can be a single integer to specify the same value for all spatial dimensions.
kernel_size :一個整數或2個整數的元組/列表,指定2D卷積窗口的高度和寬度。 可以是單個整數,以為所有空間尺寸指定相同的值。
activation: Activation function to use. The typical activations used for all the layers but the last one is ‘relu’ activation. For the last layer, the most used activation is the ‘softmax’ activation.
activation :要使用的激活功能。 除最后一層外,所有層都使用的典型激活是“ relu”激活。 對于最后一層,最常用的激活是“ softmax”激活。
pool_size: integer or tuple of 2 integers, window size over which to take the maximum. (2, 2) will take the max value over a 2x2 pooling window. If only one integer is specified, the same window length will be used for both dimensions.
pool_size :2個整數的整數或元組,最大的窗口大小。 (2, 2)將采用2x2合并窗口中的最大值。 如果僅指定一個整數,則兩個尺寸將使用相同的窗口長度。
This is the architecture of the built neural network.
這是內置神經網絡的體系結構。
訓練神經網絡 (Training the neural network)
For training the neural network, you can run the following code. Apart from training and evaluating the validation set, it will also save logs that can be afterward loaded into Tensorboard.
為了訓練神經網絡,您可以運行以下代碼。 除了訓練和評估驗證集外,它還將保存日志,然后可以將其加載到Tensorboard中。
Besides, this code will save (1) the weights of the model for each epoch, and (2) the weights of the model with maximum accuracy.
此外,此代碼還將節省(1)每個時期的模型權重,以及(2)以最大精度保存的模型權重。
Saving the best model is interesting because the last epoch is not always that one that performed best (e.g., the model is overfitting).
保存最佳模型很有趣,因為最后一個時期并不總是表現最好的那個時期(例如,模型過度擬合)。
加載并重新運行模型 (Loading and re-running the model)
Since we saved the models in the step before, now it is possible to load it and keep training the neural network.
由于我們在之前的步驟中保存了模型,因此現在可以加載模型并繼續訓練神經網絡。
The code to load the model is the following.
下面是加載模型的代碼。
To keep training the neural network from the point we left it before, just run the following code after loading the model.
為了從之前離開的地方繼續訓練神經網絡,只需在加載模型后運行以下代碼即可。
繪制結果 (Plotting the results)
The results are displayed in the following figures.
結果顯示在下圖中。
First training (15 epochs)第一次訓練(15個紀元) Whole training, after loading and rerunning the model (30 epochs)加載并重新運行模型后進行整體培訓(30個紀元)The classification results are displayed in the following table.
分類結果顯示在下表中。
翻譯自: https://medium.com/swlh/building-loading-and-saving-a-convolutional-neural-network-in-keras-2b06b1f76887
keras構建卷積神經網絡
總結
以上是生活随笔為你收集整理的keras构建卷积神经网络_在Keras中构建,加载和保存卷积神经网络的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2002年入职!SpaceX总裁荣获20
- 下一篇: “美版头条”BuzzFeed 股价连续大