Esempio n. 1
0
    def __init__(self, settings, key_bindings):
        super(KerasVisApp, self).__init__(settings, key_bindings)
        print 'Got settings', settings
        self.settings = settings
        self.bindings = key_bindings

        self._net_channel_swap = (2, 1, 0)
        self._net_channel_swap_inv = tuple([
            self._net_channel_swap.index(ii)
            for ii in range(len(self._net_channel_swap))
        ])
        self._range_scale = 1.0  # not needed; image already in [0,255]

        if isinstance(settings.kerasvis_deploy_model, basestring):
            self.net = load_model(settings.kerasvis_deploy_model)
            # self.net._make_predict_function() This doesn't look necessary
            self.graph = tf.get_default_graph()
            print('KerasVisApp: model has been loaded')
        elif isinstance(settings.kerasvis_deploy_model, list):
            self.nets = [load_model(m) for m in settings.kerasvis_deploy_model]

        self.labels = None
        if self.settings.kerasvis_labels:
            self.labels = read_label_file(self.settings.kerasvis_labels)
        self.proc_thread = None
        self.jpgvis_thread = None
        self.handled_frames = 0
        if settings.kerasvis_jpg_cache_size < 10 * 1024**2:
            raise Exception(
                'kerasvis_jpg_cache_size must be at least 10MB for normal operation.'
            )
        self.img_cache = FIFOLimitedArrayCache(
            settings.kerasvis_jpg_cache_size)

        self._populate_net_layer_info()
Esempio n. 2
0
    def __init__(self, settings, key_bindings):
        super(CaffeVisApp, self).__init__(settings, key_bindings)
        print 'Got settings', settings
        self.settings = settings
        self.bindings = key_bindings

        sys.path.insert(0, os.path.join(settings.caffevis_caffe_root, 'python'))
        import caffe

        try:
            self._data_mean = np.load(settings.caffevis_data_mean)
        except IOError:
            print '\n\nCound not load mean file:', settings.caffevis_data_mean
            print 'Ensure that the values in settings.py point to a valid model weights file, network'
            print 'definition prototxt, and mean. To fetch a default model and mean file, use:\n'
            print '$ cd models/caffenet-yos/'
            print '$ ./fetch.sh\n\n'
            raise

        # Crop center region (e.g. 227x227) if mean is larger (e.g. 256x256)
        excess_h = self._data_mean.shape[1] - self.settings.caffevis_data_hw[0]
        excess_w = self._data_mean.shape[2] - self.settings.caffevis_data_hw[1]
        assert excess_h >= 0 and excess_w >= 0, 'mean should be at least as large as %s' % repr(self.settings.caffevis_data_hw)
        self._data_mean = self._data_mean[:, excess_h:(excess_h+self.settings.caffevis_data_hw[0]),
                                          excess_w:(excess_w+self.settings.caffevis_data_hw[1])]
        self._net_channel_swap = (2,1,0)
        self._net_channel_swap_inv = tuple([self._net_channel_swap.index(ii) for ii in range(len(self._net_channel_swap))])
        self._range_scale = 1.0      # not needed; image comes in [0,255]
        #self.net.set_phase_test()
        #if settings.caffevis_mode_gpu:
        #    self.net.set_mode_gpu()
        #    print 'CaffeVisApp mode: GPU'
        #else:
        #    self.net.set_mode_cpu()
        #    print 'CaffeVisApp mode: CPU'
        # caffe.set_phase_test()       # TEST is default now
        if settings.caffevis_mode_gpu:
            caffe.set_mode_gpu()
            print 'CaffeVisApp mode: GPU'
        else:
            caffe.set_mode_cpu()
            print 'CaffeVisApp mode: CPU'
        self.net = caffe.Classifier(
            settings.caffevis_deploy_prototxt,
            settings.caffevis_network_weights,
            mean = self._data_mean,
            channel_swap = self._net_channel_swap,
            raw_scale = self._range_scale,
            #image_dims = (227,227),
        )

        self.labels = None
        if self.settings.caffevis_labels:
            self.labels = read_label_file(self.settings.caffevis_labels)
        self.proc_thread = None
        self.jpgvis_thread = None
        self.handled_frames = 0
        if settings.caffevis_jpg_cache_size < 10*1024**2:
            raise Exception('caffevis_jpg_cache_size must be at least 10MB for normal operation.')
        self.img_cache = FIFOLimitedArrayCache(settings.caffevis_jpg_cache_size)
Esempio n. 3
0
    def __init__(self, settings, key_bindings):
        '''Initialize a new visualization application.

        Arguments:
        :param settings: the settings object to be used to configure
            this application
        :param key_bindings: the key_bindings object assigning
            key_codes to tags
        '''
        super(VisApp, self).__init__(settings, key_bindings)

        print 'Got settings', settings
        self.settings = settings
        self.bindings = key_bindings

        # for statistics/debugging, used in self.handle_input()
        self.handled_frames = 0

        # to be initialized in self.start():
        self.proc_thread = None
        self.jpgvis_thread = None

        # initialize the image cache
        # (ULF: shouldn't that be done by jpgvis_thread?)
        if settings.caffevis_jpg_cache_size < 10 * 1024**2:
            raise Exception(
                'caffevis_jpg_cache_size must be at least 10MB for normal operation.'
            )
        self.img_cache = FIFOLimitedArrayCache(
            settings.caffevis_jpg_cache_size)

        # read the class labels
        self.labels = None
        if self.settings.caffevis_labels:
            self.labels = read_label_file(self.settings.caffevis_labels)

        network_path, network_class_name = self.settings.network
        network_module = importlib.import_module(network_path)
        print 'debug[app]: VisApp.__init__: got module', network_module
        network_class = getattr(network_module, network_class_name)
        print 'debug[app]: VisApp.__init__: got class', network_class
        self.my_net = network_class(self.settings)
Esempio n. 4
0
    def __init__(self, settings, key_bindings):
        super(CaffeVisApp, self).__init__(settings, key_bindings)
        print('Got settings', settings)
        self.settings = settings
        self.bindings = key_bindings

        self._net_channel_swap = settings.caffe_net_channel_swap

        if self._net_channel_swap is None:
            self._net_channel_swap_inv = None
        else:
            self._net_channel_swap_inv = tuple([
                self._net_channel_swap.index(ii)
                for ii in range(len(self._net_channel_swap))
            ])

        self._range_scale = 1.0  # not needed; image already in [0,255]

        # Set the mode to CPU or GPU. Note: in the latest Caffe
        # versions, there is one Caffe object *per thread*, so the
        # mode must be set per thread! Here we set the mode for the
        # main thread; it is also separately set in CaffeProcThread.
        sys.path.insert(0, os.path.join(settings.caffevis_caffe_root,
                                        'python'))
        import caffe
        if settings.caffevis_mode_gpu:
            caffe.set_mode_gpu()
            print('CaffeVisApp mode (in main thread):     GPU')
        else:
            caffe.set_mode_cpu()
            print('CaffeVisApp mode (in main thread):     CPU')
        self.net = caffe.Classifier(
            settings.caffevis_deploy_prototxt,
            settings.caffevis_network_weights,
            mean=
            None,  # Set to None for now, assign later         # self._data_mean,
            channel_swap=self._net_channel_swap,
            raw_scale=self._range_scale,
        )

        if isinstance(settings.caffevis_data_mean, str):
            # If the mean is given as a filename, load the file
            try:

                filename, file_extension = os.path.splitext(
                    settings.caffevis_data_mean)
                if file_extension == ".npy":
                    # load mean from numpy array
                    self._data_mean = np.load(settings.caffevis_data_mean)
                    print("Loaded mean from numpy file, data_mean.shape: ",
                          self._data_mean.shape)

                elif file_extension == ".binaryproto":

                    # load mean from binary protobuf file
                    blob = caffe.proto.caffe_pb2.BlobProto()
                    data = open(settings.caffevis_data_mean, 'rb').read()
                    blob.ParseFromString(data)
                    self._data_mean = np.array(
                        caffe.io.blobproto_to_array(blob))
                    self._data_mean = np.squeeze(self._data_mean)
                    print(
                        "Loaded mean from binaryproto file, data_mean.shape: ",
                        self._data_mean.shape)

                else:
                    # unknown file extension, trying to load as numpy array
                    self._data_mean = np.load(settings.caffevis_data_mean)
                    print("Loaded mean from numpy file, data_mean.shape: ",
                          self._data_mean.shape)

            except IOError:
                print('\n\nCound not load mean file:',
                      settings.caffevis_data_mean)
                print(
                    'Ensure that the values in settings.py point to a valid model weights file, network'
                )
                print(
                    'definition prototxt, and mean. To fetch a default model and mean file, use:\n'
                )
                print('$ cd models/caffenet-yos/')
                print('$ ./fetch.sh\n\n')
                raise
            input_shape = self.net.blobs[self.net.inputs[0]].data.shape[
                -2:]  # e.g. 227x227
            # Crop center region (e.g. 227x227) if mean is larger (e.g. 256x256)
            excess_h = self._data_mean.shape[1] - input_shape[0]
            excess_w = self._data_mean.shape[2] - input_shape[1]
            print('input_shape[0]:%s' % input_shape[0])
            print('input_shape[1]:%s' % input_shape[1])
            print('%s:%s' % (excess_w / 2, excess_w / 2 + input_shape[1]))
            assert excess_h >= 0 and excess_w >= 0, 'mean should be at least as large as %s' % repr(
                input_shape)
            self._data_mean = self._data_mean[:,
                                              (excess_h // 2):(excess_h // 2 +
                                                               input_shape[0]),
                                              (excess_w // 2):(excess_w // 2 +
                                                               input_shape[1])]
        elif settings.caffevis_data_mean is None:
            self._data_mean = None
        else:
            # The mean has been given as a value or a tuple of values
            self._data_mean = np.array(settings.caffevis_data_mean)
            # Promote to shape C,1,1
            while len(self._data_mean.shape) < 1:
                self._data_mean = np.expand_dims(self._data_mean, -1)

            #if not isinstance(self._data_mean, tuple):
            #    # If given as int/float: promote to tuple
            #    self._data_mean = tuple(self._data_mean)
        if self._data_mean is not None:
            self.net.transformer.set_mean(self.net.inputs[0], self._data_mean)

        check_force_backward_true(settings.caffevis_deploy_prototxt)

        self.labels = None
        if self.settings.caffevis_labels:
            self.labels = read_label_file(self.settings.caffevis_labels)
        self.proc_thread = None
        self.jpgvis_thread = None
        self.handled_frames = 0
        if settings.caffevis_jpg_cache_size < 10 * 1024**2:
            raise Exception(
                'caffevis_jpg_cache_size must be at least 10MB for normal operation.'
            )
        self.img_cache = FIFOLimitedArrayCache(
            settings.caffevis_jpg_cache_size)

        self._populate_net_layer_info()
Esempio n. 5
0
    def __init__(self, settings, key_bindings):
        super(CaffeVisApp, self).__init__(settings, key_bindings)
        print 'Got settings', settings
        self.settings = settings
        self.bindings = key_bindings

        self._net_channel_swap = (2, 1, 0)
        self._net_channel_swap_inv = tuple([
            self._net_channel_swap.index(ii)
            for ii in range(len(self._net_channel_swap))
        ])
        self._range_scale = 1.0  # not needed; image already in [0,255]

        # Set the mode to CPU or GPU. Note: in the latest Caffe
        # versions, there is one Caffe object *per thread*, so the
        # mode must be set per thread! Here we set the mode for the
        # main thread; it is also separately set in CaffeProcThread.
        sys.path.insert(0, os.path.join(settings.caffevis_caffe_root,
                                        'python'))
        import caffe
        if settings.caffevis_mode_gpu:
            caffe.set_mode_gpu()
            print 'CaffeVisApp mode (in main thread):     GPU'
        else:
            caffe.set_mode_cpu()
            print 'CaffeVisApp mode (in main thread):     CPU'
        self.net = caffe.Classifier(
            settings.caffevis_deploy_prototxt,
            settings.caffevis_network_weights,
            mean=
            None,  # Set to None for now, assign later         # self._data_mean,
            channel_swap=self._net_channel_swap,
            raw_scale=self._range_scale,
        )

        if isinstance(settings.caffevis_data_mean, basestring):
            # If the mean is given as a filename, load the file
            try:
                self._data_mean = np.load(settings.caffevis_data_mean)
            except IOError:
                print '\n\nCound not load mean file:', settings.caffevis_data_mean
                print 'Ensure that the values in settings.py point to a valid model weights file, network'
                print 'definition prototxt, and mean. To fetch a default model and mean file, use:\n'
                print '$ cd models/caffenet-yos/'
                print '$ ./fetch.sh\n\n'
                raise
            if settings.caffevis_should_preproc_mean:
                assert self._data_mean.shape[
                    2] == 3, '3-rd dimensions must be color channels!'
                self._data_mean = self._data_mean[:, :, ::
                                                  -1]  # change RGB to BGR
                self._data_mean = self._data_mean.transpose(
                    (2, 0, 1))  # height*width*channel -> channel*height*width

            input_shape = self.net.blobs[self.net.inputs[0]].data.shape[
                -2:]  # e.g. 227x227
            # Crop center region (e.g. 227x227) if mean is larger (e.g. 256x256)
            excess_h = self._data_mean.shape[1] - input_shape[0]
            excess_w = self._data_mean.shape[2] - input_shape[1]
            assert excess_h >= 0 and excess_w >= 0, 'mean should be at least as large as %s' % repr(
                input_shape)
            self._data_mean = self._data_mean[:,
                                              (excess_h / 2):(excess_h / 2 +
                                                              input_shape[0]),
                                              (excess_w / 2):(excess_w / 2 +
                                                              input_shape[1])]

        elif settings.caffevis_data_mean is None:
            self._data_mean = None
        else:
            # The mean has been given as a value or a tuple of values
            self._data_mean = np.array(settings.caffevis_data_mean)
            # Promote to shape C,1,1
            while len(self._data_mean.shape) < 1:
                self._data_mean = np.expand_dims(self._data_mean, -1)

            #if not isinstance(self._data_mean, tuple):
            #    # If given as int/float: promote to tuple
            #    self._data_mean = tuple(self._data_mean)
        if self._data_mean is not None:
            self.net.transformer.set_mean(self.net.inputs[0], self._data_mean)

        check_force_backward_true(settings.caffevis_deploy_prototxt)

        self.labels = None
        if self.settings.caffevis_labels:
            self.labels = read_label_file(self.settings.caffevis_labels)
        self.proc_thread = None
        self.jpgvis_thread = None
        self.handled_frames = 0
        if settings.caffevis_jpg_cache_size < 10 * 1024**2:
            raise Exception(
                'caffevis_jpg_cache_size must be at least 10MB for normal operation.'
            )
        self.img_cache = FIFOLimitedArrayCache(
            settings.caffevis_jpg_cache_size)

        self._populate_net_layer_info()