如何将tensorflow的内置模型架构用于3个以上的通道?
https://www.tensorflow.org/tutorials/images/segmentation
例如,当输入有3个通道(例如,RGB)时,在这里工作的模型可以工作。我想更改10个输入通道的代码。
注意:我不需要预先训练的模型,只需要底层架构。
发布于 2020-07-23 00:09:28
简而言之,您可以在调用tf.keras.applications对象时为输入定义一个自定义大小,添加一个或多个用于分类的层,然后就可以了!
让我们像在image segmentation tutorial中一样,使用在tf.keras.applications.MobileNetV2()中定义的模型体系结构来尝试它。假设你有10个通道,128x128像素的图像和50个不同的类别(它可以相应地改变)。
import tensorflow as tf
INPUT_WIDTH = 128
INPUT_HEIGHT = 128
N_CHANNELS = 10
N_CLASSES = 50一个玩具实现可能如下所示:
# 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教程中都有介绍)。
https://stackoverflow.com/questions/62989495
复制相似问题