-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubbish_detector_model.py
57 lines (43 loc) · 1.87 KB
/
rubbish_detector_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
import tensorflow as tf
import os
import config
from keras.applications.resnet import ResNet50
from keras.applications.mobilenet_v2 import MobileNetV2
from keras.applications.inception_v3 import InceptionV3
from keras.applications.nasnet import NASNetMobile
from keras.models import Sequential, load_model
from keras.optimizers import Adam
from keras.layers import Dense, Flatten, BatchNormalization, Dropout
def create_nn(num_classes):
print("CREATING MODEL: "+config.model_name)
model = Sequential(name=config.model_name)
if config.model_name == 'resnet50':
model.add(ResNet50(pooling='avg', weights='imagenet')) # input_shape = (224,224,3)
opt = Adam(lr=0.0001)
if config.model_name == 'inceptionv3':
model.add(InceptionV3(pooling='avg', weights='imagenet')) # input_shape = (299,299,3)
opt = Adam(lr=0.0001)
if config.model_name == 'mobilenetv2':
model.add(MobileNetV2(pooling='avg', weights='imagenet')) # input_shape = (224,224,3)
opt = Adam(lr=0.0001)
if config.model_name == 'nasnetmobile':
model.add(NASNetMobile(pooling='avg', weights='imagenet')) # input_shape = (224,224,3)
opt = Adam(lr=0.0001)
model.add(Dropout(0.2))
model.add(Dense(500, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(250, activation='relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Dense(num_classes, activation='softmax'))
# model.layers[0].trainable = False
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.summary()
return model
def restore_model(model_file):
print("LOADING MODEL from "+ model_file)
model = load_model(model_file)
opt = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
model.summary()
return model