DL – Pretrained Model Example 01

import tensorflow as tf  #pip install tensorflow 
import numpy as np  #pip install numpy
filename = 'Pictures/Saved Pictures/office03.jpg'

Lets load the Deep Learning Model – Pre trained models using mobilenet

mobile = tf.keras.applications.mobilenet.MobileNet() #DL model weights - pretrained
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet/mobilenet_1_0_224_tf.h5
17227776/17225924 [==============================] - 9s 1us/step
  1. Creating a model
  2. Training a model
  3. Test or validate – provide labels
  4. Predict classification alogorithm

Prep-processing of the image Adding a step to use mobilenet_v2

—-mobile=tf.keras.applications.mobilenet_v2.mobilenet_v2()

from tensorflow.keras.preprocessing import image
img = image.load_img(filename, target_size=(224, 224))
import matplotlib.pyplot as plt
plt.imshow(img)
<matplotlib.image.AxesImage at 0x29e5a16d0d0>
resized_img = image.img_to_array(img)
final_image = np.expand_dims(resized_img, axis=0)  #need fourth dimension
final_image = tf.keras.applications.mobilenet.preprocess_input(final_image)
final_image.shape
(1, 224, 224, 3)
predictions = mobile.predict(final_image)
print(predictions)  #can be deleted / eliminate after you see output from the script
[[5.16344812e-07 4.29362586e-07 8.73986323e-07 2.88791171e-07
  2.14516149e-06 1.48731287e-05 5.28806140e-06 7.34447326e-07
  1.36056121e-06 8.73420163e-08 5.56154987e-07 2.73330625e-06
  1.20713446e-06 1.37200601e-07 2.19754088e-06 2.08699089e-06
  1.43476723e-06 4.89133129e-07 1.03431444e-06 7.70895326e-07
  4.43114368e-06 3.10400566e-07 2.39896053e-07 8.02754130e-07
  4.56352645e-08 5.40576686e-08 1.17779948e-07 7.08913944e-07
  1.20364149e-07 2.10305438e-06 5.88034368e-07 1.16747302e-07
  1.13033920e-07 1.30712888e-05 4.11793008e-06 4.07797188e-07
  3.35411528e-07 1.81322442e-08 1.21324021e-07 4.15668774e-06
  1.26525009e-07 1.00281078e-07 2.66568293e-07 3.05088577e-07
  9.52711900e-08 2.90141793e-06 1.20561583e-06 1.25241479e-08
  2.49361790e-07 5.33998627e-06 4.91935043e-06 4.97004839e-06
  1.65215681e-06 1.32060464e-07 2.00471035e-07 2.11161066e-07
  1.14497176e-07 1.14666008e-07 2.05196301e-07 1.24695168e-06
  7.95229244e-06 1.15702967e-07 1.63893759e-07 2.19689764e-06
  1.05349602e-06 4.17304619e-07 1.17150876e-06 9.87205794e-07
  4.34937789e-07 9.16358331e-08 5.90005470e-07 2.33659790e-07
  6.01964871e-07 2.09702603e-06 2.00201455e-07 2.88092195e-07
  1.75396656e-06 5.26195493e-07 4.72845295e-06 8.02432680e-07
  1.14056121e-07 5.01673014e-08 1.28321034e-07 4.60258946e-07
  6.46333689e-08 8.69494059e-08 4.81464383e-07 6.77928028e-07
  7.60463706e-07 2.67302482e-07 1.56794806e-06 3.63888944e-07
  3.42667192e-07 2.53022847e-07 8.43923658e-07 8.55267615e-07
  3.27400016e-07 1.82778834e-07 1.37009363e-06 3.85945128e-07
  6.86967326e-07 1.85828483e-07 1.30924286e-06 1.27458441e-06
  7.33703610e-06 8.50109154e-06 5.54093185e-06 5.16194632e-07
  1.36296052e-07 2.79240510e-07 2.72499934e-09 2.55877012e-07
  1.22704478e-06 4.54595153e-08 5.40433369e-08 3.57898919e-08
  1.94126869e-07 1.46792922e-06 1.79309754e-05 6.46112198e-07
  8.60642444e-07 8.11221616e-06 1.54946883e-05 7.99460122e-06
  2.55105606e-05 3.33195771e-07 2.74339811e-07 5.69003419e-07
  8.35453761e-07 7.63723662e-08 2.61725095e-08 6.27798272e-07
  4.64383163e-07 3.84966569e-07 7.55831365e-07 2.57512795e-07
  1.14544309e-06 2.64947653e-06 3.29618857e-08 1.89719351e-08
  7.13922361e-07 1.05624849e-06 4.53797952e-07 4.68263352e-07
  1.18598351e-07 3.89519164e-06 4.71457327e-07 3.48210665e-06
  5.68045652e-06 5.47709590e-07 3.52902243e-05 1.43044042e-06
  4.03469858e-06 2.28371709e-06 2.62142912e-05 7.95036703e-06
  6.55012946e-06 9.90593799e-06 5.95887286e-06 5.68650307e-07
  6.89387480e-06 1.81659889e-05 2.35377447e-05 2.48305314e-05
  6.99175018e-07 6.53325378e-06 2.12218220e-06 1.10256553e-06
  1.15836456e-05 1.66802010e-06 7.65588356e-06 8.78135779e-06
  3.35160075e-06 2.94970789e-07 1.53671008e-05 1.33345120e-05
  5.44472141e-06 9.12121379e-07 6.79957020e-06 2.22577432e-06
  3.43027682e-06 3.36923972e-06 9.99015015e-07 1.16260831e-04
  7.42209249e-06 3.90323839e-06 8.95595440e-06 7.48884850e-06
  1.56308988e-06 8.04278272e-07 7.93761283e-05 1.29477039e-05
  3.57404679e-06 4.73517275e-06 2.55563900e-05 2.12530867e-05
  1.07620940e-06 1.91217114e-05 3.53832693e-05 4.13069138e-05
  6.94025493e-06 4.50809330e-06 1.85651697e-05 4.54703804e-05
  3.73019247e-05 2.01076673e-05 3.89749330e-05 4.25203598e-06
  1.06028492e-05 4.16137209e-06 4.19688490e-07 3.95309962e-06
  8.34674097e-07 5.84591999e-06 4.20684091e-06 3.47004652e-06
  6.63724277e-05 4.41493285e-06 7.01673616e-06 1.57180614e-06
  4.35374648e-04 1.42353874e-05 1.65165202e-05 2.09524187e-05
  5.26864596e-06 1.24556573e-06 8.23437658e-05 1.04344917e-06
  2.20275815e-05 1.38686346e-05 7.09892920e-05 7.90177510e-05
  1.96984593e-06 1.03572798e-04 6.75654519e-06 7.89324872e-07
  1.49560435e-06 7.11728308e-06 3.29014802e-05 3.39028520e-05
  2.95840618e-06 1.07142750e-05 1.18236358e-06 6.53364714e-06
  5.57713865e-05 1.08026343e-05 7.15454235e-06 1.42361944e-06
  5.40449655e-05 4.71870262e-05 8.51057848e-05 1.24829262e-06
  2.21236132e-06 7.69521466e-06 2.63038673e-05 1.75331861e-05
  3.24942630e-05 4.70574423e-06 2.49936529e-05 1.19548211e-04
  2.00678405e-05 9.51894617e-06 1.54197376e-04 1.25840714e-04
  4.48118553e-05 1.63933328e-05 1.04929932e-05 1.82496333e-05
  1.64561209e-06 1.16356443e-06 1.11989743e-06 7.12763836e-07
  1.24056257e-07 4.95777749e-06 1.86317459e-06 2.10098250e-07
  3.33292064e-07 1.14628131e-06 1.60853069e-05 4.67711061e-06
  1.03660341e-06 5.09631536e-06 5.90169020e-06 1.28627225e-05
  1.15355151e-05 1.02976492e-05 2.17709101e-07 1.08196673e-05
  3.14956566e-07 2.46175460e-07 4.32993062e-07 2.01085209e-06
  7.95802464e-07 4.80341669e-07 2.06541358e-07 1.43554871e-06
  8.67090648e-07 1.93624237e-06 3.92109257e-07 5.20820493e-07
  5.38101347e-07 4.54696583e-08 4.90116520e-07 7.65667920e-08
  2.00126902e-08 2.23698248e-06 3.96166485e-07 1.00844034e-07
  1.87409228e-07 3.19874474e-08 9.71137979e-07 2.93434653e-07
  1.40507140e-07 6.84756515e-07 1.68402778e-06 3.53886008e-07
  4.16099866e-08 6.45615410e-06 3.00583132e-08 2.90957104e-07
  2.12443709e-07 2.35576501e-08 2.07528771e-07 1.46371315e-06
  5.23173696e-07 6.86022645e-07 6.41992642e-07 1.72498142e-06
  2.39568863e-07 3.74083164e-08 1.81653121e-07 1.28729266e-06
  3.05950452e-05 3.88989065e-06 3.02107566e-07 5.00086912e-07
  3.79233097e-06 3.77692982e-06 2.40933514e-05 5.72932151e-07
  9.11874395e-07 1.11994177e-05 1.21186361e-06 1.52705127e-06
  1.64549442e-06 3.08244495e-07 1.84877464e-07 1.34209290e-06
  1.32420382e-07 5.14172598e-08 2.35048802e-07 2.18185392e-06
  2.03617606e-06 3.51576688e-07 1.45922172e-07 5.05325943e-07
  1.43444922e-05 3.29818988e-07 9.98585506e-07 9.40833434e-06
  5.33253342e-07 6.17610908e-07 1.09228240e-05 2.20271704e-06
  1.14104431e-07 2.56511015e-07 1.89579112e-07 7.84036911e-06
  2.04219305e-06 1.23781422e-06 4.42459168e-07 1.44750345e-06
  1.84219857e-06 3.98835073e-06 6.73718716e-07 1.87329888e-07
  1.63788070e-06 7.86183818e-07 6.22517177e-07 3.20085206e-07
  1.26657631e-06 1.42982046e-07 5.84684756e-07 1.47738660e-06
  3.13715667e-08 9.18282149e-06 6.83336020e-06 1.37111513e-07
  1.58616126e-06 1.56962221e-06 2.83724353e-06 2.30219939e-06
  1.14812906e-06 4.11201206e-08 5.02107150e-06 4.14011708e-07
  5.84890820e-08 3.85059273e-08 3.15288089e-05 1.68994046e-03
  1.02560688e-03 3.39394719e-05 5.79358766e-06 2.69027197e-07
  5.05295338e-06 3.23019265e-07 6.06863214e-05 3.88006665e-05
  3.79667335e-06 1.05890967e-05 9.33102456e-06 5.30070218e-04
  2.19875783e-05 4.15140721e-06 4.03673446e-04 1.55063899e-04
  2.52978585e-04 3.28215151e-06 3.75762793e-05 3.34362121e-04
  3.64049856e-06 1.89416660e-05 1.65885896e-04 7.78366229e-05
  5.22719420e-05 6.74796112e-08 2.32740717e-06 3.37948768e-05
  5.62723926e-06 8.41418242e-08 2.59330977e-06 3.80341226e-05
  7.04910781e-05 6.44066949e-06 6.74951252e-06 5.62113302e-04
  2.45173851e-05 2.80887275e-06 2.47074524e-03 2.12062969e-05
  7.80014375e-07 2.20517104e-05 3.24063112e-05 3.59494916e-05
  7.07591107e-06 7.06649371e-05 9.30105278e-04 5.30116704e-05
  8.06298090e-07 1.12511561e-05 4.37432056e-04 5.22628179e-05
  1.04436106e-06 3.39707243e-04 3.86891363e-04 4.25299731e-06
  7.03085652e-06 2.59500835e-03 5.57937824e-07 6.51277878e-05
  3.39713847e-06 2.29209448e-07 1.32377600e-05 2.42851922e-04
  3.84610274e-07 3.57572244e-05 1.70329309e-04 1.10210920e-06
  2.50707740e-06 7.07445724e-05 1.36448294e-02 4.16909461e-06
  1.39263357e-04 1.76757403e-05 1.44129999e-05 2.05750189e-06
  8.37977666e-07 3.11848526e-05 1.05355994e-03 2.76364462e-05
  2.87467847e-05 3.43974243e-05 1.09033308e-05 1.72730324e-07
  2.04025423e-06 5.04376512e-05 1.05555875e-04 5.29283949e-04
  1.06308801e-06 1.56394648e-07 5.22964683e-07 8.61070930e-07
  3.12302000e-04 2.03274772e-06 2.46450072e-05 1.72024352e-06
  3.77184028e-06 5.26447729e-06 9.57974407e-06 5.13112391e-05
  1.12768566e-06 1.09481196e-04 2.02001356e-05 2.80150653e-05
  1.25093884e-05 3.54438725e-06 1.35961182e-06 1.48175872e-07
  1.31354755e-04 1.96922974e-06 2.86011550e-06 1.35143337e-05
  4.77780259e-06 5.83087512e-06 2.36813617e-06 4.35185939e-07
  2.63827416e-04 3.17092372e-06 2.10297208e-06 1.15942174e-04
  5.48806602e-05 1.12762333e-04 7.81883409e-06 2.93056310e-05
  2.00124518e-07 3.31298139e-07 4.14834842e-02 8.36052373e-03
  5.95295978e-06 9.61416226e-05 7.55020155e-05 6.30680515e-05
  4.68797734e-05 2.37596782e-06 6.83993567e-04 2.15143441e-08
  1.56294675e-06 7.18568208e-06 4.17071669e-06 4.73277119e-04
  3.73098942e-07 1.10210931e-04 4.56766538e-05 1.11552821e-04
  1.10212619e-04 2.12993455e-05 2.63740494e-05 8.91081356e-07
  1.64097873e-05 1.15631774e-04 5.32009108e-06 1.98622947e-04
  2.25489703e-06 1.57708265e-02 1.40831446e-06 2.91861454e-07
  3.35986879e-05 7.90930972e-06 2.13353429e-04 4.75028850e-04
  6.48069332e-08 3.49884867e-05 2.84136217e-06 1.33337362e-05
  3.73376042e-06 5.01141767e-06 4.35285147e-05 3.83306133e-06
  1.23237896e-05 9.05869456e-07 1.76224676e-05 1.05893537e-06
  6.20243736e-06 7.38963572e-05 6.85464101e-07 1.78469956e-04
  9.44654494e-06 1.83264601e-05 5.63068279e-05 2.48086499e-03
  1.07477917e-05 1.55788939e-07 1.67401464e-04 6.84509068e-05
  2.31436843e-06 7.70457380e-04 4.21342975e-06 5.24129064e-05
  4.29735974e-06 5.37796113e-05 1.66126073e-03 3.49596703e-05
  2.23835996e-05 1.31888621e-04 5.96577847e-05 1.23400025e-06
  3.43674874e-05 4.08652511e-07 1.11368136e-05 2.35022817e-05
  1.94808149e-06 1.02435133e-05 2.71019853e-05 7.48114246e-07
  2.22501876e-06 8.06132739e-05 5.45596704e-04 2.99679668e-05
  5.87208662e-04 9.62047238e-07 2.13861684e-04 2.11662598e-04
  6.48154382e-05 5.44955265e-06 7.23561025e-05 1.12776688e-05
  2.35933567e-06 5.83579671e-03 5.38197892e-05 5.19207060e-06
  4.80101347e-01 6.54657015e-06 2.72619150e-06 4.45804471e-05
  1.04275988e-02 2.34390282e-05 3.18581369e-05 1.41170996e-04
  1.26175257e-06 9.39166115e-04 3.90087394e-03 1.24059166e-04
  8.69380528e-06 6.51359805e-05 3.46497909e-05 1.12746693e-05
  7.90933729e-04 2.56626890e-06 4.36850896e-05 2.25547883e-05
  1.62420562e-07 8.94944878e-06 3.14240460e-04 3.05828348e-06
  9.78269527e-05 7.15359022e-07 1.69111241e-04 2.20990369e-05
  4.09335689e-06 2.32178181e-07 1.08318800e-05 3.95762800e-05
  2.12819250e-05 2.37999648e-05 8.42092595e-06 1.25245738e-03
  2.70570763e-05 6.16310854e-06 2.57598640e-06 5.20628600e-06
  5.08821759e-05 1.82508245e-06 1.29469830e-04 3.74355665e-07
  7.57689448e-03 2.95126142e-07 5.54345297e-06 1.49953912e-03
  3.53661130e-06 6.78640276e-07 2.37280318e-07 2.90881104e-07
  2.13573367e-05 2.03852236e-04 5.96385580e-05 1.73399792e-04
  7.70816314e-06 4.29546708e-06 3.15574202e-04 6.51837070e-07
  9.67510641e-05 2.56625116e-01 3.47048990e-06 2.47878744e-03
  7.00053788e-05 3.78711007e-07 3.53592668e-06 1.32336954e-05
  1.02400500e-03 8.87126316e-06 8.06640344e-07 2.01324496e-04
  4.79947221e-06 2.89325317e-06 2.42724218e-06 2.16914827e-06
  3.34488723e-04 4.25276812e-04 4.23888923e-06 1.85431054e-05
  5.59711680e-06 1.91794516e-06 7.20693351e-05 1.47403362e-05
  4.34405564e-07 6.28928183e-06 3.02810833e-04 7.40627547e-06
  9.40497921e-06 1.05387800e-04 4.75698180e-05 6.58906501e-05
  8.68374991e-05 5.95398806e-02 7.80830487e-06 3.11868746e-07
  4.39954761e-07 7.31515627e-07 9.44643205e-07 5.76103957e-05
  2.12875075e-05 1.36655808e-05 4.83158743e-03 3.29705017e-06
  2.79337337e-06 2.35448260e-06 3.43073298e-05 1.19006063e-06
  3.92290713e-05 1.80285645e-06 8.11799782e-06 1.09710872e-05
  1.51282366e-05 7.55481778e-07 1.16880824e-06 1.34032070e-05
  9.26057692e-05 5.59910859e-06 1.90198807e-05 2.71691039e-04
  7.32048939e-05 1.16750372e-04 4.34781937e-03 1.45925864e-04
  2.76814058e-06 5.84860239e-03 1.83858288e-06 9.14645796e-07
  1.03226330e-05 4.04733745e-03 1.80670595e-05 7.96303175e-06
  1.43104751e-06 6.82383961e-06 2.17020584e-04 3.82883300e-05
  5.40103902e-05 2.31444665e-05 1.35810652e-07 5.54749340e-06
  7.39581155e-05 5.31757250e-05 5.77965111e-04 3.24384075e-07
  7.57819407e-06 1.23157239e-04 1.31493816e-05 4.88818841e-05
  2.20370353e-07 6.17195255e-05 1.16086831e-05 1.62914293e-04
  2.74096760e-06 2.11282827e-06 2.63029535e-04 1.68666054e-04
  1.08476619e-04 1.89296912e-07 3.41844687e-04 4.28702833e-06
  6.75251295e-06 1.31054321e-06 1.01276487e-03 1.09551738e-05
  5.87974937e-05 4.74014269e-06 1.36383763e-03 2.12403378e-07
  1.12840603e-03 1.34251534e-03 1.05185572e-04 5.58918255e-05
  1.19089623e-06 2.76979358e-06 2.99568660e-06 8.61208093e-07
  6.34297521e-06 7.16982540e-05 2.72858938e-06 6.90771500e-04
  1.08998529e-06 1.10477968e-05 1.24724602e-05 1.68715610e-06
  9.07565191e-05 5.51636731e-06 7.81481940e-05 4.50022460e-04
  2.93916810e-06 3.13430828e-05 2.08176003e-04 4.05429659e-04
  2.19591420e-05 5.27806333e-05 2.70938708e-05 5.43177350e-07
  6.49463274e-08 6.23714914e-06 1.45423779e-04 1.18926997e-04
  1.55338668e-07 8.48435434e-07 4.38313553e-04 2.71863246e-04
  6.22550169e-06 1.09424093e-06 7.94689458e-06 1.38799087e-05
  1.99569809e-06 1.39106651e-05 1.28076167e-03 7.66181911e-05
  1.01284547e-06 2.54180736e-06 1.06362728e-02 2.94212271e-07
  1.77226219e-04 1.20103476e-04 3.09424096e-04 6.26027656e-08
  2.18421279e-04 5.97586877e-05 4.04979255e-05 2.54303654e-06
  6.83337976e-06 2.33647534e-05 1.05791542e-05 1.57737736e-06
  5.05353419e-05 1.52908126e-06 3.95855477e-06 3.88445420e-04
  1.97830377e-05 5.45430680e-07 1.44105397e-05 2.79222440e-05
  1.55133705e-06 1.47100241e-06 8.49109298e-08 3.59488040e-04
  1.65946176e-06 1.48500054e-04 1.01562882e-05 3.40065220e-08
  3.05641890e-07 1.39801405e-05 7.46352362e-06 2.83455825e-06
  4.99778707e-06 3.08737130e-04 2.29750021e-05 1.51991605e-06
  3.72093859e-06 1.01433420e-06 4.49770766e-07 8.20756668e-06
  9.76473908e-04 2.62666319e-04 1.21155917e-05 1.31917732e-05
  1.98312136e-06 5.52129095e-05 1.66339843e-04 3.71085684e-06
  5.86745682e-06 3.64951411e-05 5.00280294e-06 2.02115072e-04
  1.01669087e-07 5.27301163e-05 4.12854888e-07 6.09306990e-05
  9.63112980e-05 7.96877430e-06 1.39324722e-04 2.95502264e-06
  7.11270695e-05 1.89374929e-04 8.01712486e-06 3.08454764e-05
  6.76473277e-08 2.79628421e-06 1.57269096e-04 2.49178993e-05
  9.58739955e-04 6.35818637e-04 4.84229822e-04 3.11647409e-06
  1.90248520e-05 1.09606399e-05 1.73691646e-04 5.03722767e-05
  6.83795918e-07 8.55931681e-09 2.92118475e-05 4.27235545e-06
  3.97616212e-04 1.51051208e-04 2.23923111e-04 5.64106949e-06
  1.97690156e-06 5.22650116e-05 5.09820646e-04 1.32225305e-05
  1.36370102e-06 1.65351794e-05 4.83700678e-05 1.31692859e-05
  2.27261285e-06 3.11208896e-05 8.64124195e-06 1.25034069e-06
  4.71836302e-06 4.34022496e-07 8.47816318e-06 2.35970242e-06
  1.17938043e-06 2.16328026e-05 5.18476736e-06 1.14408385e-06
  2.29694251e-06 1.71669546e-06 8.55203598e-06 9.40455811e-06
  6.63864739e-07 1.58853112e-07 6.13320879e-07 6.02046953e-07
  6.94016899e-06 2.22200970e-06 1.15026069e-06 1.11001623e-06
  2.95518731e-07 2.93710332e-06 3.87932459e-06 2.32016714e-06
  3.78156642e-07 3.01011852e-07 2.70898397e-07 5.95718529e-06
  2.73075671e-07 6.21518848e-05 4.10004577e-06 1.68193174e-06
  1.03334669e-05 3.15233956e-05 6.46643275e-06 1.59237097e-04
  2.42587103e-05 6.07510621e-04 4.20248426e-07 3.43414558e-06
  3.42436397e-06 3.82188659e-07 2.97754877e-06 3.19920728e-05
  5.92061099e-07 6.79860677e-05 5.63967888e-07 3.92626788e-07
  3.15320563e-07 1.38711997e-07 1.40806078e-04 1.20845903e-06
  3.32565469e-06 1.57778430e-07 4.27422776e-07 6.47556953e-05
  4.51712658e-08 3.52699807e-08 7.55673071e-08 8.83183233e-08
  1.47637804e-08 8.78524347e-08 1.42857743e-07 8.10229267e-07
  5.83619624e-07 1.02774472e-07 5.36921361e-05 4.75273100e-06]]
from tensorflow.keras.applications import imagenet_utils
results = imagenet_utils.decode_predictions(predictions)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json
40960/35363 [==================================] - 0s 4us/step
print(results)
[[('n03642806', 'laptop', 0.48010135), ('n03832673', 'notebook', 0.25662512), ('n03924679', 'photocopier', 0.05953988), ('n03179701', 'desk', 0.041483484), ('n03337140', 'file', 0.015770826)]]
plt.imshow(img)
<matplotlib.image.AxesImage at 0x29e5a5c7280>

Predict results again

predictions = mobile.predict(final_image)
results = imagenet_utils.decode_predictions(predictions)
print(results)
[[('n03642806', 'laptop', 0.48010135), ('n03832673', 'notebook', 0.25662512), ('n03924679', 'photocopier', 0.05953988), ('n03179701', 'desk', 0.041483484), ('n03337140', 'file', 0.015770826)]]