如何水平合并分类模型 [英] how to merge classification models horizontally

查看:37
本文介绍了如何水平合并分类模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了许多模型,每个模型都回答一个简单的是/否问题.伪代码:

I have many models already trained, which each answer a simple yes/no question. Pseudocode:

model_dog = keras.load('is_dog')
model_cat = keras.load('is_cat')
model_rat = keras.load('is_rat')

image = load_photo_as_numpy_array('photo.jpg')

multi_class = [ m.predict(image) for m in (model_dog,model_cat,model_rat) ]

这工作正常,但它是一个>慢,因为推理是按顺序而不是并行进行的(我有数百个这样的模型,而不仅仅是 3 个),并且 b>使用起来比我有一个进行多分类的模型要复杂得多.

This works fine, but it is a> slow because inference is done sequentially instead of in parallel (I have several hundred such models, not just 3), and b> is much more complex to use than if I had ONE model which does multi-classification.

我想要的是:

model = keras.concat_horizontal([ model_dog, model_cat, model_rat ])
model.save('combined_model')

那么每当我想使用组合模型时,就这么简单:

Then whenever I want to use the combined model, it is as simple as:

model = keras.load('combined_model')
multi_class = m.predict(image)

通过这种方式,我可以向组合模型添加新的分类,例如通过训练一个识别鱼的简单模型.

This way, I can add a new classification to the combined model, by training one simple model, for example, that recognizes a fish.

推荐答案

正如我在评论中所建议的,您可以将多个模型合并到一个新模型中,并使用这个新模型进行预测.

As I suggested in comments, you can merge multiple models in one new model and predict using this new model.

首先,我编写了一个函数来合并模型并返回一个新的组合模型.这就是你想要的:

First, I write a function to merge models and return a new combined model. This is what you want:

def concat_horizontal(models, input_shape):
  models_count = len(models)
  hidden = []
  input = tf.keras.layers.Input(shape=input_shape)
  for i in range(models_count):
    hidden.append(models[i](input))
  output = tf.keras.layers.concatenate(hidden)
  model = tf.keras.Model(inputs=input, outputs=output)
  return model

让我们探索一个例子.假设我们想像这样合并两个顺序模型:

Let's explore an example. Say we want merge two sequential models like this:

def model_1():
  model = tf.keras.models.Sequential([
                      tf.keras.layers.Flatten(input_shape=(28,28,1)),
                      tf.keras.layers.Dense(150, activation='relu'),
                      tf.keras.layers.Dense(200, activation='relu'),
                      tf.keras.layers.Dense(150, activation='relu'),
                      tf.keras.layers.Dense(10, activation='softmax')], name="model1")
  model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
  return model

def model_2():
  model = tf.keras.models.Sequential([
                      tf.keras.layers.Flatten(input_shape=(28,28,1)),
                      tf.keras.layers.Dense(150, activation='relu'),
                      tf.keras.layers.Dense(150, activation='relu'),
                      tf.keras.layers.Dense(10, activation='softmax')], name="model2")
  model.compile(optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
  return model

model1 = model_1()
model2 = model_2()

让我们使用 MNIST 作为我们两个模型的训练数据集:

Let's use MNIST as train dataset for both of our models:

import tensorflow_datasets as tfds
ds_1 = tfds.load('mnist', split='train', as_supervised=True)
ds_2 = tfds.load('mnist', split='test', as_supervised=True)

def map_fn(image, label):
  image = image / 255
  return image, label

ds_1 = ds_1.map(map_fn).shuffle(1024).batch(32)
ds_2 = ds_2.map(map_fn).shuffle(1024).batch(32)

现在,我们可以训练模型,保存它们,然后像​​这样加载它们:

Now, we can train models, save them, and then load them like this:

model1.fit(ds_1, epochs=2, validation_data=ds_1)
model2.fit(ds_2, epochs=2, validation_data=ds_2)

model1.save('model1.h5')
model2.save('model2.h5')

model3 = tf.keras.models.load_model('model1.h5')
model4 = tf.keras.models.load_model('model2.h5')

所以我们有 2 个单独的模型(model3,model4)并且想要将它们合并到一个新的模型中.将它们沿着输入形状(在本例中为 MNIST 数据形状)传递给我们上面编写的函数:

So we have 2 separate models (model3,model4) and want to merge these, to a new one. Pass them along the input shape (in this case it is MNIST data shape) to the function we have written above:

new_model = concat_horizontal([model3,model4],(28,28,1))

现在,如果我们绘制这个新模型:

Now, if we plot this new model:

tf.keras.utils.plot_model(new_model)

是时候获得模型的预测了:

It's time to get predictions of model:

sample = ds_1.unbatch().take(1)
for i,j in sample:
  img = i
  lbl = j
img = tf.expand_dims(img,axis=0)
pred = new_model.predict(img)
pred = np.reshape(pred,(2,10))
results = np.argmax(pred,axis=1)
print(results)

import matplotlib.pyplot as plt
plt.imshow(np.array(img).squeeze())
plt.show

就我而言,我将两个预测归类为 4:

In my case I get both of predictions classified as 4:

输出:

这篇关于如何水平合并分类模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆