我有一个由二进制类分布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)
下面是此混合模型的数据和模型,如何合并它们以创建图中所示的混合模型?
任何形式的帮助都将不胜感激。提前感谢您的帮助。
1条答案
按热度按时间xggvc2p61#
我不认为
Keras
直接允许这种形式的控制流作为计算图的一部分。这是因为控制流API,如tf.case
和tensorflow 中的其他类似条件语句,对Tensor的输出值起作用。一种方法是设置3个模型,分别训练它们,然后在推理过程中,使用一个条件循环来设置通过这3个模型的数据控制流。但是,既然您试图构建一个stacked
分类器,为什么不使用2 losses
(或者custom loss
)构建一个多输出分类器呢?您可以为预测动物的第一个输出定义
binary_crossentropy
,为预测品种的第二个输出定义sparse_categorical_crossentropy
。您可以将损失权重设置为60-40或其他比率,以确保哪个输出优先考虑多少。x一个一个一个一个x一个一个二个x