def SaveEstimatedLabelsFramewise(est, filename, feature=None):
    f = open(filename, "w")
    cur_label = est[0]
    st = 0
    ed = 1
    for i in range(est.size):
        if est[i] != cur_label:
            ed = i
            if feature is not None:
                feat = feature[st:ed, :]
                sign = voc.ChordSignature7thbass(cur_label,
                                                 feat,
                                                 sevenths=True,
                                                 inv=True)
            else:
                sign = voc.ChordSignature(cur_label)
            text = "%.4f %.4f %s\n" % (float(st * const.H) / const.SR,
                                       float(ed * const.H) / const.SR, sign)
            f.write(text)
            cur_label = est[i]
            st = i
    if feature is not None:
        feat = feature[st:est.size, :]
        text = "%.4f %.4f %s" % (float(st * const.H) / const.SR,
                                 float(est.size * const.H) / const.SR,
                                 voc.ChordSignature7thbass(cur_label, feat))
    else:
        text = "%.4f %.4f %s" % (float(st * const.H) / const.SR,
                                 float(est.size * const.H) / const.SR,
                                 voc.ChordSignature(cur_label))
    f.write(text)
    f.close()
def EvaluateChord(idx, verbose=True, sonify=False, cross=False):
    lablist = np.array(find_files(const.PATH_CHORDLAB, ext=["lab",
                                                            "chords"]))[idx]
    est_lablist = np.array(find_files(
        const.PATH_ESTIMATE_CROSS, ext="lab"))[idx] if cross else find_files(
            const.PATH_ESTIMATE, ext="lab")
    scorelist_majmin = np.array([])
    scorelist_sevenths = np.array([])
    scorelist_majmininv = np.array([])
    scorelist_seventhinv = np.array([])
    durations = np.array([])
    confmatrix = np.zeros((const.N_CHORDS, const.N_CHORDS))
    song_durations = np.array([])
    for labfile, estfile in zip(lablist, est_lablist):
        (ref_intervals,
         ref_labels) = mir_eval.io.load_labeled_intervals(labfile)
        (est_intervals,
         est_labels) = mir_eval.io.load_labeled_intervals(estfile)
        est_intervals, est_labels = mir_eval.util.adjust_intervals(
            est_intervals, est_labels, ref_intervals.min(),
            ref_intervals.max(), mir_eval.chord.NO_CHORD,
            mir_eval.chord.NO_CHORD)
        (intervals, ref_labels,
         est_labels) = mir_eval.util.merge_labeled_intervals(
             ref_intervals, ref_labels, est_intervals, est_labels)
        durations = mir_eval.util.intervals_to_durations(intervals)

        comparisons_sevenths = mir_eval.chord.sevenths(ref_labels, est_labels)
        comparisons_majmininv = mir_eval.chord.majmin_inv(
            ref_labels, est_labels)
        comparisons_seventhinv = mir_eval.chord.sevenths_inv(
            ref_labels, est_labels)
        comparisons_majmin = mir_eval.chord.majmin(ref_labels, est_labels)

        score_majmin = mir_eval.chord.weighted_accuracy(
            comparisons_majmin, durations)
        scorelist_majmin = np.append(scorelist_majmin, score_majmin)
        score_sevenths = mir_eval.chord.weighted_accuracy(
            comparisons_sevenths, durations)
        scorelist_sevenths = np.append(scorelist_sevenths, score_sevenths)
        score_majmininv = mir_eval.chord.weighted_accuracy(
            comparisons_majmininv, durations)
        scorelist_majmininv = np.append(scorelist_majmininv, score_majmininv)
        score_seventhinv = mir_eval.chord.weighted_accuracy(
            comparisons_seventhinv, durations)
        scorelist_seventhinv = np.append(scorelist_seventhinv,
                                         score_seventhinv)
        if verbose:
            print("%s --- %.3f" % (labfile.split('/')[-1], score_majmin))

        for i in range(len(ref_labels)):
            confmatrix[voc.GetChordIDSign(ref_labels[i]),
                       voc.GetChordIDSign(est_labels[i])] += durations[i]
        song_durations = np.append(song_durations, np.sum(durations))
    return scorelist_majmin, scorelist_sevenths, scorelist_majmininv, scorelist_seventhinv, confmatrix, song_durations
def TrainConvnetExtractorDeepChroma(trainidx,
                                    epoch=20,
                                    saveas="convnet.model"):
    cqtfilelist = np.array(find_files(const.PATH_HCQT, ext="npy"))[trainidx]
    labfilelist = np.array(
        find_files(const.PATH_CHORDLAB, ext=["lab", "chords"]))[trainidx]
    #midifilelist = find_files(const.PATH_MIDI,ext="mid")[:filecnt]
    config.train = True
    config.enable_backprop = True
    convnet = networks.ConvnetFeatExtractor()
    model = networks.ConvnetPredictor(convnet)
    model.to_gpu(0)
    opt = optimizers.MomentumSGD()
    opt.setup(model)
    print("DeepChroma Convnet Training...")
    print("start epochs...")
    S = []
    T = []

    for cqtfile, labfile in zip(cqtfilelist, labfilelist):
        cqt = np.load(cqtfile)
        spec = utils.PreprocessSpec(cqt[:const.CQT_H, :, :])
        targ = voc.LoadChromaTarget(labfile)
        minlen = min([cqt.shape[1], targ.shape[0]])
        S.append(spec[:, :minlen, :])
        T.append(targ[:minlen, :])
    S = np.concatenate(S, axis=1)
    T = np.concatenate(T, axis=0)
    assert (S.shape[1] == T.shape[0])

    for ep in range(epoch):
        sum_loss = 0

        randidx = np.random.randint(0,
                                    S.shape[1] - const.CONV_TRAIN_SEQLEN - 1,
                                    S.shape[1] // const.CONV_TRAIN_SEQLEN * 4)
        for i in range(0, randidx.size - const.CONV_TRAIN_BATCH,
                       const.CONV_TRAIN_BATCH):
            x_batch = np.stack([
                S[:, randidx[j]:randidx[j] + const.CONV_TRAIN_SEQLEN, :]
                for j in range(i, i + const.CONV_TRAIN_BATCH)
            ])
            t_batch = np.stack([
                T[randidx[j]:randidx[j] + const.CONV_TRAIN_SEQLEN, :]
                for j in range(i, i + const.CONV_TRAIN_BATCH)
            ])
            x_in = cp.asarray(x_batch)
            t_in = cp.asarray(t_batch)
            model.cleargrads()
            loss = model(x_in, t_in)
            loss.backward()
            opt.update()
            sum_loss += loss.data

        convnet.save(saveas)
        print("epoch: %d/%d  loss:%.04f" %
              (ep + 1, epoch, sum_loss / const.CONV_TRAIN_BATCH))

    convnet.save(saveas)
def LoadLabelArr(labfile, hop_size=const.H):
    f = open(labfile)
    line = f.readline()
    labarr = np.zeros(800 * const.SR // hop_size, dtype="int32")
    while line != "" and line.isspace() == False:
        items = line.split()
        st = int(round(float(items[0]) * const.SR / hop_size))
        ed = int(round(float(items[1]) * const.SR / hop_size))
        lab = voc.GetChordIDSign(items[2])
        labarr[st:ed] = lab
        line = f.readline()
    return labarr[:ed]
def ConfMatrix(idx, cross=False):
    lablist = np.array(find_files(const.PATH_CHORDLAB, ext=["lab",
                                                            "chords"]))[idx]
    est_lablist = np.array(find_files(
        const.PATH_ESTIMATE_CROSS, ext="lab"))[idx] if cross else find_files(
            const.PATH_ESTIMATE, ext="lab")
    durations = np.array([])
    confmatrix_chord = np.zeros((const.N_CHORDS, const.N_CHORDS))
    confmatrix_quality = np.zeros((3, 3))
    confmatrix_bass = np.zeros((3, 3))
    for labfile, estfile in zip(lablist, est_lablist):
        (ref_intervals,
         ref_labels) = mir_eval.io.load_labeled_intervals(labfile)
        (est_intervals,
         est_labels) = mir_eval.io.load_labeled_intervals(estfile)
        est_intervals, est_labels = mir_eval.util.adjust_intervals(
            est_intervals, est_labels, ref_intervals.min(),
            ref_intervals.max(), mir_eval.chord.NO_CHORD,
            mir_eval.chord.NO_CHORD)
        (intervals, ref_labels,
         est_labels) = mir_eval.util.merge_labeled_intervals(
             ref_intervals, ref_labels, est_intervals, est_labels)
        durations = mir_eval.util.intervals_to_durations(intervals)
        for i in range(len(ref_labels)):
            confmatrix_chord[voc.GetChordIDSign(ref_labels[i]),
                             voc.GetChordIDSign(est_labels[i])] += durations[i]
            ref_b = voc.GetBassFromSign(ref_labels[i])
            est_b = voc.GetBassFromSign(est_labels[i])
            if voc.TriadIsequal(
                    ref_labels[i],
                    est_labels[i]) and (ref_b >= 0) and (est_b >= 0):
                confmatrix_bass[ref_b, est_b] += durations[i]
            ref_q = voc.GetQualityFromSign(ref_labels[i])
            est_q = voc.GetQualityFromSign(est_labels[i])
            if voc.TriadIsequal(
                    ref_labels[i],
                    est_labels[i]) and (ref_q >= 0) and (est_q >= 0):
                confmatrix_quality[ref_q, est_q] += durations[i]
    return confmatrix_chord, confmatrix_quality, confmatrix_bass
def TrainTranscribeDNNChord(idx, epoch=20, saveas="dnn_deepchroma.model"):
    cqtfilelist = np.array(find_files(const.PATH_HCQT, ext="npy"))[idx]
    chordlablist = np.array(
        find_files(const.PATH_CHORDLAB, ext=["lab", "chords"]))[idx]

    featurelist = []
    targetlist = []
    chainer.config.train = True
    chainer.config.enable_backprop = True

    for cqtfile, labfile in zip(cqtfilelist, chordlablist):
        cqt = np.load(cqtfile)[0, :, :]
        chroma = voc.LoadChromaTarget(labfile)
        min_sz = min([cqt.shape[0], chroma.shape[0]])
        cqt = utils.Embed(utils.PreprocessSpec(cqt[:min_sz]), size=7)
        chroma = chroma[:min_sz]
        featurelist.append(cqt)
        targetlist.append(chroma.astype(np.int32))
    featurelist = np.concatenate(featurelist)
    targetlist = np.concatenate(targetlist)
    itemcnt = targetlist.shape[0]
    print("DNN Training begin...")
    dnn = networks.FeatureDNN()
    dnn.train = True
    model = networks.DNNModel(predictor=dnn)
    model.to_gpu()
    opt = optimizers.AdaDelta()
    opt.setup(model)
    for ep in range(epoch):
        randidx = np.random.permutation(itemcnt)
        sumloss = 0.0
        for i in range(0, itemcnt, const.DNN_TRAIN_BATCH):
            X = cp.asarray(featurelist[randidx[i:i + const.DNN_TRAIN_BATCH]])
            T = cp.asarray(targetlist[randidx[i:i + const.DNN_TRAIN_BATCH]])
            opt.update(model, X, T)
            sumloss += model.loss.data * const.DNN_TRAIN_BATCH
        print("epoch %d/%d  loss=%.3f" % (ep + 1, epoch, sumloss / itemcnt))

    dnn.save(saveas)