├── .dockerignore ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md ├── changelog.yml └── stale.yml ├── .gitignore ├── .pyup.yml ├── .readthedocs.yaml ├── LICENSE.rst ├── README.md ├── README.rst ├── countlines.py ├── docker ├── DockerLint.bat ├── Dockerfile ├── docs │ ├── Dockerfile │ └── sources.list.ustc ├── pypi_list.py └── version_prefix.py ├── docs ├── Makefile ├── _static │ └── fix_rtd.css ├── conf.py ├── images │ ├── affine_transform_comparison.jpg │ └── affine_transform_why.jpg ├── index.rst ├── make.bat ├── modules │ ├── activation.rst │ ├── dataflow.rst │ ├── files.rst │ ├── initializers.rst │ ├── iterate.rst │ ├── losses.rst │ ├── metrics.rst │ ├── model.rst │ ├── nlp.rst │ ├── nn.rst │ ├── ops.rst │ ├── optimizers.rst │ ├── pretrain_models.rst │ ├── vision.rst │ └── visualize.rst ├── tutorials │ ├── cifar10_cnn.md │ ├── data_processing.md │ ├── dataset.md │ ├── images │ │ ├── Structure-of-the-LSTM-cell-and-equations-that-describe-the-gates-of-an-LSTM-cell.png │ │ ├── dataset_mnist.png │ │ ├── fake_mnist_1.png │ │ ├── fake_mnist_final.png │ │ ├── flower_centercrop.png │ │ ├── flower_color.png │ │ ├── flower_flip.png │ │ ├── gan_loss.gif │ │ └── lstm.png │ ├── lstm_imdb.md │ ├── mnist_gan.md │ └── models.md └── user │ ├── contributing.rst │ ├── examples.rst │ ├── faq.rst │ ├── get_start_advance.rst │ ├── get_start_model.rst │ ├── installation.rst │ └── my_figs │ ├── basic_seq2seq.png │ ├── join_slack.png │ ├── karpathy_rnn.jpeg │ ├── mnist.jpeg │ ├── pong_game.jpeg │ ├── seq2seq.png │ ├── tlx_transparent_logo.png │ ├── tsne.png │ └── word2vec_basic.pdf ├── examples ├── basic_tutorials │ ├── README.md │ ├── automatic_inference_input_shape.py │ ├── cifar10_cnn.py │ ├── cifar10_cnn_dist.py │ ├── cifar10_cnn_torch_dist.py │ ├── cifar10_cnn_train.py │ ├── flower_demo.png │ ├── gradient_clip_mixed_tensorflow.py │ ├── imdb_lstm_simple.py │ ├── jiitor_models_tutorial.py │ ├── load_paddle_parameters_to_tensorlayerx.py │ ├── load_pytorch_parameters_to_tensorlayerx.py │ ├── mnist_dataflow.py │ ├── mnist_gan.py │ ├── mnist_mlp_custom_train.py │ ├── mnist_mlp_mix_programming.py │ ├── mnist_mlp_simple_train.py │ ├── mnist_sequential.py │ ├── module_container.py │ ├── nested_usage_of_layer.py │ ├── parameter_container.py │ ├── quick_start.py │ ├── tensorflow_model_save_to_pb.py │ ├── tensorlayerx_graph.py │ ├── tensorlayerx_model_load.py │ ├── test_dist.sh │ └── using_tensorboardx.py └── model_zoo │ ├── __init__.py │ ├── common.py │ ├── imagenet_classes.py │ ├── model │ ├── coco.names │ ├── resnet50_weights_tf_dim_ordering_tf_kernels.h5 │ ├── weights_2.txt │ ├── weights_3.txt │ ├── yolov4_weights3_config.txt │ └── yolov4_weights_config.txt │ ├── pretrained_resnet50.py │ ├── pretrained_vgg16.py │ ├── pretrained_yolov4.py │ ├── resnet.py │ ├── vgg.py │ └── yolo.py ├── requirements ├── requirements.txt ├── requirements_contrib_loggers.txt ├── requirements_db.txt ├── requirements_dev.txt ├── requirements_doc.txt ├── requirements_extra.txt ├── requirements_ms.txt ├── requirements_paddle.txt ├── requirements_test.txt ├── requirements_tf_cpu.txt ├── requirements_tf_gpu.txt └── requirements_torch.txt ├── runs └── mlp │ └── events.out.tfevents.1722986988.LAPTOP-48J7839G ├── setup.cfg ├── setup.py ├── tensorlayerx ├── __init__.py ├── backend │ ├── __init__.py │ └── ops │ │ ├── __init__.py │ │ ├── jittor_backend.py │ │ ├── jittor_nn.py │ │ ├── load_backend.py │ │ ├── mindspore_backend.py │ │ ├── mindspore_nn.py │ │ ├── oneflow_backend.py │ │ ├── oneflow_nn.py │ │ ├── paddle_backend.py │ │ ├── paddle_nn.py │ │ ├── tensorflow_backend.py │ │ ├── tensorflow_nn.py │ │ ├── torch_backend.py │ │ └── torch_nn.py ├── dataflow │ ├── __init__.py │ ├── dataloader.py │ ├── dataset.py │ ├── sampler.py │ └── utils.py ├── decorators │ ├── __init__.py │ ├── deprecated.py │ ├── deprecated_alias.py │ ├── method_decorator.py │ └── utils.py ├── distributed │ ├── __init__.py │ └── launch.py ├── files │ ├── __init__.py │ ├── dataset_loaders │ │ ├── __init__.py │ │ ├── celebA_dataset.py │ │ ├── cifar10_dataset.py │ │ ├── cyclegan_dataset.py │ │ ├── flickr_1M_dataset.py │ │ ├── flickr_25k_dataset.py │ │ ├── imdb_dataset.py │ │ ├── matt_mahoney_dataset.py │ │ ├── mnist_dataset.py │ │ ├── mnist_fashion_dataset.py │ │ ├── mnist_utils.py │ │ ├── mpii_dataset.py │ │ ├── nietzsche_dataset.py │ │ ├── ptb_dataset.py │ │ ├── voc_dataset.py │ │ └── wmt_en_fr_dataset.py │ └── utils.py ├── logging │ ├── __init__.py │ ├── contrib │ │ ├── __init__.py │ │ └── hyperdash.py │ └── tl_logging.py ├── losses │ ├── __init__.py │ ├── jittor_cost.py │ ├── mindspore_cost.py │ ├── oneflow_cost.py │ ├── paddle_cost.py │ ├── tensorflow_cost.py │ └── torch_cost.py ├── metrics │ ├── __init__.py │ ├── jittor_metric.py │ ├── mindspore_metric.py │ ├── oneflow_metric.py │ ├── paddle_metric.py │ ├── tensorflow_metric.py │ └── torch_metric.py ├── model │ ├── __init__.py │ ├── core.py │ └── utils.py ├── nn │ ├── __init__.py │ ├── core │ │ ├── __init__.py │ │ ├── common.py │ │ ├── core_jittor.py │ │ ├── core_mindspore.py │ │ ├── core_oneflow.py │ │ ├── core_paddle.py │ │ ├── core_tensorflow.py │ │ └── core_torch.py │ ├── initializers │ │ ├── __init__.py │ │ ├── jittor_initializers.py │ │ ├── load_initializers_backend.py │ │ ├── mindspore_initializers.py │ │ ├── oneflow_initializers.py │ │ ├── paddle_initializers.py │ │ ├── tensorflow_initializers.py │ │ └── torch_initializers.py │ └── layers │ │ ├── Transformer.py │ │ ├── __init__.py │ │ ├── activation.py │ │ ├── convolution │ │ ├── __init__.py │ │ ├── binary_conv.py │ │ ├── deformable_conv.py │ │ ├── depthwise_conv.py │ │ ├── dorefa_conv.py │ │ ├── group_conv.py │ │ ├── mask_conv.py │ │ ├── quan_conv.py │ │ ├── quan_conv_bn.py │ │ ├── separable_conv.py │ │ ├── simplified_conv.py │ │ ├── super_resolution.py │ │ └── ternary_conv.py │ │ ├── deprecated.py │ │ ├── dropout.py │ │ ├── embedding.py │ │ ├── extend.py │ │ ├── fold.py │ │ ├── image_resampling.py │ │ ├── inputs.py │ │ ├── lambda_layers.py │ │ ├── linear │ │ ├── __init__.py │ │ ├── base_linear.py │ │ ├── binary_linear.py │ │ ├── dorefa_linear.py │ │ ├── dropconnect.py │ │ ├── quan_linear.py │ │ ├── quan_linear_bn.py │ │ └── ternary_linear.py │ │ ├── merge.py │ │ ├── noise.py │ │ ├── normalization.py │ │ ├── padding.py │ │ ├── pooling.py │ │ ├── recurrent.py │ │ ├── scale.py │ │ ├── shape.py │ │ ├── spatial_transformer.py │ │ ├── stack.py │ │ └── utils.py ├── optimizers │ ├── __init__.py │ ├── amsgrad.py │ ├── jittor_optimizers.py │ ├── load_optimizers_backend.py │ ├── lr │ │ ├── __init__.py │ │ ├── jittor_lr.py │ │ ├── mindspore_lr.py │ │ ├── oneflow_lr.py │ │ ├── paddle_lr.py │ │ ├── tensorflow_lr.py │ │ └── torch_lr.py │ ├── mindspore_optimizers.py │ ├── oneflow_optimizers.py │ ├── paddle_optimizers.py │ ├── tensorflow_optimizers.py │ └── torch_optimizers.py ├── package_info.py ├── text │ ├── __init__.py │ └── nlp.py ├── utils │ ├── __init__.py │ ├── iterate.py │ ├── lazy_imports.py │ ├── prepro.py │ └── visualize.py └── vision │ ├── __init__.py │ ├── ops │ ├── __init__.py │ ├── jittor_ops.py │ ├── mindspore_ops.py │ ├── paddle_ops.py │ ├── tensorflow_ops.py │ └── torch_ops.py │ └── transforms │ ├── __init__.py │ ├── functional.py │ ├── transforms.py │ └── utils.py ├── tests ├── __init__.py ├── dataflow │ ├── __init__.py │ └── test_dataflow_vision.py ├── layers │ ├── __init__.py │ ├── test_layers_activation.py │ ├── test_layers_convolution.py │ ├── test_layers_core_act.py │ ├── test_layers_core_basedense_dropout.py │ ├── test_layers_core_nested.py │ ├── test_layers_deformable_convolution.py │ ├── test_layers_embedding.py │ ├── test_layers_extend.py │ ├── test_layers_lambda.py │ ├── test_layers_linear.py │ ├── test_layers_merge.py │ ├── test_layers_noise.py │ ├── test_layers_normalization.py │ ├── test_layers_padding.py │ ├── test_layers_pooling.py │ ├── test_layers_recurrent.py │ ├── test_layers_resampling.py │ ├── test_layers_scale.py │ ├── test_layers_shape.py │ ├── test_layers_stack.py │ └── test_mindspore.py ├── models │ ├── test_keras_save.py │ └── test_model_core.py ├── performance_test │ └── __init__.py └── utils │ ├── __init__.py │ ├── custom_testcase.py │ ├── list_py_files.py │ └── timeout_utils.py └── tlx /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | .log 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### New Issue Checklist 2 | 3 | - [ ] I have read the [Contribution Guidelines](https://github.com/tensorlayer/tensorlayer/blob/master/CONTRIBUTING.md) 4 | - [ ] I searched for [existing GitHub issues](https://github.com/tensorlayer/tensorlayer/issues) 5 | 6 | ### Issue Description 7 | 8 | [INSERT DESCRIPTION OF THE PROBLEM] 9 | 10 | ### Reproducible Code 11 | 12 | - Which OS are you using ? 13 | - Please provide a reproducible code of your issue. Without any reproducible code, you will probably not receive any help. 14 | 15 | [INSERT CODE HERE] 16 | 17 | ```python 18 | # ======================================================== # 19 | ###### THIS CODE IS AN EXAMPLE, REPLACE WITH YOUR OWN ###### 20 | # ======================================================== # 21 | 22 | import tensorflow as tf 23 | import tensorlayerx as tl 24 | 25 | net_in = tlx.layers.Input((3, 64)) 26 | 27 | net = tlx.nn.Linear(out_features=25, in_features=64, act='relu') 28 | 29 | print("Output Shape:", net(net_in).shape) ### Output Shape: [None, 25] 30 | 31 | # ======================================================== # 32 | ###### THIS CODE IS AN EXAMPLE, REPLACE WITH YOUR OWN ###### 33 | # ======================================================== # 34 | ``` 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 5 | 6 | ### Checklist 7 | - [ ] I've tested that my changes are compatible with the latest version of Tensorflow. 8 | - [ ] I've read the [Contribution Guidelines](https://github.com/tensorlayer/tensorlayer/blob/master/CONTRIBUTING.md) 9 | - [ ] I've updated the documentation if necessary. 10 | 11 | ### Motivation and Context 12 | 13 | 14 | 15 | 16 | ### Description 17 | 18 | -------------------------------------------------------------------------------- /.github/changelog.yml: -------------------------------------------------------------------------------- 1 | filename: CHANGELOG.md 2 | include: 3 | # Folders 4 | - /^.github\// 5 | - /^docs\// 6 | - /^example\// 7 | - /^tensorlayerx\// 8 | - /^tests\// 9 | 10 | # Python Package Files 11 | - setup.py 12 | - setup.cfg 13 | - pytest.ini 14 | 15 | # Dependency Files 16 | - requirements.txt 17 | - requirements_db.txt 18 | - requirements_dev.txt 19 | - requirements_tf_cpu.txt 20 | - requirements_tf_gpu.txt 21 | 22 | # Configuration Files 23 | - .travis.yml 24 | - .readthedocs.yml 25 | - .pyup.yml 26 | 27 | # Docker Files 28 | - Dockerfile 29 | - Dockerfile.gpu 30 | - .dockerignore 31 | 32 | # Text Files 33 | - README.md 34 | - LICENSE.rst 35 | - CHANGELOG.md 36 | - CONTRIBUTING.md 37 | 38 | # Github Files 39 | - .gitignore 40 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 30 3 | 4 | # Number of days of inactivity before a stale issue is closed 5 | daysUntilClose: 15 6 | 7 | # Issues with these labels will never be considered stale 8 | exemptLabels: 9 | - bug 10 | - discussion 11 | - enhancement 12 | - help_wanted 13 | - feature_request 14 | 15 | # Label to use when marking an issue as stale 16 | staleLabel: 17 | - stale 18 | - awaiting_response 19 | 20 | # Comment to post when marking an issue as stale. Set to `false` to disable 21 | markComment: > 22 | This issue has been automatically marked as stale because it has not had 23 | recent activity. It will be closed if no further activity occurs. Thank you 24 | for your contributions. 25 | 26 | # Comment to post when closing a stale issue. Set to `false` to disable 27 | closeComment: > 28 | This issue has been automatically closed because it has not had 29 | recent activity. Thank you for your contributions. 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | .DS_Store 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | *~ 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | .hypothesis/ 50 | .pytest_cache/ 51 | 52 | # Translations 53 | *.mo 54 | *.pot 55 | 56 | # Django stuff: 57 | *.log 58 | local_settings.py 59 | db.sqlite3 60 | 61 | # Flask stuff: 62 | instance/ 63 | .webassets-cache 64 | 65 | # Scrapy stuff: 66 | .scrapy 67 | 68 | # Sphinx documentation 69 | docs/_build/ 70 | docs/test_build/ 71 | docs/build_test/ 72 | 73 | # PyBuilder 74 | target/ 75 | 76 | # Jupyter Notebook 77 | .ipynb_checkpoints 78 | 79 | # pyenv 80 | .python-version 81 | 82 | # celery beat schedule file 83 | celerybeat-schedule 84 | 85 | # SageMath parsed files 86 | *.sage.py 87 | 88 | # Environments 89 | .env 90 | .venv 91 | env/ 92 | venv/ 93 | ENV/ 94 | env.bak/ 95 | venv.bak/ 96 | venv_/ 97 | venv2/ 98 | venv3/ 99 | venv_doc/ 100 | venv_py2/ 101 | 102 | # Spyder project settings 103 | .spyderproject 104 | .spyproject 105 | 106 | # Rope project settings 107 | .ropeproject 108 | 109 | # mkdocs documentation 110 | /site 111 | 112 | # mypy 113 | .mypy_cache/ 114 | 115 | 116 | # IDE Specific directories 117 | .DS_Store 118 | .idea 119 | .vscode/ 120 | 121 | # TensorLayer Directories 122 | checkpoints 123 | data/ 124 | lib_win/ 125 | 126 | # Custom Scripts 127 | update_tl.bat 128 | update_tl.py 129 | 130 | # Data Files and ByteCode files 131 | *.gz 132 | *.npz 133 | 134 | 135 | #oneflow logs 136 | oneflow.user* 137 | oneflow.INFO -------------------------------------------------------------------------------- /.pyup.yml: -------------------------------------------------------------------------------- 1 | ############################################################################ 2 | # see https://pyup.io/docs/configuration/ for all available options # 3 | ############################################################################ 4 | 5 | # configure updates globally 6 | # default: all 7 | # allowed: all, insecure, False 8 | update: all 9 | 10 | # configure dependency pinning globally 11 | # default: True 12 | # allowed: True, False 13 | pin: False 14 | 15 | # set the default branch 16 | # default: empty, the default branch on GitHub 17 | branch: master 18 | 19 | # update schedule 20 | # default: empty 21 | # allowed: "every day", "every week", .. 22 | schedule: "every day" 23 | 24 | # search for requirement files 25 | # default: True 26 | # allowed: True, False 27 | search: False 28 | 29 | # Specify requirement files by hand, default is empty 30 | # default: empty 31 | # allowed: list 32 | requirements: 33 | # Requirements for the library 34 | - requirements/requirements.txt 35 | 36 | # Requirements for the development 37 | - requirements/requirements_tf_cpu.txt 38 | 39 | # Requirements for the development 40 | - requirements/requirements_tf_gpu.txt 41 | 42 | # Not necessary, but recommended libraries 43 | - requirements/requirements_extra.txt 44 | 45 | # Requirements for the db 46 | - requirements/requirements_db.txt 47 | 48 | # Requirements for the development 49 | - requirements/requirements_dev.txt 50 | 51 | # Requirements for building docs 52 | - requirements/requirements_doc.txt 53 | 54 | # Requirements for running unittests 55 | - requirements/requirements_test.txt 56 | 57 | # add a label to pull requests, default is not set 58 | # requires private repo permissions, even on public repos 59 | # default: empty 60 | #label_prs: update 61 | 62 | # configure the branch prefix the bot is using 63 | # default: pyup- 64 | branch_prefix: pyup- 65 | 66 | # set a global prefix for PRs 67 | # default: empty 68 | pr_prefix: "PyUP - Dependency Update" 69 | 70 | # allow to close stale PRs 71 | # default: True 72 | close_prs: True 73 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # https://docs.readthedocs.io/en/latest/yaml-config.html 2 | # Read the Docs configuration file for Sphinx projects 3 | 4 | version: 2 5 | 6 | build: 7 | os: ubuntu-22.04 8 | tools: 9 | python: "3.7" 10 | 11 | formats: 12 | - epub 13 | - pdf 14 | 15 | python: 16 | install: 17 | - requirements: requirements/requirements_contrib_loggers.txt 18 | - requirements: requirements/requirements.txt 19 | - requirements: requirements/requirements_doc.txt 20 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | |TENSORLAYER-LOGO| 2 | 3 | TensorLayerX is a deep learning library designed for researchers and engineers that is compatible with multiple deep learning frameworks such as TensorFlow, 4 | MindSpore and PaddlePaddle, allowing users to run the code on different hardware like Nvidia-GPU and Huawei-Ascend. 5 | It provides popular DL and RL modules that can be easily customized and assembled for tackling real-world machine learning problems. 6 | More details can be found here. TensorLayerX will support TensorFlow, MindSpore, PaddlePaddle, and PyTorch backends in the future. 7 | 8 | Install 9 | ======= 10 | 11 | TensorLayerX has some prerequisites that need to be installed first, including TensorFlow , 12 | MindSpore, PaddlePaddle,numpy and matplotlib.For GPU support CUDA and cuDNN are required. 13 | 14 | .. code:: bash 15 | 16 | # for last stable version 17 | pip install --upgrade tensorlayerX 18 | 19 | # for latest release candidate 20 | pip install --upgrade --pre tensorlayerX 21 | 22 | # if you want to install the additional dependencies, you can also run 23 | pip install --upgrade tensorlayerX[all] # all additional dependencies 24 | pip install --upgrade tensorlayerX[extra] # only the `extra` dependencies 25 | pip install --upgrade tensorlayerX[contrib_loggers] # only the `contrib_loggers` dependencies 26 | 27 | Alternatively, you can install the latest or development version by directly pulling from OpenI: 28 | 29 | .. code:: bash 30 | 31 | pip3 install git+https://github.com/tensorlayer/TensorLayerX.git 32 | 33 | Containers with CPU support 34 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 35 | 36 | .. code:: bash 37 | 38 | # for CPU version and Python 2 39 | docker pull tensorlayer/tensorlayer:latest 40 | docker run -it --rm -p 8888:8888 -p 6006:6006 -e PASSWORD=JUPYTER_NB_PASSWORD tensorlayer/tensorlayer:latest 41 | 42 | # for CPU version and Python 3 43 | docker pull tensorlayer/tensorlayer:latest-py3 44 | docker run -it --rm -p 8888:8888 -p 6006:6006 -e PASSWORD=JUPYTER_NB_PASSWORD tensorlayer/tensorlayer:latest-py3 45 | 46 | Containers with GPU support 47 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48 | 49 | NVIDIA-Docker is required for these containers to work: `Project 50 | Link `__ 51 | 52 | .. code:: bash 53 | 54 | # for GPU version and Python 2 55 | docker pull tensorlayer/tensorlayer:latest-gpu 56 | nvidia-docker run -it --rm -p 8888:88888 -p 6006:6006 -e PASSWORD=JUPYTER_NB_PASSWORD tensorlayer/tensorlayer:latest-gpu 57 | 58 | # for GPU version and Python 3 59 | docker pull tensorlayer/tensorlayer:latest-gpu-py3 60 | nvidia-docker run -it --rm -p 8888:8888 -p 6006:6006 -e PASSWORD=JUPYTER_NB_PASSWORD tensorlayer/tensorlayer:latest-gpu-py3 61 | 62 | 63 | Cite 64 | ==== 65 | 66 | If you find this project useful, we would be grateful if you cite the 67 | TensorLayer papers. 68 | 69 | :: 70 | 71 | @article{tensorlayer2017, 72 | author = {Dong, Hao and Supratak, Akara and Mai, Luo and Liu, Fangde and Oehmichen, Axel and Yu, Simiao and Guo, Yike}, 73 | journal = {ACM Multimedia}, 74 | title = {{TensorLayer: A Versatile Library for Efficient Deep Learning Development}}, 75 | url = {http://tensorlayer.org}, 76 | year = {2017} 77 | } 78 | @inproceedings{tensorlayer2021, 79 | title={Tensorlayer 3.0: A Deep Learning Library Compatible With Multiple Backends}, 80 | author={Lai, Cheng and Han, Jiarong and Dong, Hao}, 81 | booktitle={2021 IEEE International Conference on Multimedia \& Expo Workshops (ICMEW)}, 82 | pages={1--3}, 83 | year={2021}, 84 | organization={IEEE} 85 | } 86 | 87 | License 88 | ======= 89 | 90 | TensorLayerX is released under the Apache 2.0 license. 91 | 92 | .. |TENSORLAYER-LOGO| image:: https://git.openi.org.cn/hanjr/tensorlayerx-image/raw/branch/master/tlx-LOGO--02.jpg 93 | :target: https://tensorlayerx.readthedocs.io/en/latest/ -------------------------------------------------------------------------------- /countlines.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | def countlines(start, lines=0, header=True, begin_start=None): 4 | if header: 5 | print('{:>10} |{:>10} | {:<20}'.format('ADDED', 'TOTAL', 'FILE')) 6 | print('{:->11}|{:->11}|{:->20}'.format('', '', '')) 7 | 8 | for thing in os.listdir(start): 9 | thing = os.path.join(start, thing) 10 | if os.path.isfile(thing): 11 | if thing.endswith('.py'): 12 | with open(thing, 'r') as f: 13 | newlines = f.readlines() 14 | newlines = len(newlines) 15 | lines += newlines 16 | 17 | if begin_start is not None: 18 | reldir_of_thing = '.' + thing.replace(begin_start, '') 19 | else: 20 | reldir_of_thing = '.' + thing.replace(start, '') 21 | 22 | print('{:>10} |{:>10} | {:<20}'.format( 23 | newlines, lines, reldir_of_thing)) 24 | 25 | for thing in os.listdir(start): 26 | thing = os.path.join(start, thing) 27 | if os.path.isdir(thing): 28 | lines = countlines(thing, lines, header=False, begin_start=start) 29 | 30 | return lines 31 | 32 | if __name__ == '__main__': 33 | countlines('.') 34 | -------------------------------------------------------------------------------- /docker/DockerLint.bat: -------------------------------------------------------------------------------- 1 | docker pull tensorlayer/tensorlayerx:tagname 2 | docker run --rm -i tensorlayer/tensorlayerx hadolint --ignore DL3007 - < Dockerfile 3 | 4 | PAUSE; -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # Build args. 2 | # * Accepted Values: 3 | # - Python 2 + CPU: "latest" => --build-arg TF_CONTAINER_VERSION="latest" 4 | # - Python 2 + GPU: "latest-gpu" => --build-arg TF_CONTAINER_VERSION="latest-gpu" 5 | # - Python 3 + CPU: "latest-py3" => --build-arg TF_CONTAINER_VERSION="latest-py3" 6 | # - Python 3 + GPU: "latest-gpu-py3" => --build-arg TF_CONTAINER_VERSION="latest-gpu-py3" 7 | 8 | ARG TF_CONTAINER_VERSION 9 | 10 | FROM tensorflow/tensorflow:${TF_CONTAINER_VERSION} 11 | 12 | LABEL version="1.0" maintainer="Jonathan DEKHTIAR " 13 | 14 | ARG TL_VERSION 15 | ARG TF_CONTAINER_VERSION 16 | 17 | RUN echo "Container Tag: ${TF_CONTAINER_VERSION}" \ 18 | && apt-get update \ 19 | && case $TF_CONTAINER_VERSION in \ 20 | latest-py3 | latest-gpu-py3) apt-get install -y python3-tk ;; \ 21 | *) apt-get install -y python-tk ;; \ 22 | esac \ 23 | && if [ -z "$TL_VERSION" ]; then \ 24 | echo "Building a Nightly Release" \ 25 | && apt-get install -y git \ 26 | && mkdir /dist/ && cd /dist/ \ 27 | && git clone https://github.com/tensorlayer/tensorlayerx.git \ 28 | && cd tensorlayerx \ 29 | && pip install --disable-pip-version-check --no-cache-dir --upgrade -e .[all]; \ 30 | else \ 31 | echo "Building Tag Release: $TL_VERSION" \ 32 | && pip install --disable-pip-version-check --no-cache-dir --upgrade tensorlayer[all]=="$TL_VERSION"; \ 33 | fi \ 34 | && apt-get autoremove -y \ 35 | && rm -rf /var/lib/apt/lists/* 36 | -------------------------------------------------------------------------------- /docker/docs/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ubuntu:bionic 2 | 3 | ADD docker/docs/sources.list.ustc /etc/apt/sources.list 4 | ENV DEBIAN_FRONTEND=noninteractive 5 | RUN apt update && \ 6 | apt install -y python3-pip python3-tk python-qt4 wget && \ 7 | pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow 8 | ADD . /tensorlayer 9 | WORKDIR /tensorlayer 10 | RUN ln -s `which pip3` /usr/bin/pip && \ 11 | ./scripts/install-horovod-for-doc-test.sh 12 | RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple . 13 | RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -e .[all] 14 | RUN make -C docs html 15 | -------------------------------------------------------------------------------- /docker/docs/sources.list.ustc: -------------------------------------------------------------------------------- 1 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted 2 | 3 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted 4 | 5 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic universe 6 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates universe 7 | 8 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic multiverse 9 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates multiverse 10 | 11 | deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse 12 | 13 | deb http://mirrors.ustc.edu.cn/ubuntu bionic-security main restricted 14 | deb http://mirrors.ustc.edu.cn/ubuntu bionic-security universe 15 | deb http://mirrors.ustc.edu.cn/ubuntu bionic-security multiverse 16 | -------------------------------------------------------------------------------- /docker/pypi_list.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import requests 3 | import logging 4 | 5 | import pip._internal 6 | 7 | if __name__ == "__main__": 8 | 9 | parser = argparse.ArgumentParser(description='Get the nth version of a given package') 10 | parser.add_argument('--package', type=str, required=True, help='The PyPI you want to inspect') 11 | parser.add_argument('--nth_last_version', type=int, default=1, help='The nth last package will be retrieved') 12 | parser.add_argument('--prerelease', help='Get PreRelease Package Version', action='store_true') 13 | parser.add_argument('--debug', help='Print debug information', action='store_true') 14 | 15 | args = parser.parse_args() 16 | 17 | # create logger 18 | logger = logging.getLogger("PyPI_CLI") 19 | 20 | formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') 21 | 22 | ch = logging.StreamHandler() 23 | ch.setLevel(logging.DEBUG) 24 | ch.setFormatter(formatter) 25 | logger.addHandler(ch) 26 | 27 | if args.debug: 28 | logger.setLevel(logging.DEBUG) 29 | 30 | logger.debug("Package: %s" % args.package) 31 | logger.debug("nth_last_version: %s" % args.nth_last_version) 32 | logger.debug("prerelease: %s" % args.prerelease) 33 | logger.debug("debug: %s" % args.debug) 34 | 35 | finder = pip._internal.index.PackageFinder([], ['https://pypi.python.org/simple'], session=requests.Session()) 36 | results = finder.find_all_candidates(args.package) 37 | tmp_versions = [str(p.version) for p in results] 38 | 39 | logger.debug("%s" % tmp_versions) 40 | 41 | versions = list() 42 | for el in tmp_versions: 43 | if el not in versions: 44 | versions.append(el) 45 | 46 | pos = -1 47 | nth_version = 1 48 | 49 | while True: 50 | fetched_version = versions[pos] 51 | 52 | logger.debug("Version: %s" % fetched_version) 53 | 54 | if nth_version == args.nth_last_version: 55 | if args.prerelease or not ("rc" in fetched_version or "a" in fetched_version or "b" in fetched_version): 56 | break 57 | else: 58 | pos -= 1 59 | continue 60 | 61 | pos -= 1 62 | nth_version += 1 63 | 64 | print(fetched_version) 65 | -------------------------------------------------------------------------------- /docker/version_prefix.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import logging 3 | 4 | if __name__ == "__main__": 5 | 6 | parser = argparse.ArgumentParser(description='Determine the version prefix to apply depending on the version name') 7 | 8 | parser.add_argument( 9 | '--version', type=str, required=True, help='The Package Version to be installed in the container' 10 | ) 11 | 12 | parser.add_argument('--debug', help='Print debug information', action='store_true') 13 | 14 | args = parser.parse_args() 15 | 16 | # create logger 17 | logger = logging.getLogger("VERSION_PREFIX_CLI") 18 | 19 | formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') 20 | 21 | ch = logging.StreamHandler() 22 | ch.setLevel(logging.DEBUG) 23 | ch.setFormatter(formatter) 24 | logger.addHandler(ch) 25 | 26 | if args.debug: 27 | logger.setLevel(logging.DEBUG) 28 | 29 | logger.debug("Package Version: %s" % args.version) 30 | 31 | if "rc" in args.version or "a" in args.version or "b" in args.version: 32 | print("latest-dev") 33 | else: 34 | print("latest") 35 | -------------------------------------------------------------------------------- /docs/_static/fix_rtd.css: -------------------------------------------------------------------------------- 1 | /* work around https://github.com/snide/sphinx_rtd_theme/issues/149 */ 2 | .rst-content table.field-list .field-body { 3 | padding-top: 8px; 4 | } 5 | 6 | /*.section #basic-2-flip-flop-synchronizer{ 7 | text-align:justify; 8 | }*/ 9 | -------------------------------------------------------------------------------- /docs/images/affine_transform_comparison.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/images/affine_transform_comparison.jpg -------------------------------------------------------------------------------- /docs/images/affine_transform_why.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/images/affine_transform_why.jpg -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to TensorLayerX 2 | ======================================= 3 | 4 | 5 | .. image:: user/my_figs/tlx_transparent_logo.png 6 | :width: 60 % 7 | :align: center 8 | :target: https://github.com/tensorlayer/TensorLayerX 9 | 10 | **Documentation Version:** |release| 11 | 12 | `TensorLayerX`_ is a deep learning library designed for researchers and engineers that is compatible with multiple deep learning frameworks 13 | such as TensorFlow, MindSpore and PaddlePaddle, allowing users to run the code on different hardware like Nvidia-GPU and Huawei-Ascend. 14 | It provides popular DL and RL modules that can be easily customized and assembled for tackling real-world machine learning problems. 15 | More details can be found `here `_. 16 | 17 | TensorLayerX is a multi-backend AI framework, which can run on almost all operation systems and AI hardwares, and support hybrid-framework programming. The currently version supports TensorFlow, MindSpore, PaddlePaddle and PyTorch(partial) as the backends. 18 | 19 | .. note:: 20 | If you got problem to read the docs online, you could download the repository 21 | on `TensorLayerX`_, then go to ``/docs/_build/html/index.html`` to read the docs 22 | offline. The ``_build`` folder can be generated in ``docs`` using ``make html``. 23 | 24 | User Guide 25 | ------------ 26 | 27 | The TensorLayerX user guide explains how to install TensorFlow, CUDA and cuDNN, 28 | how to build and train neural networks using TensorLayerX, and how to contribute 29 | to the library as a developer. 30 | 31 | .. toctree:: 32 | :maxdepth: 2 33 | 34 | user/installation 35 | user/examples 36 | user/contributing 37 | user/faq 38 | 39 | .. toctree:: 40 | :maxdepth: 2 41 | :caption: Getting started 42 | 43 | user/get_start_model 44 | user/get_start_advance 45 | 46 | API Reference 47 | ------------- 48 | 49 | If you are looking for information on a specific function, class or 50 | method, this part of the documentation is for you. 51 | 52 | .. toctree:: 53 | :maxdepth: 2 54 | :caption: Stable Functionalities 55 | 56 | modules/activation 57 | modules/losses 58 | modules/metrics 59 | modules/dataflow 60 | modules/files 61 | modules/nn 62 | modules/model 63 | modules/vision 64 | modules/initializers 65 | modules/ops 66 | modules/optimizers 67 | 68 | Command-line Reference 69 | ---------------------- 70 | 71 | TensorLayerX provides a handy command-line tool `tlx` to perform some common tasks. 72 | 73 | Indices and tables 74 | ================== 75 | 76 | * :ref:`genindex` 77 | * :ref:`modindex` 78 | * :ref:`search` 79 | 80 | 81 | .. _OpenI: https://git.openi.org.cn/OpenI/TensorLayerX 82 | .. _TensorLayerX: https://github.com/tensorlayer/TensorLayerX 83 | -------------------------------------------------------------------------------- /docs/modules/activation.rst: -------------------------------------------------------------------------------- 1 | API - Activations 2 | ========================= 3 | 4 | To make TensorLayerX simple, we minimize the number of activation functions as much as 5 | we can. So we encourage you to use Customizes activation function. 6 | For parametric activation, please read the layer APIs. 7 | 8 | Your activation 9 | ------------------- 10 | 11 | Customizes activation function in TensorLayerX is very easy. 12 | The following example implements an activation that multiplies its input by 2. 13 | 14 | .. code-block:: python 15 | 16 | from tensorlayerx.nn import Module 17 | class DoubleActivation(Module): 18 | def __init__(self): 19 | pass 20 | def forward(self, x): 21 | return x * 2 22 | double_activation = DoubleActivation() 23 | 24 | For more complex activation, TensorFlow(MindSpore, PaddlePaddle, PyTorch) API will be required. 25 | 26 | .. automodule:: tensorlayerx.nn.activation 27 | 28 | activation list 29 | ---------------- 30 | 31 | .. autosummary:: 32 | 33 | ELU 34 | PRelu 35 | PRelu6 36 | PTRelu6 37 | ReLU 38 | ReLU6 39 | Softplus 40 | LeakyReLU 41 | LeakyReLU6 42 | LeakyTwiceRelu6 43 | Ramp 44 | Swish 45 | HardTanh 46 | Tanh 47 | Sigmoid 48 | Softmax 49 | Mish 50 | LogSoftmax 51 | HardSigmoid 52 | Hardswish 53 | 54 | TensorLayerX Activations 55 | -------------------------------- 56 | 57 | ELU 58 | ------ 59 | .. autoclass:: ELU 60 | 61 | PRelu 62 | ------ 63 | .. autoclass:: PRelu 64 | 65 | PRelu6 66 | ------------ 67 | .. autoclass:: PRelu6 68 | 69 | PTRelu6 70 | ------------ 71 | .. autoclass:: PTRelu6 72 | 73 | ReLU 74 | ----------------- 75 | .. autoclass:: ReLU 76 | 77 | ReLU6 78 | ----------------- 79 | .. autoclass:: ReLU6 80 | 81 | Softplus 82 | ----------------- 83 | .. autoclass:: Softplus 84 | 85 | LeakyReLU 86 | ----------------- 87 | .. autoclass:: LeakyReLU 88 | 89 | LeakyReLU6 90 | ------------ 91 | .. autoclass:: LeakyReLU6 92 | 93 | LeakyTwiceRelu6 94 | --------------------- 95 | .. autoclass:: LeakyTwiceRelu6 96 | 97 | Ramp 98 | --------------------- 99 | .. autoclass:: Ramp 100 | 101 | Swish 102 | -------------------- 103 | .. autoclass:: Swish 104 | 105 | HardTanh 106 | ---------------- 107 | .. autoclass:: HardTanh 108 | 109 | Mish 110 | --------- 111 | .. autoclass:: Mish 112 | 113 | Tanh 114 | --------- 115 | .. autoclass:: Tanh 116 | 117 | Sigmoid 118 | --------- 119 | .. autoclass:: Sigmoid 120 | 121 | Softmax 122 | --------- 123 | .. autoclass:: Softmax 124 | 125 | LogSoftmax 126 | ------------------ 127 | .. autoclass:: LogSoftmax 128 | 129 | HardSigmoid 130 | ------------------ 131 | .. autoclass:: HardSigmoid 132 | 133 | Hardswish 134 | ------------------ 135 | .. autoclass:: Hardswish 136 | 137 | Parametric activation 138 | ------------------------------ 139 | See ``tensorlayerx.nn``. 140 | -------------------------------------------------------------------------------- /docs/modules/dataflow.rst: -------------------------------------------------------------------------------- 1 | API - Dataflow 2 | ============== 3 | 4 | .. automodule:: tensorlayerx.dataflow 5 | 6 | .. ----------------------------------------------------------- 7 | .. Dataflow List 8 | .. ----------------------------------------------------------- 9 | 10 | Dataflow list 11 | ---------------------- 12 | 13 | .. autosummary:: 14 | 15 | DataLoader 16 | Dataset 17 | IterableDataset 18 | TensorDataset 19 | ChainDataset 20 | ConcatDataset 21 | Subset 22 | random_split 23 | Sampler 24 | BatchSampler 25 | RandomSampler 26 | SequentialSampler 27 | WeightedRandomSampler 28 | SubsetRandomSampler 29 | 30 | 31 | .. ----------------------------------------------------------- 32 | .. Dataflow 33 | .. ----------------------------------------------------------- 34 | 35 | Dataflow 36 | ----------------- 37 | DataLoader 38 | ^^^^^^^^^^^^^^^^ 39 | .. autoclass:: DataLoader 40 | 41 | 42 | Dataset 43 | ^^^^^^^^^^^^^^^^ 44 | .. autoclass:: Dataset 45 | 46 | 47 | IterableDataset 48 | ^^^^^^^^^^^^^^^^ 49 | .. autoclass:: IterableDataset 50 | 51 | TensorDataset 52 | ^^^^^^^^^^^^^^^^ 53 | .. autoclass:: TensorDataset 54 | 55 | ChainDataset 56 | ^^^^^^^^^^^^^^^^ 57 | .. autoclass:: ChainDataset 58 | 59 | ConcatDataset 60 | ^^^^^^^^^^^^^^^^ 61 | .. autoclass:: ConcatDataset 62 | 63 | Subset 64 | ^^^^^^^^^^^^^^^^ 65 | .. autoclass:: Subset 66 | 67 | random_split 68 | ^^^^^^^^^^^^^^^^ 69 | .. autoclass:: random_split 70 | 71 | Sampler 72 | ^^^^^^^^^^^^^^^^ 73 | .. autoclass:: Sampler 74 | 75 | BatchSampler 76 | ^^^^^^^^^^^^^^^^ 77 | .. autoclass:: BatchSampler 78 | 79 | RandomSampler 80 | ^^^^^^^^^^^^^^^^ 81 | .. autoclass:: RandomSampler 82 | 83 | SequentialSampler 84 | ^^^^^^^^^^^^^^^^^^^^^ 85 | .. autoclass:: SequentialSampler 86 | 87 | WeightedRandomSampler 88 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 89 | .. autoclass:: WeightedRandomSampler 90 | 91 | SubsetRandomSampler 92 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 93 | .. autoclass:: SubsetRandomSampler -------------------------------------------------------------------------------- /docs/modules/initializers.rst: -------------------------------------------------------------------------------- 1 | API - Initializers 2 | ========================= 3 | 4 | To make TensorLayerX simple, TensorLayerX only warps some basic initializers. 5 | For more complex activation, TensorFlow(MindSpore, PaddlePaddle, PyTorch) API will be required. 6 | 7 | .. automodule:: tensorlayerx.nn.initializers 8 | 9 | .. autosummary:: 10 | 11 | Initializer 12 | Zeros 13 | Ones 14 | Constant 15 | RandomUniform 16 | RandomNormal 17 | TruncatedNormal 18 | HeNormal 19 | HeUniform 20 | deconv2d_bilinear_upsampling_initializer 21 | XavierNormal 22 | XavierUniform 23 | 24 | Initializer 25 | ------------ 26 | .. autoclass:: Initializer 27 | 28 | Zeros 29 | ------------ 30 | .. autoclass:: Zeros 31 | 32 | Ones 33 | ------------ 34 | .. autoclass:: Ones 35 | 36 | Constant 37 | ----------------- 38 | .. autoclass:: Constant 39 | 40 | RandomUniform 41 | -------------- 42 | .. autoclass:: RandomUniform 43 | 44 | RandomNormal 45 | --------------------- 46 | .. autoclass:: RandomNormal 47 | 48 | TruncatedNormal 49 | --------------------- 50 | .. autoclass:: TruncatedNormal 51 | 52 | HeNormal 53 | ------------ 54 | .. autoclass:: HeNormal 55 | 56 | HeUniform 57 | ------------ 58 | .. autoclass:: HeUniform 59 | 60 | deconv2d_bilinear_upsampling_initializer 61 | ------------------------------------------ 62 | .. autofunction:: deconv2d_bilinear_upsampling_initializer 63 | 64 | XavierNormal 65 | --------------------- 66 | .. autoclass:: XavierNormal 67 | 68 | XavierUniform 69 | --------------------- 70 | .. autoclass:: XavierUniform -------------------------------------------------------------------------------- /docs/modules/iterate.rst: -------------------------------------------------------------------------------- 1 | API - Iteration 2 | ========================== 3 | 4 | Data iteration. 5 | 6 | 7 | .. automodule:: tensorlayerx.utils.iterate 8 | 9 | .. autosummary:: 10 | 11 | minibatches 12 | seq_minibatches 13 | seq_minibatches2 14 | ptb_iterator 15 | 16 | 17 | Non-time series 18 | -------------------- 19 | 20 | .. autofunction:: minibatches 21 | 22 | 23 | Time series 24 | ---------------------- 25 | 26 | Sequence iteration 1 27 | ^^^^^^^^^^^^^^^^^^^^^^^ 28 | .. autofunction:: seq_minibatches 29 | 30 | Sequence iteration 2 31 | ^^^^^^^^^^^^^^^^^^^^^^^ 32 | .. autofunction:: seq_minibatches2 33 | 34 | PTB dataset iteration 35 | ^^^^^^^^^^^^^^^^^^^^^^^^ 36 | .. autofunction:: ptb_iterator 37 | -------------------------------------------------------------------------------- /docs/modules/losses.rst: -------------------------------------------------------------------------------- 1 | API - Losses 2 | ================== 3 | 4 | To make TensorLayerX simple, we minimize the number of cost functions as much as we can. 5 | For more complex loss function, TensorFlow(MindSpore, PaddlePaddle, PyTorch) API will be required. 6 | 7 | .. note:: 8 | Please refer to `Getting Started `_ for getting specific weights for weight regularization. 9 | 10 | .. automodule:: tensorlayerx.losses 11 | 12 | .. autosummary:: 13 | 14 | softmax_cross_entropy_with_logits 15 | sigmoid_cross_entropy 16 | binary_cross_entropy 17 | mean_squared_error 18 | normalized_mean_square_error 19 | absolute_difference_error 20 | dice_coe 21 | dice_hard_coe 22 | iou_coe 23 | cross_entropy_seq 24 | cross_entropy_seq_with_mask 25 | cosine_similarity 26 | li_regularizer 27 | lo_regularizer 28 | maxnorm_regularizer 29 | maxnorm_o_regularizer 30 | maxnorm_i_regularizer 31 | 32 | 33 | Softmax cross entropy 34 | ---------------------- 35 | .. autofunction:: softmax_cross_entropy_with_logits 36 | 37 | Sigmoid cross entropy 38 | ---------------------- 39 | .. autofunction:: sigmoid_cross_entropy 40 | 41 | Binary cross entropy 42 | ------------------------- 43 | .. autofunction:: binary_cross_entropy 44 | 45 | Mean squared error (L2) 46 | ------------------------- 47 | .. autofunction:: mean_squared_error 48 | 49 | Normalized mean square error 50 | -------------------------------- 51 | .. autofunction:: normalized_mean_square_error 52 | 53 | Absolute difference error (L1) 54 | -------------------------------- 55 | .. autofunction:: absolute_difference_error 56 | 57 | Dice coefficient 58 | ------------------------- 59 | .. autofunction:: dice_coe 60 | 61 | Hard Dice coefficient 62 | ------------------------- 63 | .. autofunction:: dice_hard_coe 64 | 65 | IOU coefficient 66 | ------------------------- 67 | .. autofunction:: iou_coe 68 | 69 | Cross entropy for sequence 70 | ----------------------------- 71 | .. autofunction:: cross_entropy_seq 72 | 73 | Cross entropy with mask for sequence 74 | ---------------------------------------- 75 | .. autofunction:: cross_entropy_seq_with_mask 76 | 77 | Cosine similarity 78 | ------------------- 79 | .. autofunction:: cosine_similarity 80 | 81 | Regularization functions 82 | -------------------------- 83 | 84 | For ``tf.nn.l2_loss``, ``tf.contrib.layers.l1_regularizer``, ``tf.contrib.layers.l2_regularizer`` and 85 | ``tf.contrib.layers.sum_regularizer``, see tensorflow API. 86 | Maxnorm 87 | ^^^^^^^^^^ 88 | .. autofunction:: maxnorm_regularizer 89 | 90 | Special 91 | ^^^^^^^^^^ 92 | .. autofunction:: li_regularizer 93 | .. autofunction:: lo_regularizer 94 | .. autofunction:: maxnorm_o_regularizer 95 | .. autofunction:: maxnorm_i_regularizer 96 | -------------------------------------------------------------------------------- /docs/modules/metrics.rst: -------------------------------------------------------------------------------- 1 | API - Metrics 2 | ================== 3 | 4 | The tensorlayerx.metrics directory contians Accuracy, Auc, Precision and Recall. 5 | For more complex metrics, you can encapsulates metric logic and APIs by base class. 6 | 7 | 8 | .. automodule:: tensorlayerx.metrics 9 | 10 | Metric list 11 | ------------- 12 | 13 | .. autosummary:: 14 | 15 | Metric 16 | Accuracy 17 | Auc 18 | Precision 19 | Recall 20 | acc 21 | 22 | 23 | 24 | Metric 25 | ^^^^^^^^^^^^^^^^ 26 | .. autoclass:: Metric 27 | 28 | 29 | Accuracy 30 | """""""""""""""""""""""""" 31 | .. autoclass:: Accuracy 32 | :members: 33 | 34 | 35 | Auc 36 | """""""""""""""""""""""""" 37 | .. autoclass:: Auc 38 | :members: 39 | 40 | 41 | Precision 42 | """""""""""""""""""""""""" 43 | .. autoclass:: Precision 44 | :members: 45 | 46 | 47 | Recall 48 | """""""""""""""""""""""""" 49 | .. autoclass:: Recall 50 | :members: 51 | 52 | 53 | acc 54 | """""""""""""""""""""""""" 55 | .. autofunction:: acc 56 | -------------------------------------------------------------------------------- /docs/modules/model.rst: -------------------------------------------------------------------------------- 1 | API - Model Training 2 | ================================ 3 | 4 | TensorLayerX provides two model training interfaces, which can satisfy the training of various deep learning tasks. 5 | 6 | .. automodule:: tensorlayerx.model 7 | 8 | .. autosummary:: 9 | Model 10 | WithLoss 11 | WithGrad 12 | TrainOneStep 13 | 14 | 15 | Model 16 | ---------------------- 17 | .. autofunction:: Model 18 | 19 | WithLoss 20 | ---------------------- 21 | .. autofunction:: WithLoss 22 | 23 | WithGrad 24 | ----------------------- 25 | .. autofunction:: WithGrad 26 | 27 | TrainOneStep 28 | ---------------- 29 | .. autofunction:: TrainOneStep 30 | -------------------------------------------------------------------------------- /docs/modules/nlp.rst: -------------------------------------------------------------------------------- 1 | API - Natural Language Processing 2 | ================================== 3 | 4 | Natural Language Processing and Word Representation. 5 | 6 | .. automodule:: tensorlayerx.text.nlp 7 | 8 | .. autosummary:: 9 | 10 | generate_skip_gram_batch 11 | 12 | sample 13 | sample_top 14 | 15 | SimpleVocabulary 16 | Vocabulary 17 | process_sentence 18 | create_vocab 19 | 20 | simple_read_words 21 | read_words 22 | read_analogies_file 23 | build_vocab 24 | build_reverse_dictionary 25 | build_words_dataset 26 | save_vocab 27 | 28 | words_to_word_ids 29 | word_ids_to_words 30 | 31 | basic_tokenizer 32 | create_vocabulary 33 | initialize_vocabulary 34 | sentence_to_token_ids 35 | data_to_token_ids 36 | 37 | moses_multi_bleu 38 | 39 | 40 | Iteration function for training embedding matrix 41 | ------------------------------------------------- 42 | .. autofunction:: generate_skip_gram_batch 43 | 44 | 45 | Sampling functions 46 | ------------------- 47 | 48 | Simple sampling 49 | ^^^^^^^^^^^^^^^^^^^ 50 | .. autofunction:: sample 51 | 52 | Sampling from top k 53 | ^^^^^^^^^^^^^^^^^^^^^^ 54 | .. autofunction:: sample_top 55 | 56 | Vector representations of words 57 | ------------------------------- 58 | 59 | Simple vocabulary class 60 | ^^^^^^^^^^^^^^^^^^^^^^^^^ 61 | .. autoclass:: SimpleVocabulary 62 | 63 | Vocabulary class 64 | ^^^^^^^^^^^^^^^^^^^^^^^^^ 65 | .. autoclass:: Vocabulary 66 | 67 | Process sentence 68 | ^^^^^^^^^^^^^^^^^^^^^^^^ 69 | .. autofunction:: process_sentence 70 | 71 | Create vocabulary 72 | ^^^^^^^^^^^^^^^^^^^^^^^^ 73 | .. autofunction:: create_vocab 74 | 75 | Read words from file 76 | ---------------------- 77 | 78 | Simple read file 79 | ^^^^^^^^^^^^^^^^^^ 80 | .. autofunction:: simple_read_words 81 | 82 | Read file 83 | ^^^^^^^^^^^^^^^^^^ 84 | .. autofunction:: read_words 85 | 86 | 87 | Read analogy question file 88 | ----------------------------- 89 | .. autofunction:: read_analogies_file 90 | 91 | Build vocabulary, word dictionary and word tokenization 92 | -------------------------------------------------------- 93 | 94 | Build dictionary from word to id 95 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 96 | .. autofunction:: build_vocab 97 | 98 | Build dictionary from id to word 99 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 100 | .. autofunction:: build_reverse_dictionary 101 | 102 | Build dictionaries for id to word etc 103 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 104 | .. autofunction:: build_words_dataset 105 | 106 | Save vocabulary 107 | ^^^^^^^^^^^^^^^^^^^^ 108 | .. autofunction:: save_vocab 109 | 110 | 111 | Convert words to IDs and IDs to words 112 | -------------------------------------------------------- 113 | These functions can be done by ``Vocabulary`` class. 114 | 115 | List of Words to IDs 116 | ^^^^^^^^^^^^^^^^^^^^^^^ 117 | .. autofunction:: words_to_word_ids 118 | 119 | List of IDs to Words 120 | ^^^^^^^^^^^^^^^^^^^^^ 121 | .. autofunction:: word_ids_to_words 122 | 123 | 124 | 125 | Functions for translation 126 | --------------------------- 127 | 128 | Word Tokenization 129 | ^^^^^^^^^^^^^^^^^^^ 130 | .. autofunction:: basic_tokenizer 131 | 132 | Create or read vocabulary 133 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ 134 | .. autofunction:: create_vocabulary 135 | .. autofunction:: initialize_vocabulary 136 | 137 | Convert words to IDs and IDs to words 138 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 139 | .. autofunction:: sentence_to_token_ids 140 | .. autofunction:: data_to_token_ids 141 | 142 | 143 | Metrics 144 | --------------------------- 145 | 146 | BLEU 147 | ^^^^^^^^^^^^^^^^^^^ 148 | .. autofunction:: moses_multi_bleu 149 | -------------------------------------------------------------------------------- /docs/modules/optimizers.rst: -------------------------------------------------------------------------------- 1 | API - Optimizers 2 | ================ 3 | 4 | .. automodule:: tensorlayerx.optimizers 5 | 6 | TensorLayerX provides simple API and tools to ease research, development and reduce the time to production. 7 | Therefore, we provide the latest state of the art optimizers that work with Tensorflow, MindSpore, PaddlePaddle and PyTorch. 8 | The optimizers functions provided by Tensorflow, MindSpore, PaddlePaddle and PyTorch can be used in TensorLayerX. 9 | We have also wrapped the optimizers functions for each framework, which can be found in tensorlayerx.optimizers. 10 | In addition, we provide the latest state of Optimizers Dynamic Learning Rate that work with Tensorflow, MindSpore, PaddlePaddle and PyTorch. 11 | 12 | Optimizers List 13 | --------------- 14 | 15 | .. autosummary:: 16 | 17 | Adadelta 18 | Adagrad 19 | Adam 20 | Adamax 21 | Ftrl 22 | Nadam 23 | RMSprop 24 | SGD 25 | Momentum 26 | Lamb 27 | LARS 28 | 29 | .. automodule:: tensorlayerx.optimizers.lr 30 | 31 | Optimizers Dynamic Learning Rate List 32 | -------------------------------------- 33 | 34 | .. autosummary:: 35 | 36 | LRScheduler 37 | StepDecay 38 | CosineAnnealingDecay 39 | NoamDecay 40 | PiecewiseDecay 41 | NaturalExpDecay 42 | InverseTimeDecay 43 | PolynomialDecay 44 | LinearWarmup 45 | ExponentialDecay 46 | MultiStepDecay 47 | LambdaDecay 48 | ReduceOnPlateau 49 | 50 | 51 | Adadelta 52 | ^^^^^^^^^^ 53 | .. autoclass:: tensorlayerx.optimizers.Adadelta 54 | 55 | Adagrad 56 | ^^^^^^^^^^^^^^^^ 57 | .. autoclass:: tensorlayerx.optimizers.Adagrad 58 | 59 | Adam 60 | ^^^^^^^^^^^^^^^^ 61 | .. autoclass:: tensorlayerx.optimizers.Adam 62 | 63 | Adamax 64 | ^^^^^^^^^^^^^^^^ 65 | .. autoclass:: tensorlayerx.optimizers.Adamax 66 | 67 | Ftrl 68 | ^^^^^^^^^^^^^^^^ 69 | .. autoclass:: tensorlayerx.optimizers.Ftrl 70 | 71 | Nadam 72 | ^^^^^^^^^^^^^^^^ 73 | .. autoclass:: tensorlayerx.optimizers.Nadam 74 | 75 | RMSprop 76 | ^^^^^^^^^^^^^^^^ 77 | .. autoclass:: tensorlayerx.optimizers.RMSprop 78 | 79 | SGD 80 | ^^^^^^^^^^^^^^^^ 81 | .. autoclass:: tensorlayerx.optimizers.SGD 82 | 83 | Momentum 84 | ^^^^^^^^^^^^^^^^ 85 | .. autoclass:: tensorlayerx.optimizers.Momentum 86 | 87 | Lamb 88 | ^^^^^^^^^^^^^^^^ 89 | .. autoclass:: tensorlayerx.optimizers.Lamb 90 | 91 | LARS 92 | ^^^^^^^^^^^^^^^^ 93 | .. autoclass:: tensorlayerx.optimizers.LARS 94 | 95 | 96 | 97 | 98 | LRScheduler 99 | ^^^^^^^^^^^^^^^^ 100 | .. autoclass:: LRScheduler 101 | 102 | StepDecay 103 | ^^^^^^^^^^^^^^^^ 104 | .. autoclass:: StepDecay 105 | 106 | CosineAnnealingDecay 107 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 108 | .. autoclass:: CosineAnnealingDecay 109 | 110 | NoamDecay 111 | ^^^^^^^^^^^^^^^^ 112 | .. autoclass:: NoamDecay 113 | 114 | PiecewiseDecay 115 | ^^^^^^^^^^^^^^^^ 116 | .. autoclass:: PiecewiseDecay 117 | 118 | NaturalExpDecay 119 | ^^^^^^^^^^^^^^^^ 120 | .. autoclass:: NaturalExpDecay 121 | 122 | InverseTimeDecay 123 | ^^^^^^^^^^^^^^^^^^^ 124 | .. autoclass:: InverseTimeDecay 125 | 126 | PolynomialDecay 127 | ^^^^^^^^^^^^^^^^ 128 | .. autoclass:: PolynomialDecay 129 | 130 | LinearWarmup 131 | ^^^^^^^^^^^^^^^^ 132 | .. autoclass:: LinearWarmup 133 | 134 | ExponentialDecay 135 | ^^^^^^^^^^^^^^^^^^^^ 136 | .. autoclass:: ExponentialDecay 137 | 138 | MultiStepDecay 139 | ^^^^^^^^^^^^^^^^ 140 | .. autoclass:: MultiStepDecay 141 | 142 | LambdaDecay 143 | ^^^^^^^^^^^^^^^^ 144 | .. autoclass:: LambdaDecay 145 | 146 | ReduceOnPlateau 147 | ^^^^^^^^^^^^^^^^^^^^^^ 148 | .. autoclass:: ReduceOnPlateau 149 | 150 | -------------------------------------------------------------------------------- /docs/modules/pretrain_models.rst: -------------------------------------------------------------------------------- 1 | API - Pretrained Models 2 | ================================ 3 | 4 | TensorLayerX provides many pretrained models, you can easily use the whole or a part of the pretrained models via these APIs. 5 | 6 | .. automodule:: examples.model_zoo 7 | 8 | .. autosummary:: 9 | 10 | vgg16 11 | vgg19 12 | YOLOv4 13 | ResNet50 14 | 15 | vgg16 16 | ---------------------- 17 | .. autofunction:: vgg16 18 | 19 | vgg19 20 | ---------------------- 21 | .. autofunction:: vgg19 22 | 23 | YOLOv4 24 | ---------------- 25 | .. autofunction:: YOLOv4 26 | 27 | ResNet50 28 | ---------------- 29 | .. autofunction:: ResNet50 30 | 31 | -------------------------------------------------------------------------------- /docs/modules/visualize.rst: -------------------------------------------------------------------------------- 1 | API - Visualization 2 | ================================ 3 | 4 | TensorFlow provides `TensorBoard `_ 5 | to visualize the model, activations etc. Here we provide more functions for data visualization. 6 | 7 | .. automodule:: tensorlayerx.utils.visualize 8 | 9 | .. autosummary:: 10 | 11 | read_image 12 | read_images 13 | save_image 14 | save_images 15 | draw_boxes_and_labels_to_image 16 | draw_mpii_pose_to_image 17 | draw_weights 18 | CNN2d 19 | frame 20 | images2d 21 | tsne_embedding 22 | draw_boxes_and_labels_to_image_with_json 23 | 24 | 25 | Save and read images 26 | ---------------------- 27 | 28 | Read one image 29 | ^^^^^^^^^^^^^^^^^ 30 | .. autofunction:: read_image 31 | 32 | Read multiple images 33 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 34 | .. autofunction:: read_images 35 | 36 | Save one image 37 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 38 | .. autofunction:: save_image 39 | 40 | Save multiple images 41 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 42 | .. autofunction:: save_images 43 | 44 | Save image for object detection 45 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 46 | .. autofunction:: draw_boxes_and_labels_to_image 47 | 48 | Save image for object detection with json 49 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 50 | .. autofunction:: draw_boxes_and_labels_to_image_with_json 51 | 52 | Save image for pose estimation (MPII) 53 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 54 | .. autofunction:: draw_mpii_pose_to_image 55 | 56 | Visualize model parameters 57 | ------------------------------ 58 | 59 | Visualize CNN 2d filter 60 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 61 | .. autofunction:: CNN2d 62 | 63 | Visualize weights 64 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ 65 | .. autofunction:: draw_weights 66 | 67 | Visualize images 68 | ----------------- 69 | 70 | Image by matplotlib 71 | ^^^^^^^^^^^^^^^^^^^^^ 72 | .. autofunction:: frame 73 | 74 | Images by matplotlib 75 | ^^^^^^^^^^^^^^^^^^^^^ 76 | .. autofunction:: images2d 77 | 78 | Visualize embeddings 79 | -------------------- 80 | .. autofunction:: tsne_embedding 81 | -------------------------------------------------------------------------------- /docs/tutorials/images/Structure-of-the-LSTM-cell-and-equations-that-describe-the-gates-of-an-LSTM-cell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/Structure-of-the-LSTM-cell-and-equations-that-describe-the-gates-of-an-LSTM-cell.png -------------------------------------------------------------------------------- /docs/tutorials/images/dataset_mnist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/dataset_mnist.png -------------------------------------------------------------------------------- /docs/tutorials/images/fake_mnist_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/fake_mnist_1.png -------------------------------------------------------------------------------- /docs/tutorials/images/fake_mnist_final.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/fake_mnist_final.png -------------------------------------------------------------------------------- /docs/tutorials/images/flower_centercrop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/flower_centercrop.png -------------------------------------------------------------------------------- /docs/tutorials/images/flower_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/flower_color.png -------------------------------------------------------------------------------- /docs/tutorials/images/flower_flip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/flower_flip.png -------------------------------------------------------------------------------- /docs/tutorials/images/gan_loss.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/gan_loss.gif -------------------------------------------------------------------------------- /docs/tutorials/images/lstm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/tutorials/images/lstm.png -------------------------------------------------------------------------------- /docs/user/examples.rst: -------------------------------------------------------------------------------- 1 | .. _example: 2 | 3 | ============ 4 | Examples 5 | ============ 6 | 7 | Basics 8 | ============ 9 | You can learn TensorLayerX quickly from the following two examples: 10 | 11 | - Multi-layer perceptron (MNIST), simple usage and supports multiple backends. Classification task, see `mnist_mlp.py `__. 12 | - Convolutional Network (CIFAR-10), simple usage and supports multiple backends. Classification task, see `cifar10_cnn.py `__. 13 | 14 | Resources 15 | =========================================== 16 | More official tutorials can be found in the resources below: 17 | 18 | - [Examples](https://github.com/tensorlayer/TensorLayerX/tree/main/examples) for tutorials 19 | - [GammaGL](https://github.com/BUPT-GAMMA/GammaGL) is series of graph learning algorithm 20 | - [TLXZoo](https://github.com/tensorlayer/TLXZoo) a series of pretrained backbones 21 | - [TLXCV](https://github.com/tensorlayer/TLXCV) a series of Computer Vision applications 22 | - [TLXNLP](https://github.com/tensorlayer/TLXNLP) a series of Natural Language Processing applications 23 | - [TLX2ONNX](https://github.com/tensorlayer/TLX2ONNX/) ONNX model exporter for TensorLayerX. 24 | - [Paddle2TLX](https://github.com/tensorlayer/paddle2tlx) model code converter from PaddlePaddle to TensorLayerX. 25 | 26 | 27 | .. _GitHub: https://github.com/tensorlayer/TensorLayerX 28 | .. _Deeplearning Tutorial: http://deeplearning.stanford.edu/tutorial/ 29 | .. _Convolutional Neural Networks for Visual Recognition: http://cs231n.github.io/ 30 | .. _Neural Networks and Deep Learning: http://neuralnetworksanddeeplearning.com/ 31 | .. _TensorFlow tutorial: https://www.tensorflow.org/versions/r0.9/tutorials/index.html 32 | .. _Understand Deep Reinforcement Learning: http://karpathy.github.io/2016/05/31/rl/ 33 | .. _Understand Recurrent Neural Network: http://karpathy.github.io/2015/05/21/rnn-effectiveness/ 34 | .. _Understand LSTM Network: http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 35 | .. _Word Representations: http://colah.github.io/posts/2014-07-NLP-RNNs-Representations/ 36 | -------------------------------------------------------------------------------- /docs/user/installation.rst: -------------------------------------------------------------------------------- 1 | .. _installation: 2 | 3 | ============ 4 | Installation 5 | ============ 6 | 7 | TensorLayerX has some prerequisites that need to be installed first, including NumPy, Matplotlib and one of the frameworks ( 8 | `TensorFlow`_ , `MindSpore `__, `PaddlePaddle `__, `PyTorch `__). For GPU 9 | support CUDA and cuDNN are required. 10 | 11 | If you run into any trouble, please check the `TensorFlow installation 12 | instructions `__, 13 | `MindSpore installation instructions `__, 14 | `PaddlePaddle installation instructions `__, 15 | `PyTorch installation instructions `__, 16 | which cover installing the frameworks for a range of operating systems including 17 | Mac OX, Linux and Windows, or ask for help on `tensorlayer@gmail.com `_ 18 | or `FAQ `_. 19 | 20 | Install TensorLayerX 21 | ========================= 22 | 23 | For stable version: 24 | 25 | .. code-block:: bash 26 | 27 | pip3 install tensorlayerx 28 | 29 | pip install tensorlayerx -i https://pypi.tuna.tsinghua.edu.cn/simple (faster in China) 30 | 31 | For latest version, please install from github. 32 | 33 | .. code-block:: bash 34 | 35 | pip3 install git+https://github.com/tensorlayer/TensorLayerX.git 36 | 37 | For developers, you should clone the folder to your local machine and put it along with your project scripts. 38 | 39 | .. code-block:: bash 40 | 41 | git clone https://github.com/tensorlayer/TensorLayerX.git 42 | 43 | 44 | Alternatively, you can build from the source. 45 | 46 | .. code-block:: bash 47 | 48 | # First clone the repository and change the current directory to the newly cloned repository 49 | git clone https://github.com/tensorlayer/TensorLayerX.git 50 | cd tensorlayerx 51 | python setup.py install 52 | 53 | This command will run the ``setup.py`` to install TensorLayerX. 54 | 55 | .. _TensorFlow: https://www.tensorflow.org/versions/master/get_started/os_setup.html 56 | .. _GitHub: https://github.com/tensorlayer/TensorLayerX 57 | .. _TensorLayer: https://github.com/tensorlayer/tensorlayer/ -------------------------------------------------------------------------------- /docs/user/my_figs/basic_seq2seq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/basic_seq2seq.png -------------------------------------------------------------------------------- /docs/user/my_figs/join_slack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/join_slack.png -------------------------------------------------------------------------------- /docs/user/my_figs/karpathy_rnn.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/karpathy_rnn.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/mnist.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/mnist.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/pong_game.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/pong_game.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/seq2seq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/seq2seq.png -------------------------------------------------------------------------------- /docs/user/my_figs/tlx_transparent_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/tlx_transparent_logo.png -------------------------------------------------------------------------------- /docs/user/my_figs/tsne.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/tsne.png -------------------------------------------------------------------------------- /docs/user/my_figs/word2vec_basic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/docs/user/my_figs/word2vec_basic.pdf -------------------------------------------------------------------------------- /examples/basic_tutorials/README.md: -------------------------------------------------------------------------------- 1 | # Before You Start 2 | 3 | Please read this [DOCS](https://tensorlayerx.readthedocs.io/en/latest/user/get_start_model.html) before you start. 4 | 5 | ## Define a model 6 | TensorLayerX provides Sequential and Subclass define a model. Sequential model allows you to build model in a fluent way while Subclass allows you to fully control the forward process. 7 | - [Sequential Model](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/mnist_Sequential.py) 8 | - [Module List](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/tutorial_ModuleList.py) 9 | - [Nested Usage of Layer](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/nested_usage_of_layer.py) 10 | - [Automatic Inference Input Shape](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/automatic_inference_input_shape.py) 11 | 12 | ## Data processing 13 | - [MNIST Dataflow](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/mnist_dataflow.py) 14 | 15 | ## Model training 16 | - [MNIST MLP](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/mnist_mlp_simple_train.py) 17 | - [MNIST GAN](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/mnist_gan.py) 18 | - [CIFAR10 CNN](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/cifar10_cnn.py) 19 | - [IMDB LSTM](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/imdb_LSTM_simple.py) 20 | 21 | ## TensorLayerX can be mixed programming with other frameworks. 22 | - look code comments in [MNIST MLP](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/mnist_mlp_mix_programming.py) 23 | - look code comments in [CIFAR10 CNN](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/cifar10_cnn.py) 24 | 25 | ## Others 26 | - [Interoperability of Different Back-end Models](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/tutorial_tensorlayer_model_load.py) 27 | - [TensorFlow Model Save to pb](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/tensorflow_model_save_to_pb.py) 28 | - [Gradient Clip](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/gradient_clip_mixed_tensorflow.py) 29 | - [Using tensorboard](https://github.com/tensorlayer/TensorLayerX/blob/main/examples/basic_tutorials/tutorial_using_tensorboradX.py) -------------------------------------------------------------------------------- /examples/basic_tutorials/flower_demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/examples/basic_tutorials/flower_demo.png -------------------------------------------------------------------------------- /examples/basic_tutorials/gradient_clip_mixed_tensorflow.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | # The tensorlayerx and tensorflow operators can be mixed 4 | import os 5 | # os.environ['TL_BACKEND'] = 'tensorflow' 6 | # os.environ['TL_BACKEND'] = 'paddle' 7 | os.environ['TL_BACKEND'] = 'torch' 8 | # os.environ['TL_BACKEND'] = 'jittor' 9 | 10 | 11 | import time 12 | 13 | import tensorlayerx as tlx 14 | from tensorlayerx.nn import Module 15 | from tensorlayerx.nn import Linear 16 | from tensorlayerx.dataflow import Dataset, DataLoader 17 | 18 | X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1, 784)) 19 | 20 | 21 | class CustomModel(Module): 22 | 23 | def __init__(self): 24 | super(CustomModel, self).__init__() 25 | self.linear1 = Linear(out_features=800, in_features=784) 26 | self.linear2 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=800) 27 | self.linear3 = Linear(out_features=10, act=tlx.nn.ReLU, in_features=800) 28 | 29 | def forward(self, x, foo=None): 30 | z = self.linear1(x) 31 | z = self.linear2(z) 32 | out = self.linear3(z) 33 | if foo is not None: 34 | out = tlx.relu(out) 35 | return out 36 | 37 | class mnistdataset(Dataset): 38 | 39 | def __init__(self, data=X_train, label=y_train): 40 | self.data = data 41 | self.label = label 42 | 43 | def __getitem__(self, index): 44 | data = self.data[index].astype('float32') 45 | label = self.label[index].astype('int64') 46 | return data, label 47 | 48 | def __len__(self): 49 | return len(self.data) 50 | 51 | MLP = CustomModel() 52 | n_epoch = 50 53 | batch_size = 500 54 | print_freq = 5 55 | train_weights = MLP.trainable_weights 56 | train_dataset = mnistdataset(data=X_train, label=y_train) 57 | train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True) 58 | optimizer = tlx.optimizers.Adam(lr=0.0001, weight_decay= 0.001, grad_clip=tlx.ops.ClipGradByValue()) 59 | 60 | net_with_loss = tlx.model.WithLoss(backbone=MLP, loss_fn=tlx.losses.softmax_cross_entropy_with_logits) 61 | net_with_grad_train = tlx.model.TrainOneStep(net_with_loss, optimizer, train_weights) 62 | metrics = tlx.metrics.Accuracy() 63 | 64 | for epoch in range(n_epoch): ## iterate the dataset n_epoch times 65 | start_time = time.time() 66 | ## iterate over the entire training set once (shuffle the data via training) 67 | for X_batch, y_batch in train_loader: 68 | MLP.set_train() # enable dropout 69 | loss = net_with_grad_train(X_batch, y_batch) 70 | 71 | # use training and evaluation sets to evaluate the model every print_freq epoch 72 | if epoch + 1 == 1 or (epoch + 1) % print_freq == 0: 73 | print("Epoch {} of {} took {}".format(epoch + 1, n_epoch, time.time() - start_time)) 74 | train_loss, train_acc, n_iter = 0, 0, 0 75 | for X_batch, y_batch in train_loader: 76 | _logits = MLP(X_batch) 77 | train_loss += tlx.losses.softmax_cross_entropy_with_logits(_logits, y_batch) 78 | metrics.update(_logits, y_batch) 79 | train_acc += metrics.result() 80 | metrics.reset() 81 | n_iter += 1 82 | print(" train loss: {}".format(train_loss / n_iter)) 83 | print(" train acc: {}".format(train_acc / n_iter)) 84 | -------------------------------------------------------------------------------- /examples/basic_tutorials/imdb_lstm_simple.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # The same set of code can switch the backend with one line 5 | import os 6 | # os.environ['TL_BACKEND'] = 'tensorflow' 7 | # os.environ['TL_BACKEND'] = 'mindspore' 8 | os.environ['TL_BACKEND'] = 'paddle' 9 | # os.environ['TL_BACKEND'] = 'torch' 10 | import tensorlayerx as tlx 11 | from tensorlayerx.nn import Module 12 | from tensorlayerx.nn import Linear, LSTM, Embedding 13 | from tensorlayerx.dataflow import Dataset 14 | import numpy as np 15 | prev_h = np.random.random([1, 200, 64]).astype(np.float32) 16 | prev_h = tlx.convert_to_tensor(prev_h) 17 | 18 | X_train, y_train, X_test, y_test = tlx.files.load_imdb_dataset('data', nb_words=20000, test_split=0.2) 19 | 20 | seq_Len = 200 21 | vocab_size = len(X_train) + 1 22 | 23 | 24 | class ImdbDataset(Dataset): 25 | 26 | def __init__(self, X, y): 27 | self.X = X 28 | self.y = y 29 | 30 | def __getitem__(self, index): 31 | 32 | data = self.X[index] 33 | data = np.concatenate([data[:seq_Len], [0] * (seq_Len - len(data))]).astype('int64') # set 34 | label = self.y[index].astype('int64') 35 | return data, label 36 | 37 | def __len__(self): 38 | 39 | return len(self.y) 40 | 41 | 42 | class ImdbNet(Module): 43 | 44 | def __init__(self): 45 | super(ImdbNet, self).__init__() 46 | self.embedding = Embedding(num_embeddings=vocab_size, embedding_dim=64) 47 | self.lstm = LSTM(input_size=64, hidden_size=64) 48 | self.linear1 = Linear(in_features=64, out_features=64, act=tlx.nn.ReLU) 49 | self.linear2 = Linear(in_features=64, out_features=2) 50 | 51 | def forward(self, x): 52 | x = self.embedding(x) 53 | x, _ = self.lstm(x, [prev_h, prev_h]) 54 | x = tlx.reduce_mean(x, axis=1) 55 | x = self.linear1(x) 56 | x = self.linear2(x) 57 | return x 58 | 59 | 60 | n_epoch = 5 61 | batch_size = 64 62 | print_freq = 2 63 | 64 | train_dataset = ImdbDataset(X=X_train, y=y_train) 65 | train_loader = tlx.dataflow.DataLoader(train_dataset, batch_size=batch_size, shuffle=True) 66 | 67 | net = ImdbNet() 68 | print(net) 69 | optimizer = tlx.optimizers.Adam(1e-3) 70 | metric = tlx.metrics.Accuracy() 71 | loss_fn = tlx.losses.softmax_cross_entropy_with_logits 72 | model = tlx.model.Model(network=net, loss_fn=loss_fn, optimizer=optimizer, metrics=metric) 73 | model.train(n_epoch=n_epoch, train_dataset=train_loader, print_freq=print_freq, print_train_batch=True) 74 | -------------------------------------------------------------------------------- /examples/basic_tutorials/mnist_dataflow.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | # os.environ['TL_BACKEND'] = 'tensorflow' 6 | # os.environ['TL_BACKEND'] = 'mindspore' 7 | # os.environ['TL_BACKEND'] = 'paddle' 8 | # os.environ['TL_BACKEND'] = 'jittor' 9 | os.environ['TL_BACKEND'] = 'torch' 10 | 11 | 12 | import tensorlayerx as tlx 13 | from tensorlayerx.nn import Module 14 | from tensorlayerx.nn import Linear, Flatten 15 | from tensorlayerx.vision.transforms import Normalize, Compose 16 | from tensorlayerx.dataflow import Dataset, IterableDataset 17 | 18 | transform = Compose([Normalize(mean=[127.5], std=[127.5], data_format='HWC')]) 19 | 20 | print('download training data and load training data') 21 | 22 | X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1, 28, 28, 1)) 23 | X_train = X_train * 255 24 | 25 | print('load finished') 26 | 27 | 28 | class mnistdataset(Dataset): 29 | 30 | def __init__(self, data=X_train, label=y_train, transform=transform): 31 | self.data = data 32 | self.label = label 33 | self.transform = transform 34 | 35 | def __getitem__(self, index): 36 | data = self.data[index].astype('float32') 37 | data = self.transform(data) 38 | label = self.label[index].astype('int64') 39 | 40 | return data, label 41 | 42 | def __len__(self): 43 | 44 | return len(self.data) 45 | 46 | 47 | class mnistdataset1(IterableDataset): 48 | 49 | def __init__(self, data=X_train, label=y_train, transform=transform): 50 | self.data = data 51 | self.label = label 52 | self.transform = transform 53 | 54 | def __iter__(self): 55 | 56 | for i in range(len(self.data)): 57 | data = self.data[i].astype('float32') 58 | data = self.transform(data) 59 | label = self.label[i].astype('int64') 60 | yield data, label 61 | 62 | 63 | class MLP(Module): 64 | 65 | def __init__(self): 66 | super(MLP, self).__init__() 67 | self.linear1 = Linear(out_features=120, in_features=784, act=tlx.nn.ReLU) 68 | self.linear2 = Linear(out_features=84, in_features=120, act=tlx.nn.ReLU) 69 | self.linear3 = Linear(out_features=10, in_features=84) 70 | self.flatten = Flatten() 71 | 72 | def forward(self, x): 73 | x = self.flatten(x) 74 | x = self.linear1(x) 75 | x = self.linear2(x) 76 | x = self.linear3(x) 77 | return x 78 | 79 | 80 | train_dataset = mnistdataset1(data=X_train, label=y_train, transform=transform) 81 | train_loader = tlx.dataflow.DataLoader(train_dataset, batch_size=128, shuffle=False) 82 | 83 | for i in train_loader: 84 | print(i[0].shape, i[1]) 85 | -------------------------------------------------------------------------------- /examples/basic_tutorials/mnist_mlp_simple_train.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # The same set of code can switch the backend with one line 5 | import os 6 | # os.environ['TL_BACKEND'] = 'tensorflow' 7 | # os.environ['TL_BACKEND'] = 'mindspore' 8 | # os.environ['TL_BACKEND'] = 'paddle' 9 | # os.environ['TL_BACKEND'] = 'jittor' 10 | # os.environ['TL_BACKEND'] = 'oneflow' 11 | os.environ['TL_BACKEND'] = 'torch' 12 | 13 | import tensorlayerx as tlx 14 | from tensorlayerx.nn import Module 15 | from tensorlayerx.nn import Linear, Dropout 16 | from tensorlayerx.dataflow import Dataset, DataLoader 17 | 18 | # ################## Download and prepare the MNIST dataset ################## 19 | # This is just some way of getting the MNIST dataset from an online location and loading it into numpy arrays 20 | X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1, 784)) 21 | 22 | # ################## MNIST dataset ################## 23 | # We define a Dataset class for Loading MNIST images and labels. 24 | class mnistdataset(Dataset): 25 | 26 | def __init__(self, data=X_train, label=y_train): 27 | self.data = data 28 | self.label = label 29 | 30 | def __getitem__(self, index): 31 | data = self.data[index].astype('float32') 32 | label = self.label[index].astype('int64') 33 | return data, label 34 | 35 | def __len__(self): 36 | return len(self.data) 37 | 38 | # We use DataLoader to batch and shuffle data, and make data into iterators. 39 | train_dataset = mnistdataset(data=X_train, label=y_train) 40 | train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True) 41 | 42 | # ##################### Build the neural network model ####################### 43 | # This creates an MLP of two hidden Linear layers of 800 units each, followed by a Linear output layer of 10 units. 44 | 45 | class CustomModel(Module): 46 | 47 | def __init__(self): 48 | super(CustomModel, self).__init__() 49 | # It applies 20% dropout to each Linear layer. 50 | self.dropout1 = Dropout(p=0.2) 51 | # Linear layer with 800 units, using ReLU for output. 52 | self.linear1 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=784) 53 | self.dropout2 = Dropout(p=0.2) 54 | # Linear layer with 800 units, using ReLU for output. 55 | self.linear2 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=800) 56 | self.dropout3 = Dropout(p=0.2) 57 | # Linear layer with 10 units, using ReLU for output. 58 | self.linear3 = Linear(out_features=10, act=tlx.nn.ReLU, in_features=800) 59 | 60 | # We define the forward computation process. 61 | def forward(self, x): 62 | z = self.dropout1(x) 63 | z = self.linear1(z) 64 | z = self.dropout2(z) 65 | z = self.linear2(z) 66 | z = self.dropout3(z) 67 | out = self.linear3(z) 68 | return out 69 | 70 | # We initialize the network 71 | MLP = CustomModel() 72 | # Set the number of training cycles 73 | n_epoch = 50 74 | # set print frequency. 75 | print_freq = 2 76 | 77 | # Get training parameters 78 | train_weights = MLP.trainable_weights 79 | # Define the optimizer, use the Momentum optimizer, and set the learning rate to 0.05, momentum to 0.9 80 | optimizer = tlx.optimizers.Momentum(0.05, 0.9) 81 | # Define evaluation metrics. 82 | metric = tlx.metrics.Accuracy() 83 | # Define loss function, this operator implements the cross entropy loss function with softmax. This function 84 | # combines the calculation of the softmax operation and the cross entropy loss function 85 | # to provide a more numerically stable computing. 86 | loss_fn = tlx.losses.softmax_cross_entropy_with_logits 87 | 88 | # Using a simple training method without custom trianing loops. 89 | model = tlx.model.Model(network=MLP, loss_fn=loss_fn, optimizer=optimizer, metrics=metric) 90 | model.train(n_epoch=n_epoch, train_dataset=train_loader, print_freq=print_freq, print_train_batch=False) 91 | 92 | # Optionally, you could now dump the network weights to a file like this: 93 | # model.save_weights('./model.npz', format='npz_dict') 94 | # model.load_weights('./model.npz', format='npz_dict') -------------------------------------------------------------------------------- /examples/basic_tutorials/mnist_sequential.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | # os.environ['TL_BACKEND'] = 'tensorflow' 5 | # os.environ['TL_BACKEND'] = 'mindspore' 6 | # os.environ['TL_BACKEND'] = 'paddle' 7 | # os.environ['TL_BACKEND'] = 'jittor' 8 | os.environ['TL_BACKEND'] = 'torch' 9 | 10 | from tensorlayerx.nn import Sequential 11 | from tensorlayerx.nn import Linear 12 | import tensorlayerx as tlx 13 | from tensorlayerx.dataflow import Dataset 14 | 15 | layer_list = [] 16 | layer_list.append(Linear(out_features=800, act=tlx.nn.ReLU, in_features=784, name='linear1')) 17 | layer_list.append(Linear(out_features=800, act=tlx.nn.ReLU, in_features=800, name='linear2')) 18 | layer_list.append(Linear(out_features=10, act=tlx.nn.ReLU, in_features=800, name='linear3')) 19 | MLP = Sequential(layer_list) 20 | 21 | X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1, 784)) 22 | 23 | 24 | class mnistdataset(Dataset): 25 | 26 | def __init__(self, data=X_train, label=y_train): 27 | self.data = data 28 | self.label = label 29 | 30 | def __getitem__(self, index): 31 | data = self.data[index].astype('float32') 32 | label = self.label[index].astype('int64') 33 | 34 | return data, label 35 | 36 | def __len__(self): 37 | 38 | return len(self.data) 39 | 40 | 41 | n_epoch = 50 42 | batch_size = 128 43 | print_freq = 2 44 | shuffle_buffer_size = 128 45 | 46 | train_weights = MLP.trainable_weights 47 | optimizer = tlx.optimizers.Momentum(0.05, 0.9) 48 | train_dataset = mnistdataset(data=X_train, label=y_train) 49 | train_loader = tlx.dataflow.DataLoader(train_dataset, batch_size=batch_size, shuffle=True) 50 | metric = tlx.metrics.Accuracy() 51 | model = tlx.model.Model( 52 | network=MLP, loss_fn=tlx.losses.softmax_cross_entropy_with_logits, optimizer=optimizer, metrics=metric 53 | ) 54 | model.train(n_epoch=n_epoch, train_dataset=train_loader, print_freq=print_freq, print_train_batch=False) 55 | model.save_weights('./model.npz', format='npz_dict') 56 | model.load_weights('./model.npz', format='npz_dict') 57 | -------------------------------------------------------------------------------- /examples/basic_tutorials/module_container.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | # os.environ['TL_BACKEND'] = 'tensorflow' 6 | # os.environ['TL_BACKEND'] = 'mindspore' 7 | # os.environ['TL_BACKEND'] = 'jittor' 8 | # os.environ['TL_BACKEND'] = 'paddle' 9 | os.environ['TL_BACKEND'] = 'torch' 10 | 11 | 12 | import numpy as np 13 | from tensorlayerx.nn import Module, ModuleList, Linear, ModuleDict 14 | import tensorlayerx as tlx 15 | 16 | 17 | ####################### Holds submodules in a list ######################################## 18 | 19 | d1 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=784, name='linear1') 20 | d2 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=800, name='linear2') 21 | d3 = Linear(out_features=10, act=tlx.nn.ReLU, in_features=800, name='linear3') 22 | 23 | layer_list = ModuleList([d1, d2]) 24 | # Inserts a given d2 before a given index in the list 25 | layer_list.insert(1, d2) 26 | layer_list.insert(2, d2) 27 | # Appends d2 from a Python iterable to the end of the list. 28 | layer_list.extend([d2]) 29 | # Appends a given d3 to the end of the list. 30 | layer_list.append(d3) 31 | 32 | print(layer_list) 33 | 34 | 35 | class model(Module): 36 | 37 | def __init__(self): 38 | super(model, self).__init__() 39 | self._list = layer_list 40 | 41 | def forward(self, inputs): 42 | output = self._list[0](inputs) 43 | for i in range(1, len(self._list)): 44 | output = self._list[i](output) 45 | return output 46 | 47 | 48 | net = model() 49 | print(net.trainable_weights) 50 | print(net) 51 | print(net(tlx.nn.Input((10, 784)))) 52 | 53 | ####################### Holds submodules in a Dict ######################################## 54 | class MyModule(Module): 55 | 56 | def __init__(self): 57 | super(MyModule, self).__init__() 58 | self.dict = ModuleDict({ 59 | 'linear1': Linear(out_features=800, act=tlx.nn.ReLU, in_features=784, name='linear1'), 60 | 'linear2': Linear(out_features=800, act=tlx.nn.ReLU, in_features=800, name='linear2') 61 | }) 62 | def forward(self, x, linear): 63 | x = self.dict[linear](x) 64 | return x 65 | 66 | x = tlx.convert_to_tensor(np.ones(shape=(1,784)), dtype=tlx.float32) 67 | net = MyModule() 68 | x = net(x, 'linear1') 69 | print(x) -------------------------------------------------------------------------------- /examples/basic_tutorials/parameter_container.py: -------------------------------------------------------------------------------- 1 | import os 2 | # os.environ['TL_BACKEND'] = 'tensorflow' 3 | # os.environ['TL_BACKEND'] = 'mindspore' 4 | # os.environ['TL_BACKEND'] = 'paddle' 5 | os.environ['TL_BACKEND'] = 'torch' 6 | 7 | import tensorlayerx as tlx 8 | from tensorlayerx.nn import Module, Parameter, ParameterList, ParameterDict 9 | tlx.set_device(device='CPU', id = 0) 10 | 11 | class MyModule(Module): 12 | def __init__(self): 13 | super(MyModule, self).__init__() 14 | self.params1 = ParameterDict({ 15 | 'left': Parameter(tlx.ones((5, 10))), 16 | 'right': Parameter(tlx.zeros((5, 10))) 17 | }) 18 | 19 | self.params2 = ParameterList( 20 | [Parameter(tlx.ones((10,5))), Parameter(tlx.ones((5,10)))] 21 | ) 22 | 23 | def forward(self, x, choice): 24 | x = tlx.matmul(x, self.params1[choice]) 25 | x = tlx.matmul(x, self.params2[0]) 26 | x = tlx.matmul(x, self.params2[1]) 27 | return x 28 | 29 | input = tlx.nn.Input(shape=(5,5)) 30 | net = MyModule() 31 | trainable_weights = net.trainable_weights 32 | print("-----------------------------trainable_weights-------------------------------") 33 | for weight in trainable_weights: 34 | print(weight) 35 | print("-----------------------------------output------------------------------------") 36 | output = net(input, choice = 'right') 37 | print(output) -------------------------------------------------------------------------------- /examples/basic_tutorials/tensorflow_model_save_to_pb.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # When TensorFlow is the backend, save the model in .pb format. 5 | # Reference: https://leimao.github.io/blog/Save-Load-Inference-From-TF2-Frozen-Graph/ 6 | 7 | import os 8 | os.environ['TL_BACKEND'] = 'tensorflow' 9 | 10 | import numpy as np 11 | import tensorflow as tf 12 | import tensorlayerx as tlx 13 | from tensorlayerx.nn import Module 14 | from tensorlayerx.nn import Linear, Dropout, BatchNorm1d 15 | from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2 16 | 17 | 18 | class CustomModel(Module): 19 | 20 | def __init__(self): 21 | super(CustomModel, self).__init__() 22 | self.dropout1 = Dropout(p=0.2) 23 | self.linear1 = Linear(out_features=800, in_features=784) 24 | self.batchnorm = BatchNorm1d(act=tlx.nn.ReLU, num_features=800) 25 | self.dropout2 = Dropout(p=0.2) 26 | self.linear2 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=800) 27 | self.dropout3 = Dropout(p=0.2) 28 | self.linear3 = Linear(out_features=10, act=tlx.nn.ReLU, in_features=800) 29 | 30 | def forward(self, x): 31 | z = self.dropout1(x) 32 | z = self.linear1(z) 33 | z = self.batchnorm(z) 34 | z = self.dropout2(z) 35 | z = self.linear2(z) 36 | z = self.dropout3(z) 37 | out = self.linear3(z) 38 | return out 39 | 40 | # # forward can also be defined this way 41 | # def forward(self, x): 42 | # z = self.dropout1.forward(x) 43 | # z = self.linear1.forward(z) 44 | # z = self.batchnorm.forward(z) 45 | # z = self.dropout2.forward(z) 46 | # z = self.linear2.forward(z) 47 | # z = self.dropout3.forward(z) 48 | # out = self.linear3.forward(z) 49 | # return out 50 | 51 | @tf.function(experimental_relax_shapes=True) 52 | def infer(self, x): 53 | return self.forward(x) 54 | 55 | 56 | net = CustomModel() 57 | net.set_eval() 58 | 59 | # frozen graph 60 | input_signature = tf.TensorSpec([None, 784]) 61 | concrete_function = net.infer.get_concrete_function(x=input_signature) 62 | frozen_graph = convert_variables_to_constants_v2(concrete_function) 63 | frozen_graph_def = frozen_graph.graph.as_graph_def() 64 | tf.io.write_graph(graph_or_graph_def=frozen_graph_def, logdir="./", name=f"mlp.pb", as_text=False) 65 | 66 | # Because frozen graph has been sort of being deprecated by TensorFlow, and SavedModel format is encouraged to use, 67 | # we would have to use the TensorFlow 1.x function to load the frozen graph from hard drive. 68 | 69 | with tf.io.gfile.GFile("mlp.pb", "rb") as f: 70 | graph_def = tf.compat.v1.GraphDef() 71 | loaded = graph_def.ParseFromString(f.read()) 72 | 73 | with tf.Graph().as_default() as graph: 74 | tf.compat.v1.import_graph_def(graph_def, name="") 75 | 76 | x = graph.get_tensor_by_name("x:0") 77 | y = graph.get_tensor_by_name("Identity:0") 78 | 79 | bathc_image = np.ones([1, 784]) 80 | with tf.compat.v1.Session(graph=graph) as sess: 81 | feed_dict_testing = {x: bathc_image} 82 | out = sess.run(y, feed_dict=feed_dict_testing) 83 | print(out) 84 | -------------------------------------------------------------------------------- /examples/basic_tutorials/tensorlayerx_graph.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | # os.environ['TL_BACKEND'] = 'tensorflow' 6 | # os.environ['TL_BACKEND'] = 'mindspore' 7 | # os.environ['TL_BACKEND'] = 'jittor' 8 | os.environ['TL_BACKEND'] = 'torch' 9 | 10 | import tensorlayerx as tlx 11 | from tensorlayerx.nn import Module 12 | from tensorlayerx.nn import Linear, Conv2d, BatchNorm2d, MaxPool2d, Flatten 13 | 14 | class CNN(Module): 15 | 16 | def __init__(self): 17 | super(CNN, self).__init__() 18 | W_init = tlx.nn.initializers.truncated_normal(stddev=5e-2) 19 | W_init2 = tlx.nn.initializers.truncated_normal(stddev=0.04) 20 | b_init = tlx.nn.initializers.constant(value=0.1) 21 | b_init2 = tlx.nn.initializers.constant(value=0.1) 22 | 23 | self.conv1 = Conv2d(32, (5, 5), (1, 1), padding='SAME', W_init=W_init, b_init=b_init, name='conv1', in_channels=3) 24 | self.bn1 = BatchNorm2d(num_features=32, act=tlx.nn.ReLU) 25 | self.maxpool1 = MaxPool2d((2, 2), (2, 2), padding='SAME', name='pool1') 26 | 27 | self.conv2 = Conv2d(64, (5, 5), (1, 1), padding='SAME', act=tlx.nn.ReLU, W_init=W_init, b_init=b_init, name='conv2', in_channels=32) 28 | self.bn2 = BatchNorm2d(num_features=64, act=tlx.nn.ReLU) 29 | self.maxpool2 = MaxPool2d((2, 2), (2, 2), padding='SAME', name='pool2') 30 | 31 | self.flatten = Flatten(name='flatten') 32 | self.linear1 = Linear(1024, act=tlx.nn.ReLU, W_init=W_init2, b_init=b_init2, name='linear1relu', in_features=2304) 33 | 34 | self.linear2 = Linear(10, act=None, W_init=W_init2, b_init=b_init2, name='output', in_features=1024) 35 | 36 | def forward(self, x): 37 | z = self.conv1(x) 38 | z = self.bn1(z) 39 | z = self.maxpool1(z) 40 | z = self.conv2(z) 41 | z = self.bn2(z) 42 | z = self.maxpool2(z) 43 | z = self.flatten(z) 44 | z = self.linear1(z) 45 | z = self.linear2(z) 46 | return z 47 | 48 | model = CNN() 49 | inputs = tlx.nn.Input(shape=(3, 24, 24, 3)) 50 | outputs = model(inputs) 51 | 52 | node_by_depth, all_layers = model.build_graph(inputs) 53 | 54 | for depth, nodes in enumerate(node_by_depth): 55 | if depth == 0: 56 | if isinstance(inputs, list): 57 | assert len(inputs) == len(nodes) 58 | for idx, node in enumerate(nodes): 59 | print(node.node_name, node.layer) 60 | else: 61 | print(nodes[0].node_name, nodes[0].layer) 62 | else: 63 | for node in nodes: 64 | print(node.node_name, node.layer) -------------------------------------------------------------------------------- /examples/basic_tutorials/tensorlayerx_model_load.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | # os.environ['TL_BACKEND'] = 'tensorflow' 6 | # os.environ['TL_BACKEND'] = 'paddle' 7 | # os.environ['TL_BACKEND'] = 'mindspore' 8 | os.environ['TL_BACKEND'] = 'torch' 9 | 10 | import tensorlayerx as tlx 11 | from tensorlayerx.nn import Module 12 | from tensorlayerx.nn import Linear, Dropout, Conv2d, MaxPool2d, Flatten 13 | 14 | class CustomModel(Module): 15 | 16 | def __init__(self): 17 | super(CustomModel, self).__init__() 18 | self.dropout1 = Dropout(p=0.2) 19 | self.linear1 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=784, name='linear1') 20 | self.dropout2 = Dropout(p=0.8) 21 | self.linear2 = Linear(out_features=800, act=tlx.nn.ReLU, in_features=800, name='linear2') 22 | self.dropout3 = Dropout(p=0.8) 23 | self.linear3 = Linear(out_features=10, act=tlx.nn.ReLU, in_features=800, name='linear3') 24 | 25 | def forward(self, x, foo=None): 26 | z = self.dropout1(x) 27 | z = self.linear1(z) 28 | z = self.dropout2(z) 29 | z = self.linear2(z) 30 | z = self.dropout3(z) 31 | out = self.linear3(z) 32 | if foo is not None: 33 | out = tlx.relu(out) 34 | return out 35 | 36 | 37 | class CNN(Module): 38 | 39 | def __init__(self): 40 | super(CNN, self).__init__() 41 | # weights init 42 | W_init = tlx.nn.initializers.truncated_normal(stddev=5e-2) 43 | W_init2 = tlx.nn.initializers.truncated_normal(stddev=0.04) 44 | b_init2 = tlx.nn.initializers.constant(value=0.1) 45 | 46 | self.conv1 = Conv2d(64, (5, 5), (2, 2), padding='SAME', W_init=W_init, name='conv1', in_channels=3) 47 | # self.bn = BatchNorm2d(num_features=64, act=tlx.nn.ReLU) 48 | self.maxpool1 = MaxPool2d((3, 3), (2, 2), padding='SAME', name='pool1') 49 | 50 | self.conv2 = Conv2d( 51 | 64, (5, 5), (2, 2), padding='SAME', act=tlx.nn.ReLU, W_init=W_init, b_init=None, name='conv2', in_channels=64 52 | ) 53 | self.maxpool2 = MaxPool2d((3, 3), (2, 2), padding='SAME', name='pool2') 54 | 55 | self.flatten = Flatten(name='flatten') 56 | self.linear1 = Linear(384, act=tlx.nn.ReLU, W_init=W_init2, b_init=b_init2, name='linear1', in_features=256) 57 | self.linear2 = Linear(192, act=tlx.nn.ReLU, W_init=W_init2, b_init=b_init2, name='linear2', in_features=384) 58 | self.linear3 = Linear(10, act=None, W_init=W_init2, name='linear3', in_features=192) 59 | 60 | def forward(self, x): 61 | z = self.conv1(x) 62 | print("conv1 outputs:", z[1, :, :, 1]) 63 | z = self.maxpool1(z) 64 | print("maxpool outputs:", z[1, :, :, 1]) 65 | z = self.conv2(z) 66 | print("conv2 outputs:", z[1, :, :, 1]) 67 | z = self.maxpool2(z) 68 | print("max2 outputs:", z[1, :, :, 1]) 69 | z = self.flatten(z) 70 | z = self.linear1(z) 71 | z = self.linear2(z) 72 | z = self.linear3(z) 73 | return z 74 | 75 | 76 | # # TODO The MLP model was saved to the standard npz_dict format after training at the TensorFlow backend 77 | # # and imported into TensorFlow/PyTorch/PaddlePaddle/MindSpore. 78 | # MLP = CustomModel() 79 | # # MLP.save_standard_weights('./model.npz') 80 | # MLP.load_standard_weights('./model.npz', weights_from='tensorflow', weights_to='mindspore') 81 | # MLP.set_eval() 82 | # inputs = tlx.layers.Input(shape=(10, 784)) 83 | # output = MLP(inputs) 84 | # print(output) 85 | 86 | # TODO The CNN model was saved to the standard npz_dict format after training at the TensorFlow backend 87 | # and imported into TensorFlow/PyTorch/PaddlePaddle/MindSpore. 88 | cnn = CNN() 89 | # cnn.save_standard_weights('./cnn.npz') 90 | cnn.load_standard_weights('./cnn.npz', weights_from='torch', weights_to='tensorflow') 91 | cnn.set_eval() 92 | 93 | inputs = tlx.nn.Input(shape=(10, 28, 28, 3), dtype=tlx.float32) 94 | outputs = cnn(inputs) 95 | # print(outputs) 96 | -------------------------------------------------------------------------------- /examples/basic_tutorials/test_dist.sh: -------------------------------------------------------------------------------- 1 | export MLU_VISIBLE_DEVICES=0,1 2 | python -m tensorlayerx.distributed.launch --nproc_per_node=2 cifar10_cnn_torch_dist.py 3 | -------------------------------------------------------------------------------- /examples/model_zoo/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .vgg import vgg16, vgg19 5 | from .yolo import YOLOv4 6 | from .resnet import ResNet50 7 | -------------------------------------------------------------------------------- /examples/model_zoo/model/coco.names: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | potted plant 60 | bed 61 | dining table 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /examples/model_zoo/model/resnet50_weights_tf_dim_ordering_tf_kernels.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/examples/model_zoo/model/resnet50_weights_tf_dim_ordering_tf_kernels.h5 -------------------------------------------------------------------------------- /examples/model_zoo/pretrained_resnet50.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | ResNet50 for ImageNet using TL model 5 | 6 | """ 7 | 8 | import time 9 | import numpy as np 10 | import tensorlayerx as tlx 11 | from examples.model_zoo.imagenet_classes import class_names 12 | from examples.model_zoo.resnet import ResNet50 13 | 14 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 15 | 16 | # get the whole model 17 | resnet = ResNet50(pretrained=True) 18 | resnet.set_eval() 19 | 20 | img1 = tlx.vision.load_image('data/tiger.jpeg') 21 | img1 = tlx.vision.transforms.transforms.Resize((224, 224))(img1)[:, :, ::-1] 22 | img1 = img1 - np.array([103.939, 116.779, 123.68]).reshape((1, 1, 3)) 23 | 24 | img1 = img1.astype(np.float32)[np.newaxis, ...] 25 | 26 | start_time = time.time() 27 | output = resnet(img1) 28 | prob = tlx.softmax(output)[0].numpy() 29 | print(" End time : %.5ss" % (time.time() - start_time)) 30 | preds = (np.argsort(prob)[::-1])[0:5] 31 | for p in preds: 32 | print(class_names[p], prob[p]) 33 | -------------------------------------------------------------------------------- /examples/model_zoo/pretrained_vgg16.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """VGG-16 for ImageNet using TL model.""" 4 | 5 | import time 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayerx as tlx 11 | from examples.model_zoo.imagenet_classes import class_names 12 | from examples.model_zoo.vgg import vgg16 13 | 14 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 15 | 16 | # get the whole model 17 | vgg = vgg16(pretrained=True) 18 | vgg.set_eval() 19 | 20 | img = tlx.vision.load_image('data/tiger.jpeg') 21 | img = tlx.vision.transforms.transforms.Resize((224, 224))(img).astype(np.float32) / 255 22 | 23 | start_time = time.time() 24 | output = vgg(img) 25 | probs = tf.nn.softmax(output)[0].numpy() 26 | print(" End time : %.5ss" % (time.time() - start_time)) 27 | preds = (np.argsort(probs)[::-1])[0:5] 28 | for p in preds: 29 | print(class_names[p], probs[p]) 30 | -------------------------------------------------------------------------------- /examples/model_zoo/pretrained_yolov4.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import numpy as np 5 | import cv2 6 | from PIL import Image 7 | from examples.model_zoo.common import yolo4_input_processing, yolo4_output_processing, \ 8 | result_to_json, read_class_names, draw_boxes_and_labels_to_image_with_json 9 | from examples.model_zoo.yolo import YOLOv4 10 | import tensorlayerx as tlx 11 | 12 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 13 | 14 | INPUT_SIZE = 416 15 | image_path = './data/kite.jpg' 16 | 17 | class_names = read_class_names('./model/coco.names') 18 | original_image = cv2.imread(image_path) 19 | image = cv2.cvtColor(np.array(original_image), cv2.COLOR_BGR2RGB) 20 | 21 | model = YOLOv4(NUM_CLASS=80, pretrained=True) 22 | model.set_eval() 23 | 24 | batch_data = yolo4_input_processing(original_image) 25 | feature_maps = model(batch_data) 26 | pred_bbox = yolo4_output_processing(feature_maps) 27 | json_result = result_to_json(image, pred_bbox) 28 | 29 | image = draw_boxes_and_labels_to_image_with_json(image, json_result, class_names) 30 | image = Image.fromarray(image.astype(np.uint8)) 31 | image.show() 32 | -------------------------------------------------------------------------------- /requirements/requirements.txt: -------------------------------------------------------------------------------- 1 | opencv-python>=4.5.1.48 2 | numpy>=1.16 3 | progressbar2>=3.39.3 4 | requests>=2.21.0 5 | scikit-learn>=0.21.0 6 | scikit-image>=0.15.0 7 | scipy>=1.2.1 8 | wrapt>=1.11.1 9 | h5py>=2.9 10 | cloudpickle>=0.8.1 11 | tensorboardX>=2.5 12 | rich>=12.2 13 | six==1.15.0 14 | -------------------------------------------------------------------------------- /requirements/requirements_contrib_loggers.txt: -------------------------------------------------------------------------------- 1 | hyperdash>=0.15,<0.16 -------------------------------------------------------------------------------- /requirements/requirements_db.txt: -------------------------------------------------------------------------------- 1 | pymongo>=3.8.0 2 | -------------------------------------------------------------------------------- /requirements/requirements_dev.txt: -------------------------------------------------------------------------------- 1 | autopep8>=1.3,<1.5 2 | -------------------------------------------------------------------------------- /requirements/requirements_doc.txt: -------------------------------------------------------------------------------- 1 | flake8-docstrings>=1.3,<1.4 2 | pycodestyle>=2.5.0 3 | pydocstyle>=2.1,<3.1 4 | sphinx==2.0.1 5 | jinja2==3.0.3 6 | sphinx_rtd_theme>=0.4,<0.5 7 | wrapt>=1.11.1 8 | h5py>=2.9 9 | cloudpickle>=0.8.1 10 | tensorflow>=2.0.0 11 | sphinx-mathjax-offline 12 | protobuf==3.20.* 13 | -------------------------------------------------------------------------------- /requirements/requirements_extra.txt: -------------------------------------------------------------------------------- 1 | nltk>=3.3,<3.5 2 | matplotlib>=2.2,<3.1 3 | requests>=2.21.0 4 | tqdm>=4.31.1 5 | lxml>=4.3.3 6 | -------------------------------------------------------------------------------- /requirements/requirements_ms.txt: -------------------------------------------------------------------------------- 1 | mindspore==1.8.1 2 | -------------------------------------------------------------------------------- /requirements/requirements_paddle.txt: -------------------------------------------------------------------------------- 1 | paddlepaddle==2.2.0 -------------------------------------------------------------------------------- /requirements/requirements_test.txt: -------------------------------------------------------------------------------- 1 | keras>=2.2,<2.3 2 | pycodestyle>=2.5.0 3 | pydocstyle>=2.1,<3.1 4 | pytest>=4.5.0 5 | pytest-cache>=1.0,<1.1 6 | pytest-cov>=2.7.1 7 | pytest-xdist>=1.28.0 8 | sphinx==2.0.1 9 | yapf==0.29.0 10 | autoflake==1.3.1 11 | isort==4.3.21 12 | -------------------------------------------------------------------------------- /requirements/requirements_tf_cpu.txt: -------------------------------------------------------------------------------- 1 | tensorflow==2.4.0 -------------------------------------------------------------------------------- /requirements/requirements_tf_gpu.txt: -------------------------------------------------------------------------------- 1 | tensorflow-gpu==2.4.0 2 | -------------------------------------------------------------------------------- /requirements/requirements_torch.txt: -------------------------------------------------------------------------------- 1 | torch==1.10.0 2 | -------------------------------------------------------------------------------- /runs/mlp/events.out.tfevents.1722986988.LAPTOP-48J7839G: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/runs/mlp/events.out.tfevents.1722986988.LAPTOP-48J7839G -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [tool:pytest] 2 | testpaths = tests/ 3 | 4 | [flake8] 5 | max-line-length = 120 6 | ignore = 7 | D301 8 | E221 # Space before equal sign 9 | E251 # Space after equal sign 10 | exclude = 11 | .git, 12 | venv, 13 | __pycache__, 14 | .pytest_cache, 15 | tensorlayerx.egg-info, 16 | build, 17 | dist, 18 | img 19 | 20 | [yapf] 21 | based_on_style=google 22 | 23 | # The number of columns to use for indentation. 24 | indent_width = 4 25 | 26 | # The column limit. (larger than usual) 27 | column_limit=120 28 | 29 | # Place each dictionary entry onto its own line. 30 | each_dict_entry_on_separate_line = True 31 | 32 | # Put closing brackets on a separate line, dedented, if the bracketed 33 | # expression can't fit in a single line. Applies to all kinds of brackets, 34 | # including function definitions and calls. For example: 35 | # 36 | # config = { 37 | # 'key1': 'value1', 38 | # 'key2': 'value2', 39 | # } # <--- this bracket is dedented and on a separate line 40 | # 41 | # time_series = self.remote_client.query_entity_counters( 42 | # entity='dev3246.region1', 43 | # key='dns.query_latency_tcp', 44 | # transform=Transformation.AVERAGE(window=timedelta(seconds=60)), 45 | # start_ts=now()-timedelta(days=3), 46 | # end_ts=now(), 47 | # ) # <--- this bracket is dedented and on a separate line 48 | dedent_closing_brackets=True 49 | 50 | # Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set 51 | coalesce_brackets = False 52 | 53 | # Align closing bracket with visual indentation. 54 | align_closing_bracket_with_visual_indent = False 55 | 56 | # Split named assignments onto individual lines. 57 | split_before_named_assigns = False 58 | 59 | # If an argument / parameter list is going to be split, then split before the first argument. 60 | split_before_first_argument = True 61 | 62 | # Split before arguments if the argument list is terminated by a comma. 63 | split_arguments_when_comma_terminated = False 64 | 65 | # Insert a space between the ending comma and closing bracket of a list, etc. 66 | space_between_ending_comma_and_closing_bracket = True 67 | 68 | # Join short lines into one line. E.g., single line if statements. 69 | join_multiple_lines = True 70 | 71 | # Do not include spaces around selected binary operators. 72 | # Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5 73 | no_spaces_around_selected_binary_operators = True 74 | 75 | # Allow lambdas to be formatted on more than one line. 76 | allow_multiline_lambdas = True 77 | 78 | SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = 10 79 | SPLIT_PENALTY_AFTER_OPENING_BRACKET = 500 80 | -------------------------------------------------------------------------------- /tensorlayerx/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """Deep learning and Reinforcement learning library for Researchers and Engineers""" 4 | 5 | # import backend 6 | from .backend import * 7 | import warnings 8 | import os 9 | 10 | from tensorlayerx.package_info import ( 11 | VERSION, __contact_emails__, __contact_names__, __description__, __download_url__, __homepage__, __keywords__, 12 | __license__, __package_name__, __repository_url__, __shortversion__, __version__ 13 | ) 14 | 15 | if 'TENSORLAYER_PACKAGE_BUILDING' not in os.environ: 16 | 17 | from tensorlayerx import nn 18 | from .nn import core 19 | from .nn import layers 20 | from .nn import initializers 21 | from tensorlayerx import losses 22 | from tensorlayerx import decorators 23 | from tensorlayerx import files 24 | from tensorlayerx import logging 25 | from tensorlayerx import model 26 | from tensorlayerx import optimizers 27 | from tensorlayerx import dataflow 28 | from tensorlayerx import metrics 29 | from tensorlayerx import vision 30 | 31 | from tensorlayerx.utils.lazy_imports import LazyImport 32 | 33 | # global vars 34 | global_flag = {} 35 | global_dict = {} 36 | 37 | backend_v = { 38 | 'tensorflow': '2.4.0', 39 | 'mindspore': '1.8.1', 40 | 'paddle': '2.2.0', 41 | 'torch': '1.10.0', 42 | 'jittor': '1.3.8.5', 43 | 'oneflow':'0.9.0' 44 | } 45 | 46 | if BACKEND_VERSION != backend_v[BACKEND]: 47 | warnings.warn("The version of the backend you have installed does not match the specified backend version " 48 | "and may not work, please install version {} {}.".format(BACKEND, backend_v[BACKEND])) -------------------------------------------------------------------------------- /tensorlayerx/backend/ops/load_backend.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import json 5 | import os 6 | import sys 7 | 8 | # BACKEND = 'tensorflow' 9 | # BACKEND = 'mindspore' 10 | # BACKEND = 'paddle' 11 | # BACKEND = 'torch' 12 | BACKEND = 'jittor' 13 | # BACKEND = 'oneflow' 14 | 15 | # Check for backend.json files 16 | tl_backend_dir = os.path.expanduser('~') 17 | if not os.access(tl_backend_dir, os.W_OK): 18 | tl_backend_dir = '/tmp' 19 | tl_dir = os.path.join(tl_backend_dir, '.tl') 20 | 21 | config = { 22 | 'backend': BACKEND, 23 | } 24 | if not os.path.exists(tl_dir): 25 | path = os.path.join(tl_dir, 'tl_backend.json') 26 | os.makedirs(tl_dir) 27 | with open(path, "w") as f: 28 | json.dump(config, f) 29 | BACKEND = config['backend'] 30 | sys.stderr.write("Create the backend configuration file :" + path + '\n') 31 | else: 32 | path = os.path.join(tl_dir, 'tl_backend.json') 33 | with open(path, 'r') as load_f: 34 | load_dict = json.load(load_f) 35 | if load_dict['backend'] is not config['backend']: 36 | BACKEND = config['backend'] 37 | else: 38 | BACKEND = load_dict['backend'] 39 | 40 | # Set backend based on TL_BACKEND. 41 | if 'TL_BACKEND' in os.environ: 42 | backend = os.environ['TL_BACKEND'] 43 | if backend: 44 | BACKEND = backend 45 | 46 | # import backend functions 47 | if BACKEND == 'tensorflow': 48 | from .tensorflow_backend import * 49 | from .tensorflow_nn import * 50 | import tensorflow as tf 51 | BACKEND_VERSION = tf.__version__ 52 | sys.stderr.write('Using TensorFlow backend.\n') 53 | 54 | elif BACKEND == 'mindspore': 55 | from .mindspore_backend import * 56 | from .mindspore_nn import * 57 | import mindspore as ms 58 | BACKEND_VERSION = ms.__version__ 59 | # set context 60 | import mindspore.context as context 61 | import os 62 | os.environ['DEVICE_ID'] = '0' 63 | context.set_context(mode=context.PYNATIVE_MODE), 64 | # context.set_context(mode=context.PYNATIVE_MODE, device_target='CPU'), 65 | # enable_task_sink=True, enable_loop_sink=True) 66 | # context.set_context(mode=context.PYNATIVE_MODE, device_target='Ascend') 67 | sys.stderr.write('Using MindSpore backend.\n') 68 | 69 | elif BACKEND == 'paddle': 70 | from .paddle_backend import * 71 | from .paddle_nn import * 72 | import paddle as pd 73 | BACKEND_VERSION = pd.__version__ 74 | sys.stderr.write('Using Paddle backend.\n') 75 | elif BACKEND == 'torch': 76 | from .torch_nn import * 77 | from .torch_backend import * 78 | import torch 79 | try: 80 | import torch_mlu 81 | except: 82 | pass 83 | BACKEND_VERSION = torch.__version__ 84 | sys.stderr.write('Using PyTorch backend.\n') 85 | elif BACKEND == 'oneflow': 86 | from .oneflow_nn import * 87 | from .oneflow_backend import * 88 | import oneflow as flow 89 | BACKEND_VERSION = flow.__version__ 90 | 91 | sys.stderr.write('Using OneFlow backend.\n') 92 | 93 | elif BACKEND == 'jittor': 94 | from .jittor_nn import * 95 | from .jittor_backend import * 96 | import jittor as jt 97 | BACKEND_VERSION = jt.__version__ 98 | sys.stderr.write('Using jittor backend.\n') 99 | 100 | else: 101 | raise NotImplementedError("This backend is not supported") 102 | -------------------------------------------------------------------------------- /tensorlayerx/dataflow/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | from __future__ import absolute_import, division, print_function 4 | from .dataloader import * 5 | from .sampler import * 6 | from .dataset import * -------------------------------------------------------------------------------- /tensorlayerx/decorators/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | TensorLayer provides rich layer implementations trailed for 5 | various benchmarks and domain-specific problems. In addition, we also 6 | support transparent access to native TensorFlow parameters. 7 | For example, we provide not only layers for local response normalization, but also 8 | layers that allow user to apply ``tf.ops.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .deprecated import deprecated 13 | from .deprecated_alias import deprecated_alias 14 | from .method_decorator import private_method, protected_method 15 | 16 | __all__ = ['deprecated', 'deprecated_alias', 'private_method', 'protected_method'] 17 | -------------------------------------------------------------------------------- /tensorlayerx/decorators/deprecated.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import functools 5 | import inspect 6 | import sys 7 | 8 | import wrapt 9 | 10 | from tensorlayerx.decorators.utils import ( 11 | add_deprecation_notice_to_docstring, get_qualified_name, validate_deprecation_args 12 | ) 13 | 14 | __all__ = ['deprecated'] 15 | 16 | # Allow deprecation warnings to be silenced temporarily with a context manager. 17 | _PRINT_DEPRECATION_WARNINGS = True 18 | 19 | # Remember which deprecation warnings have been printed already. 20 | _PRINTED_WARNING = {} 21 | 22 | 23 | def deprecated(wrapped=None, date='', instructions='', warn_once=True): 24 | 25 | if wrapped is None: 26 | return functools.partial(deprecated, date=date, instructions=instructions, warn_once=warn_once) 27 | 28 | @wrapt.decorator 29 | def wrapper(wrapped, instance=None, args=None, kwargs=None): 30 | 31 | validate_deprecation_args(date, instructions) 32 | 33 | if _PRINT_DEPRECATION_WARNINGS: 34 | 35 | class_or_func_name = get_qualified_name(wrapped) 36 | 37 | if class_or_func_name not in _PRINTED_WARNING: 38 | if warn_once: 39 | _PRINTED_WARNING[class_or_func_name] = True 40 | 41 | from tensorlayerx import logging 42 | 43 | logging.warning( 44 | '%s: `%s.%s` (in file: %s) is deprecated and will be removed %s.\n' 45 | 'Instructions for updating: %s\n' % ( 46 | "Class" if inspect.isclass(wrapped) else "Function", wrapped.__module__, class_or_func_name, 47 | wrapped.__code__.co_filename, 'in a future version' if date is None else 48 | ('after %s' % date), instructions 49 | ) 50 | ) 51 | 52 | return wrapped(*args, **kwargs) 53 | 54 | decorated = wrapper(wrapped) 55 | 56 | if sys.version_info > (3, 0): # docstring can only be edited with Python 3 57 | wrapt.FunctionWrapper.__setattr__( 58 | decorated, "__doc__", add_deprecation_notice_to_docstring(wrapped.__doc__, date, instructions) 59 | ) 60 | 61 | return decorated 62 | -------------------------------------------------------------------------------- /tensorlayerx/decorators/deprecated_alias.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import functools 5 | import warnings 6 | 7 | from tensorlayerx import logging 8 | 9 | 10 | def deprecated_alias(end_support_version, **aliases): 11 | 12 | def deco(f): 13 | 14 | @functools.wraps(f) 15 | def wrapper(*args, **kwargs): 16 | 17 | try: 18 | func_name = "{}.{}".format(args[0].__class__.__name__, f.__name__) 19 | except (NameError, IndexError): 20 | func_name = f.__name__ 21 | 22 | rename_kwargs(kwargs, aliases, end_support_version, func_name) 23 | 24 | return f(*args, **kwargs) 25 | 26 | return wrapper 27 | 28 | return deco 29 | 30 | 31 | def rename_kwargs(kwargs, aliases, end_support_version, func_name): 32 | 33 | for alias, new in aliases.items(): 34 | 35 | if alias in kwargs: 36 | 37 | if new in kwargs: 38 | raise TypeError('{}() received both {} and {}'.format(func_name, alias, new)) 39 | 40 | warnings.warn('{}() - {} is deprecated; use {}'.format(func_name, alias, new), DeprecationWarning) 41 | logging.warning( 42 | "DeprecationWarning: {}(): " 43 | "`{}` argument is deprecated and will be removed in version {}, " 44 | "please change for `{}.`".format(func_name, alias, end_support_version, new) 45 | ) 46 | kwargs[new] = kwargs.pop(alias) 47 | -------------------------------------------------------------------------------- /tensorlayerx/decorators/method_decorator.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import inspect 5 | 6 | 7 | def private_method(func): 8 | """Decorator for making an instance method private.""" 9 | 10 | def func_wrapper(*args, **kwargs): 11 | """Decorator wrapper function.""" 12 | outer_frame = inspect.stack()[1][0] 13 | if 'self' not in outer_frame.f_locals or outer_frame.f_locals['self'] is not args[0]: 14 | raise RuntimeError('%s.%s is a private method' % (args[0].__class__.__name__, func.__name__)) 15 | 16 | return func(*args, **kwargs) 17 | 18 | return func_wrapper 19 | 20 | 21 | def protected_method(func): 22 | """Decorator for making an instance method private.""" 23 | 24 | def func_wrapper(*args, **kwargs): 25 | """Decorator wrapper function.""" 26 | outer_frame = inspect.stack()[1][0] 27 | 28 | caller = inspect.getmro(outer_frame.f_locals['self'].__class__)[:-1] 29 | target = inspect.getmro(args[0].__class__)[:-1] 30 | 31 | share_subsclass = False 32 | 33 | for cls_ in target: 34 | if issubclass(caller[0], cls_) or caller[0] is cls_: 35 | share_subsclass = True 36 | break 37 | 38 | if ('self' not in outer_frame.f_locals or 39 | outer_frame.f_locals['self'] is not args[0]) and (not share_subsclass): 40 | raise RuntimeError('%s.%s is a protected method' % (args[0].__class__.__name__, func.__name__)) 41 | 42 | return func(*args, **kwargs) 43 | 44 | return func_wrapper 45 | -------------------------------------------------------------------------------- /tensorlayerx/distributed/__init__.py: -------------------------------------------------------------------------------- 1 | from .launch import * -------------------------------------------------------------------------------- /tensorlayerx/distributed/launch.py: -------------------------------------------------------------------------------- 1 | import os 2 | BACKEND = 'torch' 3 | 4 | 5 | # Set backend based on TL_BACKEND. 6 | if 'TL_BACKEND' in os.environ: 7 | backend = os.environ['TL_BACKEND'] 8 | if backend: 9 | BACKEND = backend 10 | 11 | 12 | def main(args=None): 13 | if BACKEND == 'torch': 14 | from torch.distributed.run import get_args_parser, run 15 | def parse_args(args): 16 | parser = get_args_parser() 17 | parser.add_argument( 18 | "--use_env", 19 | default=False, 20 | action="store_true", 21 | help="Use environment variable to pass " 22 | "'local rank'. For legacy reasons, the default value is False. " 23 | "If set to True, the script will not pass " 24 | "--local_rank as argument, and will instead set LOCAL_RANK.", 25 | ) 26 | return parser.parse_args(args) 27 | args = parse_args(args) 28 | run(args) 29 | else: 30 | raise NotImplementedError("This backend:{} is not supported".format(BACKEND)) 31 | 32 | 33 | if __name__ == "__main__": 34 | main() -------------------------------------------------------------------------------- /tensorlayerx/files/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .dataset_loaders.celebA_dataset import * 5 | from .dataset_loaders.cifar10_dataset import * 6 | from .dataset_loaders.cyclegan_dataset import * 7 | from .dataset_loaders.flickr_1M_dataset import * 8 | from .dataset_loaders.flickr_25k_dataset import * 9 | from .dataset_loaders.imdb_dataset import * 10 | from .dataset_loaders.matt_mahoney_dataset import * 11 | from .dataset_loaders.mnist_dataset import * 12 | from .dataset_loaders.mnist_fashion_dataset import * 13 | from .dataset_loaders.mpii_dataset import * 14 | from .dataset_loaders.nietzsche_dataset import * 15 | # from .dataset_loaders.ptb_dataset import * 16 | # from .dataset_loaders.voc_dataset import * 17 | # from .dataset_loaders.wmt_en_fr_dataset import * 18 | from .utils import * 19 | 20 | __all__ = [ 21 | # Dataset Loaders 22 | 'load_celebA_dataset', 23 | 'load_cifar10_dataset', 24 | 'load_cyclegan_dataset', 25 | 'load_fashion_mnist_dataset', 26 | 'load_flickr1M_dataset', 27 | 'load_flickr25k_dataset', 28 | 'load_imdb_dataset', 29 | 'load_matt_mahoney_text8_dataset', 30 | 'load_mnist_dataset', 31 | 'load_mpii_pose_dataset', 32 | 'load_nietzsche_dataset', 33 | # 'load_ptb_dataset', 34 | # 'load_voc_dataset', 35 | # 'load_wmt_en_fr_dataset', 36 | 37 | # Util Functions 38 | 'assign_weights', 39 | 'del_file', 40 | 'del_folder', 41 | 'download_file_from_google_drive', 42 | 'exists_or_mkdir', 43 | 'file_exists', 44 | 'folder_exists', 45 | 'load_and_assign_npz', 46 | 'load_and_assign_npz_dict', 47 | 'load_ckpt', 48 | 'load_cropped_svhn', 49 | 'load_file_list', 50 | 'load_folder_list', 51 | 'load_npy_to_any', 52 | 'load_npz', 53 | 'maybe_download_and_extract', 54 | 'natural_keys', 55 | 'npz_to_W_pdf', 56 | 'read_file', 57 | 'save_any_to_npy', 58 | 'save_ckpt', 59 | 'save_npz', 60 | 'save_npz_dict', 61 | 'load_and_assign_ckpt', 62 | 'ckpt_to_npz_dict' 63 | #'save_graph', 64 | #'load_graph', 65 | #'save_graph_and_params', 66 | #'load_graph_and_params', 67 | ] 68 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .celebA_dataset import * 5 | from .cifar10_dataset import * 6 | from .cyclegan_dataset import * 7 | from .flickr_1M_dataset import * 8 | from .flickr_25k_dataset import * 9 | from .imdb_dataset import * 10 | from .matt_mahoney_dataset import * 11 | from .mnist_dataset import * 12 | from .mnist_fashion_dataset import * 13 | from .mpii_dataset import * 14 | from .nietzsche_dataset import * 15 | # from .ptb_dataset import * 16 | # from .voc_dataset import * 17 | # from .wmt_en_fr_dataset import * 18 | 19 | __all__ = [ 20 | 'load_celebA_dataset', 21 | 'load_cifar10_dataset', 22 | 'load_cyclegan_dataset', 23 | 'load_fashion_mnist_dataset', 24 | 'load_flickr1M_dataset', 25 | 'load_flickr25k_dataset', 26 | 'load_imdb_dataset', 27 | 'load_matt_mahoney_text8_dataset', 28 | 'load_mnist_dataset', 29 | 'load_mpii_pose_dataset', 30 | 'load_nietzsche_dataset', 31 | # 'load_ptb_dataset', 32 | # 'load_voc_dataset', 33 | # 'load_wmt_en_fr_dataset', 34 | ] 35 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/celebA_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import zipfile 6 | from tensorlayerx import logging 7 | from tensorlayerx.files.utils import (download_file_from_google_drive, exists_or_mkdir, load_file_list) 8 | logging.set_verbosity(logging.INFO) 9 | __all__ = ['load_celebA_dataset'] 10 | 11 | 12 | def load_celebA_dataset(path='data'): 13 | """Load CelebA dataset 14 | 15 | Return a list of image path. 16 | 17 | Parameters 18 | ----------- 19 | path : str 20 | The path that the data is downloaded to, defaults is ``data/celebA/``. 21 | 22 | """ 23 | logging.info("The dataset is stored on google drive, if you can't download it from google drive, " 24 | "please download it from the official website manually. " 25 | "Large-scale CelebFaces Attributes (CelebA) Dataset . " 26 | "Please place dataset 'img_align_celeba.zip' under 'data/celebA/' by default.") 27 | 28 | data_dir = 'celebA' 29 | filename, drive_id = "img_align_celeba.zip", "0B7EVK8r0v71pZjFTYXZWM3FlRnM" 30 | file_path = os.path.join(path, data_dir) 31 | image_path = os.path.join(path, data_dir, "img_align_celeba") 32 | save_path = os.path.join(path, data_dir, filename) 33 | if os.path.exists(image_path): 34 | logging.info('[*] {} already exists'.format(image_path)) 35 | else: 36 | if not os.path.exists(save_path): 37 | exists_or_mkdir(file_path) 38 | download_file_from_google_drive(drive_id, save_path) 39 | zip_dir = '' 40 | with zipfile.ZipFile(save_path) as zf: 41 | zip_dir = zf.namelist()[0] 42 | zf.extractall(file_path) 43 | # os.remove(save_path) 44 | # os.rename(os.path.join(path, zip_dir), image_path) 45 | 46 | data_files = load_file_list(path=image_path, regx='\\.jpg', printable=False) 47 | for i, _v in enumerate(data_files): 48 | data_files[i] = os.path.join(image_path, data_files[i]) 49 | return data_files 50 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/cyclegan_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | import numpy as np 7 | 8 | from tensorlayerx import logging 9 | from tensorlayerx.vision import load_images 10 | from tensorlayerx.files.utils import (del_file, folder_exists, load_file_list, maybe_download_and_extract) 11 | logging.set_verbosity(logging.INFO) 12 | __all__ = ['load_cyclegan_dataset'] 13 | 14 | 15 | def load_cyclegan_dataset(filename='summer2winter_yosemite', path='data'): 16 | """Load images from CycleGAN's database, see `this link `__. 17 | 18 | Parameters 19 | ------------ 20 | filename : str 21 | The dataset you want, see `this link `__. 22 | path : str 23 | The path that the data is downloaded to, defaults is `data/cyclegan` 24 | 25 | Examples 26 | --------- 27 | >>> im_train_A, im_train_B, im_test_A, im_test_B = load_cyclegan_dataset(filename='summer2winter_yosemite') 28 | 29 | """ 30 | path = os.path.join(path, 'cyclegan') 31 | url = 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/' 32 | 33 | logging.info("If can't download this dataset automatically, " 34 | "please download it from the official website manually." 35 | "cyclegan Dataset ." 36 | "Please place dataset under 'data/cyclegan/' by default.") 37 | 38 | if folder_exists(os.path.join(path, filename)) is False: 39 | logging.info("[*] {} is nonexistent in {}".format(filename, path)) 40 | maybe_download_and_extract(filename + '.zip', path, url, extract=True) 41 | del_file(os.path.join(path, filename + '.zip')) 42 | 43 | def load_image_from_folder(path): 44 | return load_images(path=path, n_threads=10) 45 | 46 | im_train_A = load_image_from_folder(os.path.join(path, filename, "trainA")) 47 | im_train_B = load_image_from_folder(os.path.join(path, filename, "trainB")) 48 | im_test_A = load_image_from_folder(os.path.join(path, filename, "testA")) 49 | im_test_B = load_image_from_folder(os.path.join(path, filename, "testB")) 50 | 51 | def if_2d_to_3d(images): # [h, w] --> [h, w, 3] 52 | for i, _v in enumerate(images): 53 | if len(images[i].shape) == 2: 54 | images[i] = images[i][:, :, np.newaxis] 55 | images[i] = np.tile(images[i], (1, 1, 3)) 56 | return images 57 | 58 | im_train_A = if_2d_to_3d(im_train_A) 59 | im_train_B = if_2d_to_3d(im_train_B) 60 | im_test_A = if_2d_to_3d(im_test_A) 61 | im_test_B = if_2d_to_3d(im_test_B) 62 | 63 | return im_train_A, im_train_B, im_test_A, im_test_B 64 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/flickr_25k_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | from tensorlayerx import logging 7 | from tensorlayerx.vision import load_images 8 | logging.set_verbosity(logging.INFO) 9 | from tensorlayerx.files.utils import ( 10 | del_file, folder_exists, load_file_list, maybe_download_and_extract, natural_keys, read_file 11 | ) 12 | 13 | __all__ = ['load_flickr25k_dataset'] 14 | 15 | 16 | def load_flickr25k_dataset(tag='sky', path="data", n_threads=50, printable=False): 17 | """Load Flickr25K dataset. 18 | 19 | Returns a list of images by a given tag from Flick25k dataset, 20 | it will download Flickr25k from `the official website `__ 21 | at the first time you use it. 22 | 23 | Parameters 24 | ------------ 25 | tag : str or None 26 | What images to return. 27 | - If you want to get images with tag, use string like 'dog', 'red', see `Flickr Search `__. 28 | - If you want to get all images, set to ``None``. 29 | 30 | path : str 31 | The path that the data is downloaded to, defaults is ``data/flickr25k/``. 32 | n_threads : int 33 | The number of thread to read image. 34 | printable : boolean 35 | Whether to print infomation when reading images, default is ``False``. 36 | 37 | Examples 38 | ----------- 39 | Get images with tag of sky 40 | 41 | >>> images = tlx.files.load_flickr25k_dataset(tag='sky') 42 | 43 | Get all images 44 | 45 | >>> images = tlx.files.load_flickr25k_dataset(tag=None, n_threads=100, printable=True) 46 | 47 | """ 48 | path = os.path.join(path, 'flickr25k') 49 | 50 | filename = 'mirflickr25k.zip' 51 | url = 'http://press.liacs.nl/mirflickr/mirflickr25k/' 52 | logging.info("If can't download this dataset automatically, " 53 | "please download it from the official website manually." 54 | "flickr25k Dataset ." 55 | "Please place dataset under 'data/flickr25k/' by default.") 56 | # download dataset 57 | if folder_exists(os.path.join(path, "mirflickr")) is False: 58 | logging.info("[*] Flickr25k is nonexistent in {}".format(path)) 59 | maybe_download_and_extract(filename, path, url, extract=True) 60 | del_file(os.path.join(path, filename)) 61 | 62 | # return images by the given tag. 63 | # 1. image path list 64 | folder_imgs = os.path.join(path, "mirflickr") 65 | path_imgs = load_file_list(path=folder_imgs, regx='\\.jpg', printable=False) 66 | path_imgs.sort(key=natural_keys) 67 | 68 | # 2. tag path list 69 | folder_tags = os.path.join(path, "mirflickr", "meta", "tags") 70 | path_tags = load_file_list(path=folder_tags, regx='\\.txt', printable=False) 71 | path_tags.sort(key=natural_keys) 72 | 73 | # 3. select images 74 | if tag is None: 75 | logging.info("[Flickr25k] reading all images") 76 | else: 77 | logging.info("[Flickr25k] reading images with tag: {}".format(tag)) 78 | images_list = [] 79 | for idx, _v in enumerate(path_tags): 80 | tags = read_file(os.path.join(folder_tags, path_tags[idx])).split('\n') 81 | # logging.info(idx+1, tags) 82 | if tag is None or tag in tags: 83 | images_list.append(path_imgs[idx]) 84 | 85 | images = load_images(folder_imgs, n_threads=n_threads) 86 | return images 87 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/matt_mahoney_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import zipfile 6 | 7 | from tensorlayerx import logging 8 | from tensorlayerx.files.utils import maybe_download_and_extract 9 | logging.set_verbosity(logging.INFO) 10 | __all__ = ['load_matt_mahoney_text8_dataset'] 11 | 12 | 13 | def load_matt_mahoney_text8_dataset(path='data'): 14 | """Load Matt Mahoney's dataset. 15 | 16 | Download a text file from Matt Mahoney's website 17 | if not present, and make sure it's the right size. 18 | Extract the first file enclosed in a zip file as a list of words. 19 | This dataset can be used for Word Embedding. 20 | 21 | Parameters 22 | ---------- 23 | path : str 24 | The path that the data is downloaded to, defaults is ``data/mm_test8/``. 25 | 26 | Returns 27 | -------- 28 | list of str 29 | The raw text data e.g. [.... 'their', 'families', 'who', 'were', 'expelled', 'from', 'jerusalem', ...] 30 | 31 | Examples 32 | -------- 33 | >>> words = tlx.files.load_matt_mahoney_text8_dataset() 34 | >>> print('Data size', len(words)) 35 | 36 | """ 37 | path = os.path.join(path, 'mm_test8') 38 | logging.info("If can't download this dataset automatically, " 39 | "please download it from the official website manually." 40 | "mm_test8 Dataset ." 41 | "Please place dataset under 'data/mm_test8/' by default.") 42 | 43 | filename = 'text8.zip' 44 | url = 'http://mattmahoney.net/dc/' 45 | maybe_download_and_extract(filename, path, url, expected_bytes=31344016) 46 | 47 | with zipfile.ZipFile(os.path.join(path, filename)) as f: 48 | word_list = f.read(f.namelist()[0]).split() 49 | for idx, _ in enumerate(word_list): 50 | word_list[idx] = word_list[idx].decode() 51 | return word_list 52 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/mnist_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.files.utils import _load_mnist_dataset 5 | from tensorlayerx import logging 6 | logging.set_verbosity(logging.INFO) 7 | __all__ = ['load_mnist_dataset'] 8 | 9 | 10 | def load_mnist_dataset(shape=(-1, 784), path='data'): 11 | """Load the original mnist. 12 | 13 | Automatically download MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 digit images respectively. 14 | 15 | Parameters 16 | ---------- 17 | shape : tuple 18 | The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)). 19 | path : str 20 | The path that the data is downloaded to. 21 | 22 | Returns 23 | ------- 24 | X_train, y_train, X_val, y_val, X_test, y_test: tuple 25 | Return splitted training/validation/test set respectively. 26 | 27 | Examples 28 | -------- 29 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1,784), path='datasets') 30 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_mnist_dataset(shape=(-1, 28, 28, 1)) 31 | """ 32 | logging.info("If can't download this dataset automatically, " 33 | "please download it from the official website manually." 34 | "mnist Dataset ." 35 | "Please place dataset under 'data/mnist/' by default.") 36 | return _load_mnist_dataset(shape, path, name='mnist', url='http://yann.lecun.com/exdb/mnist/') 37 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/mnist_fashion_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.files.utils import _load_mnist_dataset 5 | from tensorlayerx import logging 6 | logging.set_verbosity(logging.INFO) 7 | __all__ = ['load_fashion_mnist_dataset'] 8 | 9 | 10 | def load_fashion_mnist_dataset(shape=(-1, 784), path='data'): 11 | """Load the fashion mnist. 12 | 13 | Automatically download fashion-MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 fashion images respectively, `examples `__. 14 | 15 | Parameters 16 | ---------- 17 | shape : tuple 18 | The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)). 19 | path : str 20 | The path that the data is downloaded to. 21 | 22 | Returns 23 | ------- 24 | X_train, y_train, X_val, y_val, X_test, y_test: tuple 25 | Return splitted training/validation/test set respectively. 26 | 27 | Examples 28 | -------- 29 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_fashion_mnist_dataset(shape=(-1,784), path='datasets') 30 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tlx.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1)) 31 | """ 32 | logging.info("If can't download this dataset automatically, " 33 | "please download it from the official website manually." 34 | "fashion_mnist Dataset ." 35 | "Please place dataset under 'data/fashion_mnist/' by default.") 36 | 37 | return _load_mnist_dataset( 38 | shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' 39 | ) 40 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/mnist_utils.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import gzip 5 | import os 6 | 7 | import numpy as np 8 | 9 | from tensorlayerx import logging 10 | from tensorlayerx.files.utils import maybe_download_and_extract 11 | 12 | __all__ = ["_load_mnist_dataset"] 13 | 14 | 15 | def _load_mnist_dataset(shape, path, name='mnist', url='http://yann.lecun.com/exdb/mnist/'): 16 | """A generic function to load mnist-like dataset. 17 | 18 | Parameters: 19 | ---------- 20 | shape : tuple 21 | The shape of digit images. 22 | path : str 23 | The path that the data is downloaded to. 24 | name : str 25 | The dataset name you want to use(the default is 'mnist'). 26 | url : str 27 | The url of dataset(the default is 'http://yann.lecun.com/exdb/mnist/'). 28 | """ 29 | path = os.path.join(path, name) 30 | 31 | # Define functions for loading mnist-like data's images and labels. 32 | # For convenience, they also download the requested files if needed. 33 | def load_mnist_images(path, filename): 34 | filepath = maybe_download_and_extract(filename, path, url) 35 | 36 | logging.info(filepath) 37 | # Read the inputs in Yann LeCun's binary format. 38 | with gzip.open(filepath, 'rb') as f: 39 | data = np.frombuffer(f.read(), np.uint8, offset=16) 40 | # The inputs are vectors now, we reshape them to monochrome 2D images, 41 | # following the shape convention: (examples, channels, rows, columns) 42 | data = data.reshape(shape) 43 | # The inputs come as bytes, we convert them to float32 in range [0,1]. 44 | # (Actually to range [0, 255/256], for compatibility to the version 45 | # provided at http://deeplearning.net/data/mnist/mnist.pkl.gz.) 46 | return data / np.float32(256) 47 | 48 | def load_mnist_labels(path, filename): 49 | filepath = maybe_download_and_extract(filename, path, url) 50 | # Read the labels in Yann LeCun's binary format. 51 | with gzip.open(filepath, 'rb') as f: 52 | data = np.frombuffer(f.read(), np.uint8, offset=8) 53 | # The labels are vectors of integers now, that's exactly what we want. 54 | return data 55 | 56 | # Download and read the training and test set images and labels. 57 | logging.info("Load or Download {0} > {1}".format(name.upper(), path)) 58 | X_train = load_mnist_images(path, 'train-images-idx3-ubyte.gz') 59 | y_train = load_mnist_labels(path, 'train-labels-idx1-ubyte.gz') 60 | X_test = load_mnist_images(path, 't10k-images-idx3-ubyte.gz') 61 | y_test = load_mnist_labels(path, 't10k-labels-idx1-ubyte.gz') 62 | 63 | # We reserve the last 10000 training examples for validation. 64 | X_train, X_val = X_train[:-10000], X_train[-10000:] 65 | y_train, y_val = y_train[:-10000], y_train[-10000:] 66 | 67 | # We just return all the arrays in order, as expected in main(). 68 | # (It doesn't matter how we do this as long as we can read them again.) 69 | X_train = np.asarray(X_train, dtype=np.float32) 70 | y_train = np.asarray(y_train, dtype=np.int32) 71 | X_val = np.asarray(X_val, dtype=np.float32) 72 | y_val = np.asarray(y_val, dtype=np.int32) 73 | X_test = np.asarray(X_test, dtype=np.float32) 74 | y_test = np.asarray(y_test, dtype=np.int32) 75 | return X_train, y_train, X_val, y_val, X_test, y_test 76 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/nietzsche_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | from tensorlayerx import logging 7 | from tensorlayerx.files.utils import maybe_download_and_extract 8 | logging.set_verbosity(logging.INFO) 9 | __all__ = ['load_nietzsche_dataset'] 10 | 11 | 12 | def load_nietzsche_dataset(path='data'): 13 | """Load Nietzsche dataset. 14 | 15 | Parameters 16 | ---------- 17 | path : str 18 | The path that the data is downloaded to, defaults is ``data/nietzsche/``. 19 | 20 | Returns 21 | -------- 22 | str 23 | The content. 24 | 25 | Examples 26 | -------- 27 | >>> see tutorial_generate_text.py 28 | >>> words = tlx.files.load_nietzsche_dataset() 29 | >>> words = basic_clean_str(words) 30 | >>> words = words.split() 31 | 32 | """ 33 | logging.info("If can't download this dataset automatically, " 34 | "please download it from the official website manually." 35 | "nietzsche Dataset ." 36 | "Please place dataset under 'data/nietzsche/' by default.") 37 | path = os.path.join(path, 'nietzsche') 38 | 39 | filename = "nietzsche.txt" 40 | url = 'https://s3.amazonaws.com/text-datasets/' 41 | filepath = maybe_download_and_extract(filename, path, url) 42 | 43 | with open(filepath, "r") as f: 44 | words = f.read() 45 | return words 46 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/ptb_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | from tensorlayerx import logging 7 | from tensorlayerx.text import nlp 8 | from tensorlayerx.files.utils import maybe_download_and_extract 9 | 10 | __all__ = ['load_ptb_dataset'] 11 | 12 | 13 | def load_ptb_dataset(path='data'): 14 | """Load Penn TreeBank (PTB) dataset. 15 | 16 | It is used in many LANGUAGE MODELING papers, 17 | including "Empirical Evaluation and Combination of Advanced Language 18 | Modeling Techniques", "Recurrent Neural Network Regularization". 19 | It consists of 929k training words, 73k validation words, and 82k test 20 | words. It has 10k words in its vocabulary. 21 | 22 | Parameters 23 | ---------- 24 | path : str 25 | The path that the data is downloaded to, defaults is ``data/ptb/``. 26 | 27 | Returns 28 | -------- 29 | train_data, valid_data, test_data : list of int 30 | The training, validating and testing data in integer format. 31 | vocab_size : int 32 | The vocabulary size. 33 | 34 | Examples 35 | -------- 36 | >>> train_data, valid_data, test_data, vocab_size = tlx.files.load_ptb_dataset() 37 | 38 | References 39 | --------------- 40 | - ``tensorflow.model.rnn.ptb import reader`` 41 | - `Manual download `__ 42 | 43 | Notes 44 | ------ 45 | - If you want to get the raw data, see the source code. 46 | 47 | """ 48 | path = os.path.join(path, 'ptb') 49 | logging.info("Load or Download Penn TreeBank (PTB) dataset > {}".format(path)) 50 | 51 | #Maybe dowload and uncompress tar, or load exsisting files 52 | filename = 'simple-examples.tgz' 53 | url = 'http://www.fit.vutbr.cz/~imikolov/rnnlm/' 54 | maybe_download_and_extract(filename, path, url, extract=True) 55 | 56 | data_path = os.path.join(path, 'simple-examples', 'data') 57 | train_path = os.path.join(data_path, "ptb.train.txt") 58 | valid_path = os.path.join(data_path, "ptb.valid.txt") 59 | test_path = os.path.join(data_path, "ptb.test.txt") 60 | 61 | word_to_id = nlp.build_vocab(nlp.read_words(train_path)) 62 | 63 | train_data = nlp.words_to_word_ids(nlp.read_words(train_path), word_to_id) 64 | valid_data = nlp.words_to_word_ids(nlp.read_words(valid_path), word_to_id) 65 | test_data = nlp.words_to_word_ids(nlp.read_words(test_path), word_to_id) 66 | vocab_size = len(word_to_id) 67 | 68 | # logging.info(nlp.read_words(train_path)) # ... 'according', 'to', 'mr.', '', ''] 69 | # logging.info(train_data) # ... 214, 5, 23, 1, 2] 70 | # logging.info(word_to_id) # ... 'beyond': 1295, 'anti-nuclear': 9599, 'trouble': 1520, '': 2 ... } 71 | # logging.info(vocabulary) # 10000 72 | # exit() 73 | return train_data, valid_data, test_data, vocab_size 74 | -------------------------------------------------------------------------------- /tensorlayerx/files/dataset_loaders/wmt_en_fr_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import gzip 5 | import os 6 | import tarfile 7 | 8 | from tensorflow.python.platform import gfile 9 | 10 | from tensorlayerx import logging 11 | from tensorlayerx.files.utils import maybe_download_and_extract 12 | 13 | __all__ = ['load_wmt_en_fr_dataset'] 14 | 15 | 16 | def load_wmt_en_fr_dataset(path='data'): 17 | """Load WMT'15 English-to-French translation dataset. 18 | 19 | It will download the data from the WMT'15 Website (10^9-French-English corpus), and the 2013 news test from the same site as development set. 20 | Returns the directories of training data and test data. 21 | 22 | Parameters 23 | ---------- 24 | path : str 25 | The path that the data is downloaded to, defaults is ``data/wmt_en_fr/``. 26 | 27 | References 28 | ---------- 29 | - Code modified from /tensorflow/model/rnn/translation/data_utils.py 30 | 31 | Notes 32 | ----- 33 | Usually, it will take a long time to download this dataset. 34 | 35 | """ 36 | path = os.path.join(path, 'wmt_en_fr') 37 | # URLs for WMT data. 38 | _WMT_ENFR_TRAIN_URL = "http://www.statmt.org/wmt10/" 39 | _WMT_ENFR_DEV_URL = "http://www.statmt.org/wmt15/" 40 | 41 | def gunzip_file(gz_path, new_path): 42 | """Unzips from gz_path into new_path.""" 43 | logging.info("Unpacking %s to %s" % (gz_path, new_path)) 44 | with gzip.open(gz_path, "rb") as gz_file: 45 | with open(new_path, "wb") as new_file: 46 | for line in gz_file: 47 | new_file.write(line) 48 | 49 | def get_wmt_enfr_train_set(path): 50 | """Download the WMT en-fr training corpus to directory unless it's there.""" 51 | filename = "training-giga-fren.tar" 52 | maybe_download_and_extract(filename, path, _WMT_ENFR_TRAIN_URL, extract=True) 53 | train_path = os.path.join(path, "giga-fren.release2.fixed") 54 | gunzip_file(train_path + ".fr.gz", train_path + ".fr") 55 | gunzip_file(train_path + ".en.gz", train_path + ".en") 56 | return train_path 57 | 58 | def get_wmt_enfr_dev_set(path): 59 | """Download the WMT en-fr training corpus to directory unless it's there.""" 60 | filename = "dev-v2.tgz" 61 | dev_file = maybe_download_and_extract(filename, path, _WMT_ENFR_DEV_URL, extract=False) 62 | dev_name = "newstest2013" 63 | dev_path = os.path.join(path, "newstest2013") 64 | if not (gfile.Exists(dev_path + ".fr") and gfile.Exists(dev_path + ".en")): 65 | logging.info("Extracting tgz file %s" % dev_file) 66 | with tarfile.open(dev_file, "r:gz") as dev_tar: 67 | fr_dev_file = dev_tar.getmember("dev/" + dev_name + ".fr") 68 | en_dev_file = dev_tar.getmember("dev/" + dev_name + ".en") 69 | fr_dev_file.name = dev_name + ".fr" # Extract without "dev/" prefix. 70 | en_dev_file.name = dev_name + ".en" 71 | dev_tar.extract(fr_dev_file, path) 72 | dev_tar.extract(en_dev_file, path) 73 | return dev_path 74 | 75 | logging.info("Load or Download WMT English-to-French translation > {}".format(path)) 76 | 77 | train_path = get_wmt_enfr_train_set(path) 78 | dev_path = get_wmt_enfr_dev_set(path) 79 | 80 | return train_path, dev_path 81 | -------------------------------------------------------------------------------- /tensorlayerx/logging/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | TensorLayer provides rich layer implementations trailed for 5 | various benchmarks and domain-specific problems. In addition, we also 6 | support transparent access to native TensorFlow parameters. 7 | For example, we provide not only layers for local response normalization, but also 8 | layers that allow user to apply ``tf.ops.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from tensorlayerx.utils.lazy_imports import LazyImport 13 | 14 | from .tl_logging import * 15 | 16 | # Lazy Imports 17 | contrib = LazyImport("tensorlayerx.logging.contrib") 18 | 19 | __all__ = [ 20 | # tl_logging 21 | 'DEBUG', 22 | 'debug', 23 | 'ERROR', 24 | 'error', 25 | 'FATAL', 26 | 'fatal', 27 | 'INFO', 28 | 'info', 29 | 'WARN', 30 | 'warn', 31 | 'warning', 32 | 'set_verbosity', 33 | 'get_verbosity' 34 | ] 35 | -------------------------------------------------------------------------------- /tensorlayerx/logging/contrib/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | TensorLayer provides rich layer implementations trailed for 5 | various benchmarks and domain-specific problems. In addition, we also 6 | support transparent access to native TensorFlow parameters. 7 | For example, we provide not only layers for local response normalization, but also 8 | layers that allow user to apply ``tf.ops.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .hyperdash import * 13 | -------------------------------------------------------------------------------- /tensorlayerx/logging/contrib/hyperdash.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import absolute_import 5 | 6 | import hyperdash as hd 7 | import tensorlayerx as tlx 8 | 9 | __all__ = ["HyperDashHandler", "monitor", "Experiment", "IPythonMagicsWrapper"] 10 | 11 | 12 | class HyperDashHandler(object): 13 | apikey = None 14 | 15 | @classmethod 16 | def reset_apikey(cls): 17 | cls.apikey = None 18 | 19 | @classmethod 20 | def set_apikey(cls, apikey): 21 | cls.apikey = apikey 22 | 23 | @classmethod 24 | def get_apikey(cls): 25 | 26 | if cls.apikey is None: 27 | raise ValueError( 28 | "Hyperdash API is not set.\n" 29 | "You can obtain your API Key using: `hyperdash login --email` or `hyperdash login --github`\n" 30 | "You should first call `HyperDashHandler.set_apikey('my_api_key')` in order to use `hyperdash`" 31 | ) 32 | 33 | tlx.logging.debug("Hyperdash API Key: %s" % cls.apikey) 34 | 35 | return cls.apikey 36 | 37 | @classmethod 38 | def monitor(cls, model_name, api_key=None, capture_io=True): 39 | 40 | if api_key is not None: 41 | cls.set_apikey(api_key) 42 | 43 | return hd.monitor(model_name, api_key_getter=cls.get_apikey, capture_io=capture_io) 44 | 45 | 46 | class Experiment(hd.Experiment): 47 | 48 | def __init__( 49 | self, 50 | model_name, 51 | api_key=None, 52 | capture_io=True, 53 | ): 54 | 55 | if api_key is not None: 56 | HyperDashHandler.set_apikey(api_key) 57 | 58 | super(Experiment, 59 | self).__init__(model_name=model_name, api_key_getter=HyperDashHandler.get_apikey, capture_io=capture_io) 60 | 61 | 62 | monitor = HyperDashHandler.monitor 63 | IPythonMagicsWrapper = hd.IPythonMagicsWrapper 64 | -------------------------------------------------------------------------------- /tensorlayerx/losses/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.backend import BACKEND 5 | 6 | if BACKEND == 'tensorflow': 7 | from .tensorflow_cost import * 8 | elif BACKEND == 'mindspore': 9 | from .mindspore_cost import * 10 | elif BACKEND == 'paddle': 11 | from .paddle_cost import * 12 | elif BACKEND == 'torch': 13 | from .torch_cost import * 14 | elif BACKEND == 'oneflow': 15 | from .oneflow_cost import * 16 | elif BACKEND == 'jittor': 17 | from .jittor_cost import * 18 | else: 19 | raise NotImplementedError("This backend is not supported") 20 | -------------------------------------------------------------------------------- /tensorlayerx/metrics/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.backend import BACKEND 5 | 6 | if BACKEND == 'tensorflow': 7 | from .tensorflow_metric import * 8 | elif BACKEND == 'mindspore': 9 | from .mindspore_metric import * 10 | elif BACKEND == 'paddle': 11 | from .paddle_metric import * 12 | elif BACKEND == 'torch': 13 | from .torch_metric import * 14 | elif BACKEND == 'oneflow': 15 | from .oneflow_metric import * 16 | elif BACKEND == 'jittor': 17 | from .jittor_metric import * 18 | 19 | else: 20 | raise NotImplementedError("This backend is not supported") 21 | -------------------------------------------------------------------------------- /tensorlayerx/metrics/paddle_metric.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import paddle 5 | from paddle.metric.metrics import Metric 6 | import six 7 | import abc 8 | 9 | __all__ = [ 10 | 'Metric', 11 | 'Accuracy', 12 | 'Auc', 13 | 'Precision', 14 | 'Recall', 15 | 'acc', 16 | ] 17 | 18 | @six.add_metaclass(abc.ABCMeta) 19 | class Metric(object): 20 | 21 | def __init__(self): 22 | pass 23 | 24 | @abc.abstractmethod 25 | def update(self, *args): 26 | raise NotImplementedError("function 'update' not implemented in {}.".format(self.__class__.__name__)) 27 | 28 | @abc.abstractmethod 29 | def result(self): 30 | raise NotImplementedError("function 'reset' not implemented in {}.".format(self.__class__.__name__)) 31 | 32 | @abc.abstractmethod 33 | def reset(self): 34 | raise NotImplementedError("function 'reset' not implemented in {}.".format(self.__class__.__name__)) 35 | 36 | 37 | class Accuracy(object): 38 | 39 | def __init__( 40 | self, 41 | topk=1, 42 | ): 43 | 44 | self.topk = topk 45 | self.accuracy = paddle.metric.Accuracy(topk=(self.topk, )) 46 | 47 | def update(self, y_pred, y_true): 48 | 49 | self.accuracy.update(self.accuracy.compute(y_pred, y_true)) 50 | 51 | def result(self): 52 | 53 | return self.accuracy.accumulate() 54 | 55 | def reset(self): 56 | 57 | self.accuracy.reset() 58 | 59 | 60 | class Auc(object): 61 | 62 | def __init__(self, curve='ROC', num_thresholds=4095): 63 | 64 | self.auc = paddle.metric.Auc(curve=curve, num_thresholds=num_thresholds) 65 | 66 | def update(self, y_pred, y_true): 67 | 68 | self.auc.update(y_pred, y_true) 69 | 70 | def result(self): 71 | 72 | return self.auc.accumulate() 73 | 74 | def reset(self): 75 | 76 | self.auc.reset() 77 | 78 | 79 | class Precision(object): 80 | 81 | def __init__(self): 82 | 83 | self.precision = paddle.metric.Precision() 84 | 85 | def update(self, y_pred, y_true): 86 | 87 | self.precision.update(y_pred, y_true) 88 | 89 | def result(self): 90 | 91 | return self.precision.accumulate() 92 | 93 | def reset(self): 94 | 95 | self.precision.reset() 96 | 97 | 98 | class Recall(object): 99 | 100 | def __init__(self): 101 | 102 | self.recall = paddle.metric.Recall() 103 | 104 | def update(self, y_pred, y_true): 105 | self.recall.update(y_pred, y_true) 106 | 107 | def result(self): 108 | return self.recall.accumulate() 109 | 110 | def reset(self): 111 | self.recall.reset() 112 | 113 | 114 | def acc(predicts, labels, topk=1): 115 | 116 | res = paddle.metric.accuracy(predicts, labels, k=topk) 117 | return res.numpy() 118 | -------------------------------------------------------------------------------- /tensorlayerx/model/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .core import Model 5 | from .core import WithLoss 6 | from .core import WithGrad 7 | from .core import TrainOneStep 8 | from .core import TrainOneStepWithGradientClipping 9 | -------------------------------------------------------------------------------- /tensorlayerx/nn/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .initializers import * 5 | from .layers import * 6 | from .core import * 7 | 8 | from . import initializers 9 | from . import layers 10 | from . import core 11 | -------------------------------------------------------------------------------- /tensorlayerx/nn/core/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.backend import BACKEND 5 | 6 | if BACKEND == 'mindspore': 7 | from .core_mindspore import * 8 | elif BACKEND == 'tensorflow': 9 | from .core_tensorflow import * 10 | elif BACKEND == 'paddle': 11 | from .core_paddle import * 12 | elif BACKEND == 'torch': 13 | from .core_torch import * 14 | elif BACKEND == 'oneflow': 15 | from .core_oneflow import * 16 | elif BACKEND == 'jittor': 17 | from .core_jittor import * 18 | else: 19 | raise ("Unsupported backend:", BACKEND) 20 | -------------------------------------------------------------------------------- /tensorlayerx/nn/initializers/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # __all__ = [ 5 | # 'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal', 6 | # 'deconv2d_bilinear_upsampling_initializer', 'He_Normal' 7 | # ] 8 | from .load_initializers_backend import Initializer 9 | from .load_initializers_backend import Zeros 10 | from .load_initializers_backend import Ones 11 | from .load_initializers_backend import Constant 12 | from .load_initializers_backend import RandomUniform 13 | from .load_initializers_backend import RandomNormal 14 | from .load_initializers_backend import TruncatedNormal 15 | from .load_initializers_backend import deconv2d_bilinear_upsampling_initializer 16 | from .load_initializers_backend import HeNormal 17 | from .load_initializers_backend import HeUniform 18 | from .load_initializers_backend import XavierNormal 19 | from .load_initializers_backend import XavierUniform 20 | 21 | # Alias 22 | zeros = Zeros 23 | ones = Ones 24 | constant = Constant 25 | random_uniform = RandomUniform 26 | random_normal = RandomNormal 27 | truncated_normal = TruncatedNormal 28 | he_normal = HeNormal 29 | he_uniform = HeUniform 30 | xavier_normal = XavierNormal 31 | xavier_uniform = XavierUniform 32 | -------------------------------------------------------------------------------- /tensorlayerx/nn/initializers/load_initializers_backend.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import absolute_import, division, print_function 5 | from tensorlayerx.backend import BACKEND 6 | 7 | if BACKEND == 'tensorflow': 8 | from .tensorflow_initializers import * 9 | elif BACKEND == 'mindspore': 10 | from .mindspore_initializers import * 11 | elif BACKEND == 'paddle': 12 | from .paddle_initializers import * 13 | elif BACKEND == 'torch': 14 | from .torch_initializers import * 15 | elif BACKEND == 'oneflow': 16 | from .oneflow_initializers import * 17 | elif BACKEND == 'jittor': 18 | from .jittor_initializers import * 19 | else: 20 | raise NotImplementedError("This backend is not supported") 21 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .activation import * 5 | from .convolution import * 6 | from .linear import * 7 | from .deprecated import * 8 | from .dropout import * 9 | from .embedding import * 10 | from .extend import * 11 | from .image_resampling import * 12 | from .inputs import * 13 | from .lambda_layers import * 14 | from .merge import * 15 | from .noise import * 16 | from .normalization import * 17 | from .padding import * 18 | from .pooling import * 19 | from .recurrent import * 20 | from .scale import * 21 | from .shape import * 22 | from .spatial_transformer import * 23 | from .stack import * 24 | # from .utils import * 25 | from .Transformer import * 26 | from .fold import * 27 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/convolution/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | TensorLayer provides rich layer implementations trailed for 5 | various benchmarks and domain-specific problems. In addition, we also 6 | support transparent access to native TensorFlow parameters. 7 | For example, we provide not only layers for local response normalization, but also 8 | layers that allow user to apply ``tf.ops.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .binary_conv import * 13 | from .deformable_conv import * 14 | from .depthwise_conv import * 15 | from .dorefa_conv import * 16 | # from .expert_conv import * 17 | # from .expert_deconv import * 18 | from .group_conv import * 19 | from .quan_conv import * 20 | from .quan_conv_bn import * 21 | from .separable_conv import * 22 | from .simplified_conv import * 23 | # from .simplified_deconv import * 24 | from .super_resolution import * 25 | from .ternary_conv import * 26 | from .mask_conv import * 27 | 28 | __all__ = [ 29 | 30 | # simplified conv 31 | 'Conv1d', 32 | 'Conv2d', 33 | 'Conv3d', 34 | 35 | # simplified deconv 36 | 'ConvTranspose1d', 37 | 'ConvTranspose2d', 38 | 'ConvTranspose3d', 39 | 40 | # binary 41 | 'BinaryConv2d', 42 | 43 | # deformable 44 | 'DeformableConv2d', 45 | 46 | # depthwise 47 | 'DepthwiseConv2d', 48 | 49 | # dorefa 50 | 'DorefaConv2d', 51 | 52 | # group 53 | 'GroupConv2d', 54 | 55 | # separable 56 | 'SeparableConv1d', 57 | 'SeparableConv2d', 58 | 59 | # subpixel 60 | 'SubpixelConv1d', 61 | 'SubpixelConv2d', 62 | 63 | # ternary 64 | 'TernaryConv2d', 65 | 66 | #quan_conv 67 | 'QuanConv2d', 68 | 'QuanConv2dWithBN', 69 | 70 | # masked conv 71 | 'MaskedConv3d' 72 | ] 73 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/dropout.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | 8 | __all__ = [ 9 | 'Dropout', 10 | ] 11 | 12 | 13 | class Dropout(Module): 14 | """ 15 | During training, randomly zeroes some of the elements of the input tensor with probability p using samples from a Bernoulli distribution. 16 | Each channel will be zeroed out independently on every forward call. 17 | 18 | Parameters 19 | ---------- 20 | p : float 21 | probability of an element to be zeroed. Default: 0.5 22 | seed : int or None 23 | The seed for random dropout. 24 | name : None or str 25 | A unique layer name. 26 | 27 | Examples 28 | -------- 29 | >>> net = tlx.nn.Input([10, 200]) 30 | >>> net = tlx.nn.Dropout(p=0.2)(net) 31 | 32 | """ 33 | 34 | def __init__(self, p=0.5, seed=0, name=None): #"dropout"): 35 | super(Dropout, self).__init__(name) 36 | self.p = p 37 | self.seed = seed 38 | 39 | self.build() 40 | self._built = True 41 | 42 | logging.info("Dropout %s: p: %f " % (self.name, self.p)) 43 | 44 | def __repr__(self): 45 | s = ('{classname}(p={p}') 46 | if self.name is not None: 47 | s += ', name=\'{name}\'' 48 | s += ')' 49 | return s.format(classname=self.__class__.__name__, **self.__dict__) 50 | 51 | def build(self, inputs_shape=None): 52 | self.dropout = tlx.ops.Dropout(p=self.p, seed=self.seed) 53 | 54 | # @tf.function 55 | def forward(self, inputs): 56 | if self.is_train: 57 | outputs = self.dropout(inputs) 58 | else: 59 | outputs = inputs 60 | 61 | if not self._nodes_fixed and self._build_graph: 62 | self._add_node(inputs, outputs) 63 | self._nodes_fixed = True 64 | return outputs 65 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/extend.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | 8 | __all__ = [ 9 | 'ExpandDims', 10 | 'Tile', 11 | ] 12 | 13 | 14 | class ExpandDims(Module): 15 | """ 16 | The :class:`ExpandDims` class inserts a dimension of 1 into a tensor's shape, 17 | see `tf.expand_dims() `__ . 18 | 19 | Parameters 20 | ---------- 21 | axis : int 22 | The dimension index at which to expand the shape of input. 23 | name : str 24 | A unique layer name. If None, a unique name will be automatically assigned. 25 | 26 | Examples 27 | -------- 28 | >>> x = tlx.nn.Input([10, 3], name='in') 29 | >>> y = tlx.nn.ExpandDims(axis=-1)(x) 30 | [10, 3, 1] 31 | """ 32 | 33 | def __init__( 34 | self, 35 | axis=-1, 36 | name=None # 'expand_dims', 37 | ): 38 | super(ExpandDims, self).__init__(name) 39 | self.axis = axis 40 | 41 | self.build((None, )) 42 | self._built = True 43 | 44 | logging.info("ExpandDims %s: axis: %d" % (self.name, self.axis)) 45 | 46 | def __repr__(self): 47 | s = '{classname}(' 48 | s += 'axis={axis},' 49 | s += 'name={name}' 50 | s += ")" 51 | return s.format(classname=self.__class__.__name__, **self.__dict__) 52 | 53 | def build(self, inputs_shape): 54 | self.expand_dims = tlx.ops.ExpandDims(axis=self.axis) 55 | 56 | def forward(self, inputs): 57 | outputs = self.expand_dims(inputs) 58 | 59 | if not self._nodes_fixed and self._build_graph: 60 | self._add_node(inputs, outputs) 61 | self._nodes_fixed = True 62 | return outputs 63 | 64 | 65 | class Tile(Module): 66 | """ 67 | The :class:`Tile` class constructs a tensor by tiling a given tensor, 68 | see `tf.tile() `__ . 69 | 70 | Parameters 71 | ---------- 72 | multiples: tensor 73 | Must be one of the following types: int32, int64. 74 | 1-D Length must be the same as the number of dimensions in input. 75 | name : None or str 76 | A unique layer name. 77 | 78 | Examples 79 | -------- 80 | >>> x = tlx.nn.Input([10, 3], name='in') 81 | >>> y = tlx.nn.Tile(multiples=[2, 3])(x) 82 | 83 | """ 84 | 85 | def __init__(self, multiples=None, name=None): #'tile'): 86 | 87 | super(Tile, self).__init__(name) 88 | self.multiples = multiples 89 | 90 | self.build((None, )) 91 | self._built = True 92 | 93 | logging.info("Tile %s: multiples: %s" % (self.name, self.multiples)) 94 | 95 | def __repr__(self): 96 | s = '{classname}(' 97 | s += 'multiples={multiples},' 98 | s += 'name={name}' 99 | s += ")" 100 | return s.format(classname=self.__class__.__name__, **self.__dict__) 101 | 102 | def build(self, inputs_shape): 103 | self.tile = tlx.ops.Tile() 104 | 105 | # @tf.function 106 | def forward(self, inputs): 107 | outputs = self.tile(inputs, multiples=self.multiples) 108 | 109 | if not self._nodes_fixed and self._build_graph: 110 | self._add_node(inputs, outputs) 111 | self._nodes_fixed = True 112 | return outputs 113 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/inputs.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | from ..initializers import * 8 | 9 | __all__ = ['Input', '_InputLayer'] 10 | 11 | 12 | class _InputLayer(Module): 13 | """ 14 | The :class:`Input` class is the starting layer of a neural network. 15 | 16 | Parameters 17 | ---------- 18 | shape : tuple (int) 19 | Including batch size. 20 | dtype: dtype or None 21 | The type of input values. By default, tf.float32. 22 | name : None or str 23 | A unique layer name. 24 | 25 | """ 26 | 27 | def __init__(self, shape, dtype=None, name=None, init_method=None): 28 | super(_InputLayer, self).__init__(name) 29 | 30 | logging.info("Input %s: %s" % (self.name, str(shape))) 31 | self.shape = shape 32 | self.dtype = dtype 33 | self.shape_without_none = [_ if _ is not None else 1 for _ in shape] 34 | 35 | if tlx.BACKEND == 'paddle': 36 | self.outputs = tlx.ops.ones(self.shape) 37 | else: 38 | if init_method is None: 39 | self.outputs = ones()(self.shape_without_none, dtype=self.dtype) 40 | else: 41 | self.outputs = init_method(self.shape_without_none, dtype=self.dtype) 42 | 43 | self._built = True 44 | self._add_node(self.outputs, self.outputs) 45 | 46 | def __repr__(self): 47 | s = 'Input(shape=%s' % str(self.shape) 48 | if self.name is not None: 49 | s += (', name=\'%s\'' % self.name) 50 | s += ')' 51 | return s 52 | 53 | def __call__(self, *args, **kwargs): 54 | return self.outputs 55 | 56 | def build(self, inputs_shape): 57 | pass 58 | 59 | def forward(self): 60 | return self.outputs 61 | 62 | 63 | def Input(shape, init=None, dtype=tlx.float32, name=None): 64 | """ 65 | The :class:`Input` class is the starting layer of a neural network. 66 | 67 | Parameters 68 | ---------- 69 | shape : tuple (int) 70 | Including batch size. 71 | init : initializer or str or None 72 | The initializer for initializing the input matrix 73 | dtype: dtype 74 | The type of input values. By default, tf.float32. 75 | name : None or str 76 | A unique layer name. 77 | 78 | Examples 79 | --------- 80 | With TensorLayer 81 | 82 | >>> ni = tlx.nn.Input([10, 50, 50, 32], name='input') 83 | >>> output shape : [10, 50, 50, 32] 84 | 85 | """ 86 | 87 | input_layer = _InputLayer(shape, dtype=dtype, name=name, init_method=init) 88 | outputs = input_layer() 89 | return outputs 90 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/linear/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | TensorLayer provides rich layer implementations trailed for 5 | various benchmarks and domain-specific problems. In addition, we also 6 | support transparent access to native TensorFlow parameters. 7 | For example, we provide not only layers for local response normalization, but also 8 | layers that allow user to apply ``tf.ops.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .base_linear import * 13 | from .binary_linear import * 14 | from .dorefa_linear import * 15 | from .dropconnect import * 16 | from .quan_linear import * 17 | from .quan_linear_bn import * 18 | from .ternary_linear import * 19 | 20 | __all__ = [ 21 | 'BinaryLinear', 22 | 'Linear', 23 | 'DorefaLinear', 24 | 'DropconnectLinear', 25 | 'TernaryLinear', 26 | 'QuanLinear', 27 | 'QuanLinearWithBN', 28 | ] -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/noise.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | 8 | __all__ = [ 9 | 'GaussianNoise', 10 | ] 11 | 12 | 13 | class GaussianNoise(Module): 14 | """ 15 | The :class:`GaussianNoise` class is noise layer that adding noise with 16 | gaussian distribution to the activation. 17 | 18 | Parameters 19 | ------------ 20 | mean : float 21 | The mean. Default is 0.0. 22 | stddev : float 23 | The standard deviation. Default is 1.0. 24 | is_always : boolean 25 | Is True, add noise for train and eval mode. If False, skip this layer in eval mode. 26 | seed : int or None 27 | The seed for random noise. 28 | name : str 29 | A unique layer name. 30 | 31 | Examples 32 | -------- 33 | With TensorLayer 34 | 35 | >>> net = tlx.nn.Input([64, 200], name='input') 36 | >>> net = tlx.nn.Linear(in_features=200, out_features=100, act=tlx.ReLU, name='linear')(net) 37 | >>> gaussianlayer = tlx.nn.GaussianNoise(name='gaussian')(net) 38 | >>> print(gaussianlayer) 39 | >>> output shape : (64, 100) 40 | 41 | """ 42 | 43 | def __init__( 44 | self, 45 | mean=0.0, 46 | stddev=1.0, 47 | is_always=True, 48 | seed=None, 49 | name=None, # 'gaussian_noise', 50 | ): 51 | super().__init__(name) 52 | self.mean = mean 53 | self.stddev = stddev 54 | self.seed = seed 55 | self.is_always = is_always 56 | 57 | self.build() 58 | self._built = True 59 | 60 | logging.info("GaussianNoise %s: mean: %f stddev: %f" % (self.name, self.mean, self.stddev)) 61 | 62 | def __repr__(self): 63 | s = '{classname}(mean={mean}, stddev={stddev}' 64 | if self.name is not None: 65 | s += ', name=\'{name}\'' 66 | s += ')' 67 | return s.format(classname=self.__class__.__name__, **self.__dict__) 68 | 69 | def build(self, inputs=None): 70 | pass 71 | 72 | def forward(self, inputs): 73 | if (self.is_train or self.is_always) is False: 74 | return inputs 75 | else: 76 | shapes = tlx.get_tensor_shape(inputs) 77 | noise = tlx.ops.random_normal(shape=shapes, mean=self.mean, stddev=self.stddev, seed=self.seed) 78 | outputs = inputs + noise 79 | 80 | if not self._nodes_fixed and self._build_graph: 81 | self._add_node(inputs, outputs) 82 | self._nodes_fixed = True 83 | return outputs 84 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/scale.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | 8 | __all__ = [ 9 | 'Scale', 10 | ] 11 | 12 | 13 | class Scale(Module): 14 | """The :class:`Scale` class is to multiple a trainable scale value to the layer outputs. Usually be used on the output of binary net. 15 | 16 | Parameters 17 | ---------- 18 | init_scale : float 19 | The initial value for the scale factor. 20 | name : a str 21 | A unique layer name. 22 | 23 | Examples 24 | ---------- 25 | >>> inputs = tlx.nn.Input([8, 3]) 26 | >>> linear = tlx.nn.Linear(out_features=10, in_channels=3)(inputs) 27 | >>> outputs = tlx.nn.Scale(init_scale=0.5)(linear) 28 | 29 | """ 30 | 31 | def __init__( 32 | self, 33 | init_scale=0.05, 34 | name='scale', 35 | ): 36 | super(Scale, self).__init__(name) 37 | self.init_scale = init_scale 38 | 39 | self.build((None, )) 40 | self._built = True 41 | 42 | logging.info("Scale %s: init_scale: %f" % (self.name, self.init_scale)) 43 | 44 | def __repr__(self): 45 | s = '{classname}(' 46 | s += 'init_scale={init_scale},' 47 | s += 'name={name}' 48 | s += ")" 49 | return s.format(classname=self.__class__.__name__, **self.__dict__) 50 | 51 | def build(self, inputs_shape): 52 | self.scale = self._get_weights("scale", shape=[1], init=tlx.nn.initializers.constant(value=self.init_scale)) 53 | 54 | # @tf.function 55 | def forward(self, inputs): 56 | outputs = inputs * self.scale 57 | 58 | if not self._nodes_fixed and self._build_graph: 59 | self._add_node(inputs, outputs) 60 | self._nodes_fixed = True 61 | return outputs 62 | -------------------------------------------------------------------------------- /tensorlayerx/nn/layers/stack.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorlayerx as tlx 5 | from tensorlayerx import logging 6 | from tensorlayerx.nn.core import Module 7 | 8 | __all__ = [ 9 | 'Stack', 10 | 'UnStack', 11 | ] 12 | 13 | 14 | class Stack(Module): 15 | """ 16 | The :class:`Stack` class is a layer for stacking a list of rank-R tensors into one rank-(R+1) tensor, see `tf.stack() `__. 17 | 18 | Parameters 19 | ---------- 20 | axis : int 21 | New dimension along which to stack. 22 | name : str 23 | A unique layer name. 24 | 25 | Examples 26 | --------- 27 | >>> import tensorlayerx as tlx 28 | >>> ni = tlx.nn.Input([10, 784], name='input') 29 | >>> net1 = tlx.nn.Linear(10, name='linear1')(ni) 30 | >>> net2 = tlx.nn.Linear(10, name='linear2')(ni) 31 | >>> net3 = tlx.nn.Linear(10, name='linear3')(ni) 32 | >>> net = tlx.nn.Stack(axis=1, name='stack')([net1, net2, net3]) 33 | (10, 3, 10) 34 | 35 | """ 36 | 37 | def __init__( 38 | self, 39 | axis=1, 40 | name=None, #'stack', 41 | ): 42 | super().__init__(name) 43 | self.axis = axis 44 | 45 | self.build(None) 46 | self._built = True 47 | logging.info("Stack %s: axis: %d" % (self.name, self.axis)) 48 | 49 | def __repr__(self): 50 | s = '{classname}(axis={axis}' 51 | if self.name is not None: 52 | s += ', name=\'{name}\'' 53 | s += ')' 54 | return s.format(classname=self.__class__.__name__, **self.__dict__) 55 | 56 | def build(self, inputs_shape): 57 | self.stack = tlx.ops.Stack(axis=self.axis) 58 | 59 | def forward(self, inputs): 60 | outputs = self.stack(inputs) 61 | 62 | if not self._nodes_fixed and self._build_graph: 63 | self._add_node(inputs, outputs) 64 | self._nodes_fixed = True 65 | return outputs 66 | 67 | 68 | class UnStack(Module): 69 | """ 70 | The :class:`UnStack` class is a layer for unstacking the given dimension of a rank-R tensor into rank-(R-1) tensors., see `tf.unstack() `__. 71 | 72 | Parameters 73 | ---------- 74 | num : int or None 75 | The length of the dimension axis. Automatically inferred if None (the default). 76 | axis : int 77 | Dimension along which axis to concatenate. 78 | name : str 79 | A unique layer name. 80 | 81 | Returns 82 | ------- 83 | list of :class:`Layer` 84 | The list of layer objects unstacked from the input. 85 | 86 | Examples 87 | -------- 88 | >>> ni = tlx.nn.Input([4, 10], name='input') 89 | >>> nn = tlx.nn.Linear(n_units=5)(ni) 90 | >>> nn = tlx.nn.UnStack(axis=1)(nn) # unstack in channel axis 91 | >>> len(nn) # 5 92 | >>> nn[0].shape # (4,) 93 | 94 | """ 95 | 96 | def __init__(self, num=None, axis=0, name=None): #'unstack'): 97 | super().__init__(name) 98 | self.num = num 99 | self.axis = axis 100 | 101 | self.build(None) 102 | self._built = True 103 | logging.info("UnStack %s: num: %s axis: %d" % (self.name, self.num, self.axis)) 104 | 105 | def __repr__(self): 106 | s = '{classname}(num={num}, axis={axis}' 107 | if self.name is not None: 108 | s += ', name=\'{name}\'' 109 | s += ')' 110 | return s.format(classname=self.__class__.__name__, **self.__dict__) 111 | 112 | def build(self, inputs_shape): 113 | self.unstack = tlx.ops.Unstack(num=self.num, axis=self.axis) 114 | 115 | def forward(self, inputs): 116 | outputs = self.unstack(inputs) 117 | 118 | if not self._nodes_fixed and self._build_graph: 119 | self._add_node(inputs, outputs) 120 | self._nodes_fixed = True 121 | return outputs 122 | -------------------------------------------------------------------------------- /tensorlayerx/optimizers/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # from .amsgrad import AMSGrad 5 | 6 | # ['Adadelta', 'Adagrad', 'Adam', 'Adamax', 'Ftrl', 'Nadam', 'RMSprop', 'SGD', 'Momentum', 'Lamb', 'LARS'] 7 | from .load_optimizers_backend import Adadelta 8 | from .load_optimizers_backend import Adagrad 9 | from .load_optimizers_backend import Adam 10 | from .load_optimizers_backend import Adamax 11 | from .load_optimizers_backend import Ftrl 12 | from .load_optimizers_backend import Nadam 13 | from .load_optimizers_backend import RMSprop 14 | from .load_optimizers_backend import SGD 15 | from .load_optimizers_backend import Momentum 16 | from .load_optimizers_backend import Lamb 17 | from .load_optimizers_backend import LARS 18 | from tensorlayerx.optimizers import lr 19 | 20 | __all__ = ['Adadelta', 'Adagrad', 'Adam', 'Adamax', 'Ftrl', 'Nadam', 'RMSprop', 'SGD', 'Momentum', 'Lamb', 'LARS'] 21 | 22 | -------------------------------------------------------------------------------- /tensorlayerx/optimizers/load_optimizers_backend.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import absolute_import, division, print_function 5 | from tensorlayerx import BACKEND 6 | 7 | if BACKEND == 'tensorflow': 8 | from .tensorflow_optimizers import * 9 | elif BACKEND == 'mindspore': 10 | from .mindspore_optimizers import * 11 | elif BACKEND == 'paddle': 12 | from .paddle_optimizers import * 13 | elif BACKEND == 'torch': 14 | from .torch_optimizers import * 15 | elif BACKEND == 'oneflow': 16 | from .oneflow_optimizers import * 17 | elif BACKEND == 'jittor': 18 | from .jittor_optimizers import * 19 | else: 20 | raise NotImplementedError("This backend is not supported") 21 | -------------------------------------------------------------------------------- /tensorlayerx/optimizers/lr/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from __future__ import absolute_import, division, print_function 5 | from tensorlayerx.backend.ops.load_backend import BACKEND 6 | 7 | if BACKEND == 'tensorflow': 8 | from .tensorflow_lr import * 9 | elif BACKEND == 'mindspore': 10 | from .mindspore_lr import * 11 | elif BACKEND == 'paddle': 12 | from .paddle_lr import * 13 | elif BACKEND == 'torch': 14 | from .torch_lr import * 15 | elif BACKEND == 'oneflow': 16 | from .oneflow_lr import * 17 | elif BACKEND == 'jittor': 18 | from .jittor_lr import * 19 | else: 20 | raise NotImplementedError("This backend is not supported") 21 | -------------------------------------------------------------------------------- /tensorlayerx/optimizers/lr/paddle_lr.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | from __future__ import absolute_import, division, print_function 4 | 5 | import paddle 6 | 7 | __all__ = [ 8 | 'LRScheduler', 'NoamDecay', 'PiecewiseDecay', 'NaturalExpDecay', 'InverseTimeDecay', 'PolynomialDecay', 9 | 'LinearWarmup', 'ExponentialDecay', 'MultiStepDecay', 'StepDecay', 'LambdaDecay', 'ReduceOnPlateau', 10 | 'CosineAnnealingDecay' 11 | ] 12 | 13 | LRScheduler = paddle.optimizer.lr.LRScheduler 14 | NoamDecay = paddle.optimizer.lr.NoamDecay 15 | PiecewiseDecay = paddle.optimizer.lr.PiecewiseDecay 16 | NaturalExpDecay = paddle.optimizer.lr.NaturalExpDecay 17 | InverseTimeDecay = paddle.optimizer.lr.InverseTimeDecay 18 | PolynomialDecay = paddle.optimizer.lr.PolynomialDecay 19 | LinearWarmup = paddle.optimizer.lr.LinearWarmup 20 | ExponentialDecay = paddle.optimizer.lr.ExponentialDecay 21 | MultiStepDecay = paddle.optimizer.lr.MultiStepDecay 22 | StepDecay = paddle.optimizer.lr.StepDecay 23 | LambdaDecay = paddle.optimizer.lr.LambdaDecay 24 | ReduceOnPlateau = paddle.optimizer.lr.ReduceOnPlateau 25 | CosineAnnealingDecay = paddle.optimizer.lr.CosineAnnealingDecay 26 | -------------------------------------------------------------------------------- /tensorlayerx/package_info.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """Deep learning and Reinforcement learning library for Researchers and Engineers.""" 4 | 5 | MAJOR = 0 6 | MINOR = 5 7 | PATCH = 8 8 | PRE_RELEASE = '' 9 | # Use the following formatting: (major, minor, patch, prerelease) 10 | VERSION = (MAJOR, MINOR, PATCH, PRE_RELEASE) 11 | 12 | __shortversion__ = '.'.join(map(str, VERSION[:3])) 13 | __version__ = '.'.join(map(str, VERSION[:3])) + ''.join(VERSION[3:]) 14 | 15 | __package_name__ = 'tensorlayerx' 16 | __contact_names__ = 'TensorLayer Contributors' 17 | __contact_emails__ = 'tensorlayerx@gmail.com' 18 | __homepage__ = 'https://tensorlayerx.readthedocs.io/en/latest/' 19 | __repository_url__ = 'https://github.com/tensorlayer/TensorLayerX' 20 | __download_url__ = 'https://github.com/tensorlayer/TensorLayerX' 21 | __description__ = 'High Level Deep Learning Library for Researcher and Engineer.' 22 | __license__ = 'apache' 23 | __keywords__ = 'deep learning, machine learning, computer vision, nlp, ' 24 | __keywords__ += 'supervised learning, unsupervised learning, reinforcement learning' 25 | -------------------------------------------------------------------------------- /tensorlayerx/text/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .nlp import * 5 | from tensorlayerx.text import nlp 6 | -------------------------------------------------------------------------------- /tensorlayerx/utils/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | # from .iterate import * 5 | # from .prepro import * 6 | from .lazy_imports import * 7 | # from .visualize import * 8 | -------------------------------------------------------------------------------- /tensorlayerx/utils/lazy_imports.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """This module provides lazy import functionality to improve the import 4 | performance of nitime. For example, some parts of nitime leverage and import 5 | matplotlib, which is quite a big package, yet most of the nitime code does not 6 | depend on matplotlib. By lazily-loading a module, we defer the overhead of 7 | importing it until the first time it is actually used, thereby speeding up 8 | nitime imports. 9 | 10 | A generic :class:`LazyImport` class is implemented which takes the module name 11 | as a parameter, and acts as a proxy for that module, importing it only when 12 | the module is used, but effectively acting as the module in every other way 13 | (including inside IPython with respect to introspection and tab completion) 14 | with the *exception* of reload() - reloading a :class:`LazyImport` raises an 15 | :class:`ImportError`. 16 | 17 | Commonly used nitime lazy imports are also defined in :mod:`nitime.lazy`, so 18 | they can be reused throughout nitime. 19 | """ 20 | import os 21 | import sys 22 | import types 23 | 24 | 25 | class LazyImport(types.ModuleType): 26 | """ 27 | This class takes the module name as a parameter, and acts as a proxy for 28 | that module, importing it only when the module is used, but effectively 29 | acting as the module in every other way (including inside IPython with 30 | respect to introspection and tab completion) with the *exception* of 31 | reload()- reloading a :class:`LazyImport` raises an :class:`ImportError`. 32 | 33 | >>> mlab = LazyImport('matplotlib.mlab') 34 | 35 | No import happens on the above line, until we do something like call an 36 | ``mlab`` method or try to do tab completion or introspection on ``mlab`` 37 | in IPython. 38 | 39 | >>> mlab 40 | 41 | 42 | Now the :class:`LazyImport` will do an actual import, and call the dist 43 | function of the imported module. 44 | 45 | >>> mlab.dist(1969,2011) 46 | 42.0 47 | """ 48 | 49 | def __getattribute__(self, x): 50 | # This method will be called only once, since we'll change 51 | # self.__class__ to LoadedLazyImport, and __getattribute__ will point 52 | # to module.__getattribute__ 53 | 54 | name = object.__getattribute__(self, '__name__') 55 | __import__(name) 56 | 57 | # if name above is 'package.foo.bar', package is returned, the docs 58 | # recommend that in order to get back the full thing, that we import 59 | # and then lookup the full name is sys.modules, see: 60 | # http://docs.python.org/library/functions.html#__import__ 61 | 62 | module = sys.modules[name] 63 | 64 | # Now that we've done the import, cutout the middleman and make self 65 | # act as the imported module 66 | 67 | class LoadedLazyImport(types.ModuleType): 68 | __getattribute__ = module.__getattribute__ 69 | __repr__ = module.__repr__ 70 | 71 | object.__setattr__(self, '__class__', LoadedLazyImport) 72 | 73 | # The next line will make "reload(l)" a silent no-op 74 | return module.__getattribute__(x) 75 | 76 | def __repr__(self): 77 | return "" % object.__getattribute__(self, '__name__') 78 | 79 | 80 | if 'READTHEDOCS' in os.environ: 81 | lazy_doc = """ 82 | WARNING: To get Sphinx documentation to build we disable 83 | LazyImports, which makes Sphinx incorrectly report this 84 | class as having a base class of object. In reality, 85 | :class:`LazyImport`'s base class is 86 | :class:`types.ModuleType`. 87 | """ 88 | 89 | lazy_doc += LazyImport.__doc__ 90 | 91 | class LazyImport(object): 92 | __doc__ = lazy_doc 93 | 94 | def __init__(self, x): 95 | __import__(x) 96 | self.module = sys.modules[x] 97 | 98 | def __getattr__(self, x): 99 | return self.module.__getattribute__(x) 100 | -------------------------------------------------------------------------------- /tensorlayerx/vision/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from tensorlayerx.vision import transforms 4 | from tensorlayerx.vision import ops 5 | from tensorlayerx.vision.transforms.utils import * -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayerx.backend import BACKEND 5 | 6 | if BACKEND == 'tensorflow': 7 | from .tensorflow_ops import * 8 | elif BACKEND == 'mindspore': 9 | from .mindspore_ops import * 10 | elif BACKEND == 'paddle': 11 | from .paddle_ops import * 12 | elif BACKEND == 'torch': 13 | from .torch_ops import * 14 | elif BACKEND == 'oneflow': 15 | from .torch_ops import * 16 | elif BACKEND == 'jittor': 17 | from .jittor_ops import *#TODO 18 | else: 19 | raise NotImplementedError("This backend is not supported") -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/jittor_ops.py: -------------------------------------------------------------------------------- 1 | import jittor as jt 2 | import jittor.transform 3 | import numpy as np 4 | __all__ = [ 5 | 'box_iou', 6 | 'nms', 7 | 'box_area', 8 | ] 9 | 10 | def box_area(boxes): 11 | if len(boxes.shape) == 1: 12 | boxes = boxes[None, :] 13 | return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) 14 | 15 | def box_iou(boxes1, boxes2): 16 | """ 17 | Return intersection-over-union (Jaccard index) of boxes. 18 | 19 | Parameters 20 | ---------- 21 | boxes1 : Tensor 22 | Tensor[N, 4] 23 | boxes2 : Tensor 24 | Tensor[M, 4] 25 | 26 | Returns 27 | ------- 28 | iou: Tensor 29 | Tensor[N, M],the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 30 | 31 | """ 32 | area1 = box_area(boxes1) 33 | area2 = box_area(boxes2) 34 | 35 | tl = jt.maximum(boxes1[:, None, :2], boxes2[:, :2]) 36 | rb = jt.minimum(boxes1[:, None, 2:], boxes2[:, 2:]) 37 | 38 | wh = np.clip(rb - tl, 0, None) 39 | inter = wh[:, :, 0] * wh[:, :, 1] 40 | iou = inter / (area1[:, None] + area2 - inter) 41 | return iou 42 | 43 | 44 | def nms(boxes, scores, iou_threshold): 45 | keep = [] 46 | idx = jt.argsort(scores) 47 | while idx.size > 0: 48 | if idx.size == 1: 49 | i = idx[0] 50 | keep.append(i) 51 | break 52 | else: 53 | max_score_index = idx[-1] 54 | max_score_box = boxes[max_score_index][None, :] 55 | keep.append(max_score_index) 56 | idx = idx[:-1] 57 | other_boxes = boxes[idx] 58 | ious = box_iou(max_score_box, other_boxes) 59 | idx = idx[ious[0] <= iou_threshold] 60 | 61 | keep = jittor.transform.to_tensor(keep) 62 | keep = jt.flatten(keep) 63 | return keep 64 | -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/mindspore_ops.py: -------------------------------------------------------------------------------- 1 | import mindspore as ms 2 | 3 | __all__ = [ 4 | 'box_iou', 5 | 'nms', 6 | 'box_area', 7 | ] 8 | 9 | def box_area(boxes): 10 | if len(boxes.shape) == 1: 11 | boxes = boxes[None, :] 12 | return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) 13 | 14 | def box_iou(boxes1, boxes2): 15 | if len(boxes1.shape) == 1: 16 | boxes1 = boxes1[None, :] 17 | if len(boxes2.shape) == 1: 18 | boxes2 = boxes2[None, :] 19 | op = ms.ops.IOU(mode='iou') 20 | return op(boxes2, boxes1) 21 | 22 | def nms(boxes, scores, iou_threshold): 23 | keep = [] 24 | idx = ms.ops.sort(scores)[1].asnumpy() 25 | while idx.size > 0: 26 | if idx.size == 1: 27 | i = idx[0] 28 | keep.append(i) 29 | break 30 | else: 31 | max_score_index = int(idx[-1]) 32 | max_score_box = boxes[max_score_index][None, :] 33 | keep.append(max_score_index) 34 | idx = idx[:-1] 35 | other_boxes = boxes[ms.Tensor(idx)] 36 | ious = box_iou(max_score_box, other_boxes) 37 | idx = ms.ops.masked_select(ms.Tensor(idx), ms.Tensor(ious[0] <= iou_threshold)) 38 | idx = idx.asnumpy() 39 | 40 | keep = ms.Tensor(keep) 41 | return keep 42 | -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/paddle_ops.py: -------------------------------------------------------------------------------- 1 | import paddle 2 | 3 | __all__ = [ 4 | 'box_iou', 5 | 'nms', 6 | 'box_area', 7 | ] 8 | 9 | def box_area(boxes): 10 | if len(boxes.shape) == 1: 11 | boxes = boxes[None, :] 12 | return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) 13 | 14 | def box_iou(boxes1, boxes2): 15 | if len(boxes1.shape) == 1: 16 | boxes1 = boxes1[None, :] 17 | if len(boxes2.shape) == 1: 18 | boxes2 = boxes2[None, :] 19 | area1 = box_area(boxes1) 20 | area2 = box_area(boxes2) 21 | 22 | lt = paddle.maximum(boxes1[:, None, :2], boxes2[:, :2]) 23 | rb = paddle.minimum(boxes1[:, None, 2:], boxes2[:, 2:]) 24 | 25 | wh = paddle.clip((rb - lt), min = 0) 26 | inter = wh[:, :, 0] * wh[:, :, 1] 27 | iou = inter / (area1[:, None] + area2 - inter) 28 | return iou 29 | 30 | 31 | def nms(boxes, scores, iou_threshold): 32 | keep = [] 33 | idx = paddle.argsort(scores) 34 | while idx.size > 0: 35 | if idx.size == 1: 36 | i = idx[0] 37 | keep.append(i) 38 | break 39 | else: 40 | max_score_index = idx[-1] 41 | max_score_box = boxes[max_score_index][None, :] 42 | keep.append(max_score_index) 43 | idx = idx[:-1] 44 | other_boxes = boxes[idx] 45 | ious = box_iou(max_score_box, other_boxes) 46 | idx = idx[ious[0] <= iou_threshold] 47 | 48 | keep = paddle.to_tensor(keep) 49 | keep = paddle.flatten(keep) 50 | return keep 51 | -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/tensorflow_ops.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | __all__ = [ 4 | 'box_iou', 5 | 'nms', 6 | 'box_area', 7 | ] 8 | 9 | def box_area(boxes): 10 | """ 11 | Computes the area of a set of bounding boxes, which are specified by its 12 | (x1, y1, x2, y2) coordinates. 13 | 14 | Parameters 15 | ---------- 16 | boxes :(Tensor[N, 4]) 17 | boxes for which the area will be computed. They 18 | are expected to be in (x1, y1, x2, y2) format with 19 | ``0 <= x1 < x2`` and ``0 <= y1 < y2``. 20 | 21 | Returns 22 | ------- 23 | area : area (Tensor[N]) 24 | area for each box 25 | """ 26 | 27 | return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) 28 | 29 | def box_iou(boxes1, boxes2): 30 | """ 31 | Return intersection-over-union (Jaccard index) of boxes. 32 | 33 | Parameters 34 | ---------- 35 | boxes1 : Tensor 36 | Tensor[N, 4] 37 | boxes2 : Tensor 38 | Tensor[M, 4] 39 | 40 | Returns 41 | ------- 42 | iou: Tensor 43 | Tensor[N, M],the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2 44 | 45 | """ 46 | area1 = box_area(boxes1) 47 | area2 = box_area(boxes2) 48 | 49 | tl = tf.maximum(boxes1[:, None, :2], boxes2[:, :2]) 50 | rb = tf.minimum(boxes1[:, None, 2:], boxes2[:, 2:]) 51 | 52 | wh = tf.clip_by_value(rb - tl, clip_value_min = 0, clip_value_max = rb - tl) 53 | inter = wh[:, :, 0] * wh[:, :, 1] 54 | 55 | iou = inter / (area1[:, None] + area2 - inter) 56 | return iou 57 | 58 | 59 | def nms(boxes, scores, iou_threshold): 60 | """Performs non-maximum suppression (NMS) on the boxes according 61 | to their intersection-over-union (IoU). 62 | 63 | Parameters 64 | ---------- 65 | boxes : Tensor 66 | Tensor[N, 4] 67 | scores : Tensor 68 | Tensor[N], scores for each one of the boxes 69 | iou_threshold : float 70 | discards all overlapping boxes with IoU > iou_threshold 71 | 72 | Returns 73 | ------- 74 | keep : Tensor 75 | int64 tensor with the indices of the elements that have been kept by NMS, sorted in decreasing order of scores 76 | """ 77 | 78 | return tf.image.non_max_suppression(boxes = boxes, scores = scores, max_output_size=50, iou_threshold=iou_threshold) 79 | -------------------------------------------------------------------------------- /tensorlayerx/vision/ops/torch_ops.py: -------------------------------------------------------------------------------- 1 | import torchvision 2 | 3 | __all__ = [ 4 | 'box_iou', 5 | 'nms', 6 | 'box_area', 7 | ] 8 | 9 | def box_area(boxes): 10 | 11 | return torchvision.ops.box_area(boxes) 12 | 13 | def box_iou(boxes1, boxes2): 14 | return torchvision.ops.box_iou(boxes1, boxes2) 15 | 16 | 17 | def nms(boxes, scores, iou_threshold): 18 | return torchvision.ops.nms(boxes, scores, iou_threshold) 19 | -------------------------------------------------------------------------------- /tensorlayerx/vision/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from .transforms import * -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/tests/__init__.py -------------------------------------------------------------------------------- /tests/dataflow/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/tests/dataflow/__init__.py -------------------------------------------------------------------------------- /tests/layers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/tests/layers/__init__.py -------------------------------------------------------------------------------- /tests/layers/test_layers_core_basedense_dropout.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | import tensorlayerx 9 | import tensorlayerx as tlx 10 | 11 | from tests.utils import CustomTestCase 12 | 13 | 14 | class Layer_Core_Test(CustomTestCase): 15 | 16 | @classmethod 17 | def setUpClass(self): 18 | 19 | self.batch_size = 8 20 | 21 | self.inputs_shape = [self.batch_size, 784] 22 | self.input = tlx.nn.Input(self.inputs_shape) 23 | self.dense1 = tlx.nn.Linear(out_features=800, act=tlx.ReLU, in_features=784, name='test_dense') 24 | self.n1 = self.dense1(self.input) 25 | 26 | self.dropout1 = tlx.nn.Dropout(p=0.2) 27 | self.n2 = self.dropout1(self.n1) 28 | 29 | self.dense2 = tlx.nn.Linear(out_features=10, act='relu', b_init=None, in_features=800) 30 | self.n3 = self.dense2(self.n2) 31 | 32 | self.dense3 = tlx.nn.Linear(out_features=10, act='relu', b_init=None, in_features=10) 33 | self.n4 = self.dense3(self.n3) 34 | 35 | self.concat = tlx.nn.Concat(concat_dim=-1)([self.n2, self.n3]) 36 | 37 | class get_model(tensorlayerx.nn.Module): 38 | 39 | def __init__(self): 40 | super(get_model, self).__init__() 41 | self.layer1 = tlx.nn.Linear(out_features=800, act=tlx.ReLU, in_features=784, name='test_dense') 42 | self.dp = tlx.nn.Dropout(p=0.2) 43 | self.layer2 = tlx.nn.Linear(out_features=10, act='relu', b_init=None, in_features=800) 44 | self.layer3 = tlx.nn.Linear(out_features=10, act='relu', b_init=None, in_features=10) 45 | 46 | def forward(self, inputs): 47 | z = self.layer1(inputs) 48 | z = self.dp(z) 49 | z = self.layer2(z) 50 | z = self.layer3(z) 51 | return z 52 | 53 | self.net = get_model() 54 | 55 | @classmethod 56 | def tearDownClass(cls): 57 | pass 58 | 59 | def test_dense(self): 60 | self.assertEqual(tlx.get_tensor_shape(self.n1), [self.batch_size, 800]) 61 | 62 | def test_dense_nonbias(self): 63 | self.assertEqual(len(self.dense2.all_weights), 1) 64 | 65 | def test_dropout(self): 66 | self.assertEqual(len(self.dropout1.all_weights), 0) 67 | 68 | def test_model(self): 69 | self.assertEqual(len(self.net.all_weights), 4) 70 | 71 | 72 | if __name__ == '__main__': 73 | 74 | unittest.main() 75 | -------------------------------------------------------------------------------- /tests/layers/test_layers_core_nested.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*-\ 3 | import os 4 | import unittest 5 | 6 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 7 | import tensorlayerx 8 | import tensorlayerx as tlx 9 | import numpy as np 10 | 11 | from tests.utils import CustomTestCase 12 | 13 | 14 | class Layer_nested(CustomTestCase): 15 | 16 | @classmethod 17 | def setUpClass(cls): 18 | print("##### begin testing nested layer #####") 19 | 20 | @classmethod 21 | def tearDownClass(cls): 22 | pass 23 | 24 | def test_nested_layer_with_inchannels(cls): 25 | 26 | class MyLayer(tensorlayerx.nn.Module): 27 | 28 | def __init__(self, name=None): 29 | super(MyLayer, self).__init__(name=name) 30 | self.input_layer = tlx.nn.Linear(in_features=50, out_features=20) 31 | self.build(None) 32 | self._built = True 33 | 34 | def build(self, inputs_shape=None): 35 | self.W = self._get_weights('weights', shape=(20, 10)) 36 | 37 | def forward(self, inputs): 38 | inputs = self.input_layer(inputs) 39 | output = tlx.ops.matmul(inputs, self.W) 40 | return output 41 | 42 | class model(tensorlayerx.nn.Module): 43 | 44 | def __init__(self, name=None): 45 | super(model, self).__init__(name=name) 46 | self.layer = MyLayer() 47 | 48 | def forward(self, inputs): 49 | return self.layer(inputs) 50 | 51 | input = tlx.nn.Input(shape=(100, 50)) 52 | model_dynamic = model() 53 | model_dynamic.set_train() 54 | cls.assertEqual(model_dynamic(input).shape, (100, 10)) 55 | cls.assertEqual(len(model_dynamic.all_weights), 3) 56 | cls.assertEqual(len(model_dynamic.trainable_weights), 3) 57 | 58 | model_dynamic.layer.input_layer.biases.assign_add(tlx.ones((20, ))) 59 | cls.assertEqual(np.sum(model_dynamic.all_weights[-1].numpy() - tlx.ones(20, ).numpy()), 0) 60 | 61 | 62 | if __name__ == '__main__': 63 | 64 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 65 | 66 | unittest.main() 67 | -------------------------------------------------------------------------------- /tests/layers/test_layers_deformable_convolution.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | import tensorlayerx 9 | import tensorlayerx as tlx 10 | from tests.utils import CustomTestCase 11 | 12 | 13 | class Layer_Convolution_2D_Test(CustomTestCase): 14 | 15 | @classmethod 16 | def setUpClass(self): 17 | print("\n#################################") 18 | 19 | self.batch_size = 5 20 | self.inputs_shape = [self.batch_size, 10, 10, 16] 21 | self.input_layer = tlx.nn.Input(self.inputs_shape, name='input_layer') 22 | 23 | self.offset1 = tlx.nn.Conv2d( 24 | out_channels=18, kernel_size=(3, 3), stride=(1, 1), padding='SAME', name='offset1' 25 | )(self.input_layer) 26 | self.init_deformconv1 = tlx.nn.DeformableConv2d( 27 | offset_layer=self.offset1, out_channels=32, kernel_size=(3, 3), act='relu', name='deformable1' 28 | ) 29 | self.deformconv1 = self.init_deformconv1(self.input_layer) 30 | self.offset2 = tlx.nn.Conv2d( 31 | out_channels=18, kernel_size=(3, 3), stride=(1, 1), padding='SAME', name='offset2' 32 | )(self.deformconv1) 33 | self.deformconv2 = tlx.nn.DeformableConv2d( 34 | offset_layer=self.offset2, out_channels=64, kernel_size=(3, 3), act='relu', name='deformable2' 35 | )(self.deformconv1) 36 | 37 | @classmethod 38 | def tearDownClass(self): 39 | pass 40 | 41 | def test_layer_n1(self): 42 | 43 | self.assertEqual(len(self.init_deformconv1.all_weights), 2) 44 | self.assertEqual(tlx.get_tensor_shape(self.deformconv1)[1:], [10, 10, 32]) 45 | 46 | def test_layer_n2(self): 47 | self.assertEqual(tlx.get_tensor_shape(self.deformconv2)[1:], [10, 10, 64]) 48 | 49 | 50 | if __name__ == '__main__': 51 | 52 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 53 | 54 | unittest.main() 55 | -------------------------------------------------------------------------------- /tests/layers/test_layers_embedding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | import tensorlayerx 9 | import tensorlayerx as tlx 10 | import numpy as np 11 | 12 | from tests.utils import CustomTestCase 13 | 14 | 15 | class Layer_Embed_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | pass 20 | 21 | @classmethod 22 | def tearDownClass(cls): 23 | pass 24 | 25 | def test_onehot(self): 26 | input = tlx.nn.Input([32], dtype=tlx.int32) 27 | onehot = tlx.nn.OneHot(depth=8, on_value=1, off_value=0, axis=-1) 28 | print(onehot) 29 | tensor = tlx.nn.OneHot(depth=8)(input) 30 | self.assertEqual(tensor.get_shape().as_list(), [32, 8]) 31 | 32 | def test_embed(self): 33 | input = tlx.nn.Input([8, 100], dtype=tlx.int32) 34 | embed = tlx.nn.Embedding(num_embeddings=1000, embedding_dim=50, name='embed') 35 | print(embed) 36 | tensor = embed(input) 37 | self.assertEqual(tensor.get_shape().as_list(), [8, 100, 50]) 38 | 39 | def test_avg_embed(self): 40 | batch_size = 8 41 | length = 5 42 | input = tlx.nn.Input([batch_size, length], dtype=tlx.int32) 43 | avgembed = tlx.nn.AverageEmbedding(num_embeddings=1000, embedding_dim=50, name='avg') 44 | print(avgembed) 45 | tensor = avgembed(input) 46 | # print(tensor) 47 | self.assertEqual(tensor.get_shape().as_list(), [batch_size, 50]) 48 | 49 | def test_word2vec_nce(self): 50 | batch_size = 8 51 | embedding_dim = 50 52 | inputs = tlx.nn.Input([batch_size], dtype=tlx.int32) 53 | labels = tlx.nn.Input([batch_size, 1], dtype=tlx.int32) 54 | emb_net = tlx.nn.Word2vecEmbedding( 55 | num_embeddings=10000, 56 | embedding_dim=embedding_dim, 57 | num_sampled=100, 58 | activate_nce_loss=True, # the nce loss is activated 59 | nce_loss_args={}, 60 | E_init=tensorlayerx.nn.initializers.random_uniform(minval=-1.0, maxval=1.0), 61 | nce_W_init=tensorlayerx.nn.initializers.truncated_normal(stddev=float(1.0 / np.sqrt(embedding_dim))), 62 | nce_b_init=tensorlayerx.nn.initializers.constant(value=0.0), 63 | ) 64 | print(emb_net) 65 | 66 | embed_tensor = emb_net([inputs, labels], use_nce_loss=False) 67 | embed_tensor, embed_nce_loss = emb_net([inputs, labels], use_nce_loss=True) 68 | embed_tensor, embed_nce_loss = emb_net([inputs, labels]) 69 | self.assertEqual(embed_tensor.get_shape().as_list(), [batch_size, embedding_dim]) 70 | 71 | def test_word2vec_no_nce(self): 72 | batch_size = 8 73 | embedding_dim = 50 74 | inputs = tlx.nn.Input([batch_size], dtype=tlx.int32) 75 | emb_net = tlx.nn.Word2vecEmbedding( 76 | num_embeddings=10000, 77 | embedding_dim=embedding_dim, 78 | num_sampled=100, 79 | activate_nce_loss=False, 80 | nce_loss_args={}, 81 | E_init=tensorlayerx.nn.initializers.random_uniform(minval=-1.0, maxval=1.0), 82 | nce_W_init=tensorlayerx.nn.initializers.truncated_normal(stddev=float(1.0 / np.sqrt(embedding_dim))), 83 | nce_b_init=tensorlayerx.nn.initializers.constant(value=0.0), 84 | ) 85 | print(emb_net) 86 | embed_tensor = emb_net(inputs) 87 | embed_tensor = emb_net(inputs, use_nce_loss=False) 88 | try: 89 | embed_tensor = emb_net(inputs, use_nce_loss=True) 90 | except AttributeError as e: 91 | print(e) 92 | self.assertEqual(embed_tensor.get_shape().as_list(), [batch_size, embedding_dim]) 93 | 94 | 95 | if __name__ == '__main__': 96 | 97 | unittest.main() 98 | -------------------------------------------------------------------------------- /tests/layers/test_layers_extend.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | import tensorlayerx as tlx 9 | 10 | from tests.utils import CustomTestCase 11 | 12 | 13 | class Layer_Extend_Test(CustomTestCase): 14 | 15 | @classmethod 16 | def setUpClass(cls): 17 | pass 18 | 19 | @classmethod 20 | def tearDownClass(cls): 21 | pass 22 | 23 | def test_expand_dims(self): 24 | x = tlx.nn.Input([8, 3]) 25 | expandlayer = tlx.nn.ExpandDims(axis=-1) 26 | y = expandlayer(x) 27 | self.assertEqual(tlx.get_tensor_shape(y), [8, 3, 1]) 28 | 29 | def test_tile(self): 30 | x = tlx.nn.Input([8, 3]) 31 | tilelayer = tlx.nn.Tile(multiples=[2, 3]) 32 | y = tilelayer(x) 33 | self.assertEqual(tlx.get_tensor_shape(y), [16, 9]) 34 | 35 | 36 | if __name__ == '__main__': 37 | 38 | unittest.main() 39 | -------------------------------------------------------------------------------- /tests/layers/test_layers_merge.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | 9 | import numpy as np 10 | import tensorlayerx as tlx 11 | import tensorlayerx 12 | from tests.utils import CustomTestCase 13 | 14 | 15 | class Layer_Merge_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | pass 20 | 21 | @classmethod 22 | def tearDownClass(cls): 23 | pass 24 | 25 | def test_concat(self): 26 | 27 | class CustomModel(tensorlayerx.nn.Module): 28 | 29 | def __init__(self): 30 | super(CustomModel, self).__init__() 31 | self.dense1 = tlx.nn.Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu1_1') 32 | self.dense2 = tlx.nn.Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu2_1') 33 | self.concat = tlx.nn.Concat(concat_dim=1, name='concat_layer') 34 | 35 | def forward(self, inputs): 36 | d1 = self.dense1(inputs) 37 | d2 = self.dense2(inputs) 38 | outputs = self.concat([d1, d2]) 39 | return outputs 40 | 41 | model = CustomModel() 42 | model.set_train() 43 | inputs = tlx.ops.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) 44 | outputs = model(inputs) 45 | print(model) 46 | 47 | self.assertEqual(outputs.get_shape().as_list(), [4, 20]) 48 | 49 | def test_elementwise(self): 50 | 51 | class CustomModel(tensorlayerx.nn.Module): 52 | 53 | def __init__(self): 54 | super(CustomModel, self).__init__() 55 | self.dense1 = tlx.nn.Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu1_1') 56 | self.dense2 = tlx.nn.Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu2_1') 57 | self.element = tlx.nn.Elementwise(combine_fn=tlx.minimum, name='minimum', act=None) 58 | 59 | def forward(self, inputs): 60 | d1 = self.dense1(inputs) 61 | d2 = self.dense2(inputs) 62 | outputs = self.element([d1, d2]) 63 | return outputs, d1, d2 64 | 65 | model = CustomModel() 66 | model.set_train() 67 | inputs = tlx.ops.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) 68 | outputs, d1, d2 = model(inputs) 69 | print(model) 70 | 71 | min = tlx.ops.minimum(d1, d2) 72 | self.assertEqual(outputs.get_shape().as_list(), [4, 10]) 73 | self.assertTrue(np.array_equal(min.numpy(), outputs.numpy())) 74 | 75 | 76 | if __name__ == '__main__': 77 | 78 | unittest.main() 79 | -------------------------------------------------------------------------------- /tests/layers/test_layers_noise.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | 9 | import tensorlayerx as tlx 10 | import tensorlayerx 11 | from tests.utils import CustomTestCase 12 | 13 | 14 | class Layer_Convolution_1D_Test(CustomTestCase): 15 | 16 | @classmethod 17 | def setUpClass(cls): 18 | print("\n#################################") 19 | 20 | cls.batch_size = 8 21 | cls.inputs_shape = [cls.batch_size, 200] 22 | cls.input_layer = tlx.layers.Input(cls.inputs_shape, name='input_layer') 23 | 24 | cls.dense = tlx.nn.Linear(out_features=100, act=tlx.ReLU, in_features=200)(cls.input_layer) 25 | 26 | cls.noiselayer = tlx.nn.GaussianNoise(name='gaussian')(cls.dense) 27 | 28 | @classmethod 29 | def tearDownClass(cls): 30 | pass 31 | 32 | def test_layer_n1(self): 33 | self.assertEqual(self.noiselayer.get_shape().as_list()[1:], [100]) 34 | 35 | 36 | if __name__ == '__main__': 37 | 38 | # tlx.logging.set_verbosity(tlx.logging.DEBUG) 39 | 40 | unittest.main() 41 | -------------------------------------------------------------------------------- /tests/layers/test_layers_padding.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | 9 | import tensorlayerx as tlx 10 | import tensorlayerx 11 | from tests.utils import CustomTestCase 12 | 13 | 14 | class Layer_Padding_Test(CustomTestCase): 15 | 16 | @classmethod 17 | def setUpClass(cls): 18 | 19 | ## 1D 20 | cls.input_layer1 = tlx.nn.Input([None, 100, 1], name='input_layer1') 21 | 22 | n2 = tlx.nn.ZeroPad1d(padding=(2, 3))(cls.input_layer1) 23 | 24 | cls.n2_shape = n2.get_shape().as_list() 25 | 26 | ## 2D 27 | cls.input_layer2 = tlx.nn.Input([None, 100, 100, 3], name='input_layer2') 28 | 29 | n0 = tlx.nn.PadLayer([[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT", name='inpad')(cls.input_layer2) 30 | n5 = tlx.nn.ZeroPad2d(padding=((3, 3), (4, 4)), data_format='channels_last')(cls.input_layer2) 31 | 32 | cls.n0_shape = n0.get_shape().as_list() 33 | cls.n5_shape = n5.get_shape().as_list() 34 | 35 | ## 3D 36 | cls.input_layer3 = tlx.nn.Input([None, 100, 100, 100, 3], name='input_layer3') 37 | 38 | n8 = tlx.nn.ZeroPad3d(padding=((3, 3), (4, 4), (5, 5)))(cls.input_layer3) 39 | 40 | cls.n8_shape = n8.get_shape().as_list() 41 | 42 | @classmethod 43 | def tearDownClass(cls): 44 | pass 45 | 46 | def test_n0_shape(self): 47 | self.assertEqual(self.n0_shape[1:], [106, 106, 3]) 48 | 49 | def test_n2_shape(self): 50 | self.assertEqual(self.n2_shape[1:], [105, 1]) 51 | 52 | def test_n5_shape(self): 53 | self.assertEqual(self.n5_shape[1:], [106, 108, 3]) 54 | 55 | def test_n8_shape(self): 56 | self.assertEqual(self.n8_shape[1:], [106, 108, 110, 3]) 57 | 58 | 59 | if __name__ == '__main__': 60 | 61 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 62 | 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /tests/layers/test_layers_resampling.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import sys 4 | sys.path.append("/home/wurundi/workspace/tensorlayer2") 5 | 6 | import os 7 | import unittest 8 | 9 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 10 | 11 | import tensorlayerx as tlx 12 | from tensorlayerx.nn import Input 13 | import tensorlayerx 14 | from tests.utils import CustomTestCase 15 | 16 | 17 | class Layer_Pooling_Test(CustomTestCase): 18 | 19 | @classmethod 20 | def setUpClass(cls): 21 | 22 | ## 1D ======================================================================== 23 | 24 | ## 2D ======================================================================== 25 | 26 | x_2_input_shape = [None, 100, 100, 3] 27 | nin_2 = Input(x_2_input_shape) 28 | 29 | 30 | n6 = tlx.nn.Conv2d(out_channels=32, kernel_size=(3, 3), stride=(2, 2), name='test_conv2d')(nin_2) 31 | 32 | n7 = tlx.nn.UpSampling2d(scale=(2, 2), name='test_UpSampling2d_1')(n6) 33 | 34 | n8 = tlx.nn.UpSampling2d(scale=3, name='test_UpSampling2d_2')(n6) 35 | 36 | n9 = tlx.nn.DownSampling2d(scale=(2, 2), name='test_DownSampling2d_1')(n6) 37 | 38 | n10 = tlx.nn.DownSampling2d(scale=5, name='test_DownSampling2d_2')(n6) 39 | 40 | cls.n6_shape = n6.get_shape().as_list() 41 | cls.n7_shape = n7.get_shape().as_list() 42 | cls.n8_shape = n8.get_shape().as_list() 43 | cls.n9_shape = n9.get_shape().as_list() 44 | cls.n10_shape = n10.get_shape().as_list() 45 | 46 | @classmethod 47 | def tearDownClass(cls): 48 | pass 49 | # tf.reset_default_graph() 50 | 51 | def test_UpSampling2d(self): 52 | self.assertEqual(self.n7_shape[1:3], [100, 100]) 53 | self.assertEqual(self.n8_shape[1:3], [150, 150]) 54 | 55 | try: 56 | layer = tlx.nn.UpSampling2d(scale=(2, 2, 2)) 57 | except Exception as e: 58 | print(e) 59 | 60 | def test_DownSampling2d(self): 61 | self.assertEqual(self.n9_shape[1:3], [25, 25]) 62 | self.assertEqual(self.n10_shape[1:3], [10, 10]) 63 | 64 | try: 65 | layer = tlx.nn.DownSampling2d(scale=(2, 2, 2)) 66 | except Exception as e: 67 | print(e) 68 | 69 | 70 | if __name__ == '__main__': 71 | 72 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 73 | 74 | unittest.main() 75 | -------------------------------------------------------------------------------- /tests/layers/test_layers_scale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 8 | 9 | from tests.utils import CustomTestCase 10 | import tensorlayerx 11 | 12 | class Layer_Scale_Test(CustomTestCase): 13 | 14 | @classmethod 15 | def setUpClass(cls): 16 | pass 17 | 18 | @classmethod 19 | def tearDownClass(cls): 20 | pass 21 | 22 | def test_scale(self): 23 | 24 | class model(tensorlayerx.nn.Module): 25 | 26 | def __init__(self): 27 | super(model, self).__init__() 28 | self.dense = tensorlayerx.layers.Linear(out_features=10) 29 | self.scalelayer = tensorlayerx.layers.Scale(init_scale=0.5) 30 | 31 | def forward(self, inputs): 32 | output1 = self.dense(inputs) 33 | output2 = self.scalelayer(output1) 34 | return output1, output2 35 | 36 | input = tensorlayerx.layers.Input((8, 3), init=tensorlayerx.nn.initializers.random_normal()) 37 | net = model() 38 | net.set_train() 39 | dout, fout = net(input) 40 | 41 | for i in range(len(dout)): 42 | for j in range(len(dout[i])): 43 | self.assertEqual(dout[i][j].numpy() * 0.5, fout[i][j].numpy()) 44 | 45 | 46 | if __name__ == '__main__': 47 | 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /tests/layers/test_layers_stack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import unittest 5 | 6 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 7 | 8 | import tensorlayerx as tlx 9 | from tensorlayerx.nn import Input, Linear, Stack, UnStack 10 | import tensorlayerx 11 | from tests.utils import CustomTestCase 12 | 13 | 14 | class Layer_Stack_Test(CustomTestCase): 15 | 16 | @classmethod 17 | def setUpClass(cls): 18 | print("-" * 20, "Layer_Stack_Test", "-" * 20) 19 | cls.batch_size = 4 20 | cls.inputs_shape = [cls.batch_size, 10] 21 | 22 | cls.ni = Input(cls.inputs_shape, name='input_layer') 23 | 24 | class model(tensorlayerx.nn.Module): 25 | 26 | def __init__(self): 27 | super(model, self).__init__() 28 | self.a = Linear(out_features=5) 29 | self.b = Linear(out_features=5) 30 | self.stack = Stack(axis=1) 31 | 32 | def forward(self, inputs): 33 | output1 = self.a(inputs) 34 | output2 = self.b(inputs) 35 | output = self.stack([output1, output2]) 36 | return output 37 | 38 | a = Linear(out_features=5)(cls.ni) 39 | b = Linear(out_features=5)(cls.ni) 40 | cls.layer1 = Stack(axis=1) 41 | cls.n1 = cls.layer1([a, b]) 42 | 43 | net = model() 44 | net.set_train() 45 | cls.inputs = Input(cls.inputs_shape) 46 | cls.n2 = net(cls.inputs) 47 | 48 | @classmethod 49 | def tearDownClass(cls): 50 | pass 51 | 52 | def test_layer_n1(self): 53 | self.assertEqual(self.n1.shape, (4, 2, 5)) 54 | 55 | def test_layer_n2(self): 56 | self.assertEqual(self.n2.shape, (4, 2, 5)) 57 | 58 | 59 | class Layer_UnStack_Test(CustomTestCase): 60 | 61 | @classmethod 62 | def setUpClass(cls): 63 | print("-" * 20, "Layer_UnStack_Test", "-" * 20) 64 | cls.batch_size = 4 65 | cls.inputs_shape = [cls.batch_size, 10] 66 | 67 | cls.ni = Input(cls.inputs_shape, name='input_layer') 68 | a = Linear(out_features=5)(cls.ni) 69 | cls.layer1 = UnStack(axis=1) 70 | cls.n1 = cls.layer1(a) 71 | 72 | class model(tensorlayerx.nn.Module): 73 | 74 | def __init__(self): 75 | super(model, self).__init__() 76 | self.a = Linear(out_features=5) 77 | self.unstack = UnStack(axis=1) 78 | 79 | def forward(self, inputs): 80 | output1 = self.a(inputs) 81 | output = self.unstack(output1) 82 | return output 83 | 84 | cls.inputs = Input(cls.inputs_shape) 85 | net = model() 86 | net.set_train() 87 | cls.n2 = net(cls.inputs) 88 | 89 | print(cls.layer1) 90 | 91 | @classmethod 92 | def tearDownClass(cls): 93 | pass 94 | 95 | def test_layer_n1(self): 96 | self.assertEqual(len(self.n1), 5) 97 | self.assertEqual(self.n1[0].shape, (self.batch_size, )) 98 | 99 | def test_layer_n2(self): 100 | self.assertEqual(len(self.n2), 5) 101 | self.assertEqual(self.n1[0].shape, (self.batch_size, )) 102 | 103 | 104 | if __name__ == '__main__': 105 | 106 | tlx.logging.set_verbosity(tlx.logging.DEBUG) 107 | 108 | unittest.main() 109 | -------------------------------------------------------------------------------- /tests/models/test_keras_save.py: -------------------------------------------------------------------------------- 1 | from tensorflow.python.keras.applications import VGG16 2 | from tensorflow.python.keras.layers import Dense, Conv2D 3 | from tensorflow.python.keras import Model 4 | from tensorflow.python.training import saver 5 | import tensorflow as tf 6 | 7 | # get the whole model 8 | # vgg = VGG16(weights=None) 9 | # print([x.name for x in vgg.weights]) 10 | 11 | 12 | class Nested_VGG(Model): 13 | 14 | def __init__(self): 15 | super(Nested_VGG, self).__init__() 16 | self.vgg1 = VGG16(weights=None) 17 | # print([x.name for x in self.vgg1.weights]) 18 | self.vgg2 = VGG16(weights=None) 19 | self.dense = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1') 20 | 21 | def call(self, inputs, training=None, mask=None): 22 | pass 23 | 24 | 25 | class MyModel(Model): 26 | 27 | def __init__(self): 28 | super(MyModel, self).__init__() 29 | self.inner = Nested_VGG() 30 | 31 | def call(self, inputs, training=None, mask=None): 32 | pass 33 | 34 | 35 | model = MyModel() 36 | print([x.name for x in model.layers]) 37 | # print([x.name for x in model.inner.weights]) 38 | print('vgg1:') 39 | print([x.name for x in model.inner.vgg1.weights]) 40 | print([x.name for x in model.inner.vgg1.layers]) 41 | 42 | print('vgg2') 43 | print(model.inner.vgg2.get_layer('block1_conv1').kernel.name) 44 | print([x.name for x in model.inner.vgg2.weights]) 45 | print([x.name for x in model.inner.vgg2.layers]) 46 | model.save_weights('./keras_model.h5') 47 | -------------------------------------------------------------------------------- /tests/performance_test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayerX/d64bfffedc2867367a826a1d57901926792dee20/tests/performance_test/__init__.py -------------------------------------------------------------------------------- /tests/utils/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tests.utils.custom_testcase import * 5 | from tests.utils.list_py_files import * 6 | from tests.utils.timeout_utils import * 7 | -------------------------------------------------------------------------------- /tests/utils/custom_testcase.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import unittest 4 | from contextlib import contextmanager 5 | 6 | __all__ = [ 7 | 'CustomTestCase', 8 | ] 9 | 10 | 11 | class CustomTestCase(unittest.TestCase): 12 | 13 | @contextmanager 14 | def assertNotRaises(self, exc_type): 15 | try: 16 | yield None 17 | except exc_type: 18 | raise self.failureException('{} raised'.format(exc_type.__name__)) 19 | -------------------------------------------------------------------------------- /tests/utils/list_py_files.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | __all__ = [ 4 | 'list_all_py_files', 5 | ] 6 | 7 | _excludes = [ 8 | 'tensorlayer/db.py', 9 | ] 10 | 11 | 12 | def _list_py_files(root): 13 | for root, _dirs, files in os.walk(root): 14 | if root.find('third_party') != -1: 15 | continue 16 | for file in files: 17 | if file.endswith('.py'): 18 | yield os.path.join(root, file) 19 | 20 | 21 | def list_all_py_files(): 22 | dirs = ['tensorlayer', 'tests', 'example'] 23 | for d in dirs: 24 | for filename in _list_py_files(d): 25 | if filename not in _excludes: 26 | yield filename 27 | -------------------------------------------------------------------------------- /tests/utils/timeout_utils.py: -------------------------------------------------------------------------------- 1 | import platform 2 | 3 | if platform.system() != "Windows": 4 | import signal 5 | else: 6 | signal = None 7 | 8 | __all__ = ['TimeoutError', 'WindowsError', 'TimeoutContext'] 9 | 10 | 11 | class TimeoutError(Exception): 12 | pass 13 | 14 | 15 | class WindowsError(Exception): 16 | pass 17 | 18 | 19 | class TimeoutContext(): 20 | """Timeout class using ALARM signal.""" 21 | 22 | def __init__(self, sec): 23 | self.sec = sec 24 | 25 | def __enter__(self): 26 | if signal is None: 27 | raise WindowsError("Windows is not supported for this test") 28 | 29 | signal.signal(signal.SIGALRM, self.raise_timeout) 30 | signal.alarm(self.sec) 31 | 32 | def __exit__(self, *args): 33 | signal.alarm(0) # disable alarm 34 | 35 | def raise_timeout(self, *args): 36 | raise TimeoutError("A timeout error have been raised.") 37 | -------------------------------------------------------------------------------- /tlx: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | SOURCE="${BASH_SOURCE[0]}" 4 | while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink 5 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 6 | SOURCE="$(readlink "$SOURCE")" 7 | [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located 8 | done 9 | DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" 10 | 11 | export PYTHONPATH="${DIR}/src:${PYTHONPATH}" 12 | --------------------------------------------------------------------------------