@Test void testBuilderCustomShapeImage() { BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, new Color(0, 0, 255).getRGB()); image.setRGB(0, 1, new Color(0, 255, 0).getRGB()); image.setRGB(1, 0, new Color(255, 0, 0).getRGB()); image.setRGB(1, 1, new Color(0, 0, 0).getRGB()); ImagesIntoTensor builder = new ImageTransformerInfo( ImageDefaults.CHANEL_PROPERTIES_3D, List.of( ImgProperties.HEIGHT, ImgProperties.WIDTH, ImgProperties.CHANEL ), new int[]{2, 2, 3} ).tensorBuilder(); Tensor tensor = builder.build(List.of(image)); assertEquals(DataType.INTEGER, tensor.getType()); assertArrayEquals(new int[]{2, 2, 3}, tensor.getShapeAsArray()); int[][][] imageArray = ((int[][][]) tensor.getData()); assertArrayEquals(new int[]{0, 0, 255}, imageArray[0][0]); assertArrayEquals(new int[]{0, 255, 0}, imageArray[1][0]); assertArrayEquals(new int[]{255, 0, 0}, imageArray[0][1]); assertArrayEquals(new int[]{0, 0, 0}, imageArray[1][1]); }
@Override public Tensor build(List<BufferedImage> inputsImages) { final int batchSize = inputsImages.size(); List<BufferedImage> imagesList = inputsImages .stream() .map(x -> { int currentHeight = x.getHeight(); int currentWidth = x.getWidth(); boolean shouldResizeHeight = this.expectedHeigth != -1 && currentHeight != this.expectedHeigth; boolean shouldResizeWidth = this.expectedWidth != -1 && currentWidth != this.expectedWidth; if (shouldResizeHeight || shouldResizeWidth) { Image scaledImg = x.getScaledInstance(this.expectedWidth, this.expectedHeigth, Image.SCALE_SMOOTH); BufferedImage output = new BufferedImage(this.expectedWidth, this.expectedHeigth, x.getType()); Graphics2D bGr = output.createGraphics(); bGr.drawImage(scaledImg, 0, 0, null); bGr.dispose(); return output; } else { return x; } }) .collect(Collectors.toList()); final Set<Integer> allWidth = imagesList .stream() .map(BufferedImage::getWidth) .collect(Collectors.toSet()); if (allWidth.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same width. Found several : %s", allWidth.toArray() ) ); } final int width = allWidth.iterator().next(); final Set<Integer> allHeight = imagesList .stream() .map(BufferedImage::getHeight) .collect(Collectors.toSet()); if (allHeight.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same height. Found several : %s", allHeight.toArray() ) ); } final int height = allHeight.iterator().next(); final int chanelSize = chanelProperties.size(); final int[] tensorShapeArray = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int dimensionSize; switch (shapeAttributes.get(i)) { case BATCH_SIZE: dimensionSize = batchSize; break; case HEIGHT: dimensionSize = height; break; case WIDTH: dimensionSize = width; break; case CHANEL: dimensionSize = chanelSize; break; default: throw new IllegalStateException( String.format("Unknown chanel property : %s", shapeAttributes.get(i).toString())); } tensorShapeArray[i] = dimensionSize; } final Tensor tensor = new Tensor(DataType.INTEGER, new TensorShape(tensorShapeArray)); for (int imageIndex = 0; imageIndex < imagesList.size(); imageIndex++) { BufferedImage image = imagesList.get(imageIndex); for (int widthIndex = 0; widthIndex < image.getWidth(); widthIndex++) { for (int heightIndex = 0; heightIndex < image.getHeight(); heightIndex++) { for (int chanelIndex = 0; chanelIndex < chanelProperties.size(); chanelIndex++) { final ImgChanelProperties chanProp = chanelProperties.get(chanelIndex); final int chanelValue = getChanelValue(image, widthIndex, heightIndex, chanProp); final int[] tensorCoordinates = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int coordinateValue; switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordinateValue = imageIndex; break; case HEIGHT: coordinateValue = heightIndex; break; case WIDTH: coordinateValue = widthIndex; break; case CHANEL: coordinateValue = chanelIndex; break; default: throw new IllegalStateException("Unknown TensorImageProperties"); } tensorCoordinates[i] = coordinateValue; } tensor.setOnCoord(chanelValue, tensorCoordinates); } } } } return tensor; }
ImagesIntoTensor implements Builder<List<BufferedImage>, Tensor> { @Override public Tensor build(List<BufferedImage> inputsImages) { final int batchSize = inputsImages.size(); List<BufferedImage> imagesList = inputsImages .stream() .map(x -> { int currentHeight = x.getHeight(); int currentWidth = x.getWidth(); boolean shouldResizeHeight = this.expectedHeigth != -1 && currentHeight != this.expectedHeigth; boolean shouldResizeWidth = this.expectedWidth != -1 && currentWidth != this.expectedWidth; if (shouldResizeHeight || shouldResizeWidth) { Image scaledImg = x.getScaledInstance(this.expectedWidth, this.expectedHeigth, Image.SCALE_SMOOTH); BufferedImage output = new BufferedImage(this.expectedWidth, this.expectedHeigth, x.getType()); Graphics2D bGr = output.createGraphics(); bGr.drawImage(scaledImg, 0, 0, null); bGr.dispose(); return output; } else { return x; } }) .collect(Collectors.toList()); final Set<Integer> allWidth = imagesList .stream() .map(BufferedImage::getWidth) .collect(Collectors.toSet()); if (allWidth.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same width. Found several : %s", allWidth.toArray() ) ); } final int width = allWidth.iterator().next(); final Set<Integer> allHeight = imagesList .stream() .map(BufferedImage::getHeight) .collect(Collectors.toSet()); if (allHeight.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same height. Found several : %s", allHeight.toArray() ) ); } final int height = allHeight.iterator().next(); final int chanelSize = chanelProperties.size(); final int[] tensorShapeArray = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int dimensionSize; switch (shapeAttributes.get(i)) { case BATCH_SIZE: dimensionSize = batchSize; break; case HEIGHT: dimensionSize = height; break; case WIDTH: dimensionSize = width; break; case CHANEL: dimensionSize = chanelSize; break; default: throw new IllegalStateException( String.format("Unknown chanel property : %s", shapeAttributes.get(i).toString())); } tensorShapeArray[i] = dimensionSize; } final Tensor tensor = new Tensor(DataType.INTEGER, new TensorShape(tensorShapeArray)); for (int imageIndex = 0; imageIndex < imagesList.size(); imageIndex++) { BufferedImage image = imagesList.get(imageIndex); for (int widthIndex = 0; widthIndex < image.getWidth(); widthIndex++) { for (int heightIndex = 0; heightIndex < image.getHeight(); heightIndex++) { for (int chanelIndex = 0; chanelIndex < chanelProperties.size(); chanelIndex++) { final ImgChanelProperties chanProp = chanelProperties.get(chanelIndex); final int chanelValue = getChanelValue(image, widthIndex, heightIndex, chanProp); final int[] tensorCoordinates = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int coordinateValue; switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordinateValue = imageIndex; break; case HEIGHT: coordinateValue = heightIndex; break; case WIDTH: coordinateValue = widthIndex; break; case CHANEL: coordinateValue = chanelIndex; break; default: throw new IllegalStateException("Unknown TensorImageProperties"); } tensorCoordinates[i] = coordinateValue; } tensor.setOnCoord(chanelValue, tensorCoordinates); } } } } return tensor; } }
ImagesIntoTensor implements Builder<List<BufferedImage>, Tensor> { @Override public Tensor build(List<BufferedImage> inputsImages) { final int batchSize = inputsImages.size(); List<BufferedImage> imagesList = inputsImages .stream() .map(x -> { int currentHeight = x.getHeight(); int currentWidth = x.getWidth(); boolean shouldResizeHeight = this.expectedHeigth != -1 && currentHeight != this.expectedHeigth; boolean shouldResizeWidth = this.expectedWidth != -1 && currentWidth != this.expectedWidth; if (shouldResizeHeight || shouldResizeWidth) { Image scaledImg = x.getScaledInstance(this.expectedWidth, this.expectedHeigth, Image.SCALE_SMOOTH); BufferedImage output = new BufferedImage(this.expectedWidth, this.expectedHeigth, x.getType()); Graphics2D bGr = output.createGraphics(); bGr.drawImage(scaledImg, 0, 0, null); bGr.dispose(); return output; } else { return x; } }) .collect(Collectors.toList()); final Set<Integer> allWidth = imagesList .stream() .map(BufferedImage::getWidth) .collect(Collectors.toSet()); if (allWidth.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same width. Found several : %s", allWidth.toArray() ) ); } final int width = allWidth.iterator().next(); final Set<Integer> allHeight = imagesList .stream() .map(BufferedImage::getHeight) .collect(Collectors.toSet()); if (allHeight.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same height. Found several : %s", allHeight.toArray() ) ); } final int height = allHeight.iterator().next(); final int chanelSize = chanelProperties.size(); final int[] tensorShapeArray = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int dimensionSize; switch (shapeAttributes.get(i)) { case BATCH_SIZE: dimensionSize = batchSize; break; case HEIGHT: dimensionSize = height; break; case WIDTH: dimensionSize = width; break; case CHANEL: dimensionSize = chanelSize; break; default: throw new IllegalStateException( String.format("Unknown chanel property : %s", shapeAttributes.get(i).toString())); } tensorShapeArray[i] = dimensionSize; } final Tensor tensor = new Tensor(DataType.INTEGER, new TensorShape(tensorShapeArray)); for (int imageIndex = 0; imageIndex < imagesList.size(); imageIndex++) { BufferedImage image = imagesList.get(imageIndex); for (int widthIndex = 0; widthIndex < image.getWidth(); widthIndex++) { for (int heightIndex = 0; heightIndex < image.getHeight(); heightIndex++) { for (int chanelIndex = 0; chanelIndex < chanelProperties.size(); chanelIndex++) { final ImgChanelProperties chanProp = chanelProperties.get(chanelIndex); final int chanelValue = getChanelValue(image, widthIndex, heightIndex, chanProp); final int[] tensorCoordinates = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int coordinateValue; switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordinateValue = imageIndex; break; case HEIGHT: coordinateValue = heightIndex; break; case WIDTH: coordinateValue = widthIndex; break; case CHANEL: coordinateValue = chanelIndex; break; default: throw new IllegalStateException("Unknown TensorImageProperties"); } tensorCoordinates[i] = coordinateValue; } tensor.setOnCoord(chanelValue, tensorCoordinates); } } } } return tensor; } ImagesIntoTensor(ImageTransformerInfo transformerInfo); }
ImagesIntoTensor implements Builder<List<BufferedImage>, Tensor> { @Override public Tensor build(List<BufferedImage> inputsImages) { final int batchSize = inputsImages.size(); List<BufferedImage> imagesList = inputsImages .stream() .map(x -> { int currentHeight = x.getHeight(); int currentWidth = x.getWidth(); boolean shouldResizeHeight = this.expectedHeigth != -1 && currentHeight != this.expectedHeigth; boolean shouldResizeWidth = this.expectedWidth != -1 && currentWidth != this.expectedWidth; if (shouldResizeHeight || shouldResizeWidth) { Image scaledImg = x.getScaledInstance(this.expectedWidth, this.expectedHeigth, Image.SCALE_SMOOTH); BufferedImage output = new BufferedImage(this.expectedWidth, this.expectedHeigth, x.getType()); Graphics2D bGr = output.createGraphics(); bGr.drawImage(scaledImg, 0, 0, null); bGr.dispose(); return output; } else { return x; } }) .collect(Collectors.toList()); final Set<Integer> allWidth = imagesList .stream() .map(BufferedImage::getWidth) .collect(Collectors.toSet()); if (allWidth.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same width. Found several : %s", allWidth.toArray() ) ); } final int width = allWidth.iterator().next(); final Set<Integer> allHeight = imagesList .stream() .map(BufferedImage::getHeight) .collect(Collectors.toSet()); if (allHeight.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same height. Found several : %s", allHeight.toArray() ) ); } final int height = allHeight.iterator().next(); final int chanelSize = chanelProperties.size(); final int[] tensorShapeArray = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int dimensionSize; switch (shapeAttributes.get(i)) { case BATCH_SIZE: dimensionSize = batchSize; break; case HEIGHT: dimensionSize = height; break; case WIDTH: dimensionSize = width; break; case CHANEL: dimensionSize = chanelSize; break; default: throw new IllegalStateException( String.format("Unknown chanel property : %s", shapeAttributes.get(i).toString())); } tensorShapeArray[i] = dimensionSize; } final Tensor tensor = new Tensor(DataType.INTEGER, new TensorShape(tensorShapeArray)); for (int imageIndex = 0; imageIndex < imagesList.size(); imageIndex++) { BufferedImage image = imagesList.get(imageIndex); for (int widthIndex = 0; widthIndex < image.getWidth(); widthIndex++) { for (int heightIndex = 0; heightIndex < image.getHeight(); heightIndex++) { for (int chanelIndex = 0; chanelIndex < chanelProperties.size(); chanelIndex++) { final ImgChanelProperties chanProp = chanelProperties.get(chanelIndex); final int chanelValue = getChanelValue(image, widthIndex, heightIndex, chanProp); final int[] tensorCoordinates = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int coordinateValue; switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordinateValue = imageIndex; break; case HEIGHT: coordinateValue = heightIndex; break; case WIDTH: coordinateValue = widthIndex; break; case CHANEL: coordinateValue = chanelIndex; break; default: throw new IllegalStateException("Unknown TensorImageProperties"); } tensorCoordinates[i] = coordinateValue; } tensor.setOnCoord(chanelValue, tensorCoordinates); } } } } return tensor; } ImagesIntoTensor(ImageTransformerInfo transformerInfo); @Override Tensor build(List<BufferedImage> inputsImages); }
ImagesIntoTensor implements Builder<List<BufferedImage>, Tensor> { @Override public Tensor build(List<BufferedImage> inputsImages) { final int batchSize = inputsImages.size(); List<BufferedImage> imagesList = inputsImages .stream() .map(x -> { int currentHeight = x.getHeight(); int currentWidth = x.getWidth(); boolean shouldResizeHeight = this.expectedHeigth != -1 && currentHeight != this.expectedHeigth; boolean shouldResizeWidth = this.expectedWidth != -1 && currentWidth != this.expectedWidth; if (shouldResizeHeight || shouldResizeWidth) { Image scaledImg = x.getScaledInstance(this.expectedWidth, this.expectedHeigth, Image.SCALE_SMOOTH); BufferedImage output = new BufferedImage(this.expectedWidth, this.expectedHeigth, x.getType()); Graphics2D bGr = output.createGraphics(); bGr.drawImage(scaledImg, 0, 0, null); bGr.dispose(); return output; } else { return x; } }) .collect(Collectors.toList()); final Set<Integer> allWidth = imagesList .stream() .map(BufferedImage::getWidth) .collect(Collectors.toSet()); if (allWidth.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same width. Found several : %s", allWidth.toArray() ) ); } final int width = allWidth.iterator().next(); final Set<Integer> allHeight = imagesList .stream() .map(BufferedImage::getHeight) .collect(Collectors.toSet()); if (allHeight.size() != 1) { throw new EvaluationException( String.format( "To merge several images as a tensor, all images should have the same height. Found several : %s", allHeight.toArray() ) ); } final int height = allHeight.iterator().next(); final int chanelSize = chanelProperties.size(); final int[] tensorShapeArray = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int dimensionSize; switch (shapeAttributes.get(i)) { case BATCH_SIZE: dimensionSize = batchSize; break; case HEIGHT: dimensionSize = height; break; case WIDTH: dimensionSize = width; break; case CHANEL: dimensionSize = chanelSize; break; default: throw new IllegalStateException( String.format("Unknown chanel property : %s", shapeAttributes.get(i).toString())); } tensorShapeArray[i] = dimensionSize; } final Tensor tensor = new Tensor(DataType.INTEGER, new TensorShape(tensorShapeArray)); for (int imageIndex = 0; imageIndex < imagesList.size(); imageIndex++) { BufferedImage image = imagesList.get(imageIndex); for (int widthIndex = 0; widthIndex < image.getWidth(); widthIndex++) { for (int heightIndex = 0; heightIndex < image.getHeight(); heightIndex++) { for (int chanelIndex = 0; chanelIndex < chanelProperties.size(); chanelIndex++) { final ImgChanelProperties chanProp = chanelProperties.get(chanelIndex); final int chanelValue = getChanelValue(image, widthIndex, heightIndex, chanProp); final int[] tensorCoordinates = new int[shapeAttributes.size()]; for (int i = 0; i < shapeAttributes.size(); i++) { final int coordinateValue; switch (shapeAttributes.get(i)) { case BATCH_SIZE: coordinateValue = imageIndex; break; case HEIGHT: coordinateValue = heightIndex; break; case WIDTH: coordinateValue = widthIndex; break; case CHANEL: coordinateValue = chanelIndex; break; default: throw new IllegalStateException("Unknown TensorImageProperties"); } tensorCoordinates[i] = coordinateValue; } tensor.setOnCoord(chanelValue, tensorCoordinates); } } } } return tensor; } ImagesIntoTensor(ImageTransformerInfo transformerInfo); @Override Tensor build(List<BufferedImage> inputsImages);  }