首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于3个以上(RGB)通道的Tensorflow内置模型架构

用于3个以上(RGB)通道的Tensorflow内置模型架构
EN

Stack Overflow用户
提问于 2020-07-20 14:04:17
回答 1查看 401关注 0票数 0

如何将tensorflow的内置模型架构用于3个以上的通道?

https://www.tensorflow.org/tutorials/images/segmentation

例如,当输入有3个通道(例如,RGB)时,在这里工作的模型可以工作。我想更改10个输入通道的代码。

注意:我不需要预先训练的模型,只需要底层架构。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-23 00:09:28

简而言之,您可以在调用tf.keras.applications对象时为输入定义一个自定义大小,添加一个或多个用于分类的层,然后就可以了!

让我们像在image segmentation tutorial中一样,使用在tf.keras.applications.MobileNetV2()中定义的模型体系结构来尝试它。假设你有10个通道,128x128像素的图像和50个不同的类别(它可以相应地改变)。

代码语言:javascript
复制
import tensorflow as tf
INPUT_WIDTH = 128
INPUT_HEIGHT = 128
N_CHANNELS = 10
N_CLASSES = 50

一个玩具实现可能如下所示:

代码语言:javascript
复制
# 1. Import the empty architecture
model_arch = tf.keras.applications.MobileNetV2(
    input_shape=[INPUT_WIDTH, INPUT_HEIGHT, N_CHANNELS],
    
    # Removing the fully-connected layer at the top of the network.
    #  Unless you have the same number of labels as the original architecture, 
    #  you should remove it.
    include_top=False,  
    
    # Using no pretrained weights (random initialization)
    weights=None)

# 2. Define the full architecture by adding a classification head.
#    For this example, I chose to flatten the results and use a single Dense layer.

model = tf.keras.Sequential()
model.add(model_arch)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(N_CLASSES))


# 3. Try the model with a toy example, a single random input image
#    Input shape: (BATCH_SIZE, INPUT_WIDTH, INPUT_HEIGHT, N_CHANNELS) 
import numpy as np
inp = np.random.rand(1, INPUT_WIDTH, INPUT_HEIGHT, N_CHANNELS)
print(inp.shape)
#> (1, 128, 128, 10)

res = model.predict(inp)
print(res.shape)
#> (1, 50)

你已经准备好了你的模型架构!你只需要一些数据来使用model.fit()训练它,定义一个损失,然后开始你的训练!(所有这些在许多TF教程中都有介绍)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62989495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档