-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasic-mobile-net.py
38 lines (30 loc) · 1.11 KB
/
basic-mobile-net.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
# organize imports
import numpy as np
from keras.preprocessing import image
from keras.models import Model
from keras.applications import imagenet_utils, mobilenet
import tensorflowjs as tfjs
# process an image to be mobilenet friendly
def process_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
pImg = mobilenet.preprocess_input(img_array)
return pImg
# main function
if __name__ == '__main__':
# path to test image
test_img_path = "G:\\git-repos\\mobile-net-projects\\dataset\\test\\test_image_1.jpg"
# process the test image
pImg = process_image(test_img_path)
# define the mobilenet model
mobilenet = mobilenet.MobileNet()
# make predictions on test image using mobilenet
prediction = mobilenet.predict(pImg)
# obtain the top-5 predictions
results = imagenet_utils.decode_predictions(prediction)
print(results)
# convert the mobilenet model into tf.js model
save_path = "output\\mobilenet"
tfjs.converters.save_keras_model(mobilenet, save_path)
print("[INFO] saved tf.js mobilenet model to disk..")