def play_notes(roll, loc, trackname): """ Converts the notes array to pypianoroll format and then using MultiTrack object, the midi file is written to the file system :return: """ pianoroll = np.zeros((roll.shape[1] * 3, 128)) for i in range(roll.shape[0]): note_track = np.array(roll[i, :]) note_track += 1 notes_pos = np.nonzero(note_track) f = 3 for pos in notes_pos[0]: # print("Error", f * pos, f * pos + self.dura_to_timecell(note_track[pos] - 1),self.dura_to_timecell(note_track[pos] - 1)) pianoroll[f * pos:f * pos + dura_to_timecell(note_track[pos] - 1) + 1, 83 - i] = 90 tracks = [] tracker = Track(pianoroll=pianoroll, program=1) tracks.append(tracker) multitrack = Multitrack(tracks=tracks) # print(save_time, ) if len(np.nonzero(pianoroll)[0]) > 0: # for preventing writing empty midi tracks multitrack.write(loc + trackname)
def pianoroll2Midi(piano_roll: np.array, name="test_midi", dir="../output/", velocity=False, # True if the input array contains velocity info (means not binary but continuous) dictionary_dict = None # True if the using the dic mode ): if dictionary_dict is None: if velocity: piano_roll = np.floor(piano_roll * CONFIG['velocity_high']) piano_roll = np.clip(piano_roll, a_min=CONFIG['velocity_low'], a_max=CONFIG['velocity_high']) else: # fill in the default velocity piano_roll = np.where(piano_roll == 1, CONFIG['velocity'], piano_roll) else: # making sure this they are all one-hot vecotr (time_length, 128) # if not np.all(np.sum(piano_roll, axis=1) == 1): # raise ValueError("This is not one-hot vector") piano_roll = embedding2pianoroll(piano_roll, dictionary_dict) make_sure_path_exists(dir) track = Track(piano_roll, is_drum=False, name="piano") multi_track = Multitrack(tracks=[track], tempo=CONFIG['tempo'], downbeat=None, beat_resolution=CONFIG['beat_resolution'], ) file_name = os.path.join(dir, name if name.endswith(".mid") else name+".mid") multi_track.write(file_name)
def notes_to_midi(self, run_folder, output, filename=None): # 배치를 미디로 변환 binarized = output > 0 # 1. 이진화 # 1. reshpae (마디 부분합치기) score = binarized.reshape(-1, binarized.shape[1] * binarized.shape[2], binarized.shape[3], binarized.shape[4]) # 2. 마디 합치기 # 2. pad설정 pad_width = ((0, 0), (0, 0), (24, 20), (0, 0)) # 3. pad적용 score = np.pad(score, pad_width, 'constant') # 3. 패드 적용 # 4. reshape(-1, pitches, Track) score = score.reshape(-1, score.shape[2], score.shape[3]) # 4. reshape ( -1, 피치 수, 트랙) # 5. multitrack 생성 multitrack = Multitrack(beat_resolution=24, tempo=120) # 4.4. Multitrack 생성 ''' for j in range(score.shape[2]) :#4.5. 트랙수만큼 반복한다. track = Track(score[..., j])#4.5.1. Track 생성(painorools[..., idx], program_nums, is_drums) multitrack.append_track(track)#4.5.2. Multitrack 에 추가한다. ''' track = Track(score[..., 0]) multitrack.append_track(track) if run_folder != None: multitrack.write('{}/{}.mid'.format(run_folder, filename)) else: multitrack.write('{}.mid'.format('sample'))
def write_midi(filepath, pianorolls, config): is_drums = config['is_drums'] track_names = config['track_names'] tempo = config['tempo'] beat_resolution = config['beat_resolution'] program_nums = config['program_nums'] if not np.issubdtype(pianorolls.dtype, np.bool_): raise TypeError("Support only binary-valued piano-rolls") if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def main(): if len(sys.argv) < 2: print("Please include experiment directory path.") return 1 elif not os.path.exists(sys.argv[1]): print("Experiment path does not exist.") return 1 else: exp_path = sys.argv[1] # for t in ['fake_x_bernoulli_sampling', 'fake_x_hard_thresholding']: # for i in range(10): # result_path = os.path.join(exp_path, 'results/inference/pianorolls/{}/{}_{}.npz'.format(t, t, i)) # m = Multitrack(result_path) # m.write(os.path.join(exp_path, 'results/inference/pianorolls/{}/result_{}.mid'.format(t, i))) # print(t, i) method = 'fake_x_hard_thresholding' for i in range(10): result_path = os.path.join( exp_path, 'results/inference/pianorolls/{}/{}_{}.npz'.format( method, method, i)) m = Multitrack(result_path) m.write( os.path.join( exp_path, 'results/inference/pianorolls/{}/result_{}.mid'.format( method, i))) print(method, i)
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=120.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. Arguments --------- filepath : str Path to save the MIDI file. pianorolls : np.array, ndim=3 The piano-roll array to be written to the MIDI file. Shape is (num_timestep, num_pitch, num_track). program_nums : int or list of int MIDI program number(s) to be assigned to the MIDI track(s). Available values are 0 to 127. Must have the same length as `pianorolls`. is_drums : list of bool Drum indicator(s) to be assigned to the MIDI track(s). True for drums. False for other instruments. Must have the same length as `pianorolls`. track_names : list of str Track name(s) to be assigned to the MIDI track(s). """ if not np.issubdtype(pianorolls.dtype, np.bool_): raise TypeError("Support only binary-valued piano-rolls") if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if pianorolls.shape[2] != len(program_nums): raise ValueError("`pianorolls` and `program_nums` must have the same" "length") if pianorolls.shape[2] != len(is_drums): raise ValueError("`pianorolls` and `is_drums` must have the same" "length") if program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=120.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. Arguments --------- filepath : str Path to save the MIDI file. pianorolls : np.array, ndim=3 The piano-roll array to be written to the MIDI file. Shape is (num_timestep, num_pitch, num_track). program_nums : int or list of int MIDI program number(s) to be assigned to the MIDI track(s). Available values are 0 to 127. Must have the same length as `pianorolls`. is_drums : list of bool Drum indicator(s) to be assigned to the MIDI track(s). True for drums. False for other instruments. Must have the same length as `pianorolls`. track_names : list of str Track name(s) to be assigned to the MIDI track(s). """ if not np.issubdtype(pianorolls.dtype, np.bool_): raise TypeError("Support only binary-valued piano-rolls") if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if pianorolls.shape[2] != len(program_nums): raise ValueError("`pianorolls` and `program_nums` must have the same" "length") if pianorolls.shape[2] != len(is_drums): raise ValueError("`pianorolls` and `is_drums` must have the same" "length") if program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(filepath)
def writeToMidi(self, midiPath): """Saves the piano roll data in a midi file. Parameters ---------- midiPath : str Folder in which the corresponding midi data is saved. """ tempTrack = StandardTrack(pianoroll=self.pianoroll, program=0, is_drum=False, name=self.name) tempMulti = Multitrack(tracks=(tempTrack, )) tempMulti.write(midiPath)
def save_midi(file_path, stacked_piano_rolls, program_nums=None, is_drums=None, track_names=None, tempo=80.0, beat_resolution=24): """ Write the given piano-roll(s) to a single MIDI file. :param file_path: :param stacked_piano_rolls: np.ndarray, shape=(num_time_step, 128, num_track) :param program_nums: :param is_drums: :param track_names: :param tempo: :param beat_resolution: :return: """ # Check arguments # if not np.issubdtype(stacked_piano_rolls.dtype, np.bool_): # raise TypeError("Support only binary-valued piano-rolls") if stacked_piano_rolls.shape[2] != len(program_nums): raise ValueError( "`stacked_piano_rolls.shape[2]` and `program_nums` must have be the same" ) if stacked_piano_rolls.shape[2] != len(is_drums): raise ValueError( "`stacked_piano_rolls.shape[2]` and `is_drums` must have be the same" ) if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if program_nums is None: program_nums = [0] * len(stacked_piano_rolls) if is_drums is None: is_drums = [False] * len(stacked_piano_rolls) # Write midi multitrack = Multitrack(tempo=tempo, beat_resolution=beat_resolution) for idx in range(stacked_piano_rolls.shape[2]): if track_names is None: track = Track(stacked_piano_rolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(stacked_piano_rolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) multitrack.write(file_path)
def saveMidiFile(self, midiFileName, divider=16): print('saveMidiFile') length = sum(item[1] for item in self.humming) pianoRoll = np.zeros((length * divider, 128), dtype=np.int8) time = 0 for (p, l) in self.humming: pianoRoll[time:time + l * divider - 1, p] = 100 time += l * divider track = Track(pianoroll=pianoRoll, program=0, name='') multitrack = Multitrack(tracks=[track], beat_resolution=2 * divider, tempo=round(30 / self.sampleStepLength / self.beatResolution)) print("sampleStepLength :", self.sampleStepLength) print("beatResolution :", self.beatResolution) multitrack.write(midiFileName)
def gen_song(tempo, title, plot_proll=False): print('\n GENERATING SONG...') noise = np.random.normal(0, 1, (1, latent_space)) print(noise) new_song = playnotes( np.array(decoder([noise])).reshape(parts * seq_len, 128)) * 100 track = Track(new_song, name='test') if plot_proll: track.plot() plt.show() multitrack = Multitrack(tracks=[track], beat_resolution=beat_resolution, tempo=tempo) multitrack.write(title + '.mid') closest_song(noise)
def save_midi(pr, filename): # (num_time_step, 84, 5) > (num_time_step, 128, 5) pr = np.concatenate((np.zeros((pr.shape[0], 24, pr.shape[2])), pr, np.zeros((pr.shape[0], 20, pr.shape[2]))), axis=1) pr = np.around(pr).astype(np.bool_) # binary tracks = list() for idx in range(pr.shape[2]): track = Track(pr[..., idx], dp.program_nums[idx], dp.is_drums[idx], dp.track_names[idx]) tracks.append(track) mt = Multitrack(tracks=tracks, tempo=dp.tempo, beat_resolution=dp.beat_resolution) mt.plot(filename=filename + '.png') plt.close('all') mt.write(filename + '.mid')
def test(): tmp_pypianoroll = Multitrack("./mz_545_1.mid") print(tmp_pypianoroll.tracks[0]) tmp_pypianoroll.remove_empty_tracks() tmp_np = tmp_pypianoroll.get_stacked_pianoroll() tarck1 = tmp_np[:960,:,0] tarck2 = tmp_np[:960,:,1] tarck1 = tarck1 > 0.1 tarck2 = tarck2 > 0.1 tarck1 = Track(tarck1, program=0, is_drum=False, name='unknown') tarck2 = Track(tarck2, program=0, is_drum=False, name='unknown') cc = Multitrack(tracks=[tarck1,tarck2], tempo=120.0,beat_resolution=24) #print(cc) cc.write('test_2.mid')
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=162.0, beat_resolution=16): # if not os.path.exists(filepath): # os.makedirs(filepath) if not np.issubdtype(pianorolls.dtype, np.bool_): raise TypeError("Support only binary-valued piano-rolls") if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if pianorolls.shape[2] != len(program_nums): raise ValueError("`pianorolls` and `program_nums` must have the same" "length") if pianorolls.shape[2] != len(is_drums): raise ValueError("`pianorolls` and `is_drums` must have the same" "length") if program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(beat_resolution=beat_resolution, tempo=tempo) for idx in range(pianorolls.shape[2]): # plt.subplot(10,1,idx+1) # plt.imshow(pianorolls[..., idx].T,cmap=plt.cm.binary, interpolation='nearest', aspect='auto') if track_names is None: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx]) else: track = Track(pianorolls[..., idx], program_nums[idx], is_drums[idx], track_names[idx]) multitrack.append_track(track) # plt.savefig(cf.MP3Name) multitrack.write(filepath)
def write_files_from_states( states, metrics, seed, kernel, f_name="./renderings/midi/t", title="tendril sequence", g=generate_pianoroll, steps=DEFAULT_SEQUENCE_STEPS, beat_duration=DEFAULT_BEAT_DURATION, save_json=False, save_midi=False, debug=False, ): # TODO: make it possible to alter parameters more easily pianoroll = g(states, steps, beat_duration) # Create a `pypianoroll.Track` instance track = Track(pianoroll=pianoroll, program=0, is_drum=False, name=title) mt = Multitrack(tracks=[track]) mid_file = "{f_name}.tendril.{ext}".format(f_name=f_name, ext="mid") json_file = "{f_name}.tendril_states.{ext}".format(f_name=f_name, ext="json") # Write MIDI file if save_midi: print("writing midi file to: ", mid_file) mt.write(mid_file) # Write JSON file states_serialized = [np.int32(s).tolist() for s in states] states_dict = {"states": states_serialized} # print("states", states) if json_file: print("writing tendril states to: {}".format(json_file)) # Save state info as json_file with open(json_file, "w") as json_file: json.dump(states_dict, json_file)
def play_single_note(self, s): """ Plays a single clicked note at a particular point :return: """ maxindex = np.argmax(self.notes[self.selected_track][:, s]) maxdura = self.notes[self.selected_track][:, s][maxindex] + 1 # temp_matrix = np.zeros((48,2**dura)) temp_matrix = np.zeros((int((2**(maxdura))), 128)) for i in range(len(self.notes[self.selected_track][:, s])): note = self.notes[self.selected_track][:, s][i] if note > -1: temp_matrix[0:int((2**(note))), 83 - i] = 90 print(temp_matrix.shape) tracker = Track(pianoroll=temp_matrix, program=self.programs[self.selected_track]) multitrack = Multitrack(tracks=[tracker]) multitrack.write("play_note.mid")
def pianorollToMidi(piano_roll: np.array, name="test_midi", dir="../output/", velocity=False, # True if the input array contains velocity info (means not binary but continuous) dictionary_dict = None # True if the using the dic mode ): if dictionary_dict is None: if velocity: piano_roll = np.floor(piano_roll * CONFIG['velocity_high']) piano_roll = np.clip(piano_roll, a_min=CONFIG['velocity_low'], a_max=CONFIG['velocity_high']) else: # fill in the default velocity piano_roll = np.where(piano_roll == 1, CONFIG['velocity'], 0) else: # making sure this they are all one-hot vecotr (time_length, 128) # if not np.all(np.sum(piano_roll, axis=1) == 1): # raise ValueError("This is not one-hot vector") reverse_dic = {value: key for key, value in dictionary_dict.items()} piano_tmp = np.zeros((len(piano_roll), 128)) # id_array = np.argwhere(piano_roll == 1)[:, 1] for i in range(len(piano_roll)): notes_list = eval(reverse_dic[piano_roll[i]]) if notes_list is None: break elif len(notes_list) == 0: continue else: piano_tmp[i, notes_list] = CONFIG['velocity'] piano_roll = piano_tmp make_sure_path_exists(dir) track = Track(piano_roll, is_drum=False, name="piano") multi_track = Multitrack(tracks=[track], tempo=CONFIG['tempo'], downbeat=None, beat_resolution=CONFIG['beat_resolution'], ) file_name = os.path.join(dir, name if name.endswith(".mid") else name+".mid") multi_track.write(file_name)
def collect_midis(base_dir, collection_dir, selected_tracks=["all"]): """ Collects .npz files from raw data into processed data folders as .mid - selected_track should be a list of track(s) > options: ['Drums', 'Piano', 'Guitar', 'Bass', 'Strings', 'all'] """ if not os.path.exists(collection_dir): print("Creating collection directory %s" % collection_dir) os.mkdir(collection_dir) selected_tracks.sort() # to keep consistency in filename later # Find all of the track name directories track_paths = list(Path(base_dir).rglob('TR*')) for path in tqdm(track_paths, desc='Collecting MIDI files', total=len(track_paths)): for checksum in os.listdir(path): load_dir = os.path.join(path, checksum) multiroll = Multitrack(load_dir) # Remove all tracks but those in selected_tracks if "all" not in selected_tracks: to_remove = [idx for idx, track in enumerate(multiroll.tracks) \ if track.name not in selected_tracks] multiroll.remove_tracks(to_remove) # Make sure our selected tracks persist assert len(multiroll.tracks) == len(selected_tracks) # e.g. save_name = TR#########-bass-piano.mid name = os.path.basename(path) save_name = '{}-{}.mid'.format(name, "-".join(selected_tracks).lower()) save_path = os.path.join(collection_dir, save_name) multiroll.write(save_path)
def write_song(song, path, data_config): """Writes multi-track pianoroll song into a MIDI file. Saves the song using the `pypianoroll` package. Args: song: A song pianoroll array, sized `[time_steps, 128, num_tracks]`. path: A path where the song should be saved. data_config: A data configuration dictionary. """ song = song * 100. instruments = data_config['instruments'] tracks = [] for i in range(len(instruments)): name = instruments[i] track = np.take(song, i, axis=-1) # Manually adjusting the sound if name == 'Piano': track = track * 0.8 elif name == 'Strings': track = track * 0.9 elif name == 'Bass': track = track * 1.2 track = Track( pianoroll=track, program=data_config['programs'][i], is_drum=data_config['is_drums'][i]) tracks.append(track) mtrack = Multitrack( tracks=tracks, tempo=data_config['tempo'], beat_resolution=data_config['beat_resolution']) mtrack.write(path)
def play_notes(self, show_map=False): """ Converts the notes array to pypianoroll format and then using MultiTrack object, the midi file is written to the file system :return: """ # print(self.programs) pianorolls = [] for track in self.notes: pianoroll = np.zeros((track.shape[1] * 3, 128)) for i in range(track.shape[0]): note_track = np.array(track[i, :]) note_track += 1 notes_pos = np.nonzero(note_track) f = 3 for pos in notes_pos[0]: # print("Error", f * pos, f * pos + self.dura_to_timecell(note_track[pos] - 1), # self.dura_to_timecell(note_track[pos] - 1)) pianoroll[f * pos:f * pos + self.dura_to_timecell(note_track[pos] - 1) + 1, 83 - i] = 90 pianorolls.append(pianoroll) # print("pianoroll") print(self.notes_index) tracks = [] for i in range(len(pianorolls)): tracker = Track(pianoroll=pianorolls[i], program=self.programs[i]) tracks.append(tracker) multitrack = Multitrack(tracks=tracks) multitrack.write("create1.mid") if show_map: print("Show map will not work")
name= 'tt_mel') track_tt_cho = Track(pianoroll=gen_32th[:,:,6], program=0, is_drum=False, name= 'tt_cho') track_madmom_cho = Track(pianoroll=gen_32th[:,:,7], program=0, is_drum=False, name= 'madmom_cho') # Create a `pypianoroll.Multitrack` instance multitrack = Multitrack(tracks=[track_bass, track_drum, track_guitar, track_piano, track_string, track_tt_mel, track_tt_cho, track_madmom_cho], tempo=100.0, beat_resolution=4) # Write the `pypianoroll.Multitrack` instance to a MIDI file directory = './exps/nowbar_hybrid/gen_4dbar/all/' if not os.path.exists(directory): os.makedirs(directory) path_o = directory + 'all_%s'%i + '.mid' multitrack.write(path_o) print("----------npy to midi completed !!------------") ####################################### # From midi to velocity dynamic midi # ####################################### directory = './output/' if not os.path.exists(directory): os.makedirs(directory) i = 0 for root, dirs, files in os.walk('./exps/nowbar_hybrid/gen_4dbar/all/', topdown=False): for name in files: # print(i) file = os.path.join(root, name) pm = pretty_midi.PrettyMIDI(file) # print(np.shape(pm.instruments))
def write_to_mid(track, out_file, downbeat=[0, 96, 192, 288]): multitrack = Multitrack(tracks=[track], tempo=120.0, downbeat=downbeat, beat_resolution=24) multitrack.write(out_file)
def pianoroll2midi(pianoroll, file_path): track = Track(pianoroll=pianoroll) multitrack = Multitrack(tracks=[track], tempo=GLOBAL_VAR.MIDI_TEMPO) multitrack.to_pretty_midi() multitrack.write(file_path)
def write_midi(filepath, pianorolls, program_nums=None, is_drums=None, track_names=None, velocity=100, tempo=120.0, beat_resolution=4): """ Write the given piano-roll(s) to a single MIDI file. Arguments --------- 1.filepath : str Path to save the MIDI file. 2.pianorolls : np.array, ndim=3 The piano-roll array to be written to the MIDI file. Shape is (num_timestep, num_pitch, num_track). 3.program_nums : int or list of int MIDI program number(s) to be assigned to the MIDI track(s). Available values are 0 to 127. Must have the same length as `pianorolls`. 4.is_drums : list of bool True for drums. False for other instruments. Must have the same length as `pianorolls`. 5.track_names : str """ if not np.issubdtype(pianorolls.dtype, np.bool_): raise TypeError("Support only binary-valued piano-rolls") if isinstance(program_nums, int): program_nums = [program_nums] if isinstance(is_drums, int): is_drums = [is_drums] if pianorolls.shape[2] != len(program_nums): raise ValueError("`pianorolls` and `program_nums` must have the same" "length") if pianorolls.shape[2] != len(is_drums): raise ValueError("`pianorolls` and `is_drums` must have the same" "length") if program_nums is None: program_nums = [0] * len(pianorolls) if is_drums is None: is_drums = [False] * len(pianorolls) multitrack = Multitrack(resolution=beat_resolution, tempo=np.array([tempo * 1.0] * pianorolls.shape[0])) for idx in range(pianorolls.shape[2]): if track_names is None: tmp = pianorolls[..., idx] below_pad = (128 - tmp.shape[1]) // 2 fore_pad = 128 - below_pad - tmp.shape[1] tmp = np.pad(tmp, ((0, 0), (below_pad, fore_pad)), "constant") track = BinaryTrack(pianoroll=tmp, program=int(program_nums[idx]), is_drum=is_drums[idx]) else: track = BinaryTrack(pianoroll=pianorolls[:, :, idx], program=program_nums[idx], is_drum=is_drums[idx], name=track_names[idx]) multitrack.append(track) multitrack.write(filepath) return multitrack
def main(): # parameters file_root = "./raw_classical_music_data" learning_rate = 0.008 num_epochs = 401 batch_size = 100 input_dim = 100 # fixed input for model eval # rand_inputs = (5,1000,1,1) , pair_inputs = (5,1000,1,1) with torch.no_grad(): rand_inputs = Variable(torch.randn(1, input_dim, 1, 1)) # create the save log file print("Create the directory") if not os.path.exists("./save"): os.makedirs("./save") if not os.path.exists("./logfile"): os.makedirs("./logfile") if not os.path.exists("./logfile/GAN"): os.makedirs("./logfile/GAN") if not os.path.exists("./save_songs"): os.makedirs("./save_songs") if not os.path.exists("./save_songs/GAN"): os.makedirs("./save_songs/GAN") # load my Dataset train_dataset = Dataset(filepath=file_root, num=1000) train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=1) print('the dataset has %d size.' % (len(train_dataset))) print('the batch_size is %d' % (batch_size)) # models setting G_tmp_model = model.Generator_tmp() G_bar_model = model.Generator_bar() D_model = model.Discriminator() # GPU enable use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") print('Device used:', device) if torch.cuda.is_available(): rand_inputs = rand_inputs.to(device) G_tmp_model = G_tmp_model.to(device) G_bar_model = G_bar_model.to(device) D_model = D_model.to(device) # setup optimizer G_tmp_optimizer = optim.Adam(G_tmp_model.parameters(), lr=learning_rate, betas=(0.5, 0.999)) G_bar_optimizer = optim.Adam(G_bar_model.parameters(), lr=learning_rate, betas=(0.5, 0.999)) D_optimizer = optim.Adam(D_model.parameters(), lr=learning_rate, betas=(0.5, 0.999)) criterion_dis = nn.BCELoss() criterion_att = nn.BCELoss() D_loss_list = [] G_loss_list = [] D_real_acc_list = [] D_fake_acc_list = [] D_Real_att_loss_list = [] D_Fake_att_loss_list = [] print("Starting training...") for epoch in range(num_epochs): G_tmp_model.train() G_bar_model.train() D_model.train() print("Epoch:", epoch + 1) epoch_D_loss = 0.0 epoch_G_loss = 0.0 D_real_total_acc = 0.0 D_fake_total_acc = 0.0 D_Real_att_total_loss = 0.0 D_Fake_att_total_loss = 0.0 if (epoch + 1) == 11: G_tmp_optimizer.param_groups[0]['lr'] /= 2 G_bar_optimizer.param_groups[0]['lr'] /= 2 D_optimizer.param_groups[0]['lr'] /= 2 if (epoch + 1) == 20: G_tmp_optimizer.param_groups[0]['lr'] /= 2 G_bar_optimizer.param_groups[0]['lr'] /= 2 D_optimizer.param_groups[0]['lr'] /= 2 if (epoch + 1) == 60: G_tmp_optimizer.param_groups[0]['lr'] /= 2 G_bar_optimizer.param_groups[0]['lr'] /= 2 D_optimizer.param_groups[0]['lr'] /= 2 if (epoch + 1) == 100: G_tmp_optimizer.param_groups[0]['lr'] /= 2 G_bar_optimizer.param_groups[0]['lr'] /= 2 D_optimizer.param_groups[0]['lr'] /= 2 if (epoch + 1) == 150: G_tmp_optimizer.param_groups[0]['lr'] /= 2 G_bar_optimizer.param_groups[0]['lr'] /= 2 D_optimizer.param_groups[0]['lr'] /= 2 for i, data in enumerate(train_loader): batch_size = len(data) real_labels = torch.ones(batch_size) fake_labels = torch.zeros(batch_size) data = Variable(data).to(device) real_labels = Variable(real_labels).to(device) fake_labels = Variable(fake_labels).to(device) # train the Generator tmp G_tmp_model.zero_grad() G_bar_model.zero_grad() z = torch.randn(batch_size, input_dim, 1, 1) z = Variable(z).to(device) fake_song_tmp = G_tmp_model(z) fake_song = G_bar_model(fake_song_tmp) outputs_dis = D_model(fake_song) G_loss = criterion_dis(outputs_dis, real_labels) epoch_G_loss += G_loss.item() G_loss.backward() G_tmp_optimizer.step() G_bar_optimizer.step() # train the Discriminator # BCE_Loss(x, y) = - y * log(D(x)) - (1-y) * log(1 - D(x)) # real images , real_labels == 1 D_model.zero_grad() #################################################### # self test print("num of non-zero :", np.sum(data[0].cpu().data.numpy() != 0.)) #print(data[0].cpu().data.numpy()) #data = data[0].cpu().data.numpy() ## torch.Size([1, 2, 384, 128]) #data = data.transpose(1, 2, 0) #tarck1 = data[:,:,0] #tarck2 = data[:,:,1] #tarck1 = Track(tarck1, program=0, is_drum=False, name='unknown') #tarck2 = Track(tarck2, program=0, is_drum=False, name='unknown') #cc = Multitrack(tracks=[tarck1,tarck2], tempo=120.0,beat_resolution=24) #cc.write('./my_test') #################################################### outputs_dis = D_model(data) D_real_loss = criterion_dis(outputs_dis, real_labels) D_real_acc = np.mean((outputs_dis > 0.5).cpu().data.numpy()) # fake images # First term of the loss is always zero since fake_labels == 0 # we don't want to colculate the G gradient outputs_dis = D_model(fake_song.detach()) D_fake_loss = criterion_dis(outputs_dis, fake_labels) D_fake_acc = np.mean((outputs_dis < 0.5).cpu().data.numpy()) D_loss = (D_real_loss + D_fake_loss) / 2. D_loss.backward() D_optimizer.step() D_real_total_acc += D_real_acc D_fake_total_acc += D_fake_acc epoch_D_loss += D_loss.item() print( 'Epoch [%d/%d], Iter [%d/%d] G loss %.4f, D loss %.4f , LR = %.6f' % (epoch + 1, num_epochs, i + 1, len(train_loader), G_loss.item(), D_loss.item(), learning_rate)) if (epoch) % 50 == 0: save_checkpoint('./save/GAN-G-tmp-%03i.pth' % (epoch), G_tmp_model, G_tmp_optimizer) save_checkpoint('./save/GAN-G-bar-%03i.pth' % (epoch), G_bar_model, G_bar_optimizer) save_checkpoint('./save/GAN-D-%03i.pth' % (epoch), D_model, D_optimizer) # save loss data print("training D Loss:", epoch_D_loss / len(train_loader.dataset)) print("training G Loss:", epoch_G_loss / len(train_loader.dataset)) D_loss_list.append(epoch_D_loss / len(train_loader.dataset)) G_loss_list.append(epoch_G_loss / len(train_loader.dataset)) D_real_acc_list.append(D_real_total_acc / len(train_loader)) D_fake_acc_list.append(D_fake_total_acc / len(train_loader)) # testing if (epoch) % 20 == 0: G_tmp_model.eval() G_bar_model.eval() tmp_output = G_tmp_model(rand_inputs) test_output = G_bar_model(tmp_output) test_song = test_output.cpu().data.numpy() print(test_song) print("test_song num of non-zero :", np.sum(test_song >= 0.2)) # torch.Size([1, 2, 384, 128]) test_song = test_song.transpose(0, 2, 3, 1) tarck1, tarck2 = make_mid(test_song, 0.2) print(tarck1) cc = Multitrack(tracks=[tarck1, tarck2], tempo=120.0, beat_resolution=24) cc.write('./save_songs/GAN/%03d' % (epoch + 1)) # epoch done print('-' * 88)
program=0) # %% # The value of tempo doesn't matter, what's matter is beat_resolution=int(60.0 / tempo * fs) multitrack = Multitrack( tracks=[track], # tempo=np.full(track.pianoroll.shape[0], midi_data.estimate_tempo()), tempo=np.full(track.pianoroll.shape[0], 6000), # beat_resolution=int(60.0 / midi_data.estimate_tempo() * 100) beat_resolution=int(60.0 / 6000 * 100) # tempo=midi_data.get_tempo_changes()[1], ) # %% multitrack.write("temp.mid") # %% test_track = Multitrack("temp.mid", tempo=6000, beat_resolution=int(60.0 / 6000 * 100)) # %% test_midi = test_track.to_pretty_midi() # %% # Play Midi import pygame pygame.init() # %%
def save2midi(song, name, config, params): song = song _save_pianoroll(array=song, suffix=None, name=name, config=config, params=params) m = Multitrack('./src/'+name+'.npz') m.write('./src/'+name+'.mid')
# %% plt.hist(ground_truth[:, 0, :].cpu().view(-1), bins=10, log=True) # %% # torch.save(net.state_dict(), 'Data/Model/test_100_epoch_modified_nn') # %% # Pianoroll to Midi from pypianoroll import Multitrack, Track # %% track = Track(pianoroll=nn.ReLU()(outputs[:, 0, :]).cpu().detach().numpy(), program=0) multitrack = Multitrack(tracks=[track]) multitrack.write(os.path.join(path_to_code, "Data/Temp/temp.mid")) # %% # Play Midi import pygame pygame.init() # %% pygame.mixer.music.load(os.path.join(path_to_code, "Data/Temp/temp.mid")) pygame.mixer.music.play() # %% pygame.mixer.music.stop()