Skip to content

Commit 365f3d7

Browse files
committed
Convert the Inception v4 model into coreML model
1 parent a3f27dc commit 365f3d7

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
inception_v4_dogscats.mlmodel
2+
inceptionv4_dogscats_weights.h5
13
us-shareprices-daily.csv
24
/.idea
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Author/ Learner: Nguyen Truong Thinh
2+
# Contact me: nguyentruongthinhvn2020@gmail.com || +84393280504
3+
#
4+
# Use case: Create a Keras Inception v4 model that can be used to convert into the Core ML Format
5+
# via CoreML Tool .
6+
# The model will be trained & converted on the Kaggle Dogs vs Cats dataset via Keras functional api.
7+
8+
import coremltools as ct
9+
10+
from ml.deep_learning.keras_functional_api.inception_v4_network import model
11+
12+
weights_path = 'inceptionv4_dogscats_weights.h5'
13+
model.load_weights(weights_path)
14+
15+
coreml_model = ct.convert(model, source="tensorflow", minimum_deployment_target=ct.target.iOS13)
16+
17+
coreml_model.author = 'Nguyen Truong Thinh'
18+
coreml_model.short_description = 'An Inception v4 model trained on the Kaggle Dogs vs Cats dataset via Keras ' \
19+
'functional api.'
20+
coreml_model.save("inception_v4_dogscats.mlmodel")
21+
22+
"""
23+
Running TensorFlow Graph Passes: 100%|██████████| 6/6 [00:00<00:00, 8.02 passes/s]
24+
Converting TF Frontend ==> MIL Ops: 100%|██████████| 1258/1258 [00:00<00:00, 1981.15 ops/s]
25+
Running MIL frontend_tensorflow2 pipeline: 100%|██████████| 7/7 [00:00<00:00, 155.55 passes/s]
26+
Running MIL default pipeline: 100%|██████████| 56/56 [00:04<00:00, 12.48 passes/s]
27+
Running MIL backend_neuralnetwork pipeline: 100%|██████████| 8/8 [00:00<00:00, 256.00 passes/s]
28+
Translating MIL ==> NeuralNetwork Ops: 100%|██████████| 1538/1538 [00:02<00:00, 647.43 ops/s]
29+
30+
Process finished with exit code 0
31+
"""

ml/deep_learning/keras_functional_api/helper_function.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ def build_inception_b_block(input_tensor):
175175
"""
176176
# (384 1x1 convolutions) - This is the first branch for the left
177177
branch_a = conv2d_batch_norm_relu(input_tensor, 384, 1, 1)
178-
# This is the second branch for the left
178+
# This is the second branch from the left
179179
branch_b = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(input_tensor)
180180
branch_b = conv2d_batch_norm_relu(branch_b, 128, 1, 1)
181-
# This is the third branch from the left
181+
# This is the third branch from the left
182182
branch_c = conv2d_batch_norm_relu(input_tensor, 192, 1, 1)
183183
branch_c = conv2d_batch_norm_relu(branch_c, 224, 1, 7)
184184
branch_c = conv2d_batch_norm_relu(branch_c, 256, 7, 1)

ml/deep_learning/keras_functional_api/inception_v4_network.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# Use the Adam optimizer and compile the model.
4040
adam_opt = Adam(lr=INITIAL_LEARNING_RATE)
4141
model = Model(INPUT_TENSOR, network_output, name='InceptionV4')
42-
model.compile(optimizer=adam_opt, loss='binary_crossentropy', metrics=["accuracy"])
42+
compiled_model = model.compile(optimizer=adam_opt, loss='binary_crossentropy', metrics=["accuracy"])
4343
# Display a summary of the layers of the model.
4444
model.summary()
4545
# ===========================================

ml/deep_learning/keras_functional_api/training_the_inception_v4_model.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
from sklearn.model_selection import train_test_split
1616

1717
from inception_v4_network import model
18+
from ml.deep_learning.keras_functional_api.helper_function import plot_training_stats
1819

1920
# ----------------- A computer without an NVIDIA GPU,
20-
# then uncomment the first two statements
21-
# weights_path = './inceptionv4.dogscats_weights.h5'
21+
# Download it at: https://drive.google.com/file/d/1NqMs2js-uOyOLL9MB0nzX0AoHx-hy8ty/view?usp=sharing
22+
# Place it on the following path: "ml/deep_learning/keras_functional_api/inceptionv4.dogscats_weights.h5"
23+
# then uncomment the first two statements:
24+
25+
# weights_path = 'inceptionv4.dogscats_weights.h5'
2226
# model.load_weights(weights_path, by_name=True)
2327

2428
# ----------------- A computer have an NVIDIA GPU
2529
# Adjust these to match the dimensions of our input image.
26-
from ml.deep_learning.keras_functional_api.helper_function import plot_training_stats
27-
2830
IMAGE_HEIGHT = 299
2931
IMAGE_WIDTH = 299
3032
# Reduce this if this model does not fit on your GPU.
@@ -114,3 +116,10 @@
114116

115117
end_time = datetime.datetime.now()
116118
print(f'training completed in: {end_time - start_time}')
119+
120+
"""
121+
=> Attention! (Note):
122+
The Inception-v4 model was trained on a Windows 10 computer workstation with 64GB of RAM
123+
and an NVIDIA RTX 2080Ti 11Gb graphics card. It took about three hours to train 30 epochs,
124+
with the best model having an accuracy of 87.9 percent.
125+
"""

0 commit comments

Comments
 (0)