## Author: SHIVANK SHARMA ## Modified: Toshio Iwata ## Released under Apache 2.0 license import numpy as np import pandas as pd import os import cv2 import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras from PIL import Image from sklearn.model_selection import train_test_split from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.optimizers import Adam from sklearn.metrics import accuracy_score np.random.seed(42) from matplotlib import style style.use('fivethirtyeight') data_dir = './' train_path = './train_1' test_path = './' # Resizing the images to 30x30x3 IMG_HEIGHT = 30 IMG_WIDTH = 30 channels = 3 NUM_CATEGORIES = len(os.listdir(train_path)) NUM_CATEGORIES # Label Overview ### Modified for lisa classes = { 0:'posi', 1:'nega', 2:'white', 3:'beju', 4:'black', 5:'wh2bk', 6:'bk2wh', 7:'wh_bk', 8:'bk_wh', 9:'bumpy' } ################################################### ## for making models ################################################### # モデルを作成 model1 = keras.Sequential(name="my_model1") # model1を確認 print(model1) model1.add(keras.layers.Conv2D(filters=16, kernel_size=(3,3), activation='relu', input_shape=(IMG_HEIGHT,IMG_WIDTH,channels))) model2 = keras.Sequential(name="my_model2") # model2を確認 print(model2) model2.add(keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28,28,16))) model2.add(keras.layers.MaxPool2D(pool_size=(2, 2))) model3 = keras.Sequential(name="my_model3") # model3を確認 print(model3) model3.add(keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', input_shape=(13,13,32))) model4 = keras.Sequential(name="my_model4") # model4を確認 print(model4) model4.add(keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu', input_shape=(11,11,64))) model4.add(keras.layers.MaxPool2D(pool_size=(2, 2))) model4.add(keras.layers.Flatten()) model5 = keras.Sequential(name="my_model5") # model5を確認 print(model5) ##model5.add(keras.layers.Dense(256, activation='relu', input_dim=2048)) model5.add(keras.layers.Dense(128, activation='relu', input_dim=2048)) model5.add(keras.layers.Dropout(rate=0.5)) model6 = keras.Sequential(name="my_model6") # model6を確認 print(model6) ##model6.add(keras.layers.Dense(43, activation='softmax', input_dim=256)) ### Midified for lisa model6.add(keras.layers.Dense(10, activation='softmax', input_dim=128)) import csv ################################################### ## for read ################################################### ########################### lay1 lay1 = model1.layers[0] tmpw = lay1.get_weights() with open('w1.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("w1 shape : ", lay1.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay1.get_weights()[0].shape[3]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 tmpval = int(tmpval) ### limitter 2023/11/08 if tmpval > 255 : tmpval = 255 elif tmpval < -256 : tmpval = -256 tmpw[0][r][c][i][k] = tmpval ###row[k] ### [0]縺ォ驥阪∩菫よ焚 c += 1 if(c == range(lay1.get_weights()[0].shape[1]).stop): c = 0 r += 1 if(r == range(lay1.get_weights()[0].shape[0]).stop): r = 0 i += 1 with open('b1.csv', mode='r') as f: reader = csv.reader(f) print("b1 shape : ", lay1.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay1.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ###row[k] ### [1]縺ォ繝舌う繧「繧ケ lay1.set_weights(tmpw) print("w1 read done") ########################### lay2 lay2 = model2.layers[1-1] tmpw = lay2.get_weights() with open('w2.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("w2 shape : ", lay2.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay2.get_weights()[0].shape[3]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 ## tmpval = int(tmpval) if tmpval > 48 : tmpval = 64 elif tmpval >= 24 : tmpval = 32 elif tmpval >= 8 : tmpval = 16 elif tmpval >= 0 : tmpval = 4 elif tmpval >= -8 : tmpval = -4 elif tmpval >= -24 : tmpval = -16 elif tmpval >= -48 : tmpval = -32 else : tmpval = -64 tmpw[0][r][c][i][k] = tmpval ###row[k] ### [0]縺ォ驥阪∩菫よ焚 c += 1 if(c == range(lay2.get_weights()[0].shape[1]).stop): c = 0 r += 1 if(r == range(lay2.get_weights()[0].shape[0]).stop): r = 0 i += 1 with open('b2.csv', mode='r') as f: reader = csv.reader(f) print("b2 shape : ", lay2.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay2.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ###row[k] ### [1]縺ォ繝舌う繧「繧ケ lay2.set_weights(tmpw) print("w2 read done") ########################### lay3 lay3 = model3.layers[3-3] tmpw = lay3.get_weights() with open('w3.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("w3 shape : ", lay3.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay3.get_weights()[0].shape[3]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 ## tmpval = int(tmpval) if tmpval > 48 : tmpval = 64 elif tmpval >= 0 : tmpval = 16 elif tmpval >= -48 : tmpval = -16 else : tmpval = -64 tmpw[0][r][c][i][k] = tmpval ## row[k] ### [0]縺ォ驥阪∩菫よ焚 c += 1 if(c == range(lay3.get_weights()[0].shape[1]).stop): c = 0 r += 1 if(r == range(lay3.get_weights()[0].shape[0]).stop): r = 0 i += 1 with open('b3.csv', mode='r') as f: reader = csv.reader(f) print("b3 shape : ", lay3.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay3.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ## row[k] ### [1]縺ォ繝舌う繧「繧ケ lay3.set_weights(tmpw) print("w3 read done") ########################### lay4 lay4 = model4.layers[4-4] tmpw = lay4.get_weights() with open('w4.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("w4 shape : ", lay4.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay4.get_weights()[0].shape[3]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 # tmpval = int(tmpval) if tmpval > 48 : tmpval = 64 elif tmpval >= 0 : tmpval = 16 elif tmpval >= -48 : tmpval = -16 else : tmpval = -64 tmpw[0][r][c][i][k] = tmpval ## row[k] ### [0]縺ォ驥阪∩菫よ焚 c += 1 if(c == range(lay4.get_weights()[0].shape[1]).stop): c = 0 r += 1 if(r == range(lay4.get_weights()[0].shape[0]).stop): r = 0 i += 1 with open('b4.csv', mode='r') as f: reader = csv.reader(f) print("b4 shape : ", lay4.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay4.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ## row[k] ### [1]縺ォ繝舌う繧「繧ケ lay4.set_weights(tmpw) print("w4 read done") ########################### lay5 lay5 = model5.layers[7-7] tmpw = lay5.get_weights() with open('w5.csv', mode='r') as f: reader = csv.reader(f) i = 0 k = 0 print("w5 shape : ", lay5.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay5.get_weights()[0].shape[1]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 # tmpval = int(tmpval) if tmpval > 48 : tmpval = 64 elif tmpval >= 0 : tmpval = 16 elif tmpval >= -48 : tmpval = -16 else : tmpval = -64 tmpw[0][i][k] = tmpval ### row[k] ### [0]縺ォ驥阪∩菫よ焚 i += 1 with open('b5.csv', mode='r') as f: reader = csv.reader(f) print("b5 shape : ", lay5.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay5.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ## row[k] ### [1]縺ォ繝舌う繧「繧ケ lay5.set_weights(tmpw) print("w5 read done") ########################### lay6 lay6 = model6.layers[9-9] tmpw = lay6.get_weights() with open('w6.csv', mode='r') as f: reader = csv.reader(f) i = 0 k = 0 print("w6 shape : ", lay6.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay6.get_weights()[0].shape[1]): tmpval = row[k] ### [0]縺ォ驥阪∩菫よ焚 tmpval = float(tmpval) * 256 tmpval = int(tmpval) ### limitter 2023/11/08 if tmpval > 255 : tmpval = 255 elif tmpval < -256 : tmpval = -256 # if tmpval > 48 : # tmpval = 64 # elif tmpval >= 0 : # tmpval = 16 # elif tmpval >= -48 : # tmpval = -16 # else : # tmpval = -64 tmpw[0][i][k] = tmpval ### row[k] ### [0]縺ォ驥阪∩菫よ焚 i += 1 with open('b6.csv', mode='r') as f: reader = csv.reader(f) print("b6 shape : ", lay6.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay6.get_weights()[1].shape[0]): tmpval = row[k] ### [1]縺ォ繝舌う繧「繧ケ tmpval = float(tmpval) * 256 * 256 tmpval = int(tmpval) tmpw[1][k] = tmpval ## row[k] ### [1]縺ォ繝舌う繧「繧ケ lay6.set_weights(tmpw) print("w6 read done") ################################################### ## for write ################################################### ########################### lay1 with open("w1_int.csv", mode='w') as f: for i in range(0, lay1.get_weights()[0].shape[2]): for r in range(0, lay1.get_weights()[0].shape[0]): for c in range(0, lay1.get_weights()[0].shape[1]): for k in range(0, lay1.get_weights()[0].shape[3]): tmpval = lay1.get_weights()[0][r][c][i][k] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b1_int.csv", mode='w') as f: for c in range(0, lay1.get_weights()[1].shape[0]): tmpval = lay1.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w1 write done") ########################### lay2 with open("w2_int.csv", mode='w') as f: for i in range(0, lay2.get_weights()[0].shape[2]): for r in range(0, lay2.get_weights()[0].shape[0]): for c in range(0, lay2.get_weights()[0].shape[1]): for k in range(0, lay2.get_weights()[0].shape[3]): tmpval = lay2.get_weights()[0][r][c][i][k] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b2_int.csv", mode='w') as f: for c in range(0, lay2.get_weights()[1].shape[0]): tmpval = lay2.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w2 write done") ########################### lay3 with open("w3_int.csv", mode='w') as f: for i in range(0, lay3.get_weights()[0].shape[2]): for r in range(0, lay3.get_weights()[0].shape[0]): for c in range(0, lay3.get_weights()[0].shape[1]): for k in range(0, lay3.get_weights()[0].shape[3]): tmpval = lay3.get_weights()[0][r][c][i][k] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b3_int.csv", mode='w') as f: for c in range(0, lay3.get_weights()[1].shape[0]): tmpval = lay3.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w3 write done") ########################### lay4 with open("w4_int.csv", mode='w') as f: for i in range(0, lay4.get_weights()[0].shape[2]): for r in range(0, lay4.get_weights()[0].shape[0]): for c in range(0, lay4.get_weights()[0].shape[1]): for k in range(0, lay4.get_weights()[0].shape[3]): tmpval = lay4.get_weights()[0][r][c][i][k] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b4_int.csv", mode='w') as f: for c in range(0, lay4.get_weights()[1].shape[0]): tmpval = lay4.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w4 write done") ########################### lay5 with open("w5_int.csv", mode='w') as f: for r in range(0, lay5.get_weights()[0].shape[0]): for c in range(0, lay5.get_weights()[0].shape[1]): tmpval = lay5.get_weights()[0][r][c] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b5_int.csv", mode='w') as f: for c in range(0, lay5.get_weights()[1].shape[0]): tmpval = lay5.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w5 write done") ########################### lay6 with open("w6_int.csv", mode='w') as f: for r in range(0, lay6.get_weights()[0].shape[0]): for c in range(0, lay6.get_weights()[0].shape[1]): tmpval = lay6.get_weights()[0][r][c] ### [0]に重み係数 f.write(str(tmpval)) f.write(', ') f.write('\n') with open("b6_int.csv", mode='w') as f: for c in range(0, lay6.get_weights()[1].shape[0]): tmpval = lay6.get_weights()[1][c] ### [1]にバイアス f.write(str(tmpval)) f.write(', ') print("w6 write done") test = pd.read_csv(data_dir + '/TestCrack.csv') ### Modified for lisa ## Modified for Shiroume def resize (image, size): h, w = image.shape[:2] aspect = w / h nh = nw = size if 1 >= aspect: nw = round(nh * aspect) else: nh = round(nw / aspect) resized = cv2.resize(image, dsize=(nw, nh)) return resized ## Modified for Shiroume def shiroume (image, size): resized = resize(image, size) h, w = resized.shape[:2] x = y = 0 if h < w: y = (size - h) // 2 else: x = (size - w) // 2 resized = Image.fromarray(resized) #一旦PIL形式に変換 canvas = Image.new(resized.mode, (size, size), (255, 255, 255)) # 黒埋めなら(0,0,0)にする canvas.paste(resized, (x, y)) dst = np.array(canvas) #numpy(OpenCV)形式に戻す return dst labels = test["ClassId"].values imgs = test["Path"].values data =[] for img in imgs: try: image = cv2.imread(data_dir + '/' +img) ## Modified for Shiroume h, w = image.shape[:2] if h < w : val = w else : val = h ume_image = shiroume(image, val) #リサイズしたいサイズを指定 image_fromarray = Image.fromarray(image, 'RGB') ## ume_image for shiroume resize_image = image_fromarray.resize((IMG_HEIGHT, IMG_WIDTH)) data.append(np.array(resize_image)) except: print("Error in " + img) X_test = np.array(data) ####X_test = X_test/255 print("x_test shape : ", X_test.shape) ######### star prediction lay1out = model1.predict(X_test) print("lay1out shape : ", lay1out.shape) lay1out = lay1out / 256 lay2out = model2.predict(lay1out) print("lay2out shape : ", lay2out.shape) lay2out = lay2out / 256 lay3out = model3.predict(lay2out) print("lay3out shape : ", lay3out.shape) lay3out = lay3out / 256 lay4out = model4.predict(lay3out) print("lay4out shape : ", lay4out.shape) lay4out = lay4out / 256 lay5out = model5.predict(lay4out) print("lay5out shape : ", lay5out.shape) lay5out = lay5out / 256 lay6out = model6.predict(lay5out) pred = np.argmax(lay6out, axis=-1) #Accuracy with the test data print('Test Data accuracy: ',accuracy_score(labels, pred)*100) ''' errno = 0 plt.figure(figsize = (25, 25)) for i in range(1470): prediction = pred[i] actual = labels[i] if prediction != actual: print('prediction error ', i, ' : pred ', pred[i], ' : actual ', labels[i]) plt.subplot(3, 5, errno + 1) plt.grid(False) plt.xticks([]) plt.yticks([]) plt.imshow(X_test[i]) errno = errno + 1 ''' from sklearn.metrics import confusion_matrix cf = confusion_matrix(labels, pred) import seaborn as sns df_cm = pd.DataFrame(cf, index = classes, columns = classes) plt.figure(figsize = (20,20)) sns.heatmap(df_cm, annot=True) from sklearn.metrics import classification_report print(classification_report(labels, pred)) plt.figure(figsize = (25, 25)) ### Modified for lisa ### changed index of images to show up for i in range(30): plt.subplot(6, 5, i + 1) plt.grid(False) plt.xticks([]) plt.yticks([]) plt.rcParams["font.size"] = 16 if i%10 == 0 : rndval = int(i/10) elif i%10 == 1 : rndval = 2066 + int(i/10) elif i%10 == 2 : rndval = 4066 + 0 + int(i/10) elif i%10 == 3 : rndval = 4066 + 3 + int(i/10) elif i%10 == 4 : rndval = 4066 + 6 + int(i/10) elif i%10 == 5 : rndval = 4066 + 9 + int(i/10) elif i%10 == 6 : rndval = 4066 + 12 + int(i/10) elif i%10 == 7 : rndval = 4066 + 15 + int(i/10) elif i%10 == 8 : rndval = 4066 + 18 + int(i/10) elif i%10 == 9 : rndval = 4087 + int(i/10) prediction = pred[rndval] actual = labels[rndval] col = 'g' if prediction != actual: col = 'r' plt.xlabel('Actual={} || Pred={}'.format(actual, prediction), color = col) for m in range(0, X_test[rndval].shape[0]): for n in range(0, X_test[rndval].shape[1]): tmpg = X_test[rndval][m][n][0] tmpr = X_test[rndval][m][n][2] X_test[rndval][m][n][0] = tmpr X_test[rndval][m][n][2] = tmpg plt.imshow(X_test[rndval]) plt.show()