├── .coveragerc ├── .github ├── ISSUE_TEMPLATE │ ├── a--tensorflow-backend-users.md │ ├── b--theano-backend-users.md │ ├── c--cntk-backend-users.md │ ├── d--keras-preprocessing-users.md │ └── e--keras-applications-users.md ├── PULL_REQUEST_TEMPLATE │ ├── a--bug-fix.md │ └── b--new-feature.md └── stale.yml ├── .gitignore ├── .travis.yml ├── .travis └── install_cntk.sh ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── MANIFEST.in ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── docker ├── Dockerfile ├── Makefile ├── README.md └── theanorc ├── docs ├── README.md ├── __init__.py ├── autogen.py ├── mkdocs.yml ├── structure.py ├── templates │ ├── activations.md │ ├── applications.md │ ├── backend.md │ ├── callbacks.md │ ├── constraints.md │ ├── datasets.md │ ├── getting-started │ │ ├── faq.md │ │ ├── functional-api-guide.md │ │ └── sequential-model-guide.md │ ├── index.md │ ├── initializers.md │ ├── layers │ │ ├── about-keras-layers.md │ │ └── writing-your-own-keras-layers.md │ ├── losses.md │ ├── metrics.md │ ├── models │ │ ├── about-keras-models.md │ │ ├── model.md │ │ └── sequential.md │ ├── optimizers.md │ ├── preprocessing │ │ ├── image.md │ │ └── text.md │ ├── regularizers.md │ ├── scikit-learn-api.md │ ├── visualization.md │ └── why-use-keras.md └── theme │ ├── 404.html │ ├── base.html │ ├── breadcrumbs.html │ ├── css │ ├── theme.css │ └── theme_extra.css │ ├── footer.html │ ├── js │ ├── jquery-2.1.1.min.js │ ├── modernizr-2.8.3.min.js │ └── theme.js │ ├── main.html │ ├── nav.html │ ├── search.html │ ├── searchbox.html │ ├── toc.html │ └── versions.html ├── examples ├── README.md ├── addition_rnn.py ├── antirectifier.py ├── babi_memnn.py ├── babi_rnn.py ├── cifar10_cnn.py ├── cifar10_resnet.py ├── class_activation_maps.py ├── cnn_seq2seq.py ├── conv_filter_visualization.py ├── conv_lstm.py ├── deep_dream.py ├── image_ocr.py ├── imdb_bidirectional_lstm.py ├── imdb_cnn.py ├── imdb_cnn_lstm.py ├── imdb_fasttext.py ├── imdb_lstm.py ├── imdb_lstm_mask_scan.py ├── lstm_seq2seq.py ├── lstm_seq2seq_restore.py ├── lstm_stateful.py ├── lstm_text_generation.py ├── mnist_acgan.py ├── mnist_cnn.py ├── mnist_denoising_autoencoder.py ├── mnist_hierarchical_rnn.py ├── mnist_irnn.py ├── mnist_mlp.py ├── mnist_net2net.py ├── mnist_siamese.py ├── mnist_sklearn_wrapper.py ├── mnist_swwae.py ├── mnist_transfer_cnn.py ├── neural_doodle.py ├── neural_style_transfer.py ├── pretrained_word_embeddings.py ├── reuters_mlp.py ├── reuters_mlp_relu_vs_selu.py ├── variational_autoencoder.py └── variational_autoencoder_deconv.py ├── keras ├── __init__.py ├── activations.py ├── applications │ ├── __init__.py │ ├── densenet.py │ ├── imagenet_utils.py │ ├── inception_resnet_v2.py │ ├── inception_v3.py │ ├── mobilenet.py │ ├── mobilenet_v2.py │ ├── nasnet.py │ ├── resnet.py │ ├── resnet50.py │ ├── resnet_v2.py │ ├── vgg16.py │ ├── vgg19.py │ └── xception.py ├── backend │ ├── __init__.py │ ├── cntk_backend.py │ ├── common.py │ ├── load_backend.py │ ├── numpy_backend.py │ ├── tensorflow_backend.py │ └── theano_backend.py ├── caffe │ ├── README.md │ ├── __init__.py │ ├── caffe2keras.py │ ├── caffe_pb2.py │ ├── caffe_utils.py │ ├── convert.py │ ├── extra_layers.py │ ├── models │ │ ├── Keras_model_structure.json │ │ ├── Keras_model_weights.h5 │ │ ├── alexnet_fcn_deploy.prototxt │ │ ├── bvlc_googlenet.caffemodel │ │ ├── cat.jpg │ │ ├── cat_gray.jpg │ │ ├── classes.txt │ │ ├── fish-bike.jpg │ │ ├── segmentation │ │ │ ├── caffemodel-url.txt │ │ │ ├── debug.prototxt │ │ │ ├── deploy.prototxt │ │ │ ├── horse.png │ │ │ ├── mini_proof.png │ │ │ └── test_segmentation.py │ │ └── train_val_for_keras.prototxt │ ├── test_converted.py │ └── test_converted_VGG_FACE.py ├── callbacks │ ├── __init__.py │ ├── callbacks.py │ ├── tensorboard_v1.py │ └── tensorboard_v2.py ├── constraints.py ├── datasets │ ├── __init__.py │ ├── boston_housing.py │ ├── cifar.py │ ├── cifar10.py │ ├── cifar100.py │ ├── fashion_mnist.py │ ├── imdb.py │ ├── mnist.py │ └── reuters.py ├── engine │ ├── __init__.py │ ├── base_layer.py │ ├── input_layer.py │ ├── network.py │ ├── saving.py │ ├── sequential.py │ ├── topology.py │ ├── training.py │ ├── training_arrays.py │ ├── training_generator.py │ └── training_utils.py ├── initializers.py ├── layers │ ├── __init__.py │ ├── advanced_activations.py │ ├── attention.py │ ├── convolutional.py │ ├── convolutional_recurrent.py │ ├── core.py │ ├── cudnn_recurrent.py │ ├── embeddings.py │ ├── googlenet_custom_layers.py │ ├── local.py │ ├── loss_layers.py │ ├── merge.py │ ├── noise.py │ ├── normalization.py │ ├── pooling.py │ ├── recurrent.py │ ├── recurrent_advanced.py │ ├── uncertainty_layers.py │ └── wrappers.py ├── legacy │ ├── __init__.py │ ├── interfaces.py │ └── layers.py ├── losses.py ├── metrics.py ├── models.py ├── objectives.py ├── optimizers.py ├── preprocessing │ ├── __init__.py │ ├── image.py │ ├── sequence.py │ └── text.py ├── regularizers.py ├── utils │ ├── __init__.py │ ├── conv_utils.py │ ├── data_utils.py │ ├── generic_utils.py │ ├── io_utils.py │ ├── layer_utils.py │ ├── losses_utils.py │ ├── metrics_utils.py │ ├── multi_gpu_utils.py │ ├── np_utils.py │ ├── test_utils.py │ └── vis_utils.py └── wrappers │ ├── __init__.py │ └── scikit_learn.py ├── pytest.ini ├── setup.cfg ├── setup.py ├── tests ├── conftest.py ├── docs │ ├── test_doc_auto_generation.py │ └── test_documentation.py ├── integration_tests │ ├── applications_test.py │ ├── imagenet_utils_test.py │ ├── preprocessing │ │ ├── image_test.py │ │ ├── sequence_test.py │ │ └── text_test.py │ ├── test_datasets.py │ ├── test_image_data_tasks.py │ ├── test_temporal_data_tasks.py │ ├── test_tensorflow_integration.py │ └── test_vector_data_tasks.py ├── keras │ ├── activations_test.py │ ├── backend │ │ └── backend_test.py │ ├── caffe │ │ └── test_convert.py │ ├── callbacks │ │ ├── callbacks_test.py │ │ └── tensorboard_test.py │ ├── constraints_test.py │ ├── datasets │ │ └── datasets_test.py │ ├── engine │ │ ├── layer_subclassing_tests.py │ │ ├── test_topology.py │ │ └── test_training.py │ ├── initializers_test.py │ ├── layers │ │ ├── advanced_activations_test.py │ │ ├── convolutional_recurrent_test.py │ │ ├── convolutional_test.py │ │ ├── core_test.py │ │ ├── cudnn_recurrent_test.py │ │ ├── embeddings_test.py │ │ ├── local_test.py │ │ ├── merge_test.py │ │ ├── noise_test.py │ │ ├── normalization_test.py │ │ ├── pooling_test.py │ │ ├── recurrent_test.py │ │ └── wrappers_test.py │ ├── legacy │ │ ├── conftest.py │ │ ├── interface_test.py │ │ └── layers_test.py │ ├── losses_test.py │ ├── metrics_confusion_matrix_test.py │ ├── metrics_correctness_test.py │ ├── metrics_functional_test.py │ ├── metrics_test.py │ ├── metrics_training_test.py │ ├── optimizers_test.py │ ├── regularizers_test.py │ ├── test_sequential_model.py │ ├── utils │ │ ├── conv_utils_test.py │ │ ├── data_utils_test.py │ │ ├── generic_utils_test.py │ │ ├── io_utils_test.py │ │ ├── layer_utils_test.py │ │ ├── multi_gpu_test.py │ │ ├── np_utils_test.py │ │ └── vis_utils_test.py │ └── wrappers │ │ └── scikit_learn_test.py ├── test_api.py ├── test_dynamic_trainability.py ├── test_loss_masking.py ├── test_loss_weighting.py ├── test_model_pickling.py ├── test_model_saving.py └── test_multiprocessing.py └── update_api.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [report] 2 | # Regexes for lines to exclude from consideration 3 | exclude_lines = 4 | pragma: no cover 5 | os.remove 6 | except ImportError 7 | # Don't complain if tests don't hit defensive assertion code: 8 | raise ImportError 9 | raise NotImplementedError 10 | 11 | # Don't complain if legacy support codes are not performed: 12 | if original_keras_version == '1': 13 | 14 | fail_under = 60 15 | show_missing = True 16 | omit = 17 | keras/applications/* 18 | keras/preprocessing/* 19 | keras/datasets/* 20 | keras/layers/cudnn_recurrent.py 21 | keras/legacy/* 22 | keras/caffe/* 23 | keras/layers/attention.py 24 | keras/layers/googlenet_custom_layers.py 25 | keras/utils/multi_gpu_utils.py 26 | keras/backend/numpy_backend.py 27 | keras/backend/theano_backend.py 28 | keras/backend/tensorflow_backend.py 29 | keras/backend/cntk_backend.py 30 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/a--tensorflow-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: a) TensorFlow backend users 3 | about: Select this if you're using Keras with the TensorFlow backend (default). 4 | 5 | --- 6 | 7 | Please make sure that this is a Bug or a Feature Request and provide all applicable information asked by the template. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | **System information** 11 | - Have I written custom code (as opposed to using example directory): 12 | - OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 13 | - TensorFlow backend (yes / no): 14 | - TensorFlow version: 15 | - Keras version: 16 | - Python version: 17 | - CUDA/cuDNN version: 18 | - GPU model and memory: 19 | 20 | You can obtain the TensorFlow version with: 21 | python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)" 22 | You can obtain the Keras version with: 23 | python -c 'import keras as k; print(k.__version__)' 24 | 25 | **Describe the current behavior** 26 | 27 | **Describe the expected behavior** 28 | 29 | **Code to reproduce the issue** 30 | Provide a reproducible test case that is the bare minimum necessary to generate the problem. 31 | 32 | **Other info / logs** 33 | Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. 34 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/b--theano-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: b) Theano backend users 3 | about: Select this if you're using Keras with the Theano backend. 4 | 5 | --- 6 | 7 | Please make sure that the boxes below are checked before you submit your issue. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | Thank you! 11 | 12 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 13 | `pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps` 14 | 15 | - [ ] Check that you are up-to-date with the master branch of Theano. You can update with: 16 | `pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps` 17 | 18 | - [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short). 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/c--cntk-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: c) CNTK backend users 3 | about: Select this if you're using Keras with the CNTK backend. 4 | 5 | --- 6 | 7 | Please make sure that the boxes below are checked before you submit your issue. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | Thank you! 11 | 12 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 13 | `pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps` 14 | 15 | - [ ] Check that your version of CNTK is up-to-date. 16 | 17 | - [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short). 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/d--keras-preprocessing-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: d) `keras.preprocessing` users 3 | about: Select this if your issue is related to the `keras.preprocessing` module. 4 | 5 | --- 6 | 7 | 8 | The [keras.preprocessing](https://github.com/keras-team/keras-preprocessing) module has been splitted from the main Keras repository. 9 | 10 | Please submit your issue using this [link](https://github.com/keras-team/keras-preprocessing/issues/new/choose). 11 | 12 | Thank you! 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/e--keras-applications-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: e) `keras.applications` users 3 | about: Select this if your issue is related to the `keras.applications` module. 4 | 5 | --- 6 | 7 | 8 | The [keras.applications](https://github.com/keras-team/keras-applications) module has been splitted from the main Keras repository. 9 | 10 | Please submit your issue using this [link](https://github.com/keras-team/keras-applications/issues/new). 11 | 12 | Thank you! 13 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/a--bug-fix.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: a) Bug fix 3 | about: Select this if you're fixing a bug in Keras. 4 | labels: bugfix 5 | --- 6 | 7 | 11 | 12 | **- What bug I fixed** 13 | 14 | This pull request fixes #issue_number_here . 15 | 16 | **- How I fixed it** 17 | 18 | **- How you can verify it** 19 | 20 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/b--new-feature.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: b) New feature 3 | about: Select this if you're adding a new feature to Keras. 4 | labels: feature 5 | assignees: fchollet 6 | --- 7 | 8 | 12 | 13 | This pull request closes #issue_number_here . 14 | 15 | **- What I did** 16 | 17 | **- How I did it** 18 | 19 | **- How to verify it** 20 | 24 | 25 | 26 | - [ ] I updated the docs. 27 | 28 | This pull request adds a new feature to Keras. @fchollet, could you please take a look at it? 29 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Configuration for probot-stale - https://github.com/probot/stale 2 | 3 | # Number of days of inactivity before an Issue or Pull Request becomes stale 4 | daysUntilStale: 90 5 | # Number of days of inactivity before a stale Issue or Pull Request is closed 6 | daysUntilClose: 30 7 | # Issues or Pull Requests with these labels will never be considered stale 8 | exemptLabels: 9 | - bug 10 | - Announcement 11 | - help wanted 12 | - To investigate 13 | # Label to use when marking as stale 14 | staleLabel: stale 15 | # Comment to post when marking as stale. Set to `false` to disable 16 | markComment: > 17 | This issue has been automatically marked as stale because it has not had 18 | recent activity. It will be closed after 30 days if no further activity 19 | occurs, but feel free to re-open a closed issue if needed. 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.pyc 3 | *.swp 4 | *.xml 5 | temp/* 6 | dist/* 7 | build/* 8 | keras/datasets/data/* 9 | keras/datasets/temp/* 10 | docs/site/* 11 | docs/sources/* 12 | docs/theme/fonts/* 13 | docs/theme/img/* 14 | tags 15 | Keras.egg-info 16 | examples/img/* 17 | 18 | # test-related 19 | .coverage 20 | .cache 21 | .pytest_cache 22 | 23 | # developer environments 24 | .idea 25 | .vscode 26 | _linux/* 27 | inspection/* 28 | codestyles/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: bionic 2 | language: python 3 | cache: 4 | directories: 5 | - $HOME/.theano 6 | matrix: 7 | include: 8 | - python: 3.7 9 | env: KERAS_BACKEND=tensorflow MODE=INTEGRATION_TESTS PIL=Pillow 10 | - python: 3.7 11 | env: KERAS_BACKEND=tensorflow MODE=PEP8_DOC PIL=Pillow 12 | - python: 3.7 13 | env: KERAS_BACKEND=tensorflow MODE=API 14 | - python: 2.7 15 | env: KERAS_BACKEND=tensorflow 16 | - python: 3.7 17 | env: KERAS_BACKEND=tensorflow 18 | - python: 3.7 19 | env: KERAS_BACKEND=theano THEANO_FLAGS=optimizer=fast_compile MKL="mkl mkl-service" 20 | install: 21 | # code below is taken from http://conda.pydata.org/docs/travis.html 22 | # We do this conditionally because it saves us some downloading if the 23 | # version is the same. 24 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then 25 | wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh; 26 | else 27 | wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh; 28 | fi 29 | - bash miniconda.sh -b -p $HOME/miniconda 30 | - export PATH="$HOME/miniconda/bin:$PATH" 31 | - hash -r 32 | - conda config --set always_yes yes --set changeps1 no 33 | - conda update -q conda 34 | # Useful for debugging any issues with conda 35 | - conda info -a 36 | 37 | - travis_retry conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION 38 | - source activate test-environment 39 | 40 | - travis_retry pip install --only-binary=numpy,scipy,pandas numpy nose scipy h5py theano pytest pytest-pep8 pandas --progress-bar off 41 | - pip install keras_applications keras_preprocessing --progress-bar off 42 | 43 | # set library path 44 | - export LD_LIBRARY_PATH=$HOME/miniconda/envs/test-environment/lib/:$LD_LIBRARY_PATH 45 | 46 | # install pydot for visualization tests 47 | - travis_retry conda install -q $MKL pydot graphviz $PIL 48 | 49 | - pip install -e .[tests] --progress-bar off 50 | 51 | # install TensorFlow (CPU version). 52 | - pip install tensorflow==1.14.0 --progress-bar off 53 | 54 | # Remove the current backend from the coverage exclusion. 55 | - sed -i "\/keras\/backend\/${KERAS_BACKEND}_backend.py/d" .coveragerc 56 | 57 | # install mkdocs 58 | - pip install mkdocs --progress-bar off 59 | 60 | # install pyux 61 | - pip install pyux 62 | 63 | # command to run tests 64 | script: 65 | - export MKL_THREADING_LAYER="GNU" 66 | # run keras backend init to initialize backend config 67 | - python -c "import keras.backend" 68 | # create dataset directory to avoid concurrent directory creation at runtime 69 | - mkdir ~/.keras/datasets 70 | # set up keras backend 71 | - sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json; 72 | - echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)" 73 | - if [[ "MODE" == "INTEGRATION_TESTS" ]]; then 74 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/integration_tests; 75 | elif [[ "MODE" == "PEP8_DOC" ]]; then 76 | PYTHONPATH=$PWD:$PYTHONPATH py.test --pep8 -m pep8 -n0 && py.test tests/docs; 77 | elif [[ "MODE" == "API" ]]; then 78 | PYTHONPATH=$PWD:$PYTHONPATH pip install git+git://www.github.com/MarcBS/keras.git && python update_api.py && pip install -e .[tests] --progress-bar off && py.test tests/test_api.py; 79 | elif [[ "$RUN_ONLY_BACKEND_TESTS" == "1" ]]; then 80 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/keras/backend/; 81 | else 82 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --ignore=tests/docs --ignore=tests/keras/legacy/layers_test.py --ignore=tests/test_api.py --cov-config .coveragerc --cov=keras tests/; 83 | fi 84 | -------------------------------------------------------------------------------- /.travis/install_cntk.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | pip install cntk 3 | 4 | # open mpi is needed for cntk 5 | rm -rf ~/mpi 6 | mkdir ~/mpi 7 | pushd ~/mpi 8 | wget http://cntk.ai/PythonWheel/ForKeras/depends/openmpi_1.10-3.zip 9 | unzip ./openmpi_1.10-3.zip 10 | sudo dpkg -i openmpi_1.10-3.deb 11 | popd 12 | -------------------------------------------------------------------------------- /ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [join the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) and ask there instead of filing a GitHub issue. 2 | 3 | Thank you! 4 | 5 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 6 | pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps 7 | 8 | - [ ] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup). 9 | 10 | - [ ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: 11 | pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps 12 | 13 | - [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short). 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | COPYRIGHT 2 | 3 | All contributions by François Chollet: 4 | Copyright (c) 2015 - 2019, François Chollet. 5 | All rights reserved. 6 | 7 | All contributions by Google: 8 | Copyright (c) 2015 - 2019, Google, Inc. 9 | All rights reserved. 10 | 11 | All contributions by Microsoft: 12 | Copyright (c) 2017 - 2019, Microsoft, Inc. 13 | All rights reserved. 14 | 15 | All other contributions: 16 | Copyright (c) 2015 - 2019, the respective contributors. 17 | All rights reserved. 18 | 19 | Each contributor holds copyright over their respective contributions. 20 | The project versioning (Git) records all such contribution source information. 21 | 22 | LICENSE 23 | 24 | The MIT License (MIT) 25 | 26 | Permission is hereby granted, free of charge, to any person obtaining a copy 27 | of this software and associated documentation files (the "Software"), to deal 28 | in the Software without restriction, including without limitation the rights 29 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 30 | copies of the Software, and to permit persons to whom the Software is 31 | furnished to do so, subject to the following conditions: 32 | 33 | The above copyright notice and this permission notice shall be included in all 34 | copies or substantial portions of the Software. 35 | 36 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 37 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 38 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 39 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 40 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 41 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 42 | SOFTWARE. 43 | 44 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.md 3 | include CONTRIBUTING.md 4 | graft docs 5 | graft examples 6 | graft tests 7 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 8 | 9 | ### Summary 10 | 11 | ### Related Issues 12 | 13 | ### PR Overview 14 | 15 | - [ ] This PR requires new unit tests [y/n] (make sure tests are included) 16 | - [ ] This PR requires to update the documentation [y/n] (make sure the docs are up-to-date) 17 | - [ ] This PR is backwards compatible [y/n] 18 | - [ ] This PR changes the current API [y/n] (all API changes need to be approved by fchollet) 19 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | ARG cuda_version=9.0 2 | ARG cudnn_version=7 3 | FROM nvidia/cuda:${cuda_version}-cudnn${cudnn_version}-devel 4 | 5 | # Install system packages 6 | RUN apt-get update && apt-get install -y --no-install-recommends \ 7 | bzip2 \ 8 | g++ \ 9 | git \ 10 | graphviz \ 11 | libgl1-mesa-glx \ 12 | libhdf5-dev \ 13 | openmpi-bin \ 14 | wget && \ 15 | rm -rf /var/lib/apt/lists/* 16 | 17 | # Install conda 18 | ENV CONDA_DIR /opt/conda 19 | ENV PATH $CONDA_DIR/bin:$PATH 20 | 21 | RUN wget --quiet --no-check-certificate https://repo.continuum.io/miniconda/Miniconda3-4.2.12-Linux-x86_64.sh && \ 22 | echo "c59b3dd3cad550ac7596e0d599b91e75d88826db132e4146030ef471bb434e9a *Miniconda3-4.2.12-Linux-x86_64.sh" | sha256sum -c - && \ 23 | /bin/bash /Miniconda3-4.2.12-Linux-x86_64.sh -f -b -p $CONDA_DIR && \ 24 | rm Miniconda3-4.2.12-Linux-x86_64.sh && \ 25 | echo export PATH=$CONDA_DIR/bin:'$PATH' > /etc/profile.d/conda.sh 26 | 27 | # Install Python packages and keras 28 | ENV NB_USER keras 29 | ENV NB_UID 1000 30 | 31 | RUN useradd -m -s /bin/bash -N -u $NB_UID $NB_USER && \ 32 | chown $NB_USER $CONDA_DIR -R && \ 33 | mkdir -p /src && \ 34 | chown $NB_USER /src 35 | 36 | USER $NB_USER 37 | 38 | ARG python_version=3.6 39 | 40 | RUN conda config --append channels conda-forge 41 | RUN conda install -y python=${python_version} && \ 42 | pip install --upgrade pip && \ 43 | pip install \ 44 | sklearn_pandas \ 45 | tensorflow-gpu \ 46 | cntk-gpu && \ 47 | conda install \ 48 | bcolz \ 49 | h5py \ 50 | matplotlib \ 51 | mkl \ 52 | nose \ 53 | notebook \ 54 | Pillow \ 55 | pandas \ 56 | pydot \ 57 | pygpu \ 58 | pyyaml \ 59 | scikit-learn \ 60 | six \ 61 | theano \ 62 | mkdocs \ 63 | && \ 64 | git clone git://github.com/keras-team/keras.git /src && pip install -e /src[tests] && \ 65 | pip install git+git://github.com/keras-team/keras.git && \ 66 | conda clean -yt 67 | 68 | ADD theanorc /home/keras/.theanorc 69 | 70 | ENV LC_ALL=C.UTF-8 71 | ENV LANG=C.UTF-8 72 | 73 | ENV PYTHONPATH='/src/:$PYTHONPATH' 74 | 75 | WORKDIR /data 76 | 77 | EXPOSE 8888 78 | 79 | CMD jupyter notebook --port=8888 --ip=0.0.0.0 80 | -------------------------------------------------------------------------------- /docker/Makefile: -------------------------------------------------------------------------------- 1 | help: 2 | @cat Makefile 3 | 4 | DATA?="${HOME}/Data" 5 | GPU?=0 6 | DOCKER_FILE=Dockerfile 7 | DOCKER=GPU=$(GPU) nvidia-docker 8 | BACKEND=tensorflow 9 | PYTHON_VERSION?=3.6 10 | CUDA_VERSION?=9.0 11 | CUDNN_VERSION?=7 12 | TEST=tests/ 13 | SRC?=$(shell dirname `pwd`) 14 | 15 | build: 16 | docker build -t keras --build-arg python_version=$(PYTHON_VERSION) --build-arg cuda_version=$(CUDA_VERSION) --build-arg cudnn_version=$(CUDNN_VERSION) -f $(DOCKER_FILE) . 17 | 18 | bash: build 19 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras bash 20 | 21 | ipython: build 22 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras ipython 23 | 24 | notebook: build 25 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --net=host --env KERAS_BACKEND=$(BACKEND) keras 26 | 27 | test: build 28 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras py.test $(TEST) 29 | 30 | -------------------------------------------------------------------------------- /docker/README.md: -------------------------------------------------------------------------------- 1 | # Using Keras via Docker 2 | 3 | This directory contains `Dockerfile` to make it easy to get up and running with 4 | Keras via [Docker](http://www.docker.com/). 5 | 6 | ## Installing Docker 7 | 8 | General installation instructions are 9 | [on the Docker site](https://docs.docker.com/installation/), but we give some 10 | quick links here: 11 | 12 | * [OSX](https://docs.docker.com/installation/mac/): [docker toolbox](https://www.docker.com/toolbox) 13 | * [ubuntu](https://docs.docker.com/installation/ubuntulinux/) 14 | 15 | ## Running the container 16 | 17 | We are using `Makefile` to simplify docker commands within make commands. 18 | 19 | Build the container and start a Jupyter Notebook 20 | 21 | $ make notebook 22 | 23 | Build the container and start an iPython shell 24 | 25 | $ make ipython 26 | 27 | Build the container and start a bash 28 | 29 | $ make bash 30 | 31 | For GPU support install NVIDIA drivers (ideally latest) and 32 | [nvidia-docker](https://github.com/NVIDIA/nvidia-docker). Run using 33 | 34 | $ make notebook GPU=0 # or [ipython, bash] 35 | 36 | Switch between Theano and TensorFlow 37 | 38 | $ make notebook BACKEND=theano 39 | $ make notebook BACKEND=tensorflow 40 | 41 | Mount a volume for external data sets 42 | 43 | $ make DATA=~/mydata 44 | 45 | Prints all make tasks 46 | 47 | $ make help 48 | 49 | You can change Theano parameters by editing `/docker/theanorc`. 50 | 51 | 52 | Note: If you would have a problem running nvidia-docker you may try the old way 53 | we have used. But it is not recommended. If you find a bug in the nvidia-docker report 54 | it there please and try using the nvidia-docker as described above. 55 | 56 | $ export CUDA_SO=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}') 57 | $ export DEVICES=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}') 58 | $ docker run -it -p 8888:8888 $CUDA_SO $DEVICES gcr.io/tensorflow/tensorflow:latest-gpu 59 | -------------------------------------------------------------------------------- /docker/theanorc: -------------------------------------------------------------------------------- 1 | [global] 2 | floatX = float32 3 | optimizer=None 4 | device = cuda 5 | 6 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Keras Documentation 2 | 3 | The source for Keras documentation is in this directory. 4 | Our documentation uses extended Markdown, as implemented by [MkDocs](http://mkdocs.org). 5 | 6 | ## Building the documentation 7 | 8 | - Install MkDocs: `pip install mkdocs` 9 | - `pip install -e .` to make sure that Python will import your modified version of Keras. 10 | - From the root directory, `cd` into the `docs/` folder and run: 11 | - `KERAS_BACKEND=tensorflow python autogen.py` 12 | - `mkdocs serve` # Starts a local webserver: [localhost:8000](http://localhost:8000) 13 | - `mkdocs build` # Builds a static site in `site/` directory 14 | -------------------------------------------------------------------------------- /docs/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/docs/__init__.py -------------------------------------------------------------------------------- /docs/mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: Keras Documentation 2 | theme: 3 | name: null 4 | custom_dir: theme 5 | static_templates: 6 | - 404.html 7 | include_search_page: true 8 | search_index_only: false 9 | highlightjs: true 10 | hljs_languages: [] 11 | include_homepage_in_sidebar: true 12 | prev_next_buttons_location: bottom 13 | navigation_depth: 4 14 | titles_only: false 15 | sticky_navigation: true 16 | collapse_navigation: true 17 | 18 | docs_dir: sources 19 | repo_url: http://github.com/keras-team/keras 20 | site_url: http://keras.io/ 21 | site_description: 'Documentation for Keras, the Python Deep Learning library.' 22 | 23 | dev_addr: '0.0.0.0:8000' 24 | google_analytics: ['UA-61785484-1', 'keras.io'] 25 | 26 | nav: 27 | - Home: index.md 28 | - Why use Keras: why-use-keras.md 29 | - Getting started: 30 | - Guide to the Sequential model: getting-started/sequential-model-guide.md 31 | - Guide to the Functional API: getting-started/functional-api-guide.md 32 | - FAQ: getting-started/faq.md 33 | - Models: 34 | - About Keras models: models/about-keras-models.md 35 | - Sequential: models/sequential.md 36 | - Model (functional API): models/model.md 37 | - Layers: 38 | - About Keras layers: layers/about-keras-layers.md 39 | - Core Layers: layers/core.md 40 | - Convolutional Layers: layers/convolutional.md 41 | - Pooling Layers: layers/pooling.md 42 | - Locally-connected Layers: layers/local.md 43 | - Recurrent Layers: layers/recurrent.md 44 | - Embedding Layers: layers/embeddings.md 45 | - Merge Layers: layers/merge.md 46 | - Advanced Activations Layers: layers/advanced-activations.md 47 | - Normalization Layers: layers/normalization.md 48 | - Noise layers: layers/noise.md 49 | - Layer wrappers: layers/wrappers.md 50 | - Writing your own Keras layers: layers/writing-your-own-keras-layers.md 51 | - Preprocessing: 52 | - Sequence Preprocessing: preprocessing/sequence.md 53 | - Text Preprocessing: preprocessing/text.md 54 | - Image Preprocessing: preprocessing/image.md 55 | - Losses: losses.md 56 | - Metrics: metrics.md 57 | - Optimizers: optimizers.md 58 | - Activations: activations.md 59 | - Callbacks: callbacks.md 60 | - Datasets: datasets.md 61 | - Applications: applications.md 62 | - Backend: backend.md 63 | - Initializers: initializers.md 64 | - Regularizers: regularizers.md 65 | - Constraints: constraints.md 66 | - Visualization: visualization.md 67 | - Scikit-learn API: scikit-learn-api.md 68 | - Utils: utils.md 69 | - Contributing: contributing.md 70 | - Examples: 71 | - Addition RNN: examples/addition_rnn.md 72 | - Custom layer - antirectifier: examples/antirectifier.md 73 | - Baby RNN: examples/babi_rnn.md 74 | - Baby MemNN: examples/babi_memnn.md 75 | - CIFAR-10 CNN: examples/cifar10_cnn.md 76 | - CIFAR-10 ResNet: examples/cifar10_resnet.md 77 | - Convolution filter visualization: examples/conv_filter_visualization.md 78 | - Convolutional LSTM: examples/conv_lstm.md 79 | - Deep Dream: examples/deep_dream.md 80 | - Image OCR: examples/image_ocr.md 81 | - Bidirectional LSTM: examples/imdb_bidirectional_lstm.md 82 | - 1D CNN for text classification: examples/imdb_cnn.md 83 | - Sentiment classification CNN-LSTM: examples/imdb_cnn_lstm.md 84 | - Fasttext for text classification: examples/imdb_fasttext.md 85 | - Sentiment classification LSTM: examples/imdb_lstm.md 86 | - Sequence to sequence - training: examples/lstm_seq2seq.md 87 | - Sequence to sequence - prediction: examples/lstm_seq2seq_restore.md 88 | - Stateful LSTM: examples/lstm_stateful.md 89 | - LSTM for text generation: examples/lstm_text_generation.md 90 | - Auxiliary Classifier GAN: examples/mnist_acgan.md 91 | -------------------------------------------------------------------------------- /docs/templates/activations.md: -------------------------------------------------------------------------------- 1 | 2 | ## Usage of activations 3 | 4 | Activations can either be used through an `Activation` layer, or through the `activation` argument supported by all forward layers: 5 | 6 | ```python 7 | from keras.layers import Activation, Dense 8 | 9 | model.add(Dense(64)) 10 | model.add(Activation('tanh')) 11 | ``` 12 | 13 | This is equivalent to: 14 | 15 | ```python 16 | model.add(Dense(64, activation='tanh')) 17 | ``` 18 | 19 | You can also pass an element-wise TensorFlow/Theano/CNTK function as an activation: 20 | 21 | ```python 22 | from keras import backend as K 23 | 24 | model.add(Dense(64, activation=K.tanh)) 25 | ``` 26 | 27 | ## Available activations 28 | 29 | {{autogenerated}} 30 | 31 | ## On "Advanced Activations" 32 | 33 | Activations that are more complex than a simple TensorFlow/Theano/CNTK function (eg. learnable activations, which maintain a state) are available as [Advanced Activation layers](layers/advanced-activations.md), and can be found in the module `keras.layers.advanced_activations`. These include `PReLU` and `LeakyReLU`. 34 | -------------------------------------------------------------------------------- /docs/templates/callbacks.md: -------------------------------------------------------------------------------- 1 | ## Usage of callbacks 2 | 3 | A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument `callbacks`) to the `.fit()` method of the `Sequential` or `Model` classes. The relevant methods of the callbacks will then be called at each stage of the training. 4 | 5 | --- 6 | 7 | {{autogenerated}} 8 | 9 | --- 10 | 11 | 12 | # Create a callback 13 | 14 | You can create a custom callback by extending the base class `keras.callbacks.Callback`. A callback has access to its associated model through the class property `self.model`. 15 | 16 | Here's a simple example saving a list of losses over each batch during training: 17 | ```python 18 | class LossHistory(keras.callbacks.Callback): 19 | def on_train_begin(self, logs={}): 20 | self.losses = [] 21 | 22 | def on_batch_end(self, batch, logs={}): 23 | self.losses.append(logs.get('loss')) 24 | ``` 25 | 26 | --- 27 | 28 | ### Example: recording loss history 29 | 30 | ```python 31 | class LossHistory(keras.callbacks.Callback): 32 | def on_train_begin(self, logs={}): 33 | self.losses = [] 34 | 35 | def on_batch_end(self, batch, logs={}): 36 | self.losses.append(logs.get('loss')) 37 | 38 | model = Sequential() 39 | model.add(Dense(10, input_dim=784, kernel_initializer='uniform')) 40 | model.add(Activation('softmax')) 41 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop') 42 | 43 | history = LossHistory() 44 | model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history]) 45 | 46 | print(history.losses) 47 | # outputs 48 | ''' 49 | [0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789] 50 | ''' 51 | ``` 52 | 53 | --- 54 | 55 | ### Example: model checkpoints 56 | 57 | ```python 58 | from keras.callbacks import ModelCheckpoint 59 | 60 | model = Sequential() 61 | model.add(Dense(10, input_dim=784, kernel_initializer='uniform')) 62 | model.add(Activation('softmax')) 63 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop') 64 | 65 | ''' 66 | saves the model weights after each epoch if the validation loss decreased 67 | ''' 68 | checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', verbose=1, save_best_only=True) 69 | model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer]) 70 | ``` 71 | -------------------------------------------------------------------------------- /docs/templates/constraints.md: -------------------------------------------------------------------------------- 1 | ## Usage of constraints 2 | 3 | Functions from the `constraints` module allow setting constraints (eg. non-negativity) on network parameters during optimization. 4 | 5 | The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API. 6 | 7 | These layers expose 2 keyword arguments: 8 | 9 | - `kernel_constraint` for the main weights matrix 10 | - `bias_constraint` for the bias. 11 | 12 | 13 | ```python 14 | from keras.constraints import max_norm 15 | model.add(Dense(64, kernel_constraint=max_norm(2.))) 16 | ``` 17 | 18 | --- 19 | 20 | ## Available constraints 21 | 22 | 23 | {{autogenerated}} 24 | 25 | --- 26 | 27 | -------------------------------------------------------------------------------- /docs/templates/index.md: -------------------------------------------------------------------------------- 1 | # Keras: The Python Deep Learning library 2 | 3 | 4 | 5 | {{autogenerated}} -------------------------------------------------------------------------------- /docs/templates/initializers.md: -------------------------------------------------------------------------------- 1 | ## Usage of initializers 2 | 3 | Initializations define the way to set the initial random weights of Keras layers. 4 | 5 | The keyword arguments used for passing initializers to layers will depend on the layer. Usually it is simply `kernel_initializer` and `bias_initializer`: 6 | 7 | ```python 8 | model.add(Dense(64, 9 | kernel_initializer='random_uniform', 10 | bias_initializer='zeros')) 11 | ``` 12 | 13 | ## Available initializers 14 | 15 | The following built-in initializers are available as part of the `keras.initializers` module: 16 | 17 | {{autogenerated}} 18 | 19 | 20 | An initializer may be passed as a string (must match one of the available initializers above), or as a callable: 21 | 22 | ```python 23 | from keras import initializers 24 | 25 | model.add(Dense(64, kernel_initializer=initializers.random_normal(stddev=0.01))) 26 | 27 | # also works; will use the default parameters. 28 | model.add(Dense(64, kernel_initializer='random_normal')) 29 | ``` 30 | 31 | 32 | ## Using custom initializers 33 | 34 | If passing a custom callable, then it must take the argument `shape` (shape of the variable to initialize) and `dtype` (dtype of generated values): 35 | 36 | ```python 37 | from keras import backend as K 38 | 39 | def my_init(shape, dtype=None): 40 | return K.random_normal(shape, dtype=dtype) 41 | 42 | model.add(Dense(64, kernel_initializer=my_init)) 43 | ``` 44 | -------------------------------------------------------------------------------- /docs/templates/layers/about-keras-layers.md: -------------------------------------------------------------------------------- 1 | # About Keras layers 2 | 3 | All Keras layers have a number of methods in common: 4 | 5 | - `layer.get_weights()`: returns the weights of the layer as a list of Numpy arrays. 6 | - `layer.set_weights(weights)`: sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of `get_weights`). 7 | - `layer.get_config()`: returns a dictionary containing the configuration of the layer. The layer can be reinstantiated from its config via: 8 | 9 | ```python 10 | layer = Dense(32) 11 | config = layer.get_config() 12 | reconstructed_layer = Dense.from_config(config) 13 | ``` 14 | 15 | Or: 16 | 17 | ```python 18 | from keras import layers 19 | 20 | config = layer.get_config() 21 | layer = layers.deserialize({'class_name': layer.__class__.__name__, 22 | 'config': config}) 23 | ``` 24 | 25 | If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via: 26 | 27 | - `layer.input` 28 | - `layer.output` 29 | - `layer.input_shape` 30 | - `layer.output_shape` 31 | 32 | If the layer has multiple nodes (see: [the concept of layer node and shared layers](/getting-started/functional-api-guide/#the-concept-of-layer-node)), you can use the following methods: 33 | 34 | - `layer.get_input_at(node_index)` 35 | - `layer.get_output_at(node_index)` 36 | - `layer.get_input_shape_at(node_index)` 37 | - `layer.get_output_shape_at(node_index)` -------------------------------------------------------------------------------- /docs/templates/layers/writing-your-own-keras-layers.md: -------------------------------------------------------------------------------- 1 | # Writing your own Keras layers 2 | 3 | For simple, stateless custom operations, you are probably better off using `layers.core.Lambda` layers. But for any custom operation that has trainable weights, you should implement your own layer. 4 | 5 | Here is the skeleton of a Keras layer, **as of Keras 2.0** (if you have an older version, please upgrade). There are only three methods you need to implement: 6 | 7 | - `build(input_shape)`: this is where you will define your weights. This method must set `self.built = True` at the end, which can be done by calling `super([Layer], self).build()`. 8 | - `call(x)`: this is where the layer's logic lives. Unless you want your layer to support masking, you only have to care about the first argument passed to `call`: the input tensor. 9 | - `compute_output_shape(input_shape)`: in case your layer modifies the shape of its input, you should specify here the shape transformation logic. This allows Keras to do automatic shape inference. 10 | 11 | ```python 12 | from keras import backend as K 13 | from keras.layers import Layer 14 | 15 | class MyLayer(Layer): 16 | 17 | def __init__(self, output_dim, **kwargs): 18 | self.output_dim = output_dim 19 | super(MyLayer, self).__init__(**kwargs) 20 | 21 | def build(self, input_shape): 22 | # Create a trainable weight variable for this layer. 23 | self.kernel = self.add_weight(name='kernel', 24 | shape=(input_shape[1], self.output_dim), 25 | initializer='uniform', 26 | trainable=True) 27 | super(MyLayer, self).build(input_shape) # Be sure to call this at the end 28 | 29 | def call(self, x): 30 | return K.dot(x, self.kernel) 31 | 32 | def compute_output_shape(self, input_shape): 33 | return (input_shape[0], self.output_dim) 34 | ``` 35 | 36 | It is also possible to define Keras layers which have multiple input tensors and multiple output tensors. To do this, you should assume that the inputs and outputs of the methods `build(input_shape)`, `call(x)` and `compute_output_shape(input_shape)` are lists. Here is an example, similar to the one above: 37 | 38 | ```python 39 | from keras import backend as K 40 | from keras.layers import Layer 41 | 42 | class MyLayer(Layer): 43 | 44 | def __init__(self, output_dim, **kwargs): 45 | self.output_dim = output_dim 46 | super(MyLayer, self).__init__(**kwargs) 47 | 48 | def build(self, input_shape): 49 | assert isinstance(input_shape, list) 50 | # Create a trainable weight variable for this layer. 51 | self.kernel = self.add_weight(name='kernel', 52 | shape=(input_shape[0][1], self.output_dim), 53 | initializer='uniform', 54 | trainable=True) 55 | super(MyLayer, self).build(input_shape) # Be sure to call this at the end 56 | 57 | def call(self, x): 58 | assert isinstance(x, list) 59 | a, b = x 60 | return [K.dot(a, self.kernel) + b, K.mean(b, axis=-1)] 61 | 62 | def compute_output_shape(self, input_shape): 63 | assert isinstance(input_shape, list) 64 | shape_a, shape_b = input_shape 65 | return [(shape_a[0], self.output_dim), shape_b[:-1]] 66 | ``` 67 | 68 | The existing Keras layers provide examples of how to implement almost anything. Never hesitate to read the source code! 69 | -------------------------------------------------------------------------------- /docs/templates/losses.md: -------------------------------------------------------------------------------- 1 | 2 | ## Usage of loss functions 3 | 4 | A loss function (or objective function, or optimization score function) is one of the two parameters required to compile a model: 5 | 6 | ```python 7 | model.compile(loss='mean_squared_error', optimizer='sgd') 8 | ``` 9 | 10 | ```python 11 | from keras import losses 12 | 13 | model.compile(loss=losses.mean_squared_error, optimizer='sgd') 14 | ``` 15 | 16 | You can either pass the name of an existing loss function, or pass a TensorFlow/Theano symbolic function that returns a scalar for each data-point and takes the following two arguments: 17 | 18 | - __y_true__: True labels. TensorFlow/Theano tensor. 19 | - __y_pred__: Predictions. TensorFlow/Theano tensor of the same shape as y_true. 20 | 21 | The actual optimized objective is the mean of the output array across all datapoints. 22 | 23 | For a few examples of such functions, check out the [losses source](https://github.com/keras-team/keras/blob/master/keras/losses.py). 24 | 25 | ## Available loss functions 26 | 27 | {{autogenerated}} 28 | 29 | ---- 30 | 31 | **Note**: when using the `categorical_crossentropy` loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros except for a 1 at the index corresponding to the class of the sample). In order to convert *integer targets* into *categorical targets*, you can use the Keras utility `to_categorical`: 32 | 33 | ```python 34 | from keras.utils import to_categorical 35 | 36 | categorical_labels = to_categorical(int_labels, num_classes=None) 37 | ``` 38 | 39 | When using the `sparse_categorical_crossentropy` loss, your targets should be *integer targets*. 40 | If you have categorical targets, you should use `categorical_crossentropy`. 41 | 42 | `categorical_crossentropy` is another term for [multi-class log loss](http://wiki.fast.ai/index.php/Log_Loss). 43 | -------------------------------------------------------------------------------- /docs/templates/metrics.md: -------------------------------------------------------------------------------- 1 | 2 | ## Usage of metrics 3 | 4 | A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the `metrics` parameter when a model is compiled. 5 | 6 | ```python 7 | model.compile(loss='mean_squared_error', 8 | optimizer='sgd', 9 | metrics=['mae', 'acc']) 10 | ``` 11 | 12 | ```python 13 | from keras import metrics 14 | 15 | model.compile(loss='mean_squared_error', 16 | optimizer='sgd', 17 | metrics=[metrics.mae, metrics.categorical_accuracy]) 18 | ``` 19 | 20 | A metric function is similar to a [loss function](/losses), except that the results from evaluating a metric are not used when training the model. You may use any of the loss functions as a metric function. 21 | 22 | You can either pass the name of an existing metric, or pass a Theano/TensorFlow symbolic function (see [Custom metrics](#custom-metrics)). 23 | 24 | #### Arguments 25 | - __y_true__: True labels. Theano/TensorFlow tensor. 26 | - __y_pred__: Predictions. Theano/TensorFlow tensor of the same shape as y_true. 27 | 28 | #### Returns 29 | Single tensor value representing the mean of the output array across all 30 | datapoints. 31 | 32 | ---- 33 | 34 | ## Available metrics 35 | 36 | 37 | {{autogenerated}} 38 | 39 | In addition to the metrics above, you may use any of the loss functions described in the [loss function](/losses) page as metrics. 40 | 41 | ---- 42 | 43 | ## Custom metrics 44 | 45 | Custom metrics can be passed at the compilation step. The 46 | function would need to take `(y_true, y_pred)` as arguments and return 47 | a single tensor value. 48 | 49 | ```python 50 | import keras.backend as K 51 | 52 | def mean_pred(y_true, y_pred): 53 | return K.mean(y_pred) 54 | 55 | model.compile(optimizer='rmsprop', 56 | loss='binary_crossentropy', 57 | metrics=['accuracy', mean_pred]) 58 | ``` 59 | -------------------------------------------------------------------------------- /docs/templates/models/model.md: -------------------------------------------------------------------------------- 1 | # Model class API 2 | 3 | In the functional API, given some input tensor(s) and output tensor(s), you can instantiate a `Model` via: 4 | 5 | ```python 6 | from keras.models import Model 7 | from keras.layers import Input, Dense 8 | 9 | a = Input(shape=(32,)) 10 | b = Dense(32)(a) 11 | model = Model(inputs=a, outputs=b) 12 | ``` 13 | 14 | This model will include all layers required in the computation of `b` given `a`. 15 | 16 | In the case of multi-input or multi-output models, you can use lists as well: 17 | 18 | ```python 19 | model = Model(inputs=[a1, a2], outputs=[b1, b2, b3]) 20 | ``` 21 | 22 | For a detailed introduction of what `Model` can do, read [this guide to the Keras functional API](/getting-started/functional-api-guide). 23 | 24 | 25 | ## Methods 26 | 27 | {{autogenerated}} 28 | -------------------------------------------------------------------------------- /docs/templates/models/sequential.md: -------------------------------------------------------------------------------- 1 | # The Sequential model API 2 | 3 | To get started, read [this guide to the Keras Sequential model](/getting-started/sequential-model-guide). 4 | 5 | ---- 6 | 7 | ## Sequential model methods 8 | 9 | {{autogenerated}} -------------------------------------------------------------------------------- /docs/templates/optimizers.md: -------------------------------------------------------------------------------- 1 | 2 | ## Usage of optimizers 3 | 4 | An optimizer is one of the two arguments required for compiling a Keras model: 5 | 6 | ```python 7 | from keras import optimizers 8 | 9 | model = Sequential() 10 | model.add(Dense(64, kernel_initializer='uniform', input_shape=(10,))) 11 | model.add(Activation('softmax')) 12 | 13 | sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 14 | model.compile(loss='mean_squared_error', optimizer=sgd) 15 | ``` 16 | 17 | You can either instantiate an optimizer before passing it to `model.compile()` , as in the above example, or you can call it by its name. In the latter case, the default parameters for the optimizer will be used. 18 | 19 | ```python 20 | # pass optimizer by name: default parameters will be used 21 | model.compile(loss='mean_squared_error', optimizer='sgd') 22 | ``` 23 | 24 | --- 25 | 26 | ## Parameters common to all Keras optimizers 27 | 28 | The parameters `clipnorm` and `clipvalue` can be used with all optimizers to control gradient clipping: 29 | 30 | ```python 31 | from keras import optimizers 32 | 33 | # All parameter gradients will be clipped to 34 | # a maximum norm of 1. 35 | sgd = optimizers.SGD(lr=0.01, clipnorm=1.) 36 | ``` 37 | 38 | ```python 39 | from keras import optimizers 40 | 41 | # All parameter gradients will be clipped to 42 | # a maximum value of 0.5 and 43 | # a minimum value of -0.5. 44 | sgd = optimizers.SGD(lr=0.01, clipvalue=0.5) 45 | ``` 46 | 47 | --- 48 | 49 | {{autogenerated}} 50 | -------------------------------------------------------------------------------- /docs/templates/preprocessing/image.md: -------------------------------------------------------------------------------- 1 | 2 | # Image Preprocessing 3 | 4 | {{autogenerated}} 5 | -------------------------------------------------------------------------------- /docs/templates/preprocessing/text.md: -------------------------------------------------------------------------------- 1 | 2 | ### Text Preprocessing 3 | 4 | {{autogenerated}} 5 | -------------------------------------------------------------------------------- /docs/templates/regularizers.md: -------------------------------------------------------------------------------- 1 | ## Usage of regularizers 2 | 3 | Regularizers allow to apply penalties on layer parameters or layer activity during optimization. These penalties are incorporated in the loss function that the network optimizes. 4 | 5 | The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API. 6 | 7 | These layers expose 3 keyword arguments: 8 | 9 | - `kernel_regularizer`: instance of `keras.regularizers.Regularizer` 10 | - `bias_regularizer`: instance of `keras.regularizers.Regularizer` 11 | - `activity_regularizer`: instance of `keras.regularizers.Regularizer` 12 | 13 | 14 | ## Example 15 | 16 | ```python 17 | from keras import regularizers 18 | model.add(Dense(64, input_dim=64, 19 | kernel_regularizer=regularizers.l2(0.01), 20 | activity_regularizer=regularizers.l1(0.01))) 21 | ``` 22 | 23 | ## Available penalties 24 | 25 | ```python 26 | keras.regularizers.l1(0.) 27 | keras.regularizers.l2(0.) 28 | keras.regularizers.l1_l2(l1=0.01, l2=0.01) 29 | ``` 30 | 31 | ## Developing new regularizers 32 | 33 | Any function that takes in a weight matrix and returns a loss contribution tensor can be used as a regularizer, e.g.: 34 | 35 | ```python 36 | from keras import backend as K 37 | 38 | def l1_reg(weight_matrix): 39 | return 0.01 * K.sum(K.abs(weight_matrix)) 40 | 41 | model.add(Dense(64, input_dim=64, 42 | kernel_regularizer=l1_reg)) 43 | ``` 44 | 45 | Alternatively, you can write your regularizers in an object-oriented way; 46 | see the [keras/regularizers.py](https://github.com/keras-team/keras/blob/master/keras/regularizers.py) module for examples. 47 | -------------------------------------------------------------------------------- /docs/templates/scikit-learn-api.md: -------------------------------------------------------------------------------- 1 | # Wrappers for the Scikit-Learn API 2 | 3 | You can use `Sequential` Keras models (single-input only) as part of your Scikit-Learn workflow via the wrappers found at `keras.wrappers.scikit_learn.py`. 4 | 5 | There are two wrappers available: 6 | 7 | `keras.wrappers.scikit_learn.KerasClassifier(build_fn=None, **sk_params)`, which implements the Scikit-Learn classifier interface, 8 | 9 | `keras.wrappers.scikit_learn.KerasRegressor(build_fn=None, **sk_params)`, which implements the Scikit-Learn regressor interface. 10 | 11 | ### Arguments 12 | 13 | - __build_fn__: callable function or class instance 14 | - __sk_params__: model parameters & fitting parameters 15 | 16 | `build_fn` should construct, compile and return a Keras model, which 17 | will then be used to fit/predict. One of the following 18 | three values could be passed to `build_fn`: 19 | 20 | 1. A function 21 | 2. An instance of a class that implements the `__call__` method 22 | 3. None. This means you implement a class that inherits from either 23 | `KerasClassifier` or `KerasRegressor`. The `__call__` method of the 24 | present class will then be treated as the default `build_fn`. 25 | 26 | `sk_params` takes both model parameters and fitting parameters. Legal model 27 | parameters are the arguments of `build_fn`. Note that like all other 28 | estimators in scikit-learn, `build_fn` should provide default values for 29 | its arguments, so that you could create the estimator without passing any 30 | values to `sk_params`. 31 | 32 | `sk_params` could also accept parameters for calling `fit`, `predict`, 33 | `predict_proba`, and `score` methods (e.g., `epochs`, `batch_size`). 34 | fitting (predicting) parameters are selected in the following order: 35 | 36 | 1. Values passed to the dictionary arguments of 37 | `fit`, `predict`, `predict_proba`, and `score` methods 38 | 2. Values passed to `sk_params` 39 | 3. The default values of the `keras.models.Sequential` 40 | `fit`, `predict`, `predict_proba` and `score` methods 41 | 42 | When using scikit-learn's `grid_search` API, legal tunable parameters are 43 | those you could pass to `sk_params`, including fitting parameters. 44 | In other words, you could use `grid_search` to search for the best 45 | `batch_size` or `epochs` as well as the model parameters. 46 | -------------------------------------------------------------------------------- /docs/templates/visualization.md: -------------------------------------------------------------------------------- 1 | 2 | ## Model visualization 3 | 4 | Keras provides utility functions to plot a Keras model (using `graphviz`). 5 | 6 | This will plot a graph of the model and save it to a file: 7 | ```python 8 | from keras.utils import plot_model 9 | plot_model(model, to_file='model.png') 10 | ``` 11 | 12 | `plot_model` takes four optional arguments: 13 | 14 | - `show_shapes` (defaults to False) controls whether output shapes are shown in the graph. 15 | - `show_layer_names` (defaults to True) controls whether layer names are shown in the graph. 16 | - `expand_nested` (defaults to False) controls whether to expand nested models into clusters in the graph. 17 | - `dpi` (defaults to 96) controls image dpi. 18 | 19 | You can also directly obtain the `pydot.Graph` object and render it yourself, 20 | for example to show it in an ipython notebook : 21 | ```python 22 | from IPython.display import SVG 23 | from keras.utils import model_to_dot 24 | 25 | SVG(model_to_dot(model).create(prog='dot', format='svg')) 26 | ``` 27 | 28 | ## Training history visualization 29 | 30 | The `fit()` method on a Keras `Model` returns a `History` object. The `History.history` attribute is a dictionary recording training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable). Here is a simple example using `matplotlib` to generate loss & accuracy plots for training & validation: 31 | 32 | ```python 33 | import matplotlib.pyplot as plt 34 | 35 | history = model.fit(x, y, validation_split=0.25, epochs=50, batch_size=16, verbose=1) 36 | 37 | # Plot training & validation accuracy values 38 | plt.plot(history.history['acc']) 39 | plt.plot(history.history['val_acc']) 40 | plt.title('Model accuracy') 41 | plt.ylabel('Accuracy') 42 | plt.xlabel('Epoch') 43 | plt.legend(['Train', 'Test'], loc='upper left') 44 | plt.show() 45 | 46 | # Plot training & validation loss values 47 | plt.plot(history.history['loss']) 48 | plt.plot(history.history['val_loss']) 49 | plt.title('Model loss') 50 | plt.ylabel('Loss') 51 | plt.xlabel('Epoch') 52 | plt.legend(['Train', 'Test'], loc='upper left') 53 | plt.show() 54 | ``` 55 | -------------------------------------------------------------------------------- /docs/theme/404.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |

404

6 | 7 |

Page not found

8 | 9 | {% endblock %} 10 | -------------------------------------------------------------------------------- /docs/theme/breadcrumbs.html: -------------------------------------------------------------------------------- 1 |
2 | 29 | {% if config.theme.prev_next_buttons_location|lower in ['top', 'both'] 30 | and page and (page.next_page or page.previous_page) %} 31 | 39 | {% endif %} 40 |
41 |
42 | -------------------------------------------------------------------------------- /docs/theme/css/theme_extra.css: -------------------------------------------------------------------------------- 1 | /* 2 | * Wrap inline code samples otherwise they shoot of the side and 3 | * can't be read at all. 4 | * 5 | * https://github.com/mkdocs/mkdocs/issues/313 6 | * https://github.com/mkdocs/mkdocs/issues/233 7 | * https://github.com/mkdocs/mkdocs/issues/834 8 | */ 9 | .rst-content code { 10 | white-space: pre-wrap; 11 | word-wrap: break-word; 12 | padding: 2px 5px; 13 | } 14 | 15 | /** 16 | * Make code blocks display as blocks and give them the appropriate 17 | * font size and padding. 18 | * 19 | * https://github.com/mkdocs/mkdocs/issues/855 20 | * https://github.com/mkdocs/mkdocs/issues/834 21 | * https://github.com/mkdocs/mkdocs/issues/233 22 | */ 23 | .rst-content pre code { 24 | white-space: pre; 25 | word-wrap: normal; 26 | display: block; 27 | padding: 12px; 28 | font-size: 12px; 29 | } 30 | 31 | /* 32 | * Fix link colors when the link text is inline code. 33 | * 34 | * https://github.com/mkdocs/mkdocs/issues/718 35 | */ 36 | a code { 37 | color: #2980B9; 38 | } 39 | a:hover code { 40 | color: #3091d1; 41 | } 42 | a:visited code { 43 | color: #9B59B6; 44 | } 45 | 46 | /* 47 | * The CSS classes from highlight.js seem to clash with the 48 | * ReadTheDocs theme causing some code to be incorrectly made 49 | * bold and italic. 50 | * 51 | * https://github.com/mkdocs/mkdocs/issues/411 52 | */ 53 | pre .cs, pre .c { 54 | font-weight: inherit; 55 | font-style: inherit; 56 | } 57 | 58 | /* 59 | * Fix some issues with the theme and non-highlighted code 60 | * samples. Without and highlighting styles attached the 61 | * formatting is broken. 62 | * 63 | * https://github.com/mkdocs/mkdocs/issues/319 64 | */ 65 | .rst-content .no-highlight { 66 | display: block; 67 | padding: 0.5em; 68 | color: #333; 69 | } 70 | 71 | 72 | /* 73 | * Additions specific to the search functionality provided by MkDocs 74 | */ 75 | 76 | .search-results { 77 | margin-top: 23px; 78 | } 79 | 80 | .search-results article { 81 | border-top: 1px solid #E1E4E5; 82 | padding-top: 24px; 83 | } 84 | 85 | .search-results article:first-child { 86 | border-top: none; 87 | } 88 | 89 | form .search-query { 90 | width: 100%; 91 | border-radius: 50px; 92 | padding: 6px 12px; /* csslint allow: box-model */ 93 | border-color: #D1D4D5; 94 | } 95 | 96 | /* 97 | * Improve inline code blocks within admonitions. 98 | * 99 | * https://github.com/mkdocs/mkdocs/issues/656 100 | */ 101 | .rst-content .admonition code { 102 | color: #404040; 103 | border: 1px solid #c7c9cb; 104 | border: 1px solid rgba(0, 0, 0, 0.2); 105 | background: #f8fbfd; 106 | background: rgba(255, 255, 255, 0.7); 107 | } 108 | 109 | /* 110 | * Account for wide tables which go off the side. 111 | * Override borders to avoid weirdness on narrow tables. 112 | * 113 | * https://github.com/mkdocs/mkdocs/issues/834 114 | * https://github.com/mkdocs/mkdocs/pull/1034 115 | */ 116 | .rst-content .section .docutils { 117 | width: 100%; 118 | overflow: auto; 119 | display: block; 120 | border: none; 121 | } 122 | 123 | td, th { 124 | border: 1px solid #e1e4e5 !important; /* csslint allow: important */ 125 | border-collapse: collapse; 126 | } 127 | 128 | /*Keras extras*/ 129 | 130 | .keras-logo { 131 | max-height: 55px; 132 | width: 100%; 133 | background: #d00000; 134 | font-size: 140%; 135 | color: white; 136 | font-family: "Source Sans Pro", "ff-tisa-web-pro", "Georgia", Arial, sans-serif; 137 | } 138 | 139 | .keras-logo-img { 140 | max-width: 45px; 141 | margin: 10px; 142 | } 143 | 144 | h1, h2, h3, h4, h5, h6, legend { 145 | font-family: "Source Sans Pro", "ff-tisa-web-pro", "Georgia", Arial, sans-serif;, 146 | } 147 | 148 | -------------------------------------------------------------------------------- /docs/theme/footer.html: -------------------------------------------------------------------------------- 1 | 27 | -------------------------------------------------------------------------------- /docs/theme/main.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | -------------------------------------------------------------------------------- /docs/theme/nav.html: -------------------------------------------------------------------------------- 1 | {{ nav_item.title }} 2 | {%- set navlevel = navlevel + 1 %} 3 | {%- if navlevel <= config.theme.navigation_depth 4 | and ((nav_item.is_page and nav_item.toc.items 5 | and (not config.theme.titles_only 6 | and (nav_item == page or not config.theme.collapse_navigation))) 7 | or (nav_item.is_section and nav_item.children)) %} 8 | 9 | {%- if nav_item.is_page %} 10 | {#- Skip first level of toc which is page title. #} 11 | {%- set toc_item = nav_item.toc.items[0] %} 12 | {%- include 'toc.html' %} 13 | {%- elif nav_item.is_section %} 14 | {%- for nav_item in nav_item.children %} 15 | 18 | {%- endfor %} 19 | {%- endif %} 20 | 21 | {%- endif %} 22 | {%- set navlevel = navlevel - 1 %} 23 | -------------------------------------------------------------------------------- /docs/theme/search.html: -------------------------------------------------------------------------------- 1 | {% extends "base.html" %} 2 | 3 | {% block content %} 4 | 5 |

Search Results

6 | 7 | 11 | 12 |
13 | Searching... 14 |
15 | 16 | {% endblock %} 17 | -------------------------------------------------------------------------------- /docs/theme/searchbox.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 |
5 |
6 | -------------------------------------------------------------------------------- /docs/theme/toc.html: -------------------------------------------------------------------------------- 1 | 2 | {%- for toc_item in toc_item.children %} 3 | 12 | {%- endfor %} 13 | -------------------------------------------------------------------------------- /docs/theme/versions.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | {% if config.repo_name == 'GitHub' %} 4 | GitHub 5 | {% elif config.repo_name == 'Bitbucket' %} 6 | BitBucket 7 | {% elif config.repo_name == 'GitLab' %} 8 | GitLab 9 | {% endif %} 10 | {% if page.previous_page %} 11 | « Previous 12 | {% endif %} 13 | {% if page.next_page %} 14 | Next » 15 | {% endif %} 16 | 17 |
18 | -------------------------------------------------------------------------------- /examples/antirectifier.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #This example demonstrates how to write custom layers for Keras. 3 | 4 | We build a custom activation layer called 'Antirectifier', 5 | which modifies the shape of the tensor that passes through it. 6 | We need to specify two methods: `compute_output_shape` and `call`. 7 | 8 | Note that the same result can also be achieved via a Lambda layer. 9 | 10 | Because our custom layer is written with primitives from the Keras 11 | backend (`K`), our code can run both on TensorFlow and Theano. 12 | ''' 13 | 14 | from __future__ import print_function 15 | import keras 16 | from keras.models import Sequential 17 | from keras import layers 18 | from keras.datasets import mnist 19 | from keras import backend as K 20 | 21 | 22 | class Antirectifier(layers.Layer): 23 | '''This is the combination of a sample-wise 24 | L2 normalization with the concatenation of the 25 | positive part of the input with the negative part 26 | of the input. The result is a tensor of samples that are 27 | twice as large as the input samples. 28 | 29 | It can be used in place of a ReLU. 30 | 31 | # Input shape 32 | 2D tensor of shape (samples, n) 33 | 34 | # Output shape 35 | 2D tensor of shape (samples, 2*n) 36 | 37 | # Theoretical justification 38 | When applying ReLU, assuming that the distribution 39 | of the previous output is approximately centered around 0., 40 | you are discarding half of your input. This is inefficient. 41 | 42 | Antirectifier allows to return all-positive outputs like ReLU, 43 | without discarding any data. 44 | 45 | Tests on MNIST show that Antirectifier allows to train networks 46 | with twice less parameters yet with comparable 47 | classification accuracy as an equivalent ReLU-based network. 48 | ''' 49 | 50 | def compute_output_shape(self, input_shape): 51 | shape = list(input_shape) 52 | assert len(shape) == 2 # only valid for 2D tensors 53 | shape[-1] *= 2 54 | return tuple(shape) 55 | 56 | def call(self, inputs): 57 | inputs -= K.mean(inputs, axis=1, keepdims=True) 58 | inputs = K.l2_normalize(inputs, axis=1) 59 | pos = K.relu(inputs) 60 | neg = K.relu(-inputs) 61 | return K.concatenate([pos, neg], axis=1) 62 | 63 | # global parameters 64 | batch_size = 128 65 | num_classes = 10 66 | epochs = 40 67 | 68 | # the data, split between train and test sets 69 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 70 | 71 | x_train = x_train.reshape(60000, 784) 72 | x_test = x_test.reshape(10000, 784) 73 | x_train = x_train.astype('float32') 74 | x_test = x_test.astype('float32') 75 | x_train /= 255 76 | x_test /= 255 77 | print(x_train.shape[0], 'train samples') 78 | print(x_test.shape[0], 'test samples') 79 | 80 | # convert class vectors to binary class matrices 81 | y_train = keras.utils.to_categorical(y_train, num_classes) 82 | y_test = keras.utils.to_categorical(y_test, num_classes) 83 | 84 | # build the model 85 | model = Sequential() 86 | model.add(layers.Dense(256, input_shape=(784,))) 87 | model.add(Antirectifier()) 88 | model.add(layers.Dropout(0.1)) 89 | model.add(layers.Dense(256)) 90 | model.add(Antirectifier()) 91 | model.add(layers.Dropout(0.1)) 92 | model.add(layers.Dense(num_classes)) 93 | model.add(layers.Activation('softmax')) 94 | 95 | # compile the model 96 | model.compile(loss='categorical_crossentropy', 97 | optimizer='rmsprop', 98 | metrics=['accuracy']) 99 | 100 | # train the model 101 | model.fit(x_train, y_train, 102 | batch_size=batch_size, 103 | epochs=epochs, 104 | verbose=1, 105 | validation_data=(x_test, y_test)) 106 | 107 | # next, compare with an equivalent network 108 | # with2x bigger Dense layers and ReLU 109 | -------------------------------------------------------------------------------- /examples/class_activation_maps.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import numpy as np 4 | import argparse 5 | import cv2 6 | import matplotlib.pyplot as plt 7 | 8 | from keras.models import Model 9 | 10 | import keras.applications.resnet50 as resnet 11 | from keras.layers import UpSampling2D, Conv2D 12 | 13 | # Set an appropriate image file 14 | parser = argparse.ArgumentParser(description='Class activation maps with Keras.') 15 | parser.add_argument('input_image', metavar='base', type=str, 16 | help='Path to the image to use.') 17 | args = parser.parse_args() 18 | input_image = args.input_image 19 | 20 | 21 | ################################################################ 22 | # The following parameters can be changed to other models 23 | # that use global average pooling. 24 | # e.g.) InceptionResnetV2 / NASNetLarge 25 | NETWORK_INPUT_SIZE = 224 26 | MODEL_CLASS = resnet.ResNet50 27 | PREPROCESS_FN = resnet.preprocess_input 28 | LAST_CONV_LAYER = 'activation_49' 29 | PRED_LAYER = 'fc1000' 30 | ################################################################ 31 | 32 | # number of imagenet classes 33 | N_CLASSES = 1000 34 | 35 | 36 | def load_img(fname, input_size, preprocess_fn): 37 | original_img = cv2.imread(fname)[:, :, ::-1] 38 | original_size = (original_img.shape[1], original_img.shape[0]) 39 | img = cv2.resize(original_img, (input_size, input_size)) 40 | imgs = np.expand_dims(preprocess_fn(img), axis=0) 41 | return imgs, original_img, original_size 42 | 43 | 44 | def get_cam_model(model_class, 45 | input_size=224, 46 | last_conv_layer='activation_49', 47 | pred_layer='fc1000'): 48 | model = model_class(input_shape=(input_size, input_size, 3)) 49 | 50 | final_params = model.get_layer(pred_layer).get_weights() 51 | final_params = (final_params[0].reshape( 52 | 1, 1, -1, N_CLASSES), final_params[1]) 53 | 54 | last_conv_output = model.get_layer(last_conv_layer).output 55 | x = UpSampling2D(size=(32, 32), interpolation='bilinear')( 56 | last_conv_output) 57 | x = Conv2D(filters=N_CLASSES, kernel_size=( 58 | 1, 1), name='predictions_2')(x) 59 | 60 | cam_model = Model(inputs=model.input, 61 | outputs=[model.output, x]) 62 | cam_model.get_layer('predictions_2').set_weights(final_params) 63 | return cam_model 64 | 65 | 66 | def postprocess(preds, cams, top_k=1): 67 | idxes = np.argsort(preds[0])[-top_k:] 68 | class_activation_map = np.zeros_like(cams[0, :, :, 0]) 69 | for i in idxes: 70 | class_activation_map += cams[0, :, :, i] 71 | return class_activation_map 72 | 73 | 74 | # 1. load image 75 | imgs, original_img, original_size = load_img(input_image, 76 | input_size=NETWORK_INPUT_SIZE, 77 | preprocess_fn=resnet.preprocess_input) 78 | 79 | # 2. prediction 80 | model = get_cam_model(resnet.ResNet50, 81 | NETWORK_INPUT_SIZE, 82 | LAST_CONV_LAYER, 83 | PRED_LAYER) 84 | preds, cams = model.predict(imgs) 85 | 86 | # 4. post processing 87 | class_activation_map = postprocess(preds, cams) 88 | 89 | # 5. plot image+cam to original size 90 | plt.imshow(original_img, alpha=0.5) 91 | plt.imshow(cv2.resize(class_activation_map, 92 | original_size), cmap='jet', alpha=0.5) 93 | plt.show() 94 | -------------------------------------------------------------------------------- /examples/imdb_bidirectional_lstm.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #Trains a Bidirectional LSTM on the IMDB sentiment classification task. 3 | 4 | Output after 4 epochs on CPU: ~0.8146 5 | Time per epoch on CPU (Core i7): ~150s. 6 | ''' 7 | 8 | from __future__ import print_function 9 | import numpy as np 10 | 11 | from keras.preprocessing import sequence 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional 14 | from keras.datasets import imdb 15 | 16 | 17 | max_features = 20000 18 | # cut texts after this number of words 19 | # (among top max_features most common words) 20 | maxlen = 100 21 | batch_size = 32 22 | 23 | print('Loading data...') 24 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 25 | print(len(x_train), 'train sequences') 26 | print(len(x_test), 'test sequences') 27 | 28 | print('Pad sequences (samples x time)') 29 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 30 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 31 | print('x_train shape:', x_train.shape) 32 | print('x_test shape:', x_test.shape) 33 | y_train = np.array(y_train) 34 | y_test = np.array(y_test) 35 | 36 | model = Sequential() 37 | model.add(Embedding(max_features, 128, input_length=maxlen)) 38 | model.add(Bidirectional(LSTM(64))) 39 | model.add(Dropout(0.5)) 40 | model.add(Dense(1, activation='sigmoid')) 41 | 42 | # try using different optimizers and different optimizer configs 43 | model.compile('adam', 'binary_crossentropy', metrics=['accuracy']) 44 | 45 | print('Train...') 46 | model.fit(x_train, y_train, 47 | batch_size=batch_size, 48 | epochs=4, 49 | validation_data=[x_test, y_test]) 50 | -------------------------------------------------------------------------------- /examples/imdb_cnn.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #This example demonstrates the use of Convolution1D for text classification. 3 | 4 | Gets to 0.89 test accuracy after 2 epochs.
5 | 90s/epoch on Intel i5 2.4Ghz CPU.
6 | 10s/epoch on Tesla K40 GPU. 7 | ''' 8 | from __future__ import print_function 9 | 10 | from keras.preprocessing import sequence 11 | from keras.models import Sequential 12 | from keras.layers import Dense, Dropout, Activation 13 | from keras.layers import Embedding 14 | from keras.layers import Conv1D, GlobalMaxPooling1D 15 | from keras.datasets import imdb 16 | 17 | # set parameters: 18 | max_features = 5000 19 | maxlen = 400 20 | batch_size = 32 21 | embedding_dims = 50 22 | filters = 250 23 | kernel_size = 3 24 | hidden_dims = 250 25 | epochs = 2 26 | 27 | print('Loading data...') 28 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 29 | print(len(x_train), 'train sequences') 30 | print(len(x_test), 'test sequences') 31 | 32 | print('Pad sequences (samples x time)') 33 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 34 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 35 | print('x_train shape:', x_train.shape) 36 | print('x_test shape:', x_test.shape) 37 | 38 | print('Build model...') 39 | model = Sequential() 40 | 41 | # we start off with an efficient embedding layer which maps 42 | # our vocab indices into embedding_dims dimensions 43 | model.add(Embedding(max_features, 44 | embedding_dims, 45 | input_length=maxlen)) 46 | model.add(Dropout(0.2)) 47 | 48 | # we add a Convolution1D, which will learn filters 49 | # word group filters of size filter_length: 50 | model.add(Conv1D(filters, 51 | kernel_size, 52 | padding='valid', 53 | activation='relu', 54 | strides=1)) 55 | # we use max pooling: 56 | model.add(GlobalMaxPooling1D()) 57 | 58 | # We add a vanilla hidden layer: 59 | model.add(Dense(hidden_dims)) 60 | model.add(Dropout(0.2)) 61 | model.add(Activation('relu')) 62 | 63 | # We project onto a single unit output layer, and squash it with a sigmoid: 64 | model.add(Dense(1)) 65 | model.add(Activation('sigmoid')) 66 | 67 | model.compile(loss='binary_crossentropy', 68 | optimizer='adam', 69 | metrics=['accuracy']) 70 | model.fit(x_train, y_train, 71 | batch_size=batch_size, 72 | epochs=epochs, 73 | validation_data=(x_test, y_test)) 74 | -------------------------------------------------------------------------------- /examples/imdb_cnn_lstm.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #Train a recurrent convolutional network on the IMDB sentiment classification task. 3 | 4 | Gets to 0.8498 test accuracy after 2 epochs. 41 s/epoch on K520 GPU. 5 | ''' 6 | from __future__ import print_function 7 | 8 | from keras.preprocessing import sequence 9 | from keras.models import Sequential 10 | from keras.layers import Dense, Dropout, Activation 11 | from keras.layers import Embedding 12 | from keras.layers import LSTM 13 | from keras.layers import Conv1D, MaxPooling1D 14 | from keras.datasets import imdb 15 | 16 | # Embedding 17 | max_features = 20000 18 | maxlen = 100 19 | embedding_size = 128 20 | 21 | # Convolution 22 | kernel_size = 5 23 | filters = 64 24 | pool_size = 4 25 | 26 | # LSTM 27 | lstm_output_size = 70 28 | 29 | # Training 30 | batch_size = 30 31 | epochs = 2 32 | 33 | ''' 34 | Note: 35 | batch_size is highly sensitive. 36 | Only 2 epochs are needed as the dataset is very small. 37 | ''' 38 | 39 | print('Loading data...') 40 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 41 | print(len(x_train), 'train sequences') 42 | print(len(x_test), 'test sequences') 43 | 44 | print('Pad sequences (samples x time)') 45 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 46 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 47 | print('x_train shape:', x_train.shape) 48 | print('x_test shape:', x_test.shape) 49 | 50 | print('Build model...') 51 | 52 | model = Sequential() 53 | model.add(Embedding(max_features, embedding_size, input_length=maxlen)) 54 | model.add(Dropout(0.25)) 55 | model.add(Conv1D(filters, 56 | kernel_size, 57 | padding='valid', 58 | activation='relu', 59 | strides=1)) 60 | model.add(MaxPooling1D(pool_size=pool_size)) 61 | model.add(LSTM(lstm_output_size)) 62 | model.add(Dense(1)) 63 | model.add(Activation('sigmoid')) 64 | 65 | model.compile(loss='binary_crossentropy', 66 | optimizer='adam', 67 | metrics=['accuracy']) 68 | 69 | print('Train...') 70 | model.fit(x_train, y_train, 71 | batch_size=batch_size, 72 | epochs=epochs, 73 | validation_data=(x_test, y_test)) 74 | score, acc = model.evaluate(x_test, y_test, batch_size=batch_size) 75 | print('Test score:', score) 76 | print('Test accuracy:', acc) 77 | -------------------------------------------------------------------------------- /examples/imdb_lstm.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #Trains an LSTM model on the IMDB sentiment classification task. 3 | 4 | The dataset is actually too small for LSTM to be of any advantage 5 | compared to simpler, much faster methods such as TF-IDF + LogReg. 6 | 7 | **Notes** 8 | 9 | - RNNs are tricky. Choice of batch size is important, 10 | choice of loss and optimizer is critical, etc. 11 | Some configurations won't converge. 12 | 13 | - LSTM loss decrease patterns during training can be quite different 14 | from what you see with CNNs/MLPs/etc. 15 | 16 | ''' 17 | from __future__ import print_function 18 | 19 | from keras.preprocessing import sequence 20 | from keras.models import Sequential 21 | from keras.layers import Dense, Embedding 22 | from keras.layers import LSTM 23 | from keras.datasets import imdb 24 | 25 | max_features = 20000 26 | # cut texts after this number of words (among top max_features most common words) 27 | maxlen = 80 28 | batch_size = 32 29 | 30 | print('Loading data...') 31 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 32 | print(len(x_train), 'train sequences') 33 | print(len(x_test), 'test sequences') 34 | 35 | print('Pad sequences (samples x time)') 36 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 37 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 38 | print('x_train shape:', x_train.shape) 39 | print('x_test shape:', x_test.shape) 40 | 41 | print('Build model...') 42 | model = Sequential() 43 | model.add(Embedding(max_features, 128)) 44 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) 45 | model.add(Dense(1, activation='sigmoid')) 46 | 47 | # try using different optimizers and different optimizer configs 48 | model.compile(loss='binary_crossentropy', 49 | optimizer='adam', 50 | metrics=['accuracy']) 51 | 52 | print('Train...') 53 | model.fit(x_train, y_train, 54 | batch_size=batch_size, 55 | epochs=15, 56 | validation_data=(x_test, y_test)) 57 | score, acc = model.evaluate(x_test, y_test, 58 | batch_size=batch_size) 59 | print('Test score:', score) 60 | print('Test accuracy:', acc) 61 | -------------------------------------------------------------------------------- /examples/imdb_lstm_mask_scan.py: -------------------------------------------------------------------------------- 1 | '''Trains a LSTM on the IMDB sentiment classification task. 2 | The dataset is actually too small for LSTM to be of any advantage 3 | compared to simpler, much faster methods such as TF-IDF + LogReg. 4 | Notes: 5 | 6 | - RNNs are tricky. Choice of batch size is important, 7 | choice of loss and optimizer is critical, etc. 8 | Some configurations won't converge. 9 | 10 | - LSTM loss decrease patterns during training can be quite different 11 | from what you see with CNNs/MLPs/etc. 12 | ''' 13 | from __future__ import print_function 14 | import numpy as np 15 | import keras.backend as K 16 | 17 | np.random.seed(1337) # for reproducibility 18 | import logging 19 | import os 20 | 21 | from keras.layers import * 22 | from keras.models import model_from_json, Model 23 | from keras.optimizers import Adam, RMSprop, Nadam, Adadelta, SGD, Adagrad, Adamax 24 | from keras.regularizers import l2 25 | from keras_wrapper.cnn_model import Model_Wrapper 26 | from keras_wrapper.extra.regularize import Regularize 27 | import argparse 28 | import ast 29 | import copy 30 | import sys 31 | import time 32 | from timeit import default_timer as timer 33 | from keras.preprocessing import sequence 34 | from keras.utils import np_utils 35 | from keras.models import Model, Sequential 36 | from keras.layers import * 37 | from keras.datasets import imdb 38 | 39 | max_features = 20000 40 | maxlen = 20 # cut texts after this number of words 41 | batch_size = 32 42 | 43 | print('Loading data...') 44 | (X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features) 45 | print(len(X_train), 'train sequences') 46 | print(len(X_test), 'test sequences') 47 | 48 | print('Pad sequences (samples x time)') 49 | X_train = sequence.pad_sequences(X_train, maxlen=maxlen) 50 | X_test = sequence.pad_sequences(X_test, maxlen=maxlen) 51 | print('X_train shape:', X_train.shape) 52 | print('X_test shape:', X_test.shape) 53 | 54 | print('Build model...') 55 | 56 | input1 = Input(name='Input_1', shape=tuple([None]), dtype='int32') 57 | # If we don't mask the emb, the error doesn't appear 58 | # If we mask it, it appears 59 | emb = Embedding(max_features, 128, dropout=0.2, mask_zero=False)(input1) 60 | lstm = AttLSTMCond(128, dropout_W=0.2, dropout_U=0.2)([emb, emb]) 61 | masked_lstm = RemoveMask()(lstm) 62 | out_lstm = Dense(1, activation='sigmoid')(masked_lstm) 63 | model = Model(input=input1, output=out_lstm) 64 | # try using different optimizers and different optimizer configs 65 | model.compile(loss='binary_crossentropy', 66 | optimizer='adam') # , 67 | # metrics=['accuracy']) 68 | 69 | print('Train...') 70 | model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=15, 71 | validation_data=(X_test, y_test)) 72 | -------------------------------------------------------------------------------- /examples/lstm_text_generation.py: -------------------------------------------------------------------------------- 1 | ''' 2 | #Example script to generate text from Nietzsche's writings. 3 | 4 | At least 20 epochs are required before the generated text 5 | starts sounding coherent. 6 | 7 | It is recommended to run this script on GPU, as recurrent 8 | networks are quite computationally intensive. 9 | 10 | If you try this script on new data, make sure your corpus 11 | has at least ~100k characters. ~1M is better. 12 | ''' 13 | 14 | from __future__ import print_function 15 | from keras.callbacks import LambdaCallback 16 | from keras.models import Sequential 17 | from keras.layers import Dense 18 | from keras.layers import LSTM 19 | from keras.optimizers import RMSprop 20 | from keras.utils.data_utils import get_file 21 | import numpy as np 22 | import random 23 | import sys 24 | import io 25 | 26 | path = get_file( 27 | 'nietzsche.txt', 28 | origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt') 29 | with io.open(path, encoding='utf-8') as f: 30 | text = f.read().lower() 31 | print('corpus length:', len(text)) 32 | 33 | chars = sorted(list(set(text))) 34 | print('total chars:', len(chars)) 35 | char_indices = dict((c, i) for i, c in enumerate(chars)) 36 | indices_char = dict((i, c) for i, c in enumerate(chars)) 37 | 38 | # cut the text in semi-redundant sequences of maxlen characters 39 | maxlen = 40 40 | step = 3 41 | sentences = [] 42 | next_chars = [] 43 | for i in range(0, len(text) - maxlen, step): 44 | sentences.append(text[i: i + maxlen]) 45 | next_chars.append(text[i + maxlen]) 46 | print('nb sequences:', len(sentences)) 47 | 48 | print('Vectorization...') 49 | x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) 50 | y = np.zeros((len(sentences), len(chars)), dtype=np.bool) 51 | for i, sentence in enumerate(sentences): 52 | for t, char in enumerate(sentence): 53 | x[i, t, char_indices[char]] = 1 54 | y[i, char_indices[next_chars[i]]] = 1 55 | 56 | 57 | # build the model: a single LSTM 58 | print('Build model...') 59 | model = Sequential() 60 | model.add(LSTM(128, input_shape=(maxlen, len(chars)))) 61 | model.add(Dense(len(chars), activation='softmax')) 62 | 63 | optimizer = RMSprop(learning_rate=0.01) 64 | model.compile(loss='categorical_crossentropy', optimizer=optimizer) 65 | 66 | 67 | def sample(preds, temperature=1.0): 68 | # helper function to sample an index from a probability array 69 | preds = np.asarray(preds).astype('float64') 70 | preds = np.log(preds) / temperature 71 | exp_preds = np.exp(preds) 72 | preds = exp_preds / np.sum(exp_preds) 73 | probas = np.random.multinomial(1, preds, 1) 74 | return np.argmax(probas) 75 | 76 | 77 | def on_epoch_end(epoch, _): 78 | # Function invoked at end of each epoch. Prints generated text. 79 | print() 80 | print('----- Generating text after Epoch: %d' % epoch) 81 | 82 | start_index = random.randint(0, len(text) - maxlen - 1) 83 | for diversity in [0.2, 0.5, 1.0, 1.2]: 84 | print('----- diversity:', diversity) 85 | 86 | generated = '' 87 | sentence = text[start_index: start_index + maxlen] 88 | generated += sentence 89 | print('----- Generating with seed: "' + sentence + '"') 90 | sys.stdout.write(generated) 91 | 92 | for i in range(400): 93 | x_pred = np.zeros((1, maxlen, len(chars))) 94 | for t, char in enumerate(sentence): 95 | x_pred[0, t, char_indices[char]] = 1. 96 | 97 | preds = model.predict(x_pred, verbose=0)[0] 98 | next_index = sample(preds, diversity) 99 | next_char = indices_char[next_index] 100 | 101 | sentence = sentence[1:] + next_char 102 | 103 | sys.stdout.write(next_char) 104 | sys.stdout.flush() 105 | print() 106 | 107 | print_callback = LambdaCallback(on_epoch_end=on_epoch_end) 108 | 109 | model.fit(x, y, 110 | batch_size=128, 111 | epochs=60, 112 | callbacks=[print_callback]) 113 | -------------------------------------------------------------------------------- /examples/mnist_cnn.py: -------------------------------------------------------------------------------- 1 | '''Trains a simple convnet on the MNIST dataset. 2 | 3 | Gets to 99.25% test accuracy after 12 epochs 4 | (there is still a lot of margin for parameter tuning). 5 | 16 seconds per epoch on a GRID K520 GPU. 6 | ''' 7 | 8 | from __future__ import print_function 9 | import keras 10 | from keras.datasets import mnist 11 | from keras.models import Sequential 12 | from keras.layers import Dense, Dropout, Flatten 13 | from keras.layers import Conv2D, MaxPooling2D 14 | from keras import backend as K 15 | 16 | batch_size = 128 17 | num_classes = 10 18 | epochs = 12 19 | 20 | # input image dimensions 21 | img_rows, img_cols = 28, 28 22 | 23 | # the data, split between train and test sets 24 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 25 | 26 | if K.image_data_format() == 'channels_first': 27 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 28 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 29 | input_shape = (1, img_rows, img_cols) 30 | else: 31 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 32 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 33 | input_shape = (img_rows, img_cols, 1) 34 | 35 | x_train = x_train.astype('float32') 36 | x_test = x_test.astype('float32') 37 | x_train /= 255 38 | x_test /= 255 39 | print('x_train shape:', x_train.shape) 40 | print(x_train.shape[0], 'train samples') 41 | print(x_test.shape[0], 'test samples') 42 | 43 | # convert class vectors to binary class matrices 44 | y_train = keras.utils.to_categorical(y_train, num_classes) 45 | y_test = keras.utils.to_categorical(y_test, num_classes) 46 | 47 | model = Sequential() 48 | model.add(Conv2D(32, kernel_size=(3, 3), 49 | activation='relu', 50 | input_shape=input_shape)) 51 | model.add(Conv2D(64, (3, 3), activation='relu')) 52 | model.add(MaxPooling2D(pool_size=(2, 2))) 53 | model.add(Dropout(0.25)) 54 | model.add(Flatten()) 55 | model.add(Dense(128, activation='relu')) 56 | model.add(Dropout(0.5)) 57 | model.add(Dense(num_classes, activation='softmax')) 58 | 59 | model.compile(loss=keras.losses.categorical_crossentropy, 60 | optimizer=keras.optimizers.Adadelta(), 61 | metrics=['accuracy']) 62 | 63 | model.fit(x_train, y_train, 64 | batch_size=batch_size, 65 | epochs=epochs, 66 | verbose=1, 67 | validation_data=(x_test, y_test)) 68 | score = model.evaluate(x_test, y_test, verbose=0) 69 | print('Test loss:', score[0]) 70 | print('Test accuracy:', score[1]) 71 | -------------------------------------------------------------------------------- /examples/mnist_hierarchical_rnn.py: -------------------------------------------------------------------------------- 1 | """Example of using Hierarchical RNN (HRNN) to classify MNIST digits. 2 | 3 | HRNNs can learn across multiple levels 4 | of temporal hierarchy over a complex sequence. 5 | Usually, the first recurrent layer of an HRNN 6 | encodes a sentence (e.g. of word vectors) 7 | into a sentence vector. 8 | The second recurrent layer then encodes a sequence of 9 | such vectors (encoded by the first layer) into a document vector. 10 | This document vector is considered to preserve both 11 | the word-level and sentence-level structure of the context. 12 | 13 | # References 14 | 15 | - [A Hierarchical Neural Autoencoder for Paragraphs and Documents] 16 | (https://arxiv.org/abs/1506.01057) 17 | Encodes paragraphs and documents with HRNN. 18 | Results have shown that HRNN outperforms standard 19 | RNNs and may play some role in more sophisticated generation tasks like 20 | summarization or question answering. 21 | - [Hierarchical recurrent neural network for skeleton based action recognition] 22 | (http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7298714) 23 | Achieved state-of-the-art results on 24 | skeleton based action recognition with 3 levels 25 | of bidirectional HRNN combined with fully connected layers. 26 | 27 | In the below MNIST example the first LSTM layer first encodes every 28 | column of pixels of shape (28, 1) to a column vector of shape (128,). 29 | The second LSTM layer encodes then these 28 column vectors of shape (28, 128) 30 | to a image vector representing the whole image. 31 | A final Dense layer is added for prediction. 32 | 33 | After 5 epochs: train acc: 0.9858, val acc: 0.9864 34 | """ 35 | from __future__ import print_function 36 | 37 | import keras 38 | from keras.datasets import mnist 39 | from keras.models import Model 40 | from keras.layers import Input, Dense, TimeDistributed 41 | from keras.layers import LSTM 42 | 43 | # Training parameters. 44 | batch_size = 32 45 | num_classes = 10 46 | epochs = 5 47 | 48 | # Embedding dimensions. 49 | row_hidden = 128 50 | col_hidden = 128 51 | 52 | # The data, split between train and test sets. 53 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 54 | 55 | # Reshapes data to 4D for Hierarchical RNN. 56 | x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) 57 | x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) 58 | x_train = x_train.astype('float32') 59 | x_test = x_test.astype('float32') 60 | x_train /= 255 61 | x_test /= 255 62 | print('x_train shape:', x_train.shape) 63 | print(x_train.shape[0], 'train samples') 64 | print(x_test.shape[0], 'test samples') 65 | 66 | # Converts class vectors to binary class matrices. 67 | y_train = keras.utils.to_categorical(y_train, num_classes) 68 | y_test = keras.utils.to_categorical(y_test, num_classes) 69 | 70 | row, col, pixel = x_train.shape[1:] 71 | 72 | # 4D input. 73 | x = Input(shape=(row, col, pixel)) 74 | 75 | # Encodes a row of pixels using TimeDistributed Wrapper. 76 | encoded_rows = TimeDistributed(LSTM(row_hidden))(x) 77 | 78 | # Encodes columns of encoded rows. 79 | encoded_columns = LSTM(col_hidden)(encoded_rows) 80 | 81 | # Final predictions and model. 82 | prediction = Dense(num_classes, activation='softmax')(encoded_columns) 83 | model = Model(x, prediction) 84 | model.compile(loss='categorical_crossentropy', 85 | optimizer='rmsprop', 86 | metrics=['accuracy']) 87 | 88 | # Training. 89 | model.fit(x_train, y_train, 90 | batch_size=batch_size, 91 | epochs=epochs, 92 | verbose=1, 93 | validation_data=(x_test, y_test)) 94 | 95 | # Evaluation. 96 | scores = model.evaluate(x_test, y_test, verbose=0) 97 | print('Test loss:', scores[0]) 98 | print('Test accuracy:', scores[1]) 99 | -------------------------------------------------------------------------------- /examples/mnist_irnn.py: -------------------------------------------------------------------------------- 1 | '''This is a reproduction of the IRNN experiment 2 | with pixel-by-pixel sequential MNIST in 3 | "A Simple Way to Initialize Recurrent Networks of Rectified Linear Units" 4 | by Quoc V. Le, Navdeep Jaitly, Geoffrey E. Hinton 5 | 6 | arxiv:1504.00941v2 [cs.NE] 7 Apr 2015 7 | http://arxiv.org/pdf/1504.00941v2.pdf 8 | 9 | Optimizer is replaced with RMSprop which yields more stable and steady 10 | improvement. 11 | 12 | Reaches 0.93 train/test accuracy after 900 epochs 13 | (which roughly corresponds to 1687500 steps in the original paper.) 14 | ''' 15 | 16 | from __future__ import print_function 17 | 18 | import keras 19 | from keras.datasets import mnist 20 | from keras.models import Sequential 21 | from keras.layers import Dense, Activation 22 | from keras.layers import SimpleRNN 23 | from keras import initializers 24 | from keras.optimizers import RMSprop 25 | 26 | batch_size = 32 27 | num_classes = 10 28 | epochs = 200 29 | hidden_units = 100 30 | 31 | learning_rate = 1e-6 32 | clip_norm = 1.0 33 | 34 | # the data, split between train and test sets 35 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 36 | 37 | x_train = x_train.reshape(x_train.shape[0], -1, 1) 38 | x_test = x_test.reshape(x_test.shape[0], -1, 1) 39 | x_train = x_train.astype('float32') 40 | x_test = x_test.astype('float32') 41 | x_train /= 255 42 | x_test /= 255 43 | print('x_train shape:', x_train.shape) 44 | print(x_train.shape[0], 'train samples') 45 | print(x_test.shape[0], 'test samples') 46 | 47 | # convert class vectors to binary class matrices 48 | y_train = keras.utils.to_categorical(y_train, num_classes) 49 | y_test = keras.utils.to_categorical(y_test, num_classes) 50 | 51 | print('Evaluate IRNN...') 52 | model = Sequential() 53 | model.add(SimpleRNN(hidden_units, 54 | kernel_initializer=initializers.RandomNormal(stddev=0.001), 55 | recurrent_initializer=initializers.Identity(gain=1.0), 56 | activation='relu', 57 | input_shape=x_train.shape[1:])) 58 | model.add(Dense(num_classes)) 59 | model.add(Activation('softmax')) 60 | rmsprop = RMSprop(learning_rate=learning_rate) 61 | model.compile(loss='categorical_crossentropy', 62 | optimizer=rmsprop, 63 | metrics=['accuracy']) 64 | 65 | model.fit(x_train, y_train, 66 | batch_size=batch_size, 67 | epochs=epochs, 68 | verbose=1, 69 | validation_data=(x_test, y_test)) 70 | 71 | scores = model.evaluate(x_test, y_test, verbose=0) 72 | print('IRNN test score:', scores[0]) 73 | print('IRNN test accuracy:', scores[1]) 74 | -------------------------------------------------------------------------------- /examples/mnist_mlp.py: -------------------------------------------------------------------------------- 1 | '''Trains a simple deep NN on the MNIST dataset. 2 | 3 | Gets to 98.40% test accuracy after 20 epochs 4 | (there is *a lot* of margin for parameter tuning). 5 | 2 seconds per epoch on a K520 GPU. 6 | ''' 7 | 8 | from __future__ import print_function 9 | 10 | import keras 11 | from keras.datasets import mnist 12 | from keras.models import Sequential 13 | from keras.layers import Dense, Dropout 14 | from keras.optimizers import RMSprop 15 | 16 | batch_size = 128 17 | num_classes = 10 18 | epochs = 20 19 | 20 | # the data, split between train and test sets 21 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 22 | 23 | x_train = x_train.reshape(60000, 784) 24 | x_test = x_test.reshape(10000, 784) 25 | x_train = x_train.astype('float32') 26 | x_test = x_test.astype('float32') 27 | x_train /= 255 28 | x_test /= 255 29 | print(x_train.shape[0], 'train samples') 30 | print(x_test.shape[0], 'test samples') 31 | 32 | # convert class vectors to binary class matrices 33 | y_train = keras.utils.to_categorical(y_train, num_classes) 34 | y_test = keras.utils.to_categorical(y_test, num_classes) 35 | 36 | model = Sequential() 37 | model.add(Dense(512, activation='relu', input_shape=(784,))) 38 | model.add(Dropout(0.2)) 39 | model.add(Dense(512, activation='relu')) 40 | model.add(Dropout(0.2)) 41 | model.add(Dense(num_classes, activation='softmax')) 42 | 43 | model.summary() 44 | 45 | model.compile(loss='categorical_crossentropy', 46 | optimizer=RMSprop(), 47 | metrics=['accuracy']) 48 | 49 | history = model.fit(x_train, y_train, 50 | batch_size=batch_size, 51 | epochs=epochs, 52 | verbose=1, 53 | validation_data=(x_test, y_test)) 54 | score = model.evaluate(x_test, y_test, verbose=0) 55 | print('Test loss:', score[0]) 56 | print('Test accuracy:', score[1]) 57 | -------------------------------------------------------------------------------- /examples/mnist_sklearn_wrapper.py: -------------------------------------------------------------------------------- 1 | '''Example of how to use sklearn wrapper 2 | 3 | Builds simple CNN models on MNIST and uses sklearn's GridSearchCV to find best model 4 | ''' 5 | 6 | from __future__ import print_function 7 | 8 | import keras 9 | from keras.datasets import mnist 10 | from keras.models import Sequential 11 | from keras.layers import Dense, Dropout, Activation, Flatten 12 | from keras.layers import Conv2D, MaxPooling2D 13 | from keras.wrappers.scikit_learn import KerasClassifier 14 | from keras import backend as K 15 | from sklearn.model_selection import GridSearchCV 16 | 17 | 18 | num_classes = 10 19 | 20 | # input image dimensions 21 | img_rows, img_cols = 28, 28 22 | 23 | # load training data and do basic data normalization 24 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 25 | 26 | if K.image_data_format() == 'channels_first': 27 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 28 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 29 | input_shape = (1, img_rows, img_cols) 30 | else: 31 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 32 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 33 | input_shape = (img_rows, img_cols, 1) 34 | 35 | x_train = x_train.astype('float32') 36 | x_test = x_test.astype('float32') 37 | x_train /= 255 38 | x_test /= 255 39 | 40 | # convert class vectors to binary class matrices 41 | y_train = keras.utils.to_categorical(y_train, num_classes) 42 | y_test = keras.utils.to_categorical(y_test, num_classes) 43 | 44 | 45 | def make_model(dense_layer_sizes, filters, kernel_size, pool_size): 46 | '''Creates model comprised of 2 convolutional layers followed by dense layers 47 | 48 | dense_layer_sizes: List of layer sizes. 49 | This list has one number for each layer 50 | filters: Number of convolutional filters in each convolutional layer 51 | kernel_size: Convolutional kernel size 52 | pool_size: Size of pooling area for max pooling 53 | ''' 54 | 55 | model = Sequential() 56 | model.add(Conv2D(filters, kernel_size, 57 | padding='valid', 58 | input_shape=input_shape)) 59 | model.add(Activation('relu')) 60 | model.add(Conv2D(filters, kernel_size)) 61 | model.add(Activation('relu')) 62 | model.add(MaxPooling2D(pool_size=pool_size)) 63 | model.add(Dropout(0.25)) 64 | 65 | model.add(Flatten()) 66 | for layer_size in dense_layer_sizes: 67 | model.add(Dense(layer_size)) 68 | model.add(Activation('relu')) 69 | model.add(Dropout(0.5)) 70 | model.add(Dense(num_classes)) 71 | model.add(Activation('softmax')) 72 | 73 | model.compile(loss='categorical_crossentropy', 74 | optimizer='adadelta', 75 | metrics=['accuracy']) 76 | return model 77 | 78 | 79 | dense_size_candidates = [[32], [64], [32, 32], [64, 64]] 80 | my_classifier = KerasClassifier(make_model, batch_size=32) 81 | validator = GridSearchCV(my_classifier, 82 | param_grid={'dense_layer_sizes': dense_size_candidates, 83 | # epochs is avail for tuning even when not 84 | # an argument to model building function 85 | 'epochs': [3, 6], 86 | 'filters': [8], 87 | 'kernel_size': [3], 88 | 'pool_size': [2]}, 89 | scoring='neg_log_loss', 90 | n_jobs=1) 91 | validator.fit(x_train, y_train) 92 | 93 | print('The parameters of the best model are: ') 94 | print(validator.best_params_) 95 | 96 | # validator.best_estimator_ returns sklearn-wrapped version of best model. 97 | # validator.best_estimator_.model returns the (unwrapped) keras model 98 | best_model = validator.best_estimator_.model 99 | metric_names = best_model.metrics_names 100 | metric_values = best_model.evaluate(x_test, y_test) 101 | for metric, value in zip(metric_names, metric_values): 102 | print(metric, ': ', value) 103 | -------------------------------------------------------------------------------- /examples/reuters_mlp.py: -------------------------------------------------------------------------------- 1 | '''Trains and evaluate a simple MLP 2 | on the Reuters newswire topic classification task. 3 | ''' 4 | from __future__ import print_function 5 | 6 | import numpy as np 7 | import keras 8 | from keras.datasets import reuters 9 | from keras.models import Sequential 10 | from keras.layers import Dense, Dropout, Activation 11 | from keras.preprocessing.text import Tokenizer 12 | 13 | max_words = 1000 14 | batch_size = 32 15 | epochs = 5 16 | 17 | print('Loading data...') 18 | (x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words, 19 | test_split=0.2) 20 | print(len(x_train), 'train sequences') 21 | print(len(x_test), 'test sequences') 22 | 23 | num_classes = np.max(y_train) + 1 24 | print(num_classes, 'classes') 25 | 26 | print('Vectorizing sequence data...') 27 | tokenizer = Tokenizer(num_words=max_words) 28 | x_train = tokenizer.sequences_to_matrix(x_train, mode='binary') 29 | x_test = tokenizer.sequences_to_matrix(x_test, mode='binary') 30 | print('x_train shape:', x_train.shape) 31 | print('x_test shape:', x_test.shape) 32 | 33 | print('Convert class vector to binary class matrix ' 34 | '(for use with categorical_crossentropy)') 35 | y_train = keras.utils.to_categorical(y_train, num_classes) 36 | y_test = keras.utils.to_categorical(y_test, num_classes) 37 | print('y_train shape:', y_train.shape) 38 | print('y_test shape:', y_test.shape) 39 | 40 | print('Building model...') 41 | model = Sequential() 42 | model.add(Dense(512, input_shape=(max_words,))) 43 | model.add(Activation('relu')) 44 | model.add(Dropout(0.5)) 45 | model.add(Dense(num_classes)) 46 | model.add(Activation('softmax')) 47 | 48 | model.compile(loss='categorical_crossentropy', 49 | optimizer='adam', 50 | metrics=['accuracy']) 51 | 52 | history = model.fit(x_train, y_train, 53 | batch_size=batch_size, 54 | epochs=epochs, 55 | verbose=1, 56 | validation_split=0.1) 57 | score = model.evaluate(x_test, y_test, 58 | batch_size=batch_size, verbose=1) 59 | print('Test score:', score[0]) 60 | print('Test accuracy:', score[1]) 61 | -------------------------------------------------------------------------------- /keras/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import utils 4 | from . import activations 5 | from . import applications 6 | from . import backend 7 | from . import datasets 8 | from . import engine 9 | from . import layers 10 | from . import preprocessing 11 | from . import wrappers 12 | from . import callbacks 13 | from . import constraints 14 | from . import initializers 15 | from . import metrics 16 | from . import models 17 | from . import losses 18 | from . import optimizers 19 | from . import regularizers 20 | 21 | # Also importable from root 22 | from .layers import Input 23 | from .models import Model 24 | from .models import Sequential 25 | 26 | __version__ = '2.3.1' 27 | -------------------------------------------------------------------------------- /keras/applications/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from .. import backend 6 | from .. import layers 7 | from .. import models 8 | from .. import utils 9 | 10 | import keras_applications 11 | 12 | 13 | def keras_modules_injection(base_fun): 14 | 15 | def wrapper(*args, **kwargs): 16 | kwargs['backend'] = backend 17 | kwargs['layers'] = layers 18 | kwargs['models'] = models 19 | kwargs['utils'] = utils 20 | return base_fun(*args, **kwargs) 21 | 22 | return wrapper 23 | 24 | 25 | from .vgg16 import VGG16 26 | from .vgg19 import VGG19 27 | from .resnet50 import ResNet50 28 | from .inception_v3 import InceptionV3 29 | from .inception_resnet_v2 import InceptionResNetV2 30 | from .xception import Xception 31 | from .mobilenet import MobileNet 32 | from .mobilenet_v2 import MobileNetV2 33 | from .densenet import DenseNet121, DenseNet169, DenseNet201 34 | from .nasnet import NASNetMobile, NASNetLarge 35 | from .resnet import ResNet101, ResNet152 36 | from .resnet_v2 import ResNet50V2, ResNet101V2, ResNet152V2 37 | -------------------------------------------------------------------------------- /keras/applications/densenet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import densenet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def DenseNet121(*args, **kwargs): 11 | return densenet.DenseNet121(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def DenseNet169(*args, **kwargs): 16 | return densenet.DenseNet169(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def DenseNet201(*args, **kwargs): 21 | return densenet.DenseNet201(*args, **kwargs) 22 | 23 | 24 | @keras_modules_injection 25 | def decode_predictions(*args, **kwargs): 26 | return densenet.decode_predictions(*args, **kwargs) 27 | 28 | 29 | @keras_modules_injection 30 | def preprocess_input(*args, **kwargs): 31 | return densenet.preprocess_input(*args, **kwargs) 32 | -------------------------------------------------------------------------------- /keras/applications/imagenet_utils.py: -------------------------------------------------------------------------------- 1 | """Utilities for ImageNet data preprocessing & prediction decoding. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_applications import imagenet_utils 8 | from . import keras_modules_injection 9 | 10 | 11 | @keras_modules_injection 12 | def decode_predictions(*args, **kwargs): 13 | return imagenet_utils.decode_predictions( 14 | *args, **kwargs) 15 | 16 | 17 | @keras_modules_injection 18 | def preprocess_input(*args, **kwargs): 19 | return imagenet_utils.preprocess_input(*args, **kwargs) 20 | -------------------------------------------------------------------------------- /keras/applications/inception_resnet_v2.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import inception_resnet_v2 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def InceptionResNetV2(*args, **kwargs): 11 | return inception_resnet_v2.InceptionResNetV2(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return inception_resnet_v2.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return inception_resnet_v2.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/inception_v3.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import inception_v3 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def InceptionV3(*args, **kwargs): 11 | return inception_v3.InceptionV3(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return inception_v3.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return inception_v3.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/mobilenet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import mobilenet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def MobileNet(*args, **kwargs): 11 | return mobilenet.MobileNet(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return mobilenet.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return mobilenet.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/mobilenet_v2.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import mobilenet_v2 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def MobileNetV2(*args, **kwargs): 11 | return mobilenet_v2.MobileNetV2(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return mobilenet_v2.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return mobilenet_v2.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/nasnet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import nasnet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def NASNetMobile(*args, **kwargs): 11 | return nasnet.NASNetMobile(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def NASNetLarge(*args, **kwargs): 16 | return nasnet.NASNetLarge(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def decode_predictions(*args, **kwargs): 21 | return nasnet.decode_predictions(*args, **kwargs) 22 | 23 | 24 | @keras_modules_injection 25 | def preprocess_input(*args, **kwargs): 26 | return nasnet.preprocess_input(*args, **kwargs) 27 | -------------------------------------------------------------------------------- /keras/applications/resnet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | try: 6 | from keras_applications import resnet 7 | except: 8 | resnet = None 9 | from . import keras_modules_injection 10 | 11 | 12 | @keras_modules_injection 13 | def ResNet50(*args, **kwargs): 14 | return resnet.ResNet50(*args, **kwargs) 15 | 16 | 17 | @keras_modules_injection 18 | def ResNet101(*args, **kwargs): 19 | return resnet.ResNet101(*args, **kwargs) 20 | 21 | 22 | @keras_modules_injection 23 | def ResNet152(*args, **kwargs): 24 | return resnet.ResNet152(*args, **kwargs) 25 | 26 | 27 | @keras_modules_injection 28 | def decode_predictions(*args, **kwargs): 29 | return resnet.decode_predictions(*args, **kwargs) 30 | 31 | 32 | @keras_modules_injection 33 | def preprocess_input(*args, **kwargs): 34 | return resnet.preprocess_input(*args, **kwargs) 35 | -------------------------------------------------------------------------------- /keras/applications/resnet50.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import resnet50 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def ResNet50(*args, **kwargs): 11 | return resnet50.ResNet50(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return resnet50.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return resnet50.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/resnet_v2.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | try: 6 | from keras_applications import resnet_v2 7 | except: 8 | resnet_v2 = None 9 | from . import keras_modules_injection 10 | 11 | 12 | @keras_modules_injection 13 | def ResNet50V2(*args, **kwargs): 14 | return resnet_v2.ResNet50V2(*args, **kwargs) 15 | 16 | 17 | @keras_modules_injection 18 | def ResNet101V2(*args, **kwargs): 19 | return resnet_v2.ResNet101V2(*args, **kwargs) 20 | 21 | 22 | @keras_modules_injection 23 | def ResNet152V2(*args, **kwargs): 24 | return resnet_v2.ResNet152V2(*args, **kwargs) 25 | 26 | 27 | @keras_modules_injection 28 | def decode_predictions(*args, **kwargs): 29 | return resnet_v2.decode_predictions(*args, **kwargs) 30 | 31 | 32 | @keras_modules_injection 33 | def preprocess_input(*args, **kwargs): 34 | return resnet_v2.preprocess_input(*args, **kwargs) 35 | -------------------------------------------------------------------------------- /keras/applications/vgg16.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import vgg16 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def VGG16(*args, **kwargs): 11 | return vgg16.VGG16(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return vgg16.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return vgg16.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/vgg19.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import vgg19 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def VGG19(*args, **kwargs): 11 | return vgg19.VGG19(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return vgg19.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return vgg19.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/xception.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import xception 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def Xception(*args, **kwargs): 11 | return xception.Xception(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return xception.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return xception.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/caffe/README.md: -------------------------------------------------------------------------------- 1 | ## Caffe to Keras converter 2 | 3 | This is intended to serve as a conversion module for Caffe models to Keras Functional API models. 4 | 5 | **Please, be aware that this feature is not regularly maintained.** Thus, some layers or parameter definitions introduced in newer versions of either Keras or Caffe might not be compatible with the converter. 6 | 7 | **For this reason, any pull requests with updated versions of the caffe2keras converter are highly welcome!** 8 | 9 | 10 | ### Conversion 11 | 12 | In order to convert a model you just need the .caffemodel weights and the .prototxt deploy or train file. In any case you will need to include the input image dimensions as a header to the .prototxt network structure as if it was a deploy model (see an example [here](models)) and also include an initial data layer: 13 | 14 | ``` 15 | layer { 16 | name: "data" 17 | type: "Data" 18 | top: "data" 19 | top: "label" 20 | } 21 | ``` 22 | 23 | Given the differences between Caffe and Keras when applying the MAX pooling opperation, in some occasions the MAX pooling layers must include a `pad: 1` value even if they did not include them in their original .prototxt. 24 | 25 | The file caffe2keras.py can be used as a command line interface for converting any model the following way: 26 | 27 | ``` 28 | python caffe2keras.py -load_path 'models/' -prototxt 'train_val_for_keras.prototxt' -caffemodel 'bvlc_googlenet.caffemodel' 29 | ``` 30 | 31 | ### Model usage 32 | 33 | In the file [test_converted.py](test_converted.py) you can see an example on how to use a converted model. 34 | 35 | 36 | ### Acknowledgments 37 | 38 | This code is a modified and improved version by Marc Bolaños of the original pieces of code originally written by [Pranav Shyam](https://github.com/pranv) and [Antonella Cascitelli](https://github.com/lenlen). 39 | 40 | Contact email: marc.bolanos@ub.edu 41 | GitHub page: https://github.com/MarcBS 42 | -------------------------------------------------------------------------------- /keras/caffe/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/__init__.py -------------------------------------------------------------------------------- /keras/caffe/caffe2keras.py: -------------------------------------------------------------------------------- 1 | import keras.caffe.convert as convert 2 | import pprint 3 | import argparse 4 | 5 | """ 6 | USAGE EXAMPLE 7 | python caffe2keras.py -load_path 'models/' -prototxt 'train_val_for_keras.prototxt' 8 | -caffemodel 'bvlc_googlenet.caffemodel' 9 | """ 10 | 11 | parser = argparse.ArgumentParser(description='Converts a Caffe model to Keras.') 12 | parser.add_argument('-load_path', type=str, 13 | help='path where both the .prototxt and the .caffemodel files are stored') 14 | parser.add_argument('-prototxt', type=str, 15 | help='name of the .prototxt file') 16 | parser.add_argument('-caffemodel', type=str, 17 | help='name of the .caffemodel file') 18 | parser.add_argument('-store_path', type=str, default='', 19 | help='path to the folder where the Keras model will be stored (default: -load_path).') 20 | parser.add_argument('-debug', action='store_true', default=0, 21 | help='use debug mode') 22 | 23 | 24 | def main(args): 25 | """Main function. 26 | Calls the converter 27 | """ 28 | if not args.store_path: 29 | store_path = args.load_path 30 | else: 31 | store_path = args.store_path 32 | 33 | print("Converting model...") 34 | model = convert.caffe_to_keras(args.load_path + '/' + args.prototxt, args.load_path + '/' + args.caffemodel, 35 | debug=args.debug) 36 | print("Finished converting model.") 37 | 38 | # Save converted model structure 39 | print("Storing model...") 40 | json_string = model.to_json() 41 | open(store_path + '/Keras_model_structure.json', 'w').write(json_string) 42 | # # Save converted model weights 43 | model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True) 44 | print("Finished storing the converted model to " + store_path) 45 | 46 | 47 | if __name__ == "__main__": 48 | arguments = parser.parse_args() 49 | main(arguments) 50 | -------------------------------------------------------------------------------- /keras/caffe/extra_layers.py: -------------------------------------------------------------------------------- 1 | from keras.layers.core import Layer 2 | import keras.backend as K 3 | 4 | 5 | class LRN2D(Layer): 6 | """ 7 | This code is adapted from pylearn2. 8 | License at: https://github.com/lisa-lab/pylearn2/blob/master/LICENSE.txt 9 | """ 10 | 11 | def __init__(self, alpha=1e-4, k=2, beta=0.75, n=5, **kwargs): 12 | super(LRN2D, self).__init__(**kwargs) 13 | self.alpha = alpha 14 | self.k = k 15 | self.beta = beta 16 | self.n = n 17 | if n % 2 == 0: 18 | raise NotImplementedError("Only works with odd n") 19 | 20 | def call(self, x, mask=None): 21 | import theano.tensor as T 22 | X = x 23 | input_dim = X.shape 24 | half_n = self.n // 2 25 | input_sqr = K.sqr(X) 26 | b, ch, r, c = input_dim 27 | extra_channels = T.alloc(0., b, ch + 2 * half_n, r, c) 28 | input_sqr = K.set_subtensor(extra_channels[:, half_n:half_n + ch, :, :], input_sqr) 29 | scale = self.k 30 | norm_alpha = self.alpha / self.n 31 | for i in range(self.n): 32 | scale += norm_alpha * input_sqr[:, i:i + ch, :, :] 33 | scale = scale ** self.beta 34 | return X / scale 35 | 36 | def get_output(self, train): 37 | import theano.tensor as T 38 | X = self.get_input(train) 39 | input_dim = X.shape 40 | half_n = self.n // 2 41 | input_sqr = K.sqr(X) 42 | b, ch, r, c = input_dim 43 | extra_channels = T.alloc(0., b, ch + 2 * half_n, r, c) 44 | input_sqr = K.set_subtensor(extra_channels[:, half_n:half_n + ch, :, :], input_sqr) 45 | scale = self.k 46 | norm_alpha = self.alpha / self.n 47 | for i in range(self.n): 48 | scale += norm_alpha * input_sqr[:, i:i + ch, :, :] 49 | scale = scale ** self.beta 50 | return X / scale 51 | 52 | def get_config(self): 53 | return { 54 | "alpha": self.alpha, 55 | "k": self.k, 56 | "beta": self.beta, 57 | "n": self.n} 58 | -------------------------------------------------------------------------------- /keras/caffe/models/Keras_model_weights.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/Keras_model_weights.h5 -------------------------------------------------------------------------------- /keras/caffe/models/bvlc_googlenet.caffemodel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/bvlc_googlenet.caffemodel -------------------------------------------------------------------------------- /keras/caffe/models/cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/cat.jpg -------------------------------------------------------------------------------- /keras/caffe/models/cat_gray.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/cat_gray.jpg -------------------------------------------------------------------------------- /keras/caffe/models/fish-bike.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/fish-bike.jpg -------------------------------------------------------------------------------- /keras/caffe/models/segmentation/caffemodel-url.txt: -------------------------------------------------------------------------------- 1 | NOTE: Please run this command to get the fcn-8s .caffemodel file before running test_segmentation.py 2 | PS. Its a big file (~500MB) 3 | 4 | wget http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel -------------------------------------------------------------------------------- /keras/caffe/models/segmentation/horse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/segmentation/horse.png -------------------------------------------------------------------------------- /keras/caffe/models/segmentation/mini_proof.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/caffe/models/segmentation/mini_proof.png -------------------------------------------------------------------------------- /keras/caffe/models/segmentation/test_segmentation.py: -------------------------------------------------------------------------------- 1 | """ 2 | author: Akshay Chawla (https://github.com/akshaychawla) 3 | TEST:rs 4 | Test convert.py's ability to handle Deconvolution and Crop laye 5 | by converting voc-fcn8s .prototxt and .caffemodel present in the caffe/models/segmentation folder 6 | """ 7 | # import os 8 | # import inspect 9 | # import numpy as np 10 | # import keras.caffe.convert as convert 11 | # from scipy import misc 12 | # import matplotlib.pyplot as plt 13 | # from subprocess import call 14 | 15 | # check whether files are present in folder 16 | """ 17 | path = os.path.dirname(inspect.getfile(inspect.currentframe())) 18 | assert os.path.exists(path + "/deploy.prototxt"), "Err. Couldn't find the debug.prototxt file" 19 | assert os.path.exists(path + "/horse.png"), "Err. Couldn't find the horse.png image file" 20 | if not os.path.exists(path + "/fcn8s-heavy-pascal.caffemodel"): 21 | call(["wget http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel -O " 22 | + "./" + path + "/fcn8s-heavy-pascal.caffemodel"], 23 | shell=True) 24 | assert os.path.exists(path + "/fcn8s-heavy-pascal.caffemodel"), "Err. Cannot find .caffemodel file. \ 25 | please download file using command : wget http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel " 26 | 27 | model = convert.caffe_to_keras(path + "/deploy.prototxt", path + "/fcn8s-heavy-pascal.caffemodel", debug=1) 28 | 29 | print ("Yay!") 30 | 31 | # 1. load image 32 | img = misc.imread(path + "/horse.png") 33 | 34 | # modify it 35 | img = np.rollaxis(img, 2) 36 | img = np.expand_dims(img, 0) 37 | 38 | # 2. run forward pass 39 | op = model.predict(img) 40 | 41 | # 3. reshape output 42 | op = op[0] 43 | op = op.reshape((500, 500, 21)) 44 | op_arg = np.argmax(op, axis=2) 45 | 46 | # 4. plot output 47 | plt.imshow(op_arg) 48 | plt.show() 49 | 50 | print ("..done") 51 | """ 52 | -------------------------------------------------------------------------------- /keras/caffe/test_converted.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential, model_from_json 2 | from keras.optimizers import SGD 3 | 4 | from scipy import misc 5 | import numpy as np 6 | import copy 7 | 8 | if __name__ == "__main__": 9 | 10 | out_layer_names = ["loss1/loss", "loss2/loss", "loss3/loss3"] 11 | 12 | print("Preparing test image.") 13 | # Read image 14 | im = misc.imread('models/cat.jpg') 15 | 16 | # Resize 17 | im = misc.imresize(im, (224, 224)).astype(np.float32) 18 | 19 | # Change RGB to BGR 20 | aux = copy.copy(im) 21 | im[:, :, 0] = aux[:, :, 2] 22 | im[:, :, 2] = aux[:, :, 0] 23 | 24 | # Remove train image mean 25 | im[:, :, 0] -= 104.006 26 | im[:, :, 1] -= 116.669 27 | im[:, :, 2] -= 122.679 28 | 29 | # Transpose image dimensions (Keras' uses the channels as the 1st dimension) 30 | im = np.transpose(im, (2, 0, 1)) 31 | 32 | # Insert a new dimension for the batch_size 33 | im = np.expand_dims(im, axis=0) 34 | 35 | # Load the converted model 36 | print("Loading model.") 37 | # Load model structure 38 | model = model_from_json(open('models/Keras_model_structure.json').read()) 39 | # Load model weights 40 | model.load_weights('models/Keras_model_weights.h5') 41 | 42 | # Compile converted model 43 | print("Compiling model.") 44 | sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True) 45 | loss = dict() 46 | for out in out_layer_names: 47 | loss[out] = 'categorical_crossentropy' 48 | last_out = out 49 | model.compile(optimizer=sgd, loss=loss) 50 | 51 | # Predict image output 52 | print("Applying prediction.") 53 | in_data = dict() 54 | for input in ['data']: 55 | in_data[input] = im 56 | out = model.predict(in_data) 57 | 58 | # Load ImageNet classes file 59 | classes = [] 60 | with open('models/classes.txt', 'r') as list_: 61 | for line in list_: 62 | classes.append(line.rstrip('\n')) 63 | 64 | for i, o in enumerate(out_layer_names): 65 | print('Prediction on output layer "' + o + '": ' + str(classes[np.argmax(out[i])])) 66 | -------------------------------------------------------------------------------- /keras/caffe/test_converted_VGG_FACE.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential, Model, model_from_json 2 | from keras.optimizers import SGD 3 | 4 | from scipy import misc 5 | import numpy as np 6 | import copy 7 | 8 | if __name__ == "__main__": 9 | 10 | model_path = '/media/HDD_3TB/CNN_MODELS/VGG_Face' 11 | 12 | print("Preparing test image.") 13 | # Read image 14 | im = misc.imread('models/cat.jpg') 15 | 16 | # Resize 17 | im = misc.imresize(im, (224, 224)).astype(np.float32) 18 | 19 | # Change RGB to BGR 20 | aux = copy.copy(im) 21 | im[:, :, 0] = aux[:, :, 2] 22 | im[:, :, 2] = aux[:, :, 0] 23 | 24 | # Remove train image mean 25 | im[:, :, 0] -= 104.006 26 | im[:, :, 1] -= 116.669 27 | im[:, :, 2] -= 122.679 28 | 29 | # Transpose image dimensions (Keras' uses the channels as the 1st dimension) 30 | im = np.transpose(im, (2, 0, 1)) 31 | 32 | # Insert a new dimension for the batch_size 33 | im = np.expand_dims(im, axis=0) 34 | 35 | # Load the converted model 36 | print("Loading model.") 37 | # Load model structure 38 | model = model_from_json(open(model_path + '/Keras_model_structure.json').read()) 39 | # Load model weights 40 | model.load_weights(model_path + '/Keras_model_weights.h5') 41 | 42 | # Get output names 43 | out_layer_names = model.output_names 44 | in_layer_names = model.input_names 45 | 46 | # Predict image output 47 | print("Applying prediction.") 48 | in_data = dict() 49 | for in_name in in_layer_names: 50 | in_data[in_name] = im 51 | out = model.predict(in_data) 52 | 53 | # Load ImageNet classes file 54 | classes = [] 55 | with open(model_path + '/names.txt', 'r') as list_: 56 | for line in list_: 57 | classes.append(line.rstrip('\n')) 58 | 59 | for i, o in enumerate(out_layer_names): 60 | print('Prediction on output layer "' + o + '": ' + str(classes[np.argmax(out[i])])) 61 | -------------------------------------------------------------------------------- /keras/callbacks/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from .callbacks import Callback 4 | from .callbacks import CallbackList 5 | from .callbacks import BaseLogger 6 | from .callbacks import TerminateOnNaN 7 | from .callbacks import ProgbarLogger 8 | from .callbacks import History 9 | from .callbacks import ModelCheckpoint 10 | from .callbacks import EarlyStopping 11 | from .callbacks import RemoteMonitor 12 | from .callbacks import LearningRateScheduler 13 | from .callbacks import ReduceLROnPlateau 14 | from .callbacks import CSVLogger 15 | from .callbacks import LambdaCallback 16 | 17 | from .. import backend as K 18 | 19 | if K.backend() == 'tensorflow' and not K.tensorflow_backend._is_tf_1(): 20 | from .tensorboard_v2 import TensorBoard 21 | else: 22 | from .tensorboard_v1 import TensorBoard 23 | -------------------------------------------------------------------------------- /keras/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import mnist 4 | from . import imdb 5 | from . import reuters 6 | from . import cifar10 7 | from . import cifar100 8 | from . import boston_housing 9 | from . import fashion_mnist 10 | -------------------------------------------------------------------------------- /keras/datasets/boston_housing.py: -------------------------------------------------------------------------------- 1 | """Boston housing price regression dataset. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from ..utils.data_utils import get_file 8 | import numpy as np 9 | 10 | 11 | def load_data(path='boston_housing.npz', test_split=0.2, seed=113): 12 | """Loads the Boston Housing dataset. 13 | 14 | # Arguments 15 | path: path where to cache the dataset locally 16 | (relative to ~/.keras/datasets). 17 | test_split: fraction of the data to reserve as test set. 18 | seed: Random seed for shuffling the data 19 | before computing the test split. 20 | 21 | # Returns 22 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 23 | """ 24 | assert 0 <= test_split < 1 25 | path = get_file( 26 | path, 27 | origin='https://s3.amazonaws.com/keras-datasets/boston_housing.npz', 28 | file_hash='f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5') 29 | with np.load(path, allow_pickle=True) as f: 30 | x = f['x'] 31 | y = f['y'] 32 | 33 | rng = np.random.RandomState(seed) 34 | indices = np.arange(len(x)) 35 | rng.shuffle(indices) 36 | x = x[indices] 37 | y = y[indices] 38 | 39 | x_train = np.array(x[:int(len(x) * (1 - test_split))]) 40 | y_train = np.array(y[:int(len(x) * (1 - test_split))]) 41 | x_test = np.array(x[int(len(x) * (1 - test_split)):]) 42 | y_test = np.array(y[int(len(x) * (1 - test_split)):]) 43 | return (x_train, y_train), (x_test, y_test) 44 | -------------------------------------------------------------------------------- /keras/datasets/cifar.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Utilities common to CIFAR10 and CIFAR100 datasets. 3 | """ 4 | from __future__ import absolute_import 5 | from __future__ import division 6 | from __future__ import print_function 7 | 8 | import sys 9 | from six.moves import cPickle 10 | 11 | 12 | def load_batch(fpath, label_key='labels'): 13 | """Internal utility for parsing CIFAR data. 14 | 15 | # Arguments 16 | fpath: path the file to parse. 17 | label_key: key for label data in the retrieve 18 | dictionary. 19 | 20 | # Returns 21 | A tuple `(data, labels)`. 22 | """ 23 | with open(fpath, 'rb') as f: 24 | if sys.version_info < (3,): 25 | d = cPickle.load(f) 26 | else: 27 | d = cPickle.load(f, encoding='bytes') 28 | # decode utf8 29 | d_decoded = {} 30 | for k, v in d.items(): 31 | d_decoded[k.decode('utf8')] = v 32 | d = d_decoded 33 | data = d['data'] 34 | labels = d[label_key] 35 | 36 | data = data.reshape(data.shape[0], 3, 32, 32) 37 | return data, labels 38 | -------------------------------------------------------------------------------- /keras/datasets/cifar10.py: -------------------------------------------------------------------------------- 1 | """CIFAR10 small images classification dataset. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from .cifar import load_batch 8 | from ..utils.data_utils import get_file 9 | from .. import backend as K 10 | import numpy as np 11 | import os 12 | 13 | 14 | def load_data(): 15 | """Loads CIFAR10 dataset. 16 | 17 | # Returns 18 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 19 | """ 20 | dirname = 'cifar-10-batches-py' 21 | origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz' 22 | path = get_file(dirname, origin=origin, untar=True) 23 | 24 | num_train_samples = 50000 25 | 26 | x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8') 27 | y_train = np.empty((num_train_samples,), dtype='uint8') 28 | 29 | for i in range(1, 6): 30 | fpath = os.path.join(path, 'data_batch_' + str(i)) 31 | (x_train[(i - 1) * 10000: i * 10000, :, :, :], 32 | y_train[(i - 1) * 10000: i * 10000]) = load_batch(fpath) 33 | 34 | fpath = os.path.join(path, 'test_batch') 35 | x_test, y_test = load_batch(fpath) 36 | 37 | y_train = np.reshape(y_train, (len(y_train), 1)) 38 | y_test = np.reshape(y_test, (len(y_test), 1)) 39 | 40 | if K.image_data_format() == 'channels_last': 41 | x_train = x_train.transpose(0, 2, 3, 1) 42 | x_test = x_test.transpose(0, 2, 3, 1) 43 | 44 | return (x_train, y_train), (x_test, y_test) 45 | -------------------------------------------------------------------------------- /keras/datasets/cifar100.py: -------------------------------------------------------------------------------- 1 | """CIFAR100 small images classification dataset. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from .cifar import load_batch 8 | from ..utils.data_utils import get_file 9 | from .. import backend as K 10 | import numpy as np 11 | import os 12 | 13 | 14 | def load_data(label_mode='fine'): 15 | """Loads CIFAR100 dataset. 16 | 17 | # Arguments 18 | label_mode: one of "fine", "coarse". 19 | 20 | # Returns 21 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 22 | 23 | # Raises 24 | ValueError: in case of invalid `label_mode`. 25 | """ 26 | if label_mode not in ['fine', 'coarse']: 27 | raise ValueError('`label_mode` must be one of `"fine"`, `"coarse"`.') 28 | 29 | dirname = 'cifar-100-python' 30 | origin = 'https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz' 31 | path = get_file(dirname, origin=origin, untar=True) 32 | 33 | fpath = os.path.join(path, 'train') 34 | x_train, y_train = load_batch(fpath, label_key=label_mode + '_labels') 35 | 36 | fpath = os.path.join(path, 'test') 37 | x_test, y_test = load_batch(fpath, label_key=label_mode + '_labels') 38 | 39 | y_train = np.reshape(y_train, (len(y_train), 1)) 40 | y_test = np.reshape(y_test, (len(y_test), 1)) 41 | 42 | if K.image_data_format() == 'channels_last': 43 | x_train = x_train.transpose(0, 2, 3, 1) 44 | x_test = x_test.transpose(0, 2, 3, 1) 45 | 46 | return (x_train, y_train), (x_test, y_test) 47 | -------------------------------------------------------------------------------- /keras/datasets/fashion_mnist.py: -------------------------------------------------------------------------------- 1 | """Fashion-MNIST dataset. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | import gzip 8 | import os 9 | 10 | from ..utils.data_utils import get_file 11 | import numpy as np 12 | 13 | 14 | def load_data(): 15 | """Loads the Fashion-MNIST dataset. 16 | 17 | # Returns 18 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 19 | """ 20 | dirname = os.path.join('datasets', 'fashion-mnist') 21 | base = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' 22 | files = ['train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz', 23 | 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'] 24 | 25 | paths = [] 26 | for fname in files: 27 | paths.append(get_file(fname, 28 | origin=base + fname, 29 | cache_subdir=dirname)) 30 | 31 | with gzip.open(paths[0], 'rb') as lbpath: 32 | y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8) 33 | 34 | with gzip.open(paths[1], 'rb') as imgpath: 35 | x_train = np.frombuffer(imgpath.read(), np.uint8, 36 | offset=16).reshape(len(y_train), 28, 28) 37 | 38 | with gzip.open(paths[2], 'rb') as lbpath: 39 | y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8) 40 | 41 | with gzip.open(paths[3], 'rb') as imgpath: 42 | x_test = np.frombuffer(imgpath.read(), np.uint8, 43 | offset=16).reshape(len(y_test), 28, 28) 44 | 45 | return (x_train, y_train), (x_test, y_test) 46 | -------------------------------------------------------------------------------- /keras/datasets/mnist.py: -------------------------------------------------------------------------------- 1 | """MNIST handwritten digits dataset. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from ..utils.data_utils import get_file 8 | import numpy as np 9 | 10 | 11 | def load_data(path='mnist.npz'): 12 | """Loads the MNIST dataset. 13 | 14 | # Arguments 15 | path: path where to cache the dataset locally 16 | (relative to ~/.keras/datasets). 17 | 18 | # Returns 19 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`. 20 | """ 21 | path = get_file(path, 22 | origin='https://s3.amazonaws.com/img-datasets/mnist.npz', 23 | file_hash='8a61469f7ea1b51cbae51d4f78837e45') 24 | with np.load(path, allow_pickle=True) as f: 25 | x_train, y_train = f['x_train'], f['y_train'] 26 | x_test, y_test = f['x_test'], f['y_test'] 27 | return (x_train, y_train), (x_test, y_test) 28 | -------------------------------------------------------------------------------- /keras/engine/__init__.py: -------------------------------------------------------------------------------- 1 | # note: `Node` is an internal class, 2 | # it isn't meant to be used by Keras users. 3 | from .input_layer import Input 4 | from .input_layer import InputLayer 5 | from .base_layer import InputSpec 6 | from .base_layer import Layer 7 | from .network import get_source_inputs 8 | from .training import Model 9 | -------------------------------------------------------------------------------- /keras/engine/topology.py: -------------------------------------------------------------------------------- 1 | """This module is deprecated, but kept around for backwards compatibility. 2 | """ 3 | from .base_layer import Layer, Node, InputSpec 4 | from .input_layer import Input, InputLayer 5 | from .network import Network, get_source_inputs 6 | -------------------------------------------------------------------------------- /keras/layers/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from ..utils.generic_utils import deserialize_keras_object 4 | from ..engine.base_layer import Layer 5 | from ..engine import Input 6 | from ..engine import InputLayer 7 | from ..engine.base_layer import InputSpec 8 | from .merge import * 9 | from .core import * 10 | from .convolutional import * 11 | from .pooling import * 12 | from .local import * 13 | from .loss_layers import * 14 | from .recurrent import * 15 | from .recurrent_advanced import * 16 | from .cudnn_recurrent import * 17 | from .normalization import * 18 | from .embeddings import * 19 | from .noise import * 20 | from .advanced_activations import * 21 | from .wrappers import * 22 | from .googlenet_custom_layers import * 23 | from .convolutional_recurrent import * 24 | from .uncertainty_layers import * 25 | # TODO: Update attention layers! 26 | from .attention import * 27 | from ..legacy.layers import * 28 | 29 | 30 | def serialize(layer): 31 | """Serialize a layer. 32 | 33 | # Arguments 34 | layer: a Layer object. 35 | 36 | # Returns 37 | dictionary with config. 38 | """ 39 | return {'class_name': layer.__class__.__name__, 40 | 'config': layer.get_config()} 41 | 42 | 43 | def deserialize(config, custom_objects=None): 44 | """Instantiate a layer from a config dictionary. 45 | 46 | # Arguments 47 | config: dict of the form {'class_name': str, 'config': dict} 48 | custom_objects: dict mapping class names (or function names) 49 | of custom (non-Keras) objects to class/functions 50 | 51 | # Returns 52 | Layer instance (may be Model, Sequential, Layer...) 53 | """ 54 | from .. import models 55 | globs = globals() # All layers. 56 | globs['Model'] = models.Model 57 | globs['Sequential'] = models.Sequential 58 | return deserialize_keras_object(config, 59 | module_objects=globs, 60 | custom_objects=custom_objects, 61 | printable_module_name='layer') 62 | -------------------------------------------------------------------------------- /keras/layers/googlenet_custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.layers.core import Layer 2 | 3 | 4 | class LRN(Layer): 5 | """Local Response Normalization (LRN). 6 | The local response normalization layer performs a kind of "lateral inhibition" 7 | by normalizing over local input regions. 8 | In ACROSS_CHANNELS mode, the local regions extend across nearby channels, 9 | but have no spatial extent (i.e., they have shape local_size x 1 x 1). 10 | In WITHIN_CHANNEL mode, the local regions extend spatially, but are in separate channels 11 | (i.e., they have shape 1 x local_size x local_size). Each input value is divided by (1+(\alpha /n)\sum_i{x_i^2)}^\beta, 12 | where n is the size of each local region, and the sum is taken over the region centered at 13 | that value (zero padding is added where necessary). 14 | 15 | # Arguments 16 | alpha: scaling parameter. 17 | k: offset for the scale 18 | beta: the exponent. 19 | n: local_size 20 | """ 21 | 22 | def __init__(self, alpha=0.0001, k=1, beta=0.75, n=5, **kwargs): 23 | self.alpha = alpha 24 | self.k = k 25 | self.beta = beta 26 | self.n = n 27 | super(LRN, self).__init__(**kwargs) 28 | 29 | def call(self, x, mask=None): 30 | import theano.tensor as T 31 | b, ch, r, c = x.shape 32 | half_n = self.n // 2 # half the local region 33 | input_sqr = T.sqr(x) # square the input 34 | extra_channels = T.alloc(0., b, ch + 2 * half_n, r, 35 | c) # make an empty tensor with zero pads along channel dimension 36 | input_sqr = T.set_subtensor(extra_channels[:, half_n:half_n + ch, :, :], 37 | input_sqr) # set the center to be the squared input 38 | scale = self.k # offset for the scale 39 | norm_alpha = self.alpha / self.n # normalized alpha 40 | for i in range(self.n): 41 | scale += norm_alpha * input_sqr[:, i:i + ch, :, :] 42 | scale = scale ** self.beta 43 | x = x / scale 44 | return x 45 | 46 | def get_config(self): 47 | config = {"alpha": self.alpha, 48 | "k": self.k, 49 | "beta": self.beta, 50 | "n": self.n} 51 | base_config = super(LRN, self).get_config() 52 | return dict(list(base_config.items()) + list(config.items())) 53 | 54 | 55 | class PoolHelper(Layer): 56 | """PoolHelper. 57 | Pooling utility. 58 | """ 59 | def __init__(self, **kwargs): 60 | super(PoolHelper, self).__init__(**kwargs) 61 | 62 | def call(self, x, mask=None): 63 | return x[:, :, 1:, 1:] 64 | 65 | def get_config(self): 66 | config = {} 67 | base_config = super(PoolHelper, self).get_config() 68 | return dict(list(base_config.items()) + list(config.items())) 69 | -------------------------------------------------------------------------------- /keras/layers/uncertainty_layers.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """Recurrent layers and their base classes. 3 | """ 4 | from __future__ import absolute_import 5 | from __future__ import division 6 | from __future__ import print_function 7 | 8 | import numpy as np 9 | import warnings 10 | 11 | from .. import backend as K 12 | from ..engine import Layer 13 | 14 | 15 | class ConcatenateOutputWithSigma(Layer): 16 | """ 17 | TODO: Write documentation. 18 | 19 | Arguments: 20 | output_dim: ... 21 | """ 22 | 23 | def __init__(self, output_dim, **kwargs): 24 | self.output_dim = output_dim 25 | super(ConcatenateOutputWithSigma, self).__init__(**kwargs) 26 | 27 | def build(self, input_shape): 28 | # Create a trainable weight variable for this layer. 29 | self.kernel = self.add_weight(shape=(1,), 30 | name='sigma', 31 | initializer='zeros', 32 | trainable=True) 33 | # Be sure to call this somewhere! 34 | super(ConcatenateOutputWithSigma, self).build(input_shape) 35 | 36 | def call(self, x): 37 | input_ones_matrix = ((K.abs(x) + 1) / (K.abs(x) + 1)) 38 | batch_kernel = K.expand_dims(input_ones_matrix[:, 0], -1) * self.kernel 39 | return K.concatenate((x, batch_kernel), -1) 40 | 41 | def compute_output_shape(self, input_shape): 42 | return self.output_dim 43 | 44 | def get_config(self): 45 | config = {'output_dim': self.output_dim} 46 | base_config = super(ConcatenateOutputWithSigma, self).get_config() 47 | return dict(list(base_config.items()) + list(config.items())) 48 | -------------------------------------------------------------------------------- /keras/legacy/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MarcBS/keras/82ee0908e5fab22e63434cceb60a680bc991b0c6/keras/legacy/__init__.py -------------------------------------------------------------------------------- /keras/objectives.py: -------------------------------------------------------------------------------- 1 | """Legacy objectives module. 2 | 3 | Only kept for backwards API compatibility. 4 | """ 5 | from __future__ import absolute_import 6 | from .losses import * 7 | -------------------------------------------------------------------------------- /keras/preprocessing/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from .. import backend 6 | from .. import utils 7 | 8 | import keras_preprocessing 9 | 10 | from . import image 11 | from . import sequence 12 | from . import text 13 | -------------------------------------------------------------------------------- /keras/preprocessing/sequence.py: -------------------------------------------------------------------------------- 1 | """Utilities for preprocessing sequence data. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_preprocessing import sequence 8 | from .. import utils 9 | 10 | pad_sequences = sequence.pad_sequences 11 | make_sampling_table = sequence.make_sampling_table 12 | skipgrams = sequence.skipgrams 13 | _remove_long_seq = sequence._remove_long_seq # TODO: make it public? 14 | 15 | 16 | class TimeseriesGenerator(sequence.TimeseriesGenerator, utils.Sequence): 17 | """Utility class for generating batches of temporal data. 18 | 19 | This class takes in a sequence of data-points gathered at 20 | equal intervals, along with time series parameters such as 21 | stride, length of history, etc., to produce batches for 22 | training/validation. 23 | 24 | # Arguments 25 | data: Indexable generator (such as list or Numpy array) 26 | containing consecutive data points (timesteps). 27 | The data should be at 2D, and axis 0 is expected 28 | to be the time dimension. 29 | targets: Targets corresponding to timesteps in `data`. 30 | It should have same length as `data`. 31 | length: Length of the output sequences (in number of timesteps). 32 | sampling_rate: Period between successive individual timesteps 33 | within sequences. For rate `r`, timesteps 34 | `data[i]`, `data[i-r]`, ... `data[i - length]` 35 | are used for create a sample sequence. 36 | stride: Period between successive output sequences. 37 | For stride `s`, consecutive output samples would 38 | be centered around `data[i]`, `data[i+s]`, `data[i+2*s]`, etc. 39 | start_index: Data points earlier than `start_index` will not be used 40 | in the output sequences. This is useful to reserve part of the 41 | data for test or validation. 42 | end_index: Data points later than `end_index` will not be used 43 | in the output sequences. This is useful to reserve part of the 44 | data for test or validation. 45 | shuffle: Whether to shuffle output samples, 46 | or instead draw them in chronological order. 47 | reverse: Boolean: if `true`, timesteps in each output sample will be 48 | in reverse chronological order. 49 | batch_size: Number of timeseries samples in each batch 50 | (except maybe the last one). 51 | 52 | # Returns 53 | A [Sequence](/utils/#sequence) instance. 54 | 55 | # Examples 56 | 57 | ```python 58 | from keras.preprocessing.sequence import TimeseriesGenerator 59 | import numpy as np 60 | 61 | data = np.array([[i] for i in range(50)]) 62 | targets = np.array([[i] for i in range(50)]) 63 | 64 | data_gen = TimeseriesGenerator(data, targets, 65 | length=10, sampling_rate=2, 66 | batch_size=2) 67 | assert len(data_gen) == 20 68 | 69 | batch_0 = data_gen[0] 70 | x, y = batch_0 71 | assert np.array_equal(x, 72 | np.array([[[0], [2], [4], [6], [8]], 73 | [[1], [3], [5], [7], [9]]])) 74 | assert np.array_equal(y, 75 | np.array([[10], [11]])) 76 | ``` 77 | """ 78 | pass 79 | -------------------------------------------------------------------------------- /keras/preprocessing/text.py: -------------------------------------------------------------------------------- 1 | """Utilities for text input preprocessing. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_preprocessing import text 8 | 9 | text_to_word_sequence = text.text_to_word_sequence 10 | one_hot = text.one_hot 11 | hashing_trick = text.hashing_trick 12 | Tokenizer = text.Tokenizer 13 | tokenizer_from_json = text.tokenizer_from_json 14 | -------------------------------------------------------------------------------- /keras/regularizers.py: -------------------------------------------------------------------------------- 1 | """Built-in regularizers. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | import six 8 | from . import backend as K 9 | from .utils.generic_utils import serialize_keras_object 10 | from .utils.generic_utils import deserialize_keras_object 11 | 12 | 13 | class Regularizer(object): 14 | """Regularizer base class. 15 | """ 16 | 17 | def __call__(self, x): 18 | return 0. 19 | 20 | @classmethod 21 | def from_config(cls, config): 22 | return cls(**config) 23 | 24 | 25 | class L1L2(Regularizer): 26 | """Regularizer for L1 and L2 regularization. 27 | 28 | # Arguments 29 | l1: Float; L1 regularization factor. 30 | l2: Float; L2 regularization factor. 31 | """ 32 | 33 | def __init__(self, l1=0., l2=0.): 34 | self.l1 = K.cast_to_floatx(l1) 35 | self.l2 = K.cast_to_floatx(l2) 36 | 37 | def __call__(self, x): 38 | regularization = 0. 39 | if self.l1: 40 | regularization += self.l1 * K.sum(K.abs(x)) 41 | if self.l2: 42 | regularization += self.l2 * K.sum(K.square(x)) 43 | return regularization 44 | 45 | def get_config(self): 46 | return {'l1': float(self.l1), 47 | 'l2': float(self.l2)} 48 | 49 | 50 | class AlphaRegularizer(Regularizer): 51 | """Doubly stochastic regularization for attention weights 52 | 53 | # Arguments 54 | alpha_factor: Float; alpha regularization factor. 55 | """ 56 | 57 | def __init__(self, alpha_factor=0.): 58 | self.alpha_factor = K.cast_to_floatx(alpha_factor) 59 | 60 | def __call__(self, x): 61 | regularization = 0. 62 | if self.alpha_factor: 63 | regularization = self.alpha_factor * K.mean(K.sum((1. - K.sum(x, axis=0)) ** 2, axis=0), axis=0) 64 | 65 | return regularization 66 | 67 | def get_config(self): 68 | return {'alpha_factor': float(self.alpha_factor)} 69 | 70 | 71 | # Aliases. 72 | 73 | 74 | def l1(l=0.01): 75 | return L1L2(l1=l) 76 | 77 | 78 | def l2(l=0.01): 79 | return L1L2(l2=l) 80 | 81 | 82 | def l1_l2(l1=0.01, l2=0.01): 83 | return L1L2(l1=l1, l2=l2) 84 | 85 | 86 | def serialize(regularizer): 87 | return serialize_keras_object(regularizer) 88 | 89 | 90 | def deserialize(config, custom_objects=None): 91 | return deserialize_keras_object(config, 92 | module_objects=globals(), 93 | custom_objects=custom_objects, 94 | printable_module_name='regularizer') 95 | 96 | 97 | def get(identifier): 98 | if identifier is None: 99 | return None 100 | if isinstance(identifier, dict): 101 | return deserialize(identifier) 102 | elif isinstance(identifier, six.string_types): 103 | config = {'class_name': str(identifier), 'config': {}} 104 | return deserialize(config) 105 | elif callable(identifier): 106 | return identifier 107 | else: 108 | raise ValueError('Could not interpret regularizer identifier: ' + 109 | str(identifier)) 110 | -------------------------------------------------------------------------------- /keras/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from . import np_utils 3 | from . import generic_utils 4 | from . import data_utils 5 | from . import io_utils 6 | from . import conv_utils 7 | from . import losses_utils 8 | from . import metrics_utils 9 | 10 | # Globally-importable utils. 11 | from .io_utils import HDF5Matrix 12 | from .io_utils import H5Dict 13 | from .data_utils import get_file 14 | from .data_utils import Sequence 15 | from .data_utils import GeneratorEnqueuer 16 | from .data_utils import OrderedEnqueuer 17 | from .generic_utils import CustomObjectScope 18 | from .generic_utils import custom_object_scope 19 | from .generic_utils import get_custom_objects 20 | from .generic_utils import serialize_keras_object 21 | from .generic_utils import deserialize_keras_object 22 | from .generic_utils import Progbar 23 | from .layer_utils import convert_all_kernels_in_model 24 | from .layer_utils import get_source_inputs 25 | from .layer_utils import print_summary 26 | from .vis_utils import model_to_dot 27 | from .vis_utils import plot_model 28 | from .np_utils import to_categorical 29 | from .np_utils import normalize 30 | from .multi_gpu_utils import multi_gpu_model 31 | -------------------------------------------------------------------------------- /keras/utils/np_utils.py: -------------------------------------------------------------------------------- 1 | """Numpy-related utilities.""" 2 | from __future__ import absolute_import 3 | from __future__ import division 4 | from __future__ import print_function 5 | 6 | import numpy as np 7 | 8 | 9 | def to_categorical(y, num_classes=None, dtype='float32'): 10 | """Converts a class vector (integers) to binary class matrix. 11 | 12 | E.g. for use with categorical_crossentropy. 13 | 14 | # Arguments 15 | y: class vector to be converted into a matrix 16 | (integers from 0 to num_classes). 17 | num_classes: total number of classes. 18 | dtype: The data type expected by the input, as a string 19 | (`float32`, `float64`, `int32`...) 20 | 21 | # Returns 22 | A binary matrix representation of the input. The classes axis 23 | is placed last. 24 | 25 | # Example 26 | 27 | ```python 28 | # Consider an array of 5 labels out of a set of 3 classes {0, 1, 2}: 29 | > labels 30 | array([0, 2, 1, 2, 0]) 31 | # `to_categorical` converts this into a matrix with as many 32 | # columns as there are classes. The number of rows 33 | # stays the same. 34 | > to_categorical(labels) 35 | array([[ 1., 0., 0.], 36 | [ 0., 0., 1.], 37 | [ 0., 1., 0.], 38 | [ 0., 0., 1.], 39 | [ 1., 0., 0.]], dtype=float32) 40 | ``` 41 | """ 42 | 43 | y = np.array(y, dtype='int') 44 | input_shape = y.shape 45 | if input_shape and input_shape[-1] == 1 and len(input_shape) > 1: 46 | input_shape = tuple(input_shape[:-1]) 47 | y = y.ravel() 48 | if not num_classes: 49 | num_classes = np.max(y) + 1 50 | n = y.shape[0] 51 | categorical = np.zeros((n, num_classes), dtype=dtype) 52 | categorical[np.arange(n), y] = 1 53 | output_shape = input_shape + (num_classes,) 54 | categorical = np.reshape(categorical, output_shape) 55 | return categorical 56 | 57 | 58 | def normalize(x, axis=-1, order=2): 59 | """Normalizes a NumPy array. 60 | 61 | # Arguments 62 | x: NumPy array to normalize. 63 | axis: axis along which to normalize. 64 | order: Normalization order (e.g. 2 for L2 norm). 65 | 66 | # Returns 67 | A normalized copy of the array. 68 | """ 69 | l2 = np.atleast_1d(np.linalg.norm(x, order, axis)) 70 | l2[l2 == 0] = 1 71 | return x / np.expand_dims(l2, axis) 72 | -------------------------------------------------------------------------------- /keras/wrappers/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import scikit_learn 4 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | # Configuration of py.test 2 | [pytest] 3 | addopts=-v 4 | -n 2 5 | --durations=20 6 | 7 | # Do not run tests in the build folder 8 | norecursedirs= build 9 | 10 | # PEP-8 The following are ignored: 11 | # E501 line too long (82 > 79 characters) 12 | # E402 module level import not at top of file - temporary measure to continue adding ros python packaged in sys.path 13 | # E731 do not assign a lambda expression, use a def 14 | # W503 line break occurred before a binary operator 15 | 16 | pep8ignore=* E402 \ 17 | * E731 \ 18 | * W503 \ 19 | keras/callbacks.py E501 \ 20 | keras/constraints.py E501 \ 21 | keras/metrics.py E501 \ 22 | keras/models.py E501 \ 23 | keras/optimizers.py E501 \ 24 | keras/regularizers.py E501 \ 25 | keras/backend/cntk_backend.py E501 \ 26 | keras/backend/common.py E501 \ 27 | keras/backend/tensorflow_backend.py E501 \ 28 | keras/backend/theano_backend.py E501 \ 29 | keras/caffe/caffe2keras.py E501 \ 30 | keras/caffe/caffe_pb2.py E501 \ 31 | keras/caffe/caffe_utils.py E501 \ 32 | keras/caffe/convert.py E501 \ 33 | keras/caffe/extra_layers.py E501 \ 34 | keras/caffe/test_converted.py E501 \ 35 | keras/caffe/test_converted_VGG_FACE.py E501 \ 36 | keras/caffe/models/segmentation/test_segmentation.py E501 \ 37 | keras/datasets/boston_housing.py E501 \ 38 | keras/datasets/imdb.py E501 \ 39 | keras/datasets/reuters.py E501 \ 40 | keras/engine/network.py E501 \ 41 | keras/engine/saving.py E501 \ 42 | keras/engine/training.py E501 \ 43 | keras/engine/training_generator.py E501 \ 44 | keras/layers/advanced_activations.py E501 \ 45 | keras/layers/attention.py E501 \ 46 | keras/layers/convolutional.py E501 \ 47 | keras/layers/convolutional_recurrent.py E501 \ 48 | keras/layers/core.py E501 \ 49 | keras/layers/cudnn_recurrent.py E501 \ 50 | keras/layers/embeddings.py E501 \ 51 | keras/layers/googlenet_custom_layers.py E501 \ 52 | keras/layers/local.py E501 \ 53 | keras/layers/loss_layers.py E501 \ 54 | keras/layers/merge.py E501 \ 55 | keras/layers/noise.py E501 \ 56 | keras/layers/normalization.py E501 \ 57 | keras/layers/recurrent_advanced.py E501 \ 58 | keras/layers/recurrent.py E501 \ 59 | keras/layers/wrappers.py E501 \ 60 | keras/legacy/interfaces.py E501 \ 61 | keras/legacy/layers.py E501 \ 62 | tests/keras/backend/backend_test.py E501 63 | 64 | # Enable line length testing with maximum line length of 85 65 | pep8maxlinelength = 85 66 | 67 | # Ignore warnings which are verbose and unrelated to Keras 68 | filterwarnings = 69 | ignore:np.asscalar:DeprecationWarning 70 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | from setuptools import find_packages 3 | 4 | long_description = ''' 5 | Keras is a high-level neural networks API, 6 | written in Python and capable of running on top of 7 | TensorFlow, CNTK, or Theano. 8 | 9 | Use Keras if you need a deep learning library that: 10 | 11 | - Allows for easy and fast prototyping 12 | (through user friendliness, modularity, and extensibility). 13 | - Supports both convolutional networks and recurrent networks, 14 | as well as combinations of the two. 15 | - Runs seamlessly on CPU and GPU. 16 | 17 | Read the documentation at: https://keras.io/ 18 | 19 | For a detailed overview of what makes Keras special, see: 20 | https://keras.io/why-use-keras/ 21 | 22 | Keras is compatible with Python 2.7-3.6 23 | and is distributed under the MIT license. 24 | ''' 25 | 26 | setup(name='Keras', 27 | version='2.3.1.1', 28 | description='Deep Learning for humans', 29 | long_description=long_description, 30 | author='Francois Chollet', 31 | author_email='francois.chollet@gmail.com', 32 | url='https://github.com/MarcBS/keras', 33 | download_url='https://github.com/MarcBS/keras/tarball/2.3.1', 34 | license='MIT', 35 | install_requires=['numpy>=1.9.1', 36 | 'scipy>=0.14', 37 | 'six>=1.9.0', 38 | 'pyyaml', 39 | 'h5py', 40 | 'keras_applications>=1.0.6', 41 | 'keras_preprocessing>=1.0.5'], 42 | extras_require={ 43 | 'visualize': ['pydot>=1.2.4'], 44 | 'tests': ['pytest', 45 | 'pytest-pep8', 46 | 'pytest-xdist', 47 | 'flaky', 48 | 'pytest-cov', 49 | 'pandas', 50 | 'requests', 51 | 'markdown'], 52 | }, 53 | classifiers=[ 54 | 'Development Status :: 5 - Production/Stable', 55 | 'Intended Audience :: Developers', 56 | 'Intended Audience :: Education', 57 | 'Intended Audience :: Science/Research', 58 | 'License :: OSI Approved :: MIT License', 59 | 'Programming Language :: Python :: 2', 60 | 'Programming Language :: Python :: 2.7', 61 | 'Programming Language :: Python :: 3', 62 | 'Programming Language :: Python :: 3.6', 63 | 'Topic :: Software Development :: Libraries', 64 | 'Topic :: Software Development :: Libraries :: Python Modules' 65 | ], 66 | packages=find_packages()) 67 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras import backend as K 3 | 4 | 5 | @pytest.fixture(autouse=True) 6 | def clear_session_after_test(): 7 | """Test wrapper to clean up after TensorFlow and CNTK tests. 8 | 9 | This wrapper runs for all the tests in the keras test suite. 10 | """ 11 | yield 12 | if K.backend() == 'tensorflow' or K.backend() == 'cntk': 13 | K.clear_session() 14 | -------------------------------------------------------------------------------- /tests/integration_tests/applications_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import random 3 | import os 4 | from multiprocessing import Process, Queue 5 | from keras import applications 6 | from keras import backend as K 7 | 8 | 9 | MODEL_LIST = [ 10 | (applications.ResNet50, 2048), 11 | (applications.ResNet101, 2048), 12 | (applications.ResNet152, 2048), 13 | (applications.ResNet50V2, 2048), 14 | (applications.ResNet101V2, 2048), 15 | (applications.ResNet152V2, 2048), 16 | (applications.VGG16, 512), 17 | (applications.VGG19, 512), 18 | (applications.Xception, 2048), 19 | (applications.InceptionV3, 2048), 20 | (applications.InceptionResNetV2, 1536), 21 | (applications.MobileNet, 1024), 22 | (applications.MobileNetV2, 1280), 23 | (applications.DenseNet121, 1024), 24 | (applications.DenseNet169, 1664), 25 | (applications.DenseNet201, 1920), 26 | # Note that NASNetLarge is too heavy to test on Travis. 27 | (applications.NASNetMobile, 1056) 28 | ] 29 | 30 | 31 | def _get_output_shape(model_fn): 32 | if K.backend() == 'cntk': 33 | # Create model in a subprocess so that 34 | # the memory consumed by InceptionResNetV2 will be 35 | # released back to the system after this test 36 | # (to deal with OOM error on CNTK backend). 37 | # TODO: remove the use of multiprocessing from these tests 38 | # once a memory clearing mechanism 39 | # is implemented in the CNTK backend. 40 | def target(queue): 41 | model = model_fn() 42 | queue.put(model.output_shape) 43 | queue = Queue() 44 | p = Process(target=target, args=(queue,)) 45 | p.start() 46 | p.join() 47 | # The error in a subprocess won't propagate 48 | # to the main process, so we check if the model 49 | # is successfully created by checking if the output shape 50 | # has been put into the queue 51 | assert not queue.empty(), 'Model creation failed.' 52 | return queue.get_nowait() 53 | else: 54 | model = model_fn() 55 | return model.output_shape 56 | 57 | 58 | def _test_application_basic(app, last_dim=1000): 59 | output_shape = _get_output_shape(lambda: app(weights=None)) 60 | assert output_shape == (None, last_dim) 61 | 62 | 63 | def _test_application_notop(app, last_dim): 64 | output_shape = _get_output_shape( 65 | lambda: app(weights=None, include_top=False)) 66 | assert len(output_shape) == 4 67 | assert output_shape[-1] == last_dim 68 | 69 | 70 | def test_applications(): 71 | for _ in range(3): 72 | app, last_dim = random.choice(MODEL_LIST) 73 | _test_application_basic(app) 74 | _test_application_notop(app, last_dim) 75 | 76 | 77 | if __name__ == '__main__': 78 | pytest.main([__file__]) 79 | -------------------------------------------------------------------------------- /tests/integration_tests/test_datasets.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import pytest 3 | import time 4 | import random 5 | from keras.datasets import cifar10 6 | from keras.datasets import cifar100 7 | from keras.datasets import reuters 8 | from keras.datasets import imdb 9 | from keras.datasets import mnist 10 | from keras.datasets import boston_housing 11 | from keras.datasets import fashion_mnist 12 | 13 | 14 | def test_cifar(): 15 | # only run data download tests 20% of the time 16 | # to speed up frequent testing 17 | random.seed(time.time()) 18 | if random.random() > 0.8: 19 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 20 | assert len(x_train) == len(y_train) == 50000 21 | assert len(x_test) == len(y_test) == 10000 22 | (x_train, y_train), (x_test, y_test) = cifar100.load_data('fine') 23 | assert len(x_train) == len(y_train) == 50000 24 | assert len(x_test) == len(y_test) == 10000 25 | (x_train, y_train), (x_test, y_test) = cifar100.load_data('coarse') 26 | assert len(x_train) == len(y_train) == 50000 27 | assert len(x_test) == len(y_test) == 10000 28 | 29 | 30 | def test_reuters(): 31 | # only run data download tests 20% of the time 32 | # to speed up frequent testing 33 | random.seed(time.time()) 34 | if random.random() > 0.8: 35 | (x_train, y_train), (x_test, y_test) = reuters.load_data() 36 | assert len(x_train) == len(y_train) 37 | assert len(x_test) == len(y_test) 38 | assert len(x_train) + len(x_test) == 11228 39 | (x_train, y_train), (x_test, y_test) = reuters.load_data(maxlen=10) 40 | assert len(x_train) == len(y_train) 41 | assert len(x_test) == len(y_test) 42 | word_index = reuters.get_word_index() 43 | assert isinstance(word_index, dict) 44 | 45 | 46 | def test_mnist(): 47 | # only run data download tests 20% of the time 48 | # to speed up frequent testing 49 | random.seed(time.time()) 50 | if random.random() > 0.8: 51 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 52 | assert len(x_train) == len(y_train) == 60000 53 | assert len(x_test) == len(y_test) == 10000 54 | 55 | 56 | def test_imdb(): 57 | # only run data download tests 20% of the time 58 | # to speed up frequent testing 59 | random.seed(time.time()) 60 | if random.random() > 0.8: 61 | (x_train, y_train), (x_test, y_test) = imdb.load_data() 62 | (x_train, y_train), (x_test, y_test) = imdb.load_data(maxlen=40) 63 | assert len(x_train) == len(y_train) 64 | assert len(x_test) == len(y_test) 65 | word_index = imdb.get_word_index() 66 | assert isinstance(word_index, dict) 67 | 68 | 69 | def test_boston_housing(): 70 | # only run data download tests 20% of the time 71 | # to speed up frequent testing 72 | random.seed(time.time()) 73 | if random.random() > 0.8: 74 | (x_train, y_train), (x_test, y_test) = boston_housing.load_data() 75 | assert len(x_train) == len(y_train) 76 | assert len(x_test) == len(y_test) 77 | 78 | 79 | def test_fashion_mnist(): 80 | # only run data download tests 20% of the time 81 | # to speed up frequent testing 82 | random.seed(time.time()) 83 | if random.random() > 0.8: 84 | (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() 85 | assert len(x_train) == len(y_train) == 60000 86 | assert len(x_test) == len(y_test) == 10000 87 | 88 | 89 | if __name__ == '__main__': 90 | pytest.main([__file__]) 91 | -------------------------------------------------------------------------------- /tests/integration_tests/test_image_data_tasks.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import numpy as np 3 | import pytest 4 | 5 | from keras.preprocessing.image import ImageDataGenerator 6 | from keras.utils.test_utils import get_test_data 7 | from keras.models import Sequential 8 | from keras import layers 9 | from keras.utils.np_utils import to_categorical 10 | 11 | 12 | def test_image_classification(): 13 | np.random.seed(1337) 14 | input_shape = (16, 16, 3) 15 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 16 | num_test=200, 17 | input_shape=input_shape, 18 | classification=True, 19 | num_classes=4) 20 | y_train = to_categorical(y_train) 21 | y_test = to_categorical(y_test) 22 | 23 | model = Sequential([ 24 | layers.Conv2D(filters=8, kernel_size=3, 25 | activation='relu', 26 | input_shape=input_shape), 27 | layers.MaxPooling2D(pool_size=2), 28 | layers.Conv2D(filters=4, kernel_size=(3, 3), 29 | activation='relu', padding='same'), 30 | layers.GlobalAveragePooling2D(), 31 | layers.Dense(y_test.shape[-1], activation='softmax') 32 | ]) 33 | model.compile(loss='categorical_crossentropy', 34 | optimizer='rmsprop', 35 | metrics=['accuracy']) 36 | model.summary() 37 | history = model.fit(x_train, y_train, epochs=12, batch_size=16, 38 | validation_data=(x_test, y_test), 39 | verbose=0) 40 | assert history.history['val_accuracy'][-1] > 0.75 41 | config = model.get_config() 42 | model = Sequential.from_config(config) 43 | 44 | 45 | def test_image_data_generator_training(): 46 | np.random.seed(1337) 47 | img_gen = ImageDataGenerator(rescale=1.) # Dummy ImageDataGenerator 48 | input_shape = (16, 16, 3) 49 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 50 | num_test=200, 51 | input_shape=input_shape, 52 | classification=True, 53 | num_classes=4) 54 | y_train = to_categorical(y_train) 55 | y_test = to_categorical(y_test) 56 | 57 | model = Sequential([ 58 | layers.Conv2D(filters=8, kernel_size=3, 59 | activation='relu', 60 | input_shape=input_shape), 61 | layers.MaxPooling2D(pool_size=2), 62 | layers.Conv2D(filters=4, kernel_size=(3, 3), 63 | activation='relu', padding='same'), 64 | layers.GlobalAveragePooling2D(), 65 | layers.Dense(y_test.shape[-1], activation='softmax') 66 | ]) 67 | model.compile(loss='categorical_crossentropy', 68 | optimizer='rmsprop', 69 | metrics=['accuracy']) 70 | history = model.fit_generator(img_gen.flow(x_train, y_train, batch_size=16), 71 | epochs=15, 72 | validation_data=img_gen.flow(x_test, y_test, 73 | batch_size=16), 74 | verbose=0) 75 | assert history.history['val_accuracy'][-1] > 0.70 76 | model.evaluate_generator(img_gen.flow(x_train, y_train, batch_size=16)) 77 | 78 | 79 | if __name__ == '__main__': 80 | pytest.main([__file__]) 81 | -------------------------------------------------------------------------------- /tests/integration_tests/test_tensorflow_integration.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import os 4 | import tempfile 5 | import pytest 6 | import keras 7 | from keras import layers 8 | from keras.utils.test_utils import get_test_data 9 | 10 | 11 | @pytest.mark.skipif(keras.backend.backend() != 'tensorflow', 12 | reason='Requires TF backend') 13 | def test_tf_optimizer(): 14 | import tensorflow as tf 15 | 16 | num_hidden = 10 17 | output_dim = 2 18 | input_dim = 10 19 | target = 0.8 20 | 21 | if tf.__version__.startswith('1.'): 22 | optimizer = tf.train.AdadeltaOptimizer( 23 | learning_rate=1., rho=0.95, epsilon=1e-08) 24 | else: 25 | optimizer = tf.keras.optimizers.Adadelta( 26 | learning_rate=1., rho=0.95, epsilon=1e-08) 27 | 28 | (x_train, y_train), (x_test, y_test) = get_test_data( 29 | num_train=1000, num_test=200, 30 | input_shape=(input_dim,), 31 | classification=True, num_classes=output_dim) 32 | 33 | model = keras.Sequential() 34 | model.add(layers.Dense(num_hidden, 35 | activation='relu', 36 | input_shape=(input_dim,))) 37 | model.add(layers.Dense(output_dim, activation='softmax')) 38 | 39 | model.compile(loss='sparse_categorical_crossentropy', 40 | optimizer=optimizer, 41 | metrics=['accuracy']) 42 | history = model.fit(x_train, y_train, epochs=8, batch_size=16, 43 | validation_data=(x_test, y_test), verbose=2) 44 | assert history.history['val_accuracy'][-1] >= target 45 | 46 | # Test saving. 47 | _, fname = tempfile.mkstemp('.h5') 48 | model.save(fname) 49 | model = keras.models.load_model(fname) 50 | assert len(model.weights) == 4 51 | os.remove(fname) 52 | 53 | 54 | if __name__ == '__main__': 55 | pytest.main([__file__]) 56 | -------------------------------------------------------------------------------- /tests/integration_tests/test_vector_data_tasks.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import pytest 3 | 4 | from keras.utils.test_utils import get_test_data 5 | from keras.models import Sequential 6 | from keras import layers 7 | import keras 8 | from keras.utils.np_utils import to_categorical 9 | 10 | num_classes = 2 11 | 12 | 13 | def test_vector_classification(): 14 | ''' 15 | Classify random float vectors into 2 classes with logistic regression 16 | using 2 layer neural network with ReLU hidden units. 17 | ''' 18 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 19 | num_test=200, 20 | input_shape=(20,), 21 | classification=True, 22 | num_classes=num_classes) 23 | y_train = to_categorical(y_train) 24 | y_test = to_categorical(y_test) 25 | 26 | # Test with Sequential API 27 | model = Sequential([ 28 | layers.Dense(16, input_shape=(x_train.shape[-1],), activation='relu'), 29 | layers.Dense(8), 30 | layers.Activation('relu'), 31 | layers.Dense(num_classes, activation='softmax') 32 | ]) 33 | model.compile(loss='categorical_crossentropy', 34 | optimizer=keras.optimizers.Adam(1e-3), 35 | metrics=['accuracy']) 36 | model.summary() 37 | history = model.fit(x_train, y_train, epochs=15, batch_size=16, 38 | validation_data=(x_test, y_test), 39 | verbose=0) 40 | assert(history.history['val_accuracy'][-1] > 0.8) 41 | config = model.get_config() 42 | model = Sequential.from_config(config) 43 | 44 | 45 | def test_vector_classification_functional(): 46 | (x_train, y_train), _ = get_test_data(num_train=500, 47 | num_test=200, 48 | input_shape=(20,), 49 | classification=True, 50 | num_classes=num_classes) 51 | # Test with functional API 52 | inputs = layers.Input(shape=(x_train.shape[-1],)) 53 | x = layers.Dense(16, activation=keras.activations.relu)(inputs) 54 | x = layers.Dense(8)(x) 55 | x = layers.Activation('relu')(x) 56 | outputs = layers.Dense(num_classes, activation='softmax')(x) 57 | model = keras.models.Model(inputs, outputs) 58 | model.compile(loss=keras.losses.sparse_categorical_crossentropy, 59 | optimizer=keras.optimizers.Adam(1e-3), 60 | metrics=['accuracy']) 61 | history = model.fit(x_train, y_train, epochs=15, batch_size=16, 62 | validation_data=(x_train, y_train), 63 | verbose=0) 64 | assert(history.history['val_accuracy'][-1] > 0.8) 65 | 66 | 67 | def test_vector_regression(): 68 | ''' 69 | Perform float data prediction (regression) using 2 layer MLP 70 | with tanh and sigmoid activations. 71 | ''' 72 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 73 | num_test=200, 74 | input_shape=(20,), 75 | output_shape=(num_classes,), 76 | classification=False) 77 | 78 | model = Sequential([ 79 | layers.Dense(16, input_shape=(x_train.shape[-1],), activation='tanh'), 80 | layers.Dense(num_classes) 81 | ]) 82 | 83 | model.compile(loss='hinge', optimizer=keras.optimizers.Adam(1e-3)) 84 | history = model.fit(x_train, y_train, epochs=20, batch_size=16, 85 | validation_data=(x_test, y_test), verbose=0) 86 | assert (history.history['val_loss'][-1] < 0.9) 87 | 88 | 89 | if __name__ == '__main__': 90 | pytest.main([__file__]) 91 | -------------------------------------------------------------------------------- /tests/keras/caffe/test_convert.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import pytest 3 | # import keras.caffe.convert as convert 4 | 5 | 6 | def test_convertGoogleNet(): 7 | pass 8 | load_path = 'keras/caffe/models' 9 | store_path = 'keras/caffe/models' 10 | prototxt = 'train_val_for_keras.prototxt' 11 | caffemodel = 'bvlc_googlenet.caffemodel' 12 | 13 | # Convert model from caffe to keras 14 | # model = convert.caffe_to_keras(load_path+'/'+prototxt, 15 | # load_path+'/'+caffemodel, debug=False) 16 | # assert(model.__class__.__name__ == 'Model') 17 | 18 | # Save converted model structure 19 | # json_string = model.to_json() 20 | # open(store_path + '/Keras_model_structure.json', 'w').write(json_string) 21 | # # Save converted model weights 22 | # model.save_weights(store_path + '/Keras_model_weights.h5', overwrite=True) 23 | 24 | 25 | if __name__ == '__main__': 26 | pytest.main([__file__]) 27 | # test_convertGoogleNet() 28 | -------------------------------------------------------------------------------- /tests/keras/constraints_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy as np 3 | from numpy.testing import assert_allclose 4 | 5 | from keras import backend as K 6 | from keras import constraints 7 | 8 | 9 | def get_test_values(): 10 | return [0.1, 0.5, 3, 8, 1e-7] 11 | 12 | 13 | def get_example_array(): 14 | np.random.seed(3537) 15 | example_array = np.random.random((100, 100)) * 100. - 50. 16 | example_array[0, 0] = 0. # 0 could possibly cause trouble 17 | return example_array 18 | 19 | 20 | def test_serialization(): 21 | all_activations = ['max_norm', 'non_neg', 22 | 'unit_norm', 'min_max_norm'] 23 | for name in all_activations: 24 | fn = constraints.get(name) 25 | ref_fn = getattr(constraints, name)() 26 | assert fn.__class__ == ref_fn.__class__ 27 | config = constraints.serialize(fn) 28 | fn = constraints.deserialize(config) 29 | assert fn.__class__ == ref_fn.__class__ 30 | 31 | 32 | def test_max_norm(): 33 | array = get_example_array() 34 | for m in get_test_values(): 35 | norm_instance = constraints.max_norm(m) 36 | normed = norm_instance(K.variable(array)) 37 | assert (np.all(K.eval(normed) < m)) 38 | 39 | # a more explicit example 40 | norm_instance = constraints.max_norm(2.0) 41 | x = np.array([[0, 0, 0], [1.0, 0, 0], [3, 0, 0], [3, 3, 3]]).T 42 | x_normed_target = np.array([[0, 0, 0], [1.0, 0, 0], 43 | [2.0, 0, 0], 44 | [2. / np.sqrt(3), 45 | 2. / np.sqrt(3), 46 | 2. / np.sqrt(3)]]).T 47 | x_normed_actual = K.eval(norm_instance(K.variable(x))) 48 | assert_allclose(x_normed_actual, x_normed_target, rtol=1e-05) 49 | 50 | 51 | def test_non_neg(): 52 | non_neg_instance = constraints.non_neg() 53 | normed = non_neg_instance(K.variable(get_example_array())) 54 | assert (np.all(np.min(K.eval(normed), axis=1) == 0.)) 55 | 56 | 57 | def test_unit_norm(): 58 | unit_norm_instance = constraints.unit_norm() 59 | normalized = unit_norm_instance(K.variable(get_example_array())) 60 | norm_of_normalized = np.sqrt(np.sum(K.eval(normalized) ** 2, axis=0)) 61 | # In the unit norm constraint, it should be equal to 1. 62 | difference = norm_of_normalized - 1. 63 | largest_difference = np.max(np.abs(difference)) 64 | assert (np.abs(largest_difference) < 10e-5) 65 | 66 | 67 | def test_min_max_norm(): 68 | array = get_example_array() 69 | for m in get_test_values(): 70 | norm_instance = constraints.min_max_norm(min_value=m, max_value=m * 2) 71 | normed = norm_instance(K.variable(array)) 72 | value = K.eval(normed) 73 | l2 = np.sqrt(np.sum(np.square(value), axis=0)) 74 | assert l2[l2 < m].size == 0 75 | assert l2[l2 > m * 2 + 1e-5].size == 0 76 | 77 | 78 | if __name__ == '__main__': 79 | pytest.main([__file__]) 80 | -------------------------------------------------------------------------------- /tests/keras/datasets/datasets_test.py: -------------------------------------------------------------------------------- 1 | import tempfile 2 | 3 | import numpy as np 4 | import pytest 5 | 6 | from keras.datasets import boston_housing 7 | from keras.datasets import imdb 8 | from keras.datasets import reuters 9 | 10 | 11 | @pytest.fixture 12 | def fake_downloaded_boston_path(monkeypatch): 13 | num_rows = 100 14 | num_cols = 10 15 | rng = np.random.RandomState(123) 16 | 17 | x = rng.randint(1, 100, size=(num_rows, num_cols)) 18 | y = rng.normal(loc=100, scale=15, size=num_rows) 19 | 20 | with tempfile.NamedTemporaryFile('wb', delete=True) as f: 21 | np.savez(f, x=x, y=y) 22 | monkeypatch.setattr(boston_housing, 'get_file', 23 | lambda *args, **kwargs: f.name) 24 | yield f.name 25 | 26 | 27 | @pytest.fixture 28 | def fake_downloaded_imdb_path(monkeypatch): 29 | train_rows = 100 30 | test_rows = 20 31 | seq_length = 10 32 | rng = np.random.RandomState(123) 33 | 34 | x_train = rng.randint(1, 100, size=(train_rows, seq_length)) 35 | y_train = rng.binomial(n=1, p=0.5, size=train_rows) 36 | x_test = rng.randint(1, 100, size=(test_rows, seq_length)) 37 | y_test = rng.binomial(n=1, p=0.5, size=test_rows) 38 | 39 | with tempfile.NamedTemporaryFile('wb', delete=True) as f: 40 | np.savez(f, x_train=x_train, y_train=y_train, x_test=x_test, y_test=y_test) 41 | monkeypatch.setattr(imdb, 'get_file', lambda *args, **kwargs: f.name) 42 | yield f.name 43 | 44 | 45 | @pytest.fixture 46 | def fake_downloaded_reuters_path(monkeypatch): 47 | num_rows = 100 48 | seq_length = 10 49 | rng = np.random.RandomState(123) 50 | 51 | x = rng.randint(1, 100, size=(num_rows, seq_length)) 52 | y = rng.binomial(n=1, p=0.5, size=num_rows) 53 | 54 | with tempfile.NamedTemporaryFile('wb', delete=True) as f: 55 | np.savez(f, x=x, y=y) 56 | monkeypatch.setattr(reuters, 'get_file', lambda *args, **kwargs: f.name) 57 | yield f.name 58 | 59 | 60 | def test_boston_load_does_not_affect_global_rng(fake_downloaded_boston_path): 61 | np.random.seed(1337) 62 | before = np.random.randint(0, 100, size=10) 63 | 64 | np.random.seed(1337) 65 | boston_housing.load_data(path=fake_downloaded_boston_path, seed=9876) 66 | after = np.random.randint(0, 100, size=10) 67 | 68 | assert np.array_equal(before, after) 69 | 70 | 71 | def test_imdb_load_does_not_affect_global_rng(fake_downloaded_imdb_path): 72 | np.random.seed(1337) 73 | before = np.random.randint(0, 100, size=10) 74 | 75 | np.random.seed(1337) 76 | imdb.load_data(path=fake_downloaded_imdb_path, seed=9876) 77 | after = np.random.randint(0, 100, size=10) 78 | 79 | assert np.array_equal(before, after) 80 | 81 | 82 | def test_reuters_load_does_not_affect_global_rng(fake_downloaded_reuters_path): 83 | np.random.seed(1337) 84 | before = np.random.randint(0, 100, size=10) 85 | 86 | np.random.seed(1337) 87 | reuters.load_data(path=fake_downloaded_reuters_path, seed=9876) 88 | after = np.random.randint(0, 100, size=10) 89 | 90 | assert np.array_equal(before, after) 91 | -------------------------------------------------------------------------------- /tests/keras/layers/advanced_activations_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras import layers 4 | from keras import backend as K 5 | 6 | 7 | @pytest.mark.parametrize('activation_layer', 8 | [layers.LeakyReLU, 9 | layers.ELU]) 10 | @pytest.mark.parametrize('alpha', [0., .5, -1.]) 11 | def test_linear_unit_activations(activation_layer, 12 | alpha): 13 | layer_test(activation_layer, kwargs={'alpha': alpha}, 14 | input_shape=(2, 3, 4)) 15 | 16 | 17 | def test_prelu(): 18 | layer_test(layers.PReLU, kwargs={}, 19 | input_shape=(2, 3, 4)) 20 | 21 | 22 | def test_prelu_share(): 23 | layer_test(layers.PReLU, kwargs={'shared_axes': 1}, 24 | input_shape=(2, 3, 4)) 25 | 26 | 27 | def test_thresholded_relu(): 28 | layer_test(layers.ThresholdedReLU, kwargs={'theta': 0.5}, 29 | input_shape=(2, 3, 4)) 30 | 31 | 32 | @pytest.mark.parametrize('axis', [1, -1]) 33 | def test_softmax(axis): 34 | layer_test(layers.Softmax, kwargs={'axis': axis}, 35 | input_shape=(2, 3, 4)) 36 | 37 | 38 | def test_relu(): 39 | layer_test(layers.ReLU, 40 | kwargs={'max_value': 10, 41 | 'negative_slope': 0.2, 42 | 'threshold': 3.0}, 43 | input_shape=(2, 3, 4)) 44 | layer_test(layers.ReLU, 45 | kwargs={'max_value': 6}, 46 | input_shape=(2, 3, 4)) 47 | layer_test(layers.ReLU, 48 | kwargs={'negative_slope': 0.2}, 49 | input_shape=(2, 3, 4)) 50 | 51 | # max_value of ReLU layer cannot be negative value 52 | with pytest.raises(ValueError): 53 | layer_test(layers.ReLU, kwargs={'max_value': -2.0}, 54 | input_shape=(2, 3, 4)) 55 | 56 | # negative_slope of ReLU layer cannot be negative value 57 | with pytest.raises(ValueError): 58 | layer_test(layers.ReLU, kwargs={'negative_slope': -2.0}, 59 | input_shape=(2, 3, 4)) 60 | 61 | 62 | @pytest.mark.skipif((K.backend() != 'tensorflow'), 63 | reason='TF-specific implementation.') 64 | def test_relu_tf_ops(): 65 | inputs = layers.Input((3,)) 66 | # Test that `relu` op gets used. 67 | outputs = layers.ReLU()(inputs) 68 | assert outputs.op.name.lower().endswith('/relu') 69 | # Test that `leakyrelu` op gets used. 70 | outputs = layers.ReLU(negative_slope=0.2)(inputs) 71 | assert outputs.op.name.lower().endswith('/leakyrelu') 72 | # Test that `relu6` op gets used. 73 | outputs = layers.ReLU(max_value=6)(inputs) 74 | assert outputs.op.name.lower().endswith('/relu6') 75 | 76 | 77 | if __name__ == '__main__': 78 | pytest.main([__file__]) 79 | -------------------------------------------------------------------------------- /tests/keras/layers/embeddings_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras.layers.embeddings import Embedding 4 | from keras.models import Sequential 5 | import keras.backend as K 6 | 7 | 8 | def test_embedding(): 9 | layer_test(Embedding, 10 | kwargs={'output_dim': 4, 'input_dim': 10, 'input_length': 2}, 11 | input_shape=(3, 2), 12 | input_dtype='int32', 13 | expected_output_dtype=K.floatx()) 14 | layer_test(Embedding, 15 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True}, 16 | input_shape=(3, 2), 17 | input_dtype='int32', 18 | expected_output_dtype=K.floatx()) 19 | layer_test(Embedding, 20 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True}, 21 | input_shape=(3, 2, 5), 22 | input_dtype='int32', 23 | expected_output_dtype=K.floatx()) 24 | layer_test(Embedding, 25 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True, 26 | 'input_length': (None, 5)}, 27 | input_shape=(3, 2, 5), 28 | input_dtype='int32', 29 | expected_output_dtype=K.floatx()) 30 | 31 | 32 | @pytest.mark.parametrize('input_shape', 33 | [(3, 4, 5), 34 | (3, 5)]) 35 | def test_embedding_invalid(input_shape): 36 | 37 | # len(input_length) should be equal to len(input_shape) - 1 38 | with pytest.raises(ValueError): 39 | model = Sequential([Embedding( 40 | input_dim=10, 41 | output_dim=4, 42 | input_length=2, 43 | input_shape=input_shape)]) 44 | 45 | 46 | if __name__ == '__main__': 47 | pytest.main([__file__]) 48 | -------------------------------------------------------------------------------- /tests/keras/layers/local_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras.utils.test_utils import layer_test 4 | from keras.layers import local 5 | 6 | 7 | def test_locallyconnected_1d(): 8 | num_samples = 2 9 | num_steps = 8 10 | input_dim = 5 11 | filter_length = 3 12 | filters = 4 13 | padding = 'valid' 14 | strides = 1 15 | 16 | layer_test(local.LocallyConnected1D, 17 | kwargs={'filters': filters, 18 | 'kernel_size': filter_length, 19 | 'padding': padding, 20 | 'kernel_regularizer': 'l2', 21 | 'bias_regularizer': 'l2', 22 | 'activity_regularizer': 'l2', 23 | 'strides': strides}, 24 | input_shape=(num_samples, num_steps, input_dim)) 25 | 26 | 27 | def test_locallyconnected_2d(): 28 | num_samples = 5 29 | filters = 3 30 | stack_size = 4 31 | num_row = 6 32 | num_col = 8 33 | padding = 'valid' 34 | 35 | for strides in [(1, 1), (2, 2)]: 36 | layer_test(local.LocallyConnected2D, 37 | kwargs={'filters': filters, 38 | 'kernel_size': 3, 39 | 'padding': padding, 40 | 'kernel_regularizer': 'l2', 41 | 'bias_regularizer': 'l2', 42 | 'activity_regularizer': 'l2', 43 | 'strides': strides, 44 | 'data_format': 'channels_last'}, 45 | input_shape=(num_samples, num_row, num_col, stack_size)) 46 | 47 | layer_test(local.LocallyConnected2D, 48 | kwargs={'filters': filters, 49 | 'kernel_size': (3, 3), 50 | 'padding': padding, 51 | 'kernel_regularizer': 'l2', 52 | 'bias_regularizer': 'l2', 53 | 'activity_regularizer': 'l2', 54 | 'strides': strides, 55 | 'data_format': 'channels_first'}, 56 | input_shape=(num_samples, stack_size, num_row, num_col)) 57 | 58 | 59 | if __name__ == '__main__': 60 | pytest.main([__file__]) 61 | -------------------------------------------------------------------------------- /tests/keras/layers/noise_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras.layers import noise 4 | from keras import backend as K 5 | 6 | 7 | @pytest.mark.skipif((K.backend() == 'cntk'), 8 | reason="cntk does not support it yet") 9 | def test_GaussianNoise(): 10 | layer_test(noise.GaussianNoise, 11 | kwargs={'stddev': 1.}, 12 | input_shape=(3, 2, 3)) 13 | 14 | 15 | @pytest.mark.skipif((K.backend() == 'cntk'), 16 | reason="cntk does not support it yet") 17 | def test_GaussianDropout(): 18 | layer_test(noise.GaussianDropout, 19 | kwargs={'rate': 0.5}, 20 | input_shape=(3, 2, 3)) 21 | 22 | 23 | @pytest.mark.skipif((K.backend() == 'cntk'), 24 | reason="cntk does not support it yet") 25 | def test_AlphaDropout(): 26 | layer_test(noise.AlphaDropout, 27 | kwargs={'rate': 0.1}, 28 | input_shape=(3, 2, 3)) 29 | 30 | 31 | if __name__ == '__main__': 32 | pytest.main([__file__]) 33 | -------------------------------------------------------------------------------- /tests/keras/legacy/conftest.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | import pytest 3 | 4 | 5 | @pytest.fixture(autouse=True) 6 | def clear_session_after_test(): 7 | """This wrapper runs for all the tests in the legacy directory (recursively). 8 | """ 9 | with warnings.catch_warnings(): 10 | warnings.filterwarnings('ignore', message=r'(.+) Keras 2 ', 11 | category=UserWarning) 12 | yield 13 | -------------------------------------------------------------------------------- /tests/keras/legacy/layers_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras.utils.test_utils import layer_test 4 | from keras.legacy import layers as legacy_layers 5 | from keras import regularizers 6 | from keras import constraints 7 | 8 | 9 | def test_highway(): 10 | layer_test(legacy_layers.Highway, 11 | kwargs={}, 12 | input_shape=(3, 2)) 13 | 14 | layer_test(legacy_layers.Highway, 15 | kwargs={'W_regularizer': regularizers.l2(0.01), 16 | 'b_regularizer': regularizers.l1(0.01), 17 | 'activity_regularizer': regularizers.l2(0.01), 18 | 'W_constraint': constraints.MaxNorm(1), 19 | 'b_constraint': constraints.MaxNorm(1)}, 20 | input_shape=(3, 2)) 21 | 22 | 23 | def test_maxout_dense(): 24 | layer_test(legacy_layers.MaxoutDense, 25 | kwargs={'output_dim': 3}, 26 | input_shape=(3, 2)) 27 | 28 | layer_test(legacy_layers.MaxoutDense, 29 | kwargs={'output_dim': 3, 30 | 'W_regularizer': regularizers.l2(0.01), 31 | 'b_regularizer': regularizers.l1(0.01), 32 | 'activity_regularizer': regularizers.l2(0.01), 33 | 'W_constraint': constraints.MaxNorm(1), 34 | 'b_constraint': constraints.MaxNorm(1)}, 35 | input_shape=(3, 2)) 36 | 37 | 38 | if __name__ == '__main__': 39 | pytest.main([__file__]) 40 | -------------------------------------------------------------------------------- /tests/keras/metrics_training_test.py: -------------------------------------------------------------------------------- 1 | """Tests for metric objects training and evaluation.""" 2 | import pytest 3 | import numpy as np 4 | 5 | from keras import metrics 6 | from keras import backend as K 7 | from keras.layers import Dense 8 | from keras.models import Sequential 9 | 10 | 11 | if K.backend() == 'cntk': 12 | pytestmark = pytest.mark.skip 13 | 14 | 15 | METRICS = [ 16 | metrics.Accuracy, 17 | metrics.MeanSquaredError, 18 | metrics.Hinge, 19 | metrics.CategoricalHinge, 20 | metrics.SquaredHinge, 21 | metrics.FalsePositives, 22 | metrics.TruePositives, 23 | metrics.FalseNegatives, 24 | metrics.TrueNegatives, 25 | metrics.BinaryAccuracy, 26 | metrics.CategoricalAccuracy, 27 | metrics.TopKCategoricalAccuracy, 28 | metrics.LogCoshError, 29 | metrics.Poisson, 30 | metrics.KLDivergence, 31 | metrics.CosineSimilarity, 32 | metrics.MeanAbsoluteError, 33 | metrics.MeanAbsolutePercentageError, 34 | metrics.MeanSquaredError, 35 | metrics.MeanSquaredLogarithmicError, 36 | metrics.RootMeanSquaredError, 37 | metrics.BinaryCrossentropy, 38 | metrics.CategoricalCrossentropy, 39 | metrics.Precision, 40 | metrics.Recall, 41 | metrics.AUC, 42 | ] 43 | SPARSE_METRICS = [ 44 | metrics.SparseCategoricalAccuracy, 45 | metrics.SparseTopKCategoricalAccuracy, 46 | metrics.SparseCategoricalCrossentropy 47 | ] 48 | 49 | 50 | @pytest.mark.parametrize('metric_cls', METRICS) 51 | def test_training_and_eval(metric_cls): 52 | model = Sequential([Dense(2, input_shape=(3,))]) 53 | model.compile('rmsprop', 'mse', metrics=[metric_cls()]) 54 | x = np.random.random((10, 3)) 55 | y = np.random.random((10, 2)) 56 | model.fit(x, y) 57 | model.evaluate(x, y) 58 | 59 | 60 | @pytest.mark.parametrize('metric_cls', SPARSE_METRICS) 61 | def test_sparse_metrics(metric_cls): 62 | model = Sequential([Dense(1, input_shape=(3,))]) 63 | model.compile('rmsprop', 'mse', metrics=[metric_cls()]) 64 | x = np.random.random((10, 3)) 65 | y = np.random.random((10,)) 66 | model.fit(x, y) 67 | model.evaluate(x, y) 68 | 69 | 70 | def test_sensitivity_metrics(): 71 | metrics_list = [ 72 | metrics.SensitivityAtSpecificity(0.5), 73 | metrics.SpecificityAtSensitivity(0.5), 74 | ] 75 | model = Sequential([Dense(2, input_shape=(3,))]) 76 | model.compile('rmsprop', 'mse', metrics=metrics_list) 77 | x = np.random.random((10, 3)) 78 | y = np.random.random((10, 2)) 79 | model.fit(x, y) 80 | model.evaluate(x, y) 81 | 82 | 83 | @pytest.mark.skipif(True, reason='It is a flaky test, see #13477 for more context.') 84 | def test_mean_iou(): 85 | import tensorflow as tf 86 | if not tf.__version__.startswith('2.'): 87 | return 88 | 89 | model = Sequential([Dense(1, input_shape=(3,))]) 90 | model.compile('rmsprop', 'mse', metrics=[metrics.MeanIoU(2)]) 91 | x = np.random.random((10, 3)) 92 | y = np.random.random((10,)) 93 | model.fit(x, y) 94 | model.evaluate(x, y) 95 | -------------------------------------------------------------------------------- /tests/keras/regularizers_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras.models import Sequential, Model 4 | from keras.layers import Dense, Input, Average 5 | from keras.utils import np_utils 6 | from keras.utils import test_utils 7 | from keras import regularizers 8 | from keras import backend as K 9 | 10 | data_dim = 5 11 | num_classes = 2 12 | batch_size = 10 13 | 14 | 15 | def get_data(): 16 | (x_train, y_train), _ = test_utils.get_test_data( 17 | num_train=batch_size, 18 | num_test=batch_size, 19 | input_shape=(data_dim,), 20 | classification=True, 21 | num_classes=num_classes) 22 | y_train = np_utils.to_categorical(y_train, num_classes) 23 | 24 | return x_train, y_train 25 | 26 | 27 | def create_model(kernel_regularizer=None, activity_regularizer=None): 28 | model = Sequential() 29 | model.add(Dense(num_classes, 30 | kernel_regularizer=kernel_regularizer, 31 | activity_regularizer=activity_regularizer, 32 | input_shape=(data_dim,))) 33 | return model 34 | 35 | 36 | def create_multi_input_model_from(layer1, layer2): 37 | input_1 = Input(shape=(data_dim,)) 38 | input_2 = Input(shape=(data_dim,)) 39 | out1 = layer1(input_1) 40 | out2 = layer2(input_2) 41 | out = Average()([out1, out2]) 42 | model = Model([input_1, input_2], out) 43 | model.add_loss(K.mean(out2)) 44 | model.add_loss(1) 45 | model.add_loss(1) 46 | return model 47 | 48 | 49 | def test_kernel_regularization(): 50 | x_train, y_train = get_data() 51 | for reg in [regularizers.l1(), 52 | regularizers.l2(), 53 | regularizers.l1_l2()]: 54 | model = create_model(kernel_regularizer=reg) 55 | model.compile(loss='categorical_crossentropy', optimizer='sgd') 56 | assert len(model.losses) == 1 57 | model.train_on_batch(x_train, y_train) 58 | 59 | 60 | def test_activity_regularization(): 61 | x_train, y_train = get_data() 62 | for reg in [regularizers.l1(), regularizers.l2()]: 63 | model = create_model(activity_regularizer=reg) 64 | model.compile(loss='categorical_crossentropy', optimizer='sgd') 65 | assert len(model.losses) == 1 66 | model.train_on_batch(x_train, y_train) 67 | 68 | 69 | def test_regularization_shared_layer(): 70 | dense_layer = Dense(num_classes, 71 | kernel_regularizer=regularizers.l1(), 72 | activity_regularizer=regularizers.l1()) 73 | 74 | model = create_multi_input_model_from(dense_layer, dense_layer) 75 | model.compile(loss='categorical_crossentropy', optimizer='sgd') 76 | assert len(model.losses) == 6 77 | 78 | 79 | def test_regularization_shared_model(): 80 | dense_layer = Dense(num_classes, 81 | kernel_regularizer=regularizers.l1(), 82 | activity_regularizer=regularizers.l1()) 83 | 84 | input_tensor = Input(shape=(data_dim,)) 85 | dummy_model = Model(input_tensor, dense_layer(input_tensor)) 86 | 87 | model = create_multi_input_model_from(dummy_model, dummy_model) 88 | model.compile(loss='categorical_crossentropy', optimizer='sgd') 89 | assert len(model.losses) == 6 90 | 91 | 92 | def test_regularization_shared_layer_in_different_models(): 93 | shared_dense = Dense(num_classes, 94 | kernel_regularizer=regularizers.l1(), 95 | activity_regularizer=regularizers.l1()) 96 | models = [] 97 | for _ in range(2): 98 | input_tensor = Input(shape=(data_dim,)) 99 | unshared_dense = Dense(num_classes, kernel_regularizer=regularizers.l1()) 100 | out = unshared_dense(shared_dense(input_tensor)) 101 | models.append(Model(input_tensor, out)) 102 | 103 | model = create_multi_input_model_from(*models) 104 | model.compile(loss='categorical_crossentropy', optimizer='sgd') 105 | assert len(model.losses) == 8 106 | 107 | 108 | if __name__ == '__main__': 109 | pytest.main([__file__]) 110 | -------------------------------------------------------------------------------- /tests/keras/utils/conv_utils_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy as np 3 | from keras.utils import conv_utils 4 | from keras import backend as K 5 | 6 | 7 | def test_normalize_tuple(): 8 | assert conv_utils.normalize_tuple(5, 2, 'kernel_size') == (5, 5) 9 | assert conv_utils.normalize_tuple([7, 9], 2, 'kernel_size') == (7, 9) 10 | 11 | with pytest.raises(ValueError): 12 | conv_utils.normalize_tuple(None, 2, 'kernel_size') 13 | with pytest.raises(ValueError): 14 | conv_utils.normalize_tuple([2, 3, 4], 2, 'kernel_size') 15 | with pytest.raises(ValueError): 16 | conv_utils.normalize_tuple(['str', 'impossible'], 2, 'kernel_size') 17 | 18 | 19 | def test_invalid_data_format(): 20 | with pytest.raises(ValueError): 21 | K.normalize_data_format('channels_middle') 22 | 23 | 24 | def test_invalid_padding(): 25 | with pytest.raises(ValueError): 26 | conv_utils.normalize_padding('diagonal') 27 | 28 | 29 | def test_invalid_convert_kernel(): 30 | with pytest.raises(ValueError): 31 | conv_utils.convert_kernel(np.zeros((10, 20))) 32 | 33 | 34 | def test_conv_output_length(): 35 | assert conv_utils.conv_output_length(None, 7, 'same', 1) is None 36 | assert conv_utils.conv_output_length(224, 7, 'same', 1) == 224 37 | assert conv_utils.conv_output_length(224, 7, 'same', 2) == 112 38 | assert conv_utils.conv_output_length(32, 5, 'valid', 1) == 28 39 | assert conv_utils.conv_output_length(32, 5, 'valid', 2) == 14 40 | assert conv_utils.conv_output_length(32, 5, 'causal', 1) == 32 41 | assert conv_utils.conv_output_length(32, 5, 'causal', 2) == 16 42 | assert conv_utils.conv_output_length(32, 5, 'full', 1) == 36 43 | assert conv_utils.conv_output_length(32, 5, 'full', 2) == 18 44 | 45 | with pytest.raises(AssertionError): 46 | conv_utils.conv_output_length(32, 5, 'diagonal', 2) 47 | 48 | 49 | def test_conv_input_length(): 50 | assert conv_utils.conv_input_length(None, 7, 'same', 1) is None 51 | assert conv_utils.conv_input_length(112, 7, 'same', 1) == 112 52 | assert conv_utils.conv_input_length(112, 7, 'same', 2) == 223 53 | assert conv_utils.conv_input_length(28, 5, 'valid', 1) == 32 54 | assert conv_utils.conv_input_length(14, 5, 'valid', 2) == 31 55 | assert conv_utils.conv_input_length(36, 5, 'full', 1) == 32 56 | assert conv_utils.conv_input_length(18, 5, 'full', 2) == 31 57 | 58 | with pytest.raises(AssertionError): 59 | conv_utils.conv_output_length(18, 5, 'diagonal', 2) 60 | 61 | 62 | def test_deconv_length(): 63 | assert conv_utils.deconv_length(None, 1, 7, 'same', None) is None 64 | assert conv_utils.deconv_length(224, 1, 7, 'same', None) == 224 65 | assert conv_utils.deconv_length(224, 2, 7, 'same', None) == 448 66 | assert conv_utils.deconv_length(32, 1, 5, 'valid', None) == 36 67 | assert conv_utils.deconv_length(32, 2, 5, 'valid', None) == 67 68 | assert conv_utils.deconv_length(32, 1, 5, 'full', None) == 28 69 | assert conv_utils.deconv_length(32, 2, 5, 'full', None) == 59 70 | assert conv_utils.deconv_length(224, 1, 7, 'same', 0) == 224 71 | assert conv_utils.deconv_length(224, 2, 7, 'same', 0) == 447 72 | assert conv_utils.deconv_length(224, 2, 7, 'same', 1) == 448 73 | assert conv_utils.deconv_length(32, 1, 5, 'valid', 0) == 36 74 | assert conv_utils.deconv_length(32, 2, 5, 'valid', 0) == 67 75 | assert conv_utils.deconv_length(32, 2, 5, 'valid', 1) == 68 76 | assert conv_utils.deconv_length(6, 1, 3, 'full', 0) == 4 77 | assert conv_utils.deconv_length(6, 2, 3, 'full', 1) == 10 78 | assert conv_utils.deconv_length(6, 2, 3, 'full', 2) == 11 79 | 80 | 81 | if __name__ == '__main__': 82 | pytest.main([__file__]) 83 | -------------------------------------------------------------------------------- /tests/keras/utils/layer_utils_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy as np 3 | from numpy.testing import assert_allclose 4 | from keras import backend as K 5 | from keras.layers import Conv2D 6 | from keras.layers import Dense 7 | from keras.layers import Flatten 8 | from keras.models import Sequential 9 | from keras.utils import layer_utils 10 | 11 | 12 | def test_convert_weights(): 13 | def get_model(shape, data_format): 14 | model = Sequential() 15 | model.add(Conv2D(filters=2, 16 | kernel_size=(4, 3), 17 | input_shape=shape, 18 | data_format=data_format)) 19 | model.add(Flatten()) 20 | model.add(Dense(5)) 21 | return model 22 | 23 | for data_format in ['channels_first', 'channels_last']: 24 | if data_format == 'channels_first': 25 | shape = (3, 5, 5) 26 | target_shape = (5, 5, 3) 27 | prev_shape = (2, 3, 2) 28 | flip = lambda x: np.flip(np.flip(x, axis=2), axis=3) 29 | transpose = lambda x: np.transpose(x, (0, 2, 3, 1)) 30 | target_data_format = 'channels_last' 31 | elif data_format == 'channels_last': 32 | shape = (5, 5, 3) 33 | target_shape = (3, 5, 5) 34 | prev_shape = (2, 2, 3) 35 | flip = lambda x: np.flip(np.flip(x, axis=1), axis=2) 36 | transpose = lambda x: np.transpose(x, (0, 3, 1, 2)) 37 | target_data_format = 'channels_first' 38 | 39 | model1 = get_model(shape, data_format) 40 | model2 = get_model(target_shape, target_data_format) 41 | conv = K.function([model1.input], [model1.layers[0].output]) 42 | 43 | x = np.random.random((1,) + shape) 44 | 45 | # Test equivalence of convert_all_kernels_in_model 46 | convout1 = conv([x])[0] 47 | layer_utils.convert_all_kernels_in_model(model1) 48 | convout2 = flip(conv([flip(x)])[0]) 49 | 50 | assert_allclose(convout1, convout2, atol=1e-5) 51 | 52 | # Test equivalence of convert_dense_weights_data_format 53 | out1 = model1.predict(x) 54 | layer_utils.convert_dense_weights_data_format( 55 | model1.layers[2], prev_shape, target_data_format) 56 | for (src, dst) in zip(model1.layers, model2.layers): 57 | dst.set_weights(src.get_weights()) 58 | out2 = model2.predict(transpose(x)) 59 | 60 | assert_allclose(out1, out2, atol=1e-5) 61 | 62 | 63 | if __name__ == '__main__': 64 | pytest.main([__file__]) 65 | -------------------------------------------------------------------------------- /tests/keras/utils/np_utils_test.py: -------------------------------------------------------------------------------- 1 | """Tests for functions in np_utils.py. 2 | """ 3 | import numpy as np 4 | import pytest 5 | from keras.utils import to_categorical 6 | 7 | 8 | def test_to_categorical(): 9 | num_classes = 5 10 | shapes = [(1,), (3,), (4, 3), (5, 4, 3), (3, 1), (3, 2, 1)] 11 | expected_shapes = [(1, num_classes), 12 | (3, num_classes), 13 | (4, 3, num_classes), 14 | (5, 4, 3, num_classes), 15 | (3, num_classes), 16 | (3, 2, num_classes)] 17 | labels = [np.random.randint(0, num_classes, shape) for shape in shapes] 18 | one_hots = [to_categorical(label, num_classes) for label in labels] 19 | for label, one_hot, expected_shape in zip(labels, 20 | one_hots, 21 | expected_shapes): 22 | # Check shape 23 | assert one_hot.shape == expected_shape 24 | # Make sure there are only 0s and 1s 25 | assert np.array_equal(one_hot, one_hot.astype(bool)) 26 | # Make sure there is exactly one 1 in a row 27 | assert np.all(one_hot.sum(axis=-1) == 1) 28 | # Get original labels back from one hots 29 | assert np.all(np.argmax(one_hot, -1).reshape(label.shape) == label) 30 | 31 | 32 | if __name__ == '__main__': 33 | pytest.main([__file__]) 34 | -------------------------------------------------------------------------------- /tests/keras/utils/vis_utils_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import os 3 | import sys 4 | import numpy as np 5 | from keras import Input, Model 6 | 7 | from keras.layers import Conv2D, Bidirectional 8 | from keras.layers import Dense 9 | from keras.layers import Embedding 10 | from keras.layers import Flatten 11 | from keras.layers import LSTM 12 | from keras.layers import TimeDistributed 13 | from keras.models import Sequential 14 | from keras.utils import vis_utils 15 | 16 | 17 | def test_plot_model(): 18 | model = Sequential() 19 | model.add(Conv2D(2, kernel_size=(2, 3), input_shape=(3, 5, 5), name='conv')) 20 | model.add(Flatten(name='flat')) 21 | model.add(Dense(5, name='dense1')) 22 | vis_utils.plot_model(model, to_file='model1.png', show_layer_names=False) 23 | os.remove('model1.png') 24 | 25 | model = Sequential() 26 | model.add(LSTM(16, return_sequences=True, input_shape=(2, 3), name='lstm')) 27 | model.add(TimeDistributed(Dense(5, name='dense2'))) 28 | vis_utils.plot_model(model, to_file='model2.png', show_shapes=True) 29 | os.remove('model2.png') 30 | 31 | inner_input = Input(shape=(2, 3), dtype='float32', name='inner_input') 32 | inner_lstm = Bidirectional(LSTM(16, name='inner_lstm'), name='bd')(inner_input) 33 | encoder = Model(inner_input, inner_lstm, name='Encoder_Model') 34 | outer_input = Input(shape=(5, 2, 3), dtype='float32', name='input') 35 | inner_encoder = TimeDistributed(encoder, name='td_encoder')(outer_input) 36 | lstm = LSTM(16, name='outer_lstm')(inner_encoder) 37 | preds = Dense(5, activation='softmax', name='predictions')(lstm) 38 | model = Model(outer_input, preds) 39 | vis_utils.plot_model(model, to_file='model3.png', show_shapes=True, 40 | expand_nested=True, dpi=300) 41 | os.remove('model3.png') 42 | 43 | 44 | def test_plot_sequential_embedding(): 45 | """Fixes #11376""" 46 | model = Sequential() 47 | model.add(Embedding(10000, 256, input_length=400, name='embed')) 48 | vis_utils.plot_model(model, 49 | to_file='model1.png', 50 | show_shapes=True, 51 | show_layer_names=True) 52 | os.remove('model1.png') 53 | 54 | 55 | if __name__ == '__main__': 56 | pytest.main([__file__]) 57 | -------------------------------------------------------------------------------- /tests/test_api.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import pyux 3 | import keras 4 | import json 5 | import os 6 | 7 | import keras.backend.tensorflow_backend 8 | import keras.backend.theano_backend 9 | # import keras.backend.cntk_backend 10 | import keras.backend.numpy_backend 11 | import keras.utils.test_utils 12 | 13 | 14 | def test_api(): 15 | api_file = os.path.join(os.getcwd(), 'api.json') 16 | with open(api_file, 'r') as f: 17 | previous_api = json.load(f) 18 | current_api = pyux.sign(keras) 19 | diff = pyux.diff(current_api, previous_api) 20 | 21 | exceptions = [ 22 | pyux.ADDED_ARG_WITH_DEFAULT_IN_METHOD, 23 | pyux.ADDED_DEFAULT_IN_METHOD 24 | ] 25 | 26 | diff = list(filter(lambda c: c[0] not in exceptions, diff)) 27 | if diff: 28 | raise pyux.APIChangedException(diff) 29 | 30 | 31 | if __name__ == '__main__': 32 | test_api() 33 | -------------------------------------------------------------------------------- /tests/test_dynamic_trainability.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import print_function 3 | import pytest 4 | 5 | from keras.models import Model, Sequential 6 | from keras.layers import Dense, Input 7 | 8 | 9 | def test_layer_trainability_switch(): 10 | # with constructor argument, in Sequential 11 | model = Sequential() 12 | model.add(Dense(2, trainable=False, input_dim=1)) 13 | assert model.trainable_weights == [] 14 | 15 | # by setting the `trainable` argument, in Sequential 16 | model = Sequential() 17 | layer = Dense(2, input_dim=1) 18 | model.add(layer) 19 | assert model.trainable_weights == layer.trainable_weights 20 | layer.trainable = False 21 | assert model.trainable_weights == [] 22 | 23 | # with constructor argument, in Model 24 | x = Input(shape=(1,)) 25 | y = Dense(2, trainable=False)(x) 26 | model = Model(x, y) 27 | assert model.trainable_weights == [] 28 | 29 | # by setting the `trainable` argument, in Model 30 | x = Input(shape=(1,)) 31 | layer = Dense(2) 32 | y = layer(x) 33 | model = Model(x, y) 34 | assert model.trainable_weights == layer.trainable_weights 35 | layer.trainable = False 36 | assert model.trainable_weights == [] 37 | 38 | 39 | def test_model_trainability_switch(): 40 | # a non-trainable model has no trainable weights 41 | x = Input(shape=(1,)) 42 | y = Dense(2)(x) 43 | model = Model(x, y) 44 | model.trainable = False 45 | assert model.trainable_weights == [] 46 | 47 | # same for Sequential 48 | model = Sequential() 49 | model.add(Dense(2, input_dim=1)) 50 | model.trainable = False 51 | assert model.trainable_weights == [] 52 | 53 | 54 | def test_nested_model_trainability(): 55 | # a Sequential inside a Model 56 | inner_model = Sequential() 57 | inner_model.add(Dense(2, input_dim=1)) 58 | 59 | x = Input(shape=(1,)) 60 | y = inner_model(x) 61 | outer_model = Model(x, y) 62 | assert outer_model.trainable_weights == inner_model.trainable_weights 63 | inner_model.trainable = False 64 | assert outer_model.trainable_weights == [] 65 | inner_model.trainable = True 66 | inner_model.layers[-1].trainable = False 67 | assert outer_model.trainable_weights == [] 68 | 69 | # a Sequential inside a Sequential 70 | inner_model = Sequential() 71 | inner_model.add(Dense(2, input_dim=1)) 72 | outer_model = Sequential() 73 | outer_model.add(inner_model) 74 | assert outer_model.trainable_weights == inner_model.trainable_weights 75 | inner_model.trainable = False 76 | assert outer_model.trainable_weights == [] 77 | inner_model.trainable = True 78 | inner_model.layers[-1].trainable = False 79 | assert outer_model.trainable_weights == [] 80 | 81 | # a Model inside a Model 82 | x = Input(shape=(1,)) 83 | y = Dense(2)(x) 84 | inner_model = Model(x, y) 85 | x = Input(shape=(1,)) 86 | y = inner_model(x) 87 | outer_model = Model(x, y) 88 | assert outer_model.trainable_weights == inner_model.trainable_weights 89 | inner_model.trainable = False 90 | assert outer_model.trainable_weights == [] 91 | inner_model.trainable = True 92 | inner_model.layers[-1].trainable = False 93 | assert outer_model.trainable_weights == [] 94 | 95 | # a Model inside a Sequential 96 | x = Input(shape=(1,)) 97 | y = Dense(2)(x) 98 | inner_model = Model(x, y) 99 | outer_model = Sequential() 100 | outer_model.add(inner_model) 101 | assert outer_model.trainable_weights == inner_model.trainable_weights 102 | inner_model.trainable = False 103 | assert outer_model.trainable_weights == [] 104 | inner_model.trainable = True 105 | inner_model.layers[-1].trainable = False 106 | assert outer_model.trainable_weights == [] 107 | 108 | 109 | if __name__ == '__main__': 110 | pytest.main([__file__]) 111 | -------------------------------------------------------------------------------- /tests/test_loss_masking.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pytest 3 | 4 | from keras.models import Sequential 5 | from keras.engine.training_utils import weighted_masked_objective 6 | from keras.layers import TimeDistributed, Masking, Dense 7 | from keras import losses 8 | from keras import backend as K 9 | 10 | 11 | def create_masking_model(): 12 | model = Sequential() 13 | model.add(Masking(mask_value=0, input_shape=(None, 1))) 14 | model.add(TimeDistributed(Dense(1, kernel_initializer='one'))) 15 | model.compile(loss='mse', optimizer='sgd') 16 | return model 17 | 18 | 19 | def test_masking(): 20 | np.random.seed(1337) 21 | x = np.array([[[1], [1]], 22 | [[0], [0]]]) 23 | model = create_masking_model() 24 | y = np.array([[[1], [1]], 25 | [[1], [1]]]) 26 | loss = model.train_on_batch(x, y) 27 | assert loss == 0 28 | 29 | 30 | def test_masking_is_all_zeros(): 31 | x = y = np.array([[[0], [0]]]) 32 | model = create_masking_model() 33 | loss = model.train_on_batch(x, y) 34 | assert loss == 0 35 | 36 | 37 | def test_loss_masking(): 38 | weighted_loss = weighted_masked_objective(losses.get('mae')) 39 | shape = (3, 4, 2) 40 | x = np.arange(24).reshape(shape) 41 | y = 2 * x 42 | 43 | # Normally the trailing 1 is added by standardize_weights 44 | weights = np.ones((3,)) 45 | mask = np.ones((3, 4)) 46 | mask[1, 0] = 0 47 | 48 | out = K.eval(weighted_loss(K.variable(x), 49 | K.variable(y), 50 | K.variable(weights), 51 | K.variable(mask))) 52 | 53 | 54 | if __name__ == '__main__': 55 | pytest.main([__file__]) 56 | -------------------------------------------------------------------------------- /update_api.py: -------------------------------------------------------------------------------- 1 | import pyux 2 | import keras 3 | import json 4 | 5 | 6 | import keras.backend.tensorflow_backend 7 | import keras.backend.theano_backend 8 | # import keras.backend.cntk_backend 9 | import keras.backend.numpy_backend 10 | import keras.utils.test_utils 11 | 12 | sign = pyux.sign(keras) 13 | 14 | with open('api.json', 'w') as f: 15 | json.dump(sign, f) 16 | --------------------------------------------------------------------------------