Changeset - a00c974af8ae
[Not reviewed]
default
0 3 0
Laman - 6 years ago 2019-06-11 17:27:57

experimental single corner Hakugen
3 files changed with 37 insertions and 14 deletions:
0 comments (0 inline, 0 general)
exp/kerokero/prepare_data.py
Show inline comments
 
@@ -62,6 +62,29 @@ class Sample:
 
		grid=list(map(lambda p: list(2*p/self.SIDE-EPoint(1,1)), grid))
 
		return (img,grid)
 

	
 
	def cut(self):
 
		width=max(p.x for p in self.grid)-min(p.x for p in self.grid)
 
		height=max(p.y for p in self.grid)-min(p.y for p in self.grid)
 
		kx=width/4
 
		ky=height/4
 
		n=self.SIDE
 
		for p in self.grid:
 
			shift=self._createNoise(0.2)
 
			abcd=[[p.x-kx,p.y-ky],[p.x-kx,p.y+ky],[p.x+kx,p.y+ky],[p.x+kx,p.y-ky]]
 
			abcd_=[[shift.x,shift.y],[shift.x,n+shift.y],[n+shift.x,n+shift.y],[n+shift.x,shift.y]]
 
			m=cv.getPerspectiveTransform(np.float32(abcd),np.float32(abcd_))
 
			t1=getTranslation(-n/2,-n/2)
 
			mir=getMirroring()
 
			proj=getProjection()
 
			rot=getRotation()
 
			t2=getTranslation(n/2,n/2)
 
			for mi in [t1,mir,proj,rot,t2]:
 
				m=np.matmul(mi,m)
 
			img=cv.warpPerspective(self.img,m,(self.SIDE,self.SIDE))
 
			img=np.uint8(img)
 
			point=p.transform(m)*2/self.SIDE-EPoint(1,1)
 
			yield (img,[point.x,point.y])
 

	
 
	def _getCenter(self):
 
		(a,b,c,d)=self.grid
 
		p=Line.fromPoints(a,c)
 
@@ -77,9 +100,9 @@ class Sample:
 
		scale=getScale(self.SIDE/(wg*(1+left+right)), self.SIDE/(hg*(1+top+bottom)))
 
		return np.matmul(scale,t2)
 

	
 
	def _createNoise(self):
 
	def _createNoise(self,mag=0.05):
 
		alpha=random.uniform(0,math.pi*2)
 
		d=random.uniform(0,self.SIDE*0.05)
 
		d=random.uniform(0,self.SIDE*mag)
 
		return EPoint(math.cos(alpha)*d, math.sin(alpha)*d)
 

	
 
	def show(self):
 
@@ -117,9 +140,10 @@ def harvestDir(path):
 
			sample=Sample(img,b.grid)
 
			# sample.show()
 
			# (transformedImg,label)=sample.transform()
 
			(transformedImg,label)=sample.rectify()
 
			# Sample(np.uint8(transformedImg),map(lambda c: (EPoint(*c)+EPoint(1,1))*Sample.SIDE/2,label)).show()
 
			yield (transformedImg,label)
 
			# (transformedImg,label)=sample.rectify()
 
			for (transformedImg,label) in sample.cut():
 
				Sample(np.uint8(transformedImg),[(EPoint(*label)+EPoint(1,1))*Sample.SIDE/2]).show()
 
				yield (transformedImg,label)
 

	
 

	
 
def loadDataset(root):
exp/kerokero/test.py
Show inline comments
 
@@ -25,7 +25,7 @@ model.summary()
 
log.info("loading data...")
 
with np.load(args.data) as data:
 
	testImages=(np.float32(data["testImages"])/128-1).reshape((-1,224,224,1))
 
	testLabels=data["testLabels"].reshape((-1,4,2))
 
	testLabels=data["testLabels"].reshape((-1,1,2))
 
log.info("done")
 

	
 
log.info(model.evaluate(testImages,testLabels))
 
@@ -34,8 +34,8 @@ for img in testImages:
 
	label=model.predict(np.reshape(img,(1,224,224,1)))
 
	print(label)
 
	points=[]
 
	for i in range(4):
 
	for i in range(1):
 
		points.append(EPoint((label[0][i][0]+1)*112,(label[0][i][1]+1)*112))
 
	corners=Corners(points)
 
	corners=points
 
	sample=Sample(np.uint8((img+1)*128),corners)
 
	sample.show()
exp/kerokero/train.py
Show inline comments
 
@@ -20,7 +20,6 @@ parser=argparse.ArgumentParser()
 
parser.add_argument("data")
 
parser.add_argument("--load_model")
 
parser.add_argument("--save_model",default="/tmp/gogo-{0:03}.h5")
 
parser.add_argument("--load_hints")
 
parser.add_argument("--log_dir",default="/tmp/tflogs")
 
parser.add_argument("--epochs",type=int,default=100)
 
parser.add_argument("--initial_epoch",type=int,default=0)
 
@@ -66,10 +65,10 @@ def createCNN():
 

	
 
	model.add(Dense(500,activation="relu"))
 
	model.add(Dense(90,activation="relu"))
 
	model.add(Dense(8))
 
	model.add(Reshape((4,2)))
 
	model.add(Dense(2))
 
	model.add(Reshape((1,2)))
 

	
 
	model.compile(optimizer="rmsprop",loss=averageDistance,metrics=["mae","accuracy"])
 
	model.compile(optimizer="rmsprop",loss="mae",metrics=["mae","accuracy"])
 
	return model
 

	
 

	
 
@@ -82,9 +81,9 @@ else:
 
log.info("loading data...")
 
with np.load(args.data) as data:
 
	trainImages=(np.float32(data["trainImages"])/128-1).reshape((-1,224,224,1))
 
	trainLabels=data["trainLabels"].reshape((-1,4,2))
 
	trainLabels=data["trainLabels"].reshape((-1,1,2))
 
	testImages=(np.float32(data["testImages"])/128-1).reshape((-1,224,224,1))
 
	testLabels=data["testLabels"].reshape((-1,4,2))
 
	testLabels=data["testLabels"].reshape((-1,1,2))
 
log.info("done")
 

	
 
n=len(trainImages)
0 comments (0 inline, 0 general)