Ejemplo n.º 1
0
 def __init__(self, model_path,epoch_num=0,max_dist=0.2,use_cuda=False,mot_type='face'):
     self.min_confidence = 0.3
     self.nms_max_overlap = 1.0
     self.img_size = [112,112]
     self.mot_type = mot_type
     if mot_type == 'person':
         self.extractor = Extractor(model_path, use_cuda=use_cuda)
     else:
         self.extractor = mx_FaceRecognize(model_path,epoch_num,self.img_size)
     max_cosine_distance = cfgs.max_cosine_distance #max_dist
     nn_budget = 100
     metric = NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
     self.tracker = Tracker(metric)
     self.feature_tmp = []
Ejemplo n.º 2
0
def main(mode, splitted):
    # READ THE DATA
    if mode == 'one':
        reader = DbReader(ONE_FOLDER_PATH,
                          mode=mode,
                          train_size=ONE_STUDENT_TRAIN_SIZE)
    elif mode == 'all':
        reader = DbReader(ALL_FOLDER_PATH,
                          mode=mode,
                          train_size=ALL_STUDENTS_TRAIN_SIZE)
    else:
        raise Exception(f"Bad argument mode: {mode}")

    valid = reader.get_valid()

    extractor = Extractor()
    valid_feats_list, valid_labels = [], []

    # LOAD THE MODEL
    model = Model(splitted=splitted)
    model.load(mode)

    # PREPERE THE FEATURES
    for valid_elem in valid:
        label, array, rate = valid_elem

        features = extractor.extract_features(signal=array, rate=rate)
        valid_feats_list.append(features)
        valid_labels += [label]

    model.score(valid_feats_list, valid_labels)

    predict = []
    classes = [
        'OTWORZ', 'ZAMKNIJ', 'GARAZ', 'ZROB', 'NASTROJ', 'WLACZ', 'WYLACZ',
        'MUZYKE', 'SWIATLO', 'ZAPAL', 'PODNIES', 'ROLETY', 'TELEWIZOR'
    ]

    for elem in valid_feats_list:
        predict.append(model.predict(X=elem))

    plot_confusion_matrix(y_true=valid_labels,
                          y_pred=predict,
                          classes=classes,
                          normalize=True,
                          title='Normalized confusion matrix for commands',
                          only_occuring_labels=True)
    plt.show()
Ejemplo n.º 3
0
def main(mode, splitted):
    # READ THE DATA
    if mode == 'one':
        reader = DbReader(ONE_FOLDER_PATH, mode=mode, train_size=ONE_STUDENT_TRAIN_SIZE)
    elif mode == 'all':
        reader = DbReader(ALL_FOLDER_PATH, mode=mode, train_size=ALL_STUDENTS_TRAIN_SIZE)
    else:
        raise Exception(f"Bad argument: mode = {mode}")

    train = reader.get_train()

    extractor = Extractor()
    train_feats_list, train_labels = [], []

    # FORMAT THE FILES
    for train_elem in train:
        label, array, rate = train_elem
        features = extractor.extract_features(signal=array, rate=rate)
        features_size = features.shape[0]

        train_feats_list.append(features)

        if splitted:
            top_count = round(TOP * features_size)
            mid_count = round(MID * features_size)
            bot_count = features_size - top_count - mid_count

            train_labels += top_count * [label + '_TOP']
            train_labels += mid_count * [label + '_MID']
            train_labels += bot_count * [label + '_BOT']
        else:
            train_labels += features_size * [label]

    train_feats = train_feats_list[0]
    for array in train_feats_list[1:]:
        train_feats = np.append(train_feats, array, axis=0)

    # TRAIN THE MODEL
    model = Model(n_estimators=500, splitted=splitted)

    t_start = time()
    model.fit(train_feats, train_labels)
    t_end = time()

    print(f"Fitting took {t_end - t_start}s.")

    # SAVE THE MODEL
    model.save(mode=mode)
Ejemplo n.º 4
0
    def process_file(self, wav_file_path, attack=False, peak=False):
        wav_file, json_file = Importer.load(wav_file_path)
        if not wav_file:
            print("Error for path " + wav_file_path)
            return
        if not json_file and not attack:
            print("Error for path " + wav_file_path)
            return

        # if attack and not json_file:
        if attack and peak:
            X = Dispatcher.peak_extract(wav_file, json_file)
            if json_file:
                y = [json_file[i] for i in sorted(json_file.keys())]
                y.pop(0)
            else:
                y = None
        else:
            X, y = Dispatcher.timestamped_no_peak_check(wav_file, json_file)

        if X is None:
            print("X not present, exiting...")
            return
        X = Extractor.transform_mfcc(X, self.winlen, self.winstep, self.numcep, self.nfilt, self.nfft)
        self.total_X.extend(X)

        if y:
            self.total_y.extend(y)
Ejemplo n.º 5
0
 def __init__(self, model_path,epoch_num=0,max_dist=0.2,use_cuda=False,mot_type='face'):
     self.min_confidence = 0.3
     self.nms_max_overlap = 1.0
     self.img_size = [112,112]
     self.mot_type = mot_type
     self.line_inout = cfgs.inout_point
     if mot_type == 'face':
         self.extractor = mx_FaceRecognize(model_path,epoch_num,self.img_size)
     else:
         self.extractor = Extractor(model_path, use_cuda=use_cuda)
     max_cosine_distance = cfgs.max_cosine_distance #max_dist
     nn_budget = 100
     metric = NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
     self.tracker = Tracker(metric)
     self.feature_tmp = []
     self.history_inout = dict()
     # if xy is 0 ,it will calculate inout for the door is right or left with x-axis
     # if xy is 1, it will calculate inout for the door is up or down with y-axis
     self.xy = 1
     self.inout_type = cfgs.inout_type
Ejemplo n.º 6
0
class DeepSort(object):
    def __init__(self, model_path,epoch_num=0,max_dist=0.2,use_cuda=False,mot_type='face'):
        self.min_confidence = 0.3
        self.nms_max_overlap = 1.0
        self.img_size = [112,112]
        self.mot_type = mot_type
        self.line_inout = cfgs.inout_point
        if mot_type == 'face':
            self.extractor = mx_FaceRecognize(model_path,epoch_num,self.img_size)
        else:
            self.extractor = Extractor(model_path, use_cuda=use_cuda)
        max_cosine_distance = cfgs.max_cosine_distance #max_dist
        nn_budget = 100
        metric = NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
        self.tracker = Tracker(metric)
        self.feature_tmp = []
        self.history_inout = dict()
        # if xy is 0 ,it will calculate inout for the door is right or left with x-axis
        # if xy is 1, it will calculate inout for the door is up or down with y-axis
        self.xy = 1
        self.inout_type = cfgs.inout_type

    def update(self, bbox_xcycwh, confidences, ori_img,update_fg=True):
        self.height, self.width = ori_img.shape[:2]
        # generate detections
        if update_fg or not(len(bbox_xcycwh)==len(self.feature_tmp)):
            features = self._get_features(bbox_xcycwh, ori_img)
            self.feature_tmp = features
        else:
            features = self.feature_tmp
        detections = [Detection(bbox_xcycwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]

        # run on non-maximum supression
        # boxes = np.array([d.tlwh for d in detections])
        # scores = np.array([d.confidence for d in detections])
        # indices = non_max_suppression( boxes, self.nms_max_overlap, scores)
        # detections = [detections[i] for i in indices]

        # update tracker
        self.tracker.predict()
        self.tracker.update(detections)
        # output bbox identities
        outputs = []
        for track in self.tracker.tracks:
            #print('time_s',track.time_since_update)
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            box = track.to_tlwh()
            x1,y1,x2,y2 = self._tlwh_to_xyxy(box)
            track_id = track.track_id
            fg = self.check_in_out(track)
            outputs.append(np.array([x1,y1,x2,y2,track_id,fg], dtype=np.int))
        if len(outputs) > 0:
            outputs = np.stack(outputs,axis=0)
        return outputs
    def check_in_out(self,track):
        '''
        according to the trajectories of a track, judge the moving direction and if move out or in
        1: in
        0: out
        2: keep state
        -1: init state
        '''
        tmp_centers = track.trajects
        keyname = track.track_id
        fg = self.history_inout.setdefault(keyname,-1)
        trj_num = len(tmp_centers)
        if trj_num >2:
            p_start = tmp_centers[0]
            p_end = tmp_centers[trj_num-1]
            diff = p_end[self.xy] - p_start[self.xy]
            if diff > 0 and p_start[self.xy] < self.line_inout[self.xy] and p_end[self.xy] > self.line_inout[self.xy]:
                if self.inout_type==0:
                    if fg == -1:
                        fg = 0
                    elif fg == 0:
                        fg = 2
                else:
                    if fg == -1:
                        fg = 1
                    elif fg == 1:
                        fg = 2
            elif diff < 0 and p_start[self.xy] > self.line_inout[self.xy] and p_end[self.xy] < self.line_inout[self.xy]:
                if self.inout_type==0:
                    if fg == -1 :
                        fg = 1
                    elif fg == 1:
                        fg = 2
                else:
                    if fg == -1:
                        fg = 0
                    elif fg == 0:
                        fg = 2
        self.history_inout[keyname] = fg 
        return fg


    """
    TODO:
        Convert bbox from xc_yc_w_h to xtl_ytl_w_h
    Thanks [email protected] for reporting this bug!
    """
    @staticmethod
    def _xywh_to_tlwh(bbox_xywh):
        bbox_xywh[:,0] = bbox_xywh[:,0] - bbox_xywh[:,2]/2.
        bbox_xywh[:,1] = bbox_xywh[:,1] - bbox_xywh[:,3]/2.
        return bbox_xywh


    def _xcycwh_to_xyxy(self, bbox_xywh):
        x,y,w,h = bbox_xywh
        x1 = max(int(x-w/2),0)
        x2 = min(int(x+w/2),self.width-1)
        y1 = max(int(y-h/2),0)
        y2 = min(int(y+h/2),self.height-1)
        return x1,y1,x2,y2

    def _tlwh_to_xyxy(self, bbox_tlwh):
        """
        TODO:
            Convert bbox from xtl_ytl_w_h to xc_yc_w_h
        Thanks [email protected] for reporting this bug!
        """
        x,y,w,h = bbox_tlwh
        x1 = max(int(x),0)
        x2 = min(int(x+w),self.width-1)
        y1 = max(int(y),0)
        y2 = min(int(y+h),self.height-1)
        return x1,y1,x2,y2
    
    def _get_features(self, bbox_xcycwh, ori_img):
        im_crops = []
        for box in bbox_xcycwh:
            _,_,w,h = box
            x1,y1,x2,y2 = self._xcycwh_to_xyxy(box)
            im = ori_img[y1:y2,x1:x2]
            #print('img',ori_img.shape)
            if self.mot_type=='face':
                #if w != self.img_size[1] or h != self.img_size[0]:
                im = cv2.resize(im,(self.img_size[1],self.img_size[0]))
            im_crops.append(im)
        if len(im_crops)>0:
            features = self.extractor.extractfeature(np.array(im_crops)) if self.mot_type=='face' else self.extractor(im_crops)
        else:
            features = np.array([])
        return features
for query in queryFile:
    queries.append(query[1])
docsIDs = []
for i, qr in enumerate(qrel):
    docsIDs.append(qr[2])
    # if documents.get(qr[2], None) == None:
    #     result = es.get(id=qr[2], doc_type="doc")
    #     documents[qr[2]] = result["_source"]["text"]
    # qrel[i][2] = documents[qr[2]]
    # qrel[i][0] = queryDict[qr[0]]
docsIDs = np.unique(docsIDs)
modC = 0
cqid = {}
docs = es.mtermvectors(ids=docsIDs, doc_type='doc', term_statistics=False, field_statistics=False)
for doc in docs:
    for term, data in doc.items():
        modC += data['term_freq']
        cqid[term] = cqid.get(term, 0) + data['term_freq']

features = [
    # CosineSimilarity(vectorizer, es, docsIDs, queries),
    # LM(vectorizer, es, docsIDs, queries, modC, cqid),
    # JMercer(vectorizer, es, docsIDs, queries, modC, cqid),
    # BM25(vectorizer, es, docsIDs, queries, query_rel_docs),
    # TfidfFeatures(vectorizer, es, docsIDs, queries)
    ESBuiltIn(vectorizer, es, docsIDs, queries)
]

extractor = Extractor(features)

extractor.transform(query_doc_map, open("/home/pavan/Projects/MS Projects/ML/trec8/esbuiltin.csv", "w"))
Ejemplo n.º 8
0

def process_video(video_input, video_output):
    print("* Processing video %s to %s" % (video_input, video_output))
    input_video_clip = VideoFileClip(video_input)
    clip = input_video_clip.fl_image(process_image)
    clip.write_videofile(video_output, audio=False)
    print("* Done")


if __name__ == "__main__":

    extractor = Extractor(color_space='YCrCb',
                          spatial_size=(16, 16),
                          hist_bins=16,
                          hog_orient=9,
                          hog_channel='ALL',
                          hog_pix_per_cell=8,
                          hog_cell_per_block=2)

    parser = argparse.ArgumentParser()
    parser.add_argument("-v",
                        action="store_true",
                        dest="process_video",
                        default=False)
    parser.add_argument("--input",
                        action="store",
                        dest="src_video",
                        default="project_video.mp4")
    args = parser.parse_args()
Ejemplo n.º 9
0
def main(mode, splitted):
    t_start = time()

    # READ THE DATA
    if mode == 'one':
        reader = DbReader(ONE_FOLDER_PATH, mode=mode, train_size=ONE_STUDENT_TRAIN_SIZE)
    elif mode == 'all':
        reader = DbReader(ALL_FOLDER_PATH, mode=mode, train_size=ALL_STUDENTS_TRAIN_SIZE)
    else:
        raise Exception(f"Bad argument mode: {mode}")

    winlens = np.arange(0.02, 0.03, 0.001)
    winsteps = np.arange(0.01, 0.02, 0.001)
    nffts = list(range(1700, 2050, 50))
    ns = list(range(1, 15, 1))

    train = reader.get_train()
    valid = reader.get_valid()
    extractor = Extractor()

    # PREPERE THE FEATURES
    best_score = 0
    best_params = {}
    for winlen in winlens:
        for winstep in winsteps:
            for nfft in nffts:
                for n in ns:
                    train_feats_list, train_labels = [], []
                    valid_feats_list, valid_labels = [], []

                    for train_elem in train:
                        label, array, rate = train_elem
                        features = extractor.extract_features(signal=array, rate=rate)
                        features_size = features.shape[0]

                        train_feats_list.append(features)

                        if splitted:
                            top_count = round(TOP * features_size)
                            mid_count = round(MID * features_size)
                            bot_count = features_size - top_count - mid_count

                            train_labels += top_count * [label + '_TOP']
                            train_labels += mid_count * [label + '_MID']
                            train_labels += bot_count * [label + '_BOT']
                        else:
                            train_labels += features_size * [label]

                    train_feats = train_feats_list[0]
                    for array in train_feats_list[1:]:
                        train_feats = np.append(train_feats, array, axis=0)

                    for valid_elem in valid:
                        label, array, rate = valid_elem

                        features = extractor.extract_features(signal=array, rate=rate, winlen=winlen, winstep=winstep,
                                                              nfft=nfft, n=n)
                        valid_feats_list.append(features)
                        valid_labels += [label]

                    # TRAIN THE MODEL
                    model = Model(n_estimators=500, splitted=splitted)
                    model.fit(train_feats, train_labels)

                    # EVAL
                    print(f"\nwinlen = {winlen}, winstep = {winstep}, nfft = {nfft}, n = {n}")
                    score = model.score(valid_feats_list, valid_labels)

                    if score > best_score:
                        best_score = score
                        best_params['winlen'] = winlen
                        best_params['winstep'] = winstep
                        best_params['nfft'] = nfft
                        best_params['n'] = n

                print()
            print()
        print()
    t_end = time()

    print(f"Best score: {best_score * 100}%")
    print(f"Best params: {dumps(best_params)}")
    print(f"It took {t_end - t_start}s.")
Ejemplo n.º 10
0
from feature_extractor import Extractor

input = [
    "Test sentences", "How do we handle punctuation?",
    "What do we do about special characters", "", "What about empty test"
]
extractor = Extractor(input)
print("input sentences:")
print(input)
print("")
vocab = extractor.get_features(7)
print("vocab:")
print(vocab)
print("")
encoded_data = extractor.encode_data_with_features(vocab)
print("encoded data:")
print(encoded_data)
Ejemplo n.º 11
0
def main(args):
	is_num_read_exist = False

	######################
	# Arguments checking
	######################
	if(len(args) ==3):
		input_filename = args[1]
		output_filename = args[2]
		num_read = 9999999
	elif(len(args) == 4):
		input_filename = args[1]
		output_filename = args[2]
		try:
			num_read = int(args[3])
			is_num_read_exist = True
			print "Number of read is %d" % num_read
		except ValueError:
			print "Parameter (%s) is not a numeric" % args[3]
	else:
		print "Wrong number of arguments"
		print "Usage: python buildarff.py <input_filename> <output_filename> [number_of_tweets_per_class]"
		sys.exit()

	arff_fp = open(output_filename,"w")
	init_arff_format(arff_fp)
	sentences = []			
	feature_extractor = Extractor()
	count_list = {0:0,2:0,4:0}
	with open(input_filename,"r") as ifp:
		lines = ifp.readlines()
		# if <A=?> is appear, append liens into sentences, until another <A=?> shows up
		tweet = None
		for l in lines:
			m = re.match("<A=(\\d)>(.*)",l)
			if(m is not None): 
				# found start of tweet
				polar = int(m.group(1))
				# save previous tweet, if exists
				if(tweet is None): #no previous tweet
					tweet = TweetText()
					tweet.set_polar(polar)	
				else: #save previous tweet
					#save previous
					#print tweet.get_polar()
					if(count_list[tweet.get_polar()] >= num_read):
						tweet = TweetText()
						tweet.set_polar(polar)	
						continue
					else:
						count_list[tweet.get_polar()] +=1 
						feature_extractor.load_tweet(tweet)
						arff_fp.write(feature_extractor.get_features_result()+"\n")
						# init new TweetText
						tweet = TweetText()
						tweet.set_polar(polar)	
			else:
				if(re.match(r"^\n$", l) is None):
					if(tweet is not None):
						tweet.add_sentences(l)
				
	arff_fp.close()
Ejemplo n.º 12
0
class DeepSort(object):
    def __init__(self, model_path,epoch_num=0,max_dist=0.2,use_cuda=False,mot_type='face'):
        self.min_confidence = 0.3
        self.nms_max_overlap = 1.0
        self.img_size = [112,112]
        self.mot_type = mot_type
        if mot_type == 'person':
            self.extractor = Extractor(model_path, use_cuda=use_cuda)
        else:
            self.extractor = mx_FaceRecognize(model_path,epoch_num,self.img_size)
        max_cosine_distance = cfgs.max_cosine_distance #max_dist
        nn_budget = 100
        metric = NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
        self.tracker = Tracker(metric)
        self.feature_tmp = []

    def update(self, bbox_xcycwh, confidences, ori_img,update_fg=True):
        self.height, self.width = ori_img.shape[:2]
        # generate detections
        if update_fg or not(len(bbox_xcycwh)==len(self.feature_tmp)):
            features = self._get_features(bbox_xcycwh, ori_img)
            self.feature_tmp = features
        else:
            features = self.feature_tmp
        detections = [Detection(bbox_xcycwh[i], conf, features[i]) for i,conf in enumerate(confidences) if conf>self.min_confidence]

        # run on non-maximum supression
        # boxes = np.array([d.tlwh for d in detections])
        # scores = np.array([d.confidence for d in detections])
        # indices = non_max_suppression( boxes, self.nms_max_overlap, scores)
        # detections = [detections[i] for i in indices]

        # update tracker
        self.tracker.predict()
        self.tracker.update(detections)

        # output bbox identities
        outputs = []
        for track in self.tracker.tracks:
            #print('time_s',track.time_since_update)
            if not track.is_confirmed() or track.time_since_update > 1:
                continue
            box = track.to_tlwh()
            x1,y1,x2,y2 = self._tlwh_to_xyxy(box)
            track_id = track.track_id
            outputs.append(np.array([x1,y1,x2,y2,track_id], dtype=np.int))
        if len(outputs) > 0:
            outputs = np.stack(outputs,axis=0)
        return outputs


    """
    TODO:
        Convert bbox from xc_yc_w_h to xtl_ytl_w_h
    Thanks [email protected] for reporting this bug!
    """
    @staticmethod
    def _xywh_to_tlwh(bbox_xywh):
        bbox_xywh[:,0] = bbox_xywh[:,0] - bbox_xywh[:,2]/2.
        bbox_xywh[:,1] = bbox_xywh[:,1] - bbox_xywh[:,3]/2.
        return bbox_xywh


    def _xcycwh_to_xyxy(self, bbox_xywh):
        x,y,w,h = bbox_xywh
        x1 = max(int(x-w/2),0)
        x2 = min(int(x+w/2),self.width-1)
        y1 = max(int(y-h/2),0)
        y2 = min(int(y+h/2),self.height-1)
        return x1,y1,x2,y2

    def _tlwh_to_xyxy(self, bbox_tlwh):
        """
        TODO:
            Convert bbox from xtl_ytl_w_h to xc_yc_w_h
        Thanks [email protected] for reporting this bug!
        """
        x,y,w,h = bbox_tlwh
        x1 = max(int(x),0)
        x2 = min(int(x+w),self.width-1)
        y1 = max(int(y),0)
        y2 = min(int(y+h),self.height-1)
        return x1,y1,x2,y2
    
    def _get_features(self, bbox_xcycwh, ori_img):
        im_crops = []
        for box in bbox_xcycwh:
            _,_,w,h = box
            x1,y1,x2,y2 = self._xcycwh_to_xyxy(box)
            im = ori_img[y1:y2,x1:x2]
            #print('img',ori_img.shape)
            if self.mot_type=='face':
                #if w != self.img_size[1] or h != self.img_size[0]:
                im = cv2.resize(im,(self.img_size[1],self.img_size[0]))
            im_crops.append(im)
        if len(im_crops)>0:
            features = self.extractor.extractfeature(np.array(im_crops)) if self.mot_type=='face' else self.extractor(im_crops)
        else:
            features = np.array([])
        return features
Ejemplo n.º 13
0
def main(args):
    is_num_read_exist = False

    ######################
    # Arguments checking
    ######################
    if (len(args) == 3):
        input_filename = args[1]
        output_filename = args[2]
        num_read = 9999999
    elif (len(args) == 4):
        input_filename = args[1]
        output_filename = args[2]
        try:
            num_read = int(args[3])
            is_num_read_exist = True
            print "Number of read is %d" % num_read
        except ValueError:
            print "Parameter (%s) is not a numeric" % args[3]
    else:
        print "Wrong number of arguments"
        print "Usage: python buildarff.py <input_filename> <output_filename> [number_of_tweets_per_class]"
        sys.exit()

    arff_fp = open(output_filename, "w")
    init_arff_format(arff_fp)
    sentences = []
    feature_extractor = Extractor()
    count_list = {0: 0, 2: 0, 4: 0}
    with open(input_filename, "r") as ifp:
        lines = ifp.readlines()
        # if <A=?> is appear, append liens into sentences, until another <A=?> shows up
        tweet = None
        for l in lines:
            m = re.match("<A=(\\d)>(.*)", l)
            if (m is not None):
                # found start of tweet
                polar = int(m.group(1))
                # save previous tweet, if exists
                if (tweet is None):  #no previous tweet
                    tweet = TweetText()
                    tweet.set_polar(polar)
                else:  #save previous tweet
                    #save previous
                    #print tweet.get_polar()
                    if (count_list[tweet.get_polar()] >= num_read):
                        tweet = TweetText()
                        tweet.set_polar(polar)
                        continue
                    else:
                        count_list[tweet.get_polar()] += 1
                        feature_extractor.load_tweet(tweet)
                        arff_fp.write(feature_extractor.get_features_result() +
                                      "\n")
                        # init new TweetText
                        tweet = TweetText()
                        tweet.set_polar(polar)
            else:
                if (re.match(r"^\n$", l) is None):
                    if (tweet is not None):
                        tweet.add_sentences(l)

    arff_fp.close()