forked from GAIYA2050/keras_classfication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
other_model.py
96 lines (85 loc) · 3.82 KB
/
other_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import os
from glob import glob
import numpy as np
from keras import backend
from keras.models import Model
from keras.optimizers import adam, Nadam, SGD
from keras.callbacks import TensorBoard, Callback
from moxing.framework import file
from data_gen import data_flow
from keras.layers import Dense, Input, Dropout, Activation,GlobalAveragePooling2D,LeakyReLU,BatchNormalization
from keras.layers import concatenate,Concatenate,multiply, LocallyConnected2D, Lambda,Conv2D,GlobalMaxPooling2D,Flatten
from keras.layers.core import Reshape
from keras.layers import multiply
import keras as ks
from keras.models import load_model
from keras.applications.xception import Xception
from keras.applications.inception_v3 import InceptionV3
from models.resnet50 import ResNet50
from keras.layers import Flatten, Dense, AveragePooling2D
from keras.models import Sequential
from keras.utils import multi_gpu_model
from Groupnormalization import GroupNormalization
from keras_efficientnets import EfficientNetB5
from keras_efficientnets import EfficientNetB4
import efficientnet.keras as efn
# ResNet50
def model_fn(FLAGS, objective, optimizer, metrics):
"""
pre-trained resnet50 model
"""
base_model = ResNet50(weights="imagenet",
include_top=False,
pooling=None,
input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
classes=FLAGS.num_classes)
base_model = multi_gpu_model(base_model,4)
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = Flatten()(x)
predictions = Dense(FLAGS.num_classes, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
return model
# SE-ResNet50
def model_fn(FLAGS, objective, optimizer, metrics):
inputs_dim = Input(shape=(FLAGS.input_size, FLAGS.input_size, 3))
x = ResNet50(weights="imagenet",
include_top=False,
pooling=max,
input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
classes=FLAGS.num_classes)(inputs_dim)
squeeze = GlobalAveragePooling2D()(x)
excitation = Dense(units=2048 // 16)(squeeze)
excitation = Activation('relu')(excitation)
excitation = Dense(units=2048)(excitation)
excitation = Activation('sigmoid')(excitation)
excitation = Reshape((1, 1, 2048))(excitation)
scale = multiply([x, excitation])
x = GlobalAveragePooling2D()(scale)
# x = Dropout(0.3)(x)
fc2 = Dense(FLAGS.num_classes)(x)
fc2 = Activation('sigmoid')(fc2) #此处注意,为sigmoid函数
model = Model(inputs=inputs_dim, outputs=fc2)
# model.load_weights('/home/work/user-job-dir/src/SE-Xception.h5',by_name=True)
# model = load_model('/home/work/user-job-dir/src/SE-Xception.h5')
model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
return model
# Xception
def model_fn(FLAGS, objective, optimizer, metrics):
inputs_dim = Input(shape=(FLAGS.input_size, FLAGS.input_size, 3))
Xception_notop = Xception(include_top=False,
weights=None,
input_tensor=None,
input_shape=(FLAGS.input_size, FLAGS.input_size, 3),
pooling=max)
Xception_notop.load_weights('/home/work/user-job-dir/src/xception_weights_tf_dim_ordering_tf_kernels_notop.h5')
output = Xception_notop.output
output = GlobalAveragePooling2D()(output)
output = Dense(FLAGS.num_classes, activation='softmax')(output)
Xception_model = Model(inputs=Xception_notop.input, outputs=output)
# Xception_model = multi_gpu_model(Xception_model, 4)
Xception_model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
return Xception_model