Background
The internet brings much convenience to the life of people, but brings much harm to the life of people. Network attacks operated by computer automatic programs are ubiquitous and time-free, such as batch registration and login, large-scale library collision, malicious voting, irrigation and the like, not only can cause great harm to computer networks, but also can cause threats to information security of people, and the technologies of the verification codes can better prevent the threats.
The types of verification codes are various, such as short message verification codes which often appear during dynamic login, image identification verification codes during website login, verification codes which click all images of the same category when the website is logged in 12306, and a series of verification codes which are strange with one another, such as sliding puzzle blocks, clicking Chinese characters, calculating questions and the like. The verification code distinguishes whether the operator is a user or a machine through the identification and input operation of the operator, and can prevent the illegal operation of the machine to a great extent to achieve the aim of protecting the website.
The type of the verification codes on a plurality of websites at present is an image type verification code, and the pass entering the website is obtained by identifying characters and inputting the characters, but the problem is that the image verification codes passed by a plurality of websites at present are low in level, although human eyes can easily identify the pass, the identification rate of a machine at present is high, and thus the function of the verification codes is greatly weakened. The low-level image-based verification code is no different from the open door to an attacker. Therefore, it is necessary to design a verification code generation method based on image style conversion.
Disclosure of Invention
In view of the above situation, in order to overcome the defects of the prior art, the verification code generation method based on image style conversion is provided, the verification code generation method is novel in structure and ingenious in concept, and the generated image verification code can be proved to have the basic properties of being friendly to human but not friendly to machine from the recognition rate and the recognition efficiency, so that the website can be well protected.
In order to achieve the purpose, the invention provides the following technical scheme: a verification code generation method based on image style conversion comprises the following steps:
step S1: training the VGG model through an RMSProp algorithm to obtain a VGG-19 network model;
step S2: sending the content image into the trained VGG-19 network model to obtain the characteristic diagram output of the content image on each layer;
step S3: sending the style image into a VGG-19 network model to obtain characteristic diagrams of the style image on each layer and outputting the characteristic diagrams;
step S4: and (4) outputting the feature maps of the content images on all layers and the feature maps of the style images on all layers into a trained VGG-19 network model, and obtaining the verification codes through an image verification code generation algorithm.
Preferably, the RMSProp algorithm performs an exclusive-weight addition of the previous gradient square accumulation and the sub-gradient square, and updates the expression of the model parameters as:
wherein eta is
0Denotes the initial learning rate, W
tThe parameters of the model representing the time t,
the time loss function J (W) is expressed in terms of the gradient of W, and a small number epsilon, E [ g ] is set to avoid the denominator being 0
2]
tAnd expressing the weighted sum of the gradient squares of the first t times, wherein alpha expresses power, the value in the most important root expression carries out exclusive weight addition on the previous gradient square accumulation and the gradient square of the time, then the sum is squared, and finally the initial learning rate is divided by the root output value, and the value is the updated learning rate.
Preferably, in step S2, the content image is represented by a Gram matrix, and the steps are as follows:
extracting feature maps of each layer of a style image and a target image (initially a noise image) by using a VGG-19 network model;
respectively converting the characteristic diagrams of a certain layer of the two images into Gram matrixes, and calculating the difference between the two Gram matrixes;
and thirdly, operating the characteristic diagrams of each layer of the two images, then adding the differences of each layer to obtain a sum, and continuously adjusting the target image in the calculation process to enable the target image to be close to the content image and the style image simultaneously.
Preferably, the expression of the Gram matrix is as follows:
representing image features.
Preferably, the expression of the image verification code generation algorithm is as follows:
in the form of an original image of the content,
in the form of an original stylistic image,
for the image to be generated (initially a noisy image), to be trained
Existing
In addition, have
Can then be combined
Content loss and
the final loss function may be represented by the following formula:
where α and β are hyper-parameters that balance the two losses, if α is too large, the restored image will be closer
The style of (1); if β is larger, then closer
The style of (1);
after training begins, a noise image is initialized randomly
This is an image object that is to be continuously optimized, and will be
Simultaneously input into the network to extract content images
Extracting a style image from the features of a certain layer
Features on multiple layers are given weight and sum through L
contentAnd L
styleDifferent weights of (2) calculate the total loss L
totalThen according to gradient descent algorithm, adjust
To reduce L
totalIterating the target until the iteration is finished and outputting
At this time
The method has the advantages of content and style and only needs to store.
Preferably, the gradient descent algorithm sets W as a parameter of the model, and thus it is an object that needs to be trained continuously. Setting J (W) as a loss function, eta as a learning rate,
for the partial derivative of the loss function J (W) with respect to the parameter W, the standard gradient descent algorithm updates the model parameters by the following expression:
wherein, WtRepresenting the model parameters at time t.
Preferably, the step S4 further includes an optimization algorithm:
a denotes an array storing images of a plurality of genres,
representing an original content image;
representing an original nth style image;
representing an image to be generated;
representing content images
And a target image
A loss function of (d);
image generation under characteristic diagram of m-th layer
With the nth style image
Then the total loss with the plurality of stylistic images under the given mth layer profile can be expressed as:
since the style image is a weighted average of five layers of feature maps, the overall style loss function of the five layers of feature maps can be expressed as:
next, the content loss and the various style losses are combined to generate an image, and the final loss function can be expressed as:
where n is the length of array A, 1/n is the sum of all stylized images and the generated image
Style loss L of
layer-totalAn averaging process is performed to make L
contentAnd L
layer-totalIn the same dimension. And alpha and beta are two in balanceIf alpha is larger, the target image is closer to the original content image
The style of (1); if beta is larger, the style is closer to the style of a mixture of a plurality of styles.
The invention has the beneficial effects that:
the generated image verification code can be proved to have the basic properties of being friendly to human but not friendly to machine from the aspects of identification rate and identification efficiency, and can well play a role in protecting websites.
Detailed Description
The following describes the present invention in further detail with reference to fig. 1.
As shown in fig. 1, the present invention provides the following technical solutions: a verification code generation method based on image style conversion comprises the following steps:
step S1: training the VGG model through an RMSProp algorithm to obtain a VGG-19 network model;
step S2: sending the content image into the trained VGG-19 network model to obtain the characteristic diagram output of the content image on each layer;
step S3: sending the style image into a VGG-19 network model to obtain characteristic diagrams of the style image on each layer and outputting the characteristic diagrams;
step S4: and (4) outputting the feature maps of the content images on all layers and the feature maps of the style images on all layers into a trained VGG-19 network model, and obtaining the verification codes through an image verification code generation algorithm.
Preferably, the RMSProp algorithm is an exclusive-or addition of the previous gradient squared sum and the sub-gradient squared, which updates the model parameters by the expression:
wherein eta is
0Denotes the initial learning rate, W
tThe parameters of the model representing the time t,
the time loss function J (W) is expressed in terms of the gradient of W, and a small number epsilon, E [ g ] is set to avoid the denominator being 0
2]
tAnd expressing the weighted sum of the gradient squares of the first t times, wherein alpha expresses power, the value in the most important root expression carries out exclusive weight addition on the previous gradient square accumulation and the gradient square of the time, then the sum is squared, and finally the initial learning rate is divided by the root output value, and the value is the updated learning rate.
Preferably, the content image is represented by a Gram matrix in step S2, which includes the following steps:
extracting feature maps of each layer of a style image and a target image (initially a noise image) by using a VGG-19 network model;
respectively converting the characteristic diagrams of a certain layer of the two images into Gram matrixes, and calculating the difference between the two Gram matrixes;
and thirdly, operating the characteristic diagrams of each layer of the two images, then adding the differences of each layer to obtain a sum, and continuously adjusting the target image in the calculation process to enable the target image to be close to the content image and the style image simultaneously.
Preferably, the expression of the Gram matrix is as follows:
representing imagesAnd (5) characterizing.
Preferably, the expression of the image verification code generation algorithm is as follows:
in the form of an original image of the content,
in the form of an original stylistic image,
for the image to be generated (initially a noisy image), to be trained
Existing
In addition, have
Can then be combined
Content loss and
the final loss function may be represented by the following formula:
where α and β are hyper-parameters that balance the two losses, if α is too large, the restored image will be closer
The style of (1); if β is larger, then closer
The style of (1);
after training begins, a noise image is initialized randomly
This is an image object that is to be continuously optimized, and will be
Simultaneously input into the network to extract content images
Extracting a style image from the features of a certain layer
Features on multiple layers are given weight and sum through L
contentAnd L
styleDifferent weights of (2) calculate the total loss L
totalThen according to gradient descent algorithm, adjust
To reduce L
totalIterating the target until the iteration is finished and outputting
At this time
The method has the advantages of content and style and only needs to store.
Preferably, the gradient descent algorithm sets W as a parameter of the model, and thus it is an object that needs to be trained continuously. Setting J (W) as a loss function, eta as a learning rate,
for the partial derivative of the loss function J (W) with respect to the parameter W, the standard gradient descent algorithm updates the model parameters by the following expression:
wherein, WtRepresenting the model parameters at time t.
Preferably, step S4 further includes an optimization algorithm:
a denotes an array storing images of a plurality of genres,
representing an original content image;
representing an original nth style image;
representing an image to be generated;
representing content images
And a target image
A loss function of (d);
image generation under characteristic diagram of m-th layer
With the nth style image
Damage ofA loss function, then the total loss with the plurality of stylistic images under the given mth layer profile can be expressed as:
since the style image is a weighted average of five layers of feature maps, the overall style loss function of the five layers of feature maps can be expressed as:
next, the content loss and the various style losses are combined to generate an image, and the final loss function can be expressed as:
where n is the length of array A, 1/n is the sum of all stylized images and the generated image
Style loss L of
layer-totalAn averaging process is performed to make L
contentAnd L
layer-totalIn the same dimension. Alpha and beta are hyper-parameters for balancing two losses, if alpha is larger, the target image is closer to the original content image
The style of (1); if beta is larger, the style is closer to the style of a mixture of a plurality of styles.
The invention has the beneficial effects that: the generated image verification code can be proved to have the basic properties of being friendly to human but not friendly to machine from the aspects of identification rate and identification efficiency, and can well play a role in protecting websites.
Finally, it should be noted that: although the present invention has been described in detail with reference to the foregoing embodiments, it will be apparent to those skilled in the art that changes may be made in the embodiments and/or equivalents thereof without departing from the spirit and scope of the invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.