def batchnorm(x, beta, gamma, mean, var, name="batchnorm"): assert len(x.shape) == 4, 'batch norm for 4d tensor' return hcl.compute( x.shape, lambda n, c, h, w: (x[n, c, h, w] - mean[c]) / hcl.sqrt(var[c]) * gamma[c] + beta[c], name, attrs=OrderedDict([('app_name', tvm.make.StringImm('batchnorm'))]))
def sobelAlgo(A, Fx, Fy): B = hcl.compute((height, width), lambda x,y :A[x][y][0]+A[x][y][1]+A[x][y][2],"B", dtype=hcl.Float()) r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Gx = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+r,y+c]*Fx[r,c],axis=[r,c]), B[x,y]), "Gx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Gy = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+t,y+g]*Fy[t,g],axis=[t,g]), B[x,y]), "Gy") return hcl.compute((height, width), lambda x,y:(hcl.sqrt(Gx[x][y]*Gx[x][y]+Gy[x][y]*Gy[x][y]))/4328*255, dtype = hcl.Float())
def sobelAlgo(A, Fx, Fy): B = hcl.compute((height+2, width+2), lambda x,y:A[x][y][0]+A[x][y][1]+A[x][y][2], "B") r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Gx = hcl.compute((height, width), lambda y,x:hcl.sum(B[y+r, x+c]*Fx[r,c], axis = [r,c]), "Gx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Gy = hcl.compute((height, width), lambda y,x:hcl.sum(B[y+t, x+g]*Fy[t,g], axis = [t,g]), "Gy") return hcl.compute((height, width), lambda y,x:(hcl.sqrt(Gx[y][x]*Gx[y][x]+Gy[y][x]*Gy[y][x]))/4328*255)
def transition(self, sVals, action, bounds, trans, goal): di = hcl.scalar(0, "di") dj = hcl.scalar(0, "dj") dk = hcl.scalar(0, "dk") dl = hcl.scalar(0, "dl") dm = hcl.scalar(0, "dm") dn = hcl.scalar(0, "dn") do = hcl.scalar(0, "do") mag = hcl.scalar(0, "mag") # Check if moving from a goal state di[0] = (sVals[0] - goal[0]) * (sVals[0] - goal[0]) dj[0] = (sVals[1] - goal[1]) * (sVals[1] - goal[1]) dk[0] = (sVals[2] - goal[2]) * (sVals[2] - goal[2]) dl[0] = (sVals[3] - goal[3]) * (sVals[3] - goal[3]) dm[0] = (sVals[4] - goal[4]) * (sVals[4] - goal[4]) dn[0] = (sVals[5] - goal[5]) * (sVals[5] - goal[5]) do[0] = (sVals[6] - goal[6]) * (sVals[6] - goal[6]) mag[0] = hcl.sqrt(di[0] + dj[0] + dk[0] + dl[0] + dm[0] + dn[0] + do[0]) # Check if moving from an obstacle with hcl.if_(mag[0] <= 5.0): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[0] <= bounds[0, 0], sVals[0] >= bounds[0, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[1] <= bounds[1, 0], sVals[1] >= bounds[1, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[2] <= bounds[2, 0], sVals[2] >= bounds[2, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[3] <= bounds[3, 0], sVals[3] >= bounds[3, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[4] <= bounds[4, 0], sVals[4] >= bounds[4, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[5] <= bounds[5, 0], sVals[5] >= bounds[5, 1])): trans[0, 0] = 0 with hcl.elif_( hcl.or_(sVals[6] <= bounds[6, 0], sVals[6] >= bounds[6, 1])): trans[0, 0] = 0 # Standard move with hcl.else_(): trans[0, 0] = 1.0 trans[0, 1] = sVals[0] + action[0] trans[0, 2] = sVals[1] + action[1] trans[0, 3] = sVals[2] + action[2] trans[0, 4] = sVals[3] + action[3] trans[0, 5] = sVals[4] + action[4] trans[0, 6] = sVals[5] + action[5] trans[0, 7] = sVals[6] + action[6]
def sobel(A,Gx,Gy): B = hcl.compute((height,width), lambda x,y: A[x][y][0]+A[x][y][1]+A[x][y][2],"B") r = hcl.reduce_axis(0,3) c = hcl.reduce_axis(0,3) D = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+r,y+c]*Gx[r,c],axis=[r,c]), B[x,y]), "Gx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) E = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+t,y+g]*Gy[t,g],axis=[t,g]), B[x,y]), "Gy") return hcl.compute((height,width), lambda x,y:hcl.sqrt(D[x][y]*D[x][y]+E[x][y]*E[x][y])/4328*255)
def sobel(A,Gx,Gy): B = hcl.compute((height,width), lambda x,y: A[x][y][0]+A[x][y][1]+A[x][y][2], "B") r = hcl.reduce_axis(0,3) c = hcl.reduce_axis(0,3) # D = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+r,y+c]*Gx[r,c],axis=[r,c]), B[x,y]), "xx") D = hcl.compute((height-2, width-2), lambda x,y: hcl.sum(B[x+r, y+c]*Gx[r,c], axis=[r,c], name="sum1"), "xx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) # E = hcl.compute((height, width), lambda x,y: hcl.select(hcl.and_(x>0,x<(height-1),y>0,y<(width-1)), hcl.sum(B[x+t,y+g]*Gy[t,g],axis=[t,g]), B[x,y]), "yy") E = hcl.compute((height-2, width-2), lambda x,y: hcl.sum(B[x+t, y+g]*Gy[t,g], axis=[t,g]), "yy") return hcl.compute((height-2,width-2), lambda x,y:hcl.sqrt(D[x][y]*D[x][y]+E[x][y]*E[x][y])*0.05891867,"Fimg")
def sobel(RGB,Gx,Gy): B = hcl.compute((height,width), lambda x,y: RGB[x][y][8:0] + RGB[x][y][16:8] + RGB[x][y][24:16], "B") r = hcl.reduce_axis(0,3) c = hcl.reduce_axis(0,3) D = hcl.compute((height-2, width-2), lambda x,y: hcl.sum(B[x+r, y+c]*Gx[r,c], axis=[r,c], name="sum1"), "xx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) E = hcl.compute((height-2, width-2), lambda x,y: hcl.sum(B[x+t, y+g]*Gy[t,g], axis=[t,g]), name="sum2"), "yy") return hcl.compute((height-2,width-2), lambda x,y:hcl.sqrt(D[x][y]*D[x][y]+E[x][y]*E[x][y])*0.05891867, "Fimg")
def reward(self, sVals, action, bounds, goal, trans): di = hcl.scalar(0, "di") dj = hcl.scalar(0, "dj") dk = hcl.scalar(0, "dk") dl = hcl.scalar(0, "dl") dm = hcl.scalar(0, "dm") dn = hcl.scalar(0, "dn") do = hcl.scalar(0, "do") mag = hcl.scalar(0, "mag") rwd = hcl.scalar(0, "rwd") # Check if moving from a collision state, if so, assign a penalty with hcl.if_( hcl.or_(sVals[0] <= bounds[0, 0], sVals[0] >= bounds[0, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[1] <= bounds[1, 0], sVals[1] >= bounds[1, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[2] <= bounds[2, 0], sVals[2] >= bounds[2, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[3] <= bounds[3, 0], sVals[3] >= bounds[3, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[4] <= bounds[4, 0], sVals[4] >= bounds[4, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[5] <= bounds[5, 0], sVals[5] >= bounds[5, 1])): rwd[0] = -400 with hcl.elif_( hcl.or_(sVals[6] <= bounds[6, 0], sVals[6] >= bounds[6, 1])): rwd[0] = -400 with hcl.else_(): # Check if moving from a goal state di[0] = (sVals[0] - goal[0]) * (sVals[0] - goal[0]) dj[0] = (sVals[1] - goal[1]) * (sVals[1] - goal[1]) dk[0] = (sVals[2] - goal[2]) * (sVals[2] - goal[2]) dl[0] = (sVals[3] - goal[3]) * (sVals[3] - goal[3]) dm[0] = (sVals[4] - goal[4]) * (sVals[4] - goal[4]) dn[0] = (sVals[5] - goal[5]) * (sVals[5] - goal[5]) do[0] = (sVals[6] - goal[6]) * (sVals[6] - goal[6]) mag[0] = hcl.sqrt(di[0] + dj[0] + dk[0] + dl[0] + dm[0] + dn[0] + do[0]) with hcl.if_(mag[0] <= 5.0): rwd[0] = 1000 # Standard move with hcl.else_(): rwd[0] = 0 return rwd[0]
def sobel(A, Gx, Gy): r = hcl.reduce_axis(0,3) c = hcl.reduce_axis(0,3) B = hcl.compute((height-2,width-2), lambda x,y: hcl.sum(A[x+r,y+c]*Gx[r,c], axis=[r,c], name="sum1"), name="B", dtype=hcl.Float()) t = hcl.reduce_axis(0,3) g = hcl.reduce_axis(0,3) C = hcl.compute((height-2,width-2), lambda x,y: hcl.sum(A[x+t,y+g]*Gy[t,g], axis=[t,g], name="sum2"), name="C", dtype=hcl.Float()) return hcl.compute((height-2,width-2), lambda x, y :hcl.sqrt(B[x,y]*B[x,y] + C[x,y]*C[x,y])/4328*255, name="output", dtype=hcl.Float())
def sobel(A, Gx, Gy): B = hcl.compute((height, width), lambda x, y: A[x][y][0] + A[x][y][1] + A[x][y][2], "B") r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) D = hcl.compute((height - 2, width - 2), lambda x, y: hcl.sum( B[x + r, y + c] * Gx[r, c], axis=[r, c], name="sum1"), "D") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) E = hcl.compute((height - 2, width - 2), lambda x, y: hcl.sum( B[x + t, y + g] * Gy[t, g], axis=[t, g], name="sum2"), "E") # constant factor to normalize the output return hcl.compute((height - 2, width - 2), lambda x, y: hcl.sqrt(D[x][ y] * D[x][y] + E[x][y] * E[x][y]) * 0.05891867, "Fimg")
def batch_norm(data, gamma, beta, moving_mean, moving_var, axis=1, epsilon=10**-7, center=1, scale=1, name="batch_norm"): if axis < 0: axis = len(data.shape) - 1 mred = [] vred = [] size = 1.0 for i in range(len(data.shape)): if not i == axis: mred.append(hcl.reduce_axis(0, data.shape[i], "mred" + str(i))) vred.append(hcl.reduce_axis(0, data.shape[i], "vred" + str(i))) size = size * data.shape[i] new_shape = (data.shape[axis], ) def insert_axis(axis, red, *indices): idx = [] cur_red = 0 for i in range(len(data.shape)): if i == axis: idx.append(indices[0]) else: idx.append(red[cur_red]) cur_red = cur_red + 1 return tuple(idx) def get_axis(axis, *indices): indices = list(indices[0]) return (indices[axis], ) out = hcl.compute(data.shape, lambda *x: (data[x] - moving_mean[get_axis(axis, x)]) / (hcl.sqrt(moving_var[get_axis(axis, x)] + epsilon) ) * gamma[get_axis(axis, x)] + beta[get_axis(axis, x)], name=name, dtype=data.dtype) return out, moving_mean, moving_var
def sobel(imgF, Gx, Gy): A = hcl.compute((height + 2, width + 2), lambda x, y: imgF[x][y][0] + imgF[x][y][1] + imgF[x][y][2], "A") r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) resX = hcl.compute((height, width), lambda x, y: hcl.sum( A[x + r, y + c] * Gx[r, c], axis=[r, c], name="sum1"), "X") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) resY = hcl.compute((height, width), lambda x, y: hcl.sum( A[x + t, y + g] * Gy[t, g], axis=[t, g], name="sum2"), "Y") return hcl.compute((height, width), lambda x, y: hcl.sqrt(resX[x][ y] * resX[x][y] + resY[x][y] * resY[x][y]) / 4328 * 255, "R")
def sobelAlgo(A, B, Fx, Fy): def rgb_sum(x, y): B[x][y] = A[x][y][0] + A[x][y][1] + A[x][y][2] hcl.mutate(B.shape, lambda x, y: rgb_sum(x, y)) #B = hcl.compute((height+2, width+2), lambda x,y:A[x][y][0]+A[x][y][1]+A[x][y][2], "B") r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Gx = hcl.compute( (height, width), lambda y, x: hcl.sum(B[y + r, x + c] * Fx[r, c], axis=[r, c]), "Gx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Gy = hcl.compute( (height, width), lambda y, x: hcl.sum(B[y + t, x + g] * Fy[t, g], axis=[t, g]), "Gy") return hcl.compute( (height, width), lambda y, x: (hcl.sqrt(Gx[y][x] * Gx[y][x] + Gy[y][x] * Gy[y][x]) * 0.05891867))
def reward(self, sVals, action, bounds, goal, trans): dx = hcl.scalar(0, "dx") dy = hcl.scalar(0, "dy") mag = hcl.scalar(0, "mag") rwd = hcl.scalar(0, "rwd") # Check if moving from a collision state, if so, assign a penalty with hcl.if_(hcl.or_(sVals[0] < bounds[0,0] + 0.2, sVals[0] > bounds[0,1] - 0.2)): rwd[0] = -400 with hcl.elif_(hcl.or_(sVals[1] < bounds[1,0] + 0.2, sVals[1] > bounds[1,1] - 0.2)): rwd[0] = -400 with hcl.else_(): # Check if moving from a goal state dx[0] = sVals[0] - goal[0,0] dy[0] = sVals[1] - goal[0,1] mag[0] = hcl.sqrt((dx[0] * dx[0]) + (dy[0] * dy[0])) with hcl.if_(hcl.and_(mag[0] <= 1.0, sVals[2] <= goal[1,1], sVals[2] >= goal[1,0])): rwd[0] = 1000 # Standard move with hcl.else_(): rwd[0] = 0 return rwd[0]
def sobel(A, Gx, Gy): D = hcl.compute((height, width), lambda x, y: (A[x][y][0], A[x][y][1], A[x][y][2]), dtype=ts) B = hcl.compute((height, width), lambda x, y: D[x][y].fa + D[x][y].fb + D[x][y].fc, "B", dtype=hcl.Float()) r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Fx = hcl.compute( (height - 2, width - 2), lambda x, y: hcl.sum(B[x + r, y + c] * Gx[r, c], axis=[r, c]), "xx") t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Fy = hcl.compute( (height - 2, width - 2), lambda x, y: hcl.sum(B[x + t, y + g] * Gy[t, g], axis=[t, g]), "yy") return hcl.compute((height - 2, width - 2), lambda x, y: hcl.sqrt(Fx[ x][y] * Fx[x][y] + Fy[x][y] * Fy[x][y]) * 0.05891867, "Fimg")
def Gaussian_Sobel_filters(A, G, Fx, Fy): h = hcl.reduce_axis(0, kernel_size) w = hcl.reduce_axis(0, kernel_size) B = hcl.compute( (height, width), lambda y, x: hcl.select( hcl.and_(y > (k - 1), y < (width - k), x > (k - 1), x < (height - k)), hcl.sum(A[y + w, x + h] * G[w, h], axis=[w, h]), B[y, x]), "B", dtype=hcl.Float()) # Sobel Filters r = hcl.reduce_axis(0, 3) c = hcl.reduce_axis(0, 3) Gx = hcl.compute( (height, width), lambda y, x: hcl.select( hcl.and_(y > (k - 1), y < (width - k), x > (k - 1), x < (height - k)), hcl.sum(B[y + r, x + c] * Fx[r, c], axis=[r, c]), B[y, x]), "Gx", dtype=hcl.Float()) t = hcl.reduce_axis(0, 3) g = hcl.reduce_axis(0, 3) Gy = hcl.compute( (height, width), lambda y, x: hcl.select( hcl.and_(y > (k - 1), y < (width - k), x > (k - 1), x < (height - k)), hcl.sum(B[y + t, x + g] * Fy[t, g], axis=[t, g]), B[y, x]), "Gy", dtype=hcl.Float()) # return the intensity matrix and the edge direction matrix? return hcl.compute( (height, width), lambda y, x: (hcl.sqrt(Gx[y][x] * Gx[y][x] + Gy[y][x] * Gy[y][x])) / 4328 * 255, dtype=hcl.Float())
def sqrt(input1, name='sqrt'): return hcl.compute(input1.shape, lambda *x: hcl.sqrt(input1[x]), name=name)
def math_func(A, B): real, imag = hlib.ip.single_fft_hls(A, B) return hcl.compute( (length, ), lambda x: hcl.sqrt(real[x] * real[x] + imag[x] * imag[x]), name="abs")
def avg(in1, in2): ll = hcl.scalar(in1, "in1") lr = hcl.scalar(in2, "in2") return hcl.sqrt(ll.v * ll.v + lr.v * lr.v)/4328*255
C = hcl.placeholder((1, )) P = hcl.placeholder((1, )) xdiv1 = hcl.placeholder((1, )) xdiv2 = hcl.placeholder((1, )) d1Local = hcl.placeholder((1, )) d2Local = hcl.placeholder((1, )) #------------------------------------------# with hcl.stage() as s: xlogterm = hcl.scalar(0) xlogterm[0] = hcl.log(S[0] / X[0]) xpowerterm = hcl.scalar(0) xpowerterm[0] = 0.5 * sigma[0] * sigma[0] xnum = hcl.scalar(0) xnum[0] = xlogterm[0] + (r[0] + xpowerterm[0]) * T[0] xsqrtterm = hcl.scalar(0) xsqrtterm[0] = hcl.sqrt(T[0]) xden = hcl.scalar(0) xden[0] = sigma[0] * xsqrtterm[0] #xdiv1 = hcl.scalar(0) xdiv1[0] = xnum[0] / xden[0] #xdiv2 = hcl.scalar(0) xdiv2[0] = xdiv1[0] - xden[0] futurevaluex = hcl.scalar(0) futurevaluex[0] = X[0] * hcl.exp(-r[0] * T[0]) #--------------------------------------------------# #Calculate N(d1), also N(-d1)=1 - N(d1) d1NPrimeofX = hcl.scalar(0) d1NPrimeofX[0] = hcl.exp( -(xdiv1[0] * xdiv1[0]) * 0.5) * 0.39894228040143270286 d1K2 = hcl.scalar(0) d1K2[0] = 1 / ((xdiv1[0] * 0.2316419) + 1.0)