# Fileset

[source_codes_data.zip](https://mdr.nims.go.jp/filesets/c293efbf-97e6-4e10-ab66-f7df4be090dd/download)

## Creator

[YOSHITAKE, Michiko](https://orcid.org/0000-0002-0973-5666), KONO, Takashi, [KADOHIRA, Takuya](https://orcid.org/0000-0003-0569-1309)

## Rights

In Copyright[In Copyright](http://rightsstatements.org/vocab/InC/1.0/)

## Other metadata

[Program for automatic numerical conversion of a line graph (line plot)](https://mdr.nims.go.jp/datasets/6c5dabcb-ae16-4c15-9054-c3240c184b32)

## Fulltext

source_codes_data/common_para_1024_2006251034.json{    "para": [        1024,        360,        2.0,        8.0,        [            0.8,            0.9,            1.0,            1.13,            1.25        ],        [            50,            80,            6        ]    ],    "asof": "2006251034"}source_codes_data/common_para_360_2006251156.json{    "para": [        360,        180,        1.0,        6.0,        [            0.8,            0.9,            1.0,            1.13,            1.25        ],        [            20,            30,            3        ]    ],    "asof": "2006251156"}source_codes_data/Data_Digitizer.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jan.18,2020 / Fixed on Jun.30,2020< 波形を数値化するための機械学習データを作成 >@author: T.Kono"""import cv2import numpy as npimport matplotlib.pyplot as pltimport sys,  string, randomimport pickle, timeimport jsonfrom datetime import datetimefrom keras.preprocessing.image import img_to_arrayimport kn_lib.line as line# ----------------------------------------------------------------------------------------------##           Data Creating of Machine Learning for Waveform Digitizer## ----------------------------------------------------------------------------------------------# --- Opening ---------------------------------------------# Date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')# start commentprint("\n< Data Creating of Machine Learning for Waveform Digitizer ",today,'>')# --- Parameter input and define ------------------------------# data dimensiondim = 1024 # dim = int(input(' Input Dimension of data => '))# data number inputprint('\n Input data number. if <= 99, datas are not saved')loop    = int(input(' => '))# line number inputli_n    = int(input(' Input Line number    = '))comment = 'Digitizer_Data_'+str(li_n)+'-lin_'# common parameter'''同じdimの学習データは画像のパラメータを共有させる'''com_n = 'common_para_1024_2006251034.json'with open(com_n, mode='r') as f:    com_para = json.load(f)# parameter importndpi     = com_para['para'][1]lw       = com_para['para'][2]f_s      = com_para['para'][3]weigt    = com_para['para'][4]g_val    = com_para['para'][5]'''g_val[0][1]　はマーカー間隔の最小値と最大値(pixel) g_val[2]はマーカーの大きさ'''pix_poit = round(ndpi/72)       # pixel number of pointww       = dim/ndpi ; hh = ww   # graph width (inchs)# peak number'''1つの波形を作るために重ね合わせるガウス関数の数'''cho_p = [2,3,4]# disturbance width'''[0][1]:周波数摂動幅の上限下限[2][3]:ピークの尖度摂動幅の上限下限[4][5]:ライン間隔摂動幅の上限下限ライン本数が3以下は摂動幅大きく,ライン本数が4以上は小さくする'''large = [-30,130,         50,200,         10,500]small = [-5,105,         20,100,         30,100]if li_n <= 3 :    lt     = largeelse :    lt     = small# marker settingmarker = ['+','D','^','o','s','v','x']''' maker sample['o','s','v']['+','D','^','o','s','v','x']['*','+',',','.','8','<','>','D','H','^','_',\    'd','h','o','p','s','v','x','|']'''# --- Parameter print and comfirmation -------------------------print('\n < Data parameter >')print(' common parameter =',com_n)print('\n Data number =',loop )print(' line number =',li_n)print(' line peak   =', cho_p)print(' dim         =',dim, ', dpi =',ndpi,      ' , fig size  =','{:3.3}'.format(ww),      'x','{:3.3}'.format(ww),'inch')print(' line width  =',lw,'p','\n font size   =',f_s,'p')print(' marker      =',marker)print('\n comment :',comment)yn = input(' Confirm data parameter, process start OK? y or n --> ')if yn == 'n' :    print('Parameter is wrong') ;sys.exit()print('\n Process start ...'); s_time = time.time()# --- Data Create Loop -------------------------# initial arry setima_data   = []out_data   = []arrayimage = np.zeros([dim,dim,3],dtype=np.int8)xxyy       = np.zeros([2])# mail loopfor l in range(0,loop):    plt.close()    fig = plt.figure(figsize=(ww,hh), dpi=ndpi)    # marker on/off    mk_on_off = random.choice([1,0])    # Base line create    p_n  = np.random.choice(cho_p)    ff  = line.line_n(dim, p_n)    fu  = ff[0]    yn  = ff[1]    pf  = ff[2]    pk  = ff[3]    ofs = ff[4]    weigt_ = []    yy     = []    y_m    = 0    # ライン間隔をランダムに変える    d_of = (np.random.randint(lt[4],lt[5]))/1000    for j in range(0,li_n+1):        y      = np.zeros([dim])        for i in range(0,p_n):            '''ベースとなる波形パラメータから下記のパラメータをランダムに            摂動させて波形を生成            '''            # 周波数の摂動量            d_f  = (np.random.randint(lt[0],lt[1]))/1000            # 尖度の摂動量            d_k  = (np.random.randint(lt[2],lt[3]))/100            # 1つのピークを持った波形の生成            y_add  = np.exp(-((fu-pf[i]+d_f)**2)/                                        ((2*(pk[i]*d_k)**2))                                    )            # p_n個のピーク波形の重ね合わせ            y   = y + y_add + d_of        # オフセットを加えて波形を収納        yy.append(y + np.full(dim,d_of)*j)    yy   = np.asarray(yy)    ymax = np.max(yy)    '''# 最小値がX軸に張り付かないための調整    ymin = np.min(yy)-(np.random.randint(5,20))/100    '''    # レンジをを0-1に入れるための調整    yn   = yy/(ymax*np.random.randint(101,            150)/100)    # 教師データを取得    out  = (dim - (yn[0:li_n,:]*(dim-1))).astype(np.int32)    # draw scale randame choice    scale = np.random.choice(weigt,                             4)/(np.random.randint(75,                              150)/100)    # figure position    ax    = fig.add_axes([0,0,1,1])    # marker style    if mk_on_off == 1 and li_n <=3 :      mak_   = np.sort(np.random.choice(marker,                                        li_n+1,                                        replace=False)                       )    else:      mak_ =['','','','','','','']    # maker の間隔を設定範囲内でランダムに変える    dif   = np.random.randint(g_val[0],g_val[1])    # legend position    lg_x = (np.random.randint(100,900))/1000    lg_y = (np.random.randint(100,900))/1000    # 3－9個のラベル文字をランダムに生成する    moj   = np.random.randint(3,9)    d_n=''.join(random.choices(string.ascii_letters, k=moj))    # line drowing loop    for k in range(0, li_n+1):        if mk_on_off == 1 :            '''マーカーがある場合は線種は実線で固定            マーカーがない場合は線種はランダムに選択する            '''            ls_='solid'        else :            ls_    = np.random.choice(['-','-.',':','--'])        '''li_n番目 はダミーライン        ダミーラインの線とマーカを白に,レジェンドのラベルを無効        '''        l_col = 'k'        lab   = d_n+str(k)        if k ==  li_n :            l_col = 'w'            lab   = ''            fc_   = 'w'            ms    =  5        else :            '''マーカーの白抜きと黒塗りをランダムに選択            '''            fc_ = np.random.choice(['w','auto'])        ax.plot(fu, yn[k,:],                    l_col,                    ls        = ls_,                    linewidth = lw*scale[0],                    label     = lab,                    markevery = dif,                    marker = mak_[k],                    mfc    = fc_ ,                    mec    = l_col,                    ms     = g_val[2]*scale[1]                    )    # tic drawing    sel  = np.random.choice( ['in','out'] )     # 目盛線の方向    sel2 = np.random.choice( [True, False] )    # Y軸の目盛り数値のON/OFF    plt.tick_params(width      = lw*scale[0],                    length     = 8*scale[1],                    direction  = sel,                    labelleft  = sel2                    )    # legend drawing    fr_ch = np.random.choice([1,0])    ax.legend(loc        = (lg_x,lg_y),              fontsize   = f_s*scale[2]*0.8,              framealpha = fr_ch,              frameon    = 'False'              )    # outer frame off    ax.spines["right" ].set_color('none')    ax.spines["top"   ].set_color('none')    ax.spines["left"  ].set_color('none')    ax.spines["bottom"].set_color('none')    plt.ylim(0,1) ; plt.xlim(0,1)    # Buffer からfigureデータを取得    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    flg = l%3    if flg == 0 :        '''3個に1個の割合でデータの前後にランダムな空白を挿入        '''        of_x = np.random.randint(0,dim*0.10,2)        cv2.rectangle(imar,(0,0),(of_x[0],dim),                        (255,255,255),-1)  # left        cv2.rectangle(imar,(dim-of_x[1],0),(dim,dim),                        (255,255,255),-1)  # right    gray_image  = cv2.cvtColor(imar,cv2.COLOR_BGR2GRAY)    arrayimage  = img_to_array(gray_image).astype(np.uint8)    ima_data.append(arrayimage)    out_data.append(out)# 学習データima_data = np.asarray(ima_data)# 教師データout_data = np.asarray(out_data)# Execution timee_time = time.time()print(' ...Finished. Execution time (s) = ',      '{:6.2f}'.format(e_time - s_time) )# ---- Figure check -------------------------------------plt.close()print('\n < Figure check >')n      = 20fig    = plt.figure(figsize=(12,15))n_list = np.random.randint(0,len(ima_data),n)print(' . sample number = ',n_list)for i in range(n):    nn = n_list[i]    ax1 = plt.subplot(5,4,i+1)    ax1.imshow(np.reshape(ima_data[nn,:],(dim,-1)),cmap='gray')    # plt.gray()    ax1.get_xaxis().set_visible(False)    ax1.get_yaxis().set_visible(False)plt.show()# ---- Data check ----------------------------------------print('\n < Figure and Data comparison > ')n      = 4fig    = plt.figure(figsize=(10,6))n_list = np.random.randint(0,len(ima_data),n)print(' . sample number = ',n_list)print(' . upper = input, lower = output')for i in range(n):    nn = n_list[i]    ax1 = plt.subplot(2,n,i+1)    ax1.imshow(np.reshape(ima_data[nn,:],(dim,-1)),cmap='gray')    ax1.get_xaxis().set_visible(False)    ax1.get_yaxis().set_visible(False)    ax = plt.subplot(2, n, i+1+n)    for ii in range(0,li_n):        plt.plot(fu,dim-out_data[nn,ii,:],'--')    plt.ylim(0,dim-1) ; plt.xlim(0,1)    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# Special exit'''データの点数が99個以下は、データの保存は行わず強制終了する'''if loop <= 99:    print('\n *** Datas were NOT saved, because Data numbers < 100.')    sys.exit()# --- Data save -------------------------------------------axis_test_graf = {'data':ima_data,                  'out':out_data,                  'para':(dim,                          ndpi,                          comment,                          li_n,                          com_n,                          marker                          )                           }data_n = './'+comment+str(dim)+'_'+str(loop)+'_'+str(today)+'.pik'with open(data_n,'wb') as f1:    pickle.dump(axis_test_graf,                f1,                protocol = 4                )print('\nData was saved >>> \n',data_n)# del large memorydel ima_data; del out_data# ___________________________＿＿＿ END OF CODE ＿_____________________________________source_codes_data/Data_FigSelection.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Oct.16,2019 / Fixed on Jun.30,2020＜ csv-file の判別結果をもとに画像選別の機械学習データを作成 ＞@author: T.KONO"""import numpy as npimport pandas as pdimport pickleimport matplotlib.pyplot as pltimport cv2from datetime import datetime# ------------------------------------------------------------------------------##             Data Creating of Machine Learning for FigSelection#             - from jadging data of csv-file##-------------------------------------------------------------------------------# --- Opning -----------------------------------------------# date and timenow = datetime.today().strftime('%y%m%d-%H%M')# start commentprint('\n< Data Creating of Machine Learning for FigSelection', now,'>')# --- CSV Read --------------------------------------------# dimansiondim  = 256# Jadging data readdir_n = '/media/tkono/USB_115G/TDM-PNG_n2_n4207/'d_n   = 'OK-NG_test'fd_n  = dir_n+d_n+'.csv'print('\n Dimendion = ',dim,      '\n Data directory :',dir_n,      '\n Judge List :    ',d_n+'.csv')# データ読み出し(欠損値は０に置換)df    = pd.read_csv(fd_n).fillna(0)# 判別結果を行列にar_d =df[['OK1/NG0']].values# --- Data Create --------------------ng_out  = []ng_ima  = []ng_list = []ok_out  = []ok_ima  = []ok_list = []loop = 0for f_n in df['name']:    # file name    # f_name  = dir_n+f_n+'.png'    f_name  = dir_n+f_n    # File Open    im_ = cv2.imread(f_name,0) # gray image    sz  = im_.size    # Resise    re_ = cv2.resize(im_,(dim,dim),cv2.INTER_LANCZOS4)    # array    arrayimage  = np.asarray(re_).astype(np.uint8)    arrayimage  = arrayimage.reshape([dim,dim,-1])    # data append    fg = float(ar_d[loop,0])    if 1.1 > fg  > 0.4 :        '''曖昧な画像を0.5と判定しているのでこれもOKに分類する        '''        ok_ima.append(arrayimage)        ok_out.append(1)        ok_list.append(loop)    else :        ng_ima.append(arrayimage)        ng_out.append(0)        ng_list.append(loop)    loop=loop+1ok_ima = np.asarray(ok_ima)ok_out = np.asarray(ok_out)ng_ima = np.asarray(ng_ima)ng_out = np.asarray(ng_out)#--- Data Check --------------------------------------------fig=plt.figure(figsize=(10,5))n = 8n_list= np.random.choice(len(ng_list), n)print('\n< NG figure Sample >', n_list)for i in range(n):    nn = n_list[i]    ax = plt.subplot(2,4, i+1)    plt.imshow(np.reshape(ng_ima[nn,:],(dim,- 1)))    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()fig=plt.figure(figsize=(10,5))n = 8n_list= np.random.choice(len(ok_list), n)print('\n< OK figure Sample >', n_list)for i in range(n):    nn = n_list[i]    ax = plt.subplot(2,4, i+1)    plt.imshow(np.reshape(ok_ima[nn,:],(dim,- 1)))    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()print('< Results >\n','Total = ',len(df),      '\n    OK = ',len(ok_out),      '\n    NG = ',len(ng_out)      )# Learning datax  = np.concatenate([ok_ima, ng_ima], axis=0)# Teaching datay  = np.concatenate([ok_out, ng_out], axis=0)# --- Data save --------------------------------------------fg = input('Data save ? y or n --> ')if fg == 'y' :    test_data = {'in':x,                 'out':y,                 'para':(dim,str(len(x)))                 }    data_n = './data/FigSerection_'+str(dim)+'_'+\    str(len(x))+'_'+str(now)+'.pik'    with open(data_n,'wb') as f1:        pickle.dump(test_data, f1, protocol=4)    print(' Data was saved :',data_n)else :    print(' Data was NOT saved. ')# ______________________________ END OF CODE ___________________________________source_codes_data/Data_LineNumber.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Nov.27,2019 / Fixed on Jun.30.2020< ライン本数読み取りのための機械学習データ作成 >@author: T.Kono"""import cv2import numpy as npimport matplotlib.pyplot as pltimport sys,  string, randomimport pickle, timefrom datetime import datetimeimport jsonfrom keras.preprocessing.image import img_to_arrayimport digtizer_lib.line as line# ------------------------------------------------------------------------------##         Data Creating of Machine Learning for LineNumber## ------------------------------------------------------------------------------# --- Opning --------------------------------------------------------# date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')# tytleprint('\n< Data Creating of Machine Learning for LineNumber.',today,'>')comment = 'LineNumber_Data_'# --- Data parameter define -----------------------------------------# data dimension(360x360 fixed)dim =360#input data numberprint('\nInput data number. if <= 99, datas are not saved')loop    = int(input(' ==> '))# common parametercom_n   = 'common_para_360_2006251156.json'with open(com_n, mode='r') as f:    com_para = json.load(f)# parameter importndpi     = com_para['para'][1]axis_lw  = com_para['para'][2]f_s      = com_para['para'][3]weigt    = com_para['para'][4]g_val    = com_para['para'][5]'''g_val[0][1]　はマーカー間隔の最小値と最大値(pixel) g_val[2]はマーカーの大きさ'''pix_poit = round(ndpi/72)       # pixel number of pointww       = dim/ndpi ; hh = ww   # graph width (inchs)# image type''' gray に固定im_type = input(' Input image type, g(gray) or m(mono)  => ')'''im_type = 'g'if im_type == 'g':    imt = 'L';i_t='gray'elif im_type == 'm':    imt = '1';i_t='mono'else :    print('Image type is wrong') ;sys.exit()# line type''' cross に固定line_type =  input(' Input line type, c(cross) or p(parallel)  => ')'''line_type = 'c'if line_type == 'c':    of_weight = [0.1,0.2,0.5,0.8];l_t='cross'elif line_type == 'p':    of_weight = [1,1.2,1.5,2];l_t='para'else :    print('Line type is wrong') ;sys.exit()# マーカーの設定marker = ['+','D','^','o','s','v','x','_']''' marker sample['o','s','v']['+','D','^','o','s','v','x']['*','+',',','.','8','<','>','D','H',\'^','_','d','h','o','p','s','v','x','|']'''mak_l=len(marker)# p_weightは各ラインのピークの大きさを変化させるp_weight  = [0.8, 0.9, 1.1, 1.2, 1.3]# Line numbercho_l = [1,2,3,4,5,6]# Peak number (重ね合わせるガウス関数の数)cho_p_l = [1,2,3]cho_p_h = [3,4,5]# --- Parameter comfirmation ----------------------------------------print('\n < Data parameter >')print(' common parameter     = ',com_n)print(' dim                  = ',dim,      '\n dpi                  = ',ndpi,      '\n data number          = ',loop )print(' Graph size           = ','{:3.3}'.format(ww),      'x','{:3.3}'.format(ww),'inch')print(' Line Number          = ',cho_l)print(' Line peak number     =  w/m',cho_p_l,      ', wo/m ', cho_p_h)print(' Line width(normal)   = ',axis_lw,      'point(',axis_lw*ndpi/72,'pixel)')print(' Font size(normal)    = ',f_s)print(' Marker number        = ',len(marker),marker)print(' Marker size(normal)  = ',g_val[2],      '\n Pitch of marker      = ',      g_val[0],'-',g_val[1],      '\n scale factor         = ',weigt)print('\n comment = ',comment)yn = input(' Data OK? y or n --> ')if yn == 'n' :    print('Parameter is wrong') ;sys.exit()print('\n Process start ...'); s_time = time.time()# --- Data create ---------------------------------------------------# arry initial setima_data   = []out_data   = []arrayimage = np.zeros([dim,dim,3],dtype=np.int8)xxyy       = np.zeros([2])# Data create loopfor l in range(0,loop):    # marker on/off    mk_on_off = random.choice([1,0])    # ラインの本数をランダムに選択する    li_n = np.random.choice(cho_l)    '''マーカーの有無とラインの本数により波形のピーク数(ガンマ関数の重ね合わせ)を変える.    マーカーを描く場合もしくはラインの本数が3本以上では少なく、マーカーを描かないかラインの本数    が3本以下の場合は多くする    '''    cho_p =  cho_p_h    if mk_on_off == 1 or li_n >3:        cho_p =  cho_p_l    # ピークの数(ガンマ関数の重ね合わせの数)をランダムに選択する    p_n  = np.random.choice(cho_p)    plt.close()    fig     = plt.figure(figsize = (ww, hh), dpi=ndpi)    xy   = [0.0, 0.0]  # origin position    xxyy = [1.0, 1.0]  # axis length    # ベースとなる波形の生成    ff  = line.line_n(dim, p_n)    fu  = ff[0]    yn  = ff[1]    pf  = ff[2]    pk  = ff[3]    g   = ff[4]    ofs = ff[5]    weigt_ = []    y      = np.zeros([dim])    yy     = []    for j in range(0,li_n+1):        ''' Lin_n＋1本の波形を形成する。        li_n＋1 番目のラインは実際のラインに交差することで欠損を生じさせるためのダミー        '''        for i in range(0,p_n):            ''' ベースとなる波形から、各パラメータを指定範囲内でランダムに摂動させて            新しい波形を生成            '''            d_f  = (np.random.randint(-30,30))/100   # 周波数の摂動量            d_k  = (np.random.randint(2,20))/10      # 尖度の摂動量            d_of = (np.random.randint(-75,50))/100   # オフセットの摂動量            y_add  = d_of + g[i]*np.exp(                                    -((fu-pf[i]+d_f)**2)/                                        ((2*(pk[i]*d_k)**2))                                    )            y = y + y_add        yy.append(y)    yy   = np.asarray(yy)    ymax = np.max(yy)    # 最小値がX軸に張り付かないための調整    ymin = np.min(yy)-(np.random.randint(5,20))/100    # レンジをを0-1に入れるための調整    yn   = (yy-ymin)/((ymax-ymin)*np.random.randint(103,            110)/100)    # legend position    loc_n = 'best'    out   = (dim - (yn*(dim-1))).astype(np.int32)    # line width scale randame choice    scale = np.random.choice(weigt,4)/(np.random.randint(500,910)/1000)    plt.rcParams['axes.linewidth'] = axis_lw*scale[0]       # axis linewidth    ax    = fig.add_axes([xy[0], xy[1], xxyy[0], xxyy[1]])  # figure position    # marker style    if mk_on_off == 1 :        mak_   = np.sort(np.random.choice(marker,                                      li_n+1,                                      replace=False                                      ))    else:        mak_ =['','','','','','','']    # marker facecolor    fc_    = np.random.choice(['w','k'])    # marker-line plot    dif   = np.random.randint(g_val[0],g_val[1])    idx_d = []    # maker の間隔を設定範囲内でランダムに変える    dif   = np.random.randint(g_val[0],g_val[1])    # 3－5個のラベル文字をランダムに生成する    moj   = np.random.randint(3,6)    d_n=''.join(random.choices(string.ascii_letters, k=moj))    # figure plot    for k in range(0,li_n+1):        # +1 のラインは実際のラインに交差することで欠損を生じさせるためのダミー        # line plot        #- ダミーラインの線とマーカを白        l_col = 'k'        lab   = d_n+str(k)        if k ==  li_n :            l_col = 'w'            lab   = ''            fc_ = 'w'            ms  =  5        else :            #マーカーの白抜きと黒塗りをランダムに選択            fc_ = np.random.choice(['w','k'])      # line style        if mk_on_off == 1 :          '''マーカーがある場合は線種は実線、マーカーがない場合は線種はランダムに選択する          '''          ls_='solid'        else :            ls_    = np.random.choice(['-','-.',':','--'])        ax.plot(fu, yn[k,:],                l_col,                ls        = ls_,                linewidth = axis_lw*scale[0],                label     = lab,                markevery = dif,                marker = mak_[k],                mfc    = fc_ ,                mec    = l_col,                ms     = g_val[2]*scale[1]                )    # draw legend    fr_ch = np.random.choice([1,0])    ax.legend(loc        = 'best',              fontsize   = f_s*scale[2]*0.8,              framealpha = 0              )    ax.spines["right" ].set_color('none')    ax.spines["top"   ].set_color('none')    ax.spines["left"  ].set_color('none')    ax.spines["bottom"].set_color('none')    plt.ylim(0,1) ; plt.xlim(0,1)    # Buffer から　figure　データを取得    '''画像出力は行わずBufferから直接画像の数値ﾃﾞｰﾀを読み出す    '''    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    flg = l%3    if flg == 0 :        '''3個に1個の割合でデータの前後にランダムな空白を上書きする        '''        of_x = np.random.randint(0,dim*0.10,2)        cv2.rectangle(imar,(0,0),(of_x[0],dim),                        (255,255,255),-1)  # left        cv2.rectangle(imar,(dim-of_x[1],0),(dim,dim),                        (255,255,255),-1)  # right    # 画像データ    gray_image  = cv2.cvtColor(imar,cv2.COLOR_BGR2GRAY)    arrayimage  = img_to_array(gray_image).astype(np.uint8)    ima_data.append(arrayimage)    # 教師データ(波形の本数)    out         = [li_n]  # ダミーラインはカウントしない    out_data    = np.append(out_data,np.array(out),axis=0)# 全画像データima_data = np.asarray(ima_data)# 全教師データ(波形の本数)out_data = np.asarray(out_data).astype(np.uint8)# Execution timee_time = time.time()print(' ...Finished. Execution time (s) = ','{:6.2f}'.format(e_time - s_time) )# ---- Data check --------------------------------------------plt.close()print('\n Data figure check ')n      = 20fig    = plt.figure(figsize=(15,12))n_list = np.random.randint(0,len(ima_data),n)print(' * sample number = ',n_list)for i in range(n):    nn = n_list[i]    ax = plt.subplot(4,5,i+1)    plt.imshow(np.reshape(ima_data[nn,:], (dim,-1)))    plt.gray()    plt.title(["Line N=",out_data[nn]])    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# Special exit'''データの数が99個以下の場合はデータの保存を行わないでプログラムを終了する'''if loop <= 99:    print('\n /// Data were not saved, because loop was < 100. ///')    sys.exit()# --- Data save ----------------------------------------------axis_test_graf = {'data':ima_data,                'out':out_data,                'para':(dim,                        ndpi,                        com_n,                        marker,                        cho_l,                        cho_p,                        im_type,                        line_type                        )                }with open('./'+comment+str(dim)+'_'+str(loop)+'_'+str(today)+'.pik','wb') as f1:    pickle.dump(axis_test_graf,                f1,                protocol = 4                )print('\n >>> Data was saved : '+comment+str(dim)+'_'+str(loop)+'_'+str(today)+'.pik')# _____________________________ End of code ____________________________source_codes_data/Data_OrgAxl.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Nov.01,2018 / Fixed on Jun.30,2020＜ 原点と軸長さの読み取りのための機械学習データ作成 ＞@author: T.Kono"""import numpy as npimport matplotlib.pyplot as pltimport cv2import sys, string, randomimport pickle, timefrom datetime import datetimeimport jsonfrom keras.preprocessing.image import img_to_arrayimport kn_lib.line as line# ----------------------------------------------------------------------------------------------##   Data Creating of Machine Learning for Origin Positin and Axis Length## ----------------------------------------------------------------------------------------------# --- Opening ---------------------------------------------# Date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')# start commentprint('\n< Data Creating of ML for Origin Position and Axis Length',      today,'>')# --- Parameter input and define --------------------------# data dimensiondim = 1024 # fixed# data numberprint('\n Input data number. *if n <= 99, datas are not saved')loop    = int(input(' ==> '))# common parameter'''同じdimの学習データは画像のパラメータを共有させる'''com_n = 'common_para_1024_2006251034.json'with open(com_n, mode='r') as f:    com_para = json.load(f)# parameter importndpi     = com_para['para'][1]axis_lw       = com_para['para'][2]f_s      = com_para['para'][3]weigt    = com_para['para'][4]g_val = com_para['para'][5]'''g_val[0][1]　はマーカー間隔の最小値と最大値(pixel) g_val[2]はマーカーの大きさ'''pix_poit = round(ndpi/72)       # pixel number of pointww       = dim/ndpi ; hh = ww   # graph width (inchs)# image typeim_type = 'g''''gray に固定'''if im_type == 'g':    imt = 'L';i_t='gray'elif im_type == 'm':    imt = '1';i_t='mono'else :    print('Image type is wrong') ;sys.exit()# line typeline_type = 'c''''cross に固定'''if line_type == 'c':    of_weight = [0.2,0.5,0.8];l_t='cross'elif line_type == 'p':    of_weight = [1,1.2,1.5,2];l_t='para'else :    print('Line type is wrong') ;sys.exit()# markermarker = ['+','D','^','o','s','v','x']'''marker sample['o','s','v']['+','D','^','o','s','v','x']['*','+',',','.','8','<','>','D','H','^','_','d','h','o','p','s','v','x','|']'''mak_l=len(marker)# p_weightは各ラインのピークの大きさを変化させるp_weight  = [0.8, 0.9, 1.1, 1.2, 1.3]# Line numbercho_l = [1,2,3,4,5,6]# Peak of line number'''1つの波形を作るために重ね合わせるガウス関数の数'''cho_p_l = [1,2,3]cho_p_h = [3,4,5]# --- Parameter comfirmationprint('\n < Data parameter >')print(' common parameter     = ',com_n)print(' dim                  = ',dim, '\n dpi                  = ',ndpi,      '\n data number          = ',loop )print(' Graph size           = ','{:3.3}'.format(ww),'x','{:3.3}'.format(ww),'inch')print(' Line Number          = ',cho_l)print(' Line width(normal)   = ',axis_lw,'point(',axis_lw*ndpi/72,'pixel)')print(' Font size(normal)    = ',f_s)print(' Marker number        = ',len(marker),marker)print(' Marker size(normal)  = ',g_val[2], '\n Pitch of marker      = ',      g_val[0],'-',g_val[1],      '\n scale factor         = ',weigt)# print('\n comment = ',comment)yn = input(' Confirm data parameter, process start OK? y or n --> ')if yn == 'n' :    print('Parameter is wrong') ;sys.exit()print('\n Creating Process Start ...'); s_time = time.time()# --- Data create -----------------------------------------# arry initial setima_data   = []out_data   = np.zeros([0, 4])arrayimage = np.zeros([dim,dim,3],dtype=np.int8)xxyy       = np.zeros([2])# Create loopfor l in range(0,loop):    # marker on/off    mk_on_off = random.choice([1,0])    # lune number select    li_n=np.random.choice(cho_l)    # peak number select    '''マーカーの有無とラインの本数により波形のピーク数(ガンマ関数の重ね合わせ)を変える.    マーカーを描く場合もしくはラインの本数が3本以上では少なく、マーカーを描かないかラインの本数    が3本以下の場合は多くする    '''    cho_p =  cho_p_h    if mk_on_off == 1 or li_n >3:        cho_p =  cho_p_l    p_n  = np.random.choice(cho_p)    # figure plot    plt.close()    fig     = plt.figure(figsize = (ww, hh), dpi=ndpi)    # Origine position and x-y axis length    xy      = np.random.randint(80,300,2)/1000                 # origin posision    xxyy[0] = np.random.randint(700,1020 - xy[0]*1000)/1000    # x length    xxyy[1] = np.random.randint(700,1020 - xy[1]*1000)/1000    # y lemgth    # Base line    nn = int(xxyy[0]*dim-4)    ff  = line.line_n(nn, p_n)    fu  = ff[0]    yn  = ff[1]    pf  = ff[2]    pp  = ff[3]    ph  = ff[4]    ofs = ff[5]    weigt_ = []    y      = np.zeros([nn])    yy     = []    for j in range(0,li_n):        for i in range(0,p_n):            of_scl = np.random.choice(of_weight,1)            p_scl  = np.random.choice(p_weight,2)            y_add  = ofs[i]+ ph[i]*np.exp(-((fu-pf[i]*p_scl[0])**2)/               (2*(pp[i]*p_scl[1]/(i+1))**2))            y = y*of_scl + y_add        yy.append(y)    yy   = np.asarray(yy)    ymax = np.max(yy)    yn   = yy/(ymax*1.1)    out   = (dim - (yn*(dim-1))).astype(np.int32)    # figure drowing    '''line width scale randame choice    '''    scale = np.random.choice(weigt,4)/(np.random.randint(800,1250)/1000)    plt.rcParams['axes.linewidth'] = axis_lw*scale[0]       # axis linewidth    ax    = fig.add_axes([xy[0], xy[1], xxyy[0], xxyy[1]])  # figure position    # marker style    if mk_on_off == 1 :        mak_   = np.sort(np.random.choice(marker,                                      li_n+1,                                      replace=False                                      ))    else:        mak_ =['','','','','','','']    # marker full color    fc_    = np.random.choice(['w','k'])    dif   = np.random.randint(g_val[0],g_val[1]) # マーカー間隔をランダムに選択    idx_d = []    moj   = np.random.randint(0,4)      # データ名、軸ラベルの文字数をランダムに選択    t_moj = np.random.randint(0,10,2)   # タイトルの文字数をランダムに選択    d_n=''.join(random.choices(string.ascii_letters, k=moj)) # データ名の文字列の生成    # line plot    for k in range(0,li_n):      if mk_on_off == 1 :        '''マーカーがある場合は線種は実線で固定、マーカーがない場合は線種はランダムに選択する        '''        ls_='solid'      else :        ls_    = np.random.choice(['-','-.',':','--'])      # line plot      ax.plot(fu, yn[k,:],      ls        = ls_,      color     = 'k',      linewidth = axis_lw*scale[1],      label     = d_n+str(k),      markevery = dif,      marker = mak_[k],      mfc    = fc_ ,      mec    = 'k',      ms     = g_val[2]*scale[1]      )    ax_n=''.join(random.choices(string.ascii_letters, k=t_moj[0])) # x軸文ラベル字列の生成    ay_n=''.join(random.choices(string.ascii_letters, k=t_moj[0])) # ｙ軸ラベル文字列の生成    ax.set_xlabel(ax_n, fontsize = f_s*scale[2])    ax.set_ylabel(ay_n, fontsize = f_s*scale[2])    # draw title    tp_    = np.random.choice(['right','left','center'])    ty_n =''.join(random.choices(string.ascii_letters, k=t_moj[1]))    ax.set_title(ty_n, loc = tp_, fontsize = f_s*scale[2])    # draw legend    loc_n = 'best'    fr_ch = np.random.choice([1,0])    ax.legend(loc        = loc_n,              fontsize   = f_s*scale[2]*0.8,              framealpha = fr_ch              )    plt.ylim(0,1) ; plt.xlim(0,1)    # draw tic    sel  = np.random.choice( ['in','out'] )     # 目盛線の方向    sel2 = np.random.choice( [True, False] )    # Y軸の目盛り数値のON/OFF    plt.tick_params(width      = axis_lw*scale[0],                    length     = 8*scale[1],                    labelsize  = f_s*scale[3],                    direction  = sel,                    labelleft  = sel2                    )    # frame shape (L and Box) randam choice    cho    = ['k','none']    select = np.random.choice(cho)    ax.spines["right"].set_color(select)    ax.spines["top"].set_color(select)    # data create    '''Buffer からデータを取得して学習データとする    '''    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    gray_image  = cv2.cvtColor(imar,cv2.COLOR_BGR2GRAY)    arrayimage  = img_to_array(gray_image).astype(np.uint8)    ima_data.append(arrayimage)    l_offset    = axis_lw*scale[0]*pix_poit   # origin position offset    o_offset=l_offset/2                       # axis length offset    out         = [xy[0]*dim+o_offset, xy[1]*dim+o_offset,                   xxyy[0]*dim-l_offset, xxyy[1]*dim-l_offset]    '''    原点位置と軸長さを教師データとして取得    '''    out_data    = np.append(out_data,np.array([out[:]]),axis=0)tn = 'auto'# Learning dataima_data = np.asarray(ima_data)# Teaching dataout_data = np.asarray(out_data)# Execution timee_time  = time.time()ext_p_n = (e_time-s_time)/loopprint(' ... Finished. Execution time (s) = ','{:4.1f}'.format(e_time - s_time),      ', exe_t/n  =','{:4.2f}'.format(ext_p_n))# ---- Data check -----------------------------------------plt.close()print('\n < Data figure >')n = 16fig=plt.figure(figsize=(ww*6,ww*6))n_list=np.random.randint(0,len(ima_data),n)print(' sample number = ',n_list)# print(' * upper = input, lower = output')for i in range(n):    nn=n_list[i]    ax=plt.subplot(4,4,i+1)    plt.imshow(np.reshape(ima_data[nn,:],(dim,-1)))    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# special exitif loop <= 99:    print('\n !!! Data were not saved, because Data number was < 100. ')    sys.exit()# --- Data save -------------------------------------------axis_test_graf = {'data':ima_data,                  'out' :out_data,                  'para':(dim,                          ndpi,                          com_n,                          min(out_data[:,0]), max(out_data[:,0]),                          min(out_data[:,2]), max(out_data[:,2]),                          marker,                          cho_l,                          cho_p)                  }sf_name = './data/OrgAxl_Data_'+str(dim)+'_'+str(loop)+'_'+str(today)with open(sf_name +'.pik','wb') as f1:    pickle.dump(axis_test_graf,                f1,                protocol=4                )print('\n Data were saved : ',sf_name+'.pik')# ______________________________ End of Code ______________________________________________source_codes_data/Data_WaveShaping.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Mar.14,2019 / Fixed on Jun.30,2020< 画像整形処理のための学習データを生成 >@author: T.Kono"""import numpy as npimport matplotlib.pyplot as pltimport cv2import sys,  string, randomimport pickle, timefrom datetime import datetimeimport jsonfrom keras.preprocessing.image import img_to_arrayimport kn_lib.line as line# -----------------------------------------------------------------------------##         Data Creating of Machine Learning for WaveShaping## -----------------------------------------------------------------------------# --- Opning --------------------------------------------------------# Date and timenow  = datetime.today().strftime('%y%m%d-%H%M')# start commentprint('\n< Data Creating of Machine Learning for WaveShaping.',now,'>')comment = 'WaveShaping_Data_'# --- Parameter input and define ------------------------------------# data dimensiondim =1024 # fixedprint('\n Data Dimension:',dim,'x',dim)# data number inputprint('\nInput data number. if <= 99, datas are not saved')loop    = int(input(' ==> '))# common parameter'''同じdimの学習データは画像のパラメータを共有させる'''com_n = 'common_para_1024_2006251034.json'with open(com_n, mode='r') as f:    com_para = json.load(f)# parameter importndpi     = com_para['para'][1]lw       = com_para['para'][2]f_s      = com_para['para'][3]weigt    = com_para['para'][4]g_val = com_para['para'][5]'''g_val[0][1]　はマーカー間隔の最小値と最大値(pixel) g_val[2]はマーカーの大きさ'''pix_poit = round(ndpi/72)       # pixel number of pointww       = dim/ndpi ; hh = ww   # graph width (inchs)# markermarker = ['+','D','^','o','s','v','x']'''marker sample['o','s','v']['+','D','^','o','s','v','x']['*','+',',','.','8','<','>','D','H','^','_','d','h','o','p','s','v','x','|']'''mak_l=len(marker)# Line numbercho_l = [1,2,3]# Peak of line number'''1つの波形を作るために重ね合わせるガウス関数の数'''cho_p = [2,3,4]# --- Parameter comfirmation ----------------------------------------print('\n < Data parameter >')print(' common parameter =',com_n)print('\n Data number =',loop )print(' line number =',cho_l)print(' line peak   =', cho_p)print(' dim         =',dim, ', dpi =',ndpi,      ' , fig size  =','{:3.3}'.format(ww),      'x','{:3.3}'.format(ww),'inch')print(' line width  =',lw,'p','\n font size   =',f_s,'p')print(' marker num  =',len(marker),marker)print('\n comment :',comment)yn = input(' Confirm data parameter, process start OK? y or n --> ')if yn == 'n' :    print('Parameter is wrong') ;sys.exit()print('\nData creating was started...')s_time = time.time()# --- Data creating -------------------------------------------------# initial arry setima_data   = []out_data   = []arrayimage = np.zeros([dim,dim,3],dtype=np.int8)xxyy       = np.zeros([2])# main loopfor l in range(0,loop):    # marker on/off    mk_on_off = random.choice([1,0])    # line number    li_n=np.random.choice(cho_l)    p_n =np.random.choice(cho_p)    plt.close()    fig = plt.figure(figsize=(ww,hh), dpi=ndpi)    # ベースとなるラインの生成    p_n  = np.random.choice(cho_p)    ff  = line.line_n(dim, p_n)    fu  = ff[0]    yn  = ff[1]    pf  = ff[2]    pk  = ff[3]    g   = ff[4]    ofs = ff[5]    yy     = []    y      = np.zeros([dim])    for j in range(0,li_n+1):        ''' li_n 番目のラインは実際のラインに交差することで欠損を生じさせるためのダミー        '''        for i in range(0,p_n):            ''' ベースとなる波形のパラメータをランダムに摂動させてベースに似た個々の波形を生成            '''            d_f  = (np.random.randint(-30,30))/100   # 周波数の摂動量            d_k  = (np.random.randint(2,20))/10      # 尖度の摂動量            d_of = (np.random.randint(-75,50))/100   # オフセットの摂動量            y_add  = d_of + g[i]*np.exp(                                    -((fu-pf[i]+d_f)**2)/                                        ((2*(pk[i]*d_k)**2))                                    )            y = y + y_add        yy.append(y)    yy   = np.asarray(yy)    ymax = np.max(yy)    # 最小値がX軸に張り付かないための調整    ymin = np.min(yy)-(np.random.randint(5,20))/100    # レンジをを0-1に入れるための調整    yn   = (yy-ymin)/((ymax-ymin)*np.random.randint(103,            110)/100)    # figure drowing    scale = np.random.choice(weigt,4)/(np.random.randint(800,1500)/1000) # line width scale randame choice    # figure position    ax    = fig.add_axes([0,0,1,1])    # marker style    if mk_on_off == 1 :        mak_   = np.sort(np.random.choice(marker,                                      li_n+1,                                      replace=False                                      ))    else:        mak_ =['','','','','','','']    # marker full color    fc_    = np.random.choice(['w','k'])    # maker の間隔を設定範囲内でランダムに変える    dif   = np.random.randint(g_val[0],g_val[1])    # 1－5個のラベル文字をランダムに生成する    moj   = np.random.randint(1,7)    d_n   =''.join(random.choices(string.ascii_letters+string.digits, k=moj)) # 文字と数字    # d_n2  =''.join(random.choices(string.ascii_letters+string.punctuation, k=moj)) #文字と記号    ''' $ 記号がbuffer読み書き中にerrorの原因となるので採用しない    '''    d_n2   =''.join(random.choices(string.ascii_letters+string.digits, k=moj)) # 文字と数字    # line plot    idx_d = []    l_col = 'k'  # ラインの色は黒に固定    for k in range(0,li_n):      # ラベルをランダム文字列から      lab   = d_n+str(k)      # line style      if mk_on_off == 1 :        '''マーカーがある場合は線種は実線、マーカーがない場合は線種はランダムに選択する        '''        ls_='solid'      else :        ls_    = np.random.choice(['-','-.',':','--'])      ax.plot(fu, yn[k,:],              l_col,              ls        = ls_,              linewidth = lw*scale[0],              label     = lab,              markevery = dif,              marker = mak_[k],              mfc    = fc_ ,              mec    = l_col,              ms     = g_val[2]*scale[1]              )    # outer frame off    ax.spines["right" ].set_color('none')    ax.spines["top"   ].set_color('none')    ax.spines["left"  ].set_color('none')    ax.spines["bottom"].set_color('none')    plt.ylim(0,1) ; plt.xlim(0,1)    # out data    '''波形だけで、レジェンドなどを何も書いていない状態. これを教師データとする    '''    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    '''3個に1個の割合でデータの前後にランダムな空白を挿入    '''    flg = l%3    if flg == 0 :        of_x = np.random.randint(0,dim*0.10,2)        cv2.rectangle(imar,(0,0),(of_x[0],dim),                        (255,255,255),-1)  # left        cv2.rectangle(imar,(dim-of_x[1],0),(dim,dim),                        (255,255,255),-1)  # right    gray_image  = cv2.cvtColor(imar,cv2.COLOR_BGR2GRAY)    arrayimage  = img_to_array(gray_image).astype(np.uint8)    out_data.append(arrayimage)    # legend draw    loc_n = 'best'    fr_ch = np.random.choice([1,0])    ax.legend(loc        = loc_n,              fontsize   = f_s*scale[2]*1.5,              framealpha = fr_ch)    # random text draw    for ii in range(0,len(pf)):        ax.text(pf[ii], pk[ii],                d_n2,                fontsize=f_s*scale[2]*1.2)    # tick draw    sel_d = np.random.choice(['in','out'])    sel_s = np.random.choice(['-',':',' ',' ',' ',' '])    ax.tick_params(width      = lw*scale[0],                    length    = 3*scale[1],                    direction = sel_d)    ## 目盛り線の数の最大値をランダムに選ぶ    plt.locator_params(nbins  = np.random.randint(1,12))    ## 縦のグリッド線を描く    ax.grid(axis='x',linestyle = sel_s)    # Buffer からデータを取得して学習データとする    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    '''3個に1個の割合でデータの前後にランダムな空白を挿入    '''    if flg == 0 :        cv2.rectangle(imar,(0,0),(of_x[0],dim),                        (255,255,255),-1)  # left        cv2.rectangle(imar,(dim-of_x[1],0),(dim,dim),                        (255,255,255),-1)  # right    gray_image  = cv2.cvtColor(imar,cv2.COLOR_BGR2GRAY)    arrayimage  = img_to_array(gray_image).astype(np.uint8)    ima_data.append(arrayimage)# Learning Dataima_data = np.asarray(ima_data)# Teaching Dataout_data = np.asarray(out_data)# Execution timee_time = time.time()ext_p_n    = (e_time-s_time)/loopprint(' ... Finished.  exe_time (s) = ',      '{:4.1f}'.format(e_time - s_time),      ', exe/n  =','{:4.2f}'.format(ext_p_n))# ---- Figure check -------------------------------------------------print('\n < Learning Data > ')n      = 20n_list = np.random.randint(0,len(ima_data),n)plt.close()fig    = plt.figure(figsize=(10,12))ax1 =fig.add_subplot(111)print(' selected number = ',n_list)for i in range(n):    nn = n_list[i]    ax1 = plt.subplot(5,4,i+1)    ax1.imshow(np.reshape(ima_data[nn,:],(dim,-1)),cmap='gray')    # plt.gray()    ax1.get_xaxis().set_visible(False)    ax1.get_yaxis().set_visible(False)    plt.gray()plt.show()print('\n < Teaching Data > ')fig    = plt.figure(figsize=(10,12))ax =fig.add_subplot(111)# print(' * sample number = ',n_list)for i in range(n):    nn = n_list[i]    ax = plt.subplot(5,4,i+1)    ax.imshow(np.reshape(out_data[nn,:],(dim,-1)),cmap='gray')    # plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# Special exitif loop <= 99:    print('\n /// Data were not saved, because loop was < 100. ///')    sys.exit()# --- Data save -----------------------------------------------------ml_data = {'data':ima_data,                'out':out_data,                'para':(dim,ndpi,com_n)}d_name = 'WaveShaping_Data_'+str(dim)+'_'+str(loop)+\'_'+str(now)+'.pik'with open(d_name,'wb') as f1:    pickle.dump(ml_data,                f1,                protocol=4                )print('\n Date is saved :',d_name)# _____________________________ END of code ___________________________________source_codes_data/digtizer_lib/__pycache__/dig.cpython-36.pycsource_codes_data/digtizer_lib/__pycache__/line.cpython-36.pycsource_codes_data/digtizer_lib/dig.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jan.15,2020 / Fixed on Jun.18,2020< Waveform Digitizerで呼び出される各種関数 >/ Final version:  Jun.18,2020, to Takada-sanText Delete FunctionGamma Correction FunctionErosion Correction FunctionColor/Black Separation FunctionColor Separation FunctionInner Figure Delete FunctionDizitization Evaluate FunctionAxis Gain Read FunctionLine Deleat Function@author: T.KONO"""import cv2import numpy as npfrom PIL import ImageDraw,Imageimport matplotlibmatplotlib.use('Agg')from matplotlib import pyplot as pltimport pickleimport pyocr, pyocr.buildersimport sys,refrom tesserocr import PyTessBaseAPIfrom tesserocr import RILfrom keras.preprocessing.image import array_to_img# ------------------------ Text Delete Function ---------------------------------------------def txt_elim(f, alf, sw):    '''    < tesserocrを用いて、グラフ内のテキストを消去する関数 >    入力 f   : イメージファイル        alf : 領域を拡張する大きさ        sw  : プロットするかどうかのスイッチ 1:yes, else:no    出力 image : テキスト消去後のメージファイル        boxes : テキストの位置    psm option:    0　テキストの傾斜角度や言語の種類を検知（OSD）して出力    1　OSDありでOCR（回転した画像にも対応してOCR可）    2　OSDなしでテキストの傾斜角度情報を標準出力（OCRなし）    3　OSDなしでOCR（デフォルトの設定はこれ）    4　単一列にさまざまなテキストサイズが入り混じったものと想定してOCR    5　縦書きのまとまった文章と想定してOCR    6　横書きのまとまった文章と想定してOCR    7　一行の文章と想定してOCR    8　一単語と想定してOCR    9　円の中に一単語がある想定でOCR（①、➁など）    10　一文字と想定してOCR    11　順序を気にせずできるだけ画像内に含まれる文章をOCRで取得    12　OSDありでできるだけ画像内に含まれる文章をOCRで取得    13　Tesseract固有の処理を飛ばして一行の文章としてOCR処理    '''    image   = f    sz      = image.size    draw    = ImageDraw.Draw(image)    imag_or = image.copy()    with PyTessBaseAPI(lang='eng',psm=11) as api:        api.SetImage(image)        boxes = api.GetComponentImages(RIL.WORD,True)        # Option: RIL.WORD, RIL.TEXTLIN, RIL.SYMBOL        for i, (im, box, _, _) in enumerate(boxes):            if box['w']*box['h'] < sz[0]*sz[1]*0.01 :                '''検出領域の面積が全pixelの 0.01 より大きな場合は                波形への影響が懸念されるため採用しない                '''                draw.rectangle([box['x']-alf,                                box['y']-alf,                                box['x']+box['w']+alf,                                box['y']+box['h']+alf],                fill    = 'white',                outline = 'white'                )    if sw == 1 :        fig = plt.figure(figsize=(7,3.5))        ax = plt.subplot(1,2,1)        plt.imshow(imag_or)        plt.title('< Original >')        ax.get_xaxis().set_visible(False)        ax.get_yaxis().set_visible(False)        ax = fig.add_subplot(1,2,2)        ax.imshow(image)        ax.set_title('< After Elimination >')        ax.get_xaxis().set_visible(False)        ax.get_yaxis().set_visible(False)        plt.show()        plt.close('all')    return (image, boxes)# ------------------------- Gamma Correction Function ------------------------------------def gamma_corrct(f, g):    """    < Gamma変換による輝度の修正する関数 >    入力 f : イメージデータ,        g : ganmma prameter    出力 im_gam      : アレイデータ ,        lookUpTable : look up table    """    image = np.asarray(f)    # lookUpTableの作成    lookUpTable = np.zeros((256, 1), dtype = 'uint8')    for i in range(256):        lookUpTable[i][0] = 255 * pow(float(i) / 255, 1.0 / g)    # ガンマ補正    im_gam = cv2.LUT(image, lookUpTable)    return (im_gam, lookUpTable)# ------------------------- Erosion Correction Function ------------------------------------def erode(f,sw,n,ngh):    """    < erosion（浸食/膨張）処理を行う関数 >        Mar.27,2019 : 処理の選択を追加    入力        f  : アレイデータ,        sw  : 処理の選択（0:浸食、1:膨張、2:浸食・拡張、3:拡張・浸食）        n   : 処理繰り返し回数        ngh : 近接領域の定義    出力        out : アレイデータ    """    # 4-点近接    neighbor4 = np.array([[0, 1, 0],                        [1, 1, 1],                        [0, 1, 0]],                        np.uint8)    # 8-点近接    neighbor8 = np.array([[1, 1, 1],                        [1, 1, 1],                        [1, 1, 1]],                        np.uint8)    if ngh == 'n4':        nghb = neighbor4    else:        nghb = neighbor8    # 補正    if sw == 0 :        out = cv2.erode(f,                        nghb,                        iterations = n                        )    elif sw == 1 :        out = cv2.dilate(f,                         nghb,                         iterations = n                         )    elif sw == 2 :        out = cv2.morphologyEx(f,                               cv2.MORPH_CLOSE,                               nghb                               )    elif sw == 3 :        out = cv2.morphologyEx(f,                               cv2.MORPH_OPEN,                               nghb                               )    return(out)# ------------------------- Color/Black Separation Function -------------------------------def rgb_mxmi(f, sl, v):    """    < RBGのMaxとMinの差から、黒とグレイ領域を抽出する関数 >        Mar.28,2019 : 2nd tex elimination ON/OFF追加    入力        f : RGB イメージデータ        sl : RGB での Max-Min のしきい値        v  : HSV(RGBでの Max) での value のしきい値    出力        imrgb2 :RGB カラー領域のアレイデータ        imbkgy :RGB 黒灰色領域のアレイデータ    Memo        RGBでの各値の最大値をMax、最小値をMinとすると        HSVでは S=(Max-Min)/Max, V=Maxとなる    """    img = np.asarray(f)    if len(img.shape) == 2:        img = cv2.cvtColor(img, cv2.COLOR_GRY2RGB)    # RGB to HSV    imhsv  = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)    bkgy   = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)    # graph size    sz = imhsv.shape    for i in range(0, sz[0]):        for j in range(0, sz[1]):            sa = float(imhsv[i,j][1]) # saturation            va = float(imhsv[i,j][2]) # value            # mx_mi はRGBでのMax-MIn            if sa == 0 or va == 0:                mx_mi = 0            else:                mx_mi = (sa*va/255)            if mx_mi <= sl and va < v:                # 白に変換                imhsv[i,j] = [0,0,255]                bkgy [i,j] = [0,0,0]            else:                # 白に変換                bkgy[i,j] = [0,0,255]    # HSV to RGB    col = cv2.cvtColor(imhsv, cv2.COLOR_HSV2RGB)    blk = cv2.cvtColor(bkgy,  cv2.COLOR_HSV2RGB)    # 2nd tex elimination ON/OFF. ON=0,OFF=else    sw = 99    if sw == 0 :        # 2nd text elimination        wo_txt_col = txt_elim(array_to_img(col),3,0)        wo_txt_blk = txt_elim(array_to_img(blk),3,0)        # image to array        imrgb2 = np.asarray(wo_txt_col[0])        imbkgy = np.asarray(wo_txt_blk[0])    else :        imrgb2 = col        imbkgy = blk    return  imrgb2, imbkgy# ------------------------- Color Separation Function --------------------------------------def color_separation(im_, f_na,gamma, now, s_sw):    """    < DBSCAN を用いてグラフの色分離をおこなう関数 >    入力        im_   : イメージ        f_na  : 入力イメージのファイル名        c_dim : リサイズする大きさ        gamma : ガンマ補正の係数        now:  : 現在時刻        s_sw: : データ保存の yes/no 1:yes, else:no    出力        color_line : 色分解されたアレイデータ、本数などの情報を含む    ＊DBSCANの計算できる数は10000個に制限がある    色分けの行程でのdimは360に固定する    """    dim   = 360    c_dim = 1024    # --- クラスタリングのための前処理 ------------------------------------------------    # ガンマ補正    g_corr = gamma_corrct(im_[0], gamma)    # Resize and 2HSV for Line Separation    image_c_dim  = cv2.resize(g_corr[0],                        (c_dim, c_dim),                        cv2.INTER_LANCZOS4                        )    # RGB --> HSV    hsv_image_c_dim = cv2.cvtColor(image_c_dim,                                   cv2.COLOR_RGB2HSV)    # Resize and 2HSV for Color Clustering    image  = cv2.resize(g_corr[0],                        (dim, dim),                        cv2.INTER_LANCZOS4                        )    '''モノクロ画像のエラーを避けるため右下隅に緑の小さな円を描く    '''    cv2.circle(image,               (dim-1, dim-1),               10,               (0,255,255)               )    # RGB --> HSV    hsv_image = cv2.cvtColor(image,                             cv2.COLOR_RGB2HSV)    # S,Vの制限    '''S（彩度）の小さな色(あいまいな色）を白に変換する    V(明度）の小さな色(黒に近い色）を黒に変換する    '''    s_lim = 30    for i in range(0,dim-1):        for j in range(0,dim-1):            # S-V 空間で左斜め下を制限            lim = 255-hsv_image[i,j,1]            # ｓがs_lim以下とS-V 空間で左斜め下の色をすべて白に置換            if hsv_image[i,j,2] <= lim or \                0 < hsv_image[i,j,1] <= s_lim:                hsv_image[i,j,:] = [0,0,255]            '''ｈが179を０に。本来円筒座標でつながっているはずなので便宜的につなげる。            ｈ=0の赤の救済'''            '''if hsv_image[i,j,0] >= 178:                hsv_image[i,j,0] = 0                hsv_orig[i,j,0]  = 0'''    # h,s,v color split    h_im, s_im, v_im = cv2.split(hsv_image)    # nolmarization    h_nol = np.reshape(h_im,(-1,1))/179    s_nol = np.reshape(s_im,(-1,1))/255    v_nol = np.reshape(v_im,(-1,1))/255    # --- 計算点数を削減するための処理 ---------------------------------------------    '''S=0 を排除する：    DBSCAN では計算点が 10000 個以内であることが推奨されている    グラフではほとんどのピクセルが白    白領域を排除することで計算点数を大幅に減らすことができる    S=0 は白、灰色、黒領域を含み色味は持たない。    S=0 を排除することで色味を持つピクセルを分離できる    灰色・黒領域は別プロセスで分離するからここで排除しても問題ない    '''    # index of nonzero    n_zero_ix = np.nonzero(s_nol)    # h,s,v non zero index    h_nol_nz = h_nol[n_zero_ix[0]]    # s_nol_nz = s_nol[n_zero_ix[0]]    v_nol_nz = v_nol[n_zero_ix[0]]    # --- DBSCANを用いた色分離 ---------------------------------------------------    # clustering data create    Z = np.c_[h_nol_nz, v_nol_nz]  # h-v space    if len(Z)>10000:        '''DBSCANは10000個以上の計算を実行するとメモリオーバーを起こす場合がある。        10000個以上の画像は対象外として中止する        '''        sys.exit(1)    # 色空間でのクラスタリングを実行    from sklearn.cluster import DBSCAN    dbs = DBSCAN(eps         = 0.02,                 min_samples = 70,                 n_jobs      = -1                 ).fit(Z)    # clustering of all element    y_dbscan   = dbs.labels_    cls_n      = np.max(y_dbscan)+1    # value of component(element witout noise)    z_c      = dbs.components_    # indx of component    i_c      = dbs.core_sample_indices_    # cluster number of component    c_n       = []    for i in i_c:        c_n.append(y_dbscan[i])    c_n = np.array(c_n)    # 色分離結果の信頼性    c_rep = np.zeros([0,6])    for j in range(0,cls_n):        dammy_  = z_c[np.where(c_n==j),:]        nn      = len(dammy_[0])        # Heu        dh_max  = np.max(dammy_[0,:,0])        dh_min  = np.min(dammy_[0,:,0])        dh_mean = np.mean(dammy_[0,:,0])        # Value        dv_max  = np.max(dammy_[0,:,1])        dv_min  = np.min(dammy_[0,:,1])        dv_mean = np.mean(dammy_[0,:,1])        if nn > len(Z)*0.025  :            # クラスタに含まれる点の数が点の総数の 0.025 より小さなものは色として採用しない            c_v   = [dh_mean,                     dh_max,                     dh_min,                     dv_mean,                     dv_max,                     dv_min                     ]            c_rep = np.append(c_rep,                              np.array([c_v[:]]),                              axis=0                              )        else :            '''print('{:3.0f}'.format(j),'|','{:4.0f}'.format(nn),                  '*** NG, Because data number are too little'                  )'''    cls_n_pass=len(c_rep)    # --- クラスタリングされた色によりラインを分離する ---------------------------------    line_data     = []    line_image    = []    for l in range(0,cls_n_pass):        # upper louer range of s, s=50~255 fix        # V-range adjustment        if 0.2 <= c_rep[l][3] <= 0.8 :            dcl = 0.3            dcu = 0.2        else :            dcl = 0            dcu = 0        lower_ = np.array([c_rep[l][2]*179,                           80,                           (c_rep[l][5]-dcl)*255]                          )        upper_ = np.array([c_rep[l][1]*179,                           255,                           (c_rep[l][4]+dcu)*255]                          )        # ラインの分離        line_image = 255 - cv2.inRange(hsv_image_c_dim,                                       lower_,                                       upper_                                       )        # append array        line_data.append(line_image)    # --- 黒ラインの処理 --------------------------------------------------------------    bkgy   = cv2.cvtColor(im_[1], cv2.COLOR_BGR2GRAY)    bklin  = cv2.resize(bkgy,                    (c_dim, c_dim),                    cv2.INTER_LANCZOS4                    )    line_data.append(bklin)    prt_sw = 0    if prt_sw == 1 :        # --- ラインイメージの確認 ------------------------------------------------------        ax = plt.figure(figsize=(10,3.5*((cls_n_pass)/3+1)))        for ii in range(0,cls_n_pass+1):            ax = plt.subplot((cls_n_pass)/3+1,3,ii+1)            plt.imshow(line_data[ii],cmap='gray')            # plt.gray()            if ii == cls_n_pass:                plt.title('\n Black-Gray')            else:                plt.title('\n Color ='+str(ii+1))            ax.get_xaxis().set_visible(False)            ax.get_yaxis().set_visible(False)        plt.show()        plt.close('all')    # --- ラインデータの保存 -----------------------------------------------------------    color_line = {'data':line_data,                  'para':(f_na,                          c_dim,                          cls_n_pass,                          len(Z)                      )                  }    if s_sw == 1 :        s_name = 'color_line_'+f_na+'_'+str(dim)+'_'+str(now)+'.pik'        with open(s_name,'wb') as f1:            pickle.dump(color_line, f1, protocol=4)        # print('\n Color_data is saved :',s_name)    '''else :        # print('\n > Color_data were not saved. ')'''    return color_line# ------------------------- Inner Figure Delete Function -----------------------------------def contandline_detect(f, lw):    """     < Open CVを用いて、入れ子グラフ を消去するジュール >    入力        f  : アレイデータ(BGR)        lw : 輪郭を白で上書きする際のラインの幅        dn : 点線を認識する投票数のしきい値        sn : 実線を認識する投票数のしきい値    出力        imrgb : アレイデータ(BGR)    """    imag    = np.asarray(f)    # Gray（shape=2）のファイルのShapeをColorの3にそろえる    if len(imag.shape) == 2:        imag = cv2.cvtColor(imag,cv2.COLOR_GRAY2BGR)    imgray  = cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY)    ret,im_ = cv2.threshold(imgray,250,255,cv2.THRESH_BINARY_INV)    # imag_or = imag.copy()   # original keep aside    dim     = im_.shape     # 画像の縦・横ピクセル数    s_dim   = dim[1]*dim[0] # 画像の総ピクセル数    # print(s_dim)    #--- 輪郭検出を行い内挿図形と文字・記号を選別して削除する -------------------    # 輪郭検出    '''findContours の output が OpenCV の Version-Up （3.4 -> 4.0?）    に伴いimage, contours, hierarchy の ３個から    contours, hierarchy の ２個に変更された    '''    '''    image,contours,hierarchy = cv2.findContours(im_,                                        cv2.RETR_LIST,                                        cv2.CHAIN_APPROX_SIMPLE                                        )    '''    contours, hierarchy = cv2.findContours(im_,                                         cv2.RETR_LIST,                                         cv2.CHAIN_APPROX_SIMPLE                                         )    # 検出された輪郭の面積、輪郭の点数により選択を行う    '''    内挿図形のインデックスを big_list,    文字や記号のインデックスを sml_list    '''    big_list  = [] ; sml_list  =[]    for i, cnt in enumerate(contours):        cnt  = np.squeeze(cnt, axis=1)        area = cv2.contourArea(cnt)        if s_dim/4 >= area >= s_dim/30 and len(contours[i]) < 100:            '''            内挿図形の抽出：            外枠、XY軸は除くため全面積の 1/4以下と            文字の輪郭などを排除するため1/30以上で選別する.            波形を取り込んだ場合を取り除くため要素数が100個以上も除く            '''            big_list.append(i)        elif s_dim/1000 <= area <= s_dim/700 :            '''            文字記号の抽出：            文字や記号を検出し、波形の選別はできるだけ避けるための選別            '''            sml_list.append(i)    # 内挿図形として抽出された輪郭を白塗りして元の画像に上書きする    for i in big_list :        cnt  = np.squeeze(contours[i], axis=1)        # 輪郭の内部を白塗り        cv2.fillConvexPoly(imag,                           points=cnt,                           color=(255,255,255)                           )        #　輪郭を太くして白塗り        cv2.drawContours(imag,                         [cnt],                         -1,                         (255,255,255),                         20                         )    # 文字・記号として抽出された輪郭を白塗りして元の画像に上書きする    for i in sml_list :        cnt  = np.squeeze(contours[i], axis=1)        # 輪郭内部を白塗り        cv2.fillConvexPoly(imag,                           points=cnt,                           color=(255,255,255)                           )        #　輪郭を白塗り        cv2.drawContours(imag,                         [cnt],                         -1,                         (255,255,255),                         lw                         )    return imag# ------------------------- Dizitization Evaluate Function ----------------------------------def wf_eval(wo_cont,            prd_ol,            l_data,            ng_bk,col_n,            lin_nu,            dim            ):    print('\n> Evaluation of the digitizing  ')    """    < 入力画像と予測数値を比較して数値化の信頼性の評価を行うモジュール >    入力        wo_cont : 背景画像は文字、輪郭を削除後の画像        prd_ol  :        l_1_data,l_2_data,l_3_data : 数値データ        ng_bk        col_n : 色数        lin_nu : ライン数        dim : 計算次数    出力        n_zp : 信頼度    """    # 背景画像は文字、輪郭を削除後の画像を用いる    base_im = cv2.resize(wo_cont,(dim,dim),cv2.INTER_LANCZOS4)    ndpi = 360    ww   = dim/ndpi ; hh = ww    lw   = ndpi/72*2    fig  = plt.figure(figsize=(ww,hh), dpi=ndpi)    ax   = fig.add_axes([0,0,1,1])    # 原点の外側の領域を白でマスキング    cv2.rectangle(base_im,                  (0,0),(prd_ol[0,0],dim-1),                  (255,255,255),thickness=-1                  ) # reft side    cv2.rectangle(base_im,                  (prd_ol[0,0]+prd_ol[0,2],0),(dim-1,dim-1),                  (255,255,255),thickness=-1                  ) # right side    cv2.rectangle(base_im,                  (0,dim-1),(dim-1,dim-prd_ol[0,1]-1),                  (255,255,255),thickness=-1                  ) # lower    cv2.rectangle(base_im,                  (0,0),(dim-1,dim-prd_ol[0,1]-prd_ol[0,3]-1),                  (255,255,255),thickness=-1                  ) # upper    ax.imshow(base_im)    '''    背景画像の画像の点数を求める. 黒にラインがない場合は    あらかじめ点数 ng_bk を引いておく    '''    base_rv   = 255-cv2.cvtColor(base_im, cv2.COLOR_BGR2GRAY)    base_n_z  = len(np.nonzero(base_rv)[0]) - ng_bk    '''    背景画像に数値化データを白いラインで上書. 一致する部分は画像上消える    lwでラインの幅を指定.    '''    fx  = np.linspace(0,dim-1,dim)    for i in range(0,sum(lin_nu)):        ax.plot(prd_ol[0,0]+fx*prd_ol[0,2]/dim ,                l_data[0:dim,0,i]*prd_ol[0,3]+                dim-prd_ol[0,1]-prd_ol[0,3],                'w-',lw=lw                )    # 不要な枠線を描かない    ax.spines["right" ].set_color('none')    ax.spines["top"   ].set_color('none')    ax.spines["left"  ].set_color('none')    ax.spines["bottom"].set_color('none')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    plt.ylim(dim,0);plt.xlim(0,dim)    '''上書き画像をカンバス上に描写. バッファに取り込みアレイ化    '''    fig.canvas.draw()    buf  = np.frombuffer (fig.canvas.tostring_rgb(),                          dtype=np.uint8                          )    imar = np.reshape(buf,(dim,dim,-1))    '''上書き画像の画像の点数を求める.    '''    gray_rv   = 255-cv2.cvtColor(imar, cv2.COLOR_BGR2GRAY)    # plt.show()    plt.close()    n_z  = len(np.nonzero(gray_rv)[0]) - ng_bk    '''上書き画像の画像点数と入力（背景）画像の画像点数の比率を求め、    １から引いて信頼度とする.    ex. 完全に一致した場合上書き画像の点数は ０ ,信頼度は１    '''    n_zp = 1-n_z/base_n_z    return n_zp, n_z, base_n_z# ------------------------- Axis Gain Read Function ----------------------------------------def axgain(ff_n,sz,prd_ol):    print( '\n> Axis Gain Read ')    """    < XY軸の軸目盛をOCR(pyocer)で読み取り、軸ｹﾞｲﾝを推定する >    input    ff_n: file name    sz: image size    prd_ol: Origon position and axis length    retuen    x0:x軸の始点の値    xe:x軸の終点の値    y0:y軸の始点の値    ye:y軸の終点の値    """    #原点位置の読み出し    org_axl=prd_ol[0]/1024     # --- 前処理 --------------------------------------------------------------------    # Image to array    ar_im = cv2.imread(ff_n)    # Origin of array    x_or_p = int(sz[0]*org_axl[0])    y_or_p = int((1-org_axl[1])*sz[1])    # Trimming    ar_=ar_im.copy()    ar_[0:y_or_p+10,x_or_p-1:(sz[0]-1),:]=255    x_trm = ar_[y_or_p:sz[1]-1,0:sz[0]-1]    y_trm = ar_[0:y_or_p+10,0:x_or_p-5]    # --- Pyocr open ----------------------------------------------------------------    tools = pyocr.get_available_tools()    if len(tools) == 0:        print("No OCR tool found")        sys.exit(1)    # --- X軸数値の読み取り ---------------------------------------------------------    # Gamma Correction    g = 0.5    lookUpTable = np.zeros((256, 1), dtype = 'uint8')    for i in range(256):        lookUpTable[i][0] = 255 * pow(float(i) / 255, 1.0 / g)    x_trm = cv2.LUT(x_trm, lookUpTable)    # Array to image    image = Image.fromarray(x_trm)    tool = tools[0]    # OCR    x_res = tool.image_to_string(image,                               lang="eng",                               builder=pyocr.builders.\                                   WordBoxBuilder\                                       (tesseract_layout=3)                                       )    x_nl=[]    x_pl=[]    i=0    for d in x_res:        # print(i,d.content,d.position)        num = re.sub("\\D", "",d.content)        jud = str.isdigit(num)        if jud == True:            er=0            # floatでのエラーで計算が止まることの回避            try:                x_nl.append(float(d.content))            except ValueError:                er = 1            if er == 0:                x_pl.append((d.position[1][0]+d.position[0][0])/2)        cv2.rectangle(x_trm,                      d.position[0],                      d.position[1],                      (255,0,0),                      1                      )        i=i+1    # 小さい順に左から並んでいるとして    xn = len(x_nl)    if xn >=2 :        x_max =x_nl[xn-1]        x_min =x_nl[0]        x_max_p =x_pl[xn-1]        x_min_p =x_pl[0]        # pixel当たりの増分を計算        x_d = x_max - x_min        x_ld =x_max_p-x_min_p        x_r = x_d/x_ld        # 原点の値を計算        x0 = round(x_min-(x_min_p-x_or_p)*x_r,2)        xe = round(x0+x_r*(sz[0]*org_axl[2]),2)        print (' X-axis Gain:',x0,'-',xe)    else:        x0  = 0        xe  = 1        print(' *I can not find X-axis Gain. Defalt value set:',x0,'-',xe)    # --- Y軸の数値読み取り ----------------------------------------------------------    # Gamma Correction    g = 0.3    lookUpTable = np.zeros((256, 1), dtype = 'uint8')    for i in range(256):        lookUpTable[i][0] = 255 * pow(float(i) / 255, 1.0 / g)    y_trm = cv2.LUT(y_trm, lookUpTable)    image = Image.fromarray(y_trm)    tool = tools[0]    y_res = tool.image_to_string(image,                               lang="eng",                               builder=pyocr.builders.\                                   WordBoxBuilder\                                       (tesseract_layout=3)                                       )    y_nl=[]    y_pl=[]    i=0    for d in y_res:        # print(i,d.content,d.position)        num = re.sub("\\D", "",d.content)        jud = str.isdigit(num)        if jud == True:            er=0            # floatでのエラーで計算が止まることの回避            try:                y_nl.append(float(d.content))            except ValueError:                er = 1            if er == 0 :                y_pl.append((d.position[1][1]+d.position[0][1])/2)        cv2.rectangle(y_trm,                      d.position[0],                      d.position[1],                      (255,0,0),                      1                      )        i=i+1    # 大きい順に上から並んでいるとして    yn = len(y_nl)    if yn >=2 :        '''OCRの結果は大きさ順ではない場合がある        '''        y_max   = y_nl[y_pl.index(min(y_pl))]        y_min   = y_nl[y_pl.index(max(y_pl))]        y_max_p = y_pl[y_pl.index(min(y_pl))]        y_min_p = y_pl[y_pl.index(max(y_pl))]        # pixel当たりの増分を計算        y_d  = y_max - y_min        y_ld = abs(y_max_p-y_min_p)        y_r  = y_d/y_ld        # 始点・終点の値を計算        y0 = round(y_min-(y_or_p-y_min_p)*y_r,2)        ye = round(y0 + y_r*(sz[1]*org_axl[3]),2)        if y0 == ye :            y0 = 0.0            ye = 1.0        print (' Y-axis Gain:',y0,'-',ye)    elif yn == 1 :        y_max   = y_nl[0]        y_min   = 0        y_max_p = y_pl[0]        y_min_p = y_or_p        # pixel当たりの増分を計算        y_d  = y_max - y_min        y_ld = abs(y_max_p-y_min_p)        y_r  = y_d/y_ld        # 始点・終点の値を計算        y0 = round(y_min-(y_or_p-y_min_p)*y_r,2)        ye = round(y0 + y_r*(sz[1]*org_axl[3]),2)        print (' Y-axis Gain:',y0,'-',ye)    else:        y0  = 0        ye  = 1        print(' *I can not find Y-axis Gain. Defalt value set:',y0,'-',ye)    return x0,xe,y0,ye# ------------------------ Line Deleat Function ---------------------------------------------def linedel(f):    """    < 軸などの長い直線を消去する関数 >    f: gray_array_data    """    imgray = f    dim    = 1024    ret,im_mono = cv2.threshold(imgray,254,255,0)    # --- 直線の検出 -----------------------------------------------------------------    minLineLength = int(dim/2)    maxLineGap    = 1    lines         = cv2.HoughLinesP(255-im_mono,                                    1,                                    np.pi/180,                                    int(dim/1.3),                                    minLineLength,                                    maxLineGap                                    )    '''第1引数は入力画像であり，2値画像    第2,3引数にはそれぞれrouとshitaの精度を指定します．    第4引数は，直線とみなされるのに必要な最低限の投票数を意味するしきい値です．    投票数は直線上の点の数に依存する．この引数は検出可能な線の長さの最小値．    '''    v_l = np.zeros([0, 4])    h_l = np.zeros([0, 4])    # s_l = np.zeros([0, 4])    if lines is not None:        '''linesが何も検出できなかったときは処理を行わない        '''        # 垂直線と水平線および斜線に分離する        for i in range(len(lines)):            for x1,y1,x2,y2 in lines[i]:                out = [x1,y1,x2,y2]                if abs(x1-x2) <= 0.5 and  dim/10 < y1-y2 :                    v_l = np.append(v_l, np.array([out[:]]),axis=0)                elif y1 == y2 and dim/10 < x2-x1:                    h_l = np.append(h_l, np.array([out[:]]),axis=0)                '''                else:                    s_l = np.append(s_l, np.array([out[:]]),axis=0)                '''        # 出力画像        for j in range(0,len(v_l)):            x1,y1,x2,y2 = v_l[j]            # plt.plot([x1,x2],[y1,y2],'r',lw = 5)            cv2.line(imgray,                     (int(x1),int(y1)),(int(x2),int(y2)),                     (255),                     int(dim/100)                     )        for k in range(0,len(h_l)):            x1,y1,x2,y2 = h_l[k]            cv2.line(imgray,                     (int(x1),int(y1)),(int(x2),int(y2)),                     (255),                     int(dim/100)                     )    return imgray# _____________________________ END of Code ______________________________________________source_codes_data/digtizer_lib/line.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jun.22,2020 / Fixed on Jun.30,2020@author: T.Kono"""import numpy as np# .................... Line Create Function ............................def line_n(nn,p_n):    '''p_n個のガウス関数を重ね合わせて波形を生成する関数    入力        nn  : data number        p_n : peak number    出力        x : xの値        y :　yの値        p_f : ピークの中心値        p_k :　ピークの尖度        g_ :　全体のゲイン        ofs :　オフセット    '''    # Xの値の生成    x    = np.linspace(0,1,nn)    # yの領域生成    y    = np.zeros([nn])    p_f  = []    p_k  = []    g_   = []    ofs  = []    for i in range(0,p_n):        ''' ガウス関数で波形を発生させる        下のパラメータをランダムに変えて、p_n本の波形を生成して重ね合わせる        fo : ピークの中心周波数        pk : ピークの尖度        h  : 全体のゲイン        ofse : オフセット量        '''        fc   = (np.random.randint(-30,130))/100        pk   = (np.random.randint(10,500 ))/1000        ga   = (np.random.randint(100,150 ))/100        ofse = (np.random.randint(10,100))/1000        # ガウス関数の生成        y_add = ofse + ga*np.exp(                                -((x-fc)**2)/                                (2*(pk)**2)                                )        # ガウス関数の重ね合わせ        y     = y + y_add        p_f.append(fc)        p_k.append(pk)        g_.append(ga)        ofs.append(ofse)    return x, y, p_f, p_k, g_, ofssource_codes_data/Learaning_FigSelection.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jun.20,2020< 種々の画像データの中から、読取り可能なグラフの選別を行う機械学習 >@author: T.KONO"""# ------------------------- Model Build Function -------------------------------------------"""Residual Net を作成するための関数"""from keras.layers import (Input,                      Conv2D,                      Add,                      Activation,                      Dense,                      Dropout,                      GlobalAveragePooling2D,                      MaxPooling2D                      ) # BatchNormalization,from keras.models import (Model)from keras.regularizers import l2def resnet(input_shape,                    num_outputs,                    m_name,                    n_filter,                    drp_n,                    leg_f                    ):    com =' for Classification \n with Dropout and l2-regularizer'    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        block = 'plain'    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'plain'    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'botteleneck'    elif m_name == 'RN101':        print('\n This is ResNet-101'+com)        nb_blocks = [3,4,23,3]        wide = 2        block = 'botteleneck'    elif m_name == 'RN152':        print('\n This is ResNet-152'+com)        nb_blocks = [3,8,36,3]        wide = 2        block = 'botteleneck'    elif m_name == 'W-RN':        print('\n This is Wide-ResNet'+com)        nb_blocks = [3,3,3]        wide = 4        block = 'plain'    else:        print('\n ???')    print('\n Dropout =',drp_n,'\n L2-Regularizer =',leg_f)    # Pre Conv    input = Input(shape=input_shape)    X = Conv2D(filters=n_filter,               kernel_size=(3,3),               strides=(2,2)               )(input)    X = Activation("relu")(X)    # pooling    X = MaxPooling2D(pool_size=(3,3),                     strides=(2,2),                     padding='same'                     )(X)    # ResNet itaration loop    shortcut = X    # X = BatchNormalization()(X)    if block == 'plain':        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter,                                       kernel_size=(1,1),                                       strides=(2,2),                                       kernel_regularizer=l2(reg_f)                                       )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                              kernel_size=(3,3),                              strides= (2,2),                              padding="same",                              kernel_regularizer=l2(reg_f)                              )(X)                    #X = BatchNormalization()(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                               kernel_size=(3,3),                               padding="same",                               kernel_regularizer=l2(reg_f)                               )(X)                    #X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                         kernel_size=(3,3),                         padding="same",                         kernel_regularizer=l2(reg_f)                         )(X)                # Add                X = Add()([X, shortcut])                shortcut = X                #X = BatchNormalization()(X)            n_filter *= wide    if block == 'botteleneck':        shortcut =  Conv2D(n_filter*4,                        kernel_size=(1,1) ,                        kernel_regularizer=l2(reg_f)                        )(shortcut)        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter*4,                                    kernel_size=(1,1),                                    strides=(2,2),                                    kernel_regularizer=l2(reg_f)                                    )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                kernel_size=(1,1),                                strides= (2,2),                                padding="same",                                kernel_regularizer=l2(reg_f)                                )(X)                    #X = BatchNormalization()(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                kernel_size=(1,1),                                padding="same",                                kernel_regularizer=l2(reg_f)                                )(X)                    #X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                            kernel_size=(3,3),                            padding="same",                            kernel_regularizer=l2(reg_f)                            )(X)                  #X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter*4,                            kernel_size=(1,1),                            padding="same",                            kernel_regularizer=l2(reg_f)                            )(X)                # add                X = Add()([X, shortcut])                shortcut = X                #X = BatchNormalization()(X)            n_filter *= wide    X = Activation("relu")(X)    X = GlobalAveragePooling2D()(X)    X = Dropout(drp_n)(X)    Out = Dense(num_outputs,                activation="sigmoid"                )(X)    model = Model(inputs=[input], outputs=[Out])    return model# ----------------------------------------------------------------------------------------------#                                           Machine Learning Process# ----------------------------------------------------------------------------------------------import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom keras.utils import np_utilsfrom sklearn.model_selection import train_test_splitimport pickle, timefrom datetime import datetime# --- Opening ----------------------------------------------# datetoday  = datetime.today().strftime('%y%m%d-%H%M')# opening tytleprint('\n＜ MECHINE LEARANINI OF IMAGE SELECTION_',      today,'＞')# start times_time = time.time()# --- Data import ------------------------------------------# data read# data_n = input(' Input Data Name --> \n ')data_n ='./data/FigSerection_ad4500-y1-y2_256_11532_20191114-0958.pik'# data_n ='./data/FigSerection_256_2678_20191024-1632.pik'print('\n Data = ',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)# X-normalize and  black-white reversedim = 256 # dim = int(test_graf['para'][0])X   = test_graf['in']if np.max(X[0,:]) == 255:    nor = 255    # case of gray dataelse:    nor = 1      # case of monochro dataX = 1-X/nor      # black-white reverseprint('\n> Learning Process Start ... ')# Y-categorizeYo = test_graf['out']Y  = np_utils.to_categorical(Yo) # 教師データをカテゴリー型に変換# input/output sizen_in  = len(X[0])n_out = len(Y[0])# --- Data split -------------------------------------------x_train, x_test, y_train, y_test = train_test_split(        X, Y,        test_size  = 0.2,        train_size = 0.8,        random_state = 0        )# --- Itaration parameter ----------------------------------drp_n        = 0.5reg_f        = 0n_epoch      = 300n_batch      = 30first_fil_n  = 32n_pat        = 100flag_ver     = 0# --- Model build ------------------------------------------input_= x_train.shape[1:]m_n  = 'RN34'model = resnet(input_,               n_out,               m_n,               first_fil_n,               drp_n,               reg_f               )# model.summary()# parametor printprint('\n Learn_n      = ',len(x_train),      ',Test_n =',len(x_test))print(' max_epoch    = ',n_epoch)print(' first_filter = ',first_fil_n)print(' batch        = ',n_batch)print(' patience     = ',n_pat)# --- Model compile ----------------------------------------# Optimizer adamoptimizer = 'adam'print('\n Opimizer : adam')''' Optimizer selection# Optimizer adamoptimizer = 'adam'print('\n Opimizer : adam')# Optimizer SGD + Momentumfrom keras.optimizers import SGDprint('\n Opimizer : SGD + Momentum')optimizer = SGD(decay=1e-6, momentum=0.9, nesterov=True)'''loss ='binary_crossentropy'# compilemodel.compile(loss       = loss,              optimizer  = optimizer,              metrics    = ['accuracy']              )# 'categorical_crossentropy','binary_crossentropy'print(' loss     :',loss)# --- Model fit --------------------------------------------early_stopping = EarlyStopping(monitor='val_loss',                               patience=n_pat                               )hist = model.fit(x_train, y_train,                 epochs          = n_epoch,                 batch_size      = n_batch,                 validation_data = (x_test, y_test),                 verbose         = flag_ver,                 callbacks       = [early_stopping]                 )# --- Predict and evaluation ------------------------------predict_ = model.predict(x_test, batch_size=n_batch)# dif      = np.round(y_test - predict_,0)dif      = (y_test - np.round(predict_,0))# errror listdif_      = abs(dif)pass_list = []; ng_list=[]for i in range(0,len(y_test)):    if max(dif_[i]) == 0:        pass_list.append(i)    else:        ng_list.append(i)# correct predict ratecpr = 1 - len(ng_list)/(len(dif)) # correct predict rate# Execution timen_ep    = len(hist.history['loss'])e_time = time.time() # end timeprint('\n* Process was finished. Exe time (s)  = ',      '{:6.2f}'.format(e_time - s_time),      ', end_ep = ',n_ep)# accuracy printde_loss = hist.history['val_loss'][n_ep-1]-\hist.history['loss'][n_ep-1]print('\n< Loss and accuracy >\n loss     = ',      '{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', val_loss = ',      '{:4.2e}'.format(hist.history['val_loss'][n_ep-1]),      ', delta = ',      '{:4.2e}'.format(de_loss),      '\n accuracy = ',      '{:4.3f}'.format(hist.history['acc'][n_ep-1])      )# loss and accuracy historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'])plt.plot(x_ep,hist.history['val_loss'])plt.yscale('log')plt.title("loss and epoch",fontsize=15)plt.subplot(2, 2, 2)x_ep = range(len(hist.history['acc']))plt.plot(x_ep,hist.history['acc'])plt.plot(x_ep,hist.history['val_acc'])plt.title("accuracy and epoch",fontsize=15)plt.ylim(0, 1.05)plt.show()print('< Error and correct rate >')print (' error_n = ',len(ng_list),'/',len(dif),       ', correct predict rate = ','{:4.3f}'.format(cpr))# error images drawingif not len(ng_list) == 0 :    print('\n< Error sample >')    fig    = plt.figure(figsize=(20,20))    n      = 6    n_list = np.random.choice(ng_list, n)    print(' figure number = ', n_list)    for i in range(n):        nn = n_list[i]        print(nn,' : ',np.argmax(y_test[nn]),'-->',              np.argmax(np.round(predict_[nn],0)),'prd')        ax = plt.subplot(4,n,i+1)        plt.imshow(np.reshape(1-x_test[nn,:], (dim, -1)))        plt.gray()        ax.get_xaxis().set_visible(False)        ax.get_yaxis().set_visible(False)        plt.ylim(dim,0) ; plt.xlim(0,dim)    plt.show()# --- Model print and save ---------------------------------mp_flag = 'n' ; ms_flag = 'n'model_n = 'FigSelection_'+m_n+'_'+str(dim)+'_'+str(today)# model printmp_flag = input(' Print  the learned model? y or n --> ')if mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model,               to_file='./Shape_'+model_n+'.png',               show_shapes=True)    print(' Model is printed --> ./model/Shape_',model_n+'.png' )else:    print(' Model is NOT printed.')# model savems_flag = input(' Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save('./model/'+model_n+'.h5')    print(' Model is saved --> ./model/',model_n+'.h5')else:    print(' Model is NOT saved.')del X# ______________________________ END of Code ____________________________________________source_codes_data/Learning_Digitizer_1-line.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Des.24,2019 / Fixed on Jun.30,2020 < 1本のラインを持つグラフを数値化するための機械学習 >@author: T.KONO"""import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom sklearn.model_selection import train_test_splitimport pickle, time, sysfrom datetime import datetimefrom keras.layers import (Input,                          Conv2D,                          Add,                          Activation,                          Dense,                          GlobalAveragePooling2D,                          MaxPooling2D,                          Dropout                          )from keras.models import (Model)from keras.regularizers import l2# ------------------------- ResNet Model Build Function -----------------------------------def resnet(input_shape,           num_outputs,           m_name,           n_fil,           l_f,           drp_n,           ln           ):    com = ' with l2-regularizer and dropout'    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'plane'    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'bottleneck'    else:        print('\n ???')    print(' L2-Regularizer =',l_f)    print(' dropout        =',drp_n)    input = Input(shape=input_shape)    n_filter = n_fil    reg_f = l_f    # --- 1st Pre convolution -------------------------------------------------------    X = input    X = Conv2D(filters=n_fil,               kernel_size=(3,3),               strides=(2,2),               padding="same",               kernel_regularizer=l2(reg_f)               )(X)    X = Activation("relu")(X)    # poollig    X = MaxPooling2D(pool_size=(3,3),                     strides=(2, 2),                     padding='same'                     )(X)    # --- 2nd Pre convolution ------------------------------------------------------    X = Conv2D(filters=n_fil,                kernel_size=(3,3),                strides=(2,2),                padding="same",                kernel_regularizer=l2(reg_f)                )(X)    X = Activation("relu")(X)    # pooling    X = MaxPooling2D(pool_size=(3,3),                    strides=(2,2),                    padding='same'                    )(X)    # --- Residual Net ---------------------------------------------------------------    shortcut = X    if block == 'plane':        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        strides= (2,2),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    if block == 'bottleneck':        shortcut =  Conv2D(n_filter * 4,                                            kernel_size=(1, 1) ,                                            kernel_regularizer=l2(reg_f)                                            )(shortcut)        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter * 4,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                            kernel_size=(1,1),                            strides= (2,2),                            padding="same",                            kernel_regularizer=l2(reg_f)                            )(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(1,1),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                    kernel_size=(3,3),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter * 4,                                    kernel_size=(1,1),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    X = Activation("relu")(X)    y = GlobalAveragePooling2D()(X)    y = Dropout(drp_n)(y)    y = Dense(int(num_outputs))(y)    model = Model(inputs=[input], outputs=[y])    return model# ----------------------------------------------------------------------------------------------#                      Machine Learning for 1-line Waveform Digitizer#                      - Mulch GPU#                      - ResNet with dorpout  and l2-regularizer#                      - lr_schedul# ----------------------------------------------------------------------------------------------# --- Opening ----------------------------------------------------------------------# Date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')print("\n< Machine Learning for 1-line Waveform Digitizer_",      today,'>')# Start times_time = time.time()# --- GPU Numbergpu_n = 2# Pass/NG Slice Level, difference between predict and targetsl  = 0.03# --- Data read ---------------------------------------------------------------------# data_n = input('\n Data --> ')data_n ='./data/Digitizer_Data_1-lin_l5000+7m5000_1024_200325-1053.pik'print(' Data:',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)dim  = 1024 # test_graf['para'][0]li_n = 1 # test_graf['para'][3]# print(' Dimension:',dim,', Line-number:',li_n)print('\nLearning Process Start...')# --- Data import and normalize -------------------------------------------------X   = test_graf['data']# X-normalize and  black-white reversedif np.max(X[0,:]) == 255:    nor = 255       # case of gray dataelse:    nor = 1         # case of monochro dataX = 1 - X/nor# Y-normalizeY = np.reshape(test_graf['out'],(-1,li_n*dim))/dim# input/output shapen_in  = len(X[0])n_out = len(Y[0])# Data splitx_train, x_test, y_train, y_test = train_test_split(        X, Y,        test_size  = 0.1,        train_size = 0.9,        random_state= 0        )# Delet large datadel X ; del test_graf# --- Itaration parameter ----------------------------------------------------------m_n         = 'RN34'reg_f       = 1e-6n_drop      = 0n_epoch     = 300n_batch     = 40first_fil_n = 16n_pat       = 100flag_ver    = 0# --- Model Built --------------------------------------------------------------------input_= x_train.shape[1:]model = resnet(input_,                    n_out,                    m_n,                    first_fil_n,                    reg_f,                    n_drop,                    li_n                    )# print prameterprint('\n Train_n   = ',len(x_train),', Test_n =',len(x_test))print(' epoch        = ',n_epoch)print(' batch         = ',n_batch)print(' first_filter    = ',first_fil_n)print(' patience    = ',n_pat)# print("\n* model_summary") ; model.summary()# --- Model compile ---------------------------------------------------------------# Optimizer settingfrom keras.optimizers import Adamoptimizer = Adam()print('\n Opimizer: Adam')'''## Optimizer Adamfrom keras.optimizers import Adamoptimizer = Adam(lr=0.001)print('\n Opimizer: Adam')## Optimizer SGD + Momentumfrom keras.optimizers import SGDoptimizer = SGD(lr=0.01,decay=1e-6, momentum=0.9, nesterov=True)print('\n Opimizer: SGD + Momentum')'''# 学習率をepoch数により切り替えるfrom keras.callbacks import LearningRateSchedulerdef lr_schedul(epoch):    '''default: adam:0.001, SDG:0.01    '''    x = 0.001    if epoch >= 100:        x = 0.00075    if epoch >= 200:        x = 0.0005    if epoch >= 400:        x = 0.0005    return xlr_decay = LearningRateScheduler(lr_schedul)# loss settingloss = 'mean_squared_error'print(' loss: ',loss)# compileif not gpu_n == 1 :    # Multi GPU    print('\n',str(gpu_n)+'-gpu running ')    from keras.utils import multi_gpu_model    parallel_model = multi_gpu_model(model, gpus=gpu_n)    parallel_model.compile(loss      = loss,                            optimizer = optimizer,                            metrics   = ['accuracy']                            )else:    # 1-GPU    print('\n 1-gpu running ')    model.compile(loss       = 'mean_squared_error',                    optimizer  = optimizer,                    metrics    = ['accuracy']                    )early_stopping = EarlyStopping(monitor='val_loss',                               patience=n_pat)# --- Model fitting -----------------------------------------------------------------if not gpu_n == 1 :    # Multi GPU    hist = parallel_model.fit(x_train, y_train,                            epochs           = n_epoch,                            batch_size       = n_batch,                            validation_data  = (x_test, y_test),                            verbose          = flag_ver,                            callbacks        = [early_stopping,lr_decay]                            )else:    # 1-GPU    hist = model.fit(x_train, y_train,                    epochs          = n_epoch,                    batch_size      = n_batch,                    validation_data = (x_test,y_test),                    verbose         = flag_ver,                    callbacks       = [early_stopping,lr_decay]                    )# --- Predict and evaluation -------------------------------------------------------predict_ = (model.predict(x_test, batch_size = n_batch))dif   = abs((y_test - predict_))'''グラフの前後10％は空白の可能性があるこの領域では精度の評価をしない'''spc     = int(dim*0.1)dif_l   = np.reshape(dif,(-1,li_n,dim))dif_ll  = np.reshape(dif_l[:,:,spc:dim-spc],(-1,(dim-2*spc)*li_n))n_ep  = len(hist.history['loss'])# error countpp_list   = []pass_list = []; ng_list=[]for i in range(0,len(y_test)):    # error list    pp = np.max(dif_ll[i,:])    pp_list.append(pp)    if pp > sl:        ng_list.append(i)     # NG list    else:        pass_list.append(i)   # PASS list# number of NGnonzero = len(ng_list)# pass ratecor_pr  = 1 - nonzero/(len(dif_ll))# Execution timee_time = time.time()print('... Finished. ')print('\n< Execution time > ')print(' Exe time (hr) = ', '{:6.2f}'.format(e_time - s_time),      '; end_ep = ',n_ep,      ', exe/ep = ',      '{:4.1f}'.format((e_time - s_time)/n_ep)      )# --- Print out of results -----------------------------------------------------------# loss and accuracyprint('\n< loss and accuracy > ')print( ' loss =','{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', val_loss =','{:4.2e}'.format(hist.history['val_loss'][n_ep-1]),      ',\n l1-acc =','{:4.3f}'.format(hist.history['acc'][n_ep-1])      )# loss historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'],label="loss")plt.plot(x_ep,hist.history['val_loss'],label="val_loss")plt.yscale('log')plt.legend(loc='best')plt.title("loss and epoch",fontsize=15)# accuracy historyplt.subplot(2, 2, 2)x_ep = range(len(hist.history['acc']))plt.plot(x_ep,hist.history['acc'],label="acc")plt.plot(x_ep,hist.history['val_acc'],label="l1_acc")plt.title("accuracy and epoch",fontsize=15)plt.ylim(0, 1.1)plt.legend(loc='best')plt.show()# errorprint('\n< Correct Rate >')print(' c_r = ','{:4.3f}'.format(cor_pr),      ', Error count  = ',nonzero,'/',len(dif),      ' (sl = ','{:3.2f}'.format(sl),')'      )print('    Note : 10% before and after data are not evaluated. Because there may be a blank.')# --- Result plotting ---------------------------------------------------------------# Best & Worstprint('\n< Best and Worst Sample >')fig = plt.figure(figsize=(7,7))# Best sample idexpp_min = np.amin(pp_list)kp     = pp_list.index(pp_min) # Best sample indexprint(' Best Sample  No.= ','{:3.0f}'.format(kp),      ', diff. = ','{:4.2f}'.format(pp_min))# Worst sample idexpp_max = np.amax(pp_list)kg     = pp_list.index(pp_max)print(' Worst Sample No.= ','{:3.0f}'.format(kg),      ', diff. = ','{:4.2f}'.format(pp_max))print(' upper: graph, lower: comparison of value')cor_list = ['r','b','g','c','m','y','w']f        = np.linspace(0,1,dim)# Best graphplt.subplot(2,2,1)plt.imshow(1-x_test[kp,:,:,0])plt.title("Best Sample",fontsize=12)plt.gray()# comparisonplt.subplot(2,2,3)for ii in range(0,li_n):    plt.plot(f,1-y_test[kp,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict_[kp,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)# Worst graphplt.subplot(2,2,2)plt.imshow(1-x_test[kg,:,:,0])plt.title("Worst Sample",fontsize=12)plt.gray()# comparisonplt.subplot(2,2,4)for ii in range(0,li_n):    plt.plot(f,1-y_test[kg,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict_[kg,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)plt.show()# NG sample plotprint('\n< NG sample >')fig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(ng_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    # graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    # comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict_[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# OK sample plotprint('\n< OK sample >')# special exitif len(pass_list) == 0:    print('\n *** The Proceess was terminated, because an OK sample does not exist. *** \n')    sys.exit()# plotfig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(pass_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    # graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    # comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict_[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# --- Model print and save--------------------------------------------------------print('\n< Model save and print >')m_para  = m_n+'_bt'+str(n_batch)+'ft'+str(first_fil_n)+'ep'+str(n_ep)model_n = 'Digitizer_'+str(li_n)+'-lin_'+str(dim)+'_'+m_para+'_'+todaymp_flag = input(' Print the model shape?  y or n --> ')# model printif mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model, to_file = './model/'+model_n+'.png', show_shapes=True)    print(' > Model was printed: '+model_n+'.png ')else :    print(' > Model was NOT printed.')# model savems_flag = input('\n Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save('./model/'+model_n+'.h5')    print(' > Model was saved: '+model_n+'.h5')else :    print(' > Model was NOT saved.')# del large datadel x_train ; del x_test# _____________________________ END of code ______________________________________________source_codes_data/Learning_Digitizer_2-lines.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Des.24,2019 / Fixed on Jun.30,2020 < 2本のラインを持つグラフを数値化するための機械学習 >@author: T.KONO"""import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom sklearn.model_selection import train_test_splitimport pickle, time, sysfrom datetime import datetimefrom keras.layers import (Input,                          Conv2D,                          Add,                          Activation,                          Dense,                          GlobalAveragePooling2D,                          MaxPooling2D,                          Dropout                          )from keras.models import (Model)from keras.regularizers import l2# ------------------------- ResNet Model Build Function -----------------------------------def resnet(input_shape,           num_outputs,           m_name,           n_fil,           l_f,           drp_n,           ln           ):    com = ' with l2-regularizer and dropout'    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'plane'    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN101':        print('\n This is ResNet-101'+com)        nb_blocks = [3,4,23,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN152':        print('\n This is ResNet-152'+com)        nb_blocks = [3,4,36,3]        wide = 2        block = 'plane'    elif m_name == 'W-RN':        print('\n This is Wide-ResNet '+com)        nb_blocks = [3,3,3]        wide = 3        block = 'plane'    elif m_name == 'RN26':        print('\n This is ResNet-26'+com)        nb_blocks = [2,2,2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN46':        print('\n This is ResNet-46'+com)        nb_blocks = [3,3,3,4,6,3]        wide = 2        block = 'plane'    elif m_name == 'RN22':        print('\n This is ResNet-22'+com)        nb_blocks = [2,2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN40':        print('\n This is ResNet-40'+com)        nb_blocks = [3,3,4,6,3]        wide = 2        block = 'plane'    else:        print('\n ???')    print(' L2-Regularizer =',l_f)    print(' dropout        =',drp_n)    input = Input(shape=input_shape)    X = input    n_filter = n_fil    reg_f = l_f    # --- Pre convolution -----------------------------------------------------------    X = Conv2D(filters=n_fil,               kernel_size=(3,3),               strides=(2,2),               padding="same",               kernel_regularizer=l2(reg_f)               )(X)    X = Activation("relu")(X)    # poolling    X = MaxPooling2D(pool_size=(3,3),                     strides=(2, 2),                     padding='same'                     )(X)    # --- Residual Net ---------------------------------------------------------------    shortcut = X    if block == 'plane':        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        strides= (2,2),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    if block == 'bottleneck':        shortcut =  Conv2D(n_filter * 4,                                            kernel_size=(1, 1) ,                                            kernel_regularizer=l2(reg_f)                                            )(shortcut)        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter * 4,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                            kernel_size=(1,1),                            strides= (2,2),                            padding="same",                            kernel_regularizer=l2(reg_f)                            )(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(1,1),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                    kernel_size=(3,3),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter * 4,                                    kernel_size=(1,1),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    X = Activation("relu")(X)    # 2-Branch Out    y1 = GlobalAveragePooling2D()(X)    y1 = Dropout(drp_n)(y1)    y1 = Dense(int(num_outputs/2),name='l1')(y1)    y2 = GlobalAveragePooling2D()(X)    y2 = Dropout(drp_n)(y2)    y2 = Dense(int(num_outputs/2),name='l2')(y2)    model = Model(inputs=[input], outputs=[y1,y2])    return model# ----------------------------------------------------------------------------------------------#                     Machine Learning for 2-lines Waveform Digitizer#                      - Mulch GPU#                      - ResNet with dorpout and L2-regularizer#                      - lr_schedul#                      - branch out type# ----------------------------------------------------------------------------------------------# --- Opening ----------------------------------------------------------------------# Date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')print("\n< Machine Learning for 2-lines Waveform Digitizer_",      today,'>')# Start times_time = time.time()# GPU Numbergpu_n = 1# Pass/NG Slice Level: difference between predict and targetsl  = 0.03# --- Data read ---------------------------------------------------------------------# data_n = input('\n Data --> ')data_n ='./data/Digitizer_Data_2-lin.pik';print(' Data:',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)dim  = 1024 # test_graf['para'][0]li_n = 2 # li_n = test_graf['para'][3]# print(' Dimension:',dim,', Line-number:',li_n)print('\nLearning Process Start...')# --- Data import and normalize -------------------------------------------------X   = test_graf['data']# X-normalize and  black-white reversedif np.max(X[0,:]) == 255:    nor = 255       # case of gray dataelse:    nor = 1         # case of monochro dataX = 1 - X/nor# Y-normalizeY = np.reshape(test_graf['out'],(-1,li_n*dim))/dim# input/output shapen_in  = len(X[0])n_out = len(Y[0])# Data splitx_train, x_test, y_train, y_test = train_test_split(        X, Y,        test_size  = 0.2,        train_size = 0.8        )# Delet large datadel X ; del test_graf# --- Itaration parameter ----------------------------------------------------------m_n  = 'RN26'reg_f       = 0n_drop      = 0.1n_epoch     = 100n_batch     = 40first_fil_n = 16n_pat       = 100flag_ver    = 0# --- Model Built -------------------------------------------------------------------input_= x_train.shape[1:]model = resnet(input_,                    n_out,                    m_n,                    first_fil_n,                    reg_f,                    n_drop,                    li_n                    )# print prameterprint('\n Train_n   = ',len(x_train),', Test_n =',len(x_test))print(' epoch        = ',n_epoch)print(' batch         = ',n_batch)print(' first_filter    = ',first_fil_n)print(' patience    = ',n_pat)# print("\n* model_summary") ; model.summary()# --- Model compile ---------------------------------------------------------------# Optimizer settingfrom keras.optimizers import Adamoptimizer = Adam()print('\n Opimizer: Adam')'''## Optimizer Adamfrom keras.optimizers import Adamoptimizer = Adam(lr=0.001)print('\n Opimizer: Adam')## Optimizer SGD + Momentumfrom keras.optimizers import SGDoptimizer = SGD(lr=0.01,decay=1e-6, momentum=0.9, nesterov=True)print('\n Opimizer: SGD + Momentum')'''# 学習率をepoch数により切り替えるfrom keras.callbacks import LearningRateSchedulerdef lr_schedul(epoch):    # default: Adam:0.001, SDG:0.01    # adam    x = 0.001    if epoch >= 100:        x = 0.00075    if epoch >= 200:        x = 0.0005    if epoch >= 400:        x = 0.0005    return xlr_decay = LearningRateScheduler(lr_schedul)# loss settingloss = 'mean_squared_error'print(' loss: ',loss)# Compileif not gpu_n == 1 :    # Multi GPU    print('\n',str(gpu_n)+'-gpu running ')    from keras.utils import multi_gpu_model    parallel_model = multi_gpu_model(model, gpus=gpu_n)    parallel_model.compile(loss      = loss,                            optimizer = optimizer,                            metrics   = ['accuracy']                            )else:    # 1-GPU    print('\n 1-gpu running ')    model.compile(loss       = 'mean_squared_error',                    optimizer  = optimizer,                    metrics    = ['accuracy']                    )early_stopping = EarlyStopping(monitor='loss',                               patience=n_pat)# --- Model fitting -----------------------------------------------------------------if not gpu_n == 1 :    # Multi GPU    hist = parallel_model.fit(x_train, [y_train[:,0:dim],y_train[:,dim:dim*2]],                            epochs           = n_epoch,                            batch_size       = n_batch,                            validation_data  = (x_test, [y_train[:,0:dim],y_train[:,dim:dim*2]]),                            verbose          = flag_ver,                            callbacks        = [early_stopping,lr_decay]                            )else:    # 1-GPU    hist = model.fit(x_train, [y_train[:,0:dim],y_train[:,dim:dim*2]],                    epochs          = n_epoch,                    batch_size      = n_batch,                    validation_data = (x_test, [y_test[:,0:dim],y_test[:,dim:dim*2]]),                    verbose         = flag_ver,                    callbacks       = [early_stopping,lr_decay]                    )# --- Predict and evaluation ------------------------------------------------------predict_ = (model.predict(x_test, batch_size = n_batch))predict  = np.concatenate([predict_[0], predict_[1]],1)dif   = abs((y_test - predict))'''グラフの前後10％は空白の可能性があるこの領域では精度の評価をしない'''spc     = int(dim*0.1)dif_l   = np.reshape(dif,(-1,li_n,dim))dif_ll  = np.reshape(dif_l[:,:,spc:dim-spc],(-1,(dim-2*spc)*li_n))n_ep  = len(hist.history['loss'])# error countpp_list   = []pass_list = []; ng_list=[]for i in range(0,len(y_test)):    # error list    pp = np.max(dif_ll[i,:])    pp_list.append(pp)    if pp > sl:        ng_list.append(i)     # NG list    else:        pass_list.append(i)   # Pass list# number of NGnonzero = len(ng_list)# pass ratecor_pr  = 1 - nonzero/(len(dif_ll))# --- Execution timee_time = time.time()print('... Finished. ')print('\n< Execution time > ')print(' Exe time (hr) = ', '{:6.2f}'.format((e_time - s_time)/3600),      '; end_ep = ',n_ep,      ', exe/ep = ',      '{:4.1f}'.format((e_time - s_time)/n_ep)      )# --- Print out of results -----------------------------------------------------------# loss and accuracyprint('\n< loss and accuracy > ')print( ' loss =','{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', val_loss =','{:4.2e}'.format(hist.history['val_loss'][n_ep-1]),      ',\n l1-acc =','{:4.3f}'.format(hist.history['l1_acc'][n_ep-1]),      ', l2-acc =','{:4.3f}'.format(hist.history['l2_acc'][n_ep-1])      )# loss historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'],label="loss")plt.plot(x_ep,hist.history['val_loss'],label="val_loss")plt.yscale('log')plt.legend(loc='best')plt.title("loss and epoch",fontsize=15)# accuracy historyplt.subplot(2, 2, 2)x_ep = range(len(hist.history['l1_acc']))plt.plot(x_ep,hist.history['l1_acc'],label="l1_acc")plt.plot(x_ep,hist.history['val_l1_acc'],label="l1_val_acc")# plt.plot(x_ep,hist.history['l2_acc'],label="accuracy")# plt.plot(x_ep,hist.history['val_l2_acc'],label="val_acc")#plt.yscale('log')plt.title("accuracy and epoch",fontsize=15)plt.ylim(0, 1.1)plt.legend(loc='best')plt.show()# errorprint('\n< Correct Rate >')print(' Correct answer rate = ','{:4.3f}'.format(cor_pr),      ', Error count  = ',nonzero,'/',len(dif),      ' (sl = ','{:3.2f}'.format(sl),')'      )print('    Note : 10% before and after data are not evaluated. Because there may be a blank.')# --- Result plotting ---------------------------------------------------------------# Best & Worstprint('\n< Best and Worst Sample >')fig = plt.figure(figsize=(7,7))# Best sample idexpp_min = np.amin(pp_list)kp     = pp_list.index(pp_min) # Best sample indexprint(' Best Sample  No.= ','{:3.0f}'.format(kp),      ', diff. = ','{:4.2f}'.format(pp_min))# Worst sample idexpp_max = np.amax(pp_list)kg     = pp_list.index(pp_max)print(' Worst Sample No.= ','{:3.0f}'.format(kg),      ', diff. = ','{:4.2f}'.format(pp_max))print(' upper: graph, lower: comparison of value')cor_list = ['r','b','g','c','m','y','w']f        = np.linspace(0,1,dim)# Best graphplt.subplot(2,2,1)plt.imshow(1-x_test[kp,:,:,0])plt.title("Best Sample",fontsize=12)plt.gray()# comparisonplt.subplot(2,2,3)for ii in range(0,li_n):    plt.plot(f,1-y_test[kp,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict[kp,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)# Worst graphplt.subplot(2,2,2)plt.imshow(1-x_test[kg,:,:,0])plt.title("Worst Sample",fontsize=12)plt.gray()# comparisonplt.subplot(2,2,4)for ii in range(0,li_n):    plt.plot(f,1-y_test[kg,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict[kg,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)plt.show()# NG sample plotprint('\n< NG sample >')fig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(ng_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    # graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    # comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# OK sample plotprint('\n< OK sample >')# --- Special exitif len(pass_list) == 0:    print('\n *** The Proceess was terminated, because an OK sample does not exist. *** \n')    sys.exit()# plotfig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(pass_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    # graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    # comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# --- Model Print and Save --------------------------------------------------------print('\n< Model save and print >')m_para  = m_n+'_bt'+str(n_batch)+'ft'+str(first_fil_n)+'ep'+str(n_ep)model_n = 'Digitizer_'+str(dim)+'_'+str(li_n)+'-lines_BranchOut_'+m_para+'_'+todaymp_flag = input(' Print the model shape?  y or n --> ')# model printif mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model, to_file = './model/'+model_n+'.png', show_shapes=True)    print(' > Model was printed: '+model_n+'.png ')else :    print(' > Model was NOT printed.')# model savems_flag = input('\n Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save('./model/'+model_n+'.h5')    print(' > Model was saved: '+model_n+'.h5')else :    print(' > Model was NOT saved.')# Del large datadel x_train ; del x_test# _______________________________ END of Code _____________________________________________source_codes_data/Learning_Digitizer_3-lines.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Des.24,2019  / Fixed on Jun.30,2020 < 3本のラインを持つグラフを数値化するための機械学習 >@author: T.KONO"""import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom sklearn.model_selection import train_test_splitimport pickle, time, sysfrom datetime import datetimefrom keras.layers import (Input,                          Conv2D,                          Add,                          Activation,                          Dense,                          GlobalAveragePooling2D,                          MaxPooling2D,                          Dropout                          )from keras.models import (Model)from keras.regularizers import l2# ------------------------- ResNet Model Build Function -----------------------------------def resnet(input_shape,           num_outputs,           m_name,           n_fil,           l_f,           drp_n,           ln           ):    com = ' with l2-regularizer and dropout'    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        bottleneck = False    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        bottleneck = False    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,6,3]        wide = 2        bottleneck = True    elif m_name == 'RN101':        print('\n This is ResNet-101'+com)        nb_blocks = [3,4,23,3]        wide = 2        bottleneck = True    elif m_name == 'RN152':        print('\n This is ResNet-152'+com)        nb_blocks = [3,4,36,3]        wide = 2        bottleneck = False    elif m_name == 'W-RN':        print('\n This is Wide-ResNet '+com)        nb_blocks = [3,3,3]        wide = 3        bottleneck = False    elif m_name == 'RN26':        print('\n This is ResNet-26'+com)        nb_blocks = [2,2,2,2,2,2]        wide = 2        bottleneck = False    elif m_name == 'RN46':        print('\n This is ResNet-46'+com)        nb_blocks = [3,4,6,3,3,3]        wide = 2        bottleneck = False    elif m_name == 'RN54':        print('\n This is ResNet-54'+com)        nb_blocks = [3,4,6,6,4,3]        wide = 2        bottleneck = False    elif m_name == 'RN22':        print('\n This is ResNet-22'+com)        nb_blocks = [2,2,2,2,2]        wide = 2        bottleneck = False    elif m_name == 'RN40':        print('\n This is ResNet-40'+com)        nb_blocks = [3,4,6,3,3]        wide = 2        bottleneck = False    else:        print('\n ???')    print(' L2-Regularizer =',l_f)    print(' dropout        =',drp_n)    input = Input(shape=input_shape)    X = input    n_filter = n_fil    reg_f = l_f    # --- 1st Pre convolution -------------------------------------------------------    X = Conv2D(filters=n_fil,               kernel_size=(3,3),               strides=(2,2),               padding="same",               kernel_regularizer=l2(reg_f)               )(X)    X = Activation("relu")(X)    # pooling    X = MaxPooling2D(pool_size=(3,3),                     strides=(2, 2),                     padding='same'                     )(X)    # --- ResNet ---------------------------------------------------------------------    shortcut = X    if bottleneck == False:      for i, repete in enumerate(nb_blocks):        for j in range(repete):          if i>0 and j == 0:            shortcut =  Conv2D(n_filter,                               kernel_size=(1,1),                               strides=(2, 2),                               kernel_regularizer=l2(reg_f)                               )(shortcut)            X = Activation("relu")(X)            X = Dropout(drp_n)(X)            X = Conv2D(n_filter,                     kernel_size=(3,3),                     strides= (2,2),                     padding="same",                     kernel_regularizer=l2(reg_f)                     )(X)          else:            X = Activation("relu")(X)            X = Dropout(drp_n)(X)            X = Conv2D(n_filter,                       kernel_size=(3,3),                       padding="same",                       kernel_regularizer=l2(reg_f)                       )(X)          X = Activation("relu")(X)          X = Dropout(drp_n)(X)          X = Conv2D(n_filter,                     kernel_size=(3,3),                     padding="same",                     kernel_regularizer=l2(reg_f)                     )(X)          # ショートカットとマージ          X = Add()([X, shortcut])          shortcut = X        n_filter *= wide    if bottleneck == True:      shortcut =  Conv2D(n_filter * 4,                         kernel_size=(1, 1) ,                         kernel_regularizer=l2(reg_f)                         )(shortcut)      for i, repete in enumerate(nb_blocks):        for j in range(repete):          if i>0 and j == 0:            shortcut =  Conv2D(n_filter * 4,                               kernel_size=(1, 1),                               strides=(2, 2),                               kernel_regularizer=l2(reg_f)                               )(shortcut)            X = Activation("relu")(X)            X = Dropout(drp_n)(X)            X = Conv2D(n_filter,                     kernel_size=(1,1),                     strides= (2,2),                     padding="same",                     kernel_regularizer=l2(reg_f)                     )(X)          else:            X = Activation("relu")(X)            X = Dropout(drp_n)(X)            X = Conv2D(n_filter,                     kernel_size=(1,1),                     padding="same",                     kernel_regularizer=l2(reg_f)                     )(X)        X = Activation("relu")(X)        X = Dropout(drp_n)(X)        X = Conv2D(n_filter,                  kernel_size=(3,3),                  padding="same",                  kernel_regularizer=l2(reg_f)                  )(X)        X = Activation("relu")(X)        X = Dropout(drp_n)(X)        X = Conv2D(n_filter * 4,                   kernel_size=(1,1),                   padding="same",                   kernel_regularizer=l2(reg_f)                   )(X)        # ショートカットとマージ        X = Add()([X, shortcut])        shortcut = X      n_filter *= wide    X = Activation("relu")(X)    # Branch Out    y1 = GlobalAveragePooling2D()(X)    y1 = Dropout(drp_n)(y1)    y1 = Dense(int(num_outputs/3),name='l1')(y1)    y2 = GlobalAveragePooling2D()(X)    y2 = Dropout(drp_n)(y2)    y2 = Dense(int(num_outputs/3),name='l2')(y2)    y3 = GlobalAveragePooling2D()(X)    y3 = Dropout(drp_n)(y3)    y3 = Dense(int(num_outputs/3),name='l3')(y3)    model = Model(inputs=[input], outputs=[y1,y2,y3])    return model# ----------------------------------------------------------------------------------------------##                     Machine Learning for 3-lines Waveform Digitizer#                      - Mulch GPU#                      - ResNet with dorpout and L2-regularizer#                      - lr_schedul#                      - branch out type## ----------------------------------------------------------------------------------------------# --- Opening ----------------------------------------------------------------------# Date and timetoday  = datetime.today().strftime('%y%m%d-%H%M')print("\n< RUN: Machine Learning for 3-lines Waveform Digitizer",      today,'>')# Start times_time = time.time()# GPU Numbergpu_n = 2# Pass/NG Slice Level, difference between predict and targetsl  = 0.03# --- Data read ---------------------------------------------------------------------# data_n = input(' Data --> ')data_n ='./data/Digitizer_Data_3-lin_l5000+m5000_1024_200319-1030.pik';print(' Data:',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)dim  = 1024 # test_graf['para'][0]li_n = 3print(' Dimension:',dim,', Line-number:',li_n)if not li_n == 3:    print('This Learning Model is for 3-Line only.')    sys.exit()print('\nLearning Process Start...')# --- Data import and normalize -------------------------------------------------X   = test_graf['data']# Y-normalizeY = np.reshape(test_graf['out'],(-1,li_n*dim))/dimdel test_graf# X-normalize and  black-white reversedif np.max(X[0,:]) == 255:    nor = 255       # case of gray dataelse:    nor = 1         # case of monochro dataX = 1 - X/nor# input/output shapen_in  = len(X[0])n_out = len(Y[0])# Data splitx_train, x_test, y_train, y_test = train_test_split(        X, Y,        test_size  = 0.1,        train_size = 0.9        )# Delet large data# del X ;# --- Itaration parameter ----------------------------------------------------------reg_f       = 0n_drop      = 0.1n_epoch     = 200n_batch     = 40first_fil_n = 18n_pat       = 100flag_ver    = 0# --- Model Built ------------------------------------------------------------------input_= x_train.shape[1:]m_n  = 'RN26'model = resnet(input_,                    n_out,                    m_n,                    first_fil_n,                    reg_f,                    n_drop,                    li_n                    )# print prameterprint('\n Train_n   = ',len(x_train),', Test_n =',len(x_test))print(' epoch        = ',n_epoch)print(' batch         = ',n_batch)print(' first_filter    = ',first_fil_n)print(' patience    = ',n_pat)# print("\n* model_summary") ; model.summary()# --- Model compile ---------------------------------------------------------------# Optimizer setting# Optimizer Adamfrom keras.optimizers import Adamoptimizer = Adam()print('\n Opimizer: Adam')'''## Optimizer Adam## Optimizer Adam (lr:default 0.001)from keras.optimizers import Adamoptimizer = Adam(lr=0.001)print('\n Opimizer: Adam')## Optimizer SGD + Momentum(lr:default 0.01)from keras.optimizers import SGDoptimizer = SGD(lr=0.01,decay=1e-6, momentum=0.9, nesterov=True)print('\n Opimizer: SGD + Momentum')'''# 学習率をepoch数により切り替えるfrom keras.callbacks import LearningRateSchedulerdef lr_schedul(epoch):    '''default: adam=0.001, SDG=0.01    '''    x = 0.001    if epoch >= 100:        x = 0.00075    if epoch >= 200:        x = 0.0005    if epoch >= 400:        x = 0.0005    return xlr_decay = LearningRateScheduler(lr_schedul)# loss settingloss = 'mean_squared_error'print(' loss: ',loss)# Compileif not gpu_n == 1 :    # Multi GPU    print('\n',str(gpu_n)+'-gpu running ')    from keras.utils import multi_gpu_model    parallel_model = multi_gpu_model(model, gpus=gpu_n)    parallel_model.compile(loss      = loss,                            optimizer = optimizer,                            metrics   = ['accuracy']                            )else:    # 1-GPU    print('\n 1-gpu running ')    model.compile(loss       = 'mean_squared_error',                    optimizer  = optimizer,                    metrics    = ['accuracy']                    )early_stopping = EarlyStopping(monitor='val_loss',                               patience=n_pat)# --- Model fit ---------------------------------------------------------------------if not gpu_n == 1 :    # Multi GPU    hist = parallel_model.fit(x_train, [y_train[:,0:dim],                                        y_train[:,dim:dim*2],                                        y_train[:,dim*2:dim*3]],                            epochs           = n_epoch,                            batch_size       = n_batch,                            validation_data  = (x_test, [y_test[:,0:dim],                                                         y_test[:,dim:dim*2],                                                         y_test[:,dim*2:dim*3]]),                            verbose          = flag_ver,                            callbacks        = [early_stopping,lr_decay]                            )else:    # 1-GPU    hist = model.fit(x_train, [y_train[:,0:dim],                               y_train[:,dim:dim*2],                               y_train[:,dim*2:dim*3]],                    epochs          = n_epoch,                    batch_size      = n_batch,                    validation_data = (x_test, [y_test[:,0:dim],                                                y_test[:,dim:dim*2],                                                y_test[:,dim*2:dim*3]]),                    verbose         = flag_ver,                    callbacks       = [early_stopping,lr_decay]                    )# --- Predict and evaluation -------------------------------------------------------# Predictpredict_ = (model.predict(x_test, batch_size = n_batch))'''これ以降の処理のためpredictを連結する'''predict  = np.concatenate([predict_[0], predict_[1], predict_[2]],1)# evaluationdif   = abs((y_test - predict))'''グラフの前後10％は空白の可能性があるこの領域では精度の評価をしない'''spc     = int(dim*0.1)dif_l   = np.reshape(dif,(-1,li_n,dim))dif_ll  = np.reshape(dif_l[:,:,spc:dim-spc],(-1,(dim-2*spc)*li_n))n_ep  = len(hist.history['loss'])# error countpp_list   = []pass_list = []; ng_list=[]for i in range(0,len(y_test)):    # error list    pp = np.max(dif_ll[i,:])    pp_list.append(pp)    if pp > sl:        ng_list.append(i)     # NG list    else:        pass_list.append(i)   # Pass list# number of NGnonzero = len(ng_list)# pass ratecor_pr  = 1 - nonzero/(len(dif_ll))# --- Execution time ---e_time = time.time()print('... Finished. ')print('\n< Execution time > ')print(' Exe time (s) = ', '{:6.1f}'.format(e_time - s_time),      '; end_ep = ',n_ep,      ', exe/ep = ',      '{:4.1f}'.format((e_time - s_time)/n_ep)      )# --- Print out of results -----------------------------------------------------------# loss and accuracyprint('\n< loss and accuracy > ')print( ' loss =','{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', val_loss =','{:4.2e}'.format(hist.history['val_loss'][n_ep-1]),      ',\n l1-accuracy =','{:4.3f}'.format(hist.history['l1_acc'][n_ep-1]),      ', l2-accuracy =','{:4.3f}'.format(hist.history['l2_acc'][n_ep-1])      )# loss historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'],label="loss")plt.plot(x_ep,hist.history['val_loss'],label="val_loss")plt.yscale('log')plt.legend(loc='best')plt.title("loss and epoch",fontsize=15)# accuracy historyplt.subplot(2, 2, 2)x_ep = range(len(hist.history['l1_acc']))# plt.plot(x_ep,hist.history['l1_acc'],label="l1_accu")# plt.plot(x_ep,hist.history['val_l1_acc'],label="l1_val_acc")plt.plot(x_ep,hist.history['l1_acc'],label="acc")plt.plot(x_ep,hist.history['val_l1_acc'],label="val_acc")#plt.yscale('log')plt.title("accuracy and epoch",fontsize=15)plt.ylim(0, 1.1)plt.legend(loc='best')plt.show()# errorprint('\n< Correct Rate >')print(' Correct answer rate = ','{:4.3f}'.format(cor_pr),      ', Error count  = ',nonzero,'/',len(dif),      ' (sl = ','{:3.2f}'.format(sl),')'      )print('    Note : 10% before and after data are not evaluated. Because there may be a blank.')# --- Result plotting ---------------------------------------------------------------# Best & Worstprint('\n< Best and Worst Sample >')fig = plt.figure(figsize=(7,7))# Best sample idexpp_min = np.amin(pp_list)kp     = pp_list.index(pp_min) # Best sample indexprint(' Best Sample  No.= ','{:3.0f}'.format(kp),      ', diff. = ','{:4.2f}'.format(pp_min))# Worst sample idexpp_max = np.amax(pp_list)kg     = pp_list.index(pp_max)print(' Worst Sample No.= ','{:3.0f}'.format(kg),      ', diff. = ','{:4.2f}'.format(pp_max))print(' upper: graph, lower: comparison of value')cor_list = ['r','b','g','c','m','y','w']f        = np.linspace(0,1,dim)# Best graphplt.subplot(2,2,1)plt.imshow(1-x_test[kp,:,:,0])plt.title("Best Sample",fontsize=12)plt.gray()#### comparisonplt.subplot(2,2,3)for ii in range(0,li_n):    plt.plot(f,1-y_test[kp,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict[kp,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)# Worst graphplt.subplot(2,2,2)plt.imshow(1-x_test[kg,:,:,0])plt.title("Worst Sample",fontsize=12)plt.gray()# comparisonplt.subplot(2,2,4)for ii in range(0,li_n):    plt.plot(f,1-y_test[kg,dim*ii:dim*(ii+1)],                        cor_list[ii],label="Target")for ii in range(0,li_n):    plt.plot(f,1-predict[kg,dim*ii:dim*(ii+1)],                          cor_list[ii]+':',label="Predict")plt.ylim(0,1);plt.xlim(0,1)plt.show()# NG sample plotprint('\n< NG sample >')fig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(ng_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    ## graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ## comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# OK sample plotprint('\n< OK sample >')# --- Special exit ---if len(pass_list) == 0:    print('\n *** The Proceess was terminated, because an OK sample does not exist. *** \n')    sys.exit()# plotfig    = plt.figure(figsize=(16, 5))n      = 6n_list = np.random.choice(pass_list, n)print(' test number = ', n_list)for i in range(n):    nn = n_list[i]    ## graph    ax = plt.subplot(2,n,i+1)    plt.imshow(1-x_test[nn,:,:,0])    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ## comparison    ax = plt.subplot(2,n,n+i+1)    for ii in range(0,li_n):        plt.plot(f,1-y_test[nn,dim*ii:dim*(ii+1)],                            cor_list[ii],label="Target")    for ii in range(0,li_n):        plt.plot(f,1-predict[nn,dim*ii:dim*(ii+1)],                              cor_list[ii]+':',label="Predict")    plt.ylim(0,1);plt.xlim(0,1)    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# --- Model print and save --------------------------------------------------------print('\n< Model save and print >')m_para  = m_n+'_bt'+str(n_batch)+'ft'+str(first_fil_n)+'ep'+str(n_ep)model_n = 'Digitizer_'+str(li_n)+'-lin_'+str(dim)+'_'+m_para+'_'+todaymp_flag = input(' Print the model shape?  y or n --> ')# model printif mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model, to_file = model_n+'.png', show_shapes=True)    print(' > Model was printed: '+model_n+'.png ')else :    print(' > Model was NOT printed.')# model savems_flag = input('\n Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save('./model/'+model_n+'.h5')    print(' > Model was saved: '+model_n+'.h5')else :    print(' > Model was NOT saved.')# Del large data ---del x_train ; del x_test# _____________________________ END of code ______________________________________________source_codes_data/Learning_LineNumber.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Nov.28,2019 < グラフの本数を読み取るための機械学習 >@author: T.KONO"""# ------------------------- Model Build Function -------------------------------------------"""Residual Net を作成するための関数"""from keras.layers import (Input,                                            Conv2D,                                            Add,                                            Activation,                                            Dense,                                            Dropout,                                            BatchNormalization,                                            GlobalAveragePooling2D,                                            MaxPooling2D                                            )from keras.models import (Model)from keras.regularizers import l2def resnet(input_shape,                    num_outputs,                    m_name,                    n_fil,                    drp_n,                    l_f                    ):    com =' \n  for Classification \n  with Dropout and l2-regularizer'    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'plane'    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,14,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN101':        print('\n This is ResNet-101'+com)        nb_blocks = [3,4,23,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN152':        print('\n This is ResNet-152'+com)        nb_blocks = [3,4,36,3]        wide = 2        block = 'plane'        print('\n ???')    print('\n Dropout =',drp_n,'\n L2-Regularizer =',l_f)    input = Input(shape=input_shape)    X = input    n_filter = n_fil    reg_f = l_f    X = Conv2D(filters=n_fil,                            kernel_size=(7,7),                            strides=(2, 2),                            padding="same",                            )(X)    X = Activation("relu")(X)    # pooling    X = MaxPooling2D(pool_size=(3, 3),                                        strides=(2, 2),                                        padding='same'                                        )(X)    shortcut = X    # X = BatchNormalization()(X)    if block == 'plane':        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        strides= (2,2),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                    # X = BatchNormalization()(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                    # X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X                # X = BatchNormalization()(X)            n_filter *= wide    if block == 'bottleneck':        shortcut =  Conv2D(n_filter * 4,                                            kernel_size=(1, 1) ,                                            kernel_regularizer=l2(reg_f)                                            )(shortcut)        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter * 4,                                                        kernel_size=(1, 1),                                                        strides=(2, 2),                                                        kernel_regularizer=l2(reg_f)                                                        )(shortcut)                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                            kernel_size=(1,1),                            strides= (2,2),                            padding="same",                            kernel_regularizer=l2(reg_f)                            )(X)                    # X = BatchNormalization()(X)                else:                    X = Activation("relu")(X)                    X = Dropout(drp_n)(X)                    X = Conv2D(n_filter,                                        kernel_size=(1,1),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                    # X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter,                                    kernel_size=(3,3),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # X = BatchNormalization()(X)                X = Activation("relu")(X)                X = Dropout(drp_n)(X)                X = Conv2D(n_filter * 4,                                    kernel_size=(1,1),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X                # X = BatchNormalization()(X)            n_filter *= wide    X = Activation("relu")(X)    X = GlobalAveragePooling2D()(X)    X = Dropout(drp_n)(X)    y = Dense(num_outputs,                        activation="softmax"                        )(X)    model = Model(inputs=[input], outputs=[y])    return model# ----------------------------------------------------------------------------------------------#                                   Machine Learning Process# ----------------------------------------------------------------------------------------------import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom keras.utils import np_utilsfrom sklearn.model_selection import train_test_splitimport pickle, timefrom datetime import datetime# --- Opening --------------------------------------------------------------------------# datetoday  = datetime.today().strftime('%y%m%d-%H%M')# Opening printprint('\n ＜ MACHINE LEARNING FOR LINE NUMBER READING __', today,'＞')# start times_time = time.time()# --- Data import  ----------------------------------------------------------------------# data read# data_n = input(' Input Learning Data Name --> \n ')data_n = './data/LineNumber_1-6l_concate_360_6000_20191203-1155.pik'\     ; print('\n Data : ',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)# X-normalize and  black-white reversedim = test_graf['para'][0]X   = test_graf['data']if np.max(X[0,:]) == 255:    nor = 255    # case of gray dataelse:    nor = 1      # case of monochro dataX = 1-X/nor      # black-white reverse# line numberl_n = 6print('\nLeraning Process Start ... ')# Y-categorizeYo = test_graf['out']Y  = np_utils.to_categorical(Yo) # 教師データをカテゴリー型に変換# input/output sizen_in  = len(X[0])n_out = len(Y[0])# Data splitx_train, x_test, y_train, y_test = train_test_split(X, Y,                                                    test_size  = 0.1,                                                    train_size = 0.9                                                    )del X,Y# --- Itaration parameter ----------------------------------------------------------m_n ='RN34'n_epoch      = 200n_batch      = 30first_fil_n  = 24drp_n       = 0.0l2_f         = 1e-6n_pat        = 20flag_ver     = 0# --- Model build --------------------------------------------------------------------input_= x_train.shape[1:]model = resnet(input_,               n_out,               m_n,               first_fil_n,               drp_n,               l2_f               )# --- Model compile -----------------------------------------------------------------# Optimizer# adamprint('\n Opimizer : adam')optimizer = 'adam'''' Optimizer selection# adamprint('\n Opimizer : adam')optimizer = 'adam'# SGD + momentumfrom keras.optimizers import SGDprint('\n Opimizer : SDG + Momentum')optimizer = SGD(decay=1e-6, momentum=0.9, nesterov=True)'''# lossloss = 'categorical_crossentropy'print(' loss     :',loss)# compilemodel.compile(loss       = loss,              optimizer  = optimizer,              metrics    = ['accuracy']              )# estimetor parametor printprint('\n Learn_n =',len(x_train),',Test_n =',len(x_test))print('\n first_filter = ',first_fil_n)print(' batch        = ',n_batch)print(' patience     = ',n_pat)# --- Model fit ----------------------------------------------------------------------early_stopping = EarlyStopping(monitor='val_loss',                               patience=n_pat)hist = model.fit(x_train, y_train,                 epochs          = n_epoch,                 batch_size      = n_batch,                 validation_data = (x_test, y_test),                 verbose         = flag_ver,                 callbacks       = [early_stopping]                 )# --- Predict and evaluation ---------------------------------------------------------predict_ = model.predict(x_test, batch_size=n_batch)dif      = np.round(y_test - predict_)# errror listdif_      = abs(dif)pass_list = []; ng_list=[]for i in range(0,len(y_test)):    if max(dif_[i]) == 0:        pass_list.append(i)    else:        ng_list.append(i)# correct predict ratecpr = 1 - len(ng_list)/(len(dif)) # correct predict rate# Execution timee_time = time.time() # end timen_ep    = len(hist.history['loss'])print('\n... Finished.\n exe time(s)  = ',      '{:6.2f}'.format(e_time - s_time),      ', n_ep =',n_ep)# accuracy printde_loss = hist.history['val_loss'][n_ep-1]-hist.history['loss'][n_ep-1]print('\n< Loss and accuracy >\n loss     = ',      '{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', val_loss = ','{:4.2e}'.format(hist.history['val_loss'][n_ep-1]),      ', delta = ','{:4.2e}'.format(de_loss),      '\n accuracy = ','{:4.3f}'.format(hist.history['acc'][n_ep-1]))# loss and accuracy historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'])plt.plot(x_ep,hist.history['val_loss'])plt.yscale('log')plt.title("loss and epoch",fontsize=15)plt.subplot(2, 2, 2)x_ep = range(len(hist.history['acc']))plt.plot(x_ep,hist.history['acc'])plt.plot(x_ep,hist.history['val_acc'])plt.title("accuracy and epoch",fontsize=15)plt.ylim(0, 1.05)plt.show()print('< Error and correct rate >')print (' error_n = ',len(ng_list),'/',len(dif),       ', correct predict rate = ','{:4.3f}'.format(cpr))# Error sample drawingprint('\n< Error sample >')fig    = plt.figure(figsize=(18,18))n      = 6n_list = np.random.choice(ng_list, n)print(' figure number = ', n_list)for i in range(n):    nn = n_list[i]    print(nn,'pred = ',np.where(np.round(predict_[nn])>0)[0])    ax = plt.subplot(4,n,i+1)    plt.imshow(np.reshape(1-x_test[nn,:], (dim, -1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    plt.ylim(dim,0) ; plt.xlim(0,dim)plt.show()# --- Model print and save ---------------------------------------------------------mp_flag = 'n' ; ms_flag = 'n'model_n = './model/LineNum_l'+str(l_n)+'_'+str(dim)+'_'+str(today)# model printmp_flag = input(' Print the model shape?  y or n --> ')if mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model, to_file= model_n+'.png',               show_shapes=True)    print(' Model is printed --> ',model_n+'.png')else :    print(' Model is not printed. ')# model savems_flag = input(' Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save(model_n+'.h5')    print(' Model is saved --> '+model_n+'.h5')else :    print(' Model is not saved. ')# _______________________________________ END OF CODE ___________________________________source_codes_data/Learning_OrgAxl.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jan.17,2020< グラフ画像からグラフの原点位置とX,Y軸長を読み取る機械学習 >@author: T.KONOで"""# ------------------------- Model Build Function -------------------------------------------"""Residual Net を作成するための関数"""import numpy as npimport matplotlib.pyplot as pltfrom keras.callbacks import EarlyStoppingfrom keras.layers import (Input,                          Conv2D,                          Add,                          Activation,                          Dense,                          GlobalAveragePooling2D,                          MaxPooling2D                          )from keras.models import (Model)from keras.regularizers import l2def resnet(input_shape,           num_outputs,           m_name,           n_fil,           l_f           ):    com = ' with l2-regularizer\n 2-branch Outputs'    n_filter = n_fil    reg_f = l_f    # --- Model parameter define -------------------------------------------------    if m_name == 'RN18':        print('\n This is ResNet-18'+com)        nb_blocks = [2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN34':        print('\n This is ResNet-34'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'plane'    elif m_name == 'RN50':        print('\n This is ResNet-50'+com)        nb_blocks = [3,4,6,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN101':        print('\n This is ResNet-101'+com)        nb_blocks = [3,4,23,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN152':        print('\n This is ResNet-152'+com)        nb_blocks = [3,8,36,3]        wide = 2        block = 'bottleneck'    elif m_name == 'RN26':        print('\n This is ResNet-26'+com)        nb_blocks = [2,2,2,2,2,2]        wide = 2        block = 'plane'    elif m_name == 'RN46':        print('\n This is ResNet-46'+com)        nb_blocks = [3,4,6,3,3,3]        wide = 2        block = 'plane'    else:        print('\n ???')# --- Model Building --------------------------------------------------------------    print('\n L2-Regularizer =',l_f)    input = Input(shape=input_shape)    X = input    #  Initial convolution    X = Conv2D(filters=n_fil,                        kernel_size=(3,3),                        strides=(2,2),                        padding="same",                        kernel_regularizer=l2(reg_f)                        )(X)    X = Activation("relu")(X)    X = MaxPooling2D(pool_size=(3,3),                     strides=(2,2),                     padding='same'                     )(X)    shortcut = X    # Iteration of resnet blocks    if block == 'plane':        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter,                                                    kernel_size=(1, 1),                                                    strides=(2, 2),                                                    kernel_regularizer=l2(reg_f)                                                    )(shortcut)                    X = Activation("relu")(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        strides= (2,2),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                else:                    X = Activation("relu")(X)                    X = Conv2D(n_filter,                                        kernel_size=(3,3),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Conv2D(n_filter,                                    kernel_size=(3,3),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    if block == 'botteleneck':        shortcut =  Conv2D(n_filter * 4,                                        kernel_size=(1, 1) ,                                        kernel_regularizer=l2(reg_f)                                        )(shortcut)        for i, repete in enumerate(nb_blocks):            for j in range(repete):                if i>0 and j == 0:                    shortcut =  Conv2D(n_filter * 4,                                                    kernel_size=(1, 1),                                                    strides=(2, 2),                                                    kernel_regularizer=l2(reg_f)                                                    )(shortcut)                    X = Activation("relu")(X)                    X = Conv2D(n_filter,                                kernel_size=(1,1),                                strides= (2,2),                                padding="same",                                kernel_regularizer=l2(reg_f)                                )(X)                else:                    X = Activation("relu")(X)                    X = Conv2D(n_filter,                                        kernel_size=(1,1),                                        padding="same",                                        kernel_regularizer=l2(reg_f)                                        )(X)                X = Activation("relu")(X)                X = Conv2D(n_filter,                                    kernel_size=(3,3),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                X = Activation("relu")(X)                X = Conv2D(n_filter * 4,                                    kernel_size=(1,1),                                    padding="same",                                    kernel_regularizer=l2(reg_f)                                    )(X)                # ショートカットとマージ                X = Add()([X, shortcut])                shortcut = X            n_filter *= wide    X = Activation("relu")(X)    # Branch to 2-Output    X1 = GlobalAveragePooling2D()(X)    y1 = Dense(2,name='org_out')(X1)    X2 = GlobalAveragePooling2D()(X)    y2 = Dense(2,name='axl_out')(X2)    model = Model(inputs=[input], outputs=[y1,y2])    return model# ----------------------------------------------------------------------------------------------#                                        Machine Learning Process　　　　　　　　　　　　　　　　　# ----------------------------------------------------------------------------------------------from sklearn.model_selection import train_test_splitimport pickle, timefrom datetime import datetime# --- Opening ----------------------------------------------------------------------# get datetoday  = datetime.today().strftime('%y%m%d-%H%M')# opening printprint('\n< RUN: Learning of Origin-position and Axis-length_',today,'>')# get start times_time = time.time()# Pass/Ng slice levelor_sl = 0.03ax_sl = 0.03# --- Data import ------------------------------------------------------------------# data# data_n = input(' Data Name --> \n ')data_n ='./data/xxx.pik'print('\n Data: ',data_n)with open(data_n, mode='rb') as f:    test_graf = pickle.load(f)# X-normalize and  black-white reverseddim = 1024X   = test_graf['data']if np.max(X[0,:]) == 255:    nor = 255    # case of gray dataelse:    nor = 1      # case of monochro dataX = 1-X/nor# Y-normalizeY = test_graf['out']/dim# input/output sizen_in  = len(X[0])n_out = len(Y[0])# Data detail printprint('\n Data dimension      =',dim,      ', dpi =',  test_graf['para'][1])print(' Origin_range        =','{:3.1f}'.format(test_graf['para'][3]),      '-','{:3.1f}'.format(test_graf['para'][4]),      '\n X-axis_length_range =','{:3.1f}'.format(test_graf['para'][5]),      '-','{:3.1f}'.format(test_graf['para'][6]))# --- Data split ---------------------------------------------------------------------x_train, x_test, y_train, y_test = train_test_split(X, Y,                                                    test_size = 0.1,                                                    train_size=0.9                                                    )print('\n Train_n =',len(x_train),',Test_n =',len(x_test))print('\n>> Learning Process Start...')# --- Parameter  input -------------------------------------------------------------m_n  = 'RN34'reg_f       = 1e-6n_epoch     = 200n_batch     = 30first_fil_n = 16n_pat       = 100flag_ver    = 0# --- Model Built -------------------------------------------------------------------input_= x_train.shape[1:]model = resnet(input_,                    n_out,                    m_n,                    first_fil_n,                    reg_f                    )# Optimizer settingoptimizer = 'adam'print('\n Opimizer : adam')'''Optimizer# Optimizer adamoptimizer = 'adam'print('\n Opimizer : adam')# Optimizer SGD + Momentumfrom keras.optimizers import SGDoptimizer = SGD(decay=1e-6, momentum=0.9, nesterov=True)print('\n Opimizer : SGD + Momentum')'''# --- Model compile -------------------------------------------------------------model.compile(loss       = 'mean_squared_error',                        optimizer  = optimizer,                        metrics    = ['accuracy']                        )# itaration parameter printprint('\n first_filter = ',first_fil_n)print(' batch        = ',n_batch)print(' max epoch    = ',n_epoch)print(' patience     = ',n_pat)# --- Model fit --------------------------------------------------------------------early_stopping = EarlyStopping(monitor='val_loss', patience=n_pat)hist = model.fit(x_train, [y_train[:,0:2],y_train[:,2:4]],                 epochs          = n_epoch,                 batch_size      = n_batch,                 validation_data = (x_test, [y_test[:,0:2],y_test[:,2:4]]),                 verbose         = flag_ver,                 callbacks       = [early_stopping]                 )# --- Predict and evaluation ------------------------------------------------------predict_ = model.predict(x_test, batch_size=n_batch)predict  = np.concatenate([predict_[0], predict_[1]],1)# difference between predict and testdif      = (y_test - predict)# error counterror_or      = dif/(y_test)error_ax      = error_ororg_pass_list = []; org_ng_list=[]axl_pass_list = []; axl_ng_list=[]for i in range(0,len(y_test)):    # origin error    if -1*or_sl < error_or[i,0] < or_sl and -1*or_sl < error_or[i,1] < or_sl:        org_pass_list.append(i)    else:        org_ng_list.append(i)    # axis error    if -1*ax_sl < error_ax[i,2] < ax_sl and -1*ax_sl < error_ax[i,3] < ax_sl:        axl_pass_list.append(i)    else:        axl_ng_list.append(i)org_nonzero = len(org_ng_list)org_accuracy     = 1 - org_nonzero/(len(dif))axl_nonzero = len(axl_ng_list)axl_accuracy     = 1 - axl_nonzero/(len(dif))# Execution timee_time = time.time() # end timen_ep = len(hist.history['loss'])print('\n ...Finished. Exe time (s) = ',      '{:6.2f}'.format(e_time - s_time),      ',n_ep =',n_ep)# print("\n* model_summary") ; model.summary()# --- Print out of results -----------------------------------------------------------# loss and accuracyuracy printprint('\n < loss and accuracyuracy >\n loss =',      '{:4.2e}'.format(hist.history['loss'][n_ep-1]),      ', org_accuracyuracy =','{:4.3f}'.format(hist.history['org_out_accuracy'][n_ep-1]),      ', axl_accuracyuracy =','{:4.3f}'.format(hist.history['axl_out_accuracy'][n_ep-1]),      )# loss and accuracyuracy historyfig = plt.figure(figsize=(7, 7))plt.subplot(2, 2, 1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'],'b')plt.plot(x_ep,hist.history['org_out_loss'],'b:')plt.plot(x_ep,hist.history['axl_out_loss'],'b--')plt.plot(x_ep,hist.history['val_loss'],'r')plt.plot(x_ep,hist.history['val_org_out_loss'],'r:')plt.plot(x_ep,hist.history['val_axl_out_loss'],'r--')plt.yscale('log')plt.title("loss and epoch",fontsize=15)plt.subplot(2, 2, 2)plt.plot(x_ep,hist.history['org_out_accuracy'],'b:')plt.plot(x_ep,hist.history['axl_out_accuracy'],'b--')plt.plot(x_ep,hist.history['val_org_out_accuracy'],'r:')plt.plot(x_ep,hist.history['val_axl_out_accuracy'],'r--')plt.title("accuracyuracy and epoch",fontsize=15)plt.ylim(0, 1.1)plt.show()print('\n< Pass_Rate > (Sl; org = +/-','{:3.3f}'.format(or_sl),                      ', axl = +/-','{:3.3f}'.format(ax_sl),')')print('Org_NG = ',org_nonzero,'/',len(dif),      ', Pass_Rate=','{:4.3f}'.format(org_accuracy))print('Axs_NG = ',axl_nonzero,'/',len(dif),      ', Pass_Rate=','{:4.3f}'.format(axl_accuracy))# result evaluation# _error ratefig=plt.figure(figsize=(7, 14))# _origin error rateplt.subplot(4, 2, 3)plt.plot(error_or[:,0],error_or[:,1],'x',color='red') #x-y originplt.title("Origin error rate",fontsize=15)plt.ylim(-1, 1) ; plt.xlim(-1, 1)# _axis error rateplt.subplot(4, 2, 4)plt.plot(error_ax[:,2],error_ax[:,3],'x',color='red') #x-y axis lengthplt.title("Axis_length error rate",fontsize=15)plt.ylim(-1, 1) ; plt.xlim(-1, 1)# _Print error position# _origin error positionplt.subplot(4, 2, 1)for j in org_ng_list:    #print('No.',j,y_test[j,:],dif[j,:])    plt.plot(y_test[j,0], y_test[j,1], 'x',color='red')for k in org_pass_list:    plt.plot(y_test[k,0], y_test[k,1], 'o',color='b')plt.title("Origin_error_position",fontsize=15)plt.ylim(0,1) ; plt.xlim(0,1)# _axis error positionplt.subplot(4, 2, 2)for j in axl_ng_list:    #print('No.',j,y_test[j,:],dif[j,:])    plt.plot(y_test[j,2], y_test[j,3], 'x', color='red')for k in axl_pass_list:    plt.plot(y_test[k,2], y_test[k,3], 'o', color='b')plt.title("Axis_length_error_position", fontsize=15)plt.ylim(0,1) ; plt.xlim(0,1)plt.show()# _NG sample drawingprint('\n < NG figure sample >')fig = plt.figure(figsize=(20,20))n = 10n_list = np.random.choice(org_ng_list, n)print(' - Org NG -')for i in range(n):    nn = n_list[i]    print(nn,', Error',error_or[nn][0:2])    ax = plt.subplot(4,n,i+1)    plt.imshow(np.reshape(1-x_test[nn,:], (dim, -1)))    plt.plot((predict[nn,0])*dim,(1-predict[nn,1])*dim,             'rx',ms=20)    plt.plot((y_test[nn,0])*dim,(1-y_test[nn,1])*dim,             'bx',ms=20)    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    plt.ylim(dim,dim*2/3) ; plt.xlim(0,dim/3)plt.show()fig=plt.figure(figsize=(20,20))n = 8n_list=scale = np.random.choice(axl_ng_list, n)for i in range(n):    nn = n_list[i]    print(nn,', Error',error_or[nn][2:4])    ax = plt.subplot(4, n, i+1)    plt.imshow(np.reshape(1-x_test[nn,:],(dim,- 1)))    plt.plot([y_test[nn,0]*dim, (y_test[nn,0]+predict[nn,2])*dim],             [(1-y_test[nn,1])*dim, (1-y_test[nn,1])*dim],             'r+:',ms=15) # X-axis    plt.plot([y_test[nn,0]*dim, y_test[nn,0]*dim],             [(1-y_test[nn,1])*dim, (1-y_test[nn,1]-predict[nn,3])*dim],             'r+:',ms=15) # Y-axis    plt.gray()    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    plt.ylim(dim+10,0) ; plt.xlim(0,dim+10)plt.show()# --- Model Print and Save -------------------------------------------------------print('< Model Save and Print >')mp_flag = 'n' ; ms_flag = 'n'm_para  = str(dim)+'_'+m_n+'_bt'+str(n_batch)+'ft'+str(first_fil_n)+\    'ep'+str(n_ep)m_name = 'OrgAxl_'+m_para+'_'+str(today)# model printmp_flag = input(' Print the model shape?  y or n --> ')if mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model, to_file = './model/'+m_name+'.png',               show_shapes=True)    print(' Model Shape is printed --> ./model/'+m_name+'.png')else:    print('  ! Model is NOT printed. ')# model savems_flag = input('\n Save the learned model? y or n --> ')if ms_flag == 'y' :    model.save('./model/'+m_name+'.h5')    print(' Model is saved --> ./model/'+m_name+'.h5')else:    print('  ! Model is NOT saved. ')# ______________________________ END OF CODE ____________________________________________source_codes_data/Learning_WaveShape.py# -*- coding: utf-8 -*-'''/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Nov.04,2019< 画像から数値化に不要な部分を取り除く整形を行うための機械学習 >@author: T.KONO'''# ------------------------- Model Build Function -------------------------------------------import numpy as npfrom keras.layers import (Input,                                            Conv2D,                                            MaxPooling2D,                                            Conv2DTranspose,                                            concatenate,                                            UpSampling2D                                            )from keras.models import Modelfrom keras.callbacks import EarlyStoppingfrom keras.regularizers import l2def unet(input_shap,nb_filt,reg_f):    '''    <U-NET モデルを作成 >    input_shap： 入力の形    nb_filt： 最初のフィルタ数 各層毎にx1x2x4x8x16x8x4x2x1    reg_f： L2-regularizer の大きさ    '''    n_conv  = 3    k_size  = 3    n_strid = 2    print('\n This is U-NET 1x2x4x8x16x8x4x2x1')    print(' l2_reg = ',reg_f)    input = Input(shape = input_shap)    X     = input    # Pre Conv    X = Conv2D(nb_filt,                        kernel_size=(21,21),                        strides=(2,2),                        activation='relu',                        padding='same',                        kernel_regularizer=l2(reg_f)                        )(X)    X = MaxPooling2D(pool_size=(3,3),                                    strides=(2,2),                                    padding='same'                                    )(X)    # U-Net    conv1  = Conv2D(nb_filt,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                ) (X)    conv1  = Conv2D(nb_filt,                                (n_conv, n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                ) (conv1)    pool1  = MaxPooling2D(pool_size=(3,3),                                            strides=(2,2),                                            padding='same'                                            )(conv1)    conv2  = Conv2D(nb_filt*2,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(pool1)    conv2  = Conv2D(nb_filt*2,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv2)    pool2  = MaxPooling2D(pool_size=(3,3),                                            strides=(2,2),                                            padding='same'                                            )(conv2)    conv3  = Conv2D(nb_filt*4,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(pool2)    conv3  = Conv2D(nb_filt*4,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv3)    pool3  = MaxPooling2D(pool_size=(3,3),                                            strides=(2,2),                                            padding='same'                                            )(conv3)    conv4  = Conv2D(nb_filt*8,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(pool3)    conv4  = Conv2D(nb_filt*8,                                (n_conv, n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv4)    pool4  = MaxPooling2D(pool_size=(3,3),                                            strides=(2,2),                                            padding='same'                                            )(conv4)    conv5  = Conv2D(nb_filt*16,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(pool4)    conv5  = Conv2D(nb_filt*16,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv5)    up6    = concatenate([Conv2DTranspose(nb_filt*8,                                       (k_size, k_size),                                       strides=(n_strid,n_strid),                                       padding='same',                                       kernel_regularizer=l2(reg_f))                                       (conv5),conv4                                       ],axis=3)    conv6  = Conv2D(nb_filt*8,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(up6)    conv6  = Conv2D(nb_filt*8,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv6)    up7    = concatenate([Conv2DTranspose(nb_filt*4,                                       (k_size, k_size),                                       strides=(n_strid, n_strid),                                       padding='same',                                       kernel_regularizer=l2(reg_f))                                       (conv6),conv3                                       ], axis=3)    conv7  = Conv2D(nb_filt*4,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(up7)    conv7  = Conv2D(nb_filt*4,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv7)    up8    = concatenate([Conv2DTranspose(nb_filt*2,                                       (k_size, k_size),                                       strides=(n_strid, n_strid),                                       padding='same',                                       kernel_regularizer=l2(reg_f))                                       (conv7),conv2                                       ],axis=3)    conv8  = Conv2D(nb_filt*2,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(up8)    conv8  = Conv2D(nb_filt*2,                                (n_conv,n_conv),                                activation='relu',                                padding='same',                                kernel_regularizer=l2(reg_f)                                )(conv8)    X    = concatenate([Conv2DTranspose(nb_filt,                                       (k_size, k_size),                                       strides=(n_strid, n_strid),                                       padding='same',                                       kernel_regularizer=l2(reg_f))                                       (conv8),conv1                                       ],axis=3)    # Post Conv    X  = Conv2D(nb_filt,                        (n_conv,n_conv),                        activation='relu',                        padding='same',                        kernel_regularizer=l2(reg_f)                        )(X)    X = UpSampling2D(size=(2,2))(X)    X = Conv2D(nb_filt,(n_conv,n_conv),                        activation='relu',                        padding='same',                        kernel_regularizer=l2(reg_f)                        )(X)    X = UpSampling2D(size=(2,2))(X)    X  = Conv2D(nb_filt,                        (n_conv,n_conv),                        activation='relu',                        padding='same',                        kernel_regularizer=l2(reg_f)                        )(X)    Y = Conv2D(1,                        (n_conv,n_conv),                        padding='same',                        kernel_regularizer=l2(reg_f)                        )(X)    model  = Model(inputs=[input], outputs=[Y])    return model#----------------------------------------------------------------------------------------------#                    MACHINE LEARNING PROCESS#----------------------------------------------------------------------------------------------import matplotlib.pyplot as pltfrom sklearn.model_selection import train_test_splitimport pickle, timefrom datetime import datetime# --- Opening ----------------------------------------------------------------------now    = datetime.today().strftime('%y%m%d-%H%M')s_time = time.time()# gpu selectiongpu_n = 2# start printprint('\n== Start: MACHINE LEARNING FOR WAVEFORM SHAPING__',      now,'==')s_time = time.time()# --- Data Reading ----------------------------------------------------------------# data_n = input('\n Data Name --> ')data_n = 'WaveShaping_Data_1024_5000_191217-0229.pik'print('\n  < Data >\n ',data_n)with open(data_n, mode='rb') as f:    test_graf=pickle.load(f)# data importdim  = 1024 # test_graf['para'][0]d_nu = len(test_graf['out'])if np.max(test_graf['data'][0,:]) == 255:    nor = 255    # case of gray dataelse:    nor = 1      # case of monochro dataX = (1-test_graf['data']/nor).astype(np.float16)  # black-white reverseY = (1-test_graf['out']/nor).astype(np.float16)del test_graf# data splitx_train, x_test, y_train, y_test = train_test_split(        X, Y,        test_size  = 0.1,        train_size = 0.9,        random_state = 0        )del X ; del Yprint('\nLearning Process Start ...')# --- Model Built -----------------------------------------------------------------# itaration parametern_epoch   = 300n_batch   = 20n_stop    = 30n_filter  = 32leg_f     = 1e-5flag_ver  = 0# model builtinput_  = x_train.shape[1:]model   = unet(input_, n_filter, leg_f )# parametor printprint('\n Learn_n      = ',len(x_train),',Test_n =',len(x_test))print(' max epoch    = ',n_epoch)print(' filter       = ',n_filter)print(' batch size   = ',n_batch)print(' e-stop       = ',n_stop)# ----- Model Compile ------------------------------------------------------------## Optimizer adamoptimizer = 'adam'print('\n Opimizer : adam')'''## Optimizer adamoptimizer = 'adam'print('\n Opimizer : adam')## Optimizer SGD + Momentumfrom keras.optimizers import SGDoptimizer = SGD(decay=1e-6, momentum=0.9, nesterov=True)print('\n Opimizer : SGD + Momentum')'''# loss settingloss = 'mean_squared_error'print(' loss     :',loss)# Compileif not gpu_n == 1 :    # Multi GPU    print('\n',str(gpu_n)+'-GPU RUNNING ')    from keras.utils import multi_gpu_model    parallel_model = multi_gpu_model(model,gpus=gpu_n)    parallel_model.compile(loss      = loss,                                            optimizer = optimizer,                                            metrics   = ['accuracy']                                            )else:    # 1-GPU    print('\n 1-GPU RUNNING ')    model.compile(loss       = loss,                                optimizer  = optimizer,                                metrics    = ['accuracy']                                )early_stopping = EarlyStopping(monitor = 'loss', patience = n_stop)# ----- Model Fit -------------------------------------------------------------------if not gpu_n == 1 :    # Multi GPU    hist = parallel_model.fit(x_train, y_train,                                            epochs           = n_epoch,                                            batch_size       = n_batch,                                            validation_data  = (x_test, y_test),                                            verbose          = flag_ver,                                            callbacks        = [early_stopping]                                            )else:    # 1-GPU    hist = model.fit(x_train, y_train,                                epochs          = n_epoch,                                batch_size      = n_batch,                                validation_data = (x_test, y_test),                                verbose         = flag_ver,                                callbacks       = [early_stopping]                                )# ----- Predict and evaluation ----------------------------------------------------# predictpredict_ = (model.predict(x_test,batch_size=n_batch))# errordif      = np.absolute(y_test-predict_)diff = np.reshape(dif,(-1,dim*dim))y_t  = np.reshape(y_test,(-1,dim*dim))'''y_test と Predict の差分の総和 を\y_test の総和で割った値を誤差の評価値とする*float16のままだと範囲を超えてしまうので32に'''err  = np.sum(diff,axis=1)/(np.sum(y_t.astype(np.float32),axis=1))sl_level =0.1pass_list = []ng_list   = []for i in range(0,len(y_test)):    if err[i] < sl_level :        pass_list.append(i)    else:        ng_list.append(i)# exe.timen_ep   = len(hist.history['loss'])e_time = time.time()print('\n... Finished. Exe_time (s) = ',      '{:6.2f}'.format(e_time - s_time),      ', n_ep = ',n_ep )# --- Results out--------------------------------------------------------------------# loss and accuracyprint('\n < loss and accuracy >')n_ep = len(hist.history['loss'])print(' loss =','{:4.2e}'.format(hist.history['loss'][n_ep-1]),  ', acc =','{:4.3f}'.format(hist.history['acc'][n_ep-1]))fig = plt.figure(figsize=(7,7))plt.subplot(2,2,1)x_ep = range(len(hist.history['loss']))plt.plot(x_ep,hist.history['loss'])plt.plot(x_ep,hist.history['val_loss'])plt.yscale('log')plt.title("loss and val_loss",fontsize=15)# plt.ylim(0.0001,1)plt.subplot(2,2,2)x_ep = range(len(hist.history['acc']))plt.plot(x_ep,hist.history['acc'])# plt.yscale('log')plt.title("accuracy",fontsize=15)plt.ylim(0,1.1)plt.show()# correc rateprint('\n < Correct predict rate > ')correct_rate =len(pass_list)/len(y_test)print(' cpr =','{:6.4f}'.format(correct_rate),      '(',len(pass_list),'/',len(y_test),'),sl=',sl_level)# comparison of figureprint('\n < Pass Figure >')fig=plt.figure(figsize=(16,12))n = 6n_list=np.random.choice(pass_list, n)# print(' figure number = ',n_list)print(' 1st = input, 2nd = predict, 3th = target - predict')for i in range(n):    nn = n_list[i]    print('  Fig ',nn,', error ',err[nn])    ax = plt.subplot(4,n,i+1)    plt.imshow(np.reshape(1-x_test[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ax = plt.subplot(4,n,i+1+n)    plt.imshow(np.reshape(1-predict_[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ax = plt.subplot(4, n, i+1+n+n)    plt.imshow(np.reshape(1-dif[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()print('\n < NG Figure >')fig=plt.figure(figsize=(16,12))n = 6n_list=np.random.choice(ng_list, n)# print(' figure number = ',n_list)print(' 1st = input, 2nd = predict, 3th = target - predict')for i in range(n):    nn = n_list[i]    print('  Fig ',nn,', error ',err[nn])    ax = plt.subplot(4,n,i+1)    plt.imshow(np.reshape(1-x_test[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ax = plt.subplot(4,n,i+1+n)    plt.imshow(np.reshape(1-predict_[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)    ax = plt.subplot(4, n, i+1+n+n)    plt.imshow(np.reshape(1-dif[nn,:].astype(np.float32),                          (dim,-1)),cmap='gray')    ax.get_xaxis().set_visible(False)    ax.get_yaxis().set_visible(False)plt.show()# --- Model Print and Save -------------------------------------------------------print('\n < model output >')mp_flag = 'n' ; ms_flag = 'n'm_para  = 'Unt_bt'+str(n_batch)+'ft'+str(n_filter)+'ep'+str(n_ep)model_n = 'Shaping_'+str(dim)+'_'+str(d_nu)+'_'+m_para+'_'+str(now)# model shape printmp_flag = input(' Print the model shape?  y or n --> ')if mp_flag == 'y' :    from keras.utils import plot_model    plot_model(model,               to_file = model_n+'.png',               show_shapes=True)    print(' --> '+model_n+'.png')else:    print(' ... Not Printed ')# model savems_flag = input(' Save the model? y or n --> ')if ms_flag == 'y' :    model.save(model_n+'.h5')    print(' --> '+model_n+'.h5')else:    print(' ... Not Saved ')# ________________________________ END of Code __________________________________________source_codes_data/Waveform_Digitizer.py# -*- coding: utf-8 -*-"""/*  * Copyright (c) 2018-2020, NIMS. All rights reserved.  *  * This document may not be reproduced or transmitted in any form,  * in whole or in part, without the express written permission of  * the copyright owners.  *  * If this document is published in the NIMS Material Data Repository (MDR),  * the license indicated on the MDR shall take precedence.  */Created on Jan.09,2020 / Fixed on Jun.18,2020< グラフ波形を数値化するプログラム  >/ Final version:  Jun.18,2020, to Takada-san@author: T.KONO"""import cv2import numpy as npfrom PIL import Imagefrom datetime import datetimefrom keras.models import load_modelfrom keras.preprocessing.image import img_to_arrayimport digtizer_lib.dig  as dg# ------------------------- Digitizer Function -----------------------------------------------def digitizer(ff_n,dim):    """< 指定されたグラフ画像の波形を数値化する関数 >    ff_n : 画像ファイル名    dim : dimension    """    # ----- 画像ファイルのOpen ------------------------------------------------------    im_    = Image.open(ff_n)    sz     = im_.size    print('\n------------------------------------------------------')    print('Name:',ff_n)    print('iamge size:',sz[0],'x',sz[1])    # ----- 画像の前処理 ------------------------------------------------------------    # 文字の削除    wo_txt_im = dg.txt_elim(im_,0.05,0)    # 内挿図形の削除    wo_cont = dg.contandline_detect(wo_txt_im[0],1)    # 黒・灰色領域と色領域の分離    mxmi    = 15        # RGB での Max-Min のしきい値    mx      = 200       # HSV(RGBでの Max) の value のしきい値    col_bk  = dg.rgb_mxmi(wo_cont, mxmi, mx)    # 色分離によるラインの抽出    line_data = dg.color_separation(col_bk,f_n,0.3,now,0)    col_n   = line_data['para'][2]    # ----- 原点と軸長さの読み取り --------------------------------------------------    print('\n> Orign-position and Axis-length Read ')    # ガンマ補正    g_fac  = 0.5    g_corr = dg.gamma_corrct(im_.convert('L'),g_fac)    # リサイズ    re_ = cv2.resize(g_corr[0],(dim,dim),cv2.INTER_LANCZOS4)    # 整形と正規化及び白黒反転    arr_1024        = 1 - np.reshape(re_,(1,dim,dim,1))/255    # 学習モデルによる読み取り    prd_ol_= orgaxl_model.predict_on_batch(arr_1024)    prd_ol = (np.concatenate([prd_ol_[0],prd_ol_[1]],1))*(dim-1)    prd_ol = prd_ol.astype(np.int16)    print(' Origin point = ','{:3.0f}'.format(prd_ol[0,0]),          ',','{:3.0f}'.format(dim-prd_ol[0,1],)          )    print(' Axis length  = ','{:3.0f}'.format(prd_ol[0,2]),          ',','{:3.0f}'.format(prd_ol[0,3])          )    # ----- 画像整形 -----------------------------------------------------------------    print('\n> Shaping of Waveform ')    # 浸食処理    ''' 色のクラスタ数が画像の総ピクセル数の 0.001 以下の場合    elode correction を発動する    '''    if line_data['para'][3] < sz[0]*sz[1]*0.001:        en = 1    else:        en = 0    # トリミングとリサイズ    color_data = []    for i in range(0,col_n+1):        color_line = np.reshape(line_data['data'][i],                                              (dim,dim,1)                                              )        # トリミング        im_tr      = color_line[(dim-prd_ol[0,1]-prd_ol[0,3]):                                (dim-prd_ol[0,1]),                                (prd_ol[0,0]):                                    (prd_ol[0,0]+prd_ol[0,2])]        # リサイズ        im_re      = cv2.resize(im_tr,                                (dim,dim),                                cv2.INTER_LANCZOS4                                )        # 黒ラインのみ直線削除を導入        if i == col_n:            im_re = dg.linedel(im_re)        # 軸領域のトリミング        '''黒領域の X,Y軸 および枠線の消え残りを消去する        外側から dim/50 の領域をすべて白に塗りつぶす        '''        ddp=int(dim/50)        if i== col_n:            im_re[0:ddp,0:dim]=255            im_re[0:dim,0:ddp]=255            im_re[dim-ddp:dim,0:dim]=255            im_re[0:dim,dim-ddp:dim]=255        array = np.reshape(im_re,(dim,dim,1))        # 浸食処理        it_n     = en   # 繰り返し回数        ngh      = 'n4' # 近接領域の取り方 n4 or n8        if it_n == 0 :            color_data.append(array)        else :            er_array  = np.reshape(dg.erode(array,0,it_n,ngh),                                   (dim,dim,1)                                   )            color_data.append(er_array)    # 正規化と白黒反転    color_data = (1 - np.asarray(color_data)/255)    # 学習モデルを呼び出して波形整形    '''学習モデルを適用した整形は黒ラインのみに適用する。色ラインに適用すると    必要なラインを消してしまうなど副作用が大きすぎるため    color_data   = shap_model.predict_on_batch(color_data)    '''    color_data[col_n] = shap_model.predict_on_batch(np.reshape(color_data[col_n],                                                                                                            [1,dim,dim,1]))    # 各ラインの点数を数える    lpn        = []    for ii in range(0,col_n+1):        # line point count        '''ラインの点数を数え lpn に収納        '''        ll   = (np.reshape(color_data[ii,:],                           (dim,-1))).astype(np.uint8)        lp_  = np.count_nonzero(ll[:,50:dim-50])        lpn.append(lp_)    # ----- 波形本数の読み取り ------------------------------------------------------    print('\n> Line Number Read ')    # 波形本数の読み取りのdimensionは360なので各ラインデータをリサイズする    col360=[]    for j in range(0,col_n+1):        c_data= cv2.resize(color_data[j],(360,360),                           cv2.INTER_LANCZOS4)        col_ar=img_to_array(c_data).astype(np.uint8)        col360.append(col_ar)    # 学習モデルを呼び出して本数を読み取る    col360 = np.asarray(col360)    predict_num  = np.round(ln_model.predict(col360))    lin_nu       = np.argmax(predict_num, axis=1)    # 黒ラインの存在判定    ''' 黒ラインの点数が色ライン点数平均の 0.2 以下でかつ点数がdim＊2以下        の場合は黒ラインはラインと認めない    '''    if not col_n == 0:        print(' Line point number =',lpn)        lp_ave = sum(lpn[0:col_n])/col_n        # print(lp_ave)        if lpn[col_n] <= lp_ave*0.2 and lpn[j] < dim*2:            lin_nu[col_n] = 0            print(' * Black-line dose not exist ')            ng_bk = lpn[col_n]        else:            ng_bk = 0    else :        print (' Black line only. ')        ng_bk = 0    # 色ラインの存在判別    '''色ラインの点数が すべての色ラインの点数の平均の1/3以下で、    かつその点数がdim＊1.5以下のものはラインと認めない    '''    for j in range(0,col_n):        if lpn[j]< lp_ave/3 and lpn[j] < dim*1.5:            lin_nu[j] = 0            print(' * Color '+str(j+1)+' has no enough data ')    print(' Line Number = ',sum(lin_nu), ', ',lin_nu)    # ----- 波形の数値化 ------------------------------------------------------------    print('\n> Digitalizing of Waveform ')    l_data = []    # line read    for j in range(0,col_n+1):        data    = np.reshape(color_data[j,:,:,:],(1,dim,dim,1))        if lin_nu[j] != 0 :            '''波形の左右に空白が存在する場合、この空白の ｘ の値を見つける            x毎に、y方向に値の総和を計算、この総和が０もしくは十分小さい場合            この ｘ 位置を空白と見なす            '''            aa = (color_data[j,:,:,0]*255).astype('uint8')/255            y_s = np.zeros([dim])            for i in range(0,dim):                y_sam  = sum(aa[:,i])  # Y方向に総和をとる                if y_sam <= 1.:                    # 小さなゴミは無視する                    y_s[i] = 0                else :                    y_s[i] = y_sam            # 左側の空白の終わりのインデックス str を見つける 0-str            srt = np.amin(np.where(y_s != 0))            # 右側の空白の始まりのインデックス stp を見つける ｓｔｐ-dim            stp = np.amax(np.where(y_s != 0))+1        else:            srt = 0            stp = dim        # 学習モデルを読み出して数値化        if lin_nu[j] == 1 :            # 1本波形の数値化            predict_line  = model1.predict_on_batch(data)            # 空白部分のデータを nan に置換            predict_line[0,0:srt]   = np.nan            predict_line[0,stp:dim] = np.nan            l_data.append(predict_line)        elif lin_nu[j] == 2 :            # 2本波形の数値化            predict_line = model2.predict_on_batch(data)            # 空白部分のデータを nan に置換            for ii in range(0,lin_nu[j]):                predict_line[ii][0,0:srt]   = np.nan                predict_line[ii][0,stp:dim] = np.nan                l_data.append(predict_line[ii])        elif lin_nu[j] == 3 :            # 3本波形の数値化            predict_line = model3.predict_on_batch(data)            # 空白部分のデータを nan に置換            for ii in range(0,lin_nu[j]):                predict_line[ii][0,0:srt]   = np.nan                predict_line[ii][0,stp:dim] = np.nan                l_data.append(predict_line[ii])    l_data = (np.asarray(l_data)).T    # ----- X,Y軸の範囲を読み取りデータを補正する-----------------------------------    # 軸数値の読み取り    gain = dg.axgain(ff_n,sz,prd_ol)    # データ補正    xd   = np.linspace(gain[0],gain[1],dim)    yg = abs(gain[3]-gain[2])/1    y0 = gain[2]    # ----- 数値化結果の評価--------------------------------------------------------    relblty= dg.wf_eval(wo_cont,prd_ol,l_data,ng_bk,col_n,lin_nu,dim)    print(' Reliability =','{:3.2f}'.format((relblty)[0]))    # -----  結果をcsvファイルとして保存 ----------------------------------------------    print('\n> Data Save ')    val_save = 'y'    '''if relblty[0] < 0.4 :        print(' * Reliability is wrong.')        val_save = 'n'    # val_s = input(' Save data to csv file?  y or n --> ')    '''    if val_save == 'y' :        ydata  = (1-l_data[0:dim,0,0:sum(lin_nu)])*yg+y0        xydata = np.insert(ydata,0,xd,axis=1)        np.savetxt(ff_n+'.csv',                   xydata,                    delimiter=',',                    fmt ='%.4f',                    )        print(' The waveform datas were saved ')    else:        print(' The waveform data were NOT saved ')    return# ----------------------------------------------------------------------------------------------##                                         Execution of  Digitizing## ----------------------------------------------------------------------------------------------import os,time'''画像ファイルの選別を行い、規格に合致した場合は digtizer を呼び出し数値化を行う'''# ----- Opening --------------------------------------------------------------------now   = datetime.today().strftime('%Y/%m/%d/%H:%M:%S')s_t = time.time()print('\n< Run: 1024x1024 Waveform Digitizer ver-α_',now,'>')dim   = 1024# ----- 学習モデルの呼び出し --------------------------------------------------------# 学習モデル名orgaxl  = './model/OrgAxl_1024.h5'shaping = './model/Shaping_1024.h5'linnum  = './model/LineNum_360.h5'line_1  = './model/Digitizer_1-lin_1024.h5'line_2  = './model/Digitizer_2-lines_1024.h5'line_3  = './model/Digitizer_3-lines_1024.h5'fig_sel = './model/FigSelection_256.h5'# 学習モデルのロードorgaxl_model = load_model(orgaxl)shap_model   = load_model(shaping)ln_model     = load_model(linnum)model1       = load_model(line_1)model2      = load_model(line_2)model3       = load_model(line_3)fig_sel_model = load_model(fig_sel)# ----- 画像検索 --------------------------------------------------------------------#   画像の種類とディレクトリの指定f_type = '.png'print('\n Search figure type :',f_type)directory = 'E:/test'print('\n<',directory,'> Serch Start ...')loop  = 0list=[]# 種類の一致する画像の検索for root, dirs, files in os.walk(directory,topdown = False):    for name in files:        if name.endswith(f_type) :            list.append([root,name])            loop  = loop+1# 画像リストの作成out_list = np.asarray(list)# ----- 画像を選別して数値化-------------------------------------------------------t_n  = len(out_list)ng_r = 0ng_f = 0error    = []ok_list  = []for n in range (0,t_n):    # 画像ファイル名    f_n  = out_list[n][1]    ff_n = out_list[n][0]+'/'+out_list[n][1]    try:        '''fileが開けない場合でも、処理を続行        '''        # 画像ファイルのOpen        gray    = cv2.imread(ff_n,0) # Gray image        sz      = gray.size        # リサイズ        re_256  = cv2.resize(gray,(256,256),cv2.INTER_LANCZOS4)        # 画像整形と正規化及び白黒反転        arr_256 = 1 - np.reshape(re_256,(1,256,256,1))/255        # 学習モデルを呼び出し画像を選別        p_n_   = fig_sel_model.predict_on_batch(arr_256)        p_n    = np.argmax(np.round(p_n_,0))    except:        pass        p_n    = 9    now_now = datetime.today().strftime('%Y/%m/%d/%H:%M:%S')    # 画像数値化    '''p_n= 1: 規格に合った画像として数値化を行う    p_n= 0: 規格外の画像として数値化を行わない    p_n= 9: ファイルが読めなかった, 数値化を行わない    '''    if p_n == 1:        try:            '''内部でerrorが発生した場合、error logを発行して処理を中止し、次の画像へ進む            '''            # 画像数値化            digitizer(ff_n, 1024)            ok_list.append([ff_n])        except:            pass            # Error log            log_ = str(now_now)+' | ========= Digitizer ERROR =========== | '+str(ff_n)            error.append([log_])            ng_r = ng_r+1    elif p_n == 9:        log_ = str(now_now)+' | ========= File Read ERROR =========== | '+str(ff_n)        error.append([log_])        ng_r = ng_r+1    else:        log_ = str(now_now)+' | This file does not fit in my standard | '+str(ff_n)        error.append([log_])        ng_f = ng_f +1# ----- 結果のまとめ ------------------------------------------------------------------# エラーをエラーログファイルにまとめるimport csvwith open(directory+'/Error_log.csv','w') as f:    writer =csv.writer(f,lineterminator='\n')    writer.writerows(error)# OK ファイルのリストをファイルにまとめるwith open(directory+'/OK_list.csv','w') as f:    writer =csv.writer(f,lineterminator='\n')    writer.writerows(ok_list)# 実行時間print('\n------------------------------------------------------')e_t = time.time()print('\n ... Finish. Exe.Time = ','{:6.2f}'.format(e_t-s_t),'s')# 結果の概要出力print('\n PNG file Number: ',t_n,      '\n  OK:',len(ok_list),      '\n  Out of standard:',ng_f,      '\n  Error:',ng_r      )# ________________________________ END of Code ___________________________________________