Translate

Read info in htop

1. How to install htop:

sudo apt install htop


2. Info in htop:


    1) CPU usage:

        Number: CPU thread ID (1 CPU core = 2 CPU thread)

        Progress bar:

            blue: the percentage of CPU for low priority threads

            green: the percentage of CPU for user threads

            red: the percentage of CPU for kernel threads

            

    2) Memory & Swap:

        green: Used memory

        blue: Buffers

        yellow: Cache


* When RAM is full, it will use swap memory. Swap memory is much slower than RAM.

* Swap: By using a swap file, the computer can use more memory than is physically installed. In other words, it can run more programs than it could run with just the limited resources of the installed RAM. Swap files are not stored in physical RAM, which is why they are a type of virtual memory.



How to add code block in blogger

Use the following code to add code block in blogger:

<pre class="prettyprint">source code here</pre>

Example: 

def test(a, b):
    return a + b


ref: https://kkyard.blogspot.com/

Some ffmpeg commands

 1. Use ffmpeg crop a part of video:

ffmpeg -i input.mp4 -filter:v "crop=640:1080:960:0" -c:a copy output.mp4

*  640: the width of cropped size
    1080: the height of cropped size
    960: x of top-left point
    0: y of top-left point

2. Use ffmpeg crop a period of video:

ffmpeg -ss 00:01:00 -to 00:02:00 -i input.mp4 -c copy output.mp4

       ffmpeg -i input.mp4 -ss 00:01:00 -to 00:02:00 -c copy output.mp4 (some version)

*  -ss: start time
    -to: end time

3. Use ffmpeg extract all the frames from a video:

        ffmpeg -i o1.mp4 -r 1/1 ./o1/o1_%03d.jpg

   


 

How to use tmux

 Advantages:

1. can open multiple terminals in one screen

2. can share the same one terminal with others

3. can let the program running in the background


Commands:

1. install tmux: sudo apt-get install tmux

2. start tmux: tmux

3. check all the tmux: tmux ls

4. connect to a certain tmux: tmux attach -t <id>

5. split into two windows horizontally (左右两个): Ctrl b + %

6. split into two windows vertically (上下两个): Ctrl b + "

7. move your cursor: Ctrl b + arrow key

8. page up to see more info in one window: Ctrl b + [

9. close tmux window: Ctrl d


zip, unzip & tar

 zip/unzip:

1. zip -r name.zip ./folder_name

2. unzip name.zip

3. unzip -o /destination_path name.zip


tar:

1. tar.gz: 

    compression: tar -zcvf name.tar.gz /path

    decompression: tar -zxvf name.tar.gz

2. tar.bz2:

    compression: tar -jcvf name.tar.bz2 /path

    decompression: tar -jxvf name.tar.bz2


Some commands for docker

install docker:

curl -fsSL https://test.docker.com -o test-docker.sh sudo sh test-docker.sh

  1. list the containers: docker ps
  2. list all the containers: docker ps -a
  3. list the images: docker images
  4. change another image name: docker tag name1 name2
  5. commit the change to image: docker commit id name
  6. delete the image: docker rmi name
  7. stop a container: docker stop name
  8. remove a container: docker rm name
  • enter into a container: 
docker exec -it Name /bin/bash
  • save a image:
docker save image_name > Name.tar
  • load a image:
docker load < Name.tar
  • start a docker:
docker run --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 --cap-add SYS_PTRACE --rm --gpus all --pid=host --net=host --security-opt apparmor:unconfined -e DISPLAY=$DISPLAY -v /home/temp:/workspace -v /home/pipeline:/data/pipeline -w /data/pipeline_capture -it Name


9. docker compose:
    start all the images:
        docker-compose up -d
        docker-compose up -d --build
        docker-compose -f docker-compose-local.yml -d --build
        (use a specific docker compose file)

    stop all the images:
        docker compose down


10. docker storage analysis:
    docker system df

11. docker clear storage:
    clean all:
        docker system prune
    clean cache:
        docker builder prune
    clean volume:
        docker volume prune
    clean container:
        docker container prune

12. docker logs:
    if i only want to check the last 100 lines of logs since a certain time:
        docker logs -f -t [--since="2018-02-08"] --tail=100 CONTAINER_ID
            -f: 跟踪实时日志
            -t: 显示时间戳
 
    if i only want to check the latest 30min of logs:
        docker logs --since 30m CONTAINER_ID





Some commands for vim

  1. delete a whole line: dd
  2. copy a line: yy
  3. paste: p
  4. copy a few lines: shift+v + y
  5. copy a few words: v + y
  6. undo: u
  7. redo: control+r
  8. replace A with B: :%s,A,B,g
  9. add a word in the beginning of each line: ctrl+v + shift+i + "words" + esc
  10. exit with saving: :wq
  11. exit without saving: :q!
  12. to the top line: gg
  13. to the end line: shift+g
  14. search a word: /"word" + enter
    1. next: n
    2. previous: shift+n
  15. to line 100: 100 + shift+g
  16. to the first of the line: 0
  17. to the end of the line: $
  18. enter replace mode: shift+r
  19. delete all lines: Esc+gg+dG

How to add LATEX Mathematical formula on blogger

  1. change to HTML mode
  2. add the follow code at the top
  3. save it

<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js">
MathJax.Hub.Config({
 extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js"],
 jax: ["input/TeX", "output/HTML-CSS"],
 tex2jax: {
     inlineMath: [ ['$','$'], ["\\(","\\)"] ],
     displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
 },
 "HTML-CSS": { availableFonts: ["TeX"] }
});
</script>

  • use dollar when you want to add it within a line like $\vec{A}$
  • use double dollar if you want to let the formula in a single line like $$\cos\theta=1$$


ref: https://2formosa.blogspot.com/2016/07/equations-in-blogspot.html

Euclidean distance VS Cosine distance

Before we talk about euclidean distance and cosine distance, let's see what is Norm firstly.

    1. What is Norm?

        The distance from the origin is called Norm of $\vec{A}$.
        To calculate Norm of $\vec{A}$, there are several methods:
            1) Euclidean distance
                $$d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$
                Norm is the distance from the origin, so it equals to $\sqrt{x_2^2+y_2^2}$
            
            Vector Norm using Euclidean distance is called L2-Norm.

            2) Manhattan distance
                $$d=|x_2-x_1|+|y_2-y_1|$$
                Norm equals to $|x_2|+|y_2|$
            
            Vector Norm using Manhattan distance is called L1-Norm.

    2. Euclidean distance
        
        If $\vec{A}$ = $(x_1,y_1)$, $\vec{B}$ = $(x_2,y_2)$,
        The Euclidean distance between $\vec{A}$ and $\vec{B}$ should be:
            $$d=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$
        Smaller, closer.

    3. Cosine similarity

$$\cos\theta=\frac{\vec{A}\cdot\vec{B}}{||\vec{A}||\cdot||\vec{B}||}=\frac{\sum_{i=1}^{n}x_iy_i}{\sqrt{\sum_{i=1}^{n}x_i^2}\cdot\sqrt{\sum_{i=1}^{n}y_i^2}}$$

        The cosine value means the angle between two vectors, $\cos\theta\in[-1,1]$
        More close to 1, more similar

    4. Cosine distance

        Cosine distance = $1-\cos\theta$. So, cosine distance $\in[0,2]$
        Smaller, closer.

* What is Normalization:
        $$\vec{y} = \frac{\vec{x}}{||\vec{x}||}$$
        $\vec{y}$ is $\vec{x}$ after normalization. All the value in $\vec{y} \in [-1,1]$. Also, $||\vec{y}||=1$.

    5. The relationship between Euclidean distance and Cosine distance:

        After normalization, $||\vec{A}||=1$, $||\vec{B}||=1$
        Cdist($\vec{A}$, $\vec{B}$) = $1-\vec{A}\cdot\vec{B}$       
        Edist($\vec{A}$, $\vec{B}$) = $\sqrt{||\vec{A}-\vec{B}||^2}$ = $\sqrt{||\vec{A}||^2+||\vec{B}||^2-2\vec{A}\vec{B}}$ = $\sqrt{2}\cdot\sqrt{1-\vec{A}\vec{B}}$
        Which means after normalization, Euclidean distance has the same monotonicity with Cosine distance.

    6. In python 
       
    Use cosine distance get topk vectors:
        f = f / np.linalg.norm(f, axis=-1. keepdims=True)
        sim = (features * f).sum(axis=1)
        topk_idx = np.argpartition(-sim, tuple(range(K)))[:K]
        topk_val = sim[topk_idx].tolist()