Training method and device of video anomaly detection model based on contrastive learning, video anomaly detection method and device
By generating negative samples and positive samples video feature groups based on comparison learning, adjusting the parameters of the video anomaly detection model, the problem of memory-enhanced autoencoder lacks abnormal sample modeling in video anomaly detection, and achieving more accurate abnormality detection and lower data dependence.
Patent Information
- Application Number
- CN202510690582.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-27
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2045-05-27
AI Technical Summary
In the prior art, memory-enhanced autoencoder lacks the ability to model abnormal samples in video anomaly detection, resulting in the model being unable to effectively learn the boundary between normal samples and abnormal samples, affecting the ability to distinguish.
Using a method based on contrast learning, the parameters of the video anomaly detection model are adjusted by generating negative sample video feature groups and positive sample video feature groups, and the comparison learning loss and reconstruction loss are used to enhance the feature learning ability of the memory module.
It improves the accuracy of video anomaly detection, reduces dependence on labeled data, and improves the model's ability to distinguish between normal and abnormal samples under unsupervised conditions.
Smart Images

Figure CN120198841B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of deep reinforcement learning technology, and more specifically to a training method and device for a video anomaly detection model based on contrastive learning, and a video anomaly detection method and device. Background Art
[0002] Video anomaly detection is an important computer vision task that is widely used in security monitoring, behavior recognition, and industrial inspection. Its goal is to automatically detect abnormal events that are significantly different from normal patterns from video streams.
[0003] In the existing technology, video anomalies are generally detected through memory-enhanced autoencoders. The memory-enhanced autoencoders use memory modules to store the features of normal patterns, thereby improving the target video anomaly detection model's ability to reconstruct normal patterns. However, the feature update of the memory module only relies on normal samples and lacks the ability to model abnormal samples. It cannot effectively learn the boundary between normal and abnormal samples, which affects the model's ability to distinguish. Summary of the Invention
[0004] In view of the above problems, the present invention provides a training method for a video anomaly detection model based on contrastive learning and a video anomaly detection method.
[0005] According to a first aspect of the present invention, a training method for a video anomaly detection model based on contrastive learning is provided, comprising: inputting a sample video into a feature extraction module of an initial video anomaly detection model to obtain sample video features; inputting the sample video features into a reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video, and based on a reconstruction loss function, obtaining a reconstruction loss according to the sample video and the reconstructed sample video; generating a negative sample video feature group based on the sample video features and the reconstruction loss; performing data enhancement processing on the sample video features to obtain a positive sample video feature group; based on a contrastive learning loss function, obtaining a contrastive learning loss according to the negative sample video feature group and the positive sample video feature group; adjusting the parameters of the initial video anomaly detection model based on the contrastive learning loss and the reconstruction loss to obtain a target video detection model.
[0006] According to an embodiment of the present invention, the above-mentioned contrastive learning loss function is based on the negative sample video feature group and the positive sample video feature group to obtain the contrastive learning loss, including: inputting the negative sample video feature group and the positive sample video feature group into the memory module of the above-mentioned initial video anomaly detection model respectively to obtain negative sample similarity and positive sample similarity, the above-mentioned negative sample similarity characterizes the degree of similarity between the above-mentioned negative sample video feature group and the memory vector feature group stored in the above-mentioned memory module, and the above-mentioned positive sample similarity characterizes the degree of similarity between the above-mentioned positive sample video feature group and the above-mentioned memory vector feature group; based on the contrastive learning loss function, the above-mentioned contrastive learning loss is obtained according to the above-mentioned negative sample similarity and the above-mentioned positive sample similarity.
[0007] According to an embodiment of the present invention, the above-mentioned generation of a negative sample video feature group based on the above-mentioned sample video features and the above-mentioned reconstruction loss includes: calculating the gradient vector of the above-mentioned reconstruction loss with respect to the above-mentioned sample video features based on a gradient extraction function, the above-mentioned gradient vector characterizing the sensitivity of the above-mentioned sample video features to reconstruction errors; generating the above-mentioned negative sample video feature group based on the gradient direction information in the above-mentioned gradient vector and the above-mentioned sample video features.
[0008] According to an embodiment of the present invention, the above-mentioned data enhancement processing is performed on the above-mentioned sample video features to obtain a positive sample video feature group, including: generating a first noise based on a first preset noise range, the above-mentioned first noise characterizing the changes in environmental factors in the sample video; generating a second noise based on a second preset noise range, the above-mentioned second noise characterizing the visual interference factors in the sample video; scaling the above-mentioned sample video features according to the above-mentioned first noise to obtain a scaled positive sample video feature group; and randomly offsetting the above-mentioned scaled positive sample video feature group according to the above-mentioned second noise to obtain the above-mentioned positive sample video feature group.
[0009] According to an embodiment of the present invention, the above-mentioned memory vector feature group includes P memory vector features, the above-mentioned negative sample video feature group includes M negative sample video features, and the above-mentioned positive sample video feature group includes N positive sample video features, P, M, and N are all positive integers greater than 0, p is a positive integer less than or equal to P and greater than 0, m is a positive integer less than or equal to M and greater than, and n is a positive integer less than or equal to N and greater than 0; the above-mentioned negative sample video feature group and the above-mentioned positive sample video feature group are respectively input into the memory module of the above-mentioned initial video anomaly detection model to obtain negative sample similarity and positive sample similarity, including: for the mth negative sample video feature in the above-mentioned negative sample video feature group, using the cosine similarity function, based on the above-mentioned mth negative sample video feature and the pth above-mentioned memory vector feature, obtain the pth sub-negative sample cosine similarity, to obtain a sub-negative sample cosine similarity group, wherein the sub-negative sample cosine similarity group includes P sub-negative sample cosine similarities; for the nth positive sample video feature in the above-mentioned positive sample video feature group, using the above-mentioned cosine similarity function, based on the above-mentioned nth positive sample video feature and the above-mentioned p-th memory vector feature, obtain the p-th sub-positive sample cosine similarity, to obtain a sub-positive sample cosine similarity group, wherein the sub-positive sample cosine similarity group includes P sub-positive sample cosine similarities; using a preset normalization function, respectively normalizing and summing the above-mentioned P sub-negative sample cosine similarities to obtain the m-th negative sample similarity, to obtain M negative sample similarities; using a preset normalization function, respectively normalizing and summing the P sub-positive sample cosine similarities to obtain the n-th positive sample similarity, to obtain N positive sample similarities.
[0010] According to an embodiment of the present invention, the above-mentioned contrastive learning loss is obtained based on the above-mentioned negative sample similarity and the above-mentioned positive sample similarity, including: summing the above-mentioned N positive sample similarity indices to obtain the positive sample index sum; summing the above-mentioned M negative sample similarity indices to obtain the negative sample index sum; obtaining the sample index sum based on the above-mentioned positive sample index sum and the above-mentioned negative sample index sum; and obtaining the above-mentioned contrastive learning loss based on the above-mentioned positive sample index sum and the above-mentioned sample index sum.
[0011] According to an embodiment of the present invention, the above-mentioned contrastive learning loss is obtained based on the above-mentioned negative sample similarity and the above-mentioned positive sample similarity, including: for the nth positive sample similarity, subtracting the above-mentioned nth positive sample similarity from the above-mentioned M negative sample similarities respectively to obtain the nth sub-positive and negative sample similarity difference, and obtaining M×N sub-positive and negative sample similarity differences; based on a preset boundary value, summing the above-mentioned M×N sub-positive and negative sample similarity differences to obtain M×N sub-contrastive learning losses; and accumulating the non-negative values in the above-mentioned M×N sub-contrastive learning losses to obtain the above-mentioned contrastive learning loss.
[0012] The second aspect of the present invention provides a video anomaly detection method, including: obtaining a target video; inputting the above-mentioned target video into a target video anomaly detection model to obtain a detection result, wherein the above-mentioned detection result characterizes whether there is an anomaly in the above-mentioned target video, and the above-mentioned target video anomaly detection model is trained using the above-mentioned method of the video anomaly detection model based on contrastive learning.
[0013] The third aspect of the present invention provides a training device for a video anomaly detection model based on contrastive learning, comprising: an input extraction module for inputting a sample video into a feature extraction module of an initial video anomaly detection model to obtain sample video features; an input reconstruction module for inputting the above-mentioned sample video features into a reconstruction module of the above-mentioned initial video anomaly detection model to obtain a reconstructed sample video, and based on a reconstruction loss function, obtaining a reconstruction loss according to the above-mentioned sample video and the above-mentioned reconstructed sample video; a negative sample generation module for generating a negative sample video feature group based on the above-mentioned sample video features and the above-mentioned reconstruction loss; a positive sample enhancement module for performing data enhancement processing on the above-mentioned sample video features to obtain a positive sample video feature group; a contrastive learning loss calculation module for obtaining a contrastive learning loss according to the above-mentioned negative sample video feature group and the positive sample video feature group based on a contrastive learning loss function; and an adjustment module for adjusting the parameters of the above-mentioned initial video anomaly detection model based on the above-mentioned contrastive learning loss and the above-mentioned reconstruction loss to obtain a target video detection model.
[0014] The fourth aspect of the present invention provides a video anomaly detection device, including: an acquisition module for acquiring a target video; an input module for inputting the above-mentioned target video into a target video anomaly detection model to obtain a detection result, wherein the above-mentioned detection result characterizes whether there is an anomaly in the above-mentioned target video, and the above-mentioned target video anomaly detection model is trained using the above-mentioned training device of the video anomaly detection model based on contrastive learning.
[0015] A fifth aspect of the present invention provides an electronic device, comprising: one or more processors; a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the above method.
[0016] The sixth aspect of the present invention further provides a computer-readable storage medium having a computer program or instructions stored thereon, which implements the steps of the above method when the computer program or instructions are executed by a processor.
[0017] The seventh aspect of the present invention further provides a computer program product, comprising a computer program or instructions, which implement the steps of the above method when executed by a processor.
[0018] According to an embodiment of the present invention, a reconstructed sample video is obtained by inputting the extracted sample video features into a reconstruction module, and a reconstruction loss is obtained based on the sample video and the reconstructed sample video. Under the guidance of the reconstruction loss, a negative sample video feature group is generated based on the sample video features, and noise is added to the sample video features to generate a positive sample video feature group. The contrastive learning loss is calculated based on the negative sample video feature group and the positive sample video feature group, and the initial video anomaly detection model is jointly optimized based on the contrastive learning loss and the reconstruction loss, so that the memory module can learn a more complex positive sample video feature representation, thereby improving the recognition ability of normal videos, and clarifying the boundary between positive samples and negative samples based on the negative sample video feature group, so that video anomaly detection is more accurate. At the same time, the target video detection model can learn the difference between normal samples and abnormal samples in an unsupervised manner, reducing dependence on labeled data. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The above contents and other objects, features and advantages of the present invention will become more apparent through the following description of the embodiments of the present invention with reference to the accompanying drawings, in which:
[0020] Figure 1 A diagram illustrating an application scenario of a video anomaly detection model training method based on contrastive learning, a video anomaly detection method, a video anomaly detection model training device based on contrastive learning, and a video anomaly detection device according to an embodiment of the present invention is shown;
[0021] Figure 2 A flowchart of a method for training a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown;
[0022] Figure 3 A flow chart of a method for detecting video anomalies according to an embodiment of the present invention is shown;
[0023] Figure 4 A flowchart of another method for training a video anomaly detection model according to an embodiment of the present invention is shown;
[0024] Figure 5 A structural block diagram of a training device for a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown;
[0025] Figure 6 It shows a structural block diagram of a video anomaly detection device according to an embodiment of the present invention;
[0026] Figure 7 A block diagram of an electronic device suitable for implementing a training method for a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown. DETAILED DESCRIPTION
[0027] Hereinafter, embodiments of the present invention will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the present invention. In the following detailed description, for ease of explanation, many specific details are set forth to provide a comprehensive understanding of embodiments of the present invention. However, it is apparent that one or more embodiments may also be implemented without these specific details. In addition, in the following description, descriptions of known structures and technologies are omitted to avoid unnecessary confusion of the concept of the present invention.
[0028] The terms used herein are only for describing specific embodiments and are not intended to limit the present invention. The terms "comprise", "include", etc. used herein indicate the presence of the features, steps, operations and / or components, but do not exclude the presence or addition of one or more other features, steps, operations or components.
[0029] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification and should not be interpreted in an idealized or overly rigid manner.
[0030] When expressions such as "at least one of A, B, and C, etc." are used, they should generally be interpreted in accordance with the meaning commonly understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include but is not limited to a system having A alone, B alone, C alone, A and B, A and C, B and C, and / or A, B, C, etc.).
[0031] Video anomaly detection is an important computer vision task, widely used in fields such as security monitoring, behavior recognition, and industrial inspection. Its goal is to automatically detect abnormal events that are significantly different from normal patterns in video streams. Memory-augmented autoencoders are a classic video anomaly detection method that introduces a memory module to store the features of normal patterns, thereby improving the model's ability to reconstruct normal patterns. The memory module stores the feature representation of normal samples, and the features of the input samples are matched and enhanced with the memory module to generate more accurate reconstruction results. The reconstruction error of normal samples is low, while the reconstruction error of abnormal samples is high because they cannot be effectively matched by the memory module, thereby achieving anomaly detection. In existing technologies, feature updates in the memory module usually rely on normal samples, but lack the ability to model abnormal samples, which prevents the model from learning stronger discrimination capabilities in the feature space.
[0032] In view of this, an embodiment of the present invention provides a training method for a video anomaly detection model based on contrastive learning, comprising: inputting a sample video into a feature extraction module of an initial video anomaly detection model to obtain sample video features; inputting the sample video features into a reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video, and based on a reconstruction loss function, obtaining a reconstruction loss according to the sample video and the reconstructed sample video; generating a negative sample video feature group based on the sample video features and the reconstruction loss; performing data enhancement processing on the sample video features to obtain a positive sample video feature group; based on a contrastive learning loss function, obtaining a contrastive learning loss according to the negative sample video feature group and the positive sample video feature group; adjusting the parameters of the initial video anomaly detection model based on the contrastive learning loss and the reconstruction loss to obtain a target video detection model.
[0033] Figure 1 The present invention provides an application scenario diagram of a video anomaly detection model training method based on contrastive learning, a video anomaly detection method, a video anomaly detection model training device based on contrastive learning, and a video anomaly detection device according to an embodiment of the present invention.
[0034] like Figure 1 As shown, the application scenario 100 according to this embodiment may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 is used as a medium for providing a communication link between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or optical fiber cables.
[0035] A user may use a first terminal device 101, a second terminal device 102, or a third terminal device 103 to interact with a server 105 via a network 104 to receive or send messages, etc. Various communication client applications may be installed on the first terminal device 101, the second terminal device 102, or the third terminal device 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (for example only).
[0036] The first terminal device 101 , the second terminal device 102 , and the third terminal device 103 may be various electronic devices having display screens and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, desktop computers, and the like.
[0037] The server 105 may be a server that provides various services, such as a background management server (for example only) that supports websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103. The background management server may analyze and process received data such as user requests, and feed back processing results (e.g., web pages, information, or data obtained or generated based on user requests) to the terminal devices.
[0038] It should be noted that the video anomaly detection model training method and the video anomaly detection method based on contrastive learning provided in the embodiments of the present invention can generally be executed by the server 105. Accordingly, the video anomaly detection model training device and the video anomaly detection device based on contrastive learning provided in the embodiments of the present invention can generally be set in the server 105. The video anomaly detection model training method and the video anomaly detection method based on contrastive learning provided in the embodiments of the present invention can also be executed by a server or server cluster that is different from the server 105 and can communicate with the first terminal device 101, the second terminal device 102, the third terminal device 103 and / or the server 105. Accordingly, the video anomaly detection model training device and the video anomaly detection device based on contrastive learning provided in the embodiments of the present invention can also be set in a server or server cluster that is different from the server 105 and can communicate with the first terminal device 101, the second terminal device 102, the third terminal device 103 and / or the server 105.
[0039] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.
[0040] The following will be based on Figure 1 The scene described by Figure 2 The training method of the video anomaly detection model based on contrastive learning according to an embodiment of the present invention is described in detail.
[0041] Figure 2 A flowchart of a method for training a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown.
[0042] like Figure 2 As shown, the training method of the video anomaly detection model based on contrastive learning in this embodiment includes operations S210 to S260.
[0043] In operation S210 , the sample video is input into a feature extraction module of an initial video anomaly detection model to obtain sample video features.
[0044] According to the embodiment of the present invention, the sample video is a normal video clip with a size of , where B is the batch size, C is the number of channels, S is the frame sequence length, H and W are the height and width of a frame image in the sample video, and F represents the number of feature channels after extraction. The feature extraction module extracts video features. The feature extraction module includes the initial convolution layer and three downsampling layers. The feature size is gradually reduced to 、 、 .
[0045] In operation S220, the sample video features are input into a reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video, and a reconstruction loss is obtained according to the sample video and the reconstructed sample video based on a reconstruction loss function.
[0046] According to an embodiment of the present invention, the reconstruction module gradually restores the spatial dimensions of the sample video features, and the output sizes are 、 、 , and finally generate the reconstructed sample video through the output convolution layer. The size of the reconstructed sample video is , the reconstruction loss can be calculated by the following formula (1).
[0047] (1)
[0048] in, represents the reconstruction loss, represents the i-th frame image of the sample video, represents the i-th frame image of the corresponding reconstructed sample video, Indicates the calculation of Euclidean distance, which is used to measure the reconstruction error. Indicates the total number of frames in the sample video.
[0049] In operation S230 , a negative sample video feature group is generated based on the sample video features and the reconstruction loss.
[0050] According to an embodiment of the present invention, disturbance noise can be added to the sample video features under the guidance of reconstruction loss to obtain a negative sample video feature group, or random noise can be added to the sample video features to generate a negative sample video feature group. The feature dimension of the above sample video features is , so the feature dimension of each negative sample video feature in the negative sample video feature group obtained after adding noise is also .
[0051] In operation S240 , data enhancement processing is performed on the sample video features to obtain a positive sample video feature group.
[0052] According to an embodiment of the present invention, the data enhancement processing can be, for example, adding random noise to the sample video features to obtain a positive sample video feature group. Since the feature dimension of the sample video features is , so the feature dimension of each positive sample video feature in the positive sample video feature group is also .
[0053] According to an embodiment of the present invention, the generated random noise may be added to the sample video features to obtain a positive sample video feature group.
[0054] In operation S250 , a contrastive learning loss is obtained based on the contrastive learning loss function according to the negative sample video feature group and the positive sample video feature group.
[0055] In operation S260 , parameters of the initial video anomaly detection model are adjusted based on the contrastive learning loss and the reconstruction loss to obtain a target video detection model.
[0056] According to an embodiment of the present invention, the contrastive learning loss and the reconstruction loss can be expressed as a total loss, which can be calculated using the following formula (2):
[0057] (2)
[0058] in, represents the total loss, represents the contrastive learning loss, represents the reconstruction loss, , is the weight parameter.
[0059] According to an embodiment of the present invention, a reconstructed sample video is obtained by inputting the extracted sample video features into a reconstruction module, and a reconstruction loss is obtained based on the sample video and the reconstructed sample video. Under the guidance of the reconstruction loss, a negative sample video feature group is generated based on the sample video features, and noise is added to the sample video features to generate a positive sample video feature group. The contrastive learning loss is calculated based on the negative sample video feature group and the positive sample video feature group, and the initial video anomaly detection model is jointly optimized based on the contrastive learning loss and the reconstruction loss, so that the memory module can learn a more complex positive feature representation, thereby improving the recognition ability of normal videos, and clarifying the boundary between positive samples and negative samples based on the negative sample video feature group, so that video anomaly detection is more accurate. At the same time, the target video detection model can learn the difference between normal samples and abnormal samples in an unsupervised manner, reducing dependence on labeled data.
[0060] According to an embodiment of the present invention, a negative sample video feature group is generated based on sample video features and reconstruction loss, including: calculating the gradient vector of the reconstruction loss with respect to the sample video feature based on a gradient extraction function, the gradient vector representing the sensitivity of the sample video feature to reconstruction error; and generating the negative sample video feature group based on the gradient direction information in the gradient vector and the sample video feature.
[0061] According to an embodiment of the present invention, the negative sample video feature group can be calculated using the following formula (3).
[0062] (3)
[0063] Among them, Z represents the sample video feature, is a hyperparameter of the perturbation intensity, which is used to control the degree to which the generated negative sample video feature N deviates from the sample video feature Z. Represents the reconstruction loss, which is used to measure the error between the sample video and the sample reconstructed video. Reconstruction loss About sample video features The gradient vector of Represents the gradient direction information in the gradient vector.
[0064] According to embodiments of the present invention, by adding gradient-based perturbations to sample video features, the generated negative sample features are intentionally deviated from the distribution of normal samples, making them more challenging. The gradient direction points in the direction of increased reconstruction error, which means that the generated negative sample features are more difficult to reconstruct by the target video detection model, resulting in a distribution closer to the abnormal samples in the feature space.
[0065] According to an embodiment of the present invention, The value of will affect the generated negative sample video features, If the value is too small, the negative sample video features may be too close to the normal sample, making it difficult to distinguish them. Too large a value may cause the negative sample video features to completely deviate from the normal mode, lose structure, and affect the training effect. It is recommended to adjust it through experiments , usually choose to Set in [0.01,0.1].
[0066] According to embodiments of the present invention, adversarially generated negative video features are generated entirely from gradient vectors, eliminating the need for manual labeling of anomalous samples. This significantly reduces data requirements and labeling costs. Furthermore, by generating difficult-to-reconstruct negative video features, the model learns stronger discriminative capabilities in feature space, further separating normal and anomalous samples in the feature space. Furthermore, while the distribution of negative video features deviates from the sample video features, it still retains a certain degree of structure, effectively filling the gap in negative samples in unsupervised learning. Furthermore, the perturbation intensity is controllable, facilitating adjustments to the difficulty of the negative video features, thereby enabling the target video detection model to gradually adapt to more complex anomalous scenarios. More importantly, the gradient direction is closely related to the sample video features and the current state of the target video detection model. Therefore, the generated negative video features dynamically change during training, resulting in a high degree of diversity. This dynamically generated negative video feature avoids the overfitting problem that can result from static negative video features, improving the discriminative capability and robustness of the target video detection model.
[0067] According to an embodiment of the present invention, data enhancement processing is performed on sample video features to obtain a positive sample video feature group, including: generating a first noise based on a first preset noise range, the first noise characterizing changes in environmental factors in the sample video; generating a second noise based on a second preset noise range, the second noise characterizing visual interference factors in the sample video; scaling the sample video features according to the first noise to obtain a scaled positive sample video feature group; and randomly offsetting the scaled positive sample video feature group according to the second noise to obtain a positive sample video feature group.
[0068] According to embodiments of the present invention, the positive sample video feature set is designed to enhance the target video detection model's ability to learn normal patterns and improve its generalization performance in complex scenarios. By adding noise perturbations or feature transformations, the generated positive sample video feature set maintains semantic consistency with the original sample video features, but with a more complex background or feature distribution, making the target video detection model face greater challenges during training.
[0069] The positive sample video feature group can be calculated using the following formula (4).
[0070] (4)
[0071] in, represents the positive sample video feature, Z represents the sample video feature, represents the first noise, Represents the second noise.
[0072] According to an embodiment of the present invention, the first preset noise range is the N(1,1) range determined from noise sampled from a normal distribution. The first noise is used to scale the sample video features, and the sampled noise values fluctuate around a mean of 1 to ensure that the overall amplitude of the sample video features changes little and the semantics remain consistent, but the feature distribution is perturbed. This step simulates the changes of the sample at different scales or amplitudes, such as changes in light intensity or scene details. The second preset noise range is the N(0,1) range determined from noise sampled from a normal distribution. The second noise is used to add a random offset to the sample features, and the sampled noise values fluctuate around a mean of 0 to simulate the random changes of the sample in the background or environment. This step simulates the random perturbation of the sample in the background or environment, such as noise interference or slight occlusion. The positive sample video features have noise perturbations but maintain semantic consistency with the sample video features.
[0073] According to an embodiment of the present invention, a first noise is generated by a first preset noise range, a second noise is generated by a second preset noise range, and the sample video features are further scaled according to the first noise. The scaled positive sample video feature group is randomly offset according to the second noise to obtain a positive sample video feature group. Under the premise of ensuring semantic consistency, the positive sample video feature group simulates the changes in complex scenes in the sample video, so that the target video detection model can adapt to more diverse normal modes and show stronger robustness in the presence of noise or environmental changes. Moreover, since the feature distribution of the positive sample video feature group is more complex, the target video detection model needs a stronger ability to correctly identify these samples as normal samples during training, thereby improving the target video detection model's modeling ability for normal modes.
[0074] According to an embodiment of the present invention, based on the contrastive learning loss function, the contrastive learning loss is obtained according to the negative sample video feature group and the positive sample video feature group, including: inputting the negative sample video feature group and the positive sample video feature group into the memory module of the initial video anomaly detection model respectively to obtain the negative sample similarity and the positive sample similarity, the negative sample similarity characterizing the similarity between the negative sample video feature group and the memory vector feature group stored in the memory module, and the positive sample similarity characterizing the similarity between the positive sample video feature group and the memory vector feature group; based on the contrastive learning loss function, the contrastive learning loss is obtained according to the negative sample similarity and the positive sample similarity.
[0075] According to an embodiment of the present invention, the memory vector feature group includes P memory vector features, the negative sample video feature group includes M negative sample video features, and the positive sample video feature group includes N positive sample video features, P, M, and N are all positive integers greater than 0, p is a positive integer less than or equal to P and greater than 0, m is a positive integer less than or equal to M and greater than , and n is a positive integer less than or equal to N and greater than 0; the negative sample video feature group and the positive sample video feature group are respectively input into the memory module of the initial video anomaly detection model to obtain negative sample similarity and positive sample similarity, including: for the mth negative sample video feature in the negative sample video feature group, using the cosine similarity function, based on the mth negative sample video feature and the pth memory vector feature, obtain the pth sub-negative sample cosine similarity to obtain A sub-negative sample cosine similarity group is obtained, wherein the sub-negative sample cosine similarity group includes P sub-negative sample cosine similarities; for the nth positive sample video feature in the positive sample video feature group, a cosine similarity function is used to obtain a pth sub-positive sample cosine similarity based on the nth positive sample video feature and the pth memory vector feature, so as to obtain a sub-positive sample cosine similarity group, wherein the sub-positive sample cosine similarity group includes P sub-positive sample cosine similarities; using a preset normalization function, the P sub-negative sample cosine similarities are normalized and summed to obtain an mth negative sample similarity, thereby obtaining M negative sample similarities; using a preset normalization function, the P sub-positive sample cosine similarities are normalized and summed to obtain an nth positive sample similarity, thereby obtaining N positive sample similarities.
[0076] According to an embodiment of the present invention, the input of the memory module includes input features and the memory vector feature set stored in the memory module , the memory vector feature group includes P memory vectors, d represents the dimension of each memory vector, Represents the first memory vector feature in the memory vector feature group, and so on. Represents the pth memory vector feature in the memory vector feature group.
[0077] According to an embodiment of the present invention, the sub-negative sample cosine similarity and the sub-positive sample sine similarity can be calculated using the following formula (5):
[0078] (5)
[0079] Where k represents the kth negative sample video feature in the negative sample video feature group or the kth positive sample video feature in the positive sample video feature group. represents the pth memory vector feature in the memory vector feature group, represents the sub-negative sample cosine similarity between the kth negative sample video feature in the negative sample video feature group and the pth memory vector feature in the memory vector feature group, or the sub-positive sample cosine similarity between the kth positive sample video feature in the positive sample video feature group and the pth memory vector feature in the memory vector feature group, express and The dot product of express of norm, express of norm.
[0080] According to an embodiment of the present invention, the preset normalization function may be, for example, a normalized exponential (Softmax) function. The normalized exponential function combines the advantages of maximum similarity and average similarity, is more flexible and robust, and can be used to calculate the negative sample similarity and the positive sample similarity using the following formula (6):
[0081] (6)
[0082] in, It represents the sub-negative sample similarity between the kth negative sample video feature in the negative sample video feature group and the memory vector feature group, or the sub-positive sample similarity between the kth positive sample video feature in the positive sample video feature group and the memory vector feature group. represents the sub-negative sample cosine similarity between the kth negative sample video feature in the negative sample video feature group and the pth memory vector feature in the memory vector feature group, or the sub-positive sample cosine similarity between the kth positive sample video feature in the positive sample video feature group and the pth memory vector feature in the memory vector feature group, represents the memory vector feature group, k represents the input feature, P is the number of memory vector features, represents the pth memory vector feature in the memory vector feature group, represents the jth memory vector feature in the memory vector feature group, Represents the temperature parameter.
[0083] According to an embodiment of the present invention, the temperature parameter Usually needs to be adjusted according to the specific task and data distribution, usually set to 0.1 or 0.07, smaller Makes the distribution more sharp, emphasizing the difference between positive and negative samples; larger This will make the distribution smoother and reduce gradient fluctuations. In practical applications, the most suitable temperature parameters can be selected through experimental tuning.
[0084] According to an embodiment of the present invention, the memory module of the initial video anomaly detection model is trained using positive and negative sample video feature groups. The target video detection model can learn the different similarity features between positive and negative samples and the memory vector feature group, thereby better understanding different types of video features and enhancing the ability to distinguish between normal and abnormal video features. This helps to improve the model's accuracy in detecting video anomalies in practical applications. Furthermore, by using a comparative learning loss function, the difference in similarity between positive and negative samples is converted into an optimizable numerical indicator, which enables the target video detection model to learn in a direction that is more conducive to distinguishing between normal and abnormal video features, thereby improving the overall performance of the target video detection model.
[0085] According to an embodiment of the present invention, a contrastive learning loss is obtained based on negative sample similarity and positive sample similarity, including: summing N positive sample similarity indices to obtain a positive sample index sum; summing M negative sample similarity indices to obtain a negative sample index sum; obtaining a sample index sum based on the positive sample index sum and the negative sample index sum; and obtaining a contrastive learning loss based on the positive sample index sum and the sample index sum.
[0086] According to an embodiment of the present invention, the contrastive learning loss can be calculated using the following formula (7).
[0087] (7)
[0088] in, represents the contrastive learning loss, represents the positive sample video feature group, represents the negative sample video feature group, Represents the temperature parameter, which is used to control the smoothness of the distribution.
[0089] According to an embodiment of the present invention, the numerator of the above formula (7) calculates the similarity between the positive sample video feature group and the memory feature vector group in the memory module, and the denominator calculates the total similarity between the positive sample video feature group and the negative sample video feature group and the memory feature vector group in the memory module.
[0090] According to an embodiment of the present invention, a contrastive learning loss is obtained based on negative sample similarity and positive sample similarity, including: obtaining a contrastive learning loss based on negative sample similarity and positive sample similarity, including: for the nth positive sample similarity, subtracting the nth positive sample similarity from M negative sample similarities to obtain an nth sub-positive and negative sample similarity difference, and obtaining M×N sub-positive and negative sample similarity differences; summing a preset boundary value with the M×N sub-positive and negative sample similarity differences to obtain M×N sub-contrastive learning losses; and accumulating non-negative values in the M×N sub-contrastive learning losses to obtain a contrastive learning loss.
[0091] According to an embodiment of the present invention, the contrastive learning loss function may also be a ternary loss function, and the contrastive learning loss may be calculated using the following formula (8):
[0092] (8)
[0093] in, represents the contrastive learning loss obtained according to the ternary loss function, Represents the preset boundary value, which is used to control the similarity difference between positive and negative sample video features. Indicates a non-negative value to ensure that the loss is not less than 0. represents the positive sample video feature group, represents the negative sample video feature group, represents the negative sample similarity between the negative sample video feature group and the memory vector feature group, Represents the positive sample similarity between the positive sample video feature group and the memory vector feature group.
[0094] According to an embodiment of the present invention, the ratio of the positive sample video feature group and the negative sample video feature group is calculated by normalized probability distribution to optimize the overall distribution of the feature space. Furthermore, the contrastive learning loss is calculated by a ternary loss function, which constrains the similarity difference between the positive sample video feature group and the negative sample video feature group, thereby optimizing the local structure of the feature space.
[0095] Figure 3 A flow chart of a video anomaly detection method according to an embodiment of the present invention is shown.
[0096] like Figure 3 As shown, the video anomaly detection method of this embodiment includes operations S310 to S320.
[0097] In operation S310 , a target video is acquired.
[0098] In operation S320 , the target video is input into a target video anomaly detection model to obtain a detection result.
[0099] The detection result represents whether there is an anomaly in the target video, and the target video anomaly detection model is trained using the above-mentioned training method of the video anomaly detection model based on contrastive learning.
[0100] According to an embodiment of the present invention, the target video anomaly detection model includes a target memory module and a target reconstruction module. The target video features extracted from the target video are first calculated by the target memory module to obtain the corresponding weighted target video features, and then reconstructed by the target reconstruction module to obtain a reconstructed target video. The corresponding weight can be calculated by the following formula (9).
[0101] The weighted target video features can be calculated using the following formula (9).
[0102] (9)
[0103] in, represents the weighted target video feature, represents the i-th target memory vector feature in the target memory vector feature group stored in the target memory module, represents the target video features, is the temperature parameter.
[0104] According to an embodiment of the present invention, when identifying a video after training is completed, there is no need to generate positive sample video feature groups and negative sample video feature groups. The reconstruction error between the target video and the target reconstructed video is directly used to determine whether the target video has an abnormality.
[0105] Figure 4 A flowchart of another method for training a video anomaly detection model according to an embodiment of the present invention is shown.
[0106] In operation S410 , the initial video anomaly detection model extracts features from the sample video to obtain sample video features.
[0107] In operation S420 , a reconstruction module of the initial video anomaly detection model performs reconstruction based on the sample video features to obtain a reconstructed sample video.
[0108] In operation S430, a reconstruction loss is calculated based on the reconstructed sample video and the sample video.
[0109] In operation S440 , noise is added to the sample video features to obtain a positive sample video feature group.
[0110] In operation S450 , noise is added to the sample video based on the reconstruction loss to obtain a negative sample video feature group.
[0111] In operation S460 , a contrastive learning loss is obtained based on the positive sample video feature group and the negative sample video feature group.
[0112] In operation S470 , the contrastive learning loss and the reconstruction loss are jointly optimized.
[0113] In operation S480 , when the contrastive learning loss and the reconstruction loss meet a preset condition, a target video detection model is obtained.
[0114] Based on the above-mentioned training method of the video anomaly detection model based on contrastive learning, the present invention also provides a training device for the video anomaly detection model based on contrastive learning. Figure 5 The device is described in detail.
[0115] Figure 5 A structural block diagram of a training device for a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown.
[0116] like Figure 5 As shown, the training device 500 of the video anomaly detection model based on contrastive learning in this embodiment includes an input extraction module 510, an input reconstruction module 520, a negative sample generation module 530, a positive sample enhancement module 540, a contrastive learning loss calculation module 550 and an adjustment module 560.
[0117] The input extraction module 510 is used to input the sample video into the feature extraction module of the initial video anomaly detection model to obtain sample video features. In one embodiment, the input extraction module 510 can be used to perform the operation S210 described above, which will not be repeated here.
[0118] The input reconstruction module 520 is configured to input the sample video features into the reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video and, based on the reconstruction loss function, to determine a reconstruction loss based on the sample video and the reconstructed sample video. In one embodiment, the input reconstruction module 520 can be configured to perform operation S220 described above and will not be further described herein.
[0119] The negative sample generation module 530 is used to generate a negative sample video feature group based on the sample video features and the reconstruction loss. In one embodiment, the negative sample generation module 530 can be used to perform the operation S230 described above, which will not be repeated here.
[0120] The positive sample enhancement module 540 is used to perform data enhancement processing on the sample video features to obtain a positive sample video feature group. In one embodiment, the positive sample enhancement module 540 can be used to perform the operation S240 described above, which will not be repeated here.
[0121] The contrastive learning loss calculation module 550 is used to obtain the contrastive learning loss based on the contrastive learning loss function and the negative sample video feature set and the positive sample video feature set. In one embodiment, the contrastive learning loss calculation module 550 can be used to perform the operation S250 described above, which will not be repeated here.
[0122] The adjustment module 560 is used to adjust the parameters of the initial video anomaly detection model based on the contrastive learning loss and the reconstruction loss to obtain the target video detection model. In one embodiment, the adjustment module 560 can be used to perform the operation S260 described above, which will not be repeated here.
[0123] According to embodiments of the present invention, any multiple modules among the input extraction module 510, input reconstruction module 520, negative sample generation module 530, positive sample enhancement module 540, contrastive learning loss calculation module 550, and adjustment module 560 may be combined into a single module, or any one of these modules may be split into multiple modules. Alternatively, at least part of the functionality of one or more of these modules may be combined with at least part of the functionality of other modules and implemented in a single module. According to embodiments of the present invention, at least one of the input extraction module 510, input reconstruction module 520, negative sample generation module 530, positive sample enhancement module 540, contrastive learning loss calculation module 550, and adjustment module 560 may be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application-specific integrated circuit (ASIC), or may be implemented in hardware or firmware through any other reasonable means of circuit integration or packaging, or implemented in any one of software, hardware, and firmware, or any suitable combination of any of these. Alternatively, at least one of the input extraction module 510, the input reconstruction module 520, the negative sample generation module 530, the positive sample enhancement module 540, the contrastive learning loss calculation module 550 and the adjustment module 560 can be at least partially implemented as a computer program module, which can perform the corresponding function when it is executed.
[0124] Based on the above video anomaly detection method, the present invention also provides a video anomaly detection device. Figure 6 The device is described in detail.
[0125] Figure 6 The figure shows a structural block diagram of a video anomaly detection device according to an embodiment of the present invention.
[0126] like Figure 6 As shown, the video anomaly detection device 600 of this embodiment includes an acquisition module 610 and an input module 620.
[0127] The acquisition module 610 is used to acquire the target video. In one embodiment, the acquisition module 610 can be used to perform the operation S310 described above, which will not be repeated here.
[0128] Input module 620 is used to input the target video into the target video anomaly detection model to obtain a detection result. The detection result indicates whether an anomaly exists in the target video. The target video anomaly detection model is trained using the aforementioned training device for the video anomaly detection model based on contrastive learning. In one embodiment, input module 620 can be used to perform operation S320 described above, and will not be further described here.
[0129] According to embodiments of the present invention, any multiple modules in the acquisition module 610 and the input module 620 may be combined into a single module, or any one of them may be split into multiple modules. Alternatively, at least part of the functionality of one or more of these modules may be combined with at least part of the functionality of other modules and implemented in a single module. According to embodiments of the present invention, at least one of the acquisition module 610 and the input module 620 may be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on a chip, a system on a substrate, a system on a package, an application-specific integrated circuit (ASIC), or may be implemented in hardware or firmware through any other reasonable means of circuit integration or packaging, or may be implemented in any one of the three implementation methods of software, hardware, and firmware, or any appropriate combination of these. Alternatively, at least one of the acquisition module 610 and the input module 620 may be at least partially implemented as a computer program module that, when executed, performs the corresponding functionality.
[0130] Figure 7 A block diagram of an electronic device suitable for implementing a training method for a video anomaly detection model based on contrastive learning according to an embodiment of the present invention is shown.
[0131] like Figure 7 As shown, an electronic device 700 according to an embodiment of the present invention includes a processor 701, which can perform various appropriate actions and processes based on programs stored in a read-only memory (ROM 702) or programs loaded from a storage unit 708 into a random access memory (RAM 703). Processor 701 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or related chipsets, and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)). Processor 701 may also include onboard memory for caching purposes. Processor 701 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present invention.
[0132] Various programs and data required for the operation of the electronic device 700 are stored in the RAM 703. The processor 701, ROM 702, and RAM 703 are connected to each other via a bus 704. The processor 701 performs various operations according to the method flow of the embodiment of the present invention by executing the programs in the ROM 702 and / or RAM 703. It should be noted that the programs may also be stored in one or more memories other than the ROM 702 and RAM 703. The processor 701 may also perform various operations according to the method flow of the embodiment of the present invention by executing the programs stored in the one or more memories.
[0133] According to an embodiment of the present invention, electronic device 700 may further include an input / output (I / O) interface 705, which is also connected to bus 704. Electronic device 700 may also include one or more of the following components connected to I / O interface 705: an input section 706 including a keyboard, mouse, etc.; an output section 707 including devices such as a cathode ray tube (CRT), liquid crystal display (LCD), and speakers; a storage section 708 including a hard disk; and a communication section 709 including a network interface card such as a LAN card or modem. Communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to I / O interface 705 as needed. Removable media 711, such as a magnetic disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed in drive 710 as needed, so that computer programs read from the removable media can be installed into storage section 708 as needed.
[0134] The present invention also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments, or may exist independently and not incorporated into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the method according to the embodiments of the present invention.
[0135] According to an embodiment of the present invention, a computer-readable storage medium may be a non-volatile computer-readable storage medium, and may include, for example, but not limited to: a portable computer disk, a hard disk, a random access memory (RAM 703), a read-only memory (ROM 702), an erasable programmable read-only memory (EPROM or flash memory), a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In the present invention, a computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to an embodiment of the present invention, a computer-readable storage medium may include the ROM 702 and / or RAM 703 described above, and / or one or more memories other than ROM 702 and RAM 703.
[0136] Embodiments of the present invention also include a computer program product comprising a computer program containing program code for executing the method shown in the flowchart. When the computer program product is executed in a computer system, the program code is configured to cause the computer system to implement the method for training a video anomaly detection model based on contrastive learning provided in an embodiment of the present invention.
[0137] The computer program executes the above functions defined in the system / device of the embodiment of the present invention when the computer program is executed by the processor 701. According to the embodiment of the present invention, the system, device, module, unit, etc. described above can be implemented by a computer program module.
[0138] In one embodiment, the computer program may be stored on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may be transmitted and distributed in the form of a signal on a network medium, downloaded and installed via the communication portion 709, and / or installed from a removable medium 711. The program code contained in the computer program may be transmitted using any appropriate network medium, including but not limited to wireless, wired, or any suitable combination thereof.
[0139] In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 709 and / or installed from the removable medium 711. When the computer program is executed by the processor 701, the above-described functions defined in the system of the embodiment of the present invention are performed. According to the embodiment of the present invention, the systems, devices, means, modules, units, etc. described above can be implemented by computer program modules.
[0140] According to an embodiment of the present invention, the program code for executing the computer program provided by the embodiment of the present invention can be written in any combination of one or more programming languages. Specifically, these computer programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, languages such as Java, C++, Python, "C" or similar programming languages. The program code can be executed entirely on the user computing device, partially on the user device, partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computing device (for example, using an Internet service provider to connect via the Internet).
[0141] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the above-mentioned module, program segment, or a part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in an order different from that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flowchart, and the combination of boxes in the block diagram or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0142] It will be understood by those skilled in the art that the features described in the various embodiments of the present invention may be combined and / or coupled in various ways, even if such combinations or couplings are not explicitly described in the present invention. In particular, the features described in the various embodiments of the present invention may be combined and / or coupled in various ways without departing from the spirit and teachings of the present invention. All such combinations and / or couplings fall within the scope of the present invention.
[0143] The above describes embodiments of the present invention. However, these embodiments are for illustrative purposes only and are not intended to limit the scope of the present invention. Although each embodiment has been described separately above, this does not mean that the measures in each embodiment cannot be advantageously used in combination. Without departing from the scope of the present invention, those skilled in the art may make various substitutions and modifications, which should all fall within the scope of the present invention.
Claims
1. A training method for a video anomaly detection model based on contrastive learning, characterized in that: The method comprises: Input the sample video into the feature extraction module of the initial video anomaly detection model to obtain the sample video features; Inputting the sample video features into a reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video, and obtaining a reconstruction loss based on the sample video and the reconstructed sample video based on a reconstruction loss function; generating a negative sample video feature group based on the sample video features and the reconstruction loss; Performing data enhancement processing on the sample video features to obtain a positive sample video feature group; Based on a contrastive learning loss function, obtaining a contrastive learning loss according to the negative sample video feature group and the positive sample video feature group; Adjusting parameters of the initial video anomaly detection model based on the contrastive learning loss and the reconstruction loss to obtain a target video detection model; The contrastive learning loss function is based on the negative sample video feature group and the positive sample video feature group, and is obtained by: Inputting the negative sample video feature group and the positive sample video feature group into the memory module of the initial video anomaly detection model respectively, obtaining negative sample similarity and positive sample similarity, wherein the negative sample similarity represents the degree of similarity between the negative sample video feature group and the memory vector feature group stored in the memory module, and the positive sample similarity represents the degree of similarity between the positive sample video feature group and the memory vector feature group; Based on the contrastive learning loss function, the contrastive learning loss is obtained according to the negative sample similarity and the positive sample similarity.
2. The method according to claim 1, characterized in that The generating of a negative sample video feature group based on the sample video feature and the reconstruction loss includes: Calculating a gradient vector of the reconstruction loss with respect to the sample video feature based on a gradient extraction function, wherein the gradient vector represents a sensitivity of the sample video feature to a reconstruction error; The negative sample video feature group is generated based on the gradient direction information in the gradient vector and the sample video feature.
3. The method according to claim 1, characterized in that The data enhancement processing is performed on the sample video features to obtain a positive sample video feature group, including: generating a first noise based on a first preset noise range, wherein the first noise represents a change in an environmental factor in the sample video; generating a second noise based on a second preset noise range, wherein the second noise represents a visual interference factor in the sample video; scaling the sample video features according to the first noise to obtain a scaled positive sample video feature group; The scaled positive sample video feature group is randomly offset according to the second noise to obtain the positive sample video feature group.
4. The method according to claim 1, wherein The memory vector feature group includes P memory vector features, the negative sample video feature group includes M negative sample video features, and the positive sample video feature group includes N positive sample video features, where P, M, and N are all positive integers greater than 0, p is a positive integer less than or equal to P and greater than 0, m is a positive integer less than or equal to M and greater than , and n is a positive integer less than or equal to N and greater than 0; The step of inputting the negative sample video feature group and the positive sample video feature group into the memory module of the initial video anomaly detection model to obtain negative sample similarity and positive sample similarity comprises: For the mth negative sample video feature in the negative sample video feature group, using a cosine similarity function, based on the mth negative sample video feature and the pth memory vector feature, obtain a pth sub-negative sample cosine similarity to obtain a sub-negative sample cosine similarity group, wherein the sub-negative sample cosine similarity group includes P sub-negative sample cosine similarities; For the nth positive sample video feature in the positive sample video feature group, using the cosine similarity function, based on the nth positive sample video feature and the pth memory vector feature, obtain a pth sub-positive sample cosine similarity to obtain a sub-positive sample cosine similarity group, wherein the sub-positive sample cosine similarity group includes P sub-positive sample cosine similarities; Using a preset normalization function, normalize the P sub-negative sample cosine similarities respectively and sum them to obtain the mth negative sample similarity, so as to obtain M negative sample similarities; The cosine similarities of the P sub-positive samples are normalized and summed using a preset normalization function to obtain the nth positive sample similarity, thereby obtaining N positive sample similarities.
5. The method according to claim 4, characterized in that The obtaining the contrastive learning loss according to the negative sample similarity and the positive sample similarity includes: Summing the similarity indices of the N positive samples to obtain a positive sample index sum; Summing the M negative sample similarity indices to obtain a negative sample index sum; Obtaining a sample index sum based on the positive sample index sum and the negative sample index sum; The contrastive learning loss is obtained based on the positive sample index sum and the sample index sum.
6. The method according to claim 4, characterized in that The obtaining the contrastive learning loss according to the negative sample similarity and the positive sample similarity includes: For the nth positive sample similarity, the difference between the nth positive sample similarity and the M negative sample similarities is calculated to obtain the nth sub-positive and negative sample similarity difference, thereby obtaining M×N sub-positive and negative sample similarity differences; Based on a preset boundary value, the similarity differences of the M×N sub-positive and negative samples are summed to obtain an M×N sub-contrastive learning loss; The non-negative values in the M×N sub-contrastive learning losses are accumulated to obtain the contrastive learning loss.
7. A video anomaly detection method, characterized in that: The method comprises: Get the target video; The target video is input into a target video anomaly detection model to obtain a detection result, wherein the detection result indicates whether there is an anomaly in the target video, and the target video anomaly detection model is trained using the method described in any one of claims 1 to 6.
8. A training device for a video anomaly detection model based on contrastive learning, characterized in that: The device comprises: An input extraction module is used to input the sample video into the feature extraction module of the initial video anomaly detection model to obtain the sample video features; An input reconstruction module is used to input the sample video features into the reconstruction module of the initial video anomaly detection model to obtain a reconstructed sample video, and based on a reconstruction loss function, obtain a reconstruction loss according to the sample video and the reconstructed sample video; A negative sample generation module, configured to generate a negative sample video feature group based on the sample video feature and the reconstruction loss; A positive sample enhancement module is used to perform data enhancement processing on the sample video features to obtain a positive sample video feature group; A contrastive learning loss calculation module, configured to obtain a contrastive learning loss based on the negative sample video feature group and the positive sample video feature group based on a contrastive learning loss function; An adjustment module, configured to adjust parameters of the initial video anomaly detection model based on the contrastive learning loss and the reconstruction loss to obtain a target video detection model; The contrastive learning loss calculation module includes: A similarity calculation submodule, configured to input the negative sample video feature group and the positive sample video feature group into the memory module of the initial video anomaly detection model, respectively, to obtain negative sample similarity and positive sample similarity, wherein the negative sample similarity represents the degree of similarity between the negative sample video feature group and the memory vector feature group stored in the memory module, and the positive sample similarity represents the degree of similarity between the positive sample video feature group and the memory vector feature group; The contrastive learning loss calculation submodule is used to obtain the contrastive learning loss according to the negative sample similarity and the positive sample similarity based on the contrastive learning loss function.
9. A video anomaly detection device, characterized in that: The device comprises: Acquisition module, used to acquire target video; An input module is used to input the target video into a target video anomaly detection model to obtain a detection result, wherein the detection result indicates whether there is an anomaly in the target video, and the target video anomaly detection model is trained using the method described in any one of claims 1 to 6.
Citation Information
Patent Citations
Data anomaly detection method and device, equipment and storage medium
CN117009903A