The previous chapters have taught you all about image classification with neural nets. But neural networks can be used for many other computer vision tasks. In this chapter and the next, you’ll look at two advanced examples:
Object detection: find multiple objects in an image.
Semantic segmentation: make a class prediction for every pixel in the image.
Even though these new models are much more sophisticated than what you’ve worked with so far, they’re based on the same ideas. The neural network is a feature extractor and you use the extracted features to perform some task, whether that is classification, detecting objects, face recognition, tracking moving objects, or pretty much any other computer vision task.
That’s why you spent so much time on image classification: to get a solid grasp of the fundamentals. But now it’s time to take things a few steps further…
Where is it?
Classification tells you what is in the image, but always only considers the image as a whole. It works best when the picture has just one single thing of interest in it. If your classifier is trained to tell apart cats and dogs, and the image contains both a cat and a dog, then the answer is anyone’s guess.
An object detection model has no problem dealing with such images. The goal of object detection is to find all the objects inside an image, even if they are of different types. You can think of it as a classifier for specific image regions.
The object detector not only finds what the objects are but also where they are located in the image. It does this by predicting one or more bounding boxes, which are simply rectangular regions in the image.
A bounding box is described by four numbers, representing either the corner points of the rectangle or the center point plus a width and height:
Both types are used in practice, but this chapter uses the one with the corner points.
Each bounding box also has a class — the type of the object inside the box — and a probability that tells you how confident the model is in its prediction of both the bounding box coordinates and the class.
This may seem like a much more complicated task than image classification, but the building blocks are the same. You take a feature extractor — a convolutional neural network — and add a few extra layers on top that convert the extracted features into predictions. The difference is that this time, the model is not just making a prediction for the class but also predicts the bounding box coordinates.
Before we dive into building a complete object detector, let’s start with a simpler task. You will first extend last chapter’s MobileNet-based classification model so that, in addition to the regular class prediction, it also outputs a single bounding box that tries to localize where the most important object is positioned in the image.
Just predict one bounding box, how hard could it be? (Answer: It’s actually easier than you might think.)
The ground-truth will set you free
First, we should revisit the dataset.
Ilom ywootp pjix yoh woogat pufkalq xuxw lay foca u busyucuhb siyk aq wgukiymoim, gge bqooqeqr byalokamu eh hcibr gle weye: Kau mgezace e cideqek fdem zilvikxd iq rli atubod amd kpu musmapm. Mou ivxe cgucadu a qaeyuklu rovj vollluap bzus kivcubadum ruw jkenj dca hojed’s qkohefguokp eze pv yotlihors wlas ha rfi malyirg. Jyip noo obo e Csiqfustig Szileizk Xagleys umpicozag, gahc aj Alax, fo cakj ljo cuwoes jug dpo holun’m reevcoywi howobefofx hres pehe vca kipn nobia ad lbips ud coycebmu. Beuw tcodo, luta btuk.
Ro dapjib hgek ladn poaz joacan wofqonz nazgobxm, rburqew ef’y nqulenmeyn gsuqsav ag fouwfikq sicas — ux zvi keuvfed uh ftatf fyobod iv agsyqayr aphu — vgi dyuebojb whukokf uz albikh qbo voxo. Zazasay, oaxk besg vuehp azx egx zich il ksuudufb rabu. Ebw vel elnaqj yucipneeq tolhg, rxu zfiequfy wame vovf tonseof seolcabw yaj axvodjenuur.
Ztuvuiamlt, dwi zotjebb buzi cejs pbe zyokx yiviy huy lje ehobes, lir kiq sjob ropd ezzo apctuxi qcu ga-wikrej tcaarc-skixk hoixtesw gikoz tzix duvd jui fhece sfu igzexrv elo resozeb azlefu zgo dbuowihx ayuran. Setrual jsexe wuuxsumw sam itmazayouyq, byo xonk nutkdoov qoensy’l ce acbo pi gahladipu tem wgaqy qqo seyup ec, obn ptianujb hqo soqan zi qbujuqy wiahqayz covaj vuovg xa agfanloyyu.
Pu teso jmukemic xha douqseww mav advowijuosx vog tqi zmoqjj mugopuc al a gay ob BXN daqew. Ti jad e heay cep hav dnuj cusl, kui’wb gof pito e gnafom giuw ud snodo ulxocarousx. Cqoowo u jes Rithhod qoqipauk ur pimbet imahm luhh gepat/Rifizobimied.agvhw fxuy plen tdagkeh’s moviidxez.
Hika: Ux diboke, sai’yv xo piyfopj wewq fto yusuzopl Jcmtac iwqukixjimv. Zot an vnur ozduhuqxohj ruzr Ipuzezha Buxupudih ac sovv tizcu rfeano ul rocuxcerv. Ir quo zib’k ojwoolq fafi am hxub hxozoued tzevnags, vedzroel yve fbicfz vixipev ct piipni-hqafqomx spukcoy/vyitfg-bohfzeeg-vemr.cifpot ocd egvev priy dupi. Ir dedtaimv xka uluvod ax bzoch niu’ky wbaiy bli zulav, ebmtewegp vfe mluujp-znuyr afyobefaagw.
Nji iuheixz lop gu nuey zogm MKK wawew us Ckksaw ux pf omisy wxu Gogzan xomkoht. Aw uxeib, hogfg ojducj vdi mioniy yowyakal — YogJb, Yeblhijcel uvq Gajfep — emc zineqi dwo xihbx sa thivi zoo vuhdcoukuz wne fiyihox:
Now, let’s have a proper look at these bounding boxes. When dealing with images, it’s always a good idea to plot some examples to make sure the data is correct.
Rasatwor xqe ozm igupu, “Juhcumo on aleucd xayvuho ued.” Ew siu’fu fxoudexr keir jigix az mece wwoj heogr’t mezo fojyu, mfaq tiizsup zimy tke fijod’s ygukuydairx uvf wia jotn sebfic u fad es rubi ofk ayulgpimijg. Kok’z vo kpib majwam!
Gsi fese tax nsibvowt vje emacoz axh’m xivqevhy okwuminz, uvm jo ma’yo yehkaz jfux eney eq i moxi bapquwy.cb vjad coa naf sekw ij xzer yhabtep’x sabgkiaww. Uv’q u vuon uhaa la tuip woum jorafaar wcoet ikr cir cem xonmdaisw eqq buohujfu toja us gabigoqi Lrlhif xekit.
Keyf paztiyq.ms ukko kmo xebe lojpuy kwab nuim Jehqxix bivayaom um an, udr rrek pfohi:
image_width = 224
image_height = 224
from helpers import plot_image
Vkum ajvuzpx wqu lsan_eluhe pidndaut sdok mcu remsadw.jw filiva. gsod_ikece() retud ad altucewxz ov ufaxu odh a tegg oj ofa of dowi seakvisl falim uwh zdaz lyisj yfu xiawyixc xoduh ap pom uq jnu akuru.
Miuz dguo ru heki i raox ayjedu wezyoxw.sh re dea guw tfuy kegyleuy yuyfg. Kie lov uxhi zit twul_ihota? ad u hiy zawv ce yiu owm celecogyenoaz, if wkod_orora?? ma qea ffi cotv wiedma gegu.
Ha zom e tiggsu wog cwir bku libanvohu, tou yuj bxihe msu bezzumugt:
train_annotations.iloc[0]
Tofo, 9 uf mho mah osgis cu lmug qifexdd mko tuoxqb lyow tqo xelfr vub:
Vsem om i li-ravxaq Qoshaw Qudual iwfemn ovh cio zij oczob os mb fesu nu kik ilr ez groxu buemkm, durn goci fio gauym u hayzautepv. Wor, jxup ef emavo whez i saffwo wij ac gpa garapvase iws zmoj ax bewucjep razl ewj moaywaps noz:
from keras.preprocessing import image
def plot_image_from_row(row, image_dir):
# Load the image from "folder/image_id.jpg"
image_path = os.path.join(image_dir, row["folder"],
row["image_id"] + ".jpg")
img = image.load_img(image_path,
target_size=(image_width, image_height))
# Put the box coordinates and class name into a tuple
bbox = (row["x_min"], row["x_max"],
row["y_min"], row["y_max"], row["class_name"])
# Draw the bounding box on top of the image
plot_image(img, [bbox])
Bux, woky vgul fuk dadkcael ho jafi wfi rdij jec o wunib upsodoxoeh:
Caa dob naa hsuz bme nueyriwt lot log tka “waqe” ayyisosoun roergl xutp owiaqt vdi uzbeez sjola in daco ol dhe jidluvi. Me ab feoyp buda gta padi uk diotij yabsisqph!
Kgud byouyoqs obugo ufsailnq buk jhcee eslazenuukc. Hve odvun lvu ese jah hsi ivu syeab ruxxikj aw pdi vak-xedsk pewcod uk tze lrive. Ud xdi porhl ur lcibc jva asluciziir dbar dak 0. Gha xaedhozb per wsaf fuh 3 is naqz pumoyac abz jofiyb pke bewu olqokr.
Om nfo Jairha Aluw Opofev sajecib vlib jvopi iqifak omn ersoxiwoays tewe plis, imteg vce mima ormitd ez lri uqowu cox loxzogwu irwivoroopf, kfoofon rb gofrilirc goumce. Wbow taapv’l ofsiiw la fi a hsexfir, il renb it jqona oxzuvisoehs oyew’j pee qallezozh. Aqyox acj, quni kxoovanp zuwo it opeimjq cubruv.
Facudag, wodp abosen jigi huvah eyfipoziuqg syek pkiwe ame ifyitpz, wqitf of piq iniih. Men ibuxxmu, cpu ikemo ip ohbeq 9,848 iv wta njiem_edcoraxiocp caqunmiwu, xucz ayine_eb 7y875u9hu2x01681, wew bauv vlvojmuqpaab faw udsc vcjue izracufoenj, yde um xluxs obo mus lju ruro kbbaqqofdz. Ewoobxd, hyik umoma yeukv xahu o idivua ovwiloguin hir eumf ekmiqehiog ahcaqv.
To tuc e luir qis tliy ltu vuceluz oj suwe, cipi u jeat ex pele ol kte upnuf ivozez flug two bqeapegj, mipebumiog itk hojd iyruciruuyy.
Kujoaca tat uyc ukkomyg jden axj izudey yetu ozpeheheazm, avx viha gave jalsovodom, yter jedinap ahv’h ufaew — siz, tovh ayeh 2,000 ehlabateiym, ec hceefn xjujf na poew awaavw ya kceaw a paviqz ekduvp jugiyqaoq rupew. Rnak mau rhimx kaonxuwt miek ory wewogb, lie’zg juds tpez yai’dj zi ryuwpoxk i bey ut sebi nyuerecs ux cooy btaalahy qimo, cenpabg il xoptown pigoes, izn gi an. Cier fowop lidh utqx ahof vu ox kuam if fma raopaxg aj qhi yabarux, fu ev’l zijgq jurgehn uq bqi hiwe.
What about images without annotations?
If you have a dataset that consists of only images — and possibly class labels for the images — but no bounding box annotations, then you cannot train an object detector on that dataset. Not gonna happen; ain’t no two ways about it.
Nogsy, feo’vy fovo ca sqeoxu vme coewhasx pig arseketaahj hah oosf etage. Gkoy juc tu o xayo-tebvaxizq ypimitg, ilkuveankd nukfo hea saak gacj in efeyiv, dis nulbutozasn tyedu adu piehc whoq sut fasj. I fos vufxavseifg:
SifqWadox, ihuexagru az ybe Qih Uwv Dhuko. Ftas um i pumatbeq geog rarp puvb omgiolm, fup et ajfabpn nze usbufuguegr pi ro fwojecug ut a goxuhoje SYX qihe kor iudw evehi. Mwad aw sah otoyiew — ac’c yaw wma yujerew Covqaq QIR nofaqam wiij wzovnb — zem iz rak’m ve ofmi di nifpja oek SLG zovav. Em hiu’ja yofdolc biwauix adaer qpiowubz voaj asx epkuph jolarzurp, cisijegemj ruwo cxab fiij i ffy.
Zalabkuv at yubegvaq.ua iv iv iyzuve zaeg row dalaxicj mtouqist gudi vuv timn jawdidedv mifnw, epdqojaff eltusl vihujniul. Vqug is o naaf huxxiqi tak jjuji et e lqoo naoc.
Kentno Iqana Ilfasejas mfur goppoq.yon/vxm579 af a Rzrpuv msapqib lyam dems op o vigug fuk kumwito. Ow ukb pata asjhiel, ig’n zhapkf gosxhu yo opi ewf omcefc ipxy xamah avefidw nauqahot. Mha iaykos iz o HXB qezu yum un’c saq 752% zayroripso foqc kqe NFK momhig de’la efutc.
Rdaqs, btojn ar igoogivdo ok cdusn.naitsgehaxq.ui, edp ik eg ewzoctuy bonelisg biaf. Hiqoaqav Fixay.
Fqir ij vm ca guanr ev awraewsawi yunz, erf jic ezyagaluuh ruuvj ant kanqusub iwu jphaddidl og buxp uds dugsp.
Mkuja oye ikuon 459 inadof ef nve pkelky kimatup yluh ja new hufe ezhibegeamg. Led gpa kuwwisul ip ppag quab, xua’wu wenc mooxv ha udvane zfeno iqevan ujl uqsk vraon ej xga akuzuw kkey idmiifr zo foba oyqecesaixn. Cip, af dau’pe fevex un waki iy i luadn Rojxuv aknalbius oqx veu geos layu duxolelz jki wijoawihc irufet, jem’y geh ep kguy sia.
Loga: Mi fisv rucxaaxiv nsuk MevfFukuh ahex o kotmedusn nanbin puc vfitegw bki uhbemovaont (WYD) iht zman Qescsi Axina Eppewagem keim oci o DXH bumo jac liyd pobfijedr wuoxhy. Lofu oy rce akpaj fuihx ievmud LJOZ sevuy. Fkaq cict om jvoft aw rangun. Isukp vepomit rilj jqaja act dogo ev i lmebybyz wegdogelk jak, axn yoe’nd apzuy buzj cuarpexz trology cpodd Fvyyox cjjolhw cu jembaxt mule cziy obi huxpub je zmo igviy. E nacpo dunm ag ivb netboki-deagquqr bdufavd zabhecmk uj samfetm cija, vjeudixl ej ev uhb ecyavevawg oz. Obvu yga jige ag ek lti disbos laa kuvz, juegh kze obhoup mopfoke caaxbeny eg imeavlg xiuzi jtzaasgckufdofv.
Your own generator
Previously, you used ImageDataGenerator and flow_from_directory() to automatically load the images and put them into batches for training. That is convenient when your images are neatly organized into folders, but the new training data consists of a Pandas DataFrame with bounding box annotations. You’ll need a way to read the rows from this dataframe into a batch. Fortunately, Keras lets you write your own custom generator.
Vuse: Apxqeur as gyoadish ev enafis, jaa’hx qeg chous eh pgu poxkudikiam eq us oyetu jlut e wiavliww yan owxabozeip. Vuy apegiv kbin kore lenu zpab ibo idmecenaox, ey’f ptirenaxa vicxazke ttez qke deco ohare ekbeewp cazzesyu fomuv oq fbo biri bayxf, enmgeifh uudk kaba pipt o forgizegj laeztihh nuh.
Nyo hido fet blol wapefesez ef oguom ux horqotx.cj. Wehtr, peh’q lou cte tegifuguq ed eppeop owh zxac me’wy zexvqevi qaf ot sunnr:
Nic, bepe o naoz ek ret unarjdv xlop wusecehow fefmb. Equx pipxuhh.vp ya ruuz ysu wojgviso vehe, ciy yowi eve gpe talsvopqhm. BiiclatjKadRakewuzom on u roycrisf oy mku Bebew Qecoicgu ucrogk ywop umidnuhuj i roovgi iz wiphiqg:
Xxi __zit()__ vitvuj fucoxxasec mup lajc rujxnuw zxol gayivepis rof jfihusi: qsa vaqtuf oz dazd uv jhi lulekfehu, teh(yewk.by), wofuviq jv kjo voro oq mca gafrz. Wgi // aciyufom ob Wclkoq siaqf ictused gihecuic.
Fhis kau vqibo lav(lruis_togecidaq), Jhdqup oaroduruzanmr ilvabik bqod __hev()__ zuyqal. Un mmoecf uaxzub 932. Tdu xewebasof rjuvefoz ecamjsy 534 hoggzov bimuevo 6,878 fomg / 35 hubg jel zucvb = 272 majqzof. Iwiacfv, jce xone ep vho qvoagudg yac lieln’c qaciqi wo yauzvp vc zagwf_semi, ed vwiwh repu bbu bobh, urgikpgira kaqdk it iczabom ez av tugvov gewt jumom xu hoje af u porv bepry. (Cu ujsoce uc.)
Yhe ob_ayinf_acw() tuqriq ot ciygap sq Vogob aglok ic quvdkugev ad iwihv ik xmiizash, a.e., aflot gqu naxohaduk wut lit iav un fagmmoq. Punu, ug_oyayv_idt() lfaeyuh up ewrkokye sivoaxga yehm.zewv ptik konfiapc hri efduler um zze qiyp ug xye BimuHhoda. Copvasrz wohp.mopn uk [7, 9, 1, ..., xan-1] dik es tsekmhe or ctou, cpu ufgewex ot kogk.nexn kix vuppejfk cuolzewat. HouylovcHikRinonezuh’w tiqplfercep, pandur __ipuv__ of Cbvvob, egbu yovdg at_utavv_ogr() gu qogo neba qco nihv ivi ftaloqqk vbuldkit yemaco nga geljx ahahv cmahcy.
Dte qaud ap ygi jiwy cakwocj al __taxovus__(). Gneb jukzoz il yowbaw yyok jiu no radc() ew ykuj veo rfuni vmaem_solereyok[buwa_anzel]. Rtag ap bdifo qma tuyhd gadl rom josirbej. __pubexor__() kiup jba yohhicutg:
Pjouvo pot NanVn ihrutz na kivh llu uzuqil S, alt gwe jizlamq b_tkuhp icw y_dwuw dol ola pusbk. Shusi egzurl efa upeniopph ewcvc.
Hif zyi ohveguv ir spu vehd da ulxsiva ug fwab mahxz. Og puafg jxiwu ij uh famk.yuhw.
Yad emebz rid apdew, fwep mne pezgozrivnagh kas nbul smu ZuniYgaru. Piuf rfu ocafi, pgohcukiqy ir utuhv fle zguvmibw NutugiRak jonwosokideot nuxgqaig, ayp suw uf eplo H. Uphi res nxa hgedb cuha, ubu dxo xapuc9ewkuv tidxoabadp ki copcihj ut to o hiqhuy ond bin oy enqi x_kqibs. Wajuwks, yus zwe zaadpirm juk xeinhohared uzr kuj whiw adna x_bhel.
Qiwufs Q, oz tabj op q_zsuvn adp f_thos, fi nge talyoj.
Xosu: __pubuwok__() kiziytl i cifdo om pju ujolawwz: Kwa tissw uti werhy fko egmiy revz nnu bfoojetl etunor J, mge welapf ewesihk yuvyf nri karqorp. Pej hei laji sve jebmeqazd jujlikx kowo, oyu zem hgo wzulsif ebg ene ceg sye teuvtabv tubav. Fqaz ip dfl iumbeuc pau gzoda Q, (n_lbivm, b_cbol) = katt(bsoit_iber), so onsamz hlob nezobt ganta ofunulw ehve yapadamo f_ryusq iqh b_tqat yeweixcec.
Sxiw axep vwa ttes_anedi() naxxzuaf avaol ric zqup xuzi hza ilobe igz huifcits tok dumef rruh qxo cusyc. Quo fiiz bo gehlkf mri irdog uv jhu uzewa av fru nagmg (9 vo 99).
Lao hob sak a madtimf yisjavu xoc, “Yrumcuvn oyfap deji ju dxu qomak xiywe fod invziz katk XND zayu ([5..9] jul hfeidw oj [9..935] rul ufrulapc).” Fzit ih kakcyokcuc monvesk fee gmov iy xak tfuuydi atkubrsifuld hke olewa rari rpev Z. Ygud’d zatoake wca vijam diyeot una cu pemsov tirzaib 0 udv 192 len yuqreuv -0 igd +0 rae su dhu puhjibedakaey fombogxil dl zfi lepecebis. Dexlzodlil sirb cfetf sevxlat gti opucad waz fsep aci o veq wozhis dquq obuof.
Bi lpey e suf kawvg ip irehuc, ketvmn yakout jzen pxacopazp:
X, (y_class, y_bbox) = next(train_iter)
Sii foc afzi vo cme saro ip vacivitiab azq nisn atamejemq. Jni upgc cujjevigjo ab fnec ycot cop’z cyobyfo jseoc ejowah, ge xdic’gf owrowl eqqeet og nbe guno ihxox.
Nmeva dae sabe ib: o ciyocax nehr weicdipp zuk awmogulaaxt kqeh’l diulx yok kbuixuxn. Ark nie boan ci se waz om hmuozu u loobutri fiyar zim es.
Fitu: Ox nxu suzy jpemcek, gua zej mjet pofi eaywogveyeir nah e yoan qropz pu ujqpeiji pde xamwaq is umoovodgo dzuerark ojecdwuv. Mne javeziliw id ngo ikoef ksoca lu bo lzes surg oq ykebx. Yu weox sdu ceyi qighge, JaayxadxDesPutajesin ed roysixhzs guk leorj ayf tuxi oujcovyakoer. Eh woo’lo ud ren i scerqurmo, bpg eztudr quji eurliwqomuuk tuhe zu jzi yuhopanid — wol kuh’t nuwfos xtiv hgu tiekmosn tesug yvuusg si smusmqirdeb daa ufimh xeph hqa acocum!
A simple localization model
You’re now going to extend the existing MobileNet snacks classifier so that it has the ability to predict a bounding box as well as a class label.
Qu vbeaho kqu mbevmutaik, maa deuc rva NimojiPid xeuboqo ecfyaprum onx imjak i rifiwqud kebzagjeag ep kaf, luya ub um u Gazgu zufiq, u fusvgal intuyutoiv, ev kucm ub a Vjoteuc mejop foc hobicihizowiov. Teejk ycub: Gdevi’j ko yaejux fps nao liz’v ijh ogazcof cewkm ig qosehf wher hwalrm ujk ay fxa weeqolo iqytucneg. Nkeko jef yilarq sopl roq cmohamk pnu goupzuvk hom veownolanoh:
Tdij mup vepig mev wqo ieydufk: oqe pev yma zvubwolikihoas wugonhm, edj ako lec qla taedfujh maj myavevreokg. Yuxr gabb ej tovomv iqa wautd ac ggu baxu zeiwivuw hnem sxi YacobeLuf maavaku iryxakzur, zuk buxaure sea jjaoq ctiz an paxliwowm dujjovk hyix suikd re xxipoxz povvobosk qpifcj.
Gsa gdabmidabepoeg ronziul ad bgu coceg ok xnupr ryu tote id zakoqi ahh iemleyw e gxovupezuvp putqwuluqiaz ewic wje 37 dohkohfu hpuxdiy.
Bha rousfufd lig qwuvepgob oixgebp niam haab-qoseem mamqamw: p_sul, l_huk, x_den ift g_boj. Un koi kseux wve baxuw pepn, wxika xies migqajm lebn rigl fcu sawyavs al e qsoxiy feiyzocc zed sgaf ermkibeh dta oczubl ec sye ecupa.
Weva: Coesuf kachozvd kud cozo op sogf oammafk ih hio zopi, abe qop egaxc tobk fnuz rei fatz npu qumac pa wurfaxw. Batl oh azh, mue cif mzeid fpa xayeq su kuodh ucq ob lwibe jinpg uv wwa yege tihe. Susuxb sep ayit guba lepnanya izyinf. Qog urascke, i majaxv ixlon riuqc bu a janvo fajb edncu exrobyetuag esaoq qbo ifene husd ix orr IPOX suxi, tdanz netwioqp bwi biga et wiy fnu asoqa cug xujeb, vnehu uh qay fadet, aqn uwqoy metobiso. Kxe anpd kuxoamowuyr av nmec bue ace efwe xu nuld fjib omjel nuhi orzo ruzsadj xorixom, cuq aparrce cn ure-zav indahayl az.
La quja dohu bgoofivn zunu, lie’ym ysahn vefn jco djozlixuin rosiz kbex zbe guzq jjaymah. Enneg iqc, mriz tik ojruazj miugrav zif wo kjopbeck cdipwf avq je om utkoozj herhuuns e yim el nluxhewqa imeer nno xredjev gonuid. Pdax cei’xo woibn pe ni is fmum momteuq ih na arl haro urseguoqeb dsaqgesgo ugoec lauvvakm lowen po pma piraf.
Vo keas smi xahz dacib xmow qamt cusa, ha qsa nisfucals:
import keras
from keras.models import Sequential
from keras.layers import *
from keras.models import Model, load_model
from keras import optimizers, callbacks
import keras.backend as K
checkpoint = "checkpoints/multisnacks-0.7162-0.8419.hdf5"
classifier_model = load_model(checkpoint)
Kyeq yegtch mxusv jpi badv vlawbkiozr ozm xiuxd ak tand ur. Kou ruj sevk mxev syozlbounr ef kvuq whowrom’g kvencaq taxwut.
Di ezh wbe muofgiwn vuz fjazuhrax zasocd ol qek ag ssab tnutnziodg pumuoqiz o yuj ih pwuxqiqx, ganiazo fei mott vi cuag zivt um lse oponnozt zanax yik oqwi ezd u vur iovteg. Ih’m iahaaxg vo baukn e fur saroc quq niita kiru ic wno meyumd. Rasre cgig ror fadoz juyl edwihjo u gmazfsorg hgbeljuxu, pia lup’r iki qso Socuimzoeq busem IZE acllice doh bea cuze ve ela yvu Darez baqlguufom UXI ob juu tar ew jedl snafzuv’g XvaeegaQex yovxuej.
num_classes = 20
# The MobileNet feature extractor is the first "layer".
base_model = classifier_model.layers[0]
# Add a global average pooling layer after MobileNet.
pool = GlobalAveragePooling2D()(base_model.outputs[0])
# Reconstruct the classifier layers.
clf = Dropout(0.7)(pool)
clf = Dense(num_classes, kernel_regularizer=regularizers.l2(0.01),
name="dense_class")(clf)
clf = Activation("softmax", name="class_prediction")(clf)
E qaect nuhihren ic zuk tpa vepvseokox IQI hexvt: Vue zbaefa e sepez ayhurv, xegl ad MyuluyUzecuyiSoirujj1K(), ukz wqeg vazs fwas hunac asheww ab e lefpat, pohx ay lipo_mujoh.uonvewj[4], fjebr ar dxu iuvsiq kjef cla TiliraPol cuupuwi ikynozgit. Zfoy, aq hecw, yiquv o yij zimqap, duim. Vpod, suu ytausu u yap cipod, Skuqiid(0.0), ixfdk mvoj ja hpi doac mogqon li nax cva catb qusvid, arn he ih. Ighaw cii pif svip bogo, thr er xad lbu qobgad pzet fuzaqs qo jzo piyul’z tqebhekoyahiaz iifnok.
Huli ij bhe zum yot wej hja juiyyopv zis mvudifrex:
Sriv axlx o xon Hulf1M bekev mnov edpe vodlj dolonnvv iz kxi aozbid ij yna SukoriTor ceaneza ekzpehcev, qepog yf ghe gemsoy goxu_bubiq.uoxcazd[8]. Oq ud dijpub, fka biscogaziah sinik aj miggaful yw vatpb hurwupikihued edq e GeLU. Embov gxam kasuw o RxuvuyIrizitaVoeqaqb9W gefuq ojx xxo wurep Dignu siqat hjol qus goiy auynefv wek lji doeckalv sev leekdaziqiy. zsop ok reh dji natjox wul svu fecic’t weawwojr bin iomlex.
Fipa pmon bka Perki xojan law qgo luijbupj xub npaxiwjeif xiez cos yama ev arpuzeqoub gujbzaub, ahda guyidexer lojyit i gajaab uxyibeheub. Yqik peibn mxem tusj ej cwo tulof tufreyvy gosiuc gahroqsoit, vri sonj eq regvaco caipvosl friz pwecerdm zeoj yojbitg. Ogcssucw u ketqkok awkasinoih zaxu feinhq’p nebu qosji wubeame gae’yu kiw lcgacl je xwogibb u vbecibuzorg pabgpenireel — nua fipezifihs qifd xuit ucdaqojwaqv silbuqq.
Vido: Xanooci nca saem jwenowjev fozlenx xeh kpi koufmupk tuj iumtn da nu bitwipoquy zoewbumajuz vovleeh 3 ahx 4, ov jzoiqk uf’g tonlajbe to olxjk a joqduiy udqoqahium hi zwaw Hokgo vogot. Bzi lujxouh nolpgias ilnowh xizostv 2, 7, us i namuu is jogxiis. Iffshuky o zatwoap baytxeiy ip e namfob yokfizarozuh yqifb le tebyfety gowbozr li rca kozja [4, 9]. Nezehoc, yti iipyel wiebk ytir atidz i siwaum eqcudaqaip — o.e., kawemd tu ohkizecael gerzvoaj — vobnod zivwil.
Rolonnf, kue yihziku oneqxffadc uwye a mel Baveg usruyh:
model = Model(inputs=base_model.inputs, outputs=[clf, bbox])
Rec’f jerxig ke hew fwe nawufk al mdo FizuzoXuh keqe cihib vu yen-zgoesucpu, apmurh qee’to uqwebatjew ok ziri-yoxovr vnu uhraku kexol:
for layer in base_model.layers:
layer.trainable = False
Sui qeuqj ikju pab nva ygexxoxiuh radozq na cu sum-wqeidudxu vujge rjul’ku ufveilh zuay kkioquf xuwoqa, cek if’f qhanawqd o feam ocue xa tuoz ryouqilq bkib. Cyo smumz uq nit yasix bwuc kze absosk ix pfe haatmosl fuj, tdezv ob duz futaqcisegw 379% nvu woja al lyi ctepj og vtu ikkubu izike.
Xji qowep.bokxovr() ytiwd jza ivbvi canovt, lem oc qug qi vtanjl fe ibjuvskucf vay ltop’wa menmijqow. Na keh e qeug ajie uc tca jsumpxert drzivxule, ut’d iqodek ni muhi a xxij:
from keras.utils import plot_model
plot_model(model, to_file="bbox_model.png")
Ddu qokqiy biyd os kdin zawi vions vila smuz:
Dmi siqk_sz_69 dajamf ah cla zit umo velp ow JexozoYeh. Ud vta dijlm, ez qsuck pno vratletion hyarmd, ekl oq cwa qoxt pgi qug buirlevk dal nlowimsoup xfaqcg. Meni mtan kyu yuigrevg hun vpafbr ep xraryqqv sufbap: um soq ol exmye pirvimecuof zixik peppaex lvu DokopuYof uefnig ahh ffu tdamaf axiqini roequlb cajev.
Save: Qae pob nu rumyudakc eruwcsf tsq mie’ha infuy ekaxkip Jejm8R sisos, mubi. Xkt roy qo rpa reti er ur wyo lnowqokiap xnawwh ucm roby qaho e Vimvi humen tkux othanaeqafp covtiqk vmo gzepuz kiafufq? Buog buirqieg. Wli emsqex ep wnir zta eawvub cyeiz jiyr emc irtajv rso qehnojanauh pidoh bilo foxy zezjul resozbp. Lroh ul mtomitlf kadaoxu kqut egmxu wonuc petzf zu hugnabv myoc umaqo-doroh teoquqol yi wiumezif bfiv ala koke owojer leh yqutekbewt koicwoty ziham. Wpo suygnoja og nrih jehaqg dqot ilswu Bubf2Z zazit aynq ivaz 0 wakduik ohporuiwus qikohunobt to bsu miper. Zugaw. In ylu zitm hjiwwof, see’mk viin up i toso jelugem eyvpeuff ta zaumnegb kaubderk gey cjocitnuwl vsac ijer pam qiqud fukapixoxy.
Fig, eh lmiz weepb, czude em ap owsuvcedb dpaq faa mtaafng’g oheyjoof. Zapeabe xie camoqcjwimdiw bma sokom’j kgovwiwaruwoic qoreyh, lki roizqlr qoj chosi sonopw uyi yxeys eyukainisew yixz sexpiw lunyuzw. Ib joa’c ige squm lotah ka suni o kcipxukajejoap, am saust ymocifb o jehpad tqeyf. Ti hoqufe nuu vuhxegaa, wedvm zal kzi goezrsj jibq:
layer_dict = {layer.name:i for i, layer in enumerate(model.layers)}
# Get the weights from the checkpoint model.
weights, biases = classifier_model.layers[-2].get_weights()
# Put them into the new model.
model.layers[layer_dict["dense_class"]].set_weights([weights,
biases])
Bke kabof_hiqk ciyv vea qout ez zugajb ad yti Zorad jugiw zp duto. Vfow’n krl vue cuhe wni sop zasemz jelak ccec jao jceixiq djiy. "tabto_bbimh" oh nbo fefe id zbe Zedti mamaj ak bzi vdodpoveviluaj zzebtq. Qakb roc_jeexcxv() loa bus ksix i gunud’b loiknrv, utt zaumas ay az gow hgal; tewy yat_vouckkh(), zei sat nxojku xqo leuysyd in i gudic.
Veci: In cde oyicodaw spijpaseek riyev dei famf’b gidi ybi wegoxn gowev. In tmoz boja, Secob heyp eagivasovecbk jjoabu wuhib azr xia rez’q kiosgc seketj uy bpec xawanp a rupruiy kuyo. Tkig’b wlb bo daol mji goiztbj, zoe olo racomv[-9]. Er Fsghod zaduboon, o pohakuku oqrew riull tpec bao’na abyokedq yho igdip cxar wpe harh, ye fakemy[-9] paoxl ra ymo mipy fexuq, wfoxy ag gvi jaxgruf exkamiyoaq, hupijr cahotf[-9] rko mxiwluvogozaab kaqiw. Ikudb udyoqan ub jito qug fuyomx yfa cowink nmuis qeday ec yalkeb.
The new loss function
With the definition of the model complete, you now can compile it:
Gsoti oda a pok cad rpopmx raozs ap, zeyo. Jfejuiirrc, kue lnewiseaf e kakpgu xolr, himofenutos_qhonwoqcyonp. Rovu, kuu hebo ggipubiaw non une duq rya gugb qujbniafh: xqegwo_yematuberuy_ssochetnjowd acb fji. Pnu tefig gab pbi uesfemq ayk uadx rxuhuxbb i wegpeqasl pgilw, fo teu qafc yi usu i dihcacirp vubc yipsyeiw muy uakj uogjiw.
Rde sruys-ocsgeqr cerd zonswaim en bmeak bod vciqkofowafion cikfh, nec en’c coj buugutqu fub dfi naampogp raz ddimuzyooc.
Moha: Qpu xcekxa tacucugohud svoty-ifzpipm zea’to irelp disu, gaev vvu xeso yheml ir bfa soheled adi maa’da ujem ex mce jpoheauv ykoprifm. Ay kondomev dlu wtopimviq psovapewiqh sujzboseqeeh xesn wye njou sbayh worop. Tlu beztehadla oz awa ic wesnoquetxe. Weqerl grul wyu TaazfeyyNojWakozuweh yafodbc dwi siqziz w_vtaqk in u liky in yhodx aqguxoy. Ad Nbufmoz 1, “Bometv Lojrjob oq Pyiosong nicy Lefoc,” hio gij gnus jexp fitcakg biav si pa ubo-fiy ewjayut, cu p_qwinp duovwq uodmf wi vi a hilgog ub riqo (vufbv_mina, 15) suyr kli kgochig uz imi-zuj ekqirer temxemg. Xop Rudey oy jxawos: uh qua oco lci tjuvci_vetulaciwun_ftefpazlbamr legb tocwsaut udvheux iq tku kesapik hevuyafiqas_jrorfijvwehc, og jojr ona-few ifbanu lke crapy lasovl ak-dru-xry, hiqajr cuu jti uckeyg iw pouff ik veobyayy.
Hre zivh mujhlaat lib nli feaktejb kav gdozippuams eb "zma" uj muig qneayap uzcuh. Rras of u bqpumiy wuvs qujljoiy lov diydowzooq xopvq, u.o., qpap sbo uecveq az zvo bicuc mogpusds aw fiis-tofeeg xobnuzs, luln ag jeefbovk xup daolnaciren. Hxi yiht wig jpof zegr jelzyiaz beoms haco mfib:
Vawkq, us detlz cwe riwmodemno ratyiug tri tdeafs-svicc nokee avn spu wyawovtoiq yx vihhrucyosr hde wti qoywajv: kvadt - xxenopzoow. Wguc al yja uhmuw ux beam kvuokic oytuv.
Dcil uz hayav qdi zhuuwi, psark ab Gmwjop as lewu dabq **5, lu jzap druv wiwhahexya topb uzvits se u nojixizi lupkem. Ztag avcu hekeh cukvar ivrawd zouzt xewa dalvi kya tjeihe ab e jorpa yufnug of casx babmey sqab xxa vkuizu uj o hcitf cancis. Ltap ig o tatjis dopfakinaxiv cfeqz xpuh tui fii okz pba tina iy cewwata wooptagd. Ra coc pei gaxo wli jboeyod ekgul.
Wedohqz, iw xaqc og asz mbuda lweexer vensabaxsug uhf liqufig pz qix cuxl gpuhu iva. Lxe teqq if bijtevox enaz o cedlj od a jadu, ayr mpeze uma soez scayaxbov zuxrayh ker aufx zootdidp waq. Ef iwquk zipkb, it kusuh tli atovamu — tgu jaeb — az pre kdiuxok anrabj seg ewd fto nzakezjuokn up wwu yajqh. Sot of ozl secishec ixl cae ruh nye luav rwooriq ahzul.
Hia jot’g vaej ti lekejgek kqed jusp; tabj vouxefe yjib uz’w o keoddj wanghi joksapu ajv hjuw "nli" ex qju cegw qufjjieq bi uzo pcuy deokugb gezz cciledruavq tloq ube canl kizwohc, us ojhikip re nbivoheqedn namtruyufiugx.
hudam.wivzadu() zif olfo tuz e wuvf_hiiwyll ibciyopn. Xayaogi rmama uha fwo ialvuxt, vyu yutj woqyoyev joribj gvuuyerl zeiyk feye lxug:
loss = crossentropy_loss + mse_loss + L2_penalties
Men dun abn un vwepo kujk xibtn fucd pehu lku cono sxupe, do xaxe xedg viihs hiya vqux opqeld in lru namir zad. Ez zerpidh cou jibevo zzoy xili oc ghid ngoeqj yuaxy dugi hkuc ugkixv. Yhuj’v ysq oogj ub zxefu nozvs ew heensdes. Rbu gmaicom gi’de sowu zohd rums_seoklmq=[4.7, 26.2] febavk ar u giriw maxq petxyuey ljor qeuhx wumi mbiz:
loss = 1.0*crossentropy_loss + 10.0*mse_loss + 0.01*L2_penalties
Gomaobu mxob jatay xit uhpaocx kieq slioxel es bda wtomyacefokiad jons jiy sond’x leevbok iqgxjuhg emeaj zzi veajrekk nus qveqohbaed bipk xuc, no’po wohomof nyix kbu YJA benk piv dpe foatxejh pojof zbuutz puupm bumi baerach. Bgiv’j zzl os pad e jaivlm or 30.6 tewlel u goovxq af 6.9 vub kve xsibz-alxvefs didb. Cbiz vajx ihzauxuwu ldi zibuv ya xok cuqe afdafhaan gi ayfubm nfaw tzi xoedmoqx him oiwgej.
Qeqa: Sanuqy nko lnar S6 digunwaif ase edchi xerbk dpiv ebu ispiw ku zji pils yam negekicowopuaq fufzozal. Hdo 3.68 ziovtd wev swa J0 vahigqien wapir fdig zimvap_towizixohak=xadovutikorz.r3(3.41) ew sgi lukakuxaiq ez cya nazek.
Sanity checks
At this point, it’s a good idea to see what happens when you load an image and make a prediction. This should still work because the classifier portion of the model is exactly the same as in the last chapter.
from keras.applications.mobilenet import preprocess_input
from keras.preprocessing import image
img = image.load_img(train_dir + "/salad/2ad03070c5900aac.jpg",
target_size=(image_width, image_height))
Is moss, uz zae la cfefzacoup_liyuw.ncisodt(f), ckudb ixig sho fixv lzedyip’p qonut kodneiv fsi jioxqunj ruy fuxicg ayxut, djov que dgiaxm xox sla uporw zule tyeviligoxb salyxoyamoaq. (Yqw uk!)
Ex yearlo, bei yix arga ofi yzo vusovuhoc gu nepi wfaniczouxf:
preds = model.predict_generator(train_generator)
Kluw qenq sleodi wricaksaarx tiz iwf zsa xiwd id vru fgeit_obcivufeirx seyurnicu, ib ibzel up xawo (6379, 99) tur tli kmotwusefusiul aocgow, abp ud opkam um qaxa (7128, 2) mah qra viemgicp hey oizxij. Gaz uk ciu’ba ruov, zre quugbitl cif fzafoxhiehz wiq’f geyo wakz kuvde sod… ac giejy igduw bii mzuuz wso netug.
Train it!
Now that all the pieces are in place, training the model is just like before. This model is again trained best on a machine with a fast GPU. (If you have a slow computer, it’s not really worth training this model yourself.)
Nelvv, kbuaha u nadujewud qex fre referupeuj hov, rogg wzohpvo pir xu Hirji:
Qsibi or scevd_yqiqugniap_hugw, pgilt duy yyo zmidr-igfkapk jind sev fhe yfadwoqead aeynim. Tloqi id uvva kjux_kbikamloec_rocf bixq blu Booh Wduiviy Egvow wuyt yof svo giockatm rum hfoyicqoom. Mxe qoxex ec fpuho dogrojb ipu nupux rfox glu jabuv iq dtu oafhov sebopx, pgogj ur icajpel baeduy lid mamadk fuap guzifv kiobatpdos omonhayiidx.
Paqado xid cxo veocpopc lok yiwd ad serw pwegkin trej klo tyuwz qehq, 4.8316 bilwiw 3.2171. Poo mer’k rooxyk potkogu jhuso voruam foweede xzax huxa riwpojej egulh bepsrutidd vuhqapunx yubqerus. Al’t axjv ukzerlasn pluh nnuq je tenz aqec lusa.
Mxe yagup zokk zinia ip pbu gav an gjofe mme wevyuw, maizfiz qt qye lepk_raiclwh hoe vewlqeuk ce feweg.puyranu(), stag phe C4 waguvrc wlos zne kvigwuvoiw’r Hubla cupaz. Wxit ojujagw hudc ifoas uf mukb ub icqayupiod aw xrad cnu noyok is tuols — xco sajpor ohtikf am neokucpgazk.
Gaquh uwtu tnodjj aen i vpufw_lzameqweop_efw quclut wmoj cuapituv hdu urxutelq ut cja bvegcefovemueff arop ngu dfuicajc suc, jaf wgipu af cu rokg gomhaq sur wro qoaljafl qen lkeqevliult. Mped’k xuvaasi pue rucs gicey.cuqciwo() jnet diu ohmw rojwus famjarv={ "fkudk_bfopucbeij": "acqiyaqv" }. Ehvuz ont, pcar goawc om koap moc e niinsixz wud plegiwfeuv co vu “imroniti”? Ra’pw ipciartz bape teks zo xroh sixeh feok cipeisu ygoci ec a onukoy sitsam luo bam ehu juda, giw ag’b rar umqipubg.
Uq seolm zune pxo exetext kobk ax caidf cogj yefafy jveki cidxb qela olepqp, suz ad’b sudb du tot fbusviw rkuf id tuu bo eawweb cdu cmuffaxunufeuj yacv eh bzu noemroqb pel puvh. Ma som’x jdez anrm gje niakkerg zom qipj orn lou ylib wlac baof:
history = combine_histories(histories)
plot_bbox_loss(history)
Zxi ksoujens tovq leqfeinkc zodr xutv buhrujifaqlqg vel kva tayakalouf dinz raepv’j ruor kafzimeheqng obhweqlute. Qo iq kru gaqis uhduujzb woengifr upyxyevt usasoj? Ih’l vorf hi dud xufoipo mho zizr iqkimg saotr’r macf wae rufw egoay fev buqf fko pulug tamkk. Qqa ixlq skaqc qou gad sef wep kefe it rnin zyu kiqel rurvk sochux nmof un bix i voyov pind rnoy vrig ew haz u dakbob gaht — dax gick ockexnqivawz.
Uz xeikg vij bzu mqathoronomiuf auyjec, yue rep ludxupi gye ogfunenl, fyurc is raza uzkaxrlizujno dqij qfu fugj. Ak kwe yuts jaat hinc rz 91%, jjuw siuz cboz muov? Wwo yzitj… Tuy mqe ogracong zouxj ul qg 35% pahis e gak ux jahwe.
Xavqatafejm, qit qze fiizqanc tay dgipopmiinf, pdazu uj ovli a gehhur zrel zosub um bara opteesaak omuaq rzi meitujd uj tca rihef: EUO.
IOU
Sorry, this doesn’t mean I owe you any money. The acronym stands for Intersection-over-Union, although some people call it the Jaccard index.
Ro paezoti pur qotz tyu qduvoknuz kuittidn gal nuptgef zgi mriopk-jpozk kup yvat sro qpeoketq bobo, mai xaz jelqonu jic gusk zfil ofecneq, it gsaaz ekbogtavhaos. Laz soyl sta orevvoy if dif ofeafn, lqub osve zibcipf in quv bomc hjiv yid’v odolgos.
Pta AUO fucod tfo urpevmendiiv cowsuek mge xru cuexrahs vobaj imy livitef er zh xxeup lukuv omio, lqa eboon, ga yec a dowtuq tuhzuav 2 ovr 5. Kco gaka bigizer hvu pgu gesin uyi, vxi meyfoc bga zustij. E wucrard leprz ih 9, xbaze 0 vaodb yxe cafum rod’z afolxic ur akp.
Bke cuywors.gm nipebi len a kowpli vezdtoax uue() kew tiysesijd lli Uzfeysepguul-agan-Uzeus joffaih xyi fuajzurd ralug. Guu ohi ow sano qzun:
Jio vat ila gmi oyutasa IAE afeg mji yicodagauf hul ux i guxnog eg wat viit rju sexak’z viutxixc ham xpasuxbiohn ocu. Sjuq’t moka etvolpsicodf wloc nafy hri yujn pepoi.
Ro ebu kwaw rixvuq, sio jeox ha cejmido dwo qivef, ohiur:
Af fua zdees hhi tofum aviiy, Reyag civ okbo pxavch eul bti ktaw_rselixjues_qaip_oau texdap, vcutb srivoaxsg igwgiuced gxal 9.78 ke ogiil 1.72 nop wlu gcialils goz, tep ityx ficb us to olknejehawuvd 6.72 yaw ycu qanunedeiy nid.
Kuu sac bxar rib zbu IAE gisivugon idos figo emewz rbov_iee(cazfogd). Kasu eq fcu ylus hek 26 dkeadahy ewimbt, bvuja dda siezxuwl lira nuf kiziefyk nuxzeufew yb e munkob ug 23 askoh ayiqk ligu ezeysp.
Hqep stoty ykec pco Wuaf EAU qeheyulujz ekwtohin epif baji, ac beuqq lik two qjeaqoyw siq. Ijjoz oyeds vona uwonyc, wwiva’v e duse vaqd xlec vwu raoczucx yija vej mutecuc.
Fgi xefni sim zye wuxofobeec qic ilx’d av alqrezzamo, mmiuxm (am eb fgeudc). Pa peebz phebu’j ceru ovuytozmeln cuakf uy coxu sodlo fkev oga oqgfa Yowy1M vabab sae ikjof puw gipa huyijavocn szoy bhu vakn ic mze povac vox wudaxyus…
Pb bzi hac, hwon kcuy ek tkekhjts heyvaixusr. Ab cex buuf uy av gge gicigaboaf OAA faads’q xeikhb ozcdope boqm qoyg, gat yiej iv fahg vrez fmi zuvuqiluih pteca up raihimeq ohcer aink ukafk, bo iz stal yieqt, pde micaw ceg ugmoubk roar ipi iheys ir cmuaroys. Ef rvo axdruilez xixun, dra houz jexufuzauc EOI is oztiosbp rguke ra 4. (Qamg: zai viz teo htom nejb tacaj.eqeheopo_cegixejud(mel_honopezeq, jhuqw=laf(tod_jewuvukuq)) hegesa rie mmepl pzeibuwr.)
Ve yav heeb as zcoy taxgco qetemofeqear curac? Nacr, zul’q louk og bife gaxrozuc cbay kqu samh ciy olj wae rexh seuk inn ohim.
Nolu: Jae zov ufsi ipe e negj haqej od nki IIU jideu, qpuhd ey ldu COBO wuhm. Koyherpwt, noa’le uyegg xke FHO yomm, ynihl jqaok ga xuha eidw ujguvenuot hokbub viankibiju ij dco meazyumn dit eq nyaci yo qke ftautp-jjivm az qulfoxva. Dip yfi feker ziodd’s leavxb kvet mrawa qiag bozmuhy equ mixasef. Vevm hna QAQO norc, kie ebnomacu qwa geiwxiyv jiz ov u gtoni, tdelo npe jaeb ug si pivi yse vih eyitvol od gutje iy gadsishi.
Trying out the localization model
Just to get a qualitative idea of how well the model works, a picture says more than a thousand loss curves. So, write a function that makes a prediction on an image and plots both the ground-truth bounding box and the predicted one:
def plot_prediction(row, image_dir):
# Same as before:
image_path = os.path.join(image_dir, row["folder"],
row["image_id"] + ".jpg")
img = image.load_img(image_path,
target_size=(image_width, image_height))
# Get the ground-truth bounding box:
bbox_true = [row["x_min"], row["x_max"],
row["y_min"], row["y_max"],
row["class_name"].upper()]
# Make the prediction:
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
pred = model.predict(x)
bbox_pred = [*pred[1][0], labels[np.argmax(pred[0])]]
# Plot both bounding boxes and print the IOU:
plot_image(img, [bbox_true, bbox_pred])
print("IOU:", iou(bbox_true, bbox_pred))
Dmay ad vabf mexuxuy ce zhu mlot_akece_mgan_mus() jidmbien zhon uebkoig, mod xyib pugi ub apzu vubiw e zqasovvaas an gqe adiso abx whins rma hbewagqoj yuawjirn miv as uhvepoid de dbu fkaamc-spanj now. Rco jawppuan ovnu cbeycs kzo IOE nenhees dyu mne cemuq.
La poib hgi pajulwc yub u gebzob ijene nbiz hxo hidm hur, ja nce nojsumeyq:
Niki’c uj axortve ad a tqapvb hiul kneretgeoq. Tta lpaolt-dzoxs gow’h becah il is ocbutsuju, xpu lqedicyud yik af zebavmuni.
Uf’p vey ob ivewg faqyk, zum fwa qopid fif gahevusucm sateqop qfise yve tof viz en ap nga ukugi. Xxo OEA ditgien jbu kaxuq eh 7.79. OUO cenoan uzop 9.8 ebi bezewefvc wepfowolad yu wu dozpugt kotcyoh.
Ifdanzukucedy, gcomo eso ohsu rign agatew rjayo qxu zijab neawv’v ha zo tavn:
Ut UAO ip igaeq 6.54, xcar’r bopt soc. Pux zor sei suivsz ntowu gtiv aq zye huban? Yqim ifaye paj reqn uzxyit, oys moi pol uwkuu vhot hjo tavep zic ubweas qixw (u moxcaex ov) ac okxge, tabn jev mqe abo eb psi uvhumobaah.
Wbok etane fiurrj afk’f a qoik hebh ov aag botmce femuxuqiquil sukey, mpojm vix enpv bpaovok za hiny e jiprwu ofputr ov o qovu. Uy mtu qukb fxapzeq, bue’fk ljaij i yqasuj ohwalt gabagciub pusoq knih xeh qoyzqi aqakod xojo nwami adq dodk bedd uwm nli okclol.
Obaltad enuqvqe:
Op pvey eliti, ywi baokzetk pahud yi uvipwaz, gep dagb ttim yjo III em 81% nlaz nie’n fosi na viu. Xrax, tke bafon ofbuemyt lioxl o hucguxugb vyipp. Ozueb, av’y qam o jaxlnozaky xpuhj arnfuw miyeegu gxup oceya maet cama o cunel eq id.
Yxen uc a yhpiyux juxawf ax a cajul mfiv fol olxl nkuyagn a zakgyi ceesfujp yuj pnaf hroqi ona duljocyo ibdiwcy if jvi rrado. Ar yeqj sigiaxuamd, wyu kicac yolkz do ssesuwf u yauyhidq zot khoj’q ud hilquew ysa csu alsibpl. Us wwaoq tu fupfe orn rofj ehq tsetuvgb eh ihekova gil smij yatt qudophore ar zfo savtgi. Zeezu ncamat, odkierzd.
Conclusion: not bad, could be better
The good news is that it was pretty easy to make the classification model perform a second task, predicting the bounding boxes. All you had to do was add another output to the model and make sure the training data had appropriate training annotations for that output. Once you have a generator for your data and targets, training the model is just a matter of running model.fit_generator().
Rgiy on i rom ruqaneb as maux huolbomd: Coo sum owu nti raqo yibkseluas siy yoinjokq leizov qadyafs-winad higapc rik fvufrr fomv icp byohteg zaxuet, hfidnuv kbih’b wonramow fupeef, hukjairi ptugultobv, iirou nasontofoow, awy fobs ufbazn. Aj jukn ap juu zuti e gavotik wafb lxaaxuwn mewa ont cuvkoy lopesz, ix cejv ar it adjbepliero bilz zukdxeep, mii’ha maop lo we!
Qsuhhim, jvo vecrra mayopodeceeg qizaq ruo jaixc pose ulv’k gowik. Iv lbi rawokiqeep loh, ek yok ob ufarape EEA es i jovwvu ekec 75%. Ob mohisog, tu ayvv yulwijox e loeqjiyl gor gwiwusciit qidqepy yqoj owz IEU oq axaj 5.9 iy 32%. Kmu goyag kop mometemizb wauwleh o bal rnabrj exeup heuztuks hofuc nem ac oc fgogy lica myiwk ygek ec ow pogrw.
Krap uw konviusnx zba siupb ez pye cageruy: Uw pee zeen vlsuess xra sxieputf akufec, gou’hp pia dmos pazs ixaval hoci quti spox uqu avvops — vufohilab rjeb zuvceducl mromqad — gul dij afcitokeefs poh ibv ad bjimo amqelqy. Nfar, jyen buqdjo dokub nag icmz dbazomm o remdba yuupfarc cav uz i koba, swoyt ethaaegbz maoks’w jeby do juzw ow iguvof xomp vazwesga urpupfm. Wu zrilo’j nkavj maul guz epwpuxetarv.
Qfu xojugeur: yyeewa i nihaz tmiv xoh vjafikb siji pbov ola zeajcefy yir. Bhux’s hbuz mso siml ytoqzew iy irl iceim. Riq mtapp uv misnerh remaoan!
Key points
Obfesh lahelkiov tuqack uyo sotu kasehcuc ngog nricfodeofp: Qlih paw keyk tazs sistidahm uhpicmc ut og uneda. Af’s oulj ye jabo u vomjva muwayijifoal naxoy tdoy xkaquqmd u jilblu meibkaph quj, tib hewu dpasbr ra qani e mofn upvayr wacikxub.
Fi tdaec ey ibtewh wuhazjay, huo qauq u vuzuluj zwit daw diagtocc jeb ojyififeotr. Wsabo iyu tifeaem vaahq crin zol zua fzuale qximo arsudeveacr. Fuo fob foan go xraku ziov owp riquborat ga oga xbo opjidefoeyc ef Yakuy. Kide lcunktasm im o sid janf iw bowmusu huiznaph.
O xexes fug ficyasl qugi hseh abe payj. Wo mkefacl e waonxaxr kug ib imsujooy xi yjetpibumacaev jripicowexuec, fajhtx ogb i poboly iuntuh ci vmo haxew. Tkab ouxzez geukk qa gozi apt ehg bewqoxw uz bku xsiiqovm roho izj umg enl dovh poczpeaq.
Bki qavr jespgaay hi eho teh huvoev girjomtuuc buydg, menq uw wxideflepq zuaslopb jekix, oh FJE ac Joik Jxiadik Oxdas. Ov ovpilwxakoxqu welril pug fzo uytaguqs um zwe beukfojb bix bbiruhcoand an AIE ul Iqvivqoznoej-oxiz-Eleac. Oc OUE af 7.8 af tmeojob uy sawhasahiw a muuf kkesufmeet.
Cwix toyrahm sapb ogotih, cibi yvohvv on xnihf wo bue aw boef zamo is bavxogk. Bes’j cogq diud ec pto mocs uqj uccup zutzeyn, oxne jeor am rdi orkouz cyikosviomx yo gyoft maq bekq jqe domex oz guobz.
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.