1. 이미지 불러오기
1. 이미지 수집 및 분류
- 하나의 폴더 하위에, 여러 개의 폴더로 나누어서 이미지를 수집 및 분류한다.
- 폴더 구조 (식물 학습 정도에 따른 분류)
- resample
- 0
- 1
- 2
- 3
- resample
2. 라이브러리 불러오기
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
3. 이미지 불러오기
import pathlib
fullPath = os.path.abspath("resample")
data_dir = pathlib.Path(fullPath)
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count) # 23058
set_2 = list(data_dir.glob('2/*'))
PIL.Image.open(str(set_2[0]))
2. 모델 정의 및 학습
1. 데이터 세트
- 데이터 세트 구성
batch_size = 32 img_height = 180 img_width = 180 # 학습에 이미지 80% 활용 train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size) # 검증에 이미지 80% 활용 val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size) class_names = train_ds.class_names print(class_names) # ['0', '1', '2', '3']
- 학습 데이터 9개 확인
import matplotlib.pyplot as plt plt.figure(figsize=(10, 10)) for images, labels in train_ds.take(1): for i in range(9): ax = plt.subplot(3, 3, i + 1) plt.imshow(images[i].numpy().astype("uint8")) plt.title(class_names[labels[i]]) plt.axis("off")
- 데이터 성능 향상
AUTOTUNE = tf.data.experimental.AUTOTUNE train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE) val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
2. 데이터 증강
- 데이터 증강
data_augmentation = keras.Sequential( [ layers.experimental.preprocessing.RandomFlip("horizontal", input_shape=(img_height, img_width, 3)), layers.experimental.preprocessing.RandomRotation(0.1), layers.experimental.preprocessing.RandomZoom(0.1), ] )
- 하나의 이미지에 데이터 증강해보기
plt.figure(figsize=(10, 10)) for images, _ in train_ds.take(1): for i in range(9): augmented_images = data_augmentation(images) ax = plt.subplot(3, 3, i + 1) plt.imshow(augmented_images[0].numpy().astype("uint8")) plt.axis("off")
3. 모델 정의
model = Sequential([
data_augmentation,
layers.experimental.preprocessing.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(len(class_names))
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.summary()
4. 학습 진행
epochs = 15
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)
model.save('./model/tf_converea')
5. 학습 결과 확인
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
3. 모델에 따른 예측
1. 라이브러리 불러오기
import cv2
from PIL import Image
import numpy as np
import tensorflow as tf
from tensorflow import keras
2. 모델 불러오기
model = keras.models.load_model('model/tf_converea')
3. 카메라의 이미지로부터 예측
img_height = 180
img_width = 180
window_title = 'aiot'
video_capture = cv2.VideoCapture(1)
if video_capture.isOpened():
try:
window_handle = cv2.namedWindow(window_title, cv2.WINDOW_AUTOSIZE)
while True:
ret_val, frame = video_capture.read()
if cv2.getWindowProperty(window_title, cv2.WND_PROP_AUTOSIZE) >= 0:
img = Image.fromarray(frame, 'RGB')
im = img.resize((img_width, img_height))
img_array = np.array(im)
img_array = np.expand_dims(img_array, axis=0)
predictions = model2.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(np.argmax(score), 100 * np.max(score))
pred_img = text_pred(frame, str(np.argmax(score)) + " :: " + str(100 * np.max(score)))
cv2.imshow(window_title, pred_img)
else:
break
keyCode = cv2.waitKey(10) & 0xFF
if keyCode == 27 or keyCode == ord('q'):
break
finally:
video_capture.release()
cv2.destroyAllWindows()
else:
print("Error: Unable to open camera")
참고