Exemplo n.º 1
0
def get_X(track_id,
          use_mp3,
          use_lyrics,
          use_artist,
          lyrics_d2v_model,
          d_artist_vec,
          music_datatype="mfcc"):
    conn = MyConn()

    rawmusic_path, vggish_embed_path, lyrics_path, artist = conn.query(
        table="sub_tracks",
        conditions={"track_id": track_id},
        fetchall=False,
        targets=[
            "rawmusic_path", "vggish_embed_path", "lyrics_path", "artist"
        ])

    vecs = []
    if use_mp3:
        if music_datatype == "mfcc":
            music_vec = get_mfcc(rawmusic_path).ravel()
        elif music_datatype == "vggish":
            with open(vggish_embed_path, "rb") as f:
                music_vec = pickle.load(f).detach().numpy()
        vecs.append(music_vec)
    if use_lyrics:
        lyrics_vec = get_d2v_vector(lyrics_path, lyrics_d2v_model)
        vecs.append(lyrics_vec)
    if use_artist:
        artist_vec = d_artist_vec[artist.lower().strip()]
        vecs.append(artist_vec)

    features_vec = concatenate_features(vecs)

    return features_vec
Exemplo n.º 2
0
	def __getitem__(self, index):
		'''
		必须自定义,使用index获取成对数据
		'''
		if index<len(self.breakouts):
			label = 1
			id_ = self.breakouts[index]
			track_id, beta = self.conn.query(targets=["track_id", "beta"], table="breakouts",
												conditions={"id":id_})[0]
			feature_words = self.conn.query(targets=["feature_words"], table="breakouts_feature_words_1",
												conditions={"breakout_id":id_})[0][0].split()
		else:
			label = 0
			index = index - len(self.breakouts)
			id_ = self.no_breakouts[index]
			track_id = id_.split('-')[0]
			beta = 1
			feature_words = self.conn.query(targets=["feature_words"], table="no_breakouts_feature_words_1",
												conditions={"id":id_})[0][0].split()

		rawmusic_path, lyrics_path = self.conn.query(
			targets=["rawmusic_path", "lyrics_path"], conditions={"track_id": track_id})[0]

		mfcc = torch.Tensor(get_mfcc(rawmusic_path))
		lyrics_vec = torch.Tensor(get_d2v_vector(lyrics_path, self.d2v_model))

		return label, beta, mfcc, lyrics_vec, feature_words
Exemplo n.º 3
0
def get_X_y_embed(tracks_2_labels, conn, d2v_model, music_feature_extractor,
                  intrinsic_feature_embed):
    # 构建.pkl文件的路径字典
    dir1 = "/Volumes/nmusic/NetEase2020/data/breakouts_rawmusic"
    dir2 = "/Volumes/nmusic/NetEase2020/data/no_breakouts_rawmusic"
    tid_2_mp3path_d = get_tid_2_mp3path_d(dir1)
    tid_2_mp3path_d2 = get_tid_2_mp3path_d(dir2)
    for tid in tid_2_mp3path_d2:
        if tid not in tid_2_mp3path_d:
            tid_2_mp3path_d[tid] = tid_2_mp3path_d2[tid]
    # print(len(tid_2_mp3path_d))

    X = []
    y = []
    flag = 1
    for tid, label in tracks_2_labels.items():
        tid = str(tid)
        try:
            # 获取音频特征向量
            mfcc = torch.tensor(get_mfcc(tid_2_mp3path_d[tid])).unsqueeze(0)
            # 获取歌词特征向量
            lyrics_path = conn.query(targets=["lyrics_path"],
                                     conditions={"track_id": tibd})[0][0]
            lyrics_vec = torch.tensor(
                get_d2v_vector(
                    "/Volumes/nmusic/NetEase2020/data/" + lyrics_path,
                    d2v_model))

            h1 = music_feature_extractor(mfcc).squeeze()
            # print(h1.shape)
            h2 = torch.cat((h1, lyrics_vec))
            # print(h2.shape)
            feature_vec = intrinsic_feature_embed(h2)

            X.append(feature_vec)
            y.append(label)

            print(flag, tid)
            flag += 1
        except KeyboardInterrupt:
            print("interrupted by user.")
            sys.exit(1)
        except:
            print("ERROR", tid)
            print(traceback.format_exc())
            # continue
            # break
    return X, y
Exemplo n.º 4
0
def get_mfcc(signal_rate=100, output_path=None, audio_paths=None):
    """
    get mfcc
    :param sr: sampling rate
    :param output_path:
    :param audio_paths:
    :return:
    """
    audio = audio_paths[0].split('.')[-1]
    for audio_path in audio_paths:
        name = re.findall('{}\/(.*)\.{}'.format(audio, audio), audio_path)[0]
        mfcc_df = utils.get_mfcc(audio_path, signal_rate)
        #mfcc_df = mfcc_df[(mfcc_df.T == 0).any() == False].reset_index(drop=True)
        path = output_path
        if not os.path.exists(path):
            os.makedirs(path)
        mfcc_df.T.to_csv(path + '/{0}.csv'.format(name), index=False)
Exemplo n.º 5
0
    def __getitem__(self, index):
        '''
        必须定义,使用index获取一条完整的数据
        '''
        tid, label = self.data[index]
        rawmusic_path, vggish_embed_path, vggish_examples_path, mel_3seconds_groups_path, lyrics_path, artist, chorus_start = self.conn.query(
            table="sub_tracks", conditions={"track_id":tid}, fetchall=False,
            targets=["rawmusic_path", "vggish_embed_path", "vggish_examples_path", "mel_3seconds_groups_path", "lyrics_path", "artist", "chorus_start"])

        if self.config.MUSIC_DATATYPE=="vggish":
            with open(vggish_embed_path, "rb") as f:
                music_vec = pickle.load(f)
        elif self.config.MUSIC_DATATYPE=="mel":
            music_vec = torch.Tensor(get_melspectrogram(rawmusic_path, config=self.config))
        elif self.config.MUSIC_DATATYPE=="mfcc":
            music_vec = torch.Tensor(get_mfcc(rawmusic_path, config=self.config))
        elif self.config.MUSIC_DATATYPE=="vggish_examples":
            with open(vggish_examples_path, "rb") as f:
                music_vec = pickle.load(f)            
        elif self.config.MUSIC_DATATYPE=="mel_3seconds_groups":
            with open(mel_3seconds_groups_path, "rb") as f:
                music_vec = pickle.load(f)
        lyrics_vec = torch.Tensor(get_d2v_vector(lyrics_path, self.d2v_model))
        artist_vec = torch.Tensor(self.d_artist_vec[artist.lower().strip()])
        # reviews_vec = torch.Tensor(get_reviews_vec(tid, breakout=label, 
        #             w2v_model=self.w2v_model, key=self.config.REVIEWS_VEC_KEY)[:self.config.TOPK])
        # reviews_topk_words = get_reviews_topk_words(tid, is_breakout=label, key="candidates")[:5]
        reviews_vec = torch.Tensor(get_reviews_vec_with_freq(tid, breakout=label, w2v_model=self.w2v_model,
                                    d_breakouts=self.d_breakouts_feature_words, d_no_breakouts=self.d_no_breakouts_feature_words, 
                                    d_pos_track_breakout=self.d_pos_track_breakout, with_freq=False)[:self.config.TOPK])

        item_data = {
            "track_id": tid,
            "label": label,
            "music_vec": music_vec,
            "lyrics_vec": lyrics_vec,
            "artist_vec": artist_vec,
            "reviews_vec": reviews_vec,
            # "reviews_topk_words": reviews_topk_words
        }

        return item_data