Original link:tecdat.cn/?p=8417

Original source:Tuo End number according to the tribe public number


 

introduce

There’s a lot of data in almost every app we use these days — listening to music, viewing friends’ images, or watching a new trailer

This is not a problem for individual users. However, imagine processing thousands, if not millions, of requests simultaneously with big data. These data streams must be reduced in some way so that we can physically provide them to the user – this is where data compression begins.

There are many compression techniques, and their usage and compatibility vary.

There are two main types of compression:

  • Lossless: Data integrity and accuracy are preferred, even if we’re not “tight-fisted.
  • Lossy: Data integrity and accuracy are not as important as the speed at which we deliver the service – imagine real-time video delivery, where “real-time” delivery is more important than having high-quality video

For example, with Autoencoders, we can decompose this image and represent it as the following 32-vector code. Using it, we can reconstruct the image. Of course, this is an example of lossy compression, because we’ve lost a lot of information.

However, we can do this more precisely by allocating more space to the presentation using exactly the same technique:

 

KerasIs a Python framework that simplifies the construction of neural networks.

First, let’s install Keras using PIP:

$ pip install keras
Copy the code

Preprocessing data

Again, we will use the LFW dataset. As usual for such projects, we will pre-process the data.

To do this, we will first define several paths:

 ATTRS_NAME = "lfw_attributes.txt"

 IMAGES_NAME = "lfw-deepfunneled.tgz"

 RAW_IMAGES_NAME = "lfw.tgz"
Copy the code

We will then use two functions – one to convert the original matrix to an image and change the color system to RGB:

def decode_image_from_raw_bytes(raw_bytes):
    img = cv2.imdecode(np.asarray(bytearray(raw_bytes), dtype=np.uint8), 1)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img
Copy the code

The other is to actually load the dataset and adapt it to our needs:

Copy the code

The implementation of

 
Copy the code

Our data X exists in a matrix in the form of a 3D matrix, which is the default representation of RGB images. By providing three matrices – red, green and blue – the combination of these three matrices produces the image color.

Each pixel of these images will have large values ranging from 0 to 255. In general, in machine learning, we tend to keep values small and centered on 0 because this helps our model train faster and get better results, so let’s normalize the image:

X = x. stype('float32') / 255.0-0.5Copy the code

Now, if we test the minimum and maximum values of the X array, which will be -.5 and.5, you can verify:

print(X.max(), X.min())
Copy the code
0.5-0.5Copy the code

To be able to see the image, let’s create a show_image function. 0.5 Since the pixel value cannot be negative, it will be added to the image:

 
Copy the code

Now, let’s take a quick look at our data:

 

show_image(X[6])
Copy the code

Now let’s break the data down into training and test sets:

 
Copy the code

The sklearn train_test_split() function splits the data by giving it a test ratio, and the rest, of course, is the amount of training. With random_state, you’ll see a lot of machine learning used to produce the same results no matter how many times you run the code.

Now the model:

  
Copy the code

This function takes image_shape (the image size) and code_size (the size of the output representation) as arguments.

Logically, the smaller the code_size, the more compressed the image will be, but the less functionality will be saved, and the more different the copied image will be from the original.

Since the network architecture does not accept 3D matrices, the job of this Flatten layer is to Flatten the (32,32,3) matrix into a one-dimensional array (3072).

 

Now, to connect them together and start our model:

 
Copy the code

After that, we create a link between them using the INP and reconstruction parameters through the Model, and compile it using the Adamax optimizer and the MSE loss function.

Compiling the model here means defining its goals and how to get there. In our context, the goal is to minimize the MSE and to do so by using an optimizer — essentially, an algorithm tuned to find a global minimum.

Results:

_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_6 (InputLayer) (None, 32, 32, 3) 0 _________________________________________________________________ sequential_3 (Sequential) (None, 32) 98336 _________________________________________________________________ sequential_4 (Sequential) (None, 32, 32, 3) 101376 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Total params: 199712 Trainable params: 199712 Non - trainable params: 0 _________________________________________________________________Copy the code

Here we can see that the input is 32,32,3,3.

The hidden layer is 32, and the decoder output you see is (32,32,3).

Model:

 
Copy the code

In this case, we will compare the constructed image to the original image, so x and y are both equal to X_train. Ideally, the input equals the output.

The EPOchs variable defines how many times we want to train data past the model. Validation_data is what we use to evaluate the model validation group after training:

Train on 11828 samples, validate on 1315 samples Epoch 1/20 11828/11828 [==============================] - 3s 272us/step - loss: 0.0128 - val_loss: 0.0087 Epoch 2/20 11828/11828 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 3 s 227 us/step - loss: - val_loss 0.0078:0.0071... Epoch 20/20 11828/11828 [= = = = = = = = = = = = = = = = = = = = = = = = = = = = = =] - 3 s 237 us/step - loss: 0.0067 - val_loss: 0.0066Copy the code

We can visualize losses to get an overview.

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
Copy the code

We can see that after the third period there is no significant progress in losses.

This can also lead to over-fitting of the model, which can make it perform poorly on new data outside of the training and test data sets.

Now, the most anticipated part – let’s visualize the results:

def visualize(img,encoder,decoder): # img [None] has the shape (1, 32, 32, 3), Predict (img[None])[0] reco = decoder.predict(code[None])[0] plt.subplot(1,3,1) PLT. Title (" the Original ") show_image (img) PLT. Subplot PLT. 31 (1) the title (" Code ") PLT. Imshow (code. Reshape ([code. The shape [1] / / 2, 1]), PLT, subplot (filling) 1 PLT. Title (" Reconstructed ") show_image (reco) plt.show() for i in range(5): img = X_test[i] visualize(img,encoder,decoder)Copy the code









Now, let’s increase code_size to 1000:









 

What we’ve just done is principal component analysis (PCA), which is a dimensionality reduction technique. We can use it to reduce the size of the feature set by generating smaller new features, but still capture important information.

Principal component analysis is a very popular usage.

Image denoising

Another popular use is denoising. Let’s add some random noise to the image:

Def applY_gaussian_noise (X, sigma=0.1): noise = NP.random. Normal (LOC =0.0, scale=sigma, size= x.shape) return X + noiseCopy the code

Here, we add some random noise from the standard normal distribution, with a size of sigma, 0.1 by default.

 

For reference, this is what noise looks like with different sigma values:

PLT. Subplot (1,4,1) show_image (X_train [0]) PLT, subplot (1,4,2) show_image (apply_gaussian_noise (X_train [1], the sigma = 0.01) [0]) PLT. Subplot (1, 3) show_image (apply_gaussian_noise (X_train [1], the sigma = 0.1) [0]) PLT, subplot (1,4,4) Show_image (apply_gaussian_noise (X_train [1], the sigma = 0.5) [0])Copy the code

As we can see, we barely see a 0.5 increase in the sigma of the graph. We will attempt to regenerate the original image 0.1 from a noisy image of σ.

The model we will generate for this is the same as the previous model, although we will have different training. This time, we will use the original and corresponding noise images to train it:

 
Copy the code

Now let’s look at the model results:

 
Copy the code








 

conclusion

Principal component analysis, which is a dimension reduction technique, image denoising and so on.

 


Most welcome insight

1. An introduction to image processing in Python using OpencV

2. Partial least squares regression (PLSR) and principal component regression (PCR) in MATLAB

3. VMD variational modal decomposition is used in MATLAB

4. Hampel filtering was used to remove outliers by MATLAB

5. Matlab uses empirical mode decomposition EMD – to denoise the signal

6. Partial least Squares regression (PLSR) and Principal component Regression (PCR) in MATLAB

7. Matlab uses Copula simulation to optimize market risk

8. R language advanced image processing

9. Matlab realizes the estimation of MCMC’s Markov switched ArMA-GarCH model