2025年12月30日 星期二

[python]計算不定期不定額的年化報酬率

 ## Gemini 解釋 XIRR


對於「不定期不定額」的投資,傳統的 CAGR 公式(只看期初和期末)已經不適用了,因為它無法反映資金進出的時間點對獲利的貢獻。


在金融界,這種情況標準的計算方式是 **XIRR (Internal Rate of Return for a schedule of cash flows)**。它考慮了每一筆資金投入的精確日期,並算出一個等效的年化報酬率。


要在 Python 中計算,最推薦的方式是使用 `pyxirr` 庫,或是手動透過 `scipy` 進行數值求解。


---


### 1. XIRR 的數學原理


XIRR 是在找一個折現率 ,使得所有現金流的現值(Present Value)總和等於零:


$$0 = \sum_{i=1}^{N} \frac{P_i}{(1+r)^{\frac{d_i - d_1}{365}}}$$


* $P_i$:第  筆現金流(投入為負數,領回或當前價值為正數)。

* $d_i$:第  筆現金流發生的日期。

* $d_1$:第一筆投資發生的日期。

* $r$:就是我們要找的年化報酬率 (XIRR)。

* $N$:現金流的總次數


---


### 2. Python 程式碼實作


#### 方法 A:使用 `pyxirr` 庫(最簡單、精準)


這是目前處理 XIRR 最推薦的套件,速度快且支援各種格式。


**安裝:** `pip install pyxirr`


```python

from pyxirr import xirr

from datetime import date


# 定義現金流與日期

# 注意:投入資金設為「負值」,最後一筆「當前市值」或「贖回金額」設為「正值」

dates = [

    date(2023, 1, 1),   # 第一次投入 10,000

    date(2023, 4, 15),  # 第二次投入 5,000

    date(2023, 10, 20), # 第三次投入 8,000

    date(2024, 5, 20)   # 當前日期與帳面總價值 (假設現在值 26,000)

]


amounts = [-10000, -5000, -8000, 26000]


# 計算 XIRR

annual_return = xirr(dates, amounts)


print(f"不定期不定額的年化報酬率 (XIRR) 為: {annual_return:.2%}")


```


---


#### 方法 B:使用 `scipy` 手寫邏輯(不依賴金融庫)


如果你不想安裝額外的金融庫,可以用 `scipy` 的優化器來解方程式。


```python

import numpy as np

from scipy.optimize import newton

from datetime import date


def calculate_xirr(dates, amounts):

    # 將日期轉換為天數差距

    days = [(d - dates[0]).days for d in dates]

    

    # 定義 NPV 函數,我們要求解 NPV = 0 的 r

    def npv(r):

        return sum(amt / (1 + r) ** (d / 365) for amt, d in zip(amounts, days))

    

    # 使用牛頓法求解,初始猜測值設為 0.1 (10%)

    try:

        return newton(npv, 0.1)

    except RuntimeError:

        return None # 無法收斂時回傳 None


# 測試資料

test_dates = [date(2023, 1, 1), date(2023, 6, 1), date(2024, 1, 1)]

test_amounts = [-100, -100, 250] # 投兩次 100,最後拿回 250


result = calculate_xirr(test_dates, test_amounts)

print(f"手動計算結果: {result:.2%}")


```


---


### 3. 寫程式時的注意事項


1. **正負號邏輯:**

* **流出 (投資):** 必須是**負數**。

* **流入 (提領或結餘):** 必須是**正數**。如果你還沒賣掉,最後一筆資料就是「今天的日期」和「當前的總市值」。



2. **現金流排序:** 日期建議由遠到近排序,雖然 `pyxirr` 會自動處理,但手寫邏輯時排序能避免計算錯誤。

3. **多重解問題:** 在極少數現金流劇烈震盪的情況下,XIRR 可能會有數學上的多重解,但對於一般的投資行為,數值解通常很穩定。

2025年12月8日 星期一

[點網]使用 openxml 從 CSV 檔轉成 excel 檔

僅供參考

參考網址:

 https://learn.microsoft.com/en-us/office/open-xml/spreadsheet/how-to-create-a-spreadsheet-document-by-providing-a-file-name

https://learn.microsoft.com/en-us/office/open-xml/getting-started

https://www.nuget.org/packages/DocumentFormat.OpenXml

https://blog.darkthread.net/blog/csvhelper/

https://www.cnblogs.com/geovindu/p/19161493


程式:

```

DataTable dataTable = ReadCsv(inputPath, encoding, delimiter);


ConvertDataTableToXls(dataTable, outputPath);


public static DataTable ReadCsv(string filePath, Encoding encoding, char delimiter)

        {

            try

            {

                using (var reader = new StreamReader(filePath, encoding))

                using (var csv = new CsvReader(reader, new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)

                {

                    Delimiter = delimiter.ToString(),

                    HasHeaderRecord = true,

                    IgnoreBlankLines = true,

                    TrimOptions = CsvHelper.Configuration.TrimOptions.Trim

                }))

                {

                    using (var dr = new CsvDataReader(csv))

                    {

                        var dt = new DataTable();

                        dt.Load(dr);

                        return dt;

                    }

                }

            }

            catch (Exception ex)

            {

                throw new Exception($"Failed to read CSV file: {ex.Message}", ex);

            }

        }


public static void ConvertDataTableToXls(DataTable dataTable, string outputPath)

        {

            try

            {

                // Create a new spreadsheet document

                using (SpreadsheetDocument document = SpreadsheetDocument.Create(outputPath, SpreadsheetDocumentType.Workbook))

                {

                    // Add a WorkbookPart to the document

                    WorkbookPart workbookPart = document.AddWorkbookPart();

                    workbookPart.Workbook = new Workbook();

 

                    // Add a WorksheetPart to the WorkbookPart

                    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();

                    worksheetPart.Worksheet = new Worksheet(new SheetData());

 

                    // Add Sheets to the Workbook

                    Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

 

                    // Append a new worksheet and associate it with the workbook

                    Sheet sheet = new Sheet()

                    {

                        Id = workbookPart.GetIdOfPart(worksheetPart),

                        SheetId = 1,

                        Name = "Sheet1"

                    };

                    sheets.Append(sheet);

 

                    // Get the SheetData object

                    SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

 

                    // Add header row

                    Row headerRow = new Row();

                    foreach (DataColumn column in dataTable.Columns)

                    {

                        Cell cell = CreateCell(column.ColumnName, CellValues.String);

                        headerRow.AppendChild(cell);

                    }

                    sheetData.AppendChild(headerRow);

 

                    // Add data rows

                    foreach (DataRow row in dataTable.Rows)

                    {

                        Row dataRow = new Row();

                        foreach (var item in row.ItemArray)

                        {

                            CellValues cellType = GetCellValueType(item);

                            string cellValue = GetCellValueAsString(item, cellType);

                            Cell cell = CreateCell(cellValue, cellType);

                            dataRow.AppendChild(cell);

                        }

                        sheetData.AppendChild(dataRow);

                    }

 

                    // Save the workbook

                    workbookPart.Workbook.Save();

                }

            }

            catch (Exception ex)

            {

                throw new Exception($"Failed to create XLS file: {ex.Message}", ex);

            }

        }


        private static Cell CreateCell(string value, CellValues cellType)

        {

            Cell cell = new Cell();

            cell.DataType = new EnumValue<CellValues>(cellType);

            cell.CellValue = new CellValue(value);

            return cell;

        }

        

        private static CellValues GetCellValueType(object value)

        {

            if (value == DBNull.Value)

                return CellValues.String;

 

            Type type = value.GetType();

 

            if (type == typeof(int) || type == typeof(long) || type == typeof(short) || type == typeof(byte))

                return CellValues.Number;

            else if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))

                return CellValues.Number;

            else if (type == typeof(DateTime))

                return CellValues.Date;

            else if (type == typeof(bool))

                return CellValues.Boolean;

            else

                return CellValues.String;

        }


         private static string GetCellValueAsString(object value, CellValues cellType)

        {

            if (value == DBNull.Value)

                return string.Empty;

 

            switch (cellType)

            {

                case CellValues.Boolean:

                    return (bool)value ? "1" : "0";

                case CellValues.Date:

                    DateTime dateValue = (DateTime)value;

                    // Excel stores dates as OLE Automation dates

                    return dateValue.ToOADate().ToString(CultureInfo.InvariantCulture);

                case CellValues.Number:

                    return Convert.ToString(value, CultureInfo.InvariantCulture);

                default:

                    return Convert.ToString(value);

            }

        }

```

2025年11月14日 星期五

[git]git 推到別站

 # 假設你已經從 A 站 clone 下來,這時 origin 指向 A 站

git remote rename origin upstream

# 新增 B 站的遠端儲存庫

git remote add origin [B 站的 Repository URL]

# 之後你就可以 push 到 B 站了

git push -u origin main

# 如果需要,也可以從 A 站拉取更新

git pull upstream main

2025年9月23日 星期二

[windows]設定 IP

 這幾年,出門去測機,每每要改別人 windows 電腦的 IP,都找不到熟悉的介面改。

一個改 IP 的介面是越藏越深,越來越難找。

乾脆,用命令列來改反而一行解決。當然,首先,要開啟的是系統管理員權限的命令列

改成固定 IP 的命令如下:

netsh interface ipv4 set address name="eth0" static 192.168.1.5 255.255.255.0

改成 DHCP 的命令如下:

netsh interface ipv4 set address name="eth0" source=dhcp

---
1. 那個 name 的值用 ipconfig 查
2. 為何不從 左下角窗戶>齒輪>網路與網際網路>乙太網路 改呢?因為改了沒生效。
3. 從 左下角窗戶>齒輪>網路與網際網路>進階網路設定 這裡有介面卡的名字,也可以改成方便記憶或下指令的名字
4. 舊的 IP 設定介面,從 左下角窗戶>齒輪>網路與網際網路>進階網路設定 點你要的介面卡,資訊會撐開,按下「編輯」按鈕。會打開舊版的設定畫面,在這裡設定 IPv4 的值,會直接生效。

2024年11月11日 星期一

[csharp]發生 Managed Debugging Assistant 'NonComVisibleBaseClass' 錯誤

 我不知道原因,但只知道解法。從解法來看,是 VS IDE 管太多卡到舊DLL了。


In Visual Studio 2019: 

Debug Menu, Windows --> Exception settings, opens the Exception settings window. 

There expand "Managed Debugging Assistants" and finally uncheck NonComVisibleBaseClass


參考:

https://stackoverflow.com/questions/1049742/noncomvisiblebaseclass-was-detected-how-do-i-fix-this

2024年8月27日 星期二

[golang]http.HandleFunc 新的 routing pattern

 在 Go1.22 的時候,讓 http.HandleFunc 新的 routing pattern 可以用變數的形式拿到 URL 路徑裡的值


要使用這個功能,要讓 go build 使用新的編譯方法,不然它總是用舊方法編譯,這是為了相容性。


最簡單就是在 go.mod 裡加上一行 go 1.22 或是 go 1.23





https://stackoverflow.com/questions/28745161/with-gos-webserver-where-does-the-root-of-the-website-map-onto-the-filesystem


https://tip.golang.org/doc/godebug


https://programmingpercy.tech/blog/exciting-go-update-v-1-22/


https://github.com/babafemi99/up-I-go/blob/main/main.go


https://gowithore.hashnode.dev/go-up-or-go-down


https://stackoverflow.com/questions/24116147/how-to-download-file-in-browser-from-go-server


https://mileslin.github.io/2020/03/Golang/%E5%BB%BA%E7%AB%8B%E4%B8%8B%E8%BC%89%E6%AA%94%E6%A1%88%E7%9A%84-Http-Response/

2024年3月18日 星期一

[secs]HSMS timeout

 T6: Active 端

    select.req <-T6-> select.rsp

T7: Passive 端

    open -> not_select <-T7-> select

T5: Active 端

    connect_fail <-T5-> connecting

T8: msg:{B <-T8-> B...}

2024年3月7日 星期四

[csharp]C# 如何得知某個 class 有實作某個介面

 ## 方法

1. objectType.GetInterfaces().Contains(interfaceType)


2. interfaceType.IsAssignableFrom(objectType)


3. objectType.IsAssignableTo(interfaceType)


4. objectType.GetInterface(nameof(interface)) != null


## 參考來源

https://www.facebook.com/groups/1706638306295947/?multi_permalinks=3338835626409532

2024年1月7日 星期日

[vmware]突然出現 misc.rsba_no 不存在的問題

 是否出現 misc.rsba_no 不存在的問題?其實解答不是表面上可以看到的。


我最近突然遇到很多個 suspend 的 vm 開起來就出現這個問題,導致許多暫存的東西皆無法回復。


回頭去查發現這情況都發生在主機的 hyper-v 功能打開之後,來回測試一番,覺得應該是 CPU 特性有改變,讓 vmware 無法回復狀態。


很重要的資料在 vm 裡沒存的話,還是改回去做處理。

2023年12月19日 星期二

[C#]C# 最近出現的 ? ! 是做什麼用的?

 C# 最近出現的 ? ! 是做什麼用的?


最近終於找到一個圖快速介紹這些?!是做什麼用的。


主要是用以解決 null 相關的問題的語法糖


https://twitter.com/mwaseemzakir/status/1647856976477450240/photo/1


Ternary Operator (?:)

Null Forgiving Operator (!.)

Null Conditional Operator (?.)

Null Coalescing Operator (??)

Null Coalescing Assignment Operator (??=)

2022年12月14日 星期三

[DDS]Vortex Opensplice 建議跳槽到 Cyclone DDS

> We, therefore, encourage the Vortex Opensplice open source community users to consider migrating to Cyclone DDS.

看到就準備要跳了喔 


https://github.com/ADLINK-IST/opensplice


For more than a decade, the Vortex Opensplice open source project helped to evangelise and successfully deploy the OMG Data Distribution Service Technology in thousands of industrial and academic projects worldwide. The Vortex Opensplice core team and the community have gained tremendous experience and know-how from these interactions.


Capitalizing on our lengthy experience developing data-centric middleware in real-time distributed systems the core team launched Cyclone DDS, a brand-new open source OMG DDS implementation. Cyclone DDS under the Eclipse foundation governance continues our mission to fuel innovation and serve a more diverse customer base. It is gaining momentum in many opensource frameworks and industrial contexts including ROS2 , Autoware etc. It also represents a genuinely open-source data distribution solution with full source code access and updates.


Buoyed by this success and adoptions, it became obvious that the focus of the core team and the community should now be Cyclone DDS. We, therefore, encourage the Vortex Opensplice open source community users to consider migrating to Cyclone DDS. The migration is straightforward when the ISO CPP V2 APIs are in use. For users that are building mission- and/or business-critical systems, ADLINK continues to support Vortex Opensplice Professional Edition and offers a commercially supported version with extra features and guarantees of support.

2022年10月30日 星期日

[arduino]Arduino IDE 2.0 強制設定為英文介面

 Arduino IDE 1.x 是以 Java 為基底。而 Arduino IDE 2.0 則是以 Javascript 為基底。


仔細的說,Arduino IDE 2.0 是以 node.js(後端) + Chromium(前端) 建構的 electron 架構為底。同樣使用 electron 為架構的有 vscode。(歷史的眼淚,開始是 github 用來做 atom 編輯器,在2022年6月8號,GitHub正式宣布在2022年12月15日關閉Atom,並存檔其儲存庫[by wiki]。)


好處是,近年來的 js 前端風潮,投入的人真的比較眾多且新鮮。進展也許真的會快很多,UI/UX 是真的不一樣。


現在對於 zh-tw 的翻譯還沒有完善,在我的電腦上,有些地方會使用到 zh-cn。如果像我這麼龜毛要處理的話,暴力處理就是強制使用 en,有兩個地方要處理。


  1. 在 menu 上的中文要變英文,超暴力方法,執行檔目錄裡的 locales 目錄,底下除了 en-US.pak 之外的都殺掉。
  2. 在 output 裡的 message 不要看到中文,在 C:\Users\<User>\.arduinoIDE 底下的 arduino-cli.yaml 內容第一行,加上 locale: en <換行>


這樣就可以全部都變英文。

2022年10月7日 星期五

[小知識]從 arduino 學到 NTP packet

 NTP 是網路對時的協定,在一般因為是作業系統自動處理,所以也不知到哪裡找資訊。

反而因為 arduino 這類 MCU 興起的關係,很多老協定的原始碼與 SPEC 就有了關鍵字可以深探與學習。

重點

* UDP 傳送,48 BYTE,UTC時間

兩段重點程式

```

unsigned long sendNTPpacket(IPAddress& address) {

    Serial.println("sending NTP packet...");

    // set all bytes in the buffer to 0

    memset(packetBuffer, 0, NTP_PACKET_SIZE);  //clear the buffer

    //Initialize values needed to form NTP request

    //(see URL above for details on the packets)

    packetBuffer[0]=0b11100011;   // LI, Version, Mode

    packetBuffer[1]=0;     // Stratum, or type of clock

    packetBuffer[2]=6;     // Polling Interval

    packetBuffer[3]=0xEC;  // Peer Clock Precision

    //8 bytes of zero for Root Delay & Root Dispersion

    packetBuffer[12]=49;

    packetBuffer[13]=0x4E;

    packetBuffer[14]=49;

    packetBuffer[15]=52;

    // all NTP fields have been given values, now

    // you can send a packet requesting a timestamp:

    udp.beginPacket(address, 123); //NTP requests are to port 123

    udp.write(packetBuffer, NTP_PACKET_SIZE); //send UDP request to NTP server

    udp.endPacket();

    }

```


```

unsigned long getUnixTime() {

    WiFi.hostByName(ntpServerName, timeServerIP);  //get a random server from the pool

    sendNTPpacket(timeServerIP);                   //send an NTP packet to a time server

    delay(1000);                                   // wait to see if a reply is available


    int cb=udp.parsePacket();                      //return bytes received

    unsigned long unix_time=0;

    if (!cb) {Serial.println("no packet yet");}

    else {  //received a packet, read the data from the buffer

        Serial.print("packet received, length=");

        Serial.println(cb);                        //=48

        udp.read(packetBuffer, NTP_PACKET_SIZE);  //read the packet into the buffer


        //the timestamp starts at byte 40 of the received packet and is four bytes,

        //or two words, long. First, esxtract the two words:

        unsigned long highWord=word(packetBuffer[40], packetBuffer[41]);

        unsigned long lowWord=word(packetBuffer[42], packetBuffer[43]);

        //combine the four bytes (two words) into a long integer

        //this is NTP time (seconds since Jan 1 1900):

        unsigned long secsSince1900=highWord << 16 | lowWord;

        Serial.print("Seconds since Jan 1 1900=" );

        Serial.println(secsSince1900);

        Serial.print("Unix time=");

        //Unix time starts on Jan 1 1970. In seconds, that's 2208988800:

        unix_time=secsSince1900 - 2208988800UL;

        Serial.print(F("Unix time stamp (seconds since 1970-01-01)="));

        Serial.println(unix_time); //print Unix time

        }  

    return unix_time; //return seconds since 1970-01-01

    }

```


參考

* [Arduino C on ESP8266 學習筆記 (三) : 從 NTP 伺服器取得網路時間](http://yhhuang1966.blogspot.com/2017/09/arduino-ide-esp8266-ntp.html)

2022年6月15日 星期三

[MSMQ]remote queue receive 遠端佇列接收

 快速說結論

在 Queue Server 端的設定 (Windows 10, Windows Server 2012R2 在 Workgroup 下測試通過)



然後 Guset 帳號不要開,Guset 帳號不要開,Guset 帳號不要開。

防火牆要注意一下。

就這樣。

其他更詳細的過程,有空再補充。


http://nthrbldyblg.blogspot.com/2017/02/msmq-between-two-computers.html


https://docs.microsoft.com/zh-tw/archive/blogs/johnbreakwell/understanding-how-msmq-security-blocks-rpc-traffic


https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms699854(v=vs.85)?redirectedfrom=MSDN

2022年2月22日 星期二

[powershell] 使用 Windows PowerShell ISE

 # Windows PowerShell ISE


在使用 Windows PowerShell ISE 遇到以下錯誤,簡單來說是因為權限的關係。

預設一般使用者無法執行 powershell script。


```

因為這個系統上已停用指令碼執行,所以無法載入 C:\x\00StartDevPCVM.ps1 檔案。如需詳細資訊,請參閱 about_Execution_Policies,網址為 https:/go.microsoft.com/fwlink/?LinkID=135170。

    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnauthorizedAccess

```


兩個解決方法,

(1)用 administrator 身份開啟 PowerShell 或 PowerShell ISE 來執行

(2)設定執行權限


設定執行權限,也是會遇到權限問題,所以也有兩個方法,

(1)用 administrator 身份開啟 PowerShell 或 PowerShell ISE 來執行「設定執行權限」指令

(2)只設定 CurrentUser 的執行權限


如果是使用方法(1)的話,以下指令可用。


`Set-ExecutionPolicy RemoteSigned`



如果是在使用者的 powershell 執行上面指令會出現



```

PS C:\x> Set-ExecutionPolicy RemoteSigned

Set-ExecutionPolicy : 拒絕存取登錄機碼 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'。 若要變更預設 (Loca

lMachine) 領域的執行原則,請使用 [以系統管理員身分執行] 選項啟動 Windows PowerShell。若要變更目前使用者的執行原則,請執行 "Set-ExecutionPolicy -Scope CurrentUser"。

位於 線路:1 字元:1

+ Set-ExecutionPolicy RemoteSigned

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : PermissionDenied: (:) [Set-ExecutionPolicy], UnauthorizedAccessException

    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

```


那就使用以下指令


`Set-ExecutionPolicy -S CurrentUser RemoteSigned`



## 參考指令

Set-ExecutionPolicy

Get-ExecutionPolicy


Get-ExecutionPolicy -List



* https://docs.microsoft.com/zh-tw/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.2

2021年5月24日 星期一

[點網]小技巧 想讓編譯出來的二進位檔固定

 在 source code 不變的情況下,使得 build binary 要一樣的話,dotnet 編譯參數可以用這個

deterministic = true

參考:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/code-generation

2020年12月22日 星期二

[超譯] pyimagesearch 的 人臉識別

 # [超譯] pyimagesearch 的 人臉識別

來源:https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/


原文作者有提供原始碼,索取需要購買訂閱專案。


## 前言


今天這個貼文會使用到


* opencv

* python

* deep learning


深度學習式的人臉識別有(1)高準確度(2)快速這兩個特點,可以用在靜態影像與動態影像。


## 用 opencv, python, deep learning 做人臉識別


在這個教程裡,你們會學到如何用 opencv, python, deep learning 做人臉識別。


我們會簡單地討論深度學習式的人臉識別如何運作,包含 "deep metric learning" 的概念。


接下來會安裝所需要的函式庫。


最後會實作適用於靜態影像與動態影像的人臉識別。


最後可發現,我們實作的人臉識別有即時處理的能力。


## 了解深度學習式人臉識別


所以,深度學習與人臉識別是如何做到的?


秘密是 "deep metric learning" 的技術。


如果你曾用過其他的深度學習的技術,一般的做法是:


* 接受一個影像

* 輸出一個 分類/標籤 給那個影像


然而,deep metric learning 不一樣。


deep metric learning 會輸出一個實數的特徵向量。


dlib 這個臉部識別網路,會輸出 128-d 的特徵向量(也就是一串數字有 128 個),該特徵向量就是用來數量化臉部特徵。訓練這個網路使用名叫 triplets 的方式來達成:


* 找三張照片,A人有兩張,

* B人有一張,調整權重讓 B人之間的兩張照片的特徵向量比較近,A人與 B人之間的特徵向量比較遠。


套用到實際例子,有三張照片,一張是 Chad Smith,兩張是 Will Ferrell。


我們的網路會數量化這些臉,為每個臉建立出 128-d 的特徵向量( embedding、quantification)


接下來,一般的想法是調整我們神經網路的權重,讓兩張 Will Ferrell 比較靠近,與 Chad Smith 比較遠。


我們的人臉識別的網路架構是取自 ResNet-34,來自 [Deep Residual Learning for Image Recognition](https://arxiv.org/abs/1512.03385) 作者是 He et al.,但是層數比較少,filter 也減半。


網路是由 [Davis King](https://www.pyimagesearch.com/2017/03/13/an-interview-with-davis-king-creator-of-the-dlib-toolkit/) 所訓練,他的資料集約有 3百萬張影像,在 [Labeled Faces in the Wild](http://vis-www.cs.umass.edu/lfw/) 相較於其他現代手法有達到 99.38% 的準確度。


Davis King ([dlib](http://dlib.net/)作者) 與 [Adam Geitgey](https://www.adamgeitgey.com/) ([face_recognition](https://github.com/ageitgey/face_recognition)作者,此模組我們待會會用到) 兩人有詳細文章說明深度學習式的人臉識別的作法。


* [High Quality Face Recognition with Deep Metric Learning](http://blog.dlib.net/2017/02/high-quality-face-recognition-with-deep.html) (Davis)

* [Modern Face Recognition with Deep Learning](https://medium.com/@ageitgey/machine-learning-is-fun-part-4-modern-face-recognition-with-deep-learning-c3cffc121d78) (Adam)


非常推薦你們閱讀上述兩篇文章。


## 安裝人臉識別函式庫


除了 python 與 opencv 之外,還需要兩個函式庫


* [dlib](http://dlib.net/)

* [face_recognition](https://github.com/ageitgey/face_recognition)


dlib 由 [Davis King](https://www.pyimagesearch.com/2017/03/13/an-interview-with-davis-king-creator-of-the-dlib-toolkit/) 維護,包含我們人臉識別工作所需要的 "deep metric learning" 的實作。


face_recognition 由 [Adam Geitgey](https://www.adamgeitgey.com/) 所創,包裝了 dlib 的人臉識別的功能,讓它更方便使用。


我假設你已經裝了 opencv,如果沒有,我的文章 [OpenCV install tutorials](https://www.pyimagesearch.com/opencv-tutorials-resources-guides/) 有介紹。


接下來,來安裝 dlib 與 face_recognition 吧。


> 原文作者非常建議使用 `virtualenv` 加上 `virtualenvwrapper`,以免有 package 污染的問題。


### 安裝 dlib


> 有可能需要安裝 cmake,這個也可以用 `pip install cmake` 安裝


> 現在新的安裝包會自動看環境內有沒有足夠的函式庫,若有就會自己編譯成支援 GPU 的版本。

> Nvidia GPU 需要的有 CUDA Development Tools 與 cuDNN Library(這個要註冊 nvidia 開發者帳號,只要 email 即可申請)


使用 pip 安裝

`pip install dlib`


結束 (時代進步真方便)


### 安裝 face_recognition


使用 pip 安裝

`pip install face_recogntition`


結束


### 安裝 imutils



[imutils](https://github.com/jrosebr1/imutils)這個是方便包,一些 opencv 的組合招式都打包成函式供人取用,原文作者推薦。


使用 pip 安裝

`pip install imutils`


## 我們的人臉識別資料集


因為 Jurassic Park (1993) 是我最喜愛的電影,為了致敬 Jurassic World: Fallen Kingdom (2018) 在美國上映,我們將人臉識別用在這電影的幾個角色上:


* Alan Grant

* Claire Dearing

* Ellie Sattler

* Ian Malcolm

* John Hammond

* Owen Grady


資料集可以在 30 分鐘內使用我的方法建構。參閱 [How to (quickly) build a deep learning image dataset](https://pyimagesearch.com/2018/04/09/how-to-quickly-build-a-deep-learning-image-dataset/)。


有了這資料集,我們將會:


* 建立每個臉的 128-d 特徵向量

* 用這些特徵向量從靜態影像與動態影像中識別出角色們的臉


## 人臉識別專案架構


```

.

├── dataset

│   ├── alan_grant [22 entries]

│   ├── claire_dearing [53 entries]

│   ├── ellie_sattler [31 entries]

│   ├── ian_malcolm [41 entries]

│   ├── john_hammond [36 entries]

│   └── owen_grady [35 entries]

├── examples

│   ├── example_01.png

│   ├── example_02.png

│   └── example_03.png

├── output

│   └── lunch_scene_output.avi

├── videos

│   └── lunch_scene.mp4

├── search_bing_api.py

├── encode_faces.py

├── recognize_faces_image.py

├── recognize_faces_video.py

├── recognize_faces_video_file.py

└── encodings.pickle

```


我們專案有 4 個上層目錄:


* dataset/: 包含六個角色的臉的影像,依據名字放置

* examples/: 三個人臉影像,不在 dataset 裡,用來測試。

* output/: 這裡會存放處理後的人臉識別的動態影像

* videos/: 輸入動態影像會放在這裡。


我們也有六個檔案放在根目錄:


* search_bing_api.py: 第一步是建立 dataset,(原文作者已經寫好程式,直接執行即可)。要學如何使用 Bing API 建立資料集,參閱:[這貼文](https://pyimagesearch.com/2018/04/09/how-to-quickly-build-a-deep-learning-image-dataset/)

* encode_faces.py:用來將人臉編碼成特徵向量。

* recognize_faces_image.py:識別靜態影像中的人臉(依據你的資料集的人臉特徵向量)。

* recognize_faces_video.py:識別來自 webcam 的動態影像中的人臉,並輸出成動態影像。

* recognize_faces_video_file.py:識別來自硬碟的動態影像中的人臉,並輸出成動態影像。但今天不會討論這個,因為其骨架跟 video stream file 一樣。

* encodings.pickle:人臉識別編碼,由 encode_faces.py 處理你的資料集後產生,並序列化到硬碟之中。


在建立完資料集後,我們會使用 encode_faces.py 建立特徵向量。


## 使用 opencv 與 深度學習 建立人臉特徵向量


在我們識別人臉之前,我們首先需要將人臉編碼。這裡並沒有真的訓練識別的網路,而是使用 dlib 已經訓練好的模型。


我們當然可以自己從頭開始訓練自己的模型,或調整已存在的模型。但在這個專案來說太超過了。從頭訓練需要許多的影像。


然後,在分類時,我們可以使用簡單 k-NN 模型加上投票的方式做出人臉分類。其他傳統機器學習模型也有這樣用。


### 建立臉部特徵模型,使用 encode_faces.py。



```

# import the necessary packages

from imutils import paths

import face_recognition

import argparse

import pickle

import cv2

import os


# construct the argument parser and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-i", "--dataset", required=True,

help="path to input directory of faces + images")

ap.add_argument("-e", "--encodings", required=True,

help="path to serialized db of facial encodings")

ap.add_argument("-d", "--detection-method", type=str, default="cnn",

help="face detection model to use: either `hog` or `cnn`")

args = vars(ap.parse_args())


# grab the paths to the input images in our dataset

print("[INFO] quantifying faces...")

imagePaths = list(paths.list_images(args["dataset"]))

# initialize the list of known encodings and known names

knownEncodings = []

knownNames = []


# loop over the image paths

for (i, imagePath) in enumerate(imagePaths):

# extract the person name from the image path

print("[INFO] processing image {}/{}".format(i + 1,

len(imagePaths)))

name = imagePath.split(os.path.sep)[-2]

# load the input image and convert it from BGR (OpenCV ordering)

# to dlib ordering (RGB)

image = cv2.imread(imagePath)

rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


    # detect the (x, y)-coordinates of the bounding boxes

# corresponding to each face in the input image

boxes = face_recognition.face_locations(rgb,

model=args["detection_method"])

# compute the facial embedding for the face

encodings = face_recognition.face_encodings(rgb, boxes)

# loop over the encodings

for encoding in encodings:

# add each encoding + name to our set of known names and

# encodings

knownEncodings.append(encoding)

knownNames.append(name)


# dump the facial encodings + names to disk

print("[INFO] serializing encodings...")

data = {"encodings": knownEncodings, "names": knownNames}

f = open(args["encodings"], "wb")

f.write(pickle.dumps(data))

f.close()


```



### 從靜態影像中識別出角色

recognize_faces_image.py

```

# import the necessary packages

import face_recognition

import argparse

import pickle

import cv2

# construct the argument parser and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-e", "--encodings", required=True,

help="path to serialized db of facial encodings")

ap.add_argument("-i", "--image", required=True,

help="path to input image")

ap.add_argument("-d", "--detection-method", type=str, default="cnn",

help="face detection model to use: either `hog` or `cnn`")

args = vars(ap.parse_args())


# load the known faces and embeddings

print("[INFO] loading encodings...")

data = pickle.loads(open(args["encodings"], "rb").read())

# load the input image and convert it from BGR to RGB

image = cv2.imread(args["image"])

rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes corresponding

# to each face in the input image, then compute the facial embeddings

# for each face

print("[INFO] recognizing faces...")

boxes = face_recognition.face_locations(rgb,

model=args["detection_method"])

encodings = face_recognition.face_encodings(rgb, boxes)

# initialize the list of names for each face detected

names = []


# loop over the facial embeddings

for encoding in encodings:

# attempt to match each face in the input image to our known

# encodings

matches = face_recognition.compare_faces(data["encodings"],

encoding)

name = "Unknown"


    # check to see if we have found a match

if True in matches:

# find the indexes of all matched faces then initialize a

# dictionary to count the total number of times each face

# was matched

matchedIdxs = [i for (i, b) in enumerate(matches) if b]

counts = {}

# loop over the matched indexes and maintain a count for

# each recognized face face

for i in matchedIdxs:

name = data["names"][i]

counts[name] = counts.get(name, 0) + 1

# determine the recognized face with the largest number of

# votes (note: in the event of an unlikely tie Python will

# select first entry in the dictionary)

name = max(counts, key=counts.get)

# update the list of names

names.append(name)


# loop over the recognized faces

for ((top, right, bottom, left), name) in zip(boxes, names):

# draw the predicted face name on the image

cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

y = top - 15 if top - 15 > 15 else top + 15

cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,

0.75, (0, 255, 0), 2)

# show the output image

cv2.imshow("Image", image)

cv2.waitKey(0)

```


### 從 webcam 識別出角色

recognize_faces_video.py

```

# import the necessary packages

from imutils.video import VideoStream

import face_recognition

import argparse

import imutils

import pickle

import time

import cv2

# construct the argument parser and parse the arguments

ap = argparse.ArgumentParser()

ap.add_argument("-e", "--encodings", required=True,

help="path to serialized db of facial encodings")

ap.add_argument("-o", "--output", type=str,

help="path to output video")

ap.add_argument("-y", "--display", type=int, default=1,

help="whether or not to display output frame to screen")

ap.add_argument("-d", "--detection-method", type=str, default="cnn",

help="face detection model to use: either `hog` or `cnn`")

args = vars(ap.parse_args())


# load the known faces and embeddings

print("[INFO] loading encodings...")

data = pickle.loads(open(args["encodings"], "rb").read())

# initialize the video stream and pointer to output video file, then

# allow the camera sensor to warm up

print("[INFO] starting video stream...")

vs = VideoStream(src=0).start()

writer = None

time.sleep(2.0)


# loop over frames from the video file stream

while True:

# grab the frame from the threaded video stream

frame = vs.read()

# convert the input frame from BGR to RGB then resize it to have

# a width of 750px (to speedup processing)

rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

rgb = imutils.resize(frame, width=750)

r = frame.shape[1] / float(rgb.shape[1])

# detect the (x, y)-coordinates of the bounding boxes

# corresponding to each face in the input frame, then compute

# the facial embeddings for each face

boxes = face_recognition.face_locations(rgb,

model=args["detection_method"])

encodings = face_recognition.face_encodings(rgb, boxes)

names = []


    # loop over the facial embeddings

for encoding in encodings:

# attempt to match each face in the input image to our known

# encodings

matches = face_recognition.compare_faces(data["encodings"],

encoding)

name = "Unknown"

# check to see if we have found a match

if True in matches:

# find the indexes of all matched faces then initialize a

# dictionary to count the total number of times each face

# was matched

matchedIdxs = [i for (i, b) in enumerate(matches) if b]

counts = {}

# loop over the matched indexes and maintain a count for

# each recognized face face

for i in matchedIdxs:

name = data["names"][i]

counts[name] = counts.get(name, 0) + 1

# determine the recognized face with the largest number

# of votes (note: in the event of an unlikely tie Python

# will select first entry in the dictionary)

name = max(counts, key=counts.get)

# update the list of names

names.append(name)


    # loop over the recognized faces

for ((top, right, bottom, left), name) in zip(boxes, names):

# rescale the face coordinates

top = int(top * r)

right = int(right * r)

bottom = int(bottom * r)

left = int(left * r)

# draw the predicted face name on the image

cv2.rectangle(frame, (left, top), (right, bottom),

(0, 255, 0), 2)

y = top - 15 if top - 15 > 15 else top + 15

cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,

0.75, (0, 255, 0), 2)


    # if the video writer is None *AND* we are supposed to write

# the output video to disk initialize the writer

if writer is None and args["output"] is not None:

fourcc = cv2.VideoWriter_fourcc(*"MJPG")

writer = cv2.VideoWriter(args["output"], fourcc, 20,

(frame.shape[1], frame.shape[0]), True)

# if the writer is not None, write the frame with recognized

# faces to disk

if writer is not None:

writer.write(frame)


    # check to see if we are supposed to display the output frame to

# the screen

if args["display"] > 0:

cv2.imshow("Frame", frame)

key = cv2.waitKey(1) & 0xFF

# if the `q` key was pressed, break from the loop

if key == ord("q"):

break


# do a bit of cleanup

cv2.destroyAllWindows()

vs.stop()

# check to see if the video writer point needs to be released

if writer is not None:

writer.release()

```


### 從影像檔中識別出角色


先前提過,recognize_faces_video_file.py 基本上跟前一個程式一模一樣,差別只在影像來源是影像檔而不是 webcam。


## 能否在樹莓派執行這些程式?


基本上可以,但是有些限制


1. 樹莓派記憶體不夠使用 CNN-based 臉部偵測

2. 所以只能用 HOG 臉部偵測

3. HOG 在樹莓派上太慢,無法勝任即時臉部偵測

4. 所以需要使用 opencv haar cascades


(譯註:我的電腦 16G 也沒辦法做 CNN-based 臉部偵測)


在樹莓派上的速度約是 1-2 FPS。好消息時之後我會回來討論如何在樹莓派上執行這些程式,敬請期待。


## 結論


在這教程,你們學到了如何使用 opencv, python, deep learning 執行人臉識別。


我們利用了 Davis King 的 dlib 與 Adam Geitgey 的 face_recognition,讓實作更方便。


我們也看到,這裡提出的程式在準確度與有 GPU 的情況下即時運算的能力皆有達到水準。


希望你們喜歡這則人臉識別的貼文。



To download the source code to this post, and be notified when future tutorials are published here on PyImageSearch, just enter your email address in the form below!

2020年12月17日 星期四

[linux] 小工具 網路流量監控 nethogs 與 vnstat 與 iftop

 vnstat 執行後會不停計算網路流量,在結束之後,會出現統計數字,結算執行開始到結束的總流量與流速。這個比較適合很多台的流量報表產生。

Monitoring eth0...    (press CTRL-C to stop)


   rx:       51 kbit/s   105 p/s          tx:     6.64 Mbit/s   357 p/s^C



 eth0  /  traffic statistics


                           rx         |       tx

--------------------------------------+------------------

  bytes                      435 KiB  |       44.99 MiB

--------------------------------------+------------------

          max             106 kbit/s  |     9.71 Mbit/s

      average           68.49 kbit/s  |     7.26 Mbit/s

          min              42 kbit/s  |     3.32 Mbit/s

--------------------------------------+------------------

  packets                       7321  |           22559

--------------------------------------+------------------

          max                218 p/s  |         600 p/s

      average                140 p/s  |         433 p/s

          min                 84 p/s  |         208 p/s

--------------------------------------+------------------

  time                    52 seconds


nethogs 依照系統 process 分別列出流量,這個會比較適合在一台機器內抓誰是異常。

NetHogs version 0.8.5-2


    PID USER     PROGRAM                    DEV        SENT      RECEIVED       

   6344 pi       ..sr/lib/vino/vino-server  eth0      154.737       1.080 KB/sec


 iftop 依照對外部端點的進出列出流量,這個適合在一群機器裡可以抓出流量去哪兒了。


參考

  • https://askubuntu.com/questions/2411/how-do-i-find-out-which-process-is-eating-up-my-bandwidth

[python] pympler檢查記憶體使用量

一個可以檢查記憶體使用量的lib。

https://pythonhosted.org/Pympler/

from pympler import muppy
from pympler import summary
all_objects = muppy.get_objects()
all = summary.summarize(all_objects)
summary.print_(all)

2020年11月18日 星期三

[NAS]檔案儲存的安全

我很早之前是使用 mac 的 softraid,將兩個USB外接硬碟組成磁碟陣列,比起當時的其他的 raid 方案更彈性。

當硬碟失效時,可以換個硬碟,拉進磁碟陣列中等重建過後,即可恢復上線。
當 mac 失效時,可以換個 mac,兩個硬碟插入開始,磁碟陣列就可上線。
對硬碟沒有什麼要求,對 mac 的新舊也沒有要求。壞什麼就換什麼。就算只剩一個硬碟是好的,也可以恢復,也可以直接讀單顆硬碟裡的資料。後來因為太好用,所以後來一個 mac 上就接到 6 個硬碟,有三組磁碟陣列。用 samba 服務提供其他電腦存取。

但是時間久了,硬碟常斷電下線,手動回復重建時又可能遇到斷電下線,後來才知道有可能 USB 電源供應會因為年紀而衰退。但當時並不知道,所以重覆個幾次,就有資料毀損的情況產生。當把 mac 退役後,才感受到,一個 mac 掛三組磁碟陣列,一但失去它,就全部檔案無法使用的影響太大。但又沒找到更好方法,接下來幾年就只有使用單碟冷備份的方式存放檔案。

後來知道有 glusterfs,可以多主機共組磁碟陣列,彈性也是很高,於是採用樹莓派加個外接硬碟組成一個節點,兩個節點組成一組磁碟陣列。如此,四個部件壞掉哪一個都不會影響正常檔案存取。就算只剩一顆硬碟,用任何 linux 也可以讀到原來的檔案,將來沒有 glusterfs 也不用怕資料無法回復。而且變更節點組成的時候,可以線上變更,在修理、維護的時候仍然可以提供服務。一開始仍是用 samba 分享,但是仍然會有多檔、大檔會複製失敗的情況,後來自己寫個非常簡單的上傳程式,也就用快三年。也經歷過幾次硬碟壞掉或樹莓派壞掉的情況,修起來因為沒有圖形介面比較需要動腦,但是真的是很放心。在本機端來看,一組磁碟陣列就是一個硬碟掛到一般目錄之下,所以可以直接透過檔案系統做更複雜的管理(新增、刪除、移動)。

最近,又得知有個 min.io 的分散式網路儲存庫,號稱是很無腦,也很有彈性,只是它的彈性是節點以上的彈性。節點本身被規定一但組成就不能被變更,每個節點可以不一樣但建議要一樣。但是群內加節點擴增容量或減節點維設都不會影響整個服務,且保證有一半的部件失效都可正常讀取,容量會是總磁碟的一半。因為其目標是針對網路儲存,所以有內建網頁服務與網路API,在網路應用上就不用我自己寫個程式做檔案上下傳,但它就沒有辦法用檔案系統直接管理檔案了而是直接使用它提供的指令或網頁服務。

glusterfs 與 min.io 並不是互斥的選項,它們兩個也可以一起使用。glusterfs 是強在它的虛擬磁碟組成的動態彈性,而 min.io 是網路節點群的動態彈性。由這兩個的彈性可以組成自己想要的網路儲存系統。