Пример #1
0
def main(opt):
    train_dataset = BADataset(opt.dataroot, opt.L, True, False, False)
    train_dataloader = BADataloader(train_dataset, batch_size=opt.batchSize, \
                                      shuffle=True, num_workers=opt.workers, drop_last=True)

    valid_dataset = BADataset(opt.dataroot, opt.L, False, True, False)
    valid_dataloader = BADataloader(valid_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    test_dataset = BADataset(opt.dataroot, opt.L, False, False, True)
    test_dataloader = BADataloader(test_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
    all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=opt.workers, drop_last=False)

    opt.n_edge_types = train_dataset.n_edge_types
    opt.n_node = train_dataset.n_node

    net = EGCN(gcn_args, activation = torch.nn.RReLU(), device = opt.device)
    print(net)

    criterion = nn.BCELoss()

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)
    early_stopping = EarlyStopping(patience=opt.patience, verbose=True)

    os.makedirs(OutputDir, exist_ok=True)
    train_loss_ls = []
    valid_loss_ls = []
    test_loss_ls = []

    for epoch in range(0, opt.niter):
        train_loss = train(epoch, train_dataloader, net, criterion, optimizer, opt)
        valid_loss = valid(valid_dataloader, net, criterion, opt)
        test_loss = test(test_dataloader, net, criterion, opt)

        train_loss_ls.append(train_loss)
        valid_loss_ls.append(valid_loss)
        test_loss_ls.append(test_loss)

        early_stopping(valid_loss, net, OutputDir)
        if early_stopping.early_stop:
            print("Early stopping")
            break

    df = pd.DataFrame({'epoch':[i for i in range(1, len(train_loss_ls)+1)], 'train_loss': train_loss_ls, 'valid_loss': valid_loss_ls, 'test_loss': test_loss_ls})
    df.to_csv(OutputDir + '/loss.csv', index=False)

    #net.load_state_dict(torch.load(OutputDir + '/checkpoint.pt'))
    net = torch.load(OutputDir + '/checkpoint.pt')
    inference(all_dataloader, net, criterion, opt, OutputDir)
Пример #2
0
def main(opt):
    all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
    all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=opt.workers, drop_last=False)

    opt.n_edge_types = all_dataset.n_edge_types
    opt.n_node = all_dataset.n_node

    inference(all_dataloader, opt, OutputDir)
Пример #3
0
def run(img_dir, output_dir, img_size, num_classes, weights, conf_thres,
        nms_thres, show):
    shutil.rmtree(output_dir, ignore_errors=True)
    os.makedirs(output_dir, exist_ok=True)
    model = YOLOV3(num_classes, img_size)
    state_dict = torch.load(weights, map_location='cpu')
    model.load_state_dict(state_dict['model'])
    model = model.to(device)
    model.eval()
    colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(80)]
    names = [n for n in os.listdir(img_dir) if osp.splitext(n)[1] in IMG_EXT]
    names.sort()
    for name in tqdm(names):
        img = cv2.imread(osp.join(img_dir, name))
        det = inference(model, [img], img_size, conf_thres, nms_thres)[0]
        det_txt = []
        # Write results
        for *xyxy, conf, _, cls in det:
            det_txt.append(' '.join(['%g'] * 6) % (*xyxy, cls, conf))
            if show:  # Add bbox to image
                label = '%d %.2f' % (int(cls), conf)
                plot_one_box(xyxy, img, label=label, color=colors[int(cls)])
        with open(osp.join(output_dir,
                           osp.splitext(name)[0] + '.txt'), 'w') as f:
            f.write('\n'.join(det_txt))
        # Stream results
        if show:
            cv2.imshow('yolo', img)
            cv2.waitKey(1)
        # Save results (image with detections)
        cv2.imwrite(osp.join(output_dir, name), img)
Пример #4
0
    def detect_bird(self, img, filename, width, height, dt):
        """
        Localize bird(s) and bird face(s) in the image and display results
            img: (ndarray) image file
            filename: (String) name of the image file
            width: (float) width of img
            height: (float) height of img
            dt: (int) time in seconds
        """
        # run inference code
        image = bf.inference(filename, img)

        # clear any previous texture information as
        # to avoid continuously writing data on top of data
        self.ids.detection.canvas.clear()

        # create kivy texture from image ndarray
        texture = Texture.create(size=(width, height), colorfmt="rgb")
        # resize image for display
        image = cv2.resize(image, ((math.floor(width), math.floor(height))))
        # convert array to string
        data = image.tostring()
        # blit data to texture
        texture.blit_buffer(data, bufferfmt="ubyte", colorfmt="rgb")
        # flip vertically to display upright
        texture.flip_vertical()

        # calculate position to center on left side of window
        x_pos = self.ids.image.parent.width * .25 - (width / 2)
        y_pos = self.ids.image.parent.height * .5 - (height / 2)

        # display result to screen
        with self.ids.detection.canvas:
            Rectangle(texture=texture,
                      pos=(x_pos, y_pos),
                      size=(width, height))

        # no bird/faces were detected
        if not len(gv.boxes[filename]['birds']) or not len(
                gv.boxes[filename]['faces']):
            # remove image transparency and display red x
            self.ids.result.color = (1, 1, 1, 1)
            self.ids.result.source = 'assets/no.png'

            # remove image from dictionary
            gv.images.pop(filename)

        # bird face is detected
        else:
            # reset result and add transparency back in the
            # case that the previous image may not have had
            # a bird or face
            self.ids.result.color = (0, 0, 0, 0)
            self.ids.result.source = ''
Пример #5
0
def run(img_dir, output_csv, weights, img_size, num_classes, rect):
    results = []
    model = MobileNetV2(num_classes)
    state_dict = torch.load(weights, map_location='cpu')
    model.load_state_dict(state_dict['model'])
    model = model.to(device)
    model.eval()
    names = [n for n in os.listdir(img_dir) if osp.splitext(n)[1] in IMG_EXT]
    for name in tqdm(names):
        path = osp.join(img_dir, name)
        img = cv2.imread(path)
        pred = inference(model, img, img_size, rect=rect)
        idx = pred.argmax()
        results.append('%s %d' % (path, idx))
    with open(output_csv, 'w') as f:
        f.write('\n'.join(results))
Пример #6
0
def run(img_dir, output_dir, img_size, num_classes, weights, show):
    shutil.rmtree(output_dir, ignore_errors=True)
    os.makedirs(output_dir, exist_ok=True)
    model = HRNet(num_classes)
    state_dict = torch.load(weights, map_location='cpu')
    model.load_state_dict(state_dict['model'])
    model = model.to(device)
    model.eval()
    names = [n for n in os.listdir(img_dir) if osp.splitext(n)[1] in IMG_EXT]
    names.sort()
    for name in tqdm(names):
        path = osp.join(img_dir, name)
        img = cv2.imread(path)
        kps = inference(model, [img], img_size)[0]
        for (x, y) in kps:
            cv2.circle(img, (int(x * img.shape[1]), int(y * img.shape[0])), 2,
                       (0, 0, 255), -1)
        cv2.imwrite(osp.join(output_dir, osp.splitext(name)[0] + '.png'), img)
Пример #7
0
train_dataset = BADataset(opt.dataroot, opt.L, True, False, False)
train_dataloader = BADataloader(train_dataset, batch_size=opt.batchSize, \
                                  shuffle=True, num_workers=opt.workers, drop_last=True)

# valid_dataset = BADataset(opt.dataroot, opt.L, False, True, False)
# valid_dataloader = BADataloader(valid_dataset, batch_size=opt.batchSize, \
#                                  shuffle=True, num_workers=opt.workers, drop_last=True)

# test_dataset = BADataset(opt.dataroot, opt.L, False, False, True)
# test_dataloader = BADataloader(test_dataset, batch_size=opt.batchSize, \
#                                  shuffle=False, num_workers=opt.workers, drop_last=True)

all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                 shuffle=False, num_workers=opt.workers, drop_last=False)

device = torch.device('cuda:' + str(opt.cuda) if opt.gpu else 'cpu')
net = DEAL(opt.output_dim, opt.annotation_dim, all_node_num, device, opt, locals()[opt.attr_model])
net.double()
print(net)

if opt.cuda:
    net.cuda()

optimizer = torch.optim.Adam(net.parameters(), lr=opt.lr)

os.makedirs(OutputDir, exist_ok=True)
train(train_dataloader, net, optimizer, opt, OutputDir)
net.load_state_dict(torch.load(OutputDir + '/checkpoint.pt'))
inference(all_dataloader, net, opt, OutputDir)
Пример #8
0
                    default='./ds_graph/output_graph.pb',
                    help='Path to trained DeepSpeech model')
parser.add_argument('--audio_fname',
                    default='./audio/sentence07.wav',
                    help='Path of input speech sequence')
parser.add_argument(
    '--template_fname',
    default='./template/FLAME_sample.ply',
    help='Path of "zero pose" template mesh in" FLAME topology to be animated')
parser.add_argument('--condition_idx',
                    type=int,
                    default=3,
                    help='Subject condition id in [1,8]')
parser.add_argument('--out_path',
                    default='./voca/animation_output',
                    help='Output path')

args = parser.parse_args()
tf_model_fname = args.tf_model_fname
ds_fname = args.ds_fname
audio_fname = args.audio_fname
template_fname = args.template_fname
condition_idx = args.condition_idx
out_path = args.out_path

if not os.path.exists(out_path):
    os.makedirs(out_path)

inference(tf_model_fname, ds_fname, audio_fname, template_fname, condition_idx,
          out_path)
Пример #9
0
def main(opt):
    train_dataset = BADataset(opt.dataroot, opt.L, True, False, False)
    train_dataloader = BADataloader(train_dataset, batch_size=opt.batchSize, \
                                      shuffle=True, num_workers=opt.workers, drop_last=True)

    valid_dataset = BADataset(opt.dataroot, opt.L, False, True, False)
    valid_dataloader = BADataloader(valid_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    test_dataset = BADataset(opt.dataroot, opt.L, False, False, True)
    test_dataloader = BADataloader(test_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
    all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=opt.workers, drop_last=False)

    opt.n_edge_types = train_dataset.n_edge_types
    opt.n_node = train_dataset.n_node

    net = STGCN(opt,
                kernel_size=2,
                n_blocks=1,
                state_dim_bottleneck=opt.state_dim,
                annotation_dim_bottleneck=opt.annotation_dim)
    net.double()
    print(net)

    criterion = nn.MSELoss()
    #criterion = nn.CosineSimilarity(dim=-1, eps=1e-6)

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)
    early_stopping = EarlyStopping(patience=opt.patience, verbose=True)

    os.makedirs(OutputDir, exist_ok=True)
    train_loss_ls = []
    valid_loss_ls = []
    test_loss_ls = []

    for epoch in range(0, opt.niter):
        train_loss = train(epoch, train_dataloader, net, criterion, optimizer,
                           opt)
        valid_loss = valid(valid_dataloader, net, criterion, opt)
        test_loss = test(test_dataloader, net, criterion, opt)

        train_loss_ls.append(train_loss)
        valid_loss_ls.append(valid_loss)
        test_loss_ls.append(test_loss)

        early_stopping(valid_loss, net, OutputDir)
        if early_stopping.early_stop:
            print("Early stopping")
            break

    df = pd.DataFrame({
        'epoch': [i for i in range(1,
                                   len(train_loss_ls) + 1)],
        'train_loss': train_loss_ls,
        'valid_loss': valid_loss_ls,
        'test_loss': test_loss_ls
    })
    df.to_csv(OutputDir + '/loss.csv', index=False)

    net.load_state_dict(torch.load(OutputDir + '/checkpoint.pt'))
    inference(all_dataloader, net, criterion, opt, OutputDir)
Пример #10
0
        return val
    elif isinstance(val, str):
        if val.lower() in ['true', 't', 'yes', 'y']:
            return True
        elif val.lower() in ['false', 'f', 'no', 'n']:
            return False
    return False

parser = argparse.ArgumentParser(description='Voice operated character animation')
parser.add_argument('--tf_model_fname', default='./model/gstep_52280.model', help='Path to trained VOCA model')
parser.add_argument('--ds_fname', default='./ds_graph/output_graph.pb', help='Path to trained DeepSpeech model')
parser.add_argument('--audio_fname', default='./audio/test_sentence.wav', help='Path of input speech sequence')
parser.add_argument('--template_fname', default='./template/FLAME_sample.ply', help='Path of "zero pose" template mesh in" FLAME topology to be animated')
parser.add_argument('--condition_idx', type=int, default=3, help='Subject condition id in [1,8]')
parser.add_argument('--out_path', default='./voca/animation_output', help='Output path')
parser.add_argument('--visualize', default='True', help='Visualize animation')

args = parser.parse_args()
tf_model_fname = args.tf_model_fname
ds_fname = args.ds_fname
audio_fname = args.audio_fname
template_fname = args.template_fname
condition_idx = args.condition_idx
out_path = args.out_path

if not os.path.exists(out_path):
    os.makedirs(out_path)

inference(tf_model_fname, ds_fname, audio_fname, template_fname, condition_idx, out_path, str2bool(args.visualize))

Пример #11
0
            return False
    return False

parser = argparse.ArgumentParser(description='Voice operated character animation')
parser.add_argument('--tf_model_fname', default='./model/gstep_52280.model', help='Path to trained VOCA model')
parser.add_argument('--ds_fname', default='./ds_graph/output_graph.pb', help='Path to trained DeepSpeech model')
parser.add_argument('--audio_fname', default='./audio/test_sentence.wav', help='Path of input speech sequence')
parser.add_argument('--template_fname', default='./template/FLAME_sample.ply', help='Path of "zero pose" template mesh in" FLAME topology to be animated')
parser.add_argument('--condition_idx', type=int, default=3, help='Subject condition id in [1,8]')
parser.add_argument('--uv_template_fname', default='', help='Path of a FLAME template with UV coordinates')
parser.add_argument('--texture_img_fname', default='', help='Path of the texture image')
parser.add_argument('--out_path', default='./voca/animation_output', help='Output path')
parser.add_argument('--visualize', default='True', help='Visualize animation')

args = parser.parse_args()
tf_model_fname = args.tf_model_fname
ds_fname = args.ds_fname
audio_fname = args.audio_fname
template_fname = args.template_fname
condition_idx = args.condition_idx
out_path = args.out_path

uv_template_fname = args.uv_template_fname
texture_img_fname = args.texture_img_fname

if not os.path.exists(out_path):
    os.makedirs(out_path)

inference(tf_model_fname, ds_fname, audio_fname, template_fname, condition_idx, out_path, str2bool(args.visualize), uv_template_fname=uv_template_fname, texture_img_fname=texture_img_fname)

Пример #12
0
def main(opt):
    train_dataset = BADataset(opt.dataroot, opt.L, True, False, False)
    train_dataloader = BADataloader(train_dataset, batch_size=opt.batchSize, \
                                      shuffle=True, num_workers=opt.workers, drop_last=True)

    valid_dataset = BADataset(opt.dataroot, opt.L, False, True, False)
    valid_dataloader = BADataloader(valid_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    test_dataset = BADataset(opt.dataroot, opt.L, False, False, True)
    test_dataloader = BADataloader(test_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
    all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=opt.workers, drop_last=False)

    net = PointNet(d0=opt.d0,
                   d1=opt.d1,
                   d2=opt.d2,
                   d3=opt.d3,
                   d4=opt.d4,
                   d5=opt.d5,
                   d6=opt.d6)
    net.double()
    print(net)

    criterion = nn.CosineSimilarity(dim=1)

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)
    early_stopping = EarlyStopping(patience=opt.patience, verbose=True)

    os.makedirs(OutputDir, exist_ok=True)
    train_loss_ls = []
    valid_loss_ls = []
    test_loss_ls = []

    for epoch in range(0, opt.niter):
        train_loss = train(epoch, train_dataloader, net, criterion, optimizer,
                           opt)
        valid_loss = valid(valid_dataloader, net, criterion, opt)
        test_loss = test(test_dataloader, net, criterion, opt)

        train_loss_ls.append(train_loss)
        valid_loss_ls.append(valid_loss)
        test_loss_ls.append(test_loss)

        early_stopping(valid_loss, net, OutputDir)
        if early_stopping.early_stop:
            print("Early stopping")
            break

    df = pd.DataFrame({
        'epoch': [i for i in range(1,
                                   len(train_loss_ls) + 1)],
        'train_loss': train_loss_ls,
        'valid_loss': valid_loss_ls,
        'test_loss': test_loss_ls
    })
    df.to_csv(OutputDir + '/loss.csv', index=False)

    net.load_state_dict(torch.load(OutputDir + '/checkpoint.pt'))
    inference(all_dataloader, net, opt, OutputDir)