2025/1/21

CARLA : bounding box

ref: CARLA 裡面的每個物件,都有一個 bounding_box 屬性。

所以就想到,直接用這個屬性來 labeling.
有幾個文章github project 就是這樣做的: 甚至有一篇論文說怎樣可以做出比較準的 2D bounding box: Improve Bounding Box in CARLA simulator

2025/1/20

CARLA : sensor radar

資料內容:

ref: radar sensor point 的資料是:
  • altitude (float – radians) : Altitude angle of the detection.
  • azimuth (float – radians) : Azimuth angle of the detection.
  • depth (float – meters) : Distance from the sensor to the detection position.
  • velocity (float – m/s) : The velocity of the detected object towards the sensor.
但是沒有說坐標系...例如 0 度是在哪 ?

follow unreal engines coordinate system ?
ref:


操作

ref: 有說明,從 raw_data 轉成 上述內容的 sample code:
# To get a numpy [[vel, azimuth, altitude, depth],...[,,,]]:
points = np.frombuffer(radar_data.raw_data, dtype=np.dtype('f4'))
points = np.reshape(points, (len(radar_data), 4))
-- comment 排列方式跟上面的剛好不一樣

PythonAPI/example 中的 manual_control.py 有 G 可以toggle radar point 在畫面上。

2025/1/16

Xorg, VirtualGL and VNC

ref: 在headless server 上,要用 remote desktop 又要使用到 GPU 的話,就要用到這三個。
  • VirtualGL : 把 application 的 OpenGL 呼叫轉送到另一個xserver,並且把這個 xserver render 的結果拿來,交給真正負責application 顯示的 x server
  • Xorg : 負責一個GPU加速功能的 X server
  • TurboVNC : 負責顯示
所以要用 Xorg create 一個使用 GPU (Nvidia) 的 X config.
然後啟動一個 X server

然後啟動 TurboVNC server,這個server 會把畫面畫出來,並且傳給 vncviewer

然後用 vglrun 來啟動 application.


以 CARLA 為例,要在 headless server 上 remote run.

先 create 一個 nvidia 的 Xserver:
$ nvidia-xconfig --query-gpu-info
Number of GPUs: 1

GPU #0:
  Name      : NVIDIA TITAN RTX
  UUID      : GPU-XXXXXXXXXXXX
  PCI BusID : PCI:1:0:0

  Number of Display Devices: 1

  Display Device 0 (TV-6):
      No EDID information available.
看一下 GPU 的 PCI Bus Id.
然後 create 一個 xorg.conf:
$ sudo nvidia-xconfig -a --allow-empty-initial-configuration --use-display-device=None --virtual=1920x1080 --busid=PCI:1:0:0

Using X configuration file: "/etc/X11/xorg.conf".
Option "AllowEmptyInitialConfiguration" "True" added to Screen "Screen0".
Backed up file '/etc/X11/xorg.conf' as '/etc/X11/xorg.conf.backup'
New X configuration file written to '/etc/X11/xorg.conf'
然後因為 nvidia driver 版本大於 440.xx,所以在 xorg.conf 的 Screen 的地方加上:
Option "HardDPMS" "false"



啟動一個 Xserver session,為了讓一班 user 也能啟動 X session,要修改 /etc/X11/Xwrapper.conf,加入下面:
allowed_users=anybody
needs_root_rights=yes
然後 user 要在 tty group 裡。

啟動一個 X session,使用 display number 7

X.Org X Server 1.21.1.11
X Protocol Version 11, Revision 0
Current Operating System: Linux i7-14700 6.8.0-35-generic #35-Ubuntu SMP PREEMPT_DYNAMIC Mon May 20 15:51:52 UTC 2024 x86_64
Kernel command line: BOOT_IMAGE=/boot/vmlinuz-6.8.0-35-generic root=UUID=e4ea2afe-cc4e-42ce-a53f-5032e417f9f7 ro
xorg-server 2:21.1.12-1ubuntu1.1 (For technical support please see http://www.ubuntu.com/support)
Current version of pixman: 0.42.2
        Before reporting problems, check http://wiki.x.org
        to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
        (++) from command line, (!!) notice, (II) informational,
        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.7.log", Time: Fri Jan 17 05:52:20 2025
(==) Using config file: "/etc/X11/xorg.conf"
(==) Using system config directory "/usr/share/X11/xorg.conf.d"
The XKEYBOARD keymap compiler (xkbcomp) reports:
> Warning:          Could not resolve keysym XF86CameraAccessEnable
> Warning:          Could not resolve keysym XF86CameraAccessDisable
> Warning:          Could not resolve keysym XF86CameraAccessToggle
> Warning:          Could not resolve keysym XF86NextElement
> Warning:          Could not resolve keysym XF86PreviousElement
> Warning:          Could not resolve keysym XF86AutopilotEngageToggle
> Warning:          Could not resolve keysym XF86MarkWaypoint
> Warning:          Could not resolve keysym XF86Sos
> Warning:          Could not resolve keysym XF86NavChart
> Warning:          Could not resolve keysym XF86FishingChart
> Warning:          Could not resolve keysym XF86SingleRangeRadar
> Warning:          Could not resolve keysym XF86DualRangeRadar
> Warning:          Could not resolve keysym XF86RadarOverlay
> Warning:          Could not resolve keysym XF86TraditionalSonar
> Warning:          Could not resolve keysym XF86ClearvuSonar
> Warning:          Could not resolve keysym XF86SidevuSonar
> Warning:          Could not resolve keysym XF86NavInfo
Errors from xkbcomp are not fatal to the X server



啟動 Turbovnc 的 vnc server,負責 application 的 display,用 8 作為 display number:
$ /opt/TurboVNC/bin/vncserver :8

Desktop 'TurboVNC: i7-14700:8 (charles-chang)' started on display i7-14700:8

Starting applications specified in /opt/TurboVNC/bin/xstartup.turbovnc
Log file is /home/charles/.vnc/i7-14700:8.log
如果是第一次啟動,會要求給一個 vncviewer 用的 password.

vnc server 啟動之後,其他pc 就可以透過 viewer 來顯示 vncserver 的內容。
vncserver 的 service port 是 5900+display numner,
也就是說,剛剛的設定,service port 是 5908


之後就可以用 vglrun 來執行 application,讓執行程式使用 virtualgl 提供的 opengl 服務,並且經由 DISPLAY 變數傳送render 的結果:
~$ DISPLAY=:8 vglrun -d :7 glxgears
先指定DISPLAY環境變數是:8 (VNCSERVER),然後用 "-d :7" 指定 opengl rendering 交給 display :7 (nvidia X session) 負責。

2025/1/15

MESA-LOADER: failed to open iris

ref:
先是...
MESA-LOADER: failed to open iris: /usr/lib/dri/iris_dri.so: cannot open shared object file: 
No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: iris
MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: 
No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  149 (GLX)
  Minor opcode of failed request:  3 (X_GLXCreateContext)
  Value in failed request:  0x0
  Serial number of failed request:  167
  Current serial number in output stream:  168
所以參考上面的 ref,去看 /usr/lib 下果然沒有 dri 目錄。所以把安裝位置 ln 過來...
sudo apt --reinstall install libgl1-mesa-dri
cd /usr/lib
sudo ln -s x86_64-linux-gnu/dri ./dri
然後 Error:
MESA-LOADER: failed to open iris: /home/charles/miniconda3/envs/carla/bin/../lib/libstdc++.so.6: 
version `GLIBCXX_3.4.30' not found (required by /lib/x86_64-linux-gnu/libLLVM-17.so.1) 
(search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
failed to load driver: iris
MESA-LOADER: failed to open swrast: /home/charles/miniconda3/envs/carla/bin/../lib/libstdc++.so.6: 
version `GLIBCXX_3.4.30' not found (required by /lib/x86_64-linux-gnu/libLLVM-17.so.1) 
(search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
X Error of failed request:  BadValue (integer parameter out of range for operation)
所以看一下 conda 系統的 libstdc++.so.6 的 GLIBCXX 支援版本有:
$ strings /home/charles/miniconda3/lib/libstdc++.so.6 | grep ^GLIBCXX
GLIBCXX_3.4
GLIBCXX_3.4.1
GLIBCXX_3.4.2
GLIBCXX_3.4.3
GLIBCXX_3.4.4
GLIBCXX_3.4.5
GLIBCXX_3.4.6
...
GLIBCXX_3.4.28
GLIBCXX_3.4.29
GLIBCXX_DEBUG_MESSAGE_LENGTH
GLIBCXX_3.4.21
GLIBCXX_3.4.9
GLIBCXX_3.4.10
GLIBCXX_3.4.16
GLIBCXX_3.4.1
...
GLIBCXX_3.4.4
GLIBCXX_3.4.26
果然沒有 3.4.30

因為是 libstdc++.so.6.0.29
系統的 /usr/lib/x86-linux-gnu/libstdc++.so 是 6.0.33

所以把 /usr/lib/x86_64-linux-gnu/ 的 6.0.33 copy 到 env 的 lib,然後重新 ln libstdc++.so.6 到 libstdc++.so.6.0.33 就可以了。

md file, markdown reader in linux console

bat

有在 ubuntu 的 apt repo 里,所以用 apt install bat 就可以。
但是command 是 batcat,
他是希望跟 cat 一樣的用法。

install 完後,因為它支援 theme,所以default theme不喜歡的話,就要產生一個 config,然後改 default theme.
$ batcat --generate-config-file
Success! Config file written to /home/charles/.config/bat/config
然後去修改 ~/.config/bat/config
-- #--theme="TwoDark"
++ --theme="GitHub"
default theme 是給 darkmode用的。bright mode 可以用 "GitHub" 這個 theme.

使用方法就跟 cat 一樣:
batcat README.md



batcat 不太好用,markdown 語法沒有render的很好,有些還是 raw output,
所以用 mdcat 試試看。
是用 rust 寫的,apt 還沒有。
所以要clone 下來 build

跟 batcat 比起來是 render 多一點 markdown 語法了。



update : 最後還是去裝 chrome 的 extension : markdown viewer

2025/1/14

Carla : Install and run on Linux

網站說明有latest, stable, 0.9.15 ...etc,但是github 的 Download 只有 0.9.15,所以猜 latest 也是 0.9.15 (?)
github Download page 還有 nightly build,說明包含 bugfix 和 new feature,所以猜是 unstable.

Download CARLA_0.9.15.tar.gz,解開會有多目錄,所以在一個目錄中解開比較好。

從 Quick start package installation : Install Client Library
使用 tar.gz 檔安裝,PythonAPI/carla/dist 中有需要的 python packages file : *.whl
提供的 python version 是 : cp37,所以 carla-0.9.15 的 python 版本應該是 python3.7

這個folder 提供的對應 whl 檔,跟用 pip install carla 安裝的檔案相同。
使用的是 carla-0.9.15-cp37-cp37m-manylinux_2_27_x86_64.whl

除此之外,還要install pygame 和 numpy 兩個 package,就可以開始 run :
文件說明是
./CarlaUE4.sh
結果發現沒有使用 GPU,參考cannot run Carla using nvidia GPU under Linux with multi-GPU installed : PRIME instruction ignored by Carla #4716
加上 option
./CarlaUE4.sh -prefernvidia
就可以了。


從主網站 carla.org 進入可以看到release 0.10.0的說明。
0.10.0 的 python 版本不同了。

一樣從whl的檔名來看,0.10.0 的 python 版本應該是 3.10
一樣解開download 的 0.10.0 (這次是zip檔),到 PythonAPI/carla 安裝 requirement.txt,再裝 dist/*.whl,之後就可以啟動:
./CarlaUnreal.sh 
0.10.0 不用家 -prefernvidia,就會用 GPU了。

0.10.0 vRAM用到 8G以上,使用 RTX4070,移動鏡頭快一點就會出現 vulkan out of memory Error.


carla script 啟動的是 server,控制的化,可以參考 Python_API/Example 下的 python code.
在server running 的情況下, run 這些 python code 就可以調整carla 的內容。

在啟動 automatic_control.py 時出現 mesa load iris error.依照這一篇 解決。


ref:
run on server without display

可以 run carla server 在遠端,然後 client 才顯示出來。
.CarlaUE4.sh -prefernvidia -RenderOffScreen



carla 是一個 server ,其他 client 用 socket api 控制,就像遊戲的 server 一樣,
carla server 只對 localhost 開放,所以遠端無法控制。

使用現有的 python script 就可以操作。每個 script 都要獨立 run,所以用 & 到背景,不然就要每個都開一個 console.
  • 先把 carla run 起來,可以不show 出來 . (-RenderOffScreen)
  • 加入一些車流 : generate_traffic.py
  • 自動改變天氣 : dynamic_weathre.py
  • 開啟開車控制: manual_control.py,這時候才會出現畫面
  • 控制環境 (車燈,路燈...) : util/environment.py
要所有的車把車燈打開:
python environment.py --cars LowBeam All



這個 韓國人寫的,從頭開始寫 python code 控制 CARLA 也不錯,很清楚。



Docker

ubuntu 24.04 直接 run carla 0.9.15 的話,使用 vulkan 會出現 memory allocation error,使用 -prefernvidia 的話,run 一陣子就會自動 hang 住。
所以還是用 docker 來 run。
docker imagge 要用 carlasim 的 : carlasim/carla:0.9.15,latest 的更新比 0.9.15 還舊,所以用 0.9.15
要使用 nvidia 的話,就要用 --gpus all,這樣就要安裝 nvidia-toolkit:
sudo apt install nvidia-container-toolkit
sudo nvidia-ctk runtime configure --runtime=docker
sudo systemctl restart docker
這樣docker 的 --cpus all 才會有作用。

啟動 carla docker 的command 是:
docker run --privileged --gpus all --net=host -e DISPLAY=$DISPLAY carlasim/carla:0.9.15 /bin/bash ./CarlaUE4.sh
啟動了之後,因為 --net=host,所以要 run PythonAPI/examples 下的 code 都一樣。
docker 改用 -it 刪掉 ./CarlaUE4.sh,進入 bash 看, 是 Ubuntu 18.04.3...

2024/11/24

Simple URL RAG with ollama locally

這篇就是翻譯下面這個 link,source code 也是下面這個 link 的 code: 文章中,embedding 用了 openai,為了作到完全 local,改用 OllamaEmbeddings

url rag 做的大概是..
列出url,把所有 url 讀進來,把內容攤平成一維
import os
os.environ['USER_AGENT'] = 'myagent'

from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
# List of URLs to load documents from
urls = [
    "https://lilianweng.github.io/posts/2023-06-23-agent/",
    "https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/",
    "https://lilianweng.github.io/posts/2023-10-25-adv-attack-llm/",
]
# Load documents from the URLs
docs = [WebBaseLoader(url).load() for url in urls]
docs_list = [item for sublist in docs for item in sublist]
再把網頁內容分成一小段一小段
# Initialize a text splitter with specified chunk size and overlap
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
    chunk_size=250, chunk_overlap=0
)
# Split the documents into chunks
doc_splits = text_splitter.split_documents(docs_list)
把這些一小段一小段的句子,轉成 embedding,也就是一個 N 維 tensor。
因為要 run locally,所以用 OllamaEmbeddings 來做:
from langchain_ollama import OllamaEmbeddings

embeddings = OllamaEmbeddings(
    model="llama3",
)
所有字句轉成 embedding/tensor 後,要放到一個 local 的 database 里,讓𠹌一下 user 問問題的時候,來databasae 找答案。
這邊用 SKLearnVectorStore 這個 database :
from langchain_community.vectorstores import SKLearnVectorStore
from langchain_openai import OpenAIEmbeddings
# Create embeddings for documents and store them in a vector store
vectorstore = SKLearnVectorStore.from_documents(
    documents=doc_splits,
    embedding=embeddings,
)
retriever = vectorstore.as_retriever(k=4)
RAG 的 vectorstore 和 sql 不同的地方是,在 query 時,vecrotstore 給的是最接近 query 的內容,而不是像 sql 一樣,要完全 match 的 data。

url 資料都準備好了,接下來就是 ollama 對接 LLM 的部份。
prompt template. TAG process chain.
from langchain_ollama import ChatOllama
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
# Define the prompt template for the LLM
prompt = PromptTemplate(
    template="""You are an assistant for question-answering tasks.
    Use the following documents to answer the question.
    If you don't know the answer, just say that you don't know.
    Use three sentences maximum and keep the answer concise:
    Question: {question}
    Documents: {documents}
    Answer:
    """,
    input_variables=["question", "documents"],
)

# Initialize the LLM with Llama 3.1 model
llm = ChatOllama(
    model="llama3.1",
    temperature=0,
)

rag_chain = prompt | llm | StrOutputParser()
做出 RAG class:
# Define the RAG application class
class RAGApplication:
    def __init__(self, retriever, rag_chain):
        self.retriever = retriever
        self.rag_chain = rag_chain
    def run(self, question):
        # Retrieve relevant documents
        documents = self.retriever.invoke(question)
        # Extract content from retrieved documents
        doc_texts = "\\n".join([doc.page_content for doc in documents])
        # Get the answer from the language model
        answer = self.rag_chain.invoke({"question": question, "documents": doc_texts})
        return answer
用這個 RAG class 來測試
# Initialize the RAG application
rag_application = RAGApplication(retriever, rag_chain)
# Example usage
question = "What is prompt engineering"
answer = rag_application.run(question)
print("Question:", question)
print("Answer:", answer)
輸出會是..
Question: What is prompt engineering
Answer: Prompt engineering is the process of designing and optimizing input prompts for language models, such as chatbots or virtual
assistants. According to Lilian Weng's 2023 article "Prompt Engineering", this involves techniques like word transformation, character 
transformation, and prompt-level obfuscations to improve model performance. The goal is to create effective and efficient prompts that 
elicit accurate responses from the model.


其他的 ref,用 web UI