def test_yuv_reader(self): yuv_reader = YuvReader( filepath=config.ROOT + "/resource/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 test_yuv_reader(self): yuv_reader = YuvReader(filepath=config.ROOT + "/resource/yuv/src01_hrc00_576x324.yuv", width=576, height=324, yuv_type='yuv420p') self.assertEquals(yuv_reader.num_bytes, 13436928) self.assertEquals(yuv_reader.num_frms, 48) self.assertEquals(yuv_reader._get_uv_width_height_multiplier(), (0.5, 0.5))
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 ref_scores_mtx = None with YuvReader(filepath=asset.ref_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type( asset.yuv_type)) as ref_yuv_reader: scores_mtx_list = [] i = 0 for ref_yuv in ref_yuv_reader: ref_y = ref_yuv[0] firstm = ref_y.mean() secondm = ref_y.var() + firstm**2 scores_mtx_list.append(np.hstack(([firstm], [secondm]))) i += 1 ref_scores_mtx = np.vstack(scores_mtx_list) dis_scores_mtx = None with YuvReader(filepath=asset.dis_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type( asset.yuv_type)) 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 dis_scores_mtx = np.vstack(scores_mtx_list) assert ref_scores_mtx is not None and dis_scores_mtx is not None log_dict = { 'ref_scores_mtx': ref_scores_mtx.tolist(), 'dis_scores_mtx': dis_scores_mtx.tolist() } log_file_path = self._get_log_file_path(asset) with open(log_file_path, 'wt') as log_file: log_file.write(str(log_dict))
def test_with(self): with YuvReader(filepath=config.ROOT + "/resource/yuv/src01_hrc00_576x324.yuv", width=576, height=324, yuv_type='yuv420p') as yuv_reader: self.assertEquals(yuv_reader.file.__class__, file)
def _generate_result(self, asset): quality_w, quality_h = asset.quality_width_height # count number of frames dis_ys = [] dis_us = [] dis_vs = [] with YuvReader(filepath=asset.dis_workfile_path, width=quality_w, height=quality_h, yuv_type=self._get_workfile_yuv_type( asset.yuv_type)) as dis_yuv_reader: for dis_yuv in dis_yuv_reader: dis_y, dis_u, dis_v = dis_yuv dis_ys.append(dis_y) dis_us.append(dis_u) dis_vs.append(dis_v) # Y if 'y' in self.channels.lower(): h5py_cache_y = self.h5py_file.create_dataset( str(asset) + '_y', (len(dis_ys), dis_ys[0].shape[0], dis_ys[0].shape[1]), dtype='float') h5py_cache_y.dims[0].label = 'frame' h5py_cache_y.dims[1].label = 'height' h5py_cache_y.dims[2].label = 'width' for idx, dis_y in enumerate(dis_ys): h5py_cache_y[idx] = dis_y # U if 'u' in self.channels.lower(): h5py_cache_u = self.h5py_file.create_dataset( str(asset) + '_u', (len(dis_us), dis_us[0].shape[0], dis_us[0].shape[1]), dtype='float') h5py_cache_u.dims[0].label = 'frame' h5py_cache_u.dims[1].label = 'height' h5py_cache_u.dims[2].label = 'width' for idx, dis_u in enumerate(dis_us): h5py_cache_u[idx] = dis_u # V if 'v' in self.channels.lower(): h5py_cache_v = self.h5py_file.create_dataset( str(asset) + '_v', (len(dis_vs), dis_vs[0].shape[0], dis_vs[0].shape[1]), dtype='float') h5py_cache_v.dims[0].label = 'frame' h5py_cache_v.dims[1].label = 'height' h5py_cache_v.dims[2].label = 'width' for idx, dis_v in enumerate(dis_vs): h5py_cache_v[idx] = dis_v
def test_iteration(self): y_1stmoments = [] y_2ndmoments = [] with YuvReader( filepath=config.ROOT + "/resource/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.assertEquals(len(y_1stmoments), 48) self.assertEquals(len(y_2ndmoments), 48) self.assertAlmostEquals(np.mean(y_1stmoments), 61.332006624999984, places=4) self.assertAlmostEquals(np.mean(y_2ndmoments), 4798.659574041666, places=4)
def test_next_y_u_v(self): with YuvReader( filepath=config.ROOT + "/resource/yuv/src01_hrc00_576x324.yuv", width=576, height=324, yuv_type='yuv420p' ) as yuv_reader: y, u, v = yuv_reader.next_y_u_v() self.assertEquals(y[0][0], 87) self.assertEquals(y[0][1], 131) self.assertEquals(y[1][0], 95) self.assertEquals(u[0][0], 92) self.assertEquals(u[0][1], 97) self.assertEquals(u[1][0], 90) self.assertEquals(v[0][0], 121) self.assertEquals(v[0][1], 126) self.assertEquals(v[1][0], 122) self.assertAlmostEquals(y.mean(), 61.928749785665296, places=4) self.assertAlmostEquals(u.mean(), 114.6326517489712, places=4) self.assertAlmostEquals(v.mean(), 122.05084019204389, places=4) y, u, v = yuv_reader.next_y_u_v() self.assertEquals(y[0][0], 142) self.assertEquals(y[0][1], 128) self.assertEquals(y[1][0], 134) self.assertEquals(u[0][0], 93) self.assertEquals(u[0][1], 102) self.assertEquals(u[1][0], 91) self.assertEquals(v[0][0], 128) self.assertEquals(v[0][1], 126) self.assertEquals(v[1][0], 124) self.assertAlmostEquals(y.mean(), 61.265260631001375, places=4) self.assertAlmostEquals(u.mean(), 114.72515860768175, places=4) self.assertAlmostEquals(v.mean(), 122.12022033607681, places=4)
def test_with(self): with YuvReader( filepath=config.ROOT + "/resource/yuv/src01_hrc00_576x324.yuv422p10le.yuv", width=576, height=324, yuv_type='yuv422p10le' ) as yuv_reader: y, u, v = yuv_reader.next_y_u_v() self.assertEquals(y[0][0], 87) self.assertEquals(y[0][1], 131) self.assertEquals(y[1][0], 95) self.assertEquals(u[0][0], 92.25) self.assertEquals(u[0][1], 97.5) self.assertEquals(u[1][0], 91.75) self.assertEquals(v[0][0], 121) self.assertEquals(v[0][1], 126.25) self.assertEquals(v[1][0], 121.25) self.assertAlmostEquals(y.mean(), 61.928749785665296, places=4) self.assertAlmostEquals(u.mean(), 114.63283661265432, places=4) self.assertAlmostEquals(v.mean(), 122.05113490226337, places=4) y, u, v = yuv_reader.next_y_u_v() self.assertEquals(y[0][0], 142) self.assertEquals(y[0][1], 128) self.assertEquals(y[1][0], 134) self.assertEquals(u[0][0], 93.25) self.assertEquals(u[0][1], 102.75) self.assertEquals(u[1][0], 92.75) self.assertEquals(v[0][0], 128.25) self.assertEquals(v[0][1], 126.5) self.assertEquals(v[1][0], 127.25) self.assertAlmostEquals(y.mean(), 61.265260631001375, places=4) self.assertAlmostEquals(u.mean(), 114.72527917095336, places=4) self.assertAlmostEquals(v.mean(), 122.12047217935527, places=4)
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.yuv_type)) \ 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 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