def test_should_return_all_frames_when_no_predicate_is_applied(self): frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(1, 2 * np.ones((1, 1)), None) frame_3 = Frame(1, 3 * np.ones((1, 1)), None) outcome_1 = Prediction(frame_1, ["car", "bus"], [0.5, 0.6]) outcome_2 = Prediction(frame_2, ["bus"], [0.5, 0.6]) outcome_3 = Prediction(frame_3, ["car", "train"], [0.5, 0.6]) batch = FrameBatch( frames=[ frame_1, frame_2, frame_3, ], info=None, outcomes={"test": [outcome_1, outcome_2, outcome_3]}) plan = type("ScanPlan", (), {"predicate": None}) predicate_executor = SequentialScanExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) expected = FrameBatch( frames=[frame_1, frame_2, frame_3], info=None, outcomes={"test": [outcome_1, outcome_2, outcome_3]}) filtered = list(predicate_executor.next())[0] self.assertEqual(expected, filtered)
def test_should_return_the_new_path_after_execution(self, mock_class): class_instatnce = mock_class.return_value dummy_expr = type('dummy_expr', (), {"evaluate": lambda x=None: [True, False, True]}) # Build plan tree video = VideoMetaInfo("dummy.avi", 10, VideoFormat.AVI) class_instatnce.load.return_value = map( lambda x: x, [FrameBatch([1, 2, 3], None), FrameBatch([4, 5, 6], None)]) storage_plan = StoragePlan(video) seq_scan = SeqScanPlan(predicate=dummy_expr) seq_scan.append_child(storage_plan) # Execute the plan executor = PlanExecutor(seq_scan) actual = executor.execute_plan() expected = [FrameBatch([1, 3], None), FrameBatch([4, 6], None)] mock_class.assert_called_once() self.assertEqual(expected, actual)
def test_slicing_on_batched_should_return_new_batch_frame(self): batch = FrameBatch(frames=create_dataframe(2), outcomes={'test': [[None], [None]]}) expected = FrameBatch(frames=create_dataframe(), outcomes={'test': [[None]]}) self.assertEqual(batch, batch[:]) self.assertEqual(expected, batch[:-1])
def test_set_outcomes_method_should_set_temp_outcome_when_bool_is_true( self): batch = FrameBatch(frames=create_dataframe()) batch.set_outcomes('test', [1], is_temp=True) expected = FrameBatch(frames=create_dataframe(), temp_outcomes={'test': [1]}) self.assertEqual(expected, batch)
def load(self): video = cv2.VideoCapture(self.video_metadata.file) video_start = self.offset if self.offset else 0 video.set(cv2.CAP_PROP_POS_FRAMES, video_start) LoggingManager().log("Loading frames", LoggingLevel.CRITICAL) _, frame = video.read() frame_ind = video_start - 1 info = None if frame is not None: (height, width, num_channels) = frame.shape info = FrameInfo(height, width, num_channels, ColorSpace.BGR) frames = [] while frame is not None: frame_ind += 1 eva_frame = Frame(frame_ind, frame, info) if self.skip_frames > 0 and frame_ind % self.skip_frames != 0: _, frame = video.read() continue frames.append(eva_frame) if self.limit and frame_ind >= self.limit: return FrameBatch(frames, info) if len(frames) % self.batch_size == 0: yield FrameBatch(frames, info) frames = [] _, frame = video.read() if frames: return FrameBatch(frames, info)
def test_should_return_only_frames_satisfy_predicate(self): frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(1, 2 * np.ones((1, 1)), None) frame_3 = Frame(1, 3 * np.ones((1, 1)), None) outcome_1 = Prediction(frame_1, ["car", "bus"], [0.5, 0.6]) outcome_2 = Prediction(frame_2, ["bus"], [0.5, 0.6]) outcome_3 = Prediction(frame_3, ["car", "train"], [0.5, 0.6]) batch = FrameBatch( frames=[ frame_1, frame_2, frame_3, ], info=None, outcomes={"test": [outcome_1, outcome_2, outcome_3]}) expression = type("AbstractExpression", (), {"evaluate": lambda x: [False, False, True]}) plan = type("ScanPlan", (), {"predicate": expression}) predicate_executor = SequentialScanExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) expected = FrameBatch(frames=[frame_3], info=None, outcomes={"test": [outcome_3]}) filtered = list(predicate_executor.next())[0] self.assertEqual(expected, filtered)
def test_should_return_the_new_path_after_execution(self, mock_class): class_instatnce = mock_class.return_value dummy_expr = type('dummy_expr', (), {"evaluate": lambda x=None: [True, False, True]}) # Build plan tree video = DataFrameMetadata("dataset", "dummy.avi") batch_1 = FrameBatch(pd.DataFrame({'data': [1, 2, 3]})) batch_2 = FrameBatch(pd.DataFrame({'data': [4, 5, 6]})) class_instatnce.load.return_value = map(lambda x: x, [batch_1, batch_2]) storage_plan = StoragePlan(video) seq_scan = SeqScanPlan(predicate=dummy_expr, column_ids=[]) seq_scan.append_child(storage_plan) # Execute the plan executor = PlanExecutor(seq_scan) actual = executor.execute_plan() expected = [batch_1[::2], batch_2[::2]] mock_class.assert_called_once() self.assertEqual(expected, actual)
def test_set_outcomes_method_should_set_temp_outcome_when_bool_is_true( self): batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None) batch.set_outcomes('test', [1], is_temp=True) expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None, temp_outcomes={'test': [1]}) self.assertEqual(expected, batch)
def test_fetching_frames_by_index_should_also_return_temp_outcomes(self): batch = FrameBatch(frames=create_dataframe_same(2), outcomes={'test': [[1], [2]]}, temp_outcomes={'test2': [[3], [4]]}) expected = FrameBatch(frames=create_dataframe(), outcomes={'test': [[1]]}, temp_outcomes={'test2': [[3]]}) self.assertEqual(expected, batch[[0]])
def test_frames_as_numpy_array_should_frames_as_numpy_array(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, np.ones((1, 1)), None) ], info=None) expected = list(np.ones((2, 1, 1))) actual = list(batch.frames_as_numpy_array()) self.assertEqual(expected, actual)
def test_return_only_frames_specified_in_the_indices(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, np.ones((1, 1)), None) ], info=None) expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None) output = batch[[0]] self.assertEqual(expected, output)
def test_should_update_the_batch_with_outcomes_in_exec_mode(self): values = [1, 2, 3] expression = FunctionExpression(lambda x: values, mode=ExecutionMode.EXEC, name="test") expected_batch = FrameBatch(frames=pd.DataFrame(), outcomes={"test": [1, 2, 3]}) input_batch = FrameBatch(frames=pd.DataFrame()) expression.evaluate(input_batch) self.assertEqual(expected_batch, input_batch)
def test_fetching_frames_by_index_should_also_return_outcomes(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, np.ones((1, 1)), None) ], info=None, outcomes={'test': [[None], [None]]}) expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None, outcomes={'test': [[None]]}) self.assertEqual(expected, batch[[0]])
def test_slicing_should_word_for_negative_stop_value(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, 2 * np.ones((1, 1)), None) ], info=None, outcomes={'test': [[None], [None]]}) expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None, outcomes={'test': [[None]]}) self.assertEqual(expected, batch[:-1])
def test_should_update_temp_outcomes_when_is_temp_set_exec_mode(self): values = [1, 2, 3] expression = FunctionExpression(lambda x: values, mode=ExecutionMode.EXEC, name="test", is_temp=True) expected_batch = FrameBatch(frames=[], info=None, temp_outcomes={"test": [1, 2, 3]}) input_batch = FrameBatch(frames=[], info=None) expression.evaluate(input_batch) self.assertEqual(expected_batch, input_batch)
def test_slicing_on_batched_should_return_new_batch_frame(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, 2 * np.ones((1, 1)), None) ], info=None, outcomes={'test': [[None], [None]]}) expected = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None, outcomes={'test': [[None]]}) self.assertEqual(batch, batch[:]) self.assertEqual(expected, batch[:-1])
def evaluate(self, batch: FrameBatch): args = [] if self.get_children_count() > 0: child = self.get_child(0) args.append(child.evaluate(batch)) else: args.append(batch) outcome = self.function(*args) if self.mode == ExecutionMode.EXEC: batch.set_outcomes(self.name, outcome, is_temp=self.is_temp) return outcome
def test_has_outcomes_returns_true_if_the_given_name_is_in_outcomes(self): batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None) batch.set_outcomes('test_temp', [1], is_temp=True) batch.set_outcomes('test', [1]) self.assertTrue(batch.has_outcome('test')) self.assertTrue(batch.has_outcome('test_temp'))
def test_has_outcomes_returns_true_if_the_given_name_is_in_outcomes(self): batch = FrameBatch(frames=create_dataframe()) batch.set_outcomes('test_temp', [1], is_temp=True) batch.set_outcomes('test', [1]) self.assertTrue(batch.has_outcome('test')) self.assertTrue(batch.has_outcome('test_temp'))
def test_slicing_should_work_with_skip_value(self): batch = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, 2 * np.ones((1, 1)), None), Frame(1, np.ones((1, 1)), None) ], info=None, outcomes={'test': [[None], [None], [None]]}) expected = FrameBatch(frames=[ Frame(1, np.ones((1, 1)), None), Frame(1, np.ones((1, 1)), None) ], info=None, outcomes={'test': [[None], [None]]}) self.assertEqual(expected, batch[::2])
def test_frame_filtering_for_depth_estimation(self): """ Unit test method to test frame filtering functionality. it loops over frames and sends them over to frame filtering object's apply_filter method. Finally it verifies that depth mask is applied to the frames except every fifth one. """ # create two frames from kitti car dataset frame_1 = Frame( 1, self._load_image( os.path.join(self.base_path, 'data', 'kitti_car_1.png')), None) frame_2 = Frame( 1, self._load_image( os.path.join(self.base_path, 'data', 'kitti_car_2.png')), None) # create a batch of 2 frames frame_batch = FrameBatch([frame_1, frame_2], None) frames = frame_batch.frames_as_numpy_array() # initialize the frame filtering class object frame_filter = FrameFilter() # create a random depth mask array depth_mask = np.random.rand(frames[0].shape[0], frames[0].shape[1], frames[0].shape[2]) # iterate over frames in the batch for i, img in enumerate(frames): # apply frame filter on each frame img = frame_filter.apply_filter(img, depth_mask) # For every fifth frame the mask should not be applied. Hence, the # frame returned by apply_filter method should be same as original # frame if i % 5 == 0: self.assertTrue(np.array_equal(img, frames[0])) else: # Every other frame should be transformed after applying depth # mask self.assertTrue( np.array_equal(img, frames[i] * depth_mask[:, :, None]))
def test_logical_or(self): tpl_exp = TupleValueExpression(0) const_exp = ConstantValueExpression(1) comparison_expression_left = ComparisonExpression( ExpressionType.COMPARE_EQUAL, tpl_exp, const_exp) tpl_exp = TupleValueExpression(0) const_exp = ConstantValueExpression(1) comparison_expression_right = ComparisonExpression( ExpressionType.COMPARE_GREATER, tpl_exp, const_exp) logical_expr = LogicalExpression(ExpressionType.LOGICAL_OR, comparison_expression_left, comparison_expression_right) frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(2, 2 * np.ones((1, 1)), None) frame_3 = Frame(3, 3 * np.ones((1, 1)), None) input_batch = FrameBatch(frames=[ frame_1, frame_2, frame_3, ], info=None) expected_value = [[True], [True], [True]] output_value = logical_expr.evaluate(input_batch) self.assertEqual(expected_value, output_value)
def load_video(self, searchDir): print("load") self.path = searchDir (self.videoMetaList, self.labelList, self.labelMap) = self.findDataNames(self.path) videoMetaIndex = 0 while videoMetaIndex < len(self.videoMetaList): # Get a single batch frames = [] labels = np.zeros((0, 51)) while len(frames) < self.batchSize: # Load a single video meta = self.videoMetaList[videoMetaIndex] videoFrames, info = self.loadVideo(meta) videoLabels = np.zeros((len(videoFrames), 51)) videoLabels[:, self.labelList[videoMetaIndex]] = 1 videoMetaIndex += 1 # Skip unsupported frame types if info != FrameInfo(240, 320, 3, ColorSpace.RGB): continue # Append onto frames and labels frames += videoFrames labels = np.append(labels, videoLabels, axis=0) yield FrameBatch(frames, info), labels
def classify(self, batch: FrameBatch) -> List[Prediction]: frames = batch.frames_as_numpy_array() (pred_classes, pred_scores, pred_boxes) = self._get_predictions(frames) return Prediction.predictions_from_batch_and_lists(batch, pred_classes, pred_scores, boxes=pred_boxes)
def test_should_return_list_of_predictions_for_each_frame_in_batch(self): batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None) predictions = [['A', 'B']] scores = [[1, 1]] expected = [Prediction(batch.frames[0], predictions[0], scores[0])] actual = Prediction.predictions_from_batch_and_lists( batch, predictions, scores) self.assertEqual(expected, actual)
def test_should_check_if_batch_frames_equivalent_to_number_of_scores(self): batch = FrameBatch(frames=[Frame(1, np.ones((1, 1)), None)], info=None) predictions = [['A', 'B']] scores = [] self.assertRaises( AssertionError, lambda x=None: Prediction.predictions_from_batch_and_lists( batch, predictions, scores))
def test_should_return_only_frames_satisfy_predicate(self): frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(1, 2 * np.ones((1, 1)), None) frame_3 = Frame(1, 3 * np.ones((1, 1)), None) batch = FrameBatch(frames=[ frame_1, frame_2, frame_3, ], info=None) expression = type("AbstractExpression", (), {"evaluate": lambda x: [False, False, True]}) plan = type("PPScanPlan", (), {"predicate": expression}) predicate_executor = PPExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) expected = FrameBatch(frames=[frame_3], info=None) filtered = list(predicate_executor.next())[0] self.assertEqual(expected, filtered)
def load(self) -> Iterator[FrameBatch]: """ This is a generator for loading the frames of a video. Uses the video metadata and other class arguments Yields: :obj: `eva.models.FrameBatch`: An object containing a batch of frames and frame specific metadata """ frames = [] for frame in self._load_frames(): if self.skip_frames > 0 and frame.index % self.skip_frames != 0: continue if self.limit and frame.index >= self.limit: return FrameBatch(frames, frame.info) frames.append(frame) if len(frames) % self.batch_size == 0: yield FrameBatch(frames, frame.info) frames = [] if frames: return FrameBatch(frames, frames[0].info)
def test_should_return_only_frames_satisfy_predicate(self): dataframe = create_dataframe(3) batch = FrameBatch(frames=dataframe) expression = type("AbstractExpression", (), {"evaluate": lambda x: [ False, False, True]}) plan = type("PPScanPlan", (), {"predicate": expression}) predicate_executor = PPExecutor(plan) predicate_executor.append_child(DummyExecutor([batch])) expected = batch[[2]] filtered = list(predicate_executor.exec())[0] self.assertEqual(expected, filtered)
def test_func_expr_with_cmpr_and_const_expr_should_work(self): frame_1 = Frame(1, np.ones((1, 1)), None) frame_2 = Frame(1, 2 * np.ones((1, 1)), None) outcome_1 = Prediction(frame_1, ["car", "bus"], [0.5, 0.6]) outcome_2 = Prediction(frame_1, ["bus"], [0.6]) func = FunctionExpression(lambda x: [outcome_1, outcome_2]) value_expr = ConstantValueExpression("car") expression_tree = ComparisonExpression(ExpressionType.COMPARE_EQUAL, func, value_expr) batch = FrameBatch(frames=[frame_1, frame_2]) self.assertEqual([True, False], expression_tree.evaluate(batch))