Java 类名:com.alibaba.alink.pipeline.image.WriteTensorToImage
Python 类名:WriteTensorToImage

功能介绍

将张量列转换为图片,并写入根目录对应的相对路径列中,然后原样输出结果。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
relativeFilePathCol 文件路径列 文件路径列 String
rootFilePath 文件路径 文件路径 String
tensorCol tensor列 tensor列 String
imageType 图片类型 图片类型 String “PNG”, “JPEG” “PNG”
reservedCols 算法保留列名 算法保留列 String[] null

代码示例

Python 代码

  1. df_data = pd.DataFrame([
  2. 'sphx_glr_plot_scripted_tensor_transforms_001.png'
  3. ])
  4. batch_data = BatchOperator.fromDataframe(df_data, schemaStr = 'path string')
  5. readImageToTensorBatchOp = ReadImageToTensorBatchOp()\
  6. .setRootFilePath("https://pytorch.org/vision/stable/_images/")\
  7. .setRelativeFilePathCol("path")\
  8. .setOutputCol("tensor")
  9. writeTensorToImageBatchOp = WriteTensorToImageBatchOp()\
  10. .setRootFilePath("/tmp/write_tensor_to_image")\
  11. .setTensorCol("tensor")\
  12. .setImageType("png")\
  13. .setRelativeFilePathCol("path")
  14. batch_data.link(readImageToTensorBatchOp).link(writeTensorToImageBatchOp).print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  3. import com.alibaba.alink.params.image.HasImageType.ImageType;
  4. import com.alibaba.alink.pipeline.image.WriteTensorToImage;
  5. import org.junit.Test;
  6. import java.util.Collections;
  7. import java.util.List;
  8. public class WriteTensorToImageTest {
  9. @Test
  10. public void testWriteTensorToImage() throws Exception {
  11. List <Row> data = Collections.singletonList(
  12. Row.of("sphx_glr_plot_scripted_tensor_transforms_001.png")
  13. );
  14. MemSourceBatchOp memSourceBatchOp = new MemSourceBatchOp(data, "path string");
  15. ReadImageToTensorBatchOp readImageToTensorBatchOp = new ReadImageToTensorBatchOp()
  16. .setRootFilePath("https://pytorch.org/vision/stable/_images/")
  17. .setRelativeFilePathCol("path")
  18. .setOutputCol("tensor");
  19. WriteTensorToImage writeTensorToImageBatchOp = new WriteTensorToImage()
  20. .setRootFilePath("/tmp/write_tensor_to_image")
  21. .setTensorCol("tensor")
  22. .setImageType(ImageType.PNG)
  23. .setRelativeFilePathCol("path");
  24. writeTensorToImageBatchOp.transform(memSourceBatchOp.link(readImageToTensorBatchOp)).print();
  25. }
  26. }

运行结果

可以在 /tmp/write_tensor_to_image/sphx_glr_plot_scripted_tensor_transforms_001.png 中找到 https://pytorch.org/vision/stable/_images/sphx_glr_plot_scripted_tensor_transforms_001.png
同时组件的输出结果为:
| path | tensor |
|—————————————————————————+————————————————|
| sphx_glr_plot_scripted_tensor_transforms_001.png | FLOAT#250,520,4#255.0 255.0… |