Predict classification probabilities of inputs. Parameters ---------- inputs : iterable of (H x W x K) input ndarrays. oversample : boolean average predictions across center, corners, and mirrors when True (default). Center-only predi
(self, inputs, oversample=True)
| 45 | self.image_dims = image_dims |
| 46 | |
| 47 | def predict(self, inputs, oversample=True): |
| 48 | """ |
| 49 | Predict classification probabilities of inputs. |
| 50 | |
| 51 | Parameters |
| 52 | ---------- |
| 53 | inputs : iterable of (H x W x K) input ndarrays. |
| 54 | oversample : boolean |
| 55 | average predictions across center, corners, and mirrors |
| 56 | when True (default). Center-only prediction when False. |
| 57 | |
| 58 | Returns |
| 59 | ------- |
| 60 | predictions: (N x C) ndarray of class probabilities for N images and C |
| 61 | classes. |
| 62 | """ |
| 63 | # Scale to standardize input dimensions. |
| 64 | input_ = np.zeros((len(inputs), |
| 65 | self.image_dims[0], |
| 66 | self.image_dims[1], |
| 67 | inputs[0].shape[2]), |
| 68 | dtype=np.float32) |
| 69 | for ix, in_ in enumerate(inputs): |
| 70 | input_[ix] = caffe.io.resize_image(in_, self.image_dims) |
| 71 | |
| 72 | if oversample: |
| 73 | # Generate center, corner, and mirrored crops. |
| 74 | input_ = caffe.io.oversample(input_, self.crop_dims) |
| 75 | else: |
| 76 | # Take center crop. |
| 77 | center = np.array(self.image_dims) / 2.0 |
| 78 | crop = np.tile(center, (1, 2))[0] + np.concatenate([ |
| 79 | -self.crop_dims / 2.0, |
| 80 | self.crop_dims / 2.0 |
| 81 | ]) |
| 82 | crop = crop.astype(int) |
| 83 | input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :] |
| 84 | |
| 85 | # Classify |
| 86 | caffe_in = np.zeros(np.array(input_.shape)[[0, 3, 1, 2]], |
| 87 | dtype=np.float32) |
| 88 | for ix, in_ in enumerate(input_): |
| 89 | caffe_in[ix] = self.transformer.preprocess(self.inputs[0], in_) |
| 90 | out = self.forward_all(**{self.inputs[0]: caffe_in}) |
| 91 | predictions = out[self.outputs[0]] |
| 92 | |
| 93 | # For oversampling, average predictions across crops. |
| 94 | if oversample: |
| 95 | predictions = predictions.reshape((len(predictions) / 10, 10, -1)) |
| 96 | predictions = predictions.mean(1) |
| 97 | |
| 98 | return predictions |
no test coverage detected