コード例 #1
0
def evaluate_model(trained_model, data_loader, netname='CrowdCounter'):
    if (netname is 'CrowdCounter'):
        net = CrowdCounter()
    elif (netname is 'MCNNNet'):
        net = MCNNNet()
    elif (netname is 'AmendNet'):
        net = AmendNet()
    elif (netname is 'CrowdCounter_MSCNN'):
        net = CrowdCounter_MSCNN()
    else:
        raise (
            'netname should be one of ["CrowdCounter", "MCNNNet", "AmendNet", "CrowdCounter_MSCNN"],\
        but we got ', netname)
    network.load_net(trained_model, net)
    net.cuda()
    net.eval()
    mae = 0.0
    mse = 0.0
    for blob in data_loader:
        im_data = blob['data']
        gt_data = blob['gt_density']
        density_map = net(im_data, gt_data)
        density_map = density_map.data.cpu().numpy()
        gt_count = np.sum(gt_data)
        et_count = np.sum(density_map)
        mae += abs(gt_count - et_count)
        mse += ((gt_count - et_count) * (gt_count - et_count))
    mae = mae / data_loader.get_num_samples()
    mse = np.sqrt(mse / data_loader.get_num_samples())
    return mae, mse
コード例 #2
0
    def init_model(self, model_path=None):
        if model_path is not None:
            network.load_net(model_path, self.model)
        else:
            network.weights_normal_init(self.model, dev=1e-6)
            # network.load_net('../../pruned_VGG.h5', self.model.front_end, skip=True)
            # network.load_net("../../vgg16.h5", self.model.front_end, skip=True)

        def calpara(model):
            print('---------- Networks initialized -------------')
            num_params = 0
            for param in model.parameters():
                num_params += param.numel()
            print('[Network] Total number of parameters : %.3f M' % (num_params / 1e6))
            print('-----------------------------------------------')

        calpara(self.model)

        network.weights_normal_init(self.loss_fn_, dev=0.01)

        if len(self.opt.gpus) > 0:
            assert(torch.cuda.is_available())
            self.model.to(self.device)
            self.model = torch.nn.DataParallel(self.model, self.opt.gpus)  # multi-GPUs
            if self.opt.loss is not None and 'SSIM' in self.opt.loss:
                self.loss_fn_.to(self.device)
                self.loss_fn = torch.nn.DataParallel(self.loss_fn_, self.opt.gpus)  # multi-GPUs
            else:
                self.loss_fn = self.loss_fn_
コード例 #3
0
 def init_model(self, model_path=None):
     if model_path is not None:
         network.load_net(model_path, self.model, prefix='model.module.')
     else:
         network.weights_normal_init(self.model, dev=1e-6)
         network.load_net('pretrained_models/pruned_VGG.h5',
                          self.model.front_end,
                          skip=True)
         for m in self.model.passing_weight4.named_children():
             m[1].bias.data.fill_(-2.19)
         for m in self.model.decoder5.named_children():
             if m[0] == '1':
                 m[1].conv.bias.data.fill_(-2.19)
コード例 #4
0
def evaluate_model(trained_model, data_loader):
    net = CrowdCounter()
    network.load_net(trained_model, net)
    # net.cuda()
    net.eval()
    mae = 0.0
    mse = 0.0
    for blob in data_loader:
        im_data = blob['data']
        gt_data = blob['gt_density']
        density_map = net(im_data, gt_data)
        density_map = density_map.data.cpu().numpy()
        gt_count = np.sum(gt_data)
        et_count = np.sum(density_map)
        mae += abs(gt_count - et_count)
        mse += ((gt_count - et_count) * (gt_count - et_count))
    mae = mae / data_loader.get_num_samples()
    mse = np.sqrt(mse / data_loader.get_num_samples())
    return mae, mse
コード例 #5
0
            data_log.image_summary(tag='task2_image',images=im2show,step=i)
            #cv2.imshow('test', im2show)
            #cv2.waitKey(1)
            

    with open(det_file, 'wb') as f:
        cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    aps = imdb.evaluate_detections(all_boxes, output_dir)
    return aps


if __name__ == '__main__':
    # load data
    imdb = get_imdb(imdb_name)
    imdb.competition_mode(on=True)

    # load net
    net = WSDDN(classes=imdb.classes, debug=False)
    trained_model = trained_model_fmt.format(cfg.TRAIN.SNAPSHOT_PREFIX,50000)
    network.load_net(trained_model, net)
    print('load model successfully!')

    net.cuda()
    net.eval()

    # evaluation
    aps = test_net(save_name, net, imdb, 
                   max_per_image, thresh=thresh, visualize=False)
コード例 #6
0
firstEval = 1

# load imdb and create data later
imdb = get_imdb(imdb_name)
rdl_roidb.prepare_roidb(imdb)
roidb = imdb.roidb
data_layer = RoIDataLayer(roidb, imdb.num_classes)

# Create network and initialize
net = WSDDN(classes=imdb.classes, debug=_DEBUG)
network.weights_normal_init(net, dev=0.001)

# If checkpoint file exists
ckpt_file = 'models/saved_model/wsddn_' + str(args.checkpoint) + '.h5'
if os.path.exists(ckpt_file):
    network.load_net(ckpt_file, net)
    start_step = args.checkpoint
    print('Loaded from checkpoint:{}'.format(args.checkpoint))
# Using pretrained AlexNet
else:
    if os.path.exists('pretrained_alexnet.pkl'):
        pret_net = pkl.load(open('pretrained_alexnet.pkl', 'r'))
    else:
        pret_net = model_zoo.load_url(
            'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth')
        pkl.dump(pret_net, open('pretrained_alexnet.pkl', 'wb'),
                 pkl.HIGHEST_PROTOCOL)
    own_state = net.state_dict()
    for name, param in pret_net.items():
        if 'classifier' in name:
            name = str(name[:11] + str(int(name[11]) - 1) + name[12:])
コード例 #7
0
ファイル: testing.py プロジェクト: moscalej/Alpha-2Zero
def test_networks(datasetname, statesonly, expanded, name, batchsize=20000):
    """
    Measure the accuracy and the legality of a triplet of networks
    
    Loads three networks and a dataset and than measure how accurate are the
    networks predictions with respect to the dataset and if their choices are
    legal. If the dataset is made only by states (therefore no moves), the
    accuracy test is not performed.

    Parameters
    ----------
    datasetname : string
        The name of the dataset file.
    statesonly : boolean
        True if the dataset has doesn't contains informations about the moves.
    expanded : boolean
        True if the dataset has been already expanded through symmetries.
    name : string
        The name of the network configuration to load
    """

    print("Testing " + name)
    print("Loading networks...")
    print("\tloading " + name + "_TO")
    TOnet = load_net(name + "_TO")
    print("\tloading " + name + "_FROM")
    FROMnet = load_net(name + "_FROM")

    print("\tloading " + name + "_REMOVE")
    REMOVEnet = load_net(name + "_REMOVE")
    print("\tNetworks loaded!")

    data_format = TOnet[3]
    print(str(data_format))

    orderc = ['X', 'X', 'X']
    orderc[TOnet[2]] = 'T'
    orderc[FROMnet[2]] = 'F'
    orderc[REMOVEnet[2]] = 'R'
    order = "" + orderc[0] + orderc[1] + orderc[2]
    print("\tOrder: " + order)

    print("Loading data...")

    if statesonly:
        if (expanded):
            A, B = load_expanded_states_dataset(datasetname)
        else:
            A, B = load_states_dataset(datasetname)
    else:
        if expanded:
            A, B = load_expanded_dataset(datasetname)
        else:
            A, B = load_dataset(datasetname)

    print("\tData loaded! Loaded: " + str(len(B)) + " data")

    print("Processing data and getting choices")
    X_TO_set = []
    X_FROM_set = []
    X_REMOVE_set = []

    TO_choice_set = []
    FROM_choice_set = []
    REMOVE_choice_set = []

    y_TO_set = []
    y_FROM_set = []
    y_REMOVE_set = []

    numbatch = int(len(A) / batchsize) + 1

    for i in range(1, numbatch + 1):
        print("\t" + str(i * batchsize * 100.0 / len(A)) + "%")

        if (i * batchsize <= len(A)):
            Ai = A[(i - 1) * batchsize:i * batchsize]
            Bi = B[(i - 1) * batchsize:i * batchsize]

        else:
            Ai = A[(i - 1) * batchsize:]
            Bi = B[(i - 1) * batchsize:]

        y_TO = process_move_onlyTO(Bi)
        y_FROM = process_move_onlyFROM(Bi)
        y_REMOVE = process_move_onlyREMOVE(Bi)

        init_state = process_state_binary(Ai, data_format)

        if order == "FTR":
            X_FROM = init_state
            FROM_choice = get_choices(FROMnet, X_FROM)

            X_TO = add_CHOICE_binary_raw(X_FROM, FROM_choice)
            TO_choice = get_choices(TOnet, X_TO)

            X_REMOVE = add_CHOICE_binary_raw(X_TO, TO_choice)
            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)
        elif order == "RFT":
            X_REMOVE = init_state
            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)

            X_FROM = add_CHOICE_binary_raw(X_REMOVE, REMOVE_choice)
            FROM_choice = get_choices(FROMnet, X_FROM)

            X_TO = add_CHOICE_binary_raw(X_FROM, FROM_choice)
            TO_choice = get_choices(TOnet, X_TO)
        elif order == "FRT":
            X_FROM = init_state
            FROM_choice = get_choices(FROMnet, X_FROM)

            X_REMOVE = add_CHOICE_binary_raw(X_FROM, FROM_choice)
            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)

            X_TO = add_CHOICE_binary_raw(X_REMOVE, REMOVE_choice)
            TO_choice = get_choices(TOnet, X_TO)
        elif order == "RTF":
            X_REMOVE = init_state
            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)

            X_TO = add_CHOICE_binary_raw(X_REMOVE, REMOVE_choice)

            TO_choice = get_choices(TOnet, X_TO)

            X_FROM = add_CHOICE_binary_raw(X_TO, TO_choice)

            FROM_choice = get_choices(FROMnet, X_FROM)
        elif order == "TRF":
            X_TO = init_state
            TO_choice = get_choices(TOnet, X_TO)

            X_REMOVE = add_CHOICE_binary_raw(X_TO, TO_choice)
            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)

            X_FROM = add_CHOICE_binary_raw(X_REMOVE, REMOVE_choice)
            FROM_choice = get_choices(FROMnet, X_FROM)
        elif order == "TFR":
            X_TO = init_state
            TO_choice = get_choices(TOnet, X_TO)

            #print("\tGot TO choices")

            X_FROM = add_CHOICE_binary_raw(X_TO, TO_choice)

            FROM_choice = get_choices(FROMnet, X_FROM)

            #print("\tGot FROM choices")

            X_REMOVE = add_CHOICE_binary_raw(X_FROM, FROM_choice)

            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)
        else:
            print("Unknown configuration. Using TFR")
            X_TO = init_state
            TO_choice = get_choices(TOnet, X_TO)

            #print("\tGot TO choices")

            X_FROM = add_CHOICE_binary_raw(X_TO, TO_choice)

            FROM_choice = get_choices(FROMnet, X_FROM)

            #print("\tGot FROM choices")

            X_REMOVE = add_CHOICE_binary_raw(X_FROM, FROM_choice)

            REMOVE_choice = get_choices(REMOVEnet, X_REMOVE)

        for j in range(len(X_TO)):
            X_TO_set.append(X_TO[j])
            X_FROM_set.append(X_FROM[j])
            X_REMOVE_set.append(X_REMOVE[j])

            TO_choice_set.append(TO_choice[j])
            FROM_choice_set.append(FROM_choice[j])
            REMOVE_choice_set.append(REMOVE_choice[j])

            y_TO_set.append(y_TO[j])
            y_FROM_set.append(y_FROM[j])
            y_REMOVE_set.append(y_REMOVE[j])

        #print("\tGot REMOVE choices")

    # variable renaming
    X_TO = X_TO_set
    X_FROM = X_FROM_set
    X_REMOVE = X_REMOVE_set

    TO_choice = TO_choice_set
    FROM_choice = FROM_choice_set
    REMOVE_choice = REMOVE_choice_set

    y_TO = y_TO_set
    y_FROM = y_FROM_set
    y_REMOVE = y_REMOVE_set

    print("Testing legality")

    legalities = get_legalities(TO_choice, FROM_choice, REMOVE_choice,
                                X_REMOVE, data_format)

    print("\ttesting the legality of " + str(len(legalities[0])) + " data\n")

    TO_self_leg = legalities[0]
    FROM_self_leg = legalities[1]
    REMOVE_self_leg = legalities[2]
    FROM_leg = legalities[3]
    REMOVE_leg = legalities[4]
    wholeFROM = legalities[5]
    wholeREMOVE = legalities[6]
    wholeMOVE = legalities[7]

    fileT = open(name + "_testing.txt", 'a')

    leg = ("Legality response on " + datasetname + ":" + "\n\tOnly TO leg:\t" +
           str(numpy.mean(TO_self_leg) * 100) + "\n\tOnly FROM leg:\t" +
           str(numpy.mean(FROM_self_leg) * 100) + "\n\tOnly REMOVE leg:\t" +
           str(numpy.mean(REMOVE_self_leg) * 100) + "\n\tOnly FROM-TO leg:\t" +
           str(numpy.mean(FROM_leg) * 100) + "\n\tOnly REMOVE-FROM-TO leg:\t" +
           str(numpy.mean(REMOVE_leg) * 100) + "\n\tWhole FROM leg:\t" +
           str(numpy.mean(wholeFROM) * 100) + "\n\tWhole REMOVE leg:\t" +
           str(numpy.mean(wholeREMOVE) * 100) + "\n\tWhole MOVE leg:\t" +
           str(numpy.mean(wholeMOVE) * 100))

    print(leg)
    fileT.write(leg)

    print("\nTesting accuracy and special cases\n")

    # accuracy valutation and legality evaluation for some particular cases:
    # the FROM move in phase 2 and the REMOVE when is not 0
    correctTO = 0
    correctFROM = 0
    correctREMOVE = 0
    correctWHOLE = 0

    # legality for FROM in phase 2
    legalFROM2 = 0
    f2 = 0

    # legality for REMOVE != 0 for the network
    legalREMOVEeat = 0
    re = 0

    # accuracy when REMOVE != 0 in dataset
    correctREMOVEyes = 0
    ry = 0

    # accuracy when FROM != 0 in dataset
    correctFROMyes = 0
    fy = 0

    # accuracy for the different phases
    correctWHOLE1 = 0
    p1 = 0
    correctWHOLE2 = 0
    p2 = 0
    correctWHOLE3 = 0
    p3 = 0

    size = len(TO_choice)

    print("\tTesting the accuracy of " + str(size) + " data")

    for i in range(size):

        # TO DECISION
        if TO_choice[i] == y_TO[i]:
            correctTO += 1

        # legal FROM decision in phase 2
        if (is_phase_2(X_REMOVE[i], data_format)):
            f2 += 1
            # legality
            l = get_legalities(TO_choice[i:i + 1], FROM_choice[i:i + 1],
                               REMOVE_choice[i:i + 1], X_REMOVE[i:i + 1],
                               data_format)
            if (l[5] == 1):
                legalFROM2 += 1

        # FROM decision is different from 0
        if y_FROM[i] != 0:
            fy += 1

        # FROM decision
        if FROM_choice[i] == y_FROM[i]:
            correctFROM += 1
            # correct FROM decision if different from 0
            if y_FROM[i] != 0:
                correctFROMyes += 1

        # REMOVE decision when present
        if y_REMOVE[i] != 0:
            ry += 1

        if REMOVE_choice[i] != 0:
            re += 1
            # legality
            l = get_legalities(TO_choice[i:i + 1], FROM_choice[i:i + 1],
                               REMOVE_choice[i:i + 1], X_REMOVE[i:i + 1],
                               data_format)
            if (l[6] == 1):
                legalREMOVEeat += 1

        # REMOVE decision
        if REMOVE_choice[i] == y_REMOVE[i]:
            correctREMOVE += 1
            if (y_REMOVE[i] != 0):
                correctREMOVEyes += 1

        if is_phase_1(X_REMOVE[i], data_format):
            p1 += 1
        elif is_phase_3(X_REMOVE[i], data_format):
            p3 += 1
        else:
            p2 += 1

        # WHOLE move (in different phases of game)
        if (TO_choice[i] == y_TO[i] and FROM_choice[i] == y_FROM[i]
                and REMOVE_choice[i] == y_REMOVE[i]):
            correctWHOLE += 1

            if is_phase_1(X_REMOVE[i], data_format):
                correctWHOLE1 += 1
            elif is_phase_3(X_REMOVE[i], data_format):
                correctWHOLE3 += 1
            else:
                correctWHOLE2 += 1

    if (size > 0):
        correctTO = correctTO * 100.0 / size
        correctFROM = correctFROM * 100.0 / size
        correctREMOVE = correctREMOVE * 100.0 / size
        correctWHOLE = correctWHOLE * 100.0 / size
    else:
        correctTO = -1
        correctFROM = -1
        correctREMOVE = -1
        correctWHOLE = -1

    if (f2 > 0):
        legalFROM2 = legalFROM2 * 100.0 / f2
    else:
        legalFROM2 = -1

    if (re > 0):
        legalREMOVEeat = (legalREMOVEeat * 100.0) / re
    else:
        legalREMOVEeat = -1

    if (ry > 0):
        correctREMOVEyes = correctREMOVEyes * 100.0 / ry
    else:
        correctREMOVEyes = -1

    if (fy > 0):
        correctFROMyes = correctFROMyes * 100.0 / fy
    else:
        correctFROMyes = -1

    if (p1 > 0):
        correctWHOLE1 = correctWHOLE1 * 100.0 / p1
    else:
        correctWHOLE1 = -1

    if (p2 > 0):
        correctWHOLE2 = correctWHOLE2 * 100.0 / p2
    else:
        correctWHOLE2 = -1

    if (p3 > 0):
        correctWHOLE3 = correctWHOLE3 * 100.0 / p3
    else:
        correctWHOLE3 = -1

    leg = ("\n\tFROM phase 2 leg:\t" + str(legalFROM2) +
           "\n\tREMOVE when chosen leg:\t" + str(legalREMOVEeat))

    if (not statesonly):
        leg += ("\n--------------------\n\nAccuracy response on " +
                datasetname + ":" + "\n\tTO accuracy:\t" + str(correctTO) +
                "\n\tFROM accuracy:\t" + str(correctFROM) +
                "\n\tREMOVE accuracy:\t" + str(correctREMOVE) +
                "\n\tWhole MOVE accuracy:\t" + str(correctWHOLE) +
                "\n\n\tWhole MOVE accuracy in phase 1:\t" +
                str(correctWHOLE1) + "\n\tWhole MOVE accuracy in phase 2:\t" +
                str(correctWHOLE2) + "\n\tWhole MOVE accuracy in phase 3:\t" +
                str(correctWHOLE3) + "\n\n\tFROM not 0 accuracy:\t" +
                str(correctFROMyes) + "\n\tREMOVE not 0 accuracy:\t" +
                str(correctREMOVEyes) + "\n\n\n")

    print(leg)
    fileT.write(leg)

    fileT.close()
コード例 #8
0
ファイル: testing.py プロジェクト: moscalej/Alpha-2Zero
def test_networks_reliability(datasetname, expanded, name, batchsize=10000):
    """
    Loads three existent networks and evaluate their mean reliability.

    The reliability is similar to a precision-recall curve, with the aim to
    measure the ability of a network to assign highest probability to legal
    moves.
    Defined the task to retrieve all the legal moves, the possible choices of
    each network are ranked accordingly their probability score.
    For defined percentages of recall, the precision percentage is computed.
    These values are saved in a realtive file.
    If the previous networks have taken an illegal choice for a state, that
    state it is not taken into account to compute the mean values.
    Also the cases in which the only legal choice is 0 are not considered.

    Parameters
    ----------
    datasetname : string
        The name of the dataset file.
    expanded : boolean
        True if the dataset has been already expanded through symmetries.
    name : string
        The name of the network configuration to load
    """

    start_time = time.time()

    print("Testing " + name)
    print("Loading networks...")
    print("\tloading " + name + "_TO")
    TOnet = load_net(name + "_TO")
    print("\tloading " + name + "_FROM")
    FROMnet = load_net(name + "_FROM")

    print("\tloading " + name + "_REMOVE")
    REMOVEnet = load_net(name + "_REMOVE")
    print("\tNetworks loaded!")

    data_format = TOnet[3]

    # an array in which is loaded the configuration: TFR/RFT/FTR ecc.
    orderc = ['X', 'X', 'X']
    orderc[TOnet[2]] = 'T'
    orderc[FROMnet[2]] = 'F'
    orderc[REMOVEnet[2]] = 'R'
    order = "" + orderc[0] + orderc[1] + orderc[2]

    nets = ['X', 'X', 'X']
    nets[TOnet[2]] = TOnet
    nets[FROMnet[2]] = FROMnet
    nets[REMOVEnet[2]] = REMOVEnet

    ordernames = ['X', 'X', 'X']
    ordernames[TOnet[2]] = "TO"
    ordernames[FROMnet[2]] = "FROM"
    ordernames[REMOVEnet[2]] = "REMOVE"

    print("\tOrder: " + order)

    print("Loading data from \"" + datasetname + "\" ...")
    if expanded:
        A, B = load_expanded_states_dataset(datasetname)
    else:
        A, B = load_states_dataset(datasetname)

    print("\tData loaded! Loaded: " + str(len(A)) + " data")

    numbatch = int(len(A) / batchsize) + 1

    T_p = []
    O_p = []
    Z_p = []

    num_data = len(A)
    perc = 0

    for i in range(1, numbatch + 1):

        if (i * batchsize <= len(A)):
            Ai = A[(i - 1) * batchsize:i * batchsize]
            percentage = i * batchsize * 100 / (num_data * 1.0)

        else:
            Ai = A[(i - 1) * batchsize:]
            percentage = 100.0

        X_0 = process_state_binary(Ai, data_format)
        bin_state = process_state_binary(Ai, "binary raw")

        ZERO_choice = get_choices(nets[0], X_0)
        ZERO_predictions = get_predictions(nets[0], X_0)

        X_1 = add_CHOICE_binary_raw(X_0, ZERO_choice)

        ONE_choice = get_choices(nets[1], X_1)
        ONE_predictions = get_predictions(nets[1], X_1)

        X_2 = add_CHOICE_binary_raw(X_1, ONE_choice)

        # remove scorings
        TWO_predictions = get_predictions(nets[2], X_2)

        # for each state
        for j in range(len(TWO_predictions)):

            moves = find_legal_moves(Ai[j], bin_state[j], "binary raw")

            # approximated precision values
            casep = []
            # real precision values
            prec = []

            # choices of the first and second networks
            ZC = ZERO_choice[j]
            OC = ONE_choice[j]

            choices = [ZC, OC, -1]

            Tsp = TWO_predictions[j]

            num_pred = len(Tsp)

            # se la seconda rete pensa che sia il caso di rimuovere
            if (numpy.argmax(Tsp) != 0):

                l = 0

                # SPEED UP: exclude this state if the previous networks have
                # already made illegal decisions
                TOchoice = choices[TOnet[2]]
                FROMchoice = choices[FROMnet[2]]
                REMOVEchoice = choices[REMOVEnet[2]]
                if orderc[2] == 'T':
                    move = ("F" + str(FROMchoice) + "R" + str(REMOVEchoice))
                elif orderc[2] == 'F':
                    move = ("T" + str(TOchoice) + "R" + str(REMOVEchoice))
                elif orderc[2] == 'R':
                    move = ("T" + str(TOchoice) + "F" + str(FROMchoice))

                if move in moves:
                    # per ogni possibile terza scelta
                    for k in range(num_pred):
                        TWO_choice = numpy.argmax(Tsp)
                        Tsp[TWO_choice] = -1
                        choices[2] = TWO_choice

                        TOchoice = choices[TOnet[2]]
                        FROMchoice = choices[FROMnet[2]]
                        REMOVEchoice = choices[REMOVEnet[2]]

                        move = ("T" + str(TOchoice) + "F" + str(FROMchoice) +
                                "R" + str(REMOVEchoice))

                        # each time a legal move is found,
                        # compute the precision
                        if move in moves:
                            l += 1
                            prec.append(l * 1.0 / (k + 1.0) * 100.0)

                    # compute the approximated values
                    if (l > 0):
                        rec = 0
                        for k in range(num_pred):
                            # if the approximated recal il less than the
                            # real recall
                            if ((k + 1.0) / num_pred) <= ((rec + 1.0) / l):
                                # assign the next value of precision
                                casep.append(prec[rec])
                            else:
                                # look at the next recall value and
                                # assign its precision value
                                rec += 1
                                casep.append(prec[rec])
                        T_p.append(casep)

            # probabilities of this state for the second network
            casep = []
            prec = []

            choices = [ZC, -1, -1]
            Osp = ONE_predictions[j]

            num_pred = len(Osp)

            l = 0

            # exclude this state if
            if (numpy.argmax(Osp) != 0):

                # SPEED UP: exclude this state if the previous networks have
                # already made illegal decisions
                TOchoice = choices[TOnet[2]]
                FROMchoice = choices[FROMnet[2]]
                REMOVEchoice = choices[REMOVEnet[2]]
                if orderc[0] == 'T':
                    move = ("T" + str(TOchoice))
                elif orderc[0] == 'F':
                    move = ("F" + str(FROMchoice))
                elif orderc[0] == 'R':
                    move = ("R" + str(REMOVEchoice))

                if move in moves:
                    # per ogni possibile seconda scelta
                    for k in range(num_pred):
                        OC = numpy.argmax(Osp)
                        Osp[OC] = -1

                        choices[1] = OC

                        #print("Choices: " + str(choices))

                        TOchoice = choices[TOnet[2]]
                        FROMchoice = choices[FROMnet[2]]
                        REMOVEchoice = choices[REMOVEnet[2]]

                        move = "ERROR"

                        # create the move according to the lacking network (the 3)
                        if orderc[2] == 'T':
                            move = ("F" + str(FROMchoice) + "R" +
                                    str(REMOVEchoice))
                        elif orderc[2] == 'F':
                            move = ("T" + str(TOchoice) + "R" +
                                    str(REMOVEchoice))
                        elif orderc[2] == 'R':
                            move = ("T" + str(TOchoice) + "F" +
                                    str(FROMchoice))

                        if move in moves:
                            l += 1
                            prec.append(l * 1.0 / (k + 1.0) * 100.0)

                    if (l > 0):
                        rec = 0
                        for k in range(num_pred):
                            #print (rec)
                            if ((k + 1.0) / num_pred) <= ((rec + 1.0) / l):
                                casep.append(prec[rec])
                            else:
                                rec += 1
                                casep.append(prec[rec])
                        #print(str(casep))
                        O_p.append(casep)

            # probabilities of this state for the first network
            casep = []
            prec = []
            choices = [-1, -1, -1]

            Zsp = ZERO_predictions[j]

            num_pred = len(Zsp)

            # se la prima rete pensa che sia il caso di rimuovere
            if (numpy.argmax(Zsp) != 0):
                l = 0.0

                # per ogni possibile rimozione
                for k in range(num_pred):
                    ZC = numpy.argmax(Zsp)
                    Zsp[ZC] = -1
                    choices[0] = ZC

                    TOchoice = choices[TOnet[2]]
                    FROMchoice = choices[FROMnet[2]]
                    REMOVEchoice = choices[REMOVEnet[2]]

                    move = "ERROR"

                    # create the move according to the only network
                    if orderc[0] == 'T':
                        move = ("T" + str(TOchoice))
                    elif orderc[0] == 'F':
                        move = ("F" + str(FROMchoice))
                    elif orderc[0] == 'R':
                        move = ("R" + str(REMOVEchoice))

                    if move in moves:
                        l += 1
                        prec.append(l * 1.0 / (k + 1.0) * 100.0)

                if (l > 0):
                    rec = 0
                    for k in range(num_pred):
                        #print (rec)
                        if ((k + 1.0) / num_pred) <= ((rec + 1.0) / l):
                            casep.append(prec[rec])
                        else:
                            rec += 1
                            casep.append(prec[rec])
                    #print(str(casep))
                    Z_p.append(casep)

        act_time = time.time()
        # gives feedback about the amount of data processed
        while (percentage > perc):
            perc += 1
        if perc != 100:
            print(
                str(round(percentage, 2)) + "%\t" + str(i * batchsize) +
                " data processed\ttime passed: " + str(act_time - start_time))

    print("100%\t" + str(num_data) + " data processed\ttime passed: " +
          str(act_time - start_time))

    filerel = open(name + "_reliability.txt", 'a')
    filerel.write("Test on: " + datasetname)

    report = ""

    report += ("\n\n" + ordernames[2] + ":\n")
    if len(T_p) > 0:
        m = numpy.mean(T_p, 0)
        for num in m:
            report += (str(round(num, 2)) + "\t")

    report += ("\n\n" + ordernames[1] + ":\n")
    if len(O_p) > 0:
        m = numpy.mean(O_p, 0)
        for num in m:
            report += (str(round(num, 2)) + "\t")

    report += ("\n\n" + ordernames[0] + ":\n")
    m = numpy.mean(Z_p, 0)
    for num in m:
        report += (str(round(num, 2)) + "\t")

    report += ("\n\n\n\n\n")
    filerel.write(report)
    print(report)
    filerel.close()

    act_time = time.time()

    print("Time occurred: " + str(act_time - start_time))
def detect(per_img, back_img, threshold, model_path, scene):

    # 用于存储最终结果
    result_dic = {}
    person_list = []
    total_person = 0
    police_num = 0
    prisoner_num = 0
    other = 0

    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.benchmark = False
    vis = False
    save_output = True

    model_name = os.path.basename(model_path).split('.')[0]
    # 初始化网络
    net = CrowdCounter()

    trained_model = os.path.join(model_path)
    # 加载权值文件
    network.load_net(trained_model, net)
    # net.cuda() 用于进行cuda加速
    net.eval()
    # 读取图片
    img_r = cv2.imread(per_img)
    img1 = cv2.imread(per_img, 0)
    # 保存灰度化之后的图片
    # img_gray = img1
    img = img1.astype(np.float32, copy=False)
    ht = img.shape[0]
    wd = img.shape[1]
    ht_1 = int((ht / 4) * 4)
    wd_1 = int((wd / 4) * 4)
    img = cv2.resize(img, (wd_1, ht_1))
    img = img.reshape((1, 1, img.shape[0], img.shape[1]))

    # 对图片进行预测
    heat_map = net.forward(img)
    heat_map = torch.squeeze(heat_map)
    heat_map = heat_map.data.numpy()
    [height, width] = heat_map.shape
    dst = cv2.resize(img_r, (width, height), interpolation=cv2.INTER_LINEAR)

    # img_gray = cv2.resize(img_gray, (width, height), interpolation=cv2.INTER_LINEAR)
    # 计算背景图和目标图片的梯度
    hist1 = gradient(per_img, width, height)
    hist2 = gradient(back_img, width, height)

    result = np.empty([height, width])
    # 计算梯度差
    for i in range(width - 1):
        for j in range(height - 1):
            result[j, i] = abs(hist1[j, i] - hist2[j, i])
    # 输出提督差结果
    # for i in range(width - 1):
    #     for j in range(height - 1):
    #         print('{0}-{1},{2}'.format(result[j, i], i, j))
    # 根据梯度差得出目标区域
    l = []
    for i in range(width - 1):
        for j in range(height - 1):
            # print('{0}-({1},{2})'.format(result[j, i], j, i))
            count = 0
            if heat_map[j, i] > 0:  # 预测结果大于0即可能是人
                # dst = cv2.circle(dst, (i, j), 1, (0, 0, 255), -1)
                # 将目标点向左向右个扩充15个像素向下扩充50个像素对比梯度差
                for ix in range(30):
                    for jy in range(50):
                        if (jy + j > 0) and (jy + j < height - 1) and (
                                ix + i - 5 > 0) and (ix + i - 5 < width - 1):
                            if result[jy + j, ix + i - 15] > threshold:
                                # 输出满足条件的点的阈值和坐标
                                count += 1
                # 根据图片的大小对左下角和右上角文字区域进行重点过滤
                if height == 180 and width == 320:
                    if j > 22 and (i < 175 or j < 156) and (i < 276
                                                            or j < 127):
                        if count > 500:
                            l.append([i, j])
                            # 在满足条件的地方绘制十字
                            # dst = cv2.circle(dst, (i, j), 1, (0, 0, 255), -1)
                elif height == 144 and width == 176:
                    if (i > 87 or j > 12) and (i < 116
                                               or j < 122) and (i < 148
                                                                or j < 103):
                        if count > 500:
                            l.append([i, j])
                            # dst = cv2.circle(dst, (i, j), 1, (0, 0, 255), -1)
                else:
                    if j > 22 and (i < 175 or j < 156) and (i < 276
                                                            or j < 127):
                        if count > 500:
                            l.append([i, j])
                            # 在满足条件的地方绘制十字
                            # dst = cv2.circle(dst, (i, j), 1, (0, 0, 255), -1)

    rects = []
    entry = True
    x = 0
    y = 0
    old_x = 0
    old_y = 0
    hsv = cv2.cvtColor(dst, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    lower_blue = np.array([110, 100, 50])
    upper_blue = np.array([130, 255, 255])
    lower_black = np.array([0, 0, 0])
    upper_black = np.array([170, 255, 48])
    mask1 = cv2.inRange(hsv, lower_blue, upper_blue)
    mask2 = cv2.inRange(hsv, lower_black, upper_black)
    # 将结果与原图做按位与
    # blueThings = cv2.bitwise_and(dst, dst, mask=mask)

    # 对目标区域通过矩形框进行聚类
    # 走廊/ab门
    if scene == 1:
        for (i, j) in l:
            if entry:
                # cv2.rectangle(dst, (i - 10, j - 10), (i + 20, j + 40), (0, 255, 0), 1)
                rects.append([i - 10, j - 10, 30, 35])
                x = i + 10
                y = j + 20
                old_x = i
                old_y = j
                entry = False
            if i > x or (j - y) > 35 or (old_y - j > 15
                                         and abs(old_x - i) < 15):
                entry = True
            # else:
            #     if j < old_y:
            #         rects.pop()
            #         rects.append([i - 10, j - 10, 30, 35])

        # 对图片进行二值化
        # ret, thresh1 = cv2.threshold(img_gray, 75, 255, cv2.THRESH_BINARY)
        # cv2.imshow("binary", thresh1)
        # thresh1 = thresh1.astype(np.uint32, copy=False)  # for i in range(width - 1):
        #     for j in range(height - 1):
        #         print('{0}-({1},{2})'.format(thresh1[j, i], i, j))
        # 对矩形框区域使用梯度差进行再次过滤
        l2 = []
        # 通过颜色判断人物的角色
        # 黑色:警察
        # 蓝色:囚犯
        # 其他:普通人
        for (x, y, w, h) in rects:
            count = 0
            # 保存矩形框类的黑色和蓝色区域
            black_count = 0
            blue_count = 0
            # 用于标示是否已添加文字类别
            label_entry = True
            for i in range(w - 1):
                for j in range(h - 1):
                    # 人员信息
                    person_dic = {}
                    if mask1[y + j, x + i] == 255:
                        blue_count += 1
                    if mask2[y + j, x + i] == 255:
                        black_count += 1
                    # if thresh1[y + j, x + i] == 255:
                    #     thresh_count = thresh_count + 1
                    if result[y + j, x + i] > threshold:
                        count += 1
                    if count > 350:  # and thresh_count < (w * h)/3:
                        l2.append([x, y, w, h])
                        # 根据颜色阈值对警察和囚犯进行标记
                        if label_entry:
                            if blue_count > 50:
                                total_person += 1  # 用于计数总人数
                                prisoner_num += 1  # 用于计数囚犯数目
                                label_entry = False
                                person_dic['position'] = [x, y, x + w, y + h]
                                person_dic['role'] = 'prisoner'
                                cv2.rectangle(dst, (x, y), (x + w, y + h),
                                              (0, 0, 255), 1)
                                cv2.putText(dst, 'prisoner', (x, y + 10),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 1)
                                person_list.append(person_dic)
                            elif black_count > 170:
                                total_person += 1  # 用于计数总人数
                                police_num += 1  # 用于计数警察数目
                                label_entry = False
                                person_dic['position'] = [x, y, x + w, y + h]
                                person_dic['role'] = 'police'
                                cv2.rectangle(dst, (x, y), (x + w, y + h),
                                              (255, 0, 0), 1)
                                cv2.putText(dst, 'police', (x, y + 10),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (255, 0, 0), 1)
                                person_list.append(person_dic)
            # 人员信息
            person_dic = {}
            if count > 350 and label_entry:
                label_entry = False
                total_person += 1  # 用于计数总人数
                other += 1  # 用于计数普通人数目
                person_dic['position'] = [x, y, x + w, y + h]
                person_dic['role'] = 'other'
                cv2.rectangle(dst, (x, y), (x + w, y + h), (255, 255, 0), 1)
                cv2.putText(dst, 'other', (x, y + 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 0), 1)
                person_list.append(person_dic)

    # 会见室
    elif scene == 2:
        l.reverse()
        for (i, j) in l:
            if entry:
                # cv2.rectangle(dst, (i - 10, j - 10), (i + 20, j + 25), (0, 255, 0), 1)
                rects.append([i - 10, j - 10, 30, 35])
                x = i + 20
                y = j + 20
                entry = False
            if i > x or j > y:
                entry = True
        # 对矩形框区域使用梯度差进行再次过滤
        l2 = []
        for (x, y, w, h) in rects:
            count = 0
            # 保存矩形框类的黑色和蓝色区域
            black_count = 0
            blue_count = 0
            # 用于标示是否已添加文字类别
            label_entry = True
            for i in range(w - 1):
                for j in range(h - 1):
                    # 人员信息
                    person_dic = {}
                    if mask1[y + j, x + i] == 255:
                        blue_count += 1
                    if mask2[y + j, x + i] == 255:
                        black_count += 1
                    if result[y + j, x + i] > threshold:
                        count += 1
                    if count > 420:
                        l2.append([x, y, w, h])
                        if label_entry:
                            if blue_count > 50:
                                total_person += 1  # 用于计数总人数
                                prisoner_num += 1  # 用于计数囚犯数目
                                label_entry = False
                                person_dic['position'] = [x, y, x + w, y + h]
                                person_dic['role'] = 'prisoner'
                                cv2.rectangle(dst, (x, y), (x + w, y + h),
                                              (0, 0, 255), 1)
                                cv2.putText(dst, 'prisoner', (x, y + 10),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (0, 0, 255), 1)
                                person_list.append(person_dic)
                            elif black_count > 170:
                                label_entry = False
                                total_person += 1  # 用于计数总人数
                                police_num += 1  # 用于计数警察数目
                                person_dic['position'] = [x, y, x + w, y + h]
                                person_dic['role'] = 'police'
                                cv2.rectangle(dst, (x, y), (x + w, y + h),
                                              (255, 0, 0), 1)
                                cv2.putText(dst, 'police', (x, y + 10),
                                            cv2.FONT_HERSHEY_SIMPLEX, 0.4,
                                            (255, 0, 0), 1)
                                person_list.append(person_dic)
            # 人员信息
            person_dic = {}
            if count > 420 and label_entry:
                label_entry = False
                total_person += 1  # 用于计数总人数
                other += 1  # 用于计数普通人数目
                person_dic['position'] = [x, y, x + w, y + h]
                person_dic['role'] = 'other'
                cv2.rectangle(dst, (x, y), (x + w, y + h), (255, 255, 0), 1)
                cv2.putText(dst, 'other', (x, y + 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 0), 1)
                person_list.append(person_dic)
            print(count)
    # 组装生成最后的结果
    result_dic['total_people'] = total_person
    result_dic['police_num'] = police_num
    result_dic['prisoner_num'] = prisoner_num
    result_dic['other'] = other
    result_dic['people_list'] = person_list
    print(result_dic)
    cv2.namedWindow("result")
    cv2.imshow("result", dst)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return result_dic
コード例 #10
0
    def forward(self,
                im_data,
                im_info,
                gt_ocr,
                image,
                gt_boxes=None,
                gt_ishard=None,
                dontcare_areas=None):
        features, rois = self.rpn(im_data, im_info, gt_boxes, gt_ishard,
                                  dontcare_areas)

        if self.training:
            roi_data = self.proposal_target_layer(rois, gt_boxes, gt_ishard,
                                                  dontcare_areas,
                                                  self.n_classes)
            rois = roi_data[0]

        # roi pool
        pooled_features = self.roi_pool(features, rois)
        x = pooled_features.view(pooled_features.size()[0], -1)
        x = self.fc6(x)
        x = F.dropout(x, training=self.training)
        x = self.fc7(x)
        x = F.dropout(x, training=self.training)

        cls_score = self.score_fc(x)
        cls_prob = F.softmax(cls_score)
        bbox_pred = self.bbox_fc(x)

        if self.training:
            self.cross_entropy, self.loss_box = self.build_loss(
                cls_score, bbox_pred, roi_data)
        pred_boxes, scores, classes = \
            self.interpret_faster_rcnn(cls_prob, bbox_pred, rois, im_info, image.shape, min_score=0.00)
        print classes
        ocr_batch = []
        # print gt_boxes[2,4],gt_ocr,classes
        # os.exit()
        for i in range(0, 128):
            a = classes[i]
            # print a
            for j in range(gt_boxes.shape[0]):
                if a == gt_boxes[j, 4]:
                    # print b,"wow"

                    ocr_batch.append(gt_ocr[j])
                    # print text[j],"yohoo"
                    break
                if a == 0:
                    ocr_batch.append('')
                    break

        # coordi = np.zeros((128, 4))
        print(ocr_batch)
        if len(ocr_batch) < 128:
            for i in range(0, 128 - len(ocr_batch)):
                ocr_batch.append('')
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # concat=image
        # concat=cv2.resize(concat, (128, 32), interpolation=cv2.INTER_AREA)
        # concat = np.expand_dims(concat, axis=0)
        # concat = np.expand_dims(concat, axis=0)
        # print("yohoo")
        count = 0
        for i in range(0, 128):
            x = pred_boxes[i][0]
            y = pred_boxes[i][1]
            h = pred_boxes[i][2]
            w = pred_boxes[i][3]
            # print x,y,h,w,"dims"
            #		image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
            #		np.expand_dims(image,axis=0)
            # if(math.isnan(x)==True or math.isnan(y)==True or math.isnan(w)==True or math.isnan(h)==True):
            #     continue
            # print("yoyo")
            crop = self.crop_Img(image, int(x), int(y), int(w), int(h))
            # print(crop.shape)
            #		np.swapaxes(crop,1,2)
            # print(crop.shape, "crop shape")
            #		np.swapaxes(crop,0,1)
            if crop.shape[0] > 0 and crop.shape[1] > 0:
                crop = cv2.resize(crop, (128, 32),
                                  interpolation=cv2.INTER_AREA)
                cv2.imwrite("image_processed.png", crop)
                crop = np.expand_dims(crop, axis=0)
                crop = np.expand_dims(crop, axis=0)
                if count == 0:
                    concat = crop
                else:
                    concat = np.concatenate((crop, concat), axis=0)
                count = count + 1
        crnn = crnn_py.CRNN(32, 1, 63, 256, 1).cuda()
        # model_path = '/home/gabbar/netCRNN_9_30000.pth'

        # network.load_net(model_path,self.crnn)
        # for log
        network.load_net('/home/gabbar/crnn_path/crnn_9.h5', crnn)
        self.concat = concat
        self.cost = crnn(self.concat, ocr_batch)