<jupyter_start><jupyter_text>303 - Transfer Learning by DNN FeaturizationClassify automobile vs airplane using DNN featurization and transfer learningagainst a subset of images from CIFAR-10 dataset. Load DNN Model and pick one of the inner layers as feature output<jupyter_code>from mmlspark.cntk import CNTKModel
from mmlspark.downloader import ModelDownloader
import numpy as np, os, urllib, tarfile, pickle, array
from os.path import abspath
from pyspark.sql.functions import col, udf
from pyspark.sql.types import *
modelName = "ConvNet"
modelDir = "file:" + abspath("models")
d = ModelDownloader(spark, modelDir)
model = d.downloadByName(modelName)
print(model.layerNames)
cntkModel = CNTKModel().setInputCol("images").setOutputCol("features") \
                       .setModelLocation(model.uri).setOutputNode("l8")<jupyter_output><empty_output><jupyter_text>Format raw CIFAR data into correct shape.<jupyter_code>imagesWithLabels = spark.read.parquet("wasbs://publicwasb@m<EMAIL>.blob.core.windows.net/CIFAR10_test.parquet")<jupyter_output><empty_output><jupyter_text>Select airplanes (label=0) and automobiles (label=1)<jupyter_code>imagesWithLabels = imagesWithLabels.filter("labels<2")
imagesWithLabels.cache()<jupyter_output><empty_output><jupyter_text>Featurize images<jupyter_code>featurizedImages = cntkModel.transform(imagesWithLabels).select(["features","labels"])<jupyter_output><empty_output><jupyter_text>Use featurized images to train a classifier<jupyter_code>from mmlspark.train import TrainClassifier
from pyspark.ml.classification import RandomForestClassifier

train,test = featurizedImages.randomSplit([0.75,0.25])

model = TrainClassifier(model=RandomForestClassifier(),labelCol="labels").fit(train)<jupyter_output><empty_output><jupyter_text>Evaluate the accuracy of the model<jupyter_code>from mmlspark.train import ComputeModelStatistics
predictions = model.transform(test)
metrics = ComputeModelStatistics(evaluationMetric="accuracy").transform(predictions)
metrics.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10122850122850123, 'fraction_numerical': 0.008353808353808353, 'mean_word_length': 8.788461538461538, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '224718', 'license': 'MIT', 'n_tokens_mistral': 659, 'n_tokens_neox': 633, 'n_words': 123, 'path': 'notebooks/samples/DeepLearning - Transfer Learning.ipynb', 'repo_name': 'diego-mazon/mmlspark', 'size': '2035'}