コード例 #1
0
def demo():
    parser = argparse.ArgumentParser(description="""
                                                 Please specify the csv file of the Datasets path.
                                                 In default, path is 'Data/TestData_Path.csv'
                                                 """)

    parser.add_argument('-p', '--path', default='TestData_Path.csv')  # Testdata path csv
    parser.add_argument('-wd', '--width', type=int, default=640)  # image width that input to model
    parser.add_argument('-ht', '--height', type=int, default=360)  # image height thta input to model
    parser.add_argument('-mw', '--model_weight', default="CrowdCounting_model_cpu_epoch_45.pth")

    args = parser.parse_args()
    test_d_path = args.path
    model_weights = args.model_weight

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    CANnet = model.CANNet()
    CANnet.to(device)
    CANnet.load_state_dict(torch.load(model_weights), strict=False)
    CANnet.eval()

    trans = torchvision.transforms.ToTensor()
    Testdataset = LD.CrowdDatasets(
        transform=trans,
        width=args.width,
        height=args.height,
        Trainpath=test_d_path,
        test_on=True)
    TestLoader = torch.utils.data.DataLoader(
        Testdataset, batch_size=1, shuffle=False)

    for i, data in enumerate(TestLoader):
        if i > 0:
            break
        inputs, persons, flows = data

        tm_img, t_img = inputs[0], inputs[1]
        t_person = persons[0]
        tm2t_flow = flows[0]

        tm_img, t_img = tm_img.to(device, dtype=torch.float),\
            t_img.to(device, dtype=torch.float)
        t_person = t_person.to(device, dtype=torch.float)
        tm2t_flow = tm2t_flow.to(device, dtype=torch.float)

        with torch.set_grad_enabled(False):
            output_before_forward = CANnet(tm_img, t_img)

            output = torch.sum(output_before_forward, dim=1, keepdim=True)
            functions.output_to_img(output)
コード例 #2
0
def test():
    parser = argparse.ArgumentParser(description="""
                                                 Please specify the csv file of the Datasets path.
                                                 In default, path is 'Data/TestData_Path.csv'
                                                 """)

    parser.add_argument('-p', '--path',
                        default='TestData_Path.csv')  # Testdata path csv
    parser.add_argument('-wd', '--width', type=int,
                        default=640)  # image width that input to model
    parser.add_argument('-ht', '--height', type=int,
                        default=360)  # image height thta input to model
    parser.add_argument('-mw',
                        '--model_weight',
                        default="CrowdCounting_model_cpu_epoch_50.pth")
    parser.add_argument('-nl', '--normarize_loss', type=bool, default=False)

    args = parser.parse_args()
    test_d_path = args.path
    model_weights = args.model_weight
    is_normalize = args.normarize_loss

    minibatch_size = 32

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    CANnet = model.CANNet()
    CANnet.to(device)
    CANnet.load_state_dict(torch.load(model_weights), strict=False)
    CANnet.eval()

    # Test Data Loader Settings
    trans = torchvision.transforms.ToTensor()
    Testdataset = LD.CrowdDatasets(transform=trans,
                                   width=args.width,
                                   height=args.height,
                                   Trainpath=test_d_path,
                                   test_on=True)
    TestLoader = torch.utils.data.DataLoader(Testdataset,
                                             batch_size=minibatch_size,
                                             shuffle=False)
    data_len = len(Testdataset)

    # Loss Func
    mae = torch.nn.L1Loss()
    mse = torch.nn.MSELoss()

    all_mae = 0
    all_rmse = 0

    bar = Bar('testing... ', max=int(-(-data_len // minibatch_size)))
    for i, data in enumerate(TestLoader):
        inputs, persons, flows = data

        tm_img, t_img = inputs[0], inputs[1]
        t_person = persons[0]
        tm2t_flow = flows[0]

        tm_img, t_img = tm_img.to(device, dtype=torch.float),\
            t_img.to(device, dtype=torch.float)
        t_person = t_person.to(device, dtype=torch.float)
        tm2t_flow = tm2t_flow.to(device, dtype=torch.float)

        flow = torch.sum(tm2t_flow, dim=1)

        with torch.set_grad_enabled(False):
            output_before_forward = CANnet(tm_img, t_img)

            output = torch.sum(output_before_forward, dim=1, keepdim=True)

            # pixel range 0~1(float) → 0~255(float)
            if not is_normalize:
                output *= 255
                output = output.type(torch.uint8)
                output = output.type(torch.float)
                t_person *= 255
                t_person = t_person.type(torch.uint8)
                t_person = t_person.type(torch.float)

        d_mae = mae(output, t_person)
        d_mse = mse(output, t_person)
        d_rmse = torch.sqrt(d_mse)

        all_mae += d_mae.item() / int(-(-data_len // minibatch_size))
        all_rmse += d_rmse.item() / int(-(-data_len // minibatch_size))
        bar.next()
    bar.finish()
    print("MAE: {}, RMSE: {}".format(all_mae, all_rmse))
コード例 #3
0
def train(lr=1e-3, wd=1e-3, gamma=1e-2):
    parser = argparse.ArgumentParser(description="""
                                                 Please specify the csv file of the Datasets path.
                                                 In default, path is 'TrainData_Path.csv'
                                                 """)
    parser.add_argument('-p', '--path', default='TrainData_Path.csv')
    parser.add_argument('-e', '--epoch', type=int, default=50)
    parser.add_argument('-wd', '--width', type=int, default=640)
    parser.add_argument('-ht', '--height', type=int, default=360)
    args = parser.parse_args()
    train_d_path = args.path

    minibatch_size = 48
    epock_num = args.epoch

    lr = 0.004883871414132006
    wd = 1.1233220133366867e-08
    gamma = 0.00010961378774781317
    ol = 1
    dl = 0
    datatime = str(datetime.date.today())
    savedir = os.path.join('exp', datatime)
    if not os.path.isdir(savedir):
        os.mkdir(savedir)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    CANnet = model.CANNet()
    if torch.cuda.device_count() > 1:
        print("You can use {} GPUs!".format(torch.cuda.device_count()))
        CANnet = torch.nn.DataParallel(CANnet)

    CANnet.to(device)
    CANnet.train()

    torch.backends.cudnn.benchmark = True

    trans = torchvision.transforms.ToTensor()
    Traindataset = LD.CrowdDatasets(transform=trans,
                                    width=args.width,
                                    height=args.height,
                                    Trainpath=train_d_path)
    TrainLoader = torch.utils.data.DataLoader(Traindataset,
                                              batch_size=minibatch_size,
                                              shuffle=True,
                                              num_workers=8)
    data_len = len(Traindataset)

    criterion = Losses.AllLoss(device=device,
                               batchsize=minibatch_size,
                               optical_loss_on=ol,
                               direction_loss_on=dl)
    optimizer = optim.Adam(CANnet.parameters(),
                           lr=lr,
                           betas=(0.9, 0.999),
                           eps=1e-8,
                           weight_decay=wd)

    batch_repeet_num = int(-(-data_len // minibatch_size))

    losses = []

    for epock in range(epock_num):
        e_loss = 0.0
        e_floss = 0.0
        e_closs = 0.0
        e_oloss = 0.0
        e_dloss = 0.0

        print('-------------')
        print('Epoch {}/{}'.format(epock + 1, epock_num))
        print('-------------')
        print('(train)')

        bar = Bar('training... ', max=batch_repeet_num)

        for i, data in enumerate(TrainLoader):

            torch.cuda.empty_cache()

            inputs, persons, flows = data

            tm_img, t_img, tp_img = inputs[0], inputs[1], inputs[2]
            tm_person, t_person, tp_person = persons[0], persons[1], persons[2]
            tm2t_flow, t2tp_flow = flows[0], flows[1]

            tm_img, t_img, tp_img = tm_img.to(device, dtype=torch.float),\
                t_img.to(device, dtype=torch.float),\
                tp_img.to(device, dtype=torch.float)

            tm_person, t_person, tp_person = tm_person.to(
                device, dtype=torch.float), t_person.to(
                device, dtype=torch.float), tp_person.to(
                device, dtype=torch.float)

            tm2t_flow, t2tp_flow = tm2t_flow.to(device, dtype=torch.float),\
                t2tp_flow.to(device, dtype=torch.float)

            with torch.set_grad_enabled(False):
                output_befoer_forward = CANnet(tm_img, t_img)
                output_before_back = CANnet(t_img, tm_img)
                output_after_back = CANnet(tp_img, t_img)

            with torch.set_grad_enabled(True):
                output_after_forward = CANnet(t_img, tp_img)

            loss_all = criterion.forward(
                tm_person,
                t_person,
                tm2t_flow,
                output_befoer_forward,
                output_before_back,
                output_after_forward,
                output_after_back,
                gamma=gamma)

            loss_item = loss_all[0].item()  # all loss
            floss_item = loss_all[1].item()  # flow loss
            closs_item = loss_all[2].item()  # cycle loss
            oloss_item = loss_all[3].item()  # optical loss
            dloss_item = loss_all[4].item()  # direct loss

            e_loss += loss_item / batch_repeet_num
            e_floss += floss_item / batch_repeet_num
            e_closs += closs_item / batch_repeet_num
            e_oloss += oloss_item / batch_repeet_num
            e_dloss += dloss_item / batch_repeet_num

            # assert not torch.isnan(floss).item(), "floss is Nan !!"
            if torch.isnan(loss_all[1]).item():
                return loss_all[0].item()

            optimizer.zero_grad()
            loss_all[0].backward()
            optimizer.step()
            bar.next()

            del tm_img, t_img, tp_img
            del tm_person, t_person, tp_person
            del tm2t_flow, t2tp_flow
        bar.finish()

        losses.append(e_loss)
        print('-------------')
        print(
            'epoch {} || Loss:{}, FlowLoss:{}, CycleLoss:{}, OptiLoss:{}, DirectLoss:{}'.format(
                epock + 1,
                e_loss,
                e_floss,
                e_closs,
                e_oloss,
                e_dloss))
        if (epock + 1) == epock_num or (epock + 1) % 50 == 0:
            save_path = os.path.join(
                savedir, '{}_{}_{}_e{}.pth'.format(
                    "normal", "oloss" * ol, "dloss" * dl, epock + 1))
            torch.save(CANnet.state_dict(), save_path)

    print("Training Done!!")

    save_fig_name = os.path.join(savedir,
                                 'h_{}_w_{}_lr_{}_wd_{}.png'.format(
                                     args.height,
                                     args.width,
                                     lr,
                                     wd))
    x = [i for i in range(len(losses))]
    plt.plot(x, losses, label="lr:{}, wd:{}".format(lr, wd))
    plt.title("loss")
    plt.legend(bbox_to_anchor=(1, 1), loc='upper right', borderaxespad=0)
    plt.show()
    plt.savefig(save_fig_name)

    return losses[-1]
コード例 #4
0
def demo():
    parser = argparse.ArgumentParser(description="""
                                                 Please specify the csv file of the Datasets path.
                                                 In default, path is 'Data/TestData_Path.csv'
                                                 """)

    parser.add_argument('-p', '--path', default='TestData_Path.csv')  # Testdata path csv
    parser.add_argument('-wd', '--width', type=int, default=640)  # image width that input to model
    parser.add_argument('-ht', '--height', type=int, default=360)  # image height thta input to model
    parser.add_argument('-nw', '--normal_weight', default=normal_path)
    parser.add_argument('-dw', '--direct_weight', default=direct_path)
    parser.add_argument('-num', '--img_num', default=10)

    args = parser.parse_args()
    test_d_path = args.path
    normal_weights = args.normal_weight
    direct_weights = args.direct_weight
    num = args.img_num

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    CANnet = model.CANNet()
    CANnet.to(device)
    CANnet.load_state_dict(torch.load(normal_weights), strict=False)
    CANnet.eval()

    D_CANnet = model.CANNet()
    D_CANnet.to(device)
    D_CANnet.load_state_dict(torch.load(direct_weights), strict=False)
    D_CANnet.eval()

    trans = torchvision.transforms.ToTensor()
    Testdataset = LD.CrowdDatasets(
        transform=trans,
        width=args.width,
        height=args.height,
        Trainpath=test_d_path,
        test_on=True)
    TestLoader = torch.utils.data.DataLoader(
        Testdataset, batch_size=1, shuffle=True)
    sigma = torch.nn.Sigmoid()

    img_dict_keys = ['input',
                     'label',
                     'normal',
                     'normal_quiver',
                     'normal_dense_res',
                     'direct',
                     'direct_quiver',
                     'direct_dense_res']

    img_dict = {
        img_dict_keys[0]: ('img', None),
        img_dict_keys[1]: ('img', None),
        img_dict_keys[2]: ('img', None),
        img_dict_keys[3]: ('quiver', None),
        img_dict_keys[4]: ('img', None),
        img_dict_keys[5]: ('img', None),
        img_dict_keys[6]: ('quiver', None),
        img_dict_keys[7]: ('img', None)
    }

    DemoImg = Valid.CompareOutput(img_dict_keys)

    for i, data in enumerate(TestLoader):
        if i >= num:
            print("\n")
            break
        inputs, persons, flows = data

        tm_img, t_img = inputs[0], inputs[1]
        tm_person = persons[0]
        tm2t_flow = flows[0]

        tm_img, t_img = tm_img.to(device, dtype=torch.float),\
            t_img.to(device, dtype=torch.float)
        tm_person = tm_person.to(device, dtype=torch.float)
        tm2t_flow = tm2t_flow.to(device, dtype=torch.float)

        with torch.set_grad_enabled(False):
            output_normal = CANnet(tm_img, t_img)
            # output_normal = sigma(output_normal) - 0.5

            output_direct = D_CANnet(tm_img, t_img)
            # output_direct = sigma(output_direct) - 0.5

        input_num = tm_img[0, :, :, :].detach().cpu().numpy()
        input_num = input_num.transpose((1, 2, 0))
        label_num = tm_person[0, :, :, :].detach().cpu().numpy()
        label_num = label_num.transpose((1, 2, 0))

        normal_num = output_normal[0, :, :, :].detach().cpu().numpy()
        normal_quiver = Valid.NormalizeQuiver(normal_num)
        normal_num = normal_num.transpose((1, 2, 0))

        direct_num = output_direct[0, :, :, :].detach().cpu().numpy()
        direct_quiver = Valid.NormalizeQuiver(direct_num)
        direct_num = direct_num.transpose((1, 2, 0))

        normal_dense = Valid.tm_output_to_dense(normal_num)
        direct_dense = Valid.tm_output_to_dense(direct_num)

        normal_res_dense = Valid.output_res_img(np.squeeze(label_num), normal_dense)
        direct_res_dense = Valid.output_res_img(np.squeeze(label_num), direct_dense)

        img_dict = {
            img_dict_keys[0]: ('img', input_num),
            img_dict_keys[1]: ('img', label_num),
            img_dict_keys[2]: ('img', normal_dense),
            img_dict_keys[3]: ('quiver', normal_quiver),
            img_dict_keys[4]: ('img', normal_res_dense),
            img_dict_keys[5]: ('img', direct_dense),
            img_dict_keys[6]: ('quiver', direct_quiver),
            img_dict_keys[7]: ('img', direct_res_dense),
        }

        DemoImg.append_pred(img_dict)

        print("{} / {} done\r".format((i+1), num), end="")

    DemoImg.plot_img()
    DemoImg.save_fig()
コード例 #5
0
def train():
    parser = argparse.ArgumentParser(description="""
                                                 Please specify the csv file of the Datasets path.
                                                 In default, path is 'Data/TrainData_Path.csv'
                                                 """)
    parser.add_argument('-p', '--path', default='TrainData_Path.csv')
    parser.add_argument('-e', '--epoch', type=int, default=50)
    parser.add_argument('-wd', '--width', type=int, default=640)
    parser.add_argument('-ht', '--height', type=int, default=360)
    args = parser.parse_args()
    train_d_path = args.path

    minibatch_size = 32
    epock_num = args.epoch

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    # device = "cpu"

    CANnet = model.CANNet()
    if torch.cuda.device_count() > 1:
        print("You can use {} GPUs!".format(torch.cuda.device_count()))
        CANnet = torch.nn.DataParallel(CANnet)
    for p in CANnet.parameters():
        p.data.clamp_(-1.0, 1.0)
    CANnet.to(device)
    CANnet.train()

    # reporter = PM.MemReporter(CANnet)

    torch.backends.cudnn.benchmark = True

    trans = torchvision.transforms.ToTensor()
    Traindataset = LD.CrowdDatasets(transform=trans,
                                    width=args.width,
                                    height=args.height,
                                    Trainpath=train_d_path)

    TrainLoader = torch.utils.data.DataLoader(Traindataset,
                                              batch_size=minibatch_size,
                                              shuffle=True)
    data_len = len(Traindataset)

    criterion = functions.AllLoss(batchsize=minibatch_size, optical_loss_on=0)
    optimizer = optim.Adam(CANnet.parameters(),
                           lr=0.001,
                           betas=(0.9, 0.999),
                           eps=1e-8,
                           weight_decay=0.5)

    # reporter.report()

    losses = []

    for epock in range(epock_num):
        e_loss = 0.0

        print('-------------')
        print('Epoch {}/{}'.format(epock + 1, epock_num))
        print('-------------')
        print('(train)')

        bar = Bar('training... ', max=int(-(-data_len // minibatch_size)))

        for i, data in enumerate(TrainLoader):

            inputs, persons, flows = data

            tm_img, t_img, tp_img = inputs[0], inputs[1], inputs[2]
            tm_person, t_person, tp_person = persons[0], persons[1], persons[2]
            tm2t_flow, t2tp_flow = flows[0], flows[1]

            tm_img, t_img, tp_img = tm_img.to(device, dtype=torch.float),\
                t_img.to(device, dtype=torch.float),\
                tp_img.to(device, dtype=torch.float)

            tm_person, t_person, tp_person = tm_person.to(
                device, dtype=torch.float), t_person.to(
                    device, dtype=torch.float), tp_person.to(device,
                                                             dtype=torch.float)

            tm2t_flow, t2tp_flow = tm2t_flow.to(device, dtype=torch.float),\
                t2tp_flow.to(device, dtype=torch.float)

            optimizer.zero_grad()

            with torch.set_grad_enabled(False):
                output_befoer_forward = CANnet(tm_img, t_img)
                output_before_back = CANnet(t_img, tm_img)
                output_after_back = CANnet(tp_img, t_img)

            with torch.set_grad_enabled(True):
                output_after_forward = CANnet(t_img, tp_img)

            loss = criterion.forward(tm_person, t_person, tm2t_flow,
                                     output_befoer_forward, output_before_back,
                                     output_after_forward, output_after_back)

            e_loss += loss.item() / int(-(-data_len // minibatch_size))
            print(loss.item())
            loss.backward()
            optimizer.step()
            bar.next()

        bar.finish()

        losses.append(e_loss)
        print('-------------')
        print('epoch {} || Epoch_Loss:{}'.format(epock + 1, e_loss))
        if (epock + 1) == (epock_num - 5) or (epock + 1) == epock_num:
            torch.save(
                CANnet.state_dict(), 'CrowdCounting_{}_{}_epoch_{}.pth'.format(
                    args.height, args.width, epock + 1))

    print("Training Done!!")
    # reporter.report()
    # CANnet = CANnet.to('cpu')
    # print("Save Done!!")

    x = [i for i in range(len(losses))]
    plt.plot(x, losses)
    plt.title("loss")
    plt.show()
    plt.savefig("loss_record.png")