|
| 1 | +// Copyright 2016 Yahoo Inc. |
| 2 | +// Licensed under the terms of the Apache 2.0 license. |
| 3 | +// Please see LICENSE file in the project root for terms. |
| 4 | +package com.yahoo.ml.caffe |
| 5 | + |
| 6 | +import org.apache.spark.SparkContext |
| 7 | +import org.apache.spark.rdd.RDD |
| 8 | +import org.apache.spark.sql.{DataFrame, SQLContext} |
| 9 | +import org.apache.spark.storage.StorageLevel |
| 10 | + |
| 11 | +/** |
| 12 | + * ImageDataFrame is a built-in data source class using Spark dataframe format. |
| 13 | + * |
| 14 | + * ImageDataFrame expects dataframe with 2 required columns (lable:String, data:byte[]), |
| 15 | + * and 5 optional columns (id: String, channels :Int, height:Int, width:Int, encoded: Boolean). |
| 16 | + * |
| 17 | + * ImageDataFrame could be configured via 2 MemoryDataLayer parameter: |
| 18 | + * (1) dataframe_column_select ... a collection of dataframe SQL selection statements |
| 19 | + * (ex. "sampleId as id", "abs(height) as height") |
| 20 | + * (2) image_ecnoded ... indicate whether image data are encoded or not. (default: false) |
| 21 | + * |
| 22 | + * @param conf CaffeSpark configuration |
| 23 | + * @param layerId the layer index in the network protocol file |
| 24 | + * @param isTrain |
| 25 | + */ |
| 26 | +class ImageDataFrame(conf: Config, layerId: Int, isTrain: Boolean) |
| 27 | + extends ImageDataSource(conf, layerId, isTrain) { |
| 28 | + |
| 29 | + /* construct a sample RDD */ |
| 30 | + def makeRDD(sc: SparkContext): RDD[(String, String, Int, Int, Int, Boolean, Array[Byte])] = { |
| 31 | + val sqlContext = new SQLContext(sc) |
| 32 | + //load DataFrame |
| 33 | + var df: DataFrame = sqlContext.read.format(conf.inputFormat).load(sourceFilePath) |
| 34 | + |
| 35 | + //select columns if specified |
| 36 | + if (memdatalayer_param.getDataframeColumnSelectCount() > 0) { |
| 37 | + val selects = memdatalayer_param.getDataframeColumnSelectList() |
| 38 | + |
| 39 | + import scala.collection.JavaConversions._ |
| 40 | + df = df.selectExpr(selects.toList:_*) |
| 41 | + } |
| 42 | + |
| 43 | + //check optional columns |
| 44 | + val column_names : Array[String] = df.columns |
| 45 | + val has_id : Boolean = column_names.contains("id") |
| 46 | + val has_channels : Boolean = column_names.contains("channels") |
| 47 | + val has_height : Boolean = column_names.contains("height") |
| 48 | + val has_width : Boolean = column_names.contains("width") |
| 49 | + val has_encoded : Boolean = column_names.contains("encoded") |
| 50 | + |
| 51 | + //mapping each row to RDD tuple |
| 52 | + df.map(row => { |
| 53 | + var id: String = if (!has_id) "" else row.getAs[String]("id") |
| 54 | + var label: String = row.getAs[String]("label") |
| 55 | + val channels : Int = if (!has_channels) 0 else row.getAs[Int]("channels") |
| 56 | + val height : Int = if (!has_height) 0 else row.getAs[Int]("height") |
| 57 | + val width : Int = if (!has_width) 0 else row.getAs[Int]("width") |
| 58 | + val encoded : Boolean = if (!has_encoded) memdatalayer_param.getImageEncoded() else row.getAs[Boolean]("encoded") |
| 59 | + val data : Array[Byte] = row.getAs[Any]("data") match { |
| 60 | + case str: String => str.getBytes |
| 61 | + case arr: Array[Byte@unchecked] => arr |
| 62 | + case _ => { |
| 63 | + log.error("Unsupport value type") |
| 64 | + null |
| 65 | + } |
| 66 | + } |
| 67 | + (id, label, channels, height, width, encoded, data) |
| 68 | + }).persist(StorageLevel.DISK_ONLY) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * ImageDataFrame using Parquet data frame format |
| 74 | + * |
| 75 | + * @param conf CaffeSpark configuration |
| 76 | + * @param layerId the layer index in the network protocol file |
| 77 | + * @param isTrain |
| 78 | + */ |
| 79 | +class ImageDataFrameParquet(conf: Config, layerId: Int, isTrain: Boolean) |
| 80 | + extends ImageDataFrame(conf, layerId, isTrain) { |
| 81 | + conf.inputFormat = "parquet" |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * ImageDataFrame using Json data frame format |
| 86 | + * |
| 87 | + * @param conf CaffeSpark configuration |
| 88 | + * @param layerId the layer index in the network protocol file |
| 89 | + * @param isTrain |
| 90 | + */ |
| 91 | +class ImageDataFrameJson(conf: Config, layerId: Int, isTrain: Boolean) |
| 92 | + extends ImageDataFrame(conf, layerId, isTrain) { |
| 93 | + conf.inputFormat = "json" |
| 94 | +} |
0 commit comments