-
Notifications
You must be signed in to change notification settings - Fork 3
/
make_model.py
105 lines (75 loc) · 2.83 KB
/
make_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Flatten, BatchNormalization
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
path_dir1 = './images/'
path_dir2 = './mask_images/'
file_list1 = os.listdir(path_dir1) # path에 존재하는 파일 목록 가져오기
file_list2 = os.listdir(path_dir2)
file_list1_num = len(file_list1)
file_list2_num = len(file_list2)
file_num = file_list1_num + file_list2_num
#%% 이미지 전처리
num = 0;
all_img = np.float32(np.zeros((file_num, 224, 224, 3)))
all_label = np.float64(np.zeros((file_num, 1)))
for img_name in file_list1:
img_path = path_dir1+img_name
img = load_img(img_path, target_size=(224, 224))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
all_img[num, :, :, :] = x
all_label[num] = 0 # nomask
num = num + 1
for img_name in file_list2:
img_path = path_dir2+img_name
img = load_img(img_path, target_size=(224, 224))
x = img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
all_img[num, :, :, :] = x
all_label[num] = 1 # mask
num = num + 1
# 데이터셋 섞기(적절하게 훈련되게 하기 위함)
n_elem = all_label.shape[0]
indices = np.random.choice(n_elem, size=n_elem, replace=False)
all_label = all_label[indices]
all_img = all_img[indices]
# 훈련셋 테스트셋 분할
num_train = int(np.round(all_label.shape[0]*0.8))
num_test = int(np.round(all_label.shape[0]*0.2))
train_img = all_img[0:num_train, :, :, :]
test_img = all_img[num_train:, :, :, :]
train_label = all_label[0:num_train]
test_label = all_label[num_train:]
#%%
# create the base pre-trained model
IMG_SHAPE = (224, 224, 3)
base_model = ResNet50(input_shape=IMG_SHAPE, weights='imagenet', include_top=False)
base_model.trainable = False
base_model.summary()
print("Number of layers in the base model: ", len(base_model.layers))
flatten_layer = Flatten()
dense_layer1 = Dense(128, activation='relu')
bn_layer1 = BatchNormalization()
dense_layer2 = Dense(1, activation=tf.nn.sigmoid)
model = Sequential([
base_model,
flatten_layer,
dense_layer1,
bn_layer1,
dense_layer2,
])
base_learning_rate = 0.001
model.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()
model.fit(train_img, train_label, epochs=10, batch_size=16, validation_data = (test_img, test_label))
# save model
model.save("model.h5")
print("Saved model to disk")