Reputation: 3241
How to make use of tensorflow's builtin model architectures for more than 3 channels?
https://www.tensorflow.org/tutorials/images/segmentation
For example, the model working on here works when input has 3 channels (e.g., RGB). I want to change the code for 10 input channels.
NOTE: I don't need the pretrained model, just the underlying architecture.
Upvotes: 0
Views: 1302
Reputation: 7631
In short, you can define a custom size for the input when calling a tf.keras.applications
object, add one or several layers for classification, and you're good to go!
Let's try it out with the model architecture defined in tf.keras.applications.MobileNetV2()
just like in the image segmentation tutorial. Say you have 10 channels, 128x128 pixel images and 50 different classes (it can be changed accordingly).
import tensorflow as tf
INPUT_WIDTH = 128
INPUT_HEIGHT = 128
N_CHANNELS = 10
N_CLASSES = 50
A toy implementation might look like this:
# 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)
You have your model architecture ready! All you need is some data to train it using model.fit()
, define a loss and start your training! (all of these are covered in many TF tutorials).
Upvotes: 2