def print_explanations(cls, exps, assets=None, ys=None, ys_pred=None): # asserts N = cls.assert_explanations(exps, assets, ys, ys_pred) print "Features: {}".format(exps['feature_names']) for n in range(N): weights = exps['feature_weights'][n] features = exps['features_normalized'][n] asset = assets[n] if assets is not None else None y = ys['label'][n] if ys is not None else None y_pred = ys_pred[n] if ys_pred is not None else None print "{ref}".format(ref=get_file_name_without_extension( asset.ref_path) if asset is not None else "Asset {}".format(n)) if asset is not None: print "\tDistorted: {dis}".format( dis=get_file_name_without_extension(asset.dis_path)) if y is not None: print "\tground truth: {y:.3f}".format(y=y) if y_pred is not None: print "\tpredicted: {y_pred:.3f}".format(y_pred=y_pred) print "\tfeature value: {}".format(features) print "\tfeature weight: {}".format(weights)
def print_explanations(cls, exps, assets=None, ys=None, ys_pred=None): # asserts N = cls.assert_explanations(exps, assets, ys, ys_pred) print "Features: {}".format(exps['feature_names']) for n in range(N): weights = exps['feature_weights'][n] features = exps['features_normalized'][n] asset = assets[n] if assets is not None else None y = ys['label'][n] if ys is not None else None y_pred = ys_pred[n] if ys_pred is not None else None print "{ref}".format( ref=get_file_name_without_extension(asset.ref_path) if asset is not None else "Asset {}".format(n)) if asset is not None: print "\tDistorted: {dis}".format( dis=get_file_name_without_extension(asset.dis_path)) if y is not None: print "\tground truth: {y:.3f}".format(y=y) if y_pred is not None: print "\tpredicted: {y_pred:.3f}".format(y_pred=y_pred) print "\tfeature value: {}".format(features) print "\tfeature weight: {}".format(weights)
def print_assets(test_assets): print "\n".join( map( lambda (i, asset): "Asset {i}: {name}".format( i=i, name=get_file_name_without_extension(asset.dis_path) ), enumerate(test_assets), ) )
def dis_str(self): """ String representation for distorted video. :return: """ s = "" path = get_file_name_without_extension(self.dis_path) s += "{path}".format(path=path) w, h = self.dis_width_height s += "_{w}x{h}".format(w=w, h=h) if self.yuv_type != self.DEFAULT_YUV_TYPE: s += "_{}".format(self.yuv_type) if self.dis_start_end_frame: start, end = self.dis_start_end_frame s += "_{start}to{end}".format(start=start, end=end) return s
def dis_str(self): """ String representation for distorted video. :return: """ s = "" path = get_file_name_without_extension(self.dis_path) s += "{path}".format(path=path) w, h = self.dis_width_height s += "_{w}x{h}".format(w=w, h=h) if self.yuv_type != self.DEFAULT_YUV_TYPE: s += "_{}".format(self.yuv_type) if self.dis_start_end_frame: start, end = self.dis_start_end_frame s += "_{start}to{end}".format(start=start, end=end) return s
def print_assets(test_assets): print '\n'.join(map( lambda (i, asset): "Asset {i}: {name}".format( i=i, name=get_file_name_without_extension(asset.dis_path)), enumerate(test_assets) ))
def main(): if len(sys.argv) < 6: print_usage() return 2 try: fmt = sys.argv[1] width = int(sys.argv[2]) height = int(sys.argv[3]) ref_file = sys.argv[4] dis_file = sys.argv[5] except ValueError: print_usage() return 2 if width < 0 or height < 0: print "width and height must be non-negative, but are {w} and {h}".format( w=width, h=height) print_usage() return 2 if fmt not in FMTS: print_usage() return 2 model_path = get_cmd_option(sys.argv, 6, len(sys.argv), '--model') out_fmt = get_cmd_option(sys.argv, 6, len(sys.argv), '--out-fmt') if not (out_fmt is None or out_fmt == 'xml' or out_fmt == 'json' or out_fmt == 'text'): print_usage() return 2 pool_method = get_cmd_option(sys.argv, 6, len(sys.argv), '--pool') if not (pool_method is None or pool_method in POOL_METHODS): print '--pool can only have option among {}'.format( ', '.join(POOL_METHODS)) return 2 show_local_explanation = cmd_option_exists(sys.argv, 6, len(sys.argv), '--local-explain') asset = Asset( dataset="cmd", content_id=abs(hash(get_file_name_without_extension(ref_file))) % (10**16), asset_id=abs(hash(get_file_name_without_extension(ref_file))) % (10**16), workdir_root=config.ROOT + "/workspace/workdir", ref_path=ref_file, dis_path=dis_file, asset_dict={ 'width': width, 'height': height, 'yuv_type': fmt }) assets = [asset] if not show_local_explanation: runner_class = VmafQualityRunner else: runner_class = VmafQualityRunnerWithLocalExplainer if model_path is None: optional_dict = None else: optional_dict = {'model_filepath': model_path} runner = runner_class( assets, None, fifo_mode=True, delete_workdir=True, result_store=None, optional_dict=optional_dict, optional_dict2=None, ) # run runner.run() result = runner.results[0] # pooling if pool_method == 'harmonic_mean': result.set_score_aggregate_method(ListStats.harmonic_mean) elif pool_method == 'min': result.set_score_aggregate_method(np.min) elif pool_method == 'median': result.set_score_aggregate_method(np.median) elif pool_method == 'perc5': result.set_score_aggregate_method(ListStats.perc5) elif pool_method == 'perc10': result.set_score_aggregate_method(ListStats.perc10) elif pool_method == 'perc20': result.set_score_aggregate_method(ListStats.perc20) else: # None or 'mean' pass # output if out_fmt == 'xml': print result.to_xml() elif out_fmt == 'json': print result.to_json() else: # None or 'text' print str(result) # local explanation if show_local_explanation: import matplotlib.pyplot as plt runner.show_local_explanations([result]) plt.show() return 0
def main(): if len(sys.argv) < 6: print_usage() return 2 try: fmt = sys.argv[1] width = int(sys.argv[2]) height = int(sys.argv[3]) ref_file = sys.argv[4] dis_file = sys.argv[5] except ValueError: print_usage() return 2 if width < 0 or height < 0: print "width and height must be non-negative, but are {w} and {h}".format(w=width, h=height) print_usage() return 2 if fmt not in FMTS: print_usage() return 2 model_path = get_cmd_option(sys.argv, 6, len(sys.argv), '--model') out_fmt = get_cmd_option(sys.argv, 6, len(sys.argv), '--out-fmt') if not (out_fmt is None or out_fmt == 'xml' or out_fmt == 'json' or out_fmt == 'text'): print_usage() return 2 pool_method = get_cmd_option(sys.argv, 6, len(sys.argv), '--pool') if not (pool_method is None or pool_method in POOL_METHODS): print '--pool can only have option among {}'.format(', '.join(POOL_METHODS)) return 2 show_local_explanation = cmd_option_exists(sys.argv, 6, len(sys.argv), '--local-explain') asset = Asset(dataset="cmd", content_id=abs(hash(get_file_name_without_extension(ref_file))) % (10 ** 16), asset_id=abs(hash(get_file_name_without_extension(ref_file))) % (10 ** 16), workdir_root=config.ROOT + "/workspace/workdir", ref_path=ref_file, dis_path=dis_file, asset_dict={'width':width, 'height':height, 'yuv_type':fmt} ) assets = [asset] if not show_local_explanation: runner_class = VmafQualityRunner else: runner_class = VmafQualityRunnerWithLocalExplainer if model_path is None: optional_dict = None else: optional_dict = {'model_filepath':model_path} runner = runner_class( assets, None, fifo_mode=True, delete_workdir=True, result_store=None, optional_dict=optional_dict, optional_dict2=None, ) # run runner.run() result = runner.results[0] # pooling if pool_method == 'harmonic_mean': result.set_score_aggregate_method(ListStats.harmonic_mean) elif pool_method == 'min': result.set_score_aggregate_method(np.min) elif pool_method == 'median': result.set_score_aggregate_method(np.median) elif pool_method == 'perc5': result.set_score_aggregate_method(ListStats.perc5) elif pool_method == 'perc10': result.set_score_aggregate_method(ListStats.perc10) elif pool_method == 'perc20': result.set_score_aggregate_method(ListStats.perc20) else: # None or 'mean' pass # output if out_fmt == 'xml': print result.to_xml() elif out_fmt == 'json': print result.to_json() else: # None or 'text' print str(result) # local explanation if show_local_explanation: import matplotlib.pyplot as plt runner.show_local_explanations([result]) plt.show() return 0
def plot_explanations(cls, exps, assets=None, ys=None, ys_pred=None): # asserts N = cls.assert_explanations(exps, assets, ys, ys_pred) figs = [] for n in range(N): weights = exps['feature_weights'][n] features = exps['features'][n] normalized = exps['features_normalized'][n] asset = assets[n] if assets is not None else None y = ys['label'][n] if ys is not None else None y_pred = ys_pred[n] if ys_pred is not None else None img = None if asset is not None: w, h = asset.dis_width_height with YuvReader(filepath=asset.dis_path, width=w, height=h, yuv_type=asset.yuv_type) as yuv_reader: for yuv in yuv_reader: img, _, _ = yuv break assert img is not None title = "" if asset is not None: title += "{}\n".format( get_file_name_without_extension(asset.ref_path)) if y is not None: title += "ground truth: {:.3f}\n".format(y) if y_pred is not None: title += "predicted: {:.3f}\n".format(y_pred) if title != "" and title[-1] == '\n': title = title[:-1] assert len(weights) == len(features) M = len(weights) fig = plt.figure() ax_top = plt.subplot(2, 1, 1) ax_left = plt.subplot(2, 3, 4) ax_mid = plt.subplot(2, 3, 5, sharey=ax_left) ax_right = plt.subplot(2, 3, 6, sharey=ax_left) if img is not None: ax_top.imshow(img, cmap='Greys_r') ax_top.get_xaxis().set_visible(False) ax_top.get_yaxis().set_visible(False) ax_top.set_title(title) pos = np.arange(M) + 0.1 ax_left.barh(pos, features, color='b', label='feature') ax_left.set_xticks(np.arange(0, 1.1, 0.2)) ax_left.set_yticks(pos + 0.35) ax_left.set_yticklabels(exps['feature_names']) ax_left.set_title('feature') ax_mid.barh(pos, normalized, color='g', label='fnormal') ax_mid.get_yaxis().set_visible(False) ax_mid.set_title('fnormal') ax_right.barh(pos, weights, color='r', label='weight') ax_right.get_yaxis().set_visible(False) ax_right.set_title('weight') plt.tight_layout() figs.append(fig) return figs
if model_filepath == 'none': model_filepath = None else: model_filepath = None if len(sys.argv) >= 8: cache_result = sys.argv[7] if cache_result == 'yes': result_store = FileSystemResultStore() else: result_store = None else: result_store = None asset = Asset(dataset="run_vmaf", content_id=abs(hash(get_file_name_without_extension(ref_file))) % (10 ** 16), asset_id=abs(hash(get_file_name_without_extension(ref_file))) % (10 ** 16), workdir_root=config.ROOT + "/workspace/workdir", ref_path=ref_file, dis_path=dis_file, asset_dict={'width':width, 'height':height, 'yuv_type':fmt} ) assets = [asset] runner_class = VmafQualityRunner optional_dict = { 'model_filepath':model_filepath } runner = runner_class(
def plot_explanations(cls, exps, assets=None, ys=None, ys_pred=None): # asserts N = cls.assert_explanations(exps, assets, ys, ys_pred) figs = [] for n in range(N): weights = exps['feature_weights'][n] features = exps['features'][n] normalized = exps['features_normalized'][n] asset = assets[n] if assets is not None else None y = ys['label'][n] if ys is not None else None y_pred = ys_pred[n] if ys_pred is not None else None img = None if asset is not None: w, h = asset.dis_width_height with YuvReader(filepath=asset.dis_path, width=w, height=h, yuv_type=asset.yuv_type) as yuv_reader: for yuv in yuv_reader: img, _, _ = yuv break assert img is not None title = "" if asset is not None: title += "{}\n".format(get_file_name_without_extension(asset.ref_path)) if y is not None: title += "ground truth: {:.3f}\n".format(y) if y_pred is not None: title += "predicted: {:.3f}\n".format(y_pred) if title != "" and title[-1] == '\n': title = title[:-1] assert len(weights) == len(features) M = len(weights) fig = plt.figure() ax_top = plt.subplot(2, 1, 1) ax_left = plt.subplot(2, 3, 4) ax_mid = plt.subplot(2, 3, 5, sharey=ax_left) ax_right = plt.subplot(2, 3, 6, sharey=ax_left) if img is not None: ax_top.imshow(img, cmap='Greys_r') ax_top.get_xaxis().set_visible(False) ax_top.get_yaxis().set_visible(False) ax_top.set_title(title) pos = np.arange(M) + 0.1 ax_left.barh(pos, features, color='b', label='feature') ax_left.set_xticks(np.arange(0, 1.1, 0.2)) ax_left.set_yticks(pos + 0.35) ax_left.set_yticklabels(exps['feature_names']) ax_left.set_title('feature') ax_mid.barh(pos, normalized, color='g', label='fnormal') ax_mid.get_yaxis().set_visible(False) ax_mid.set_title('fnormal') ax_right.barh(pos, weights, color='r', label='weight') ax_right.get_yaxis().set_visible(False) ax_right.set_title('weight') plt.tight_layout() figs.append(fig) return figs
model_filepath = None else: model_filepath = None if len(sys.argv) >= 8: cache_result = sys.argv[7] if cache_result == 'yes': result_store = FileSystemResultStore() else: result_store = None else: result_store = None asset = Asset( dataset="run_vmaf", content_id=abs(hash(get_file_name_without_extension(ref_file))) % (10**16), asset_id=abs(hash(get_file_name_without_extension(ref_file))) % (10**16), workdir_root=config.ROOT + "/workspace/workdir", ref_path=ref_file, dis_path=dis_file, asset_dict={ 'width': width, 'height': height, 'yuv_type': fmt }) assets = [asset] runner_class = VmafQualityRunner