-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmodel_mobile.py
64 lines (48 loc) · 1.93 KB
/
model_mobile.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
"""
https://github.com/FrederikSchorr/sign-language
Load pretrained MobileNet or Inception CNN and perform some checks.
Models are loaded without top layers, suitable for feature extraction.
"""
import os
import glob
import time
import sys
import warnings
import numpy as np
import pandas as pd
import keras
def features_2D_load_model(diFeature:dict) -> keras.Model:
sModelName = diFeature["sName"]
print("Load 2D extraction model %s ..." % sModelName)
# load pretrained keras models
if sModelName == "mobilenet":
# load base model with top
keBaseModel = keras.applications.mobilenet.MobileNet(
weights="imagenet",
input_shape = (224, 224, 3),
include_top = True)
# We'll extract features at the final pool layer
keModel = keras.models.Model(
inputs=keBaseModel.input,
outputs=keBaseModel.get_layer('global_average_pooling2d_1').output,
name=sModelName + " without top layer")
elif sModelName == "inception":
# load base model with top
keBaseModel = keras.applications.inception_v3.InceptionV3(
weights='imagenet',
include_top=True)
# We'll extract features at the final pool layer
keModel = keras.models.Model(
inputs=keBaseModel.input,
outputs=keBaseModel.get_layer('avg_pool').output,
name=sModelName + " without top layer")
else: raise ValueError("Unknown 2D feature extraction model")
# check input & output dimensions
tuInputShape = keModel.input_shape[1:]
tuOutputShape = keModel.output_shape[1:]
print("Expected input shape %s, output shape %s" % (str(tuInputShape), str(tuOutputShape)))
if tuInputShape != diFeature["tuInputShape"]:
raise ValueError("Unexpected input shape")
if tuOutputShape != diFeature["tuOutputShape"]:
raise ValueError("Unexpected output shape")
return keModel