Example #1
0
class Predictor:
    def __init__(self):
        test_model_path = r'E:\PycharmProjects\crowdcount_8\best_models\model_20190318_DPLNet\shanghaitechA\pool_4_64.06\saved_models_shtA\shtechA_13.h5'
        # test_model_path = r'.\best_models\model_20190318_DPLNet\trancos\pool_4_3.08_4.39_5.79_7.62\saved_models_trancos\trancos_889.h5'

        self.alpha = 0.5

        self.net = CrowdCount()
        network.load_net(test_model_path, self.net)
        self.net.cuda()
        self.net.eval()

    def predict(self, frame):
        height = frame.shape[0]
        width = frame.shape[1]

        process_height = 400
        process_width = int(width / height * process_height)

        frame = cv2.resize(frame, (process_width, process_height))

        # get original size
        height = frame.shape[0]
        width = frame.shape[1]

        reshaped_frame = np.moveaxis(frame, 2, 0).astype(
            np.float32)  # reshape (h, w, 3) to (3, h, w)
        reshaped_frame = reshaped_frame.reshape((1, 3, height, width))

        image_data = ndarray_to_tensor(reshaped_frame, is_cuda=True)
        estimate_map, _ = self.net(image_data)
        estimate_map = estimate_map.data.cpu().numpy()

        estimate_count = np.sum(estimate_map)

        max_value = np.max(estimate_map)
        if max_value > 0:
            estimate_prior_normalized = estimate_map[0][0] / np.max(
                estimate_map)
        else:
            estimate_prior_normalized = estimate_map[0][0]

        estimate_prior_normalized_bgr = gray_to_bgr(estimate_prior_normalized)

        image_estimate_map = cv2.addWeighted(
            frame, self.alpha,
            cv2.resize(estimate_prior_normalized_bgr, (width, height)),
            1 - self.alpha, 0)

        estimate_count_text = '%.f' % estimate_count
        t_size = cv2.getTextSize(estimate_count_text, cv2.FONT_HERSHEY_PLAIN,
                                 1, 1)[0]
        cv2.putText(image_estimate_map, estimate_count_text,
                    (0, t_size[1] + 4), cv2.FONT_HERSHEY_PLAIN, 1, [0, 0, 255],
                    1)

        return image_estimate_map, estimate_count
Example #2
0
    def __init__(self):
        test_model_path = r'E:\PycharmProjects\crowdcount_8\best_models\model_20190318_DPLNet\shanghaitechA\pool_4_64.06\saved_models_shtA\shtechA_13.h5'
        # test_model_path = r'.\best_models\model_20190318_DPLNet\trancos\pool_4_3.08_4.39_5.79_7.62\saved_models_trancos\trancos_889.h5'

        self.alpha = 0.5

        self.net = CrowdCount()
        network.load_net(test_model_path, self.net)
        self.net.cuda()
        self.net.eval()
Example #3
0
import os
import torch
import numpy as np
import cv2
from src.utils import gray_to_bgr
from src.crowd_count import CrowdCount
from src import network
from src.utils import ndarray_to_tensor

alpha = 0.5

test_model_path = test_model_path = r'./saved_models_shtA/shtechA_31_5217.h5'
net = CrowdCount()
network.load_net(test_model_path, net)

net.cuda()
net.eval()

# cap = cv2.VideoCapture(r'E:\PycharmProjects\data\video\MVI_1582.MOV')
cap = cv2.VideoCapture(r'E:\PycharmProjects\data\video\DJI_0001.MOV')
# cap = cv2.VideoCapture(0)
if not cap.isOpened():
    raise Exception('can not connect to camera')

out = cv2.VideoWriter('test_cam_output.mp4', cv2.VideoWriter_fourcc(*'H264'),
                      30.0, (1920, 1080))

index = 0

# calculate error on the test dataset
while cap.isOpened():
Example #4
0
train_flag = dict()
train_flag['preload'] = False
train_flag['label'] = False
train_flag['mask'] = False


original_dataset_name = 'shanghaitechB_train'
train_data_config = dict()
train_data_config['shanghaitechB_train'] = train_flag.copy()
all_data = multithread_dataloader(train_data_config)


###########################加载网络
##########################固定初始化部分参数

net=CrowdCount()
vgg16=torch.load("VGG16.pth")
prior=net.features.prior
vgg=net.features.vgg16

vgg_16=[]
for k,v in  vgg16.state_dict().items():
    vgg_16.append(v)
useful_vgg16=vgg_16[:26]


i=0
useful_dict={}

for k,v in prior.state_dict().items():
    if(i<26):
def evaluate_model(model_path, data):
    net = CrowdCount()
    network.load_net(model_path, net)
    net.cuda()
    net.eval()

    build_ssim = SSIM(window_size=11)

    game = GridAverageMeanAbsoluteError()

    mae = 0.0
    mse = 0.0
    psnr = 0.0
    ssim = 0.0
    game_0 = 0.0
    game_1 = 0.0
    game_2 = 0.0
    game_3 = 0.0
    index = 0

    for blob in data:
        image_data = blob['image']
        ground_truth_data = blob['density']
        roi = blob['roi']
        # filename = blob['filename']

        if image_data.shape[0] != 1:
            raise Exception('invalid image batch size (%d) for evaluation' %
                            image_data.shape[0])

        estimate_map, _ = net(image_data, roi=roi)

        ground_truth_data = ground_truth_data.data.cpu().numpy()
        density_map = estimate_map.data.cpu().numpy()

        ground_truth_count = np.sum(ground_truth_data)
        estimate_count = np.sum(density_map)

        mae += abs(ground_truth_count - estimate_count)
        mse += (ground_truth_count - estimate_count)**2
        psnr += build_psnr(ground_truth_data, density_map)
        ssim += build_ssim(ndarray_to_tensor(ground_truth_data),
                           ndarray_to_tensor(density_map)).item()
        game_0 += game.calculate_error(ground_truth_data, density_map, 0)
        game_1 += game.calculate_error(ground_truth_data, density_map, 1)
        game_2 += game.calculate_error(ground_truth_data, density_map, 2)
        game_3 += game.calculate_error(ground_truth_data, density_map, 3)
        index += 1

    result_dict = dict()
    result_dict['name'] = os.path.basename(model_path)
    result_dict['number'] = int(index)
    result_dict['mae'] = float(mae / index)
    result_dict['mse'] = float(np.sqrt(mse / index))
    result_dict['psnr'] = float(psnr / index)
    result_dict['ssim'] = float(ssim / index)
    result_dict['game_0'] = float(game_0 / index)
    result_dict['game_1'] = float(game_1 / index)
    result_dict['game_2'] = float(game_2 / index)
    result_dict['game_3'] = float(game_3 / index)

    return result_dict
Example #6
0
    if random_seed is not None:
        np.random.seed(random_seed)
        torch.manual_seed(random_seed)
        torch.cuda.manual_seed_all(random_seed)

    # load data
    # all_data = Data({**train_data_config, **validation_data_config, **test_data_config})
    # all_data = all_data.get()
    all_data = multithread_dataloader({
        **train_data_config,
        **validation_data_config,
        **test_data_config
    })

    # initialize net
    net = CrowdCount()
    network.weights_normal_init(net, dev=0.01)  # default dev=0.01
    # network.save_net('model_init.h5', net)
    if is_load_pretrained_model:
        for path in pretrained_model_path:
            network.load_net_safe(path, net)
    # network.load_net(finetune_model, net)
    # network.save_net('model_loaded.h5', net)

    net.cuda()
    net.train()

    # optimizer = torch.optim.Adam(net.parameters(), lr=lr)
    optimizer = torch.optim.Adam([{
        'params': net.features.vgg16.parameters()
    }, {
Example #7
0
import os
import random
import pandas as pd
import torch

##############开始复现
from src.crowd_count import CrowdCount
from src.data_multithread_preload import multithread_dataloader
from src.models import Model
from src.data_multithread_preload import PreloadData
import torchvision

image_path = r"F:\CV project\shanghaitech\part_B_final\train_data\images"
density_map_path = r"F:\CV project\shanghaitech\part_B_final\train_data\ground_truth"

net = CrowdCount()
vgg16 = torch.load("VGG16.pth")
prior = net.features.prior
vgg = net.features.vgg16

vgg_16 = []
for k, v in vgg16.state_dict().items():
    vgg_16.append(v)
useful_vgg16 = vgg_16[:26]

i = 0
useful_dict = {}

for k, v in prior.state_dict().items():
    if (i < 26):
        useful_dict[k] = useful_vgg16[i]