예제 #1
0
    def pipeline(self,func,data,train):
        import os
        os.system("taskset -p 0xff %d" % os.getpid())
        
        func.acts=self.acts
        logger.debug('Starting .... %s' % (func.shortrunname))
        Tdata=func.preprocessor.process(self.datasetdscr, data)
        logger.debug('Preprocessing Finished %s' % (func.preprocessor.shortname()))
        Sdata=prepare_segment2(func,Tdata,self.datasetdscr)
        logger.debug('Segmentation Finished %d segment created %s' % (len(Sdata.set_window), func.segmentor.shortname()))
        Sdata.set=featureExtraction(func.featureExtractor,self.datasetdscr,Sdata,True)
        logger.debug('FeatureExtraction Finished shape %s , %s' % (str(Sdata.set.shape), func.featureExtractor.shortname()))
        if(train):
            func.classifier.createmodel(Sdata.set[0].shape,len(self.acts))
            func.classifier.setWeight(self.weight)
            logger.debug('Classifier model created  %s' % (func.classifier.shortname()))
            func.classifier.train(Sdata.set, Sdata.label) 
            logger.debug('Classifier model trained  %s' % (func.classifier.shortname()))

        logger.info("Evaluating....")
        result=Data('Result')
        result.shortrunname=func.shortrunname
        result.Sdata=Sdata
        result.functions={}
        for f in func.__dict__:
            obj = func.__dict__[f]
            if isinstance(obj, MyTask):              
                result.functions[f]=(obj.shortname(),obj.params)

        
        result.predicted        =func.classifier.predict(Sdata.set)
        result.predicted_classes=func.classifier.predict_classes(Sdata.set)    
        pred_events             =func.combiner.combine(Sdata.s_event_list,Sdata.set_window,result.predicted)
        logger.debug('events merged  %s' % (func.combiner.shortname()))
        
        
        result.pred_events  =pred_events
        result.real_events  =data.a_events

        result.event_cm     =event_confusion_matrix(data.a_events,pred_events,self.acts)
        result.quality      =CMbasedMetric(result.event_cm,'macro',self.weight)
        #eventeval=EventBasedMetric(Sdata.a_events,pred_events,self.acts)
        
        logger.debug('Evalution quality is %s'%result.quality)
        return result
예제 #2
0
    def fusion(self, results, real_events, isTrain):
        intree = IntervalTree()
        logger.info("\n=======================fusion activties ========")
        # intree = IntervalTree()
        # Segmentaion ###########################
        for indx, tacts in enumerate(self.gacts):
            result = results[indx]

            for i in range(0, len(result.Sdata.set_window)):
                idx = result.Sdata.set_window[i]
                start = result.Sdata.s_event_list[idx[0], 1]
                end = result.Sdata.s_event_list[idx[-1], 1]
                rcls = result.Sdata.label[i]
                pcls = result.predicted_classes[i]
                fullprob = result.predicted[i]
                if (end == start):
                    continue
                d = Data(str(i))
                d.real = rcls
                d.pred = pcls
                d.pred_prob = fullprob
                if (isTrain):
                    self.train_quality[indx] = result.quality

                d.gindx = indx
                # {'real':rcls,'pred':pcls,'pred_prob':fullprob,'train_q':result.quality}
                intree[start:end] = d

        intree.split_overlaps()
        segments = defaultdict(dict)
        for item in intree.items():
            segments[item.begin.value << 64
                     | item.end.value]['begin'] = item.begin
            segments[item.begin.value << 64 | item.end.value]['end'] = item.end
            segments[item.begin.value << 64
                     | item.end.value][item.data.gindx] = item.data

        # Feature Extraction ###########################
        f = np.zeros((len(segments), len(self.gacts) * len(self.acts)))
        label = np.zeros(len(segments))
        times = []
        iseg = 0
        for timeseg in segments:
            seg = segments[timeseg]
            b = seg['begin']
            e = seg['end']
            times.append({'begin': b, 'end': e})
            for indx in range(len(self.gacts)):
                if (indx in seg):
                    label[iseg] = seg[indx].real
                    start = indx * len(self.acts)
                    end = (indx + 1) * len(self.acts)
                    if (self.train_quality[indx]['f1'] < 0.1):
                        continue
                    f[iseg, start:end] = seg[indx].pred_prob
            iseg += 1

        #TRAIN #######################

        if (isTrain):
            inputsize = (len(f[0]), )
            outputsize = len(self.acts)
            self.fusion_model = tf.keras.models.Sequential([
                tf.keras.layers.Dense(128, input_shape=inputsize),
                tf.keras.layers.Dense(512, activation=tf.nn.relu),
                tf.keras.layers.Dropout(0.2),
                tf.keras.layers.Dense(outputsize, activation=tf.nn.softmax)
            ],
                                                           name='fusion')
            if (np.max(label) == 0):
                # self.trained=False
                cw = np.ones(len(self.acts))
            else:
                cw = compute_class_weight("balanced", self.acts, label)

            self.fusion_model.summary()
            self.fusion_model.compile(
                optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=[
                    tf.keras.metrics.SparseCategoricalAccuracy(name='acc')
                ])
            self.fusion_model.fit(f, label, epochs=10, class_weight=cw)

        #EVALUATE #######################
        result = Data('result')
        result.results = results
        result.predicted = self.fusion_model.predict(f)
        result.predicted_classes = self.fusion_model.predict_classes(f)
        # predicted   = np.argmax(model.predict(f), axis=1)
        pred_events = []
        ptree = {}
        epsilon = pd.to_timedelta('1s')

        for i in range(len(f)):
            start = times[i]['begin']
            end = times[i]['end']
            pclass = result.predicted_classes[i]
            pred_events.append({
                'Activity': pclass,
                'StartTime': start,
                'EndTime': end
            })

        pred_events = pd.DataFrame(pred_events)
        pred_events = pred_events.sort_values(['StartTime'])
        pred_events = pred_events.reset_index()
        pred_events = pred_events.drop(['index'], axis=1)

        result.shortrunname = "fusion model" + str(
            {r: results[r].shortrunname
             for r in results})
        result.times = times
        result.pred_events = pred_events
        result.real_events = real_events

        result.event_cm = event_confusion_matrix(result.real_events,
                                                 result.pred_events, self.acts)
        result.quality = CMbasedMetric(result.event_cm, 'macro')
        result.functions = {r: results[r].functions for r in results}
        logger.debug('Evalution quality is %s' % result.quality)

        return result
예제 #3
0
    def fusion(self, results, real_events, isTrain):
        intree = IntervalTree()
        logger.info("\n=======================fusion activties ========")
        # intree = IntervalTree()
        # Segmentaion ###########################
        for indx, tacts in enumerate(self.gacts):
            result = results[indx]

            for i in range(0, len(result.Sdata.set_window)):
                idx = result.Sdata.set_window[i]
                start = result.Sdata.s_event_list[idx[0], 1]
                end = result.Sdata.s_event_list[idx[-1], 1]
                rcls = result.Sdata.label[i]
                pcls = result.predicted_classes[i]
                fullprob = result.predicted[i]
                if (end == start):
                    continue
                d = Data(str(i))
                d.real = rcls
                d.pred = pcls
                d.pred_prob = fullprob
                if (isTrain):
                    self.train_quality[indx] = result.quality

                d.gindx = indx
                # {'real':rcls,'pred':pcls,'pred_prob':fullprob,'train_q':result.quality}
                intree[start:end] = d

        intree.split_overlaps()
        segments = defaultdict(dict)
        for item in intree.items():
            segments[item.begin.value << 64
                     | item.end.value]['begin'] = item.begin
            segments[item.begin.value << 64 | item.end.value]['end'] = item.end
            segments[item.begin.value << 64
                     | item.end.value][item.data.gindx] = item.data

        probs = np.zeros((len(segments), len(self.acts)))

        # Feature Extraction ###########################

        label = np.zeros(len(segments))
        times = []
        iseg = 0
        for timeseg in segments:
            seg = segments[timeseg]
            b = seg['begin']
            e = seg['end']
            times.append({'begin': b, 'end': e})
            for indx in range(len(self.gacts)):
                if (indx in seg):
                    label[iseg] = seg[indx].real
                    if (self.mode == 1):
                        probs[iseg, :] += np.array(
                            seg[indx].pred_prob
                        ) * self.train_quality[indx]['f1'] / len(self.gacts)
                    elif self.mode == 2:
                        p = np.zeros(len(self.acts))
                        p[np.argmax(seg[indx].pred_prob)] = 1
                        probs[iseg, :] += p
                    else:
                        p = np.zeros(len(self.acts))
                        p[np.argmax(seg[indx].pred_prob
                                    )] = self.train_quality[indx]['f1']
                        probs[iseg, :] += p
            iseg += 1
        plabel = np.argmax(probs, 1)

        #EVALUATE #######################
        result = Data('result')
        result.results = results
        result.predicted = probs
        result.predicted_classes = plabel
        # predicted   = np.argmax(model.predict(f), axis=1)
        pred_events = []
        ptree = {}
        epsilon = pd.to_timedelta('1s')

        for i in range(len(segments)):
            start = times[i]['begin']
            end = times[i]['end']
            pclass = result.predicted_classes[i]
            pred_events.append({
                'Activity': pclass,
                'StartTime': start,
                'EndTime': end
            })

        pred_events = pd.DataFrame(pred_events)
        pred_events = pred_events.sort_values(['StartTime'])
        pred_events = pred_events.reset_index()
        pred_events = pred_events.drop(['index'], axis=1)

        result.shortrunname = "fusion model" + str(
            {r: results[r].shortrunname
             for r in results})
        result.times = times
        result.pred_events = pred_events
        result.real_events = real_events

        result.event_cm = event_confusion_matrix(result.real_events,
                                                 result.pred_events, self.acts)
        result.quality = CMbasedMetric(result.event_cm, 'macro')
        result.functions = {r: results[r].functions for r in results}
        logger.debug('Evalution quality is %s' % result.quality)

        return result
예제 #4
0
model.evaluate(f,label)


combiner=EmptyCombiner()
predicted=model.predict_classes(f)
# predicted   = np.argmax(model.predict(f), axis=1) 
pred_events      = []
ptree       = {}
epsilon=pd.to_timedelta('1s')

for i in range(len(f)):
    
    start   = times[i][0]
    end     = times[i][1]
    pclass  = predicted[i]

    pred_events.append({'Activity': pclass, 'StartTime': start, 'EndTime': end})

pred_events = pd.DataFrame(pred_events)
pred_events = pred_events.sort_values(['StartTime'])
pred_events = pred_events.reset_index()
pred_events = pred_events.drop(['index'], axis=1)

result=Data('result')
result.pred_events  =pred_events
result.real_events  =savedata.train_results[11].real_events

result.event_cm     =event_confusion_matrix(result.real_events,result.pred_events,savedata.acts)
result.quality      =CMbasedMetric(result.event_cm,'macro')

print(result.quality)