Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the invention, as detailed in the appended claims.
The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used in this specification and the appended claims, the singular forms "a", "an", and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It should also be understood that the term "and/or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
It is to be understood that although the terms first, second, third, etc. may be used herein to describe various information, these information should not be limited to these terms. These terms are only used to distinguish one type of information from another. For example, first information may also be referred to as second information, and similarly, second information may also be referred to as first information, without departing from the scope of the present invention. The word "if" as used herein may be interpreted as "at … …" or "when … …" or "in response to a determination", depending on the context.
The invention provides a human face simple stroke generation method, which aims to overcome the influence of human face attribute change on the synthesis quality of portrait simple strokes, so that high-quality portrait simple strokes with clear and attractive appearance and consistent identity can be synthesized for human face images with any attribute, and the individual requirements of different human face attributes are met.
The following describes the method for generating human face strokes in detail by using a specific embodiment.
Fig. 1 is a flowchart illustrating an embodiment of a face sketch generating method according to an exemplary embodiment of the present invention, where the face sketch generating method may be applied to an electronic device (e.g., a PC, a terminal, a server, etc.). As shown in fig. 1, the method for generating human face sketching includes the following steps:
step 101: cutting out a face image from the received image, and predicting the attribute category of the face in the face image.
In an embodiment, for a process of cutting out a face image from a received image, the image may be input into a trained face detection model, so that the face detection model detects a face in the image, predicts a position of a face key point, performs affine transformation on the image according to the position of the face key point to correct the face in the image, and finally cuts out a face image with a set size from the image after affine transformation.
The face key points may include key positions such as a left eye center, a right eye center, a nose tip, two mouth corners, and the like. The face in the image can be corrected by affine transformation. Optionally, the left eye and the right eye of the human face in the image may be located at horizontal positions through affine transformation, and a set pixel distance is provided between the left eye and the right eye.
For example, the left eye and the right eye can be adjusted to horizontal positions by affine transformation, and the distance between the two eyes is adjusted to 120 pixels, when clipping is performed, a face image with the size of 512 by 512 pixels can be clipped from the boundary of the two eyes to the upper edge of the image, wherein the distance between the two eyes is 250 pixels, and the center point of the two eyes is located on the vertical central line of the face image.
Those skilled in the art will understand that the face detection model may be implemented in the related art, and the specific implementation manner of the face detection model in the present invention is not limited, for example, the MTCNN model may be used to perform face key point detection.
It should be noted that before predicting the attribute type of the face in the face image, operations of adjusting brightness and beautifying skin may be performed on the face image, so as to improve the visual quality of the face image.
In an embodiment, for the process of predicting the attribute class of the face in the face image, the face image may be input into a trained prediction model, so as to extract a feature map of the face image from a feature extraction network in the prediction model and output to an attribute prediction network in the prediction model, where the attribute prediction network predicts the attribute class of the face based on the feature map.
Illustratively, the attribute categories may include young men, young women, old men, old women, and the like.
In the present invention, the prediction model is a multi-task learning model, that is, the prediction model further includes a weight prediction network, and for the training process of the prediction model, please refer to the following related description in step 104, which is not detailed herein first.
Step 102: and inputting the face image into a trained general face portrait synthesis model so as to enable the general face portrait synthesis model to synthesize a first face simple stroke of the face image.
And the output of the general human face portrait synthesis model is a human face portrait simple stroke directly synthesized without considering the attribute class of the human face.
Step 103: and determining a trained special face portrait synthetic model required by the attribute category, and inputting the face image into the special face portrait synthetic model so as to enable the special face portrait synthetic model to synthesize a second face simple stroke of the face image.
The output of the special face portrait synthesis model is a face portrait simplified stroke synthesized by considering the face attribute category, and each attribute category correspondingly uses one special face portrait synthesis model to synthesize the face portrait simplified stroke.
Before executing step 102 and step 103, a generic face portrait synthesis model and a dedicated face portrait synthesis model corresponding to each attribute class need to be trained respectively.
The general face portrait synthetic model and the special face portrait synthetic model both adopt a generation confrontation network structure during training. And the human face portrait synthesis model and the special human face portrait synthesis model are both realized by adopting an encoder-decoder structure.
The encoder is implemented using multiple convolutional layers, e.g., the encoder may employ a VGGFace feature extractor, and correspondingly, the decoder is implemented using multiple transposed convolutional layers, a normalization layer, and an activation layer.
The training process of the dedicated human face portrait synthesis model corresponding to each attribute category may include:
the attribute categories include four categories: the method comprises the steps of firstly obtaining a face sample set, marking each face sample in the face sample set with an attribute category, obtaining real face simple strokes corresponding to each face sample, constructing a corresponding special face portrait synthesis model and a discrimination model aiming at each attribute category, and optimizing the constructed special face portrait synthesis model and the discrimination model in an alternating iteration mode by using the face samples marked with the attribute categories and the corresponding real face simple strokes.
The special human face portrait synthesis model is input as a human face sample and output as a synthesized human face simple stroke; the judgment model is input as a synthesized face sketch, output as the judgment result and the face attribute of the face sketch, input as a real face sketch and output as the judgment result and the face attribute of the real face sketch.
Based on the description, the loss value of the discrimination model is obtained from the discrimination result and the classification of the synthesized face sketch strokes and the discrimination result and the attribute classification of the real face sketch strokes, and the loss value of the special face portrait synthesis model is obtained from the content loss value between the synthesized face sketch strokes and the face sample, the style loss value between the synthesized face sketch strokes and the real face sketch strokes and the loss value of the discrimination model.
Wherein, the loss value L of the discrimination model is calculated according to the discrimination result and the classification of the synthesized face sketch strokes and the discrimination result and the attribute classification of the real face sketch strokesca-advCross entropy loss function calculations may be employed.
The calculation formula of the content loss value between the synthesized face strokes and the face samples is as follows:
representing encoders, i.e.
After the face sample x is input into the coder, the feature diagram of the j-th computing layer is obtained,
representing the characteristic graph of the j-th computing layer after the synthesized face sketch strokes G (x) are input into the encoder, C
j、H
jAnd W
jThe channel number, the length and the width of the feature map output by the jth computing layer are respectively.
The formula for calculating the style loss value between the synthesized face sketch and the real face sketch is as follows:
gram (. circle.) denotes a Gram matrix, i.e.
Representing the Gram matrix of the characteristic diagram output by the k-th computing layer after the synthesized face simple stroke G (x) is input into the encoder,
and the Gram matrix represents the characteristic diagram output by the k-th computing layer after the strokes s of the real face are input into the encoder.
Loss value L based on the above discriminant modelca-advContent loss value LcontentStyle loss value LstyleThe formula for calculating the loss value of the special face portrait synthesis model is as follows:
Lglobal=Lidentity+λLstyle+βLca-adv(formula 3)
Wherein lambda is more than or equal to 0, and β is more than or equal to 0.
In the training process, the special human face portrait synthesis model and the discrimination model are alternately and iteratively optimized, namely the optimization formula is as follows:
wherein G represents the optimization of the special human face portrait synthesis model, and D represents the optimization of the discrimination model.
It should be noted that the training process for the general face portrait synthesis model is the same as the training principle of the special face portrait synthesis model, and the difference is that the general face portrait synthesis model can be trained and optimized by using all face samples in the face sample set, and details are not repeated.
Optionally, the general face portrait synthesis model may be trained first, then the special face portrait synthesis model is initialized by using the general face portrait synthesis model, and then the special face portrait synthesis model is subjected to fine tuning optimization, so as to improve the model training efficiency.
Step 104: and fusing the first face sketch and the second face sketch to obtain a third face sketch.
The prediction model described in the step 101 further includes a weight prediction network, the feature extraction network in the prediction model extracts the feature map of the face image and outputs the feature map to the weight prediction network, and the weight prediction network predicts fusion weights based on the feature map.
Further, the first face sketch and the second face sketch are fused by utilizing the fusion weight, and the fusion formula is as follows:
Ofinal=β·Gk*(x)+(1-β)·Gu(x) (formula 5)
Wherein, represents the dot product of pixel values, Gk*(x) Representing a second face of the simple stroke, Gu(x) Representing the first face sketch and β representing the fusion weight.
Aiming at the training process of the prediction model, each face sample in the face sample set can be used for optimizing a feature extraction network and an attribute prediction network in the constructed prediction model until the feature extraction network and the attribute prediction network are converged, and then a feature map obtained by each face sample in the face sample set through the optimized feature extraction network is used for optimizing a weight prediction network in the constructed prediction model until the loss value of the weight prediction network is lower than a preset value;
the characteristic extraction network comprises a plurality of convolution layers, and the attribute prediction network and the weight prediction network are both realized by a plurality of fully-connected layers.
And the loss value of the weight prediction network is a content loss value between the face sample and the third face simple stroke obtained by fusion.
And the third face simple-strokes obtained by fusion are obtained by fusing first face simple-strokes and second face simple-strokes with fusion weights obtained by a feature diagram of the face samples through a weight prediction network after the face samples are respectively subjected to a general face portrait synthetic model and a corresponding special face portrait synthetic model.
Therefore, the attribute type prediction is realized by the feature extraction network and the attribute prediction network in the prediction model, and the fusion weight prediction is realized by the feature extraction network and the weight prediction network in the prediction model, so that the prediction model is a multi-task learning model.
Before optimizing the weight prediction network, the optimization training of the general face portrait synthesis model and the special face portrait synthesis model needs to be completed.
It should be noted that after the face image is cut out from the received image, the face image may be input into the trained face analysis model, so that the face analysis model segments each region of the face in the face image, and obtains the position of the face region output by the face analysis model.
The region of each part of the human face may include 11 region areas, such as left eyebrow, right eyebrow, left eye, right eye, nose, mouth, face, hair, neck, trunk, and background. Referring to fig. 2, as shown in fig. 2, the analysis results corresponding to 11 regions output by the face analysis model, the 11 regions include a left eyebrow, a right eyebrow, a left eye, a right eye, a nose, a mouth, a face, hair, a neck, a trunk, and a background.
It should be further noted that after the third face skeleton strokes are obtained, post-processing operation may be performed on the third face skeleton strokes, skeleton strokes located in the face region in the processed third face skeleton strokes may be adjusted, and vectorization operation may be performed on the adjusted third face skeleton strokes to obtain final face skeleton strokes.
The post-processing operation comprises the operations of blurring, binarization, expansion and the like so as to make up for narrower gaps and slender gullies, eliminate smaller cavities and fill up fractures in the contour line so as to achieve the purpose of smoothing the contour.
Optionally, when the simple strokes in the face region are adjusted, the simple strokes located in the face region may be removed for a user whose attribute category is young women, so as to achieve an effect of removing black lines and spots in the face skin region, for a user whose attribute category is old men, the left eye, the right eye and the nose region are respectively expanded by a preset pixel distance to obtain an expanded region, and the simple strokes located in the face region but not located in the expanded region are removed, so as to achieve an effect of constraining lines corresponding to crow's feet and statute lines in the face skin region within a threshold length.
Finally, vectorization operation is carried out on the adjusted third face sketch so that the generated lines are smoother, the final face portrait sketch is simpler and more attractive, and the characteristics and the requirements of users of different ages and sexes are met.
For the process from the step 101 to the step 104, referring to the system structure shown in fig. 3, firstly, inputting a face photo into a general generator to obtain first face simple strokes, inputting the face photo into a prediction model to obtain attribute categories and fusion weights, selecting corresponding special generators by an attribute category control gating module, inputting the face photo into the selected special generators to obtain second face simple strokes, further fusing the first face simple strokes and the second face simple strokes by using the fusion weights to obtain third face simple strokes, and then performing operations such as blurring, binaryzation, expansion, adjustment of simple strokes in a face area, vectorization and the like on the third face simple strokes by using an adaptive post-processing module to obtain final face image simple strokes for output.
In this embodiment, when a general face portrait synthesis model is used to synthesize first face simple strokes, different special face portrait synthesis models are used to synthesize second face simple strokes according to different face attribute categories, so as to overcome the influence of face attribute changes on the synthesis quality of the portrait simple strokes, and then third face simple strokes obtained by fusing the first face simple strokes and the second face simple strokes are more accurate, and meet the personalized requirements of different face attributes.
Fig. 4 is a hardware block diagram of an electronic device according to an exemplary embodiment of the present invention, the electronic device including: a communication interface 401, a processor 402, a machine-readable storage medium 403, and a bus 404; wherein the communication interface 401, the processor 402 and the machine-readable storage medium 403 communicate with each other via a bus 404. The processor 402 can execute the above-described face sketch generating method by reading and executing machine executable instructions corresponding to the control logic of the face sketch generating method in the machine readable storage medium 403, and the details of the method are as described in the above embodiments, and will not be described herein again.
The machine-readable storage medium 403 referred to in this disclosure may be any electronic, magnetic, optical, or other physical storage device that can contain or store information such as executable instructions, data, and the like. For example, the machine-readable storage medium may be: volatile memory, non-volatile memory, or similar storage media. In particular, the machine-readable storage medium 403 may be a RAM (Random Access Memory), a flash Memory, a storage drive (e.g., a hard disk drive), any type of storage disk (e.g., an optical disk, a DVD, etc.), or similar storage medium, or a combination thereof.
Corresponding to the embodiment of the face sketch generating method, the invention also provides an embodiment of a face sketch generating device.
Fig. 5 is a flowchart illustrating an embodiment of a face sketch generating device according to an exemplary embodiment of the present invention, where the face sketch generating device may be applied to an electronic device. As shown in fig. 5, the face sketch generating device includes:
an attribute prediction module 510, configured to crop a face image from a received image, and predict an attribute category of a face in the face image;
a general synthesis module 520, configured to input the face image into a trained general face portrait synthesis model, so that the general face portrait synthesis model synthesizes a first face simple stroke of the face image;
a special synthesis module 530, configured to determine a trained special face portrait synthesis model that needs to be used by the attribute category, and input the face image into the special face portrait synthesis model, so that the special face portrait synthesis model synthesizes a second face simple stroke of the face image;
and the fusion module 540 is configured to fuse the first face sketch and the second face sketch to obtain a third face sketch.
In an optional implementation manner, the attribute prediction module 510 is specifically configured to, in a process of predicting an attribute category of a face in the face image, input the face image into a trained prediction model, extract a feature map of the face image by using a feature extraction network in the prediction model, and output the feature map to an attribute prediction network in the prediction model, where the attribute prediction network predicts an attribute category of the face based on the feature map.
In an alternative implementation, the apparatus further comprises (not shown in fig. 5):
the training module is used for acquiring a face sample set, wherein each face sample in the face sample set is marked with an attribute type, and the attribute types comprise young males, young females, old males and old females; acquiring real face simplified strokes corresponding to each face sample in the face sample set; aiming at each attribute category, constructing a corresponding special human face portrait synthesis model and a discrimination model, and optimizing the constructed special human face portrait synthesis model and the discrimination model in an alternate iteration mode by using human face samples marked with the attribute category and corresponding real human face simple strokes; the special human face portrait synthesis model is input as a human face sample and output as a synthesized human face simple stroke; the judging model inputs the synthesized face strokes and outputs the judging results and the face attributes of the face strokes, and inputs the real face strokes and outputs the judging results and the face attributes of the real face strokes; the loss value of the discrimination model is obtained from the discrimination result and the classification of the synthesized face sketch strokes and the discrimination result and the attribute classification of the real face sketch strokes, and the loss value of the special face portrait synthesis model is obtained from the content loss value between the synthesized face sketch strokes and the face sample, the style loss value between the synthesized face sketch strokes and the real face sketch strokes and the loss value of the discrimination model.
In an optional implementation manner, the prediction model further includes a weight prediction network, and the attribute prediction module 510 is further configured to extract a feature map of the face image by using a feature extraction network and output the feature map to the weight prediction network, where the weight prediction network predicts and fuses weights based on the feature map;
the fusion module 540 is specifically configured to fuse the first face sketch and the second face sketch by using the fusion weight.
In an optional implementation manner, the training module is further configured to optimize a feature extraction network and an attribute prediction network in the constructed prediction model by using each face sample in the face sample set until the feature extraction network and the attribute prediction network converge; optimizing a weight prediction network in a constructed prediction model by using a feature map obtained by each face sample in the face sample set through an optimized feature extraction network until the loss value of the weight prediction network is lower than a preset value; the loss value of the weight prediction network is a content loss value between a face sample and a third face simple stroke obtained by fusion, the third face simple stroke is obtained by fusing a first face simple stroke and a second face simple stroke by using a feature map of the face sample through a fusion weight obtained by the weight prediction network after the face sample is subjected to a general face portrait synthetic model and a corresponding special face portrait synthetic model respectively.
In an alternative implementation, the apparatus further comprises (not shown in fig. 5):
a face analysis module, configured to, after the attribute prediction module 510 cuts out a face image from a received image, input the face image into a trained face analysis model, so that the face analysis model segments each region of a face in the face image, and obtains a position of a face region output by the face analysis model;
a post-processing module, configured to perform post-processing on the third face sketch after the fusion module 540 fuses the first face sketch and the second face sketch to obtain a third face sketch; and adjusting the simplified strokes in the face area in the processed third face simplified strokes, and performing vectorization operation on the adjusted third face simplified strokes to obtain final face simplified strokes.
The implementation process of the functions and actions of each unit in the above device is specifically described in the implementation process of the corresponding step in the above method, and is not described herein again.
For the device embodiments, since they substantially correspond to the method embodiments, reference may be made to the partial description of the method embodiments for relevant points. The above-described embodiments of the apparatus are merely illustrative, and the units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of the invention. One of ordinary skill in the art can understand and implement it without inventive effort.
Other embodiments of the invention will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the invention and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the invention being indicated by the following claims.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents, improvements and the like made within the spirit and principle of the present invention should be included in the scope of the present invention.