您的位置:首页 > 编程语言 > Java开发

Encog3Java-User.pdf翻译:第九章 使用图像数据

2017-12-05 09:17 375 查看
Chapter 9

第九章

Using Image Data

使用图像数据

? Processing Images

? Finding the Bounds

? Downsampling

? Using the Image Dataset

Using neural networks to recognize images is a very common task. This chapter will explore how to use images with Encog. By using the same feedforward neural networks as seen in earlier chapters, neural networks can be designed to recognize certain images.
Specialized datasets ease the process of getting image data into the neural network.

使用神经网络识别图像是一项非常常见的任务。本章将探讨encog如何利用图像。通过使用前面章节所见的相同的前馈神经网络,可以设计神经网络来识别某些图像。专用数据集简化了将图像数据输入神经网络的过程。

This chapter will introduce the ImageMLDataSet. This class can accept a list of images that will be loaded and processed into an Encog-friendly form. The ImageMLDataSet is based upon the BasicMLDataSet, which is really just an array of double values for input
and idea. The ImageMLDataSet simply adds special functions to load images into arrays of doubles.

本章将介绍ImageMLDataSet。这个类可以接受一个图像列表,将加载和处理成一个encog友好的格式。ImageMLDataSet是基于basicmldataset,只是输入和期望的double数组。ImageMLDataSet只是增加了加载图像到double数组的特殊功能。

There are several important issues to consider when loading image data into a neural network. The ImageMLDataSet takes care of two important aspects of this. The first aspect is detecting boundaries on what is to be recognized. The second is downsampling where
images are usually formatted in high-resolution and must be downsampled to a consistent lower resolution to be fed to the neural network.

在神经网络中加载图像数据时需要考虑几个重要的问题。ImageMLDataSet考虑到了这两个重要的方面。第一个方面是检测什么是可识别的边界。二是下采样图像,通常在高分辨率下采样格式必须以合适的低分辨率输入到神经网络。

9.1 Finding the Bounds

9.1 发现边界

An image is a rectangular region that represents the data important to the neural network. Only a part of the image may be useful. Ideally, the actual image the neural network must recognize is equal to the entire physical image - rather than just a portion
of the original image. Such is the case with Figure 9.1.

图像是表示对神经网络重要数据的矩形区域。图像中只有一部分可能有用。理想情况下,神经网络必须识别的实际图像等于整个物理图像,而不只是原始图像的一部分。图9.1就是这样。



As you can see in the above figure, the letter “X” was drawn over nearly the entire physical image. This image would require minimal, if any, boundary detection. Images will not always be so perfectly created. Consider the image presented in Figure 9.2.

正如你在上面的图中看到的,字母“X”几乎是在整个物理图像上绘制的。如果有的话,此图像将需要最小的边界检测。图像不会总是如此完美地创造。考虑图9.2中所示的图像。



Here the letter “X” is scaled differently than in the previous image and is also off-center. To properly recognize it, we must find the bounds of the second letter “X.” Figure 9.3 shows a bounding box around the letter “X.” Only data inside of the bounding
box will be used to recognize the image. 

这里字母“X”的大小与前一个图像不同,也是偏离中心的。为了正确识别它,我们必须找到第二个字母“x”的边界。图9.3显示了字母“x”周围的边界框。只有边框内的数据才会被用来识别图像。



As you can see, the bounds have been detected for the letter “X.” The bounding box signifies that only data inside of that box will be recognized. Now the “X” is in approximately the same orientation as Figure 9.1.

正如你所看到的,边界被检测到字母“x”。边框意味着只有那个盒子里面的数据才会被识别。现在“x”的方向与图9.1大致相同。

9.2 Downsampling an Image

9.2 下采样一张图片

Even with bounding boxes, images may not be consistently sized. The letter “X” in Figure 9.3 is considerably smaller than Figure 9.1. When recognizing the image, we will draw a grid over the image and line up each grid cell to an input neuron. To do this, the
images must be consistently sized. Further, most images have too high a resolution to be used with a neural network.

即使使用边框,图像也不能大小一致。图9.3中的字母“X”比图9.1要小得多。当识别图像时,我们将在图像上画一个网格,并将每个网格单元排列成一个输入神经元。要做到这一点,图像必须一致大小。此外,大多数图像的分辨率太高,不能与神经网络一起使用。

Downsampling solves both of these problems by reducing the image resolution and scaling all images to a consistent size. To see this in action, consider Figure 9.4. This figure shows the Encog logo at full resolution.

下采样解决这些问题,降低图像的分辨率和缩放图像到大小一致。要看到这一点,请考虑图9.4。这个数字表明,在全分辨率的encog标志。



Figure 9.5 shows this same image downsampled.

图9.5显示同一张图片下采样。



Do you notice the grid-like pattern? It has been reduced to 32x32 pixels. These pixels would form the input to a neural network. This neural network would require 1,024 input neurons, if the network were to only look at the intensity of each square. Looking
at the intensity limits the neural network to see in “black and white”.

你注意到网格状的图案吗?它已经减小为32x32像素。这些像素将形成一个神经网络的输入。这个神经网络需要1024个输入神经元,如果网络只看每个正方形的强度。看着强度限制神经网络在“黑白”中看到。

If you would like the neural network to seen in color, then it is necessary to provide red, green and blue (RGB) values for each of these pixels. This would mean three input neurons for each pixel, which would push the input neuron count to 3,072.

如果你想让神经网络在颜色中看到,那么就必须为这些像素中的每一个提供红、绿、蓝(RGB)值。这意味着每个像素有三个输入神经元,这将把输入神经元计数增加到3072。

The Encog image dataset provides both boundary detection, as well as RGB and intensity downsampling. In the next section, the Encog image dataset will be introduced.

encog图像数据集提供了边界检测,以及RGB和强度下采样。在下一节中,将介绍的encog图像数据集。

9.2.1 What to Do With the Output Neurons 

9.2.1 输出神经元做什么

The output neurons should represent the groups that these images will fall into. For example, if writing an OCR application, use one output neuron for every character to be recognized. Equilateral encoding is also useful in this respect, as discussed in Chapter
2 “Obtaining Data for Encog”.

输出神经元应表示这些图像归属的组。例如,如果编写一个OCR应用程序,就要使用一个输出神经元来识别每个字符。等边编码在这方面也是有用的,如在2章“为Encog获取数据”。

Supervised training also requires ideal output data for each image. For a simple OCR, there might be 26 output neurons, one for each letter of the alphabet. These ideal outputs train the neural network for what the image actually is. Whether training is supervised
or unsupervised train
4000
ing, the output neurons will relay how the neural network interpreted each image.

监督训练也要求每个图像的理想输出数据。对于一个简单的OCR,可能有26个输出神经元,字母表中的每个字母。这些理想输出训练神经网络的图像实际上是什么。无论训练是监督还是非监督训练,输出神经元都会传达神经网络如何解释每个图像。

9.3 Using the Encog Image Dataset

9.3 使用Encog图像数据集

Before instantiating an ImageMLDataSet object, a downsampled object must be created. This object is a tool for Encog to use to perform the downsample. All Encog downsample objects must implement the interface Downsample. Encog currently supports two downsample
classes, listed below:

ImageMLDataSet实例化对象前,须创建下采样对象。这个对象是一个encog进行下采样的工具。所有的encog采样对象必须实现Downsample接口。encog目前支持两个下采样类,如下:

? RGBDownsample

? SimpleIntensityDownsample

The SimpleIntensityDownsample does not take color into consideration. It simply calculates the brightness or darkness of a pixel. The number of input neurons will be height multiplied by the width, as there is only one input neuron needed per pixel.

SimpleIntensityDownsample没有考虑颜色。它只是计算像素的亮度或暗度。将高度乘以宽度就是输入神经元的数目,因为每个像素只需要一个输入神经元。

The RGBDownsample is more advanced than SimpleIntensityDownsample. This downsample object converts to the resolution that you specify and turns every pixel into a three-color (RGB) input. The total number of input neuron values produced by this object will
be height times width times three. The following code instantiates a SimpleIntensityDownsample object. This object will be used to create the training set.

比SimpleIntensityDownsample更先进的RGBDownsample。这下采样对象转换为指定的分辨率,将每一个像素分为三颜色(RGB)输入。该对象生成的输入神经元总数将是高度乘以宽度乘以三。下面的代码创建了一个SimpleIntensityDownsample对象。该对象将用于创建训练集。

downsample = new SimpleIntensityDownsample ( ) ;

Now that a downsample object is created, it is time to use an ImageMLDataSet class. It must be instantiated with several parameters. The following code does this.

现在,一个采样的对象被创建,它是使用一个ImageMLDataSet类。它必须用几个参数实例化。下面的代码如下所示。

training = new ImageMLDataSet ( downsample , false , 1 , -1) ;

The parameters 1 and -1 specify the range to which the colors will be normalized, either by the intensity color or the three individual RGB colors. The false value means that the dataset should not attempt to detect the edges. If this value were true, Encog
would attempt to detect the edges.

参数1和- 1指定颜色将被标准化的范围,针对强度颜色或三个单独的RGB颜色。false值意味着数据集不应试图检测边缘。如果这个值是true,encog会试图检测边缘。

The current Encog edge detection is not very advanced. It looks for one consistent color around the sides of an image and attempts to remove as much of that region as it can. More advanced edge detection will likely be built into future versions of Encog. If
advanced edge detection is necessary, it is best to trim the images before sending them to the ImageMLDataSet object.

目前encog边缘检测不是很先进。它在图像的两侧寻找一个一致的颜色,并试图尽可能地删除该区域。更先进的边缘检测将有可能建立在encog未来版本。如果先进的边缘检测是必要的,最好是在送他们到ImageMLDataSet对象前修剪图像。

Now that the ImageMLDataSet object has been created, it is time to add some images. To add images to this dataset, an ImageMLData object must be created for each image. The following lines of code will add one image from a file.

现在,ImageMLDataSet对象已经创建,现在是时候添加一些图片。图像添加到该数据集,一个ImageMLData对象必须为每幅图像建立。下面的代码行将从一个文件中添加一个图像。

Image img = ImageIO.read ( [ filename of image ] ) ;

ImageMLData data = new ImageMLData( img ) ;

this.training.add ( data , [ ideal output ] ) ;

The image is loaded from a file using the Java ImageIO class, which reads images from files. Any valid Java image object can be used by the dataset. The ideal output should be specified when using supervised training. With unsupervised training, this parameter
can be omitted. Once the ImageMLData object is instantiated, it is added to the dataset. These steps are repeated for every image to be added.

图像是从一个文件中使用java的ImageIO类加载,读取图像文件。可以通过数据集对象使用任何有效的java图像。使用监督训练时,应指定理想输出。通过无监督训练,这个参数可以省略。一旦ImageMLData对象实例化时,它被添加到数据集。每一个要添加的图像都要重复这些步骤。

Once all of the images are loaded, they are ready to be downsampled. To downsample the images call the downsample method.

一旦所有的图片都加载了,他们就要被下采样。调用采样方法下采样图像。

this.training.downsample ( [ downsample height ] ,[ downsample width ] ) ;

Specify the downsample height and width. All of the images will be downsampled to this size. After calling the downsample method, the training data is generated and can train a neural network.

指定采下样高度和宽度。所有的图像将这个尺寸下采样。调用采样方法后,训练数据产生,可以训练一个神经网络了。

9.4 Image Recognition Example

9.4 图片识别例子

We will now see how to tie all Encog image classes together into an example. A generic image recognition program will serve as an example and could easily become the foundation of a much more complex image recognition program. This example is driven from a
script file. Listing 9.1 shows the type of script file that might drive this program.

我们将看到如何把所有encog图像捆绑一起放到一个例子里。一个通用的图像识别程序将作为一个例子,可以很容易地成为一个更复杂的图像识别程序的基础。该示例是从脚本文件驱动的。清单9.1显示了驱动此程序的脚本文件类型。

Listing 9.1: Image Recognition Script

CreateTraining : width : 1 6 , height : 1 6 , type :RGB

Input : image :./coins/dime.png ,identity: dime

Input : image :./coins/dollar.png ,identity:dollar

Input : image :./coins/half.png ,identity:halfdollar

Input : image :./coins/nickle.png ,identity:nickle

Input : image :./coins/penny.png ,identity: penny

Input : image :./coins/ quarter.png ,identity: quarter

Network : hidden1 :100 , hidden2 : 0

Train : Mode : console , Minutes : 1 , StrategyError : 0.2 5 , StrategyCycles:50

Whatis : image :./coins/dime.png

Whatis : image :./coins/half.png

Whatis : image :./coins/tescoin.png

The syntax used by this script file is very simple. There is a command, followed by a colon. This command is followed by a comma-separated list of parameters. Each parameter is a name-value pair that is also separated by a colon. There are five commands in
all: CreateTraining, Input, Network, Train and WhatIs.

这个脚本文件使用的语法非常简单。有一个命令,后面是冒号。这个命令后面跟着一个逗号分隔的参数列表。每个参数都是由冒号分隔的name值对。有五个命令:CreateTraining,输入网络,训练和Whatis。

The CreateTraining command creates a new training set. To do so, specify the downsample height, width, and type - either RGB or Brightness. The Input command inputs a new image for training. Each input command specifies the image as well as the identity of
the image. Multiple images can have the same identity. For example, the above script could have provided a second image of a dime by causing the second Input command to also have the identity of “dime”.

createtraining命令创建一个新的训练集。这样做,指定采样高度,宽度,和类型-无论是RGB或亮度。输入命令输入一个用于训练的新图像。每个输入命令都指定图像以及图像的标识。多个图像可以具有相同的标识。例如,上面的脚本可以通过使第二个输入命令也具有“一元”的身份,提供了一个硬币的第二个图像。

The Network command creates a new neural network for training and recognition. Two parameters specify the size of the first and second hidden layers. If you do not wish to have a second hidden layer, specify zero for the hidden2 parameter.

网络命令为训练和识别创建了一个新的神经网络。两个参数指定第一和第二隐藏层的大小。如果你不希望有一个第二隐层的参数,指定hidden2零。

The Train command trains the neural network and mode specifies either console or GUI training. The minutes parameter specifies how many minutes are required to train the network. This parameter is only used with console training; for GUI training this parameter
should be set to zero. The strategy tells the training algorithm how many cycles to wait to reset the neural network if the error level has not dropped below the specified amount.

训练命令训练神经网络,模式指定控制台或GUI培训。分钟参数指定训练网络需要多少分钟。此参数仅用于控制台培训;对于GUI培训,此参数应设置为零。策略告诉训练算法,如果错误级别未降到指定的数量,则需要等待多少个周期来重置神经网络。

The WhatIs command accepts an image and tries to recognize it. The example will print the identity of the image that it thought was most similar. We will now take a look at the image recognition example. This example can be found at the following location. 

whatis命令接受图像并试图认识它。该示例将打印它认为最相似的图像的标识。现在我们来看看图像识别的例子。此示例可在以下位置找到。

org.encog.examples.neural.image.ImageNeuralNetwork

Some of the code in the above example deals with parsing the script file and arguments. Because string parsing is not really the focus of this book, we will focus on how each command is carried out and how the neural network is constructed. The next sections
discuss how each of these commands is implemented.

上述示例中的一些代码处理解析脚本文件和参数。由于字符串解析并不是本书的重点,所以我们将重点讨论每个命令是如何执行的,以及如何构造神经网络。下一节讨论如何实现这些命令。

9.4.1 Creating the Training Set

9.4.1 创建训练集

The CreateTraining command is implemented by the processCreateTraining method. This method is shown here.

createtraining命令是由processcreatetraining方法实现。此处显示此方法。

private void processCreateTraining ( ) {

The CreateTraining command takes three parameters. The following lines read these parameters.

final String strWidth = getArg ( ” width ” ) ;

final String strHeight = getArg ( ” height ” ) ;

final String strType = getArg ( ” type ” ) ;

The width and height parameters are both integers and need to be parsed.

this.downsampleHeight = Integer.parseInt ( strWidth ) ;

this.downsampleWidth = Integer.parseInt ( strHeight ) ;

We must now create the downsample object. If the mode is RGB, use RGBDownsample. Otherwise, use SimpleIntensityDownsample.

i f ( strType.equals ( ”RGB” ) ) {

this.downsample = new RGBDownsample ( ) ;

} else {

this.downsample = new SimpleIntensityDownsample ( ) ;

}

The ImageMLDataSet can now be created.

this.training = new ImageMLDataSet (

this.downsample , false , 1 , ?1) ;

System.out.println ( ” Training s e t created ” ) ;

}

Now that the training set is created, we can input images. The next section describes how this is done.

现在已经创建了训练集,我们可以输入图像了。下一节描述如何完成此操作。

9.4.2 Inputting an Image

9.4.2 输入图像

The Input command is implemented by the processInput method. This method is shown here.

private void processInput ( ) throws IOException {

The Input command takes two parameters. The following lines read these parameters.

final String image = getArg ( ” image ” ) ;

final Stringidentity= getArg ( ”identity” ) ;

The identity is a text string that represents what the image is. We track the number of unique identities and assign an increasing number to each. These unique identities will form the neural network’s output layer. Each unique identity will be assigned an
output neuron. When images are presented to the neural network later, the output neuron with the highest output will represent the image identity to the network. The assignIdentity method is a simple method that assigns this increasing count and maps the identity
strings to their neuron index.

标识是表示图像是什么的文本字符串。我们跟踪唯一标识的数量,并为每个标识分配一个递增的数字。这些独特的标识将形成神经网络的输出层。每个唯一标识将被分配一个输出神经元。当图像输入到神经网络之后,输出最高的输出神经元将代表网络的图像同一性。assignidentity方法是一种简单的方法,分配增加计数和映射标识字符串到神经指数。

final int idx = assignidentity(identity) ;

A File object is created to hold the image. This will later be used to also read the image.

final filefile= new file( image ) ;

At this point we do not wish to actually load the individual images. We will simply make note of the image by saving an ImagePair object. The ImagePair object links the image to its output neuron index number. The ImagePair class is not built into Encog. Rather,
it is a structure used by this example to map the images.

在这一点上,我们不希望实际加载单个图像。我们将通过简单的保存imagepair对象来标记图像。imagepair对象链接图像到它的输出神经元数指数。imagepair类没有建在encog里。相反,它是这个例子用于映射图像的一种结构。

this.imageList.add (new ImagePair ( file, idx ) ) ;

Finally, we display a message that tells us that the image has been added.

System.out.println ( ”Added input image : ” + image ) ;}

Once all the images are added, the number of output neurons is apparent and we can create the actual neural network. Creating the neural network is explained in the next section.

一旦所有的图像被添加,输出神经元的数量是明显的,我们可以创建实际的神经网络。在下一节中解释了创建神经网络。

9.4.3 Creating the Network

9.4.3 创建网络

The Network command is implemented by the processInput method. This method is shown here.

private void processNetwork ( ) throws IOException {

Begin by downsampling the images. Loop over every ImagePair previously created.

System.out.println ( ”Downsampling images...” ) ;

for ( final ImagePair pair : this.imageList ) {

Create a new BasicMLData to hold the ideal output for each output neuron.

final MLData ideal = new Bas
c213
icMLData ( this.outputCount ) ;

The output neuron that corresponds to the identity of the image currently being trained will be set to 1. All other output neurons will be set to -1.

与当前正在训练的图像的标识相对应的输出神经元将被设置为1。所有其他输出神经元将被设置为- 1。

final int idx = pair.g e tidentity( ) ;

for ( int i = 0 ; i < this.outputCount ; i++) {

i f ( i == idx ) {

ideal.setData ( i , 1) ;

}

else {

ideal.setData ( i , ?1) ; }

}

The input data for this training set item will be the downsampled image. First, load the image into a Java Image object.

final Image img = ImageIO.read ( pair.g e t file( ) ) ;

Create an ImageMLData object to hold this image and add it to the training set.

final ImageMLData data = new ImageMLData( img ) ;

this.training.add ( data , ideal ) ;

}

There are two parameters provided to the Network command that specify the number of neurons in each of the two hidden layers. If the second hidden layer has no neurons, there is a single hidden layer.

有两个参数提供给网络命令,指定两个隐藏层中每个神经元的个数。如果第二隐藏层没有神经元,则有一个隐藏层。

final String strHidden1 = getArg ( ” hidden1 ” ) ;

final String strHidden2 = getArg ( ” hidden2 ” ) ;

final int hidden1 = Integer.parseInt ( strHidden1 ) ;

final int hidden2 = Integer.parseInt ( strHidden2 ) ;

We are now ready to downsample all of the images.

this.training.downsample (

this.downsampleHeight , this.downsampleWidth ) ;

Finally, the new neural network is created according to the specified parameters. The final true parameter specifies that we would like to use a hyperbolic tangent activation function.

最后,根据指定的参数建立新的神经网络。最后的真参数指定我们希望使用双曲正切激活函数。

this.network = EncogUtility.simpleFeedForward (

this.training.getInputSize ( ) ,

hidden1 , hidden2 ,

this.training.g e t ideal S i z e ( ) , true ) ;

Once the network is created, report its completion by printing a message.

System.out.println ( ” Created network : ” +

this.network.t o S tring ( ) ) ;

}

Now that the network has been created, it can be trained. Training is handled in the next section.

既然已经创建了网络,就可以训练它了。培训将在下一节进行。

9.4.4 Training the Network

9.4.4 训练网络

The Train command is implemented by the processTrain method. This method is shown here.

private void processTrain ( ) throws IOException {

The Train command takes four parameters. The following lines read these parameters.

final String strMode = getArg ( ”mode” ) ;

final String strMinutes = getArg ( ” minutes ” ) ;

final String strStrategyError = getArg ( ”strategyerror” ) ;

final String strStrategyCycles = getArg ( ”strategycycles” ) ;

Once the parameters are read, display a message stating that training has begun.

System.out.println ( ” Training Beginning...Output patterns=”

+ this.outputCount ) ;

Parse the two strategy parameters.

final double strategyError = Double.parseDouble ( strStrategyError

) ;

final int strategyCycles = Integer.parseInt ( strStrategyCycles ) ;

The neural network is initialized to random weight and threshold values. Sometimes the random set of weights and thresholds will cause the neural network training to stagnate. In this situation, reset a new set of random values and begin training again. Training
is initiated by creating a new ResilientPropagation trainer. RPROP training was covered in Chapter 5 “Propagation Training”.

将神经网络初始化为随机权值和阈值。有时,权值和阈值的随机设定会导致神经网络训练停滞。在这种情况下,重新设置一组新的随机值,然后再次开始训练。培训是创造一个新的resilientpropagation。RPROP训练在第5章“扩展训练”有说明。

final ResilientPropagation train =

new ResilientPropagation ( this.network , this.training ) ;

Encog allows training strategies to be added to handle situations such as this. One particularly useful training strategy is the ResetStrategy, which takes two parameters. The first states the minimum error that the network must achieve before it will be automatically
reset to new random values. The second parameter specifies the number of cycles that the network is allowed to achieve this error rate. If the specified number of cycles is reached and the network is not at the required error rate, the weights and thresholds
will be randomized.

encog允许训练策略被添加到处理这种情况。一个特别有用的培训策略是resetstrategy,这需要两个参数。第一个状态说明网络在自动重置为新的随机值之前必须达到的最小错误。第二个参数指定网络允许达到这个错误率的周期数。如果达到指定的周期数,并且网络没有达到所需的错误率,则权重和阈值将被随机化。

Encog supports a number of different training strategies. Training strategies enhance whatever training method in use. They allow minor adjustments as training progresses. Encog supports the following strategies:

encog支持许多不同的训练策略。培训策略提高了培训中的培训方法。他们允许在训练过程中进行小的调整。encog支持以下策略:

? Greedy

? HybridStrategy

? ResetStrategy

? SmartLearningRate

? SmartMomentum

? StopTrainingStrategy

The Greedy strategy only allows a training iteration to save its weight and threshold changes if the error rate was improved. The HybridStrategy allows a backup training method to be used if the primary training method stagnates. The hybrid strategy was explained
in Chapter 7 “Other Neural Network Types”. The ResetStrategy resets the network if it stagnates. The SmartLearningRate and SmartMomentum strategies are used with backpropagation training to attempt to automatically adjust momentum and learning rate. The StopTrainingStrategy
stops training if it has reached a certain level. The following lines of code add a reset strategy.

贪婪策略只允许训练迭代保存其权重和阈值变化,如果错误率得到改善。如果主要训练方法停止,hybridstrategy允许备份的训练方法使用。第7章“其他神经网络类型”解释了混合策略。resetstrategy重置网络如果它停滞不前。smartlearningrate和smartmomentum策略的使用与反向传播训练尝试自动调整学习速率和动量。如果已经达到了一定的误差水平,stoptrainingstrategy停止训练。下面的代码行添加了重置策略。

train.addStrategy (

new ResetStrategy ( strategyError , strategyCycles ) ) ;

If we are truing using the GUI, then we must use trainDialog, otherwise we should use trainConsole.

if (strMode.equalsIgnoreCase (”gui”) ) {

EncogUtility.trainDialog ( train , this.network , this.training ) ;

} else {

final int minutes = Integer.parseInt ( strMinutes ) ;

EncogUtility.trainConsole ( train , this.network , this.training ,

minutes ) ;

}

The program will indicate that training has stopped by displaying a message such as the one shown below. The training process stops when it is canceled by the dialog or, in the case of GUI mode, has been canceled.

该程序将显示通过显示如下消息来停止培训。当对话框被取消时,或者在GUI模式被取消时,培训过程停止。

System.out.println ( ” Training Stopped...” ) ;

}

Once the neural network is trained, it is ready to recognize images. This is discussed in the next section.

一旦训练好神经网络,它就可以识别图像。这将在下一节中讨论。

9.4.5 Recognizing Images

9.4.5 识别图像

The WhatIs command is implemented by the processWhatIs method. This method is shown here.

public void processWhatIs ( ) throws IOException {

The WhatIs command takes one parameter. The following lines read this parameter.

final String filename = getArg ( ” image ” ) ;

The image is then loaded into an ImageMLData object.

final filefile= new file( filename ) ;

final Image img = ImageIO.read ( file) ;

final ImageMLData input = new ImageMLData( img ) ;

The image is downsampled to the correct dimensions.

input.downsample ( this.downsample , false , this.downsampleHeight ,

this.downsampleWidth , 1 , -1) ;

The downsampled image is presented to the neural network, which chooses the “winner” neuron. The winning neuron is the neuron with the greatest output for the pattern that was presented. This is simple “one-of” normalization as discussed in Chapter 2. Chapter
2 also introduced equilateral normalization, which could also be used.

下采样后的图像呈现给神经网络,选择“胜利者”神经元。获胜的神经元是所呈现模式输出最大的神经元。这是简单的“第2章中讨论的标准化”之一。第2章也引入了等边正规化,也可以使用。

final int winner = this.network.winner ( input ) ;

System.out.println ( ”What i s : ” + filename + ” , i t seems to be : ”

+ this.neuron2identity.get ( winner ) ) ;

}

Finally, we display the pattern recognized by the neural network. This example demonstrated a simple script-based image recognition program. This application could easily be used as the starting point for other more advanced image recognition applications.
One very useful extension to this application may be the ability to load and save the trained neural network.

最后,我们展示了神经网络识别的模式。这个例子演示了一个简单的基于脚本的图像识别程序。此应用程序可以很容易地用作其他更高级的图像识别应用程序的起点。此应用程序的一个非常有用的扩展可能是加载和保存受过训练的神经网络的能力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Encog 神经网络
相关文章推荐