We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Misiing import in list_pictures
`list_pictures` abborts with error `NameError: global name 're' is not defined`


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `keras/preprocessing/image.py`
Content:
```
1 from __future__ import absolute_import
2 
3 import numpy as np
4 from scipy import ndimage
5 from scipy import linalg
6 
7 from os import listdir
8 from os.path import isfile, join
9 import random, math
10 from six.moves import range
11 
12 '''
13     Fairly basic set of tools for realtime data augmentation on image data.
14     Can easily be extended to include new transforms, new preprocessing methods, etc...
15 '''
16 
17 def random_rotation(x, rg, fill_mode="nearest", cval=0.):
18     angle = random.uniform(-rg, rg)
19     x = ndimage.interpolation.rotate(x, angle, axes=(1,2), reshape=False, mode=fill_mode, cval=cval)
20     return x
21 
22 def random_shift(x, wrg, hrg, fill_mode="nearest", cval=0.):
23     crop_left_pixels = 0
24     crop_right_pixels = 0
25     crop_top_pixels = 0
26     crop_bottom_pixels = 0
27 
28     original_w = x.shape[1]
29     original_h = x.shape[2]
30 
31     if wrg:
32         crop = random.uniform(0., wrg)
33         split = random.uniform(0, 1)
34         crop_left_pixels = int(split*crop*x.shape[1])
35         crop_right_pixels = int((1-split)*crop*x.shape[1])
36 
37     if hrg:
38         crop = random.uniform(0., hrg)
39         split = random.uniform(0, 1)
40         crop_top_pixels = int(split*crop*x.shape[2])
41         crop_bottom_pixels = int((1-split)*crop*x.shape[2])
42 
43     x = ndimage.interpolation.shift(x, (0, crop_left_pixels, crop_top_pixels), mode=fill_mode, cval=cval)
44     return x
45 
46 def horizontal_flip(x):
47     for i in range(x.shape[0]):
48         x[i] = np.fliplr(x[i])
49     return x
50 
51 def vertical_flip(x):
52     for i in range(x.shape[0]):
53         x[i] = np.flipud(x[i])
54     return x
55 
56 
57 def random_barrel_transform(x, intensity):
58     # TODO
59     pass
60 
61 def random_shear(x, intensity):
62     # TODO
63     pass
64 
65 def random_channel_shift(x, rg):
66     # TODO
67     pass
68 
69 def random_zoom(x, rg, fill_mode="nearest", cval=0.):
70     zoom_w = random.uniform(1.-rg, 1.)
71     zoom_h = random.uniform(1.-rg, 1.)
72     x = ndimage.interpolation.zoom(x, zoom=(1., zoom_w, zoom_h), mode=fill_mode, cval=cval)
73     return x # shape of result will be different from shape of input!
74 
75 
76 
77 
78 def array_to_img(x, scale=True):
79     from PIL import Image
80     x = x.transpose(1, 2, 0) 
81     if scale:
82         x += max(-np.min(x), 0)
83         x /= np.max(x)
84         x *= 255
85     if x.shape[2] == 3:
86         # RGB
87         return Image.fromarray(x.astype("uint8"), "RGB")
88     else:
89         # grayscale
90         return Image.fromarray(x[:,:,0].astype("uint8"), "L")
91 
92 
93 def img_to_array(img):
94     x = np.asarray(img, dtype='float32')
95     if len(x.shape)==3:
96         # RGB: height, width, channel -> channel, height, width
97         x = x.transpose(2, 0, 1)
98     else:
99         # grayscale: height, width -> channel, height, width
100         x = x.reshape((1, x.shape[0], x.shape[1]))
101     return x
102 
103 
104 def load_img(path, grayscale=False):
105     from PIL import Image
106     img = Image.open(open(path))
107     if grayscale:
108         img = img.convert('L')
109     else: # Assure 3 channel even when loaded image is grayscale
110         img = img.convert('RGB')
111     return img
112 
113 
114 def list_pictures(directory, ext='jpg|jpeg|bmp|png'):
115     return [join(directory,f) for f in listdir(directory) \
116         if isfile(join(directory,f)) and re.match('([\w]+\.(?:' + ext + '))', f)]
117 
118 
119 
120 class ImageDataGenerator(object):
121     '''
122         Generate minibatches with 
123         realtime data augmentation.
124     '''
125     def __init__(self, 
126             featurewise_center=True, # set input mean to 0 over the dataset
127             samplewise_center=False, # set each sample mean to 0
128             featurewise_std_normalization=True, # divide inputs by std of the dataset
129             samplewise_std_normalization=False, # divide each input by its std
130 
131             zca_whitening=False, # apply ZCA whitening
132             rotation_range=0., # degrees (0 to 180)
133             width_shift_range=0., # fraction of total width
134             height_shift_range=0., # fraction of total height
135             horizontal_flip=False,
136             vertical_flip=False,
137         ):
138         self.__dict__.update(locals())
139         self.mean = None
140         self.std = None
141         self.principal_components = None
142 
143 
144     def flow(self, X, y, batch_size=32, shuffle=False, seed=None, save_to_dir=None, save_prefix="", save_format="jpeg"):
145         if seed:
146             random.seed(seed)
147 
148         if shuffle:
149             seed = random.randint(1, 10e6)
150             np.random.seed(seed)
151             np.random.shuffle(X)
152             np.random.seed(seed)
153             np.random.shuffle(y)
154 
155         nb_batch = int(math.ceil(float(X.shape[0])/batch_size))
156         for b in range(nb_batch):
157             batch_end = (b+1)*batch_size
158             if batch_end > X.shape[0]:
159                 nb_samples = X.shape[0] - b*batch_size
160             else:
161                 nb_samples = batch_size
162 
163             bX = np.zeros(tuple([nb_samples]+list(X.shape)[1:]))
164             for i in range(nb_samples):
165                 x = X[b*batch_size+i]
166                 x = self.random_transform(x.astype("float32"))
167                 x = self.standardize(x)
168                 bX[i] = x
169 
170             if save_to_dir:
171                 for i in range(nb_samples):
172                     img = array_to_img(bX[i], scale=True)
173                     img.save(save_to_dir + "/" + save_prefix + "_" + str(i) + "." + save_format)
174 
175             yield bX, y[b*batch_size:b*batch_size+nb_samples]
176 
177 
178     def standardize(self, x):
179         if self.featurewise_center:
180             x -= self.mean
181         if self.featurewise_std_normalization:
182             x /= self.std
183 
184         if self.zca_whitening:
185             flatx = np.reshape(x, (x.shape[0]*x.shape[1]*x.shape[2]))
186             whitex = np.dot(flatx, self.principal_components)
187             x = np.reshape(whitex, (x.shape[0], x.shape[1], x.shape[2]))
188 
189         if self.samplewise_center:
190             x -= np.mean(x)
191         if self.samplewise_std_normalization:
192             x /= np.std(x)
193 
194         return x
195 
196 
197     def random_transform(self, x):
198         if self.rotation_range:
199             x = random_rotation(x, self.rotation_range)
200         if self.width_shift_range or self.height_shift_range:
201             x = random_shift(x, self.width_shift_range, self.height_shift_range)
202         if self.horizontal_flip:
203             if random.random() < 0.5:
204                 x = horizontal_flip(x)
205         if self.vertical_flip:
206             if random.random() < 0.5:
207                 x = vertical_flip(x)
208 
209         # TODO:
210         # zoom
211         # barrel/fisheye
212         # shearing
213         # channel shifting
214         return x
215 
216 
217     def fit(self, X, 
218             augment=False, # fit on randomly augmented samples
219             rounds=1, # if augment, how many augmentation passes over the data do we use
220             seed=None
221         ):
222         '''
223             Required for featurewise_center, featurewise_std_normalization and zca_whitening.
224         '''
225         X = np.copy(X)
226         
227         if augment:
228             aX = np.zeros(tuple([rounds*X.shape[0]]+list(X.shape)[1:]))
229             for r in range(rounds):
230                 for i in range(X.shape[0]):
231                     img = array_to_img(X[i])
232                     img = self.random_transform(img)
233                     aX[i+r*X.shape[0]] = img_to_array(img)
234             X = aX
235 
236         if self.featurewise_center:
237             self.mean = np.mean(X, axis=0)
238             X -= self.mean
239         if self.featurewise_std_normalization:
240             self.std = np.std(X, axis=0)
241             X /= self.std
242 
243         if self.zca_whitening:
244             flatX = np.reshape(X, (X.shape[0], X.shape[1]*X.shape[2]*X.shape[3]))
245             fudge = 10e-6
246             sigma = np.dot(flatX.T, flatX) / flatX.shape[1]
247             U, S, V = linalg.svd(sigma)
248             self.principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + fudge))), U.T)
249 
250 
251
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py
--- a/keras/preprocessing/image.py
+++ b/keras/preprocessing/image.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import
 
 import numpy as np
+import re
 from scipy import ndimage
 from scipy import linalg

{"golden_diff": "diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py\n--- a/keras/preprocessing/image.py\n+++ b/keras/preprocessing/image.py\n@@ -1,6 +1,7 @@\n from __future__ import absolute_import\n \n import numpy as np\n+import re\n from scipy import ndimage\n from scipy import linalg\n", "issue": "Misiing import in list_pictures\n`list_pictures` abborts with error `NameError: global name 're' is not defined`\n\n", "before_files": [{"content": "from __future__ import absolute_import\n\nimport numpy as np\nfrom scipy import ndimage\nfrom scipy import linalg\n\nfrom os import listdir\nfrom os.path import isfile, join\nimport random, math\nfrom six.moves import range\n\n'''\n    Fairly basic set of tools for realtime data augmentation on image data.\n    Can easily be extended to include new transforms, new preprocessing methods, etc...\n'''\n\ndef random_rotation(x, rg, fill_mode=\"nearest\", cval=0.):\n    angle = random.uniform(-rg, rg)\n    x = ndimage.interpolation.rotate(x, angle, axes=(1,2), reshape=False, mode=fill_mode, cval=cval)\n    return x\n\ndef random_shift(x, wrg, hrg, fill_mode=\"nearest\", cval=0.):\n    crop_left_pixels = 0\n    crop_right_pixels = 0\n    crop_top_pixels = 0\n    crop_bottom_pixels = 0\n\n    original_w = x.shape[1]\n    original_h = x.shape[2]\n\n    if wrg:\n        crop = random.uniform(0., wrg)\n        split = random.uniform(0, 1)\n        crop_left_pixels = int(split*crop*x.shape[1])\n        crop_right_pixels = int((1-split)*crop*x.shape[1])\n\n    if hrg:\n        crop = random.uniform(0., hrg)\n        split = random.uniform(0, 1)\n        crop_top_pixels = int(split*crop*x.shape[2])\n        crop_bottom_pixels = int((1-split)*crop*x.shape[2])\n\n    x = ndimage.interpolation.shift(x, (0, crop_left_pixels, crop_top_pixels), mode=fill_mode, cval=cval)\n    return x\n\ndef horizontal_flip(x):\n    for i in range(x.shape[0]):\n        x[i] = np.fliplr(x[i])\n    return x\n\ndef vertical_flip(x):\n    for i in range(x.shape[0]):\n        x[i] = np.flipud(x[i])\n    return x\n\n\ndef random_barrel_transform(x, intensity):\n    # TODO\n    pass\n\ndef random_shear(x, intensity):\n    # TODO\n    pass\n\ndef random_channel_shift(x, rg):\n    # TODO\n    pass\n\ndef random_zoom(x, rg, fill_mode=\"nearest\", cval=0.):\n    zoom_w = random.uniform(1.-rg, 1.)\n    zoom_h = random.uniform(1.-rg, 1.)\n    x = ndimage.interpolation.zoom(x, zoom=(1., zoom_w, zoom_h), mode=fill_mode, cval=cval)\n    return x # shape of result will be different from shape of input!\n\n\n\n\ndef array_to_img(x, scale=True):\n    from PIL import Image\n    x = x.transpose(1, 2, 0) \n    if scale:\n        x += max(-np.min(x), 0)\n        x /= np.max(x)\n        x *= 255\n    if x.shape[2] == 3:\n        # RGB\n        return Image.fromarray(x.astype(\"uint8\"), \"RGB\")\n    else:\n        # grayscale\n        return Image.fromarray(x[:,:,0].astype(\"uint8\"), \"L\")\n\n\ndef img_to_array(img):\n    x = np.asarray(img, dtype='float32')\n    if len(x.shape)==3:\n        # RGB: height, width, channel -> channel, height, width\n        x = x.transpose(2, 0, 1)\n    else:\n        # grayscale: height, width -> channel, height, width\n        x = x.reshape((1, x.shape[0], x.shape[1]))\n    return x\n\n\ndef load_img(path, grayscale=False):\n    from PIL import Image\n    img = Image.open(open(path))\n    if grayscale:\n        img = img.convert('L')\n    else: # Assure 3 channel even when loaded image is grayscale\n        img = img.convert('RGB')\n    return img\n\n\ndef list_pictures(directory, ext='jpg|jpeg|bmp|png'):\n    return [join(directory,f) for f in listdir(directory) \\\n        if isfile(join(directory,f)) and re.match('([\\w]+\\.(?:' + ext + '))', f)]\n\n\n\nclass ImageDataGenerator(object):\n    '''\n        Generate minibatches with \n        realtime data augmentation.\n    '''\n    def __init__(self, \n            featurewise_center=True, # set input mean to 0 over the dataset\n            samplewise_center=False, # set each sample mean to 0\n            featurewise_std_normalization=True, # divide inputs by std of the dataset\n            samplewise_std_normalization=False, # divide each input by its std\n\n            zca_whitening=False, # apply ZCA whitening\n            rotation_range=0., # degrees (0 to 180)\n            width_shift_range=0., # fraction of total width\n            height_shift_range=0., # fraction of total height\n            horizontal_flip=False,\n            vertical_flip=False,\n        ):\n        self.__dict__.update(locals())\n        self.mean = None\n        self.std = None\n        self.principal_components = None\n\n\n    def flow(self, X, y, batch_size=32, shuffle=False, seed=None, save_to_dir=None, save_prefix=\"\", save_format=\"jpeg\"):\n        if seed:\n            random.seed(seed)\n\n        if shuffle:\n            seed = random.randint(1, 10e6)\n            np.random.seed(seed)\n            np.random.shuffle(X)\n            np.random.seed(seed)\n            np.random.shuffle(y)\n\n        nb_batch = int(math.ceil(float(X.shape[0])/batch_size))\n        for b in range(nb_batch):\n            batch_end = (b+1)*batch_size\n            if batch_end > X.shape[0]:\n                nb_samples = X.shape[0] - b*batch_size\n            else:\n                nb_samples = batch_size\n\n            bX = np.zeros(tuple([nb_samples]+list(X.shape)[1:]))\n            for i in range(nb_samples):\n                x = X[b*batch_size+i]\n                x = self.random_transform(x.astype(\"float32\"))\n                x = self.standardize(x)\n                bX[i] = x\n\n            if save_to_dir:\n                for i in range(nb_samples):\n                    img = array_to_img(bX[i], scale=True)\n                    img.save(save_to_dir + \"/\" + save_prefix + \"_\" + str(i) + \".\" + save_format)\n\n            yield bX, y[b*batch_size:b*batch_size+nb_samples]\n\n\n    def standardize(self, x):\n        if self.featurewise_center:\n            x -= self.mean\n        if self.featurewise_std_normalization:\n            x /= self.std\n\n        if self.zca_whitening:\n            flatx = np.reshape(x, (x.shape[0]*x.shape[1]*x.shape[2]))\n            whitex = np.dot(flatx, self.principal_components)\n            x = np.reshape(whitex, (x.shape[0], x.shape[1], x.shape[2]))\n\n        if self.samplewise_center:\n            x -= np.mean(x)\n        if self.samplewise_std_normalization:\n            x /= np.std(x)\n\n        return x\n\n\n    def random_transform(self, x):\n        if self.rotation_range:\n            x = random_rotation(x, self.rotation_range)\n        if self.width_shift_range or self.height_shift_range:\n            x = random_shift(x, self.width_shift_range, self.height_shift_range)\n        if self.horizontal_flip:\n            if random.random() < 0.5:\n                x = horizontal_flip(x)\n        if self.vertical_flip:\n            if random.random() < 0.5:\n                x = vertical_flip(x)\n\n        # TODO:\n        # zoom\n        # barrel/fisheye\n        # shearing\n        # channel shifting\n        return x\n\n\n    def fit(self, X, \n            augment=False, # fit on randomly augmented samples\n            rounds=1, # if augment, how many augmentation passes over the data do we use\n            seed=None\n        ):\n        '''\n            Required for featurewise_center, featurewise_std_normalization and zca_whitening.\n        '''\n        X = np.copy(X)\n        \n        if augment:\n            aX = np.zeros(tuple([rounds*X.shape[0]]+list(X.shape)[1:]))\n            for r in range(rounds):\n                for i in range(X.shape[0]):\n                    img = array_to_img(X[i])\n                    img = self.random_transform(img)\n                    aX[i+r*X.shape[0]] = img_to_array(img)\n            X = aX\n\n        if self.featurewise_center:\n            self.mean = np.mean(X, axis=0)\n            X -= self.mean\n        if self.featurewise_std_normalization:\n            self.std = np.std(X, axis=0)\n            X /= self.std\n\n        if self.zca_whitening:\n            flatX = np.reshape(X, (X.shape[0], X.shape[1]*X.shape[2]*X.shape[3]))\n            fudge = 10e-6\n            sigma = np.dot(flatX.T, flatX) / flatX.shape[1]\n            U, S, V = linalg.svd(sigma)\n            self.principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + fudge))), U.T)\n\n\n", "path": "keras/preprocessing/image.py"}], "after_files": [{"content": "from __future__ import absolute_import\n\nimport numpy as np\nimport re\nfrom scipy import ndimage\nfrom scipy import linalg\n\nfrom os import listdir\nfrom os.path import isfile, join\nimport random, math\nfrom six.moves import range\n\n'''\n    Fairly basic set of tools for realtime data augmentation on image data.\n    Can easily be extended to include new transforms, new preprocessing methods, etc...\n'''\n\ndef random_rotation(x, rg, fill_mode=\"nearest\", cval=0.):\n    angle = random.uniform(-rg, rg)\n    x = ndimage.interpolation.rotate(x, angle, axes=(1,2), reshape=False, mode=fill_mode, cval=cval)\n    return x\n\ndef random_shift(x, wrg, hrg, fill_mode=\"nearest\", cval=0.):\n    crop_left_pixels = 0\n    crop_right_pixels = 0\n    crop_top_pixels = 0\n    crop_bottom_pixels = 0\n\n    original_w = x.shape[1]\n    original_h = x.shape[2]\n\n    if wrg:\n        crop = random.uniform(0., wrg)\n        split = random.uniform(0, 1)\n        crop_left_pixels = int(split*crop*x.shape[1])\n        crop_right_pixels = int((1-split)*crop*x.shape[1])\n\n    if hrg:\n        crop = random.uniform(0., hrg)\n        split = random.uniform(0, 1)\n        crop_top_pixels = int(split*crop*x.shape[2])\n        crop_bottom_pixels = int((1-split)*crop*x.shape[2])\n\n    x = ndimage.interpolation.shift(x, (0, crop_left_pixels, crop_top_pixels), mode=fill_mode, cval=cval)\n    return x\n\ndef horizontal_flip(x):\n    for i in range(x.shape[0]):\n        x[i] = np.fliplr(x[i])\n    return x\n\ndef vertical_flip(x):\n    for i in range(x.shape[0]):\n        x[i] = np.flipud(x[i])\n    return x\n\n\ndef random_barrel_transform(x, intensity):\n    # TODO\n    pass\n\ndef random_shear(x, intensity):\n    # TODO\n    pass\n\ndef random_channel_shift(x, rg):\n    # TODO\n    pass\n\ndef random_zoom(x, rg, fill_mode=\"nearest\", cval=0.):\n    zoom_w = random.uniform(1.-rg, 1.)\n    zoom_h = random.uniform(1.-rg, 1.)\n    x = ndimage.interpolation.zoom(x, zoom=(1., zoom_w, zoom_h), mode=fill_mode, cval=cval)\n    return x # shape of result will be different from shape of input!\n\n\n\n\ndef array_to_img(x, scale=True):\n    from PIL import Image\n    x = x.transpose(1, 2, 0) \n    if scale:\n        x += max(-np.min(x), 0)\n        x /= np.max(x)\n        x *= 255\n    if x.shape[2] == 3:\n        # RGB\n        return Image.fromarray(x.astype(\"uint8\"), \"RGB\")\n    else:\n        # grayscale\n        return Image.fromarray(x[:,:,0].astype(\"uint8\"), \"L\")\n\n\ndef img_to_array(img):\n    x = np.asarray(img, dtype='float32')\n    if len(x.shape)==3:\n        # RGB: height, width, channel -> channel, height, width\n        x = x.transpose(2, 0, 1)\n    else:\n        # grayscale: height, width -> channel, height, width\n        x = x.reshape((1, x.shape[0], x.shape[1]))\n    return x\n\n\ndef load_img(path, grayscale=False):\n    from PIL import Image\n    img = Image.open(open(path))\n    if grayscale:\n        img = img.convert('L')\n    else: # Assure 3 channel even when loaded image is grayscale\n        img = img.convert('RGB')\n    return img\n\n\ndef list_pictures(directory, ext='jpg|jpeg|bmp|png'):\n    return [join(directory,f) for f in listdir(directory) \\\n        if isfile(join(directory,f)) and re.match('([\\w]+\\.(?:' + ext + '))', f)]\n\n\n\nclass ImageDataGenerator(object):\n    '''\n        Generate minibatches with \n        realtime data augmentation.\n    '''\n    def __init__(self, \n            featurewise_center=True, # set input mean to 0 over the dataset\n            samplewise_center=False, # set each sample mean to 0\n            featurewise_std_normalization=True, # divide inputs by std of the dataset\n            samplewise_std_normalization=False, # divide each input by its std\n\n            zca_whitening=False, # apply ZCA whitening\n            rotation_range=0., # degrees (0 to 180)\n            width_shift_range=0., # fraction of total width\n            height_shift_range=0., # fraction of total height\n            horizontal_flip=False,\n            vertical_flip=False,\n        ):\n        self.__dict__.update(locals())\n        self.mean = None\n        self.std = None\n        self.principal_components = None\n\n\n    def flow(self, X, y, batch_size=32, shuffle=False, seed=None, save_to_dir=None, save_prefix=\"\", save_format=\"jpeg\"):\n        if seed:\n            random.seed(seed)\n\n        if shuffle:\n            seed = random.randint(1, 10e6)\n            np.random.seed(seed)\n            np.random.shuffle(X)\n            np.random.seed(seed)\n            np.random.shuffle(y)\n\n        nb_batch = int(math.ceil(float(X.shape[0])/batch_size))\n        for b in range(nb_batch):\n            batch_end = (b+1)*batch_size\n            if batch_end > X.shape[0]:\n                nb_samples = X.shape[0] - b*batch_size\n            else:\n                nb_samples = batch_size\n\n            bX = np.zeros(tuple([nb_samples]+list(X.shape)[1:]))\n            for i in range(nb_samples):\n                x = X[b*batch_size+i]\n                x = self.random_transform(x.astype(\"float32\"))\n                x = self.standardize(x)\n                bX[i] = x\n\n            if save_to_dir:\n                for i in range(nb_samples):\n                    img = array_to_img(bX[i], scale=True)\n                    img.save(save_to_dir + \"/\" + save_prefix + \"_\" + str(i) + \".\" + save_format)\n\n            yield bX, y[b*batch_size:b*batch_size+nb_samples]\n\n\n    def standardize(self, x):\n        if self.featurewise_center:\n            x -= self.mean\n        if self.featurewise_std_normalization:\n            x /= self.std\n\n        if self.zca_whitening:\n            flatx = np.reshape(x, (x.shape[0]*x.shape[1]*x.shape[2]))\n            whitex = np.dot(flatx, self.principal_components)\n            x = np.reshape(whitex, (x.shape[0], x.shape[1], x.shape[2]))\n\n        if self.samplewise_center:\n            x -= np.mean(x)\n        if self.samplewise_std_normalization:\n            x /= np.std(x)\n\n        return x\n\n\n    def random_transform(self, x):\n        if self.rotation_range:\n            x = random_rotation(x, self.rotation_range)\n        if self.width_shift_range or self.height_shift_range:\n            x = random_shift(x, self.width_shift_range, self.height_shift_range)\n        if self.horizontal_flip:\n            if random.random() < 0.5:\n                x = horizontal_flip(x)\n        if self.vertical_flip:\n            if random.random() < 0.5:\n                x = vertical_flip(x)\n\n        # TODO:\n        # zoom\n        # barrel/fisheye\n        # shearing\n        # channel shifting\n        return x\n\n\n    def fit(self, X, \n            augment=False, # fit on randomly augmented samples\n            rounds=1, # if augment, how many augmentation passes over the data do we use\n            seed=None\n        ):\n        '''\n            Required for featurewise_center, featurewise_std_normalization and zca_whitening.\n        '''\n        X = np.copy(X)\n        \n        if augment:\n            aX = np.zeros(tuple([rounds*X.shape[0]]+list(X.shape)[1:]))\n            for r in range(rounds):\n                for i in range(X.shape[0]):\n                    img = array_to_img(X[i])\n                    img = self.random_transform(img)\n                    aX[i+r*X.shape[0]] = img_to_array(img)\n            X = aX\n\n        if self.featurewise_center:\n            self.mean = np.mean(X, axis=0)\n            X -= self.mean\n        if self.featurewise_std_normalization:\n            self.std = np.std(X, axis=0)\n            X /= self.std\n\n        if self.zca_whitening:\n            flatX = np.reshape(X, (X.shape[0], X.shape[1]*X.shape[2]*X.shape[3]))\n            fudge = 10e-6\n            sigma = np.dot(flatX.T, flatX) / flatX.shape[1]\n            U, S, V = linalg.svd(sigma)\n            self.principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + fudge))), U.T)\n\n\n", "path": "keras/preprocessing/image.py"}]}