# # Collective Knowledge (classify image using various models such as DNN) # # See CK LICENSE.txt for licensing details # See CK COPYRIGHT.txt for copyright details # # Developer: Grigori Fursin, Grigori.Fursin@cTuning.org, http://fursin.net # cfg={} # Will be updated by CK (meta description of this module) work={} # Will be updated by CK (temporal data) ck=None # Will be updated by CK (initialized CK kernel) # Local settings hextra='
\n' hextra+=' [ Community-driven AI R&D powered by CK ], ' hextra+=' [ CK-Caffe ], ' hextra+=' [ CK-Caffe2 ], ' hextra+=' [ CK-TensorFlow ], ' hextra+=' [ Wikipedia, \n' hextra+='paper 1, \n' hextra+='Paper 2, \n' hextra+='YouTube CK intro ] \n' hextra+='
\n' hextra+='
\n' hextra+='Optimizations results are continuously shared by volunteers across diverse programs, data sets and platforms: ' hextra+='GCC , \n' hextra+='LLVM \n' dlabels='collective_training_set' ############################################################################## # Initialize module def init(i): """ Input: {} Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ return {'return':0} ############################################################################## # show dashboard def show(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ import os import shutil import time import copy form_name='milepost_web_form' sh=i.get('skip_html','') ww=False if i.get('widget','')=='yes': ww=True # State reset dar=False if 'dnn_action_reset' in i: dar=True # Start HTML h='' st='' h+='
\n' h+='\n\n\n\n' if not ww: h+=hextra if ww: url0=i.get('prepared_url0','') url1=i.get('prepared_url1','') form_name=i.get('prepared_form_name','') else: # Check host URL prefix and default module/action rx=ck.access({'action':'form_url_prefix', 'module_uoa':'wfe', 'host':i.get('host',''), 'port':i.get('port',''), 'template':i.get('template','')}) if rx['return']>0: return rx url0=rx['url'] template=rx['template'] url=url0 action=i.get('action','') muoa=i.get('module_uoa','') url+='action=index&module_uoa=wfe&native_action='+action+'&'+'native_module_uoa='+muoa url1=url # Start form r=ck.access({'action':'start_form', 'module_uoa':cfg['module_deps']['wfe'], 'url':url1, 'name':form_name}) if r['return']>0: return r h+=r['html'] url=url0 onchange='document.'+form_name+'.submit();' # Header if not ww: h+='

Unified CK AI JSON API to classify images using different DNN engines (Caffe, TensorFlow, etc) while monitoring/optimizing performance

\n' # Select engine dt=[ {'name':'Caffe', 'value':'caffe'}, {'name':'Caffe2','value':'caffe2'}, {'name':'TensorFlow', 'value':'tensorflow'} ] engine=i.get('dnn_engine','') if engine=='': engine='caffe' ii={'action':'create_selector', 'module_uoa':cfg['module_deps']['wfe'], 'data':dt, 'name':'dnn_engine', 'onchange':onchange, 'skip_sort':'yes', 'selected_value':engine} r=ck.access(ii) if r['return']>0: return r x=r['html'] h+='DNN engine: '+x+'

\n' # Search env to check if has installed r=ck.access({'action':'search', 'module_uoa':cfg['module_deps']['env'], 'tags':'lib,'+engine}) if r['return']>0: return r el=r['lst'] warning='' prediction='' if len(el)==0: h+='DNN engine is not installed - contact admin to install this engine and related models in the CK AI cloud

' warning='DNN engine is not installed' else: h+=''+str(len(el))+' engine(s) installed (different optimizations and platforms)
' # Search models to check if has installed tx=engine+'model' if engine=='caffe2': tx='caffemodel2' r=ck.access({'action':'search', 'module_uoa':cfg['module_deps']['env'], 'tags':tx}) if r['return']>0: return r models=r['lst'] if len(models)==0: h+='DNN models for this engine are not installed - contact admin to install more models in the CK AI cloud

' warning='DNN models for this engine are not installed' else: h+=''+str(len(models))+' model(s) installed (different topology and parameters)

' h+='Optimization statistics shared by the community:\n' h+='[ desktops and servers ], \n' h+='[ mobile devices and IoT ] \n' h+='
' h+='Mispredictions shared by the community:\n' h+='[ images and classifications ] \n' h+='

' fc=i.get('file_content_base64','') fcu=i.get('file_content_uploaded','') if dar: fc='' fcu='' if 'dnn_add_correct_label' in i: # Record correct label ftmp=i.get('dnn_saved_image_file','') label=i.get('dnn_correct_label','').strip() wlabel=i.get('dnn_original_classification','').strip() if label!='' and os.path.isfile(ftmp): # Check if already has holder for correct images r=ck.access({'action':'find', 'module_uoa':work['self_module_uid'], 'data_uoa':dlabels, 'repo_uoa':ck.cfg.get('record_local_repo_uoa','')}) if r['return']>0 and r['return']!=16: return r if r['return']==16: r=ck.access({'action':'add', 'module_uoa':work['self_module_uid'], 'data_uoa':dlabels, 'repo_uoa':ck.cfg.get('record_local_repo_uoa','')}) if r['return']>0: return r pl=r['path'] # Copy image pl1=os.path.join(pl,os.path.basename(ftmp)) shutil.copy(ftmp,pl1) # Record label r=ck.save_text_file({'text_file':pl1+'.label', 'string':label}) if r['return']>0: return r if wlabel!='': r=ck.save_text_file({'text_file':pl1+'.wrong_label', 'string':wlabel}) if r['return']>0: return r # Record label h+='Thank you for submitting correct label and participating in a creation of a realistic and collective training sets!

\n' else: if fc=='' and fcu=='': # Select image h+='Your JPEG image:

' h+='

' else: # Gen tmp file rx=ck.gen_tmp_file({'prefix':'tmp-', 'suffix':'.jpg'}) if rx['return']>0: return rx ftmp=rx['file_name'] if fcu=='': # Save user image rx=ck.convert_upload_string_to_file({'file_content_base64':fc, 'filename':ftmp}) if rx['return']>0: return rx else: shutil.copy(fcu,ftmp) h+='\n' # Classify ii={'action':'run', 'module_uoa':cfg['module_deps']['program'], 'data_uoa':cfg['classify_program'][engine], 'cmd_key':'classify_ck_ai_api', 'quiet':'yes', 'generate_rnd_tmp_dir':'yes', 'random':'yes', 'env':{'CK_AI_API_IMAGE_FILE':ftmp}} r=ck.access(ii) if r['return']>0: warning='Problem running DNN engine: '+r['error'] h+='WARNING: '+warning+'

' else: ch=r.get('characteristics',{}) if ch.get('run_success','')!='yes': warning='Problem running DNN engine: '+ch.get('fail_reason','') h+='WARNING: '+warning+'

' else: te=ch.get('execution_time','') td=r.get('tmp_dir','') deps=r.get('deps',{}) k1=engine+'model' k2='lib-'+engine n1=deps.get(k1,{}).get('name','')+' '+deps.get(k1,{}).get('cus',{}).get('package_extra_name','') n2=deps.get(k2,{}).get('name','')+' '+deps.get(k2,{}).get('cus',{}).get('package_extra_name','') # Attempt to read outputs p1=os.path.join(td,'stdout.log') p2=os.path.join(td,'stderr.log') s1='' s2='' r=ck.load_text_file({'text_file':p1}) if r['return']==0: s1=r['string'].strip() r=ck.load_text_file({'text_file':p2}) if r['return']==0: s2=r['string'] s=s1+'\n'+s2 sx=s.split('\n') sy='' started=False top='' for q in sx: q=q.strip() if started: if top=='': top=q if q=='': break sy+=q+'\n' if q.startswith('-----'): started=True prediction=sy s=sy.strip().replace('\n','
') # Create table (left - classification, right - stats) h+='\n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+='
\n' h+=' Classification output:

\n' h+= s+'

\n' h+=' \n' h+='
\n' h+=' If classification is wrong, please provide correct label:


\n' h+=' \n' h+=' ( view shared labels )\n' h+='
\n' h+='
\n' h+=' Engine species: '+n2+'

' h+=' Model variant: '+n1+'

' h+=' Execution time: '+( '%.1f' % te )+' sec.
' h+='
\n' h+='

' h+='\n' h+='

' if sh=='yes': h='' st='' return {'return':0, 'html':h, 'style':st, 'warning':warning, 'prediction':prediction} ############################################################################## # open dashboard def dashboard(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ i['action']='browser' i['cid']='' i['module_uoa']='' i['extra_url']='native_action=show&native_module_uoa=model.image.classification' return ck.access(i) ############################################################################## # CK AI JSON API for web (needed to automatically find such function from higher-level CK AI API) def ask_ai_web(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ return show(i) ############################################################################## # return json instead of html def show_json(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ r=show(i) if 'html' in r: del(r['html']) if 'style' in r: del(r['style']) return r ############################################################################## # user-friendly html view def html_viewer(i): """ Input: { } Output: { return - return code = 0, if successful > 0, if error (error) - error text if return > 0 } """ import os h='' #'
' # Check host URL prefix and default module/action ********************************************* rx=ck.access({'action':'form_url_prefix', 'module_uoa':cfg['module_deps']['wfe'], 'host':i.get('host',''), 'port':i.get('port',''), 'template':i.get('template','')}) if rx['return']>0: return rx url0=rx['url'] url0w=rx['url'] #rx['url_without_template'] template=rx['template'] purl=i['url_pull'] muoa=i['module_uoa'] duoa=i['data_uoa'] r=ck.access({'action':'load', 'module_uoa':muoa, 'data_uoa':duoa}) if r['return']>0: return r p=r['path'] # First find images l=[] d=os.listdir(p) for f in d: if f.endswith('.jpg'): y={'file':f, 'original_file':f, 'url':purl} f1=os.path.join(p,f+'.label') f2=os.path.join(p,f+'.wrong_label') if os.path.isfile(f1): r=ck.load_text_file({'text_file':f1}) if r['return']>0: return r x1=r['string'].strip() y['label']=x1 if os.path.isfile(f2): r=ck.load_text_file({'text_file':f2}) if r['return']>0: return r x2=r['string'].strip().replace('\n','
') y['wrong_label']=x2 l.append(y) # Trying to add visualization from Mobile crowd-benchmarking/crowd-tuning/crowd-learning entries r=ck.access({'action':'search', 'module_uoa':'experiment.bench.dnn.mobile', 'add_meta':'yes'}) if r['return']==0: lst=r['lst'] for q in lst: duid=q['data_uid'] p=q['path'] # FGG: temporal hack xurl=purl.replace('42b9a1221eb50259:287d4bee982e03c1','experiment.bench.dnn.mobile:'+duid) yurl=url0+'wcid=experiment.bench.dnn.mobile:'+duid arr=q['meta'].get('all_raw_results',[]) for y in arr: m=y.get('mispredictions',[]) for z in m: f=z.get('mispredicted_image','') px=os.path.join(p,f) if os.path.isfile(px): ff=f+'.cached.jpg' pxx=os.path.join(p,ff) if not os.path.isfile(pxx): try: import PIL from PIL import Image img=Image.open(px) wx=float(240/float(img.size[0])) h=int((float(img.size[1])*wx)) img=img.resize((240, h)) img.save(pxx) except Exception as e: pass if os.path.isfile(pxx): x1=z.get('correct_answer','') x2=z.get('misprediction_results','').replace('\n','
') y={'file':ff, 'original_file':f, 'entry_url':yurl, 'url':xurl, 'label':x1, 'wrong_label':x2} l.append(y) if len(l)==0: h+='
No shared mispredictions - community training set is empty!

\n' else: h+='
\n' h+='

Collaborative and realistic training set (mispredictions)


\n' h+='\n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' q=0 for y in l: q+=1 f=y['file'] ff=y['original_file'] url=y['url'] eurl=y.get('entry_url','') px=url+f pxx=url+ff x1=y['label'] x2=y['wrong_label'] z=str(q) if eurl!='': z=''+z+'' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+=' \n' h+='
#ImageCorrect classificationMisclassification
'+z+''+x1+''+x2+'
\n' h+='
\n' h+='
Please, report any illegal/copyrighted content here and we will remove it within 48 hours!


' h+='
\n' return {'return':0, 'html':h, 'show_top':'yes'}