Keras条件通过模型、训练与预测

9gm1akwq  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(212)

我有一个由二进制类分布cat and dog组成的数据集,每个类中有four subclasses (breeds of cat or dog),所以我的数据目录结构是

现在,我面临问题二,创建一个two-stage Neural Network。在first stage中,模型将执行binary classification (cat or dog),在second step中,模型将基于第一阶段模型输出执行multi-class classification (breeds of dog or breeds of cat)。我的模型的图示为:

如何在Keras中实现此功能?
**注:**阶段1和阶段2的单独代码实现以及这两个阶段的伪数据生成器为,
导入库:

from keras import metrics
import keras
import tensorflow as tf
from keras.datasets import mnist,cifar10
from keras.layers import *
from keras.models import *
from keras.utils import *
from matplotlib import pyplot as plt
import numpy as np
np.random.seed(1)

两阶段模型的数据为

num_images = 200
# binary data generator
X_train_2_class = np.random.random([num_images, 32, 32, 3])
y_train_2_class = np.random.randint(low=0, high=2, size=num_images)

# half of images are cat & half of them are dog

# cat breeds data 
X_train_4_cat_breeds = np.random.random([num_images//2, 32, 32, 3])
y_train_4_cat_breeds = np.random.randint(low=0, high=4, size=num_images)
# dog breeds data     
X_train_4_dog_breeds = np.random.random([num_images//2, 32, 32, 3])
y_train_4_dog_breeds = np.random.randint(low=0, high=4, size=num_images)

阶段1和阶段2的模型

img_rows,img_cols,number_of_class = 32,32,2
number_of_cat_breeds, number_of_dog_breeds = 4,4

input = Input(shape=(img_rows,img_cols,3))

#----------- 1st Stage Model ------------------------------------
conv_01 = Convolution2D(64, 3, 3, activation='relu',name = 'conv_01') (input)

skip_cat =  conv_01

conv_02 = Convolution2D(64, 3, 3, activation='relu',name = 'conv_02') (conv_01)

skip_dog =  conv_02

flatten_first_stage_model =  Flatten() (conv_02)
Output_main_model = Dense(units = number_of_class , activation = 'softmax', name = "Output_layer")(flatten_first_stage_model)
# #----------- 2nd stage Conditional  Cat model ------------------------------------ 
conv_03 = Convolution2D(64, 3, 3, activation='relu',name = 'conv_03') (skip_cat)
flatten_cat_model =  Flatten() (conv_03)
Output_cat_model = Dense(units = number_of_cat_breeds , activation = 'softmax', name = "Output_layer_cat")(flatten_cat_model)
# #----------- 2nd stage Conditional  Dog model ------------------------------------ 
conv_04 = Convolution2D(64, 3, 3, activation='relu',name = 'conv_04') (skip_dog)
flatten_dog_model =  Flatten() (conv_04)
Output_dog_model = Dense(units = number_of_dog_breeds , activation = 'softmax', name = "Output_layer_dog")(flatten_dog_model)
# --------------------------- Models of 1st & 2nd stage -----------------------
model_at_stage_1 = Model(inputs = input , outputs = Output_main_model,name = 'model_main')
cat_model_at_stage_2 = Model(inputs = input , outputs = Output_cat_model ,name = 'cat_breeds_model')
dog_model_at_stage_2 = Model(inputs = input , outputs = Output_dog_model ,name = 'dog_breeds_model')

# plot_model(model_at_stage_1,show_shapes=1)

下面是此混合模型的数据和模型,如何合并它们以创建图中所示的混合模型?

任何形式的帮助都将不胜感激。提前感谢您的帮助。

xggvc2p6

xggvc2p61#

我不认为Keras直接允许这种形式的控制流作为计算图的一部分。这是因为控制流API,如tf.case和tensorflow 中的其他类似条件语句,对Tensor的输出值起作用。一种方法是设置3个模型,分别训练它们,然后在推理过程中,使用一个条件循环来设置通过这3个模型的数据控制流。但是,既然您试图构建一个stacked分类器,为什么不使用2 losses(或者custom loss)构建一个多输出分类器呢?
您可以为预测动物的第一个输出定义binary_crossentropy,为预测品种的第二个输出定义sparse_categorical_crossentropy。您可以将损失权重设置为60-40或其他比率,以确保哪个输出优先考虑多少。

from tensorflow.keras import layers, Model, utils

#Model architecture
inp = layers.Input((500,500,3))
x = layers.Conv2D(10, 3)(inp)
x = layers.MaxPool2D(3)(x)
x = layers.Conv2D(10, 3)(x)
x = layers.MaxPool2D(3)(x)
x = layers.Conv2D(10, 3)(x)
x = layers.MaxPool2D(3)(x)
x1 = layers.Flatten()(x)
out1 = layers.Dense(1, activation='sigmoid', name='animal')(x1)
x2 = layers.Conv2D(10, 3)(x)
x2 = layers.MaxPool2D(3)(x2)
x2 = layers.Flatten()(x2)
out2 = layers.Dense(10, activation='softmax', name='breed')(x2)

model = Model(inp, [out1, out2])

utils.plot_model(model, show_shapes=True, show_layer_names=False)

x一个一个一个一个x一个一个二个x

相关问题