def make_init_model():
input_data = Input(shape=(32, 32, 3))
init_model_index = random.randint(1, 4)
init_model_index = 2
if init_model_index == 1: # one conv layer with kernel num = 64
stem_conv_1 = Conv2D(64, (1, 1), padding='same')(input_data)
elif init_model_index == 2: # two conv layers with kernel num = 64
stem_conv_1 = Conv2D(64, (1, 1), padding='same')(input_data)
stem_conv_2 = Conv2D(64, (1, 1), padding='same')(stem_conv_1)
elif init_model_index == 3: # one conv layer with a wider kernel num = 128
stem_conv_1 = Conv2D(128, (1, 1), padding='same')(input_data)
elif init_model_index == 4: # two conv layers with a wider kernel_num = 128
stem_conv_1 = Conv2D(128, (1, 1), padding='same')(input_data)
stem_conv_2 = Conv2D(128, (1, 1), padding='same')(stem_conv_1)
stem_global_pooling_1 = GlobalMaxPooling2D()(stem_conv_1)
stem_softmax_1 = Activation('softmax')(stem_global_pooling_1)
model = Model(inputs=input_data, outputs=stem_softmax_1)
return model
python类GlobalMaxPooling2D()的实例源码
def Build(model_list):
print model_list
for idx, layer in enumerate(model_list):
type = layer[0]
if type == 'InputLayer':
input = Input(shape=layer[1])
x = input
elif type == 'Conv2D':
x = Conv2D(filters=layer[2], kernel_size=layer[1], padding='same')(x)
elif type == 'InceptionBlock':
x = inception_block(x, idx)
elif type == 'ResidualBlock':
x = residual_block(x, layer[1], idx)
elif type == "GlobalMaxPooling2D":
x = GlobalMaxPooling2D()(x)
elif type == "Activation":
x = Activation('softmax')(x)
model = Model(inputs=input, outputs=x)
return model
def get_model_list(self, model):
model_list = []
model_dict = json.loads(model.to_json())
model_layer = model_dict['config']['layers']
for layer in model_layer:
layer_name = layer['config']['name']
layer_output_shape = model.get_layer(layer_name).output_shape
if layer['class_name'] == 'Conv2D' and layer['config']['name'].lower().startswith('conv'):
model_list.append([layer['class_name'], layer['config']['name'],
{'kernel_size': layer['config']['kernel_size'],
'filters': layer['config']['filters']}])
elif layer['class_name'] == 'GlobalMaxPooling2D':
model_list.append([layer['class_name'],
layer['config']['name'],
{}])
elif layer['class_name'] == 'Activation':
model_list.append([layer['class_name'],
layer['config']['name'],
{'activation_type': 'softmax'}])
return model_list
def test_keras_import(self):
# Global Pooling 1D
model = Sequential()
model.add(GlobalMaxPooling1D(input_shape=(1, 16)))
model.build()
self.keras_param_test(model, 0, 5)
# Global Pooling 2D
model = Sequential()
model.add(GlobalMaxPooling2D(input_shape=(1, 16, 16)))
model.build()
self.keras_param_test(model, 0, 8)
# Pooling 1D
model = Sequential()
model.add(MaxPooling1D(pool_size=2, strides=2, padding='same', input_shape=(1, 16)))
model.build()
self.keras_param_test(model, 0, 5)
# Pooling 2D
model = Sequential()
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same', input_shape=(1, 16, 16)))
model.build()
self.keras_param_test(model, 0, 8)
# Pooling 3D
model = Sequential()
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='same',
input_shape=(1, 16, 16, 16)))
model.build()
self.keras_param_test(model, 0, 11)
# ********** Locally-connected Layers **********
def get_model_list(model):
model_list = []
model_dict = json.loads(model.to_json())
with open('model_frame.json', 'w') as outfile:
json.dump(model_dict, outfile)
model_layer = model_dict['config']['layers']
for layer in model_layer:
layer_name = layer['config']['name']
layer_output_shape = model.get_layer(layer_name).output_shape
if layer['class_name'] == 'InputLayer':
model_list.append([layer['class_name'], layer['config']['batch_input_shape'][1:]])
elif layer['class_name'] == 'Conv2D' and layer['config']['name'].startswith('conv'):
model_list.append([layer['class_name'], layer['config']['kernel_size'], layer['config']['filters']])
elif layer['class_name'] == 'Add' and layer['config']['name'].startswith('res'):
model_list.append(['ResidualBlock', layer_output_shape[3]])
elif layer['class_name'] == 'Concatenate':
model_list.append(['InceptionBlock', layer_output_shape[3]])
elif layer['class_name'] == 'GlobalMaxPooling2D':
model_list.append([layer['class_name']])
elif layer['class_name'] == 'Activation':
model_list.append([layer['class_name']])
return model_list
def make_init_model(self):
models = []
input_data = Input(shape=self.gl_config.input_shape)
import random
init_model_index = random.randint(1, 4)
init_model_index = 1
if init_model_index == 1: # one conv layer with kernel num = 64
stem_conv_1 = Conv2D(128, 3, padding='same', name='conv2d1' )(input_data)
stem_conv_1 = PReLU()(stem_conv_1)
elif init_model_index == 2: # two conv layers with kernel num = 64
stem_conv_0 = Conv2D(128, 3, padding='same', name='conv2d1')(input_data)
stem_conv_0 = PReLU()(stem_conv_0)
stem_conv_1 = Conv2D(128, 3, padding='same', name='conv2d2')(stem_conv_0)
stem_conv_1 = PReLU()(stem_conv_1)
elif init_model_index == 3: # one conv layer with a wider kernel num = 128
stem_conv_1 = Conv2D(256, 3, padding='same', name='conv2d1')(input_data)
stem_conv_1 = PReLU()(stem_conv_1)
elif init_model_index == 4: # two conv layers with a wider kernel_num = 128
stem_conv_0 = Conv2D(256, 3, padding='same', name='conv2d1')(input_data)
stem_conv_0 = PReLU()(stem_conv_0)
stem_conv_1 = Conv2D(256, 3, padding='same', name='conv2d2')(stem_conv_0)
stem_conv_1 = PReLU()(stem_conv_1)
import keras
stem_conv_1 = keras.layers.MaxPooling2D(name='maxpooling2d1')(stem_conv_1)
stem_conv_1 = Conv2D(self.gl_config.nb_class, 3, padding='same', name='conv2d3')(stem_conv_1)
stem_global_pooling_1 = GlobalMaxPooling2D(name='globalmaxpooling2d1')(stem_conv_1)
stem_softmax_1 = Activation('softmax', name='activation1')(stem_global_pooling_1)
model = Model(inputs=input_data, outputs=stem_softmax_1)
return model
def test_global_max_pooling(self):
model = Sequential()
model.add(GlobalMaxPooling2D(input_shape=(16,16,3)))
self._test_keras_model(model)
def run_network(model, total_hid, train_bags, test_bags, y_bags):
# model.add(Dense(1)) # binary classification
# model.add(Activation('softmax')) # #instance * 2
model.add(GlobalMaxPooling2D()) # max pooling multi instance
model.summary()
savemodelpng = 'net.png'
plot_model(model, to_file=savemodelpng, show_shapes=True)
print(len(train_bags), len(test_bags), len(y_bags), train_bags[0].shape, y_bags[0].shape, len(train_bags[0]))
#categorical_crossentropy, binary_crossentropy, mil_squared_error
#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#model.compile(loss=mil_squared_error, optimizer='rmsprop')
# print 'model training'
#nb_epos= 5
#model.fit(train_bags, y_bags, batch_size = 60, epochs=nb_epos, verbose = 0)
#categorical_crossentropy, binary_crossentropy
#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) custom_objective
model.compile(loss='binary_crossentropy', optimizer='rmsprop')
#model.compile(loss=custom_objective, optimizer='rmsprop')
print 'model training'
nb_epos= 5
for iterate in range(nb_epos):
print 'train epoch', iterate
for training, y in zip(train_bags, y_bags):
tmp_size = len(training)
#pdb.set_trace()
#ys = np.array(tmp_size *[y]) # make the labels in the bag all have the same labels, maybe not correct?
# ys = np.zeros((tmp_size,2))
# ys[:, y] = 1 # binary class ############################################################################### one hot encoding
# ys = y*np.ones((4,)) # I do not understand the correspondence of ys and tarining, need to confirm ####
# trainingreshap = np.reshape(training, (1, training.shape[0], training.shape[1], training.shape[2]))
# print(training.shape, y.shape)
model.fit(training, y*np.ones((1,1)), batch_size = tmp_size, epochs=1, verbose = 0)
model.reset_states()
#ys = np_utils.to_categorical(ys)
#model.train_on_batch(training, ys)
print 'predicting'
predictions = []
for testing in test_bags:
pred = model.predict_proba(testing, verbose = 0)
predictions.append(max(pred))
return predictions