|
6 | 6 |
|
7 | 7 | import numpy as np |
8 | 8 | import re |
9 | | -from scipy import linalg |
10 | | -import scipy.ndimage as ndi |
11 | 9 | from six.moves import range |
12 | 10 | import os |
13 | 11 | import threading |
|
25 | 23 | from PIL import Image as pil_image |
26 | 24 | except ImportError: |
27 | 25 | pil_image = None |
| 26 | + ImageEnhance = None |
| 27 | + |
| 28 | +try: |
| 29 | + import scipy |
| 30 | +except ImportError: |
| 31 | + scipy = None |
28 | 32 |
|
29 | 33 | if pil_image is not None: |
30 | 34 | _PIL_INTERPOLATION_METHODS = { |
@@ -208,6 +212,9 @@ def apply_brightness_shift(x, brightness): |
208 | 212 | # Raises |
209 | 213 | ValueError if `brightness_range` isn't a tuple. |
210 | 214 | """ |
| 215 | + if ImageEnhance is None: |
| 216 | + raise ImportError('Using brightness shifts requires PIL. ' |
| 217 | + 'Install PIL or Pillow.') |
211 | 218 | x = array_to_img(x) |
212 | 219 | x = imgenhancer_Brightness = ImageEnhance.Brightness(x) |
213 | 220 | x = imgenhancer_Brightness.enhance(brightness) |
@@ -272,6 +279,9 @@ def apply_affine_transform(x, theta=0, tx=0, ty=0, shear=0, zx=1, zy=1, |
272 | 279 | # Returns |
273 | 280 | The transformed version of the input. |
274 | 281 | """ |
| 282 | + if scipy is None: |
| 283 | + raise ImportError('Image transformations require SciPy. ' |
| 284 | + 'Install SciPy.') |
275 | 285 | transform_matrix = None |
276 | 286 | if theta != 0: |
277 | 287 | theta = np.deg2rad(theta) |
@@ -316,7 +326,7 @@ def apply_affine_transform(x, theta=0, tx=0, ty=0, shear=0, zx=1, zy=1, |
316 | 326 | final_affine_matrix = transform_matrix[:2, :2] |
317 | 327 | final_offset = transform_matrix[:2, 2] |
318 | 328 |
|
319 | | - channel_images = [ndi.interpolation.affine_transform( |
| 329 | + channel_images = [scipy.ndimage.interpolation.affine_transform( |
320 | 330 | x_channel, |
321 | 331 | final_affine_matrix, |
322 | 332 | final_offset, |
@@ -1230,10 +1240,13 @@ def fit(self, x, |
1230 | 1240 | x /= (self.std + backend.epsilon()) |
1231 | 1241 |
|
1232 | 1242 | if self.zca_whitening: |
| 1243 | + if scipy is None: |
| 1244 | + raise ImportError('Using zca_whitening requires SciPy. ' |
| 1245 | + 'Install SciPy.') |
1233 | 1246 | flat_x = np.reshape( |
1234 | 1247 | x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3])) |
1235 | 1248 | sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0] |
1236 | | - u, s, _ = linalg.svd(sigma) |
| 1249 | + u, s, _ = scipy.linalg.svd(sigma) |
1237 | 1250 | s_inv = 1. / np.sqrt(s[np.newaxis] + self.zca_epsilon) |
1238 | 1251 | self.principal_components = (u * s_inv).dot(u.T) |
1239 | 1252 |
|
|
0 commit comments