Created
September 18, 2019 16:51
-
-
Save anoken/e37f4bcfa4e93a2454c550cad3dec7db to your computer and use it in GitHub Desktop.
190919_har_test_train.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers import Activation, Dense, Dropout,Conv2D,MaxPooling2D,Flatten,ZeroPadding2D | |
from keras.utils.np_utils import to_categorical | |
from keras.optimizers import Adagrad | |
from keras.optimizers import Adam | |
import numpy as np | |
from PIL import Image | |
import os | |
from sklearn.model_selection import train_test_split | |
import tensorflow as tf | |
# 学習用のデータを作る. | |
image_list = [] | |
label_list = [] | |
for dir in os.listdir("train"): | |
if dir == ".DS_Store": | |
continue | |
dir1 = "./train/" + dir | |
label = 0 | |
if dir == "001": | |
label = 0 | |
elif dir == "002": | |
label = 1 | |
elif dir == "003": | |
label = 2 | |
elif dir == "004": | |
label = 3 | |
elif dir == "005": | |
label = 4 | |
for file in os.listdir(dir1): | |
if file != ".DS_Store": | |
label_list.append(label) | |
filepath = dir1 + "/" + file | |
image = Image.open(filepath) | |
# image = Image.open(filepath).resize((14, 14)).convert("RGB") | |
data = np.asarray(image) | |
image_list.append(data) | |
image_list = np.array(image_list) | |
image_list = image_list.astype('float32') | |
image_list = image_list / 255.0 | |
Y = to_categorical(label_list) | |
print(Y) | |
print(image_list) | |
X_train, X_test, y_train, y_test = train_test_split(image_list, Y, test_size=0.20) | |
# モデルを生成してニューラルネットを構築 | |
model = Sequential() | |
input_shape=(14, 14, 3) | |
model.add(ZeroPadding2D(padding=((1, 1), (1, 1)), input_shape=input_shape)) | |
model.add(Conv2D(32, (3, 3),input_shape=input_shape)) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(ZeroPadding2D(padding=((1, 1), (1, 1)), input_shape=input_shape)) | |
model.add(Conv2D(32, (3, 3))) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(32, activation='relu')) | |
#model.add(Dense(512)) | |
model.add(Dropout(0.5)) | |
model.add(Dense(5,activation='softmax')) | |
opt = Adam(lr=0.001) | |
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) | |
history=model.fit(X_train, y_train, nb_epoch=100) | |
#print(image_list) | |
from matplotlib import pyplot as plt | |
def plot_graph(history): | |
epochs = range(len(history.history['acc'])) | |
plt.plot(epochs,history.history['acc'], marker='.', label='acc') | |
# plt.plot(epochs,history.history['val_acc'], marker='.', label='val_acc') | |
plt.autoscale() | |
plt.title('model accuracy&loss') | |
plt.grid() | |
plt.xlabel('epoch') | |
plt.ylabel('accuracy') | |
plt.legend(loc='best') | |
# plt.savefig('./acc_graph.png') | |
# plt.show() | |
# plt.clf() | |
plt.plot(epochs,history.history['loss'], marker='.', label='loss') | |
# plt.plot(epochs,history.history['val_loss'], marker='.', label='val_loss') | |
plt.autoscale() | |
# plt.title('model loss') | |
plt.grid() | |
plt.xlabel('epoch') | |
plt.ylabel('loss') | |
plt.legend(loc='best') | |
plt.savefig('./loss_graph.png') | |
# plt.show() | |
plot_graph(history) | |
model.save('my_mbnet.h5') | |
converter = tf.lite.TFLiteConverter.from_keras_model_file('my_mbnet.h5') | |
tflite_model = converter.convert() | |
open('my_mbnet.tflite', "wb").write(tflite_model) | |
import subprocess | |
subprocess.run(['./ncc/ncc','my_mbnet.tflite','my_mbnet.kmodel','-i','tflite','-o', | |
'k210model','--dataset','images']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment