├── .circleci └── config.yml ├── .codacy.yaml ├── .dockerignore ├── .github ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md ├── changelog.yml └── stale.yml ├── .gitignore ├── .pyup.yml ├── .readthedocs.yml ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.rst ├── Makefile ├── README.md ├── README.rst ├── docker ├── DockerLint.bat ├── Dockerfile ├── docs │ ├── Dockerfile │ └── sources.list.ustc ├── pypi_list.py └── version_prefix.py ├── docs ├── Makefile ├── _static │ └── fix_rtd.css ├── conf.py ├── images │ ├── 3d_human_pose_result.jpg │ ├── affine_transform_comparison.jpg │ ├── affine_transform_why.jpg │ ├── human_pose_points.jpg │ ├── yolov4_image_result.png │ └── yolov4_video_result.gif ├── index.rst ├── make.bat ├── modules │ ├── activation.rst │ ├── array_ops.rst │ ├── cli.rst │ ├── cost.rst │ ├── db.rst │ ├── distributed.rst │ ├── files.rst │ ├── initializers.rst │ ├── iterate.rst │ ├── layers.rst │ ├── models.rst │ ├── nlp.rst │ ├── optimizers.rst │ ├── prepro.rst │ ├── rein.rst │ ├── utils.rst │ └── visualize.rst └── user │ ├── contributing.rst │ ├── examples.rst │ ├── faq.rst │ ├── get_involved.rst │ ├── get_start_advance.rst │ ├── get_start_model.rst │ ├── installation.rst │ └── my_figs │ ├── basic_seq2seq.png │ ├── img_tensorlayer.png │ ├── img_tlayer.png │ ├── img_tlayer_big.png │ ├── img_tunelayer.png │ ├── karpathy_rnn.jpeg │ ├── mnist.jpeg │ ├── pong_game.jpeg │ ├── seq2seq.png │ ├── tl_black_logo.png │ ├── tl_transparent_logo.png │ ├── tl_white_logo.png │ ├── tsne.png │ └── word2vec_basic.pdf ├── examples ├── README.md ├── app_tutorials │ ├── README.md │ ├── model │ │ ├── coco.names │ │ ├── pose_weights_config.txt │ │ └── yolov4_weights_config.txt │ ├── tutorial_human_3dpose_estimation_LCN.py │ ├── tutorial_object_detection_yolov4_image.py │ └── tutorial_object_detection_yolov4_video.py ├── basic_tutorials │ ├── README.md │ ├── tutorial_cifar10_cnn_static.py │ ├── tutorial_mnist_mlp_dynamic.py │ ├── tutorial_mnist_mlp_dynamic_2.py │ ├── tutorial_mnist_mlp_static.py │ ├── tutorial_mnist_mlp_static_2.py │ ├── tutorial_mnist_siamese.py │ └── tutorial_mnist_simple.py ├── data_process │ ├── README.md │ ├── data │ │ ├── .DS_Store │ │ ├── __init__.py │ │ ├── cat │ │ │ ├── img1.jpg │ │ │ ├── img2.jpg │ │ │ ├── img3.jpg │ │ │ ├── img4.jpg │ │ │ ├── img5.jpg │ │ │ ├── img6.jpg │ │ │ ├── img7.jpg │ │ │ ├── img8.jpg │ │ │ └── img9.jpg │ │ ├── cat_caption.json │ │ ├── dog │ │ │ ├── img1.jpg │ │ │ ├── img2.jpg │ │ │ ├── img3.jpg │ │ │ ├── img4.jpg │ │ │ ├── img5.jpg │ │ │ ├── img6.jpg │ │ │ ├── img7.jpg │ │ │ ├── img8.jpg │ │ │ └── img9.jpg │ │ ├── greenbackground │ │ │ ├── 0.jpg │ │ │ └── 1.jpg │ │ └── tiger.jpeg │ ├── tutorial_fast_affine_transform.py │ ├── tutorial_tf_dataset_voc.py │ ├── tutorial_tfrecord.py │ ├── tutorial_tfrecord2.py │ └── tutorial_tfrecord3.py ├── database │ ├── README.md │ ├── dispatch_tasks.py │ ├── run_tasks.py │ └── task_script.py ├── deprecated_tutorials │ ├── tutorial_image_preprocess.py │ ├── tutorial_imagenet_inceptionV3_distributed.py │ ├── tutorial_mnist_distributed.py │ └── tutorial_mnist_distributed.yml ├── distributed_training │ ├── README.md │ ├── tutorial_cifar10_distributed_trainer.py │ └── tutorial_mnist_distributed_trainer.py ├── keras_tfslim │ ├── README.md │ └── tutorial_keras.py ├── pretrained_cnn │ ├── README.md │ ├── data │ │ ├── __init__.py │ │ ├── imagenet_class_index.json │ │ ├── imagenet_classes.py │ │ ├── laska.png │ │ ├── puzzle.jpeg │ │ └── tiger.jpeg │ ├── tutorial_load_ckpt_weights_to_tensorlayer.py │ ├── tutorial_models_mobilenetv1.py │ ├── tutorial_models_resnet50.py │ ├── tutorial_models_squeezenetv1.py │ ├── tutorial_models_vgg16.py │ ├── tutorial_models_vgg19.py │ └── tutorial_models_vgg_static.py ├── quantized_net │ ├── README.md │ ├── tutorial_binarynet_cifar10_tfrecord.py │ ├── tutorial_binarynet_mnist_cnn.py │ ├── tutorial_dorefanet_cifar10_tfrecord.py │ ├── tutorial_dorefanet_mnist_cnn.py │ ├── tutorial_quanconv_cifar10.py │ ├── tutorial_quanconv_mnist.py │ ├── tutorial_ternaryweight_cifar10_tfrecord.py │ └── tutorial_ternaryweight_mnist_cnn.py ├── reinforcement_learning │ ├── .gitignore │ ├── README.md │ ├── tutorial_A3C.py │ ├── tutorial_AC.py │ ├── tutorial_C51.py │ ├── tutorial_DDPG.py │ ├── tutorial_DPPO.py │ ├── tutorial_DQN.py │ ├── tutorial_DQN_variants.py │ ├── tutorial_PG.py │ ├── tutorial_PPO.py │ ├── tutorial_Qlearning.py │ ├── tutorial_SAC.py │ ├── tutorial_TD3.py │ ├── tutorial_TRPO.py │ ├── tutorial_atari_pong.py │ ├── tutorial_format.py │ ├── tutorial_prioritized_replay.py │ └── tutorial_wrappers.py ├── spatial_transformer_network │ ├── README.md │ ├── tutorial_spatial_transformer_network_dynamic.py │ └── tutorial_spatial_transformer_network_static.py ├── text_classification │ ├── readme.md │ └── tutorial_imdb_fasttext.py ├── text_generation │ ├── README.md │ ├── data │ │ ├── .DS_Store │ │ ├── __init__.py │ │ ├── trump │ │ │ ├── trump_text.txt │ │ │ └── trump_twitter.txt │ │ └── word_counts.txt │ └── tutorial_generate_text.py ├── text_ptb │ ├── README.md │ ├── tutorial_ptb_lstm.py │ └── tutorial_ptb_lstm_state_is_tuple.py ├── text_word_embedding │ ├── tutorial_word2vec_basic.py │ └── word2vec_basic.pdf └── tutorial_work_with_onnx.py ├── img ├── TL_gardener.png ├── TL_gardener.psd ├── awesome-mentioned.png ├── github_mascot.png ├── img_tensorflow.png ├── img_tensorlayer.png ├── img_tlayer1.png ├── join_slack.png ├── join_slack.psd ├── medium │ ├── Readme.md │ ├── medium_header.png │ └── medium_header.psd ├── tl_black_logo.png ├── tl_transparent_logo.png └── tl_white_logo.png ├── requirements ├── requirements.txt ├── requirements_contrib_loggers.txt ├── requirements_db.txt ├── requirements_dev.txt ├── requirements_doc.txt ├── requirements_extra.txt ├── requirements_test.txt ├── requirements_tf_cpu.txt └── requirements_tf_gpu.txt ├── scripts ├── download_and_install_openmpi3_ubuntu.sh ├── install-horovod-for-doc-test.sh └── install-requirements-for-rtd.sh ├── setup.cfg ├── setup.py ├── setup.travis.cfg ├── setup.travis_doc.cfg ├── tensorlayer ├── __init__.py ├── activation.py ├── app │ ├── __init__.py │ ├── computer_vision.py │ ├── computer_vision_object_detection │ │ ├── __init__.py │ │ ├── common.py │ │ └── yolov4.py │ └── human_pose_estimation │ │ ├── LCN.py │ │ ├── __init__.py │ │ └── common.py ├── array_ops.py ├── cli │ ├── __init__.py │ ├── __main__.py │ └── train.py ├── cost.py ├── db.py ├── decorators │ ├── __init__.py │ ├── deprecated.py │ ├── deprecated_alias.py │ ├── method_decorator.py │ └── utils.py ├── distributed.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 ├── initializers.py ├── iterate.py ├── layers │ ├── __init__.py │ ├── activation.py │ ├── convolution │ │ ├── __init__.py │ │ ├── binary_conv.py │ │ ├── deformable_conv.py │ │ ├── depthwise_conv.py │ │ ├── dorefa_conv.py │ │ ├── expert_conv.py │ │ ├── expert_deconv.py │ │ ├── group_conv.py │ │ ├── quan_conv.py │ │ ├── quan_conv_bn.py │ │ ├── separable_conv.py │ │ ├── simplified_conv.py │ │ ├── simplified_deconv.py │ │ ├── super_resolution.py │ │ └── ternary_conv.py │ ├── core.py │ ├── dense │ │ ├── __init__.py │ │ ├── base_dense.py │ │ ├── binary_dense.py │ │ ├── dorefa_dense.py │ │ ├── dropconnect.py │ │ ├── quan_dense.py │ │ ├── quan_dense_bn.py │ │ └── ternary_dense.py │ ├── deprecated.py │ ├── dropout.py │ ├── embedding.py │ ├── extend.py │ ├── image_resampling.py │ ├── inputs.py │ ├── lambda_layers.py │ ├── merge.py │ ├── noise.py │ ├── normalization.py │ ├── padding.py │ ├── pooling.py │ ├── quantize.py │ ├── recurrent.py │ ├── scale.py │ ├── shape.py │ ├── spatial_transformer.py │ ├── stack.py │ └── utils.py ├── lazy_imports.py ├── logging │ ├── __init__.py │ ├── contrib │ │ ├── __init__.py │ │ └── hyperdash.py │ └── tl_logging.py ├── models │ ├── __init__.py │ ├── core.py │ ├── imagenet_class_index.json │ ├── imagenet_classes.py │ ├── mobilenetv1.py │ ├── resnet.py │ ├── seq2seq.py │ ├── seq2seq_with_attention.py │ ├── squeezenetv1.py │ └── vgg.py ├── nlp.py ├── optimizers │ ├── __init__.py │ └── amsgrad.py ├── package_info.py ├── prepro.py ├── rein.py ├── utils.py └── visualize.py ├── tests ├── __init__.py ├── files │ ├── model_basic.h5 │ └── test_utils_saveload.py ├── layers │ ├── __init__.py │ ├── test_layernode.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_dense.py │ ├── test_layers_embedding.py │ ├── test_layers_extend.py │ ├── test_layers_lambda.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 ├── models │ ├── test_auto_naming.py │ ├── test_keras_save.py │ ├── test_model_core.py │ ├── test_model_save.py │ ├── test_model_save_graph.py │ ├── test_seq2seq_model.py │ └── test_seq2seq_with_attention.py ├── pending │ ├── __init__.py │ ├── test_array_ops.py │ ├── test_decorators.py │ ├── test_documentation.py │ ├── test_layers_basic.py │ ├── test_layers_flow_control.py │ ├── test_layers_importer.py │ ├── test_layers_normalization.py │ ├── test_layers_padding.py │ ├── test_layers_spatial_transformer.py │ ├── test_layers_stack.py │ ├── test_layers_super_resolution.py │ ├── test_layers_time_distributed.py │ ├── test_logging.py │ ├── test_logging_hyperdash.py │ ├── test_mnist_simple.py │ ├── test_models.py │ ├── test_optimizer_amsgrad.py │ ├── test_pydocstyle.py │ ├── test_reuse_mlp.py │ ├── test_tf_layers.py │ ├── test_timeout.py │ ├── test_utils_predict.py │ └── test_yapf_format.py ├── performance_test │ ├── __init__.py │ └── vgg │ │ ├── README.md │ │ ├── __init__.py │ │ ├── exp_config.py │ │ ├── keras_test.py │ │ ├── pytorch_test.py │ │ ├── tf2-autograph.py │ │ ├── tf2-eager.py │ │ ├── tl2-autograph.py │ │ ├── tl2-eager.py │ │ ├── tl2-static-autograph.py │ │ └── tl2-static-eager.py ├── test_activations.py ├── test_initializers.py ├── test_nlp.py └── utils │ ├── __init__.py │ ├── custom_layers │ ├── __init__.py │ ├── basic_layers.py │ └── inception_blocks.py │ ├── custom_networks │ ├── __init__.py │ └── inceptionv4.py │ ├── custom_testcase.py │ ├── list_py_files.py │ └── timeout_utils.py └── tl /.codacy.yaml: -------------------------------------------------------------------------------- 1 | # https://support.codacy.com/hc/en-us/articles/115002130625-Codacy-Configuration-File 2 | --- 3 | engines: 4 | bandit: 5 | enabled: false 6 | exclude_paths: 7 | - scripts/* 8 | - setup.py 9 | - docker/**/* 10 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # dockerignore 2 | .git 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 tensorlayer as tl 24 | 25 | x = tf.placeholder(tf.float32, [None, 64]) 26 | net_in = tl.layers.InputLayer(x) 27 | 28 | net = tl.layers.DenseLayer(net_in, n_units=25, act=tf.nn.relu, name='relu1') 29 | 30 | print("Output Shape:", net.outputs.get_shape().as_list()) ### Output Shape: [None, 25] 31 | 32 | # ======================================================== # 33 | ###### THIS CODE IS AN EXAMPLE, REPLACE WITH YOUR OWN ###### 34 | # ======================================================== # 35 | ``` 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /.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 | - /^tensorlayer\// 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 | - requirements_doc.txt 22 | - requirements_test.txt 23 | 24 | # Configuration Files 25 | - .travis.yml 26 | - .readthedocs.yml 27 | - .pyup.yml 28 | 29 | # Docker Files 30 | - Dockerfile 31 | - Dockerfile.gpu 32 | - .dockerignore 33 | 34 | # Text Files 35 | - README.md 36 | - LICENSE.rst 37 | - CHANGELOG.md 38 | - CONTRIBUTING.md 39 | 40 | # Github Files 41 | - .gitignore 42 | -------------------------------------------------------------------------------- /.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 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | *~ 28 | 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | docs/test_build/ 69 | docs/build_test/ 70 | 71 | # PyBuilder 72 | target/ 73 | 74 | # Jupyter Notebook 75 | .ipynb_checkpoints 76 | 77 | # pyenv 78 | .python-version 79 | 80 | # celery beat schedule file 81 | celerybeat-schedule 82 | 83 | # SageMath parsed files 84 | *.sage.py 85 | 86 | # Environments 87 | .env 88 | .venv 89 | env/ 90 | venv/ 91 | ENV/ 92 | env.bak/ 93 | venv.bak/ 94 | venv_/ 95 | venv2/ 96 | venv3/ 97 | venv_doc/ 98 | venv_py2/ 99 | 100 | # Spyder project settings 101 | .spyderproject 102 | .spyproject 103 | 104 | # Rope project settings 105 | .ropeproject 106 | 107 | # mkdocs documentation 108 | /site 109 | 110 | # mypy 111 | .mypy_cache/ 112 | 113 | 114 | # IDE Specific directories 115 | .DS_Store 116 | .idea 117 | .vscode/ 118 | 119 | # TensorLayer Directories 120 | checkpoints 121 | data/ 122 | lib_win/ 123 | 124 | # Custom Scripts 125 | update_tl.bat 126 | update_tl.py 127 | 128 | # Data Files and ByteCode files 129 | *.gz 130 | *.npz 131 | -------------------------------------------------------------------------------- /.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 contrib loggers 46 | - requirements_contrib_loggers.txt 47 | 48 | # Requirements for the db 49 | - requirements/requirements_db.txt 50 | 51 | # Requirements for the development 52 | - requirements/requirements_dev.txt 53 | 54 | # Requirements for building docs 55 | - requirements/requirements_doc.txt 56 | 57 | # Requirements for running unittests 58 | - requirements/requirements_test.txt 59 | 60 | # configure the branch prefix the bot is using 61 | # default: pyup- 62 | branch_prefix: pyup- 63 | 64 | # set a global prefix for PRs 65 | # default: empty 66 | pr_prefix: "PyUP - Dependency Update" 67 | 68 | # allow to close stale PRs 69 | # default: True 70 | close_prs: True 71 | -------------------------------------------------------------------------------- /.readthedocs.yml: -------------------------------------------------------------------------------- 1 | # https://docs.readthedocs.io/en/latest/yaml-config.html 2 | 3 | build: 4 | image: latest 5 | 6 | formats: 7 | - epub 8 | - pdf 9 | 10 | python: 11 | version: 3.6 12 | 13 | requirements_file: 14 | requirements/requirements_doc.txt 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | default: 2 | @echo "Usage:" 3 | @echo "\tmake lint # run pylint" 4 | @echo "\tmake format # run yapf, autoflake and isort" 5 | @echo "\tmake install3 # install tensorlayer in current workspace with pip3" 6 | 7 | lint: 8 | pylint examples/*.py 9 | pylint tensorlayer 10 | 11 | test: 12 | python3 tests/models/test_model_core.py 13 | python3 tests/layers/test_layernode.py 14 | python3 tests/files/test_utils_saveload.py 15 | 16 | format: 17 | autoflake -ir examples 18 | autoflake -ir tensorlayer 19 | autoflake -ir tests 20 | 21 | isort -rc examples 22 | isort -rc tensorlayer 23 | isort -rc tests 24 | 25 | yapf -ir examples 26 | yapf -ir tensorlayer 27 | yapf -ir tests 28 | 29 | install3: 30 | pip3 install -U . --user 31 | 32 | 33 | TAG = tensorlayer-docs:snaphot 34 | 35 | doc: 36 | docker build --rm -t $(TAG) -f docker/docs/Dockerfile . 37 | -------------------------------------------------------------------------------- /docker/DockerLint.bat: -------------------------------------------------------------------------------- 1 | docker pull hadolint/hadolint:latest 2 | docker run --rm -i hadolint/hadolint 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/tensorlayer.git \ 28 | && cd tensorlayer \ 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 | logger = logging.getLogger("PyPI_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: %s" % args.package) 30 | logger.debug("nth_last_version: %s" % args.nth_last_version) 31 | logger.debug("prerelease: %s" % args.prerelease) 32 | logger.debug("debug: %s" % args.debug) 33 | 34 | finder = pip._internal.index.PackageFinder([], ['https://pypi.python.org/simple'], session=requests.Session()) 35 | results = finder.find_all_candidates(args.package) 36 | tmp_versions = [str(p.version) for p in results] 37 | 38 | logger.debug("%s" % tmp_versions) 39 | 40 | versions = list() 41 | for el in tmp_versions: 42 | if el not in versions: 43 | versions.append(el) 44 | 45 | pos = -1 46 | nth_version = 1 47 | 48 | while True: 49 | fetched_version = versions[pos] 50 | 51 | logger.debug("Version: %s" % fetched_version) 52 | 53 | if nth_version == args.nth_last_version: 54 | if args.prerelease or not ("rc" in fetched_version or "a" in fetched_version or "b" in fetched_version): 55 | break 56 | else: 57 | pos -= 1 58 | continue 59 | 60 | pos -= 1 61 | nth_version += 1 62 | 63 | print(fetched_version) 64 | -------------------------------------------------------------------------------- /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 | logger = logging.getLogger("VERSION_PREFIX_CLI") 17 | 18 | formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') 19 | 20 | ch = logging.StreamHandler() 21 | ch.setLevel(logging.DEBUG) 22 | ch.setFormatter(formatter) 23 | logger.addHandler(ch) 24 | 25 | if args.debug: 26 | logger.setLevel(logging.DEBUG) 27 | 28 | logger.debug("Package Version: %s" % args.version) 29 | 30 | if "rc" in args.version or "a" in args.version or "b" in args.version: 31 | print("latest-dev") 32 | else: 33 | print("latest") 34 | -------------------------------------------------------------------------------- /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/3d_human_pose_result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/3d_human_pose_result.jpg -------------------------------------------------------------------------------- /docs/images/affine_transform_comparison.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/affine_transform_comparison.jpg -------------------------------------------------------------------------------- /docs/images/affine_transform_why.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/affine_transform_why.jpg -------------------------------------------------------------------------------- /docs/images/human_pose_points.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/human_pose_points.jpg -------------------------------------------------------------------------------- /docs/images/yolov4_image_result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/yolov4_image_result.png -------------------------------------------------------------------------------- /docs/images/yolov4_video_result.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/images/yolov4_video_result.gif -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | Welcome to TensorLayer 2 | ======================================= 3 | 4 | 5 | .. image:: user/my_figs/tl_transparent_logo.png 6 | :width: 30 % 7 | :align: center 8 | :target: https://github.com/tensorlayer/tensorlayer 9 | 10 | **Documentation Version:** |release| 11 | 12 | **Jun 2020** `Deep Reinforcement Learning Book Is Released `__. 13 | 14 | **Good News:** We won the **Best Open Source Software Award** `@ACM Multimedia (MM) 2017 `_. 15 | 16 | `TensorLayer`_ is a Deep Learning (DL) and Reinforcement Learning (RL) library extended from `Google TensorFlow `_. It provides popular DL and RL modules that can be easily customized and assembled for tackling real-world machine learning problems. 17 | More details can be found `here `_. 18 | 19 | .. note:: 20 | If you got problem to read the docs online, you could download the repository 21 | on `GitHub`_, 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 TensorLayer user guide explains how to install TensorFlow, CUDA and cuDNN, 28 | how to build and train neural networks using TensorLayer, 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/get_involved 38 | user/faq 39 | 40 | .. toctree:: 41 | :maxdepth: 2 42 | :caption: Getting started 43 | 44 | user/get_start_model 45 | user/get_start_advance 46 | 47 | API Reference 48 | ------------- 49 | 50 | If you are looking for information on a specific function, class or 51 | method, this part of the documentation is for you. 52 | 53 | .. toctree:: 54 | :maxdepth: 2 55 | :caption: Stable Functionalities 56 | 57 | modules/activation 58 | modules/array_ops 59 | modules/cost 60 | modules/prepro 61 | modules/files 62 | modules/iterate 63 | modules/layers 64 | modules/models 65 | modules/nlp 66 | modules/initializers 67 | modules/rein 68 | modules/utils 69 | modules/visualize 70 | 71 | .. toctree:: 72 | :maxdepth: 2 73 | :caption: Alpha Version Functionalities 74 | 75 | modules/db 76 | modules/optimizers 77 | modules/distributed 78 | 79 | Command-line Reference 80 | ---------------------- 81 | 82 | TensorLayer provides a handy command-line tool `tl` to perform some common tasks. 83 | 84 | .. toctree:: 85 | :maxdepth: 2 86 | :caption: Command Line Interface 87 | 88 | modules/cli 89 | 90 | 91 | Indices and tables 92 | ================== 93 | 94 | * :ref:`genindex` 95 | * :ref:`modindex` 96 | * :ref:`search` 97 | 98 | 99 | .. _GitHub: https://github.com/tensorlayer/tensorlayer 100 | .. _TensorLayer: https://github.com/tensorlayer/tensorlayer/ 101 | -------------------------------------------------------------------------------- /docs/modules/activation.rst: -------------------------------------------------------------------------------- 1 | API - Activations 2 | ========================= 3 | 4 | To make TensorLayer simple, we minimize the number of activation functions as much as 5 | we can. So we encourage you to use TensorFlow's function. TensorFlow provides 6 | ``tf.nn.relu``, ``tf.nn.relu6``, ``tf.nn.elu``, ``tf.nn.softplus``, 7 | ``tf.nn.softsign`` and so on. 8 | For parametric activation, please read the layer APIs. 9 | 10 | The shortcut of ``tensorlayer.activation`` is ``tensorlayer.act``. 11 | 12 | Your activation 13 | ------------------- 14 | 15 | Customizes activation function in TensorLayer is very easy. 16 | The following example implements an activation that multiplies its input by 2. 17 | For more complex activation, TensorFlow API will be required. 18 | 19 | .. code-block:: python 20 | 21 | def double_activation(x): 22 | return x * 2 23 | 24 | double_activation = lambda x: x * 2 25 | 26 | .. automodule:: tensorlayer.activation 27 | 28 | .. autosummary:: 29 | 30 | leaky_relu 31 | leaky_relu6 32 | leaky_twice_relu6 33 | ramp 34 | swish 35 | sign 36 | hard_tanh 37 | pixel_wise_softmax 38 | mish 39 | 40 | Ramp 41 | ------ 42 | .. autofunction:: ramp 43 | 44 | Leaky ReLU 45 | ------------ 46 | .. autofunction:: leaky_relu 47 | 48 | Leaky ReLU6 49 | ------------ 50 | .. autofunction:: leaky_relu6 51 | 52 | Twice Leaky ReLU6 53 | ----------------- 54 | .. autofunction:: leaky_twice_relu6 55 | 56 | Swish 57 | ------------ 58 | .. autofunction:: swish 59 | 60 | Sign 61 | --------------------- 62 | .. autofunction:: sign 63 | 64 | Hard Tanh 65 | --------------------- 66 | .. autofunction:: hard_tanh 67 | 68 | Pixel-wise softmax 69 | -------------------- 70 | .. autofunction:: pixel_wise_softmax 71 | 72 | mish 73 | --------- 74 | .. autofunction:: mish 75 | 76 | Parametric activation 77 | ------------------------------ 78 | See ``tensorlayer.layers``. 79 | -------------------------------------------------------------------------------- /docs/modules/array_ops.rst: -------------------------------------------------------------------------------- 1 | API - Array Operations 2 | ====================== 3 | 4 | .. automodule:: tensorlayer.array_ops 5 | 6 | .. autosummary:: 7 | 8 | alphas 9 | alphas_like 10 | 11 | Tensorflow Tensor Operations 12 | ---------------------------- 13 | 14 | tl.alphas 15 | ^^^^^^^^^ 16 | .. autofunction:: alphas 17 | 18 | tl.alphas_like 19 | ^^^^^^^^^^^^^^ 20 | .. autofunction:: alphas_like 21 | -------------------------------------------------------------------------------- /docs/modules/cli.rst: -------------------------------------------------------------------------------- 1 | CLI - Command Line Interface 2 | ============================== 3 | 4 | .. automodule:: tensorlayer.cli 5 | 6 | .. automodule:: tensorlayer.cli.train 7 | -------------------------------------------------------------------------------- /docs/modules/cost.rst: -------------------------------------------------------------------------------- 1 | API - Cost 2 | ================== 3 | 4 | To make TensorLayer simple, we minimize the number of cost functions as much as 5 | we can. So we encourage you to use TensorFlow's function, , see `TensorFlow API `_. 6 | 7 | .. note:: 8 | Please refer to `Getting Started `_ for getting specific weights for weight regularization. 9 | 10 | .. automodule:: tensorlayer.cost 11 | 12 | .. autosummary:: 13 | 14 | cross_entropy 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 | huber_loss 32 | 33 | 34 | Softmax cross entropy 35 | ---------------------- 36 | .. autofunction:: cross_entropy 37 | 38 | Sigmoid cross entropy 39 | ---------------------- 40 | .. autofunction:: sigmoid_cross_entropy 41 | 42 | Binary cross entropy 43 | ------------------------- 44 | .. autofunction:: binary_cross_entropy 45 | 46 | Mean squared error (L2) 47 | ------------------------- 48 | .. autofunction:: mean_squared_error 49 | 50 | Normalized mean square error 51 | -------------------------------- 52 | .. autofunction:: normalized_mean_square_error 53 | 54 | Absolute difference error (L1) 55 | -------------------------------- 56 | .. autofunction:: absolute_difference_error 57 | 58 | Dice coefficient 59 | ------------------------- 60 | .. autofunction:: dice_coe 61 | 62 | Hard Dice coefficient 63 | ------------------------- 64 | .. autofunction:: dice_hard_coe 65 | 66 | IOU coefficient 67 | ------------------------- 68 | .. autofunction:: iou_coe 69 | 70 | Cross entropy for sequence 71 | ----------------------------- 72 | .. autofunction:: cross_entropy_seq 73 | 74 | Cross entropy with mask for sequence 75 | ---------------------------------------- 76 | .. autofunction:: cross_entropy_seq_with_mask 77 | 78 | Cosine similarity 79 | ------------------- 80 | .. autofunction:: cosine_similarity 81 | 82 | Regularization functions 83 | -------------------------- 84 | 85 | For ``tf.nn.l2_loss``, ``tf.contrib.layers.l1_regularizer``, ``tf.contrib.layers.l2_regularizer`` and 86 | ``tf.contrib.layers.sum_regularizer``, see tensorflow API. 87 | Maxnorm 88 | ^^^^^^^^^^ 89 | .. autofunction:: maxnorm_regularizer 90 | 91 | Special 92 | ^^^^^^^^^^ 93 | .. autofunction:: li_regularizer 94 | .. autofunction:: lo_regularizer 95 | .. autofunction:: maxnorm_o_regularizer 96 | .. autofunction:: maxnorm_i_regularizer 97 | 98 | Huber Loss 99 | ^^^^^^^^^^ 100 | .. autofunction:: huber_loss -------------------------------------------------------------------------------- /docs/modules/distributed.rst: -------------------------------------------------------------------------------- 1 | API - Distributed Training 2 | ============================= 3 | 4 | (Alpha release - usage might change later) 5 | 6 | Helper API to run a distributed training. 7 | Check these `examples `_. 8 | 9 | .. automodule:: tensorlayer.distributed 10 | 11 | .. autosummary:: 12 | 13 | Trainer 14 | 15 | Distributed training 16 | -------------------- 17 | 18 | Trainer 19 | ^^^^^^^^^^^ 20 | 21 | .. autofunction:: Trainer 22 | 23 | 24 | -------------------------------------------------------------------------------- /docs/modules/initializers.rst: -------------------------------------------------------------------------------- 1 | API - Initializers 2 | ========================= 3 | 4 | To make TensorLayer simple, TensorLayer only warps some basic initializers. For more advanced initializer, 5 | e.g. ``tf.initializers.he_normal``, please refer to TensorFlow provided initializers 6 | `here `_. 7 | 8 | .. automodule:: tensorlayer.initializers 9 | 10 | .. autosummary:: 11 | 12 | Initializer 13 | Zeros 14 | Ones 15 | Constant 16 | RandomUniform 17 | RandomNormal 18 | TruncatedNormal 19 | deconv2d_bilinear_upsampling_initializer 20 | 21 | Initializer 22 | ------------ 23 | .. autoclass:: Initializer 24 | 25 | Zeros 26 | ------------ 27 | .. autoclass:: Zeros 28 | 29 | Ones 30 | ------------ 31 | .. autoclass:: Ones 32 | 33 | Constant 34 | ----------------- 35 | .. autoclass:: Constant 36 | 37 | RandomUniform 38 | -------------- 39 | .. autoclass:: RandomUniform 40 | 41 | RandomNormal 42 | --------------------- 43 | .. autoclass:: RandomNormal 44 | 45 | TruncatedNormal 46 | --------------------- 47 | .. autoclass:: TruncatedNormal 48 | 49 | deconv2d_bilinear_upsampling_initializer 50 | ------------------------------------------ 51 | .. autofunction:: deconv2d_bilinear_upsampling_initializer 52 | -------------------------------------------------------------------------------- /docs/modules/iterate.rst: -------------------------------------------------------------------------------- 1 | API - Iteration 2 | ========================== 3 | 4 | Data iteration. 5 | 6 | 7 | .. automodule:: tensorlayer.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/models.rst: -------------------------------------------------------------------------------- 1 | API - Models 2 | ================================ 3 | 4 | TensorLayer provides many pretrained models, you can easily use the whole or a part of the pretrained models via these APIs. 5 | 6 | .. automodule:: tensorlayer.models 7 | 8 | .. autosummary:: 9 | 10 | Model 11 | 12 | VGG16 13 | VGG19 14 | SqueezeNetV1 15 | MobileNetV1 16 | ResNet50 17 | Seq2seq 18 | Seq2seqLuongAttention 19 | 20 | 21 | Base Model 22 | ----------- 23 | 24 | .. autoclass:: Model 25 | 26 | VGG16 27 | ---------------------- 28 | 29 | .. autofunction:: VGG16 30 | 31 | VGG19 32 | ---------------------- 33 | 34 | .. autofunction:: VGG19 35 | 36 | SqueezeNetV1 37 | ---------------- 38 | .. autofunction:: SqueezeNetV1 39 | 40 | MobileNetV1 41 | ---------------- 42 | 43 | .. autofunction:: MobileNetV1 44 | 45 | ResNet50 46 | ---------------- 47 | 48 | .. autofunction:: ResNet50 49 | 50 | Seq2seq 51 | ------------------------ 52 | 53 | .. autoclass:: Seq2seq 54 | 55 | 56 | Seq2seq Luong Attention 57 | ------------------------ 58 | 59 | .. autoclass:: Seq2seqLuongAttention 60 | -------------------------------------------------------------------------------- /docs/modules/optimizers.rst: -------------------------------------------------------------------------------- 1 | API - Optimizers 2 | ================ 3 | 4 | .. automodule:: tensorlayer.optimizers 5 | 6 | TensorLayer 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. 8 | 9 | Optimizers List 10 | --------------- 11 | 12 | .. autosummary:: 13 | 14 | AMSGrad 15 | 16 | AMSGrad Optimizer 17 | ----------------- 18 | .. autoclass:: AMSGrad 19 | :members: -------------------------------------------------------------------------------- /docs/modules/rein.rst: -------------------------------------------------------------------------------- 1 | API - Reinforcement Learning 2 | ============================== 3 | 4 | We provide two reinforcement learning libraries: 5 | 6 | - `RL-tutorial `__ for professional users with low-level APIs. 7 | - `RLzoo `__ for simple usage with high-level APIs. 8 | 9 | .. automodule:: tensorlayer.rein 10 | 11 | .. autosummary:: 12 | 13 | discount_episode_rewards 14 | cross_entropy_reward_loss 15 | log_weight 16 | choice_action_by_probs 17 | 18 | 19 | Reward functions 20 | --------------------- 21 | .. autofunction:: discount_episode_rewards 22 | 23 | Cost functions 24 | --------------------- 25 | 26 | Weighted Cross Entropy 27 | ^^^^^^^^^^^^^^^^^^^^^^^^ 28 | .. autofunction:: cross_entropy_reward_loss 29 | 30 | Log weight 31 | ^^^^^^^^^^^^^^ 32 | .. autofunction:: log_weight 33 | 34 | Sampling functions 35 | --------------------- 36 | .. autofunction:: choice_action_by_probs 37 | -------------------------------------------------------------------------------- /docs/modules/utils.rst: -------------------------------------------------------------------------------- 1 | API - Utility 2 | ======================== 3 | 4 | .. automodule:: tensorlayer.utils 5 | 6 | .. autosummary:: 7 | 8 | fit 9 | test 10 | predict 11 | evaluation 12 | class_balancing_oversample 13 | get_random_int 14 | dict_to_one 15 | list_string_to_dict 16 | flatten_list 17 | exit_tensorflow 18 | open_tensorboard 19 | set_gpu_fraction 20 | 21 | Training, testing and predicting 22 | ---------------------------------- 23 | 24 | Training 25 | ^^^^^^^^^^^ 26 | .. autofunction:: fit 27 | 28 | Evaluation 29 | ^^^^^^^^^^^^^ 30 | .. autofunction:: test 31 | 32 | Prediction 33 | ^^^^^^^^^^^^ 34 | .. autofunction:: predict 35 | 36 | Evaluation functions 37 | --------------------- 38 | .. autofunction:: evaluation 39 | 40 | Class balancing functions 41 | ---------------------------- 42 | .. autofunction:: class_balancing_oversample 43 | 44 | Random functions 45 | ---------------------------- 46 | .. autofunction:: get_random_int 47 | 48 | Dictionary and list 49 | -------------------- 50 | 51 | Set all items in dictionary to one 52 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 53 | .. autofunction:: dict_to_one 54 | 55 | Convert list of string to dictionary 56 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 57 | .. autofunction:: list_string_to_dict 58 | 59 | Flatten a list 60 | ^^^^^^^^^^^^^^^^^^^ 61 | .. autofunction:: flatten_list 62 | 63 | Close TF session and associated processes 64 | ----------------------------------------- 65 | .. autofunction:: exit_tensorflow 66 | 67 | Open TensorBoard 68 | ---------------- 69 | .. autofunction:: open_tensorboard 70 | 71 | Set GPU functions 72 | ----------------- 73 | .. autofunction:: set_gpu_fraction 74 | -------------------------------------------------------------------------------- /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:: tensorlayer.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/user/my_figs/basic_seq2seq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/basic_seq2seq.png -------------------------------------------------------------------------------- /docs/user/my_figs/img_tensorlayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/img_tensorlayer.png -------------------------------------------------------------------------------- /docs/user/my_figs/img_tlayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/img_tlayer.png -------------------------------------------------------------------------------- /docs/user/my_figs/img_tlayer_big.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/img_tlayer_big.png -------------------------------------------------------------------------------- /docs/user/my_figs/img_tunelayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/img_tunelayer.png -------------------------------------------------------------------------------- /docs/user/my_figs/karpathy_rnn.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/karpathy_rnn.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/mnist.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/mnist.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/pong_game.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/pong_game.jpeg -------------------------------------------------------------------------------- /docs/user/my_figs/seq2seq.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/seq2seq.png -------------------------------------------------------------------------------- /docs/user/my_figs/tl_black_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/tl_black_logo.png -------------------------------------------------------------------------------- /docs/user/my_figs/tl_transparent_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/tl_transparent_logo.png -------------------------------------------------------------------------------- /docs/user/my_figs/tl_white_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/tl_white_logo.png -------------------------------------------------------------------------------- /docs/user/my_figs/tsne.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/tsne.png -------------------------------------------------------------------------------- /docs/user/my_figs/word2vec_basic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/docs/user/my_figs/word2vec_basic.pdf -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 |
5 | 6 |
7 |
8 | 9 |
10 | 11 | This page contains basic tutorials and examples that help you to learn TensorLayer quick, but for real-world applications, such as Chatbot, Super-Resolution, Pose Estimation, please check [Awesome-TensorLayer](https://github.com/tensorlayer/awesome-tensorlayer) and [Home-TensorLayer](https://github.com/tensorlayer) 12 | 13 | - [Basic tutorials](https://tensorlayer.readthedocs.io/en/latest/user/get_start_model.html) 14 | - [Basic examples](https://github.com/tensorlayer/tensorlayer/tree/master/examples/basic_tutorials) 15 | - [Using pre-trained CNNs](https://github.com/tensorlayer/tensorlayer/tree/master/examples/pretrained_cnn) 16 | - [Quantized networks](https://github.com/tensorlayer/tensorlayer/tree/master/examples/quantized_net) 17 | - [Reinforcement learning](https://github.com/tensorlayer/tensorlayer/tree/master/examples/reinforcement_learning) 18 | - [Spatial transformer](https://github.com/tensorlayer/tensorlayer/tree/master/examples/spatial_transformer_network) 19 | - [Text/sentence classification](https://github.com/tensorlayer/tensorlayer/tree/master/examples/text_classification) 20 | - [Text/sentence generation](https://github.com/tensorlayer/tensorlayer/tree/master/examples/text_generation) 21 | - [Language modeling](https://github.com/tensorlayer/tensorlayer/tree/master/examples/text_ptb) 22 | - [Word embedding](https://github.com/tensorlayer/tensorlayer/tree/master/examples/text_word_embedding) 23 | - [Many more ...](https://github.com/tensorlayer) 24 | -------------------------------------------------------------------------------- /examples/app_tutorials/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/app_tutorials/model/pose_weights_config.txt: -------------------------------------------------------------------------------- 1 | linear_model/w1 2 | linear_model/b1 3 | linear_model/batch_normalization/beta 4 | linear_model/batch_normalization/gamma 5 | linear_model/batch_normalization/moving_mean 6 | linear_model/batch_normalization/moving_variance 7 | linear_model/two_linear_0/w2_0 8 | linear_model/two_linear_0/b2_0 9 | linear_model/two_linear_0/batch_normalization10/beta 10 | linear_model/two_linear_0/batch_normalization10/gamma 11 | linear_model/two_linear_0/batch_normalization10/moving_mean 12 | linear_model/two_linear_0/batch_normalization10/moving_variance 13 | linear_model/two_linear_0/w3_0 14 | linear_model/two_linear_0/b3_0 15 | linear_model/two_linear_0/batch_normalization20/beta 16 | linear_model/two_linear_0/batch_normalization20/gamma 17 | linear_model/two_linear_0/batch_normalization20/moving_mean 18 | linear_model/two_linear_0/batch_normalization20/moving_variance 19 | linear_model/two_linear_1/w2_1 20 | linear_model/two_linear_1/b2_1 21 | linear_model/two_linear_1/batch_normalization11/beta 22 | linear_model/two_linear_1/batch_normalization11/gamma 23 | linear_model/two_linear_1/batch_normalization11/moving_mean 24 | linear_model/two_linear_1/batch_normalization11/moving_variance 25 | linear_model/two_linear_1/w3_1 26 | linear_model/two_linear_1/b3_1 27 | linear_model/two_linear_1/batch_normalization21/beta 28 | linear_model/two_linear_1/batch_normalization21/gamma 29 | linear_model/two_linear_1/batch_normalization21/moving_mean 30 | linear_model/two_linear_1/batch_normalization21/moving_variance 31 | linear_model/two_linear_2/w2_2 32 | linear_model/two_linear_2/b2_2 33 | linear_model/two_linear_2/batch_normalization12/beta 34 | linear_model/two_linear_2/batch_normalization12/gamma 35 | linear_model/two_linear_2/batch_normalization12/moving_mean 36 | linear_model/two_linear_2/batch_normalization12/moving_variance 37 | linear_model/two_linear_2/w3_2 38 | linear_model/two_linear_2/b3_2 39 | linear_model/two_linear_2/batch_normalization22/beta 40 | linear_model/two_linear_2/batch_normalization22/gamma 41 | linear_model/two_linear_2/batch_normalization22/moving_mean 42 | linear_model/two_linear_2/batch_normalization22/moving_variance 43 | linear_model/w4 44 | linear_model/b4 -------------------------------------------------------------------------------- /examples/app_tutorials/tutorial_human_3dpose_estimation_LCN.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayer.app.human_pose_estimation.common import DataReader, visualize_3D_pose, flip_data 5 | from tensorlayer.app import computer_vision 6 | import numpy as np 7 | 8 | datareader = DataReader() 9 | train_data, test_data = datareader.read_2d(which='scale', mode='gt', read_confidence=False) 10 | train_labels, test_labels = datareader.read_3d(which='scale', mode='gt') 11 | network = computer_vision.human_pose_estimation('3D-pose') 12 | test_data = flip_data(test_data) 13 | result = network(test_data) 14 | result = datareader.denormalize3D(np.asarray(result), which='scale') 15 | test_data = datareader.denormalize2D(test_data, which='scale') 16 | test_labels = datareader.denormalize3D(test_labels, which='scale') 17 | visualize_3D_pose( 18 | test_data, test_labels, result 19 | ) # We plot 4 examples. You can modify this function according to your own needs. 20 | -------------------------------------------------------------------------------- /examples/app_tutorials/tutorial_object_detection_yolov4_image.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayer.app import computer_vision 5 | from tensorlayer import visualize 6 | from tensorlayer.app.computer_vision_object_detection.common import read_class_names 7 | import numpy as np 8 | import cv2 9 | from PIL import Image 10 | INPUT_SIZE = 416 11 | image_path = './data/kite.jpg' 12 | 13 | class_names = read_class_names('./model/coco.names') 14 | original_image = cv2.imread(image_path) 15 | image = cv2.cvtColor(np.array(original_image), cv2.COLOR_BGR2RGB) 16 | net = computer_vision.object_detection('yolo4-mscoco') 17 | json_result = net(original_image) 18 | image = visualize.draw_boxes_and_labels_to_image_with_json(image, json_result, class_names) 19 | image = Image.fromarray(image.astype(np.uint8)) 20 | image.show() 21 | -------------------------------------------------------------------------------- /examples/app_tutorials/tutorial_object_detection_yolov4_video.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayer.app import computer_vision 5 | from tensorlayer import visualize 6 | from tensorlayer.app.computer_vision_object_detection.common import read_class_names 7 | import cv2 8 | INPUT_SIZE = 416 9 | video_path = './data/road.mp4' 10 | 11 | class_names = read_class_names('./model/coco.names') 12 | vid = cv2.VideoCapture(video_path) 13 | ''' 14 | vid = cv2.VideoCapture(0) # the serial number of camera on you device 15 | ''' 16 | 17 | if not vid.isOpened(): 18 | raise ValueError("Read Video Failed!") 19 | net = computer_vision.object_detection('yolo4-mscoco') 20 | frame_id = 0 21 | while True: 22 | return_value, frame = vid.read() 23 | if return_value: 24 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 25 | else: 26 | if frame_id == vid.get(cv2.CAP_PROP_FRAME_COUNT): 27 | print("Video processing complete") 28 | break 29 | raise ValueError("No image! Try with another video format") 30 | 31 | json_result = net(frame) 32 | image = visualize.draw_boxes_and_labels_to_image_with_json(frame, json_result, class_names) 33 | result = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) 34 | 35 | cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) 36 | cv2.imshow("result", result) 37 | if cv2.waitKey(1) & 0xFF == ord('q'): break 38 | frame_id += 1 39 | -------------------------------------------------------------------------------- /examples/basic_tutorials/README.md: -------------------------------------------------------------------------------- 1 | # Before You Start 2 | 3 | TensorLayer has two types of models. 4 | Static model allows you to build model in a fluent way while dynamic model allows you to fully control the forward process. 5 | Please read this [DOCS](https://tensorlayer.readthedocs.io/en/latest/user/get_start_model.html#) before you start. 6 | 7 | - [MNIST Simplest Example](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_simple.py) 8 | - [MNIST Static Example](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_mlp_static.py) 9 | - [MNIST Static Example for Reused Model](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_mlp_static_2.py) 10 | - [MNIST Dynamic Example](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_mlp_dynamic.py) 11 | - [MNIST Dynamic Example for Seperated Models](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_mlp_dynamic_2.py) 12 | - [MNIST Static Siamese Model Example](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_mnist_siamese.py) 13 | - [CIFAR10 Static Example with Data Augmentation](https://github.com/tensorlayer/tensorlayer/blob/master/examples/basic_tutorials/tutorial_cifar10_cnn_static.py) 14 | -------------------------------------------------------------------------------- /examples/basic_tutorials/tutorial_mnist_simple.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import numpy as np 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | 9 | tl.logging.set_verbosity(tl.logging.DEBUG) 10 | 11 | # set gpu mem fraction or allow growth 12 | # tl.utils.set_gpu_fraction() 13 | 14 | # prepare data 15 | X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 784)) 16 | 17 | # define the network 18 | ni = tl.layers.Input([None, 784]) 19 | nn = tl.layers.Dropout(keep=0.8)(ni) 20 | nn = tl.layers.Dense(n_units=800, act=tf.nn.relu)(nn) 21 | nn = tl.layers.Dropout(keep=0.5)(nn) 22 | nn = tl.layers.Dense(n_units=800, act=tf.nn.relu)(nn) 23 | nn = tl.layers.Dropout(keep=0.5)(nn) 24 | nn = tl.layers.Dense(n_units=10, act=None)(nn) 25 | network = tl.models.Model(inputs=ni, outputs=nn, name="mlp") 26 | 27 | 28 | # define metric. 29 | def acc(_logits, y_batch): 30 | # return np.mean(np.equal(np.argmax(_logits, 1), y_batch)) 31 | return tf.reduce_mean( 32 | tf.cast(tf.equal(tf.argmax(_logits, 1), tf.convert_to_tensor(y_batch, tf.int64)), tf.float32), name='accuracy' 33 | ) 34 | 35 | 36 | # print network information 37 | print(network) 38 | 39 | # open tensorboard 40 | # tl.utils.open_tensorboard('./tb_log', port=6006) 41 | 42 | # train the network 43 | tl.utils.fit( 44 | network, train_op=tf.optimizers.Adam(learning_rate=0.0001), cost=tl.cost.cross_entropy, X_train=X_train, 45 | y_train=y_train, acc=acc, batch_size=256, n_epoch=20, X_val=X_val, y_val=y_val, eval_train=True, 46 | tensorboard_dir='./tb_log' 47 | ) 48 | 49 | # test 50 | tl.utils.test(network, acc, X_test, y_test, batch_size=None, cost=tl.cost.cross_entropy) 51 | 52 | # evaluation 53 | _logits = tl.utils.predict(network, X_test) 54 | y_pred = np.argmax(_logits, 1) 55 | tl.utils.evaluation(y_test, y_pred, n_classes=10) 56 | 57 | # save network weights 58 | network.save_weights('model.h5') 59 | 60 | # close tensorboard 61 | # tl.utils.exit_tensorflow(port=6006) 62 | -------------------------------------------------------------------------------- /examples/data_process/README.md: -------------------------------------------------------------------------------- 1 | The examples show here are not the best, `tl.prepro.threading_data` is for quick testing. 2 | The state-of-the-art method is TensorFlow's `tf.data` and `tf.image`. 3 | We will change all examples later. 4 | 5 | Please use `basic_tutorials/tutorial_cifar10_datasetapi.py`. 6 | 7 | 8 | ### Blogs 9 | 10 | - [如何用TensorLayer做目标检测的数据增强](https://zhuanlan.zhihu.com/p/31466173) -------------------------------------------------------------------------------- /examples/data_process/data/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/.DS_Store -------------------------------------------------------------------------------- /examples/data_process/data/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import * 4 | -------------------------------------------------------------------------------- /examples/data_process/data/cat/img1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img1.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img2.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img3.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img4.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img5.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img6.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img7.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img8.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat/img9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/cat/img9.jpg -------------------------------------------------------------------------------- /examples/data_process/data/cat_caption.json: -------------------------------------------------------------------------------- 1 | { 2 | "info": {"description": "This is a cat dataset.", 3 | "url": "http://tensorlayer.org", 4 | "version": "1.0.0", 5 | "year": 2016, 6 | "contributor": "Hao Dong", 7 | "date_created": "2016-09-30 12:11:50.00000"}, 8 | "images":[ 9 | {"file_name":"img1.jpg", "caption":"a yellow cat looks up"}, 10 | {"file_name":"img2.jpg", "caption":"a grey cat with yellow eyes"}, 11 | {"file_name":"img3.jpg", "caption":"grey cat with yellow eyes"}, 12 | {"file_name":"img4.jpg", "caption":"yellow cat looks at you"}, 13 | {"file_name":"img5.jpg", "caption":"a yellow cat stands up"}, 14 | {"file_name":"img6.jpg", "caption":"a small cat sits down"}, 15 | {"file_name":"img7.jpg", "caption":"it is a white cat with white eyes"}, 16 | {"file_name":"img8.jpg", "caption":"it shows a head of cat"}, 17 | {"file_name":"img9.jpg", "caption":"a black cat is running very fast"} 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /examples/data_process/data/dog/img1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img1.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img2.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img3.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img4.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img5.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img6.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img7.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img8.jpg -------------------------------------------------------------------------------- /examples/data_process/data/dog/img9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/dog/img9.jpg -------------------------------------------------------------------------------- /examples/data_process/data/greenbackground/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/greenbackground/0.jpg -------------------------------------------------------------------------------- /examples/data_process/data/greenbackground/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/greenbackground/1.jpg -------------------------------------------------------------------------------- /examples/data_process/data/tiger.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/data_process/data/tiger.jpeg -------------------------------------------------------------------------------- /examples/database/README.md: -------------------------------------------------------------------------------- 1 | # Dispatch Tasks 2 | 3 | 1. This script (`dispatch_tasks.py`) creates 3 tasks (`task_script.py`) with different hyper-parameters and a dataset and pushes these tasks into the database. 4 | 2. On your GPU servers (for testing, it can be a new terminal on your local machine), run tasks as shown in `run_tasks.py`. 5 | This script pulls and runs pending tasks, and saves the models and results to the database. 6 | 3. When all tasks complete, the dispatcher (`dispatch_tasks.py`) then selects the best model according to its accuracy. 7 | 8 | 9 | # Save and load models 10 | 11 | - `task_script.py` shows how to save model. 12 | - `dispatch_tasks.py ` shows how to find and load the model with the best testing accuracy. 13 | 14 | # Save and load datasets 15 | 16 | - `dispatch_tasks.py ` shows how to save a dataset. 17 | - `task_script.py ` show how to find and load a dataset. 18 | 19 | #### More information in the online documentation. -------------------------------------------------------------------------------- /examples/database/dispatch_tasks.py: -------------------------------------------------------------------------------- 1 | """ 2 | A sample script that shows how to distribute multiple tasks to multiple machine 3 | using the database module. 4 | 5 | """ 6 | import time 7 | 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | 12 | tl.logging.set_verbosity(tl.logging.DEBUG) 13 | # tf.logging.set_verbosity(tf.logging.DEBUG) 14 | 15 | # connect to database 16 | db = tl.db.TensorHub(ip='localhost', port=27017, dbname='temp', project_name='tutorial') 17 | 18 | # delete existing tasks, models and datasets in this project 19 | db.delete_tasks() 20 | db.delete_model() 21 | db.delete_datasets() 22 | 23 | # save dataset into database, then allow other servers to use it 24 | X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 784)) 25 | db.save_dataset((X_train, y_train, X_val, y_val, X_test, y_test), 'mnist', description='handwriting digit') 26 | 27 | # push tasks into database, then allow other servers pull tasks to run 28 | db.create_task( 29 | task_name='mnist', script='task_script.py', hyper_parameters=dict(n_units1=800, n_units2=800), 30 | saved_result_keys=['test_accuracy'], description='800-800' 31 | ) 32 | 33 | db.create_task( 34 | task_name='mnist', script='task_script.py', hyper_parameters=dict(n_units1=600, n_units2=600), 35 | saved_result_keys=['test_accuracy'], description='600-600' 36 | ) 37 | 38 | db.create_task( 39 | task_name='mnist', script='task_script.py', hyper_parameters=dict(n_units1=400, n_units2=400), 40 | saved_result_keys=['test_accuracy'], description='400-400' 41 | ) 42 | 43 | # wait for tasks to finish 44 | while db.check_unfinished_task(task_name='mnist'): 45 | print("waiting runners to finish the tasks") 46 | time.sleep(1) 47 | 48 | # get the best model 49 | print("all tasks finished") 50 | net = db.find_top_model(model_name='mlp', sort=[("test_accuracy", -1)]) 51 | print("the best accuracy {} is from model {}".format(net._test_accuracy, net._name)) 52 | -------------------------------------------------------------------------------- /examples/database/run_tasks.py: -------------------------------------------------------------------------------- 1 | """ 2 | Run this script on servers, it will monitor the database and run tasks when 3 | task distributor push a task to the database. 4 | 5 | """ 6 | import time 7 | 8 | import tensorlayer as tl 9 | 10 | # tl.logging.set_verbosity(tl.logging.DEBUG) 11 | 12 | # connect to database 13 | db = tl.db.TensorHub(ip='localhost', port=27017, dbname='temp', project_name='tutorial') 14 | 15 | # monitors the database and pull tasks to run 16 | while True: 17 | print("waiting task from distributor") 18 | db.run_top_task(task_name='mnist', sort=[("time", -1)]) 19 | time.sleep(1) 20 | -------------------------------------------------------------------------------- /examples/database/task_script.py: -------------------------------------------------------------------------------- 1 | """Sample task script.""" 2 | 3 | import tensorflow as tf 4 | 5 | import tensorlayer as tl 6 | 7 | # tf.logging.set_verbosity(tf.logging.DEBUG) 8 | tl.logging.set_verbosity(tl.logging.DEBUG) 9 | 10 | # connect to database 11 | db = tl.db.TensorHub(ip='localhost', port=27017, dbname='temp', project_name='tutorial') 12 | 13 | # load dataset from database 14 | X_train, y_train, X_val, y_val, X_test, y_test = db.find_top_dataset('mnist') 15 | 16 | 17 | # define the network 18 | def mlp(): 19 | ni = tl.layers.Input([None, 784], name='input') 20 | net = tl.layers.Dropout(keep=0.8, name='drop1')(ni) 21 | net = tl.layers.Dense(n_units=n_units1, act=tf.nn.relu, name='relu1')(net) 22 | net = tl.layers.Dropout(keep=0.5, name='drop2')(net) 23 | net = tl.layers.Dense(n_units=n_units2, act=tf.nn.relu, name='relu2')(net) 24 | net = tl.layers.Dropout(keep=0.5, name='drop3')(net) 25 | net = tl.layers.Dense(n_units=10, act=None, name='output')(net) 26 | M = tl.models.Model(inputs=ni, outputs=net) 27 | return M 28 | 29 | 30 | network = mlp() 31 | 32 | # cost and accuracy 33 | cost = tl.cost.cross_entropy 34 | 35 | 36 | def acc(y, y_): 37 | correct_prediction = tf.equal(tf.argmax(y, 1), tf.convert_to_tensor(y_, tf.int64)) 38 | return tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 39 | 40 | 41 | # define the optimizer 42 | train_op = tf.optimizers.Adam(learning_rate=0.0001) 43 | 44 | # train the network 45 | # tl.utils.fit( 46 | # network, train_op, cost, X_train, y_train, acc=acc, batch_size=500, n_epoch=20, print_freq=5, 47 | # X_val=X_val, y_val=y_val, eval_train=False 48 | # ) 49 | 50 | tl.utils.fit( 51 | network, 52 | train_op=tf.optimizers.Adam(learning_rate=0.0001), 53 | cost=tl.cost.cross_entropy, 54 | X_train=X_train, 55 | y_train=y_train, 56 | acc=acc, 57 | batch_size=256, 58 | n_epoch=20, 59 | X_val=X_val, 60 | y_val=y_val, 61 | eval_train=False, 62 | ) 63 | 64 | # evaluation and save result that match the result_key 65 | test_accuracy = tl.utils.test(network, acc, X_test, y_test, batch_size=None, cost=cost) 66 | test_accuracy = float(test_accuracy) 67 | 68 | # save model into database 69 | db.save_model(network, model_name='mlp', name=str(n_units1) + '-' + str(n_units2), test_accuracy=test_accuracy) 70 | # in other script, you can load the model as follow 71 | # net = db.find_model(sess=sess, model_name=str(n_units1)+'-'+str(n_units2) 72 | -------------------------------------------------------------------------------- /examples/deprecated_tutorials/tutorial_image_preprocess.py: -------------------------------------------------------------------------------- 1 | """Data Augmentation by numpy, scipy, threading and queue. 2 | 3 | Note that, TensorFlow's TFRecord and Dataset API are faster. 4 | 5 | """ 6 | 7 | import time 8 | 9 | import tensorlayer as tl 10 | 11 | tl.logging.set_verbosity(tl.logging.DEBUG) 12 | 13 | X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False) 14 | 15 | 16 | def distort_img(x): 17 | x = tl.prepro.flip_axis(x, axis=1, is_random=True) 18 | x = tl.prepro.crop(x, wrg=28, hrg=28, is_random=True) 19 | return x 20 | 21 | 22 | s = time.time() 23 | results = tl.prepro.threading_data(X_train[0:100], distort_img) 24 | print("took %.3fs" % (time.time() - s)) 25 | print(results.shape) 26 | 27 | tl.vis.save_images(X_train[0:10], [1, 10], '_original.png') 28 | tl.vis.save_images(results[0:10], [1, 10], '_distorted.png') 29 | -------------------------------------------------------------------------------- /examples/deprecated_tutorials/tutorial_mnist_distributed.yml: -------------------------------------------------------------------------------- 1 | # https://docs.docker.com/compose/compose-file/ 2 | # 3 | # reference: https://docs.microsoft.com/en-us/azure/container-service/dcos-swarm/container-service-swarm-walkthrough 4 | # 1. create a swarm cluster on azure: 5 | # $ az group create -l southeastasia -n tensorlayer-swarm -o table --debug 6 | # $ az acs create -n tl-swarm-culster --orchestrator-type Swarm -g tensorlayer-swarm --agent-count 3 -o table --debug 7 | # 8 | # 2. create a ssh tunnel to swarm master: 9 | # $ master=$(az acs show -n tl-swarm-culster -g tensorlayer-swarm --query 'masterProfile.fqdn' | jq -r .) 10 | # $ ssh -p 2200 -fNL 2375:localhost:2375 azureuser@$master 11 | # $ export DOCKER_HOST=:2375 12 | # 13 | # 3. start 14 | # $ docker-compose -f tutorial_mnist_distributed.yml up 15 | 16 | --- 17 | version: '3' 18 | services: 19 | master: 20 | image: tensorlayer/tensorlayer:latest 21 | entrypoint: 22 | - python 23 | - /tensorlayer/example/tutorial_mnist_distributed.py 24 | environment: 25 | CUDA_VISIBLE_DEVICES: '' 26 | TF_CONFIG: |- 27 | { 28 | "cluster": { 29 | "ps": [ 30 | "ps:3001" 31 | ], 32 | "worker": [ 33 | "master:3002", 34 | "worker:3003" 35 | ] 36 | }, 37 | "task": { 38 | "type": "worker", 39 | "index": 0 40 | } 41 | } 42 | worker: 43 | image: tensorlayer/tensorlayer:latest 44 | entrypoint: 45 | - python 46 | - /tensorlayer/example/tutorial_mnist_distributed.py 47 | environment: 48 | CUDA_VISIBLE_DEVICES: '' 49 | TF_CONFIG: |- 50 | { 51 | "cluster": { 52 | "ps": [ 53 | "ps:3001" 54 | ], 55 | "worker": [ 56 | "master:3002", 57 | "worker:3003" 58 | ] 59 | }, 60 | "task": { 61 | "type": "worker", 62 | "index": 1 63 | } 64 | } 65 | ps: 66 | image: tensorlayer/tensorlayer:latest 67 | entrypoint: 68 | - python 69 | - /tensorlayer/example/tutorial_mnist_distributed.py 70 | environment: 71 | CUDA_VISIBLE_DEVICES: '' 72 | TF_CONFIG: |- 73 | { 74 | "cluster": { 75 | "ps": [ 76 | "ps:3001" 77 | ], 78 | "worker": [ 79 | "master:3002", 80 | "worker:3003" 81 | ] 82 | }, 83 | "task": { 84 | "type": "ps", 85 | "index": 0 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /examples/distributed_training/README.md: -------------------------------------------------------------------------------- 1 | Mai Luo: -------------------------------------------------------------------------------- /examples/keras_tfslim/README.md: -------------------------------------------------------------------------------- 1 | ### All other TensorFlow's libraries can be connected into TensorLayer via LambdaLayer. -------------------------------------------------------------------------------- /examples/pretrained_cnn/README.md: -------------------------------------------------------------------------------- 1 | ## Please read the docs for using [Pre-trained Models](https://tensorlayer.readthedocs.io/en/latest/user/get_start_advance.html#pre-trained-cnn) 2 | 3 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/data/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import * 4 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/data/laska.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/pretrained_cnn/data/laska.png -------------------------------------------------------------------------------- /examples/pretrained_cnn/data/puzzle.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/pretrained_cnn/data/puzzle.jpeg -------------------------------------------------------------------------------- /examples/pretrained_cnn/data/tiger.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/pretrained_cnn/data/tiger.jpeg -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_mobilenetv1.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | MobileNetV1 for ImageNet using TL models 5 | 6 | - mobilenetv2 : https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet 7 | - tf.slim : https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models 8 | """ 9 | 10 | import time 11 | 12 | import numpy as np 13 | import tensorflow as tf 14 | 15 | import tensorlayer as tl 16 | from tensorlayer.models.imagenet_classes import class_names 17 | 18 | # tf.logging.set_verbosity(tf.logging.DEBUG) 19 | tl.logging.set_verbosity(tl.logging.DEBUG) 20 | 21 | # get the whole model 22 | mobilenetv1 = tl.models.MobileNetV1(pretrained=True) 23 | 24 | img1 = tl.vis.read_image('data/tiger.jpeg') 25 | img1 = tl.prepro.imresize(img1, (224, 224)) / 255 26 | img1 = img1.astype(np.float32)[np.newaxis, ...] 27 | 28 | start_time = time.time() 29 | output = mobilenetv1(img1, is_train=False) 30 | prob = tf.nn.softmax(output)[0].numpy() 31 | print(" End time : %.5ss" % (time.time() - start_time)) 32 | preds = (np.argsort(prob)[::-1])[0:5] 33 | for p in preds: 34 | print(class_names[p], prob[p]) 35 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_resnet50.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """ 4 | ResNet50 for ImageNet using TL models 5 | 6 | """ 7 | 8 | import time 9 | 10 | import numpy as np 11 | import tensorflow as tf 12 | 13 | import tensorlayer as tl 14 | from tensorlayer.models.imagenet_classes import class_names 15 | 16 | # tf.logging.set_verbosity(tf.logging.DEBUG) 17 | tl.logging.set_verbosity(tl.logging.DEBUG) 18 | 19 | # get the whole model 20 | resnet = tl.models.ResNet50(pretrained=True) 21 | 22 | img1 = tl.vis.read_image('data/tiger.jpeg') 23 | img1 = tl.prepro.imresize(img1, (224, 224))[:, :, ::-1] 24 | img1 = img1 - np.array([103.939, 116.779, 123.68]).reshape((1, 1, 3)) 25 | 26 | img1 = img1.astype(np.float32)[np.newaxis, ...] 27 | 28 | start_time = time.time() 29 | output = resnet(img1, is_train=False) 30 | prob = tf.nn.softmax(output)[0].numpy() 31 | print(" End time : %.5ss" % (time.time() - start_time)) 32 | preds = (np.argsort(prob)[::-1])[0:5] 33 | for p in preds: 34 | print(class_names[p], prob[p]) 35 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_squeezenetv1.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """SqueezeNet for ImageNet using TL models.""" 4 | 5 | import time 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tensorlayer.models.imagenet_classes import class_names 12 | 13 | # tf.logging.set_verbosity(tf.logging.DEBUG) 14 | tl.logging.set_verbosity(tl.logging.DEBUG) 15 | 16 | # get the whole model 17 | squeezenet = tl.models.SqueezeNetV1(pretrained=True) 18 | print(squeezenet) 19 | 20 | img1 = tl.vis.read_image('data/tiger.jpeg') 21 | img1 = tl.prepro.imresize(img1, (224, 224)) / 255 22 | img1 = img1.astype(np.float32)[np.newaxis, ...] 23 | 24 | start_time = time.time() 25 | output = squeezenet(img1, is_train=False) 26 | prob = tf.nn.softmax(output)[0].numpy() 27 | print(" End time : %.5ss" % (time.time() - start_time)) 28 | preds = (np.argsort(prob)[::-1])[0:5] 29 | for p in preds: 30 | print(class_names[p], prob[p]) 31 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_vgg16.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """VGG-16 for ImageNet using TL models.""" 4 | 5 | import time 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tensorlayer.models.imagenet_classes import class_names 12 | 13 | tl.logging.set_verbosity(tl.logging.DEBUG) 14 | 15 | # get the whole model 16 | vgg = tl.models.vgg16(pretrained=True) 17 | 18 | img = tl.vis.read_image('data/tiger.jpeg') 19 | img = tl.prepro.imresize(img, (224, 224)).astype(np.float32) / 255 20 | 21 | start_time = time.time() 22 | output = vgg(img, is_train=False) 23 | probs = tf.nn.softmax(output)[0].numpy() 24 | print(" End time : %.5ss" % (time.time() - start_time)) 25 | preds = (np.argsort(probs)[::-1])[0:5] 26 | for p in preds: 27 | print(class_names[p], probs[p]) 28 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_vgg19.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """VGG-19 for ImageNet using TL models.""" 4 | 5 | import time 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tensorlayer.models.imagenet_classes import class_names 12 | 13 | tl.logging.set_verbosity(tl.logging.DEBUG) 14 | 15 | # get the whole model 16 | vgg = tl.models.vgg19(pretrained=True) 17 | 18 | img = tl.vis.read_image('data/tiger.jpeg') 19 | img = tl.prepro.imresize(img, (224, 224)).astype(np.float32) / 255 20 | 21 | start_time = time.time() 22 | output = vgg(img, is_train=False) 23 | probs = tf.nn.softmax(output)[0].numpy() 24 | print(" End time : %.5ss" % (time.time() - start_time)) 25 | preds = (np.argsort(probs)[::-1])[0:5] 26 | for p in preds: 27 | print(class_names[p], probs[p]) 28 | -------------------------------------------------------------------------------- /examples/pretrained_cnn/tutorial_models_vgg_static.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """VGG for ImageNet using TL models.""" 4 | 5 | import time 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tensorlayer.models.imagenet_classes import class_names 12 | 13 | tl.logging.set_verbosity(tl.logging.DEBUG) 14 | 15 | # get the whole model 16 | vgg = tl.models.vgg16(pretrained=True, mode='static') 17 | 18 | img = tl.vis.read_image('data/tiger.jpeg') 19 | img = tl.prepro.imresize(img, (224, 224)).astype(np.float32) / 255 20 | 21 | start_time = time.time() 22 | output = vgg(img, is_train=False) 23 | probs = tf.nn.softmax(output)[0].numpy() 24 | print(" End time : %.5ss" % (time.time() - start_time)) 25 | preds = (np.argsort(probs)[::-1])[0:5] 26 | for p in preds: 27 | print(class_names[p], probs[p]) 28 | -------------------------------------------------------------------------------- /examples/quantized_net/README.md: -------------------------------------------------------------------------------- 1 | ### TODO 2 | - All TFRecord implementation is better to be changed to Dataset API. 3 | 4 | ### Blogs 5 | - [Google量化网络实现(CVPR2018)](https://zhuanlan.zhihu.com/p/41121544) 6 | - [神经网络加速之量化模型(附带代码)](https://zhuanlan.zhihu.com/p/37220669) -------------------------------------------------------------------------------- /examples/reinforcement_learning/.gitignore: -------------------------------------------------------------------------------- 1 | model/ 2 | image/ 3 | -------------------------------------------------------------------------------- /examples/spatial_transformer_network/README.md: -------------------------------------------------------------------------------- 1 | # Spatial Transformer Networks 2 | 3 | [Spatial Transformer Networks](https://arxiv.org/abs/1506.02025) (STN) is a dynamic mechanism that produces transformations of input images (or feature maps)including scaling, cropping, rotations, as well as non-rigid deformations. This enables the network to not only select regions of an image that are most relevant (attention), but also to transform those regions to simplify recognition in the following layers. 4 | 5 | Video for different transformation [click me](https://drive.google.com/file/d/0B1nQa_sA3W2iN3RQLXVFRkNXN0k/view). 6 | 7 | In this repositary, we implemented a STN for [2D Affine Transformation](https://en.wikipedia.org/wiki/Affine_transformation) on MNIST dataset. We generated images with size of 40x40 from the original MNIST dataset, and distorted the images by random rotation, shifting, shearing and zoom in/out. The STN was able to learn to automatically apply transformations on distorted images via classification task. 8 | 9 | 10 |
11 | 12 |
13 | Fig 1:Transformation 14 |
15 | 16 | 17 |
18 | 19 |
20 | Fig 2:Network 21 |
22 | 23 |
24 | 25 |
26 | Fig 3:Formula 27 |
28 | 29 | ## Result 30 | 31 | After classification task, the STN is able to transform the distorted image from Fig 4 back to Fig 5. 32 | 33 |
34 | 35 |
36 | Fig 4: Input 37 |
38 | 39 |
40 | 41 |
42 | Fig 5: Output 43 |
44 | 45 | -------------------------------------------------------------------------------- /examples/text_classification/readme.md: -------------------------------------------------------------------------------- 1 | 2 | ### Introduction 3 | 4 | The demos implement [FastText](http://arxiv.org/abs/1607.01759)[1] for sentence classification. 5 | 6 | Code: [tutorial_imdb_fasttext.py](tutorial_imdb_fasttext.py) 7 | 8 | FastText is a simple model for text classification with performance often close 9 | to state-of-the-art, and is useful as a solid baseline. 10 | 11 | There are some important differences between this implementation and what 12 | is described in the paper. Instead of Hogwild! SGD[2], we use Adam optimizer 13 | with mini-batches. Hierarchical softmax is also not supported; if you have 14 | a large label space, consider utilizing candidate sampling methods provided 15 | by TensorFlow[3]. 16 | 17 | After 5 epochs, you should get test accuracy around 90.3%. 18 | 19 | ### References 20 | 21 | [1] Joulin, A., Grave, E., Bojanowski, P., & Mikolov, T. (2016). 22 | Bag of Tricks for Efficient Text Classification. 23 | 24 | 25 | [2] Recht, B., Re, C., Wright, S., & Niu, F. (2011). 26 | Hogwild: A Lock-Free Approach to Parallelizing Stochastic Gradient Descent. 27 | In Advances in Neural Information Processing Systems 24 (pp. 693–701). 28 | 29 | [3] 30 | -------------------------------------------------------------------------------- /examples/text_generation/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/text_generation/README.md -------------------------------------------------------------------------------- /examples/text_generation/data/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/text_generation/data/.DS_Store -------------------------------------------------------------------------------- /examples/text_generation/data/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | 3 | from . import imagenet_classes 4 | 5 | # from . import 6 | -------------------------------------------------------------------------------- /examples/text_ptb/README.md: -------------------------------------------------------------------------------- 1 | ### Language modeling on Penn Tree Bank (PTB) dataset -------------------------------------------------------------------------------- /examples/text_word_embedding/word2vec_basic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/examples/text_word_embedding/word2vec_basic.pdf -------------------------------------------------------------------------------- /img/TL_gardener.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/TL_gardener.png -------------------------------------------------------------------------------- /img/TL_gardener.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/TL_gardener.psd -------------------------------------------------------------------------------- /img/awesome-mentioned.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/awesome-mentioned.png -------------------------------------------------------------------------------- /img/github_mascot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/github_mascot.png -------------------------------------------------------------------------------- /img/img_tensorflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/img_tensorflow.png -------------------------------------------------------------------------------- /img/img_tensorlayer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/img_tensorlayer.png -------------------------------------------------------------------------------- /img/img_tlayer1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/img_tlayer1.png -------------------------------------------------------------------------------- /img/join_slack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/join_slack.png -------------------------------------------------------------------------------- /img/join_slack.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/join_slack.psd -------------------------------------------------------------------------------- /img/medium/Readme.md: -------------------------------------------------------------------------------- 1 | # Medium GuideLines 2 | 3 | ## Publication Avatar 4 | 5 | ![Medium Avatar](https://help.medium.com/hc/article_attachments/115008917667/pub1.png) 6 | 7 | ## Publication Logo (appears on posts) 8 | 9 | ![Medium Publication Logo 1](https://help.medium.com/hc/article_attachments/115009021788/pub2.png) 10 | ![Medium Publication Logo 2](https://help.medium.com/hc/article_attachments/115009021788/pub3.png) 11 | 12 | Add a publication logo, which appears at the top of all your publication's stories. 13 | 14 | It is 72px tall and can have a maximum width of 600px. 15 | 16 | ## Publication homepage images 17 | 18 | Under Homepage and settings > Layout, you can select a header size, upload a logo and add a background image (large header size only). -------------------------------------------------------------------------------- /img/medium/medium_header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/medium/medium_header.png -------------------------------------------------------------------------------- /img/medium/medium_header.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/medium/medium_header.psd -------------------------------------------------------------------------------- /img/tl_black_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/tl_black_logo.png -------------------------------------------------------------------------------- /img/tl_transparent_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/tl_transparent_logo.png -------------------------------------------------------------------------------- /img/tl_white_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/img/tl_white_logo.png -------------------------------------------------------------------------------- /requirements/requirements.txt: -------------------------------------------------------------------------------- 1 | imageio>=2.5.0 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 | -------------------------------------------------------------------------------- /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 | sphinx_rtd_theme>=0.4,<0.5 6 | wrapt>=1.11.1 7 | h5py>=2.9 8 | cloudpickle>=0.8.1 9 | -------------------------------------------------------------------------------- /requirements/requirements_extra.txt: -------------------------------------------------------------------------------- 1 | opencv-python>=4.1.0.25 2 | nltk>=3.3,<3.5 3 | matplotlib>=2.2,<3.1 4 | requests>=2.21.0 5 | tqdm>=4.31.1 6 | lxml>=4.3.3 7 | -------------------------------------------------------------------------------- /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.0.0-alpha0 2 | -------------------------------------------------------------------------------- /requirements/requirements_tf_gpu.txt: -------------------------------------------------------------------------------- 1 | tensorflow-gpu>=2.0.0-rc1 2 | -------------------------------------------------------------------------------- /scripts/download_and_install_openmpi3_ubuntu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | if [ $(uname) == "Darwin" ]; then 6 | NPROC=$(sysctl -n hw.ncpu) 7 | else 8 | NPROC=$(nproc) 9 | fi 10 | 11 | mkdir -p $HOME/openmpi_tmp && cd $HOME/openmpi_tmp 12 | 13 | # TODO: upgrade to latest version once https://github.com/open-mpi/ompi/pull/5296 is in the release 14 | MPI_MAJOR=3 15 | MPI_MINOR=1 16 | 17 | VERSION=${MPI_MAJOR}.${MPI_MINOR}.0 18 | FILENAME=openmpi-${VERSION}.tar.bz2 19 | FOLDER=openmpi-${VERSION} 20 | URL=https://download.open-mpi.org/release/open-mpi/v${MPI_MAJOR}.${MPI_MINOR}/${FILENAME} 21 | 22 | [ ! -f ${FILENAME} ] && curl -vLOJ $URL 23 | tar -xf ${FILENAME} 24 | cd ${FOLDER} 25 | 26 | # will take about 8 min or longer depends on your machine. 27 | ./configure --prefix=$HOME/local/openmpi 28 | make -j ${NPROC} all 29 | make install 30 | 31 | rm -rf $HOME/openmpi_tmp 32 | 33 | echo 'Update the PATH with OpenMPI bin by running: PATH=$PATH:$HOME/local/openmpi/bin' 34 | echo 'Update the PATH in ~/.bashrc if you want OpenMPI to be ready once the machine start' 35 | -------------------------------------------------------------------------------- /scripts/install-horovod-for-doc-test.sh: -------------------------------------------------------------------------------- 1 | # This script is for installing horovod on travis-CI only! 2 | 3 | set -e 4 | 5 | [ ! -z "$1" ] && export PATH=$1:$PATH 6 | 7 | mkdir -p /opt 8 | chmod a+rx /opt 9 | cd /opt 10 | wget https://github.com/lgarithm/openmpi-release/raw/master/releases/openmpi-bin-3.1.0.tar.bz2 11 | tar -xf openmpi-bin-3.1.0.tar.bz2 12 | 13 | PATH=/opt/openmpi/bin:$PATH pip install horovod 14 | 15 | echo "done $0" 16 | -------------------------------------------------------------------------------- /scripts/install-requirements-for-rtd.sh: -------------------------------------------------------------------------------- 1 | # This script is for installing horovod on readthedocs only! 2 | 3 | set -e 4 | 5 | pwd 6 | SCRIPT_DIR=$(cd $(dirname $0) && pwd) 7 | 8 | [ ! -z "$1" ] && export PATH=$1:$PATH 9 | 10 | LOCATION=/home/docs 11 | URL=https://github.com/lgarithm/openmpi-release/raw/master/releases/openmpi-bin-3.1.0-rtd.tar.bz2 12 | 13 | mkdir -p ${LOCATION} 14 | chmod a+rx ${LOCATION} 15 | cd ${LOCATION} 16 | curl -vLOJ ${URL} 17 | tar -xf *.tar.bz2 18 | 19 | pip install tensorflow==1.5.0 # must install tensorflow before horovod 20 | PATH=${LOCATION}/openmpi/bin:$PATH pip install horovod 21 | 22 | # install all requirements except tensorflow 23 | for req in $(find $SCRIPT_DIR/../requirements -type f); do 24 | if [ ! $(grep tensorflow $req) ]; then 25 | pip install -r $req 26 | fi 27 | done 28 | 29 | echo "done $0" 30 | -------------------------------------------------------------------------------- /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 | tensorlayer.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 | dedent_closing_brackets=True 34 | 35 | # Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set 36 | coalesce_brackets = False 37 | 38 | # Align closing bracket with visual indentation. 39 | align_closing_bracket_with_visual_indent = False 40 | 41 | # Split named assignments onto individual lines. 42 | split_before_named_assigns = False 43 | 44 | # If an argument / parameter list is going to be split, then split before the first argument. 45 | split_before_first_argument = True 46 | 47 | # Split before arguments if the argument list is terminated by a comma. 48 | split_arguments_when_comma_terminated = False 49 | 50 | # Insert a space between the ending comma and closing bracket of a list, etc. 51 | space_between_ending_comma_and_closing_bracket = True 52 | 53 | # Join short lines into one line. E.g., single line if statements. 54 | join_multiple_lines = True 55 | 56 | # Do not include spaces around selected binary operators. 57 | # Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5 58 | no_spaces_around_selected_binary_operators = True 59 | 60 | # Allow lambdas to be formatted on more than one line. 61 | allow_multiline_lambdas = True 62 | 63 | SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = 10 64 | SPLIT_PENALTY_AFTER_OPENING_BRACKET = 500 65 | -------------------------------------------------------------------------------- /setup.travis_doc.cfg: -------------------------------------------------------------------------------- 1 | [tool:pytest] 2 | testpaths = tests/ 3 | python_files=*test_documentation* 4 | *test_yapf_format* 5 | 6 | [flake8] 7 | max-line-length = 120 8 | ignore = 9 | D301 10 | E221 # Space before equal sign 11 | E251 # Space after equal sign 12 | exclude = 13 | .git, 14 | venv, 15 | __pycache__, 16 | .pytest_cache, 17 | tensorlayer.egg-info, 18 | build, 19 | dist, 20 | img 21 | 22 | [yapf] 23 | based_on_style=google 24 | 25 | # The number of columns to use for indentation. 26 | indent_width = 4 27 | 28 | # The column limit. 29 | column_limit=120 30 | 31 | # Place each dictionary entry onto its own line. 32 | each_dict_entry_on_separate_line = True 33 | 34 | # Put closing brackets on a separate line, dedented, if the bracketed 35 | dedent_closing_brackets=True 36 | 37 | # Do not split consecutive brackets. Only relevant when DEDENT_CLOSING_BRACKETS is set 38 | coalesce_brackets = False 39 | 40 | # Align closing bracket with visual indentation. 41 | align_closing_bracket_with_visual_indent = False 42 | 43 | # Split named assignments onto individual lines. 44 | split_before_named_assigns = False 45 | 46 | # If an argument / parameter list is going to be split, then split before the first argument. 47 | split_before_first_argument = True 48 | 49 | # Split before arguments if the argument list is terminated by a comma. 50 | split_arguments_when_comma_terminated = False 51 | 52 | # Insert a space between the ending comma and closing bracket of a list, etc. 53 | space_between_ending_comma_and_closing_bracket = True 54 | 55 | # Join short lines into one line. E.g., single line if statements. 56 | join_multiple_lines = True 57 | 58 | # Do not include spaces around selected binary operators. 59 | # Example: 1 + 2 * 3 - 4 / 5 => 1 + 2*3 - 4/5 60 | no_spaces_around_selected_binary_operators = True 61 | 62 | # Allow lambdas to be formatted on more than one line. 63 | allow_multiline_lambdas = True 64 | 65 | SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT = 10 66 | SPLIT_PENALTY_AFTER_OPENING_BRACKET = 500 -------------------------------------------------------------------------------- /tensorlayer/__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 os 6 | from distutils.version import LooseVersion 7 | 8 | from tensorlayer.package_info import ( 9 | VERSION, __contact_emails__, __contact_names__, __description__, __download_url__, __homepage__, __keywords__, 10 | __license__, __package_name__, __repository_url__, __shortversion__, __version__ 11 | ) 12 | 13 | if 'TENSORLAYER_PACKAGE_BUILDING' not in os.environ: 14 | 15 | try: 16 | import tensorflow 17 | except Exception as e: 18 | raise ImportError( 19 | "Tensorflow is not installed, please install it with the one of the following commands:\n" 20 | " - `pip install --upgrade tensorflow`\n" 21 | " - `pip install --upgrade tensorflow-gpu`" 22 | ) 23 | 24 | if ("SPHINXBUILD" not in os.environ and "READTHEDOCS" not in os.environ and 25 | LooseVersion(tensorflow.__version__) < LooseVersion("2.0.0")): 26 | raise RuntimeError( 27 | "TensorLayer does not support Tensorflow version older than 2.0.0.\n" 28 | "Please update Tensorflow with:\n" 29 | " - `pip install --upgrade tensorflow`\n" 30 | " - `pip install --upgrade tensorflow-gpu`" 31 | ) 32 | 33 | from tensorlayer import activation 34 | from tensorlayer import array_ops 35 | from tensorlayer import cost 36 | from tensorlayer import decorators 37 | from tensorlayer import files 38 | from tensorlayer import initializers 39 | from tensorlayer import iterate 40 | from tensorlayer import layers 41 | from tensorlayer import lazy_imports 42 | from tensorlayer import logging 43 | from tensorlayer import models 44 | from tensorlayer import optimizers 45 | from tensorlayer import rein 46 | from tensorlayer import utils 47 | from tensorlayer import app 48 | 49 | from tensorlayer.lazy_imports import LazyImport 50 | 51 | # Lazy Imports 52 | db = LazyImport("tensorlayer.db") 53 | distributed = LazyImport("tensorlayer.distributed") 54 | nlp = LazyImport("tensorlayer.nlp") 55 | prepro = LazyImport("tensorlayer.prepro") 56 | utils = LazyImport("tensorlayer.utils") 57 | visualize = LazyImport("tensorlayer.visualize") 58 | 59 | # alias 60 | act = activation 61 | vis = visualize 62 | 63 | alphas = array_ops.alphas 64 | alphas_like = array_ops.alphas_like 65 | 66 | # global vars 67 | global_flag = {} 68 | global_dict = {} 69 | -------------------------------------------------------------------------------- /tensorlayer/app/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .computer_vision_object_detection import * 5 | from .human_pose_estimation import * 6 | from .computer_vision import * 7 | -------------------------------------------------------------------------------- /tensorlayer/app/computer_vision_object_detection/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .yolov4 import YOLOv4 5 | from .common import * 6 | -------------------------------------------------------------------------------- /tensorlayer/app/human_pose_estimation/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .common import * 5 | from .LCN import CGCNN 6 | -------------------------------------------------------------------------------- /tensorlayer/cli/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | """The tensorlayer.cli module provides a command-line tool for some common tasks.""" 4 | -------------------------------------------------------------------------------- /tensorlayer/cli/__main__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import argparse 5 | 6 | from tensorlayer.cli import train 7 | 8 | if __name__ == "__main__": 9 | parser = argparse.ArgumentParser(prog='tl') 10 | subparsers = parser.add_subparsers(dest='cmd') 11 | train_parser = subparsers.add_parser('train', help='train a model using multiple local GPUs or CPUs.') 12 | train.build_arg_parser(train_parser) 13 | args = parser.parse_args() 14 | if args.cmd == 'train': 15 | train.main(args) 16 | else: 17 | parser.print_help() 18 | -------------------------------------------------------------------------------- /tensorlayer/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.nn.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 | -------------------------------------------------------------------------------- /tensorlayer/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 tensorlayer.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 tensorlayer 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 | -------------------------------------------------------------------------------- /tensorlayer/decorators/deprecated_alias.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import functools 5 | import warnings 6 | 7 | from tensorlayer 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 | -------------------------------------------------------------------------------- /tensorlayer/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 | -------------------------------------------------------------------------------- /tensorlayer/files/__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.nn.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from tensorlayer.lazy_imports import LazyImport 13 | 14 | from .dataset_loaders.celebA_dataset import * 15 | from .dataset_loaders.cifar10_dataset import * 16 | from .dataset_loaders.cyclegan_dataset import * 17 | from .dataset_loaders.flickr_1M_dataset import * 18 | from .dataset_loaders.flickr_25k_dataset import * 19 | from .dataset_loaders.imdb_dataset import * 20 | from .dataset_loaders.matt_mahoney_dataset import * 21 | from .dataset_loaders.mnist_dataset import * 22 | from .dataset_loaders.mnist_fashion_dataset import * 23 | from .dataset_loaders.mpii_dataset import * 24 | from .dataset_loaders.nietzsche_dataset import * 25 | from .dataset_loaders.ptb_dataset import * 26 | from .dataset_loaders.voc_dataset import * 27 | from .dataset_loaders.wmt_en_fr_dataset import * 28 | from .utils import * 29 | 30 | __all__ = [ 31 | # Dataset Loaders 32 | 'load_celebA_dataset', 33 | 'load_cifar10_dataset', 34 | 'load_cyclegan_dataset', 35 | 'load_fashion_mnist_dataset', 36 | 'load_flickr1M_dataset', 37 | 'load_flickr25k_dataset', 38 | 'load_imdb_dataset', 39 | 'load_matt_mahoney_text8_dataset', 40 | 'load_mnist_dataset', 41 | 'load_mpii_pose_dataset', 42 | 'load_nietzsche_dataset', 43 | 'load_ptb_dataset', 44 | 'load_voc_dataset', 45 | 'load_wmt_en_fr_dataset', 46 | 47 | # Util Functions 48 | 'assign_params', 49 | 'del_file', 50 | 'del_folder', 51 | 'download_file_from_google_drive', 52 | 'exists_or_mkdir', 53 | 'file_exists', 54 | 'folder_exists', 55 | 'load_and_assign_npz', 56 | 'load_and_assign_npz_dict', 57 | 'load_ckpt', 58 | 'load_cropped_svhn', 59 | 'load_file_list', 60 | 'load_folder_list', 61 | 'load_npy_to_any', 62 | 'load_npz', 63 | 'maybe_download_and_extract', 64 | 'natural_keys', 65 | 'npz_to_W_pdf', 66 | 'read_file', 67 | 'save_any_to_npy', 68 | 'save_ckpt', 69 | 'save_npz', 70 | 'save_npz_dict', 71 | #'save_graph', 72 | #'load_graph', 73 | #'save_graph_and_params', 74 | #'load_graph_and_params', 75 | 'load_and_assign_ckpt', 76 | 'ckpt_to_npz_dict' 77 | ] 78 | -------------------------------------------------------------------------------- /tensorlayer/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 | -------------------------------------------------------------------------------- /tensorlayer/files/dataset_loaders/celebA_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import zipfile 6 | 7 | from tensorlayer import logging 8 | from tensorlayer.files.utils import (download_file_from_google_drive, exists_or_mkdir, load_file_list) 9 | 10 | __all__ = ['load_celebA_dataset'] 11 | 12 | 13 | def load_celebA_dataset(path='data'): 14 | """Load CelebA dataset 15 | 16 | Return a list of image path. 17 | 18 | Parameters 19 | ----------- 20 | path : str 21 | The path that the data is downloaded to, defaults is ``data/celebA/``. 22 | 23 | """ 24 | data_dir = 'celebA' 25 | filename, drive_id = "img_align_celeba.zip", "0B7EVK8r0v71pZjFTYXZWM3FlRnM" 26 | save_path = os.path.join(path, filename) 27 | image_path = os.path.join(path, data_dir) 28 | if os.path.exists(image_path): 29 | logging.info('[*] {} already exists'.format(save_path)) 30 | else: 31 | exists_or_mkdir(path) 32 | download_file_from_google_drive(drive_id, save_path) 33 | zip_dir = '' 34 | with zipfile.ZipFile(save_path) as zf: 35 | zip_dir = zf.namelist()[0] 36 | zf.extractall(path) 37 | os.remove(save_path) 38 | os.rename(os.path.join(path, zip_dir), image_path) 39 | 40 | data_files = load_file_list(path=image_path, regx='\\.jpg', printable=False) 41 | for i, _v in enumerate(data_files): 42 | data_files[i] = os.path.join(image_path, data_files[i]) 43 | return data_files 44 | -------------------------------------------------------------------------------- /tensorlayer/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 tensorlayer import logging, visualize 9 | from tensorlayer.files.utils import (del_file, folder_exists, load_file_list, maybe_download_and_extract) 10 | 11 | __all__ = ['load_cyclegan_dataset'] 12 | 13 | 14 | def load_cyclegan_dataset(filename='summer2winter_yosemite', path='data'): 15 | """Load images from CycleGAN's database, see `this link `__. 16 | 17 | Parameters 18 | ------------ 19 | filename : str 20 | The dataset you want, see `this link `__. 21 | path : str 22 | The path that the data is downloaded to, defaults is `data/cyclegan` 23 | 24 | Examples 25 | --------- 26 | >>> im_train_A, im_train_B, im_test_A, im_test_B = load_cyclegan_dataset(filename='summer2winter_yosemite') 27 | 28 | """ 29 | path = os.path.join(path, 'cyclegan') 30 | url = 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/' 31 | 32 | if folder_exists(os.path.join(path, filename)) is False: 33 | logging.info("[*] {} is nonexistent in {}".format(filename, path)) 34 | maybe_download_and_extract(filename + '.zip', path, url, extract=True) 35 | del_file(os.path.join(path, filename + '.zip')) 36 | 37 | def load_image_from_folder(path): 38 | path_imgs = load_file_list(path=path, regx='\\.jpg', printable=False) 39 | return visualize.read_images(path_imgs, path=path, n_threads=10, printable=False) 40 | 41 | im_train_A = load_image_from_folder(os.path.join(path, filename, "trainA")) 42 | im_train_B = load_image_from_folder(os.path.join(path, filename, "trainB")) 43 | im_test_A = load_image_from_folder(os.path.join(path, filename, "testA")) 44 | im_test_B = load_image_from_folder(os.path.join(path, filename, "testB")) 45 | 46 | def if_2d_to_3d(images): # [h, w] --> [h, w, 3] 47 | for i, _v in enumerate(images): 48 | if len(images[i].shape) == 2: 49 | images[i] = images[i][:, :, np.newaxis] 50 | images[i] = np.tile(images[i], (1, 1, 3)) 51 | return images 52 | 53 | im_train_A = if_2d_to_3d(im_train_A) 54 | im_train_B = if_2d_to_3d(im_train_B) 55 | im_test_A = if_2d_to_3d(im_test_A) 56 | im_test_B = if_2d_to_3d(im_test_B) 57 | 58 | return im_train_A, im_train_B, im_test_A, im_test_B 59 | -------------------------------------------------------------------------------- /tensorlayer/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 tensorlayer import logging 8 | from tensorlayer.files.utils import maybe_download_and_extract 9 | 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 = tl.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("Load or Download matt_mahoney_text8 Dataset> {}".format(path)) 39 | 40 | filename = 'text8.zip' 41 | url = 'http://mattmahoney.net/dc/' 42 | maybe_download_and_extract(filename, path, url, expected_bytes=31344016) 43 | 44 | with zipfile.ZipFile(os.path.join(path, filename)) as f: 45 | word_list = f.read(f.namelist()[0]).split() 46 | for idx, _ in enumerate(word_list): 47 | word_list[idx] = word_list[idx].decode() 48 | return word_list 49 | -------------------------------------------------------------------------------- /tensorlayer/files/dataset_loaders/mnist_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayer.files.utils import _load_mnist_dataset 5 | 6 | __all__ = ['load_mnist_dataset'] 7 | 8 | 9 | def load_mnist_dataset(shape=(-1, 784), path='data'): 10 | """Load the original mnist. 11 | 12 | Automatically download MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 digit images respectively. 13 | 14 | Parameters 15 | ---------- 16 | shape : tuple 17 | The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)). 18 | path : str 19 | The path that the data is downloaded to. 20 | 21 | Returns 22 | ------- 23 | X_train, y_train, X_val, y_val, X_test, y_test: tuple 24 | Return splitted training/validation/test set respectively. 25 | 26 | Examples 27 | -------- 28 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1,784), path='datasets') 29 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1)) 30 | """ 31 | return _load_mnist_dataset(shape, path, name='mnist', url='http://yann.lecun.com/exdb/mnist/') 32 | -------------------------------------------------------------------------------- /tensorlayer/files/dataset_loaders/mnist_fashion_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tensorlayer.files.utils import _load_mnist_dataset 5 | 6 | __all__ = ['load_fashion_mnist_dataset'] 7 | 8 | 9 | def load_fashion_mnist_dataset(shape=(-1, 784), path='data'): 10 | """Load the fashion mnist. 11 | 12 | Automatically download fashion-MNIST dataset and return the training, validation and test set with 50000, 10000 and 10000 fashion images respectively, `examples `__. 13 | 14 | Parameters 15 | ---------- 16 | shape : tuple 17 | The shape of digit images (the default is (-1, 784), alternatively (-1, 28, 28, 1)). 18 | path : str 19 | The path that the data is downloaded to. 20 | 21 | Returns 22 | ------- 23 | X_train, y_train, X_val, y_val, X_test, y_test: tuple 24 | Return splitted training/validation/test set respectively. 25 | 26 | Examples 27 | -------- 28 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1,784), path='datasets') 29 | >>> X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_fashion_mnist_dataset(shape=(-1, 28, 28, 1)) 30 | """ 31 | return _load_mnist_dataset( 32 | shape, path, name='fashion_mnist', url='http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/' 33 | ) 34 | -------------------------------------------------------------------------------- /tensorlayer/files/dataset_loaders/nietzsche_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | from tensorlayer import logging 7 | from tensorlayer.files.utils import maybe_download_and_extract 8 | 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 = tl.files.load_nietzsche_dataset() 29 | >>> words = basic_clean_str(words) 30 | >>> words = words.split() 31 | 32 | """ 33 | logging.info("Load or Download nietzsche dataset > {}".format(path)) 34 | path = os.path.join(path, 'nietzsche') 35 | 36 | filename = "nietzsche.txt" 37 | url = 'https://s3.amazonaws.com/text-datasets/' 38 | filepath = maybe_download_and_extract(filename, path, url) 39 | 40 | with open(filepath, "r") as f: 41 | words = f.read() 42 | return words 43 | -------------------------------------------------------------------------------- /tensorlayer/files/dataset_loaders/ptb_dataset.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | 6 | from tensorlayer import logging, nlp 7 | from tensorlayer.files.utils import maybe_download_and_extract 8 | 9 | __all__ = ['load_ptb_dataset'] 10 | 11 | 12 | def load_ptb_dataset(path='data'): 13 | """Load Penn TreeBank (PTB) dataset. 14 | 15 | It is used in many LANGUAGE MODELING papers, 16 | including "Empirical Evaluation and Combination of Advanced Language 17 | Modeling Techniques", "Recurrent Neural Network Regularization". 18 | It consists of 929k training words, 73k validation words, and 82k test 19 | words. It has 10k words in its vocabulary. 20 | 21 | Parameters 22 | ---------- 23 | path : str 24 | The path that the data is downloaded to, defaults is ``data/ptb/``. 25 | 26 | Returns 27 | -------- 28 | train_data, valid_data, test_data : list of int 29 | The training, validating and testing data in integer format. 30 | vocab_size : int 31 | The vocabulary size. 32 | 33 | Examples 34 | -------- 35 | >>> train_data, valid_data, test_data, vocab_size = tl.files.load_ptb_dataset() 36 | 37 | References 38 | --------------- 39 | - ``tensorflow.models.rnn.ptb import reader`` 40 | - `Manual download `__ 41 | 42 | Notes 43 | ------ 44 | - If you want to get the raw data, see the source code. 45 | 46 | """ 47 | path = os.path.join(path, 'ptb') 48 | logging.info("Load or Download Penn TreeBank (PTB) dataset > {}".format(path)) 49 | 50 | #Maybe dowload and uncompress tar, or load exsisting files 51 | filename = 'simple-examples.tgz' 52 | url = 'http://www.fit.vutbr.cz/~imikolov/rnnlm/' 53 | maybe_download_and_extract(filename, path, url, extract=True) 54 | 55 | data_path = os.path.join(path, 'simple-examples', 'data') 56 | train_path = os.path.join(data_path, "ptb.train.txt") 57 | valid_path = os.path.join(data_path, "ptb.valid.txt") 58 | test_path = os.path.join(data_path, "ptb.test.txt") 59 | 60 | word_to_id = nlp.build_vocab(nlp.read_words(train_path)) 61 | 62 | train_data = nlp.words_to_word_ids(nlp.read_words(train_path), word_to_id) 63 | valid_data = nlp.words_to_word_ids(nlp.read_words(valid_path), word_to_id) 64 | test_data = nlp.words_to_word_ids(nlp.read_words(test_path), word_to_id) 65 | vocab_size = len(word_to_id) 66 | 67 | # logging.info(nlp.read_words(train_path)) # ... 'according', 'to', 'mr.', '', ''] 68 | # logging.info(train_data) # ... 214, 5, 23, 1, 2] 69 | # logging.info(word_to_id) # ... 'beyond': 1295, 'anti-nuclear': 9599, 'trouble': 1520, '': 2 ... } 70 | # logging.info(vocabulary) # 10000 71 | # exit() 72 | return train_data, valid_data, test_data, vocab_size 73 | -------------------------------------------------------------------------------- /tensorlayer/layers/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | from .activation import * 5 | from .convolution import * 6 | from .core import * 7 | from .dense import * 8 | from .deprecated import * 9 | from .dropout import * 10 | from .embedding import * 11 | from .extend import * 12 | from .image_resampling import * 13 | from .inputs import * 14 | from .lambda_layers import * 15 | from .merge import * 16 | from .noise import * 17 | from .normalization import * 18 | from .padding import * 19 | from .pooling import * 20 | from .quantize import * 21 | from .recurrent import * 22 | from .scale import * 23 | from .shape import * 24 | from .spatial_transformer import * 25 | from .stack import * 26 | from .utils import * 27 | -------------------------------------------------------------------------------- /tensorlayer/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.nn.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 | 27 | __all__ = [ 28 | 29 | # simplified conv 30 | 'Conv1d', 31 | 'Conv2d', 32 | 'Conv3d', 33 | 34 | # simplified deconv 35 | 'DeConv2d', 36 | 'DeConv3d', 37 | 38 | # expert conv 39 | 'Conv1dLayer', 40 | 'Conv2dLayer', 41 | 'Conv3dLayer', 42 | 43 | # expert conv 44 | 'DeConv1dLayer', 45 | 'DeConv2dLayer', 46 | 'DeConv3dLayer', 47 | 48 | # atrous 49 | # 'AtrousConv1dLayer', 50 | # 'AtrousConv2dLayer', 51 | # 'AtrousDeConv2d', 52 | 53 | # binary 54 | 'BinaryConv2d', 55 | 56 | # deformable 57 | 'DeformableConv2d', 58 | 59 | # depthwise 60 | 'DepthwiseConv2d', 61 | 62 | # dorefa 63 | 'DorefaConv2d', 64 | 65 | # group 66 | 'GroupConv2d', 67 | 68 | # separable 69 | 'SeparableConv1d', 70 | 'SeparableConv2d', 71 | 72 | # subpixel 73 | 'SubpixelConv1d', 74 | 'SubpixelConv2d', 75 | 76 | # ternary 77 | 'TernaryConv2d', 78 | 79 | #quan_conv 80 | 'QuanConv2d', 81 | 'QuanConv2dWithBN', 82 | ] 83 | -------------------------------------------------------------------------------- /tensorlayer/layers/dense/__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.nn.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .base_dense import * 13 | from .binary_dense import * 14 | from .dorefa_dense import * 15 | from .dropconnect import * 16 | from .quan_dense import * 17 | from .quan_dense_bn import * 18 | from .ternary_dense import * 19 | 20 | __all__ = [ 21 | 'BinaryDense', 22 | 'Dense', 23 | 'DorefaDense', 24 | 'DropconnectDense', 25 | 'TernaryDense', 26 | 'QuanDense', 27 | 'QuanDenseWithBN', 28 | ] 29 | -------------------------------------------------------------------------------- /tensorlayer/layers/dropout.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorflow as tf 5 | 6 | from tensorlayer import logging 7 | from tensorlayer.decorators import deprecated_alias 8 | from tensorlayer.layers.core import Layer 9 | 10 | # from tensorlayer.layers.core import LayersConfig 11 | 12 | __all__ = [ 13 | 'Dropout', 14 | ] 15 | 16 | 17 | class Dropout(Layer): 18 | """ 19 | The :class:`Dropout` class is a noise layer which randomly set some 20 | activations to zero according to a keeping probability. 21 | 22 | Parameters 23 | ---------- 24 | keep : float 25 | The keeping probability. 26 | The lower the probability it is, the more activations are set to zero. 27 | seed : int or None 28 | The seed for random dropout. 29 | name : None or str 30 | A unique layer name. 31 | 32 | """ 33 | 34 | def __init__(self, keep, seed=None, name=None): #"dropout"): 35 | super(Dropout, self).__init__(name) 36 | self.keep = keep 37 | self.seed = seed 38 | 39 | self.build() 40 | self._built = True 41 | 42 | logging.info("Dropout %s: keep: %f " % (self.name, self.keep)) 43 | 44 | def __repr__(self): 45 | s = ('{classname}(keep={keep}') 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 | pass 53 | 54 | # @tf.function 55 | def forward(self, inputs): 56 | if self.is_train: 57 | outputs = tf.nn.dropout(inputs, rate=1 - (self.keep), seed=self.seed, name=self.name) 58 | else: 59 | outputs = inputs 60 | return outputs 61 | -------------------------------------------------------------------------------- /tensorlayer/layers/inputs.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import numpy as np 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | from tensorlayer import logging 9 | from tensorlayer.layers.core import Layer, LayerNode 10 | 11 | # from tensorlayer.layers.core import LayersConfig 12 | 13 | __all__ = ['Input', '_InputLayer'] 14 | 15 | 16 | class _InputLayer(Layer): 17 | """ 18 | The :class:`Input` class is the starting layer of a neural network. 19 | 20 | Parameters 21 | ---------- 22 | shape : tuple (int) 23 | Including batch size. 24 | dtype: dtype 25 | The type of input values. By default, tf.float32. 26 | name : None or str 27 | A unique layer name. 28 | 29 | """ 30 | 31 | def __init__(self, shape, dtype=tf.float32, name=None): #'input'): 32 | # super(InputLayer, self).__init__(prev_layer=inputs, name=name) 33 | super(_InputLayer, self).__init__(name) 34 | 35 | if isinstance(dtype, str): 36 | try: 37 | dtype = eval(dtype) 38 | except Exception as e: 39 | raise RuntimeError("%s is not a valid dtype for InputLayer." % (dtype)) 40 | if not isinstance(dtype, tf.DType): 41 | raise RuntimeError("%s is not a valid dtype for InputLayer." % (dtype)) 42 | 43 | logging.info("Input %s: %s" % (self.name, str(shape))) 44 | self.shape = shape # shape is needed in __repr__ 45 | 46 | shape_without_none = [_ if _ is not None else 1 for _ in shape] 47 | # self.outputs = self.forward(tl.initializers.random_normal()(shape_without_none)) 48 | outputs = self.forward(tl.initializers.ones()(shape_without_none, dtype=dtype)) 49 | 50 | self._built = True 51 | 52 | self._add_node(outputs, outputs) 53 | 54 | def __repr__(self): 55 | s = 'Input(shape=%s' % str(self.shape) 56 | if self.name is not None: 57 | s += (', name=\'%s\'' % self.name) 58 | s += ')' 59 | return s 60 | 61 | def __call__(self, inputs, *args, **kwargs): 62 | return super(_InputLayer, self).__call__(inputs) 63 | 64 | def build(self, inputs_shape): 65 | pass 66 | 67 | def forward(self, inputs): 68 | return inputs 69 | 70 | 71 | def Input(shape, dtype=tf.float32, name=None): 72 | """ 73 | The :class:`Input` class is the starting layer of a neural network. 74 | 75 | Parameters 76 | ---------- 77 | shape : tuple (int) 78 | Including batch size. 79 | name : None or str 80 | A unique layer name. 81 | 82 | """ 83 | input_layer = _InputLayer(shape, dtype=dtype, name=name) 84 | outputs = input_layer._nodes[0].out_tensors[0] 85 | return outputs 86 | -------------------------------------------------------------------------------- /tensorlayer/layers/noise.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorflow as tf 5 | 6 | import tensorlayer as tl 7 | from tensorlayer import logging 8 | from tensorlayer.decorators import deprecated_alias 9 | from tensorlayer.layers.core import Layer 10 | 11 | __all__ = [ 12 | 'GaussianNoise', 13 | ] 14 | 15 | 16 | class GaussianNoise(Layer): 17 | """ 18 | The :class:`GaussianNoise` class is noise layer that adding noise with 19 | gaussian distribution to the activation. 20 | 21 | Parameters 22 | ------------ 23 | mean : float 24 | The mean. Default is 0.0. 25 | stddev : float 26 | The standard deviation. Default is 1.0. 27 | is_always : boolean 28 | Is True, add noise for train and eval mode. If False, skip this layer in eval mode. 29 | seed : int or None 30 | The seed for random noise. 31 | name : str 32 | A unique layer name. 33 | 34 | Examples 35 | -------- 36 | With TensorLayer 37 | 38 | >>> net = tl.layers.Input([64, 200], name='input') 39 | >>> net = tl.layers.Dense(n_units=100, act=tf.nn.relu, name='dense')(net) 40 | >>> gaussianlayer = tl.layers.GaussianNoise(name='gaussian')(net) 41 | >>> print(gaussianlayer) 42 | >>> output shape : (64, 100) 43 | 44 | """ 45 | 46 | def __init__( 47 | self, 48 | mean=0.0, 49 | stddev=1.0, 50 | is_always=True, 51 | seed=None, 52 | name=None, # 'gaussian_noise', 53 | ): 54 | super().__init__(name) 55 | self.mean = mean 56 | self.stddev = stddev 57 | self.seed = seed 58 | self.is_always = is_always 59 | 60 | self.build() 61 | self._built = True 62 | 63 | logging.info("GaussianNoise %s: mean: %f stddev: %f" % (self.name, self.mean, self.stddev)) 64 | 65 | def __repr__(self): 66 | s = '{classname}(mean={mean}, stddev={stddev}' 67 | if self.name is not None: 68 | s += ', name=\'{name}\'' 69 | s += ')' 70 | return s.format(classname=self.__class__.__name__, **self.__dict__) 71 | 72 | def build(self, inputs=None): 73 | pass 74 | 75 | def forward(self, inputs): 76 | if (self.is_train or self.is_always) is False: 77 | return inputs 78 | else: 79 | # noise = np.random.normal(0.0 , sigma , tf.to_int64(self.inputs).get_shape()) 80 | noise = tf.random.normal(shape=inputs.get_shape(), mean=self.mean, stddev=self.stddev, seed=self.seed) 81 | outputs = inputs + noise 82 | return outputs 83 | -------------------------------------------------------------------------------- /tensorlayer/layers/quantize.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorflow as tf 5 | 6 | from tensorlayer import logging 7 | from tensorlayer.decorators import deprecated_alias 8 | from tensorlayer.layers.core import Layer 9 | from tensorlayer.layers.utils import quantize 10 | 11 | __all__ = [ 12 | 'Sign', 13 | ] 14 | 15 | 16 | class Sign(Layer): 17 | """The :class:`SignLayer` class is for quantizing the layer outputs to -1 or 1 while inferencing. 18 | 19 | Parameters 20 | ---------- 21 | name : a str 22 | A unique layer name. 23 | 24 | """ 25 | 26 | # @deprecated_alias(layer='prev_layer', end_support_version=1.9) # TODO remove this line for the 1.9 release 27 | def __init__( 28 | self, 29 | name=None # 'sign', 30 | ): 31 | super().__init__(name) 32 | logging.info("Sign %s" % self.name) 33 | 34 | self.build() 35 | self._built = True 36 | 37 | def build(self, inputs_shape=None): 38 | pass 39 | 40 | def __repr__(self): 41 | s = ('{classname}(') 42 | if self.name is not None: 43 | s += ', name=\'{name}\'' 44 | s += ')' 45 | return s.format(classname=self.__class__.__name__, **self.__dict__) 46 | 47 | def forward(self, inputs): 48 | # with tf.variable_scope(name): 49 | ## self.outputs = tl.act.sign(self.inputs) 50 | # self.outputs = quantize(self.inputs) 51 | outputs = quantize(inputs) 52 | return outputs 53 | -------------------------------------------------------------------------------- /tensorlayer/layers/scale.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import tensorflow as tf 5 | 6 | from tensorlayer import logging 7 | from tensorlayer.initializers import constant 8 | from tensorlayer.layers.core import Layer 9 | 10 | __all__ = [ 11 | 'Scale', 12 | ] 13 | 14 | 15 | class Scale(Layer): 16 | """The :class:`Scale` class is to multiple a trainable scale value to the layer outputs. Usually be used on the output of binary net. 17 | 18 | Parameters 19 | ---------- 20 | init_scale : float 21 | The initial value for the scale factor. 22 | name : a str 23 | A unique layer name. 24 | 25 | Examples 26 | ---------- 27 | >>> inputs = tl.layers.Input([8, 3]) 28 | >>> dense = tl.layers.Dense(n_units=10)(inputs) 29 | >>> outputs = tl.layers.Scale(init_scale=0.5)(dense) 30 | >>> model = tl.models.Model(inputs=inputs, outputs=[dense, outputs]) 31 | >>> dense_out, scale_out = model(data, is_train=True) 32 | 33 | """ 34 | 35 | def __init__( 36 | self, 37 | init_scale=0.05, 38 | name='scale', 39 | ): 40 | super(Scale, self).__init__(name) 41 | self.init_scale = init_scale 42 | 43 | self.build((None, )) 44 | self._built = True 45 | 46 | logging.info("Scale %s: init_scale: %f" % (self.name, self.init_scale)) 47 | 48 | def __repr__(self): 49 | s = '{classname}(' 50 | s += 'init_scale={init_scale},' 51 | s += 'name={name}' 52 | s += ")" 53 | return s.format(classname=self.__class__.__name__, **self.__dict__) 54 | 55 | def build(self, inputs_shape): 56 | self.scale = self._get_weights("scale", shape=[1], init=constant(value=self.init_scale)) 57 | 58 | # @tf.function 59 | def forward(self, inputs): 60 | outputs = inputs * self.scale 61 | return outputs 62 | -------------------------------------------------------------------------------- /tensorlayer/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.nn.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from tensorlayer.lazy_imports import LazyImport 13 | 14 | from .tl_logging import * 15 | 16 | # Lazy Imports 17 | contrib = LazyImport("tensorlayer.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 | -------------------------------------------------------------------------------- /tensorlayer/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.nn.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .hyperdash import * 13 | -------------------------------------------------------------------------------- /tensorlayer/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 tensorlayer as tl 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 | tl.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 | -------------------------------------------------------------------------------- /tensorlayer/models/__init__.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | # """A collections of pre-defined well known models.""" 5 | 6 | from .core import * 7 | from .mobilenetv1 import MobileNetV1 8 | from .resnet import ResNet50 9 | from .seq2seq import Seq2seq 10 | from .seq2seq_with_attention import Seq2seqLuongAttention 11 | from .squeezenetv1 import SqueezeNetV1 12 | from .vgg import * 13 | -------------------------------------------------------------------------------- /tensorlayer/optimizers/__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.nn.lrn`` on ``network.outputs``. 9 | More functions can be found in `TensorFlow API `__. 10 | """ 11 | 12 | from .amsgrad import AMSGrad 13 | -------------------------------------------------------------------------------- /tensorlayer/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 = 2 6 | MINOR = 2 7 | PATCH = 4 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__ = 'tensorlayer' 16 | __contact_names__ = 'TensorLayer Contributors' 17 | __contact_emails__ = 'tensorlayer@gmail.com' 18 | __homepage__ = 'http://tensorlayer.readthedocs.io/en/latest/' 19 | __repository_url__ = 'https://github.com/tensorlayer/tensorlayer' 20 | __download_url__ = 'https://github.com/tensorlayer/tensorlayer' 21 | __description__ = 'High Level Tensorflow 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, tensorflow' 25 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/__init__.py -------------------------------------------------------------------------------- /tests/files/model_basic.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/files/model_basic.h5 -------------------------------------------------------------------------------- /tests/layers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/layers/__init__.py -------------------------------------------------------------------------------- /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 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tensorlayer.layers import * 11 | from tensorlayer.models import * 12 | from tests.utils import CustomTestCase 13 | 14 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 15 | 16 | 17 | class Layer_Convolution_2D_Test(CustomTestCase): 18 | 19 | @classmethod 20 | def setUpClass(cls): 21 | print("\n#################################") 22 | 23 | cls.batch_size = 5 24 | cls.inputs_shape = [cls.batch_size, 10, 10, 16] 25 | cls.input_layer = Input(cls.inputs_shape, name='input_layer') 26 | 27 | cls.offset1 = tl.layers.Conv2d(n_filter=18, filter_size=(3, 3), strides=(1, 1), padding='SAME', 28 | name='offset1')(cls.input_layer) 29 | cls.deformconv1 = tl.layers.DeformableConv2d( 30 | offset_layer=cls.offset1, n_filter=32, filter_size=(3, 3), act=tf.nn.relu, name='deformable1' 31 | )(cls.input_layer) 32 | cls.offset2 = tl.layers.Conv2d(n_filter=18, filter_size=(3, 3), strides=(1, 1), padding='SAME', 33 | name='offset2')(cls.deformconv1) 34 | cls.deformconv2 = tl.layers.DeformableConv2d( 35 | offset_layer=cls.offset2, n_filter=64, filter_size=(3, 3), act=tf.nn.relu, name='deformable2' 36 | )(cls.deformconv1) 37 | 38 | cls.model = Model(cls.input_layer, cls.deformconv2) 39 | print("Testing Deformable Conv2d model: \n", cls.model) 40 | 41 | @classmethod 42 | def tearDownClass(cls): 43 | pass 44 | 45 | def test_layer_n1(self): 46 | 47 | self.assertEqual(len(self.deformconv1._info[0].layer.all_weights), 2) 48 | self.assertEqual(self.deformconv1.get_shape().as_list()[1:], [10, 10, 32]) 49 | 50 | def test_layer_n2(self): 51 | 52 | self.assertEqual(len(self.deformconv2._info[0].layer.all_weights), 2) 53 | self.assertEqual(self.deformconv2.get_shape().as_list()[1:], [10, 10, 64]) 54 | 55 | 56 | if __name__ == '__main__': 57 | 58 | tl.logging.set_verbosity(tl.logging.DEBUG) 59 | 60 | unittest.main() 61 | -------------------------------------------------------------------------------- /tests/layers/test_layers_extend.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Extend_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | pass 20 | 21 | @classmethod 22 | def tearDownClass(cls): 23 | pass 24 | 25 | def test_expand_dims(self): 26 | x = tl.layers.Input([8, 3]) 27 | expandlayer = tl.layers.ExpandDims(axis=-1) 28 | y = expandlayer(x) 29 | print(expandlayer) 30 | self.assertEqual(y.get_shape().as_list(), [8, 3, 1]) 31 | 32 | def test_tile(self): 33 | x = tl.layers.Input([8, 3]) 34 | tilelayer = tl.layers.Tile(multiples=[2, 3]) 35 | y = tilelayer(x) 36 | print(tilelayer) 37 | self.assertEqual(y.get_shape().as_list(), [16, 9]) 38 | 39 | 40 | if __name__ == '__main__': 41 | 42 | unittest.main() 43 | -------------------------------------------------------------------------------- /tests/layers/test_layers_merge.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Layer_Merge_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | pass 21 | 22 | @classmethod 23 | def tearDownClass(cls): 24 | pass 25 | 26 | def test_concat(self): 27 | 28 | class CustomModel(tl.models.Model): 29 | 30 | def __init__(self): 31 | super(CustomModel, self).__init__() 32 | self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1') 33 | self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1') 34 | self.concat = tl.layers.Concat(concat_dim=1, name='concat_layer') 35 | 36 | def forward(self, inputs): 37 | d1 = self.dense1(inputs) 38 | d2 = self.dense2(inputs) 39 | outputs = self.concat([d1, d2]) 40 | return outputs 41 | 42 | model = CustomModel() 43 | model.train() 44 | inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) 45 | outputs = model(inputs) 46 | print(model) 47 | 48 | self.assertEqual(outputs.get_shape().as_list(), [4, 20]) 49 | 50 | def test_elementwise(self): 51 | 52 | class CustomModel(tl.models.Model): 53 | 54 | def __init__(self): 55 | super(CustomModel, self).__init__() 56 | self.dense1 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu1_1') 57 | self.dense2 = tl.layers.Dense(in_channels=20, n_units=10, act=tf.nn.relu, name='relu2_1') 58 | self.element = tl.layers.Elementwise(combine_fn=tf.minimum, name='minimum', act=tf.identity) 59 | 60 | def forward(self, inputs): 61 | d1 = self.dense1(inputs) 62 | d2 = self.dense2(inputs) 63 | outputs = self.element([d1, d2]) 64 | return outputs, d1, d2 65 | 66 | model = CustomModel() 67 | model.train() 68 | inputs = tf.convert_to_tensor(np.random.random([4, 20]).astype(np.float32)) 69 | outputs, d1, d2 = model(inputs) 70 | print(model) 71 | 72 | min = tf.minimum(d1, d2) 73 | self.assertEqual(outputs.get_shape().as_list(), [4, 10]) 74 | self.assertTrue(np.array_equal(min.numpy(), outputs.numpy())) 75 | 76 | 77 | if __name__ == '__main__': 78 | 79 | unittest.main() 80 | -------------------------------------------------------------------------------- /tests/layers/test_layers_noise.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tensorlayer.layers import * 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Layer_Convolution_1D_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | print("\n#################################") 21 | 22 | cls.batch_size = 8 23 | cls.inputs_shape = [cls.batch_size, 200] 24 | cls.input_layer = Input(cls.inputs_shape, name='input_layer') 25 | 26 | cls.dense = tl.layers.Dense(n_units=100, act=tf.nn.relu, in_channels=200)(cls.input_layer) 27 | 28 | cls.noiselayer = tl.layers.GaussianNoise(name='gaussian')(cls.dense) 29 | 30 | print("Testing GaussianNoise: \n", cls.noiselayer._info[0].layer) 31 | 32 | @classmethod 33 | def tearDownClass(cls): 34 | pass 35 | 36 | def test_layer_n1(self): 37 | self.assertEqual(self.noiselayer.get_shape().as_list()[1:], [100]) 38 | 39 | 40 | if __name__ == '__main__': 41 | 42 | tl.logging.set_verbosity(tl.logging.DEBUG) 43 | 44 | unittest.main() 45 | -------------------------------------------------------------------------------- /tests/layers/test_layers_resampling.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import sys 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tensorlayer.layers import * 11 | from tests.utils import CustomTestCase 12 | 13 | sys.path.append("/home/wurundi/workspace/tensorlayer2") 14 | 15 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 16 | 17 | 18 | class Layer_Pooling_Test(CustomTestCase): 19 | 20 | @classmethod 21 | def setUpClass(cls): 22 | 23 | ## 1D ======================================================================== 24 | 25 | ## 2D ======================================================================== 26 | 27 | x_2_input_shape = [None, 100, 100, 3] 28 | nin_2 = Input(x_2_input_shape) 29 | 30 | n6 = tl.layers.Conv2d(n_filter=32, filter_size=(3, 3), strides=(2, 2), name='test_conv2d')(nin_2) 31 | 32 | n7 = tl.layers.UpSampling2d(scale=(2, 2), name='test_UpSampling2d_1')(n6) 33 | 34 | n8 = tl.layers.UpSampling2d(scale=3, name='test_UpSampling2d_2')(n6) 35 | 36 | n9 = tl.layers.DownSampling2d(scale=(2, 2), name='test_DownSampling2d_1')(n6) 37 | 38 | n10 = tl.layers.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 | print("Printing UpSampling2d") 47 | print(nin_2._info[0].layer) 48 | print(n6._info[0].layer) 49 | print(n7._info[0].layer) 50 | print(n8._info[0].layer) 51 | print(n9._info[0].layer) 52 | print(n10._info[0].layer) 53 | 54 | @classmethod 55 | def tearDownClass(cls): 56 | pass 57 | # tf.reset_default_graph() 58 | 59 | def test_UpSampling2d(self): 60 | self.assertEqual(self.n7_shape[1:3], [100, 100]) 61 | self.assertEqual(self.n8_shape[1:3], [150, 150]) 62 | 63 | try: 64 | layer = tl.layers.UpSampling2d(scale=(2, 2, 2)) 65 | except Exception as e: 66 | print(e) 67 | 68 | def test_DownSampling2d(self): 69 | self.assertEqual(self.n9_shape[1:3], [25, 25]) 70 | self.assertEqual(self.n10_shape[1:3], [10, 10]) 71 | 72 | try: 73 | layer = tl.layers.DownSampling2d(scale=(2, 2, 2)) 74 | except Exception as e: 75 | print(e) 76 | 77 | 78 | if __name__ == '__main__': 79 | 80 | tl.logging.set_verbosity(tl.logging.DEBUG) 81 | 82 | unittest.main() 83 | -------------------------------------------------------------------------------- /tests/layers/test_layers_scale.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Layer_Scale_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | pass 21 | 22 | @classmethod 23 | def tearDownClass(cls): 24 | pass 25 | 26 | def test_scale(self): 27 | inputs = tl.layers.Input([8, 3]) 28 | dense = tl.layers.Dense(n_units=10)(inputs) 29 | scalelayer = tl.layers.Scale(init_scale=0.5) 30 | outputs = scalelayer(dense) 31 | model = tl.models.Model(inputs=inputs, outputs=[dense, outputs]) 32 | 33 | print(scalelayer) 34 | 35 | data = np.random.random(size=[8, 3]).astype(np.float32) 36 | dout, fout = model(data, is_train=True) 37 | 38 | for i in range(len(dout)): 39 | for j in range(len(dout[i])): 40 | self.assertEqual(dout[i][j].numpy() * 0.5, fout[i][j].numpy()) 41 | 42 | 43 | if __name__ == '__main__': 44 | 45 | unittest.main() 46 | -------------------------------------------------------------------------------- /tests/layers/test_layers_stack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | import os 4 | import unittest 5 | 6 | import tensorflow as tf 7 | 8 | import tensorlayer as tl 9 | from tensorlayer.layers import * 10 | from tensorlayer.models import * 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Layer_Stack_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | print("-" * 20, "Layer_Stack_Test", "-" * 20) 21 | cls.batch_size = 4 22 | cls.inputs_shape = [cls.batch_size, 10] 23 | 24 | cls.ni = Input(cls.inputs_shape, name='input_layer') 25 | a = Dense(n_units=5)(cls.ni) 26 | b = Dense(n_units=5)(cls.ni) 27 | cls.layer1 = Stack(axis=1) 28 | cls.n1 = cls.layer1([a, b]) 29 | cls.M = Model(inputs=cls.ni, outputs=cls.n1) 30 | 31 | cls.inputs = tf.random.uniform(cls.inputs_shape) 32 | cls.n2 = cls.M(cls.inputs, is_train=True) 33 | 34 | print(cls.layer1) 35 | 36 | @classmethod 37 | def tearDownClass(cls): 38 | pass 39 | 40 | def test_layer_n1(self): 41 | self.assertEqual(self.n1.shape, (4, 2, 5)) 42 | 43 | def test_layer_n2(self): 44 | self.assertEqual(self.n2.shape, (4, 2, 5)) 45 | 46 | 47 | class Layer_UnStack_Test(CustomTestCase): 48 | 49 | @classmethod 50 | def setUpClass(cls): 51 | print("-" * 20, "Layer_UnStack_Test", "-" * 20) 52 | cls.batch_size = 4 53 | cls.inputs_shape = [cls.batch_size, 10] 54 | 55 | cls.ni = Input(cls.inputs_shape, name='input_layer') 56 | a = Dense(n_units=5)(cls.ni) 57 | cls.layer1 = UnStack(axis=1) # unstack in channel axis 58 | cls.n1 = cls.layer1(a) 59 | cls.M = Model(inputs=cls.ni, outputs=cls.n1) 60 | 61 | cls.inputs = tf.random.uniform(cls.inputs_shape) 62 | cls.n2 = cls.M(cls.inputs, is_train=True) 63 | 64 | print(cls.layer1) 65 | 66 | @classmethod 67 | def tearDownClass(cls): 68 | pass 69 | 70 | def test_layer_n1(self): 71 | self.assertEqual(len(self.n1), 5) 72 | self.assertEqual(self.n1[0].shape, (self.batch_size, )) 73 | 74 | def test_layer_n2(self): 75 | self.assertEqual(len(self.n2), 5) 76 | self.assertEqual(self.n1[0].shape, (self.batch_size, )) 77 | 78 | 79 | if __name__ == '__main__': 80 | 81 | tl.logging.set_verbosity(tl.logging.DEBUG) 82 | 83 | unittest.main() 84 | -------------------------------------------------------------------------------- /tests/models/test_keras_save.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.python.keras import Model 3 | from tensorflow.python.keras.applications import VGG16 4 | from tensorflow.python.keras.layers import Conv2D, Dense 5 | from tensorflow.python.training import saver 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/pending/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/pending/__init__.py -------------------------------------------------------------------------------- /tests/pending/test_decorators.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tensorlayer.decorators import private_method 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Layer_Pooling_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | 21 | class MyClass(object): 22 | 23 | @private_method 24 | def _private_func(self): 25 | tl.logging.debug("I am private") 26 | 27 | def public_func(self): 28 | tl.logging.debug("I am public and calling now the private func") 29 | self._private_func() 30 | 31 | cls.my_object = MyClass() 32 | 33 | def test_call_from_public_method(self): 34 | with self.assertNotRaises(RuntimeError): 35 | self.my_object.public_func() 36 | 37 | def test_call_private_method(self): 38 | with self.assertRaises(RuntimeError): 39 | self.my_object._private_func() 40 | 41 | 42 | if __name__ == '__main__': 43 | 44 | tf.logging.set_verbosity(tf.logging.DEBUG) 45 | tl.logging.set_verbosity(tl.logging.DEBUG) 46 | 47 | unittest.main() 48 | -------------------------------------------------------------------------------- /tests/pending/test_documentation.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | from sphinx.application import Sphinx 8 | 9 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 10 | 11 | 12 | class DocTest(unittest.TestCase): 13 | source_dir = u'docs/' 14 | config_dir = u'docs/' 15 | output_dir = u'docs/test_build' 16 | doctree_dir = u'docs/test_build/doctrees' 17 | 18 | all_files = True 19 | 20 | @classmethod 21 | def setUpClass(cls): 22 | os.environ["SPHINXBUILD"] = "1" 23 | 24 | def test_html_documentation(self): 25 | app = Sphinx( 26 | self.source_dir, 27 | self.config_dir, 28 | self.output_dir, 29 | self.doctree_dir, 30 | buildername='html', 31 | warningiserror=True, 32 | ) 33 | app.build(force_all=self.all_files) 34 | # TODO: additional checks here if needed 35 | 36 | def test_text_documentation(self): 37 | # The same, but with different buildername 38 | app = Sphinx( 39 | self.source_dir, 40 | self.config_dir, 41 | self.output_dir, 42 | self.doctree_dir, 43 | buildername='text', 44 | warningiserror=False, 45 | ) 46 | app.build(force_all=self.all_files) 47 | # TODO: additional checks if needed 48 | 49 | def tearDown(self): 50 | # TODO: clean up the output directory 51 | pass 52 | 53 | 54 | if __name__ == '__main__': 55 | unittest.main() 56 | -------------------------------------------------------------------------------- /tests/pending/test_layers_basic.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Basic_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | 20 | x = tf.placeholder(tf.float32, [None, 100]) 21 | 22 | n = tl.layers.InputLayer(x, name='in') 23 | n = tl.layers.DenseLayer(n, n_units=80, name='d1') 24 | n = tl.layers.DenseLayer(n, n_units=80, name='d2') 25 | 26 | n.print_layers() 27 | n.print_params(False) 28 | 29 | n2 = n[:, :30] 30 | n2.print_layers() 31 | 32 | cls.n_params = n.count_params() 33 | cls.all_layers = n.all_layers 34 | cls.all_params = n.all_params 35 | cls.shape_n = n.outputs.get_shape().as_list() 36 | 37 | cls.shape_n2 = n2.outputs.get_shape().as_list() 38 | 39 | @classmethod 40 | def tearDownClass(cls): 41 | tf.reset_default_graph() 42 | 43 | def test_n_params(self): 44 | self.assertEqual(self.n_params, 14560) 45 | 46 | def test_shape_n(self): 47 | self.assertEqual(self.shape_n[-1], 80) 48 | 49 | def test_all_layers(self): 50 | self.assertEqual(len(self.all_layers), 3) 51 | 52 | def test_all_params(self): 53 | self.assertEqual(len(self.all_params), 4) 54 | 55 | def test_shape_n2(self): 56 | self.assertEqual(self.shape_n2[-1], 30) 57 | 58 | 59 | if __name__ == '__main__': 60 | 61 | tf.logging.set_verbosity(tf.logging.DEBUG) 62 | tl.logging.set_verbosity(tl.logging.DEBUG) 63 | 64 | unittest.main() 65 | -------------------------------------------------------------------------------- /tests/pending/test_layers_flow_control.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Flow_Control_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | x = tf.placeholder(tf.float32, shape=(None, 784), name='x') 20 | 21 | # define the network 22 | net_in = tl.layers.InputLayer(x, name='in') 23 | net_in = tl.layers.DropoutLayer(net_in, keep=0.8, name='in/drop') 24 | # net 0 25 | net_0 = tl.layers.DenseLayer(net_in, n_units=800, act=tf.nn.relu, name='net0/relu1') 26 | net_0 = tl.layers.DropoutLayer(net_0, keep=0.5, name='net0/drop1') 27 | net_0 = tl.layers.DenseLayer(net_0, n_units=800, act=tf.nn.relu, name='net0/relu2') 28 | # net 1 29 | net_1 = tl.layers.DenseLayer(net_in, n_units=800, act=tf.nn.relu, name='net1/relu1') 30 | net_1 = tl.layers.DropoutLayer(net_1, keep=0.8, name='net1/drop1') 31 | net_1 = tl.layers.DenseLayer(net_1, n_units=800, act=tf.nn.relu, name='net1/relu2') 32 | net_1 = tl.layers.DropoutLayer(net_1, keep=0.8, name='net1/drop2') 33 | net_1 = tl.layers.DenseLayer(net_1, n_units=800, act=tf.nn.relu, name='net1/relu3') 34 | # multiplexer 35 | net_mux = tl.layers.MultiplexerLayer(layers=[net_0, net_1], name='mux') 36 | network = tl.layers.ReshapeLayer(net_mux, shape=(-1, 800), name='reshape') 37 | network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3') 38 | # output layer 39 | network = tl.layers.DenseLayer(network, n_units=10, name='output') 40 | 41 | network.print_layers() 42 | network.print_params(False) 43 | 44 | cls.net_shape = network.outputs.get_shape().as_list() 45 | cls.net_layers = network.all_layers 46 | cls.net_params = network.all_params 47 | cls.net_all_drop = network.all_drop 48 | cls.net_n_params = network.count_params() 49 | 50 | @classmethod 51 | def tearDownClass(cls): 52 | tf.reset_default_graph() 53 | 54 | def test_net_shape(self): 55 | self.assertEqual(self.net_shape[-1], 10) 56 | 57 | def test_net_layers(self): 58 | self.assertEqual(len(self.net_layers), 14) 59 | 60 | def test_net_params(self): 61 | self.assertEqual(len(self.net_params), 12) 62 | 63 | def test_net_all_drop(self): 64 | self.assertEqual(len(self.net_all_drop), 5) 65 | 66 | def test_net_n_params(self): 67 | self.assertEqual(self.net_n_params, 3186410) 68 | 69 | 70 | if __name__ == '__main__': 71 | 72 | tf.logging.set_verbosity(tf.logging.DEBUG) 73 | tl.logging.set_verbosity(tl.logging.DEBUG) 74 | 75 | unittest.main() 76 | -------------------------------------------------------------------------------- /tests/pending/test_layers_stack.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Stack_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | 20 | x = tf.placeholder(tf.float32, shape=[None, 30]) 21 | net_in = tl.layers.InputLayer(x, name='input') 22 | 23 | net_d1 = tl.layers.DenseLayer(net_in, n_units=10, name='dense1') 24 | net_d2 = tl.layers.DenseLayer(net_in, n_units=10, name='dense2') 25 | net_d3 = tl.layers.DenseLayer(net_in, n_units=10, name='dense3') 26 | 27 | cls.net_stack = tl.layers.StackLayer([net_d1, net_d2, net_d3], axis=1, name='stack') 28 | 29 | cls.net_stack.print_layers() 30 | cls.net_stack.print_params(False) 31 | 32 | cls.net_unstack = tl.layers.UnStackLayer(cls.net_stack, axis=1, name='unstack') 33 | 34 | cls.net_unstack.print_layers() 35 | cls.net_unstack.print_params(False) 36 | 37 | @classmethod 38 | def tearDownClass(cls): 39 | tf.reset_default_graph() 40 | 41 | def test_StackLayer(self): 42 | self.assertEqual(self.net_stack.outputs.get_shape().as_list()[-1], 10) 43 | self.assertEqual(len(self.net_stack.all_layers), 5) 44 | self.assertEqual(len(self.net_stack.all_params), 6) 45 | self.assertEqual(self.net_stack.count_params(), 930) 46 | 47 | def test_UnStackLayer(self): 48 | 49 | for n in self.net_unstack.outputs: 50 | shape = n.outputs.get_shape().as_list() 51 | 52 | self.assertEqual(shape[-1], 10) 53 | self.assertEqual(len(n.all_layers), 5) 54 | self.assertEqual(len(n.all_params), 6) 55 | self.assertEqual(n.count_params(), 930) 56 | 57 | 58 | if __name__ == '__main__': 59 | 60 | tf.logging.set_verbosity(tf.logging.DEBUG) 61 | tl.logging.set_verbosity(tl.logging.DEBUG) 62 | 63 | unittest.main() 64 | -------------------------------------------------------------------------------- /tests/pending/test_layers_super_resolution.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Super_Resolution_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | t_signal = tf.placeholder('float32', [10, 100, 4], name='x') 20 | n = tl.layers.InputLayer(t_signal, name='in') 21 | n = tl.layers.Conv1d(n, n_filter=32, filter_size=3, stride=1, padding='SAME', name='conv1d') 22 | net1 = tl.layers.SubpixelConv1d(n, scale=2, name='subpixel') 23 | 24 | net1.print_layers() 25 | net1.print_params(False) 26 | 27 | cls.net1_shape = net1.outputs.get_shape().as_list() 28 | cls.net1_layers = net1.all_layers 29 | cls.net1_params = net1.all_params 30 | cls.net1_n_params = net1.count_params() 31 | 32 | ## 2D 33 | x = tf.placeholder('float32', [10, 100, 100, 3], name='x') 34 | n = tl.layers.InputLayer(x, name='in') 35 | n = tl.layers.Conv2d(n, n_filter=32, filter_size=(3, 2), strides=(1, 1), padding='SAME', name='conv2d') 36 | net2 = tl.layers.SubpixelConv2d(n, scale=2, name='subpixel2d') 37 | 38 | net2.print_layers() 39 | net2.print_params(False) 40 | 41 | cls.net2_shape = net2.outputs.get_shape().as_list() 42 | cls.net2_layers = net2.all_layers 43 | cls.net2_params = net2.all_params 44 | cls.net2_n_params = net2.count_params() 45 | 46 | @classmethod 47 | def tearDownClass(cls): 48 | tf.reset_default_graph() 49 | 50 | def test_net1_shape(self): 51 | self.assertEqual(self.net1_shape, [10, 200, 16]) 52 | self.assertEqual(len(self.net1_layers), 3) 53 | self.assertEqual(len(self.net1_params), 2) 54 | self.assertEqual(self.net1_n_params, 416) 55 | 56 | def test_net2_shape(self): 57 | self.assertEqual(self.net2_shape, [10, 200, 200, 8]) 58 | self.assertEqual(len(self.net2_layers), 3) 59 | self.assertEqual(len(self.net2_params), 2) 60 | self.assertEqual(self.net2_n_params, 608) 61 | 62 | 63 | if __name__ == '__main__': 64 | 65 | tf.logging.set_verbosity(tf.logging.DEBUG) 66 | tl.logging.set_verbosity(tl.logging.DEBUG) 67 | 68 | unittest.main() 69 | -------------------------------------------------------------------------------- /tests/pending/test_layers_time_distributed.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | def model(x, is_train=True, reuse=False, name_scope="env1"): 16 | with tf.variable_scope(name_scope, reuse=reuse): 17 | net = tl.layers.InputLayer(x, name='input') 18 | net = tl.layers.TimeDistributedLayer( 19 | net, layer_class=tl.layers.DenseLayer, args={ 20 | 'n_units': 50, 21 | 'name': 'dense' 22 | }, name='time_dense' 23 | ) 24 | return net 25 | 26 | 27 | class Layer_Time_Distributed_Test(CustomTestCase): 28 | 29 | @classmethod 30 | def setUpClass(cls): 31 | 32 | batch_size = 32 33 | timestep = 20 34 | input_dim = 100 35 | 36 | cls.x = tf.placeholder(dtype=tf.float32, shape=[batch_size, timestep, input_dim], name="encode_seqs") 37 | net = model(cls.x, is_train=True, reuse=False) 38 | 39 | cls.net_shape = net.outputs.get_shape().as_list() 40 | cls.n_params = net.count_params() 41 | net.print_params(False) 42 | 43 | @classmethod 44 | def tearDownClass(cls): 45 | tf.reset_default_graph() 46 | 47 | def test_net_shape(self): 48 | self.assertEqual(self.net_shape, [32, 20, 50]) 49 | 50 | def test_net_n_params(self): 51 | self.assertEqual(self.n_params, 5050) 52 | 53 | def test_reuse(self): 54 | 55 | with self.assertNotRaises(Exception): 56 | model(self.x, is_train=True, reuse=False, name_scope="env2") 57 | model(self.x, is_train=False, reuse=True, name_scope="env2") 58 | 59 | with self.assertRaises(Exception): 60 | model(self.x, is_train=True, reuse=False) # Already defined model with the same var_scope 61 | 62 | 63 | if __name__ == '__main__': 64 | 65 | tf.logging.set_verbosity(tf.logging.DEBUG) 66 | tl.logging.set_verbosity(tl.logging.DEBUG) 67 | 68 | unittest.main() 69 | -------------------------------------------------------------------------------- /tests/pending/test_logging.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class TL_Logger_Test(CustomTestCase): 16 | 17 | def test_debug(self): 18 | with self.assertNotRaises(Exception): 19 | tl.logging.debug("This is a debug message") 20 | 21 | def test_error(self): 22 | with self.assertNotRaises(Exception): 23 | tl.logging.error("This is an error message") 24 | 25 | def test_fatal(self): 26 | with self.assertNotRaises(Exception): 27 | tl.logging.fatal("This is a fatal error message") 28 | 29 | def test_info(self): 30 | with self.assertNotRaises(Exception): 31 | tl.logging.info("This is an information message") 32 | 33 | def test_warn(self): 34 | with self.assertNotRaises(Exception): 35 | tl.logging.warn("This is a warning message") 36 | 37 | def test_set_verbosity(self): 38 | with self.assertNotRaises(Exception): 39 | tl.logging.set_verbosity(tl.logging.DEBUG) 40 | tl.logging.set_verbosity(tl.logging.INFO) 41 | tl.logging.set_verbosity(tl.logging.WARN) 42 | tl.logging.set_verbosity(tl.logging.ERROR) 43 | tl.logging.set_verbosity(tl.logging.FATAL) 44 | 45 | def test_get_verbosity(self): 46 | with self.assertNotRaises(Exception): 47 | tl.logging.get_verbosity() 48 | 49 | 50 | if __name__ == '__main__': 51 | 52 | tf.logging.set_verbosity(tf.logging.DEBUG) 53 | tl.logging.set_verbosity(tl.logging.DEBUG) 54 | 55 | unittest.main() 56 | -------------------------------------------------------------------------------- /tests/pending/test_optimizer_amsgrad.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | class Layer_Pooling_Test(CustomTestCase): 16 | 17 | @classmethod 18 | def setUpClass(cls): 19 | cls.x = tf.placeholder(tf.float32, shape=[None, 784], name='x') 20 | cls.y_ = tf.placeholder(tf.int64, shape=[None], name='y_') 21 | 22 | # define the network 23 | cls.network = tl.layers.InputLayer(cls.x, name='input') 24 | cls.network = tl.layers.DropoutLayer(cls.network, keep=0.8, name='drop1') 25 | cls.network = tl.layers.DenseLayer(cls.network, 800, tf.nn.relu, name='relu1') 26 | cls.network = tl.layers.DropoutLayer(cls.network, keep=0.5, name='drop2') 27 | cls.network = tl.layers.DenseLayer(cls.network, 800, tf.nn.relu, name='relu2') 28 | cls.network = tl.layers.DropoutLayer(cls.network, keep=0.5, name='drop3') 29 | 30 | cls.network = tl.layers.DenseLayer(cls.network, n_units=10, name='output') 31 | 32 | # define cost function and metric. 33 | cls.y = cls.network.outputs 34 | cls.cost = tl.cost.cross_entropy(cls.y, cls.y_, name='cost') 35 | 36 | correct_prediction = tf.equal(tf.argmax(cls.y, 1), cls.y_) 37 | 38 | cls.acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 39 | 40 | # define the optimizer 41 | train_params = cls.network.all_params 42 | optimizer = tl.optimizers.AMSGrad(learning_rate=1e-4, beta1=0.9, beta2=0.999, epsilon=1e-8) 43 | cls.train_op = optimizer.minimize(cls.cost, var_list=train_params) 44 | 45 | @classmethod 46 | def tearDownClass(cls): 47 | tf.reset_default_graph() 48 | 49 | def test_training(self): 50 | 51 | with self.assertNotRaises(Exception): 52 | 53 | X_train, y_train, X_val, y_val, _, _ = tl.files.load_mnist_dataset(shape=(-1, 784)) 54 | 55 | with tf.Session() as sess: 56 | # initialize all variables in the session 57 | tl.layers.initialize_global_variables(sess) 58 | 59 | # print network information 60 | self.network.print_params() 61 | self.network.print_layers() 62 | 63 | # train the network 64 | tl.utils.fit( 65 | sess, self.network, self.train_op, self.cost, X_train, y_train, self.x, self.y_, acc=self.acc, 66 | batch_size=500, n_epoch=1, print_freq=1, X_val=X_val, y_val=y_val, eval_train=False 67 | ) 68 | 69 | 70 | if __name__ == '__main__': 71 | 72 | tf.logging.set_verbosity(tf.logging.DEBUG) 73 | tl.logging.set_verbosity(tl.logging.DEBUG) 74 | 75 | unittest.main() 76 | -------------------------------------------------------------------------------- /tests/pending/test_pydocstyle.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | from pydocstyle.checker import check, violations 8 | from tests.utils import list_all_py_files 9 | 10 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 11 | 12 | registry = violations.ErrorRegistry 13 | 14 | 15 | def lookup_error_params(code): 16 | for group in registry.groups: 17 | for error_params in group.errors: 18 | if error_params.code == code: 19 | return error_params 20 | 21 | 22 | class PyDOC_Style_Test(unittest.TestCase): 23 | 24 | @classmethod 25 | def setUpClass(cls): 26 | 27 | cls.violations = list() 28 | 29 | # TODO: fix all violations to make it empty 30 | _disabled_checks = [ 31 | 'D205', # 1 blank line required between summary line and description 32 | 'D102', # Missing docstring in public method 33 | 'D400', # First line should end with a period 34 | 'D100', # Missing docstring in public module 35 | 'D107', # Missing docstring in __init__ 36 | 'D103', # Missing docstring in public function 37 | 'D401', # First line should be in imperative mood 38 | 'D101', # Missing docstring in public class 39 | 'D413', # Missing blank line after last section 40 | 'D105', # Missing docstring in magic method 41 | 'D104', # Missing docstring in public package 42 | 'D302', # Use u"""for Unicode docstrings 43 | 44 | # Rules that has conflict with yapf 45 | 'D202', # No blank lines allowed after function docstring 46 | ] 47 | 48 | for filename in list_all_py_files(): 49 | print(filename) 50 | for err in check([filename]): 51 | if not err.code in _disabled_checks: 52 | cls.violations.append(err) 53 | 54 | def test_violations(self): 55 | if self.violations: 56 | counts = dict() 57 | 58 | for err in self.violations: 59 | counts[err.code] = counts.get(err.code, 0) + 1 60 | print(err) 61 | 62 | for n, code in sorted([(n, code) for code, n in counts.items()], reverse=True): 63 | p = lookup_error_params(code) 64 | print('%s %8d %s' % (code, n, p.short_desc)) 65 | 66 | raise Exception('PyDoc Coding Style: %d violations have been found' % (len(self.violations))) 67 | 68 | 69 | if __name__ == '__main__': 70 | unittest.main() 71 | -------------------------------------------------------------------------------- /tests/pending/test_reuse_mlp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import tensorflow as tf 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | 15 | # define the network 16 | def mlp(x, is_train=True, reuse=False): 17 | with tf.variable_scope("MLP", reuse=reuse): 18 | tl.layers.set_name_reuse(reuse) # print warning 19 | network = tl.layers.InputLayer(x, name='input') 20 | network = tl.layers.DropoutLayer(network, keep=0.8, is_fix=True, is_train=is_train, name='drop1') 21 | network = tl.layers.DenseLayer(network, n_units=800, act=tf.nn.relu, name='relu1') 22 | network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True, is_train=is_train, name='drop2') 23 | network = tl.layers.DenseLayer(network, n_units=800, act=tf.nn.relu, name='relu2') 24 | network = tl.layers.DropoutLayer(network, keep=0.5, is_fix=True, is_train=is_train, name='drop3') 25 | network = tl.layers.DenseLayer(network, n_units=10, name='output') 26 | return network 27 | 28 | 29 | class MLP_Reuse_Test(CustomTestCase): 30 | 31 | @classmethod 32 | def setUpClass(cls): 33 | 34 | # define placeholder 35 | cls.x = tf.placeholder(tf.float32, shape=[None, 784], name='x') 36 | 37 | # define inferences 38 | mlp(cls.x, is_train=True, reuse=False) 39 | mlp(cls.x, is_train=False, reuse=True) 40 | 41 | @classmethod 42 | def tearDownClass(cls): 43 | tf.reset_default_graph() 44 | 45 | def test_reuse(self): 46 | 47 | with self.assertRaises(Exception): 48 | mlp(self.x, is_train=False, reuse=False) # Already defined model with the same var_scope 49 | 50 | 51 | if __name__ == '__main__': 52 | 53 | tf.logging.set_verbosity(tf.logging.DEBUG) 54 | tl.logging.set_verbosity(tl.logging.DEBUG) 55 | 56 | unittest.main() 57 | -------------------------------------------------------------------------------- /tests/pending/test_utils_predict.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import numpy as np 8 | import tensorflow as tf 9 | 10 | import tensorlayer as tl 11 | from tests.utils import CustomTestCase 12 | 13 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 14 | 15 | 16 | class Util_Predict_Test(CustomTestCase): 17 | 18 | @classmethod 19 | def setUpClass(cls): 20 | cls.x1 = tf.placeholder(tf.float32, [None, 5, 5, 3]) 21 | cls.x2 = tf.placeholder(tf.float32, [8, 5, 5, 3]) 22 | cls.X1 = np.ones([127, 5, 5, 3]) 23 | cls.X2 = np.ones([7, 5, 5, 3]) 24 | cls.batch_size = 8 25 | 26 | @classmethod 27 | def tearDownClass(cls): 28 | tf.reset_default_graph() 29 | 30 | def test_case1(self): 31 | with self.assertNotRaises(Exception): 32 | with tf.Session() as sess: 33 | n = tl.layers.InputLayer(self.x1) 34 | y = n.outputs 35 | y_op = tf.nn.softmax(y) 36 | tl.utils.predict(sess, n, self.X1, self.x1, y_op, batch_size=self.batch_size) 37 | sess.close() 38 | 39 | def test_case2(self): 40 | with self.assertRaises(Exception): 41 | with tf.Session() as sess: 42 | n = tl.layers.InputLayer(self.x2) 43 | y = n.outputs 44 | y_op = tf.nn.softmax(y) 45 | tl.utils.predict(sess, n, self.X2, self.x2, y_op, batch_size=self.batch_size) 46 | sess.close() 47 | 48 | 49 | if __name__ == '__main__': 50 | 51 | tf.logging.set_verbosity(tf.logging.DEBUG) 52 | tl.logging.set_verbosity(tl.logging.DEBUG) 53 | 54 | unittest.main() 55 | -------------------------------------------------------------------------------- /tests/pending/test_yapf_format.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | import unittest 6 | 7 | from yapf.yapflib.yapf_api import FormatCode 8 | 9 | from tests.utils import CustomTestCase, list_all_py_files 10 | 11 | 12 | def _read_utf_8_file(filename): 13 | if sys.version_info.major == 2: ## Python 2 specific 14 | with open(filename, 'rb') as f: 15 | return unicode(f.read(), 'utf-8') 16 | else: 17 | with open(filename, encoding='utf-8') as f: 18 | return f.read() 19 | 20 | 21 | class YAPF_Style_Test(CustomTestCase): 22 | 23 | @classmethod 24 | def setUpClass(cls): 25 | 26 | cls.badly_formatted_files = list() 27 | cls.files_2_test = list_all_py_files() 28 | 29 | def test_files_format(self): 30 | 31 | for file in list_all_py_files(): 32 | 33 | try: 34 | 35 | print(file) 36 | code = _read_utf_8_file(file) 37 | 38 | # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module 39 | diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) 40 | 41 | if changed: 42 | print(diff) 43 | self.badly_formatted_files.append(file) 44 | except Exception as e: 45 | print("Error while processing file: `%s`\n" "Error: %s" % (file, str(e))) 46 | 47 | with self.assertNotRaises(Exception): 48 | 49 | str_err = "" 50 | 51 | if self.badly_formatted_files: 52 | for filename in self.badly_formatted_files: 53 | str_err += 'yapf -i --style=setup.cfg %s\n' % filename 54 | 55 | str_err = "\n======================================================================================\n" \ 56 | "Bad Coding Style: %d file(s) need to be formatted, run the following commands to fix: \n%s" \ 57 | "======================================================================================" % ( 58 | len(self.badly_formatted_files), str_err) 59 | 60 | raise Exception(str_err) 61 | 62 | 63 | if __name__ == '__main__': 64 | unittest.main() 65 | -------------------------------------------------------------------------------- /tests/performance_test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/performance_test/__init__.py -------------------------------------------------------------------------------- /tests/performance_test/vgg/README.md: -------------------------------------------------------------------------------- 1 | # Performance Test (VGG16) 2 | 3 | ### Introduction 4 | 5 | This test compares performance of the following libraries: 6 | 7 | 1. TensorLayer v. 2.0 8 | 2. TensorFlow ([tf.keras](https://www.tensorflow.org/api_docs/python/tf/keras)) v. 2.0.0-alpha 9 | 3. [Keras](https://keras.io/) v. 2.2.4 10 | 11 | 12 | 13 | ### With GPU 14 | 15 | __Hardware:__ 16 | 17 | - CPU: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz 40 core 18 | - GPU: TITAN Xp 19 | 20 | __Experiment Settings:__ 21 | - Model: [VGG16](http://www.robots.ox.ac.uk/~vgg/research/very_deep/) 22 | - Batch size: 32 23 | - Number of iterations: 300 24 | 25 | __Results:__ 26 | 27 | | Mode | Lib | Data Format | Max GPU Memory Usage(MB) |Max CPU Memory Usage(MB) | Avg CPU Memory Usage(MB) | Runtime (sec) | 28 | | :-------: | :-------------: | :-----------: | :-----------------: | :-----------------: | :-----------------: | :-----------: | 29 | | AutoGraph | TensorFlow 2.0 | channel last | 11833 | 2161 | 2136 | 74 | 30 | | | Tensorlayer 2.0 | channel last | 11833 | 2187 | 2169 | 76 | 31 | | Graph | Keras | channel last | 8677 | 2580 | 2576 | 101 | 32 | | Eager | TensorFlow 2.0 | channel last | 8723 | 2052 | 2024 | 97 | 33 | | | TensorLayer 2.0 | channel last | 8723 | 2010 | 2007 | 95 | 34 | 35 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tensorlayer/TensorLayer/0681633252667b317a23b803c11a8a060a44bf31/tests/performance_test/vgg/__init__.py -------------------------------------------------------------------------------- /tests/performance_test/vgg/exp_config.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def random_input_generator(num, batchsize=32, format='NHWC'): 5 | input_shape = (batchsize, 224, 224, 3) if format == 'NHWC' else (batchsize, 3, 224, 224) 6 | rng = np.random.RandomState(1234) 7 | for i in range(num): 8 | x = rng.uniform(0.0, 1.0, size=input_shape).astype(np.float32) 9 | y = rng.randint(0, 1000, size=(batchsize, )) 10 | yield (x, y) 11 | 12 | 13 | MONITOR_INTERVAL = 50 14 | NUM_ITERS = 300 15 | BATCH_SIZE = 32 16 | LERANING_RATE = 0.0001 17 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/keras_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | 7 | import keras 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | from keras.applications.vgg16 import VGG16 10 | from keras.backend.tensorflow_backend import set_session 11 | from keras.utils import to_categorical 12 | 13 | config = tf.ConfigProto() 14 | config.gpu_options.allow_growth = True 15 | sess = tf.Session(config=config) 16 | set_session(sess) 17 | 18 | # get the whole model 19 | vgg = VGG16(weights=None) 20 | 21 | # system monitor 22 | info = psutil.virtual_memory() 23 | monitor_interval = MONITOR_INTERVAL 24 | avg_mem_usage = 0 25 | max_mem_usage = 0 26 | count = 0 27 | total_time = 0 28 | 29 | # training setting 30 | num_iter = NUM_ITERS 31 | batch_size = BATCH_SIZE 32 | vgg.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adam(lr=LERANING_RATE)) 33 | 34 | # data generator 35 | gen = random_input_generator(num_iter, batch_size) 36 | 37 | # begin training 38 | 39 | for idx, data in enumerate(gen): 40 | x_batch = data[0] 41 | y_batch = to_categorical(data[1], num_classes=1000) 42 | 43 | start_time = time.time() 44 | 45 | # forward + backward 46 | vgg.train_on_batch(x_batch, y_batch) 47 | 48 | end_time = time.time() 49 | consume_time = end_time - start_time 50 | total_time += consume_time 51 | 52 | if idx % monitor_interval == 0: 53 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 54 | max_mem_usage = max(cur_usage, max_mem_usage) 55 | avg_mem_usage += cur_usage 56 | count += 1 57 | print( 58 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format( 59 | idx, cur_usage / (1024 * 1024), consume_time 60 | ) 61 | ) 62 | 63 | print('consumed time:', total_time) 64 | 65 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 66 | max_mem_usage = max_mem_usage / (1024 * 1024) 67 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 68 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 69 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/pytorch_test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import numpy as np 5 | import psutil 6 | import torch 7 | import torch.nn.functional as F 8 | import torch.optim as optim 9 | 10 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 11 | from torchvision.models import vgg16 12 | 13 | # set gpu_id 0 14 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 15 | 16 | # system monitor 17 | info = psutil.virtual_memory() 18 | monitor_interval = MONITOR_INTERVAL 19 | avg_mem_usage = 0 20 | max_mem_usage = 0 21 | count = 0 22 | total_time = 0 23 | 24 | # get the whole model 25 | vgg = vgg16() 26 | 27 | start_time = time.time() 28 | vgg = vgg.to(device) 29 | total_time += time.time() - start_time 30 | 31 | # training setting 32 | num_iter = NUM_ITERS 33 | batch_size = BATCH_SIZE 34 | optimizer = optim.Adam(vgg.parameters(), lr=LERANING_RATE) 35 | 36 | # data generator 37 | gen = random_input_generator(num_iter, batch_size, format='NCHW') 38 | 39 | # begin training 40 | 41 | for idx, data in enumerate(gen): 42 | 43 | start_time = time.time() 44 | 45 | x_batch = torch.Tensor(data[0]) 46 | y_batch = torch.Tensor(data[1]).long() 47 | x_batch = x_batch.to(device) 48 | y_batch = y_batch.to(device) 49 | 50 | # forward + backward 51 | outputs = vgg(x_batch) 52 | loss = F.cross_entropy(outputs, y_batch) 53 | optimizer.zero_grad() 54 | loss.backward() 55 | optimizer.step() 56 | 57 | end_time = time.time() 58 | consume_time = end_time - start_time 59 | total_time += consume_time 60 | 61 | if idx % monitor_interval == 0: 62 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 63 | max_mem_usage = max(cur_usage, max_mem_usage) 64 | avg_mem_usage += cur_usage 65 | count += 1 66 | print( 67 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format( 68 | idx, cur_usage / (1024 * 1024), consume_time 69 | ) 70 | ) 71 | 72 | print('consumed time:', total_time) 73 | 74 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 75 | max_mem_usage = max_mem_usage / (1024 * 1024) 76 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 77 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 78 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tf2-autograph.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | from tensorflow.python.keras.applications import VGG16 7 | 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | # get the whole model 16 | vgg = VGG16(weights=None) 17 | 18 | # system monitor 19 | info = psutil.virtual_memory() 20 | monitor_interval = MONITOR_INTERVAL 21 | avg_mem_usage = 0 22 | max_mem_usage = 0 23 | count = 0 24 | total_time = 0 25 | 26 | # training setting 27 | num_iter = NUM_ITERS 28 | batch_size = BATCH_SIZE 29 | train_weights = vgg.trainable_variables 30 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 31 | loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) 32 | 33 | # data generator 34 | gen = random_input_generator(num_iter, batch_size) 35 | 36 | 37 | # training function 38 | @tf.function 39 | def train_step(x_batch, y_batch): 40 | # forward + backward 41 | with tf.GradientTape() as tape: 42 | ## compute outputs 43 | _logits = vgg(x_batch, training=True) 44 | ## compute loss and update model 45 | _loss = loss_object(y_batch, _logits) 46 | 47 | grad = tape.gradient(_loss, train_weights) 48 | optimizer.apply_gradients(zip(grad, train_weights)) 49 | 50 | 51 | # begin training 52 | for idx, data in enumerate(gen): 53 | start_time = time.time() 54 | 55 | x_batch = tf.convert_to_tensor(data[0]) 56 | y_batch = tf.convert_to_tensor(data[1]) 57 | train_step(x_batch, y_batch) 58 | 59 | end_time = time.time() 60 | consume_time = end_time - start_time 61 | total_time += consume_time 62 | 63 | if idx % monitor_interval == 0: 64 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 65 | max_mem_usage = max(cur_usage, max_mem_usage) 66 | avg_mem_usage += cur_usage 67 | count += 1 68 | tf.print( 69 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format( 70 | idx, cur_usage / (1024 * 1024), consume_time 71 | ) 72 | ) 73 | 74 | print('consumed time:', total_time) 75 | 76 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 77 | max_mem_usage = max_mem_usage / (1024 * 1024) 78 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 79 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 80 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tf2-eager.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | from tensorflow.python.keras.applications import VGG16 7 | 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | # get the whole model 16 | vgg = VGG16(weights=None) 17 | 18 | # system monitor 19 | info = psutil.virtual_memory() 20 | monitor_interval = MONITOR_INTERVAL 21 | avg_mem_usage = 0 22 | max_mem_usage = 0 23 | count = 0 24 | total_time = 0 25 | 26 | # training setting 27 | num_iter = NUM_ITERS 28 | batch_size = BATCH_SIZE 29 | train_weights = vgg.trainable_variables 30 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 31 | loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) 32 | 33 | # data generator 34 | gen = random_input_generator(num_iter, batch_size) 35 | 36 | 37 | # training function 38 | def train_step(x_batch, y_batch): 39 | # forward + backward 40 | with tf.GradientTape() as tape: 41 | ## compute outputs 42 | _logits = vgg(x_batch, training=True) 43 | ## compute loss and update model 44 | _loss = loss_object(y_batch, _logits) 45 | 46 | grad = tape.gradient(_loss, train_weights) 47 | optimizer.apply_gradients(zip(grad, train_weights)) 48 | 49 | return _loss 50 | 51 | 52 | # begin training 53 | for idx, data in enumerate(gen): 54 | start_time = time.time() 55 | 56 | x_batch = tf.convert_to_tensor(data[0]) 57 | y_batch = tf.convert_to_tensor(data[1]) 58 | loss = train_step(x_batch, y_batch) 59 | 60 | end_time = time.time() 61 | consume_time = end_time - start_time 62 | total_time += consume_time 63 | 64 | if idx % monitor_interval == 0: 65 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 66 | max_mem_usage = max(cur_usage, max_mem_usage) 67 | avg_mem_usage += cur_usage 68 | count += 1 69 | tf.print( 70 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s, loss {:.4f}".format( 71 | idx, cur_usage / (1024 * 1024), consume_time, loss 72 | ) 73 | ) 74 | 75 | print('consumed time:', total_time) 76 | 77 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 78 | max_mem_usage = max_mem_usage / (1024 * 1024) 79 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 80 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 81 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tl2-autograph.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | tl.logging.set_verbosity(tl.logging.DEBUG) 16 | 17 | # get the whole model 18 | vgg = tl.models.vgg16() 19 | 20 | # system monitor 21 | info = psutil.virtual_memory() 22 | monitor_interval = MONITOR_INTERVAL 23 | avg_mem_usage = 0 24 | max_mem_usage = 0 25 | count = 0 26 | total_time = 0 27 | 28 | # training setting 29 | num_iter = NUM_ITERS 30 | batch_size = BATCH_SIZE 31 | train_weights = vgg.trainable_weights 32 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 33 | loss_object = tl.cost.cross_entropy 34 | 35 | # data generator 36 | gen = random_input_generator(num_iter, batch_size) 37 | 38 | 39 | # training function 40 | @tf.function 41 | def train_step(x_batch, y_batch): 42 | # forward + backward 43 | with tf.GradientTape() as tape: 44 | ## compute outputs 45 | _logits = vgg(x_batch) 46 | ## compute loss and update model 47 | _loss = loss_object(_logits, y_batch) 48 | 49 | grad = tape.gradient(_loss, train_weights) 50 | optimizer.apply_gradients(zip(grad, train_weights)) 51 | 52 | 53 | # begin training 54 | vgg.train() 55 | 56 | for idx, data in enumerate(gen): 57 | start_time = time.time() 58 | 59 | train_step(data[0], data[1]) 60 | 61 | end_time = time.time() 62 | consume_time = end_time - start_time 63 | total_time += consume_time 64 | 65 | if idx % monitor_interval == 0: 66 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 67 | max_mem_usage = max(cur_usage, max_mem_usage) 68 | avg_mem_usage += cur_usage 69 | count += 1 70 | tl.logging.info( 71 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format( 72 | idx, cur_usage / (1024 * 1024), consume_time 73 | ) 74 | ) 75 | 76 | print('consumed time:', total_time) 77 | 78 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 79 | max_mem_usage = max_mem_usage / (1024 * 1024) 80 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 81 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 82 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tl2-eager.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | tl.logging.set_verbosity(tl.logging.DEBUG) 16 | 17 | # get the whole model 18 | vgg = tl.models.vgg16() 19 | 20 | # system monitor 21 | info = psutil.virtual_memory() 22 | monitor_interval = MONITOR_INTERVAL 23 | avg_mem_usage = 0 24 | max_mem_usage = 0 25 | count = 0 26 | total_time = 0 27 | 28 | # training setting 29 | num_iter = NUM_ITERS 30 | batch_size = BATCH_SIZE 31 | train_weights = vgg.trainable_weights 32 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 33 | loss_object = tl.cost.cross_entropy 34 | 35 | # data generator 36 | gen = random_input_generator(num_iter, batch_size) 37 | 38 | 39 | # training function 40 | def train_step(x_batch, y_batch): 41 | # forward + backward 42 | with tf.GradientTape() as tape: 43 | ## compute outputs 44 | _logits = vgg(x_batch) 45 | ## compute loss and update model 46 | _loss = loss_object(_logits, y_batch) 47 | 48 | grad = tape.gradient(_loss, train_weights) 49 | optimizer.apply_gradients(zip(grad, train_weights)) 50 | return _loss 51 | 52 | 53 | # begin training 54 | vgg.train() 55 | 56 | for idx, data in enumerate(gen): 57 | start_time = time.time() 58 | 59 | loss = train_step(data[0], data[1]) 60 | 61 | end_time = time.time() 62 | consume_time = end_time - start_time 63 | total_time += consume_time 64 | 65 | if idx % monitor_interval == 0: 66 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 67 | max_mem_usage = max(cur_usage, max_mem_usage) 68 | avg_mem_usage += cur_usage 69 | count += 1 70 | tl.logging.info( 71 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s, loss {:.4f}".format( 72 | idx, cur_usage / (1024 * 1024), consume_time, loss 73 | ) 74 | ) 75 | 76 | print('consumed time:', total_time) 77 | 78 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 79 | max_mem_usage = max_mem_usage / (1024 * 1024) 80 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 81 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 82 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tl2-static-autograph.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | tl.logging.set_verbosity(tl.logging.DEBUG) 16 | 17 | # get the whole model 18 | vgg = tl.models.vgg16(mode='static') 19 | 20 | # system monitor 21 | info = psutil.virtual_memory() 22 | monitor_interval = MONITOR_INTERVAL 23 | avg_mem_usage = 0 24 | max_mem_usage = 0 25 | count = 0 26 | total_time = 0 27 | 28 | # training setting 29 | num_iter = NUM_ITERS 30 | batch_size = BATCH_SIZE 31 | train_weights = vgg.trainable_weights 32 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 33 | loss_object = tl.cost.cross_entropy 34 | 35 | # data generator 36 | gen = random_input_generator(num_iter, batch_size) 37 | 38 | 39 | # training function 40 | @tf.function 41 | def train_step(x_batch, y_batch): 42 | # forward + backward 43 | with tf.GradientTape() as tape: 44 | ## compute outputs 45 | _logits = vgg(x_batch) 46 | ## compute loss and update model 47 | _loss = loss_object(_logits, y_batch) 48 | 49 | grad = tape.gradient(_loss, train_weights) 50 | optimizer.apply_gradients(zip(grad, train_weights)) 51 | 52 | 53 | # begin training 54 | vgg.train() 55 | 56 | for idx, data in enumerate(gen): 57 | start_time = time.time() 58 | 59 | train_step(data[0], data[1]) 60 | 61 | end_time = time.time() 62 | consume_time = end_time - start_time 63 | total_time += consume_time 64 | 65 | if idx % monitor_interval == 0: 66 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 67 | max_mem_usage = max(cur_usage, max_mem_usage) 68 | avg_mem_usage += cur_usage 69 | count += 1 70 | tl.logging.info( 71 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s".format( 72 | idx, cur_usage / (1024 * 1024), consume_time 73 | ) 74 | ) 75 | 76 | print('consumed time:', total_time) 77 | 78 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 79 | max_mem_usage = max_mem_usage / (1024 * 1024) 80 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 81 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 82 | -------------------------------------------------------------------------------- /tests/performance_test/vgg/tl2-static-eager.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | import psutil 5 | import tensorflow as tf 6 | 7 | import tensorlayer as tl 8 | from exp_config import (BATCH_SIZE, LERANING_RATE, MONITOR_INTERVAL, NUM_ITERS, random_input_generator) 9 | 10 | gpus = tf.config.experimental.list_physical_devices('GPU') 11 | if gpus: 12 | for gpu in gpus: 13 | tf.config.experimental.set_memory_growth(gpu, True) 14 | 15 | tl.logging.set_verbosity(tl.logging.DEBUG) 16 | 17 | # get the whole model 18 | vgg = tl.models.vgg16(mode='static') 19 | 20 | # system monitor 21 | info = psutil.virtual_memory() 22 | monitor_interval = MONITOR_INTERVAL 23 | avg_mem_usage = 0 24 | max_mem_usage = 0 25 | count = 0 26 | total_time = 0 27 | 28 | # training setting 29 | num_iter = NUM_ITERS 30 | batch_size = BATCH_SIZE 31 | train_weights = vgg.trainable_weights 32 | optimizer = tf.optimizers.Adam(learning_rate=LERANING_RATE) 33 | loss_object = tl.cost.cross_entropy 34 | 35 | # data generator 36 | gen = random_input_generator(num_iter, batch_size) 37 | 38 | 39 | # training function 40 | def train_step(x_batch, y_batch): 41 | # forward + backward 42 | with tf.GradientTape() as tape: 43 | ## compute outputs 44 | _logits = vgg(x_batch) 45 | ## compute loss and update model 46 | _loss = loss_object(_logits, y_batch) 47 | 48 | grad = tape.gradient(_loss, train_weights) 49 | optimizer.apply_gradients(zip(grad, train_weights)) 50 | return _loss 51 | 52 | 53 | # begin training 54 | vgg.train() 55 | 56 | for idx, data in enumerate(gen): 57 | start_time = time.time() 58 | 59 | loss = train_step(data[0], data[1]) 60 | 61 | end_time = time.time() 62 | consume_time = end_time - start_time 63 | total_time += consume_time 64 | 65 | if idx % monitor_interval == 0: 66 | cur_usage = psutil.Process(os.getpid()).memory_info().rss 67 | max_mem_usage = max(cur_usage, max_mem_usage) 68 | avg_mem_usage += cur_usage 69 | count += 1 70 | tl.logging.info( 71 | "[*] {} iteration: memory usage {:.2f}MB, consume time {:.4f}s, loss {:.4f}".format( 72 | idx, cur_usage / (1024 * 1024), consume_time, loss 73 | ) 74 | ) 75 | 76 | print('consumed time:', total_time) 77 | 78 | avg_mem_usage = avg_mem_usage / count / (1024 * 1024) 79 | max_mem_usage = max_mem_usage / (1024 * 1024) 80 | print('average memory usage: {:.2f}MB'.format(avg_mem_usage)) 81 | print('maximum memory usage: {:.2f}MB'.format(max_mem_usage)) 82 | -------------------------------------------------------------------------------- /tests/test_nlp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | import os 5 | import unittest 6 | 7 | import nltk 8 | 9 | import tensorlayer as tl 10 | from tests.utils import CustomTestCase 11 | 12 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' 13 | 14 | nltk.download('punkt') 15 | 16 | 17 | class Test_Leaky_ReLUs(CustomTestCase): 18 | 19 | @classmethod 20 | def setUpClass(cls): 21 | pass 22 | 23 | @classmethod 24 | def tearDownClass(cls): 25 | pass 26 | 27 | def test_as_bytes(self): 28 | origin_str = "str" 29 | origin_bytes = b'bytes' 30 | converted_str = tl.nlp.as_bytes(origin_str) 31 | converted_bytes = tl.nlp.as_bytes(origin_bytes) 32 | print('str after using as_bytes:', converted_str) 33 | print('bytes after using as_bytes:', converted_bytes) 34 | 35 | def test_as_text(self): 36 | origin_str = "str" 37 | origin_bytes = b'bytes' 38 | converted_str = tl.nlp.as_text(origin_str) 39 | converted_bytes = tl.nlp.as_text(origin_bytes) 40 | print('str after using as_text:', converted_str) 41 | print('bytes after using as_text:', converted_bytes) 42 | 43 | def test_save_vocab(self): 44 | words = tl.files.load_matt_mahoney_text8_dataset() 45 | vocabulary_size = 50000 46 | data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True) 47 | tl.nlp.save_vocab(count, name='vocab_text8.txt') 48 | 49 | def test_basic_tokenizer(self): 50 | c = "how are you?" 51 | tokens = tl.nlp.basic_tokenizer(c) 52 | print(tokens) 53 | 54 | def test_generate_skip_gram_batch(self): 55 | data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] 56 | batch, labels, data_index = tl.nlp.generate_skip_gram_batch( 57 | data=data, batch_size=8, num_skips=2, skip_window=1, data_index=0 58 | ) 59 | print(batch) 60 | print(labels) 61 | 62 | def test_process_sentence(self): 63 | c = "how are you?" 64 | c = tl.nlp.process_sentence(c) 65 | print(c) 66 | 67 | def test_words_to_word_id(self): 68 | words = tl.files.load_matt_mahoney_text8_dataset() 69 | vocabulary_size = 50000 70 | data, count, dictionary, reverse_dictionary = tl.nlp.build_words_dataset(words, vocabulary_size, True) 71 | ids = tl.nlp.words_to_word_ids(words, dictionary) 72 | context = tl.nlp.word_ids_to_words(ids, reverse_dictionary) 73 | # print(ids) 74 | # print(context) 75 | 76 | 77 | if __name__ == '__main__': 78 | 79 | unittest.main() 80 | -------------------------------------------------------------------------------- /tests/utils/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tests.utils.custom_layers import * 5 | from tests.utils.custom_networks import * 6 | from tests.utils.custom_testcase import * 7 | from tests.utils.list_py_files import * 8 | from tests.utils.timeout_utils import * 9 | -------------------------------------------------------------------------------- /tests/utils/custom_layers/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tests.utils.custom_layers.basic_layers import * 5 | from tests.utils.custom_layers.inception_blocks import * 6 | -------------------------------------------------------------------------------- /tests/utils/custom_networks/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from tests.utils.custom_networks.inceptionv4 import * 5 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tl: -------------------------------------------------------------------------------- 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 | 13 | python3 -m tensorlayer.cli "$@" 14 | --------------------------------------------------------------------------------