Format input for Caffe: - convert to single - resize to input dimensions (preserving number of channels) - transpose dimensions to K x H x W - reorder channels (for instance color to BGR) - scale raw input (e.g. from [0, 1] to [0, 255] for ImageNet mo
(self, in_, data)
| 120 | in_, self.inputs)) |
| 121 | |
| 122 | def preprocess(self, in_, data): |
| 123 | """ |
| 124 | Format input for Caffe: |
| 125 | - convert to single |
| 126 | - resize to input dimensions (preserving number of channels) |
| 127 | - transpose dimensions to K x H x W |
| 128 | - reorder channels (for instance color to BGR) |
| 129 | - scale raw input (e.g. from [0, 1] to [0, 255] for ImageNet models) |
| 130 | - subtract mean |
| 131 | - scale feature |
| 132 | |
| 133 | Parameters |
| 134 | ---------- |
| 135 | in_ : name of input blob to preprocess for |
| 136 | data : (H' x W' x K) ndarray |
| 137 | |
| 138 | Returns |
| 139 | ------- |
| 140 | caffe_in : (K x H x W) ndarray for input to a Net |
| 141 | """ |
| 142 | self.__check_input(in_) |
| 143 | caffe_in = data.astype(np.float32, copy=False) |
| 144 | transpose = self.transpose.get(in_) |
| 145 | channel_swap = self.channel_swap.get(in_) |
| 146 | raw_scale = self.raw_scale.get(in_) |
| 147 | mean = self.mean.get(in_) |
| 148 | input_scale = self.input_scale.get(in_) |
| 149 | in_dims = self.inputs[in_][2:] |
| 150 | if caffe_in.shape[:2] != in_dims: |
| 151 | caffe_in = resize_image(caffe_in, in_dims) |
| 152 | if transpose is not None: |
| 153 | caffe_in = caffe_in.transpose(transpose) |
| 154 | if channel_swap is not None: |
| 155 | caffe_in = caffe_in[channel_swap, :, :] |
| 156 | if raw_scale is not None: |
| 157 | caffe_in *= raw_scale |
| 158 | if mean is not None: |
| 159 | caffe_in -= mean |
| 160 | if input_scale is not None: |
| 161 | caffe_in *= input_scale |
| 162 | return caffe_in |
| 163 | |
| 164 | def deprocess(self, in_, data): |
| 165 | """ |
no test coverage detected