Skip to Content
Deep Learning 向けの PyTorch プログラミング
book

Deep Learning 向けの PyTorch プログラミング

by Ian Pointer
May 2025
Intermediate to advanced
220 pages
3h 27m
Japanese
O'Reilly Media, Inc.
Content preview from Deep Learning 向けの PyTorch プログラミング

第8章. 制作におけるPyTorch

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

さて、PyTorchを使って画像、テキスト、音声を分類する方法を学んだので、次はPyTorchアプリケーションを本番環境でデプロイする方法を見ていこう。この章では、HTTPとgRPCでPyTorchモデルの推論を実行するアプリケーションを作成する。そして、それらのアプリケーションをDockerコンテナにパッケージし、Google Cloud上で動作するKubernetesクラスタにデプロイする。

後半では、PyTorch 1.0で導入された新技術であるTorchScriptについて見ていく。TorchScriptは、ジャストインタイム(JIT)トレースを使って、C++から実行できる最適化されたモデルを生成することができる。また、量子化を使ってモデルを圧縮する方法についても簡単に説明する。最初に、モデルサービングを見てみよう。

モデル・サービング

これまでの6章ではPyTorchでモデルを構築してきたが、モデルの構築はDeep Learningアプリケーション構築の一部に過ぎない。結局のところ、モデルは驚くべき精度(または他の関連メトリック)を持っているかもしれないが、もしそれが何の予測もしなければ、何の価値もないのだろうか?我々が求めているのは、モデルを簡単にパッケージ化し、(後述するようにウェブやその他の手段で)リクエストに応答できるようにし、最小限の労力で本番環境で実行できるようにすることだ。

ありがたいことに、Pythonを使えば、Flaskフレームワークを使ってウェブサービスを素早く立ち上げて実行することができる。このセクションでは、ResNetベースの猫と魚のモデルをロードし、画像のURLを含むリクエストを受け付け、画像に猫と魚のどちらが含まれているかを示すJSONレスポンスを返すシンプルなサービスを構築する。

モデルに犬の写真を送ったらどうなるか?モデルは魚か猫だと言うだろう。モデルには利用可能な選択肢以外の概念はなく、常にどちらかを選ぶ。Deepラーニングの実践者の中には、トレーニング中に追加のクラス(Unknown )を追加し、必要なクラスのいずれでもないラベル付き例を投入する人もいる。これはある程度うまくいくが、本質的にニューラルネットに猫や魚でないものをすべて学習させようとするもので、一連の行列計算はおろか、あなたや私にも表現するのは難しい!もうひとつの選択肢は、最終的なsoftmax によって生成された確率出力を見ることだ。もしモデルが、猫と魚がほぼ半々、あるいはクラス全体に分散した予測を出しているのであれば、Unknown

Flaskサービスを構築する

Webサービス対応版のモデルを立ち上げて動かしてみよう。FlaskはPythonでWebサービスを作成するための有名なフレームワークで、この章ではこれをベースとして使うことにする。pip またはconda のどちらかを使ってFlaskライブラリをインストールする:

conda install -c anaconda flask
pip install flask

catfishという新しいディレクトリを作成し、モデル定義をmodel.pyとしてコピーする:

from torchvision import models

CatfishClasses = ["cat","fish"]

CatfishModel ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成 Deep Learning 第2版 ―絵を描き、物語や音楽を作り、ゲームをプレイする

生成 Deep Learning 第2版 ―絵を描き、物語や音楽を作り、ゲームをプレイする

David Foster, 松田 晃一, 小沼 千絵
直感 Deep Learning ―Python×Kerasでアイデアを形にするレシピ

直感 Deep Learning ―Python×Kerasでアイデアを形にするレシピ

Antonio Gulli, Sujit Pal, 大串 正矢, 久保 隆宏, 中山 光樹

Publisher Resources

ISBN: 9798341650367