# PIL cv2 numpy read image and toTensor Comparison
## Read Image
**以下實驗皆是重複執行兩百次的平均時間**
### 1. PIL.Image.open() average time
=> read image from ramdisk time = 7.078141877145478e-05
### 2. cv2.imread() average time
=> read image from ramdisk time = 0.010074698708274148
### 3. numpy.load() average time
=> read numpy from ramdisk time = 0.0007499661108460089
這邊看起來PIL讀取速度有絕對的優勢,因此下一步我們計算一下,若是包含讀取時間,
### 4. cv2.FileStorage() average time
```python=1
imgPath = "/mnt/ramdisk/24262_00000006.bin"
fs = cv2.FileStorage(imgPath, cv2.FILE_STORAGE_READ)
x = fs.getNode("image").mat()
```
=> time cost = 0.1551884485013557
### 5. sio.loadmat() reading average time
```python=1
x = sio.loadmat('/mnt/ramdisk/np_vector.mat')['vect']
```
0.0004106507156834458
## To Tensor
由於cv2.imread之後所得即為numpy格式,因此兩者toTensor的時間基本上市相同,差別只在讀取速度而已。
**以下實驗皆是重複執行兩百次的平均時間**
### 1. read JPG by *PIL* and toTensor average time
```python=1
y = Image.open(imgPath)
pilTensor = toTensor(imagePILsrc.convert('RGB'))
```
=> time cost = 0.019735742096949106
### 2. read JPG by *cv2* and toTensor average time
```python=1
y = cv2.imread(imgPath)
cv2Tensor = toTensor(y[...,::-1].copy())
```
=> time cost = 0.020389318466186523
### 3. read *npy* file and toTensor average time
```python=1
x = np.load(np_path)
npTensor = toTensor(x[...,::-1].copy())
```
=> time cost = 0.008736178128406255
### 4. cv2.FileStorage() average time
```python=1
imgPath = "/mnt/ramdisk/24262_00000006.bin"
fs = cv2.FileStorage(imgPath, cv2.FILE_STORAGE_READ)
x = fs.getNode("image").mat()
npTensor = toTensor(x[...,::-1].copy())
```
=> time cost = 0.17995472267420606
### 5. sio.loadmat() and to tensor
```python=1
x = sio.loadmat('/mnt/ramdisk/np_vector.mat')['vect']
npTensor = toTensor(x[...,::-1].copy())
```
=> time cost = 0.009023726588547831
## To Tensor 後的數值差異
1. cv2轉tensor跟npy轉tensor是沒有差別的 (相減全部=0)
2. np轉tensor跟PIL轉tensor 數值上是有差別的,差別如下
2.1. 在解析度為1280720的圖片中,相同的像素佔整張圖片的 **9.6%**
2.2. 差異的值的範圍為 0.00784 至 0.0784
2.3. 若是在toTensor之前,先將np array轉為float16 or 32,不相同的值佔整張圖片的**99.96%**