2D HMM for Image Processing
local graph
Chapter 8 — 2D HMM for Image Processing
Section titled “Chapter 8 — 2D HMM for Image Processing”In this chapter, we will introduce the application of HMM in the case of image segmentation. For image segmentation, we usually split up the given image into multiple blocks of equal size and then perform an estimation for each of these blocks. However, these algorithms usually ignore the contextual information from the neighboring blocks. To deal with that issue, 2D HMMs were introduced, which consider feature vectors to be dependent through an underlying 2D Markovian mesh. In this chapter, we will discuss how these 2D HMMs work and will derive parameter estimation algorithms for them. In this chapter, we will discuss the following topics:
- Pseudo 2D HMMs
- Introduction to 2D HMMs
- Parameter learning in 2D HMMs
- Applications
Let’s recap how 1D HMMs work, which we discussed in the previous chapters of this book. We have seen that HMM is a just a process over Markov chains. At any point in time, an HMM is in one of the possible states, and the next state that the model will transition to depends on the current state and the transition probability of the model.
Suppose that there are M = {1, 2,…, M} possible states for HMM, and the transition probability of going from some state i to state j is given by a i,j. For such a model, if at time t-1 the model is at state i, then at time t it would be in state j with a probability of a i,j. This probability is known as the transition probability. Also, we have defined the observed variable in the model, which only depends on the current state of our hidden variable. We can define the observed variable at time t as u t, so let’s say the emission distribution for the state i for the variable u t is given by b i (u t).
We also need to define the initial probability, π i, as the probability of being in state i at time t = 1. With all these given values, we can determine the likelihood of observing any given sequence,, as follows:
In most situations, we assume the states to be a Gaussian mixture model; in which case, the previous equation can be generalized further, as follows:
Here, u i is the mean, ∑ i is the covariance matrix, and k is the dimensionality of the observed variable u t.
Now we have our model defined, we can move on to the estimation method. Estimation is usually performed using the Baum-Welch algorithm that we saw in Chapter 4, Parameter Inference using Maximum Likelihood, which performs a maximum-likelihood estimation. Let L i (t) be the conditional distribution of being in state i at time t, given the observations, and H i,j (t) be the conditional probability of transitioning to state j from state i at time t + 1, again, given the observations. Then, we can re-estimate the mean, covariance, and transition probability as follows:
To compute the values of L i (t) and H i,j (t), we use the forward-backward algorithm. The forward algorithm gives us the probability, α i (t), of observing the first t outcomes,, and being in state i at time t.
This probability can be evaluated using the following set of equations:
We also define the backward probability, β i (t), as the conditional probability of having the observations {u r }r=t + 1,…,T, given that the model is in state i at time t. The conditional probability β i (t) can be computed as follows:
With these values to hand, we can compute that L i (t) and H i,j (t) can be solved as follows:
We can approximate this algorithm by assuming each observation to have resulted from the most likely hidden state. This allows us to simplify the Baum-Welch algorithm; this is commonly also known as the Viterbi training algorithm. Given the observed states, and assuming the state sequence to be with the maximum conditional probability, this can be given as follows:
Here, can be computed as follows:
With these values, we can then compute the model parameters as follows:
Here, I is the indicator function, which returns 1 if the function’s argument is true; otherwise, it returns 0.
In this section, we have quickly reviewed the basic concepts of 1D HMMs when the states are parameterized using Gaussian mixture models, so we can now move on to 2D HMMs.
A lot of work has been done regarding 2D HMMs, but the most recent work and well-received work has been done by Jia Li, Amir Najmi, and Robert Gray in their paper, *Image Classification by a Two Dimensional Hidden Markov Model.*This section has been written based on their work. We will start by giving the general algorithm they have introduced, and then, in further subsections, we will see how the algorithm works.
The algorithm for image classification is as follows:
- Training:
- Divide the training images into non-overlapping blocks with equal size and extract a feature vector for each block
- Select the number of states for the 2D HMM
- Estimate the model parameters based on the feature vectors and the training labels
- Divide the training images into non-overlapping blocks with equal size and extract a feature vector for each block
- Testing:
- Similar to training, generate feature vectors for the testing images
- Search for the set of classes with the maximum a posteriori probability, given the feature vectors, according to the training model
- Similar to training, generate feature vectors for the testing images
In this section, we will quickly go through the assumptions for the 2D HMM model and the derivation of how these assumptions simplify our equations. For a more detailed derivation, please refer to the original paper.
We start by dividing the image into smaller blocks, from which we evaluate the feature vectors, and, using these feature vectors, we classify the image. In the case of a 2D HMM, we make the assumption that the feature vectors are generated by a Markov model with a state change happening once every block. We also define the relationship between the blocks based on which block comes before or after which block. A block at position (i’, j’) is said to come before the block at position (i, j) if i’ or i’ = i and j’ < j. Assuming that there are M = {1, 2,… M} states, the state of any given block at position (i, j) is denoted by S i,j, the feature vector is denoted by u i,j, and the class is denoted by c i,j. Another point to keep in mind is that the order of the blocks has been introduced just to explain the assumptions of the model, and the algorithm doesn’t consider any order of blocks while doing the classification.
The first assumption made by the model is as follows:
This assumption states that knowing the current state is a sufficient statistic for estimating the transition probabilities, which means that u is conditionally independent. Also, according to the assumption, the state transition is a Markov process in two dimensions, and the probability of the system entering any particular state depends on the state of the model in both the horizontal and vertical directions in the previous time and observation instance. We also assume that there is one-to-one mapping from state to class, so that once we know the state, the class can be directly computed.
The second assumption is that the feature vectors are a Gaussian mixture distribution for each state. We know that any M-component Gaussian mixture can be split into M substates with single Gaussian distributions; therefore, for a block with state s and feature vector u, the probability of the distribution is given by the following:
Here, ∑ s is the covariance matrix and μ s is the mean vector of the Gaussian distribution.
We can now use the Markovian assumptions to simplify the evaluation of the probability of the states. The probability of the states for all the blocks in the image is denoted by P{s i,j: (i,j) ∈ N}, where N = {(i,j): 0 ≤ i < w, 0 ≤ j < z}. But before we use the Markovian assumptions to efficiently expand the probability, we need to prove that, given the two previous assumptions, a rotated form of two-dimensional Markovian property holds for the image.
We define a rotated relation of, denoted by, which specifies, if j’ < j, or j’ = j and i’ < i. We need to prove the following:
So, we use the previous definition of and introduce the following new notation:
From the preceding equations, we can also see this:
Now, to simplify notation, let’s introduce and. From these we can do the following derivation:
Expanding the conditional probability, we get the following:
Using the Markovian assumption, we get the following:
And, finally, using the Markovian assumption and the assumption that the feature vector of a block is conditionally independent of other blocks given its state, we have the following:
Where m = s i-1,j, n = s i,j-1, and l = s i,j.
If we replace with, and replace with in the derivation, all the equations will still hold. This gives us the following:
Since the preceding equation implies the original Markovian assumption and its rotated version, we can show the equivalent end of the two assumptions as follows:
Now we can simplify the expansion of P{s i,j: (i,j) ∈ N}, as follows:
Here, w and z are the numbers of rows and columns in the image, respectively, and T i is the sequence of states for blocks on the diagonal i. Next, we need to prove that P(T i |T i-1,…,T 0) = P(T i |T i-1). Assuming T i = {s i,0, s i-1,1,…, s 0,i }, this means T i-1 = {s i-1,0, s i-2,1,…, s 0,i-1 } and hence we have the following:
Therefore, we can conclude:
Using this, we get the following simplified equation:
Since we have the model ready, we now need to estimate the parameters of the model. We need to estimate the mean vectors μ m; the covariance matrices ∑ m; and the transition probabilities a m,n,l, where m,n,l = 1,…, M, and M is the total number of states. We will use the expectation maximization (EM) algorithm.
As we have seen in previous chapters, EM is an iterative algorithm that can learn the maximum likelihood estimates in the case of missing data; that is, when we have unobserved variables in our data. Let’s say that our unobserved variable x is in the sample space x, and the observed variable y is in the sample space y. If we postulate a family of distribution f(x|Φ), with parameters Φ ∈ Ω, then the distribution over y is given as follows:
The EM algorithm will then try to find the value of that would maximize the value of g(y|Φ) given the observed y. The EM iteration Φ (p) → Φ (p+1) is defined as follows:
- E-step: Compute Q (Φ| Φ (p)) where Q(Φ’|Φ) is the expected value of log f(x|Φ’)
- M-step: Choose Φ (p+1) to be a value of Φ ∈ Ω that maximizes Q (Φ| Φ (p))
Let’s now define the terms needed for 2D HMMs:
- The set of observed feature vectors for the entire image is
- The set of states for the image is
- The set of classes for the image is
- The mapping from a state s i,j to its class is C(s i,j), and the set of classes mapped from states s, is denoted by C(s)
Now let’s define the distribution over x for a 2D HMM, as follows:
From this, we can compute the following log f(x|Φ’):
We now know that, given y, x can only have a finite number of values corresponding to states that are consistent with the value of y. Therefore, the distribution over x is given as follows:
Here, α is the normalizing constant and I is the indicator function. Now, for the M-step, we need to set the value of Φ (p+1) to Φ’, which will maximize the following:
Since the previous term has two parts with an addition between them, we can deal with each term separately, since we are trying to maximize the total. Consider the first term:
By defining, the preceding equation can be reduced to this:
This term is concave in; therefore, using the Lagrangian multiplier and taking the derivative we get the following:
Coming back to maximizing the second term of:
Again, to simplify the preceding equation, we define, and the preceding term becomes this:
In this case, our ML estimates for our Gaussian distribution are given by the following:
To summarize the whole derivation, we can write the EM algorithm in the following two steps:
- Given the model estimation Φ (p), the parameters are updated as follows:
Here, the term can be computed as this:
- The transition probability is updated as follows:
And, here, is calculated as follows:
By applying the preceding two equations iteratively, our algorithm will converge to the maximum likelihood estimation of the parameters of the model.
In this chapter we started with a short recap of 1D HMMs which we introduced in the previous chapter. Later we introduced the concepts of 2D HMMs and derived the various assumptions that we make for 2D HMMs to simplify our computations and it can be applied in image processing tasks. We then introduce a generic EM algorithm for learning the parameters in the case of 2D-HMMs.
In the next chapter, we will look at another application of HMMs in the field of reinforcement learning and will introduce MDP.