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
评论列表
文章目录