1. Import Libraries:

import library

Đoạn code sau đây có thể cho phép GPU dùng đến đâu thì chiếm đụng dến đấy. Rất tuyệt nếu trainning nhiều model nhỏ nhỏ.

import os
from keras.layers import Bidirectional,GRU,LSTM,Conv1D,Lambda,BatchNormalization,Activation,Dropout,multiply,add
from keras.models import Input,Model,K#*
from keras.optimizers import adam#*
from keras.callbacks import ReduceLROnPlateau,ModelCheckpoint,EarlyStopping,TensorBoard #*
from datetime import datetime as dt
from os.path import exists,join
import numpy as np
import keras as kr
import matplotlib.pyplot as plt
from keras.layers import multiply,add

########## Using allow_growth on keras with tensorflow ######################################
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.allow_growth = True  # dynamically grow the memory used on the GPU
config.log_device_placement = True  # to log device placement (on which device the operation ran)
                                    # (nothing gets printed in Jupyter, only if you run it standalone)
sess = tf.Session(config=config)
set_session(sess)  # set this TensorFlow session as the default session for Keras
############################################################################################

##

  • import from module:
import imp,sys,time
print(time.asctime())
#-------------------------------------------------------------------------------------
sModules=os.getcwd() +'/Modules'
if not(sModules in sys.path): sys.path.append(sModules)

f, filename, desc = imp.find_module('aiLibs', [sModules]) #import file commons.py in folder Modules
Paths = imp.load_module('aiLibs', f, filename, desc)
# access variable examples:
pdev__375x20=Paths.pdev__375x20
ptest_375x20=Paths.ptest_375x20
ptrain375x20=Paths.ptrain375x20

### CHANGE #####################################################
ModelName='LSTM1Layer'
################################################################
KWS=Paths.KWS
pPrjModel=Paths.pPrjModel.format(ModelName=ModelName)
if not exists(pPrjModel):os.makedirs(pPrjModel)
print('pPrjModel=',pPrjModel)
sCpy="cp  {}.ipynb  {}".format(ModelName, pPrjModel)
print(sCpy)
print()

pHis        = Paths.pHis.format(ModelName=ModelName)
pchkPoint   = Paths.pchkPoint.format(ModelName=ModelName)
plog_dir    = Paths.plog_dir.format(ModelName=ModelName)
pModel      = Paths.pModel.format(ModelName=ModelName)
pbase_model = Paths.pbase_model.format(ModelName=ModelName)
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    2. Load data

    Load data from saved files:

DBs=np.load(pdev__375x20)
DE_MFCCs_Pad    =DBs['MFCCs_Pad']
DE_LBLs         =DBs['LBLs']
DE_LEN_CHARs    =DBs['LEN_CHARs']
DE_CHAR_VECs_Pad=DBs['CHAR_VECs_Pad']
DE_LABEL_KWSs   =DBs['LABEL_KWSs']
All_maxlen_MFCC =float(DBs['All_maxlen_MFCC'])
All_max_charvec =float(DBs['All_max_charvec'])
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    3 Setup Model

    Load model from module

model,base_model=Paths.build_model()
model.save(pModel)
base_model.save(pbase_model)
# -------------------------------------------------------------------
print('"{}":{}'.format(ModelName,'{'))
print('    "pchkPoint"  :"{}",'.format(pchkPoint))
print('    "pModel"     :"{}",'.format(pModel))
print('    "pbase_model":"{}"{},\n'.format(pbase_model,'}'))

load check point-weight:

  • load weight from check point
    initial_epoch=0
    if exists(pchkPoint):
      print('loading check point:',pchkPoint)
      model.load_weights(pchkPoint)
    #     with open(pchkPoint+'.txt','r')as ff: line=ff.read() #ff.write('\nEpoch {}:{}, saving model to {}'.format(epoch + 1,current, filepath))
    #     line=line.splitlines()[-1].split(':')[0][6:]
    #     print('Last trained epoch=',line)
    #     initial_epoch=int(line)
    
  • Load model + weight
    from keras.models import load_model
    bm=load_model(pbase_model)
    bm.summary()
    bm.set_weights(model.get_weights())
    # check:
    for layer in model.layers:
      g=layer.get_config()
      h=layer.get_weights()
      print (g)
      print (h)
    
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

    4. Save - Load model

Đã thử nghiệm thành công.

Save model, load_model

model.save('zzz.h5')
....................
m1=load_model('zzz.h5',compile=False)
m1.summary()
m1.layers.pop() # Remove last layers
m1.layers.pop()
m1.layers.pop()
m1.layers.pop()
m1.summary()
m1.load_weights('/home/u/path/to/chkPoint.pkl')

Save/load models 2

  # Suppose we have a model
from keras.applications import resnet50
model = resnet50.ResNet50(include_top=True, weights='imagenet')
model.load_weights('resnet50_weights_tf_dim_ordering_tf_kernels.h5')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')

  # Import dependencies
import json
from keras.models import model_from_json, load_model

  # Option 1: Save Weights + Architecture
model.save_weights('model_weights.h5')
with open('model_arch.json', 'w') as f:f.write(model.to_json())
  # Option 1: Load Weights + Architecture
with open('model_arch.json', 'r') as f: new_model_1 = model_from_json(f.read())
new_model_1.load_weights('model_weights.h5')

  # Option 2: Save/load the entire model
model.save('my_model.h5')
new_model_2 = load_model('my_model.h5')

S/L model

xem chi tiết tại đây

from keras.models import load_model

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
del model  # deletes the existing model

# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

load model json

# load json and create model
json_file = open('model_num.json', 'r')

loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

# load weights into new model
loaded_model.load_weights("model_num.h5")
print("Loaded model from disk")

model.save('model_num.hdf5')
loaded_model=load_model('model_num.hdf5')

#To predict for different data you can use this

loaded_model.predict_classes("your_test_data here")

Check Save/Load results:

View here

import tensorflow.contrib.keras as keras
from tensorflow.contrib.keras import backend as K
m = train_keras_cnn_model() # Fill in the gaps with your model
model_fn = "test-keras-model-serialization.hdf5"
m_weights = m.get_weights()
keras.models.save_model(m, model_fn)
K.clear_session()
m_load = keras.models.load_model(model_fn)
m_load_weights = m_load.get_weights()
assert len(m_load_weights) == len(m_weights)
for i in range(len(m_weights)):
    assert np.array_equal(m_load_weights[i], m_weights[i])
print("Model weight serialization test passed")
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  • = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

5. Trainning

Custom callback:

Cách khai báo hàm callback và tương tác với model, có thể tham khảo thêm tại đây:

class AccuracyHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.acc = []

    def on_epoch_end(self, batch, logs={}):
        self.acc.append(logs.get('val_acc'))

history = AccuracyHistory()

model.fit(x, y,
          ...
          callbacks=[history])

6. CTC Decode:

7. Matplotlib - plot

random color

Thay giá trị chỗ (i*8)%30 để thấy sự khác biệt

#Ver1.0
%matplotlib inline
import matplotlib.pyplot as plt

def get_cmap(n, name='hsv'):
    '''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
    RGB color; the keyword argument name must be a standard mpl colormap name.'''
    return plt.cm.get_cmap(name, n)

def main():
    N = 30
    fig=plt.figure()
    ax=fig.add_subplot(111)   
    plt.axis('scaled')
    ax.set_xlim([ 0, N])
    ax.set_ylim([-0.5, 0.5])
    cmap = get_cmap(N)
    for i in range(N):
        rect = plt.Rectangle((i, -0.5), 1, 1, facecolor=cmap((i*8)%30))
        ax.add_artist(rect)
    ax.set_yticks([])
    plt.show()

if __name__=='__main__':
    main()

Hoặc cách này dễ hơn, chỉ việc khai bái color vào list là xong:

%matplotlib inline
#ver 2
import matplotlib.pyplot as plt
import numpy as np

colors = "bgrcmykw"
color_index = 0
data=np.random.rand(30,2)
x0,y0=0,0
for X,Y in data:
    plt.plot([0,X],[0,Y], c=colors[color_index%len(colors)])
#     x0,y0=X,Y
    color_index += 1

#ver 2.1
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
data=np.arange(len(marker))+1

colors = "bgrmk"
linstye= ['-','--','-.',':']
marker=['o','v','s','*','+','x','D','d','|','_','.',',','^','<','>','2','3','4','p','1','h']
plt.figure(figsize=(15,6), dpi=80)

for i,(X) in enumerate(data):
    plt.plot([0,X],[0,10], marker[i%len(marker)]+linstye[i%len(linstye)]+colors[i%len(colors)], linewidth=2,label=i)

ax = plt.gca();plt.grid(True)
ax.set_ylim(0, 11)
ax.set_xlim(0, 24)    
plt.legend(fontsize=10, loc='best')

Leave a comment