def showrpca(): recall = np.load('rpca_rec.npy') diffall = np.load('rpca_diff.npy') from bigrec import bigrec from layerbase import DrawPatch drec = recall.reshape((-1,1,70-25,90-0)) drec2 = np.load('glassline.npy').astype('f').reshape((-1,1,70-25,90-0)) glassorig = np.load('glassorig.npy').reshape((-1,105,90)) glassall = np.copy(glassorig)[:drec.shape[0]] drecall = np.zeros((drec.shape[0]+drec2.shape[0],1,70-25,90-0),'f') drecall[::2]=drec drecall[1::2]=drec2 for i in range(drec.shape[0]): glassall[i] = bigrec(drec[i,0], glassall[i]) misc.toimage(DrawPatch(glassall.reshape((-1,1,105,90)))).save('glasses.jpg') drecall = np.zeros((drec.shape[0]+drec.shape[0],105,90),'f') drecall[::2]=glassall drecall[1::2]=glassorig misc.toimage(DrawPatch(drecall.reshape((-1,1,105,90)))).save('recoverypair.jpg') #return #misc.toimage(DrawPatch(drecall)).save('rpcapair.jpg') drec = diffall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('rpcadiff.jpg')
def cutratio_all(ratio): import cPickle sparsedirect = cPickle.load(file('sparsepcadirect','rb')) sparsedirect.set_params(transform_algorithm='lasso_lars') glassmodel = np.load('glassline.npy').astype('f') glassmodel = glassmodel[:200] recall = np.empty_like(glassmodel) diffall = np.empty_like(glassmodel) maskall = np.empty_like(glassmodel) for idx in range(glassmodel.shape[0]): print idx,glassmodel.shape[0] recall[idx],diffall[idx],maskall[idx] = cutratio(ratio, idx, sparsedirect, glassmodel) np.save('cut_rec.npy',recall) np.save('cut_diff.npy',diffall) np.save('cut_mask.npy',maskall) from layerbase import DrawPatch drec = recall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('irec.jpg') drec = diffall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('idiff.jpg') drec = maskall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('imask.jpg')
def scvisdecomp(): "可视化结果" from layerbase import DrawPatch import cPickle sparsedirect = cPickle.load(file('sparsepcadirect','rb')) drec = sparsedirect.components_.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('dict.jpg') drec = np.load('glassline.npy').astype('f').reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('src.jpg') drec = np.load('glassdecomppca.npy').reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('decomppca.jpg') drec = np.abs(np.load('glassdiffpca.npy')).reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('diffpca.jpg')
def showcut(): recall = np.load('cut_rec.npy') diffall = np.load('cut_diff.npy') maskall = np.load('cut_mask.npy') from layerbase import DrawPatch drec = recall.reshape((-1,1,70-25,90-0)) drec2 = np.load('glassline.npy').astype('f').reshape((-1,1,70-25,90-0))[0:4] drecall = np.zeros((drec.shape[0]+drec2.shape[0],1,70-25,90-0),'f') drecall[::2]=drec drecall[1::2]=drec2 misc.toimage(DrawPatch(drecall)).save('ipair.jpg') drec = diffall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('idiff.jpg') drec = maskall.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('imask.jpg')
def domedmask(): diff = np.load('rpca_diff.npy').reshape((-1,70-25,90-0)) diffmask = np.where(diff>50,255,0) from bigrec import bigrec maskall = np.zeros((diff.shape[0],105,90),np.int0) glassorig = np.load('glassorig.npy').reshape((-1,105,90)) noglass = np.zeros_like(glassorig) for i in range(diff.shape[0]): print i maskall[i] = bigrec(diffmask[i], maskall[i]) medresult = maskedmedfilt(glassorig[i], maskall[i], 6,4) noglass[i] = np.where(maskall[i], medresult, glassorig[i]) np.save('medfilt_noglass.npy',noglass) from layerbase import DrawPatch misc.toimage(DrawPatch(noglass.reshape((-1,1,105,90)))).save('mednoglass.png') misc.toimage(DrawPatch(maskall.reshape((-1,1,105,90)))).save('medmask.png')
def pairdump(): "可视化模型列" from layerbase import DrawPatch import cPickle sparsedirect = cPickle.load(file('sparselinked','rb')) drec = sparsedirect.components_.reshape((-1,1,70-25,90-0)) misc.toimage(DrawPatch(drec)).save('dictpair.jpg')
def showtransform(): "显示神经网络转换结果" trans = np.load('transform.npy')[:,0] glassmodel = np.load('glassline.npy').astype('f').reshape((-1,45,90)) glassmodel /= np.max(glassmodel) trans = np.insert(trans,2,glassmodel,axis=1) p = trans[:,0].reshape((trans.shape[0],-1)) trans[:,0] -= ((p.max(axis=1)+p.mean(axis=1))*0.5).reshape((-1,1,1)) trans[:,0] = np.where(trans[:,0]>0, trans[:,0],0) from layerbase import DrawPatch misc.toimage(DrawPatch(trans,False,'bgy')).save('nntrans.png')
def rpca_noglass(): "用RPCA学习不戴眼镜的字典" dic = np.load('nglassline.npy') dicflat = dic.reshape((dic.shape[0],-1)) import os if os.path.exists('eyemodel.npz'): partialmodel = np.load('eyemodel.npz') else: partialmodel = {'components':None, 'nglasscodes':None} from layerbase import DrawPatch idx=0 for sdict, scode in rpca_learn(dicflat,stepdict=partialmodel['components'],stepcode=partialmodel['nglasscodes']): np.savez('eyemodel.npz', components=sdict, nglasscodes=scode) misc.toimage(DrawPatch(sdict.reshape((-1,1,45,90)),False,'bgy')).save('dictrpca_%s.png'%idx) print "SAVED" idx += 1
def getF(ab): pr = [] #valid = inp[230:330] #voutp = oup[230:330] a, b = ab.astype('f') for j in range(100 / BS): #sys.stdout.write('.') #sys.stdout.flush() v = valid_binary_model(valid[j * BS:(j + 1) * BS], voutp[j * BS:(j + 1) * BS], a, b) resp = vis_binary_model(valid[j * BS:(j + 1) * BS], voutp[j * BS:(j + 1) * BS], a, b) layer = 0 for i in resp: layer += 1 if len(i.shape) != 4: PIL.Image.fromarray( np.array((i - np.min(i)) / (np.max(i) - np.min(i)) * 255, np.uint8)).save('stepf789/%s_resp%s.png' % (j, layer)) continue PIL.Image.fromarray(DrawPatch(i[0:1])).save( 'stepf789/%s_resp%s.jpg' % (j, layer), quality=100) PIL.Image.fromarray( DrawMaskedPatch(resp[0][0:1], resp[-1][0:1, 0:1])).save( 'stepf789/maskresult_%s.jpg' % (j), quality=100) PIL.Image.fromarray( DrawMaskedPatch(resp[0][0:1], resp[-2][0:1, 0:1])).save( 'stepf789/masktruth_%s.jpg' % (j), quality=100) #v=v[:-1] print v pr.append(v) pr = np.array(pr).sum(axis=0) tp = pr[0] fp = pr[1] tn = pr[2] fn = pr[3] prec = tp / (tp + fp) reca = tp / (tp + fn) F = -2 * prec * reca / (prec + reca) print ab, F return F[:-1].sum()
inpt.std(axis=(2, 3), keepdims=True) + 1e-10) d += [float(t) for t in train_model(inpt, oupt)][1] sys.stdout.write('.') sys.stdout.flush() if i % BATCHSTEP == BATCHSTEP - 1: print d, "DRAW" d = 0 #Draw model layer = 0 for i in model.paramlayers(): layer += 1 param = i.params[0].get_value() if len(param.shape) != 4: if hasattr(i, 'reshape') and i.reshape != None: PIL.Image.fromarray( DrawPatch( param.reshape((-1, ) + i.reshape[1:]))).save( 'stepf789/layer_%s.jpg' % layer, quality=100) else: PIL.Image.fromarray( np.array((param - np.min(param)) / (np.max(param) - np.min(param)) * 255, np.uint8)).save('stepf789/layer_%s.png' % layer) continue PIL.Image.fromarray(DrawPatch(param)).save( 'stepf789/layer_%s.jpg' % layer, quality=100) #Draw response resp = vis_model(inpt, oupt) layer = 0 for i in resp:
print "Get precision & recall" pr = [] valid = inp[230:330] voutp = oup[230:330] for j in range(100/BS): #sys.stdout.write('.') #sys.stdout.flush() v=valid_binary_model(valid[j*BS:(j+1)*BS],voutp[j*BS:(j+1)*BS]) resp = vis_binary_model(valid[j*BS:(j+1)*BS],voutp[j*BS:(j+1)*BS]) layer = 0 for i in resp: layer += 1 if len(i.shape)!=4: PIL.Image.fromarray(np.array((i-np.min(i))/(np.max(i)-np.min(i))*255,np.uint8)).save('stepallface/%s_resp%s.png'%(j,layer)) continue PIL.Image.fromarray(DrawPatch(i[0:1])).save('stepallface/%s_resp%s.jpg'%(j,layer), quality=100) PIL.Image.fromarray(DrawMaskedPatch(resp[0][0:1], resp[-1][0:1,0:1])).save('stepallface/%s_maskresult_%s.jpg'%(v[-1],j), quality=100) PIL.Image.fromarray(DrawMaskedPatch(resp[0][0:1], resp[-2][0:1,0:1])).save('stepallface/%s_masktruth_%s.jpg'%(v[-1],j), quality=100) v=v[:-1] print v pr.append(v) pr = np.array(pr).sum(axis=0) tp=pr[0] fp=pr[1] tn=pr[2] fn=pr[3] prec = tp/(tp+fp) reca = tp/(tp+fn) F = 2*prec*reca/(prec+reca) print F sys.exit(0)
def showdata(): "绘出数据" data = np.load('glassdata2.npz') from layerbase import DrawPatch misc.toimage(DrawPatch(data['input'])).save('datainput.png') misc.toimage(DrawPatch(data['output'])).save('dataoutput.png')