def extract_data(): """ Construct the data files :return: 2 List<EP>: files of content and style """ cp = EPath('content') sp = EPath('style') if cp.exists() and sp.exists(): # Check if I have to reconstruct it or not cp_list = cp.listdir(concat=True) sp_list = sp.listdir(concat=True) if len(cp_list) > 0 and len(sp_list) > 0: # Data is already there return cp_list, sp_list else: # Remove everything to do it again cp.rmdir() sp.rmdir() EPath('content').mkdir() EPath('style').mkdir() possible_parents = [ '.', '..' ] # The working folder (my computer) or the parent folder (colab) data_found = False for possible_parent in possible_parents: cp_zip = EPath(possible_parent, 'content.zip') sp_zip = EPath(possible_parent, 'style.zip') if cp_zip.exists() and sp_zip.exists() and not data_found: data_found = True # the zip files are provided in the project folder with ZipFile(cp_zip, 'r') as zip_ref: zip_ref.extractall('./') with ZipFile(sp_zip, 'r') as zip_ref: zip_ref.extractall('./') if not data_found: # no image provided cp = tf.keras.utils.get_file( 'YellowLabradorLooking_new.jpg', 'https://storage.googleapis.com/download.tensorflow.org/example_images/YellowLabradorLooking_new.jpg' ) sp = tf.keras.utils.get_file( 'kandinsky5.jpg', 'https://storage.googleapis.com/download.tensorflow.org/example_images/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg' ) shutil.move(cp, 'content/YellowLabradorLooking_new.jpg') shutil.move(sp, 'style/kandinsky5.jpg') content_path_list = EPath('content').listdir(concat=True) style_path_list = EPath('style').listdir(concat=True) return content_path_list, style_path_list
def get_data(): """ Construct the data files :return: 2 List<EP>: files of content and style """ cp = EPath('content') sp = EPath('style') if cp.exists() and sp.exists(): cp_list = cp.listdir(concat=True) sp_list = sp.listdir(concat=True) if len(cp_list) > 0 and len(sp_list) > 0: # Data is already there return cp_list, sp_list return extract_data()
def eval_songs_folder(path): """ :param path: :return: """ path = EPath(path) files = list( filter(lambda x: x.suffix == '.mid', path.listdir(concat=True))) all_res = {} text = "" res_seed = 0 res_created = 0 for file in files: seed_length = 8 if file.rstem[20:] != "redo_song_generate_3" else None res = eval_song_midi(file, seed_length) all_res[file.rstem] = res h_s = res['harmony_seed'] h_c = res['harmony_created'] if h_s is not None and not np.isnan(h_s): res_seed += res['harmony_seed'] if h_c is not None and not np.isnan(h_c): res_created += h_c text += file.rstem + '\n' for k in res: text += f'\t{k}: {res[k]}\n' plots.plot_res((file.parent / file.rstem + '_harmony_measure').with_suffix('.jpg'), res) res_seed /= len(files) res_created /= len(files) text = f'\t\tRes for generation\n\nMean:\n\tharmony_seed: {res_seed}\n\tharmony_created: {res_created}\n' + text with open(path / 'res.txt', 'w') as f: f.write(text) with open(path / 'res.p', 'wb') as dump_file: pickle.dump(all_res, dump_file)
def get_next_files(content_path_list, style_path_list): """ :param content_path_list: List of the content_path :param style_path_list: List of the style_path :return: """ for content_path in content_path_list: for style_path in style_path_list: result_path = EPath('results', content_path.stem, style_path.stem) if not result_path.exists(): return content_path, style_path, result_path, var.p.image_start[ 0] else: files = result_path.listdir(t='str') for img_start in var.p.image_start: if not functools.reduce( lambda x, y: x or y.startswith(img_start), files, False): return content_path, style_path, result_path, img_start return None, None, None, None
def get_next_files(content_path_list, style_path_list, image_start=options.image_start, data_path=None): """ :param content_path_list: List of the content_path :param style_path_list: List of the style_path :return: """ for content_path in content_path_list: for style_path in style_path_list: result_path = EPath('results', content_path.stem, style_path.stem) start_path_list = get_start_path_list(content_path=content_path, style_path=style_path, image_start=image_start, data_path=data_path) if not result_path.exists(): return FileCombination(content_path=content_path, style_path=style_path, start_path=start_path_list[0], n=0) else: files = result_path.listdir( t='str') # existing files in the result folder for start_path in start_path_list: for p in range(param.length): file_combination = FileCombination( content_path=content_path, style_path=style_path, start_path=start_path, n=p) if not functools.reduce( lambda x, y: x or y.startswith( file_combination.result_stem), files, False): return file_combination return None
res = eval_song_midi(file, seed_length) all_res[file.rstem] = res h_s = res['harmony_seed'] h_c = res['harmony_created'] if h_s is not None and not np.isnan(h_s): res_seed += res['harmony_seed'] if h_c is not None and not np.isnan(h_c): res_created += h_c text += file.rstem + '\n' for k in res: text += f'\t{k}: {res[k]}\n' plots.plot_res((file.parent / file.rstem + '_harmony_measure').with_suffix('.jpg'), res) res_seed /= len(files) res_created /= len(files) text = f'\t\tRes for generation\n\nMean:\n\tharmony_seed: {res_seed}\n\tharmony_created: {res_created}\n' + text with open(path / 'res.txt', 'w') as f: f.write(text) with open(path / 'res.p', 'wb') as dump_file: pickle.dump(all_res, dump_file) if __name__ == '__main__': path = EPath('..', '..', '..', 'big_runs', 'lstm') for folder in path.listdir(concat=True): if not folder.is_dir(): continue midi_folder = folder / 'generated_midis' midi_folder /= midi_folder.listdir(concat=False)[0] eval_songs_folder(midi_folder)