Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Last active February 7, 2019 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukpiz/052e313ce6256483e48577cbeffddcb7 to your computer and use it in GitHub Desktop.
Save yukpiz/052e313ce6256483e48577cbeffddcb7 to your computer and use it in GitHub Desktop.
# codeing:utf-8
import cv2
import os
import glob
import imghdr
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
PROC_PATHS = [
"data/dst/face/zombi1.mp4/",
"data/dst/face/zombi2.mp4/",
"data/dst/face/zombi3.mp4/",
]
IMG_DIR_NAMES = [
"0_sakura_minamoto",
"1_saki_nikaido",
"2_ai_mizuno",
"3_junko_konno",
"4_yugiri",
"5_lily_hoshikawa",
"6_tae_yamada",
"7_kotaro_tatsumi",
]
CLASS_NAMES = [
"Sakura Minamoto",
"Saki Nikaido",
"Ai Mizuno",
"Junko Konno",
"Yugiri",
"Lily Hoshikawa",
"Tae Yamada",
"Kotaro Tatsumi",
]
FIXED_IMAGE_SIZE = 28
class NotMatchCountError(Exception):
def __init__(self, message):
self.message = message
def main(proc_paths, img_dir_names):
image_bytes = []
image_labels = []
image_count = 0
for proc_path in proc_paths:
label_index = 0
for img_dir_name in img_dir_names:
target = os.path.join(proc_path, img_dir_name)
for image_path in glob.glob('{}/*.*'.format(target), recursive=False):
# 画像ファイル以外はスキップ
if imghdr.what(image_path) is None:
continue
image_count += 1
image_byte = cv2.imread(image_path, cv2.IMREAD_COLOR)
image_byte = cv2.resize(
image_byte, (FIXED_IMAGE_SIZE, FIXED_IMAGE_SIZE))
image_bytes.append(image_byte)
image_labels.append(label_index)
label_index += 1
print("Image Count: %d" % len(image_bytes))
print("Label Count: %d" % len(image_labels))
return train_model(image_bytes, image_labels)
def train_model(image_bytes, image_labels):
if len(image_bytes) != len(image_labels):
raise NotMatchCountError("画像枚数とラベル数が一致しません")
# NumPy配列化
train_images = np.array(image_bytes)/255.0
train_labels = np.array(image_labels)
print(train_images.shape)
print(train_labels.shape)
# ???????
model = keras.Sequential([
keras.layers.Flatten(input_shape=(
FIXED_IMAGE_SIZE, FIXED_IMAGE_SIZE, 3)),
keras.layers.Dense(256, activation=tf.nn.relu),
keras.layers.Dense(20, activation=tf.nn.softmax)
])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=10)
return model
def test(model, *image_paths):
image_bytes = []
for image_path in image_paths:
image_byte = cv2.imread(image_path, cv2.IMREAD_COLOR)
image_byte = cv2.resize(
image_byte, (FIXED_IMAGE_SIZE, FIXED_IMAGE_SIZE))
image_bytes.append(image_byte)
test_images = np.array(image_bytes)/255.0
predictions = model.predict(test_images)
return predictions
if __name__ == "__main__":
print("===> START")
model = main(PROC_PATHS, IMG_DIR_NAMES)
predictions = test(model, "270face.jpg")
# plot_image(predictions[0])
print("===> FINISH")
@yukpiz
Copy link
Author

yukpiz commented Feb 5, 2019

===> START
Image Count: 4217
Label Count: 4217
(4217, 28, 28, 3)
(4217,)
Epoch 1/5
4217/4217 [==============================] - 1s 127us/step - loss: 0.9553 - acc: 0.7183
Epoch 2/5
4217/4217 [==============================] - 0s 101us/step - loss: 0.4070 - acc: 0.8926
Epoch 3/5
4217/4217 [==============================] - 0s 101us/step - loss: 0.3220 - acc: 0.9078
Epoch 4/5
4217/4217 [==============================] - 0s 106us/step - loss: 0.2611 - acc: 0.9272
Epoch 5/5
4217/4217 [==============================] - 0s 98us/step - loss: 0.2647 - acc: 0.9253
<tensorflow.python.keras.engine.sequential.Sequential object at 0x7f4850b91cc0>
===> FINISH

@yukpiz
Copy link
Author

yukpiz commented Feb 5, 2019

===> START
Image Count: 4217
Label Count: 4217
(4217, 28, 28, 3)
(4217,)
Epoch 1/50
4217/4217 [==============================] - 1s 130us/step - loss: 1.0705 - acc: 0.6948
Epoch 2/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.4336 - acc: 0.8805
Epoch 3/50
4217/4217 [==============================] - 0s 98us/step - loss: 0.3435 - acc: 0.9030
Epoch 4/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.2764 - acc: 0.9241
Epoch 5/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.2367 - acc: 0.9338
Epoch 6/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.2071 - acc: 0.9455
Epoch 7/50
4217/4217 [==============================] - 0s 96us/step - loss: 0.1882 - acc: 0.9497
Epoch 8/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.1747 - acc: 0.9538
Epoch 9/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.1822 - acc: 0.9516
Epoch 10/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.1572 - acc: 0.9557
Epoch 11/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.1316 - acc: 0.9666
Epoch 12/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.1224 - acc: 0.9701
Epoch 13/50
4217/4217 [==============================] - 0s 106us/step - loss: 0.1158 - acc: 0.9704
Epoch 14/50
4217/4217 [==============================] - 0s 107us/step - loss: 0.1638 - acc: 0.9535
Epoch 15/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.1052 - acc: 0.9715
Epoch 16/50
4217/4217 [==============================] - 0s 104us/step - loss: 0.0945 - acc: 0.9744
Epoch 17/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.0974 - acc: 0.9725
Epoch 18/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.0948 - acc: 0.9708
Epoch 19/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0927 - acc: 0.9742
Epoch 20/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.1072 - acc: 0.9677
Epoch 21/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0894 - acc: 0.9746
Epoch 22/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0683 - acc: 0.9808
Epoch 23/50
4217/4217 [==============================] - 0s 104us/step - loss: 0.0678 - acc: 0.9806
Epoch 24/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0766 - acc: 0.9779
Epoch 25/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0527 - acc: 0.9851
Epoch 26/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0533 - acc: 0.9848
Epoch 27/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0551 - acc: 0.9848
Epoch 28/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0620 - acc: 0.9810
Epoch 29/50
4217/4217 [==============================] - 0s 105us/step - loss: 0.0808 - acc: 0.9723
Epoch 30/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0602 - acc: 0.9825
Epoch 31/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0447 - acc: 0.9877
Epoch 32/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.0463 - acc: 0.9889
Epoch 33/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0510 - acc: 0.9865
Epoch 34/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0472 - acc: 0.9848
Epoch 35/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0405 - acc: 0.9896
Epoch 36/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0361 - acc: 0.9898
Epoch 37/50
4217/4217 [==============================] - 0s 105us/step - loss: 0.0273 - acc: 0.9915
Epoch 38/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0309 - acc: 0.9915
Epoch 39/50
4217/4217 [==============================] - 0s 105us/step - loss: 0.0392 - acc: 0.9889
Epoch 40/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0350 - acc: 0.9891
Epoch 41/50
4217/4217 [==============================] - 0s 102us/step - loss: 0.0377 - acc: 0.9903
Epoch 42/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.0305 - acc: 0.9922
Epoch 43/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0507 - acc: 0.9825
Epoch 44/50
4217/4217 [==============================] - 0s 99us/step - loss: 0.0727 - acc: 0.9796
Epoch 45/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0577 - acc: 0.9815
Epoch 46/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.0287 - acc: 0.9912
Epoch 47/50
4217/4217 [==============================] - 0s 103us/step - loss: 0.0178 - acc: 0.9957
Epoch 48/50
4217/4217 [==============================] - 0s 101us/step - loss: 0.0261 - acc: 0.9929
Epoch 49/50
4217/4217 [==============================] - 0s 100us/step - loss: 0.0235 - acc: 0.9929
Epoch 50/50
4217/4217 [==============================] - 0s 109us/step - loss: 0.0426 - acc: 0.9889
<tensorflow.python.keras.engine.sequential.Sequential object at 0x7fbddf98dcc0>
===> FINISH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment