栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Keras:如何将fit_generator与不同类型的多个输出一起使用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Keras:如何将fit_generator与不同类型的多个输出一起使用

下面的示例可能是不言自明的!“虚拟”模型采用1个输入(图像),并输出2个值。该模型为每个输出计算MSE。

x = Convolution2D(8, 5, 5, subsample=(1, 1))(image_input)x = Activation('relu')(x)x = Flatten()(x)x = Dense(50, W_regularizer=l2(0.0001))(x)x = Activation('relu')(x)output1 = Dense(1, activation='linear', name='output1')(x)output2 = Dense(1, activation='linear', name='output2')(x)model = Model(input=image_input, output=[output1, output2])model.compile(optimizer='adam', loss={'output1': 'mean_squared_error', 'output2': 'mean_squared_error'})

下面的函数生成批次以在训练过程中提供模型。它采用训练数据

x
和标签
y
,其中y = [y1,y2]

batch_generator(x, y, batch_size, is_train):    sample_idx = 0    while True:       X = np.zeros((batch_size, input_height, input_width, n_channels), dtype='float32')       y1 = np.zeros((batch_size, mask_height, mask_width), dtype='float32')       y2 = np.zeros((batch_size, 1), dtype='float32')       # fill up the batch       for row in range(batch_sz):image = x[sample_idx]mask = y[0][sample_idx]binary_value = y[1][sample_idx]# transform/preprocess imageimage = cv2.resize(image, (input_width, input_height))if is_train:    image, mask = my_data_augmentation_function(image, mask)X_batch[row, ;, :, :] = imagey1_batch[row, :, :] = masky2_batch[row, 0] = binary_valuesample_idx += 1       # Normalize inputs       X_batch = X_batch/255.       yield(X_batch, {'output1': y1_batch, 'output2': y2_batch} ))

最后,我们调用fit_generator()

    model.fit_generator(batch_generator(X_train, y_train, batch_size, is_train=1))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号