#
# 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'
# 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)
'
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+'
'
h+=' Execution time: '+( '%.1f' % te )+' sec. '
h+='
\n'
h+='
\n'
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+='
Image
\n'
h+='
Correct classification
\n'
h+='
Misclassification
\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+='
'+z+'
\n'
h+='
\n'
h+='
'+x1+'
\n'
h+='
'+x2+'
\n'
h+='
\n'
h+='
\n'
h+='
\n'
h+='
Please, report any illegal/copyrighted content here and we will remove it within 48 hours!