internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: keras-team/keras

Issue:
Incorrect sample-wise normalization in ImageDataGenerator
I think the ImageDataGenerator is doing sample-wise normalization wrong. The goal should be "subtracting the mean from each image, then rescaling it so that the standard deviation across its pixels is equal to some constant", quoted from Goodfellow et al's _Deep Learning_. 

In other words it should uniformly normalize pixels by the image's mean/std across all pixels and channels.

Instead, it is normalizing each pixel separately by that pixel's own mean/std across channels.

https://github.com/fchollet/keras/blob/0ff700abccc71ceb0794ddc8e77945e178f10599/keras/preprocessing/image.py#L528-L532

Correct would be
```python
img_row_axis = self.row_axis - 1
img_col_axis = self.col_axis - 1
img_channel_axis = self.channel_axis - 1
if self.samplewise_center: 
    x -= np.mean(x, axis=(img_channel_axis, img_row_axis, img_col_axis), keepdims=True)
if self.samplewise_std_normalization: 
    x /= (np.std(x, axis=(img_channel_axis, img_row_axis, img_col_axis), keepdims=True) + 1e-7)
```


Discussion:

Resolving patch:
diff --git a/keras/preprocessing/image.py b/keras/preprocessing/image.py
--- a/keras/preprocessing/image.py
+++ b/keras/preprocessing/image.py
@@ -524,12 +524,10 @@ def standardize(self, x):
             x = self.preprocessing_function(x)
         if self.rescale:
             x *= self.rescale
-        # x is a single image, so it doesn't have image number at index 0
-        img_channel_axis = self.channel_axis - 1
         if self.samplewise_center:
-            x -= np.mean(x, axis=img_channel_axis, keepdims=True)
+            x -= np.mean(x, keepdims=True)
         if self.samplewise_std_normalization:
-            x /= (np.std(x, axis=img_channel_axis, keepdims=True) + 1e-7)
+            x /= np.std(x, keepdims=True) + 1e-7
 
         if self.featurewise_center:
             if self.mean is not None:


Test patch:
Repository: keras-team/keras

Issue:
[BUG] CSVLogger fails if training stops in the first epoch
`callbacks.py: CSVLogger: on_epoch_end` - the `self.keys` dict is `None` when `model.stop_training = True` and the first epoch have not been completed yet. This results in `TypeError: 'NoneType' object is not iterable`.

Short example:
```python
from keras.callbacks import Callback, CSVLogger
from keras.layers import InputLayer
from keras.models import Sequential


class TerminateInFirstEpoch(Callback):
    """Callback that terminates before the first epoch is completed.
    """

    def on_batch_end(self, batch, logs=None):
        self.model.stop_training = True


model = Sequential()
model.add(InputLayer(input_shape=(1,)))
model.compile(optimizer='sgd', loss='mse')

terminator = TerminateInFirstEpoch()
logger = CSVLogger('./log.csv')

x, y = [1, 2, 3, 4], [1, 2, 3, 4]
model.fit(x, y, batch_size=2, callbacks=[terminator, logger])
```


Discussion:

Resolving patch:
diff --git a/keras/callbacks.py b/keras/callbacks.py
--- a/keras/callbacks.py
+++ b/keras/callbacks.py
@@ -965,13 +965,14 @@ def handle_value(k):
             else:
                 return k
 
+        if self.keys is None:
+            self.keys = sorted(logs.keys())
+
         if self.model.stop_training:
             # We set NA so that csv parsers do not fail for this last epoch.
             logs = dict([(k, logs[k]) if k in logs else (k, 'NA') for k in self.keys])
 
         if not self.writer:
-            self.keys = sorted(logs.keys())
-
             class CustomDialect(csv.excel):
                 delimiter = self.sep
 


Test patch:
Repository: keras-team/keras

Issue:
Tokenization crashes when split string has more than one character
`from keras.preprocessing.text import Tokenizer

texts = ['Just any text.']
t = Tokenizer(split="any")
t.fit_on_texts(texts)
print(t.word_index)`

throws an exception:
ValueError: the first two maketrans arguments must have equal length


Discussion:

Resolving patch:
diff --git a/keras/preprocessing/text.py b/keras/preprocessing/text.py
--- a/keras/preprocessing/text.py
+++ b/keras/preprocessing/text.py
@@ -38,12 +38,21 @@ def text_to_word_sequence(text,
     if lower:
         text = text.lower()
 
-    if sys.version_info < (3,) and isinstance(text, unicode):
-        translate_map = dict((ord(c), unicode(split)) for c in filters)
+    if sys.version_info < (3,):
+        if isinstance(text, unicode):
+            translate_map = dict((ord(c), unicode(split)) for c in filters)
+            text = text.translate(translate_map)
+        elif len(split) == 1:
+            translate_map = maketrans(filters, split * len(filters))
+            text = text.translate(translate_map)
+        else:
+            for c in filters:
+                text = text.replace(c, split)
     else:
-        translate_map = maketrans(filters, split * len(filters))
+        translate_dict = dict((c, split) for c in filters)
+        translate_map = maketrans(translate_dict)
+        text = text.translate(translate_map)
 
-    text = text.translate(translate_map)
     seq = text.split(split)
     return [i for i in seq if i]
 


Test patch:
diff --git a/tests/keras/preprocessing/text_test.py b/tests/keras/preprocessing/text_test.py
--- a/tests/keras/preprocessing/text_test.py
+++ b/tests/keras/preprocessing/text_test.py
@@ -73,11 +73,21 @@ def test_text_to_word_sequence():
     assert text_to_word_sequence(text) == ['hello', 'world']
 
 
+def test_text_to_word_sequence_multichar_split():
+    text = 'hello!stop?world!'
+    assert text_to_word_sequence(text, split='stop') == ['hello', 'world']
+
+
 def test_text_to_word_sequence_unicode():
     text = u'ali! veli? kırk dokuz elli'
     assert text_to_word_sequence(text) == [u'ali', u'veli', u'kırk', u'dokuz', u'elli']
 
 
+def test_text_to_word_sequence_unicode_multichar_split():
+    text = u'ali!stopveli?stopkırkstopdokuzstopelli'
+    assert text_to_word_sequence(text, split='stop') == [u'ali', u'veli', u'kırk', u'dokuz', u'elli']
+
+
 def test_tokenizer_unicode():
     texts = [u'ali veli kırk dokuz elli', u'ali veli kırk dokuz elli veli kırk dokuz']
     tokenizer = Tokenizer(num_words=5)
Repository: keras-team/keras

Issue:
Missing space before a comma in a docstring
https://github.com/keras-team/keras/blob/e85d3dc15072f774736eb0e3c216eb0b7da1db9a/keras/layers/convolutional_recurrent.py#L860


Discussion:

Resolving patch:
diff --git a/keras/layers/convolutional_recurrent.py b/keras/layers/convolutional_recurrent.py
--- a/keras/layers/convolutional_recurrent.py
+++ b/keras/layers/convolutional_recurrent.py
@@ -857,10 +857,10 @@ class ConvLSTM2D(ConvRNN2D):
     # Input shape
         - if data_format='channels_first'
             5D tensor with shape:
-            `(samples,time, channels, rows, cols)`
+            `(samples, time, channels, rows, cols)`
         - if data_format='channels_last'
             5D tensor with shape:
-            `(samples,time, rows, cols, channels)`
+            `(samples, time, rows, cols, channels)`
 
      # Output shape
         - if `return_sequences`


Test patch:
Repository: keras-team/keras

Issue:
Small error in Documentation (multiple outputs -> multiple inputs)
I think this should be changed from "multiple outputs" to "multiple inputs".

https://github.com/keras-team/keras/blob/2d183db0372e5ac2a686608cb9da0a9bd4319764/keras/engine/training.py#L1128


Discussion:

Resolving patch:
diff --git a/keras/engine/training.py b/keras/engine/training.py
--- a/keras/engine/training.py
+++ b/keras/engine/training.py
@@ -1125,7 +1125,7 @@ def predict(self, x,
 
         # Arguments
             x: The input data, as a Numpy array
-                (or list of Numpy arrays if the model has multiple outputs).
+                (or list of Numpy arrays if the model has multiple inputs).
             batch_size: Integer. If unspecified, it will default to 32.
             verbose: Verbosity mode, 0 or 1.
             steps: Total number of steps (batches of samples)


Test patch:
Repository: keras-team/keras

Issue:
Sync naming convention and style in NLP datasets
Also fixes a possible bug with np.load()/f.close() pair not being exception-safe.


Discussion:
Tests are failing. Changes appear incorrect.
Fixed. Sorry about that. The typo was real, but was causing problems only in Python2 (maybe due to  unpacking changes in Python3). CNTK backend tested it in Travis:
```
keras/datasets/imdb.py   48      7    85%   45-47, 49, 71-72, 77, 89
```

Resolving patch:
diff --git a/keras/datasets/boston_housing.py b/keras/datasets/boston_housing.py
--- a/keras/datasets/boston_housing.py
+++ b/keras/datasets/boston_housing.py
@@ -25,10 +25,9 @@ def load_data(path='boston_housing.npz', test_split=0.2, seed=113):
     path = get_file(path,
                     origin='https://s3.amazonaws.com/keras-datasets/boston_housing.npz',
                     file_hash='f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5')
-    f = np.load(path)
-    x = f['x']
-    y = f['y']
-    f.close()
+    with np.load(path) as f:
+        x = f['x']
+        y = f['y']
 
     np.random.seed(seed)
     indices = np.arange(len(x))


Test patch:
Repository: keras-team/keras

Issue:
Suggesting keras.utils.*_utils packages should not be part of the official API
In general, all `keras.utils.*_utils.*` functions and classes that are documented on keras.io are available directly in `keras.utils` and documented as such. However there are a few discrepancies:
* `keras.utils.vis_utils.model_to_dot` is not available in `keras.utils`.
* `keras.utils.np_utils.to_categorical` sometimes appears in the documentation, instead of `keras.utils.to_categorical`.
* `keras.utils.io_utils.HDF5Matrix` sometimes appears in the documentation, instead of `keras.utils.HDF5Matrix`.

This introduces some confusion as to what is part of the official Keras API or not: in particular, are `keras.utils.*_utils` packages part of the Keras API or not? Possibly as a result of this confusion, tf.keras is not consistent with keras-team/keras, as it has no `tf.keras.utils.*_utils` packages, and is missing `model_to_dot` altogether. Arguably this is a tf.keras issue, but the fact that only three utility functions are placed in `keras.utils.*_utils` packages is surprising IMHO.

I will propose a PR to fix this by:
* Adding `model_to_dot` to `keras.utils`
* Fixing the documentation to remove all references to `keras.utils.*_utils` packages.


Discussion:

Resolving patch:
diff --git a/keras/utils/__init__.py b/keras/utils/__init__.py
--- a/keras/utils/__init__.py
+++ b/keras/utils/__init__.py
@@ -21,6 +21,7 @@
 from .layer_utils import convert_all_kernels_in_model
 from .layer_utils import get_source_inputs
 from .layer_utils import print_summary
+from .vis_utils import model_to_dot
 from .vis_utils import plot_model
 from .np_utils import to_categorical
 from .np_utils import normalize


Test patch:
Repository: keras-team/keras

Issue:
Weighted metrics are not loaded using load_model, only normal metrics are set
When using sample weights its important to use `weighted_metrics` instead of `metrics` to get the correct accuracy. However, the current implementation of load/save_model does not take these metrics in to account. 

Current implementations:

- `save_model` only saves metrics, see https://github.com/keras-team/keras/blob/master/keras/engine/saving.py#L144
- `load_model` only sets metrics, see https://github.com/keras-team/keras/blob/master/keras/engine/saving.py#L286

This problem only occurs after a loading a model and then trying to continue training (or using one of the weighted metrics). Ideally, the weighted metrics should also be saved and subsequently loaded.


Discussion:
Even I faced the same issue . Is there any way to re load the metrics back to Network.
I can confirm that this is still an issue. After loading an model, weighted_metrics remains None.
Steps to reproduce (example): 
1. build some model and compile with `model.compile(optimizer="Adam", loss='binary_crossentropy', metrics=['mae', 'acc'], weighted_metrics=['mae', 'acc'])`
2. train some stuff, evaluate -> weighted_metrics are given
3. save the model
4. load it
5. weighted_metrics are gone

However, there's a workaround: you can load the model without compiling and add the weighted metrics yourself:
`model = keras.models.load_model(filename, compile=False)`
`model.weighted_metrics = ['mae', 'acc']self.model.compile(optimizer="Adam", loss='binary_crossentropy', metrics=['mae', 'acc'], weighted_metrics=['mae', 'acc'])`

Resolving patch:
diff --git a/keras/engine/saving.py b/keras/engine/saving.py
--- a/keras/engine/saving.py
+++ b/keras/engine/saving.py
@@ -319,6 +319,10 @@ def convert_custom_objects(obj):
         # Recover loss functions and metrics.
         loss = convert_custom_objects(training_config['loss'])
         metrics = convert_custom_objects(training_config['metrics'])
+        # Earlier versions of keras didn't dump weighted_metrics properly. Use
+        # a get to avoid failing if the key is missing
+        weighted_metrics = convert_custom_objects(
+            training_config.get('weighted_metrics'))
         sample_weight_mode = training_config['sample_weight_mode']
         loss_weights = training_config['loss_weights']
 
@@ -326,6 +330,7 @@ def convert_custom_objects(obj):
         model.compile(optimizer=optimizer,
                       loss=loss,
                       metrics=metrics,
+                      weighted_metrics=weighted_metrics,
                       loss_weights=loss_weights,
                       sample_weight_mode=sample_weight_mode)
 


Test patch:
Repository: keras-team/keras

Issue:
is_tensor_or_variable in losses#get_config not defined
**System information**  
- Have I written custom code (as opposed to using example directory):  yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):  macOS
- TensorFlow backend (yes / no):  yes
- TensorFlow version:  v1.14.0-rc1-22-gaf24dc91b5 1.14.0
- Keras version:  2.3.0 88ca804d94a0f978d0522d82a2e2bda26362076a (current master branch)
- Python version:  3.7.4
- CUDA/cuDNN version:  n/a
- GPU model and memory:  Intel HD graphics

**Describe the current behavior**  
When storing a checkpoint using the ModelCheckpoint callback, a call to the undefined `is_tensor_or_variable` is performed.
```
  File ".../keras/losses.py", line 137, in get_config
    config[k] = K.eval(v) if is_tensor_or_variable(v) else v
NameError: name 'is_tensor_or_variable' is not defined
```
The function is used but not defined here:
https://github.com/keras-team/keras/blob/88ca804d94a0f978d0522d82a2e2bda26362076a/keras/losses.py#L137

**Describe the expected behavior**  
I assume that this should be something like https://github.com/tensorflow/tensorflow/blob/e2a6861c2be77412d86bdf433b640ed7dd1de32c/tensorflow/python/keras/utils/tf_utils.py#L384, to act accordingly.

**Code to reproduce the issue**  
```
import keras

inputs = keras.layers.Input(shape=(1, ))
outputs = keras.layers.Dense(1)(inputs)
model = keras.models.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='adam', loss=keras.losses.BinaryCrossentropy())

cb = keras.callbacks.ModelCheckpoint('chkpts', period=1)
model.fit([[0]], [[1]], callbacks=[cb])
```



Discussion:
Maybe `is_tensor_or_variable(v)` can be replaced by `K.is_tensor(v) or K.is_variable(v)`?

Resolving patch:
diff --git a/keras/losses.py b/keras/losses.py
--- a/keras/losses.py
+++ b/keras/losses.py
@@ -134,7 +134,7 @@ def call(self, y_true, y_pred):
     def get_config(self):
         config = {}
         for k, v in six.iteritems(self._fn_kwargs):
-            config[k] = K.eval(v) if is_tensor_or_variable(v) else v
+            config[k] = K.eval(v) if K.is_tensor(v) or K.is_variable(v) else v
         base_config = super(LossFunctionWrapper, self).get_config()
         return dict(list(base_config.items()) + list(config.items()))
 


Test patch:
Repository: keras-team/keras

Issue:
NameError: name 'math_ops' is not defined
**System information**  
- Have I written custom code (as opposed to using example directory):  
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04):  
- TensorFlow backend (yes / no):  
- TensorFlow version: 1.14.0 
- Keras version:  2.3.0
- Python version:  
- CUDA/cuDNN version:  
- GPU model and memory:  


**Describe the current behavior**  

  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 222, in compile
    masks=masks)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 871, in _handle_metrics
    self._per_output_metrics[i], target, output, output_mask)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training.py", line 842, in _handle_per_output_metrics
    metric_fn, y_true, y_pred, weights=weights, mask=mask)
  File "/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py", line 1022, in call_metric_function
    mask = math_ops.cast(mask, y_pred.dtype)
NameError: name 'math_ops' is not defined

**Describe the expected behavior**  

**Code to reproduce the issue**  

**Other info / logs**  



Discussion:

Resolving patch:
diff --git a/keras/engine/training_utils.py b/keras/engine/training_utils.py
--- a/keras/engine/training_utils.py
+++ b/keras/engine/training_utils.py
@@ -1019,7 +1019,7 @@ def call_metric_function(metric_fn,
                          mask=None):
     """Invokes metric function and returns the metric result tensor."""
     if mask is not None:
-        mask = math_ops.cast(mask, y_pred.dtype)
+        mask = K.cast(mask, y_pred.dtype)
         if weights is None:
             # Use mask as sample weight.
             weights = mask


Test patch:
Repository: keras-team/keras

Issue:
CSVLogger leaks file handles on interuption
**Describe the current behavior**  
If an interruption occurs after training starts and before training ends, a file handle will be leaked, such as a Ctrl+C KeyboardInterupt.

**Describe the expected behavior**  
A destructor should implement the required behavior.  Then implementors should use `with CSVLogger() as csv_logger: ` syntax

**Code to reproduce the issue**  
Any code using CSVLogger will do, and a simple Ctrl+C.  Even error handling code could at best call on_train_end() which is awkward.

**Other info / logs**  
The source code here makes the leak easy to identify: https://github.com/keras-team/keras/blob/master/keras/callbacks/callbacks.py#L1071


Discussion:

Resolving patch:
diff --git a/keras/callbacks/callbacks.py b/keras/callbacks/callbacks.py
--- a/keras/callbacks/callbacks.py
+++ b/keras/callbacks/callbacks.py
@@ -1155,6 +1155,10 @@ def on_train_end(self, logs=None):
         self.csv_file.close()
         self.writer = None
 
+    def __del__(self):
+        if hasattr(self, 'csv_file') and not self.csv_file.closed:
+            self.csv_file.close()
+
 
 class LambdaCallback(Callback):
     r"""Callback for creating simple, custom callbacks on-the-fly.


Test patch:
Repository: keras-team/keras

Issue:
Error in the documentation of tf.keras.losses.MeanAbsolutePercentageError
# Description of the issue

The documentation of MAPE wrongly states that it is defined as `loss = 100 * abs(y_true - y_pred) / y_true` (see https://github.com/keras-team/keras/blob/2c48a3b38b6b6139be2da501982fd2f61d7d48fe/keras/losses.py#L372-L429).

However, it is implemented the right way (see this function, both documentation and computation are good : https://github.com/keras-team/keras/blob/2c48a3b38b6b6139be2da501982fd2f61d7d48fe/keras/losses.py#L1349)

# Proposed fix

Change `loss = 100 * abs(y_true - y_pred) / y_true` to `loss = 100 * abs((y_true - y_pred) / y_true)` to match the true formula used in computation. I can make the PR if you think it's relevant!

# Addendum

I also noticed that the implementation uses an epsilon in the denominator to avoid 0-division, which makes sense (same as in scikit-learn, for instance, see https://github.com/scikit-learn/scikit-learn/blob/0d378913b/sklearn/metrics/_regression.py#L286). I think it would be worthwhile to mention it also in keras' doc.





Discussion:

Resolving patch:
diff --git a/keras/losses.py b/keras/losses.py
--- a/keras/losses.py
+++ b/keras/losses.py
@@ -372,7 +372,9 @@ def __init__(self,
 class MeanAbsolutePercentageError(LossFunctionWrapper):
   """Computes the mean absolute percentage error between `y_true` and `y_pred`.
 
-  `loss = 100 * abs(y_true - y_pred) / y_true`
+  `loss = 100 * abs((y_true - y_pred) / y_true)`
+  
+  Note that to avoid dividing by zero, a small epsilon value is added to the denominator.
 
   Standalone usage:
 


Test patch:
Repository: keras-team/keras

Issue:
tf.keras.layers.Conv2D seems to accept 0 as the value of filters by mistake.
**System information**.
- Have I written custom code (as opposed to using a stock example script provided in Keras): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
- TensorFlow installed from (source or binary): 2.7.0
- Python version: 3.8

Please refer to tensorflow/python/keras/layers/convolutional.py, lines 139-141:
```python
 if filters is not None and filters < 0:
      raise ValueError(f'Received a negative value for `filters`.'
                       f'Was expecting a positive value, got {filters}.')
```
The error message says that the filters expects a positive value. It seems that the validation code  should be "filters <= 0".


Discussion:

Resolving patch:
diff --git a/keras/layers/convolutional/base_conv.py b/keras/layers/convolutional/base_conv.py
--- a/keras/layers/convolutional/base_conv.py
+++ b/keras/layers/convolutional/base_conv.py
@@ -118,9 +118,10 @@ def __init__(self,
 
     if isinstance(filters, float):
       filters = int(filters)
-    if filters is not None and filters < 0:
-      raise ValueError(f'Received a negative value for `filters`.'
-                       f'Was expecting a positive value. Received {filters}.')
+    if filters is not None and filters <= 0:
+      raise ValueError('Invalid value for argument `filters`. '
+                       'Expected a strictly positive value. '
+                       f'Received filters={filters}.')
     self.filters = filters
     self.groups = groups or 1
     self.kernel_size = conv_utils.normalize_tuple(


Test patch: