def test_yuv_reader(self): yuv_reader = YuvReader( filepath=VmafConfig.test_resource_path("yuv", "src01_hrc00_576x324.yuv422p10le.yuv"), width=576, height=324, yuv_type='yuv422p10le' ) self.assertEquals(yuv_reader.num_bytes, 35831808) self.assertEquals(yuv_reader.num_frms, 48) self.assertEquals(yuv_reader._get_uv_width_height_multiplier(), (0.5, 1.0))
def _generate_result(self, asset): # routine to generate feature scores in the log file. quality_w, quality_h = asset.quality_width_height with YuvReader(filepath=asset.dis_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type(asset)) \ as dis_yuv_reader: scores_mtx_list = [] i = 0 for dis_yuv in dis_yuv_reader: dis_y = dis_yuv[0] firstm = dis_y.mean() secondm = dis_y.var() + firstm**2 scores_mtx_list.append(np.hstack(([firstm], [secondm]))) i += 1 scores_mtx = np.vstack(scores_mtx_list) # write scores_mtx to log file log_file_path = self._get_log_file_path(asset) with open(log_file_path, "wb") as log_file: np.save(log_file, scores_mtx)
def _generate_result(self, asset): # routine to call the command-line executable and generate feature # scores in the log file. quality_w, quality_h = asset.quality_width_height with YuvReader( filepath=asset.dis_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type(asset)) as dis_yuv_reader: scores_mtx_list = [] for dis_yuv in dis_yuv_reader: dis_y = dis_yuv[0] list_features = self.mscn_extract_niqe(dis_y, self.patch_size, self.mode) scores_mtx_list += list_features scores_mtx = np.vstack(scores_mtx_list) # write scores_mtx to log file log_file_path = self._get_log_file_path(asset) with open(log_file_path, "wb") as log_file: np.save(log_file, scores_mtx)
def test_iteration_manual(self): y_1stmoments = [] y_2ndmoments = [] with YuvReader( filepath=VmafConfig.test_resource_path("yuv", "src01_hrc01_576x324.yuv"), width=576, height=324, yuv_type='yuv420p') as yuv_reader: while True: try: y, u, v = yuv_reader.next() y, u, v = y.astype(np.double), u.astype(np.double), v.astype(np.double) y_1stmoments.append(y.mean()) y_2ndmoments.append(y.var() + y.mean() * y.mean()) except StopIteration: break self.assertEqual(len(y_1stmoments), 48) self.assertEqual(len(y_2ndmoments), 48) self.assertAlmostEqual(float(np.mean(y_1stmoments)), 61.332006624999984, places=4) self.assertAlmostEqual(float(np.mean(y_2ndmoments)), 4798.659574041666, places=4)
def _open_dis_procfile(self, asset, fifo_mode): # only need to open dis procfile if the path is different from dis path assert asset.use_workpath_as_procpath is False and asset.dis_workfile_path != asset.dis_procfile_path dis_proc_callback = asset.dis_proc_callback if asset.dis_proc_callback is not None else lambda x: x if fifo_mode: os.mkfifo(asset.dis_procfile_path) quality_width, quality_height = self._get_quality_width_height(asset) yuv_type = asset.workfile_yuv_type with YuvReader(filepath=asset.dis_workfile_path, width=quality_width, height=quality_height, yuv_type=yuv_type) as dis_yuv_reader: with YuvWriter(filepath=asset.dis_procfile_path, width=quality_width, height=quality_height, yuv_type=yuv_type) as dis_yuv_writer: while True: try: y, u, v = dis_yuv_reader.next(format='float') y, u, v = dis_proc_callback(y), u, v dis_yuv_writer.next(y, u, v, format='float2uint') except StopIteration: break
def test_next_y_u_v_gray(self): with YuvReader(filepath=VmafConfig.test_resource_path( "yuv", "src01_hrc00_576x324.yuv"), width=576, height=324, yuv_type='gray') as yuv_reader: y, u, v = yuv_reader.next() y = y.astype(np.double) self.assertEqual(y[0][0], 87) self.assertTrue(u is None) self.assertTrue(v is None) self.assertAlmostEqual(y.mean(), 61.928749785665296, places=4) y, u, v = yuv_reader.next() y = y.astype(np.double) self.assertEqual(y[0][0], 92) self.assertTrue(u is None) self.assertTrue(v is None)
def test_iteration(self): y_1stmoments = [] y_2ndmoments = [] with YuvReader(filepath=VmafConfig.test_resource_path( "yuv", "src01_hrc01_576x324.yuv"), width=576, height=324, yuv_type='yuv420p') as yuv_reader: for y, u, v in yuv_reader: y_1stmoments.append(y.mean()) y_2ndmoments.append(y.var() + y.mean() * y.mean()) self.assertEqual(len(y_1stmoments), 48) self.assertEqual(len(y_2ndmoments), 48) self.assertAlmostEqual(np.mean(y_1stmoments), 61.332006624999984, places=4) self.assertAlmostEqual(np.mean(y_2ndmoments), 4798.659574041666, places=4)
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.dis_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