databricks / spark-deep-learning

Deep Learning Pipelines for Apache Spark
https://databricks.github.io/spark-deep-learning
Apache License 2.0
1.99k stars 494 forks source link

registerKerasImageUDF example not working - graph placeholder nChaneels was not given a corresponding dataframe name #146

Closed dimagoldin closed 6 years ago

dimagoldin commented 6 years ago

I am able to create and train and evaluate a custom keras model based on InceptionV3 outside of spark. When using the model as udf (after registering it with registerKerasImageUDF) I am getting the following error:

py4j.protocol.Py4JJavaError: An error occurred while calling o96.selectExpr.
: java.lang.Exception: The graph placeholder nChannels was not given a corresponding dataframe field name as input:hints: Map()

It led me to test the examples in the README and i am getting the same error.

I am doing the following:

sparkSession = SparkSession.builder \
    .master("local[*]") \
    .appName("KerasModel") \
    .getOrCreate()
sparkSession.sparkContext.setLogLevel("WARN")

from keras.applications import InceptionV3
from sparkdl.udf.keras_image_model import registerKerasImageUDF

registerKerasImageUDF("inceptionV3_udf", InceptionV3(weights="imagenet"))

imagesDf = ImageSchema.readImages("/path/to/jpgs")
imagesDf.selectExpr("inceptionV3_udf(image) as predictions").show()

also tested with preprocessing:

sparkSession = SparkSession.builder \
    .master("local[*]") \
    .appName("KerasModel") \
    .getOrCreate()
sparkSession.sparkContext.setLogLevel("WARN")

from keras.applications import InceptionV3
from sparkdl.udf.keras_image_model import registerKerasImageUDF

def keras_load_img(fpath):
    from keras.preprocessing.image import load_img, img_to_array
    import numpy as np
    img = load_img(fpath, target_size=(299, 299))
    return img_to_array(img).astype(np.uint8)

registerKerasImageUDF("inceptionV3_udf", InceptionV3(weights="imagenet"), keras_load_img)

schema = StructType([
    StructField("uri", StringType()),
    StructField("label", IntegerType())
])

imagesDf = sparkSession.read.csv("/path/to/csv/with/image/uris",schema=schema)
imagesDf.selectExpr("inceptionV3_udf(uri) as predictions").show()

Both versions result in the following error:

  File "/opt/spark/python/lib/pyspark.zip/pyspark/sql/dataframe.py", line 1216, in selectExpr
  File "/opt/spark/python/lib/py4j-0.10.7-src.zip/py4j/java_gateway.py", line 1257, in __call__
  File "/opt/spark/python/lib/pyspark.zip/pyspark/sql/utils.py", line 63, in deco
  File "/opt/spark/python/lib/py4j-0.10.7-src.zip/py4j/protocol.py", line 328, in get_return_value
py4j.protocol.Py4JJavaError: An error occurred while calling o96.selectExpr.
: java.lang.Exception: The graph placeholder nChannels was not given a corresponding dataframe field name as input:hints: Map()
    at org.tensorframes.impl.SchemaTransforms$$anonfun$get$1.apply(DebugRowOps.scala:55)
    at org.tensorframes.impl.SchemaTransforms$$anonfun$get$1.apply(DebugRowOps.scala:55)
    at scala.Option.getOrElse(Option.scala:121)
    at org.tensorframes.impl.SchemaTransforms$class.get(DebugRowOps.scala:54)
    at org.tensorframes.impl.SchemaTransforms$.get(DebugRowOps.scala:275)

I am using spark version 2.3.1 tried with both version 1.1.0 and 1.0.0 of spark deep learning

OS ubuntu 16.04 Python 3.6.5

Any ideas?

dimagoldin commented 6 years ago

ok i found most of the problems

Tensorflow-gpu (or normal tensorflow) version 1.6.0 must be installed and not the newer versions. if using cuda9 than libcudden7.0.5 must be installed and not higher version (even 7.1.5 wont do)

most of the problems should go away