def random_select_data_as_noise_for_pred(cur_file, all_files, random_seed=None): results = [] # start_time = time.time() # random.seed(random_seed) cur_speaker = os.path.basename(get_parent_dir(cur_file['path'])) # print(type(all_files)) # print(len(all_files)) chosen = random.choice(all_files) # i = np.random.randint(0, len(all_files)) # faster? # chosen = all_files[i] chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) while cur_speaker == chosen_speaker: chosen = random.choice(all_files) chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) assert cur_speaker != chosen_speaker results.append(chosen) remaining_duration = cur_file['duration'] - chosen['duration'] while remaining_duration > -1: # just to be safe chosen = random.choice(all_files) chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) while cur_speaker == chosen_speaker: chosen = random.choice(all_files) chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) assert cur_speaker != chosen_speaker results.append(chosen) remaining_duration = remaining_duration - chosen['duration'] # print(cur_speaker, chosen_speaker) # print("--- %s seconds in randselect ---" % (time.time() - start_time)) # print('safe_len:', safe_len) return results
def random_select_data_as_noise(cur_item, all_files, safe_len=1, random_seed=None): # start_time = time.time() # random.seed(random_seed) # construct choice (data) lists - list of tuples # item[0]: video clip index # item[1]: data first bit's index in video clip # item[2]: data bit stream # item[3]: audio_path # item[4]: framerate cur_speaker = os.path.basename(get_parent_dir(cur_item[3])) # print(type(all_files)) # print(len(all_files)) chosen = random.choice(all_files) # i = np.random.randint(0, len(all_files)) # faster? # chosen = all_files[i] chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) while cur_speaker == chosen_speaker or math.floor( chosen['duration']) < safe_len: chosen = random.choice(all_files) chosen_speaker = os.path.basename(get_parent_dir(chosen['path'])) assert cur_speaker != chosen_speaker assert math.floor(chosen['duration']) >= safe_len # print(cur_speaker, chosen_speaker) # print("--- %s seconds in randselect ---" % (time.time() - start_time)) # print('safe_len:', safe_len) return chosen['audio_path'], random.uniform( 0, math.floor(chosen['duration']) - safe_len ) # -safe_len here because our data is less than safe_len seconds long
def __init__(self, buffer_id, buffer_width, buffer_height): super(BrowserBuffer, self).__init__() self.buffer_id = buffer_id self.buffer_width = buffer_width self.buffer_height = buffer_height # self.setPage(WebPage()) self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page().linkClicked.connect(self.link_clicked) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) self.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(os.path.join(get_parent_dir(__file__), "theme.css"))) self.settings().setAttribute(QWebSettings.PluginsEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True) self.adjust_size(self.buffer_width, self.buffer_height) self.view_dict = {} self.titleChanged.connect(self.change_title) self.press_ctrl_flag = False self.page().mainFrame().initialLayoutCompleted.connect(self.inject_js)
def __init__(self, buffer_id, buffer_width, buffer_height): super(BrowserBuffer, self).__init__() self.buffer_id = buffer_id self.buffer_width = buffer_width self.buffer_height = buffer_height # self.setPage(WebPage()) self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page().linkClicked.connect(self.link_clicked) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) self.settings().setUserStyleSheetUrl( QUrl.fromLocalFile( os.path.join(get_parent_dir(__file__), "theme.css"))) self.settings().setAttribute(QWebSettings.PluginsEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True) self.adjust_size(self.buffer_width, self.buffer_height) self.view_dict = {} self.titleChanged.connect(self.change_title) self.press_ctrl_flag = False
def __init__(self, buffer_id, buffer_width, buffer_height): super(BrowserBuffer, self).__init__() self.buffer_id = buffer_id self.buffer_width = buffer_width self.buffer_height = buffer_height self.setPage(WebPage()) self.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) cookie_jar.restore_cookies() self.page().networkAccessManager().setCookieJar(cookie_jar) self.page().userAgentForUrl = self.customize_user_agent self.settings().setUserStyleSheetUrl(QUrl.fromLocalFile(os.path.join(get_parent_dir(__file__), "theme.css"))) self.settings().setAttribute(QWebSettings.PluginsEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptEnabled, True) self.settings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True) self.settings().setFontFamily(QWebSettings.StandardFont, "Sans") self.adjust_size(self.buffer_width, self.buffer_height) self.view_dict = {} self.titleChanged.connect(self.change_title) self.press_ctrl_flag = False self.loading_flag = False self.loading_percent = 0 self.loadFinished.connect(self.handle_load_finished) self.loadStarted.connect(self.handle_load_started) self.loadProgress.connect(self.handle_load_progress)
def __init__(self,images_dir,scale=0,width=0,height=0,img_ext='bmp'): self.images_dir = images_dir self.scale = scale self.width = width self.height = height self.img_ext = img_ext parent_dir,tail = utils.get_parent_dir(images_dir) self.out_dir = os.path.join(parent_dir,tail+'_scale_'+str(scale)+'/') utils.create_new_empty_dir(self.out_dir) self.start_scale()
def __init__(self,images_dir,ratio_limit=(0.5,2),min_width=128,min_height=128,img_ext='bmp'): self.images_dir = images_dir self.ratio_limit = ratio_limit # 宽高比 ratio = w/h self.min_width = min_width self.min_height = min_height self.img_ext = img_ext parent_dir,tail = utils.get_parent_dir(images_dir) self.out_dir = os.path.join(parent_dir,tail+'_sifted_w'+str(self.min_width)+'_h'+str(self.min_height)+'/') utils.create_new_empty_dir(self.out_dir) self.start_sift()
def export_image_and_label_selfdefine(labels_folder_path,images_folder): parent_dir, _ = utils.get_parent_dir(labels_folder_path) target_dir = utils.create_new_empty_dir(parent_dir + '/images/') file_list = utils.get_dir_filelist_by_extension(dir=labels_folder_path, ext='xml') for file in file_list: tree = ET.ElementTree(file=file) for elem in tree.iter(tag='filename'): file_name = elem.text full_path = images_folder+'/'+file_name try: shutil.copy2(src=full_path, dst=target_dir + '/' + file_name) except: print(full_path + '复制失败')
def change_tag_text(file_list, target_dir): for file in file_list: tree = ET.ElementTree(file=file) root = tree.getroot() # print(root.tag) # print(root.attrib) print(file) for elem in tree.iter(tag='path'): # file_name = elem.text # index = file_name.find('.') # ext = file_name[index:] _, fn = utils.get_parent_dir(file) fn = fn.replace('xml', 'bmp') elem.text = '' # print(elem.text) tree.write(target_dir + os.path.basename(file))
def reloadRoot(self, fullpath): self.clear() tail = '' pname = self.getModuleName(fullpath) for i, name in enumerate(reversed(pname.split('.'))): if name == '__init__': continue tail = '.'.join([name, tail]) if tail else name path = None if i <= 0 else utils.get_parent_dir(fullpath, i) file = os.path.join(path, '__init__.py') if path else fullpath m = ast.parse(open(file).read()) self.builder.build(m, tail, file, path, is_rely=False) self.modules[tail] = m if not self.root: self.root = m return self.root
def validate_path(path): """ This function validates the path to the directory provided bby the user. The default is the current working directory. :Arguments: 1. path (str) = user input that contains path to the directory. :Returns: 1. path (str) = Validated path to the directory. """ if path == "": current_dir = get_parent_dir(os.path.realpath(__file__)) print "Path: " + current_dir return current_dir if not os.path.exists(path): path = raw_input("Invalid path! Please provide a correct path to " "the folder: ") path = validate_path(path) return path
import sys import time import re import os import tensorflow as tf import numpy as np from tensorflow.keras.models import Model from tensorflow.keras.layers import Conv2D, BatchNormalization, LeakyReLU, ZeroPadding2D, UpSampling2D, Concatenate, Add, Input, Lambda from tensorflow.keras.optimizers import Adam from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping from yolo_loss_function import yolo_loss from utils import data_generator_wrapper, get_parent_dir, change_to_new_machine CONFIG_FILE = "yolo_config.txt" DARKNET_WEIGHTS_FILE = "darknet_weights.h5" ANNOTATIONS_FILE = os.path.join(get_parent_dir(0), "training_images", "vott-csv-export", "data_train.txt") CLASSES_FILE = os.path.join(get_parent_dir(0), "data_classes.txt") ANCHORS_FILE = os.path.join(get_parent_dir(0), "yolo_anchors.txt") LOG_DIR = os.path.join(get_parent_dir(0), "log_dir") LOG_DIR_TIME = os.path.join(LOG_DIR, "{}".format(int(time()))) VAL_PERCENT = 0.1 def main(): anchors, num_anchors = get_anchors() num_classes = get_classes() annotation_lines = [line.strip(" ") for line in open(ANNOTATIONS_FILE)] change_to_new_machine(annotation_lines, repo="yolo", remote_machine="") obj_list = parse_config(num_anchors, num_classes) generate_model(anchors, num_classes, annotation_lines, obj_list)
def __init__(self, target_dir): parent_dir, tail = utils.get_parent_dir(target_dir) self.out_dir = parent_dir + '/' + tail + '_centercut/' utils.create_new_empty_dir(self.out_dir) self.load_images(target_dir) self.start_crop()
def load_qml(self, filename): qml_path = os.path.join(get_parent_dir(__file__), "qml", filename) self.setSource(QtCore.QUrl(qml_path))
# This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import gtk from accounts import AccountView import utils DATA_DIR = os.path.join(utils.get_parent_dir(__file__, 2), "data") class TiebaWindow(gtk.Window): def __init__(self): gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL) self.set_size_request(600, 450) self.set_position(gtk.WIN_POS_CENTER) self.connect("delete-event", gtk.main_quit) self.accounts_view = AccountView() self.__init_accounts() scrolled_window = gtk.ScrolledWindow() scrolled_window.add(self.accounts_view) self.add(scrolled_window) self.show_all()
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import gtk from accounts import AccountView import utils DATA_DIR = os.path.join(utils.get_parent_dir(__file__, 2), "data") class TiebaWindow(gtk.Window): def __init__(self): gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL) self.set_size_request(600, 450) self.set_position(gtk.WIN_POS_CENTER) self.connect("delete-event", gtk.main_quit) self.accounts_view = AccountView() self.__init_accounts() scrolled_window = gtk.ScrolledWindow() scrolled_window.add(self.accounts_view) self.add(scrolled_window) self.show_all()
# # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import gtk import cairo import webkit import webbrowser import javascriptcore as jscore from sina import Sina from utils import parse_sina_datetime, get_parent_dir from constant import MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT static_dir = os.path.join(get_parent_dir(__file__, 2), "static", "html") class PopupWindow(gtk.Window): def __init__(self): gtk.Window.__init__(self) self.set_position(gtk.WIN_POS_CENTER) self.set_colormap(gtk.gdk.Screen().get_rgba_colormap()) self.set_size_request(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT) self.connect("destroy", gtk.main_quit) # self.connect("expose-event", self.on_expose_event) self.sina = Sina() self.webview = webkit.WebView() self.webview.set_transparent(True)
# 将分类标记好bbox的图像一股脑的放在一起,并规范化命名 import sys import utils import os import shutil import random if __name__ == "__main__": root_dir = sys.argv[1] parent_dir, _ = utils.get_parent_dir(root_dir) target_dir = os.path.join(parent_dir, 'results') utils.create_new_empty_dir(target_dir) os.mkdir(os.path.join(target_dir, 'images')) os.mkdir(os.path.join(target_dir, 'annotations')) sub_dirs = utils.get_immediate_subdirectories(root_dir) total_count = 0 for sub_dir in sub_dirs: sub_dir = os.path.join(root_dir, sub_dir) image_dir = os.path.join(sub_dir, 'images') image_file_list = utils.get_dir_filelist_by_extension(image_dir, 'bmp') total_count += len(image_file_list) print('图像总数:%d' % total_count) file_paths = [] for i in range(1, total_count + 1): new_name = utils.fixed_length(i, 4) a = { 'image': os.path.join(target_dir, 'images', new_name + '.bmp'), 'annotation': os.path.join(target_dir, 'annotations', new_name + '.xml')
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # This module *just* use for test module under dtk.ui from skin_config import skin_config from theme import Theme, ui_theme from utils import get_parent_dir import os # Init skin config. skin_config.init_skin( "01", os.path.join(get_parent_dir(__file__, 3), "skin"), os.path.expanduser("~/.config/deepin-demo/skin"), os.path.expanduser("~/.config/deepin-demo/skin_config.ini"), "deepin-media-player", "1.0" ) # Create application theme. app_theme = Theme( os.path.join(get_parent_dir(__file__, 3), "app_theme"), os.path.expanduser("~/.config/deepin-demo/theme") ) # Set theme. skin_config.load_themes(ui_theme, app_theme)
def get_data_from_first_model(first_model_json_path, sr=16000, snr=2.5, n_fft=510, hop_length=158, win_length=400, unknown_clean_signal=False): # load prediction json all_data_info = [] dataset_path = '' num_videos = 0 data_total_frames = 0 data_center_frames = 0 sigmoid_threshold = 0 with open(first_model_json_path, 'r') as fp: json_obj = json.load(fp) all_data_info = json_obj['files'] dataset_path = json_obj['dataset_path'] num_videos = json_obj['num_videos'] data_total_frames = json_obj['data_total_frames'] data_center_frames = json_obj['data_center_frames'] sigmoid_threshold = json_obj['sigmoid_threshold'] snr = json_obj['snr'] print('========== SUMMARY ==========') print('Dataset JSON:', first_model_json_path) print('Dataset path:', dataset_path) print('Num videos:', num_videos) print('Sample rate: {}'.format(sr)) print('SNR: {}'.format(snr)) print('n_fft: {}'.format(n_fft)) print('hop_length: {}'.format(hop_length)) print('win_length: {}'.format(win_length)) print('========== Loading data ==========') data_list = [] for idx, data in enumerate(tqdm(all_data_info)): if not unknown_clean_signal: # load clean audio # clean_audio_path = data['path'].split('.mp4')[0] + '.wav' clean_audio_path = os.path.join( get_parent_dir(first_model_json_path), data['clean_audio']) clean_sig, _ = librosa.load(clean_audio_path, sr=sr) # clean_sig = librosa.util.normalize(clean_sig) # load full noise # full_noise_path = os.path.join(get_parent_dir(first_model_json_path),\ # 'noise_' + os.path.splitext(os.path.basename(first_model_json_path))[0].split('_')[-1],\ # os.path.basename(clean_audio_path).split('.wav')[0] + '_noise.wav') full_noise_path = os.path.join( get_parent_dir(first_model_json_path), data['full_noise']) full_noise, _ = librosa.load(full_noise_path, sr=sr) # full_noise = librosa.util.normalize(full_noise) # load noisy audio mixed_audio_path = os.path.join(get_parent_dir(first_model_json_path), data['mixed_audio']) mixed_sig, _ = librosa.load(mixed_audio_path, sr=sr) # convert predicted bitstream to audio mask bitstream = data[BIT_STREAM_LABEL] gt_bitstream = data[GT_BIT_STREAM_LABEL] frames_to_audiosample_ratio = float(sr) / data['framerate'] mask = convert_bitstreammask_to_audiomask(mixed_sig, frames_to_audiosample_ratio, bitstream) # create ground truth mask if not unknown_clean_signal: gt_mask = convert_bitstreammask_to_audiomask( clean_sig, frames_to_audiosample_ratio, gt_bitstream) # noise_sig(masked noise) = mixed_sig * mask noise_sig = mixed_sig * mask # stft mixed_sig_stft = fast_stft(mixed_sig, n_fft=n_fft, hop_length=hop_length, win_length=win_length) noise_sig_stft = fast_stft(noise_sig, n_fft=n_fft, hop_length=hop_length, win_length=win_length) if not unknown_clean_signal: # enforce silent intervals to be truly silent (clean_sig) clean_sig = clean_sig * (1 - gt_mask) clean_sig_stft = fast_stft(clean_sig, n_fft=n_fft, hop_length=hop_length, win_length=win_length) full_noise_sig_stft = fast_stft(full_noise, n_fft=n_fft, hop_length=hop_length, win_length=win_length) # icrm = fast_cRM_sigmoid(clean_sig_stft, mixed_sig_stft) mixed_sig_stft = torch.tensor(mixed_sig_stft.transpose((2, 0, 1)), dtype=torch.float32).unsqueeze(0) noise_sig_stft = torch.tensor(noise_sig_stft.transpose((2, 0, 1)), dtype=torch.float32).unsqueeze(0) if not unknown_clean_signal: clean_sig_stft = torch.tensor(clean_sig_stft.transpose((2, 0, 1)), dtype=torch.float32).unsqueeze(0) full_noise_sig_stft = torch.tensor( full_noise_sig_stft.transpose( (2, 0, 1)), dtype=torch.float32).unsqueeze(0) if unknown_clean_signal: item = OrderedDict([ ("id", os.path.splitext(os.path.basename(data['path']))[0]), ("path", data['path']), ("mixed_audio_path", mixed_audio_path), ("bitstream", bitstream), ("mixed", mixed_sig_stft), ("noise", noise_sig_stft), ("mask", mask), ("snr", snr), ("sr", sr), ]) else: item = OrderedDict([ ("id", os.path.splitext(os.path.basename(data['path']))[0]), ("path", data['path']), ("clean_audio_path", clean_audio_path), ("mixed_audio_path", mixed_audio_path), ("full_noise_path", full_noise_path), ("bitstream", bitstream), ("mixed", mixed_sig_stft), ("clean", clean_sig_stft), ("noise", noise_sig_stft), ("full_noise", full_noise_sig_stft), ("mask", mask), ("snr", snr), ("sr", sr), ]) data_list.append(item) info = OrderedDict([('dataset_path', dataset_path), ('num_videos', num_videos), ('data_total_frames', data_total_frames), ('data_center_frames', data_center_frames), ('sigmoid_threshold', sigmoid_threshold)]) return (data_list, info)
# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # To test other language, use below method: # env LANG=zh_CN LANGUAGE=zh_CN foo.py from utils import get_parent_dir import gettext import os LOCALE_DIR=os.path.join(get_parent_dir(__file__, 2), "locale") if not os.path.exists(LOCALE_DIR): LOCALE_DIR="/usr/share/locale" _ = None try: gettext.bindtextdomain("deepin-ui", LOCALE_DIR) gettext.textdomain("deepin-ui") _ = gettext.gettext except Exception, e: _ = lambda i : i
def change_tag_text(file_list, target_dir): for file in file_list: tree = ET.ElementTree(file=file) root = tree.getroot() # print(root.tag) # print(root.attrib) print(file) for elem in tree.iter(tag='path'): # file_name = elem.text # index = file_name.find('.') # ext = file_name[index:] _, fn = utils.get_parent_dir(file) fn = fn.replace('xml', 'bmp') elem.text = '' # print(elem.text) tree.write(target_dir + os.path.basename(file)) if __name__ == "__main__": xmls_dir = '/Users/shidanlifuhetian/All/data/KHB_ANNO/USTB中厚板检测数据集/test/xmls1' # 工具作用:规范xml内容,filename规范为真实文件名 file_list = utils.get_dir_filelist_by_extension(dir=xmls_dir, ext='xml') parent_dir, _ = utils.get_parent_dir(xmls_dir) utils.create_new_empty_dir(parent_dir + '/xmls2/') target_dir = parent_dir + '/xmls2/' change_tag_text(file_list, target_dir)
def run(args, data_list, save_results=True, save_stat=False, is_ids=False): # get config config = get_config(args) # get network net = get_network() name = args.ckpt if args.ckpt == 'latest' or args.ckpt == 'best_acc' else "ckpt_epoch{}".format( args.ckpt) load_path = os.path.join(config.model_dir, "{}.pth".format(name)) print('Load saved model: {}'.format(load_path)) net.load_state_dict(torch.load(load_path)['model_state_dict']) net = net.cuda() stat = [] # forward net.eval() print('Save individual result:', save_results) print('Save overall results:', save_stat) print('Num test samples:', len(data_list)) for data_id, data in enumerate(tqdm(data_list)): if is_ids: data = get_data_by_id(data) frames = data['frames'] label = data['label'] audio = data['audio'].cuda() audio_raw = data['audio_raw'] with torch.no_grad(): output_value = net(audio).detach().cpu().numpy() output_value = float(np.squeeze(sigmoid(output_value))) pred_label = 1.0 if output_value >= SIGMOID_THRESHOLD else 0.0 # pred_label = (torch.sigmoid(net(audio)).detach().cpu().numpy() >= SIGMOID_THRESHOLD).astype(np.float) info = OrderedDict([('id', data_id), ('path', data['path']), ('full_bit_stream', data['full_bit_stream']), ('num_frames', data['num_frames']), ('framerate', data['framerate']), ('frame_start_idx', data['frame_start_idx']), ('bit_stream', data['bits']), ('label', label), ('pred_label', pred_label), ('match', (label == pred_label)), ('confidence', output_value)]) # save results if save_results: # only save interesting results if label != pred_label or label == 0.0 or pred_label == 0.0: save_dir = os.path.join(os.path.abspath(args.outputs), 'frames', str(data_id)) ensure_dir(save_dir) with open(os.path.join(save_dir, 'stat.json'), 'w') as fp: json.dump(info, fp, **JSON_DUMP_PARAMS) # save images to visualize input frame_images = save_imgs_from_tensor(frames, save_dir) # save gif gif_path = os.path.join(os.path.abspath(args.outputs), 'gifs', str(data_id) + '.gif') ensure_dir(get_parent_dir(gif_path)) info['gif'] = os.path.join('gifs', str(data_id) + '.gif') # need relative path! create_gif3(frame_images, gif_path, fps=DATA_REQUIRED_FPS) # save audio audio_path = os.path.join(os.path.abspath(args.outputs), 'audio', str(data_id) + '.wav') ensure_dir(get_parent_dir(audio_path)) info['audio'] = os.path.join('audio', str(data_id) + '.wav') librosa.output.write_wav(audio_path, audio_raw, DATA_REQUIRED_SR) stat.append(info) if save_stat: # labels = [item['label'] for item in stat] # pred_labels = [item['pred_label'] for item in stat] # stat_dict = show_metrics(labels, pred_labels) stat_dict = OrderedDict() stat_dict['data_total_frames'] = CLIP_FRAMES stat_dict['data_center_frames'] = SILENT_CONSECUTIVE_FRAMES stat_dict['sigmoid_threshold'] = SIGMOID_THRESHOLD labels_all = [item['label'] for item in stat] pred_labels_all = [item['pred_label'] for item in stat] labels_partial = [item['label'] for item in stat\ if item['bit_stream'].count('0') == len(item['bit_stream'])\ or item['bit_stream'].count('1') > len(item['bit_stream']) - MAX_NUM_OF_0_IN_1_DATA] pred_labels_partial = [item['pred_label'] for item in stat\ if item['bit_stream'].count('0') == len(item['bit_stream'])\ or item['bit_stream'].count('1') > len(item['bit_stream']) - MAX_NUM_OF_0_IN_1_DATA] # stat_dict['prediction_statistics'] = OrderedDict([ # (str(TRAIN_DATA_RATIO) + '-' + str(1-TRAIN_DATA_RATIO), show_metrics(labels_partial, pred_labels_partial)), # (str(PRED_DATA_RATIO) + '-' + str(1-PRED_DATA_RATIO), show_metrics(labels_all, pred_labels_all)) # ]) # stat_dict['prediction_statistics'] = OrderedDict([ # ('filtered', show_metrics(labels_all, pred_labels_all)), # # (str(PRED_DATA_RATIO) + '-' + str(1-PRED_DATA_RATIO), show_metrics(labels_all, pred_labels_all)) # ]) stat_dict['prediction_statistics'] = OrderedDict([ ('filtered', show_metrics(labels_partial, pred_labels_partial)), ('all', show_metrics(labels_all, pred_labels_all)) ]) # sort stat by confidence descending order stat = sorted(stat, key=lambda x: x['confidence'], reverse=True) stat_dict['data'] = stat # save_stat_path = os.path.abspath('eval_results.json') save_stat_path = os.path.join(os.path.abspath(args.outputs), 'eval_results.json') ensure_dir(get_parent_dir(save_stat_path)) with open(save_stat_path, 'w') as fp: json.dump(stat_dict, fp, **JSON_DUMP_PARAMS) print('Overall results saved to: \'{}\''.format(save_stat_path))
def evaluate(args, save_individual_results=True, save_noise_info=True, save_stat=True, dataset_json=None, clean_audio=True): # all_ids = list(range(len(ALL_CHOICES))) # run(args, all_ids, save_results=save_results, save_stat=save_stat, is_ids=True) # if dataset_json is not None: if not clean_audio: args.outputs = os.path.join( EXPERIMENT_PREDICTION_OUTPUT_DIR, os.path.basename(dataset_json).split('.json')[0]) ensure_dir(args.outputs) print('Output redirected to: \"{}\"'.format(args.outputs)) # get config config = get_config(args) # get network net = get_network() name = args.ckpt if args.ckpt == 'latest' or args.ckpt == 'best_acc' else "ckpt_epoch{}".format( args.ckpt) load_path = os.path.join(config.model_dir, "{}.pth".format(name)) print('Load saved model: {}'.format(load_path)) net.load_state_dict(torch.load(load_path)['model_state_dict']) if torch.cuda.device_count() > 1: print('For multi-GPU') net = nn.DataParallel(net.cuda()) # For multi-GPU else: print('For single-GPU') net = net.cuda() # For single-GPU # evaluate net.eval() stat = [] print('Save individual results:', save_individual_results) print('Save overall results:', save_stat) # dataloader = get_dataloader(PHASE_PREDICTION, batch_size=config.batch_size, num_workers=config.num_workers, snr_idx=args.snr_idx) dataloader = get_dataloader(PHASE_PREDICTION, batch_size=1, num_workers=20, snr_idx=args.snr_idx, dataset_json=dataset_json, clean_audio=clean_audio) if clean_audio: suffix = convert_snr_to_suffix(dataloader.dataset.snrs, args.snr_idx) else: # suffix = convert_snr_to_suffix(SNRS, args.snr_idx) suffix = '' # save noise info if clean_audio: if save_noise_info: noise_dir = os.path.join(os.path.abspath(args.outputs), 'noise' + suffix) ensure_dir(noise_dir) noise_infos = OrderedDict([('snrs', dataloader.dataset.snrs)]) noise_files = OrderedDict() noise_dict = dataloader.dataset.noise_dict for k, v in noise_dict.items(): corr_video = os.path.basename( dataloader.dataset.files[k]['path']) noise_name = corr_video.split('.mp4')[0] + '_noise.wav' noise_path = os.path.join(noise_dir, noise_name) librosa.output.write_wav(noise_path, v[0], DATA_REQUIRED_SR) noise_files[corr_video] = OrderedDict([ ('audio', corr_video.split('.mp4')[0] + '.wav'), ('noise', noise_name), ('snr', v[1]) ]) noise_infos['files'] = noise_files # noise_json_path = os.path.join(noise_dir, 'noise.json') noise_json_path = os.path.join(noise_dir, suffix[1:] + '.json') with open(noise_json_path, 'w') as fpnoise: json.dump(noise_infos, fpnoise, **JSON_DUMP_PARAMS) print('Noise info saved to: \'{}\''.format(noise_json_path)) for i, data in enumerate(tqdm(dataloader)): # batch_frames = data['frames'].cuda() # print('batch_frames.shape:', batch_frames.shape) # batch_frames_raws = data['frames'].detach().cpu() batch_labels = data['label'].detach().cpu().numpy() # print('batch_labels.shape:', batch_labels.shape) batch_audios = data['audio'].cuda() batch_audio_raws = data['audio'].detach().cpu().numpy() # print('batch_audio_raws.shape:', batch_audio_raws.shape) with torch.no_grad(): batch_output_values = torch.sigmoid( net(s=batch_audios, v_num_frames=batch_labels.shape[1])).detach().cpu().numpy( ) # print('batch_output_values.shape:', batch_output_values.shape) batch_pred_labels = (batch_output_values >= SIGMOID_THRESHOLD).astype(np.float) # print('batch_pred_labels.shape:', batch_pred_labels.shape) for idx in range(batch_output_values.shape[0]): data_id = i * dataloader.batch_size + idx item = dataloader.dataset.items[data_id] file_info_dict = dataloader.dataset.files[item[0]] # print(batch_labels[idx].shape) label = list(batch_labels[idx].astype(np.int).astype(str)) # print(len(label)) pred_label = list(batch_pred_labels[idx].astype( np.int).astype(str)) # print(len(pred_label)) info = OrderedDict([ ('id', data_id), ('path', file_info_dict['path']), ('full_bit_stream', file_info_dict[BITSTREAM_JSON_LABEL]), ('num_frames', file_info_dict['num_frames']), ('framerate', file_info_dict['framerate']), ('audio_sample_rate', file_info_dict['audio_sample_rate']), ('audio_samples', file_info_dict['audio_samples']), ('duration', file_info_dict['duration']), ('frame_start_idx', item[1]), ('label', label), ('pred_label', pred_label), ('match', (label == pred_label)), ('confidence', list(batch_output_values[idx].astype(str))) ]) stat.append(info) # save results if save_individual_results: # only save interesting results if label != pred_label or label == 0.0 or pred_label == 0.0: # save_dir = os.path.join(os.path.abspath(args.outputs), 'frames' + suffix, str(data_id)) # ensure_dir(save_dir) # with open(os.path.join(save_dir, 'stat.json'), 'w') as fp: # json.dump(info, fp, **JSON_DUMP_PARAMS) # save images to visualize input # frame_images = save_imgs_from_tensor(batch_frames_raws[idx], save_dir) # save gif gif_path = os.path.join(os.path.abspath(args.outputs), 'gifs' + suffix, str(data_id) + '.jpg') ensure_dir(get_parent_dir(gif_path)) info['gif'] = os.path.join( 'gifs' + suffix, str(data_id) + '.jpg') # need relative path! # create_gif3(frame_images, gif_path, fps=DATA_REQUIRED_FPS) plt.figure(figsize=(PLOT_H, PLOT_H), dpi=PLOT_DPI) # librosa.display.waveplot(np.squeeze(batch_audio_raws[idx]), sr=DATA_REQUIRED_SR) mixed_stft = batch_audio_raws[idx].transpose((1, 2, 0)) mixed_sig = fast_istft(mixed_stft) librosa.display.waveplot(mixed_sig, sr=DATA_REQUIRED_SR) plt.savefig(gif_path) # save audio audio_path = os.path.join( os.path.abspath(args.outputs), 'audio' + suffix, str(data_id) + '.wav') ensure_dir(get_parent_dir(audio_path)) info['audio'] = os.path.join('audio' + suffix, str(data_id) + '.wav') # librosa.output.write_wav(audio_path, np.squeeze(batch_audio_raws[idx]), DATA_REQUIRED_SR) # mixed_stft = batch_audio_raws[idx].transpose((1, 2, 0)) # mixed_sig = fast_istft(mixed_stft) librosa.output.write_wav(audio_path, mixed_sig, DATA_REQUIRED_SR) if save_stat: # labels = [item['label'] for item in stat] # pred_labels = [item['pred_label'] for item in stat] # stat_dict = show_metrics(labels, pred_labels) stat_dict = OrderedDict() stat_dict['data_total_frames'] = CLIP_FRAMES stat_dict['data_center_frames'] = SILENT_CONSECUTIVE_FRAMES stat_dict['sigmoid_threshold'] = SIGMOID_THRESHOLD if args.snr_idx is not None: stat_dict['snr'] = dataloader.dataset.snrs[args.snr_idx] # stat_dict['snr'] = SNRS[args.snr_idx] else: stat_dict['snr'] = None # labels_all = [item['label'] for item in stat] labels_all = [element for item in stat for element in item['label']] # pred_labels_all = [item['pred_label'] for item in stat] pred_labels_all = [ element for item in stat for element in item['pred_label'] ] # labels_partial = [item['label'] for item in stat\ # if item['bit_stream'].count('0') == len(item['bit_stream'])\ # or item['bit_stream'].count('1') > len(item['bit_stream']) - MAX_NUM_OF_0_IN_1_DATA] # pred_labels_partial = [item['pred_label'] for item in stat\ # if item['bit_stream'].count('0') == len(item['bit_stream'])\ # or item['bit_stream'].count('1') > len(item['bit_stream']) - MAX_NUM_OF_0_IN_1_DATA] # stat_dict['prediction_statistics'] = OrderedDict([ # (str(TRAIN_DATA_RATIO) + '-' + str(1-TRAIN_DATA_RATIO), show_metrics(labels_partial, pred_labels_partial)), # (str(PRED_DATA_RATIO) + '-' + str(1-PRED_DATA_RATIO), show_metrics(labels_all, pred_labels_all)) # ]) # stat_dict['prediction_statistics'] = OrderedDict([ # ('filtered', show_metrics(labels_all, pred_labels_all)), # # (str(PRED_DATA_RATIO) + '-' + str(1-PRED_DATA_RATIO), show_metrics(labels_all, pred_labels_all)) # ]) stat_dict['prediction_statistics'] = OrderedDict([ # ('filtered', show_metrics(labels_partial, pred_labels_partial)), ('all', show_metrics(labels_all, pred_labels_all)) ]) # sort stat by confidence descending order stat = sorted( stat, key=lambda x: np.mean([float(i) for i in x['confidence']]), reverse=True) stat_dict['data'] = stat # save_stat_path = os.path.abspath('eval_results.json') save_stat_path = os.path.join(os.path.abspath(args.outputs), 'eval_results' + suffix + '.json') ensure_dir(get_parent_dir(save_stat_path)) with open(save_stat_path, 'w') as fp: json.dump(stat_dict, fp, **JSON_DUMP_PARAMS) print('Overall results saved to: \'{}\''.format(save_stat_path))
def change_theme(self, new_theme_name): '''Change ui_theme.''' # Update ticker. self.ticker += 1 # Change theme name. self.theme_name = new_theme_name # Update dynmaic pixbuf. for (path, pixbuf) in self.pixbuf_dict.items(): pixbuf.update(self.get_theme_file_path("image/%s" % (path))) # Update dynamic colors. theme_info = eval_file(self.get_theme_file_path(self.theme_info_file)) for (color_name, color) in theme_info["colors"].items(): self.color_dict[color_name].update(color) # Update dynamic alpha colors. for (color_name, color_info) in theme_info["alpha_colors"].items(): self.alpha_color_dict[color_name].update(color_info) # Update shadow colors. for (color_name, color_info) in theme_info["shadow_colors"].items(): self.shadow_color_dict[color_name].update(color_info) # Init. ui_theme = Theme(os.path.join(get_parent_dir(__file__, 2), "theme"), os.path.expanduser("~/.config/deepin-ui/theme"))