์บ๊ธ์์ ๋ฐ์ดํฐ์ ์ ๋ค์ด ๋ฐ์๋ค
๋ค๋ฅธ ๋ถ๋ค์ด CNN์ผ๋ก ํฌํธํ ํ์งํ๋ ์ฝ๋๋ฅผ ์ฌ๋ ค์ฃผ์ ์ ๋ณด๋ฉด์ ๋ฐ๋ผํ๋ค
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค import !
import numpy as np
import pandas as pd
import tensorflow as tf
import cv2
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
์ฌ์ง๋ค ํ์ธํด๋ณด๊ธฐ
plt.imshow(cv2.imread("pothole_dataset/potholes/125.jpg"))
training set์ด๋ validation set์ผ๋ก ๋๋๊ธฐ !
# data preprocessing
train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True, validation_split=0.2)
training_set = train_datagen.flow_from_directory('pothole_dataset', target_size = (64, 64), batch_size = 32,class_mode = 'binary',subset='training')
validation_set = train_datagen.flow_from_directory('pothole_dataset', target_size=(64, 64), batch_size=32, class_mode='binary', subset='validation')
CNN ๋น๋ฉ !
# bulding CNN
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu', input_shape=[64, 64, 3])) # convolution
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2)) # pooling
## second conv
cnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2, strides=2))
cnn.add(tf.keras.layers.Flatten()) # flattening
cnn.add(tf.keras.layers.Dense(units=128, activation='relu')) # full connection
cnn.add(tf.keras.layers.Dense(units=1, activation='sigmoid')) # output layer
CNN ํ์ต์ํค๊ธฐ !
# traing CNN
cnn.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy']) # compiling
cnn.fit(x = training_set, validation_data = validation_set, epochs = 25) # training set -> training / validation set -> evaluating
test ๊ฒฐ๊ณผ ํ์ธํ๊ธฐ !
test_img = 'pothole_dataset/potholes/184.jpg' # another_images/normal/1.jpg ๋ pothole์ผ๋ก ์ธ์๋จ (ํฐ๋ ์
๊ตฌ ๋๋ฌธ์ธ ๊ฒ ๊ฐ์)
test_image = image.load_img(test_img, target_size = (64, 64))
plt.imshow(cv2.imread(test_img)) # test img ์ถ๋ ฅ
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
prediction = 'pothole'
else:
prediction = 'normal'
print(prediction)
์ผ๋จ ๋ฐ๋ผํด๋ณด๋ฉด์ ์ฝ๋๋ฅผ ์ดํดํด๋ดค๋ค
๋งจ๋ ๋งคํธ๋ฉ์ผ๋ก๋ง ์ธ๊ณต์ง๋ฅ ๋จธ์ ๋ฌ๋ ํด๋ดค์ง ํ์ด์ฌ์ผ๋ก๋ ์ฒ์์ธ๋ฐ ์ฌ์ฉํ๋ ํจ์ ๋ช๋ช๊ฐ๋ง ๋ค๋ฅด๊ณ ํฐ ํ์ ๋๊ฐ์ ๊ฒ ๊ฐ๋ค!!
's t u d y . . ๐ง > AI ์ค ML ์ค DL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[YOLOv5] YOLOv5 ์ฌ์ฉ๋ฒ (1) | 2022.10.07 |
---|---|
[YOLOv5] Custom Dataset์ผ๋ก Pothole detection (0) | 2022.10.07 |
[YOLOv3] Object Detection๊ณผ Bounding Box (0) | 2022.10.02 |
[DARKNET] ๋คํฌ๋ท์ผ๋ก YOLOV3 ๋๋ฆฌ๋ ๋ฒ (0) | 2022.10.02 |
[YOLOv3] Object detection (1) | 2022.09.30 |