@Test void testBuildDefaultImage() { Tensor tensor = new Tensor( DataType.INTEGER, new int[]{1, 2, 2, 3}, new int[][][][]{ new int[][][]{ new int[][]{ new int[]{0, 0, 255}, new int[]{0, 255, 0} }, new int[][]{ new int[]{255, 0, 0}, new int[]{0, 0, 0} } } } ); List<BufferedImage> images = ImageTransformerInfo.fromShape(tensor.getShapeAsArray()) .get() .imageBuilder() .build(tensor); assertEquals(1, images.size()); BufferedImage image = images.get(0); assertEquals(2, image.getWidth()); assertEquals(2, image.getHeight()); assertEquals(new Color(0, 0, 255), new Color(image.getRGB(0, 0))); assertEquals(new Color(255, 0, 0), new Color(image.getRGB(0, 1))); assertEquals(new Color(0, 255, 0), new Color(image.getRGB(1, 0))); assertEquals(new Color(0, 0, 0), new Color(image.getRGB(1, 1))); }
@Override public List<BufferedImage> build(Tensor input) throws EvaluationException { final int indexOfBatchSize = shapeAttributes.indexOf(ImgProperties.BATCH_SIZE); final int indexOfWidth = shapeAttributes.indexOf(ImgProperties.WIDTH); final int indexOfHeight = shapeAttributes.indexOf(ImgProperties.HEIGHT); final int indexOfChanel = shapeAttributes.indexOf(ImgProperties.CHANEL); if (indexOfWidth < 0 || indexOfHeight < 0 || indexOfChanel < 0) { throw new EvaluationException( String.format( "Shape attributes for converting a tensor into an image should contains at least these " + "3 elements : %s, but was given %s", Arrays.toString(List.of(ImgProperties.WIDTH, ImgProperties.HEIGHT, ImgProperties.CHANEL).toArray()), Arrays.toString(shapeAttributes.toArray()) ) ); } final int[] shape = input.getShapeAsArray(); if (shape.length != this.shapeAttributes.size()) { throw new EvaluationException( String.format( "The length of the tensor shape should be the same than the attribute shape " + "of the image converter. Tensor shape is %s, image converter attribute shape is %s", Arrays.toString(shape), Arrays.toString(shapeAttributes.toArray()) ) ); } int batchSize; if (indexOfBatchSize < 0) { batchSize = 1; } else { batchSize = shape[indexOfBatchSize]; } final int width = shape[indexOfWidth]; final int height = shape[indexOfHeight]; final int chanelSize = shape[indexOfChanel]; if (chanelSize != chanelProperties.size()) { throw new EvaluationException( String.format( "Chanel is expected to be of size %s but found tensor with chanel size of %s", chanelProperties.size(), chanelSize ) ); } List<BufferedImage> images = new ArrayList<>(); for (int batchIndex = 0; batchIndex < batchSize; batchIndex++) { final int imageType = getImageType(); final BufferedImage image = new BufferedImage(width, height, imageType); for (int widthIndex = 0; widthIndex < width; widthIndex++) { for (int heightIndex = 0; heightIndex < height; heightIndex++) { int[] coordChanel = new int[chanelSize]; for (int chanelIndex = 0; chanelIndex < chanelSize; chanelIndex++) { int[] coordTensor = new int[shape.length]; for (int i = 0; i < coordTensor.length; i++) { switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordTensor[i] = batchIndex; break; case WIDTH: coordTensor[i] = widthIndex; break; case HEIGHT: coordTensor[i] = heightIndex; break; case CHANEL: coordTensor[i] = chanelIndex; break; default: throw new IllegalStateException("Impossible to get there"); } int coordValue = ((Number) input.getCoord(coordTensor)).intValue(); switch (chanelProperties.get(chanelIndex)) { case GRAY_SCALE: case RED: coordChanel[0] = coordValue; break; case GREEN: coordChanel[1] = coordValue; break; case BLUE: coordChanel[2] = coordValue; break; default: throw new IllegalStateException("Impossible to get there"); } } } image.getRaster().setPixel(widthIndex, heightIndex, coordChanel); } } images.add(image); } return images; }
TensorIntoImages implements Builder<Tensor, List<BufferedImage>> { @Override public List<BufferedImage> build(Tensor input) throws EvaluationException { final int indexOfBatchSize = shapeAttributes.indexOf(ImgProperties.BATCH_SIZE); final int indexOfWidth = shapeAttributes.indexOf(ImgProperties.WIDTH); final int indexOfHeight = shapeAttributes.indexOf(ImgProperties.HEIGHT); final int indexOfChanel = shapeAttributes.indexOf(ImgProperties.CHANEL); if (indexOfWidth < 0 || indexOfHeight < 0 || indexOfChanel < 0) { throw new EvaluationException( String.format( "Shape attributes for converting a tensor into an image should contains at least these " + "3 elements : %s, but was given %s", Arrays.toString(List.of(ImgProperties.WIDTH, ImgProperties.HEIGHT, ImgProperties.CHANEL).toArray()), Arrays.toString(shapeAttributes.toArray()) ) ); } final int[] shape = input.getShapeAsArray(); if (shape.length != this.shapeAttributes.size()) { throw new EvaluationException( String.format( "The length of the tensor shape should be the same than the attribute shape " + "of the image converter. Tensor shape is %s, image converter attribute shape is %s", Arrays.toString(shape), Arrays.toString(shapeAttributes.toArray()) ) ); } int batchSize; if (indexOfBatchSize < 0) { batchSize = 1; } else { batchSize = shape[indexOfBatchSize]; } final int width = shape[indexOfWidth]; final int height = shape[indexOfHeight]; final int chanelSize = shape[indexOfChanel]; if (chanelSize != chanelProperties.size()) { throw new EvaluationException( String.format( "Chanel is expected to be of size %s but found tensor with chanel size of %s", chanelProperties.size(), chanelSize ) ); } List<BufferedImage> images = new ArrayList<>(); for (int batchIndex = 0; batchIndex < batchSize; batchIndex++) { final int imageType = getImageType(); final BufferedImage image = new BufferedImage(width, height, imageType); for (int widthIndex = 0; widthIndex < width; widthIndex++) { for (int heightIndex = 0; heightIndex < height; heightIndex++) { int[] coordChanel = new int[chanelSize]; for (int chanelIndex = 0; chanelIndex < chanelSize; chanelIndex++) { int[] coordTensor = new int[shape.length]; for (int i = 0; i < coordTensor.length; i++) { switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordTensor[i] = batchIndex; break; case WIDTH: coordTensor[i] = widthIndex; break; case HEIGHT: coordTensor[i] = heightIndex; break; case CHANEL: coordTensor[i] = chanelIndex; break; default: throw new IllegalStateException("Impossible to get there"); } int coordValue = ((Number) input.getCoord(coordTensor)).intValue(); switch (chanelProperties.get(chanelIndex)) { case GRAY_SCALE: case RED: coordChanel[0] = coordValue; break; case GREEN: coordChanel[1] = coordValue; break; case BLUE: coordChanel[2] = coordValue; break; default: throw new IllegalStateException("Impossible to get there"); } } } image.getRaster().setPixel(widthIndex, heightIndex, coordChanel); } } images.add(image); } return images; } }
TensorIntoImages implements Builder<Tensor, List<BufferedImage>> { @Override public List<BufferedImage> build(Tensor input) throws EvaluationException { final int indexOfBatchSize = shapeAttributes.indexOf(ImgProperties.BATCH_SIZE); final int indexOfWidth = shapeAttributes.indexOf(ImgProperties.WIDTH); final int indexOfHeight = shapeAttributes.indexOf(ImgProperties.HEIGHT); final int indexOfChanel = shapeAttributes.indexOf(ImgProperties.CHANEL); if (indexOfWidth < 0 || indexOfHeight < 0 || indexOfChanel < 0) { throw new EvaluationException( String.format( "Shape attributes for converting a tensor into an image should contains at least these " + "3 elements : %s, but was given %s", Arrays.toString(List.of(ImgProperties.WIDTH, ImgProperties.HEIGHT, ImgProperties.CHANEL).toArray()), Arrays.toString(shapeAttributes.toArray()) ) ); } final int[] shape = input.getShapeAsArray(); if (shape.length != this.shapeAttributes.size()) { throw new EvaluationException( String.format( "The length of the tensor shape should be the same than the attribute shape " + "of the image converter. Tensor shape is %s, image converter attribute shape is %s", Arrays.toString(shape), Arrays.toString(shapeAttributes.toArray()) ) ); } int batchSize; if (indexOfBatchSize < 0) { batchSize = 1; } else { batchSize = shape[indexOfBatchSize]; } final int width = shape[indexOfWidth]; final int height = shape[indexOfHeight]; final int chanelSize = shape[indexOfChanel]; if (chanelSize != chanelProperties.size()) { throw new EvaluationException( String.format( "Chanel is expected to be of size %s but found tensor with chanel size of %s", chanelProperties.size(), chanelSize ) ); } List<BufferedImage> images = new ArrayList<>(); for (int batchIndex = 0; batchIndex < batchSize; batchIndex++) { final int imageType = getImageType(); final BufferedImage image = new BufferedImage(width, height, imageType); for (int widthIndex = 0; widthIndex < width; widthIndex++) { for (int heightIndex = 0; heightIndex < height; heightIndex++) { int[] coordChanel = new int[chanelSize]; for (int chanelIndex = 0; chanelIndex < chanelSize; chanelIndex++) { int[] coordTensor = new int[shape.length]; for (int i = 0; i < coordTensor.length; i++) { switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordTensor[i] = batchIndex; break; case WIDTH: coordTensor[i] = widthIndex; break; case HEIGHT: coordTensor[i] = heightIndex; break; case CHANEL: coordTensor[i] = chanelIndex; break; default: throw new IllegalStateException("Impossible to get there"); } int coordValue = ((Number) input.getCoord(coordTensor)).intValue(); switch (chanelProperties.get(chanelIndex)) { case GRAY_SCALE: case RED: coordChanel[0] = coordValue; break; case GREEN: coordChanel[1] = coordValue; break; case BLUE: coordChanel[2] = coordValue; break; default: throw new IllegalStateException("Impossible to get there"); } } } image.getRaster().setPixel(widthIndex, heightIndex, coordChanel); } } images.add(image); } return images; } TensorIntoImages(ImageTransformerInfo transformerInfo); }
TensorIntoImages implements Builder<Tensor, List<BufferedImage>> { @Override public List<BufferedImage> build(Tensor input) throws EvaluationException { final int indexOfBatchSize = shapeAttributes.indexOf(ImgProperties.BATCH_SIZE); final int indexOfWidth = shapeAttributes.indexOf(ImgProperties.WIDTH); final int indexOfHeight = shapeAttributes.indexOf(ImgProperties.HEIGHT); final int indexOfChanel = shapeAttributes.indexOf(ImgProperties.CHANEL); if (indexOfWidth < 0 || indexOfHeight < 0 || indexOfChanel < 0) { throw new EvaluationException( String.format( "Shape attributes for converting a tensor into an image should contains at least these " + "3 elements : %s, but was given %s", Arrays.toString(List.of(ImgProperties.WIDTH, ImgProperties.HEIGHT, ImgProperties.CHANEL).toArray()), Arrays.toString(shapeAttributes.toArray()) ) ); } final int[] shape = input.getShapeAsArray(); if (shape.length != this.shapeAttributes.size()) { throw new EvaluationException( String.format( "The length of the tensor shape should be the same than the attribute shape " + "of the image converter. Tensor shape is %s, image converter attribute shape is %s", Arrays.toString(shape), Arrays.toString(shapeAttributes.toArray()) ) ); } int batchSize; if (indexOfBatchSize < 0) { batchSize = 1; } else { batchSize = shape[indexOfBatchSize]; } final int width = shape[indexOfWidth]; final int height = shape[indexOfHeight]; final int chanelSize = shape[indexOfChanel]; if (chanelSize != chanelProperties.size()) { throw new EvaluationException( String.format( "Chanel is expected to be of size %s but found tensor with chanel size of %s", chanelProperties.size(), chanelSize ) ); } List<BufferedImage> images = new ArrayList<>(); for (int batchIndex = 0; batchIndex < batchSize; batchIndex++) { final int imageType = getImageType(); final BufferedImage image = new BufferedImage(width, height, imageType); for (int widthIndex = 0; widthIndex < width; widthIndex++) { for (int heightIndex = 0; heightIndex < height; heightIndex++) { int[] coordChanel = new int[chanelSize]; for (int chanelIndex = 0; chanelIndex < chanelSize; chanelIndex++) { int[] coordTensor = new int[shape.length]; for (int i = 0; i < coordTensor.length; i++) { switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordTensor[i] = batchIndex; break; case WIDTH: coordTensor[i] = widthIndex; break; case HEIGHT: coordTensor[i] = heightIndex; break; case CHANEL: coordTensor[i] = chanelIndex; break; default: throw new IllegalStateException("Impossible to get there"); } int coordValue = ((Number) input.getCoord(coordTensor)).intValue(); switch (chanelProperties.get(chanelIndex)) { case GRAY_SCALE: case RED: coordChanel[0] = coordValue; break; case GREEN: coordChanel[1] = coordValue; break; case BLUE: coordChanel[2] = coordValue; break; default: throw new IllegalStateException("Impossible to get there"); } } } image.getRaster().setPixel(widthIndex, heightIndex, coordChanel); } } images.add(image); } return images; } TensorIntoImages(ImageTransformerInfo transformerInfo); @Override List<BufferedImage> build(Tensor input); }
TensorIntoImages implements Builder<Tensor, List<BufferedImage>> { @Override public List<BufferedImage> build(Tensor input) throws EvaluationException { final int indexOfBatchSize = shapeAttributes.indexOf(ImgProperties.BATCH_SIZE); final int indexOfWidth = shapeAttributes.indexOf(ImgProperties.WIDTH); final int indexOfHeight = shapeAttributes.indexOf(ImgProperties.HEIGHT); final int indexOfChanel = shapeAttributes.indexOf(ImgProperties.CHANEL); if (indexOfWidth < 0 || indexOfHeight < 0 || indexOfChanel < 0) { throw new EvaluationException( String.format( "Shape attributes for converting a tensor into an image should contains at least these " + "3 elements : %s, but was given %s", Arrays.toString(List.of(ImgProperties.WIDTH, ImgProperties.HEIGHT, ImgProperties.CHANEL).toArray()), Arrays.toString(shapeAttributes.toArray()) ) ); } final int[] shape = input.getShapeAsArray(); if (shape.length != this.shapeAttributes.size()) { throw new EvaluationException( String.format( "The length of the tensor shape should be the same than the attribute shape " + "of the image converter. Tensor shape is %s, image converter attribute shape is %s", Arrays.toString(shape), Arrays.toString(shapeAttributes.toArray()) ) ); } int batchSize; if (indexOfBatchSize < 0) { batchSize = 1; } else { batchSize = shape[indexOfBatchSize]; } final int width = shape[indexOfWidth]; final int height = shape[indexOfHeight]; final int chanelSize = shape[indexOfChanel]; if (chanelSize != chanelProperties.size()) { throw new EvaluationException( String.format( "Chanel is expected to be of size %s but found tensor with chanel size of %s", chanelProperties.size(), chanelSize ) ); } List<BufferedImage> images = new ArrayList<>(); for (int batchIndex = 0; batchIndex < batchSize; batchIndex++) { final int imageType = getImageType(); final BufferedImage image = new BufferedImage(width, height, imageType); for (int widthIndex = 0; widthIndex < width; widthIndex++) { for (int heightIndex = 0; heightIndex < height; heightIndex++) { int[] coordChanel = new int[chanelSize]; for (int chanelIndex = 0; chanelIndex < chanelSize; chanelIndex++) { int[] coordTensor = new int[shape.length]; for (int i = 0; i < coordTensor.length; i++) { switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordTensor[i] = batchIndex; break; case WIDTH: coordTensor[i] = widthIndex; break; case HEIGHT: coordTensor[i] = heightIndex; break; case CHANEL: coordTensor[i] = chanelIndex; break; default: throw new IllegalStateException("Impossible to get there"); } int coordValue = ((Number) input.getCoord(coordTensor)).intValue(); switch (chanelProperties.get(chanelIndex)) { case GRAY_SCALE: case RED: coordChanel[0] = coordValue; break; case GREEN: coordChanel[1] = coordValue; break; case BLUE: coordChanel[2] = coordValue; break; default: throw new IllegalStateException("Impossible to get there"); } } } image.getRaster().setPixel(widthIndex, heightIndex, coordChanel); } } images.add(image); } return images; } TensorIntoImages(ImageTransformerInfo transformerInfo); @Override List<BufferedImage> build(Tensor input);  }