def process_frame(self, frame, model, verbose=1):
        """

        :param frame:
        :param BaseModel model:
        :return list: should be a list with the prediction (because of compatibility with warping prediciton)
        """

        frame = cv2.resize(frame, config.target_size()[::-1])
        frame_norm = datagen.normalize(frame, config.target_size())

        input = [np.array([frame_norm])]
        return [model.k.predict(input, 1, verbose)]
    def process_frame_warping(self,
                              frame,
                              last_frame,
                              model,
                              last_prediction=None,
                              verbose=1):
        """

        :param frame:
        :param last_frame:
        :param BaseModel model:
        :param verbose:
        :return:
        """

        flow = datagen.calc_optical_flow(frame, last_frame)
        frame_norm = datagen.normalize(frame, config.target_size())
        last_frame_norm = datagen.normalize(last_frame, config.target_size())

        input_with_flow = [
            np.array([last_frame_norm]),
            np.array([frame_norm]),
            np.array([flow])
        ]

        if last_prediction is not None:
            input_with_flow += last_prediction[1:]
        else:
            # takes all layers to warp (should be specified by model) and creates array of ones of the same shape
            input_with_flow += [
                np.ones((1, ) + out_shape[1:])
                for out_shape in model.k.output_shape[1:]
            ]

        all_predictions = model.k.predict(input_with_flow, 1, verbose)
        return all_predictions
        parser.add_argument('--gid', help='GPU id', default=None)

        parser.add_argument('-i',
                            '--input',
                            help='Input file',
                            default='/home/mlyko/data/stuttgart_00.mp4')

        parser.add_argument('-o', '--output', help='Output file', default=None)

        args = parser.parse_args()
        return args

    args = parse_arguments()

    size = config.target_size()

    videoEvaluator = VideoEvaluator()
    videoEvaluator.select_device(args.gid)

    datagen = CityscapesFlowGenerator(config.data_path())

    videoEvaluator.load_model({
        'model':
        ICNet(config.target_size(), datagen.n_classes, for_training=False),
        'weights':
        config.weights_path() +
        'city/rel/ICNet/1612:37e200.b8.lr-0.001000._dec-0.000000.of-farn.h5',
        'warp':
        False
    })
    def process_video(self,
                      datagen,
                      input_file,
                      skip_from_start=0,
                      until_frame=None):
        # may process more models
        for model_params in self._models:
            # load input video
            vid, fps = self._open_video(input_file)
            try:
                # load model
                model = model_params['model']
                model.k.load_weights(model_params['weights'], by_name=True)
                model.compile()

                print('-- predicting model %s' % model.name)

                # prepare output file for the model and input file
                out = self._prepare_output(input_file, model, fps)
                # reset old state
                self._last_frame = None
                self._last_prediction = None
                frame_i = 0

                while True:
                    ret, frame = vid.read()
                    if not ret:
                        vid.release()
                        print("-- Released Video Resource")
                        break

                    # skipping boring part of video
                    if frame_i < skip_from_start:
                        frame_i += 1
                        continue

                    # cut from end
                    if until_frame is not None and frame_i > until_frame:
                        print(" -- Finishing at frame %d" % until_frame)
                        break

                    frame = cv2.resize(frame, config.target_size()[::-1])

                    if model_params['warp']:
                        if self._last_frame is None:
                            self._last_frame = frame

                        predictions = self.process_frame_warping(
                            frame, self._last_frame, model,
                            self._last_prediction)
                    else:
                        predictions = self.process_frame(frame, model)

                    colored_prediction = datagen.one_hot_to_bgr(
                        predictions[0], config.target_size(),
                        datagen.n_classes, datagen.labels)
                    out.write(colored_prediction)

                    print('-- processed frame %d' % frame_i)
                    self._last_frame = frame
                    self._last_prediction = predictions
                    frame_i += 1
                out.release()
                vid.release()

                print("-- Finished input stream")
            except KeyboardInterrupt:
                # Release the Video Device
                vid.release()
                # Message to be displayed after releasing the device
                print("-- Released Video Resource")
Ejemplo n.º 5
0
    def parse_arguments():
        parser = argparse.ArgumentParser(description='Train model in keras')
        parser.add_argument(
            '-r', '--restart',
            action='store_true',
            help='Restarts training from last saved epoch',
            default=False
        )

        parser.add_argument(
            '-d', '--debug',
            help='Just debug training (number to pick from dataset)',
            default=0
        )

        parser.add_argument(
            '--summaries',
            action='store_true',
            help='If should plot model and summary',
            default=False)

        parser.add_argument(
            '-g', '--gpus',
            help='Number of GPUs used for training',
            default=1
        )

        parser.add_argument(
            '-m', '--model',
            help='Model to train [segnet, mobile_unet]',
            default='segnet'
        )

        parser.add_argument(
            '-b', '--batch',
            help='Batch size',
            default=2
        )

        parser.add_argument(
            '-e', '--epochs',
            help='Number of epochs',
            default=200
        )

        parser.add_argument(
            '-s', '--stop',
            help='Early stopping',
            default=20
        )

        parser.add_argument(
            '--gid',
            help='GPU id',
            default=None
        )

        parser.add_argument(
            '-lr',
            help='Learning rate',
            default=None
        )

        parser.add_argument(
            '--dec',
            help='Learning rate decay',
            default=None
        )

        parser.add_argument(
            '-n', '--name',
            help='Run Name',
            default=strftime("%d%H:%M", gmtime())
        )

        parser.add_argument(
            '-o', '--optic',
            help='Optical flow',
            default='farn'
        )

        parser.add_argument(
            '--height',
            help='Target image height',
            default=config.target_size()[0]
        )

        parser.add_argument(
            '--width',
            help='Target image width',
            default=config.target_size()[1]
        )

        parser.add_argument(
            '--aug',
            help='Data Augmentation',
            default=False
        )

        parser.add_argument(
            '--workers',
            help='Workers',
            default=1
        )
        parser.add_argument(
            '--multiprocess',
            help='Multiprocess',
            default=False
        )

        parser.add_argument(
            '--queue',
            help='Max queue',
            default=50
        )

        parser.add_argument(
            '--gpu_percent',
            help='How much GPU memory will be taken',
            default=None
        )

        args = parser.parse_args()
        return args