Image Classification Method and Device Based on Multithreaded Nearest Neighbor Node Algorithm
By adopting multi-threaded processing technology in the KNN algorithm, grouping and parallel computing feature points is solved, and the existing KNN algorithm is low in computing efficiency in big data processing is achieved, achieving more efficient information classification and machine learning efficiency.
Patent Information
- Application Number
- CN202210517470.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-12
- Publication Date
- 2025-06-20
- Estimated Expiration
- 2042-05-12
AI Technical Summary
When existing KNN algorithms process data with high computational volume, their computational efficiency is low, which affects the efficiency of information classification and thus affects the efficiency of machine learning.
The nearest node algorithm based on multithreading is adopted to improve computing efficiency by obtaining the target image set, generating feature points, and grouping them for thread allocation processing.
The calculation efficiency when processing data with high computational volume is improved, thereby improving the efficiency of information classification and thus improving the efficiency of machine learning.
Smart Images

Figure CN114998608B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present disclosure generally relate to the field of image processing technology, and more particularly, to an image classification method and apparatus based on a multi-threaded nearest neighbor node algorithm. Background Art
[0002] With the development of artificial intelligence technology, information processing technology has also been increasingly widely used. In the process of information processing, information classification is often involved. In existing calculations, the KNN (K-Nearest Neighbor) nearest neighbor node algorithm is usually used to classify information.
[0003] The K-nearest neighbor method (KNN, K-Nearest Neighbor) is a commonly used machine learning algorithm. The idea of this method is very simple and intuitive: if most of the K most similar (i.e., the nearest in the feature space) samples of a sample in the feature space belong to a certain category, then this sample also belongs to this category. This method only determines the category of the sample to be classified based on the categories of one or several nearest samples in the classification decision. The KNN algorithm is more suitable for automatic classification of class domains with a relatively large sample size, and it is easier to misclassify those class domains with a relatively small sample size.
[0004] However, the biggest problem with this method is the large amount of calculation, because for each text to be classified, it is necessary to calculate its distance to all known samples in order to find its K nearest points. In common fitting algorithms, the sample features can be represented by formulas, thus greatly reducing the amount of calculation; while KNN needs to calculate the entire domain every time, so as the number of samples increases, the amount of calculation will become larger and larger.
[0005] The existing KNN algorithm has low computational efficiency when processing data with a large amount of calculation, thus affecting the efficiency of information classification, and directly affecting the efficiency of machine learning. Summary of the Invention
[0006] According to embodiments of the present disclosure, there is provided an image classification method and apparatus based on a multi-threaded nearest neighbor node algorithm to improve the computational efficiency when processing data with a large amount of calculation, thereby improving the efficiency of information classification, and further improving the efficiency of machine learning.
[0007] In a first aspect of the present disclosure, there is provided an image classification method based on a multi-threaded nearest neighbor node algorithm, including:
[0008] Obtain a target image set, where the target image set includes multiple images to be classified;
[0009] Process the images in the target image set to generate feature points in the corresponding multi-dimensional space. Each feature point corresponds to a feature vector, and store the feature points in memory in the form of a linked list;
[0010] Group the feature points and divide the feature points into multiple data groups according to a preset rule;
[0011] Perform thread allocation processing on the data groups to determine the threads corresponding to the data groups, and calculate the result data corresponding to each thread;
[0012] Sort the result data, and select a preset number of the smallest result data from the sorting result as a category in the target image set.
[0013] In some embodiments, it further includes:
[0014] While performing the thread allocation processing on the data groups to determine the threads corresponding to the data groups and calculating the result data corresponding to each thread, continuously receive the images to be classified newly added to the target image set, process the newly received images to be classified into corresponding feature points, and store the generated feature points in memory in the form of a linked list.
[0015] In some embodiments, it includes:
[0016] While performing the thread allocation processing on the data groups to determine the threads corresponding to the data groups and calculating the result data corresponding to each thread, receive the images to be classified newly added to the target image set at a preset time interval, process the newly received images to be classified into corresponding feature points, and store the generated feature points in memory in the form of a linked list.
[0017] In some embodiments, it further includes:
[0018] Update the divided data groups at a preset time interval, process the images to be classified newly added to the target image set into corresponding feature points, and divide the generated feature points into the corresponding data groups.
[0019] In some embodiments, the grouping of the feature points and dividing the feature points into multiple data groups according to a preset rule includes:
[0020] Group the feature points and divide the feature points into multiple data groups with a data length of n*k, where n is a positive integer and k is the number of expected output values of the nearest neighbor node algorithm.
[0021] In some embodiments, the process of allocating threads to the data group, determining the threads corresponding to the data group, and calculating the result data corresponding to each thread includes:
[0022] Perform thread allocation processing on the data group, allocate it to different threads one by one in the order of the data group, and use the threads for parallel calculation to obtain the result data corresponding to each thread.
[0023] In some embodiments, the process of sorting the result data and selecting the smallest preset number of result data from the sorting result as a category in the target image set includes:
[0024] Input the result data into a B-tree, and select the smallest k result data from the B-tree as a category in the target image set.
[0025] In a second aspect of the present disclosure, there is provided an image classification device based on a multi-threaded nearest neighbor node algorithm, including:
[0026] An image acquisition module for acquiring a target image set, where the target image set includes multiple images to be classified;
[0027] An image processing module for processing the images in the target image set to generate feature points in a corresponding multi-dimensional space, each feature point corresponding to a feature vector, and storing the feature points in the memory in the form of a linked list;
[0028] A feature point grouping module for grouping the feature points and dividing the feature points into multiple data groups according to a preset rule;
[0029] A thread allocation module for performing thread allocation processing on the data group, determining the threads corresponding to the data group, and calculating the result data corresponding to each thread;
[0030] A category generation module for sorting the result data and selecting the smallest preset number of result data from the sorting result as a category in the target image set.
[0031] In a third aspect of the present disclosure, there is provided an electronic device including a memory and a processor, where a computer program is stored on the memory, and when the processor executes the program, the method described above is implemented.
[0032] In a fourth aspect of the present disclosure, there is provided a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processor, the method described above is implemented.
[0033] Through the image classification method based on the multi-threaded nearest neighbor node algorithm of the present disclosure, the computing efficiency when processing data with large computing amounts can be improved, thereby improving the efficiency of machine learning.
[0034] The content described in the section of the invention content is not intended to limit the key or important features of the embodiments of the present disclosure, nor is it used to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Combined with the accompanying drawings and referring to the following detailed description, the above and other features, advantages and aspects of the embodiments of the present disclosure will become more obvious. In the drawings, the same or similar reference numerals represent the same or similar elements, where:
[0036] Figure 1 The flowchart of the image classification method based on the multi-threaded nearest neighbor node algorithm according to Embodiment 1 of the present disclosure is shown;
[0037] Figure 2 The structural schematic diagram of the image classification device based on the multi-threaded nearest neighbor node algorithm according to Embodiment 2 of the present disclosure is shown;
[0038] Figure 3 The structural schematic diagram of the image classification device based on the multi-threaded nearest neighbor node algorithm according to Embodiment 3 of the present disclosure is shown. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0039] To make the objectives, technical solutions and advantages of the embodiments of the present disclosure clearer, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present disclosure. Obviously, the described embodiments are some, but not all, of the embodiments of the present disclosure. All other embodiments obtained by those of ordinary skill in the art based on the embodiments in the present disclosure without creative efforts shall fall within the scope of protection of the present disclosure.
[0040] In addition, the term "and / or" in this article is merely a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. In addition, the character " / " in this article generally represents an "or" relationship between the associated objects before and after.
[0041] The image classification method based on the multi-threaded nearest neighbor node algorithm of the embodiments of the present disclosure includes:
[0042] Sort the result data, and select a preset number of the smallest result data from the sorting result as one category in the target image set.
[0043] , it can be applied to image classification in the image processing process. Specifically, the image to be classified is transformed into feature points in a multi-dimensional space, and then the optimized KNN algorithm, namely the multi-threaded nearest neighbor node algorithm, is used to classify the feature points to generate result data. The result data is sorted, and a preset number of the smallest result data are selected from the sorted results as a category in the target image set. Thereby, the calculation efficiency when processing data with a large amount of calculation is improved, and further the efficiency of machine learning is improved.
[0044] For the conventional KNN algorithm, it is necessary to calculate the average distance from all other nodes to the target node and determine whether this average distance is the minimum value. If it is not the minimum value, the target node is replaced and the calculation is repeated. The final target node is determined through multiple iterations, so that the average distance value from other nodes to the final target node is the smallest. For the conventional KNN algorithm, every time the target node is replaced, it is necessary to calculate the distance from other nodes to the target node, and then calculate the average distance. In this way, when the data volume is too large, for example, reaching more than one million levels, the required time is too long, which affects the algorithm efficiency and further affects the improvement of the machine learning efficiency.
[0045] Due to the above technical problems existing in the conventional KNN algorithm in the prior art when classifying images, the embodiments of the present disclosure provide an image classification method based on the multi-threaded nearest neighbor node algorithm. Specifically, as Figure 1 shown, it is a flowchart of the image classification method based on the multi-threaded nearest neighbor node algorithm in Embodiment 1 of the present disclosure. In this embodiment, the image classification method based on the multi-threaded nearest neighbor node algorithm may include the following steps:
[0046] S101: Obtain a target image set, where the target image set includes multiple images to be classified.
[0047] The method of the embodiments of the present disclosure optimizes the conventional KNN algorithm to improve the calculation efficiency of the KNN algorithm, and uses the optimized KNN algorithm to classify images.
[0048] First, it is necessary to obtain the images to be classified. The images to be processed in the embodiments of the present disclosure may be intermediate images generated by an upstream program, such as the images to be classified generated after cropping the images, or image data received from other programs, such as the images to be classified directly sent by other programs.
[0049] S102: Process the images in the target image set to generate corresponding feature points in a multi-dimensional space. Each feature point corresponds to a feature vector, and the feature points are stored in memory in the form of a linked list.
[0050] In this embodiment, after obtaining the target image set, the images in the target image set are processed to generate corresponding feature points. The feature points are points in a multi-dimensional space, and each feature point corresponds to a feature vector. Taking a face image as an example, the dimensions of the feature points can be predefined, such as the distance between eyes, the distance between eyebrows, the maximum width of the lips, the maximum height of the lips, etc. In this way, after processing the images in the target image set to generate the corresponding feature points, the feature points are stored in memory in the form of a linked list, that is, the feature points are represented in the memory usage state, and the data structure is a linked list and stored in memory. In this way, the feature points can be dynamically increased as the usage time increases, and can be periodically persisted and written to memory.
[0051] S103: Group the feature points and divide the feature points into multiple data groups according to a preset rule.
[0052] In an embodiment of the present disclosure, after storing the feature points in memory in the form of a linked list, the feature points can be further grouped. The feature points are divided into multiple data groups according to a preset rule. For example, the feature points can be divided into multiple data groups with a data length of n*k, where n is a positive integer and k is the number of expected output values of the nearest neighbor node algorithm.
[0053] S103: Perform thread allocation processing on the data groups, determine the threads corresponding to the data groups, and calculate the result data corresponding to each thread.
[0054] After dividing the node data into multiple data groups according to a preset rule, perform thread allocation processing on the divided data groups, determine the threads corresponding to the data groups, and calculate the result data corresponding to each thread. Specifically, the data groups can be sequentially allocated to different threads one by one, and parallel calculations are performed using the threads to obtain the result data corresponding to each thread. Of course, in some other embodiments, the data groups can also be allocated in other orders, which will not be listed one by one here.
[0055] In this embodiment, assuming that P processors are used for calculation, the already grouped node data is randomly allocated threads for processing, and the grouped content is allocated in order. For example, processor No. 1 processes group1, group4, group7,...; processor No. 2 processes group2, group5, group8,...; processor No. 3 processes group3, group6, group9,... and so on. After evenly distributing the grouped data to each group, parallel calculations are performed using threads. For each calculation, it is not necessary to return all the calculated values. Only the calculation results need to be sorted, and then the first k are selected and returned. k is the number of expected output values of the nearest neighbor node algorithm.
[0056] S104: Sort the result data, and select the smallest preset number of result data from the sorting result as the final result.
[0057] S105: After each thread outputs the corresponding result data, sort the result data, and select the smallest preset number of result data from the sorting result as a category in the target image set.
[0058] For example, the result data can be input into a B-tree, and the smallest k result data can be selected from the B-tree as a category in the target image set.
[0059] The image classification method based on the multi-threaded nearest neighbor node algorithm of the present disclosure can improve the computing efficiency when processing data with large computational amounts, thereby improving the efficiency of machine learning.
[0060] In addition, as an optional embodiment of the present disclosure, in the above embodiment, it further includes: while performing thread allocation processing on the data group, determining the threads corresponding to the data group, and calculating the result data corresponding to each thread, continuously receiving the images to be classified newly added to the target image set, processing the newly received images to be classified into corresponding feature points, and storing the generated feature points in the memory in the form of a linked list. Or, it further includes: while performing thread allocation processing on the data group, determining the threads corresponding to the data group, and calculating the result data corresponding to each thread, receiving the images to be classified newly added to the target image set at a preset time interval, processing the newly received images to be classified into corresponding feature points, and storing the generated feature points in the memory in the form of a linked list.
[0061] After receiving the images to be classified newly added to the target image set, the divided data group can also be updated at a preset time interval, processing the newly added images to be classified into corresponding feature points, and dividing the generated feature points into the corresponding data groups.
[0062] It should be noted that for the foregoing method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the present disclosure is not limited by the described action sequence, because according to the present disclosure, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all optional embodiments, and the actions and modules involved are not necessarily essential to the present disclosure.
[0063] The above is the introduction of the method embodiments. The following further illustrates the solution of the present disclosure through device embodiments.
[0064] As Figure 2 shown, it is a schematic structural diagram of an image classification device based on a multi-threaded nearest neighbor node algorithm according to the second embodiment of the present disclosure. The image classification device based on the multi-threaded nearest neighbor node algorithm of this embodiment includes:
[0065] An image acquisition module 201, configured to acquire a target image set, where the target image set includes multiple images to be classified.
[0066] An image processing module 202, configured to process the images in the target image set to generate feature points in a corresponding multi-dimensional space, each feature point corresponding to a feature vector, and store the feature points in a linked list form in memory.
[0067] A feature point grouping module 203, configured to group the feature points and divide the feature points into multiple data groups according to a preset rule.
[0068] A thread allocation module 204, configured to perform thread allocation processing on the data groups, determine the threads corresponding to the data groups, and calculate the result data corresponding to each thread.
[0069] A category generation module 205, configured to sort the result data, and select a preset number of the smallest result data from the sorting result as a category in the target image set.
[0070] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working processes of the described modules can refer to the corresponding processes in the foregoing method embodiments, and will not be elaborated herein.
[0071] Figure 3 FIG. shows a schematic block diagram of an electronic device 300 that can be used to implement the embodiments of the present disclosure. As shown in the figure, the device 300 includes a central processing unit (CPU) 301, which can execute various appropriate actions and processes according to computer program instructions stored in a read-only memory (ROM) 302 or computer program instructions loaded from a storage unit 308 into a random access memory (RAM) 303. In the RAM 303, various programs and data required for the operation of the device 300 can also be stored. The CPU 301, ROM 302, and RAM 303 are connected to each other through a bus 304. An input / output (I / O) interface 305 is also connected to the bus 304.
[0072] Multiple components in device 300 are connected to I / O interface 305, including: input unit 306, such as a keyboard, mouse, etc.; output unit 307, such as various types of displays, speakers, etc.; storage unit 308, such as a disk, optical disc, etc.; and communication unit 309, such as a network card, modem, wireless communication transceiver, etc. Communication unit 309 allows device 300 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.
[0073] Processing unit 301 executes the various methods and processes described above, which are tangibly contained in a machine-readable medium, such as storage unit 308. In some embodiments, part or all of the computer program can be loaded and / or installed onto device 300 via ROM 302 and / or communication unit 309. When the computer program is loaded into RAM 703 and executed by CPU 301, one or more steps of the methods described above can be executed. Alternatively, in other embodiments, CPU 301 can be configured to execute the above methods in any other suitable manner (e.g., by means of firmware).
[0074] The functions described above herein can be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that can be used include: Field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), Systems on Chip (SOCs), Complex Programmable Logic Devices (CPLDs), and so on.
[0075] The program code for implementing the methods of the present disclosure can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing devices, such that when the program codes are executed by the processor or controller, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The program codes can be executed entirely on the machine, partially on the machine, as an independent software package partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0076] In the context of the present disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in connection with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0077] Moreover, although the operations are depicted in a particular order, this should be understood as requiring that such operations be performed in the particular order shown or in a sequential order, or that all illustrated operations be performed to achieve the desired result. In certain circumstances, multitasking and parallel processing may be advantageous. Likewise, although several specific implementation details are included in the foregoing discussion, these should not be construed as limitations on the scope of the present disclosure. Certain features described in the context of separate embodiments can also be implemented in combination in a single implementation. Conversely, various features described in the context of a single implementation can also be implemented separately or in any suitable sub-combination in multiple implementations.
[0078] Although the subject matter has been described in language specific to structural features and / or methodological acts, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are merely example forms of implementing the claims.
Claims
1. An image classification method based on a multi-threaded nearest neighbor node algorithm, characterized in that, including: obtaining a target image set, where the target image set includes multiple images to be classified; processing the images in the target image set to generate feature points in a corresponding multi-dimensional space, each feature point corresponding to a feature vector, and storing the feature points in memory in the form of a linked list; grouping the feature points, dividing the feature points into multiple data groups with a data length of n*k, where n is a positive integer and k is the number of expected output values of the nearest neighbor node algorithm; performing thread allocation processing on the data groups, determining the threads corresponding to the data groups, and calculating the result data corresponding to each thread; at the same time, continuously receiving or receiving at preset time intervals the images to be classified newly added to the target image set, processing the newly received images to be classified into corresponding feature points, and storing the generated feature points in memory in the form of a linked list; updating the divided data groups at preset time intervals, and dividing the generated feature points into the corresponding data groups; sorting the result data, and selecting a preset number of the smallest result data from the sorting result as a category in the target image set.
2. The image classification method according to claim 1, characterized in that, The performing thread allocation processing on the data groups, determining the threads corresponding to the data groups, and calculating the result data corresponding to each thread includes: performing thread allocation processing on the data groups, sequentially allocating the data groups to different threads one by one, and performing parallel calculation using the threads to obtain the result data corresponding to each thread.
3. The image classification method according to claim 2, characterized in that, The sorting the result data, and selecting the smallest preset number of result data from the sorting result as a category in the target image set includes: inputting the result data into a B-tree, and selecting the smallest k result data from the B-tree as a category in the target image set.
4. An image classification device based on a multi-threaded nearest neighbor node algorithm, characterized in that, including: an image acquisition module, configured to obtain a target image set, where the target image set includes multiple images to be classified; an image processing module, configured to process the images in the target image set to generate feature points in a corresponding multi-dimensional space, each feature point corresponding to a feature vector, and storing the feature points in memory in the form of a linked list; a feature point grouping module, configured to group the feature points, and divide the feature points into multiple data groups with a data length of n*k, where n is a positive integer and k is the number of expected output values of the nearest neighbor node algorithm; a thread allocation module, configured to perform thread allocation processing on the data groups, determine the threads corresponding to the data groups, and calculate the result data corresponding to each thread; at the same time, continuously receiving or receiving at preset time intervals the images to be classified newly added to the target image set, processing the newly received images to be classified into corresponding feature points, and storing the generated feature points in memory in the form of a linked list; updating the divided data groups at preset time intervals, and dividing the generated feature points into the corresponding data groups; a category generation module, configured to sort the result data, and select a preset number of the smallest result data from the sorting result as a category in the target image set.
5. An electronic device, comprising a memory and a processor, wherein a computer program is stored on the memory, characterized in that, When the processor executes the program, the method described in any one of claims 1 to 3 is implemented.
6. A computer-readable storage medium, on which a computer program is stored, characterized in that, When the program is executed by the processor, the method described in any one of claims 1 to 3 is implemented.