# ML Samples with Docker for Windows Container
### Windows base image for containers install
> https://hub.docker.com/_/microsoft-windows
Upgrade os版本到Microsoft Windows [Version 10.0.17763.1339]
= 2020年7月14日-KB4558998 (作業系統組建17763.1339)
更新完後執行docker前必須先到該路徑執行dockerd.exe
接著docker 中pull下來
```
docker pull mcr.microsoft.com/windows:[version]
```
:::info
測試是使用10.0.17763.1339-amd64或是1809兩個版本都ok
以下皆以1809為範例操作
:::

---
### DirectX Container Sample
> https://github.com/MicrosoftDocs/Virtualization-Documentation/tree/master/windows-container-samples/directx
:::info
這邊是用[WinMLRunner(Windows-Machine-Learning Tool)](https://github.com/Microsoft/Windows-Machine-Learning/tree/master/Tools/WinMLRunner)去跑tinyyolov2的一個sample
主要為了示範GPU加速容器化及運行DirectX workload
:::
用notepad++ create a file called "Dockerfile"
Dockerfile中寫入以下
```
> Dockerfile
FROM mcr.microsoft.com/windows:1809
WORKDIR C:/App
# Download and extract the ONNX model to be used for evaluation.
RUN curl.exe -o tiny_yolov2.tar.gz https://onnxzoo.blob.core.windows.net/models/opset_7/tiny_yolov2/tiny_yolov2.tar.gz && \
tar.exe -xf tiny_yolov2.tar.gz && \
del tiny_yolov2.tar.gz
# Download and extract cli tool for evaluation .onnx model with WinML.
RUN curl.exe -L -o WinMLRunner_x64_Release.zip https://github.com/microsoft/Windows-Machine-Learning/releases/download/1.2.1.1/WinMLRunner.v1.2.1.1.zip && \
tar.exe -xf C:/App/WinMLRunner_x64_Release.zip && \
del WinMLRunner_x64_Release.zip
# Run the model evaluation when container starts.
ENTRYPOINT ["C:/App/WinMLRunner v1.2.1.1/x64/WinMLRunner.exe", "-model", "C:/App/tiny_yolov2/model.onnx", "-terse", "-iterations", "100", "-perf"]
```
接著回到cmd cd到剛檔案 將該dockerfile build起來
```
docker build . -t winml-runner
```
build完如果沒出錯 就可run
```
docker run --isolation process --device class/5B45201D-F2F2-4F3B-85BB-30FF1F953599 winml-runner
```
這支sample跑一個ML Model 100次
一開始用CPU優化,接著跑GPU
最後產出一個report
---
### Windows Container Sample - Python
> https://github.com/MicrosoftDocs/Virtualization-Documentation/tree/master/windows-container-samples/python
:::info
這個sample主要是在windows container裡搭建python3.7.3環境
然後簡單的print一個hello world出來
創建一個新的資料夾 接著新增一個Dockerfile
:::
```
> Dockerfile
# This dockerfile utilizes components licensed by their respective owners/authors.
# Prior to utilizing this file or resulting images please review the respective licenses at: https://docs.python.org/3/license.html
FROM mcr.microsoft.com/windows:1809
LABEL Description="Python" Vendor="Python Software Foundation" Version="3.7.3"
RUN powershell.exe -Command \
$ErrorActionPreference = 'Stop'; \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
wget https://www.python.org/ftp/python/3.7.3/python-3.7.3.exe -OutFile c:\python-3.7.3.exe ; \
Start-Process c:\python-3.7.3.exe -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1' -Wait ; \
Remove-Item c:\python-3.7.3.exe -Force
RUN echo print("Hello World!") > c:\hello.py
CMD ["py", "c:/hello.py"]
```
跟上一個範例一樣 先build
```
docker build -t 'name'
```
```
docker run -it 'name'
```
>Install Python via command line/powershell without UI
(quietly/slient install python)
透過cmd以無UI的方式安裝Python
選擇版本:https://www.python.org/ftp/python/
```
> powershell
Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
wget https://www.python.org/ftp/python/[version].exe -OutFile c:\[version].exe
Start-Process c:\[version].exe -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1'
```
---
### Tensorflow Directml Sample
> https://docs.microsoft.com/en-us/windows/win32/direct3d12/gpu-tensorflow-windows
::: warning
*這邊注意的是tensorflow只支援64 bits Python 3.5 - 3.7
以及tensorflow需要msvcp140.dll這個元件
解決方式是安裝Microsoft Visual C++ 2015 Redistributable Update 3
範例中用的python檔 放在dockerfile同一個directory中
:::
```
> Dockerfile
FROM mcr.microsoft.com/windows:1809
# assign work directory
WORKDIR /python
# move all files to work directory including test.py
COPY . /python
# Silent Install Microsoft Visual C++ 2015 Redistributable Update 3
RUN powershell.exe -Command \
wget https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x64.exe -OutFile vc_redist.x64.exe ; \
Start-Process vc_redist.x64.exe -ArgumentList '/q /norestart' -Wait
Remove-Item vc_redist.x64.exe -Force
# Silent Install Python 3.6.1 64bits
RUN powershell.exe -Command \
$ErrorActionPreference = 'Stop'; \
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
wget https://www.python.org/ftp/python/3.6.1/python-3.6.1rcl-amd64.exe -OutFile python-3.6.1rcl-amd64.exe ; \
Start-Process python-3.6.1rcl-amd64.exe -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1' -Wait ; \
Remove-Item python-3.6.1rcl-amd64.exe -Force
RUN pip install tensorflow-directml
# -u to insure python print is working
CMD ["py", "-u", "test.py"]
```
test.py中的內容只是用來測試tensorflow是否成功安裝
```python=
import tensorflow.compat.v1 as tf
tf.enable_eager_execution(tf.ConifProto(log_device_placement=True))
print(tf.add([1.0, 2.0], [3.0, 4.0]))
```
```
docker build -t tensorflow-directml .
```
```
docker run -it tensorflow-directml
```
result:
```python
2020-07-23 20:06:09.756930: I tensorflow/core/common_runtime/dml/dml_device_factory.cc:45] DirectML device enumeration: found 1 compatible adapters.
2020-07-23 20:06:09.917532: I tensorflow/core/common_runtime/dml/dml_device_factory.cc:32] DirectML: creating device on adapter 0 (Microsoft Basic Render Driver)
2020-07-23 20:06:09.433379: I tensorflow/stream_executor/platform/default/dso_loader.cc:60] Successfully opened dynamic library DirectMLba106a7c621ea741d2159d8708ee581c11918380.dll
2020-07-23 20:06:09.558039: I tensorflow/core/common_runtime/eager/execute.cc:571] Executing op Add in device /job:localhost/replica:0/task:0/device:DML:0
tf.Tensor([4. 6.], shape=(2,), dtype=float32)
```
已經包好push到Docker hub
https://hub.docker.com/r/msxlol/tensorflow-directml-sample
*How to Use*
```
docker run msxlol/tensorflow-directml
```
---
### GPU Enabled Images Examples
>https://www.tensorflow.org/install/docker
```
docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu \
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
```
result:

{"metaMigratedAt":"2023-06-15T11:01:25.424Z","metaMigratedFrom":"Content","title":"ML Samples with Docker for Windows Container","breaks":true,"contributors":"[{\"id\":\"a93c4ac0-2e40-422a-8326-700c575a1cdb\",\"add\":6613,\"del\":206}]"}