How to use different size grayscale images as input?
How to use different size grayscale images as input? I trained a convolutional neural network with 32x32x1 small image patches. My output is the same size of image (32x32x1). Here is my network structure. I used deserializers to read input and output images with same pixel dimension.
modelPath = "Model/convnet.cmf"
TrainConvNet = {
action = "train"
BrainScriptNetworkBuilder = {
model (features) = {
l1 = ConvolutionalLayer {...} (features)
l2 = ConvolutionalLayer {...} (l1)
z = ConvolutionalLayer {1, (5:5), ...} (l2)
}.z
# inputs
features = ImageInput(32, 32, 1)
labels = ImageInput(32, 32, 1)
z = model (features)
# mean squared error
ce = Sqrt(SquareError(labels, z)) * $recip_num_pixels$
errs = Sqrt(SquareError(labels, z)) * $recip_num_pixels$
featureNodes = (features)
labelNodes = (labels)
criterionNodes = (ce)
evaluationNodes = (errs)
outputNodes = (z)
}
SGD = {...}
reader = {
verbosity = 0
randomize = true
deserializers = (
{
type = "ImageDeserializer" ; module = "ImageReader"
file = "$dataDir$/train_map_input.txt"
input = {
features = {
transforms = (
{ type = "Crop" ; cropType = "center" ; cropRatio = 1.0 ; jitterType = "None" } :
{ type = "Scale" ; width = 32 ; height = 32 ; channels = 1 ; interpolations = "linear" }
)
}
ignored = { labelDim = 2 }
}
}:{
type = "ImageDeserializer" ; module = "ImageReader"
file = "$dataDir$/train_map_output.txt"
input = {
labels = {
transforms = (
{ type = "Crop" ; cropType = "center" ; cropRatio = 1.0 ; jitterType = "None" } :
{ type = "Scale" ; width = 32 ; height = 32 ; channels = 1 ; interpolations = "linear" }
)
}
ignored = { labelDim = 2 }
}
}
) # deserializers end
} # reader end
}
I was able to read one of my test images (32x32x1) using ImageReader and write my network's output into txt file using "write" action.
Write = {
action = "write"
outputNodeNames = (z, features)
reader = {
readerType = ImageReader
file = "$dataDir$/test_map.txt"
radomize = Auto
features={
width = 32
height = 32
channels = 1
labelDim=2;
}
}
outputPath = "$outputDir$/out" # dump the output to this text file
}
However, if I have a test image with different pixel dimension (ex, 640x480) and feed it to my network (run "write" action), then it looks like the network always downsizes the input image to 32x32 and outputs 32x32 image. Is there anyway to keep the original pixel dimension and make the network take the input image and apply the learned convolution filters? Thank you!
You can create a new network that matches the size of your new input images, but uses the same parameters as your trained network. You can do so with a BrainScriptNetworkBuilder block in your write action block or create a new network on disc using the edit action and use that network instead in your write block. E.g.,
create_net = {
action = "edit"
outputModelPath = "Model/convnet640.cmf"
BrainScriptNetworkBuilder = (new ComputationNetwork {
# Inputs
features = ImageInput(640, 480, 1)
# Load current model
network = BS.Network.Load ("Model/convnet.cmf")
model = BS.Network.CloneFunction (network.features, network.z, parameters="constant")
# Build high-res model
z = model (features )
# Special Nodes
featureNodes = (features)
outputNodes = (z)
})
}
Disclaimer: Code written in browser and not tested.
Thank you so much. I tested your code, it works well. Thank you!