JapanKanji
624 строки · 22.3 Кб
1import datetime2import sys, os3
4import cv2 as cv25import numpy as np6
7import pandas as pd8import matplotlib.pyplot as plt9
10from formSubs import s, softmax11
12###############################################################################################
13
14
15def xTrainCreate (nameFiles, shape_net, dirFiles='.',16bwOK=True, norm=True, stats=False) :17
18dd = [1,] if bwOK else [3,]19bw = 0 if bwOK else -120
21zxtrain = np.zeros([len(nameFiles),]+list(shape_net), dtype=np.float32)22
23scale_all = []24shape_in_all = []25
26for ii,image_id in enumerate(nameFiles) :27img = cv2.imread(os.path.join(dirFiles,image_id+'.jpg'),bw)28shape_in = img.shape[:2]29scale = min(np.array(shape_net[:2])/np.array(shape_in[:2]));30
31newX,newY = shape_in[1]*scale, shape_in[0]*scale32newimg = cv2.resize(img,(int(newX),int(newY)))33
34
35if stats :36if ii==0 : means, stds = [], []37means.append(newimg.mean())38stds.append(newimg.std())39
40newimg = newimg.astype(np.float64)41newimg = newimg-newimg.mean()42#newimg = newimg/127.043newimg = newimg/max(abs(newimg.min()),abs(newimg.max()))44
45##newimg = newimg/newimg.std()46
47###newimg = np.clip(newimg,-1.0,1.0) ####### shift mean on +0.1548
49if bwOK : newimg = newimg.reshape(newimg.shape+(1,))50zxtrain[ii,:int(newY),:int(newX)] = newimg51scale_all.append(scale)52shape_in_all.append(shape_in)53
54scale_all = np.array(scale_all)55shape_in_all = np.array(shape_in_all)56
57if stats : return(zxtrain,scale_all,shape_in_all,np.array(means),np.array(stds))58
59return(zxtrain,scale_all,shape_in_all)60
61
62###############################################################################################
63
64def xCreate2 (nameFile, shape_net, bwOK=True, coef=0.3) :65
66dd = [1,] if bwOK else [3,]67bw = 0 if bwOK else -168
69zx = np.zeros(shape_net, dtype=np.float32)70
71img = cv2.imread(nameFile,bw)72shape_in = img.shape[:2]73
74img = cv2.resize(img,(int(coef*shape_in[1]),int(coef*shape_in[0])))75#shape_in = img.shape[:2]76
77newimg = img.astype(np.float64)78newimg = newimg-newimg.mean()79newimg = newimg/(np.abs(newimg)).max()80
81if bwOK : newimg = newimg.reshape(newimg.shape+(1,))82
83deltas, x = [], []84
85for rr in range(0,shape_in[0],shape_net[0]) :86for cc in range(0,shape_in[1],shape_net[1]) :87xx = zx.copy()88ximg = newimg[rr:rr+shape_net[0],cc:cc+shape_net[1]]89xx[0:ximg.shape[0],0:ximg.shape[1]] = ximg90if (xx==0.0).all() : continue91#xx = xx.reshape((1,)+xx.shape)92x.append(xx)93deltas.append([rr,cc])94
95x = np.stack(x,axis=0)96
97return(x,deltas,shape_in,img.mean(),img.std())98
99###############################################################################################
100
101def CLAHEF (image_path, two=False, **kwarg) :102rgb = cv2.imread(image_path)103if two : return(rgb,CLAHEA(rgb,**kwarg))104return(CLAHEA(rgb,**kwarg))105
106def CLAHEA (rgb, gridsize=(94,77)) :107lab = cv2.cvtColor(rgb, cv2.COLOR_RGB2LAB)108lab_planes = cv2.split(lab)109clahe = cv2.createCLAHE(clipLimit=2.0,tileGridSize=gridsize) #(gridsize,gridsize))110lab_planes[0] = clahe.apply(lab_planes[0])111lab = cv2.merge(lab_planes)112rgb1= cv2.cvtColor(lab, cv2.COLOR_LAB2RGB)113return(rgb1)114
115def CLAHEAGray (gray, gridsize=(94,77)) :116clahe = cv2.createCLAHE(clipLimit=1.0,tileGridSize=gridsize) #(gridsize,gridsize))117gray1 = clahe.apply(gray)118return(gray1)119
120def xCreate2F (nameFile, shape_net, bwOK=True, scale=0.3,121contrast=True, clahe=False, count=None,122**kwarg) :123
124dd = (1,) if bwOK else (3,)125bw = 0 if bwOK else -1126
127zx = np.zeros(shape_net, dtype=np.float32)128
129img = cv2.imread(nameFile,bw)130shape_in = img.shape[:2]131
132if clahe and img.ndim==2 : img = CLAHEAGray(img)133
134if type(scale)==type(2.6) or type(scale)==type(2) : # scale_ is float135scale_ = scale136newsize=(int(scale_*shape_in[1]),int(scale_*shape_in[0]))137newimg = cv2.resize(img,newsize)138else : # scale_ is list or tuple (rows,columns) - new size139scale_ = min(1.0,min(scale[0]/shape_in[0],scale[1]/shape_in[1]))140newsize = (int(shape_in[1]*scale_),int(shape_in[0]*scale_))141#print('????',scale,scale_,shape_in,newsize)142newimg = cv2.resize(img,newsize)143
144if img.ndim==2 :145if 1 :146newimg = newimg.astype(float)147newimg = newimg-newimg.mean()148if contrast : newimg = newimg/(np.abs(newimg)).max()149if 1 :150newimg = np.clip((newimg*127+127),0,255).astype(np.uint8)151
152if bwOK : newimg = newimg.reshape(newimg.shape+dd)153
154deltas, x = [], []155
156zx = np.zeros(shape_net, dtype=newimg.dtype)157
158#print(newimg.shape,shape_net)159
160for rr in range(0,newimg.shape[0],shape_net[0]) :161for cc in range(0,newimg.shape[1],shape_net[1]) :162xx = zx.copy()163ximg = newimg[rr:rr+shape_net[0],cc:cc+shape_net[1]]164
165xx[0:ximg.shape[0],0:ximg.shape[1]] = ximg.copy()166
167if rr>0 or cc>0 :168if count is not None and count==0 and ximg.size <= 0.75*zx.size : continue169if count is not None and ximg.size <= 0.5*zx.size : continue170
171if (xx==255).all() : continue172if (xx==127).all() : continue173if (xx==0).all() : continue174
175#xx = xx.reshape((1,)+xx.shape)176x.append(xx.copy())177deltas.append([rr,cc])178
179#plt.imshow(xx.squeeze());180#plt.show()181
182x = np.stack(x,axis=0)183#x = np.stack(x)184
185return(x,deltas,scale_,shape_in,img.mean(),img.std())186
187def xCreate2FF(dirData, nFiles_, shape_net, ext='.jpg', counts=None, **kwarg) :188nFiles = nFiles_189if ext is not None : nFiles = [nFile+ext for nFile in nFiles]190
191aCount, count = 0, None192for ii,nFile in enumerate(nFiles) :193if counts is not None : count = counts[ii]194xdata, deltas, scale, shape_in, xmean, xstd = xCreate2F(os.path.join(dirData,nFile),195shape_net, count=count, **kwarg)196aCount = aCount + len(xdata)197
198for ii,nFile in enumerate(nFiles) :199if counts is not None : count = counts[ii]200xdata, xdelta, scale, shape_in, xmean, xstd = xCreate2F(os.path.join(dirData,nFile),201shape_net, count=count, **kwarg)202if ii==0 :203xdatas = np.zeros((aCount,)+xdata[0].shape, dtype=xdata.dtype)204xdeltas = []205xscale = np.zeros((len(nFiles),), dtype=float)206xshape = np.zeros((len(nFiles),len(shape_in)), dtype=int)207iiCount = 0208xdeltas.append(xdelta)209xdatas[iiCount:iiCount+len(xdata)], xscale[ii], xshape[ii] = xdata.copy(), scale, shape_in210iiCount = iiCount+len(xdata)211
212return xdatas,xdeltas,xscale,xshape213
214###############################################################################################
215
216def yTrainCreate (numbers, trainY, classes, shape_net, shape_out,217
218anchorsl, scale_all, shape_in_all,219printOK=False) :220
221n_anchors = len(anchorsl)222array_size= len(numbers)223
224ytrain = np.zeros([array_size,]+list(shape_out), dtype=np.float32)225
226train10 = np.zeros((5+len(classes)), dtype=np.float32)227
228grid_net = shape_net[0:2]/shape_out[0:2]229
230cc1, cc2, cc3 = 0, 0, 0231
232for iinn, ii in enumerate(numbers) :233
234ll = trainY.iloc[[ii]].fillna('').labels.values[0];235
236scale = scale_all[ii] ### bug 2019-08-13 iinn --> ii237shape_in = shape_in_all[ii] ### bug 2019-08-13 iinn --> ii238anmax = anchorsl239
240ii1, ii2 = 0, 0241
242lx5sort = np.array(ll.split()).reshape((-1,5))243temp = lx5sort[:,3].astype(np.float32)*lx5sort[:,4].astype(np.float32)244
245for lx5 in lx5sort[temp.argsort()] :246
247# x,y,w,h - x columns, y rows248lx4 = lx5[1:].astype(np.float32)249
250if (((lx4[0]+lx4[2]/2)>=shape_in[1]) or ((lx4[1]+lx4[3]/2)>=shape_in[0])) :251if (cc3<20) and printOK :252print(trainY.image_id[ii], 'bad:', iinn, ii, np.round(lx4), 'shape_in:',shape_in)253cc3 = cc3+1254continue255
256cclass = -1257
258if len(classes)>0 :259try : cclass = classes.tolist().index(lx5[0])260except : cclass = -1261
262cc1 = cc1 + 1263ii1 = ii1 + 1264
265lx4 = lx5[1:].astype(np.float32)266
267lx2max = lx5[3:].astype(np.float32)*scale268temp = np.array([np.minimum(lx2max,anmax[jj]) for jj in range(len(anmax))])269temp = temp[:,0]*temp[:,1]270iou = temp/(np.add(anmax[:,0]*anmax[:,1],lx2max[0]*lx2max[1])-temp)271
272ian = np.argmax(iou)273
274tc = lx4[:2]+lx4[2:]/2275tc = np.floor(tc*scale/(grid_net[1],grid_net[0])).astype(np.int32)276
277lx4[:2] = (lx4[:2]+lx4[2:]/2) # center point278
279lx4[0:2] = lx4[0:2]*scale/(shape_net[1],shape_net[0])280lx4[2:4] = lx4[2:4]*scale/(shape_net[1],shape_net[0])281
282train1 = train10.copy().astype(np.float32)283train1 [:] = 0.0284train1 [0] = 1.0 if cclass>=0 or len(classes)==0 else 0.0 ###########!!!!!!!!!!!!!!!!!! 2019-08-08285train1 [1:5] = lx4286if (len(classes)>0 and cclass>=0) :287train1 [5+cclass] = 1.0288#print(iinn,tc[1],tc[0],ian)289
290ianNew = ian291try :292while ytrain[ii,tc[1],tc[0],ianNew,0]==1.0 :293ianNew = (ianNew+1) % n_anchors294if ian==ianNew : break295
296if ytrain[ii,tc[1],tc[0],ianNew,0]==1.0 :297cc2 = cc2 + 1298ii2 = ii2 + 1299
300except :301if printOK :302print('except: ', ii, tc[1], tc[0], ianNew)303print('lx5 ',lx5)304print('lx4 ',lx4)305print('in:',shape_in)306print(lx4[0]*shape_net[1],lx4[1]*shape_net[0])307print(np.array(shape_net)/np.array(shape_in[:2]))308
309ytrain[iinn,tc[1],tc[0],ianNew,:] = train1310
311return (ytrain, (cc1,cc2,cc3))312
313###############################################################################################
314
315def yTrainCreate2 (numbers, trainY, classes, shape_net, shape_out,316
317anchorsl, scale_all, shape_in_all,318printOK=False) :319
320n_anchors = len(anchorsl)321array_size= len(numbers)322
323ytrain = np.zeros([array_size,]+list(shape_out), dtype=np.float32)324
325train10 = np.zeros((5+len(classes)), dtype=np.float32)326
327grid_net = shape_net[0:2]/shape_out[0:2]328
329cc1, cc2, cc3 = 0, 0, 0330
331for iinn, ii in enumerate(numbers) :332
333ll = trainY[ii];334
335scale = scale_all[ii] ### bug 2019-08-13 iinn --> ii336shape_in = shape_in_all[ii] ### bug 2019-08-13 iinn --> ii337anmax = anchorsl338
339ii1, ii2 = 0, 0340
341lx5sort = np.array(ll.split()).reshape((-1,5))342temp = lx5sort[:,3].astype(np.float32)*lx5sort[:,4].astype(np.float32)343
344for lx5 in lx5sort[temp.argsort()] :345
346# x,y,w,h - x columns, y rows347lx4 = lx5[1:].astype(np.float32)348
349if (((lx4[0]+lx4[2]/2)>=shape_in[1]) or ((lx4[1]+lx4[3]/2)>=shape_in[0])) :350if (cc3<20) and printOK :351print(trainY.image_id[ii], 'bad:', iinn, ii, np.round(lx4), 'shape_in:',shape_in)352cc3 = cc3+1353continue354
355cclass = -1356
357if len(classes)>0 :358try : cclass = classes.tolist().index(lx5[0])359except : cclass = -1360
361cc1 = cc1 + 1362ii1 = ii1 + 1363
364lx4 = lx5[1:].astype(np.float32)365
366lx2max = lx5[3:].astype(np.float32)*scale367temp = np.array([np.minimum(lx2max,anmax[jj]) for jj in range(len(anmax))])368temp = temp[:,0]*temp[:,1]369iou = temp/(np.add(anmax[:,0]*anmax[:,1],lx2max[0]*lx2max[1])-temp)370
371ian = np.argmax(iou)372
373tc = lx4[:2]+lx4[2:]/2374tc = np.floor(tc*scale/(grid_net[1],grid_net[0])).astype(np.int32)375
376lx4[:2] = (lx4[:2]+lx4[2:]/2) # center point377
378lx4[0:2] = lx4[0:2]*scale/(shape_net[1],shape_net[0])379lx4[2:4] = lx4[2:4]*scale/(shape_net[1],shape_net[0])380
381train1 = train10.copy().astype(np.float32)382train1 [:] = 0.0383train1 [0] = 1.0 if cclass>=0 or len(classes)==0 else 0.0 ###########!!!!!!!!!!!!!!!!!! 2019-08-08384train1 [1:5] = lx4385if (len(classes)>0 and cclass>=0) :386train1 [5+cclass] = 1.0387#print(iinn,tc[1],tc[0],ian)388
389ianNew = ian390try :391while ytrain[ii,tc[1],tc[0],ianNew,0]==1.0 :392ianNew = (ianNew+1) % n_anchors393if ian==ianNew : break394
395if ytrain[ii,tc[1],tc[0],ianNew,0]==1.0 :396cc2 = cc2 + 1397ii2 = ii2 + 1398
399except :400if printOK :401print('except: ', ii, tc[1], tc[0], ianNew)402print('lx5 ',lx5)403print('lx4 ',lx4)404print('in:',shape_in)405print(lx4[0]*shape_net[1],lx4[1]*shape_net[0])406print(np.array(shape_net)/np.array(shape_in[:2]))407
408ytrain[iinn,tc[1],tc[0],ianNew,:] = train1409
410return (ytrain, (cc1,cc2,cc3))411
412###############################################################################################
413
414def yTrainCreate3(subTrain5, classes, shape_net, shape_out, anchorsl,415train_scale, train_shape_in,416debug=False,417printOK=False) :418
419##assert(type(subTrain5)==type(np.array(np.zeros((1,)))))420assert(len(subTrain5)==len(train_scale))421assert(len(subTrain5)==len(train_shape_in))422
423ii = -100424
425n_anchors = len(anchorsl)426array_size= len(subTrain5)427
428ytrain = np.zeros([array_size,]+list(shape_out), dtype=np.float32)429
430train10 = np.zeros((5+len(classes)), dtype=np.float32)431
432grid_net = (shape_net[0]/shape_out[0],shape_net[1]/shape_out[1])433
434cc1, cc2 = 0, 0435
436for iinn, (ll,scale,shape_in) in enumerate(zip(subTrain5,train_scale,train_shape_in)) :437
438anmax = anchorsl439
440ii1, ii2 = 0, 0441
442lx5sort = np.array(ll.split()).reshape((-1,5))443temp = (lx5sort[:,3]).astype(float)*lx5sort[:,4].astype(float)444
445for lx5 in lx5sort[temp.argsort()] :446
447# x,y,w,h - x columns, y rows448lx4 = lx5[1:].astype(float)449
450cclass = -1451
452if len(classes)>0 :453try : cclass = classes.tolist().index(lx5[0])454except : cclass = -1455
456cc1 = cc1 + 1457ii1 = ii1 + 1458
459lx2max = lx5[3:].astype(np.float32)*scale460temp = np.array([np.minimum(lx2max,anmax[jj]) for jj in range(len(anmax))])461temp = temp[:,0]*temp[:,1]462iou = temp/(np.add(anmax[:,0]*anmax[:,1],lx2max[0]*lx2max[1])-temp)463
464ian = np.argmax(iou)465
466lx4[0:2] = ((lx4[:2]+lx4[2:]/2)*scale)467lx4[2:4] = (lx4[2:4]*scale)468
469lx4[0:2] = np.clip(lx4[0:2],0.0,(shape_net[1],shape_net[0]))470
471center_col_row = np.clip((lx4[0:2]/(grid_net[1],grid_net[0])).astype(int),4720,(shape_out[1]-1,shape_out[0]-1))473
474lx4[[0,2]] = lx4[[0,2]]/shape_net[1]475lx4[[1,3]] = lx4[[1,3]]/shape_net[0]476
477train1 = train10.copy().astype(float)478train1 [0] = 1.0 if cclass>=0 or len(classes)==0 else 0.0 ###########!!!!!!!!!!!!!!!!!! 2019-08-08479train1 [1:5] = lx4480if (len(classes)>0 and cclass>=0) :481train1 [5+cclass] = 1.0482
483ianNew = ian484try :485while ytrain[iinn,center_col_row[1],center_col_row[0],ianNew,0]==1.0 :486ianNew = (ianNew+1) % n_anchors487if ian==ianNew : break488
489if ytrain[iinn,center_col_row[1],center_col_row[0],ianNew,0]==1.0 :490cc2 = cc2 + 1491ii2 = ii2 + 1492
493except :494if printOK :495print('except: ', iinn, tc[1], tc[0], ianNew)496print('lx5 ',lx5)497print('lx4 ',lx4)498print('in:',shape_in)499print(lx4[0]*shape_net[1],lx4[1]*shape_net[0])500print(np.array(shape_net)/np.array(shape_in[:2]))501
502if ytrain[iinn,center_col_row[1],center_col_row[0],ianNew,0]==0.0 :503ytrain[iinn,center_col_row[1],center_col_row[0],ianNew,:] = train1504
505if debug : return (ytrain, (cc1,cc2))506return ytrain507
508###############################################################################################
509
510def drawTrain1(img1, ytrain1, shape_net, iiNumber=-1, show=True ) :511
512if show : plt.figure(figsize=(10,15))513plt.imshow(img1.squeeze())514
515iCount, rCount = 0, 0516for rr, a1 in enumerate(ytrain1) :517for cc, a2 in enumerate(a1) :518for aa, a4 in enumerate(a2) :519if a4[0]>0.5 :520iCount +=1521#print(len(pp), ii, rr, cc, aa, pp[0], np.round(pp[1:5],4),shape_net)522
523lx4 = a4[1:5].copy()524lx4[[0,2]], lx4[[1,3]] = lx4[[0,2]]*shape_net[1], lx4[[1,3]]*shape_net[0]525tl, dr = np.floor(lx4[0:2]-lx4[2:4]/2), np.ceil(lx4[0:2]+lx4[2:4]/2)526
527tl = np.clip(tl,0,(shape_net[1],shape_net[0]))528dr = np.clip(dr,0,(shape_net[1],shape_net[0]))529
530#print(len(pp), ii, rr, cc, aa, pp[0], np.round(pp[1:5],4),tl,dr,lx4)531
532class1, color = -1, 'r'533try :534class1 = a4[5:].argmax()535color = 'r' if a4[5+class1]>0.5 else 'b'536if color=='r' :537#print(lx4)538rCount = rCount+1539except :540class1, color = -1, 'r'541finally :542#print(lx4,class1,color)543plt.scatter(lx4[0],lx4[1], c=color)544
545border = [tl,[tl[0],dr[1]],dr,[dr[0],tl[1]]]546#print(lx4)547#print(border)548line = plt.Polygon(border, closed=True, fill=None, edgecolor=color)549plt.gca().add_line(line)550
551
552plt.title(str(iCount)+' '+str(rCount))553plt.title(str(iCount)+' '+str(rCount)+(' ('+str(iiNumber)+')' if iiNumber>=0 else ''))554
555if show :556plt.show()557plt.close()558print(iCount)559return560
561#drawTrain1 (xtrain[0],ytrain[0])
562
563###############################################################################################
564
565def drawPred1 (img1, pred1, anchors, grid_net, shape_net, threshold=0.5,566iiNumber=-1, f1=-1,567sigmoidOK=True, show=True, printOK=False) :568
569if show : plt.figure(figsize=(10,15))570plt.imshow(img1.squeeze())571
572iCount, rCount = 0, 0573
574ecolor = ['r','g','b','w']575
576for rr, a1 in enumerate(pred1) :577for cc, a2 in enumerate(a1) :578for aa, pr in enumerate(a2) :579if s(pr[0])>=threshold :580iCount = iCount + 1581lx4 = pr[1:5].copy()582if sigmoidOK : lx4[0:2] = s(lx4[0:2])583
584#print(lx4[0:2],rr,cc,grid_net)585lx4[0:2] = np.round((lx4[0:2]+np.array((cc,rr)))*(grid_net[1],grid_net[0])) # x, y586#print(lx4[0:2])587
588pclass = -1589if len(pr)>5 :590aacc = s(pr[5:])591iclass = aacc.argmax()592pclass = aacc[iclass]593marker = 'o' if pclass>0.5 else 's'594color = 'r' if pclass>0.5 else 'b'595if color=='r' : rCount = rCount+1596if len(pr)<=5 :597marker = 'o'598color = 'r'599
600plt.scatter(lx4[0],lx4[1], color=color, marker=marker)601
602lx4[2:4] = np.exp(lx4[2:4])*anchors[aa] # w,h603
604tl, dr = np.floor(lx4[0:2]-lx4[2:4]/2), np.ceil(lx4[0:2]+lx4[2:4]/2)605
606tl[0] = np.clip(tl[0],0,shape_net[1])607tl[1] = np.clip(tl[1],0,shape_net[0])608dr[0] = np.clip(dr[0],0,shape_net[1])609dr[1] = np.clip(dr[1],0,shape_net[0])610
611border = [tl,[tl[0],dr[1]],dr,[dr[0],tl[1]]]612line = plt.Polygon(border, closed=True, fill=None, edgecolor=ecolor[aa]) #'r')613plt.gca().add_line(line)614
615
616plt.title(str(iCount)+' '+str(rCount)+617(' ('+str(iiNumber)+')' if iiNumber>=0 else '')+(' ('+str(f1)+')' if f1>=0 else ''))618
619if show :620plt.show()621plt.close()622print(iCount)623
624return