This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.applications.inception_v3 import preprocess_input | |
from keras.preprocessing.image import img_to_array, load_img | |
import numpy as np | |
from pyspark.sql.types import StringType | |
from sparkdl import KerasImageFileTransformer | |
def loadAndPreprocessKerasInceptionV3(uri): | |
# this is a typical way to load and prep images in keras | |
image = img_to_array(load_img(uri, target_size=(299, 299))) # image dimensions for InceptionV3 | |
image = np.expand_dims(image, axis=0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = !ls flower_photos/sample/*.jpg | |
uri_df = spark.createDataFrame(fs, StringType()).toDF("uri") | |
keras_pred_df = transformer.transform(uri_df) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sparkdl import KerasTransformer | |
from keras.models import Sequential | |
from keras.layers import Dense | |
import numpy as np | |
# Generate random input data | |
num_features = 10 | |
num_examples = 100 | |
input_data = [{"features" : np.random.randn(num_features).astype(float).tolist()} for i in range(num_examples)] | |
schema = StructType([ StructField("features", ArrayType(FloatType()), True)]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.applications import InceptionV3 | |
from sparkdl.udf.keras_image_model import registerKerasImageUDF | |
registerKerasImageUDF("inceptionV3_udf", InceptionV3(weights="imagenet")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyspark.ml.image import ImageSchema | |
image_df = ImageSchema.readImages("flower_photos/sample/") | |
image_df.registerTempTable("sample_images") | |
df = spark.sql("SELECT inceptionV3_udf_with_preprocessing(image) as predictions from sample_images").show(truncate=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_with_preprocessing", InceptionV3(weights="imagenet"), keras_load_img) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def softmax(z): | |
z -= np.max(z) | |
sm = (np.exp(z).T / np.sum(np.exp(z), axis=1)) | |
return sm | |
def initialize(dim1, dim2): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def softmax(z): | |
z -= np.max(z) | |
sm = (np.exp(z).T / np.sum(np.exp(z),axis=1)) | |
return sm | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def softmax(z): | |
cache = z | |
z -= np.max(z) | |
sm = (np.exp(z).T / np.sum(np.exp(z), axis=1)) | |
return sm, cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''README, Author - Anurag Kumar(mailto:[email protected]) | |
Requirements: | |
- sklearn | |
- numpy | |
- matplotlib | |
Python: | |
- 3.5 |