私的AI研究会 > PyReview1
2021年8月から、坂本俊之著「PyTorch ではじめる AI開発」(C&R研究所 刊) を元に進めた AI学習をもう一度復習する。
c:\anaconda_win\workspace_pylearn\ ← Windows の場合 ~/workspace_pylearn/ ← Linux の場合 ├ chapter01 ├ chapter02 ├ forest-path-movie-dataset │ ├── scenes │ ├── all_file.csv │ └── scene.csv └ sample ├── chapt01 ├── chapt02 ├── chapt03 ├── chapt04 ├── chapt05 ├── chapt06 ├── chapt07 └── chapt09
Windows の場合 Windows11 / Windows10
(base) PS C:\anaconda_win> conda create -n py_learn python=3.11
(base) PS > conda activate py_learn
(py_learn) PS > pip3 install torch torchvision torchaudio ← CPU の場合 (2024.02.07) (py_learn) PS > pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 ← GPU の場合 (2024.02.12)
(py_learn) PS > conda install opencv pandas tqdm matplotlib PyYAML
(py_learn) PS > pip install scikit-learn pycocotools facenet-pytorch
(py_learn) PS > python -V Python 3.11.7・「Pytorch」のインストール確認とバージョン
(py_learn) PS > python -c 'import torch;print(torch.__version__)'・「Pytorch」で GPU 使用できるかの確認
(py_learn) PS > python -c 'import torch;print(torch.zeros(1).cuda())'・「tkinter」パッケージのインストール確認
(py_learn) PS > python -c 'import tkinter'・「ffmpeg」のコマンド確認
(py_learn) PS > ffmpeg -h
Linux の場合 Ubuntu22.04LTS / Ubuntu20.04LTS
(base) $ conda create -n py_learn python=3.11
(base) $ conda activate py_learn
(py_learn) $ pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu ← CPU の場合 (2024.02.07) (py_learn) $ pip3 install torch torchvision torchaudio ← GPU の場合 (2024.02.12)
(py_learn) $ sudo apt install libgtk2.0-dev (py_learn) $ sudo apt install pkg-config
(py_learn) $ conda install -c loopbio gtk2 ffmpeg gtk2-feature opencv pandas tqdm matplotlib PyYAML
(py_learn) $ pip install scikit-learn pycocotools facenet-pytorch
(py_learn) $ python -V Python 3.11.0・「Pytorch」のインストール確認とバージョン
(py_learn) $ python -c 'import torch;print(torch.__version__)'・「Pytorch」で GPU 使用できるかの確認
(py_learn) $ python -c 'import torch;print(torch.zeros(1).cuda())'・「tkinter」パッケージのインストール確認
(py_learn) $ python -c 'import tkinter'・「ffmpeg」のコマンド確認
(py_learn) $ ffmpeg -h・各種コマンド確認
(py_learn) $ which python /home/USER/anaconda3/envs/py_learn/bin/python (py_learn) $ which pip /home/USER/anaconda3/envs/py_learn/bin/pip (py_learn) $ which conda /home/USER/anaconda3/condabin/conda
(py_learn) pip install opencv-python
(py_learn) PS > cd /anaconda_win/workspace_pylearn/chapter01 (py_learn) PS > python chapt01_1x.pyLinux の場合
(py_learn) $ cd ~/workspace_pylearn/chapter01 (py_learn) $ python chapt01_1x.py
(py_learn) pip install PyMuPDF
(py_learn) PS > cd /anaconda_win/workspace_pylearn/chapter02Linux の場合
(py_learn) $ cd ~/workspace_pylearn/chapter02
(py_learn) PS > python chapt02_1x.py learning start >> 2024-02-11 16:41:51.736780 : learning end >> 2024-02-11 17:31:33.180019 learning time >> 0:49:41.443341
(py_learn) $ python chapt02_1x.py learning start >> 2024-02-12 16:51:44.443271 : learning end >> 2024-02-12 18:00:20.908547 learning time >> 1:08:36.465290
OS | 機種 | 開始日時 | 終了日時 | 処理時間 (h:m) |
Windows11 | HP ENVY TE02-1097 NVIDIA GeForce RTX 4070 Ti | 2024/02/11 16:42 | 2024/17/31 00:09 | 00:50 |
UIbuntu22.04LTS | HP ENVY TE02-1097 NVIDIA GeForce RTX 4070 Ti | 2024/02/12 16:52 | 2024/01/12 18:00 | 01:09 |
Windows10 | HP EliteDesk 800 NVIDIA GeForce GTX 1050 Ti | 2023/10/12 19:01 | 2023/10/13 00:09 | 05:08 |
Windows11 | DELL XPS Plus 9320 Intel® Core™ i7-1260P CPU | 2024/02/04 09:43 | 2024/02/05 11:29 | 25:46 |
Ubuntu20.04LTS | DELL Latitude 7520 Intel® Core™ i7-1185G7 CPU | 2023/09/05 14:35 | 2023/09/07 02:02 | 35:27 |
Windows10 | HP EliteDesk 800 Intel® Core™ i7-6700 CPU | 2023/09/05 11:00 | 2023/09/07 05:15 | 41:55 |
Windows11 | DELL Vostro 3500 Intel® Core™ i3-1115G4 CPU | 2024/02/15 16:10 | 2024/02/18 00:22 | 56:12 |
(py_learn) python chapt02_2x.py
(py_learn) python chapt02_2y.py
(py_learn) pip install openvino (py_learn) pip install onnx (py_learn) pip install onnxruntime
(py_learn) python chapt02_3x.py
(py_learn) pip install openvino-dev・onnx モデルを IR モデルにのコンバート
(py_learn) mo --input_model chapt02-model1.onnx --mean_values=input[123.675,116.28,103.53] --scale_values=input[58.395,57.12,57.375] --reverse_input_channels [ INFO ] Generated IR will be compressed to FP16. If you get lower accuracy, please consider disabling compression explicitly by adding argument --compress_to_fp16=False. Find more information about compression to FP16 at https://docs.openvino.ai/2023.0/openvino_docs_MO_DG_FP16_Compression.html [ INFO ] The model was converted to IR v11, the latest model format that corresponds to the source DL framework input/output format. While IR v11 is backwards compatible with OpenVINO Inference Engine API v1.0, please use API v2.0 (as of 2022.1) to take advantage of the latest improvements in IR v11. Find more information about API v2.0 and IR v11 at https://docs.openvino.ai/2023.0/openvino_2_0_transition_guide.html [ INFO ] MO command line tool is considered as the legacy conversion API as of OpenVINO 2023.2 release. Please use OpenVINO Model Converter (OVC). OVC represents a lightweight alternative of MO and provides simplified model conversion API. Find more information about transition from MO to OVC at https://docs.openvino.ai/2023.2/openvino_docs_OV_Converter_UG_prepare_model_convert_model_MO_OVC_transition.html [ SUCCESS ] Generated IR version 11 model. [ SUCCESS ] XML file: C:\anaconda_win\workspace_pylearn\chapter02\chapt02-model1.xml [ SUCCESS ] BIN file: C:\anaconda_win\workspace_pylearn\chapter02\chapt02-model1.bin・コンバート結果の確認
(py_learn) python chapt02_model_data_x.py --- OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02 --- OpenCV: 4.9.0 OpenVINO inference_engine: 2023.3.0-13775-ceeafaf64f3-releases/2023/3 OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02: Starting application... - Model : ./chapt02-model1.xml - Device : CPU - Image file : ../sample/chapt02/chapt02-sample_off.jpg input blob: name='input', N=1, C=3, H=224, W=224 >>> Inference execution... >>> output >>> Type : <class 'openvino.runtime.utils.data_helpers.wrappers.OVDict'> Length : 1 KeyList : [<ConstOutput: names[output] shape[1,2] type: f32>] ValueList: [array([[ 1.8649045, -1.0063534]], dtype=float32)] *** STER 1 *** {<ConstOutput: names[output] shape[1,2] type: f32>: array([[ 1.8649045, -1.0063534]], dtype=float32)} *** STER 2 *** [[ 1.8649045 -1.0063534]] Type : <class 'numpy.ndarray'> Shape : (1, 2) Dimension: 2 *** STER 3 *** [ 1.8649045 -1.0063534] Type : <class 'numpy.ndarray'> Shape : (2,) Dimension: 1 Finished.※ 変換したファイル「chapt02-model1.xml」「chapt02-model1.bin」
(py_learn) python chapt02_4y.py
#from openvino.inference_engine import IECore #from openvino.inference_engine import get_version import openvino.runtime as ov
#print("OpenVINO inference_engine:", get_version()) print("OpenVINO inference_engine:", ov.get_version())
#ie = IECore() core = ov.Core() #net = ie.read_network(model = model_detector, weights = model_detector[:-4] + '.bin') #exec_net = ie.load_network(network = net, device_name = device) compiled_model = core.compile_model(model_detector, device_name = device) infer_request = compiled_model.create_infer_request()
#out = exec_net.infer(inputs={input_blob_name: img}) out = infer_request.infer(img)
#out = out[output_blob_name] #out = np.squeeze(out) # サイズ1の次元を全て削除 out = np.squeeze(out[0]) # サイズ1の次元を全て削除
(py_learn) python chapt02_4y.py --- Surveillance camera with OpenVINO(API 2.0) Ver 0.02 --- OpenCV: 4.9.0 OpenVINO inference_engine: 2023.3.0-13775-ceeafaf64f3-releases/2023/3 Surveillance camera with OpenVINO(API 2.0) Ver 0.02: Starting application... - Image File : ../sample/chapt02/chapt02-sample-a.mp4 - m_detect : chapt02-model1.xml - Device : CPU - Language : jp - Input Shape : input - Output Shape : output - Program Title: y - Speed flag : y - Processed out: non FPS average: 32.30 Finished.
#from openvino.inference_engine import IECore #from openvino.inference_engine import get_version import openvino.runtime as ov
#print("OpenVINO inference_engine:", get_version()) print("OpenVINO inference_engine:", ov.get_version())
#ie = IECore() core = ov.Core()
#net = ie.read_network(model=model, weights=model[:-4] + '.bin') #exec_net = ie.load_network(network=net, device_name=device, num_requests=1) compiled_model = core.compile_model(model, device_name = device) infer_request = compiled_model.create_infer_request()
#input_key = list(net.input_info.keys())[0] # 入力データ・キー名 #input_blob_name = net.input_info[input_key].name #output_blob_name = next(iter(net.outputs)) #batch,channel,height,width = net.input_info[input_blob_name].input_data.shape input_layer = compiled_model.input(0) input_blob_name = input_layer.any_name output_layer = compiled_model.output(0) output_blob_name = output_layer.any_name batch,channel,height,width = input_layer.shape
(py_learn) python chapt02_model_data_x.py
(py_learn) python chapt02_model_data_x.py --- OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02 --- OpenCV: 4.9.0 OpenVINO inference_engine: 2023.3.0-13775-ceeafaf64f3-releases/2023/3 OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02: Starting application... - Model : ./chapt02-model1.xml - Device : CPU - Image file : ../sample/chapt02/chapt02-sample_off.jpg input blob: name='input', N=1, C=3, H=224, W=224 >>> Inference execution... ##### {<ConstOutput: names[output] shape[1,2] type: f32>: array([[ 1.603146 , -0.7632909]], dtype=float32)} >>> output >>> Type : <class 'openvino.runtime.utils.data_helpers.wrappers.OVDict'> Length : 1 KeyList : [<ConstOutput: names[output] shape[1,2] type: f32>] ValueList: [array([[ 1.603146 , -0.7632909]], dtype=float32)] *** STER 1 *** {<ConstOutput: names[output] shape[1,2] type: f32>: array([[ 1.603146 , -0.7632909]], dtype=float32)} *** STER 2 *** [[ 1.603146 -0.7632909]] Type : <class 'numpy.ndarray'> Shape : (1, 2) Dimension: 2 *** STER 3 *** [ 1.603146 -0.7632909] Type : <class 'numpy.ndarray'> Shape : (2,) Dimension: 1 Finished.
(py_learn) python chapt02_model_data_x.py -m chapt02-model1.onnx --- OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02 --- OpenCV: 4.9.0 OpenVINO inference_engine: 2023.3.0-13775-ceeafaf64f3-releases/2023/3 OpenVINO™ Model Data Check with OpenVINO(API 2.0) Ver 0.02: Starting application... - Model : chapt02-model1.onnx - Device : CPU - Image file : ../sample/chapt02/chapt02-sample_off.jpg input blob: name='input', N=1, C=3, H=224, W=224 >>> Inference execution... ##### {<ConstOutput: names[output] shape[1,2] type: f32>: array([[-23.688124, 41.998665]], dtype=float32)} >>> output >>> Type : <class 'openvino.runtime.utils.data_helpers.wrappers.OVDict'> Length : 1 KeyList : [<ConstOutput: names[output] shape[1,2] type: f32>] ValueList: [array([[-23.688124, 41.998665]], dtype=float32)] *** STER 1 *** {<ConstOutput: names[output] shape[1,2] type: f32>: array([[-23.688124, 41.998665]], dtype=float32)} *** STER 2 *** [[-23.688124 41.998665]] Type : <class 'numpy.ndarray'> Shape : (1, 2) Dimension: 2 *** STER 3 *** [-23.688124 41.998665] Type : <class 'numpy.ndarray'> Shape : (2,) Dimension: 1 Finished.
(py_learn) python chapt01_1.py [ WARN:0@0.030] global /croot/opencv-suite_1691620365762/work/modules/videoio/src/cap_gstreamer.cpp (862) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
# OpenCVのカメラキャプチャーを用意する # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
: Traceback (most recent call last): File "C:\anaconda_win\workspace_pylearn\chapter01\chapt01_1.py", line 19, in <module> cv2.imshow('frame',frame) cv2.error: OpenCV(4.6.0) C:\b\abs_f8n1j3l9l0\croot\opencv-suite_1691622637237\work\modules\highgui\src\window.cpp:1267: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'
(py_learn) > pip install opencv-python
(py_learn) python chapt02_1.py RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom in the main module: if __name__ == '__main__': freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable.
(py_learn) python chapt02_1.py ----: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead. warnings.warn( ----: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=None`. warnings.warn(msg)
# model = models.resnet50(pretrained=True) model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V1)・torchvision.models の pretrained が非推奨
(py_learn) python chapt02_2a.py Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\tkinter\__init__.py", line 1948, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\tkinter\__init__.py", line 861, in callit func(*args) File "C:\anaconda_win\workspace_pylearn\chapter02\chapt02_2x.py", line 77, in updateFrame frame = frame[:,:,::-1] ~~~~~^^^^^^^^^^ TypeError: 'NoneType' object is not subscriptable
----: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead. warnings.warn( ----: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=None`. warnings.warn(msg)
# model = models.resnet50(pretrained=False) model = models.resnet50(weights=None)・torchvision.models の pretrained が非推奨
----: DeprecationWarning: 'imghdr' is deprecated and slated for removal in Python 3.13 import imghdr
Traceback (most recent call last): : File "/home/USER/workspace/lib/myfunction.py", line 17, in cv2_putText text_w, text_h = dummy_draw.textsize(text, font=fontPIL) ^^^^^^^^^^^^^^^^^^^ AttributeError: 'ImageDraw' object has no attribute 'textsize'
「python chapt02_2x.py」「python chapt02_2y.py」【Windows / Linux】CUDA 実行環境の場合のみ発生
(py_learn) python chapt02_2x.py Exception in thread Thread-1 (detect): Traceback (most recent call last): File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/threading.py", line 1045, in _bootstrap_inner self.run() File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/threading.py", line 982, in run self._target(*self._args, **self._kwargs) File "/home/mizutu/workspace_pylearn/chapter02/chapt02_2x.py", line 103, in detect batch_result = model(batch_tensor) ^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torchvision/models/resnet.py", line 285, in forward return self._forward_impl(x) ^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torchvision/models/resnet.py", line 268, in _forward_impl x = self.conv1(x) ^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 460, in forward return self._conv_forward(input, self.weight, self.bias) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/mizutu/anaconda3/envs/py_learn/lib/python3.11/site-packages/torch/nn/modules/conv.py", line 456, in _conv_forward return F.conv2d(input, weight, bias, self.stride, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same
: model.load_state_dict(torch.load('chapt02-model1.pth', map_location=torch.device(USE_DEVICE))) model.to(USE_DEVICE) # 2024.02.18 追加 # モデルを推論用に設定する model.eval()・model を CUDA に転送しておく必要がある
「python chapt02_3a.py」【Windows / Linux】CUDA 実行環境の場合のみ発生
(py_learn) PS C:\anaconda_win\workspace_pylearn\chapter02> python chapt02_3a.py Traceback (most recent call last): File "C:\anaconda_win\workspace_pylearn\chapter02\chapt02_3a.py", line 14, in <module> from torchvision import transforms, models File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\site-packages\torchvision\__init__.py", line 6, in <module> from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\site-packages\torchvision\datasets\__init__.py", line 1, in <module> from ._optical_flow import FlyingChairs, FlyingThings3D, HD1K, KittiFlow, Sintel File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\site-packages\torchvision\datasets\_optical_flow.py", line 10, in <module> from PIL import Image File "C:\Users\izuts\anaconda3\envs\py_learn\Lib\site-packages\PIL\Image.py", line 84, in <module> from . import _imaging as core ImportError: DLL load failed while importing _imaging: このオペレーティング システムでは %1 は実行されません。
: from PIL import Image, ImageTk # 2024.02.18 追加 : # 保存しておいたモデルを読み込む #model = models.resnet50(pretrained=False) model = models.resnet50(weights=None) # 2024.02.04 : model.load_state_dict(torch.load('chapt02-model1.pth', map_location=torch.device(USE_DEVICE))) model.to(USE_DEVICE) # 2024.02.18 追加 : #dummy_input = torch.randn(batch_size, *input_shape) # ダミーインプット生成 dummy_input = torch.randn(batch_size, *input_shape, device=USE_DEVICE) # ダミーインプット生成