013-01.手書き文字-日本語の認識モデルの作成

サイトの紹介と使い方動画


created: 2021/10/02 modified: 2021/10/29

概要

  1. 手書き文字の認識モデルを構築します。

モデルの作成

1グループ対応

共有変数と関数-c001.py

c001.手書き文字-日本語-共有ソースを参照してください。

数字(numbers):011-02.py
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D
from tensorflow.keras.layers import Activation,Dropout,Flatten,Dense
from tensorflow.keras.utils import to_categorical

import c001_env as env

def build_model( x_train,nb_classes ):
    model = Sequential(
        [ 
         Conv2D( 128,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-11' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 128,3,activation='relu',name='Conv-12' ),
         Dropout( 0.3 ),

         Conv2D( 64,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-21' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 64,3,activation='relu',name='Conv-22' ),
         Dropout( 0.2 ),

         Conv2D( 32,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-31' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 32,3,activation='relu',name='Conv-32' ),
         Dropout( 0.1 ),

         Flatten(),
         Dense( nb_classes, activation='softmax',name='Dense-class' )
        ] )

    return model

def main():
    x_train = np.load( env.npy_x_train,allow_pickle=True)
    y_train = np.load( env.npy_y_train,allow_pickle=True)
    x_train = x_train.astype( 'float32' ) /255
    y_train = to_categorical( y_train,env.nb_classes )

    x_test,y_test = env.load_img( env.test_dir )
    x_test = x_test.astype( 'float32' ) /255
    y_test = to_categorical( y_test,env.nb_classes )

    model = build_model( x_train,env.nb_classes )
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
    history = model.fit(x_train,y_train, batch_size=5, epochs=20, validation_data=(x_test, y_test),verbose=1 )

    json_string = model.to_json()
    open( env.fs_json,'w').write(json_string )
    model.save_weights( env.fs_hdf5 )

    print( 'success' )

main()
カタカナ(katakana):012-02.py
  1. 数字(numbers)を変更
1.    history = model.fit(x_train,y_train, batch_size=20, epochs=100, validation_data=(x_test, y_test),verbose=1 )
変更箇所:batch_size=20, epochs=100
アイウエオカキクケコサシスセソタツテト(チは似た文字対象のため除外)
loss: 9.2774e-04 - acc: 1.0000 - val_loss: 1.0692 - val_acc: 0.9200

Family対応 :012-51.py

共有変数と関数-c002.py

c001.手書き文字-日本語-共有ソースを参照してください。

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D
from tensorflow.keras.layers import Activation,Dropout,Flatten,Dense
from tensorflow.keras.utils import to_categorical

import c002_env as env

def build_model( x_train,nb_classes ):
    model = Sequential(
        [ 
         Conv2D( 512,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-11' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 128,3,activation='relu',name='Conv-12' ),
         Dropout( 0.3 ),

         Conv2D( 64,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-21' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 64,3,activation='relu',name='Conv-22' ),
         Dropout( 0.3 ),

         Conv2D( 32,3,input_shape=x_train.shape[1:],activation='relu',name='Conv-31' ),
         MaxPooling2D( pool_size=(2, 2) ),
         Conv2D( 32,3,activation='relu',name='Conv-32' ),
         Dropout( 0.3 ),

         Flatten(),
         Dense( nb_classes, activation='softmax',name='Dense-class' )
        ] )

    return model

def main():
    x_train = np.load( env.npy_x_train,allow_pickle=True)
    y_train = np.load( env.npy_y_train,allow_pickle=True)
    x_train = x_train.astype( 'float32' ) /255
    y_train = to_categorical( y_train,env.nb_classes )

    x_test,y_test = env.load_img2( '/test/' )
    x_test = x_test.astype( 'float32' ) /255
    y_test = to_categorical( y_test,env.nb_classes )

    model = build_model( x_train,env.nb_classes )
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
    history = model.fit(x_train,y_train, batch_size=20, epochs=100, validation_data=(x_test, y_test),verbose=1 )

    json_string = model.to_json()
    open( env.fs_json,'w').write(json_string )
    model.save_weights( env.fs_hdf5 )

    print( 'success' )

main()

前の記事:012-01.手書き文字-日本語の文字画像をnumpyに変換
次の記事:014-01.手書き文字-日本語の文字を外部評価-教師有り

お問合せ・御要望

  • お問合せ
  • トップへ戻る
    タイトルとURLをコピーしました