def discrim_l2(self, y, y_, step): """ facial semantic feature loss evaluate loss use l1 at pixel space :param y: input photo, numpy array [H, W, C] :param y_: generated image, tensor [B, C, W, H] :param step: train step :return: l1 loss in pixel space """ img1 = parse_evaluate(y.astype(np.uint8), cp=self.parsing, cuda=self.cuda) y_ = y_.cpu().detach().numpy() y_ = np.squeeze(y_, axis=0) y_ = np.swapaxes(y_, 0, 2) * 255 img2 = parse_evaluate(y_.astype(np.uint8), cp=self.parsing, cuda=self.cuda) edge_img1 = utils.img_edge(img1).astype(np.float32) edge_img2 = utils.img_edge(img2).astype(np.float32) w_g = 1.0 w_r = 1.0 if step % self.args.eval_prev_freq == 0: path = os.path.join(self.prev_path, "l2_{0}.jpg".format(step)) edge1_v3 = 255. - ops.fill_grey(edge_img1) edge2_v3 = 255. - ops.fill_grey(edge_img2) merge = ops.merge_4image(y, y_, edge1_v3, edge2_v3, transpose=False) cv2.imwrite(path, merge) return np.mean(np.abs(w_r * edge_img1 - w_g * edge_img2))
def process_item(self, path, save, cuda): """ 预处理 change database to 64x64 edge pictures :param path: 图片路径 512x512 :param save: 是否将处理好的图片保存本地 :param cuda: gpu speedup """ # log.info(path) img = utils.evalute_face(path, self.args.parsing_checkpoint, cuda) img = utils.img_edge(img) if img.shape[0] != 64: img = cv2.resize(img, (64, 64), interpolation=cv2.INTER_AREA) if save: cv2.imwrite(path, img) return img
def capture(path, tensor1, tensor2, parse, cuda): """ imitator 快照 :param cuda: use gpu :param path: save path :param tensor1: input photo :param tensor2: generated image :param parse: parse checkpoint's path """ img1 = ops.tensor_2_image(tensor1)[0].swapaxes(0, 1).astype(np.uint8) img2 = ops.tensor_2_image(tensor2)[0].swapaxes(0, 1).astype(np.uint8) img1 = cv2.resize(img1, (512, 512), interpolation=cv2.INTER_LINEAR) img3 = utils.faceparsing_ndarray(img1, parse, cuda) img4 = utils.img_edge(img3) img4 = 255 - ops.fill_gray(img4) image = ops.merge_4image(img1, img2, img3, img4, transpose=False) cv2.imwrite(path, image)
def inference(self, cp_name, photo_path, cuda): """ feature extractor: 由图片生成捏脸参数 :param cuda: gpu speed up :param cp_name: checkpoint's path :param photo_path: input photo's path :return: params [1, params_cnt] """ img = cv2.imread(photo_path) scaled = align.align_face(img, size=(64, 64)) self.load_checkpoint(cp_name, training=False, cuda=cuda) img = utils.faceparsing_ndarray(scaled, self.args.parsing_checkpoint, cuda) img = utils.img_edge(img) with torch.no_grad: input = torch.from_numpy(img) input = input.view([1, 1, 64, 64]) params_ = self(input) log.info(params_) return params_
elif args.phase == "faceparsing": log.info("faceparsing") im = utils.evalute_face("./output/face/db_0000_3.jpg", args.parsing_checkpoint, cuda) cv2.imwrite("./output/eval.jpg", im) elif args.phase == "align": path = '../export/star' for file in os.listdir(path): p = os.path.join(path, file) log.info(p) p2 = os.path.join(path, "a-" + file) al = align.face_features(p, p2) ev = utils.parse_evaluate(al, args.parsing_checkpoint, cuda=cuda) p = os.path.join(path, "b-" + file) cv2.imwrite(p, ev) ev = 255 - utils.img_edge(ev) p = os.path.join(path, "c-" + file) cv2.imwrite(p, ev) elif args.phase == "dataset": dataset = FaceDataset(args, "test") dataset.pre_process(cuda) elif args.phase == "preview": log.info("preview picture") path = "../export/regular/model.jpg" img = cv2.imread(path) img2 = utils.parse_evaluate(img, args.parsing_checkpoint, cuda) img3 = utils.img_edge(img2) img3_ = ops.fill_grey(img3) img4 = align.face_features(path) log.info("{0} {1} {2} {3}".format(img.shape, img2.shape, img3_.shape, img4.shape))
def image_transfer(self, im_path): if im_path.find('.jpg') >= 0: img = utils.evalute_face(im_path, self.cp, False) img = utils.img_edge(img) img = cv2.resize(img, (64, 64), interpolation=cv2.INTER_AREA) cv2.imwrite(im_path, img)
for file in os.listdir(path): p = os.path.join(path, file) log.info(p) p2 = os.path.join(path, "a_" + file) al = align.face_features(p, p2) ev = utils.faceparsing_ndarray(al, args.parsing_checkpoint, cuda=cuda) elif args.phase == "dataset": dataset = FaceDataset(args, "test") dataset.pre_process(cuda) elif args.phase == "preview": log.info("preview picture") path = "../export/regular/model.jpg" img = cv2.imread(path) img2 = utils.faceparsing_ndarray(img, args.parsing_checkpoint, cuda) img3 = utils.img_edge(img2) img3_ = ops.fill_gray(img3) img4 = align.face_features(path) log.info("{0} {1} {2} {3}".format(img.shape, img2.shape, img3_.shape, img4.shape)) ops.merge_4image(img, img2, img3_, img4, show=True) elif args.phase == "evaluate": log.info("evaluation mode start") evl = Evaluate(args, cuda=cuda) img = cv2.imread(args.eval_image).astype(np.float32) x_ = evl.itr_train(img) evl.output(x_, img) else: log.error("not known phase %s", args.phase)