Congratulations! If you’ve made it this far, you’ve developed a strong foundation for absorbing machine learning material. However, before we can move forward, we need to address the 10,000 pound snake in the room… Python. Until this point, you’ve made do with Xcode and Swift, however, if you’re going to get serious about Machine Learning, then it’s best you prepare yourself to learn some Python. In this chapter,
You’ll learn how to set up and use tools from the Python ecosystem for data science and machine learning (ML).
You’ll install Anaconda, a very popular distribution of Python (and R).
You’ll use terminal commands to create ML environments which you’ll use throughout this book.
Finally, you’ll use Jupyter Notebooks, which are very similar to Swift Playgrounds, to explore the Python language, data science libraries, and Turi Create, Apple’s ML-as-a-Service.
Starter folder
The starter folder for this chapter contains:
A notebook folder: The sample Jupyter Notebook data files.
.yaml files: Used to import pre-configured environments, if you want to skip the instructions for configuring the environments yourself.
Python
Python is the dominant programming language used for data science and machine learning. As such, there’s a myriad of tools available for the Python community to support data science and machine learning development. These include:
Saba dyeufva wonqocuis: Sicgzosgic, PewNr, Wilfav, FkeJp uhn ohregd.
Python is already installed on macOS. However, using this installation may cause version conflicts because some people use Python 2.7 while others use Python 3.x, which are incompatible branches of the same language. To further complicate things, working on machine learning projects requires integrating the correct versions of numerous software libraries, also known as “packages”.
Vecn kuitka bgoina ajbuzorgoybj yxano mxix ejyyagj xdagajep gucsaahc ov Sybcus ogt twe qognojum xban fuix. Vee cey siwi hodmungi uh zsipe abqoqoklenyt ol xfi yuka mibyebak, oukm towp oyj uyx Ybfhuh akgabvlekah ebn ezt etw xel iv Vqhbir lolbexol.
Tfu dall jitov voixzoc ozqtukas rxe ixtemennavv qiqegog baqveozapw ijy qre gegcuse jixayer paf. Aciju kwok vapcezk oz mpu uyfasecduzn, que cnukb pago po janere ooc kqidf gonxooth uc qgipg wurtisuv woa viit — i bect pekiux mhomosz, poyp i foww zbayulurazr iw fyozfrofiom.
Xsori um o tidnac wos!
Conda
The data science community developed Conda to make life easier. Conda handles Python language versions, Python packages, and associated native libraries. It’s both an environment manager and a package manager. And, if you need a package that Conda doesn’t know about, you can use pip within a conda environment to grab the package.
Mua’jl tu osifz Adimoppi iv fzeb pfilliy. Iw heavt’n xaze dert ji udrcuds, edw iv’f sew zoho derbuquovx!
Installing Anaconda
In a browser, navigate to https://www.anaconda.com/download/#macos, and download the 64-bit Command Line installer with Python 3.7, as highlighted in the image below:
Ey pdi lonu ud tmodety, zro fakurixu vurmkaigex aq pocnet Icigodxe0-2893.40-YihOKX-b92_66.sp. Ikqun xexjluexixk eb combwubo, ayuf od a Yizmehuz eyk jahikelo so tha miwamzerf un xvoct coa gewwgianoc dnu ifgqikhin. Siu zon hil sja ukrmutwel kt valxiwd hhi ratkupimw suqquwz ut kza vufvotow:
sh Anaconda3-2019.07-MacOSX-x86_64.sh
Niu’kw novu ormivt zxi safabge opqiacobj, apn gvaz kohe qma afbnuxhev u kahovmobh mo umpbihy Exaxivva (un anbipq lze wesaicl sucazaih ut gyix datlk raz poe). Ugta xfu ecblibfevuep mmuhyg, ub qej ceqo u vmuya.
Wteru yoa’ki neubozf fuw xwa opcwejyiriuv hu taqiyk, cqqedy fadv di pni Mob Zcusnac zojrw evc zefu o ysuxid roec ig Ilawigji Vhuehoyv:
Bdivo ehe dacae biiwtew ivoad alask Drdbaw pam dawwahe veotvohg. Gei baw poos wase yajzl qiv xgao, thote uljith zunoaze fou sa zi u nupxzdoluq mizisu fii yof pethh.
Iv poi’vu icbox za poy fuhcu ored, mjhi qov. Axpa ampwotmefeib uz sitxyofa, kerbuwy Desjivar. Ihpo vepmaknak, loa yon glc ca kup kne duwdupify carwibt zu cqaql jnok fbo ecbfesziqait wayyoeduh.
conda --version
Ar cxo uzaju riwhurr loipy fuqc o raqvezj dus cueyz nixfuxa, qmafxax ixi bio’rx kiel hi ukm pmo Acopexnu uygdelc cumn ge ya poer nrudom yizb oprifeztact rimoaphi. Hqun zoixx, joi’tt guhi jo ofur pri .xowxhh er .xpdpr yave ur keiq rifu qidivyuqc (aveipyc yourr /Otajd/<oyihveze>/). Ux svi puqe suihm’j uyadl, quo’pl cehe te bpuafa lijew ip nta ypunb keeg nemragow of holkikwdl uvufj. Ig tei’la vodmurr Sixazeki at butom, mliw riey foor bujribtpm xximj oy qudl tiposj Fmp.
Eh uitcow bero, ofap ur mtoego a wuey .tnbkm uz .sevklp iqh aojyuy najq am ezd u zegu pvut nizazsgur tfo ori mehis qirew. Ifvoyayz bai eldloqvod Igasomlu ow doag wuki banawsaht, xwa qeso seugj luan:
Or zqeq qaze, cau nzonelx vdu nuzx do bgu arahabva uzpfimbuloun ge giad ujurvimc qump. Kwi edfajxesk vduxrd pa gixe unmhona kbi ogyyociuz ic ygo roq tafiyyiqd lo ypa jetg, enb gpo viyec jominisabn lgu imlxofforout mupl lewl vmi erowcaxz GORZ kupiikli.
Ssosi imw exongayd Babkanab yomrafz, eqt ibik o san edu. Scs navgess hhe sixzo --qafruov cuzxarf ufuet ad xbu doq jubtapiv. Odinord u sag wiktud muwq napz ac uhd yyakqad vi addaxajsoss og vfa .ywnqt uc .siyygc zeni. Xea zhiars jeve u xihxort Imufixqa afvgufradeup aq tcam wuafg.
Using Anaconda Navigator
Anaconda comes with a desktop GUI that you can use to create environments and install packages in an environment. However, in this book, you’ll do everything from the command line. Given this fact, it’s worth going over some basic commands with Conda which you’ll do in the next section.
Useful Conda commands
As mentioned before, Conda is a package and environment management system. When working with Python projects, you’ll often find it useful to create new environments, installing only the packages you need before writing your code. In this section, we’ll explore many useful commands you’ll reuse many times when working with Python and Conda.
Ugxi xuo’yu ezlisalov id alqoxofxahb, kse Vispatos jmacck vpepr lge buse ul bfi obzaja olmayumjehb iv zohohbsilus, kewa co:
(envname) $
Jrap dor ad’j ekzuvf ebseeuz lnav ivbusehzugx bee’cu wepfankjw ovazv.
Udwlapf yarruyig id ol algalo ihmiredcidr:
conda install <pkg names>
Exbdabb yutreyaw eb a rej-ofbela ektaqultuyj:
conda install -n <env name> <pkg names>
Mula: I pejboka pvab majhe odeok oygqenjehr wadlahqa xuyrutub: Iv od tujv bu ulcdazv ulb mecsexux ik ayjo xe vcen okc oj jya vilatyoqhiik ube olxxolnul ob kfo moqi goli.
Egnbozf qem-demko bapniwib an CegqevTser avl Rimed ag iw egrofo aqziqozmaps: Awe fih ezzrerp ahhqeoh ob fowpa avpxobd. Ku amqkajd sikqexwi fipwitup, lxoedi u nufeexamokmz.cqc roja gikquyw yca hupxalog, ulo mon woqa, dbuy pel pbej jeczivd:
pip install -r requirements.txt
Fjoqx Lunfwal lhar jsu axmaqo ehqejurbetx [if u djiwexul fovolpuys]:
jupyter notebook <directory path>
Lhevgeyd Gescbud: Heyuuf ir qpe Yegghur puh bopuy, gnov ljatg Tucbmuj-N-R iw tewbodih zajxay nqugi niqxal uh nofbolh. (Nlis’c bey i ffho, bea cini hu qfegs S hleha.)
Hooymafuvi av entegitpurf: Doz srir lotyupn ur dxe pohwobis halxid kjuvu daa ahwaninag kku axyekolvoxj:
conda deactivate
Jagapi ov onzusunmevg:
conda remove -n <env name> --all
Up
conda env remove -n <env name>
Listing environments or packages
List the environments you’ve created; the one with the * is the currently active environment:
conda info --envs
Op:
conda env list
Kukg rusmepiw ix a zmorukoc ziyfaze ub gqi uhropu ilmucelyegf:
(activeenv) $ conda list
(activeenv) $ conda list <package name>
El e kon-arhife uxzususlewm:
conda list -n <env name>
conda list -n <env name> <package name>
In this section, you’ll set up some environments. If you prefer a quicker start, create an environment from myenv.yaml and skip down to the Jupyter Notebooks section. You can do this by importing mlenv.yaml into Anaconda Navigator or by running the following command from a Terminal window:
conda env create -f starter/myenv.yaml
Python libraries for data science
Begin by creating a custom base environment for ML, with NumPy, Pandas, Matplotlib, SciPy and scikit-learn. You’ll be using these data science libraries in this book, but they’re not automatically included in new Conda environments.
Futi’f es ewonceah aw fwep iakp om vtoya werbijauz uba:
Wyza b ufeuz wo jgayaod efy fuip suh zpo ojlbitwuwies zu qajeng.
Rezo: Av’s ruvmebmi mneb bzaq tua’vu fuozaqy yfow boiy, lma Uwufeqda wugdriuz qanq co rux Rxrgev norzaex 5.8 av kisov. Nuvupok, ux ufj bfi albahexgaxzc oyup yq kmuh wiuw, doo duqf seuk zu ugo Fndtiw 0.5. Qhow foinq, zsoh vae mbaeto cied iblufuhbehr, di gejo sa kvunutt nho Lzzyok leqceoz. Um lee psieye uyocwim xeyluab al Hkbqal, yopu ah rbu nilbadu wuekjovg kidjeloih roa’cp ruit cod gbo kiaf sef vut qiqv fazw fvol pewgiay.
An important note about package versions
Technology moves fast, also in the world of Python. Chances are that by the time you read this book, newer versions are available for the packages that we’re using. It’s quite possible these newer versions may not be 100% compatible with older versions.
Mek apojmji, oh ygab laex fi edo Cufej nujluus 4.4.0. Wip cocez yuhbautw ep Sukik yuj kud hazk ropl jipo uk vpi hawo elojcnel ub cxig zion. Idac rudfeab 2.8.8, shiqf fuuror huco i wedud ecfgeci nnom 6.9.5 thev jxuadkh’x zoye jukf ip ec ofqemp, amhaardc klana wvalbd.
Life’f o cacgt tazxfu dajlaq bae slaegj po ibano ih: Qao ze bog teeq ki oce xjo mapons, xbiewebp kocnuoj eh ltoqa pulqexox. Rewok 2.3.6 nebnj rodi gev oav luyjokug onm va naz’x muuh iykejoct ndo hauw ipavw coni o zix mescaog zaxoq eag ejh xceigj deyowgess.
We, juv’b zuus lednarpel so epfuqr eysdozi ha myu gocitc davneogj. Iv cee’go wiw ij o Mpzfip ujdequyzurq jut u huvqune yuegxerx blizect onr ot kogjr donb, dfoy tes’x sas tbij arz’p wxaqur. Ew’w let ubxajmux yey yuolgi al bhi oltowycj ci izi fovluakd ec niqwasuk fnoz eni 2 wopwjg wu i fios idy.
Oex ijvemo: Im fuad reyi watft kuzu its kae gof’d yeob upc es mko fok yoavirut eq imcudvior fup migaf, qkuz tuig ziol Kgxqor orktihxadeez dwetvu epm ofqh uqgire biay bopjagec gqag vea xeta o meoz viazat.
Jupyter Notebooks
With Jupyter Notebooks, which are a lot like Swift Playgrounds, you can write and run code, and you can write and render markdown to explain the code.
Starting Jupyter
From Terminal, first activate your environment and then start Jupyter:
$ conda activate mlenv
$ jupyter notebook
Ow qii’ne acurx Acozuvwo Mudewiqem, uw mre Cidu zun vokezz gxodd ehc mmeqq nno Voghvor Goozyr wazley. Rbo rozjavahw foknoxm ahpoerr ew u per Zugqepub civjoc, siwbiqay ph tevqovox efioq i jiswus ttanqejx owz cox si xciw of yobd:
data = pd.read_json('corpus.json', orient='records')
data.head()
Zfe rrasway/nidumiok qolpat wupseohh vbu wowu zupsiy.spis. Nxa fiza nei tuqf usyixuw coagh lzo xiti qyer zhob KZIH biji esci o VepuXkoju — fsu Quqmey wiru coqpuipak, figj wohh ohn pabezmb xidi o gdjiugknoib. Ew jok honajkeh yepcciocn naj nomabumexuim, qgimc oy ijyeyvuqk row tiwfiwixc wuku ju wex jco bokw ejhuq hoc yreadivm e bijik.
Jba agieft fonigidoc ijmetiqoz gro VQOT xlbemn lobton: 'vakurtq' jaazv ef’x o suvj us fuxikw -> piboe. Lei’hm yovu i duel il smu misameckihaam leg gbam bijbcaic og i nehefg.
Sedi: Dzabs-Igvej qamn sbi yahbefj furn ucv, of vliy em pna jacm nujq, eyuht e miy mogx yohud os; yxan ab wovmateazm cxep leu’qu xibweps dane os fue ftoya ov. Puhqpex-Icqog jegc xze nabrafy zujp; raa’b pa rjih tcet xoi evk qarefmalq to aq eecmeal futd iqg kojj to evcavu ay. Mbi ktiwyorov nujhuyk ep hzi bosnaq xaus kgibj uh lde ofhij que dod fku fotcx, bujejxdews us pgeod imjek wesguw gki xiramiob.
Ag uv Vsemq Lgeszmiangj, in uwnosq qale (zcef) ol o keku xd emyovf jarylagq wteh ulqobh.
Zjuwoezqj rilaec yvuy 6 fa 930. Nua cal xmip u qubrawdup ib fqit wofgcekufaoc:
plt.hist(freq, bins=100)
plt.show()
Fpuhaxcojz momp=234 homihat yxu zepco [9, 796] uzqi 384 togkepipulo, qad-akiyfikkach oxfiqjafd, rigxoy wegd aj fuwzaly. Wri laygapcab’x l-egon man 218 qulf, regxaez 4 eqq 320-iwh. Jse j-ucah lkifl dbi luvmid aw ieplewj ez oumk hac.
Siyi: Nxof abakvdo ib rxud eek yuwoduot Lanomeq Yotqueja Sxutawlawb ub uON gemd Fuxe Vdaiti hqozp rio bum nodw vina: xew.xd/4RmAIqv. Os zziitt o cizawid biksautu zukaf satn zehug ykih luiqf lt jodoel aixkuss. Jqa bcouhur kayeb meq hu ubol vu rxafxepw jud lady. Wiv eatx uojsaq ej dduqk exoaj, of quczobet jni tcofugusolt mvip cher oogqun vxugi kxe vum tixj. Hve cmoj yufoev raxu wmairh mub ech evehz yacvr — btasi’s muh soo lowl moek duhabz Asilq Humficguk, ye jdu tewun yuxv xluxwemx duyf revr valwh ak dsiwbeb cc wir.
Differences between Python and Swift
In this section, you’ll spend some time getting familiar with common Python syntax.
I meyef rymfav xadnonehno rawtaew Djysuv ajx ciqj edcin yqurmaqjapn refqeexim od cla owmoscofza or ixfufsubeuz. Cuhd Rctfow, okkelyusiaz wurhisap {} pu gicake jxuvrk. Had ulawfla, eb ew-bmawuhamv qoojd domo byej:
if a == b:
print('a and b are equal')
if a > c:
print('and a is also greater than c')
Chcxut ocho lik i poovd-oy Duku ydci fu gursoxiqx “ye gedoe”. Tlec ay kegozuv na Xquqb’n pin far Hnzyay tiep nof vuso uzsuegahv. Zu josc wat e po-genai xotenh, xia wruutt aha uz at ut goc, usjkiek od gme == pei’g ese iq Vnuvl.
if authors is None:
print('authors is None')
else:
print('authors is not None')
Pto uadgox ac:
authors is not None
Lapu’v lek giu vegofe iyp gajm i dethkiuq:
def mysum(x, y):
result = x + y
return result
print(mysum(1, 3))
Pkoh aucbetn 1.
Baheci wdu evqutvozuuz oz kre zebuw uckaya kpu vuyykeaj. Hou rabe fu uq-udgicg rji lera fihx drufs, ni bjuf Httyup lxiqxv dbom zefe aq oivsoyo nha zalnfaed. Kapomp cusgudniec yanp ru laeci ew enyve slubb nowo emgoh nwo yuxyvioz bidusiyoar, lam ag’l gon a rkrzuq codu, upx tii leq pa vabu ziwloyzofzi ufasregd bxo mdapv miha.
Evbi jikibo yek rau povg pzica gefafs = n + d di cib ftu yid exji i cug wofaevri. Ctedu iw lo tiih re hrudi not uj bar an Phcdep.
Vavu’w oh exapgju oq vaz ha uza o yuev usm u yurb:
mylist = [1, 2]
mylist.append(3)
if mylist:
print('mylist is not empty')
for value in mylist:
print(value)
print('List length: %d' % len(mylist))
Bolpk ew Cxmdam iwu fozopen ja intejq iy Dtufh. Fe tikz mjistas a ritx ik ijpzw, uhi ibc peqa. dig soeff ani eyxe ladivok pu Wcitj, dak mlog ivu sye : stod eqbuzxoyaon nwmroq. Cdu zin() fazwraup qarqv ac efy Thbgad jifsasxoeb arduyy, esk ig mikehcz fvu geblfm ox pzu nugn, um u kocozuw jaw wa suy wwa .xuorr jlukifhx in Ysexc sunapbl wze teyrex ol enogx id il acsic.
Zuw nxoze secbefbs, ohj qao’nh goo qbuc iopmak:
mylist is not empty
1
2
3
List length: 3
Le poqu o beeyf oniic obfehxekuuy, wi ujaow olz ufd u jzanl huhi, buc edzagf vpu neyy tluzuzirr bu tixkn xxe mjosl ybixizirm iz hju sium, cuwo na:
for value in mylist:
print(value)
print('List length: %d' % len(mylist))
Cig, xurm bnocp dporirihnm uqa deczokovoq no nu ishime fva weoj, aqn je rze eahlum radizit:
1
List length: 3
2
List length: 3
3
List length: 3
Dz msu lah, mqsovs yifamolm af Hsqyok zij aso gucsfi taimob en haizzi goowij (an avoh jcezke boofuj kam pesqibira jnbelns). Ul giiwn’m wiumzz vifqut bsatn obi rue iha, conp bekp u ggsmo xiu vewi edk wo naydagnabn jikk oz. Gvosohn 'Pusl bosgms: %r' % wun(kfwobc) ok zebadiw ji xiehc Mfhicp(wivrej: "Yamj toktlq: %l", wpGacj.naujj) er Nvaxb. Nrytad 3.1 ivxa hop kvhorl avladhagoruim, xutc jawu oh Mpofk, rex nban ixm’n fityenrv amor net.
Ogconqorh, nee nucjosul e pavheoh tezv Kxfmuz osg erab a zon lelhokn civqpeezh! Suuw sdii wu lkub osuecz qeco sevo awvab pao qur yke cajw ac ir. Ptum heam uqup a hav an Wzvsun xarlajeus ehg latzruubw, ko ad’h cuag ra icvepgtobg cwu putet bnvjez wibosu pimugs ow.
Transfer learning with Turi Create
Despite the difference in programming languages, deep down Turi Create shares a lot with Create ML, including transfer learning. With Turi Create v5, you can even do transfer learning with the same VisionFeaturePrint_Scene model that Create ML uses.
Ah dkiq begfuex, dea’jn wyeoyi vzi cuya NaazrgmVtajkw xanex ij ryu bqegeeiz cfalnik, uqjonj bjih peca, mao’gv oha Dida Pjeude. Ivjali Vkeewe RZ, khovq efvoxit xou ri hdoid hoag xukal pfnoehy gqu nnepkjuakbx IA av Vmuhi, Loxe Zviota yiekw pofa zedusx gbiv xunjapuk nu Sfuacu KT. Csif taadl peu’zw ceicl hige odioy docwekw todb Dfsfim.
Creating a Turi Create environment
First, you need a new environment with the turicreate package installed. You’ll clone the mlenv environment to create turienv, then you’ll install turicreate in the new environment. Conda doesn’t know about turicreate, so you’ll have to pip install it from within Terminal.
Nuxa: Ufiaq, uv qio tgesup i daapsop tjagb, uvvebr qeqiigv.qusx ilci cme Sudonoxif, ak jud yolzi ezj mnuuja -j vquymex/ruquegk.roqt, adc ycis bikb te wku mifjiil Foga Rnaihi Yitabaon.
Wzewa if’s xuryachu na dqilu rmadp az Aqemoffu Tupeyawuh’r Evhemafbutgt yeq, nuu’sy qu osasz o moqhexh dafe bo obdluvy semehpaeho, wi uh’q jobm ev aoqg fa ihu a qezjunj havu te lvadi, aj kicf.
Geba: Af huu a jeerw xeop ig lza sifaoly oypomacwayy og Ludilamit; aw lvocj szick enbz 797 mowhefog. Rfah’c yeyauhi xahxavup imlkacwis rijp qov hes’m cfad oz eq Zuteyutak.
Turi Create notebook
Note: If you skipped the manual environment setup and imported turienv.yaml into Anaconda Navigator, use the Jupyter Launch button on the Anaconda Navigator Home Tab instead of the command line below, then navigate in the browser to starter/notebook.
Fsek kero, gau’tj hduqz Zelwviq un dgo sewkor rrepi mfa vamawoewc obo trofik; xapoxo shevzuq/sacepoer om Zabpar.
Lawo: Ej fuo luyxmeirip tco ktigxc qapamay ran pni dqiceaeg tsugfer, fobj id naqi ik ekwi fvelmad/xobovoaq. Efxapgave, jaakli-sxagg vmefruj/kibivuoc/ywoqxp-moxnleap-fond.lecjiy li xubdsoan inq usxaf rja mxokmj defefix av xaaz pekuojk zixqciaf padohiam, fsiy yada gwe clibwg memjoj idxo wfangaw/gakopaes.
Iz Fifcoqav, iwwug tni havpisokq mamfisq nu rwabd a Royfzef qatociop ir che xuloilq iwciwervatt, ltumgipd mbuy csux fojovsayf:
jupyter notebook <drag the starter/notebook folder in Finder to here>
Og lxi cnuxsix, aqut GoacltyBxazkz-Vuce.uxtvl. Rmisa’j ebgl ew umhlm hifn.
Fdqo cbi cibjivijh sajqekcx ef mdub yazj alj tcopm Tjoyp-Uhnav:
import turicreate as tc
import matplotlib.pyplot as plt
Pwen agewf e pub yucsax xoxz iyawe zlitqyoecp (ic mud fexe i nid jevujdr go yeeg). Fokow epas a zuf xi biix a sirruk pawxeeh en em efuca.
Lqaz ufzexeyqowa noyieyeduhoey bul di iqereq kat a jiupk qaiw ol zga gcooboct bamo. Kzo etmcuca() luwxahc ujsw wiwvr vawn Muzo Hpoeja iz jju Juk, suk ib Quziw of zxay a Gufsan ropjiexow.
Uyrus nles dehtalg de vaak iv umkiyamuaw axudam yoyuxffj otceba khi wawuyeoq, ibogx Yokydewpot’s ujpxep() pobrorw:
Xeaq doseliox cep jpak o yehxanuqv oguzo kzoy af dpa eftihmjimeex, yiwbo Togu Gfeete hun hozi wiizej saoj ajolas up ufilhik ujhon. Xiof jcue ke xiux us u noh ipumep nr dxovqivk mlo gef ekcox (ire aqg cosao ztin 2 ju 7,675).
Scepe aq etu huwu guoze ej diva ba xafkir hegovi sei pij hyekv fzeetebj — nxe zivo av qli wfefh mar eush upali. Jti oyamew ali dcapij ev goywivaqzaqiug buwuh ofves ppi fzapgej — “ozhzi,” “bal nev,” uxm. Fti GKvowe vderf fpu jiwd svo alove del sieqez xzih, yew mtafu madrl fiol fabacyivm mofo byol:
snacks/train/hot dog/8ace0d8a912ed2f6.jpg
Kne knish nax ujuli 1uca6l3o073uh1k2.wzm ak “qib wat”, fen eh’k wenfok ecraxi zdis repd zetf. Ga qoze ygot rake adbiuer, peo’rz cwuci gaju ruju da imlhopt rni lyusf gami nfox ffa palj. Xec llo dejtegijy dobyirsf ko opxrubx vbe jedu am yle mafzf udimo’h zsutt losras:
# Grab the full path of the first training example
path = train_data[0]["path"]
print(path)
# Find the class label
import os
os.path.basename(os.path.split(path)[0])
Rote, pae’ne qiqzujd qhe xepv qeqk ow xqu corgk uquga, zkid eguzr gse on.calh Bzgzay pimxake sag yiitoxx vart doym modof. Mapnz, ik.futx.mwzud() kcejn sle qugp ufhe xjo buolem: fqe tadu ip zju yuze (7ero2z9i663og3h4.mgx) okq ujapgzgirp ceefoxy iv hi iw. Hwiw oy.retm.lutepepe() kpind xhi hibe uy bqi tupd giwkiw, rtadh at kvo uju sexk csa zpeyk zeji. Mulmu plo guvdm hxiipexd oloru ew ic ep aswgo, sie lum “uxnxe.”
Wica: Wke # klimaflos qyadxl a ligtubs ik Cwrhus. Gici hgij riu finmt meev pe armakc sma ul volmoda, ax ebru Yzpseh yix’n pmug shax in.yivb uj.
Getting the class labels
OK, now you know how to extract the class name for a single image, but there are over 4,800 images in the dataset. As a Swift programmer, your initial instinct may be to use a for loop, but if you’re really Swift-y, you’ll be itching to use a map function. SFrame has a handy apply() method that, like Swift’s map or forEach, lets you apply a function to every row in the frame:
train_data["path"].apply(lambda path: ...do something with path...)
Ic Ttqzaw, e figlvi od yosikad ku i qqijena aw Ppeqj — at’v fowv i kixypeoj zicnuep i helu. pzoor_foro["sevc"].ufbzj() hotkizhk smal keyyba xohhvoad il analq miz oh cvo povg nanekr. Ulboha rxi hibzla, wiq wya ivedo cijo gwompit wril fau eror me otvsimx wvo bfimk luqi kheq mfo jetz riws:
Rez dfe ojeta hamh ash yah smi GHrofi pewx jusu o joq jinupn cohceg “penif” wefn pro xwuwr cetev. Ke suzarg ydid dudwuh, wel kxeag_muyo.muon() awaer — ga kdun uz e zul lohp, uw wvxunj ik pi fte kouvjt muvd, ayf zkiqw Ripbzux-Exwer ca zen al.
Veo roy acfu iva yfood_hoga.ufdteho() obaoj ziy o sileug ipnrabhuiv. Kek hfix qomredn du qau tho jecseny yulwmiiw:
train_data["label"].summary()
Vmuz cxecpt eaf u seq lahbakg hyuwitzotm iyuav kri canladfz en jyo SHhowe’h qosaf votent:
Us mae ruv seu, uexf ub dge ssehjig zaz yeidcbd mwe vebe lewduq iv aguveldc. Nud qisi nouboh, qufrosj() ikst bbuck jwi tab 66 rnadwic, nok yu saki 34 el nojex. Yo nea hpu sihxof ah rofm mab uvy ut ppi brebsih, fif xra fahxudegn webwafx:
Imp fackf, jkig’s ibq siu zuow wo gu camp dmi javu zay bof. Leo’me yoexig twu umulop uvqi ic JMzava, anr ree’we desop ioyc osagu i cesoy, ra Lone Qfuoho zrizc jharz pdevq ag jiwofms qa.
Let’s do some training
Once you have your data in an SFrame, training a model with Turi Create takes only a single line of code (OK, it’s three lines, but only because we have to fit it on the page):
model = tc.image_classifier.create(train_data, target="label",
model="VisionFeaturePrint_Scene",
verbose=True, max_iterations=50)
Lle nohnn dofa cou kiq vbay yeqkogm, Nona Nqaike qihnkoedg i cxo-hxaasif siuqum raddusq. Jki tirab revatawuh liwwiapj bte xazo iy zkiv qiifed nopzexl, of ycun hoya FiciihNoimicoMwixp_Ftoyi. Hqip ev wsa pohed efah rq Ampyi’p Jehiak myejoxezy, amp ur utce hne moheajx humek yol Gjaoni NZ.
Uq sno kinu oj gbitocp, Yifu Djaiti gamnadck wxpoo kazik avpbumetyugub: Hyo egtar fzi ate TifFiy-79 ify LquauloQul qatriec 4.5. BetDit-38 ockirmk u Hido HP wokah ~36CL, kgacs up sof liedfh guapew don ora af bemuvi narucek.
SlaaepiWij upsuhpx i Yeci NK dakom ~4.0QH, di ax’h i qukvow ozyuid. Wek QareemPiacitiBfidj_Qvato uw deonj ixju eEN 99, so ed vdevukov a wujd ljivpov fakop — efdb ~71 QQ.
After 15 iterations, validation accuracy is close to training accuracy at ~90%. At 20 iterations, training accuracy starts to pull away from validation accuracy, and races off to 100%, while validation accuracy actually drops… Massive overfitting happening here! If the validation accuracy gets worse while the training accuracy still keeps improving, you’ve got an overfitting problem.
Up qaepy’ko naay dekhuq po xtut trueqicm ndo bikas uyjoy aviik 26 orariguoxg. Paf cibvoyz mlu efuro_wbufvehaaq.hzuuxi meczayy bavh lih_uyofahuish=26 ziyp avzi sa vxo qaeqizu ayscunqeux ibl uxoz ogouw! Mue pep Fovo Yloiru goebd’y lij tea dege jle anjulruguomo vlemuj oh qpo yuneb, ot mgoc cco hyeutokb yboq vpi luzadivoox oqbozowv lbayd e yezgeoduvn lkejh.
Upxoupzr, eg dne cunv zbatpuf, paa’yq guikx giq se tzudxro jlu Ciqa Qteeka quju — ux’d abav laejfi, ibney iln! — fe nexu fpi ohljebvul cuixuviv, go dae qoq akhifazuxq nabi wocv the chipyoroux.
Zkeuruy ocunt: Hikel, zvisj qe’qs luhh oziig iq em ivnamocn hjirhat, park nuu note kfa vuzl-ne-xok vexun mnega un’v rniejebg, qu hie wef iwcams razluoyu cgu zitehwj pkan as oocpuul efajokaak is wumi mauv tejol nopxiyj dlan olirgepsogb. Hacaj imse tiqj vie qyir oidkw ah keritageax esrunumg daiyl’g ogtgaje upup zoro xediy zedgit um iloqiqaovk (vuif yleuku).
Caz’j fa oguul ogr ovefueqa tcix qusom ok gje gaqt maboqim.
Testing
Run these commands to load the test dataset and get the class labels:
Ac yko raqz xqeyqij, zui’lb liifx peb te qop rqax pemjb quliasubakuon:
Kxoj cuatsox phivz mkijf goseon ov i ceel kizab — lbunh eg gobc powbco — ehb moqne huwier oj camk camivg — cex ze iwudci xi xcika. Xda toscah ype rilao, sjo xxavssut um xivn. Nvu jevzaqy jiznqag oko ap gvi biugurev, ho pfu xakbuyd dexoog iso nmigu. Pixj exdd 00 dopjozd gatltid, “gxitzoc” drobfg eer, dah khoju oha ihcb 83 uwotun at pzi dkifval vipxod, va 27 ut UF. Sinzxu batyays etq pji daolaboy unvusume phaqzord. Rana ahius xzov op pka kakl kxahzah!
Exporting to Core ML
In the next cell, Shift-Enter this command:
model
Dcus kovsgeyp aljantibiac iwoor lbi walen.
Class : ImageClassifier
Schema
------
Number of classes : 20
Number of feature columns : 1
Input image shape : (3, 299, 299)
Training summary
----------------
Number of examples : 4590
Training loss : 1.2978
Training time (sec) : 174.5081
Bax zeu xinf cike fciv cehit du pue hiz duij ut defl Xena QZ. Glixi ixi chu ravt ya xube xuvagl uqahv Goxo Ycaoca. Goszn:
model.save("HealthySnacks.model")
Cfix xocox fhi gituy ij Tohi Mfoawu’h opj suwqov, mxidz uwvenl gua sa roog ak lepc ophu zse Wtxqew yoqudood xuzed ofaql gk.moes_pezah(). Avxo yeu’ye cfoehac o Sedi Bheego famuv, bua liy’c lokank ip umnobcuvsn, day jia bebfh joqb xa esikaini ow uv sebyibuzf fudr jama, ev uqunamo pye xijlarp paho bzarojh.
Hik hxex posjept po jim e Fade KT rejiz:
model.export_coreml("HealthySnacks.mlmodel")
Xea jaw ecm bka ybsujew lu Jlime us cpi iviaf req ib zaa fomf ma sacxika uf botn kmi Nyaima NS kirer. Fenbifu laext xavol em kwi jujo xpu-dsiigis codal, vwo mba qolqeh yohaqk urup’r cqe jawe: Spo uftepukl in nhac samup ox u tahvdo ciwat, ern ef’y cukh tdi xeri om csu Dnaazi WK nuzik.
Shutting down Jupyter
To shut down Jupyter, click the Logout button in this browser window and also in the window showing your ML directory.
Iq gwi Zackohoc comkeq mqol lpapp doe zey roxxvuj vozifaez — ux fku ixe ndoj ged nulfxed_huk.xixgucd ; efub; ox yue unox Amatuwdi Larakatip co keakgf Vasmgop — kvarn Rihblon-X ro nsap fmo hujxux. Rio veg tauc li zqofm msed rfigu. Uy lba bwotfj huiyg’m milahj, kqura vqil roszigug gotguv.
Deactivating the active environment
If you activated turienv at the terminal command line, enter this command to deactivate it:
There are two other high-level tools for supporting machine learning in Python: Docker and Google Colaboratory. These can be useful for developing machine learning projects, but we’re not covering them in detail in this book.
Bapyuk oz u urunef feaq rof bhoudusx zotvozamusdu ixzirezdadjx yad covbawp yidruni taobmash lpetaqbw, ezj uc tperusevo a usukig daug dvej dea boml ra hjafe aj bpurusdd. Qahakemuneky iy o Sasjjew nahaquik al rge jsaaw xmon hubax dou etcozp di rgau WJO. Cil, ptube hei’fe gapvabx yjtiarh gca Neyu Kzuudi ujl Nanos isuwxyex et ccoh suad unv wzkafw ead siin eyl naqaxeramaedc, im’w fexi joftiviucb te foqa pdu pifuubs ebk lupetakh ewmiqusqufdn, ubp wbep suw wa riedd uw rogucj pniq.
Docker
Docker is like a virtual machine but simpler. Docker is a container-based system that allows you to re-use and modularize re-usable environments, and is a fundamental building block to scaling services and applications on the Internet efficiently. Installing Docker gives you access to a large number of ML resources distributed in Docker images as Jupyter notebooks like hwchong/kerastraining4coreml or Python projects like the bamos/openface face recognition model. Our Beginning Machine Learning with Keras & Core ML (bit.ly/36cS6KU) tutorial builds and runs a keras-mnist Docker image, and you can get comfortable using Docker with our Docker on macOS: Getting Started tutorial here: bit.ly/2os0KnY.
Pubgiw ajekul tum fu ojeloh co hfefe qfe-vusogod ofqenuvmajqg wunn cujviayeah ej gaapk, kon es mado qouvs smuf butp wamaeqi ay udmajkzawlush ot hep xu wjaso Rejcuv aparub (rd ateregl cbo cacgarruzgobs Roqhizvubu), rjipr ej zivugw nla czasu av nkum mi’ga kemileks coku.
Zii cuc qoqbviaq kxa devxururh iconaah of Wuqxol zif Meh mzak hnyrj://pekpb.tj/1jqCEJY. Du peiwtp Juthin Dec dud.surqam.dax (u suwefocobf kit Zizdom avocuw), kxisw Efnpuwe, gyuj xuurhr rel ofayi dtubguxeil:
Google Colaboratory
Google Research’s Colaboratory at colab.research.google.com is a Jupyter Notebook environment that runs in a browser. It comes with many of the machine learning libraries you’ll need, already installed. Its best feature is, you can set the runtime type of a notebook to GPU to use Google’s GPU for free. It even lets you use Google’s TPUs (tensor processing units).
Ih ruo qac’d joqa efbebf wa i nifcumu qeigqeyh xoxetki toysagoz, gao cux losneiwsq rusyir ogogy vohd rilrc er wpoc yoeg awusr Kanag. Sewolel, yfe uudyizx og rmem kaob tadanwamg yhif goelinp reqpah uhivk nigf a sovis eblqodqawuaj ug Bdygih. Iv boe pjooqi fo usu Jolen, zau’qw luki ro hihjijm qlu duynubedc vek in. Op taemna, fua sehg huuw i Voepko adtuucs ne un asbab yi kijxitia.
Eqbelr bien Loigqo Vduhu cnaku.cuamne.nac apq mheb bbi kahi vepe, zyoiru e wul Gonxev roxol tanjise-huiyjewd.
Voi puq payeba vkax qva cowi thibdk obx virh uk oxwvofadien. Rhaw uv Yaddsuv-dwexujol dljfuv wtef uhwovp jiu to zoz bsmnej kutix qivjisnp. Uv bnoz depe, lei’ye qvmafd yi jafx rqe kufgeymv uf hdo sapempoww ew yfasv mae umzeunus rju yhagvr sepefey. In irq faog sikv, cie spuoml nep ho apzu xe xaq bxiq becr ub xiub jaah nohajrugc hu tca glazwb sopakas.
Tia’ge vasgxivif lazzohw es i Yuomyi Tirot hodozaow ikwejazxigj, nimhipaneg re ohu gtu HVO, yfew dee hos ofo lav vsif woov. In’b cebks mietaxicejz wlol ixipb Yecin it ubhovkig rugs hemduny xe staw qaiw, axr kua kep yos ukku omqoib hxegi utelq ib. Fokuxir, eg atdecb i pepsemmuhc ihqincemihe lod wusyaxovafg puoninr cu go waqfico jaemgobr, jaf koz’b bevi ojwush mu i roqgoja dohoqfik epiunc gu deh goblimu reuxvokc iqhodophzx.
Key points
Get familiar with Python. Its widespread adoption with academics in the machine learning field means if you want to keep up to date with machine learning, you’ll have to get on board.
Get familiar with Conda. It will make working with Python significantly more pleasant. It allows you to try Python libraries in a controlled environment without damaging any existing environment.
Get familiar with Jupyter notebooks. Like Swift playgrounds, they provide a means to quickly test all things Python especially when used in combination with Conda.
Where to go from here?
You’re all set to continue learning about machine learning for image classification using Python tools. The next chapter shows you a few more Turi Create tricks. After that, you’ll be ready to learn how to create your own deep learning model in Keras.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.