-
Notifications
You must be signed in to change notification settings - Fork 2
/
inception_v3.py
47 lines (41 loc) · 1.46 KB
/
inception_v3.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
# -*- coding: utf-8 -*-
""" Pretrained inception v3
# Reference
- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)
"""
from __future__ import print_function
from __future__ import absolute_import
from keras.models import Model
from keras.backend import tf as ktf
from keras.applications import InceptionV3
from keras import layers
def inception_pretrained():
"""
Build pretrained inception_v3 model
Resize images before applying
Includes trainable layers.
"""
inputs = layers.Input(shape=(None, None, 3))
# Resize images
inputs = layers.Lambda(lambda image: ktf.image.resize_images(image, (139, 139)))(inputs)
# Build model
base_model = InceptionV3(
include_top=False,
weights='imagenet',
input_shape=(139, 139, 3),
input_tensor=inputs)
for layer in base_model.layers:
layer.trainable = False
base_model.trainable = False
# Trainable layers
model = layers.Flatten()(base_model.output)
model = layers.Dense(384, activation='relu')(model)
model = layers.Dropout(0.5)(model)
model = layers.Dense(128, activation='relu')(model)
model = layers.Dropout(0.5)(model)
model = layers.Dense(10, activation='softmax')(model)
model = Model(inputs=base_model.input, outputs=model)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
return model