Пример #1
0
def calculate_dev_eer(n_epochs, preds_fd, dump_path, fold):
    csv_path = cfg.dev_cv_csv_path
    anno_fd = cfg.dev_anno_fd

    mean_eer_list = []
    for epoch in xrange(n_epochs):
        pred_path = preds_fd + '/' + str(epoch) + 'epoch.txt'

        # get ground truth dict
        gt_dict = {}
        with open(csv_path, 'rb') as f:
            reader = csv.reader(f)
            lis = list(reader)

            for li in lis:
                na = li[1]
                curr_fold = int(li[2])

                if fold == curr_fold:
                    anno_path = anno_fd + '/' + na + '.csv'
                    tags = GetTags(anno_path)
                    gt_dict[na + '.16kHz.wav'] = tags

        # evaluate eer for each tag and average them
        eer_ary = []
        for tag in cfg.labels:
            if tag is not 'S':
                gt_dict_curr = {}
                for key in gt_dict.keys():
                    if tag in gt_dict[key]:
                        gt_dict_curr[key] = 1
                    else:
                        gt_dict_curr[key] = 0

                eer_val = eer.compute_eer(pred_path, tag, gt_dict_curr)
                eer_ary.append(eer_val)

        # average err of each tag
        mean_eer_list.append(np.mean(eer_ary))

    # dump err list of all epochs
    pickle.dump(mean_eer_list, open(dump_path, 'wb'))
Пример #2
0
def main(args):
    model = load_model(args.model)

    data_test = pp_data.DataLoader(args.data, partition='evaluation')

    pred_scores = np.zeros((len(data_test), n_classes))
    true_scores = np.zeros((len(data_test), n_classes))
    result_rows = []
    chunk_refs = []

    for it, [X, y] in enumerate(data_test):
        pred = model.predict(X)

        pred = pred.mean(axis=0)
        y = y[0, :]

        pred_scores[it, :] = pred
        true_scores[it, :] = y

        chunk_ref = "chunk_" + str(it)
        chunk_refs.append(chunk_ref)

        for cl in range(n_classes):
            result_rows.append([chunk_ref, ind_to_tag[cl], pred[cl]])

    eval_result_filename = "eval_result.csv"
    with open(os.path.join(args.data, eval_result_filename), "w") as csvfile:
        writer = csv.writer(csvfile)
        writer.writerows(result_rows)

    eers = np.zeros(n_classes)
    for cl in range(n_classes):
        eers[cl] = compute_eer(os.path.join(args.data, eval_result_filename),
                               ind_to_tag[cl],
                               dict(zip(chunk_refs, list(true_scores[:, cl]))))

    print("EER scores per tag:", eers)
def recognize():
    ## prepare data
    #_, _, te_X, te_y = pp_data.GetAllData(fe_fd_right, fe_fd_left, fe_fd_mean, fe_fd_diff, agg_num, hop, fold )
    ##te_X = reshapeX(te_X)
    #print te_X.shape
    
    # do recognize and evaluation
    thres = 0.4     # thres, tune to prec=recall, if smaller, make prec smaller
    n_labels = len( cfg.labels )
    
    gt_roll = []
    pred_roll = []
    result_roll = []
    y_true_binary_c = []
    y_true_file_c = []
    y_true_binary_m = []
    y_true_file_m = []
    y_true_binary_f = []
    y_true_file_f = []
    y_true_binary_v = []
    y_true_file_v = []
    y_true_binary_p = []
    y_true_file_p = []
    y_true_binary_b = []
    y_true_file_b = []
    y_true_binary_o = []
    y_true_file_o = []
    with open( cfg.dev_cv_csv_path, 'rb') as f:
        reader = csv.reader(f)
        lis = list(reader)
    
        # read one line
        for li in lis:
            na = li[1]
            curr_fold = int(li[2])
            
            if fold==curr_fold:
                # get features, tags
                fe_path_left = fe_fd_left + '/' + na + '.f'
                fe_path_right = fe_fd_right + '/' + na + '.f'
                fe_path_mean = fe_fd_mean + '/' + na + '.f'
                fe_path_diff = fe_fd_diff + '/' + na + '.f'
                fe_path_ipd = fe_fd_ipd + '/' + na + '.f'
                fe_path_ild = fe_fd_ild + '/' + na + '.f'
                #fe_path_ori = fe_fd_ori + '/' + na + '.f'
                info_path = cfg.dev_wav_fd + '/' + na + '.csv'
                #print na
                tags = pp_data.GetTags( info_path )
                #print tags
                y = pp_data.TagsToCategory( tags )
                #print y
                #sys.exit()
                #X_l = cPickle.load( open( fe_path_left, 'rb' ) )
                #X_r = cPickle.load( open( fe_path_right, 'rb' ) )
                X_m = cPickle.load( open( fe_path_mean, 'rb' ) )
                #X_d = cPickle.load( open( fe_path_diff, 'rb' ) )
                #X_ipd = cPickle.load( open( fe_path_ipd, 'rb' ) )
                #X_ild = cPickle.load( open( fe_path_ild, 'rb' ) )
                #X_o = cPickle.load( open( fe_path_ori, 'rb' ) )

                # aggregate data
                #X3d_l = mat_2d_to_3d( X_l, agg_num, hop )
                #X3d_r = mat_2d_to_3d( X_r, agg_num, hop )
                X3d_m = mat_2d_to_3d( X_m, agg_num, hop )
   		#X3d_d = mat_2d_to_3d( X_d, agg_num, hop )
                #X3d_ipd = mat_2d_to_3d( X_ipd, agg_num, hop )
   		#X3d_ild = mat_2d_to_3d( X_ild, agg_num, hop )
   		#X3d_o = mat_2d_to_3d( X_o, agg_num, hop )
     	        ## reshape 3d to 4d
       	        #X4d_l = reshape_3d_to_4d( X3d_l)
                #X4d_r = reshape_3d_to_4d( X3d_r)
                #X4d_m = reshape_3d_to_4d( X3d_m)
                #X4d_d = reshape_3d_to_4d( X3d_d)
                # concatenate
                #X4d=mat_concate_multiinmaps6in(X3d_l, X3d_r, X3d_m, X3d_d, X3d_ipd, X3d_ild)
                X3d_m=reshapeX1(X3d_m)
                #X4d=np.swapaxes(X4d,1,2) # or np.transpose(x,(1,0,2))  1,0,2 is axis
                te_X1=X3d_m
                #te_X2=X3d_ild
                #te_X1 = reshapeX1(te_X1)
                #te_X2 = reshapeX2(te_X2)
                
                if debug:
                    # with a Sequential model
                    #md.summary()
                    print na
                    get_3rd_layer_output = K.function([md.layers[0].input, K.learning_phase()], [md.layers[4].output])
                    layer_output = get_3rd_layer_output([te_X1, 0])[0]
                    print layer_output.shape
                    #layer_output1=layer_output[5,:,:]
                    layer_output1=layer_output[:,16,:]
                    imgplot=plt.matshow((layer_output1.T))
                    #imgplot.set_cmap('spectral')
                    plt.colorbar()
                    plt.show()
                    sys.pause()
                
                #p_y_pred = md.predict( [te_X1,te_X2] )
                p_y_pred = md.predict( te_X1 )
                p_y_pred = np.mean( p_y_pred, axis=0 )     # shape:(n_label)
                pred = np.zeros(n_labels)
                pred[ np.where(p_y_pred>thres) ] = 1
                ind=0
                for la in cfg.labels:
                    if la=='S':
                        break
                    elif la=='c':
                        y_true_file_c.append(na)
                        y_true_binary_c.append(y[ind])
                    elif la=='m':
                        y_true_file_m.append(na)
                        y_true_binary_m.append(y[ind])
                    elif la=='f':
                        y_true_file_f.append(na)
                        y_true_binary_f.append(y[ind])
                    elif la=='v':
                        y_true_file_v.append(na)
                        y_true_binary_v.append(y[ind])
                    elif la=='p':
                        y_true_file_p.append(na)
                        y_true_binary_p.append(y[ind])
                    elif la=='b':
                        y_true_file_b.append(na)
                        y_true_binary_b.append(y[ind])
                    elif la=='o':
                        y_true_file_o.append(na)
                        y_true_binary_o.append(y[ind])
                    result=[na,la,p_y_pred[ind]]
                    result_roll.append(result)
                    ind=ind+1
                
                
                pred_roll.append( pred )
                gt_roll.append( y )
    
    pred_roll = np.array( pred_roll )
    gt_roll = np.array( gt_roll )
    #write csv for EER computation
    csvfile=file('result.csv','wb')
    writer=csv.writer(csvfile)
    #writer.writerow(['fn','label','score'])
    writer.writerows(result_roll)
    csvfile.close()
    
    # calculate prec, recall, fvalue
    prec, recall, fvalue = prec_recall_fvalue( pred_roll, gt_roll, thres )
    # EER for each tag : [ 'c', 'm', 'f', 'v', 'p', 'b', 'o', 'S' ]
    EER_c=eer.compute_eer('result.csv', 'c', dict(zip(y_true_file_c, y_true_binary_c)))
    EER_m=eer.compute_eer('result.csv', 'm', dict(zip(y_true_file_m, y_true_binary_m)))
    EER_f=eer.compute_eer('result.csv', 'f', dict(zip(y_true_file_f, y_true_binary_f)))
    EER_v=eer.compute_eer('result.csv', 'v', dict(zip(y_true_file_v, y_true_binary_v)))
    EER_p=eer.compute_eer('result.csv', 'p', dict(zip(y_true_file_p, y_true_binary_p)))
    EER_b=eer.compute_eer('result.csv', 'b', dict(zip(y_true_file_b, y_true_binary_b)))
    EER_o=eer.compute_eer('result.csv', 'o', dict(zip(y_true_file_o, y_true_binary_o)))
    EER=(EER_c+EER_m+EER_v+EER_p+EER_f+EER_b+EER_o)/7.0
    print prec, recall, fvalue
    print EER_c,EER_m,EER_f,EER_v,EER_p,EER_b,EER_o
    print EER
Пример #4
0
def recognize():

    # do recognize and evaluation
    thres = 0.4  # thres, tune to prec=recall, if smaller, make prec smaller
    n_labels = len(cfg.labels)

    gt_roll = []
    pred_roll = []
    result_roll = []
    y_true_binary_c = []
    y_true_file_c = []
    y_true_binary_m = []
    y_true_file_m = []
    y_true_binary_f = []
    y_true_file_f = []
    y_true_binary_v = []
    y_true_file_v = []
    y_true_binary_p = []
    y_true_file_p = []
    y_true_binary_b = []
    y_true_file_b = []
    y_true_binary_o = []
    y_true_file_o = []
    pred_roll_c = []
    gt_roll_c = []
    pred_roll_m = []
    gt_roll_m = []
    pred_roll_f = []
    gt_roll_f = []
    pred_roll_v = []
    gt_roll_v = []
    pred_roll_p = []
    gt_roll_p = []
    pred_roll_b = []
    gt_roll_b = []
    pred_roll_o = []
    gt_roll_o = []
    with open(cfg.dev_cv_csv_path, 'rb') as f:
        reader = csv.reader(f)
        lis = list(reader)

        # read one line
        line_n = 0
        for li in lis:
            na = li[1]
            curr_fold = int(li[2])

            if fold == curr_fold:
                line_n = line_n + 1
                print line_n
                # get features, tags
                fe_path = fe_fd + '/' + na + '.f'
                info_path = cfg.dev_wav_fd + '/' + na + '.csv'
                #info_path = '/vol/vssp/msos/yx/chime_home/chunk_annotations/annotations' + '/' + na + '.csv'
                #print na
                tags = pp_data.GetTags(info_path)
                #print tags
                y = pp_data.TagsToCategory(tags)
                #print y
                #sys.exit()
                X = cPickle.load(open(fe_path, 'rb'))

                X = scaler.transform(X)
                X_1 = X[:6, :]
                X_n = np.mean(X_1, axis=0)

                # aggregate data
                X3d = mat_2d_to_3d(X, agg_num, hop)
                ## reshape 3d to 4d
                #X4d = reshape_3d_to_4d( X3d)
                #X4d=np.swapaxes(X4d,2,3) # or np.transpose(x,(1,0,2))  1,0,2 is axis
                #X4d=reshapeX( X)
                X3d = reshapeX(X3d)
                #print X3d.shape
                X_n = np.tile(X_n, (len(X3d), 1))
                X_in = np.concatenate((X3d, X_n), axis=1)

                p_y_pred = md.predict(X_in)
                p_y_pred = np.mean(p_y_pred, axis=0)  # shape:(n_label)
                pred = np.zeros(n_labels)
                pred[np.where(p_y_pred > thres)] = 1
                ind = 0
                for la in cfg.labels:
                    if la == 'S':
                        break
                    elif la == 'c':
                        y_true_file_c.append(na)
                        y_true_binary_c.append(y[ind])
                        pred_roll_c.append(pred[ind])
                        gt_roll_c.append(y[ind])
                    elif la == 'm':
                        y_true_file_m.append(na)
                        y_true_binary_m.append(y[ind])
                        pred_roll_m.append(pred[ind])
                        gt_roll_m.append(y[ind])
                    elif la == 'f':
                        y_true_file_f.append(na)
                        y_true_binary_f.append(y[ind])
                        pred_roll_f.append(pred[ind])
                        gt_roll_f.append(y[ind])
                    elif la == 'v':
                        y_true_file_v.append(na)
                        y_true_binary_v.append(y[ind])
                        pred_roll_v.append(pred[ind])
                        gt_roll_v.append(y[ind])
                    elif la == 'p':
                        y_true_file_p.append(na)
                        y_true_binary_p.append(y[ind])
                        pred_roll_p.append(pred[ind])
                        gt_roll_p.append(y[ind])
                    elif la == 'b':
                        y_true_file_b.append(na)
                        y_true_binary_b.append(y[ind])
                        pred_roll_b.append(pred[ind])
                        gt_roll_b.append(y[ind])
                    elif la == 'o':
                        y_true_file_o.append(na)
                        y_true_binary_o.append(y[ind])
                        pred_roll_o.append(pred[ind])
                        gt_roll_o.append(y[ind])
                    result = [na, la, p_y_pred[ind]]
                    result_roll.append(result)
                    ind = ind + 1

                pred_roll.append(pred)
                gt_roll.append(y)

    pred_roll = np.array(pred_roll)
    gt_roll = np.array(gt_roll)
    pred_roll_c = np.array(pred_roll_c)
    gt_roll_c = np.array(gt_roll_c)
    pred_roll_m = np.array(pred_roll_m)
    gt_roll_m = np.array(gt_roll_m)
    pred_roll_f = np.array(pred_roll_f)
    gt_roll_f = np.array(gt_roll_f)
    pred_roll_v = np.array(pred_roll_v)
    gt_roll_v = np.array(gt_roll_v)
    pred_roll_p = np.array(pred_roll_p)
    gt_roll_p = np.array(gt_roll_p)
    pred_roll_b = np.array(pred_roll_b)
    gt_roll_b = np.array(gt_roll_b)
    pred_roll_o = np.array(pred_roll_o)
    gt_roll_o = np.array(gt_roll_o)
    #write csv for EER computation
    csvfile = file('result.csv', 'wb')
    writer = csv.writer(csvfile)
    #writer.writerow(['fn','label','score'])
    writer.writerows(result_roll)
    csvfile.close()

    # calculate prec, recall, fvalue
    prec, recall, fvalue = prec_recall_fvalue(pred_roll, gt_roll, thres)
    prec_c, recall_c, fvalue_c = prec_recall_fvalue(pred_roll_c, gt_roll_c,
                                                    thres)
    prec_m, recall_m, fvalue_m = prec_recall_fvalue(pred_roll_m, gt_roll_m,
                                                    thres)
    prec_f, recall_f, fvalue_f = prec_recall_fvalue(pred_roll_f, gt_roll_f,
                                                    thres)
    prec_v, recall_v, fvalue_v = prec_recall_fvalue(pred_roll_v, gt_roll_v,
                                                    thres)
    prec_p, recall_p, fvalue_p = prec_recall_fvalue(pred_roll_p, gt_roll_p,
                                                    thres)
    prec_b, recall_b, fvalue_b = prec_recall_fvalue(pred_roll_b, gt_roll_b,
                                                    thres)
    prec_o, recall_o, fvalue_o = prec_recall_fvalue(pred_roll_o, gt_roll_o,
                                                    thres)
    # EER for each tag : [ 'c', 'm', 'f', 'v', 'p', 'b', 'o', 'S' ]
    EER_c = eer.compute_eer('result.csv', 'c',
                            dict(zip(y_true_file_c, y_true_binary_c)))
    EER_m = eer.compute_eer('result.csv', 'm',
                            dict(zip(y_true_file_m, y_true_binary_m)))
    EER_f = eer.compute_eer('result.csv', 'f',
                            dict(zip(y_true_file_f, y_true_binary_f)))
    EER_v = eer.compute_eer('result.csv', 'v',
                            dict(zip(y_true_file_v, y_true_binary_v)))
    EER_p = eer.compute_eer('result.csv', 'p',
                            dict(zip(y_true_file_p, y_true_binary_p)))
    EER_b = eer.compute_eer('result.csv', 'b',
                            dict(zip(y_true_file_b, y_true_binary_b)))
    EER_o = eer.compute_eer('result.csv', 'o',
                            dict(zip(y_true_file_o, y_true_binary_o)))
    EER = (EER_c + EER_m + EER_v + EER_p + EER_f + EER_b + EER_o) / 7.0
    prec2 = (prec_c + prec_m + prec_f + prec_v + prec_p + prec_b +
             prec_o) / 7.0
    recall2 = (recall_c + recall_m + recall_f + recall_v + recall_p +
               recall_b + recall_o) / 7.0
    fvalue2 = (fvalue_c + fvalue_m + fvalue_f + fvalue_v + fvalue_p +
               fvalue_b + fvalue_o) / 7.0
    print prec, recall, fvalue
    print prec2, recall2, fvalue2
    print EER_c, EER_m, EER_f, EER_v, EER_p, EER_b, EER_o
    print prec_c, prec_m, prec_f, prec_v, prec_p, prec_b, prec_o
    print recall_c, recall_m, recall_f, recall_v, recall_p, recall_b, recall_o
    print fvalue_c, fvalue_m, fvalue_f, fvalue_v, fvalue_p, fvalue_b, fvalue_o
    print EER
Пример #5
0
def recognize():
    ## prepare data
    #_, _, te_X, te_y = pp_data.GetAllData(fe_fd_right, fe_fd_left, fe_fd_mean, fe_fd_diff, agg_num, hop, fold )
    ##te_X = reshapeX(te_X)
    #print te_X.shape

    # do recognize and evaluation
    thres = 0.4  # thres, tune to prec=recall, if smaller, make prec smaller
    n_labels = len(cfg.labels)

    gt_roll = []
    pred_roll = []
    result_roll = []
    y_true_binary_c = []
    y_true_file_c = []
    y_true_binary_m = []
    y_true_file_m = []
    y_true_binary_f = []
    y_true_file_f = []
    y_true_binary_v = []
    y_true_file_v = []
    y_true_binary_p = []
    y_true_file_p = []
    y_true_binary_b = []
    y_true_file_b = []
    y_true_binary_o = []
    y_true_file_o = []
    with open(cfg.dev_cv_csv_path, 'rb') as f:
        reader = csv.reader(f)
        lis = list(reader)

        line_n = 0
        # read one line
        for li in lis:
            na = li[1]
            curr_fold = int(li[2])

            if fold == curr_fold:
                line_n = line_n + 1
                # get features, tags
                fe_path_left = fe_fd_left + '/' + na + '.f'
                fe_path_right = fe_fd_right + '/' + na + '.f'
                fe_path_mean = fe_fd_mean + '/' + na + '.f'
                fe_path_diff = fe_fd_diff + '/' + na + '.f'
                fe_path_ipd = fe_fd_ipd + '/' + na + '.f'
                fe_path_ild = fe_fd_ild + '/' + na + '.f'
                #fe_path_ori = fe_fd_ori + '/' + na + '.f'
                info_path = cfg.dev_wav_fd + '/' + na + '.csv'
                #print na
                tags = pp_data.GetTags(info_path)
                print tags
                y = pp_data.TagsToCategory(tags)
                #print y
                #sys.exit()
                #X_l = cPickle.load( open( fe_path_left, 'rb' ) )
                #X_r = cPickle.load( open( fe_path_right, 'rb' ) )
                #X_m = cPickle.load( open( fe_path_mean, 'rb' ) )
                X_m = cPickle.load(
                    open(
                        '/vol/vssp/msos/yx/chime_home/DCASE2016_task4_scrap_2ch_spec_ipd_ild_overlap/Fe/Mel_m/CR_lounge_220110_0731.s0_chunk70.f',
                        'rb'))
                if debug:  ### for fbank
                    # with a Sequential model
                    #md.summary()
                    print na
                    if line_n == 3:
                        #layer_output=np.mean(layer_output[:,:],axis=1)
                        #layer_output=layer_output[0,:,7]
                        #imgplot1,=plt.plot(X_m)
                        #print     layer_output
                        #imgplot=plt.matshow(np.rot90(X_m))
                        imgplot = plt.imshow(20 * np.log10(abs(X_m.T)),
                                             origin='lower',
                                             aspect='auto')
                        #imgplot.set_cmap('spectral')
                        #plt.colorbar()
                        plt.xlabel('Frame number')
                        plt.ylabel('Frequency')
                        plt.show()
                        sys.pause()
                continue
                #print X_m.shape
                #X_d = cPickle.load( open( fe_path_diff, 'rb' ) )
                #X_ipd = cPickle.load( open( fe_path_ipd, 'rb' ) )
                #X_ild = cPickle.load( open( fe_path_ild, 'rb' ) )
                #X_o = cPickle.load( open( fe_path_ori, 'rb' ) )

                # aggregate data
                #X3d_l = mat_2d_to_3d( X_l, agg_num, hop )
                #X3d_r = mat_2d_to_3d( X_r, agg_num, hop )
                X3d_m = mat_2d_to_3d(X_m, agg_num, hop)
                #X3d_d = mat_2d_to_3d( X_d, agg_num, hop )
                #X3d_ipd = mat_2d_to_3d( X_ipd, agg_num, hop )
                #X3d_ild = mat_2d_to_3d( X_ild, agg_num, hop )
                #X3d_o = mat_2d_to_3d( X_o, agg_num, hop )
                ## reshape 3d to 4d
                #X4d_l = reshape_3d_to_4d( X3d_l)
                #X4d_r = reshape_3d_to_4d( X3d_r)
                #X4d_m = reshape_3d_to_4d( X3d_m)
                #X4d_d = reshape_3d_to_4d( X3d_d)
                # concatenate
                #X4d=mat_concate_multiinmaps6in(X3d_l, X3d_r, X3d_m, X3d_d, X3d_ipd, X3d_ild)
                X3d_m = reshapeX1(X3d_m)
                #X4d=np.swapaxes(X4d,1,2) # or np.transpose(x,(1,0,2))  1,0,2 is axis
                te_X1 = X3d_m
                #te_X2=X3d_ild
                #te_X1 = reshapeX1(te_X1)
                #te_X2 = reshapeX2(te_X2)

                if not debug:  ### for localization
                    # with a Sequential model
                    #md.summary()
                    print na
                    get_3rd_layer_output = K.function(
                        [md.layers[0].input,
                         K.learning_phase()], [md.layers[20].output])
                    layer_output = get_3rd_layer_output([te_X1, 0])[0]
                    print layer_output.shape
                    #layer_output1=layer_output[:,:]
                    if line_n == 3:
                        #layer_output=np.mean(layer_output[:,:],axis=1)
                        #layer_output=layer_output[0,:,7]
                        imgplot1, = plt.plot(layer_output[0, :, 0],
                                             label='c',
                                             linewidth=4)
                        #plt.legend(handles=[imgplot1])
                        plt.hold(True)
                        imgplot2, = plt.plot(layer_output[0, :, 1],
                                             label='m',
                                             linewidth=2,
                                             linestyle='--')  #,linewidth=4
                        plt.hold(True)
                        imgplot3, = plt.plot(layer_output[0, :, 2],
                                             label='f')  #,linewidth=4
                        plt.hold(True)
                        imgplot4, = plt.plot(layer_output[0, :, 3],
                                             label='v',
                                             linestyle='--')  #,linewidth=4
                        plt.hold(True)
                        imgplot5, = plt.plot(layer_output[0, :, 4],
                                             label='p',
                                             linewidth=2,
                                             linestyle='--')  #,linewidth=4
                        plt.hold(True)
                        imgplot6, = plt.plot(layer_output[0, :, 5],
                                             label='b',
                                             linestyle='--')  #,linewidth=4
                        plt.hold(True)
                        imgplot7, = plt.plot(layer_output[0, :, 6],
                                             label='o',
                                             linestyle='--')  #,linewidth=4
                        #plt.hold(True)
                        #imgplot8,=plt.plot(layer_output[0,:,7],label='S',linewidth=3) #,linewidth=4
                        plt.legend(handles=[
                            imgplot1, imgplot2, imgplot3, imgplot4, imgplot5,
                            imgplot6, imgplot7
                        ])
                        plt.xlabel('Frame number')
                        plt.ylabel('Event presence probability')
                        #print     layer_output
                        #imgplot=plt.matshow((layer_output1.T))
                        #imgplot.set_cmap('spectral')
                        #plt.colorbar()
                        plt.show()
                        sys.pause()

                if not debug:  ### for attention
                    # with a Sequential model
                    #md.summary()
                    print na
                    get_3rd_layer_output = K.function(
                        [md.layers[0].input,
                         K.learning_phase()], [md.layers[6].output])
                    layer_output = get_3rd_layer_output([te_X1, 0])[0]
                    print layer_output.shape
                    #layer_output1=layer_output[:,:]
                    if line_n == 3:
                        #layer_output=np.mean(layer_output[:,:],axis=1)
                        #layer_output=layer_output[0,:,7]
                        imgplot1, = plt.plot(layer_output[0, :])
                        #print     layer_output
                        #imgplot=plt.matshow((layer_output1.T))
                        #imgplot.set_cmap('spectral')
                        #plt.colorbar()
                        plt.xlabel('Frame number')
                        plt.ylabel('Attention factor')
                        plt.show()
                        sys.pause()

                #p_y_pred = md.predict( [te_X1,te_X2] )
                p_y_pred = md.predict(te_X1)
                p_y_pred = np.mean(p_y_pred, axis=0)  # shape:(n_label)
                #print p_y_pred.shape
                pred = np.zeros(n_labels)
                pred[np.where(p_y_pred > thres)] = 1
                ind = 0
                for la in cfg.labels:
                    if la == 'S':
                        break
                    elif la == 'c':
                        y_true_file_c.append(na)
                        y_true_binary_c.append(y[ind])
                    elif la == 'm':
                        y_true_file_m.append(na)
                        y_true_binary_m.append(y[ind])
                    elif la == 'f':
                        y_true_file_f.append(na)
                        y_true_binary_f.append(y[ind])
                    elif la == 'v':
                        y_true_file_v.append(na)
                        y_true_binary_v.append(y[ind])
                    elif la == 'p':
                        y_true_file_p.append(na)
                        y_true_binary_p.append(y[ind])
                    elif la == 'b':
                        y_true_file_b.append(na)
                        y_true_binary_b.append(y[ind])
                    elif la == 'o':
                        y_true_file_o.append(na)
                        y_true_binary_o.append(y[ind])
                    result = [na, la, p_y_pred[ind]]
                    result_roll.append(result)
                    ind = ind + 1

                pred_roll.append(pred)
                gt_roll.append(y)

    pred_roll = np.array(pred_roll)
    gt_roll = np.array(gt_roll)
    #write csv for EER computation
    csvfile = file('result.csv', 'wb')
    writer = csv.writer(csvfile)
    #writer.writerow(['fn','label','score'])
    writer.writerows(result_roll)
    csvfile.close()

    # calculate prec, recall, fvalue
    prec, recall, fvalue = prec_recall_fvalue(pred_roll, gt_roll, thres)
    # EER for each tag : [ 'c', 'm', 'f', 'v', 'p', 'b', 'o', 'S' ]
    EER_c = eer.compute_eer('result.csv', 'c',
                            dict(zip(y_true_file_c, y_true_binary_c)))
    EER_m = eer.compute_eer('result.csv', 'm',
                            dict(zip(y_true_file_m, y_true_binary_m)))
    EER_f = eer.compute_eer('result.csv', 'f',
                            dict(zip(y_true_file_f, y_true_binary_f)))
    EER_v = eer.compute_eer('result.csv', 'v',
                            dict(zip(y_true_file_v, y_true_binary_v)))
    EER_p = eer.compute_eer('result.csv', 'p',
                            dict(zip(y_true_file_p, y_true_binary_p)))
    EER_b = eer.compute_eer('result.csv', 'b',
                            dict(zip(y_true_file_b, y_true_binary_b)))
    EER_o = eer.compute_eer('result.csv', 'o',
                            dict(zip(y_true_file_o, y_true_binary_o)))
    EER = (EER_c + EER_m + EER_v + EER_p + EER_f + EER_b + EER_o) / 7.0
    print prec, recall, fvalue
    print EER_c, EER_m, EER_f, EER_v, EER_p, EER_b, EER_o
    print EER