## 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:'bike', 1:'car', 2:'human', 3:'white', 4:'beju', 5:'black', 6:'wh2bk', 7:'bk2wh', 8:'wh_bk', 9:'bk_wh' } ################################################### ## 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))) model4B = keras.Sequential(name="my_model4B") model4B.add(keras.layers.Flatten(input_shape=(4,4,128))) model5 = keras.Sequential(name="my_model5") # model5を確認 print(model5) model5.add(keras.layers.Dense(128, activation='relu', input_dim=2048)) model5.add(keras.layers.Dropout(rate=0.5)) model5B = keras.Sequential(name="my_model5B") model5B.add(keras.layers.Dropout(rate=0.5, input_dim=128)) model6 = keras.Sequential(name="my_model6") # model6を確認 print(model6) ###model6.add(keras.layers.Dense(43, activation='softmax', input_dim=128)) ### Modified for lisa model6.add(keras.layers.Dense(10, input_dim=128)) # 2023/03/08 import csv ################################################### ## for read ################################################### ########################### lay1 lay1 = model1.layers[0] tmpw = lay1.get_weights() with open('w1_int.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]): tmpw[0][r][c][i][k] = 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_int.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]): tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay1.set_weights(tmpw) print("w1 read done") ########################### lay2 lay2 = model2.layers[1-1] tmpw = lay2.get_weights() with open('w2_int.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("weight shape : ", lay2.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay2.get_weights()[0].shape[3]): tmpw[0][r][c][i][k] = 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_int.csv', mode='r') as f: reader = csv.reader(f) print("weight shape : ", lay2.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay2.get_weights()[1].shape[0]): tmpf = float(row[k]) / 8 tmpi = int(tmpf) tmpw[1][k] = tmpi ### row[k] ### [1]縺ォ繝舌う繧「繧ケ ###tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay2.set_weights(tmpw) print("w2 read done") ########################### lay3 lay3 = model3.layers[3-3] tmpw = lay3.get_weights() with open('w3_int.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("weight shape : ", lay3.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay3.get_weights()[0].shape[3]): tmpw[0][r][c][i][k] = 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_int.csv', mode='r') as f: reader = csv.reader(f) print("weight shape : ", lay3.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay3.get_weights()[1].shape[0]): tmpf = float(row[k]) / 8 tmpi = int(tmpf) tmpw[1][k] = tmpi ### row[k] ### [1]縺ォ繝舌う繧「繧ケ ###tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay3.set_weights(tmpw) print("w3 read done") ########################### lay4 lay4 = model4.layers[4-4] tmpw = lay4.get_weights() with open('w4_int.csv', mode='r') as f: reader = csv.reader(f) r = 0 c = 0 i = 0 k = 0 print("weight shape : ", lay4.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay4.get_weights()[0].shape[3]): tmpw[0][r][c][i][k] = 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_int.csv', mode='r') as f: reader = csv.reader(f) print("weight shape : ", lay4.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay4.get_weights()[1].shape[0]): tmpf = float(row[k]) / 8 tmpi = int(tmpf) tmpw[1][k] = tmpi ### row[k] ### [1]縺ォ繝舌う繧「繧ケ ###tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay4.set_weights(tmpw) print("w4 read done") ########################### lay5 lay5 = model5.layers[7-7] tmpw = lay5.get_weights() with open('w5_int.csv', mode='r') as f: reader = csv.reader(f) i = 0 k = 0 print("weight shape : ", lay5.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay5.get_weights()[0].shape[1]): tmpw[0][i][k] = row[k] ### [0]縺ォ驥阪∩菫よ焚 i += 1 with open('b5_int.csv', mode='r') as f: reader = csv.reader(f) print("weight shape : ", lay5.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay5.get_weights()[1].shape[0]): tmpf = float(row[k]) / 8 tmpi = int(tmpf) tmpw[1][k] = tmpi ### row[k] ### [1]縺ォ繝舌う繧「繧ケ ###tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay5.set_weights(tmpw) print("w5 read done") ########################### lay6 lay6 = model6.layers[9-9] tmpw = lay6.get_weights() with open('w6_int.csv', mode='r') as f: reader = csv.reader(f) i = 0 k = 0 print("weight shape : ", lay6.get_weights()[0].shape) for row in reader: # print(row) for k in range(0, lay6.get_weights()[0].shape[1]): tmpw[0][i][k] = row[k] ### [0]縺ォ驥阪∩菫よ焚 i += 1 with open('b6_int.csv', mode='r') as f: reader = csv.reader(f) print("weight shape : ", lay6.get_weights()[1].shape) for row in reader: # print(row) for k in range(0, lay6.get_weights()[1].shape[0]): tmpf = float(row[k]) / 8 tmpi = int(tmpf) tmpw[1][k] = tmpi ### row[k] ### [1]縺ォ繝舌う繧「繧ケ ###tmpw[1][k] = row[k] ### [1]縺ォ繝舌う繧「繧ケ lay6.set_weights(tmpw) print("w6 read done") test = pd.read_csv(data_dir + '/TestMovObj.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(ume_image, 'RGB') 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 for n in range(0, X_test.shape[0]): for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): tmpval = X_test[n][r][c][0] ### [0]にblue value tmpval = tmpval / 8 X_test[n][r][c][0] = int(tmpval) * 8 for n in range(0, X_test.shape[0]): for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): tmpval = X_test[n][r][c][1] ### [1]にgreen value tmpval = tmpval / 4 X_test[n][r][c][1] = int(tmpval) * 4 for n in range(0, X_test.shape[0]): for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): tmpval = X_test[n][r][c][2] ### [2]にred value tmpval = tmpval / 8 X_test[n][r][c][2] = int(tmpval) * 8 X_test = np.asarray(X_test, dtype=int) print("x_test shape : ", X_test.shape) TNN = 0 #print(X_test[TNN]) ##################################################### ### write input image to a csv ##################################################### with open("Bin1_int.csv", mode='w') as f: for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): ### X_test[TNN][r][c][0] = 0 tmpval = X_test[TNN][r][c][0] ### [0]にblue value f.write(str(tmpval)) f.write(', ') f.write('\n') with open("Gin1_int.csv", mode='w') as f: for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): ### X_test[TNN][r][c][1] = 255 tmpval = X_test[TNN][r][c][1] ### [1]にgreen value f.write(str(tmpval)) f.write(', ') f.write('\n') with open("Rin1_int.csv", mode='w') as f: for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): ### X_test[TNN][r][c][2] = 0 tmpval = X_test[TNN][r][c][2] ### [2]にred value f.write(str(tmpval)) f.write(', ') f.write('\n') ### Modified for lisa ##################################################### outstr = "RGB565.csv" with open(outstr, mode='w') as f: for i in range(20): ### 2023/09/12, use rndval and 20 pics ## outstr = "RGB565_30x30_" + "{0:02d}".format(outn) + ".csv" if i%10 == 0 : rndval = int(i/10) elif i%10 == 1 : rndval = 180 + int(i/10) elif i%10 == 2 : rndval = 470 + int(i/10) elif i%10 == 3 : rndval = 1470 + 0 + int(i/10) elif i%10 == 4 : rndval = 1470 + 3 + int(i/10) elif i%10 == 5 : rndval = 1470 + 6 + int(i/10) elif i%10 == 6 : rndval = 1470 + 9 + int(i/10) elif i%10 == 7 : rndval = 1470 + 12 + int(i/10) elif i%10 == 8 : rndval = 1470 + 15 + int(i/10) elif i%10 == 9 : rndval = 1470 + 18 + int(i/10) for r in range(0, X_test.shape[1]): for c in range(0, X_test.shape[2]): tmpr = X_test[TNN+rndval][r][c][2] ### [2]にred value tmpr = tmpr >> 3 ### lower 3bit is 0 tmpr = tmpr << 11 tmpg = X_test[TNN+rndval][r][c][1] ### [1]にgreen value tmpg = tmpg >> 2 ### lower 2bit is 0 tmpg = tmpg << 5 tmpb = X_test[TNN+rndval][r][c][0] ### [0]にblue value tmpb = tmpb >> 3 ### lower 3bit is 0 tmpval = tmpr + tmpg + tmpb f.write(str(tmpval)) f.write(', ') f.write('\n') ##################################################### ### start prediction ##################################################### lay1out = model1.predict(X_test) lay1out = lay1out / 256 lay1out = lay1out / 8 lay1out = np.asarray(lay1out, dtype=int) lay1out = np.where(lay1out > 31, 31, lay1out) ##print(lay1out[TNN]) ######### write lay1 output to a csv print("lay1out shape : ", lay1out.shape) outn = 0 for outn in range(lay1out.shape[3]): outstr = "lay1out_" + "{0:02d}".format(outn) + ".csv" with open(outstr, mode='w') as f: for r in range(0, lay1out.shape[1]): for c in range(0, lay1out.shape[2]): tmpval = lay1out[TNN][r][c][outn] ### channel outn f.write(str(tmpval)) f.write(',') f.write('\n') lay2out = model2.predict(lay1out) print("lay2out shape : ", lay2out.shape) lay2out = lay2out / 256 lay2out = np.asarray(lay2out, dtype=int) lay2out = np.where(lay2out > 31, 31, lay2out) ######### write lay2 output to a csv print("lay2out shape : ", lay2out.shape) outn = 0 for outn in range(lay2out.shape[3]): outstr = "lay2out_" + "{0:02d}".format(outn) + ".csv" with open(outstr, mode='w') as f: for r in range(0, lay2out.shape[1]): for c in range(0, lay2out.shape[2]): tmpval = lay2out[TNN][r][c][outn] ### channel outn f.write(str(tmpval)) f.write(',') f.write('\n') lay3out = model3.predict(lay2out) print("lay3out shape : ", lay3out.shape) lay3out = lay3out / 256 lay3out = np.asarray(lay3out, dtype=int) lay3out = np.where(lay3out > 31, 31, lay3out) ######### write lay3 output to a csv print("lay3out shape : ", lay3out.shape) outn = 0 for outn in range(lay3out.shape[3]): outstr = "lay3out_" + "{0:02d}".format(outn) + ".csv" with open(outstr, mode='w') as f: for r in range(0, lay3out.shape[1]): for c in range(0, lay3out.shape[2]): tmpval = lay3out[TNN][r][c][outn] ### channel outn f.write(str(tmpval)) f.write(',') f.write('\n') lay4out = model4.predict(lay3out) print("lay4out shape : ", lay4out.shape) lay4out = lay4out / 256 lay4out = np.asarray(lay4out, dtype=int) lay4out = np.where(lay4out > 31, 31, lay4out) ######### write lay4 output to a csv print("lay4out shape : ", lay4out.shape) outn = 0 for outn in range(lay4out.shape[3]): outstr = "lay4out_" + "{0:03d}".format(outn) + ".csv" with open(outstr, mode='w') as f: for r in range(0, lay4out.shape[1]): for c in range(0, lay4out.shape[2]): tmpval = lay4out[TNN][r][c][outn] ### channel outn f.write(str(tmpval)) f.write(',') f.write('\n') lay4Bout = model4B.predict(lay4out) print("lay4Bout shape : ", lay4Bout.shape) lay4Bout = np.asarray(lay4Bout, dtype=int) lay4Bout = np.where(lay4Bout > 31, 31, lay4Bout) ######### write lay4B output to a csv print("lay4Bout shape : ", lay4Bout.shape) outn = 0 outstr = "lay4Bout.csv" with open(outstr, mode='w') as f: for r in range(0, lay4Bout.shape[1]): tmpval = lay4Bout[TNN][r] f.write(str(tmpval)) f.write(',') lay5out = model5.predict(lay4Bout) print("lay5out shape : ", lay5out.shape) lay5out = lay5out / 256 lay5out = np.asarray(lay5out, dtype=int) lay5out = np.where(lay5out > 31, 31, lay5out) ######### write lay5 output to a csv print("lay5out shape : ", lay5out.shape) outn = 0 outstr = "lay5out.csv" with open(outstr, mode='w') as f: for r in range(0, lay5out.shape[1]): tmpval = lay5out[TNN][r] f.write(str(tmpval)) f.write(',') lay5Bout = model5B.predict(lay5out) print("lay5Bout shape : ", lay5Bout.shape) lay5Bout = np.asarray(lay5Bout, dtype=int) lay5Bout = np.where(lay5Bout > 31, 31, lay5Bout) ######### write lay5B output to a csv print("lay5Bout shape : ", lay5Bout.shape) outn = 0 outstr = "lay5Bout.csv" with open(outstr, mode='w') as f: for r in range(0, lay5Bout.shape[1]): tmpval = lay5Bout[TNN][r] f.write(str(tmpval)) f.write(',') lay6out = model6.predict(lay5Bout) ###lay6out = lay6out / 256 # 2023/03/08 print("lay6out shape : ", lay6out.shape) pred = np.argmax(lay6out, axis=-1) lay6out = np.asarray(lay6out, dtype=int) lay6out = lay6out >> 6 ### Modified for lisa ######### write lay6 output to a csv print("lay6out shape : ", lay6out.shape) for i in range(20): ### 2023/07/06, result of lay6 for 20 pictures if i%10 == 0 : rndval = int(i/10) elif i%10 == 1 : rndval = 180 + int(i/10) elif i%10 == 2 : rndval = 470 + int(i/10) elif i%10 == 3 : rndval = 1470 + 0 + int(i/10) elif i%10 == 4 : rndval = 1470 + 3 + int(i/10) elif i%10 == 5 : rndval = 1470 + 6 + int(i/10) elif i%10 == 6 : rndval = 1470 + 9 + int(i/10) elif i%10 == 7 : rndval = 1470 + 12 + int(i/10) elif i%10 == 8 : rndval = 1470 + 15 + int(i/10) elif i%10 == 9 : rndval = 1470 + 18 + int(i/10) outstr = "lay6out_sample_" + "{0:02d}".format(i) + ".csv" ### outstr = "lay6out.csv" with open(outstr, mode='w') as f: for r in range(0, lay6out.shape[1]): tmpval = lay6out[TNN+rndval][r] f.write(str(tmpval)) f.write(',') #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(1479): prediction = pred[i] actual = labels[i] if prediction != actual: print('prediction error ', i, ' : pred ', pred[i], ' : actual ', labels[i]) plt.subplot(3, 6, 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 = 180 + int(i/10) elif i%10 == 2 : rndval = 470 + int(i/10) elif i%10 == 3 : rndval = 1470 + 0 + int(i/10) elif i%10 == 4 : rndval = 1470 + 3 + int(i/10) elif i%10 == 5 : rndval = 1470 + 6 + int(i/10) elif i%10 == 6 : rndval = 1470 + 9 + int(i/10) elif i%10 == 7 : rndval = 1470 + 12 + int(i/10) elif i%10 == 8 : rndval = 1470 + 15 + int(i/10) elif i%10 == 9 : rndval = 1470 + 18 + 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()