How could we convert reshaped `FloatArray` Back to...
# datascience
a
How could we convert reshaped
FloatArray
Back to
BufferedImage
or save it as .jpg
z
Do you keep the shape? Is it known before converting to the buffered image? Is it a reshaped float array of that? That is the one float in the float array?
a
It's usually 3024 by 3024 BGR 3 mb file I was trying to convert the Image -> floatarray -> reshape -> image
Copy code
mk.ndarray(ImageConverter.toNormalizedFloatArray(File("shoe.jpg")),28,28,1).toList().toFloatArray()
z
So, as I understand the initial shape is 3024*3024*3 (BGR), but what is the desired shape? Looks like you want to resize, not reshape to 28*28*1 to create a greyscale image with size 28*28, isn't it? Correct me if I'm wrong. What is the goal? Use the image in the LeNet trained on fashionMnist with KotlinDL? Why did you use Multik? For reshape only?
In this example for KotlinDL you could use ImagePreprocessing to load an image, resize it and save the image to the directory https://github.com/JetBrains/KotlinDL/blob/release_0.3/examples/src/main/kotlin/examples/dataset/PreprocessOnlyOneImage.kt or I changed its example to be closer your needs
Copy code
val imageResource = ImagePreprocessing::class.java.getResource("/datasets/vgg/image2.jpg")
    val image = File(imageResource!!.toURI())
    val preprocessedImagesDirectory = File("processedImages")

    val preprocessing: Preprocessing = preprocess {
        load {
            pathToData = image
            imageShape = ImageShape(3024, 3024, 3)
            colorMode = ColorOrder.BGR
        }
        transformImage {
           
            resize {
                outputWidth = 28
                outputHeight = 28
                interpolation = InterpolationType.NEAREST
                save {
                    dirLocation = preprocessedImagesDirectory
                }
            }
        }
        transformTensor {
            rescale {
                scalingCoefficient = 255f
            }
        }
    }

    val rawImage = preprocessing().first
This is an example how to use ImagePreprocessing together with the Neural Network https://github.com/JetBrains/KotlinDL/blob/release_0.3/examples/src/main/kotlin/examples/dataset/VGGDogCatOnFly.kt
a
Sorry I am kinda new for DS U somewhat got the idea Idk if its possible but I was looking for a way to reshape the given image as the same way I used to train the model(fashionMinst) then predict it using
TensorFlowInferenceModel
Thnx a lot for the help
z
It's ok to ask the help with the Python or Kotlin part of your example here or in #kotlindl channel. I'll try to help. Usually, if you want to put the large image into the input of a neural net like LeNet with 28*28 input, you need to resize it first and if it has a plain input (784 instead of 28*28) you could reshape it to plain 1D FloatArray.
Unfortunately, currently the KotlinDL doesn't support conversion of BGR images to greyscale
a
Oh I never thought the existence of #kotlindl channel I will ask there for next time Btw thnx