예제 #1
0
def gen_train_data(in_path, out_path):
    matchObj = re.compile(r'(.+):([0-9\.]+)', re.M | re.I)
    res, qid = [], 0
    qw = query_weight()
    text = [e.strip().split("\t") for e in open(in_path, encoding="utf8").readlines() if e.strip()]
    for i, ele in enumerate(tqdm(text, total=len(text))):
        line_ele, sen2terms = [], []
        for e in ele:
            matchRes = matchObj.match(e)
            term, weight = matchRes.group(1), matchRes.group(2)
            line_ele.append((term, weight))
            sen2terms.append(term)
        qw.run_step(" ".join(sen2terms))
        weight_attn, weight_idf, weight_lm = qw.weight_attn, qw.weight_idf, qw.weight_lm
        sorted_line_ele = sorted(line_ele, key=lambda d: d[1], reverse=True)
        for i in range(len(sorted_line_ele)):
            feature_vector, fmap = get_feature(sorted_line_ele[i][0], sen2terms, weight_attn, weight_idf, weight_lm)
            res.append(" ".join([str(len(sorted_line_ele) - i - 1), "qid:" + str(qid)] + [str(i+1) + ":" + str(e) for i, e in enumerate(feature_vector)]))
        qid += 1
    print("train data length: %d" % (len(res)))
    with open(out_path + "train.txt", "w", encoding="utf8") as fin:
        fin.write("\n".join(res[:int(len(res) * 0.9)]))
    with open(out_path + "test.txt", "w", encoding="utf8") as fin:
        fin.write("\n".join(res[int(len(res) * 0.9):]))
    with open(out_path + "valid.txt", "w", encoding="utf8") as fin:
        fin.write("\n".join(res[int(len(res) * 0.9):]))
    with open(out_path + "feature.fmap", "w", encoding="utf8") as fin:
        fin.write("\n".join(fmap))
예제 #2
0
def get_motifs(model, data_x, data_y, protein_name):
    model.trainable = False
    pos_x = get_pos(data_x, data_y)
    data_x = pos_x.reshape(pos_x.shape[0], pos_x.shape[1], pos_x.shape[2], 1)
    features = get_feature(model=model, data=data_x)
    features = features.reshape(features.shape[0], features.shape[1], features.shape[3])
    if os.path.exists('./' + protein_name + '.fa'):
        os.remove('./' + protein_name + '.fa')
    fp = open('./' + protein_name + '.fa', 'w')

    count = 0
    for i in range(features.shape[0]):
        seq = get_seq(data_x[i])
        for j in range(features.shape[1]):
            count_1 = 0
            for k in range(features.shape[2]):
                if features[i][j][k] > 0:
                    count_1 += 1
            if count_1 < 33 + int(len(data_y) / 4000):
                continue
            else:
                for k in range(features.shape[2]):
                    if features[i][j][k] > 0.4:
                        fp.write('>' + 'seq_' + str(i) + '_' + 'filter' + str(j) + '_' + str(k) + '\n')
                        fp.write(seq[j:j + 7] + '\n')
                        count += 1
    fp.close()
    print('count:', count)
    print('{}start get {} logo{}'.format('*' * 10, protein_name, '*' * 10))
    get_logo(protein_name)
    print('{}draw {} logo done{}'.format('*' * 10, protein_name, '*' * 10))
예제 #3
0
    def inference(self, question, source='solr'):
        if not source:
            source = 'solr'
        documents = self.search(question, source)
        # print(documents)
        assert len(documents) != 0, 'Can not search any passages.'

        response = []
        for document in documents:
            result = OrderedDict()
            passage = document['passage']
            source = document['source']
            inps = (passage, question)
            feed_dict = get_feature(self.model, inps, self.vocab, self.config)
            start_prob, end_prob = self.sess.run(
                [self.model.start_probs, self.model.end_probs],
                feed_dict=feed_dict)
            best_start, best_end, max_prob = find_best_answer(
                start_prob[0], end_prob[0], self.model.max_a_len)
            answer = ''.join(list(jieba.cut(passage))[best_start:best_end + 1])
            score = max_prob
            result['answer'] = answer
            result['score'] = str(score)
            result['passage'] = passage
            result['source'] = source
            response.append(result)
        response = sorted(response,
                          key=lambda x: float(x['score']),
                          reverse=True)
        return {'response': response}
예제 #4
0
 def __update(self):
     patch = get_subwindow(self.img, self.pos, self.window_size)
     xf = fft2(
         get_feature(patch, self.feature, self.cell_size, self.cos_window))
     alphaf = self.__train(xf)
     self.model_xf = (
         1 - self.interp_factor) * self.model_xf + self.interp_factor * xf
     self.model_alphaf = (
         1 - self.interp_factor
     ) * self.model_alphaf + self.interp_factor * alphaf
예제 #5
0
 def rank_weight(self, sen2terms, weight_attn, weight_idf, weight_lm):
     tmp, score_sum = [], 1e-8
     for term in sen2terms:
         feature_vector, _ = get_feature(term, sen2terms, weight_attn,
                                         weight_idf, weight_lm)
         feature = np.array(feature_vector)
         feature_csr = sparse.csr_matrix(feature)
         input = DMatrix(feature_csr)
         score = self.xgb_model.predict(input)[0]
         prob = 1.0 / (1 + math.exp(-1 * score))
         tmp.append((term, prob))
         score_sum += prob
     res = [(k, round(v / score_sum, 3)) for k, v in tmp]
     return res
예제 #6
0
파일: main.py 프로젝트: kamahori/ESP-RL
def main():
    eps = eps_start

    for i_episode in range(1, n_episode + 1):
        state = env.reset()
        done = False
        step = 0

        for t in range(1, n_timestep + 1):
            step += 1
            action, _ = choose_action(
                state, F_network, C_network, n_action, n_state, eps, device
            )
            next_state, reward, done, _ = env.step(action)
            feature = get_feature(state)
            action_vector = np.zeros(n_action)
            action_vector[action] = 1.0
            experience = experience_t(
                np.concatenate([state, action_vector]),
                reward,
                feature,
                next_state,
                done and t < n_timestep,
            )
            state = next_state
            memory.append(experience)

            if step % freq_update_network == 0 and len(memory) > batch_size:
                train()

            if t % freq_target_update == 0:
                soft_update(F_network, F_aux, tau)
                soft_update(C_network, C_aux, tau)

            eps = max(eps_end, eps - (eps_start - eps_end) / (eps_decrease_steps - 1))

            if done:
                break

        if i_episode % freq_evaluation == 0:
            evaluation()

    torch.save(F_network.state_dict(), F_net_name)
    torch.save(C_network.state_dict(), C_net_name)
예제 #7
0
파일: main.py 프로젝트: kamahori/ESP-RL
def evaluation():
    total_reward = 0
    total_GVF_loss = 0

    n_trial = 100

    for _ in range(n_trial):
        state = env.reset()
        done = False

        gt_feature = torch.zeros(n_timestep, n_feature)
        pred_feature = torch.zeros(n_timestep, n_feature)
        discounted_para = torch.zeros(n_timestep, 1)

        step = 0
        for t in range(1, n_timestep + 1):
            step += 1
            action, pred_feature_vector = choose_action(
                state, F_network, C_network, n_action, n_state, 0.0, device
            )

            next_state, reward, done, _ = env.step(action)
            total_reward += reward
            gt_feature_vector = torch.tensor(get_feature(state))

            pred_feature[step - 1] = pred_feature_vector

            gt_feature[:step] += (
                gt_feature_vector * (GVF_discount_factor ** discounted_para)
            )[:step]

            discounted_para[:step] += 1
            state = next_state

            if done:
                break

        with torch.no_grad():
            criterion = nn.MSELoss()
            total_GVF_loss += criterion(gt_feature[:step], pred_feature[:step]).item()

    avg_reward = total_reward / n_trial
    avg_GVF_loss = total_GVF_loss / n_trial
예제 #8
0
def evaluate(model, dev_data, batch_size, max_sent_length, device):
    was_training = model.training
    model.eval()

    vocab_tag = model.vocab_tag
    tok2idx, idx2tok = vocab_tag['token_to_index'], vocab_tag['index_to_token']
    tag2idx, idx2tag = vocab_tag['tag_to_index'], vocab_tag['index_to_tag']

    data_loader = DataLoader(dev_data, batch_size=batch_size)
    val_loss = 0
    cum_cnt = 0
    with torch.no_grad():
        for iter_idx, data in enumerate(data_loader):
            chars_batch, tags_batch = get_feature(data)
            chars_batch, chars_mask = pad_sents(chars_batch,
                                                pad_token,
                                                max_len=max_sent_length)
            tags_batch, tags_mask = pad_sents(tags_batch,
                                              pad_token,
                                              max_len=max_sent_length)

            input_chars = torch.tensor(token2id(chars_batch,
                                                tok2idx,
                                                unk_id=tok2idx['<UNK>']),
                                       device=device)
            target_tags = torch.tensor(token2id(tags_batch, tag2idx),
                                       device=device)
            tags_mask = torch.tensor(tags_mask,
                                     dtype=torch.uint8,
                                     device=device)

            #            output_emission = conv_seg(input_chars.to(device))
            #            loss = -crf_model(output_emission.transpose(0,1), target_tags.transpose(0,1).to(device), tags_mask.transpose(0,1).to(device))
            loss = -model(input_chars, target_tags, tags_mask)
            val_loss += loss
            cum_cnt = cum_cnt + input_chars.shape[0]
    val_loss = val_loss / cum_cnt

    if was_training:
        model.train()

    return val_loss
예제 #9
0
    def dectect(self, img):
        # 这个其实和下面update是一样的,不知道为什么源代码会分成两部分写
        self.original_img = img
        self.img = img
        if self.resize:
            self.img = cv2.resize(self.img, self.img_size[::-1])

        if self.feature == 'gray':
            self.img = cv2.cvtColor(
                self.img,
                cv2.COLOR_BGR2GRAY)  # translate image from rgb to gray

        patch = get_subwindow(self.img, self.pos, self.window_size)
        zf = fft2(
            get_feature(patch, self.feature, self.cell_size, self.cos_window))

        if self.kernel_type == "gaussian":
            kzf = gaussian_correlation(zf, self.model_xf, self.kernel_sigma)
        elif self.kernel_type == "polynomial":
            kzf = polynomial_correlation(zf, self.model_xf, self.kernel_poly_a,
                                         self.kernel_poly_b)
            pass

        response = np.real(ifft2(self.model_alphaf * kzf))
        cv2.imshow("response", response)
        cv2.waitKey(10)

        [vert_delta, horiz_delta] = np.unravel_index(response.argmax(),
                                                     response.shape)
        # print("[vert_delta, horiz_delta] = " + str([vert_delta, horiz_delta]))
        if vert_delta > zf.shape[0] / 2:
            vert_delta = vert_delta - zf.shape[0]
        if horiz_delta > zf.shape[1] / 2:
            horiz_delta = horiz_delta - zf.shape[1]
        # print("after handled [vert_delta, horiz_delta] = " + str([vert_delta, horiz_delta]))

        self.pos = int(self.pos[0] + self.cell_size *
                       (vert_delta)), int(self.pos[1] + self.cell_size *
                                          (horiz_delta))
        self.__show_image()
        self.__update()
        return self.original_pos, self.original_target_size
예제 #10
0
def save_person_information(name):
    saved_model = './ArcFace/model/068.pth'
    info_path = './users/'+name
    if not os.path.exists(info_path):
        os.makedirs(info_path)

    # threshold =  0.30896
    model = mobileFaceNet()
    model.load_state_dict(t.load(saved_model)['backbone_net_list'])
    model.eval()
    use_cuda = t.cuda.is_available() and True
    device = t.device("cuda" if use_cuda else "cpu")
    # is_cuda_avilableqq
    trans = transforms.Compose([
        transforms.Resize((112,112)),
        transforms.ToTensor(),
        transforms.Normalize([0.5,0.5,0.5],[0.5,0.5,0.5])
    ])
    model.to(device)

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print('failed open camara!!!')
    ret, frame = cap.read()
    while ret :
        frame = frame[:,:,::-1]
        img = Image.fromarray(frame)
        bboxes, landmark = detect_faces(img)
        show_img = show_bboxes(img,bboxes,landmark)
        show_img = np.array(show_img)[:,:,::-1]
        cv2.imshow('img',show_img) # 480 640 3
        if cv2.waitKey(1) & 0xFF == ord('c'):
            person_img = frame[int(bboxes[0,1]):int(bboxes[0,3]),int(bboxes[0,0]):int(bboxes[0,2])]
            cv2.imshow('crop',person_img[:,:,::-1])
            cv2.imwrite(os.path.join(info_path,'%s.jpg'%(name)),person_img[:,:,::-1])
            feature = np.squeeze(get_feature(person_img,model,trans,device))
            np.savetxt(os.path.join(info_path,'%s.txt'%(name)),feature)

            # cv2.waitKey(30)
        if cv2.waitKey(30) & 0xFF == ord('q'):
            break
        ret, frame = cap.read()
예제 #11
0
def get_pos_states(alist):
    global cap

    x, y = alist[0], alist[1]
    # print("x, y:", x, y)
    ret,img = cap.read()
    cv2.imshow("imag",img)
    img_trans = perTrans(img)
    cv2.imshow("perTrans_rst",img_trans)
    
    if pos_to_id(x, y) == None:
        return 0
    # print("get_pos_states:", x, y)
    x_t, y_t = pos_to_id(x, y)
    x_t, y_t = 15+45*x_t, 13+46*y_t
    roi = img_trans[y_t-10:y_t+10, x_t-10:x_t+10, :]

    cv2.imwrite(r"C:\Users\SadAngel\Desktop\myworkplace\test1.jpg", roi) 
    
    img_path = r"C:\Users\SadAngel\Desktop\myworkplace\test1.jpg"
    
    f1, f2, f3, f4, f5 = list(get_feature(img_path))[:5]
    print(f1, f2, f3, f4, f5)

    # 此处应该转格式
    roi = Image.open(r"C:\Users\SadAngel\Desktop\myworkplace\test1.jpg") 

    image = roi.convert('RGB')  
    x, y, z = get_dominant_color(image)
    if x > 150 :
    #x , y , z = hsv_caculate(x,y,z)
    # if x > 170 and (y > 100 or z > 100): 
        print(x, y, z, 'checker')
        # print(alist[0], alist[1])
        result = 'Checker' 
        return 1
    else:
        # print(x, y, z, 'null')
        result = 'Null' 
        return 2
    
    return 0
예제 #12
0
def test(model_path, data_path, split, batch_size, max_sent_length, cuda):
    device = torch.device("cuda:0" if cuda else "cpu")
    dataset = SIGHAN(split='dev', root_path=data_path)
    model = CharWordSeg.load(model_path)
    model = model.to(device)
    model.eval()

    vocab_tag = model.vocab_tag
    tok2idx, idx2tok = vocab_tag['token_to_index'], vocab_tag['index_to_token']
    tag2idx, idx2tag = vocab_tag['tag_to_index'], vocab_tag['index_to_tag']

    data_loader = DataLoader(dataset, batch_size=batch_size)
    val_loss = 0
    cum_cnt = 0
    with torch.no_grad():
        for iter_idx, data in enumerate(data_loader):
            chars_batch, tags_batch = get_feature(data)
            chars_batch, chars_mask = pad_sents(chars_batch,
                                                pad_token,
                                                max_len=max_sent_length)
            tags_batch, tags_mask = pad_sents(tags_batch,
                                              pad_token,
                                              max_len=max_sent_length)

            input_chars = torch.tensor(token2id(chars_batch,
                                                tok2idx,
                                                unk_id=tok2idx['<UNK>']),
                                       device=device)
            target_tags = torch.tensor(token2id(tags_batch, tag2idx),
                                       device=device)
            tags_mask = torch.tensor(tags_mask,
                                     dtype=torch.uint8,
                                     device=device)

            loss = -model(input_chars, target_tags, tags_mask)
            val_loss += loss
            cum_cnt = cum_cnt + input_chars.shape[0]
    val_loss = val_loss / cum_cnt
    return val_loss
예제 #13
0
    def backend(self, sess, model, config, run_event):
        global query, response

        while run_event.is_set():
            sleep(0.1)
            if query:
                local_response = list()
                for passage in query[0]:
                    inp = (passage, query[1])
                    passage_tokens, passage_token_ids, question_token_ids, \
                        p_length, q_length, passage_char_ids, \
                        question_char_ids = get_feature(
                            inp, model.vocab, config)
                    feed_dict = {
                        model.p: [passage_token_ids],
                        model.q: [question_token_ids],
                        model.p_length: [p_length],
                        model.q_length: [q_length],
                        model.ph: [passage_char_ids],
                        model.qh: [question_char_ids],
                        model.start_label: [0],
                        model.end_label: [0],
                        model.dropout: config.dropout
                    }
                    start_prob, end_prob = sess.run(
                        [model.start_probs, model.end_probs],
                        feed_dict=feed_dict)

                    best_start, best_end, max_prob = find_best_answer(
                        start_prob[0], end_prob[0], model.max_a_len)

                    answer = ''.join(passage_tokens[best_start:best_end + 1])
                    score = str(max_prob)

                    local_response.append({'answer': answer, 'score': score})
                response = local_response
                local_response = list()
                query = tuple()
예제 #14
0
def predict(model_path, data_path, split, output_path, batch_size,
            max_sent_length, cuda):
    device = torch.device("cuda:0" if cuda else "cpu")
    dataset = SIGHAN(split=split, root_path=data_path)
    output = []

    model = CharWordSeg.load(model_path)
    model = model.to(device)
    model.eval()

    vocab_tag = model.vocab_tag
    tok2idx, idx2tok = vocab_tag['token_to_index'], vocab_tag['index_to_token']
    tag2idx, idx2tag = vocab_tag['tag_to_index'], vocab_tag['index_to_tag']

    data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=False)
    with torch.no_grad():
        for iter_idx, data in enumerate(data_loader):
            chars = get_feature(data, stage='predict')
            chars_batch, chars_mask = pad_sents(chars,
                                                pad_token,
                                                max_len=max_sent_length)

            input_chars = torch.tensor(token2id(chars_batch,
                                                tok2idx,
                                                unk_id=tok2idx['<UNK>']),
                                       device=device)
            chars_mask = torch.tensor(chars_mask,
                                      dtype=torch.uint8,
                                      device=device)

            pred_tags = id2token(model.decode(input_chars, chars_mask),
                                 idx2tag)
            output.extend(create_output(chars, pred_tags))

    with codecs.open(output_path, 'w', 'utf8') as f:
        for sent in output:
            print(sent, file=f)
예제 #15
0
def train(split, data_path, save_to, batch_size, max_sent_length,
          char_embed_size, num_hidden_layer, channel_size, kernel_size,
          learning_rate, max_epoch, log_every, patience, max_num_trial,
          lr_decay, last_model_path, cuda):
    if not Path(save_to).exists():
        Path(save_to).mkdir()

    device = torch.device("cuda:0" if cuda else "cpu")
    dataset = SIGHAN(split=split, root_path=data_path)
    val_dataset = SIGHAN(split='dev', root_path=data_path)

    # build vocab
    train_chars, train_tags = get_feature(dataset.data)
    vocab = build_vocab(chain.from_iterable(train_chars))
    tok2idx, idx2tok = add_id(vocab)
    tags = build_vocab(["B", "M", "E", "S"], add_unk=False, add_pad=True)
    tag2idx, idx2tag = add_id(tags)

    # build model
    # conv_seg = CharWordSeg(len(tags), len(vocab), char_embed_size, num_hidden_layer, channel_size, kernel_size).to(device)
    # crf_model = CRF(num_tags=5).to(device)
    # conv_seg.train()
    # crf_model.train()
    # optimizer = torch.optim.Adam(list(conv_seg.parameters())+list(crf_model.parameters()), lr=learning_rate)
    vocab_tag = {
        "token_to_index": tok2idx,
        "index_to_token": idx2tok,
        "tag_to_index": tag2idx,
        "index_to_tag": idx2tag
    }

    data_loader = DataLoader(dataset, batch_size=batch_size)
    model = CharWordSeg(vocab_tag, char_embed_size, num_hidden_layer,
                        channel_size, kernel_size).to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    if last_model_path is not None:
        # load model
        logging.info(f'load model from  {last_model_path}')
        params = torch.load(last_model_path,
                            map_location=lambda storage, loc: storage)
        model.load_state_dict(params['state_dict'])
        logging.info('restore parameters of the optimizers')
        optimizer.load_state_dict(torch.load(last_model_path + '.optim'))

    model.train()

    epoch = 0
    cur_trial = 0
    hist_valid_scores = []
    train_time = begin_time = time.time()
    logging.info("begin training!")
    while True:
        epoch += 1
        train_loss = 0
        cum_cnt = 0
        for iter_idx, data in enumerate(data_loader):
            optimizer.zero_grad()
            chars_batch, tags_batch = get_feature(data)
            chars_batch, chars_mask = pad_sents(chars_batch,
                                                pad_token,
                                                max_len=max_sent_length)
            tags_batch, tags_mask = pad_sents(tags_batch,
                                              pad_token,
                                              max_len=max_sent_length)

            input_chars = torch.tensor(token2id(chars_batch,
                                                tok2idx,
                                                unk_id=tok2idx['<UNK>']),
                                       device=device)
            target_tags = torch.tensor(token2id(tags_batch, tag2idx),
                                       device=device)
            tags_mask = torch.tensor(tags_mask,
                                     dtype=torch.uint8,
                                     device=device)

            #            output_emission = conv_seg(input_chars.to(device))
            #            loss = -crf_model(output_emission.transpose(0,1), target_tags.transpose(0,1).to(device), tags_mask.transpose(0,1).to(device))
            loss = -model(input_chars, target_tags, tags_mask)
            train_loss += loss
            cum_cnt = cum_cnt + input_chars.shape[0]
            loss.backward()
            optimizer.step()

        train_loss = train_loss / cum_cnt
        val_loss = evaluate(model, val_dataset, batch_size, max_sent_length,
                            device)

        logging.info(
            f'epoch {epoch}\t train_loss: {train_loss}\t val_loss:{val_loss}\t  speed:{time.time()-train_time:.2f}s/epoch\t time elapsed {time.time()-begin_time:.2f}s'
        )
        train_time = time.time()

        is_better = len(
            hist_valid_scores) == 0 or val_loss < min(hist_valid_scores)
        hist_valid_scores.append(val_loss)

        if epoch % log_every == 0:
            model.save(f"{save_to}/model_step_{epoch}")
            torch.save(optimizer.state_dict(),
                       f"{save_to}/model_step_{epoch}.optim")
        if is_better:
            cur_patience = 0
            model_save_path = f"{save_to}/model_best"
            print(f'save currently the best model to [{model_save_path}]')
            model.save(model_save_path)
            # also save the optimizers' state
            torch.save(optimizer.state_dict(), model_save_path + '.optim')
        elif cur_patience < patience:
            cur_patience += 1
            print('hit patience %d' % cur_patience)

            if cur_patience == patience:
                cur_trial += 1
                print(f'hit #{cur_trial} trial')
                if cur_trial == max_num_trial:
                    print('early stop!')
                    exit(0)

                # decay lr, and restore from previously best checkpoint
                lr = optimizer.param_groups[0]['lr'] * lr_decay
                logging.info(
                    f'load previously best model and decay learning rate to {lr}'
                )

                # load model
                params = torch.load(model_save_path,
                                    map_location=lambda storage, loc: storage)
                model.load_state_dict(params['state_dict'])
                model = model.to(device)

                logging.info('restore parameters of the optimizers')
                optimizer.load_state_dict(
                    torch.load(model_save_path + '.optim'))

                # set new lr
                for param_group in optimizer.param_groups:
                    param_group['lr'] = lr

                # reset patience
                cur_patience = 0

        if epoch == max_epoch:
            print('reached maximum number of epochs!')
            exit(0)
예제 #16
0
import numpy as np
from sklearn import datasets
from utils import get_feature
import cv2

if __name__ == '__main__':
    img_path = r'.\vision\2-classify\data\1\test1554022732.042874.jpg'
    X_data = []
    X_data.append(get_feature(img_path))

    X = np.array(X_data)

    print(type(X))
    print(X.shape)
    X = X.astype(np.float32)

    # svm = cv2.ml.SVM_create()
    # svm.load("svmtest.mat")
    svm = cv2.ml.SVM_load(r".\vision\2-classify\svmtest.mat")
    '''开始预测'''
    print(X)
    print(svm.predict(X))
    _, y_pred = svm.predict(X)
    print(y_pred)
예제 #17
0
from utils import get_feature
import os

rootdir = r'.\vision\2-classify\data\0'
alist = os.listdir(rootdir)  #列出文件夹下所有的目录与文件
# print(alist[:20])
X_data = []

for i, path in enumerate(alist):
    img_path = os.path.join(rootdir, alist[i])
    print(img_path)
    if os.path.isfile(img_path):
        fea = get_feature(img_path)
        X_data.append(fea)

lenth = len(alist)
y_data = [0] * lenth

rootdir = r'.\vision\2-classify\data\1'
alist = os.listdir(rootdir)  #列出文件夹下所有的目录与文件
for i, path in enumerate(alist):
    img_path = os.path.join(rootdir, alist[i])
    print(img_path)

    if os.path.isfile(img_path):
        fea = get_feature(img_path)
        X_data.append(fea)

lenth = len(alist)
y_data = y_data + [1] * lenth
예제 #18
0
    def __init__(self,
                 batch_size,
                 steps,
                 embeddings,
                 embedding_size,
                 rnn_size,
                 num_rnn_layers,
                 max_grad_norm,
                 filter_sizes,
                 num_filters,
                 self_att=True,
                 model_choice=0,
                 mode="qa",
                 rnn_windows=5,
                 l2_reg_lambda=0.0,
                 adjust_weight=False,
                 label_weight=[],
                 is_training=True):

        self.batch_size = batch_size
        self.embeddings = embeddings
        self.embedding_size = embedding_size
        self.adjust_weight = adjust_weight
        self.label_weight = label_weight
        self.rnn_size = rnn_size
        self.steps = steps
        self.max_grad_norm = max_grad_norm
        self.l2_reg_lambda = l2_reg_lambda
        self.is_training = is_training
        self.filter_sizes = list(map(int, filter_sizes.split(',')))
        self.num_filters = num_filters
        self.mode_choice = model_choice
        self.rnn_windows = rnn_windows
        self.att = self_att

        self.keep_pron = tf.placeholder(tf.float32, name='keep_prob')
        self.lr = tf.Variable(0.0, trainable=False)
        self.new_lr = tf.placeholder(tf.float32,
                                     shape=[],
                                     name='new-learning-rate')
        self.lr_update = tf.assign(self.lr, self.new_lr)

        self.ori_input = tf.placeholder(tf.int32,
                                        shape=[None, self.steps],
                                        name='ori_inputs_quests')
        self.cand_input = tf.placeholder(tf.int32,
                                         shape=[None, self.steps],
                                         name='cand_inputs_quests')
        self.neg_input = tf.placeholder(tf.int32,
                                        shape=[None, self.steps],
                                        name='neg_inputs_quests')
        self.test_q = tf.placeholder(tf.int32,
                                     shape=[None, self.steps],
                                     name='test_q')
        self.test_a = tf.placeholder(tf.int32,
                                     shape=[None, self.steps],
                                     name='test_a')

        self.ori_q_len = tf.count_nonzero(self.ori_input, 1)
        self.cand_a_len = tf.count_nonzero(self.cand_input, 1)
        self.neg_a_len = tf.count_nonzero(self.neg_input, 1)
        self.test_q_len = tf.count_nonzero(self.test_q, 1)
        self.test_a_len = tf.count_nonzero(self.test_a, 1)

        #embedding layer
        with tf.device('/cpu:0'), tf.name_scope('embedding_layer'):
            W = tf.Variable(tf.to_float(self.embeddings),
                            trainable=Trur,
                            name='W')
            ori_que = tf.nn.embedding_lookup(W, self.ori_input)
            cand_que = tf.nn.embedding_lookup(W, self.cand_input)
            neg_que = tf.nn.embedding_lookup(W, self.neg_input)
            test_que = tf.nn.embedding_lookup(W, self.test_q)
            test_ans = tf.nn.embedding_lookup(W, self.test_a)

        # biLSTM
        with tf.variable_scope("LSTM_scope1", reuse=None):
            ori_q = bilstm(ori_que, self.rnn_size)

        with tf.variable_scope("LSTM_scope1", reuse=True):
            test_q = bilstm(test_que, self.rnn_size)
            test_a = bilstm(test_ans, self.rnn_size)

            if mode == 'qq':
                cand_a = bilstm(cand_que, self.rnn_size)
                neg_a = bilstm(neg_que, self.rnn_size)

        if mode == 'qa':
            with tf.variable_scope("LSTM_scope2", reuse=None):
                cand_a = bilstm(cand_que, self.rnn_size)
            with tf.variable_scope("LSTM_scope2", reuse=True):
                neg_a = bilstm(neg_que, self.rnn_size)

        ori_q = mask(ori_q, self.ori_q_len, self.steps)
        cand_a = mask(cand_a, self.cand_a_len, self.steps)
        neg_a = mask(neg_a, self.neg_a_len, self.steps)
        test_q = mask(test_q, self.test_q_len, self.steps)
        test_a = mask(test_a, self.test_a_len, self.steps)

        for i, filter_size in enumerate(filter_sizes):
            with tf.name_scope("conv-maxpool-%s" % filter_size):
                if self.mode_choice == 3:
                    filter_size = [
                        filter_size, self.embedding_size, 1, self.num_filters
                    ]
                else:
                    filter_size = [
                        filter_size, self.rnn_size * 2, 1, self.num_filters
                    ]
                W = tf.Variable(tf.truncated_normal(filter_size, stddev=0.1),
                                name='Kernel_W')
                b = tf.Variable(tf.constant(0.1, shape=[self.num_filters]),
                                name='Kernel_b')
                self.kernel.append((W, b))
        in_dim = ori_que.get_shape()[2]
        with tf.variable_scope('door'):
            if self.mode_choice == 5 or self.mode_choice == 6:
                door_w = {
                    'd_w': tf.get_variable('d_w', [100, 300]),
                    'd_b': tf.get_variable('d_b', [1, 300]),
                }
            else:
                door_w = {
                    'd_w': tf.get_variable('d_w', [in_dim, in_dim]),
                    'd_b': tf.get_variable('d_b', [in_dim, in_dim]),
                }

        with tf.variable_scope('change'):
            if self.mode_choice == 5 or self.mode_choice == 6:
                change_w = {
                    'c_w': tf.get_variable('c_w', [300, 300]),
                    'c_b': tf.get_variable('c_b', [1, 300]),
                }
            else:
                change_w = {
                    'c_w': tf.get_variable('c_w', [in_dim, in_dim]),
                    'c_b': tf.get_variable('c_b', [1, in_dim]),
                }

        with tf.variable_scope('self_att'):
            self_att = {
                'att_w': tf.get_variable('att_w', [300, 300]),
                'att_b': tf.get_variable('att_b', [1, 300]),
                'att_u': tf.get_variable('att_u', [300, 1])
            }

        if self.mode_choice == 0:  ##biLSTM + mask + highway+cnn_qa
            ori_q_highway, cand_a_highway = get_rnn2cnn_out(
                ori_q, cand_a, door_w, change_w, self.steps)
            _, neg_a_highway = get_rnn2cnn_out(ori_q, neg_a, door_w, change_w,
                                               self.steps)
            test_q_highway, test_a_highway = get_rnn2cnn_out(
                test_q, test_a, door_w, change_w, self.steps)
            print(ori_q_highway.shape)

            ori_q_fea, cand_a_fea, neg_a_fea = cnn_qa(
                ori_q=ori_q_highway,
                can_a=cand_a_highway,
                neg_a=neg_a_highway,
                seq_len=self.steps,
                hidden_size=2 * self.rnn_size,
                filter_sizes=self.filter_sizes,
                num_filters=self.num_filters)
            test_q_fea, test_a_fea, _ = cnn_qa(ori_q=test_q_highway,
                                               cand_a=test_a_highway,
                                               neg_a=neg_a_highway,
                                               seq_len=self.steps,
                                               hidden_size=2 * self.rnn_size,
                                               filter_sizes=self.filter_sizes,
                                               num_filters=self.num_filters)

        if self.mode_choice == 1:  ##biLSTM + mask + max_pooling
            ori_q_fea, cand_a_fea = get_feature_mask(ori_q, cand_a,
                                                     self.ori_q_len,
                                                     self.cand_a_len,
                                                     self.steps)
            _, neg_a_fea = get_rnn2cnn_out(ori_q, neg_a, self.ori_q_len,
                                           self.neg_a_len, self.steps)
            test_q_fea, test_a_fea = get_feature_mask(test_q, test_a,
                                                      self.test_q_len,
                                                      self.test_a_len,
                                                      self.steps)

        if self.mode_choice == 2:  ##biLSTM+ mask + highway + max_pooling
            ori_q_highway, cand_a_highway = get_rnn2cnn_out(
                ori_q, cand_a, door_w, change_w, self.steps)
            _, neg_a_highway = get_rnn2cnn_out(ori_q, neg_a, door_w, change_w,
                                               self.steps)
            test_q_highway, test_a_highway = get_rnn2cnn_out(
                test_q, test_a, door_w, change_w, self.steps)
            print(ori_q_highway.shape)

            ori_q_fea, cand_a_fea = get_feature(ori_q_highway, cand_a_highway)
            ori_nq_fea, neg_a_fea = get_feature(ori_q_highway, neg_a_highway)
            test_q_fea, test_a_fea, _ = get_feature(test_q_highway,
                                                    test_a_highway)

        if self.mode_choice == 3:  ##embedding + CNN
            ori_q_fea, cand_a_fea, neg_a_fea = cnn_qa(
                ori_q=ori_que,
                can_a=cand_que,
                neg_a=neg_que,
                seq_len=self.steps,
                hidden_size=self.embedding_size,
                filter_sizes=self.filter_sizes,
                num_filters=self.num_filters)
            test_q_fea, test_a_fea, _ = cnn_qa(ori_q=test_q,
                                               cand_a=test_a,
                                               neg_a=neg_que,
                                               seq_len=self.steps,
                                               hidden_size=self.embedding_size,
                                               filter_sizes=self.filter_sizes,
                                               num_filters=self.num_filters)
        if self.mode_choice == 4:  ## biLSTM + mask + highway + highway + maxpooling
            ori_q_highway, cand_a_highway = get_rnn2cnn_out(
                ori_q, cand_a, door_w, change_w, self.steps)
            _, neg_a_highway = get_rnn2cnn_out(ori_q, neg_a, door_w, change_w,
                                               self.steps)
            test_q_highway, test_a_highway = get_rnn2cnn_out(
                test_q, test_a, door_w, change_w, self.steps)
            ori_q_2highway, cand_a_2highway = get_rnn2cnn_out(
                ori_q_highway, cand_a_highway, door_w, change_w, self.steps)
            _, neg_a_2highway = get_rnn2cnn_out(ori_q_highway, neg_a_highway,
                                                door_w, change_w, self.steps)
            test_q_2highway, test_a_2highway = get_rnn2cnn_out(
                test_q_highway, test_a_highway, door_w, change_w, self.steps)

            ori_q_fea, cand_a_fea = get_feature(ori_q_2highway,
                                                cand_a_2highway)
            ori_nq_fea, neg_a_fea = get_feature(ori_q_2highway, neg_a_2highway)
            test_q_fea, test_a_fea, _ = get_feature(test_q_2highway,
                                                    test_a_2highway)

        if self.mode_choice == 5:  ## biLSTM + mask + concat + highway + attention
            ori_q_concat = tf.concat([ori_q, ori_que], 2)
            cand_a_concat = tf.concat([cand_a, cand_que], 2)
            neg_a_concat = tf.concat([neg_a, neg_que], 2)
            test_q_concat = tf.concat([test_q, test_que], 2)
            test_a_concat = tf.concat([test_a, test_ans], 2)

            print(ori_q_concat.shape)
            ori_q_highway, cand_a_highway = get_rnn2cnn_out_hxh(
                ori_que, ori_q_concat, cand_que, cand_a_concat, door_w,
                change_w, self.steps)
            print(ori_q_highway)
            _, neg_a_highway = get_rnn2cnn_out_hxh(ori_que, ori_q_concat,
                                                   neg_que, neg_a_concat,
                                                   door_w, change_w,
                                                   self.steps)
            test_q_highway, test_a_highway = get_rnn2cnn_out(
                test_que, test_q_concat, test_ans, test_a_concat, door_w,
                change_w, self.steps)

            if self.att:
                ori_q_fea = get_feature_att(ori_q_highway, self_att,
                                            self.ori_q_len, self.steps)
                cand_a_fea = get_feature_att(cand_a_concat, self_att,
                                             self.cand_a_len, self.steps)
                neg_a_fea = get_feature_att(neg_a_concat, self_att,
                                            self.neg_a_len, self.steps)
                test_q_fea = get_feature_att(test_q_highway, self_att,
                                             self.test_q_len, self.steps)
                test_a_fea = get_feature_att(test_a_concat, self_att,
                                             self.neg_a_len, self.steps)

                self.ori_q_fea = tf.reshape(ori_q_fea, [-1, 300],
                                            name='ori_q_feature')

                print('ori_q_shape is :', ori_q_fea.shape)
            else:
                ori_q_fea, cand_a_fea = get_feature(ori_q_highway,
                                                    cand_a_highway)
                _, neg_a_fea = get_feature(ori_q_highway, neg_a_highway)
                test_q_fea, test_a_fea = get_feature(test_q_highway,
                                                     test_a_highway)
                self.ori_q_fea = tf.reshape(ori_q_fea, [-1, 300],
                                            name='ori_q_feature')

        if self.mode_choice == 6:  ##model 5 - attention + window + maxpooling
            ori_q_concat = tf.concat([ori_q, ori_que], 2)
            cand_a_concat = tf.concat([cand_a, cand_que], 2)
            neg_a_concat = tf.concat([neg_a, neg_que], 2)
            test_q_concat = tf.concat([test_q, test_que], 2)
            test_a_concat = tf.concat([test_a, test_ans], 2)

            print(ori_q_concat.shape)
            ori_q_highway, cand_a_highway = get_rnn2cnn_out_hxh(
                ori_que, ori_q_concat, cand_que, cand_a_concat, door_w,
                change_w, self.steps)
            print(ori_q_highway)
            _, neg_a_highway = get_rnn2cnn_out_hxh(ori_que, ori_q_concat,
                                                   neg_que, neg_a_concat,
                                                   door_w, change_w,
                                                   self.steps)
            test_q_highway, test_a_highway = get_rnn2cnn_out(
                test_que, test_q_concat, test_ans, test_a_concat, door_w,
                change_w, self.steps)

            ori_q_list, cand_a_list, neg_a_list, test_q_list, test_a_list = [], [], [], [], []

            for i in range(self.steps - self.rnn_windows + 1):
                ori_q_slice = tf.slice(ori_q_highway, [0, i, 0],
                                       [-1, self.rnn_windows, 300])
                cand_a_slice = tf.slice(cand_a_highway, [0, i, 0],
                                        [-1, self.rnn_windows, 300])
                neg_a_slice = tf.slice(neg_a_highway, [0, i, 0],
                                       [-1, self.rnn_windows, 300])
                test_q_slice = tf.slice(test_q_highway, [0, i, 0],
                                        [-1, self.rnn_windows, 300])
                test_a_slice = tf.slice(test_a_highway, [0, i, 0],
                                        [-1, self.rnn_windows, 300])

                if i > 0:
                    tf.get_variable_scope().reuse
                with tf.variable_scope("LSTM_scope3", reuse=None):
                    ori_q_slice_fea = bilstm(ori_q_slice, self.rnn_size)
                with tf.variabel_scope("LSTM_scope4", reuse=True):
                    cand_a_slice_fea = bilstm((cand_a_slice, self.rnn_size))
                    neg_a_slice_fea = bilstm(neg_a_slice, self.rnn_size)
                    test_q_slice_fea = bilstm(test_q_slice, self.rnn_size)
                    test_a_slice_fea = bilstm(test_a_slice, self.rnn_size)
                print(ori_q_slice_fea.shape)
                ori_q_f, cand_a_f = get_feature(ori_q_slice_fea,
                                                cand_a_slice_fea)
                _, neg_a_f = get_feature(ori_q_slice_fea, neg_a_slice_fea)
                test_q_f, test_a_f = get_feature(test_q_slice_fea,
                                                 test_a_slice_fea)
                print('model 6 ori_q_fea.shape:', ori_q_f.shape)
                ori_q_list.append(ori_q_f)
                cand_a_list.append(cand_a_f)
                neg_a_list.append(neg_a_f)
                test_q_list.append(test_q_f)
                test_a_list.append(test_a_f)

            ori_q_feat = tf.transpose(ori_q_list, perm=[1, 0, 2])
            cand_a_feat = tf.transpose(cand_a_list, perm=[1, 0, 2])
            neg_a_feat = tf.transpose(neg_a_list, perm=[1, 0, 2])
            test_q_feat = tf.transpose(test_q_list, perm=[1, 0, 2])
            test_a_feat = tf.transpose(test_a_list, perm=[1, 0, 2])
            ori_q_fea, cand_a_fea = get_feature(ori_q_feat, cand_a_feat)
            _, neg_a_fea = get_feature(ori_q_feat, neg_a_feat)
            test_q_fea, test_a_fea = get_feature(test_q_feat, test_a_feat)

        self.ori_q_fea = ori_q_fea
        self.ori_cand = feature2cos(ori_q_fea, cand_a_feat)
        self.ori_neg = feature2cos(ori_q_fea, neg_a_fea)
        self.loss, self.acc = cal_loss_and_acc(self.ori_cand, self.ori_neg)

        self.test_q_a = feature2cos(test_q_fea, test_a_fea)
예제 #19
0
    def __init__(self,
                 img,
                 start_pos,
                 target_size,
                 padding=2.5,
                 lamb=0.0001,
                 output_sigma_factor=0.1,
                 interp_factor=0.075,
                 cell_size=1,
                 feature='gray',
                 resize=False,
                 kernel={
                     'kernel_type': 'gaussian',
                     'sigma': 0.2
                 },
                 showvideo=True):
        self.original_img = img
        self.img = img
        self.padding = padding
        self.lamb = lamb
        self.output_sigma_factor = output_sigma_factor
        self.interp_factor = interp_factor
        self.cell_size = cell_size
        self.feature = feature
        self.showvideo = showvideo
        self.original_pos = start_pos
        self.original_target_size = target_size
        self.pos = start_pos  # the box's CENTER point coordinate,it is format as [y, x]
        self.target_size = target_size  # the box's size , it is format as [h, w]
        self.base_size = target_size

        self.kernel_type = kernel['kernel_type']
        if self.kernel_type == 'gaussian':
            self.kernel_sigma = kernel['sigma']
        elif self.kernel_type == 'polynomial':
            self.kernel_poly_a = kernel['poly_a']
            self.kernel_poly_b = kernel['poly_b']

        self.resize = resize
        if np.sqrt(np.prod(self.target_size)) >= 150:
            print("resize image")
            self.resize = True

        if self.resize:
            print("image is resized")
            self.pos = tuple([int(ele / 2) for ele in self.pos])
            self.target_size = tuple(
                [int(ele / 2) for ele in self.target_size])
            self.img_size = (int(img.shape[0] / 2), int(img.shape[1] / 2))
            img = cv2.resize(img, self.img_size[::-1])

        # in opencv the img's size get from shape
        # the shape is format as (h,w,c), c is the chanel of image
        self.img_size = (img.shape[0], img.shape[1])
        self.window_size = (int(self.target_size[0] * self.padding),
                            int(self.target_size[1] * self.padding))

        if self.feature == 'gray':
            img = cv2.cvtColor(
                img, cv2.COLOR_BGR2GRAY)  # translate image from rgb to gray

        # 一些不会变的变量,如余弦窗,期望分布
        output_sigma = np.sqrt(np.prod(
            self.target_size)) * self.output_sigma_factor / self.cell_size
        self.y = gaussian_shaped_labels(
            output_sigma, (int(self.window_size[0] / self.cell_size),
                           int(self.window_size[1] / self.cell_size)))
        self.yf = fft2(self.y)
        self.cos_window = np.outer(np.hanning(self.yf.shape[0]),
                                   np.hanning(self.yf.shape[1]))

        # 初始化模型
        patch = get_subwindow(img, self.pos, self.window_size)
        xf = fft2(
            get_feature(patch, self.feature, self.cell_size, self.cos_window))
        self.model_xf = xf
        self.model_alphaf = self.__train(xf)
        self.__show_image()
예제 #20
0
from ruamel import yaml
from sklearn.model_selection import train_test_split
import lightgbm as lgb

# Read data
df_train = pd.read_csv('./data/train.csv')

# setup
words = get_words(df_train)
counts = Counter(words)
weights = {word: get_weight(count) for word, count in counts.items()}
df_train['word_shares'] = df_train.apply(lambda x: word_shares(x, weights),
                                         axis=1)

# get feature
x = get_feature(df_train)
feature_names = list(x.columns.values)
print("Features:", feature_names)
y = df_train['label'].values

# Load params
params = yaml.safe_load(open('./src/Essemble/params_lightgbm.yaml'))
MODEL_PATH = './models/lightgbm.bin'
ROUNDS = 200
RS = 123457


def train(X, y, params):
    print('Train for {} rounds, Randomseed: {}'.format(ROUNDS, RS))
    x_train, x_val, y_train, y_val = train_test_split(X,
                                                      y,
def verification():
    saved_model = './ArcFace/model/068.pth'
    name_list = os.listdir('./users')
    path_list = [os.path.join('./users', i, '%s.txt' % (i)) for i in name_list]
    total_features = np.empty((128, ), np.float32)
    people_num = len(path_list)

    font = ImageFont.truetype('simhei.ttf', 20, encoding='utf-8')

    if people_num > 1:
        are = 'are'
        people = 'people'
    else:
        are = 'is'
        people = 'person'
    print('start retore users information, there %s %d %s information' %
          (are, people_num, people))
    for i in path_list:
        temp = np.loadtxt(i)
        total_features = np.vstack((total_features, temp))
    total_features = total_features[1:]

    # threshold = 0.30896     #阈值并不合适,可能是因为训练集和测试集的差异所致!!!
    threshold = 0.5
    model = mobileFaceNet()
    model.load_state_dict(t.load(saved_model)['backbone_net_list'])
    model.eval()
    use_cuda = t.cuda.is_available() and True
    device = t.device("cuda" if use_cuda else "cpu")

    # is_cuda_avilable
    trans = transforms.Compose([
        transforms.Resize((112, 112)),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    ])
    model.to(device)

    cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        print('failed open camara!!!')
    ret, frame = cap.read()

    while ret:
        frame = frame[:, :, ::-1]
        img = Image.fromarray(frame)
        bboxes, landmark = detect_faces(img)
        # print(bbox)  # [[296.89171371 211.27569699 441.8924298  396.48678774   0.99999869]]

        if len(bboxes) == 0:
            cv2.imshow('img', frame[:, :, ::-1])
            # videoWriter.write(frame[:,:,::-1])
            cv2.waitKey(10)
            ret, frame = cap.read()
            continue

        show_img = frame.copy()
        for bbox in bboxes:
            loc_x_y = [bbox[2], bbox[1]]
            person_img = frame[int(bbox[1]):int(bbox[3]),
                               int(bbox[0]):int(bbox[2])].copy()
            feature = np.squeeze(get_feature(person_img, model, trans, device))
            cos_distance = cosin_metric(total_features, feature)
            index = np.argmax(cos_distance)
            if not cos_distance[index] > threshold:
                ret, frame = cap.read()
                continue
            person = name_list[index]
            show_img = draw_ch_zn(show_img, person, font, loc_x_y)
            cv2.rectangle(show_img, (int(bbox[0]), int(bbox[1])),
                          (int(bbox[2]), int(bbox[3])), (0, 0, 255))

        cv2.imshow('img', show_img[:, :, ::-1])

        if cv2.waitKey(10) & 0xFF == ord('q'):
            # videoWriter.release()
            break

        ret, frame = cap.read()
예제 #22
0
    def __init__(self, batch_size, num_unroll_steps, embeddings, embedding_size, rnn_size, num_rnn_layers, max_grad_norm,attention_matrix_size,loss_ratio, l2_reg_lambda=0.0, adjust_weight=False,label_weight=[],is_training=True,m=0.1):
        # define input variable
        self.batch_size = batch_size
        self.embeddings = embeddings
        self.embedding_size = embedding_size
        self.adjust_weight = adjust_weight
        self.label_weight = label_weight
        self.rnn_size = rnn_size
        self.num_rnn_layers = num_rnn_layers
        self.num_unroll_steps = num_unroll_steps
        self.max_grad_norm = max_grad_norm
        self.l2_reg_lambda = l2_reg_lambda
        self.is_training = is_training

        self.keep_prob = tf.placeholder(tf.float32, name="keep_drop")
        
        self.lr = tf.Variable(0.0,trainable=False)
        self.new_lr = tf.placeholder(tf.float32, shape=[],name="new_learning_rate")
        self._lr_update = tf.assign(self.lr, self.new_lr)

        self.ori_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps])
        self.cand_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps])
        self.neg_input_quests = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps])


        self.test_input_q = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps], name='test_q')
        self.test_input_a = tf.placeholder(tf.int32, shape=[None, self.num_unroll_steps], name='test_a')
        self.q_cats = tf.placeholder(tf.int32, [None, CAT_NUMBER], name='q_cats')
        self.a_cats = tf.placeholder(tf.int32, [None, CAT_NUMBER], name='a_cats')

        #embedding layer
        with tf.device("/cpu:0"),tf.name_scope("embedding_layer"):
            W = tf.Variable(tf.to_float(self.embeddings), trainable=True, name="W")
            ori_quests =tf.nn.embedding_lookup(W, self.ori_input_quests)
            cand_quests =tf.nn.embedding_lookup(W, self.cand_input_quests)
            neg_quests =tf.nn.embedding_lookup(W, self.neg_input_quests)

            test_q =tf.nn.embedding_lookup(W, self.test_input_q)
            test_a =tf.nn.embedding_lookup(W, self.test_input_a)

        # run lstm without attention
        # with tf.variable_scope("LSTM_scope") as scope:
        #     ori_q = biLSTM(ori_quests, self.rnn_size)
        #     ori_q_feat = tf.nn.tanh(max_pooling(ori_q))
        #
        #     scope.reuse_variables()
        #
        #     cand_a = biLSTM(cand_quests, self.rnn_size)
        #     neg_a = biLSTM(neg_quests, self.rnn_size)
        #     cand_q_feat = tf.nn.tanh(max_pooling(cand_a))
        #     neg_q_feat = tf.nn.tanh(max_pooling(neg_a))
        #
        #     test_q_out = biLSTM(test_q, self.rnn_size)
        #     test_q_out = tf.nn.tanh(max_pooling(test_q_out))
        #     test_a_out = biLSTM(test_a, self.rnn_size)
        #     test_a_out = tf.nn.tanh(max_pooling(test_a_out))

        #build LSTM network
        with tf.variable_scope("LSTM_scope") as scope:
            ori_q = biLSTM(ori_quests, self.rnn_size)
            #ori_q_feat = tf.nn.tanh(max_pooling(ori_q))

            scope.reuse_variables()

            cand_a = biLSTM(cand_quests, self.rnn_size)
            neg_a = biLSTM(neg_quests, self.rnn_size)
            #cand_q_feat = tf.nn.tanh(max_pooling(cand_a))
            #neg_q_feat = tf.nn.tanh(max_pooling(neg_a))

            test_q_out = biLSTM(test_q, self.rnn_size)
            #test_q_out = tf.nn.tanh(max_pooling(test_q_out))
            test_a_out = biLSTM(test_a, self.rnn_size)
            #test_a_out = tf.nn.tanh(max_pooling(test_a_out))

        with tf.name_scope("att_weight"):
            # attention params
            att_W = {
            	'Wam': tf.Variable(tf.truncated_normal([2 * self.rnn_size, attention_matrix_size], stddev=0.1)),
            	'Wqm': tf.Variable(tf.truncated_normal([2 * self.rnn_size, attention_matrix_size], stddev=0.1)),
            	'Wms': tf.Variable(tf.truncated_normal([attention_matrix_size, 1], stddev=0.1))
            }
            ori_nq_feat, cand_q_feat = get_feature(ori_q, cand_a, att_W)
            ori_q_feat, neg_q_feat = get_feature(ori_q, neg_a, att_W)
            test_q_out, test_a_out = get_feature(test_q_out, test_a_out, att_W)

        # multitasking
        with tf.variable_scope("multitasking") as scope:

            feature_size = int(ori_q_feat.get_shape()[1])

            logits_q = fc(ori_q_feat,feature_size)
            # scope.reuse_variables()
            logits_a = fc(cand_q_feat, feature_size)
            logits_ng_a = fc(neg_q_feat, feature_size)

            #feature_size = int(ori_q_feat.get_shape()[1])

            #w = tf.get_variable(name='weights', shape=(feature_size, CAT_NUMBER, initializer=tf.random_normal_initializer())
            #b = tf.get_variable(name='bias', shape=(1, CAT_NUMBER), initializer=tf.zeros_initializer())

            # positive_qa = tf.concat([out_ori,out_cand],1,name="embedding_for_multitask")

            #logits = tf.matmul(ori_q_feat, w) + b

            entropy_q = tf.nn.softmax_cross_entropy_with_logits(logits=logits_q, labels=self.q_cats, name='loss1')
            entropy_a= tf.nn.softmax_cross_entropy_with_logits(logits=logits_a, labels=self.q_cats, name='loss2')
            entropy_ng_a = tf.nn.softmax_cross_entropy_with_logits(logits=logits_ng_a, labels=self.a_cats, name='loss3')
            loss_multitask_q = tf.reduce_mean(entropy_q)
            loss_multitask_a = tf.reduce_mean(entropy_a)
            loss_multitask_ng_a = tf.reduce_mean(entropy_ng_a)

            loss_multitask = loss_multitask_q+loss_multitask_a+loss_multitask_ng_a

            loss_multitask = loss_multitask_q
        # acc
        self.ori_cand_score = feature2cos_sim(ori_q_feat, cand_q_feat)
        self.ori_neg_score = feature2cos_sim(ori_q_feat, neg_q_feat)
        loss_origin, self.acc = cal_loss_and_acc(self.ori_cand_score, self.ori_neg_score,m)

        self.loss = loss_origin * (1 - loss_ratio) + loss_multitask * loss_ratio

        self.test_q_a = feature2cos_sim(test_q_out, test_a_out)

        #evaluate multitasking_acc
        with tf.name_scope("multi_acc"):
            self.preds_q = tf.nn.softmax(logits_q)
            self.correct_preds_q = tf.equal(tf.argmax(self.preds_q, 1), tf.argmax(self.q_cats, 1))
            self.multi_acc_q = tf.reduce_sum(tf.cast(self.correct_preds_q, tf.float32))

            self.preds_a = tf.nn.softmax(logits_a)
            self.correct_preds_a = tf.equal(tf.argmax(self.preds_a, 1), tf.argmax(self.q_cats, 1))
            self.multi_acc_a = tf.reduce_sum(tf.cast(self.correct_preds_a, tf.float32))

            self.preds_ng_a = tf.nn.softmax(logits_ng_a)
            self.correct_preds_ng_a = tf.equal(tf.argmax(self.preds_ng_a, 1), tf.argmax(self.a_cats, 1))
            self.multi_acc_ng_a = tf.reduce_sum(tf.cast(self.correct_preds_ng_a, tf.float32))

            self.multi_acc = (self.multi_acc_q + self.multi_acc_a + self.multi_acc_ng_a)/3

            self.multi_acc = self.multi_acc_q 
예제 #23
0
    # for p1, p2 in itertools.chain(cars):
    #     # Draw SVC boxes
    #     cv2.rectangle(svc_img, p1, p2, (255, 255, 0), 3)

    svc_img = cv2.addWeighted(svc_img, 1.0, heatmap, 0.8, 0.0)
    return svc_img


if __name__ == "__main__":
    # Import car and not car images
    cars = get_file_names('./data/vehicles', pattern='*.png')
    not_cars = get_file_names('./data/non-vehicles', pattern='*.png')

    # Calculate car features & not-car features
    car_features = get_feature(cars, workers=4)
    not_car_features = get_feature(not_cars, workers=4)

    # Create data set
    x = np.vstack((car_features, not_car_features)).astype(np.float64)
    y = np.concatenate(
        (np.ones(len(car_features)), np.zeros(len(not_car_features))))

    # SVC classifier
    clf = SupportVectorMachineClassifier()
    clf.train(x, y)

    # Vehicle Tracker
    car_tracker = VehicleTracker(looking_back_frames=10)

    output = 'output.mp4'
예제 #24
0
if __name__ == "__main__":
    warnings.filterwarnings("ignore")
    file_path = "~/Documents/pyTest/HMMELM/ok_data/S01R01.txt"
    raw_data = utils.read_data_from(file_path=file_path)
    print("Have read data:", raw_data.shape)
    # segmentation
    fs = 64
    window_time = 2
    overlap_time = 0
    frames = utils.sliding_window(raw_data,
                                  window=fs * window_time,
                                  overlap=fs * overlap_time)
    # extract features
    features = []
    for frame in frames:
        feature = utils.get_feature(frame=frame, col_range=[1, 2, 3])
        features.append(feature)
    features = np.array(features)
    pre_length = 4
    split_data = utils.abandon_pre_fog(features[:, -1], pre_length)

    # train GMM-HMM
    frame_num = 4
    fog_indices = split_data['fog_indices']
    pre_fog_indices = split_data['pre_fog_indices']
    normal_indices = split_data['normal_indices']
    normal_down_indices = utils.down_sampling(normal_indices,
                                              2 * len(fog_indices))
    fog_train, fog_test = train_test_split(fog_indices,
                                           test_size=0.1,
                                           random_state=0)
예제 #25
0
    def __init__(self,
                 batch_size,
                 num_unroll_steps,
                 embeddings,
                 embedding_size,
                 rnn_size,
                 num_rnn_layers,
                 max_grad_norm,
                 attention_matrix_size,
                 l2_reg_lambda=0.0,
                 adjust_weight=False,
                 label_weight=[],
                 is_training=True):
        """
        LSTM-BASED DEEP LEARNING MODELS FOR NON-FACTOID ANSWER SELECTION
        """
        # define input variable
        self.batch_size = batch_size
        self.embeddings = embeddings
        self.embedding_size = embedding_size
        self.adjust_weight = adjust_weight
        self.label_weight = label_weight
        self.rnn_size = rnn_size
        self.num_rnn_layers = num_rnn_layers
        self.num_unroll_steps = num_unroll_steps
        self.max_grad_norm = max_grad_norm
        self.l2_reg_lambda = l2_reg_lambda
        self.is_training = is_training

        self.keep_prob = tf.placeholder(tf.float32, name="keep_drop")

        self.lr = tf.Variable(0.0, trainable=False)
        self.new_lr = tf.placeholder(tf.float32,
                                     shape=[],
                                     name="new_learning_rate")
        self._lr_update = tf.assign(self.lr, self.new_lr)

        self.ori_input_quests = tf.placeholder(
            tf.int32, shape=[None, self.num_unroll_steps])
        self.cand_input_quests = tf.placeholder(
            tf.int32, shape=[None, self.num_unroll_steps])
        self.neg_input_quests = tf.placeholder(
            tf.int32, shape=[None, self.num_unroll_steps])
        self.test_input_q = tf.placeholder(tf.int32,
                                           shape=[None, self.num_unroll_steps])
        self.test_input_a = tf.placeholder(tf.int32,
                                           shape=[None, self.num_unroll_steps])

        #embedding layer
        with tf.device("/cpu:0"), tf.name_scope("embedding_layer"):
            W = tf.Variable(tf.to_float(self.embeddings),
                            trainable=True,
                            name="W")
            ori_quests = tf.nn.embedding_lookup(W, self.ori_input_quests)
            cand_quests = tf.nn.embedding_lookup(W, self.cand_input_quests)
            neg_quests = tf.nn.embedding_lookup(W, self.neg_input_quests)
            test_q = tf.nn.embedding_lookup(W, self.test_input_q)
            test_a = tf.nn.embedding_lookup(W, self.test_input_a)

        #build LSTM network
        with tf.variable_scope("LSTM_scope", reuse=None):
            ori_q = biLSTM(ori_quests, self.rnn_size)
            #ori_q_feat = tf.nn.tanh(max_pooling(ori_q))
        with tf.variable_scope("LSTM_scope", reuse=True):
            cand_a = biLSTM(cand_quests, self.rnn_size)
            neg_a = biLSTM(neg_quests, self.rnn_size)
            #cand_q_feat = tf.nn.tanh(max_pooling(cand_a))
            #neg_q_feat = tf.nn.tanh(max_pooling(neg_a))
            test_q_out = biLSTM(test_q, self.rnn_size)
            #test_q_out = tf.nn.tanh(max_pooling(test_q_out))
            test_a_out = biLSTM(test_a, self.rnn_size)
            #test_a_out = tf.nn.tanh(max_pooling(test_a_out))

        with tf.name_scope("att_weight"):
            # attention params
            att_W = {
                'Wam':
                tf.Variable(
                    tf.truncated_normal(
                        [2 * self.rnn_size, attention_matrix_size],
                        stddev=0.1)),
                'Wqm':
                tf.Variable(
                    tf.truncated_normal(
                        [2 * self.rnn_size, attention_matrix_size],
                        stddev=0.1)),
                'Wms':
                tf.Variable(
                    tf.truncated_normal([attention_matrix_size, 1],
                                        stddev=0.1))
            }
            ori_q_feat, cand_q_feat = get_feature(ori_q, cand_a, att_W)
            ori_nq_feat, neg_q_feat = get_feature(ori_q, neg_a, att_W)
            test_q_out, test_a_out = get_feature(test_q_out, test_a_out, att_W)

        self.ori_cand = feature2cos_sim(ori_q_feat, cand_q_feat)
        self.ori_neg = feature2cos_sim(ori_q_feat, neg_q_feat)
        self.loss, self.acc = cal_loss_and_acc(self.ori_cand, self.ori_neg)

        self.test_q_a = feature2cos_sim(test_q_out, test_a_out)