以下のDL先からStable Releasesの最新版の「Windows installer(64-bit)」をDLする
https://www.python.org/downloads/windows/
DLしたインストーラを実行すると以下の画面になるので、必要な個所を選択してインストールを行う
C:\temp\Python\d_object>python --version Python 3.10.6
MXNetはPythonを含めた複数のプログラミング言語から利用できる深層学習フレームワークをインストールする
コマンドプロンプトから以下のコマンドを実行する
MXNetはCPU用とGPU用がある。今回はCPU用をインストールする
''pip install mxnet -f https://dist.mxnet.io/python/cpu''
> cd C:\temp\Python\d_object C:\temp\Python\d_object>pip install mxnet -f https://dist.mxnet.io/python/cpu Looking in links: https://dist.mxnet.io/python/cpu Collecting mxnet Downloading https://repo.mxnet.io/dist/python/cpu/mxnet-1.8.0-py2.py3-none-win_amd64.whl (20.7 MB) ---------------------------------------- 20.7/20.7 MB 7.3 MB/s eta 0:00:00 Collecting requests>=2.18.4 Downloading requests-2.28.1-py3-none-any.whl (62 kB) ---------------------------------------- 62.8/62.8 kB 3.3 MB/s eta 0:00:00 (略) Collecting idna<4,>=2.5 Downloading idna-3.3-py3-none-any.whl (61 kB) ---------------------------------------- 61.2/61.2 kB ? eta 0:00:00 Installing collected packages: urllib3, numpy, idna, graphviz, charset-normalizer, certifi, requests, mxnet Successfully installed certifi-2022.6.15 charset-normalizer-2.1.0 graphviz-0.20.1 idna-3.3 mxnet-1.8.0 numpy-1.23.2 requests-2.28.1 urllib3-1.26.11 [notice] A new release of pip available: 22.2.1 -> 22.2.2 [notice] To update, run: python.exe -m pip install --upgrade pip
C:\temp\Python\d_object> python.exe -m pip install --upgrade pip Requirement already satisfied: pip in c:\usr\python\python310\lib\site-packages (22.2.1) Collecting pip Downloading pip-22.2.2-py3-none-any.whl (2.0 MB) ---------------------------------------- 2.0/2.0 MB 10.0 MB/s eta 0:00:00 Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 22.2.1 Uninstalling pip-22.2.1: Successfully uninstalled pip-22.2.1 Successfully installed pip-22.2.2
GluonCV とは、ディープラーニングのフレームワーク MXNet が提供するコンピュータビジョン用のインターフェースになります。
C:\temp\Python\d_object>pip install gluoncv --upgrade Collecting gluoncv Downloading gluoncv-0.10.5.post0-py2.py3-none-any.whl (1.3 MB) ---------------------------------------- 1.3/1.3 MB 8.9 MB/s eta 0:00:00 Collecting matplotlib Downloading matplotlib-3.5.3-cp310-cp310-win_amd64.whl (7.2 MB) ---------------------------------------- 7.2/7.2 MB 8.8 MB/s eta 0:00:00 (略) Installing collected packages: pywin32, pytz, six, scipy, pyyaml, pyparsing, \ portalocker, Pillow, opencv-python, kiwisolver, fonttools, cycler, colorama, \ yacs, tqdm, python-dateutil, packaging, autocfg, pandas, matplotlib, gluoncv Successfully installed Pillow-9.2.0 autocfg-0.0.8 colorama-0.4.5 cycler-0.11.0 \ fonttools-4.35.0 gluoncv-0.10.5.post0 kiwisolver-1.4.4 matplotlib-3.5.3 \ opencv-python-4.6.0.66 packaging-21.3 pandas-1.4.3 portalocker-2.5.1 \ pyparsing-3.0.9 python-dateutil-2.8.2 pytz-2022.2.1 pywin32-304 pyyaml-6.0 \ scipy-1.9.0 six-1.16.0 tqdm-4.64.0 yacs-0.1.8
写真に写っている車とバイクの数を数えるプログラム
#写真の中の2種類の画像(car,motorcycle)の数を数える
import mxnet as mx from gluoncv import model_zoo, data, utils # url = 'https://cdn-ak.f.st-hatena.com/images/fotolife/t/touch-sp/20190814/20190814122423.jpg' filename = '02.jpg' # utils.download(url, filename) net = model_zoo.get_model('faster_rcnn_fpn_resnet101_v1d_coco', pretrained=True, root='./models') net.reset_class(['car'], reuse_weights=['car']) """ すべての画像を読み込み、正規化を適用してテンソルに変換するユーティリティ関数 この関数は 2 つの結果を返します。1 つ目は、形状 (batch_size, RGB_channels, height, width) を持つ NDArrayです。 モデルに直接入力できます。2番目のものには、簡単にプロットできるようにnumpy形式の画像が含まれています。 単一の画像のみを読み込んだため、xの最初の次元は 1 です。https://cv.gluon.ai/api/data.transforms.html """ x, img = data.transforms.presets.rcnn.load_test(filename) class_IDs, scores, bounding_boxs = net(x) count = int(mx.nd.sum(scores[0]>0.9).asscalar()) #-------------- net = model_zoo.get_model('faster_rcnn_fpn_resnet101_v1d_coco', pretrained=True, root='./models') net.reset_class(['motorcycle'], reuse_weights=['motorcycle']) x, img = data.transforms.presets.rcnn.load_test(filename) class_IDs, scores, bounding_boxs = net(x) count2 = int(mx.nd.sum(scores[0]>0.6).asscalar()) print('car = ', count) print('motorcycle= ', count2)
インストール初回実行時はモジュールのDLが始まる
D:\Temp\Python\d_object>python d_object2.py ----(以下のWarningは無視していいみたい)---- C:\Usr\Python\Python310\lib\site-packages\mxnet\gluon\block.py:1591: UserWarning: Cannot decide type for the following arguments. Consider providing them as input: C:\Usr\Python\Python310\lib\site-packages\mxnet\gluon\block.py:1591: UserWarning: Cannot decide type for the following arguments. Consider providing them as input: ----(ここまで)----- data: None input_sym_arg_type = in_param.infer_type()[0] ----(ここからモジュールのDL)---- Downloading ./models\faster_rcnn_fpn_resnet101_v1d_coco-1194ab4e.zip from \ https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/models/faster_rcnn_fpn_resnet101_v1d_coco-1194ab4e.zip... 231264KB [01:11, 3222.49KB/s] ----(DLここまで)----
car = 4 motorcycle= 2
&ref(): File not found: "02.jpg" at page "Windows_Router Memo/Python Install (Windows)";
from gluoncv import model_zoo net = model_zoo.get_model('faster_rcnn_fpn_resnet101_v1d_coco', pretrained=True, root='./models') print('検出種類', net.classes) print('検出種類数' ,len(net.classes))
python howtype.py C:\Usr\Python\Python310\lib\site-packages\mxnet\gluon\block.py:1591: UserWarning: Cannot decide type for the following arguments. Consider providing them as input: data: None input_sym_arg_type = in_param.infer_type()[0] 検出種類 ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck',\ 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench',\ 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', \ 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', \ 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', \ 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', \ 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', \ 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', \ 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', \ 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', \ 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] 検出種類数 80
C:\Usr\Python\Python310\lib\site-packages\mxnet\gluon\block.py:1591: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
data: None
C:\temp\Python\d_object>python verchk.py mxnet version 1.8.0