def count_flow(nets, img, cn=[0, 0], size=512, work=1): if not isinstance(nets, list): nets = [nets] img = np.asarray(img)[None, cn, :, :] h, w = img.shape[-2:] if not (h == w == size): img = resize(img, (size, size)) y = np.zeros((1, 3) + img.shape[2:], img.dtype) style = np.zeros((1, 256), img.dtype) def one(net, img): i, s = net(img) y[:] += i style[:] += s if work > 1 and len(nets) > 1: from concurrent.futures import ThreadPoolExecutor pool = ThreadPoolExecutor(max_workers=work, thread_name_prefix="flow") for net in nets: pool.submit(one, net, img) pool.shutdown(wait=True) else: for net in nets: one(net, img) if len(nets) > 0: y /= len(nets) style /= len(nets) if not (h == w == size): y = resize(y, (h, w)) return y[0].transpose(1, 2, 0), style
def recognize(img): img = planer.asarray(img) x = planer.resize(img, (224, 224)) x = x[None, :,:,:].astype('float32') x = (x/255).transpose(0, 3, 1, 2) y = np.argmax(net(x), axis=-1) return classes[str(y[0])]
def get_flow(nets, img, cn=[0, 0], sample=1, size=512, tile=True, work=1, callback=progress): if not isinstance(nets, list): nets = [nets] if img.ndim == 2: img = np.asarray(img[None, :, :])[cn] else: img = np.asarray(img.transpose(2, 0, 1))[cn] (_, H, W), k = img.shape, sample h, w = (size, size) if not tile else (max(size, int(H * k)), max(int(W * k), size)) needresize = ((k != 1 or min(H, W) < size) and tile) or (not (H == W == size) and not tile) simg = img if not needresize else resize(img[:, :, :], (h, w)) rcs = grid_slice(h, w, size, size // 10) flow = np.zeros((3, h, w), dtype=simg.dtype) style = np.zeros((1, 256), dtype=simg.dtype) count = np.zeros(simg.shape[1:], 'uint8') def one(sr, sc, sz, wk, s=[0]): flw_prb, sty = count_flow(nets, simg[:, sr, sc], slice(None), sz, wk) flow[:, sr, sc] += flw_prb.transpose(2, 0, 1) style[:] += sty count[sr, sc] += 1 s[0] += 1 callback(s[0], len(rcs)) if work > 1 and len(rcs) > 1: from concurrent.futures import ThreadPoolExecutor pool = ThreadPoolExecutor(max_workers=work, thread_name_prefix="net") for i in range(len(rcs)): pool.submit(one, *rcs[i], size, 1) pool.shutdown(wait=True) else: for slr, slc in rcs: one(slr, slc, size, work) flow /= count style /= len(rcs) if needresize: flow = resize(flow, (H, W)) flow[2] *= -1 np.exp(flow[2], out=flow[2]) flow[2] += 1 np.divide(1, flow[2], out=flow[2]) return flow.transpose(1, 2, 0), style
def get_face_key(img, scale=True): img = planer.asarray(img, dtype='float32') x = planer.resize(img, (224, 224)) / 255 if img.ndim == 2: x = x[:, :, None] x = x.transpose(2, 0, 1)[None, :, :, :] rc = net(x).reshape(-1, 2)[:, ::-1] * 50 + 100 rc = planer.asnumpy(rc) if scale: rc *= np.array(img.shape[:2]) / 224 return rc
def get_flow(image, scale=(400,600)): shp = image.shape[:2] if isinstance(scale, float): scale = shp[0]*scale, shp[1]*scale image = planer.np.asarray(image) scale = int(scale[0]/8+0.5)*8, int(scale[1]/8+0.5)*8 k = shp[0]/scale[0], shp[1]/scale[1] img = planer.resize(image, scale) img = img.transpose(2,0,1)[None,:]/255 return net.run(None, {'inputc': img}) +(k,)
def preprocess(img, size): (h, w), (H, W) = img.shape[:2], size k = min(H / h, W / w) nh, nw = int(h * k + 0.5) // 2 * 2, int(w * k + 0.5) // 2 * 2 img.shape = img.shape[:2] + (-1, ) img = resize(img, (nh, nw), backend=np) k = np.ones((1, 1, 3), dtype='uint8') img = img / (k * 255) dh, dw = (H - nh) // 2, (W - nw) // 2 pads = [(dh, dh), (dw, dw), (0, 0)] return np.pad(img, pads, constant_values=0.5)
def get_mark(img, sample=1): shp = np.array(img.shape[:2]) size = (shp * sample // 32) * 32 size = size.astype(int) img = img.astype(np.float32) img = resize(img, size) img = global_norm(img) img = img.transpose(2, 0, 1) k = (size / shp).astype('float32') k[:] = 1, 1 size = size.astype('float32') data = { 'image': img[None, :], 'im_shape': size[None, :], 'scale_factor': k[None, :] } y = list(net.run(None, data)) rs, cs = np.mgrid[:shp[0], :shp[1]] rs = rs * (size[0] - 1) / (shp[0] - 1) cs = cs * (size[1] - 1) / (shp[1] - 1) rs, cs = rs.astype(int), cs.astype(int) y[-1] = y[-1][:, rs, cs] return y
from skimage import io from matplotlib import pyplot as plt from time import time # get planer array library, numpy # pal = planer.core(numpy) # get planer array library, cupy pal = planer.core(cupy) img = io.imread('test.jpg') x = (img/255.0).transpose(2, 0, 1) x = x[None, :, :, :].astype('float32') x = pal.array(x) x = resize(x, (224, 224)) net = read_net('mobile') print('load done!') net(x) print('start timing!') start = time() for i in range(10): y = net(x) print('planer mobilenet-v1 time (x10):', time()-start) y = pal.argmax(y, axis=-1) rst = classes[int(y[0])] print('result:', rst)
def makesize32(img): h, w = img.shape[-2:] w = w // 32 * 32 h = h // 32 * 32 return resize(img, (h, w))
def flow_map(img, flow): np = planer.np des = np.mgrid[0:224, 0:224].transpose(1, 2, 0) + flow des = planer.resize(des, img.shape[:2]) des *= np.array(img.shape[:2]).reshape(1, 1, 2) / 224 return planer.mapcoord(img, *des.transpose(2, 0, 1))
import sys sys.path.append('../../') import planer from planer import read_net, resize import cupy from matplotlib import pyplot as plt import matplotlib.patches as patches from time import time from skimage import io pal = planer.core(cupy) img = io.imread('dog.jpg')[:, :, ::-1] x = pal.array(img.astype('float32') / 255.0) x = x.transpose(2, 0, 1)[None, :, :, :] x = resize(x, (416, 416)) anchors_all = pal.array([ 10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326 ]).reshape(9, 2) def sigmoid(x): return 1 / (1 + pal.exp(-x)) def post_process(fmps, anchors, thresh=0.4): # batch results bbox = {}
def greedy_search(raw, blank=0): max_id = raw.argmax(2).ravel() msk = max_id[1:] != max_id[:-1] max_id = max_id[1:][msk] return max_id[max_id != blank] # net = planer.onnx2planer('./crnn.onnx') net = planer.read_net('./crnn-ocr') x = page()[:40, :150].astype('float32') w = 48 * x.shape[1] // x.shape[0] x = planer.resize(x, (48, w)) x = (x - 0.5) / (90 / 255) x = x[None, None, :, :] net.timer = {} start = time() y = net(x) print(time() - start) for k in net.timer: print(k, net.timer[k]) pred = greedy_search(y) pred = [dic[i] for i in pred] text = ''.join(pred)
def flow_map(img, flow): des = np.mgrid[0:224, 0:224] + flow.transpose(2, 0, 1) des = planer.resize(des, img.shape[:2]) des *= np.array(img.shape[:2]).reshape(2, 1, 1) / 224 rst = planer.mapcoord(img.transpose(2, 0, 1), *des * 1) return rst.transpose(1, 2, 0).astype(np.uint8)
# generate check board def check_board(shp, d=20): checkboard = np.zeros(shp, dtype=np.uint8) msk = np.tile([False] * d + [True] * d, 1000) checkboard[msk[:shp[0]]] += 128 checkboard[:, msk[:shp[1]]] += 128 return ((checkboard > 0) * 255).astype(np.uint8) if __name__ == '__main__': net = planer.read_net('./face_key') face = imread('./face.jpg') x = np.array(face.transpose(2, 0, 1)[None, :, :, :]) x = planer.resize(x, (224, 224)).astype(np.float32) start = time() y = net(x / 255) print(time() - start) rc = y.reshape(-1, 2)[:, ::-1] * 50 + 100 thin = flow_map(face, count_flow(rc, fac=-10)) fat = flow_map(face, count_flow(rc, fac=10)) grid = check_board(face.shape, 30) thin_grid = flow_map(grid, count_flow(rc, fac=-10)) fat_grid = flow_map(grid, count_flow(rc, fac=10)) # 绘图