Skip to content

torch vision toImmutableImage meet error ,the tensor toArray byte shape length is not the same with the image bytes make ImageParseException #89

@mullerhai

Description

@mullerhai

Hi,
Now ,we try to use Storch vision to process image for tenso , I know this part dependency with scrimage library. but meet ImageParseException: Image parsing failed for unknown image format ,from image to Tensor then convert to Image

I know the demo image have 3 channels colorful image , so get the pixel matrix / tensor shape is [3, 1199, 907],so get the byte length is 3* 1199 * 907 = 3262479 but use the
val image = ImmutableImage.loader().fromFile(file)

val bytes = image.forWriter(JpegWriter.Default).bytes()
val outImage = ImmutableImage.loader().fromBytes(bytes)
outImage.output(JpegWriter.Default, new File("src/main/resources/flipx.jpeg"))
, get the bytes length is 147369

object functional {
  def toTensor(pic: ImmutableImage): Tensor[Float32] =
    val bytes = pic.rgb.flatten
    // transpose NxHxWxC to NxCxHxW because pytorch expects channels first
    Tensor(ArraySeq.unsafeWrapArray(bytes))
      .reshape(pic.height, pic.width, 3)
      .permute(2, 0, 1)
      .to(dtype = float32) / 255

  def toImmutableImage[D <: FloatNN](pic: Tensor[D]): ImmutableImage =
    var _pic = pic
    if !Seq(2, 3).contains(pic.dim) then
      throw new IllegalArgumentException(
        s"pic should be 2/3 dimensional. Got ${pic.dim} dimensions."
      )
    else if pic.dim == 2 then
      // if 2D image, add channel dimension (CHW)
      _pic = pic.unsqueeze(0)
    // check number of channels
    println(pic.shape)
    if pic.shape(-3) > 4 then
      throw new IllegalArgumentException(
        s"pic should not have > 4 channels. Got ${pic.shape(-3)} channels."
      )
    val intImage = (_pic.permute(1, 2, 0) * 255).to(dtype = int8)
    val bytes = intImage.toArray
    ImmutableImage.loader().fromBytes(bytes)
}

  import java.io.File

extension (pic: ImmutableImage)
    def toTensors() = toTensor(pic)

  def grayscale(pic: ImmutableImage, p: GrayscaleMethod = GrayscaleMethod.AVERAGE): Tensor[Float32] =
    pic.toGrayscale(p).toTensors()

  val file = new File("src/main/resources/a1.jpg")

  val image = ImmutableImage.loader().fromFile(file)

  val tensor2 = functional.grayscale(image)

  println(tensor9)
  val outImage =functional.toImmutableImage(tensor9)
  outImage.output(JpegWriter.Default, new File("src/main/resources/flipx.jpeg"))

error console log


tensor dtype=float32, shape=[3, 1199, 907], device=CPU 
[[[-0.6965, -0.0801, 0.5022, ..., -0.3883, -0.4054, -0.4054],
  [-0.3883, 0.6221, 1.3413, ..., -0.4054, -0.4054, -0.4054],
  [0.0227, 0.0569, -0.1999, ..., -0.2856, -0.2856, -0.3027],
  ...,
  [0.9474, 0.9303, 0.9132, ..., 1.2557, 1.2214, 1.1872],
  [1.0502, 1.0331, 0.9988, ..., 1.1700, 1.1015, 1.0331],
  [1.1015, 1.0844, 1.0502, ..., 1.5125, 1.4440, 1.3584]],

 [[-0.5826, 0.0476, 0.6429, ..., -0.2675, -0.2850, -0.2850],
  [-0.2675, 0.7654, 1.5007, ..., -0.2850, -0.2850, -0.2850],
  [0.1527, 0.1877, -0.0749, ..., -0.1625, -0.1625, -0.1800],
  ...,
  [1.0980, 1.0805, 1.0630, ..., 1.4132, 1.3782, 1.3431],
  [1.2031, 1.1856, 1.1506, ..., 1.3256, 1.2556, 1.1856],
  [1.2556, 1.2381, 1.2031, ..., 1.6758, 1.6057, 1.5182]],

 [[-0.3578, 0.2696, 0.8622, ..., -0.0441, -0.0615, -0.0615],
  [-0.0441, 0.9842, 1.7163, ..., -0.0615, -0.0615, -0.0615],
  [0.3742, 0.4091, 0.1476, ..., 0.0605, 0.0605, 0.0431],
  ...,
  [1.3154, 1.2980, 1.2805, ..., 1.6291, 1.5942, 1.5594],
  [1.4200, 1.4025, 1.3677, ..., 1.5420, 1.4722, 1.4025],
  [1.4722, 1.4548, 1.4200, ..., 1.8905, 1.8208, 1.7337]]]
ArraySeq(3, 1199, 907)
Exception in thread "main" com.sksamuel.scrimage.ImageParseException: Image parsing failed for unknown image format. Tried the following ImageReader implementations:
com.sksamuel.scrimage.nio.ImageIOReader (delegates to the JDK javax.imageio readers) failed due to No javax.imageio.ImageReader supported this image format
com.sksamuel.scrimage.nio.PngReader (delegates to ar.com.hjg.pngj) failed
com.sksamuel.scrimage.webp.WebpImageReader@37864b77 failed due to java.io.IOException: [Decoding of C:\Users\hai71\AppData\Local\Temp\input6936903937282349564webp failed., Status: 3(BITSTREAM_ERROR)]
	at com.sksamuel.scrimage.nio.ImageReaders.read(ImageReaders.java:64)
	at com.sksamuel.scrimage.nio.ImageReaders.read(ImageReaders.java:37)
	at com.sksamuel.scrimage.nio.ImmutableImageLoader.load(ImmutableImageLoader.java:150)
	at com.sksamuel.scrimage.nio.ImmutableImageLoader.fromBytes(ImmutableImageLoader.java:103)
	at torchvision.transforms.functional$.toImmutableImage(functional.scala:650)
	at main$package$.main(main.scala:27)
	at main.main(main.scala:5)


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions