Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Training.py 4.7 KB

You have to be logged in to leave a comment. Sign In
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  1. # USAGE
  2. # python train_mask_detector.py --dataset dataset
  3. # import the necessary packages
  4. from tensorflow.keras.preprocessing.image import ImageDataGenerator
  5. from tensorflow.keras.applications import MobileNetV2
  6. from tensorflow.keras.layers import AveragePooling2D
  7. from tensorflow.keras.layers import Dropout
  8. from tensorflow.keras.layers import Flatten
  9. from tensorflow.keras.layers import Dense
  10. from tensorflow.keras.layers import Input
  11. from tensorflow.keras.models import Model
  12. from tensorflow.keras.optimizers import Adam
  13. from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
  14. from tensorflow.keras.preprocessing.image import img_to_array
  15. from tensorflow.keras.preprocessing.image import load_img
  16. from tensorflow.keras.utils import to_categorical
  17. from sklearn.preprocessing import LabelBinarizer
  18. from sklearn.model_selection import train_test_split
  19. from sklearn.metrics import classification_report
  20. from imutils import paths
  21. import matplotlib.pyplot as plt
  22. import numpy as np
  23. import os
  24. # %%
  25. # 資料集路徑
  26. DATASET = 'dataset'
  27. # Learning Rate、ephchs、batch size
  28. INIT_LR = 1e-4
  29. EPOCHS = 20
  30. BS = 32
  31. def main():
  32. # 圖片存成list,並透過轉成img_to_array轉成ndarray
  33. print("[INFO] loading images...")
  34. imagePaths = list(paths.list_images(DATASET))
  35. data = []
  36. labels = []
  37. for imagePath in imagePaths:
  38. # 透過資料夾取得Label(with_mask、without_mask)
  39. label = imagePath.split(os.path.sep)[-2]
  40. # 以224 * 224 讀入圖片
  41. image = load_img(imagePath, target_size=(224, 224))
  42. image = img_to_array(image)
  43. image = preprocess_input(image)
  44. data.append(image)
  45. labels.append(label)
  46. # 資料轉成Numpy array
  47. data = np.array(data, dtype="float32")
  48. labels = np.array(labels)
  49. # 將標籤(with_mask、without_mak),one hot encoder轉換模型可用型態
  50. lb = LabelBinarizer()
  51. labels = lb.fit_transform(labels)
  52. labels = to_categorical(labels)
  53. """
  54. 全部以[[0, 1]
  55. [1, 0]]表示
  56. """
  57. # %%
  58. # 切分dataset 75% for training、25% for testing 建立模型
  59. (trainX, testX, trainY, testY) = train_test_split(data, labels,test_size=0.2, stratify=labels, random_state=0)
  60. # 建立數據增強器augmentation
  61. # 參數說明參考https://keras.io/zh/preprocessing/image/
  62. aug = ImageDataGenerator(
  63. rotation_range=20,
  64. zoom_range=0.15,
  65. width_shift_range=0.2,
  66. height_shift_range=0.2,
  67. shear_range=0.15,
  68. horizontal_flip=True,
  69. fill_mode="nearest")
  70. # transfer learning MobileNetV2
  71. baseModel = MobileNetV2(weights="imagenet", include_top=False,
  72. input_tensor=Input(shape=(224, 224, 3)))
  73. headModel = baseModel.output
  74. headModel = AveragePooling2D(pool_size=(7, 7))(headModel)
  75. headModel = Flatten(name="flatten")(headModel)
  76. headModel = Dense(128, activation="relu")(headModel)
  77. headModel = Dropout(0.5)(headModel)
  78. headModel = Dense(2, activation="softmax")(headModel)
  79. # 更換Fully Connected Layer
  80. model = Model(inputs=baseModel.input, outputs=headModel)
  81. for layer in baseModel.layers:
  82. layer.trainable = False
  83. print("[INFO] Compiling model ......")
  84. opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS)
  85. model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
  86. print("[INFO] Training ......")
  87. H = model.fit(
  88. aug.flow(trainX, trainY, batch_size=BS),
  89. steps_per_epoch=len(trainX) // BS,
  90. validation_data=(testX, testY),
  91. validation_steps=len(testX) // BS,
  92. epochs=EPOCHS)
  93. # 預測testing data
  94. print("[INFO] evaluating network...")
  95. predIdxs = model.predict(testX, batch_size=BS)
  96. predIdxs = np.argmax(predIdxs, axis=1)
  97. # 混淆矩陣
  98. print(classification_report(testY.argmax(axis=1), predIdxs,
  99. target_names=lb.classes_))
  100. # 儲存模型
  101. print("[INFO] Saving model ......")
  102. model.save('mask_detector_2.model', save_format="h5")
  103. # plot the Training loss and Accuracy
  104. N = EPOCHS
  105. plt.style.use("ggplot")
  106. plt.figure()
  107. plt.plot(np.arange(0, N), H.history["loss"], label="train_loss")
  108. plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss")
  109. plt.plot(np.arange(0, N), H.history["accuracy"], label="train_acc")
  110. plt.plot(np.arange(0, N), H.history["val_accuracy"], label="val_acc")
  111. plt.title("Training Loss and Accuracy")
  112. plt.xlabel("Epoch #")
  113. plt.ylabel("Loss/Accuracy")
  114. plt.legend(loc="lower left")
  115. plt.savefig('history.png', dpi=150)
  116. if __name__ == '__main__':
  117. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...