Deep Demosaicing for Edge Implementation
Ramchalam Kinattinkara Ramakrishnan Shangling Jui Vahid Patrovi Nia







[
    Boris Hasselblatt
    December 30, 2023
=====================














Most digital cameras use sensors coated with a Color Filter Array (CFA) to capture channel components at every pixel location, resulting in a mosaic image that does not contain pixel values in all channels. Current research on reconstructing these missing channels, also known as demosaicing, introduces many artifacts, such as zipper effect and false color. Many deep learning demosaicing techniques  outperform other classical techniques in reducing the impact of artifacts. However, most of these models tend to be over-parametrized. Consequently, edge implementation  of the state-of-the-art deep learning-based demosaicing algorithms on low-end edge devices is a major challenge. We provide an exhaustive search of deep neural network architectures and obtain a Pareto front of Color Peak Signal to Noise Ratio (CPSNR) as the performance criterion versus the number of parameters as the model complexity that outperforms the state-of-the-art. Architectures on the Pareto front can then be used to choose the best architecture for a variety of resource constraints. Simple architecture search methods such as exhaustive search and grid search requires some conditions of the loss function to converge to the optimum. We clarify these conditions in a brief theoretical study.


Copyright Notice: This paper has been accepted in 16th International Conference of Image Analysis and Recognition (ICIAR 2019). Authors have assigned The Springer Verlage all rights under the Springer copyright. The revised version of the article is published in the Lecture Notes in Computer Science Springer series under the same title.







§ INTRODUCTION






Deep Learning is changing lives on a day to day basis. Overparametrization of deep models can have an impact in their deployment for variety of applications. For instance, model size could adversely affect the real-time updates in mobile phone implementation. Moreover, the training and inference time increases for a large model. For instance, ResNet152 takes 1.5 weeks to train on the Image-Net dataset using one Nvidia TitanX GPU <cit.>. In terms of hardware, much of the impact of such models is in the energy consumption. As an example, Dynamic Random Access Memory (DRAM) consumes most of the energy at inference. Therefore,  it is imperative to reduce the model size <cit.>. 



The success of deep learning models has been proven in many high-level vision tasks such as image classification and object detection. However, deep learning solutions are less explored for  low-level vision problems where the edge implementation is an obligation. Previous work shows the effectiveness on other low-level vision problems such as image denoising and image demosaicing <cit.>. Here we explore various architectures for demosaicing, with the aim of finding suitable models for deployment on the edge. It is imperative to make use of models that can be stored on edge devices. 





§ DEMOSAICING
 
Image demosaicing involves interpolating full-resolution images from incomplete color samples produced by an image sensor. Limitations in camera sensor resolution and sensitivity leads to the problem of mosaicing. 
For a digital camera to capture or produce a full color image, it uses sensors to detect all 3 colors (RGB) at every pixel location. One way this is done involves using a beam-splitter to project the image onto three separate sensors, see Figure <ref>a, one for each color (RGB). Color filters are placed in front of each sensor to filter specific wavelengths 
such that three full-channel color images are obtained. This is a costly process that is generally used in scientific-grade microscopes. Most digital cameras, on the other hand, make use of just one sensor coated with a Color Filter Array (CFA). In this system, one color component is captured at every pixel location and the missing channels are reconstructed. The resultant image is often called a mosaic image, derived from the CFA's mosaic pattern. This mosaic image is then subjected to software-based interpolation, resulting in a full-resolution image; a process termed as demosaicing.



Various existing CFA patterns are currently used, with the Bayer CFA being the most commonly used approach for mosaicing. The Bayer CFA measures the green image on quincinx grid, and the red/blue on the rectangular grids (Figure <ref>b). The green channel has higher sampling because the human retina has a higher sensitivity toward the green wavelength. 



Let I^CFA:ℤ^2 →ℤ^3 denote an M× N Bayer CFA image. If we consider the sampling pattern shown in Figure <ref>b, then the mosaic image is as follows:

    I(i,j) = 
      
         R_i,j   for i odd and j even,
    
         B_i,j   for i even and j odd,
    
         G_i,j   Otherwise,



where R_i,j, G_i,j, B_i,j includes values between 0 and 255. To estimate the missing two missing channels for each pixel location using demosaicing, 


    Î(i,j) = 
      
         (R_i,j,B̂_i,j, Ĝ_i,j)    for i odd and j even,
    
         (R̂_i,j,B_i,j, Ĝ_i,j)    for i even and j odd,
    
         (R̂_i,j,B̂_i,j, G_i,j)    Otherwise,

where R̂_i,j,Ĝ_i,j,B̂_i,j are the estimates for the channels at each pixel location.


Demosaicing approaches are divided into various categories:
1. Edge-sensitive methods, 2. Directional interpolation and decision methods, 3. Frequency domain approaches, 4. Wavelet-based methods, 5. Statistical reconstruction techniques, and 6. Deep learning <cit.>. Early studies in bi-linear/bi-cubic <cit.> and spline interpolation make use of single-channel interpolation techniques that treat each channel separately, without using the inter-channel information and correlation. For instance, bi-linear interpolation computes the red value of a non-red pixel as the average of the 2/4 adjacent red pixels; this method is then repeated with the blue and green pixels. Many comparatively advanced algorithms use the results of bi-linear interpolation as a baseline. These algorithms make use of a weighted average of the neighboring pixels to fill the missing channel values.


Recently, neural network approaches like convolutional neural networks (CNN) have been effectively used for joint denoising and demosaicing <cit.>. This method down-samples the mosaiced image using multiple convolutions and computes the residual at lower resolution; this is further concatenated with the input mosaic image. The final output is then calculated using another group of convolutions at full resolution to reconstruct the full resolution image. Studies <cit.> have also used Deep Residual Learning for image demosaicing for Bayer CFA. This neural network uses a two-stage architecture: 1) the first step recovers an intermediate result of the Green and Red/Blue channel separately as a guiding prior to use as input for the second stage, 2) the final demosaiced image is reconstructed. 











The current state-of-the-art model, DMCNN-VD <cit.> architecture (Figure <ref>) consists of a deep network that uses same dimensions for  the input and output layers. They use the Bayer CFA mosaic layer and 20 blocks (CNN, BN, SELU), instead of the 3 layers in the DMCNN model. 







 §.§ Artifacts

Although various approaches to the demosaicing problem have been discussed, most techniques have a propensity to introduce unwanted artifacts into the resulting images. A previous study shows that bi-linear interpolation of mosaic images results in various structural errors <cit.>, such as the two homogeneous areas with 2 gray levels of  low and high intensity (L and H, L<H) represented in Figure <ref>.1. Another artifact introduced is the mismatch in the green and blue/red channel intensity.




 These 2 artifacts are generalized as zipper effect and false color. The zipper effect is an abrupt or sudden change in intensity over the neighboring pixels, generally seen along the edges (Figure <ref>.2a). False colors (Figure <ref>.2b), on the other hand, are pseudo-colors that are not an accurate representation of the original image. This generally occurs due to inconsistencies in color channels of the original mosaic image. Both these effects are categorized as misguidance color artifacts, generated as a result of wrong interpolation direction. Interestingly, these artifacts are observed even when the interpolation direction is appropriate. However, the effects in this case are less evident when compared to misguided color artifacts. Many deep learning demosaicing techniques outperform other classical techniques in reducing the impact of artifacts. 














 §.§ Performance Evaluation

A common demosaicing approach is to capture the ground truth images using a professional 3-sensor camera, convert them into a mosaic format using Bayer CFA, interpolate using a demosaicing algorithm, and then compare the results with the ground truth image. Widely used performance criteria are mean Squared Error (MSE) or the equivalent Peak Signal to Noise Ratio (PSNR). The Kodak / McMaster <cit.> dataset is generally used as a baseline because of the realistic scenes and varied complexities. The MSE in each color plane is:


    MSE(k) = 1/MN∑_i=1^M ∑_j=1^N {Î_k(i,j) - I_k(i,j)}^2,

where Î_k(i,j) is the predicted and I_k(i,j) is the original pixel color component.
Another measure is the Color Mean Squared Error (CMSE) 
<cit.> which averages over the color channel as well,

    CMSE(I,Î) = 1/3MN∑_k ∈{R, G, B}∑_i=1^M ∑_j=1^N {Î_k(i,j) - I_k(i,j)}^2,

The peak signal to noise ratio (PSNR) is defined as,


    PSNR(k) = 10  log_10(R^2/MSE),

where R is 1.0 (for float datatype), R is 255 (for integer datatype).


The Color Peak Signal to Noise Ratio (CPSNR) is computed using CMSE. Higher the PSNR value, better is the quality of the demosaiced image.





§ ARCHITECTURE SEARCH
 
For the exhaustive search of architectures, we made use of various parameters chosen based on state-of-the-art models to obtain the Pareto front between negative CPSNR as the loss and the number of parameters as the model complexity. To make the comparisons fair, we ran each model for 500 epochs and retrained the state-of-the-art architecture in this new setting. The various hyper-parameters for exhaustive search were mostly discrete and are as follows:



  * The use of depthwise separable convolutions <cit.>, similar to MobileNet V2 architecture <cit.>, over the traditional convolutional layers as per the state-of-the-art models.

  * The number of filters used in the models (16, 32, 64, 128 and 256).

  * Number of blocks, each block consisted of sets of i) depthwise separable ii) standard convolution, and iii) activation function, chosen from (3,5,7 blocks).

  * We use a constant kernel size of 3 × 3 since much of the hardware has shown to be optimized for this convolution <cit.>. 

  * The length of skip connections (1,2).

  * The use of cosine annealing <cit.>  or a fixed learning rate.
 
Some of the reasoning behind choosing the search space is as follows:



  * Primary reason for choosing this range of filter and block values was to obtain networks that have a smaller number of parameters and FLOPs as the current state-of-the-art networks. The filters and block size dominate the parameter density in a network. We defined an upper bound based on this.

  * Motivated from <cit.>, choosing  equal channel width minimizes memory access cost (MAC). Hence, we retained the same channel width across all the layers.

  * Furthermore, <cit.> computed the relative costs of using group convolution, so we used the depth-wise convolution in all the layers.

  * Moreover, for deployment in edge devices, various restrictions are placed based on the platform for inference. In this problem, the model size, the number of FLOPs and MAC play a key role in inference time. For instance, the largest model we have has around 7× 10^5 parameters and 5.2× 10^6 multiplication FLOPs. Deploying such a model would have severe consequences in the overall usability of the edge device

  * We chose a lower bound on the number of filters, around the region, based on the smallest state-of-the-art model, i.e. DMCNN <cit.>.

  * We used point-wise convolutions to avoid the use of fully-connected layers as the last layer, and therefore reduce the model size.

Our goal in this study was to explore architectures that improve the state of the art models by using fewer number of parameters and maintaining reasonable accuracy for edge implementation.







§ METHODOLOGY AND THEORY 
 








Neural architecture search is a discrete optimization problem. Here, we used an exhaustive search over a  small space, where the convergence to the optimum in the designed space is trivial and independent of the loss function, because all points are evaluated. However, most of these neural architecture search techniques are motivated by a very large design space. We show that as the designed space gets infinitely large, even simple methods such as exhaustive search and grid search, require some minimal properties of the loss function to converge to the optimum. Exhaustive search and grid search techniques are common optimization methods that rely only on function evaluation. Therefore, it is counter-intuitive that finding the optimum point using these techniques may require some mathematical assumptions about the search space, or the objective function. Motivated from the derivative-free optimization theory, we aim at clarifying some minimal conditions that the exhaustive search and grid search converges to the optimum. 


Let us set the notation first. We denote a vector using bold  and an element of the this vector using unbold θ. The search space can be a mix of continuous or discrete parameters and denoted by  Θ.
Suppose a neural network is uniquely defined by its hyperparameters embedded in ∈Θ. Note that the complexity of each network might differ. Complexity may be defined as the number of parameters of the network, inference delay, training time, etc. Often the objective is to discover the neural network with minimum loss (or maximum accuracy) on a fixed validation set. Let's denote the validation loss of the neural network with () and its complexity  (). It is preferable to minimize () and () at the same time, i.e. between two neural networks with the same validation loss  (.), the neural network with smaller complexity (.) is preferred. To simplify the theoretical analysis we assume that  (.) is fixed and focus on minimizing  the validation loss function (.) regardless of (.). In the context of demosaicing (.) is the negative CPSNR and (.) is the number of model parameters.




Assume the minimum exists within the search space Θ 

    _∈Θ() ≠∅

Let's define the exhaustive search algorithm to minimize (.)  as

    
  * Enumerate: {_0, _1, …, _K}.
    
  * Initialize: k = 0, ^*=_0, ^*=(_0). 
    
  *  Evaluate  (_k+1).
    
  * If (_k+1)< ^*,  then update ^*=_k+1, ^* = (_k+1).
    
  * If k<K then k=k+1 go to <ref>, otherwise end.

It is trivial that after iterating the algorithm for K iterations, the global minimum of the objective function is obtained (^*, (^*)), for simplicity of notation we may denote   (^*, ^*)  where

    ^* = _∈Θ(),
    ^* = min_∈Θ().

Convergence to the optimum requires no restriction on the loss function (.) except the existence of the minimum (<ref>).
However, if the algorithm is iterated only k<K iterations there is no guarantee of convergence to the minimum. 


Often the cardinality ||Θ||=K is a large value because in a real scenario  a grid search on the hyperparameter vector  is implemented, where the search becomes NP-hard, K=(e^d) where d=(), and the algorithm is run only  k<K iterations depending on computation budget.  


In the following sections we explore under what conditions the exhaustive search convergence  is assured. Suppose the search space is large but is countable,  K=∞. Infinitely many evaluations requires  more conditions on (.). For simplicity of the notation we may assume Θ⊂^d where d=(). Suppose the minimum argument and the objective value  at iteration k of exhaustive search is  denoted by  (^*_k,^*_k)


If  Θ is dense and (.) is continuous,  the exhaustive search converges to the minimum value 

    lim_k→∞^*_k = ^*.



Take the optimal solution ^* ∈Θ and because 
(.) is continuous  and Θ is dense

    ∃δ>0, _k∈Θ_k   where  ||_k - ^* || < δ , |(_k)-(^*)|<ϵ,

or equivalently (θ_k)<(^*)+ϵ.
On the other hand   ∀ k, (^*) ≤(^*_k) ≤(_k)  and by definition ^* = (^*), ^*_k = (^*_k), so

    ^* ≤^*_k ≤(θ_k)≤^*+ϵ,


    lim_k→∞^*_k = ^*.



If Θ is compact and ^*=_∈Θ() is unique, the exhaustive algorithm converges to the unique minimum. 


∀ k, _k∈Θ which is compact, so there is a sub-sequence that converges to ^*_k, call it ^*_ik

    lim_k→∞lim_i→∞^*_ik= .

By continuity of (.)

    lim_k→∞lim_i→∞(^*_ik)=  (),

but from Theorem <ref> we know lim_k→∞(^*_k), therefore ∈Θ. From the uniqueness of the minimum =^*.


The convergence of exhaustive search relies on the continuity of (.). It is easy to construct cases where the exhaustive search fails to converge if (.) is non-continuous <cit.>. Although exhaustive search is the first tool to explore the discrete space, in a continuous space exhaustive search is in-feasible. Therefore optimizing over a continuous parameter such as learning rate, annealing rate, regularization constant, etc requires other tools. Bayesian optimization <cit.> is often used and some open source packages are available <cit.>. However, grid search is still one of the most used tool to tune a continuous parameter.

Grid search on multiple variables is cumbersome as a large dimension of the search variable d implies the evaluation set being (n^d). Multivariate grid search algorithm over the set ∏_i=1^d [a_i, b_i] ⊂^d is as follows: 

    
  * Initialize number of grid evaluation n ∈♮.
    
  * Set the step size δ_i = b_i - a_i  n-1, i=1,…, d.
    
  * Create the grid search space with step iteration j
    
    Θ_n = {| = l_i +  j  δ_i, j=0,…, n-1, i=1,…, d}

    
  * Evaluate the objective function (.) for all n^d grid points θ∈Θ_n.
    
  * Find the grid search solution (^*_n, ^*_n) where
        
    ^*_n = _θ∈Θ_n(),   ^*_n = min_θ∈Θ_n() .




Grid search on d variables is NP-hard, so it is commonly  implemented only on one element of the vector  while keeping the other elements fixed to certain values. Therefore, it makes more sense to explore the convergence of the grid search on a  uni-variate variable θ.

Suppose the continuous uni-variate parameter θ∈[a,b] achieves its minimum at (θ^*,^*). The uni-variate parameter θ is a continuous element, such as  the learning rate, and is also an element of the vector , in which  the vector  fully determines the neural network. 

The grid search convergence result relies on creating a nearly dense set Θ_n by choosing a large number of grid evaluation n. The convergence also depends on (θ) being Lipschitz continuous, i.e.

    ∀θ, θ'∈ [a,b],   |(θ)-(θ')|≤ M |θ-θ'|,

where M is the Lipschitz constant.


Suppose (.) is Lipschitz continuous on θ∈ [a,b] and a grid search is run over n grids to create Θ_n with step size δ, then

    ^*_n  - Mδ 2≤^*



Suppose θ^*∈Θ, take _n ∈Θ_n as a grid search candidate. For any point in the grid Θ_n, define the closest point to θ^* in the grid space
θ_n^* = _θ_n∈Θ_n |θ_n-θ^*|.
The distance between two consecutive points is δ, so  |θ^*_n-θ^*|≤δ 2.  By the Lipschitz continuity of (.)

    |(θ^*_n)-(θ^*)|≤ M |θ^*_n-θ^*| ≤Mδ 2


    ^*_n  - Mδ 2≤^*.




Theorem <ref> means for a finite n, ^*_n may never reach the true minimum ^* as ^* ≤^*_n, but improves swiftly as δ gets smaller by evaluating a smaller step size or equivalently by increasing the number of evaluations n; the lower-bound improves by (n^-1).



§ APPLICATION
 
We first describe the dataset that was used for our experiments and elaborate on the results obtained.


Flickr500 data is used by <cit.> to generate around 3.5 million patches. The authors choose the images with some criteria such as i) being colorful enough to obtain the proper color distributions of real world, ii) high-frequency patterns in the images and iii) of high-quality.  However, there are some key points missing in the description to re-generate the exact results such as:


  * Specifications on exactly how the patches are generated from the original Flickr500 images (related to the stride etc while cropping the patches).

  * Specifications on how to apply the Bayer CFA for creating the mosaic image. 

Therefore, some assumptions were made while generating our data.


  * We generated random patches of 32× 32× 3 to obtain approximately 1.5 million image patches from the 500 images. Since the method is not specified, we used random points in the image and extracted patches from the random points. 1382400 patches were used for the training set and 153600 patches for the validation set. Note: In the paper, they use patches of size 33 ×  33 × 3 but we used 32 × 32 × 3 because the training is much faster on GPU due the dimensions being a power of 2 for tiling.

  * The Bayer CFA generation is also based on the python package. We used the default setting of RGGB and filled the missing channels with zeros to maintain the channel dimensions.

  * All the results in the section were calculated against the McMaster dataset (MCM) containing 18 photos (58000 patches created), and the Kodak images containing 24 photos (78408 patches created).




The Pareto front of our exhaustive search is shown in Figure <ref>. The y-axis represents the validation loss (.) which indicates the negative CPSNR and the x-axis represents the model complexity (.), which is the total number of parameters in the model. The standard error for each experiment is calculated and as they are comparable, they produce a similar prediction interval.



Most of the architectures in the search space outperform the current state-of-the-art (SOTA) as in Figure <ref> in a similar setting, see also Table <ref>. The total number of parameters in the current SOTA is approximately 7× 10^5. Architectures on the Pareto front maintain the same accuracy while significantly reducing the number of parameters. In our search the effect of cosine annealing was not obvious and would need further tuning on cycle rate. The ResNet skip connections length of 2 blocks worked better than 1.


Some of the models on the Pareto front are shown in the table below in comparison to the current state-of-the-art models for the MCM dataset. Once the initial search was completed, the models from the Pareto front architectures were fed through a Bayesian optimizer using the ORION package <cit.>, to optimize the learning rate and regularization constant for L_2 regularization. Figure <ref> indicates further improvement over the initial CPSNR obtained with a fixed learning rate 10^-4 and regularization constant 10^-8, so Bayesian optimization is recommended as an extra step after the architecture search as including it in the first step would further increase the complexity of the search. In our experiments, the PSNR was calculated separately for each image in the test dataset; the final CPSNR value was the mean of all the PSNRs of all images in the dataset.










§ CONCLUSION

Our designed space with a simple exhaustive search outperforms the state of the art and brings a range of loss versus complexity for edge implementation with varying resource constraints. Although in most of architecture search, the number of evaluations and complex search algorithm implementation is the bottleneck, here we showed using prior vision domain knowledge, we can overcome these drawbacks and a simple exhaustive search becomes an effective search tool. We only focused on demosaicing as an example of a low-vision problem where edge implementation is an obligation. We wonder whether a similar approach is useful in edge implementation of other low vision problems. A simple theoretical study suggests the continuity of the loss function and compactness of the search space for exhaustive and grid search to converge to the optimum. 







§ ACKNOWLEDGEMENT

We want to acknowledge Jingwei Chen, Yanhui Geng, and Heng Liao;  executing this research project was impossible without their continuous support. We appreciate the assistance of Huawei media research lab of Japan that provided us details about demosaicing and camera sensors. We  declare our deep gratitude to  Charles Audet and Sébastien Le Digabel who introduced to us the theory behind derivative-free optimization.





