1. 程式人生 > >Spark-2.4 Deep Learning Pipelines Image Claasifer Demo

Spark-2.4 Deep Learning Pipelines Image Claasifer Demo

(原文連結)-這是Spark2018 Submit 的一個演講Demo, 針對Keras圖片分類和使用Spark做分類的方法做了講解,供學習使用。文章排版不好,最好看原文。

databricks-logo

tf_keras_dlp_sai_demo_image_classifier(Python)

Part 1: Exploring and Classifying Images with Pretrained Models

We will use Keras with TensorFlow as the backend, and download VGG16 from Keras.

VGG16

from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions, VGG16
import numpy as np
import os

vgg16Model = VGG16(weights='imagenet')

/databricks/python/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters Using TensorFlow backend. Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5 8192/553467096 [..............................] - ETA: 1:25:37 24576/553467096 [..............................] - ETA: 57:10 40960/553467096 [..............................] - ETA: 51:31 73728/553467096 [..............................] - ETA: 38:11 90112/553467096 [..............................] - ETA: 39:03 122880/553467096 [..............................] - ETA: 34:22 163840/553467096 [..............................] - ETA: 30:05 212992/553467096 [..............................] - ETA: 26:26 262144/553467096 [..............................] - ETA: 24:10 319488/553467096 [..............................] - ETA: 22:02 401408/553467096 [..............................] - ETA: 19:17 475136/553467096 [..............................] - ETA: 17:46 581632/553467096 [..............................] - ETA: 15:43 679936/553467096 [..............................] - ETA: 14:29 802816/553467096 [..............................] - ETA: 13:08 942080/553467096 [..............................] - ETA: 11:56 1097728/553467096 [..............................] - ETA: 10:53 1277952/553467096 [..............................] - ETA: 9:54 1433600/553467096 [..............................] - ETA: 9:18 1622016/553467096 [..............................] - ETA: 8:39 1810432/553467096 [..............................] - ETA: 8:08 2023424/553467096 [..............................] - ETA: 7:38 2252800/553467096 [..............................] - ETA: 7:09 2490368/553467096 [..............................] - ETA: 6:45 2809856/553467096 [..............................] - ETA: 6:14 3137536/553467096 [..............................] - ETA: 5:48 3538944/553467096 [..............................] - ETA: 5:20 3989504/553467096 [..............................] - ETA: 4:54 4546560/553467096 [..............................] - ETA: 4:27 5152768/553467096 [..............................] - ETA: 4:03 5890048/553467096 [..............................] - ETA: 3:40 6692864/553467096 [..............................] - ETA: 3:19 7610368/553467096 [..............................] - ETA: 3:00 8642560/553467096 [..............................] - ETA: 2:43 9756672/553467096 [..............................] - ETA: 2:29 11067392/553467096 [..............................] - ETA: 2:14 12476416/553467096 [..............................] - ETA: 2:02 13983744/553467096 [..............................] - ETA: 1:51 15556608/553467096 [..............................] - ETA: 1:43 17129472/553467096 [..............................] - ETA: 1:35 18702336/553467096 [>.............................] - ETA: 1:29 20127744/553467096 [>.............................] - ETA: 1:25 21127168/553467096 [>.............................] - ETA: 1:22 21864448/553467096 [>.............................] - ETA: 1:21 22781952/553467096 [>.............................] - ETA: 1:20 23912448/553467096 [>.............................] - ETA: 1:17 25075712/553467096 [>.............................] - ETA: 1:15 26255360/553467096 [>.............................] - ETA: 1:13 27451392/553467096 [>.............................] - ETA: 1:11 28647424/553467096 [>.............................] - ETA: 1:09 29876224/553467096 [>.............................] - ETA: 1:08 31105024/553467096 [>.............................] - ETA: 1:06 32333824/553467096 [>.............................] - ETA: 1:05 33579008/553467096 [>.............................] - ETA: 1:03 34840576/553467096 [>.............................] - ETA: 1:02 36102144/553467096 [>.............................] - ETA: 1:01 37380096/553467096 [=>............................] - ETA: 1:00 38674432/553467096 [=>............................] - ETA: 58s 39968768/553467096 [=>............................] - ETA: 57s 41263104/553467096 [=>............................] - ETA: 56s 42590208/553467096 [=>............................] - ETA: 55s 43900928/553467096 [=>............................] - ETA: 54s 45228032/553467096 [=>............................] - ETA: 54s 46555136/553467096 [=>............................] - ETA: 53s 47915008/553467096 [=>............................] - ETA: 52s 49258496/553467096 [=>............................] - ETA: 51s 50618368/553467096 [=>............................] - ETA: 50s 51978240/553467096 [=>............................] - ETA: 50s 53354496/553467096 [=>............................] - ETA: 49s 54747136/553467096 [=>............................] - ETA: 48s 56123392/553467096 [==>...........................] - ETA: 48s 57516032/553467096 [==>...........................] - ETA: 47s 58925056/553467096 [==>...........................] - ETA: 46s 60334080/553467096 [==>...........................] - ETA: 46s 61743104/553467096 [==>...........................] - ETA: 45s 63168512/553467096 [==>...........................] - ETA: 45s 64593920/553467096 [==>...........................] - ETA: 44s 66019328/553467096 [==>...........................] - ETA: 44s 67461120/553467096 [==>...........................] - ETA: 43s 68902912/553467096 [==>...........................] - ETA: 43s 70361088/553467096 [==>...........................] - ETA: 42s 71819264/553467096 [==>...........................] - ETA: 42s 73277440/553467096 [==>...........................] - ETA: 41s 74735616/553467096 [===>..........................] - ETA: 41s 76210176/553467096 [===>..........................] - ETA: 40s 77684736/553467096 [===>..........................] - ETA: 40s 79159296/553467096 [===>..........................] - ETA: 39s 80650240/553467096 [===>..........................] - ETA: 39s 82141184/553467096 [===>..........................] - ETA: 39s 83632128/553467096 [===>..........................] - ETA: 38s 85139456/553467096 [===>..........................] - ETA: 38s 86630400/553467096 [===>..........................] - ETA: 37s 88137728/553467096 [===>..........................] - ETA: 37s 89661440/553467096 [===>..........................] - ETA: 37s 91168768/553467096 [===>..........................] - ETA: 36s 92692480/553467096 [====>.........................] - ETA: 36s 94216192/553467096 [====>.........................] - ETA: 36s 95739904/553467096 [====>.........................] - ETA: 35s 97263616/553467096 [====>.........................] - ETA: 35s 98803712/553467096 [====>.........................] - ETA: 35s 100343808/553467096 [====>.........................] - ETA: 34s 101883904/553467096 [====>.........................] - ETA: 34s 103424000/553467096 [====>.........................] - ETA: 34s 104964096/553467096 [====>.........................] - ETA: 33s 106520576/553467096 [====>.........................] - ETA: 33s 108060672/553467096 [====>.........................] - ETA: 33s 109617152/553467096 [====>.........................] - ETA: 33s 111173632/553467096 [=====>........................] - ETA: 32s 112730112/553467096 [=====>........................] - ETA: 32s 114302976/553467096 [=====>........................] - ETA: 32s 115859456/553467096 [=====>........................] - ETA: 32s 117432320/553467096 [=====>........................] - ETA: 31s 119005184/553467096 [=====>........................] - ETA: 31s 120561664/553467096 [=====>........................] - ETA: 31s 122134528/553467096 [=====>........................] - ETA: 31s 123707392/553467096 [=====>........................] - ETA: 30s 125280256/553467096 [=====>........................] - ETA: 30s 126853120/553467096 [=====>........................] - ETA: 30s 128425984/553467096 [=====>........................] - ETA: 30s 129998848/553467096 [======>.......................] - ETA: 29s 131571712/553467096 [======>.......................] - ETA: 29s 133144576/553467096 [======>.......................] - ETA: 29s 134717440/553467096 [======>.......................] - ETA: 29s 136290304/553467096 [======>.......................] - ETA: 29s 137863168/553467096 [======>.......................] - ETA: 28s 139436032/553467096 [======>.......................] - ETA: 28s 141008896/553467096 [======>.......................] - ETA: 28s 142581760/553467096 [======>.......................] - ETA: 28s 144154624/553467096 [======>.......................] - ETA: 27s 145727488/553467096 [======>.......................] - ETA: 27s 147300352/553467096 [======>.......................] - ETA: 27s 148873216/553467096 [=======>......................] - ETA: 27s 150429696/553467096 [=======>......................] - ETA: 27s 152002560/553467096 [=======>......................] - ETA: 27s 153575424/553467096 [=======>......................] - ETA: 26s 155148288/553467096 [=======>......................] - ETA: 26s 156721152/553467096 [=======>......................] - ETA: 26s 158294016/553467096 [=======>......................] - ETA: 26s 159866880/553467096 [=======>......................] - ETA: 26s 161439744/553467096 [=======>......................] - ETA: 25s 163012608/553467096 [=======>......................] - ETA: 25s 164585472/553467096 [=======>......................] - ETA: 25s 165928960/553467096 [=======>......................] - ETA: 25s 167043072/553467096 [========>.....................] - ETA: 25s 168157184/553467096 [========>.....................] - ETA: 25s 169271296/553467096 [========>.....................] - ETA: 25s 170401792/553467096 [========>.....................] - ETA: 25s 171548672/553467096 [========>.....................] - ETA: 25s 172695552/553467096 [========>.....................] - ETA: 25s 173842432/553467096 [========>.....................] - ETA: 25s 175005696/553467096 [========>.....................] - ETA: 24s 176168960/553467096 [========>.....................] - ETA: 24s 177348608/553467096 [========>.....................] - ETA: 24s 178511872/553467096 [========>.....................] - ETA: 24s 179707904/553467096 [========>.....................] - ETA: 24s 180887552/553467096 [========>.....................] - ETA: 24s 182083584/553467096 [========>.....................] - ETA: 24s 183279616/553467096 [========>.....................] - ETA: 24s 184475648/553467096 [========>.....................] - ETA: 24s 185688064/553467096 [=========>....................] - ETA: 24s 186900480/553467096 [=========>....................] - ETA: 24s 188112896/553467096 [=========>....................] - ETA: 24s 189341696/553467096 [=========>....................] - ETA: 23s 190570496/553467096 [=========>....................] - ETA: 23s 191799296/553467096 [=========>....................] - ETA: 23s 193028096/553467096 [=========>....................] - ETA: 23s 194273280/553467096 [=========>....................] - ETA: 23s 195518464/553467096 [=========>....................] - ETA: 23s 196763648/553467096 [=========>....................] - ETA: 23s 198025216/553467096 [=========>....................] - ETA: 23s 199270400/553467096 [=========>....................] - ETA: 23s 200531968/553467096 [=========>....................] - ETA: 23s 201793536/553467096 [=========>....................] - ETA: 23s 203071488/553467096 [==========>...................] - ETA: 22s 204333056/553467096 [==========>...................] - ETA: 22s 205611008/553467096 [==========>...................] - ETA: 22s 206888960/553467096 [==========>...................] - ETA: 22s 208166912/553467096 [==========>...................] - ETA: 22s 209444864/553467096 [==========>...................] - ETA: 22s 210722816/553467096 [==========>...................] - ETA: 22s 212017152/553467096 [==========>...................] - ETA: 22s 213311488/553467096 [==========>...................] - ETA: 22s 214605824/553467096 [==========>...................] - ETA: 22s 215900160/553467096 [==========>...................] - ETA: 21s 217194496/553467096 [==========>...................] - ETA: 21s 218488832/553467096 [==========>...................] - ETA: 21s 219783168/553467096 [==========>...................] - ETA: 21s 221093888/553467096 [==========>...................] - ETA: 21s 222404608/553467096 [===========>..................] - ETA: 21s 223715328/553467096 [===========>..................] - ETA: 21s 225026048/553467096 [===========>..................] - ETA: 21s 226336768/553467096 [===========>..................] - ETA: 21s 227647488/553467096 [===========>..................] - ETA: 21s 228958208/553467096 [===========>..................] - ETA: 21s 230285312/553467096 [===========>..................] - ETA: 20s 231596032/553467096 [===========>..................] - ETA: 20s 232923136/553467096 [===========>..................] - ETA: 20s 234233856/553467096 [===========>..................] - ETA: 20s 235560960/553467096 [===========>..................] - ETA: 20s 236888064/553467096 [===========>..................] - ETA: 20s 238215168/553467096 [===========>..................] - ETA: 20s 239542272/553467096 [===========>..................] - ETA: 20s 240869376/553467096 [============>.................] - ETA: 20s 242196480/553467096 [============>.................] - ETA: 20s 243539968/553467096 [============>.................] - ETA: 19s 244867072/553467096 [============>.................] - ETA: 19s 246194176/553467096 [============>.................] - ETA: 19s 247537664/553467096 [============>.................] - ETA: 19s 248864768/553467096 [============>.................] - ETA: 19s 250191872/553467096 [============>.................] - ETA: 19s 251535360/553467096 [============>.................] - ETA: 19s 252862464/553467096 [============>.................] - ETA: 19s 254205952/553467096 [============>.................] - ETA: 19s 255533056/553467096 [============>.................] - ETA: 19s 256876544/553467096 [============>.................] - ETA: 18s 258220032/553467096 [============>.................] - ETA: 18s 259547136/553467096 [=============>................] - ETA: 18s 260890624/553467096 [=============>................] - ETA: 18s 262004736/553467096 [=============>................] - ETA: 18s 263151616/553467096 [=============>................] - ETA: 18s 264478720/553467096 [=============>................] - ETA: 18s 265822208/553467096 [=============>................] - ETA: 18s 267165696/553467096 [=============>................] - ETA: 18s 268509184/553467096 [=============>................] - ETA: 18s 269836288/553467096 [=============>................] - ETA: 18s 271179776/553467096 [=============>................] - ETA: 17s 272523264/553467096 [=============>................] - ETA: 17s 273866752/553467096 [=============>................] - ETA: 17s 275210240/553467096 [=============>................] - ETA: 17s 276553728/553467096 [=============>................] - ETA: 17s 277880832/553467096 [==============>...............] - ETA: 17s 279224320/553467096 [==============>...............] - ETA: 17s 280567808/553467096 [==============>...............] - ETA: 17s 281911296/553467096 [==============>...............] - ETA: 17s 283254784/553467096 [==============>...............] - ETA: 17s 284581888/553467096 [==============>...............] - ETA: 17s 285925376/553467096 [==============>...............] - ETA: 16s 287268864/553467096 [==============>...............] - ETA: 16s 288612352/553467096 [==============>...............] - ETA: 16s 289955840/553467096 [==============>...............] - ETA: 16s 291299328/553467096 [==============>...............] - ETA: 16s 292626432/553467096 [==============>...............] - ETA: 16s 293601280/553467096 [==============>...............] - ETA: 16s 294395904/553467096 [==============>...............] - ETA: 16s 295632896/553467096 [===============>..............] - ETA: 16s 296656896/553467096 [===============>..............] - ETA: 16s 298000384/553467096 [===============>..............] - ETA: 16s 299343872/553467096 [===============>..............] - ETA: 15s 300670976/553467096 [===============>..............] - ETA: 15s 302014464/553467096 [===============>..............] - ETA: 15s 303357952/553467096 [===============>..............] - ETA: 15s 304701440/553467096 [===============>..............] - ETA: 15s 306044928/553467096 [===============>..............] - ETA: 15s 307388416/553467096 [===============>..............] - ETA: 15s 308690944/553467096 [===============>..............] - ETA: 15s 309141504/553467096 [===============>..............] - ETA: 15s 310484992/553467096 [===============>..............] - ETA: 15s 311828480/553467096 [===============>..............] - ETA: 15s 313171968/553467096 [===============>..............] - ETA: 15s 314466304/553467096 [================>.............] - ETA: 14s 315432960/553467096 [================>.............] - ETA: 14s 316694528/553467096 [================>.............] - ETA: 14s 317186048/553467096 [================>.............] - ETA: 14s 318480384/553467096 [================>.............] - ETA: 14s 319447040/553467096 [================>.............] - ETA: 14s 320561152/553467096 [================>.............] - ETA: 14s 321216512/553467096 [================>.............] - ETA: 14s 322510848/553467096 [================>.............] - ETA: 14s 323477504/553467096 [================>.............] - ETA: 14s 324583424/553467096 [================>.............] - ETA: 14s 325246976/553467096 [================>.............] - ETA: 14s 326516736/553467096 [================>.............] - ETA: 14s 327524352/553467096 [================>.............] - ETA: 14s 328687616/553467096 [================>.............] - ETA: 14s 329293824/553467096 [================>.............] - ETA: 13s 330555392/553467096 [================>.............] - ETA: 13s 331554816/553467096 [================>.............] - ETA: 13s 332627968/553467096 [=================>............] - ETA: 13s 333340672/553467096 [=================>............] - ETA: 13s 334585856/553467096 [=================>............] - ETA: 13s 335601664/553467096 [=================>............] - ETA: 13s 336715776/553467096 [=================>............] - ETA: 13s 337387520/553467096 [=================>............] - ETA: 13s 338681856/553467096 [=================>............] - ETA: 13s 339648512/553467096 [=================>............] - ETA: 13s 340762624/553467096 [=================>............] - ETA: 13s 341450752/553467096 [=================>............] - ETA: 13s 342745088/553467096 [=================>............] - ETA: 13s 343728128/553467096 [=================>............] - ETA: 13s 345071616/553467096 [=================>............] - ETA: 12s 346349568/553467096 [=================>............] - ETA: 12s 346873856/553467096 [=================>............] - ETA: 12s 348233728/553467096 [=================>............] - ETA: 12s 349560832/553467096 [=================>............] - ETA: 12s 350527488/553467096 [=================>............] - ETA: 12s 351887360/553467096 [==================>...........] - ETA: 12s 353263616/553467096 [==================>...........] - ETA: 12s 354623488/553467096 [==================>...........] - ETA: 12s 355999744/553467096 [==================>...........] - ETA: 12s 357359616/553467096 [==================>...........] - ETA: 12s 358735872/553467096 [==================>...........] - ETA: 12s 360112128/553467096 [==================>...........] - ETA: 11s 361488384/553467096 [==================>...........] - ETA: 11s 362864640/553467096 [==================>...........] - ETA: 11s 364240896/553467096 [==================>...........] - ETA: 11s 365633536/553467096 [==================>...........] - ETA: 11s 367026176/553467096 [==================>...........] - ETA: 11s 368402432/553467096 [==================>...........] - ETA: 11s 369795072/553467096 [===================>..........] - ETA: 11s 371187712/553467096 [===================>..........] - ETA: 11s 372580352/553467096 [===================>..........] - ETA: 11s 373989376/553467096 [===================>..........] - ETA: 11s 375382016/553467096 [===================>..........] - ETA: 10s 376791040/553467096 [===================>..........] - ETA: 10s 378200064/553467096 [===================>..........] - ETA: 10s 379609088/553467096 [===================>..........] - ETA: 10s 381018112/553467096 [===================>..........] - ETA: 10s 382443520/553467096 [===================>..........] - ETA: 10s 383868928/553467096 [===================>..........] - ETA: 10s 385294336/553467096 [===================>..........] - ETA: 10s 386719744/553467096 [===================>..........] - ETA: 10s 388161536/553467096 [====================>.........] - ETA: 10s 389586944/553467096 [====================>.........] - ETA: 10s 391028736/553467096 [====================>.........] - ETA: 9s 392470528/553467096 [====================>.........] - ETA: 9s 393928704/553467096 [====================>.........] - ETA: 9s 395370496/553467096 [====================>.........] - ETA: 9s 396828672/553467096 [====================>.........] - ETA: 9s 398303232/553467096 [====================>.........] - ETA: 9s 399761408/553467096 [====================>.........] - ETA: 9s 401235968/553467096 [====================>.........] - ETA: 9s 402710528/553467096 [====================>.........] - ETA: 9s 404185088/553467096 [====================>.........] - ETA: 9s 405676032/553467096 [====================>.........] - ETA: 9s 407166976/553467096 [=====================>........] - ETA: 8s 408674304/553467096 [=====================>........] - ETA: 8s 410165248/553467096 [=====================>........] - ETA: 8s 411672576/553467096 [=====================>........] - ETA: 8s 413196288/553467096 [=====================>........] - ETA: 8s 414720000/553467096 [=====================>........] - ETA: 8s 416243712/553467096 [=====================>........] - ETA: 8s 417783808/553467096 [=====================>........] - ETA: 8s 419323904/553467096 [=====================>........] - ETA: 8s 420864000/553467096 [=====================>........] - ETA: 8s 422420480/553467096 [=====================>........] - ETA: 7s 423976960/553467096 [=====================>........] - ETA: 7s 425549824/553467096 [======================>.......] - ETA: 7s 427122688/553467096 [======================>.......] - ETA: 7s 428695552/553467096 [======================>.......] - ETA: 7s 430268416/553467096 [======================>.......] - ETA: 7s 431841280/553467096 [======================>.......] - ETA: 7s 433414144/553467096 [======================>.......] - ETA: 7s 434987008/553467096 [======================>.......] - ETA: 7s 436559872/553467096 [======================>.......] - ETA: 7s 438116352/553467096 [======================>.......] - ETA: 6s 439689216/553467096 [======================>.......] - ETA: 6s 441262080/553467096 [======================>.......] - ETA: 6s 442834944/553467096 [=======================>......] - ETA: 6s 444407808/553467096 [=======================>......] - ETA: 6s 445980672/553467096 [=======================>......] - ETA: 6s 447553536/553467096 [=======================>......] - ETA: 6s 449126400/553467096 [=======================>......] - ETA: 6s 450699264/553467096 [=======================>......] - ETA: 6s 452272128/553467096 [=======================>......] - ETA: 6s 453844992/553467096 [=======================>......] - ETA: 5s 455417856/553467096 [=======================>......] - ETA: 5s 456990720/553467096 [=======================>......] - ETA: 5s 458563584/553467096 [=======================>......] - ETA: 5s 460136448/553467096 [=======================>......] - ETA: 5s 461709312/553467096 [========================>.....] - ETA: 5s 463282176/553467096 [========================>.....] - ETA: 5s 464855040/553467096 [========================>.....] - ETA: 5s 466427904/553467096 [========================>.....] - ETA: 5s 468000768/553467096 [========================>.....] - ETA: 5s 469573632/553467096 [========================>.....] - ETA: 4s 471146496/553467096 [========================>.....] - ETA: 4s 472702976/553467096 [========================>.....] - ETA: 4s 474079232/553467096 [========================>.....] - ETA: 4s 474783744/553467096 [========================>.....] - ETA: 4s 476356608/553467096 [========================>.....] - ETA: 4s 477929472/553467096 [========================>.....] - ETA: 4s 479502336/553467096 [========================>.....] - ETA: 4s 481075200/553467096 [=========================>....] - ETA: 4s 482648064/553467096 [=========================>....] - ETA: 4s 484220928/553467096 [=========================>....] - ETA: 4s 485793792/553467096 [=========================>....] - ETA: 3s 487366656/553467096 [=========================>....] - ETA: 3s 488939520/553467096 [=========================>....] - ETA: 3s 490512384/553467096 [=========================>....] - ETA: 3s 492085248/553467096 [=========================>....] - ETA: 3s 493658112/553467096 [=========================>....] - ETA: 3s 495230976/553467096 [=========================>....] - ETA: 3s 496803840/553467096 [=========================>....] - ETA: 3s 498360320/553467096 [==========================>...] - ETA: 3s 499933184/553467096 [==========================>...] - ETA: 3s 501506048/553467096 [==========================>...] - ETA: 3s 503078912/553467096 [==========================>...] - ETA: 2s 504651776/553467096 [==========================>...] - ETA: 2s 506224640/553467096 [==========================>...] - ETA: 2s 507797504/553467096 [==========================>...] - ETA: 2s 509370368/553467096 [==========================>...] - ETA: 2s 510943232/553467096 [==========================>...] - ETA: 2s 512516096/553467096 [==========================>...] - ETA: 2s 514088960/553467096 [==========================>...] - ETA: 2s 515661824/553467096 [==========================>...] - ETA: 2s 517234688/553467096 [===========================>..] - ETA: 2s 518807552/553467096 [===========================>..] - ETA: 2s 520380416/553467096 [===========================>..] - ETA: 1s 521953280/553467096 [===========================>..] - ETA: 1s 523526144/553467096 [===========================>..] - ETA: 1s 525099008/553467096 [===========================>..] - ETA: 1s 526671872/553467096 [===========================>..] - ETA: 1s 528228352/553467096 [===========================>..] - ETA: 1s 529801216/553467096 [===========================>..] - ETA: 1s 531374080/553467096 [===========================>..] - ETA: 1s 532946944/553467096 [===========================>..] - ETA: 1s 534519808/553467096 [===========================>..] - ETA: 1s 536092672/553467096 [============================>.] - ETA: 1s 537665536/553467096 [============================>.] - ETA: 0s 539238400/553467096 [============================>.] - ETA: 0s 540811264/553467096 [============================>.] - ETA: 0s 542384128/553467096 [============================>.] - ETA: 0s 543956992/553467096 [============================>.] - ETA: 0s 545529856/553467096 [============================>.] - ETA: 0s 547102720/553467096 [============================>.] - ETA: 0s 548675584/553467096 [============================>.] - ETA: 0s 550248448/553467096 [============================>.] - ETA: 0s 551821312/553467096 [============================>.] - ETA: 0s 553394176/553467096 [============================>.] - ETA: 0s 553467904/553467096 [==============================] - 32s 0us/step

Function to predict category

def predict_images(images, m):
  for i in images:
    print ('processing image:', i)
    img = image.load_img(i, target_size=(224, 224))
    #convert to numpy array for Keras image formate processing
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = m.predict(x)
    # decode the results into a list of tuples (class, description, probability
    print('Predicted:', decode_predictions(preds, top=3)[0], '\n')

Classify African and Indian Elephants

Load and predict images using VGG16 pretrained model

African Elephants    

elephants_img_paths = ["/dbfs/brooke/spark-summit-sf/elephants/" + path for path in os.listdir("/dbfs/brooke/spark-summit-sf/elephants/")]
predict_images(elephants_img_paths, vgg16Model)

processing image: /dbfs/brooke/spark-summit-sf/elephants/african_elephant_1.jpg Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json 8192/35363 [=====>........................] - ETA: 0s 24576/35363 [===================>..........] - ETA: 0s 40960/35363 [==================================] - 0s 4us/step Predicted: [('n02504458', 'African_elephant', 0.7304009), ('n01871265', 'tusker', 0.25355038), ('n02504013', 'Indian_elephant', 0.016048659)] processing image: /dbfs/brooke/spark-summit-sf/elephants/african_elephant_2.jpg Predicted: [('n01871265', 'tusker', 0.4814257), ('n02504458', 'African_elephant', 0.43547475), ('n02504013', 'Indian_elephant', 0.07725713)] processing image: /dbfs/brooke/spark-summit-sf/elephants/big_ox.jpg Predicted: [('n02410509', 'bison', 0.43230733), ('n02403003', 'ox', 0.19804387), ('n02133161', 'American_black_bear', 0.08488397)] processing image: /dbfs/brooke/spark-summit-sf/elephants/indian_elephant_1.jpeg Predicted: [('n01871265', 'tusker', 0.63030255), ('n02504013', 'Indian_elephant', 0.3172723), ('n02504458', 'African_elephant', 0.052417696)] processing image: /dbfs/brooke/spark-summit-sf/elephants/indian_elephant_3.jpeg Predicted: [('n03980874', 'poncho', 0.27748922), ('n02504013', 'Indian_elephant', 0.15591854), ('n03884397', 'panpipe', 0.118131705)]

hotdog_img_paths = ["/dbfs/brooke/spark-summit-sf/hotdog/" + path for path in os.listdir("/dbfs/brooke/spark-summit-sf/hotdog/")]
predict_images(hotdog_img_paths, vgg16Model)

processing image: /dbfs/brooke/spark-summit-sf/hotdog/hamburger_1.jpeg Predicted: [('n07697313', 'cheeseburger', 0.98272187), ('n07693725', 'bagel', 0.0061851414), ('n07613480', 'trifle', 0.005066536)] processing image: /dbfs/brooke/spark-summit-sf/hotdog/hotdog_1.jpeg Predicted: [('n07697537', 'hotdog', 0.9997451), ('n07697313', 'cheeseburger', 8.424303e-05), ('n07615774', 'ice_lolly', 4.0920193e-05)] processing image: /dbfs/brooke/spark-summit-sf/hotdog/hotdog_2.jpeg Predicted: [('n07697537', 'hotdog', 0.99958676), ('n07684084', 'French_loaf', 0.00022811872), ('n07697313', 'cheeseburger', 0.00011211785)] processing image: /dbfs/brooke/spark-summit-sf/hotdog/weiner_hotdog.jpeg Predicted: [('n02090379', 'redbone', 0.23659879), ('n02100583', 'vizsla', 0.14053573), ('n02100877', 'Irish_setter', 0.09921835)] processing image: /dbfs/brooke/spark-summit-sf/hotdog/yang_2.jpeg Predicted: [('n04356056', 'sunglasses', 0.44147384), ('n02871525', 'bookshop', 0.094294004), ('n04355933', 'sunglass', 0.06100458)] processing image: /dbfs/brooke/spark-summit-sf/hotdog/yang_3.jpg Predicted: [('n03877472', 'pajama', 0.062383126), ('n04317175', 'stethoscope', 0.02806277), ('n02730930', 'apron', 0.027307687)]

DeepImagePredictor

Let's make these predictions in parallel on our Spark cluster!

from pyspark.ml.image import ImageSchema
from sparkdl.image import imageIO
from sparkdl import DeepImagePredictor

nerds_df = ImageSchema.readImages("brooke/spark-summit-sf/nerds/")

predictor = DeepImagePredictor(inputCol="image", outputCol="predicted_labels", modelName="VGG16", decodePredictions=True, topK=5)
predictions_df = predictor.transform(nerds_df).cache()
predictions_df.count()

INFO:tensorflow:Froze 32 variables. Converted 32 variables to const ops. INFO:tensorflow:Froze 0 variables. Converted 0 variables to const ops. Out[5]: 8

display(predictions_df)
image predicted_labels
[{"class":"n03045698","description":"cloak","probability":0.39211166},{"class":"n03787032","description":"mortarboard","probability":0.091029376},{"class":"n03404251","description":"fur_coat","probability":0.08471853},{"class":"n04371774","description":"swing","probability":0.056981083},{"class":"n04370456","description":"sweatshirt","probability":0.028172707}]
[{"class":"n04350905","description":"suit","probability":0.41138414},{"class":"n02916936","description":"bulletproof_vest","probability":0.10152984},{"class":"n03763968","description":"military_uniform","probability":0.09318812},{"class":"n04591157","description":"Windsor_tie","probability":0.07702819},{"class":"n02669723","description":"academic_gown","probability":0.03608404}]
[{"class":"n03630383","description":"lab_coat","probability":0.23492548},{"class":"n04591157","description":"Windsor_tie","probability":0.09487544},{"class":"n03838899","description":"oboe","probability":0.049194943},{"class":"n04350905","description":"suit","probability":0.043708242},{"class":"n03832673","description":"notebook","probability":0.041520175}]
[{"class":"n04350905","description":"suit","probability":0.25813994},{"class":"n01440764","description":"tench","probability":0.03799466},{"class":"n03838899","description":"oboe","probability":0.03496751},{"class":"n02883205","description":"bow_tie","probability":0.033893984},{"class":"n03394916","description":"French_horn","probability":0.03332546}]
[{"class":"n03595614","description":"jersey","probability":0.3530513},{"class":"n04370456","description":"sweatshirt","probability":0.13232166},{"class":"n03942813","description":"ping-pong_ball","probability":0.097091846},{"class":"n03141823","description":"crutch","probability":0.018438742},{"class":"n04270147","description":"spatula","probability":0.017245641}]
[{"class":"n03000247","description":"chain_mail","probability":0.14295407},{"class":"n02672831","description":"accordion","probability":0.10376813},{"class":"n02787622","description":"banjo","probability":0.069579415},{"class":"n02804610","description":"bassoon","probability":0.061210092},{"class":"n03838899","description":"oboe","probability":0.058611386}]
[{"class":"n03630383","description":"lab_coat","probability":0.6628539},{"class":"n04317175","description":"stethoscope","probability":0.12459004},{"class":"n04370456","description":"sweatshirt","probability":0.038363792},{"class":"n04039381","description":"racket","probability":0.0132558},{"class":"n03595614","description":"jersey","probability":0.008680802}]
[{"class":"n04350905","description":"suit","probability":0.17892571},{"class":"n02883205","description":"bow_tie","probability":0.0947369},{"class":"n04591157","description":"Windsor_tie","probability":0.08621924},{"class":"n04162706","description":"seat_belt","probability":0.07562429},{"class":"n03630383","description":"lab_coat","probability":0.06052583}]

 Show image preview 

Detected data types for which enhanced rendering is supported. For details, see the Databricks Guide.

Let's change the model

inception = DeepImagePredictor(inputCol="image", outputCol="predicted_labels", modelName="InceptionV3", decodePredictions=True, topK=5)
inception_df = inception.transform(nerds_df).cache()
inception_df.count()

Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5 8192/96112376 [..............................] - ETA: 17:10 24576/96112376 [..............................] - ETA: 11:27 40960/96112376 [..............................] - ETA: 10:19 73728/96112376 [..............................] - ETA: 7:38 106496/96112376 [..............................] - ETA: 6:36 139264/96112376 [..............................] - ETA: 6:04 180224/96112376 [..............................] - ETA: 5:28 229376/96112376 [..............................] - ETA: 4:54 278528/96112376 [..............................] - ETA: 4:32 335872/96112376 [..............................] - ETA: 4:11 401408/96112376 [..............................] - ETA: 3:51 475136/96112376 [..............................] - ETA: 3:32 540672/96112376 [..............................] - ETA: 3:22 630784/96112376 [..............................] - ETA: 3:06 720896/96112376 [..............................] - ETA: 2:55 802816/96112376 [..............................] - ETA: 2:47 925696/96112376 [..............................] - ETA: 2:34 1048576/96112376 [..............................] - ETA: 2:23 1187840/96112376 [..............................] - ETA: 2:13 1327104/96112376 [..............................] - ETA: 2:05 1482752/96112376 [..............................] - ETA: 1:58 1654784/96112376 [..............................] - ETA: 1:50 1867776/96112376 [..............................] - ETA: 1:42 2088960/96112376 [..............................] - ETA: 1:35 2318336/96112376 [..............................] - ETA: 1:29 2564096/96112376 [..............................] - ETA: 1:23 2826240/96112376 [..............................] - ETA: 1:18 3088384/96112376 [..............................] - ETA: 1:14 3383296/96112376 [>.............................] - ETA: 1:10 3694592/96112376 [>.............................] - ETA: 1:06 4022272/96112376 [>.............................] - ETA: 1:02 4374528/96112376 [>.............................] - ETA: 59s 4775936/96112376 [>.............................] - ETA: 55s 5226496/96112376 [>.............................] - ETA: 52s 5734400/96112376 [>.............................] - ETA: 48s 6324224/96112376 [>.............................] - ETA: 45s 7004160/96112376 [=>............................] - ETA: 41s 7798784/96112376 [=>............................] - ETA: 37s 8749056/96112376 [=>............................] - ETA: 34s 9797632/96112376 [==>...........................] - ETA: 31s 10944512/96112376 [==>...........................] - ETA: 28s 12304384/96112376 [==>...........................] - ETA: 25s 13762560/96112376 [===>..........................] - ETA: 22s 15335424/96112376 [===>..........................] - ETA: 20s 16908288/96112376 [====>.........................] - ETA: 18s 18481152/96112376 [====>.........................] - ETA: 17s 20054016/96112376 [=====>........................] - ETA: 15s 21626880/96112376 [=====>........................] - ETA: 14s 23216128/96112376 [======>.......................] - ETA: 13s 24788992/96112376 [======>.......................] - ETA: 12s 26361856/96112376 [=======>......................] - ETA: 11s 27934720/96112376 [=======>......................] - ETA: 11s 29261824/96112376 [========>.....................] - ETA: 10s 30392320/96112376 [========>.....................] - ETA: 10s 31539200/96112376 [========>.....................] - ETA: 9s 32702464/96112376 [=========>....................] - ETA: 9s 33882112/96112376 [=========>....................] - ETA: 9s 35078144/96112376 [=========>....................] - ETA: 8s 36274176/96112376 [==========>...................] - ETA: 8s 37502976/96112376 [==========>...................] - ETA: 8s 38731776/96112376 [===========>..................] - ETA: 7s 39960576/96112376 [===========>..................] - ETA: 7s 41205760/96112376 [===========>..................] - ETA: 7s 42434560/96112376 [============>.................] - ETA: 7s 42786816/96112376 [============>.................] - ETA: 7s 43745280/96112376 [============>.................] - ETA: 6s 45023232/96112376 [=============>................] - ETA: 6s 46317568/96112376 [=============>................] - ETA: 6s 47628288/96112376 [=============>................] - ETA: 6s 48939008/96112376 [==============>...............] - ETA: 5s 50266112/96112376 [==============>...............] - ETA: 5s 51593216/96112376 [===============>..............] - ETA: 5s 52936704/96112376 [===============>..............] - ETA: 5s 54280192/96112376 [===============>..............] - ETA: 4s 55590912/96112376 [================>.............] - ETA: 4s 56213504/96112376 [================>.............] - ETA: 4s 57016320/96112376 [================>.............] - ETA: 4s 58392576/96112376 [=================>............] - ETA: 4s 59736064/96112376 [=================>............] - ETA: 4s 60358656/96112376 [=================>............] - ETA: 4s 61177856/96112376 [==================>...........] - ETA: 3s 62586880/96112376 [==================>...........] - ETA: 3s 63995904/96112376 [==================>...........] - ETA: 3s 65404928/96112376 [===================>..........] - ETA: 3s 66781184/96112376 [===================>..........] - ETA: 3s 67469312/96112376 [====================>.........] - ETA: 3s 68272128/96112376 [====================>.........] - ETA: 2s 69697536/96112376 [====================>.........] - ETA: 2s 71090176/96112376 [=====================>........] - ETA: 2s 71876608/96112376 [=====================>........] - ETA: 2s 72597504/96112376 [=====================>........] - ETA: 2s 74006528/96112376 [======================>.......] - ETA: 2s 74809344/96112376 [======================>.......] - ETA: 2s 75530240/96112376 [======================>.......] - ETA: 2s 76939264/96112376 [=======================>......] - ETA: 1s 77692928/96112376 [=======================>......] - ETA: 1s 78479360/96112376 [=======================>......] - ETA: 1s 79904768/96112376 [=======================>......] - ETA: 1s 80683008/96112376 [========================>.....] - ETA: 1s 81444864/96112376 [========================>.....] - ETA: 1s 82886656/96112376 [========================>.....] - ETA: 1s 83705856/96112376 [=========================>....] - ETA: 1s 84443136/96112376 [=========================>....] - ETA: 1s 85884928/96112376 [=========================>....] - ETA: 0s 86786048/96112376 [==========================>...] - ETA: 0s 87474176/96112376 [==========================>...] - ETA: 0s 88981504/96112376 [==========================>...] - ETA: 0s 90439680/96112376 [===========================>..] - ETA: 0s 91332608/96112376 [===========================>..] - ETA: 0s 92028928/96112376 [===========================>..] - ETA: 0s 93487104/96112376 [============================>.] - ETA: 0s 94314496/96112376 [============================>.] - ETA: 0s 95092736/96112376 [============================>.] - ETA: 0s 96116736/96112376 [==============================] - 9s 0us/step INFO:tensorflow:Froze 378 variables. Converted 378 variables to const ops. INFO:tensorflow:Froze 0 variables. Converted 0 variables to const ops. Out[7]: 8

display(inception_df)
image predicted_labels
[{"class":"n04350905","description":"suit","probability":0.14165702},{"class":"n04479046","description":"trench_coat","probability":0.11945703},{"class":"n03404251","description":"fur_coat","probability":0.027757034},{"class":"n04370456","description":"sweatshirt","probability":0.024130477},{"class":"n02837789","description":"bikini","probability":0.021668304}]
[{"class":"n02916936","description":"bulletproof_vest","probability":0.81811774},{"class":"n03787032","description":"mortarboard","probability":0.036147255},{"class":"n03763968","description":"military_uniform","probability":0.02605114},{"class":"n02669723","description":"academic_gown","probability":0.01372046},{"class":"n04350905","description":"suit","probability":0.008794671}]
[{"class":"n04479046","description":"trench_coat","probability":0.054543473},{"class":"n03838899","description":"oboe","probability":0.042113375},{"class":"n03630383","description":"lab_coat","probability":0.0317195},{"class":"n02787622","description":"banjo","probability":0.029045274},{"class":"n02804610","description":"bassoon","probability":0.026370155}]
[{"class":"n04350905","description":"suit","probability":0.7987358},{"class":"n02883205","description":"bow_tie","probability":0.053425536},{"class":"n04591157","description":"Windsor_tie","probability":0.011151478},{"class":"n02992529","description":"cellular_telephone","probability":0.0053684525},{"class":"n03763968","description":"military_uniform","probability":0.0039382246}]
[{"class":"n03595614","description":"jersey","probability":0.15574361},{"class":"n03942813","description":"ping-pong_ball","probability":0.05970348},{"class":"n04370456","description":"sweatshirt","probability":0.048369024},{"class":"n02804610","description":"bassoon","probability":0.034532476},{"class":"n03838899","description":"oboe","probability":0.03400313}]
[{"class":"n03763968","description":"military_uniform","probability":0.117212564},{"class":"n04350905","description":"suit","probability":0.035018962},{"class":"n02787622","description":"banjo","probability":0.033046678},{"class":"n04584207","description":"wig","probability":0.032433487},{"class":"n04317175","description":"stethoscope","probability":0.028688557}]
[{"class":"n03630383","description":"lab_coat","probability":0.36856785},{"class":"n04317175","description":"stethoscope","probability":0.037452906},{"class":"n03832673","description":"notebook","probability":0.03503557},{"class":"n04350905","description":"suit","probability":0.028838113},{"class":"n03787032","description":"mortarboard","probability":0.020943912}]
[{"class":"n03763968","description":"military_uniform","probability":0.9151895},{"class":"n03787032","description":"mortarboard","probability":0.012946242},{"class":"n04350905","description":"suit","probability":0.007009363},{"class":"n02669723","description":"academic_gown","probability":0.0068341326},{"class":"n02883205","description":"bow_tie","probability":0.0064667594}]

 Show image preview 

Detected data types for which enhanced rendering is supported. For details, see the Databricks Guide.

Part 2: Transfer Learning with Deep Learning Pipelines (DLP)

Deep Learning Pipelines provides utilities to perform transfer learning on images, which is one of the fastest (code and run-time-wise) ways to start using deep learning. Using Deep Learning Pipelines, it can be done in just several lines of code.

The idea behind transfer learning is to take knowledge from one model doing some task, and transfer it to build another model doing a similar task.

from pyspark.ml.image import ImageSchema
from pyspark.sql.functions import lit
from sparkdl.image import imageIO

img_dir = 'dbfs:/brooke/spark-summit-sf'
cats_df = ImageSchema.readImages(img_dir + "/cats").withColumn("label", lit(1))
dogs_df = ImageSchema.readImages(img_dir + "/dogs").withColumn("label", lit(0))

cats_train, cats_test = cats_df.randomSplit([.8, .2], seed=42)
dogs_train, dogs_test = dogs_df.randomSplit([.8, .2], seed=42)

train_df = cats_train.unionAll(dogs_train).cache()
test_df = cats_test.unionAll(dogs_test).cache()
display(train_df.select("image", "label"))
image label
1
1
1
1
1
0
0
0
0
0

 Show image preview 

Detected data types for which enhanced rendering is supported. For details, see the Databricks Guide.

Build the MLlib Pipeline

from pyspark.ml.classification import LogisticRegression
from pyspark.ml import Pipeline
from sparkdl import DeepImageFeaturizer 

featurizer = DeepImageFeaturizer(inputCol="image", outputCol="features", modelName="InceptionV3")
lr = LogisticRegression(maxIter=20, regParam=0.05, elasticNetParam=0.3, labelCol="label")
p = Pipeline(stages=[featurizer, lr])

p_model = p.fit(train_df)

Evaluate the Accuracy

from pyspark.ml.evaluation import MulticlassClassificationEvaluator

pred_df = p_model.transform(test_df).cache()
evaluator = MulticlassClassificationEvaluator(metricName="accuracy")
print("Test set accuracy = " + str(evaluator.evaluate(pred_df.select("prediction", "label"))*100) + "%")

Test set accuracy = 100.0%

display(pred_df.select("image", "label", "probability"))
image label probability
1 [1,2,[],[0.07983768538504338,0.9201623146149567]]
1 [1,2,[],[0.0735124824751803,0.9264875175248197]]
1 [1,2,[],[0.0688419453818859,0.9311580546181142]]
0 [1,2,[],[0.9475188514834973,0.0524811485165027]]
0 [1,2,[],[0.9026450467442289,0.0973549532557711]]
0 [1,2,[],[0.7731177886783923,0.2268822113216076]]

 Show image preview 

Detected data types for which enhanced rendering is supported. For details, see the Databricks Guide.