Bag-of-words: the original bag-of-words is also called "bag of words". In information retrieval, the bag-of-words model assumes that for a text, ignore its word order and syntax, and regard it as a word set, or a combination of words. The appearance of each word in the text is independent, it does not depend on whether other words appear.
Bow simple example for text
John likes to watch movies. Mary likes too.
John also likes to watch football games.
Based on the words in the above two sentences, we can build a dictionary
{"John": 1, "likes": 2, "to": 3, "watch": 4, "Movies": 5, "also": 6, "football": 7, "games": 8, "Mary": 9, "too": 10}
This dictionary contains 10 words and each word has a unique index. Based on this dictionary, We Can reexpress the above two sentences as the following two vectors:
[1, 2, 1, 1, 1, 0, 0, 0, 1, 1]
[1, 1, 1, 1, 0, 1, 1, 1, 0, 0]
These two vectors contain 10 elements. The I element indicates the number of times that the I word appears in the sentence in the dictionary. Therefore, the Bow model can be considered as a statistical histogram. In text retrieval and processing applications, you can use this model to conveniently calculate word frequency.
Bag-of-words is used in image processing background. Introduction to sift, scale-invariant feature transform (SIFT) is a description of image processing. This description has scale immutability and can detect key points in the image. Is a partial description.
The advantages of Sift: SIFT features not only have scale immutability, but can still achieve good detection even if the rotation angle, image brightness or shooting angle are changed. Therefore, when applied to image recognition, the effects of image scale, angle, and brightness can be suppressed.
Image Feature Extraction images can be analogous to documents. Words in images can be defined as feature vectors of an image block. Then, the Bow model of the image is "the histogram obtained from the feature vectors of all image blocks ".
1. Feature Extraction assumes that there are n images. The I images can be composed of N (I) image patches, that is, they can be expressed by N (I) feature vectors. Then sum (N (I) feature vectors (words) are obtained in total ).
Feature vectors can be obtained using the sift method. The dimension of each patch feature vector is 128.
2. Generate a dictionary/code book. Assume that the dictionary size is 100, and there are 100 words. Use the K-means algorithm to cluster all patches. k = 100. When k-means converges, we obtain the final center of each cluster, the 100 centers (with 128 dimensions) are the 100 words in the dictionary. The dictionary has been constructed.
3. generate a histogram Based on the code book for each image. calculate each word of the Image Based on the nearest neighbor and obtain the bow representation of the image corresponding to the code book.
After the bag-of-words model is built, you can perform classification, prediction, and other training.