差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
ディープラーニング入門_2025 [2025/03/19 18:30] – suikou | ディープラーニング入門_2025 [2025/03/24 07:23] (現在) – [練習問題3-1] suikou | ||
---|---|---|---|
行 197: | 行 197: | ||
クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | クラス、インスタンス、モジュール、Colabのランタイム変更、手書き数字の認識 | ||
- | [[https:// | + | [[https:// |
### コメント | ### コメント | ||
行 213: | 行 213: | ||
### 練習問題3-1 | ### 練習問題3-1 | ||
- | `' | + | ``` |
+ | A) `' | ||
+ | B)「初期化時に指定された文字列+何回呼び出されたか」を文字列で返す | ||
+ | ``` | ||
### モジュール | ### モジュール | ||
行 219: | 行 222: | ||
[[https:// | [[https:// | ||
+ | ### 練習問題3-2 | ||
+ | |||
+ | 先ほどのSayCountクラスをsay.pyファイルとして保存して、Google Colabにアップロードし、自作sayモジュールをimportで読み込んで、適当な引数で初期化して使ってみる。 | ||
+ | |||
+ | ### 手書き文字認識 | ||
+ | |||
+ | ``` | ||
+ | # prompt: 手書き文字を認識するプログラムを書いて | ||
+ | |||
+ | !pip install tensorflow | ||
+ | !pip install keras | ||
+ | !pip install pillow | ||
+ | |||
+ | import tensorflow as tf | ||
+ | from tensorflow import keras | ||
+ | from tensorflow.keras import layers | ||
+ | from PIL import Image | ||
+ | import numpy as np | ||
+ | |||
+ | # MNISTデータセットをロード | ||
+ | (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() | ||
+ | |||
+ | # データの前処理 | ||
+ | x_train = x_train.astype(' | ||
+ | x_test = x_test.astype(' | ||
+ | y_train = keras.utils.to_categorical(y_train, | ||
+ | y_test = keras.utils.to_categorical(y_test, | ||
+ | |||
+ | # モデルの構築 | ||
+ | model = keras.Sequential([ | ||
+ | layers.Flatten(input_shape=(28, | ||
+ | layers.Dense(128, | ||
+ | layers.Dense(10, | ||
+ | ]) | ||
+ | |||
+ | # モデルのコンパイル | ||
+ | model.compile(optimizer=' | ||
+ | loss=' | ||
+ | metrics=[' | ||
+ | |||
+ | # モデルの学習 | ||
+ | model.fit(x_train, | ||
+ | |||
+ | # モデルの評価 | ||
+ | test_loss, test_acc = model.evaluate(x_test, | ||
+ | print(' | ||
+ | |||
+ | # 手書き文字の認識 | ||
+ | def recognize_handwritten_digit(image_path): | ||
+ | img = Image.open(image_path).convert(' | ||
+ | img_array = np.array(img) | ||
+ | img_array = img_array.astype(' | ||
+ | img_array = np.expand_dims(img_array, | ||
+ | prediction = model.predict(img_array) | ||
+ | digit = np.argmax(prediction) | ||
+ | return digit | ||
+ | |||
+ | # 手書き文字画像のパスを指定して認識 | ||
+ | image_path = ' | ||
+ | recognized_digit = recognize_handwritten_digit(image_path) | ||
+ | print(' | ||
+ | ``` | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Mac用のペイントソフト: | ||
+ | |||
+ | {{2.png}} | ||
+ | |||
+ | 手書き文字は黒字に白文字で書くこと! | ||
+ | |||
+ | ### Colabのランタイム変更 | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | 参考:T4のGPUカードのお値段 | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ### 畳み込みニューラルネットワーク(CNN) | ||
+ | |||
+ | 動物の脳が光情報を処理するメカニズム [[https:// | ||
+ | |||
+ | CNNについて [[https:// | ||
+ | |||
+ | ### 最終課題 | ||
+ | |||
+ | CNNを利用して手書き文字を認識するプログラムを作る。いくつか手書き文字を作成し、実際の正答率を調べてみる。 | ||