├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── books ├── README.md └── deep-learning-design-patterns │ ├── Deep Learning Design Patterns Primer.pdf │ ├── README.md │ └── Workshops │ ├── Junior │ ├── Deep Learning Design Patterns - Workshop - Chapter 1.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter 1.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 2.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter 2.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 3.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter 3.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 4.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter 4.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 5.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 6.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 7.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 8.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 9-2.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter 9.pptx │ ├── README.md │ ├── block-v1.jpg │ ├── macro.png │ ├── naive-inception.jpg │ └── se-link.jpg │ ├── Novice │ ├── Deep Learning Design Patterns - Workshop - Chapter I - answers.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter I.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter I.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter II.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter II.pptx │ ├── Deep Learning Design Patterns - Workshop - Chapter III - 1 - answers.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter III - 1.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter III - 2 - answers.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter III - 2.ipynb │ ├── Deep Learning Design Patterns - Workshop - Chapter III.pptx │ ├── README.md │ └── apple.jpg │ └── README.md ├── community-labs ├── Community Lab - Intro to Composable (Common).pptx ├── README.md ├── encoders_for_cnn │ ├── Community Lab - Encoders for CNN.ipynb │ └── Community Lab - Encoders for CNN.pptx ├── ensemble │ ├── Community Lab - Ensemble.ipynb │ └── Community Lab - Ensemble.pptx └── regularization │ ├── Community Lab - Regularization.ipynb │ └── Community Lab - Regularization.pptx ├── handbooks ├── README.md ├── The Idiomatic Programmer - Learning Keras - Handbook 1 - Computer Vision Models.pdf ├── The Idiomatic Programmer - Learning Keras - Handbook 2 - Computer Vision Data Engineering.pdf ├── The Idiomatic Programmer - Learning Keras - Handbook 3 - Computer Vision - Training and Deployment.pdf └── The Idiomatic Programmer - Statistics Primer.pdf ├── notebooks ├── README.md ├── ai_hub │ ├── devrelres_AutoML Language Sentiment.ipynb │ ├── devrelres_AutoML Vision Classification Part II.ipynb │ └── devrelres_AutoML Vision Classification.ipynb ├── building_in_processing_in_graph.ipynb ├── estimating_your_training_utilization.ipynb ├── fun_with_ml_production.ipynb ├── prediction_with_example_key.ipynb └── prestem_deconvolution.ipynb ├── papers ├── Identity Normalization.ipynb ├── covid-19 │ ├── covid19-resnetv2.ipynb │ └── covid19.csv └── identity_link_unnormalized_numeric_feature.ipynb ├── presentations ├── Automatic Learning.pdf ├── Full-In Production.pptx ├── ML in the Workplace.pptx └── TF 2.0_ Transitioning to Production.pptx ├── what-is-idiomatic.md ├── workshops ├── Advanced_Convolutional_Neural_Networks │ ├── Idiomatic Programmer - handbook 1 - Codelab 4.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Advanced CNN.pptx ├── Convolutional_Neural_Networks │ ├── Idiomatic Programmer - handbook 1 - Codelab 2.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Convolutional Neural Networks.pptx ├── Data_Augmentation │ ├── Idiomatic Programmer - handbook 2 - Codelab 2.ipynb │ ├── Idiomatic Programmer - handbook 2 - Codelab 3.ipynb │ ├── The Idiomatic Programmer - Learning Keras - Workshop - Data Augmentation.pptx │ └── apple.jpg ├── Data_Engineering │ ├── Idiomatic Programmer - handbook 2 - Codelab 1.ipynb │ ├── The Idiomatic Programmer - Learning Keras - Workshop - Data Engineering.pptx │ └── apple.jpg ├── Mobile_Networks │ └── The Idiomatic Programmer - Learning Keras - Workshop - Mobile Networks.pptx ├── Modern_CNN │ ├── Modern Convolutional Neural Network Architectures.pdf │ ├── Modern Convolutional Neural Network Architectures.pptx │ ├── Modern CNN - lab 1.ipynb │ ├── Modern CNN - lab 2.ipynb │ └── README.md ├── Neural_Networks │ ├── Idiomatic Programmer - handbook 1 - Codelab 1.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Neural Networks.pptx ├── Production │ └── The Idiomatic Programmer - Production - Workshop.pptx ├── README.md ├── Training │ ├── Idiomatic Programmer - handbook 3 - Codelab 1.ipynb │ ├── Idiomatic Programmer - handbook 3 - Codelab 2.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Training.pptx ├── Transfer_Learning │ ├── Idiomatic Programmer - handbook 3 - Codelab 3.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Transfer Learning.pptx └── Wide_Convolutional_Neural_Networks │ ├── .ipynb_checkpoints │ └── Idiomatic Programmer - handbook 1 - Codelab 3-checkpoint.ipynb │ ├── Idiomatic Programmer - handbook 1 - Codelab 3.ipynb │ └── The Idiomatic Programmer - Learning Keras - Workshop - Wide Convolutional Neural Networks.pptx └── zoo ├── README.md ├── alexnet ├── README.md ├── alexnet.py ├── alexnet_p.py ├── classifier.jpg ├── macro-simplified.jpg ├── macro.jpg └── stem.jpg ├── autoencoder ├── README.md ├── autoencoder.py ├── autoencoder_c.py ├── dc_autoencoder.py ├── dc_autoencoder_c.py ├── decoder.jpg ├── encoder.jpg └── macro.jpg ├── datasets_c.py ├── dcgan ├── README.md ├── dcgan_c.py ├── macro-dis.jpg ├── macro-gen.jpg └── macro.jpg ├── densenet ├── README.md ├── classifier.jpg ├── dense-block.jpg ├── densenet.py ├── densenet_c.py ├── macro.jpg ├── micro.jpg ├── residual-block.jpg ├── stem.jpg └── trans-block.jpg ├── fast r-cnn ├── README.md └── macro.jpg ├── faster r-cnn ├── README.md ├── macro.jpg ├── rpn.jpg ├── rpnroi.jpg └── vgg16.jpg ├── hypertune_c.py ├── inception ├── README.md ├── auxiliary-v3.jpg ├── auxiliary.jpg ├── block-17.jpg ├── block-35.jpg ├── block-8.jpg ├── block-v1.jpg ├── block-v2.jpg ├── classifier.jpg ├── inception_v1.py ├── inception_v1_c.py ├── inception_v2.py ├── inception_v2_c.py ├── inception_v3.py ├── inception_v3_c.py ├── macro-v3.jpg ├── macro.jpg ├── micro-v1.jpg ├── micro-v3.jpg ├── reduction-17.jpg ├── reduction-8.jpg ├── stem-v1.jpg ├── stem-v3.jpg └── stem-v4.jpg ├── jumpnet ├── README.md ├── jumpnet.py ├── jumpnet_c.py ├── macro.jpg └── models_c.py ├── layers_c.py ├── macro.jpg ├── micro.jpg ├── mobilenet ├── README.md ├── attention.jpg ├── classifier-v2.jpg ├── classifier-v3.jpg ├── classifier.jpg ├── depthwise-block.jpg ├── inverted-block.jpg ├── macro-v2.jpg ├── macro-v3.jpg ├── macro.jpg ├── micro-v2.jpg ├── micro-v3.jpg ├── micro.jpg ├── mobilenet_v1.py ├── mobilenet_v1_c.py ├── mobilenet_v2.py ├── mobilenet_v2_c.py ├── mobilenet_v3_c.py ├── squeeze.jpg ├── stem-v2.jpg ├── stem-v3.jpg ├── stem.jpg ├── strided-depthwise-block.jpg └── strided-inverted-block.jpg ├── models_c.py ├── preprocess_c.py ├── pretraining_c.py ├── r-cnn ├── README.md └── macro.jpg ├── resnet ├── README.md ├── classifier.jpg ├── identity-block.jpg ├── macro.jpg ├── micro.jpg ├── projection-block-v1.5.jpg ├── projection-block.jpg ├── resnet34.py ├── resnet_cifar10.py ├── resnet_cifar10_v1_c.py ├── resnet_cifar10_v2.py ├── resnet_cifar10_v2_c.py ├── resnet_v1.5.py ├── resnet_v1.5_c.py ├── resnet_v1.py ├── resnet_v1_c.py ├── resnet_v2.py ├── resnet_v2_c.py └── stem.jpg ├── resnext ├── README.md ├── cardinality.jpg ├── classifier.jpg ├── identity-block.jpg ├── macro.jpg ├── micro.jpg ├── projection-block.jpg ├── resnext.py ├── resnext_c.py ├── resnext_cifar10.py └── stem.jpg ├── senet ├── README.md ├── identity-block.jpg ├── macro.jpg ├── micro.jpg ├── projection-block.jpg ├── se-block.jpg ├── se_resnet.py ├── se_resnet_c.py ├── se_resnext.py └── se_resnext_c.py ├── shufflenet ├── README.md ├── block.png ├── classifier.jpg ├── macro.png ├── micro.png ├── shufflenet.py ├── shufflenet_c.py ├── stem.jpg └── strided-block.png ├── siamese_twin ├── README.md ├── block-conv.jpg ├── classifier.jpg ├── encoder.jpg ├── macro.jpg ├── micro.jpg ├── siamese_twin.py ├── siamese_twin_c.py └── stem.jpg ├── squeezenet ├── README.md ├── bypass-block.jpg ├── classifier.jpg ├── complex-block.jpg ├── fire.jpg ├── macro.jpg ├── micro-bypass.jpg ├── micro-complex.jpg ├── micro.jpg ├── squeezenet.py ├── squeezenet_bypass.py ├── squeezenet_bypass_c.py ├── squeezenet_c.py ├── squeezenet_complex.py ├── squeezenet_complex_c.py └── stem.jpg ├── srcnn ├── README.md ├── macro-post.jpg ├── macro-pre.jpg ├── srcnn-post.py ├── srcnn-post_c.py ├── srcnn.py └── srcnn_c.py ├── training_c.py ├── unet ├── classifier.jpg ├── contract-block.jpg ├── contract-macro.jpg ├── expand-block.jpg ├── expand-macro.jpg ├── macro.jpg ├── readme.md └── unet_c.py ├── vgg ├── README.md ├── classifier.jpg ├── macro.jpg ├── micro-conv.jpg ├── stem.jpg ├── vgg.py └── vgg_c.py ├── wrn ├── README.md ├── classifier.jpg ├── identity-block.jpg ├── macro.jpg ├── micro.jpg ├── projection-block.jpg ├── stem.jpg └── wrn_c.py ├── xception ├── README.md ├── block-exit-conv.jpg ├── block-exit-residual.jpg ├── block-middle.jpg ├── block-projection.jpg ├── classifier.jpg ├── macro.jpg ├── micro-entry.jpg ├── micro-exit.jpg ├── micro-middle.jpg ├── stem.jpg ├── xception.py └── xception_c.py └── zfnet └── zfnet.py /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are just a few small guidelines you need to follow. 4 | 5 | ## Contributor License Agreement 6 | 7 | Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to https://cla.developers.google.com/ to see your current agreements on file or to sign a new one. 8 | 9 | You generally only need to submit a CLA once, so if you've already submitted one (even if it was for a different project), you probably don't need to do it again. 10 | 11 | ## Code reviews 12 | All submissions, including submissions by project members, require review. We use GitHub pull requests for this purpose. Consult GitHub Help for more information on using pull requests. 13 | 14 | ## Community Guidelines 15 | This project follows Google's Open Source Community Guidelines. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) 3 | [![License](https://i.creativecommons.org/l/by/4.0/80x15.png)](LICENSE) 4 | 5 | # Google Cloud AI Developer Relations 6 | 7 | This repository contains content produced by *Google Cloud AI Developer Relations* for machine learning and artificial intelligence. The content covers a wide spectrum from educational, training, and research, covering from novices, junior/intermediate to advanced. 8 | 9 | The content has been designed both for self-learning, and for being incorporating into instructional material for universities, private coding schools, and professional training. Licensed as Apache 2.0 and CC-BY, organizations are free to integrate and customize the material into their curriculum. 10 | 11 | The repository is organized by: 12 | 13 | | Directory | Description | 14 | | ------------- | ------------- | 15 | | [`handbooks`](handbooks) | Google (FREE) PDF softcopy of handbooks - CC-BY 4.0 license (2019) | 16 | | [`books`](books) | PDF softcopy of books - preview versions | 17 | | [`workshops`](workshops) | workshops (presentation slides and code labs) - CC-BY 4.0 license (2019) | 18 | | [`notebooks`](notebooks) | notebooks for production-grade solutions | 19 | | [`zoo`](zoo) | tf.keras model zoo (coded in idiomatic and composable design pattern) - Apache 2.0 license | 20 | | [`community labs`](community-labs)| Labs for community participation in research | 21 | 22 | ## Reviewers and Contributors 23 | 24 | We thank the following for their reviews and contributions to the Idiomatic Programmer: 25 | 26 | *Google Cloud AI - Developer Relations* 27 | 28 | [Andrew Ferlitsch](https://github.com/andrewferlitsch)
29 | [Noah Negrey](https://www.linkedin.com/in/noah-negrey-bb0a395a)
30 | [Yu-Han Liu](https://www.linkedin.com/in/yu-han-liu-7719281a)
31 | [Shahin Saadati](https://www.linkedin.com/in/shahinsaadati)
32 | [Torry Yang](https://www.linkedin.com/in/torryyang)
33 | [Gonzalo Gasca Meza](https://www.linkedin.com/in/gogasca)
34 | [Amy Unruh](https://www.linkedin.com/in/amyunruh)
35 | [Martin Gorner](https://www.linkedin.com/in/martingorner)
36 | [Brad Miro](https://www.linkedin.com/in/brad-miro)
37 | [Tianzi Cai](https://www.linkedin.com/in/tianzi)
38 | [Sara Robinson](https://www.linkedin.com/in/sara-robinson-40377924)
39 | [Puneith Kaul](https://www.linkedin.com/in/puneith)
40 | [Crystal Gomes](https://www.linkedin.com/in/crystalgomes) 41 | 42 | *Other Current/Former Googlers* 43 | 44 | William Barret
45 | [Sharon Maher](https://www.linkedin.com/in/sharonmeetworld) 46 | 47 | *Google Developer Experts (GDE)* 48 | 49 | [Margaret Maynard-Reid / Seattle](https://github.com/margaretmz) 50 | 51 | *ML Practioners* 52 | 53 | [Hobson Lane](https://www.linkedin.com/in/hobsonlane)
54 | [Enoch Tetteh](https://www.linkedin.com/in/enoch-tetteh-80450211a)
55 | [Bill Liu](https://www.linkedin.com/in/billliu1202)
56 | [Miklos Toth](https://www.linkedin.com/in/miklostoth/) 57 | 58 | ## Disclaimer 59 | 60 | This is not an officially supported Google product. 61 | -------------------------------------------------------------------------------- /books/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | This section is for books on Deep Learning that I am authoring for publication through 3rd party publishers. 4 | 5 | | Book | Description | 6 | | ---------- | ------------ | 7 | | [`Deep Learning Design Patterns`](deep-learning-design-patterns) | `Book Proposal approved by Manning`| 8 | -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Deep Learning Design Patterns Primer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Deep Learning Design Patterns Primer.pdf -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/README.md: -------------------------------------------------------------------------------- 1 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) 2 | [![License](https://i.creativecommons.org/l/by/4.0/80x15.png)](LICENSE) 3 | 4 | 5 | 6 | This repository is origanized as follows: 7 | 8 | |File | Description| 9 | |-----------|------------| 10 | | `Primer` | Section 1 (chapters I, II and II) made freely available in PDF| 11 | | [`Workshops`](Workshops) |Workshop content bundles for each chapter in the book (FREE)| 12 | 13 | ### About the book. 14 | 15 | *What is the technology or idea that you’re writing about?* 16 | 17 | Coding computer vision models using design patterns that are AutoML friendly -- bring your own model to automl and guide the search space. 18 | 19 | Teaches junior to advanced techniques for coding models in Tensorflow 2.0 with Keras Functional API. 20 | 21 | Readers will learn concepts of design patterns (object oriented programming) and how design patterns speed up model design and understandability to users. 22 | 23 | Starting with section 2 (Intermediate) the composable design pattern will be used to demonstrate Automatic Learning concepts and how using the design pattern one can incorporate automatic learning concepts at a level higher of abstraction and guide the search space. 24 | 25 | *Why is it important now?* 26 | 27 | I find as my role at Google interfacing with Enterprise clients, that decision makers are looking to move their data science/ML teams to automatic learning, such that their talents/skills can be redirected to more challenging tasks. 28 | 29 | To make that transition, they need automatic learning to be demystified and not a black box, where ML practitioners and ML ops can guide the search space and effectively evaluate the models for production-scale use. 30 | 31 | 32 | ## Topics 33 | 34 | ### Preface 35 | The Machine Learning Steps 36 | Classical vs. Narrow AI 37 | 38 | ### Novice (Primer) 39 | Chapter I - Deep Neural Networks 40 | I.I. Input Layer 41 | I.II. Deep Neural Networks (DNN) 42 | I.III. Feed Forward 43 | I.IV. DNN Binary Classifier 44 | I.V. DNN Multi-Class Classifier 45 | I.VI. DNN Multi-Label Multi-Class Classifier 46 | I.VII. Simple Image Classifier 47 | Chapter II - Convolutional and ResNet Neural Networks 48 | II.I Convolutional Neural Networks 49 | II.II. CNN Classifier 50 | II.III. Basic CNN 51 | II.IV. VGG 52 | II.V. Residual Networks (ResNet) 53 | II.VI. ResNet50 54 | Chapter III - Training Foundation 55 | III.I. Feed Forward and Backward Propagation 56 | III.II. Dataset Splitting 57 | III.III. Normalization 58 | III.IV. Validation & Overfitting 59 | III.V. Convergence 60 | III.VI. Checkpointing & Earlystopping 61 | III.V. Hyperparameters 62 | III.VI. Invariance 63 | III.VII. Raw (Disk) Datasets 64 | III.VIII. Model Save/Restore 65 | 66 | ### Junior 67 | Chapter 1 - Introduction 68 | 1.1 How I Teach 69 | 1.2. What You Will Take Away 70 | 1.3. The Machine Learning Steps 71 | Chapter 2 - Procedural Design Pattern 72 | 2.1. Procedural Design Pattern 73 | 2.2. Stem Component 74 | 2.3. Learner Component 75 | 2.4. Classifier Component 76 | Chapter 3 - Wide Convolutional Neural Networks 77 | 3.1. Inception V1 78 | 3.2. Inception V2 79 | 2.3. Inception V3 80 | 3.4. ResNeXt 81 | 3.5. Wide Residual Network (WRN) 82 | Chapter 4 - Alternative ConnectivityPatterns 83 | 4.1. Densely Connected CNN (DenseNet) 84 | 4.2. SE-Net 85 | 4.3. Xception 86 | Chapter 5 - Mobile Convolutional Neural Networks 87 | 5.1. MobileNet 88 | 5.2 SqueezeNet 89 | 5.3. ShuffleNet 90 | 5.4. Quantization 91 | Chapter 5 - AutoEncoders 92 | 6.1. AutoEncoder 93 | 6.2. Convolutional AutoEncoder 94 | 6.3. Sparse AutoEncoder 95 | 6.4. Denoising AutoEncoder 96 | 6.5. Pre-text Tasks 97 | Chapter 7 - Hyperparameter Tuning 98 | 7.1. Warmup (Numerical Stability) 99 | 7.2. Grid Search 100 | 7.3. Random Search 101 | 7.4. Learning Rate Scheduler 102 | 7.5. Regularization 103 | Chapter 8 - Transfer Learning 104 | 8.1. Overview 105 | 8.2. Fine-Tuning 106 | 8.3. Full-Tuning 107 | Chapter 9 - Training Pipeline 108 | 9.1. Data Formats & Storage 109 | 9.2. Dataset Curation 110 | 9.3. Data Preprocessing 111 | 9.4. Label Smoothing 112 | 9.5. Model Feeding 113 | 9.6. Training Schedulers 114 | 9.7. Model Evaluations 115 | Chapter 10 - Data Augmentation 116 | 10.1. Crop / Flip / Rotate 117 | 10.2. Augmentation Pipeline 118 | 119 | 120 | ### Intermediate 121 | 122 | Chapter 11 - AutoML by Design (Patterns) 123 | 11.1. Metaparameters 124 | 11.2. Embeddings 125 | 11.3. Macro-Architecture 126 | 11.4. Model Configuration 127 | 11.5. Dynamic Regularization 128 | Chapter 12 - Multi-Task Models 129 | 12.1. Object Detection - (Region Based) 130 | 12.2. Object Detection - (Pyramid Based) 131 | 12.3. Object Detection - (Single Shot) 132 | 12.4. Generative Adversarial Networks (GAN) 133 | 12.5. Style Transfer 134 | Chapter 13 - Network Architecture Search 135 | 13.1. NasNet 136 | 13.2. MNasNet 137 | 13.3. MobileNet V3 138 | Chapter 14 - Automatic Hyperparameter Search 139 | 14.1. Bayesian Optimization 140 | 14.2. Data Augmentation Strategies 141 | Chapter 15 - Production Foundation (Training at Scale) 142 | 15.1. Data Schema 143 | 15.2. Data Validation 144 | 15.3. Model Versioning 145 | 15.4. Model Deployment 146 | 15.5. A/B Testing 147 | 15.6. Continuous Evaluation 148 | 15.7. Distribution Skew / Data Drift 149 | 150 | ### Advanced 151 | 152 | Chapter 16 - Model Amalgamation 153 | 16.1. Inter-Model Connectivity 154 | 16.2. Factory Pattern 155 | 16.3. Abstract Factory Pattern 156 | 16.4. Intelligent Automation 157 | Chapter 17 - Automatic Macro-Architecture Search 158 | 17.1. Dynamic Group/Block configuration 159 | 17.2. Guiding the Search Space 160 | Chapter 18 - Knowledge Distillation (Student/Teacher) 161 | 18.1. Student/Teacher Networks 162 | 18.2. Label Distillation 163 | 18.3. Weight Transfusion 164 | Chapter 19 - Semi/Weakly Supervised Learning 165 | 19.1. Pseudo Labeling / Noisy Data 166 | 19.2. Synthetic Data 167 | 19.3. Optical Flow 168 | Chapter 20 - Self Supervised Learning 169 | 20.1. Pre-text Training (Learn Essential Features) 170 | 20.2. Adversarial Training (Robustness) 171 | 20.3. Data Weighting (Reinforcement Learning) 172 | -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 1.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 1.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 2.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 2.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 3.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 3.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 4.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 4.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 5.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 5.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 6.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 6.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 7.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 7.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 8.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 8.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 9-2.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 9-2.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 9.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/Deep Learning Design Patterns - Workshop - Chapter 9.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | This section on the workshop bundles for the Deep Learning Design Patterns Primer. 4 | Each workshop covers one chapter and consists of a presentation that summarizes the chapter along with one or more 5 | lab exercises. The intended audience is junior -- Deep Learning engineer with under 12 months on-the-job experience, or recent graduate in datascience. 6 | 7 | |File | Description| 8 | |-----------|------------| 9 | | Chapter 1 || 10 | | [`Workshop - Chapter 1.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%201.pptx) |Presentation: Chapter 1 - Models By Design | 11 | | [`Workshop - Chapter 1.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%201.ipynb)|Lab Exercise #5 - Chapter 1 - Models By Design| 12 | | Chapter 2 || 13 | | [`Workshop - Chapter 2.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%202.pptx) |Presentation: Chapter 2 - Wide Convolutional Networks | 14 | | [`Workshop - Chapter 2.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%202.ipynb)|Lab Exercise #6 - Chapter 2 - Wide Convolutional Networks| 15 | | Chapter 3 || 16 | | [`Workshop - Chapter 3.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%203.pptx) |Presentation: Chapter 3 - Alternative Connectivity Patterns || 17 | [`Workshop - Chapter 3.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%203.ipynb)|Lab Exercise #7 - Chapter 3 - Alternative Connectivity Patterns| 18 | -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/block-v1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/block-v1.jpg -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/macro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/macro.png -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/naive-inception.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/naive-inception.jpg -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Junior/se-link.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Junior/se-link.jpg -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter I.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter I.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter II.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter II.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter III.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Novice/Deep Learning Design Patterns - Workshop - Chapter III.pptx -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Novice/README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | This section on the workshop bundles for the Deep Learning Design Patterns Primer. Each workshop covers one chapter and consists of a presentation that summarizes the chapter along with one or more lab exercises. The intended audience is novice -- experienced with Python but only minimal knowledge/experience with Deep Learning. 4 | 5 | |File | Description| 6 | |-----------|------------| 7 | | Chapter I || 8 | | [`Workshop - Chapter I.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20I.pptx) |Presentation: Chapter I - Deep Neural Networks | 9 | | [`Workshop - Chapter I.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20I.ipynb)|Lab Exercise #1 - Chapter I - Deep Neural Networks| 10 | | Chapter II || 11 | | [`Workshop - Chapter II.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20II.pptx) |Presentation: Chapter II - Convolutional Neural Networks | 12 | | [`Workshop - Chapter II.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20II.ipynb)|Lab Exercise #2 - Chapter II - Convolutional Neural Networks| 13 | | Chapter III || 14 | | [`Workshop - Chapter III.pptx`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20III.pptx) |Presentation: Chapter III - Training Foundation || 15 | [`Workshop - Chapter III - 1.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20III%20-%201.ipynb)|Lab Exercise #3 - Chapter III - Data Preprocessing| 16 | [`Workshop - Chapter III - 2.ipynb`](Deep%20Learning%20Design%20Patterns%20-%20Workshop%20-%20Chapter%20III%20-%202.ipynb)|Lab Exercise #4 - Chapter III - Hyperparameters| 17 | 18 | 19 | -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/Novice/apple.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/books/deep-learning-design-patterns/Workshops/Novice/apple.jpg -------------------------------------------------------------------------------- /books/deep-learning-design-patterns/Workshops/README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | Each chapter in the book and primer is accompanied with a workshop bundle. These bundles consist of presentation over for the corresponding chapter and lab exercises (juypter notebooks). You are free to use and incorporate into training and educational purposes. 4 | 5 | |Direcory | Description| 6 | |-----------|------------| 7 | | [`Novice`](Novice) |Workshop content bundles for the Primer (Chapters I, II, 3)| 8 | | [`Junior`](Junior) |Workshop content bundles for Junior Level - Chapters 1, 2, 3, ...| 9 | -------------------------------------------------------------------------------- /community-labs/Community Lab - Intro to Composable (Common).pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/community-labs/Community Lab - Intro to Composable (Common).pptx -------------------------------------------------------------------------------- /community-labs/README.md: -------------------------------------------------------------------------------- 1 | # Labs for Community Participation in Research 2 | 3 | This section contains labs that consist of a notebook and corresponding presentation ("content bundle") for community participation in machine learning research. The target audience for these hands-on labs are ML researchers, ML engineers, data scientists and those who are already familiar with deep learning. 4 | 5 | The *Community Lab - Intro to Composable (Common).pptx* presentation is common across all the content bundles. 6 | 7 |
8 | A deep understanding of AutoML, a new approach based on new design patterns, latest research -- turning AutoML from 9 | a blackbox to one where the data scientist can bring their own custom macro/micro architectures and self-guide the 10 | search space. 11 |
12 | 13 | | Lab | Description | 14 | |-----|-------------| 15 | | `AutoEncoder for CNN` | Research using lower dimensional encoded inputs to CNN (vs original image) | 16 | | `Regularization` | Research using regularization to counter overfitting | 17 | | `Ensemble` | Research using intra-model ensemnble methods | 18 | 19 | ### AutoEncoder for Convolutional Neural Networks 20 | 21 | **Objective** 22 | 23 | To replace a traditional "stem convolution group" of higher input dimensionality with lower dimensionality encoding, learned from first training the dataset on an autoencoder. Goal is that by using a lower dimensionality encoding, one can substantially increase training time of a model. 24 | 25 | *Question:* Can one achieve the same accuracy as using the original input image? 26 | 27 | *Question:* How fast can we speed up training? 28 | 29 | ### Regularization 30 | 31 | **Objective** 32 | 33 | To explore methods of regularization and learning rates to prevent the training data from "fitting" to the weights in a compact model -- without use of historical methods such as dropout or data augmentation. 34 | 35 | *Question:* Can we generalize a compact model without image augmentation? 36 | 37 | *Question:* How is training time effected? 38 | 39 | *Question:* How small can a compact model be made and maintain accuracy on the validation/test data? 40 | 41 | ### Ensemble 42 | 43 | **Objective** 44 | 45 | To replace a traditional "inter-model" ensemble of models of high complexity with an "intra-model" ensemble of lower complexity, while retaining the performance benefits. 46 | 47 | *Question:* Can one achieve the same performance with intra-model bagging vs. traditional inter-model ensemble? 48 | 49 | *Question:* Can one achieve the same performance with intra-model stacking vs. traditional inter-model ensemble? 50 | -------------------------------------------------------------------------------- /community-labs/encoders_for_cnn/Community Lab - Encoders for CNN.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/community-labs/encoders_for_cnn/Community Lab - Encoders for CNN.pptx -------------------------------------------------------------------------------- /community-labs/ensemble/Community Lab - Ensemble.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/community-labs/ensemble/Community Lab - Ensemble.pptx -------------------------------------------------------------------------------- /community-labs/regularization/Community Lab - Regularization.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/community-labs/regularization/Community Lab - Regularization.pptx -------------------------------------------------------------------------------- /handbooks/README.md: -------------------------------------------------------------------------------- 1 | | Description | 2 | | ------------- | 3 | | Primer - Statistics Fundamentals for Machine Learning | 4 | | Handbook 1 - Learning Keras - Computer Vision Models | 5 | | Handbook 2 - Learning Keras (OpenCV) - Computer Vision Data Engineering | 6 | | Handbook 3 - Learning Keras - Computer Vision Training and Deployment | 7 | 8 | ## Quotes 9 | 10 | *This is an absolutely excellent tutorial. I love the style and level of detail---not too much, not too little.* -- Andrew Moore, VP of Engineering, Google Cloud 11 | 12 | *The Keras Idiomatic Programmer repository is a neat collection of workshops and handbooks introducing key machine learning concepts to the developer audience. I think it's a great project with very useful content*. -- Francois Chollet (creator of Keras) 13 | 14 | *This collection is literally my Keras bible. a great one.* - Roozebh Sanaei, Computer Vision Engineer at Rapsodo 15 | 16 | -------------------------------------------------------------------------------- /handbooks/The Idiomatic Programmer - Learning Keras - Handbook 1 - Computer Vision Models.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/handbooks/The Idiomatic Programmer - Learning Keras - Handbook 1 - Computer Vision Models.pdf -------------------------------------------------------------------------------- /handbooks/The Idiomatic Programmer - Learning Keras - Handbook 2 - Computer Vision Data Engineering.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/handbooks/The Idiomatic Programmer - Learning Keras - Handbook 2 - Computer Vision Data Engineering.pdf -------------------------------------------------------------------------------- /handbooks/The Idiomatic Programmer - Learning Keras - Handbook 3 - Computer Vision - Training and Deployment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/handbooks/The Idiomatic Programmer - Learning Keras - Handbook 3 - Computer Vision - Training and Deployment.pdf -------------------------------------------------------------------------------- /handbooks/The Idiomatic Programmer - Statistics Primer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/handbooks/The Idiomatic Programmer - Statistics Primer.pdf -------------------------------------------------------------------------------- /notebooks/README.md: -------------------------------------------------------------------------------- 1 | # Notebooks 2 | 3 | These notebooks address *goto production (GTP)* questions that I receive as a member of Google AI Developer Relations. 4 | 5 | | Notebook | Description | 6 | | ------------------------------------- | ------------- | 7 | | `Prediction with Example Key` | Adding Unique Identifier to Predictions in asynchronous distributed batch prediction | 8 | | `Pre-Stem Deconvolution` | Using deconvolution (transpose) to learn optimal transformations for different input sizes to existing model | 9 | | `Building Data preprocessing into Graph` | Using TF 2.0 Subclassing and @tf.function decorator to put the data preprocessing as part of the model graph | 10 | | `Estimating the CPU/GPU utilization for training` | Using pre-warmpup methods to estimate utilization across compute resources, for the purpose of planning the optimal utilization prior to full training. | 11 | | `Fun with ML in Production` | A composition of fun things (tricks) that one may try in ML production environment | 12 | | `community-labs` | notebooks for community-based research experiments` | 13 | 14 | ### Prediction with Example Key 15 | 16 | #### Problem 17 | 18 | You have an existing model that does prediction which is put into production. Due to the load, the serving side uses a distributed (load balanced) batch prediction. 19 | 20 | How does one identity which prediction goes with which request when the predictions are returned "real-time" asynchronous, where you don't know the order that they will be returned in? 21 | 22 | #### Solution 23 | 24 | The solution is very trival. We simply create a wrapper model around the existing pretrained model using tf.keras multiple inputs/outputs functionality in the Functional API to add an identity link which passes a unique identifier per request to the output layer; whereby, each prediction returns the prediction result and the unique identifier. 25 | 26 | ### Pre-Stem Deconvolution 27 | 28 | #### Problem 29 | 30 | You have an existing model architecture optimized for an input shape which is put into production. The model is repurposed to take inputs of a substantially smaller image size. 31 | 32 | How does one use a small input size on a model designed for substantially larger input size and maintain comparable performance? 33 | 34 | #### Solution 35 | 36 | The solution is very trival. We simply create an additional group that is added to the input layer of the existing model, which consists of deconvolution layers to learn the optimal method to upsample the images to match the input shape of the input layer of the existing model. 37 | 38 | ### Building Data Preprocessing into the Graph 39 | 40 | #### Problem 41 | 42 | TF 2.0 has a ton of new rich features and power. Before TF 2.0, preprocessing of your data was done upstream on the CPU, which could starve the GPU's from running at full throttle. Think of this upstream preprocessing as the fuel, and if constrained, the GPU is choking waiting for fuel. 43 | 44 | #### Solution 45 | 46 | In this notebook, we show you how easy it is to do this, by creating a new input layer using subclassing of tf.keras.layers, and the @tf.function decorator to convert the Python code for preprocessing into TF graph operations. Another benefit to this, is that if you later want to change the preprocessing w/o re-training the model, you can simple replace the new preprocessing layer in your existing model, since it has no trainable weights. 47 | 48 | ### Estimating your CPU/GPU utilization for Training 49 | 50 | #### Problem 51 | 52 | Currently training infrastructure does not do auto-scaling (unlike batch prediction). Instead, you sent your utilization strategy as part of starting your training job. 53 | 54 | If your training on the cloud, a poor utilization may result in an under or over utilization. In under utilization, you're leaving compute power (money) on the table. In over utilization, the training job may become bottleneck or excessively interrupted by other processes. 55 | 56 | Things you might consider when under utilizing. Do I scale up (larger instances) or do I scale out (distributed training). 57 | 58 | #### Solution 59 | 60 | In this notebook, we use short training runs (warm-start) combined with the `psutil` module to see what our utilization will be when we do a full training run. Since we are only interested in utilization, we don't care what the accuracy is --we can just use a defacto (best guess) on hyperparameters. 61 | 62 | In my experience, I find the sweetspot for utilization on a single instance is 70%. That leaves enough compute power from background processes pre-empting the training and if training headless, to be able to ssh in and monitor the system. 63 | 64 | ### Fun with ML in Production 65 | 66 | #### Topics 67 | 68 | This notebook demonstrates a compilation of techniques one might consider when training in a modern production environment. Covered are: 69 | 70 | * Model Aggregation & Model Cutout 71 | * Adding Layers to Trained Models 72 | * Auxilliary Classifiers (why not to use them) 73 | -------------------------------------------------------------------------------- /presentations/Automatic Learning.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/presentations/Automatic Learning.pdf -------------------------------------------------------------------------------- /presentations/Full-In Production.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/presentations/Full-In Production.pptx -------------------------------------------------------------------------------- /presentations/ML in the Workplace.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/presentations/ML in the Workplace.pptx -------------------------------------------------------------------------------- /presentations/TF 2.0_ Transitioning to Production.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/presentations/TF 2.0_ Transitioning to Production.pptx -------------------------------------------------------------------------------- /what-is-idiomatic.md: -------------------------------------------------------------------------------- 1 | # Idiomatic Teaching 2 | 3 | The material in this repo is designed for the idiomatic teaching method. It differs from the traditional A-Z methodology. In A-Z, 4 | education/training material is split into a set of tasks (A, B, C...) and then sequenced into a progression (start at A and end at Z). 5 | 6 | The vast proponderance of education/training material available (and its quite vast) for machine learning and artificial intelligence 7 | is of the A-Z method. At Google Developer Relations, we have heard from software developers that it's not working. They say they 8 | can go through the material, complete exercises, pass quizzes, etc -- yet it still doesn't click. Personally, as an educator, I have 9 | heard this frequently. 10 | 11 | In Idiomatic teaching, one teaches patterns, and how those patterns are connected. Many will feel instead of starting at the beginning 12 | and doing a vertical progression, one starts in the middle and then spirals around -- always teaching patterns and how they connect. 13 | What I have personal found switching from A-Z to Idiomatic teaching of machine learning in 2018, were my students telling me now I get 14 | it --thank you. It's a style of teaching that appears to work universally. 15 | 16 | ## What are the elements of Idiomatic Teaching 17 | 18 | - It's inclusive - eliminate all gender specific phrasing, and replace with us, we and one. 19 | - It's welcoming - frequent use of the word let's, where students feel let's learn together. 20 | - It's lifelong learning - instructor present themselves not as a master, but master/student -- while having mastered the subject, the instructor continues to learn from the students. 21 | - It's exploring - eliminate pushing to correct answers, and replace with exercises that push the learner to explore. 22 | - It's bias free - eliminate rewards for excellence, make learning non-judgemental, reward intiative, and intuition. 23 | - It's intuitive - connecting with patterns and examples. 24 | 25 | ## Objective 26 | 27 | The education is inclusive where no software developer feels they will be left behind. 28 | 29 | -------------------------------------------------------------------------------- /workshops/Advanced_Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Advanced CNN.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Advanced_Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Advanced CNN.pptx -------------------------------------------------------------------------------- /workshops/Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Convolutional Neural Networks.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Convolutional Neural Networks.pptx -------------------------------------------------------------------------------- /workshops/Data_Augmentation/The Idiomatic Programmer - Learning Keras - Workshop - Data Augmentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Data_Augmentation/The Idiomatic Programmer - Learning Keras - Workshop - Data Augmentation.pptx -------------------------------------------------------------------------------- /workshops/Data_Augmentation/apple.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Data_Augmentation/apple.jpg -------------------------------------------------------------------------------- /workshops/Data_Engineering/The Idiomatic Programmer - Learning Keras - Workshop - Data Engineering.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Data_Engineering/The Idiomatic Programmer - Learning Keras - Workshop - Data Engineering.pptx -------------------------------------------------------------------------------- /workshops/Data_Engineering/apple.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Data_Engineering/apple.jpg -------------------------------------------------------------------------------- /workshops/Mobile_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Mobile Networks.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Mobile_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Mobile Networks.pptx -------------------------------------------------------------------------------- /workshops/Modern_CNN/Modern Convolutional Neural Network Architectures.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Modern_CNN/Modern Convolutional Neural Network Architectures.pdf -------------------------------------------------------------------------------- /workshops/Modern_CNN/Modern Convolutional Neural Network Architectures.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Modern_CNN/Modern Convolutional Neural Network Architectures.pptx -------------------------------------------------------------------------------- /workshops/Modern_CNN/README.md: -------------------------------------------------------------------------------- 1 | 2 | ### Would you like to understand how AutoML works under the hood? 3 |
4 | A deep understanding of AutoML, a new approach based on new design patterns, latest research -- turning AutoML from 5 | a blackbox to one where the data scientist can bring their own custom macro/micro architectures and self-guide the 6 | search space. 7 |
8 | -------------------------------------------------------------------------------- /workshops/Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Neural Networks.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Neural Networks.pptx -------------------------------------------------------------------------------- /workshops/Production/The Idiomatic Programmer - Production - Workshop.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Production/The Idiomatic Programmer - Production - Workshop.pptx -------------------------------------------------------------------------------- /workshops/README.md: -------------------------------------------------------------------------------- 1 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) 2 | [![License](https://i.creativecommons.org/l/by/4.0/80x15.png)](LICENSE) 3 | 4 | # Workshops 5 | 6 | This folder contains packets which can be used as workshops or study jams. Each packet contains presentation slides and 7 | accompanying code labs. The presentation materials are licensed as CC-BY 4.0 and the code labs as Apache License 2.0 --you are 8 | free to use, modify and redistribute with the requirement to retain attribution to Google LLC. 9 | 10 | Each workshop packet is designed for a 2 hour workshop or study jam. The code labs can be ran either on attendee's laptop or on Google Cloud using [colab](https://colab.research.google.com). 11 | 12 | ## 2019 13 | | Workshop | Level | 14 | | ------------- | ------------- | 15 | | *Models* | | 16 | | `Neural Networks` | Fundamental | 17 | | `Convolutional Neural Networks` | Fundamental | 18 | | `Wide Convolutional Neural Networks` | Intermediate | 19 | | `Advanced Convolutional Neural Networks` | Intermediate-Advanced | 20 | | `Mobile Convolutional Neural Networks` | Intermediate-Advanced | 21 | | *Data* | | 22 | | `Data Engineering` | Fundamental |
23 | | `Data Augmentation` | Intermediate-Advanced |
24 | | *Training* | | 25 | | `Training` | Fundamental | 26 | | `Transfer Learning` | Intermediate | 27 | | *Production* | | 28 | | `Production` | Intermediate-Advanced | 29 | | `Modern CNN` | Intermediate-Advanced | 30 | 31 | ## 2020 32 | 33 | | Workshop | Level | 34 | | ------------- | ------------- | 35 | | *Models* | | 36 | | *Data* | | 37 | | *Training* | | 38 | | *Production* | | 39 | | `Modern CNN` | Intermediate-Advanced | 40 | 41 | 42 | -------------------------------------------------------------------------------- /workshops/Training/The Idiomatic Programmer - Learning Keras - Workshop - Training.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Training/The Idiomatic Programmer - Learning Keras - Workshop - Training.pptx -------------------------------------------------------------------------------- /workshops/Transfer_Learning/The Idiomatic Programmer - Learning Keras - Workshop - Transfer Learning.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Transfer_Learning/The Idiomatic Programmer - Learning Keras - Workshop - Transfer Learning.pptx -------------------------------------------------------------------------------- /workshops/Wide_Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Wide Convolutional Neural Networks.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/workshops/Wide_Convolutional_Neural_Networks/The Idiomatic Programmer - Learning Keras - Workshop - Wide Convolutional Neural Networks.pptx -------------------------------------------------------------------------------- /zoo/alexnet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # AlexNet 3 | 4 | [alexnet.py](alexnet.py) - simplified (single path) academic (idiomatic)
5 | [alexnet_p.py](alexnet.py) - original (dual path) academic (idiomatic)
6 | 7 | [Paper](https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf) 8 | 9 | ## Macro-Architecture 10 | 11 | ### Simplified 12 | 13 | 14 | 15 | ### Paper 16 | 17 | 18 | 19 | ## Micro-Architecture 20 | 21 | ### Stem 22 | 23 | 24 | 25 | ### Classifier 26 | 27 | 28 | -------------------------------------------------------------------------------- /zoo/alexnet/alexnet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AlexNet (2012) - simplified as a single path 16 | # Paper: https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Model, Input 20 | from tensorflow.keras.layers import Conv2D, ReLU, MaxPooling2D, Dense, Flatten 21 | 22 | def stem(inputs): 23 | """ Construct the Stem Convolutional Group 24 | inputs : the input vector 25 | """ 26 | # First Convolutional layer which uses an extremely large (coarse) filter 27 | x = Conv2D(96, (11, 11), strides=(4, 4), padding='same')(inputs) 28 | x = ReLU()(x) 29 | 30 | # Second Convolutional layer 31 | x = Conv2D(256, (5, 5), strides=(1, 1), padding='same')(x) 32 | x = ReLU()(x) 33 | 34 | # Pooled feature maps will be reduced by 75% 35 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 36 | return x 37 | 38 | def learner(x): 39 | """ Construct the Learner 40 | x : input to the learner 41 | """ 42 | # Third Convolutional layer 43 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same')(x) 44 | x = ReLU()(x) 45 | 46 | # Pooled feature maps will be reduced by 75% 47 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 48 | 49 | # Fourth Convolutional layer 50 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same')(x) 51 | x = ReLU()(x) 52 | 53 | # Ffth Convolutional layer 54 | x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x) 55 | x = ReLU()(x) 56 | 57 | # Pooled feature maps will be reduced by 75% 58 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 59 | 60 | return x 61 | 62 | def classifier(x, n_classes): 63 | """ Construct the Classifier Group 64 | x : input to the classifier 65 | n_classes : number of output classes 66 | """ 67 | # Flatten into 1D vector 68 | x = Flatten()(x) 69 | 70 | # Two dense layers of 4096 71 | x = Dense(4096, activation='relu')(x) 72 | x = Dense(4096, activation='relu')(x) 73 | 74 | # Final Dense Outputting Layer for the outputs 75 | outputs = Dense(n_classes, activation='softmax')(x) 76 | return outputs 77 | 78 | 79 | # The input tensor 80 | inputs = Input(shape=(224, 224, 3)) 81 | 82 | # The stem convolutional group 83 | x = stem(inputs) 84 | 85 | # The learner 86 | x = learner(x) 87 | 88 | # The classifier for 1000 classes 89 | outputs = classifier(x, 1000) 90 | 91 | # Instantiate the Model 92 | # model = Model(inputs, outputs) 93 | -------------------------------------------------------------------------------- /zoo/alexnet/alexnet_p.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AlexNet (2012) - Parallel path version for 2 GPUs 16 | # Paper: https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Model, Input 20 | from tensorflow.keras.layers import Conv2D, ReLU, MaxPooling2D, Dense, Flatten, Concatenate 21 | 22 | def stem(inputs): 23 | """ Construct the Stem Convolutional Group 24 | inputs : the input vector 25 | """ 26 | # First Convolutional layer which uses an extremely large (coarse) filter 27 | x = Conv2D(96, (11, 11), strides=(4, 4), padding='same')(inputs) 28 | x = ReLU()(x) 29 | 30 | # Second Convolutional layer (1st path) 31 | x1 = Conv2D(128, (5, 5), strides=(1, 1), padding='same')(x) 32 | x1 = ReLU()(x1) 33 | 34 | # Pooled feature maps will be reduced by 75% (1st path) 35 | x1 = MaxPooling2D((3, 3), strides=(2, 2))(x1) 36 | 37 | # Second Convolutional layer (2nd path) 38 | x2 = Conv2D(128, (5, 5), strides=(1, 1), padding='same')(x) 39 | x2 = ReLU()(x2) 40 | 41 | # Pooled feature maps will be reduced by 75% (2nd path) 42 | x2 = MaxPooling2D((3, 3), strides=(2, 2))(x2) 43 | 44 | return x1, x2 45 | 46 | def learner(x1, x2): 47 | """ Construct the Learner 48 | x1, x2 : inputs to the learner 49 | """ 50 | # Third Convolutional layer (1st path) 51 | x1 = Conv2D(192, (3, 3), strides=(1, 1), padding='same')(x1) 52 | x1 = ReLU()(x1) 53 | # Pooled feature maps will be reduced by 75% (1st path) 54 | x1 = MaxPooling2D((3, 3), strides=(2, 2))(x1) 55 | 56 | # Third Convolutional layer (2nd path) 57 | x2 = Conv2D(192, (3, 3), strides=(1, 1), padding='same')(x2) 58 | x2 = ReLU()(x2) 59 | # Pooled feature maps will be reduced by 75% (2nd path) 60 | x2 = MaxPooling2D((3, 3), strides=(2, 2))(x2) 61 | 62 | # Fourth Convolutional layer (1st path) 63 | x1 = Conv2D(192, (3, 3), strides=(1, 1), padding='same')(x1) 64 | x1 = ReLU()(x1) 65 | 66 | # Fourth Convolutional layer (2nd path) 67 | x2 = Conv2D(192, (3, 3), strides=(1, 1), padding='same')(x2) 68 | x2 = ReLU()(x2) 69 | 70 | # Ffth Convolutional layer (1st path) 71 | x1 = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x1) 72 | x1 = ReLU()(x1) 73 | # Pooled feature maps will be reduced by 75% (1st path) 74 | x1 = MaxPooling2D((3, 3), strides=(2, 2))(x1) 75 | 76 | # Ffth Convolutional layer (2nd path) 77 | x2 = Conv2D(128, (3, 3), strides=(1, 1), padding='same')(x2) 78 | x2 = ReLU()(x2) 79 | # Pooled feature maps will be reduced by 75% (2nd path) 80 | x2 = MaxPooling2D((3, 3), strides=(2, 2))(x2) 81 | 82 | return x1, x2 83 | 84 | def classifier(x1, x2, n_classes): 85 | """ Construct the Classifier Group 86 | x1, x2 : inputs to the classifier 87 | n_classes : number of output classes 88 | """ 89 | # Flatten into 1D vector (1st path) 90 | x1 = Flatten()(x1) 91 | # Flatten into 1D vector (2n path) 92 | x2 = Flatten()(x2) 93 | 94 | # Two dense layers of 2048 (1st path) 95 | x1 = Dense(2048, activation='relu')(x1) 96 | x1 = Dense(2048, activation='relu')(x1) 97 | # Two dense layers of 2048 (2nd path) 98 | x2 = Dense(2048, activation='relu')(x2) 99 | x2 = Dense(2048, activation='relu')(x2) 100 | 101 | # Concatenate the feature maps from the two parallel paths 102 | x = Concatenate()([x1, x2]) 103 | 104 | # Final Dense Outputting Layer for the outputs 105 | outputs = Dense(n_classes, activation='softmax')(x) 106 | return outputs 107 | 108 | 109 | # The input tensor 110 | inputs = Input(shape=(224, 224, 3)) 111 | 112 | # The stem convolutional group 113 | x1, x2 = stem(inputs) 114 | 115 | # The learner 116 | x1, x2 = learner(x1, x2) 117 | 118 | # The classifier for 1000 classes 119 | outputs = classifier(x1, x2, 1000) 120 | 121 | # Instantiate the Model 122 | # model = Model(inputs, outputs) 123 | -------------------------------------------------------------------------------- /zoo/alexnet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/alexnet/classifier.jpg -------------------------------------------------------------------------------- /zoo/alexnet/macro-simplified.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/alexnet/macro-simplified.jpg -------------------------------------------------------------------------------- /zoo/alexnet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/alexnet/macro.jpg -------------------------------------------------------------------------------- /zoo/alexnet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/alexnet/stem.jpg -------------------------------------------------------------------------------- /zoo/autoencoder/README.md: -------------------------------------------------------------------------------- 1 | 2 | # AutoEncoder 3 | 4 | [autoencoder.py](autoencoder.py) - academic (idomatic)
5 | [autoencoder_c.py](autoencoder_c.py) - production (composable) 6 | 7 | 8 | ## Macro-Architecture 9 | 10 | 11 | 12 | ## Micro-Architecture 13 | 14 | ### Encoder 15 | 16 | 17 | 18 | ### Decoder 19 | 20 | 21 | 22 | 23 | ## Composable 24 | 25 | *Example Instantiate a AutoEncoder model* 26 | 27 | ```python 28 | from autoencoder_c import AutoEncoder 29 | 30 | # Create an AutoEncoder for 32x32x3 (CIFAR-10) 31 | autoencoder = AutoEncoder() 32 | 33 | # Create a custom AutoEncoder 34 | autoencoder = AutoEncoder(input_shape=(128, 128, 3), layers=[128, 64, 32]) 35 | ``` 36 | -------------------------------------------------------------------------------- /zoo/autoencoder/autoencoder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AutoEncoder - Pooling with Dense Layers/Hidden Units 16 | 17 | import tensorflow as tf 18 | from tensorflow.keras import Model, Input 19 | from tensorflow.keras.layers import Dense, ReLU, BatchNormalization, Reshape, Flatten 20 | 21 | def encoder(x, layers): 22 | ''' Construct the Encoder 23 | x : input to the encoder 24 | layers: number of nodes per layer 25 | ''' 26 | 27 | # Flatten the input image 28 | x = Flatten()(x) 29 | 30 | # Progressive Unit Pooling 31 | for layer in layers: 32 | n_nodes = layer['n_nodes'] 33 | x = Dense(n_nodes)(x) 34 | x = BatchNormalization()(x) 35 | x = ReLU()(x) 36 | 37 | # The Encoding 38 | return x 39 | 40 | def decoder(x, layers, input_shape): 41 | ''' Construct the Decoder 42 | x : input to the decoder 43 | layers: nodes per layer 44 | ''' 45 | 46 | # Progressive Unit Unpooling 47 | for _ in range(len(layers)-1, 0, -1): 48 | n_nodes = layers[_]['n_nodes'] 49 | x = Dense(n_nodes)(x) 50 | x = BatchNormalization()(x) 51 | x = ReLU()(x) 52 | 53 | # Last unpooling and match shape to input 54 | units = input_shape[0] * input_shape[1] * input_shape[2] 55 | x = Dense(units, activation='sigmoid')(x) 56 | 57 | # Reshape back into an image 58 | outputs = Reshape(input_shape)(x) 59 | 60 | # The decoded image 61 | return outputs 62 | 63 | def example(): 64 | ''' Example for constructing/training an AutoEncoder model on MNIST 65 | ''' 66 | # Example of constructing an AutoEncoder 67 | # metaparameter: number of filters per layer 68 | layers = [ {'n_nodes': 256 }, { 'n_nodes': 128 }, { 'n_nodes': 64 } ] 69 | 70 | inputs = Input((28, 28, 1)) 71 | _encoder = encoder(inputs, layers) 72 | outputs = decoder(_encoder, layers, (28, 28, 1)) 73 | ae = Model(inputs, outputs) 74 | 75 | ae.summary() 76 | 77 | from tensorflow.keras.datasets import mnist 78 | import numpy as np 79 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 80 | x_train = (x_train / 255.0).astype(np.float32) 81 | x_test = (x_test / 255.0).astype(np.float32) 82 | x_train = np.expand_dims(x_train, axis=-1) 83 | x_test = np.expand_dims(x_test, axis=-1) 84 | 85 | ae.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) 86 | ae.fit(x_train, x_train, epochs=10, batch_size=32, validation_split=0.1, verbose=1) 87 | ae.evaluate(x_test, x_test) 88 | 89 | 90 | example() 91 | -------------------------------------------------------------------------------- /zoo/autoencoder/autoencoder_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AutoEncoder - Pooling with Dense Layers/Hidden Units 16 | 17 | import tensorflow as tf 18 | from tensorflow.keras import Model, Input 19 | from tensorflow.keras.layers import Dense, ReLU, BatchNormalization, Reshape, Flatten 20 | 21 | import sys 22 | sys.path.append('../') 23 | from models_c import Composable 24 | 25 | class AutoEncoder(Composable): 26 | ''' Construct an AutoEncoder ''' 27 | # metaparameter: number of filters per layer 28 | layers = [ {'n_nodes': 256 }, { 'n_nodes': 128 }, { 'n_nodes': 64 } ] 29 | 30 | # Initial Hyperparameters 31 | hyperparameters = { 'initializer': 'he_normal', 32 | 'regularizer': None, 33 | 'relu_clip' : None, 34 | 'bn_epsilon' : None, 35 | 'use_bias' : False 36 | } 37 | 38 | def __init__(self, layers=None, input_shape=(32, 32, 3), 39 | **hyperparameters): 40 | ''' Construct an AutoEncoder 41 | input_shape : input shape to the autoencoder 42 | layers : the number of filters per layer 43 | initializer : kernel initializer 44 | regularizer : kernel regularizer 45 | relu_clip : clip value for ReLU 46 | bn_epsilon : epsilon for batch normalization 47 | use_bias : whether to use bias 48 | ''' 49 | # Configure base (super) class 50 | Composable.__init__(self, self.hyperparameters, **hyperparameters) 51 | 52 | if layers is None: 53 | layers = self.layers 54 | 55 | # remember the layers 56 | self.layers = layers 57 | 58 | # remember the input shape 59 | self.input_shape = input_shape 60 | 61 | inputs = Input(input_shape) 62 | encoder = self.encoder(inputs, layers=layers) 63 | outputs = self.decoder(encoder, layers=layers) 64 | self._model = Model(inputs, outputs) 65 | 66 | def encoder(self, x, **metaparameters): 67 | ''' Construct the Encoder 68 | x : input to the encoder 69 | layers: number of nodes per layer 70 | ''' 71 | layers = metaparameters['layers'] 72 | 73 | # Flatten the input image 74 | x = Flatten()(x) 75 | 76 | # Progressive Unit Pooling 77 | for layer in layers: 78 | n_nodes = layer['n_nodes'] 79 | x = self.Dense(x, n_nodes) 80 | x = self.BatchNormalization(x) 81 | x = self.ReLU(x) 82 | 83 | # The Encoding 84 | return x 85 | 86 | def decoder(self, x, **metaparameters): 87 | ''' Construct the Decoder 88 | x : input to the decoder 89 | layers: number of nodes per layer 90 | ''' 91 | layers = metaparameters['layers'] 92 | 93 | # Progressive Unit Unpooling 94 | for _ in range(len(layers)-1, 0, -1): 95 | n_nodes = layers[_]['n_nodes'] 96 | x = self.Dense(x, n_nodes) 97 | x = self.BatchNormalization(x) 98 | x = self.ReLU(x) 99 | 100 | # Last unpooling and match shape to input 101 | units = self.input_shape[0] * self.input_shape[1] * self.input_shape[2] 102 | print("INPUT", self.input_shape, "UNITS", units) 103 | x = self.Dense(x, units, activation='sigmoid') 104 | 105 | # Reshape back into an image 106 | x = Reshape(self.input_shape)(x) 107 | 108 | # The decoded image 109 | return x 110 | 111 | def compile(self, optimizer='adam'): 112 | ''' Compile the model ''' 113 | self._model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy']) 114 | 115 | def extract(self): 116 | ''' Extract the pretrained encoder 117 | ''' 118 | # Get the trained weights from the autoencoder 119 | weights = self._model.get_weights() 120 | 121 | # Extract out the weights for just the encoder (6 sets per layer) 122 | encoder_weights = weights[0 : int((6 * len(self.layers)))] 123 | 124 | # Construct a copy the encoder 125 | inputs = Input(self.input_shape) 126 | outputs = self.encoder(inputs, layers=self.layers) 127 | encoder = Model(inputs, outputs) 128 | 129 | # Initialize the encoder with the pretrained weights 130 | encoder.set_weights(encoder_weights) 131 | 132 | return encoder 133 | 134 | # Example autoencoder 135 | # autoencoder = AutoEncoder() 136 | 137 | # Train the model, and extract pretrained encoder 138 | # e = autoencoder.extract() 139 | 140 | def example(): 141 | ''' Example for constructing/training an AutoEncoder model on MNIST 142 | ''' 143 | # Example of constructing an AutoEncoder 144 | ae = AutoEncoder(input_shape=(28, 28, 1)) 145 | ae.model.summary() 146 | 147 | from tensorflow.keras.datasets import mnist 148 | import numpy as np 149 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 150 | x_train = (x_train / 255.0).astype(np.float32) 151 | x_test = (x_test / 255.0).astype(np.float32) 152 | x_train = np.expand_dims(x_train, axis=-1) 153 | x_test = np.expand_dims(x_test, axis=-1) 154 | 155 | ae.compile() 156 | ae.model.fit(x_train, x_train, epochs=10, batch_size=32, validation_split=0.1, verbose=1) 157 | ae.model.evaluate(x_test, x_test) 158 | 159 | 160 | # example() 161 | -------------------------------------------------------------------------------- /zoo/autoencoder/dc_autoencoder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AutoEncoder 16 | 17 | import tensorflow as tf 18 | from tensorflow.keras import Model, Input 19 | from tensorflow.keras.layers import Conv2D, Conv2DTranspose, ReLU, BatchNormalization 20 | 21 | def encoder(inputs, layers): 22 | """ Construct the Encoder 23 | inputs : the input vector 24 | layers : number of filters per layer 25 | """ 26 | x = inputs 27 | 28 | # Feature pooling by 1/2H x 1/2W 29 | for n_filters in layers: 30 | x = Conv2D(n_filters, (3, 3), strides=(2, 2), padding='same', use_bias=False, kernel_initializer='he_normal')(x) 31 | x = BatchNormalization()(x) 32 | x = ReLU()(x) 33 | 34 | return x 35 | 36 | def decoder(x, layers): 37 | """ Construct the Decoder 38 | x : input to decoder 39 | layers : the number of filters per layer (in encoder) 40 | """ 41 | 42 | # Feature unpooling by 2H x 2W 43 | for _ in range(len(layers)-1, 0, -1): 44 | n_filters = layers[_] 45 | x = Conv2DTranspose(n_filters, (3, 3), strides=(2, 2), padding='same', use_bias=False, kernel_initializer='he_normal')(x) 46 | x = BatchNormalization()(x) 47 | x = ReLU()(x) 48 | 49 | # Last unpooling, restore number of channels 50 | x = Conv2DTranspose(3, (3, 3), strides=(2, 2), padding='same', use_bias=False, kernel_initializer='he_normal')(x) 51 | x = BatchNormalization()(x) 52 | x = ReLU()(x) 53 | 54 | return x 55 | 56 | # metaparameter: number of filters per layer in encoder 57 | layers = [64, 32, 32] 58 | 59 | # The input tensor 60 | inputs = Input(shape=(32, 32, 3)) 61 | 62 | # The encoder 63 | x = encoder(inputs, layers) 64 | 65 | # The decoder 66 | outputs = decoder(x, layers) 67 | 68 | # Instantiate the Model 69 | model = Model(inputs, outputs) 70 | -------------------------------------------------------------------------------- /zoo/autoencoder/dc_autoencoder_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # AutoEncoder 16 | 17 | import tensorflow as tf 18 | from tensorflow.keras import Model, Input 19 | from tensorflow.keras.layers import Conv2D, Conv2DTranspose, ReLU, BatchNormalization 20 | 21 | import sys 22 | sys.path.append('../') 23 | from models_c import Composable 24 | 25 | class AutoEncoder(Composable): 26 | ''' Construct an AutoEncoder ''' 27 | # metaparameter: number of filters per layer 28 | layers = [ {'n_filters': 64 }, { 'n_filters': 32 }, { 'n_filters': 16 } ] 29 | 30 | # Initial Hyperparameters 31 | hyperparameters = { 'initializer': 'he_normal', 32 | 'regularizer': None, 33 | 'relu_clip' : None, 34 | 'bn_epsilon' : None, 35 | 'use_bias' : False 36 | } 37 | 38 | def __init__(self, layers=None, input_shape=(32, 32, 3), 39 | **hyperparameters): 40 | ''' Construct an AutoEncoder 41 | input_shape : input shape to the autoencoder 42 | layers : the number of filters per layer 43 | initializer : kernel initializer 44 | regularizer : kernel regularizer 45 | relu_clip : clip value for ReLU 46 | bn_epsilon : epsilon for batch norm 47 | use_bias : whether to use bias 48 | ''' 49 | # Configure base (super) class 50 | Composable.__init__(self, input_shape, None, self.hyperparameters, **hyperparameters) 51 | 52 | if layers is None: 53 | layers = self.layers 54 | 55 | # remember the layers 56 | self.layers = layers 57 | 58 | # remember the input shape 59 | self.input_shape = input_shape 60 | 61 | inputs = Input(input_shape) 62 | encoder = self.encoder(inputs, layers=layers) 63 | outputs = self.decoder(encoder, layers=layers) 64 | self._model = Model(inputs, outputs) 65 | 66 | def encoder(self, x, **metaparameters): 67 | ''' Construct the Encoder 68 | x : input to the encoder 69 | layers: number of filters per layer 70 | ''' 71 | layers = metaparameters['layers'] 72 | 73 | # Progressive Feature Pooling 74 | for layer in layers: 75 | n_filters = layer['n_filters'] 76 | x = self.Conv2D(x, n_filters, (3, 3), strides=2, padding='same') 77 | x = self.BatchNormalization(x) 78 | x = self.ReLU(x) 79 | 80 | # The Encoding 81 | return x 82 | 83 | def decoder(self, x, init_weights=None, **metaparameters): 84 | ''' Construct the Decoder 85 | x : input to the decoder 86 | layers: filters per layer 87 | ''' 88 | layers = metaparameters['layers'] 89 | 90 | # Progressive Feature Unpooling 91 | for _ in range(len(layers)-1, 0, -1): 92 | n_filters = layers[_]['n_filters'] 93 | x = self.Conv2DTranspose(x, n_filters, (3, 3), strides=2, padding='same') 94 | x = self.BatchNormalization(x) 95 | x = self.ReLU(x) 96 | 97 | # Last unpooling and match shape to input 98 | x = self.Conv2DTranspose(x, 3, (3, 3), strides=2, padding='same') 99 | x = self.BatchNormalization(x) 100 | x = self.ReLU(x) 101 | 102 | # The decoded image 103 | return x 104 | 105 | def compile(self, optimizer='adam'): 106 | ''' Compile the model using Mean Square Error loss ''' 107 | self._model.compile(loss='mse', optimizer=optimizer, metrics=['accuracy']) 108 | 109 | def extract(self): 110 | ''' Extract the pretrained encoder 111 | ''' 112 | # Get the trained weights from the autoencoder 113 | weights = self._model.get_weights() 114 | 115 | # Extract out the weights for just the encoder (6 sets per layer) 116 | encoder_weights = weights[0 : int((6 * len(self.layers)))] 117 | 118 | # Construct a copy the encoder 119 | inputs = Input(self.input_shape) 120 | outputs = self.encoder(inputs, layers=self.layers) 121 | encoder = Model(inputs, outputs) 122 | 123 | # Initialize the encoder with the pretrained weights 124 | encoder.set_weights(encoder_weights) 125 | 126 | return encoder 127 | 128 | # Example autoencoder 129 | # autoencoder = AutoEncoder() 130 | 131 | # Train the model, and extract pretrained encoder 132 | # e = autoencoder.extract() 133 | 134 | def example(): 135 | ''' Example for constructing/training an AutoEncoder model on CIFAR-10 136 | ''' 137 | # Example of constructing an AutoEncoder 138 | ae = AutoEncoder(input_shape=(32, 32, 3)) 139 | ae.model.summary() 140 | 141 | from tensorflow.keras.datasets import cifar10 142 | import numpy as np 143 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 144 | x_train = (x_train / 255.0).astype(np.float32) 145 | x_test = (x_test / 255.0).astype(np.float32) 146 | 147 | ae.compile() 148 | ae.model.fit(x_train, x_train, epochs=10, batch_size=32, validation_split=0.1, verbose=1) 149 | ae.model.evaluate(x_test, x_test) 150 | 151 | 152 | # example() 153 | -------------------------------------------------------------------------------- /zoo/autoencoder/decoder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/autoencoder/decoder.jpg -------------------------------------------------------------------------------- /zoo/autoencoder/encoder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/autoencoder/encoder.jpg -------------------------------------------------------------------------------- /zoo/autoencoder/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/autoencoder/macro.jpg -------------------------------------------------------------------------------- /zoo/datasets_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import tensorflow as tf 16 | from tensorflow.keras import Sequential, Model, Input 17 | from tensorflow.keras import layers 18 | from tensorflow.keras.layers import ReLU, Dense, Conv2D, Conv2DTranspose 19 | from tensorflow.keras.layers import DepthwiseConv2D, SeparableConv2D, Dropout 20 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation, BatchNormalization 21 | from tensorflow.keras.regularizers import l2 22 | from tensorflow.keras.optimizers import Adam, SGD 23 | from tensorflow.compat.v1.keras.initializers import glorot_uniform, he_normal 24 | from tensorflow.keras.callbacks import LearningRateScheduler 25 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 26 | from tensorflow.keras.utils import to_categorical 27 | import tensorflow_datasets as tfds 28 | import tensorflow.keras.backend as K 29 | import numpy as np 30 | from sklearn.model_selection import train_test_split 31 | 32 | import random 33 | import math 34 | import sys 35 | 36 | from layers_c import Layers 37 | from preprocess_c import Preprocess 38 | from pretraining_c import Pretraining 39 | from hypertune_c import HyperTune 40 | from training_c import Training 41 | 42 | class Dataset(object): 43 | ''' Dataset base (super) class for Models ''' 44 | 45 | def __init__(self): 46 | """ Constructor 47 | """ 48 | self.x_train = None 49 | self.y_train = None 50 | self.x_test = None 51 | self.y_test = None 52 | self.n_classes = 0 53 | 54 | @property 55 | def data(self): 56 | return (x_train, y_train), (x_test, y_test) 57 | 58 | def load_data(self, train, test=None, std=False, onehot=False, smoothing=0.0): 59 | """ Load in memory data 60 | train: expect form: (x_train, y_train) 61 | """ 62 | self.x_train, self.y_train = train 63 | if test is not None: 64 | self.x_test, self.y_test = test 65 | if std: 66 | self.x_train, self.x_test = self.standardization(self.x_train, self.x_test) 67 | 68 | if self.y_train.ndim == 2: 69 | self.n_classes = np.max(self.y_train) + 1 70 | else: 71 | self.n_classes = self.y_train.shape[1] 72 | if onehot: 73 | self.y_train = to_categorical(self.y_train, self.n_classes) 74 | self.y_test = to_categorical(self.y_test, self.n_classes) 75 | if smoothing > 0.0: 76 | self.y_train = self.label_smoothing(self.y_train, self.n_classes, smoothing) 77 | 78 | def cifar10(self, epochs=10, decay=('cosine', 0)): 79 | """ Train on CIFAR-10 80 | epochs : number of epochs for full training 81 | """ 82 | from tensorflow.keras.datasets import cifar10 83 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 84 | x_train, x_test = self.standardization(x_train, x_test) 85 | y_train = to_categorical(y_train, 10) 86 | y_test = to_categorical(y_test, 10) 87 | y_train = self.label_smoothing(y_train, 10, 0.1) 88 | 89 | # compile the model 90 | self.compile(loss='categorical_crossentropy', metrics=['acc']) 91 | 92 | self.warmup(x_train, y_train) 93 | 94 | lr, batch_size = self.random_search(x_train, y_train, x_test, y_test) 95 | 96 | self.training(x_train, y_train, epochs=epochs, batch_size=batch_size, 97 | lr=lr, decay=decay) 98 | self.evaluate(x_test, y_test) 99 | 100 | def cifar100(self, epochs=20, decay=('cosine', 0)): 101 | """ Train on CIFAR-100 102 | epochs : number of epochs for full training 103 | """ 104 | from tensorflow.keras.datasets import cifar100 105 | (x_train, y_train), (x_test, y_test) = cifar100.load_data() 106 | x_train, x_test = self.normalization(x_train, x_test) 107 | y_train = to_categorical(y_train, 100) 108 | y_test = to_categorical(y_test, 100) 109 | y_train = self.label_smoothing(y_train, 10, 0.1) 110 | self.compile(loss='categorical_crossentropy', metrics=['acc']) 111 | 112 | self.warmup(x_train, y_train) 113 | 114 | lr, batch_size = self.grid_search(x_train, y_train, x_test, y_test) 115 | 116 | self.training(x_train, y_train, epochs=epochs, batch_size=batch_size, 117 | lr=lr, decay=decay) 118 | self.evaluate(x_test, y_test) 119 | 120 | def coil100(self, epochs=20, decay=('cosine', 0)): 121 | """ 122 | """ 123 | # Get TF.dataset generator for COIL100 124 | train, info = tfds.load('coil100', split='train', shuffle_files=True, with_info=True, as_supervised=True) 125 | n_classes = info.features['label'].num_classes 126 | n_images = info.splits['train'].num_examples 127 | input_shape = info.features['image'].shape 128 | 129 | # Get the dataset into memory 130 | train = train.shuffle(n_images).batch(n_images) 131 | for images, labels in train.take(1): 132 | pass 133 | 134 | images = np.asarray(images) 135 | images, _ = self.standardization(images, None) 136 | labels = to_categorical(np.asarray(labels), n_classes) 137 | 138 | # split the dataset into train/test 139 | x_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.2) 140 | 141 | self.compile(loss='categorical_crossentropy', metrics=['acc']) 142 | 143 | self.warmup(x_train, y_train) 144 | 145 | lr, batch_size = self.grid_search(x_train, y_train, x_test, y_test) 146 | 147 | self.training(x_train, y_train, epochs=epochs, batch_size=batch_size, 148 | lr=lr, decay=decay) 149 | self.evaluate(x_test, y_test) 150 | 151 | -------------------------------------------------------------------------------- /zoo/dcgan/README.md: -------------------------------------------------------------------------------- 1 | # DCGAN 2 | 3 | [dcgan_c.py](dcgan_c.py) - production (composable) 4 | 5 | 6 | [Paper](https://arxiv.org/pdf/1511.06434.pdf) 7 | 8 | ## Macro-Architecture 9 | 10 | 11 | 12 | ### Generator 13 | 14 | 15 | 16 | ### Discriminator 17 | 18 | 19 | 20 | ## Composable 21 | 22 | *Example: Compose and Train for CIFAR-30* 23 | 24 | ``` 25 | gan = DCGAN(input_shape=(32, 32, 3)) 26 | gan.model.summary() 27 | 28 | from tensorflow.keras.datasets import cifar10 29 | import numpy as np 30 | (x_train, _), (_, _) = cifar10.load_data() 31 | x_train = x_train / 127.5 - 1. 32 | gan.train(x_train, latent=100, epochs=6000) 33 | ``` 34 | -------------------------------------------------------------------------------- /zoo/dcgan/macro-dis.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/dcgan/macro-dis.jpg -------------------------------------------------------------------------------- /zoo/dcgan/macro-gen.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/dcgan/macro-gen.jpg -------------------------------------------------------------------------------- /zoo/dcgan/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/dcgan/macro.jpg -------------------------------------------------------------------------------- /zoo/densenet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # DenseNet 3 | 4 | [densenet.py](densenet.py) - academic (idiomatic)
5 | [densenet_c.py](densenet_c.py) - production (composable) 6 | 7 | 8 | [Paper](https://arxiv.org/pdf/1608.06993.pdf) 9 | 10 | ## Macro-Architecture 11 | 12 | 13 | 14 | ## Micro-Architecture 15 | 16 | 17 | 18 | ### Stem Group 19 | 20 | 21 | 22 | ### Dense Block 23 | 24 | 25 | 26 | ### Transitional Block 27 | 28 | 29 | 30 | ### Residual Block 31 | 32 | 33 | 34 | ### Classifier 35 | 36 | 37 | 38 | ## Composable 39 | 40 | *Example Instantiate a stock DenseNet model* 41 | 42 | ```python 43 | from densenet_c import DenseNet 44 | 45 | # DenseNet121 from research paper 46 | densenet = DenseNet(121) 47 | 48 | # DenseNet121 custom input shape/classes 49 | densenet = DenseNet(121, input_shape=(128, 128, 3), n_classes=50) 50 | 51 | # getter for the tf.keras model 52 | model = densenet.model 53 | ``` 54 | 55 | *Example: Composable a Train a DenseNet* 56 | 57 | ```python 58 | ''' Example for constructing/training a DenseNet model on CIFAR-10 59 | ''' 60 | # Example of constructing a mini-DenseNet 61 | groups = [ { 'n_blocks': 3 }, { 'n_blocks': 6 }, { 'n_blocks': 12 } ] 62 | densenet = DenseNet(groups, input_shape=(32, 32, 3), n_classes=10) 63 | densenet.model.summary() 64 | densenet.cifar10() 65 | ``` 66 | -------------------------------------------------------------------------------- /zoo/densenet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/classifier.jpg -------------------------------------------------------------------------------- /zoo/densenet/dense-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/dense-block.jpg -------------------------------------------------------------------------------- /zoo/densenet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/macro.jpg -------------------------------------------------------------------------------- /zoo/densenet/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/micro.jpg -------------------------------------------------------------------------------- /zoo/densenet/residual-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/residual-block.jpg -------------------------------------------------------------------------------- /zoo/densenet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/stem.jpg -------------------------------------------------------------------------------- /zoo/densenet/trans-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/densenet/trans-block.jpg -------------------------------------------------------------------------------- /zoo/fast r-cnn/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Fast R-CNN 3 | 4 | [Paper](https://arxiv.org/pdf/1504.08083.pdf) 5 | 6 | ## Macro-Architecture 7 | 8 | 9 | -------------------------------------------------------------------------------- /zoo/fast r-cnn/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/fast r-cnn/macro.jpg -------------------------------------------------------------------------------- /zoo/faster r-cnn/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Faster R-CNN 3 | 4 | [Paper](https://arxiv.org/pdf/1506.01497.pdf) 5 | 6 | ## Macro-Architecture 7 | 8 | 9 | 10 | ## Micro-Architecture 11 | 12 | ### Stem Group + Conv Net 13 | 14 | 15 | 16 | ### RPN + RoI Pooling 17 | 18 | 19 | 20 | ### Region of Proposal Network 21 | 22 | 23 | -------------------------------------------------------------------------------- /zoo/faster r-cnn/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/faster r-cnn/macro.jpg -------------------------------------------------------------------------------- /zoo/faster r-cnn/rpn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/faster r-cnn/rpn.jpg -------------------------------------------------------------------------------- /zoo/faster r-cnn/rpnroi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/faster r-cnn/rpnroi.jpg -------------------------------------------------------------------------------- /zoo/faster r-cnn/vgg16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/faster r-cnn/vgg16.jpg -------------------------------------------------------------------------------- /zoo/inception/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Inception 3 | 4 | [inception(v1/v2/v3).py](inception_v1.py) - academic (idiomatic)
5 | [inception_(v1/v2/v3)_c.py](inception_v1_c.py) - production (composable) 6 | 7 | [Paper V1/V2](https://arxiv.org/pdf/1409.4842.pdf)
8 | [Paper V3](https://arxiv.org/pdf/1512.00567.pdf) 9 | [Corrected Paper V3](https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Szegedy_Rethinking_the_Inception_CVPR_2016_paper.pdf) 10 | 11 | The paper submitted to ARXIV, and last revised on Dec 11, 2015, has some typos in it that were addressed with a revision that is stored on CV-Foundation. Mostly notably, correctly the reference to the model submitted to the ILSVRC 2015 image classification (1st runner up), from V2 to V3. 12 | 13 | It is generally agreed that V2 is the same as V1 with the additional of batch normalization, but no additional 14 | factorization. 15 | 16 | The later paper continues to have the typos in Table 1. Per my discussion with two of the paper's authors: 17 | 18 | Sergey Ioffe: *The inception v3 model has been opensourced. Please see 19 | https://github.com/tensorflow/models/blob/master/research/inception/inception/slim/inception_model.py (which also cites 20 | the paper where this model was described).* 21 | 22 | Christian Szegedy: *I agree with Sergey that the implementation serves as the best reference.* 23 | 24 | inception_(v1/v2/v3).py - academic - procedural 25 | inception_(v1.v2/v3)_c.py - composable - OOP 26 | 27 | Below is the corrected version of Table 1 in the paper for V3: 28 | 29 | | type | patch size/stride . | input size | note 30 | | ------------| ---------------------- |------------|-----------| 31 | | conv | 3x3/2 | 299x299x3 || 32 | | conv | 3x3/1 | 149x149x32 || 33 | | conv padded | 3x3/1 | 147x147x32 || 34 | | pool | 3x3/2 | 147x147x64 || 35 | | conv | 3x3/1 | 73x73x64 || 36 | | conv | 3x3/2 | 71x71x80 || 37 | | **pool** | 3x3/2 | 35x35x192 | incorrectly listed as conv | 38 | | 3xinception | **fig. 4/with double 3x3** | 35x35x288 | incorrectly listed as fig. 5, includes grid reduction fig. 10| 39 | | 5xinception | fig. 6 | 17x17x768 | includes grid reduction, no fig. | 40 | | 2xinception | fig. 7 | 8x8x1280 || 41 | | **ave** pool | 8x8 | 8x8x2048 || 42 | | linear | logits | 1x1x2048 || 43 | | softmax | classifier | 1x1x1000 || 44 | 45 | 46 | ## Macro-Architecture v1.0 and v2.0 47 | 48 | 49 | 50 | ## Macro-Architecture v3.0 51 | 52 | 53 | 54 | ## Micro-Architecture v1.0 and v2.0 55 | 56 | 57 | 58 | ## Micro-Architecture v3.0 59 | 60 | 61 | 62 | ### Stem v1.0 63 | 64 | 65 | 66 | ### Stem v2.0 67 | 68 | Adds batch normalization to the convolutional layers and uses the common convention to drop biases in the convolutional layer when it is followed by batch normalization. 69 | 70 | ### Stem v3.0 71 | 72 | 73 | 74 | ### Stem v4.0 75 | 76 | 77 | 78 | ### Inception Block v1.0 79 | 80 | Adds batch normalization to the convolutional layers and uses the common convention to drop biases in the convolutional layer when it is followed by batch normalization. 81 | 82 | 83 | 84 | ### Inception Block v2.0 85 | 86 | 87 | 88 | ### Inception Block v3.0 89 | 90 | #### Inception Block for 35 x 35 grid 91 | 92 | 93 | 94 | #### Reduction Block to 17 x 17 Grid 95 | 96 | 97 | 98 | #### Inception Block 17 x 17 Grid 99 | 100 | 101 | 102 | #### Reduction Block to 8 x 8 Grid 103 | 104 | 105 | 106 | #### Inception Block 8 x 8 Grid 107 | 108 | 109 | 110 | ### Classifier v1.0, v2.0 & v3.0 111 | 112 | 113 | 114 | ### Auxiliary Classifier v1.0 & v2.0 115 | 116 | 117 | 118 | ### Auxiliary Classifier v3.0 119 | 120 | 121 | 122 | 123 | ## Composable 124 | 125 | *Example: Instantiate a stock Inception V1 model* 126 | 127 | ```python 128 | from inception_v1_c import InceptionV1 129 | 130 | # Inception V1 from research paper 131 | inception = InceptionV1() 132 | 133 | # InceptionV1 custom input shape/classes 134 | inception = InceptionV1(input_shape=(128, 128, 3), n_classes=50) 135 | 136 | # getter for the tf.keras model 137 | model = inception.model 138 | ``` 139 | 140 | *Example: Compose and Train an Inception V1 model* 141 | 142 | ```python 143 | ``` 144 | 145 | -------------------------------------------------------------------------------- /zoo/inception/auxiliary-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/auxiliary-v3.jpg -------------------------------------------------------------------------------- /zoo/inception/auxiliary.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/auxiliary.jpg -------------------------------------------------------------------------------- /zoo/inception/block-17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/block-17.jpg -------------------------------------------------------------------------------- /zoo/inception/block-35.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/block-35.jpg -------------------------------------------------------------------------------- /zoo/inception/block-8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/block-8.jpg -------------------------------------------------------------------------------- /zoo/inception/block-v1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/block-v1.jpg -------------------------------------------------------------------------------- /zoo/inception/block-v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/block-v2.jpg -------------------------------------------------------------------------------- /zoo/inception/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/classifier.jpg -------------------------------------------------------------------------------- /zoo/inception/macro-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/macro-v3.jpg -------------------------------------------------------------------------------- /zoo/inception/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/macro.jpg -------------------------------------------------------------------------------- /zoo/inception/micro-v1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/micro-v1.jpg -------------------------------------------------------------------------------- /zoo/inception/micro-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/micro-v3.jpg -------------------------------------------------------------------------------- /zoo/inception/reduction-17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/reduction-17.jpg -------------------------------------------------------------------------------- /zoo/inception/reduction-8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/reduction-8.jpg -------------------------------------------------------------------------------- /zoo/inception/stem-v1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/stem-v1.jpg -------------------------------------------------------------------------------- /zoo/inception/stem-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/stem-v3.jpg -------------------------------------------------------------------------------- /zoo/inception/stem-v4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/inception/stem-v4.jpg -------------------------------------------------------------------------------- /zoo/jumpnet/jumpnet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # JumpNet (50, 101, 152) 16 | # Residual Groups and Blocks are ResNet v2 - w/o projection block 17 | # Stem convolution is a stack of two 3x3 filters (factorized 5x5), as in Inception v3 18 | 19 | import tensorflow as tf 20 | from tensorflow.keras import Model, Input 21 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, ReLU 22 | from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Add, Concatenate 23 | from tensorflow.keras.regularizers import l2 24 | 25 | def stem(inputs): 26 | """ Construct the Stem Convolutional Group 27 | inputs : the input vector 28 | """ 29 | 30 | # Stack of two 3x3 filters 31 | x = Conv2D(32, (3, 3), strides=(2, 2), padding='same', 32 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(inputs) 33 | x = BatchNormalization()(x) 34 | x = ReLU()(x) 35 | 36 | x = Conv2D(64, (3, 3), strides=(1, 1), padding='same', 37 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(x) 38 | x = BatchNormalization()(x) 39 | x = ReLU()(x) 40 | 41 | return x 42 | 43 | def learner(x, groups): 44 | """ Construct the Learner 45 | x : input to the learner 46 | groups: list of groups: number of filters and blocks 47 | """ 48 | # Residual Groups 49 | for n_filters, n_blocks in groups: 50 | x = group(x, n_filters, n_blocks) 51 | return x 52 | 53 | def group(x, n_filters, n_blocks): 54 | """ Construct a Residual Group 55 | x : input into the group 56 | n_filters : number of filters for the group 57 | n_blocks : number of residual blocks with identity link 58 | """ 59 | # Save the input to the group for the jump link at the end. 60 | shortcut = BatchNormalization()(x) 61 | shortcut = Conv2D(n_filters, (1, 1), strides=(2, 2), 62 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(shortcut) 63 | 64 | # Identity residual blocks 65 | for _ in range(n_blocks): 66 | x = identity_block(x, n_filters) 67 | 68 | # Feature Pooling at the end of the group 69 | x = BatchNormalization()(x) 70 | x = ReLU()(x) 71 | x = Conv2D(n_filters, (1, 1), strides=(2, 2), 72 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(x) 73 | 74 | # Construct the jumpn link 75 | x = Concatenate()([shortcut, x]) 76 | return x 77 | 78 | def identity_block(x, n_filters): 79 | """ Construct a Bottleneck Residual Block with Identity Link 80 | x : input into the block 81 | n_filters: number of filters 82 | """ 83 | 84 | # Save input vector (feature maps) for the identity link 85 | shortcut = x 86 | 87 | ## Construct the 1x1, 3x3, 1x1 convolution block 88 | 89 | # Dimensionality reduction 90 | x = BatchNormalization()(x) 91 | x = ReLU()(x) 92 | x = Conv2D(n_filters, (1, 1), strides=(1, 1), 93 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(x) 94 | 95 | # Bottleneck layer 96 | x = BatchNormalization()(x) 97 | x = ReLU()(x) 98 | x = Conv2D(n_filters, (3, 3), strides=(1, 1), padding="same", 99 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(x) 100 | 101 | # no dimensionality restoration 102 | x = BatchNormalization()(x) 103 | x = ReLU()(x) 104 | x = Conv2D(n_filters, (1, 1), strides=(1, 1), 105 | kernel_initializer='he_normal', kernel_regularizer=l2(reg))(x) 106 | 107 | # Add the identity link (input) to the output of the residual block 108 | x = Add()([shortcut, x]) 109 | return x 110 | 111 | 112 | def classifier(x, n_classes): 113 | """ Construct the Classifier Group 114 | x : input to the classifier 115 | n_classes : number of output classes 116 | """ 117 | # Pool at the end of all the convolutional residual blocks 118 | x = GlobalAveragePooling2D()(x) 119 | 120 | # Final Dense Outputting Layer for the outputs 121 | outputs = Dense(n_classes, activation='softmax', kernel_initializer='he_normal')(x) 122 | return outputs 123 | 124 | # Meta-parameter: list of groups: number of filters and number of blocks 125 | groups = { 50 : [ (64, 3), (128, 4), (256, 6), (512, 3) ], # ResNet50 126 | 101: [ (64, 3), (128, 4), (256, 23), (512, 3) ], # ResNet101 127 | 152: [ (64, 3), (128, 8), (256, 36), (512, 3) ] # ResNet152 128 | } 129 | 130 | # L2 regularization (weight decay) 131 | reg = 0.001 132 | 133 | # The input tensor 134 | inputs = Input(shape=(224, 224, 3)) 135 | 136 | # The stem convolutional group 137 | x = stem(inputs) 138 | 139 | # The learner 140 | x = learner(x, groups[50]) 141 | 142 | # The classifier for 1000 classes 143 | outputs = classifier(x, 1000) 144 | 145 | # Instantiate the Model 146 | model = Model(inputs, outputs) 147 | 148 | model.summary() 149 | -------------------------------------------------------------------------------- /zoo/jumpnet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/jumpnet/macro.jpg -------------------------------------------------------------------------------- /zoo/jumpnet/models_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import tensorflow as tf 16 | from tensorflow.keras import Sequential, Model, Input 17 | from tensorflow.keras import layers 18 | from tensorflow.keras.layers import ReLU, Dense, Conv2D, Conv2DTranspose 19 | from tensorflow.keras.layers import DepthwiseConv2D, SeparableConv2D, Dropout 20 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation, BatchNormalization 21 | from tensorflow.keras.regularizers import l2 22 | from tensorflow.keras.optimizers import Adam, SGD 23 | from tensorflow.compat.v1.keras.initializers import glorot_uniform, he_normal 24 | from tensorflow.keras.callbacks import LearningRateScheduler 25 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 26 | from tensorflow.keras.utils import to_categorical 27 | import tensorflow_datasets as tfds 28 | import tensorflow.keras.backend as K 29 | import numpy as np 30 | from sklearn.model_selection import train_test_split 31 | 32 | import random 33 | import math 34 | import sys 35 | 36 | from layers_c import Layers 37 | from preprocess_c import Preprocess 38 | from pretraining_c import Pretraining 39 | from hypertune_c import HyperTune 40 | from training_c import Training 41 | from datasets_c import Dataset 42 | 43 | class Composable(Layers, Preprocess, Pretraining, HyperTune, Training, Dataset): 44 | ''' Composable base (super) class for Models ''' 45 | 46 | def __init__(self, default_hyperparameters=None, **hyperparameters): 47 | """ Constructor 48 | initializer : kernel initializer 49 | regularizer : kernel regularizer 50 | relu_clip : clip value for ReLU 51 | bn_epsilon : epsilon for batch norm 52 | use_bias : whether to use bias 53 | """ 54 | for key, value in hyperparameters.items(): 55 | default_hyperparameters[key] = value 56 | Layers.__init__(self, **default_hyperparameters) 57 | Preprocess.__init__(self) 58 | Pretraining.__init__(self) 59 | HyperTune.__init__(self) 60 | Training.__init__(self) 61 | Dataset.__init__(self) 62 | 63 | # Feature maps encoding at the bottleneck layer in classifier (high dimensionality) 64 | self._encoding = None 65 | # Pooled and flattened encodings at the bottleneck layer (low dimensionality) 66 | self._embedding = None 67 | # Pre-activation conditional probabilities for classifier 68 | self._probabilities = None 69 | # Post-activation conditional probabilities for classifier 70 | self._softmax = None 71 | 72 | self._model = None 73 | 74 | @property 75 | def model(self): 76 | return self._model 77 | 78 | @model.setter 79 | def model(self, _model): 80 | self._model = _model 81 | 82 | @property 83 | def encoding(self): 84 | return self._encoding 85 | 86 | @encoding.setter 87 | def encoding(self, layer): 88 | self._encoding = layer 89 | 90 | @property 91 | def embedding(self): 92 | return self._embedding 93 | 94 | @embedding.setter 95 | def embedding(self, layer): 96 | self._embedding = layer 97 | 98 | @property 99 | def probabilities(self): 100 | return self._probabilities 101 | 102 | @probabilities.setter 103 | def probabilities(self, layer): 104 | self._probabilities = layer 105 | 106 | 107 | -------------------------------------------------------------------------------- /zoo/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/macro.jpg -------------------------------------------------------------------------------- /zoo/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/micro.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # MobileNet v1.0 3 | 4 | [Paper](https://arxiv.org/pdf/1704.04861.pdf) 5 | 6 | [mobilenet(v1).py](mobilenet_v1.py) - academic (idiomatic)
7 | [mobilenet(v1)_c.py](mobilenet_v1_c.py) - production (composable) 8 | 9 | ## Macro-Architecture 10 | 11 | 12 | 13 | ## Micro-Architecture 14 | 15 | 16 | 17 | ### Stem Group 18 | 19 | 20 | 21 | ### Depthwise Separable Block 22 | 23 | 24 | 25 | ### Strided Depthwise Separable Block 26 | 27 | 28 | 29 | ### Classifier Group 30 | 31 | 32 | 33 | # MobileNet v2.0 34 | 35 | [mobilenet(v2).py](mobilenet_v2.py) - academic (idiomatic)
36 | [mobilenet(v2)_c.py](mobilenet_v2_c.py) - production (composable) 37 | 38 | [Paper](https://arxiv.org/pdf/1801.04381.pdf) 39 | 40 | ## Macro-Architecture 41 | 42 | 43 | 44 | ## Micro-Architecture 45 | 46 | 47 | 48 | ### Stem Group 49 | 50 | 51 | 52 | ### Inverted Residual Block 53 | 54 | 55 | 56 | ### Strided Inverted Residual Block 57 | 58 | 59 | 60 | ### Classifier 61 | 62 | 63 | 64 | # MobileNet v3.0 65 | 66 | [mobilenet(v3).py](mobilenet_v3.py) - academic (idiomatic)
67 | [mobilenet(v3)_c.py](mobilenet_v3_c.py) - production (composable) 68 | 69 | [Paper](https://arxiv.org/pdf/1905.02244.pdf) 70 | 71 | ## Macro-Architecture 72 | 73 | 74 | 75 | ## Micro-Architecture 76 | 77 | 78 | 79 | ### Stem Group 80 | 81 | 82 | 83 | ### Attention Block 84 | 85 | 86 | 87 | ### Squeeze Block 88 | 89 | 90 | 91 | ### Classifier Group 92 | 93 | 94 | 95 | 96 | ## Composable 97 | 98 | *Example: Instantiate a stock MobileNet V2 model* 99 | 100 | ```python 101 | from mobilenet_v2_c import MobileNetV2 102 | 103 | # MobileNet v2.0 from research paper 104 | mobilenet = MobileNetV2() 105 | 106 | # MobileNet v2.0 custom input shape/classes 107 | mobilenet = MobileNetV2(input_shape=(128, 128, 3), n_classes=50) 108 | 109 | # getter for the tf.keras model 110 | model = mobilenet.model 111 | ``` 112 | 113 | *Example: Composable and Train a MobileNet V1 model* 114 | 115 | ```python 116 | ''' Example for constructing/training a MobileNet V1 model on CIFAR-10 117 | ''' 118 | # Example of constructing a mini-MobileNet 119 | groups = [ { 'n_filters': 128, 'n_blocks': 1 }, 120 | { 'n_filters': 256, 'n_blocks': 1 }, 121 | { 'n_filters': 1024, 'n_blocks': 2 } ] 122 | mobilenet = MobileNetV1(groups, input_shape=(32, 32, 3), n_classes=10) 123 | mobilenet.model.summary() 124 | mobilenet.cifar10() 125 | ``` 126 | 127 | 128 | -------------------------------------------------------------------------------- /zoo/mobilenet/attention.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/attention.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/classifier-v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/classifier-v2.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/classifier-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/classifier-v3.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/classifier.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/depthwise-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/depthwise-block.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/inverted-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/inverted-block.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/macro-v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/macro-v2.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/macro-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/macro-v3.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/macro.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/micro-v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/micro-v2.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/micro-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/micro-v3.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/micro.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/mobilenet_v1.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | # MobileNet 224 (2017) 17 | # Paper: https://arxiv.org/pdf/1704.04861.pdf 18 | 19 | import tensorflow as tf 20 | from tensorflow.keras import Input, Model 21 | from tensorflow.keras.layers import ZeroPadding2D, Conv2D, BatchNormalization, ReLU 22 | from tensorflow.keras.layers import DepthwiseConv2D, GlobalAveragePooling2D, Reshape, Dropout 23 | 24 | def stem(inputs, alpha): 25 | """ Construct the Stem Group 26 | inputs : input tensor 27 | alpha : width multiplier 28 | """ 29 | # Convolutional block 30 | x = ZeroPadding2D(padding=((0, 1), (0, 1)))(inputs) 31 | x = Conv2D(32 * alpha, (3, 3), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='glorot_uniform')(x) 32 | x = BatchNormalization()(x) 33 | x = ReLU(6.0)(x) 34 | 35 | # Depthwise Separable Convolution Block 36 | x = depthwise_block(x, 64, alpha, (1, 1)) 37 | return x 38 | 39 | def learner(x, alpha): 40 | """ Construct the Learner 41 | x : input to the learner 42 | alpha : width multiplier 43 | """ 44 | # First Depthwise Separable Convolution Group 45 | x = group(x, 128, 2, alpha) 46 | 47 | # Second Depthwise Separable Convolution Group 48 | x = group(x, 256, 2, alpha) 49 | 50 | # Third Depthwise Separable Convolution Group 51 | x = group(x, 512, 6, alpha) 52 | 53 | # Fourth Depthwise Separable Convolution Group 54 | x = group(x, 1024, 2, alpha) 55 | return x 56 | 57 | def group(x, n_filters, n_blocks, alpha): 58 | """ Construct a Depthwise Separable Convolution Group 59 | x : input to the group 60 | n_filters : number of filters 61 | n_blocks : number of blocks in the group 62 | alpha : width multiplier 63 | """ 64 | # In first block, the depthwise convolution is strided - feature map size reduction 65 | x = depthwise_block(x, n_filters, alpha, strides=(2, 2)) 66 | 67 | # Remaining blocks 68 | for _ in range(n_blocks - 1): 69 | x = depthwise_block(x, n_filters, alpha, strides=(1, 1)) 70 | return x 71 | 72 | def depthwise_block(x, n_filters, alpha, strides): 73 | """ Construct a Depthwise Separable Convolution block 74 | x : input to the block 75 | n_filters : number of filters 76 | alpha : width multiplier 77 | strides : strides 78 | """ 79 | # Apply the width filter to the number of feature maps 80 | filters = int(n_filters * alpha) 81 | 82 | # Strided convolution to match number of filters 83 | if strides == (2, 2): 84 | x = ZeroPadding2D(padding=((0, 1), (0, 1)))(x) 85 | padding = 'valid' 86 | else: 87 | padding = 'same' 88 | 89 | # Depthwise Convolution 90 | x = DepthwiseConv2D((3, 3), strides, padding=padding, use_bias=False, kernel_initializer='glorot_uniform')(x) 91 | x = BatchNormalization()(x) 92 | x = ReLU(6.0)(x) 93 | 94 | # Pointwise Convolution 95 | x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', use_bias=False, kernel_initializer='glorot_uniform')(x) 96 | x = BatchNormalization()(x) 97 | x = ReLU(6.0)(x) 98 | return x 99 | 100 | def classifier(x, alpha, dropout, n_classes): 101 | """ Construct the classifier group 102 | x : input to the classifier 103 | alpha : width multiplier 104 | dropout : dropout percentage 105 | n_classes : number of output classes 106 | """ 107 | # Flatten the feature maps into 1D feature maps (?, N) 108 | x = GlobalAveragePooling2D()(x) 109 | 110 | # Reshape the feature maps to (?, 1, 1, 1024) 111 | shape = (1, 1, int(1024 * alpha)) 112 | x = Reshape(shape)(x) 113 | # Perform dropout for preventing overfitting 114 | x = Dropout(dropout)(x) 115 | 116 | # Use convolution for classifying (emulates a fully connected layer) 117 | x = Conv2D(n_classes, (1, 1), padding='same', activation='softmax', kernel_initializer='glorot_uniform')(x) 118 | # Reshape the resulting output to 1D vector of number of classes 119 | x = Reshape((n_classes, ))(x) 120 | return x 121 | 122 | # Meta-parameter: width multiplier (0 .. 1) for reducing number of filters. 123 | alpha = 1 124 | 125 | # Meta-parameter: resolution multiplier (0 .. 1) for reducing input size 126 | pho = 1 127 | 128 | # Meta-parameter: dropout rate 129 | dropout = 0.5 130 | 131 | inputs = Input(shape=(int(224 * pho), int(224 * pho), 3)) 132 | 133 | # The Stem Group 134 | x = stem(inputs, alpha) 135 | 136 | # The Learner 137 | x = learner(x, alpha) 138 | 139 | # The classifier for 1000 classes 140 | outputs = classifier(x, alpha, dropout, 1000) 141 | 142 | # Instantiate the Model 143 | model = Model(inputs, outputs) 144 | -------------------------------------------------------------------------------- /zoo/mobilenet/mobilenet_v2.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | 16 | # MobileNet v2 (2019) 17 | # Paper: https://arxiv.org/pdf/1801.04381.pdf 18 | # 224x224 input: 3,504,872 parameters 19 | 20 | import tensorflow as tf 21 | from tensorflow.keras import Input, Model 22 | from tensorflow.keras.layers import ZeroPadding2D, Conv2D, BatchNormalization, ReLU 23 | from tensorflow.keras.layers import DepthwiseConv2D, Add, GlobalAveragePooling2D, Dense 24 | 25 | def stem(inputs, alpha): 26 | """ Construct the Stem Group 27 | inputs : input tensor 28 | alpha : width multiplier 29 | """ 30 | # Calculate the number of filters for the stem convolution 31 | # Must be divisible by 8 32 | n_filters = max(8, (int(32 * alpha) + 4) // 8 * 8) 33 | 34 | # Convolutional block 35 | x = ZeroPadding2D(padding=((0, 1), (0, 1)))(inputs) 36 | x = Conv2D(n_filters, (3, 3), strides=(2, 2), padding='valid', use_bias=False, kernel_initializer='glorot_uniform')(x) 37 | x = BatchNormalization()(x) 38 | x = ReLU(6.)(x) 39 | 40 | return x 41 | 42 | def learner(x, alpha, expansion=6): 43 | """ Construct the Learner 44 | x : input to the learner 45 | alpha : width multiplier 46 | expansion: multipler to expand number of filters 47 | """ 48 | # First Inverted Residual Convolution Group 49 | x = group(x, 16, 1, alpha, expansion=1, strides=(1, 1)) 50 | 51 | # Second Inverted Residual Convolution Group 52 | x = group(x, 24, 2, alpha, expansion) 53 | 54 | # Third Inverted Residual Convolution Group 55 | x = group(x, 32, 3, alpha, expansion) 56 | 57 | # Fourth Inverted Residual Convolution Group 58 | x = group(x, 64, 4, alpha, expansion) 59 | 60 | # Fifth Inverted Residual Convolution Group 61 | x = group(x, 96, 3, alpha, expansion, strides=(1, 1)) 62 | 63 | # Sixth Inverted Residual Convolution Group 64 | x = group(x, 160, 3, alpha, expansion) 65 | 66 | # Seventh Inverted Residual Convolution Group 67 | x = group(x, 320, 1, alpha, expansion, strides=(1, 1)) 68 | 69 | # Last block is a 1x1 linear convolutional layer, 70 | # expanding the number of filters to 1280. 71 | x = Conv2D(1280, (1, 1), use_bias=False, kernel_initializer='glorot_uniform')(x) 72 | x = BatchNormalization()(x) 73 | x = ReLU(6.)(x) 74 | return x 75 | 76 | def group(x, n_filters, n_blocks, alpha, expansion=6, strides=(2, 2)): 77 | """ Construct an Inverted Residual Group 78 | x : input to the group 79 | n_filters : number of filters 80 | n_blocks : number of blocks in the group 81 | alpha : width multiplier 82 | expansion : multiplier for expanding the number of filters 83 | strides : whether first inverted residual block is strided. 84 | """ 85 | # In first block, the inverted residual block maybe strided - feature map size reduction 86 | x = inverted_block(x, n_filters, alpha, expansion, strides=strides) 87 | 88 | # Remaining blocks 89 | for _ in range(n_blocks - 1): 90 | x = inverted_block(x, n_filters, alpha, expansion, strides=(1, 1)) 91 | return x 92 | 93 | def inverted_block(x, n_filters, alpha, expansion=6, strides=(1, 1)): 94 | """ Construct an Inverted Residual Block 95 | x : input to the block 96 | n_filters : number of filters 97 | alpha : width multiplier 98 | strides : strides 99 | expansion : multiplier for expanding number of filters 100 | """ 101 | # Remember input 102 | shortcut = x 103 | 104 | # Apply the width filter to the number of feature maps for the pointwise convolution 105 | filters = int(n_filters * alpha) 106 | 107 | n_channels = int(x.shape[3]) 108 | 109 | # Dimensionality Expansion (non-first block) 110 | if expansion > 1: 111 | # 1x1 linear convolution 112 | x = Conv2D(expansion * n_channels, (1, 1), padding='same', use_bias=False, kernel_initializer='glorot_uniform')(x) 113 | x = BatchNormalization()(x) 114 | x = ReLU(6.)(x) 115 | 116 | # Strided convolution to match number of filters 117 | if strides == (2, 2): 118 | x = ZeroPadding2D(padding=((0, 1), (0, 1)))(x) 119 | padding = 'valid' 120 | else: 121 | padding = 'same' 122 | 123 | # Depthwise Convolution 124 | x = DepthwiseConv2D((3, 3), strides, padding=padding, use_bias=False, kernel_initializer='glorot_uniform')(x) 125 | x = BatchNormalization()(x) 126 | x = ReLU(6.)(x) 127 | 128 | # Linear Pointwise Convolution 129 | x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', use_bias=False, kernel_initializer='glorot_uniform')(x) 130 | x = BatchNormalization()(x) 131 | 132 | # Number of input filters matches the number of output filters 133 | if n_channels == filters and strides == (1, 1): 134 | x = Add()([shortcut, x]) 135 | return x 136 | 137 | def classifier(x, n_classes): 138 | """ Construct the classifier group 139 | x : input to the classifier 140 | n_classes : number of output classes 141 | """ 142 | # Flatten the feature maps into 1D feature maps (?, N) 143 | x = GlobalAveragePooling2D()(x) 144 | 145 | # Dense layer for final classification 146 | x = Dense(n_classes, activation='softmax', kernel_initializer='glorot_uniform')(x) 147 | return x 148 | 149 | # Meta-parameter: width multiplier (0 .. 1) for reducing number of filters. 150 | alpha = 1 151 | 152 | # Meta-parameter: multiplier to expand number of filters 153 | expansion = 6 154 | 155 | inputs = Input(shape=(224, 224, 3)) 156 | 157 | # The Stem Group 158 | x = stem(inputs, alpha) 159 | 160 | # The Learner 161 | x = learner(x, alpha, expansion) 162 | 163 | # The classifier for 1000 classes 164 | outputs = classifier(x, 1000) 165 | 166 | # Instantiate the Model 167 | model = Model(inputs, outputs) 168 | -------------------------------------------------------------------------------- /zoo/mobilenet/squeeze.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/squeeze.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/stem-v2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/stem-v2.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/stem-v3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/stem-v3.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/stem.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/strided-depthwise-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/strided-depthwise-block.jpg -------------------------------------------------------------------------------- /zoo/mobilenet/strided-inverted-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/mobilenet/strided-inverted-block.jpg -------------------------------------------------------------------------------- /zoo/models_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import tensorflow as tf 16 | from tensorflow.keras import Sequential, Model, Input 17 | from tensorflow.keras import layers 18 | from tensorflow.keras.layers import ReLU, Dense, Conv2D, Conv2DTranspose 19 | from tensorflow.keras.layers import DepthwiseConv2D, SeparableConv2D, Dropout 20 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation, BatchNormalization 21 | from tensorflow.keras.regularizers import l2 22 | from tensorflow.keras.optimizers import Adam, SGD 23 | from tensorflow.compat.v1.keras.initializers import glorot_uniform, he_normal 24 | from tensorflow.keras.callbacks import LearningRateScheduler 25 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 26 | from tensorflow.keras.utils import to_categorical 27 | import tensorflow_datasets as tfds 28 | import tensorflow.keras.backend as K 29 | import numpy as np 30 | from sklearn.model_selection import train_test_split 31 | 32 | import random 33 | import math 34 | import sys 35 | 36 | from layers_c import Layers 37 | from preprocess_c import Preprocess 38 | from pretraining_c import Pretraining 39 | from hypertune_c import HyperTune 40 | from training_c import Training 41 | from datasets_c import Dataset 42 | 43 | class Composable(Layers, Preprocess, Pretraining, HyperTune, Training, Dataset): 44 | ''' Composable base (super) class for Models ''' 45 | 46 | def __init__(self, input_shape, include_task, default_hyperparameters=None, **hyperparameters): 47 | """ Constructor 48 | input_shape : input tensor to the model 49 | include_task : include the task component 50 | default_hyperparameters: parent model default hyperparameter settings 51 | **hyperparameters: overridden hyperparameter settings 52 | """ 53 | for key, value in hyperparameters.items(): 54 | default_hyperparameters[key] = value 55 | Layers.__init__(self, **default_hyperparameters) 56 | Preprocess.__init__(self) 57 | Pretraining.__init__(self) 58 | HyperTune.__init__(self) 59 | Training.__init__(self) 60 | Dataset.__init__(self) 61 | 62 | self.input_shape = input_shape 63 | self.include_task = include_task 64 | 65 | # Feature maps encoding at the bottleneck layer in classifier (high dimensionality) 66 | self._encoding = None 67 | # Pooled and flattened encodings at the bottleneck layer (low dimensionality) 68 | self._embedding = None 69 | # Pre-activation conditional probabilities for classifier 70 | self._probabilities = None 71 | # Post-activation conditional probabilities for classifier 72 | self._softmax = None 73 | 74 | self._model = None 75 | 76 | @property 77 | def model(self): 78 | return self._model 79 | 80 | @model.setter 81 | def model(self, _model): 82 | self._model = _model 83 | 84 | @property 85 | def encoding(self): 86 | return self._encoding 87 | 88 | @encoding.setter 89 | def encoding(self, layer): 90 | self._encoding = layer 91 | 92 | @property 93 | def embedding(self): 94 | return self._embedding 95 | 96 | @embedding.setter 97 | def embedding(self, layer): 98 | self._embedding = layer 99 | 100 | @property 101 | def probabilities(self): 102 | return self._probabilities 103 | 104 | @probabilities.setter 105 | def probabilities(self, layer): 106 | self._probabilities = layer 107 | 108 | @property 109 | def softmax(self): 110 | return self._softmax 111 | 112 | @softmax.setter 113 | def softmax(self, layer): 114 | self._softmax = layer 115 | 116 | 117 | @property 118 | def inputs(self): 119 | return self._model.inputs 120 | 121 | 122 | -------------------------------------------------------------------------------- /zoo/preprocess_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import tensorflow as tf 16 | from tensorflow.keras import Sequential, Model, Input 17 | from tensorflow.keras import layers 18 | from tensorflow.keras.layers import ReLU, Dense, Conv2D, Conv2DTranspose 19 | from tensorflow.keras.layers import DepthwiseConv2D, SeparableConv2D, Dropout 20 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation, BatchNormalization 21 | from tensorflow.keras.regularizers import l2 22 | from tensorflow.keras.optimizers import Adam, SGD 23 | from tensorflow.compat.v1.keras.initializers import glorot_uniform, he_normal 24 | from tensorflow.keras.callbacks import LearningRateScheduler 25 | from tensorflow.keras.preprocessing.image import ImageDataGenerator 26 | from tensorflow.keras.utils import to_categorical 27 | import tensorflow_datasets as tfds 28 | import tensorflow.keras.backend as K 29 | import numpy as np 30 | from sklearn.model_selection import train_test_split 31 | 32 | import random 33 | import math 34 | import sys 35 | 36 | class Preprocess: 37 | ''' Preprocess base (super) class for Composable Models ''' 38 | 39 | def __init__(self): 40 | """ Constructor 41 | """ 42 | pass 43 | 44 | ### 45 | # Preprocessing 46 | ### 47 | 48 | def normalization(self, x_train, x_test=None, centered=False): 49 | """ Normalize the input 50 | x_train : training images 51 | y_train : test images 52 | """ 53 | if x_train.dtype == np.uint8: 54 | if centered: 55 | x_train = ((x_train - 1) / 127.5).astype(np.float32) 56 | if x_test is not None: 57 | x_test = ((x_test - 1) / 127.5).astype(np.float32) 58 | else: 59 | x_train = (x_train / 255.0).astype(np.float32) 60 | if x_test is not None: 61 | x_test = (x_test / 255.0).astype(np.float32) 62 | return x_train, x_test 63 | 64 | def standardization(self, x_train, x_test=None): 65 | """ Standardize the input 66 | x_train : training images 67 | x_test : test images 68 | """ 69 | self.mean = np.mean(x_train) 70 | self.std = np.std(x_train) 71 | x_train = ((x_train - self.mean) / self.std).astype(np.float32) 72 | if x_test is not None: 73 | x_test = ((x_test - self.mean) / self.std).astype(np.float32) 74 | return x_train, x_test 75 | 76 | def label_smoothing(self, y_train, n_classes, factor=0.1): 77 | """ Convert a matrix of one-hot row-vector labels into smoothed versions. 78 | y_train : training labels 79 | n_classes: number of classes 80 | factor : smoothing factor (between 0 and 1) 81 | """ 82 | if 0 <= factor <= 1: 83 | # label smoothing ref: https://www.robots.ox.ac.uk/~vgg/rg/papers/reinception.pdf 84 | y_train *= 1 - factor 85 | y_train += factor / n_classes 86 | else: 87 | raise Exception('Invalid label smoothing factor: ' + str(factor)) 88 | return y_train 89 | -------------------------------------------------------------------------------- /zoo/r-cnn/README.md: -------------------------------------------------------------------------------- 1 | 2 | # R-CNN 3 | 4 | [Paper](https://arxiv.org/pdf/1311.2524.pdf) 5 | 6 | ## Macro-Architecture 7 | 8 | 9 | -------------------------------------------------------------------------------- /zoo/r-cnn/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/r-cnn/macro.jpg -------------------------------------------------------------------------------- /zoo/resnet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # ResNet 3 | 4 | [resnet(v1/v1.5/v2).py](resnet_v1.py) - academic (idiomatic)
5 | [resnet_cifar10(v1/v2).py](resnet_v1_cifar10.py) - academic (idiomatic)
6 | [resnet(v1/v1.5/v2)_c.py](resnet_v1_c.py) - production (composable) 7 | 8 | [Paper](https://arxiv.org/pdf/1512.03385.pdf) 9 | 10 | ## Macro-Architecture 11 | 12 | 13 | 14 | ## Micro-Architecture 15 | 16 | 17 | 18 | ### Stem Group 19 | 20 | 21 | 22 | ### ResNet Block with Identity Shortcut 23 | 24 | 25 | 26 | #### v2.0 27 | 28 | In v2.0, the BatchNormalization and ReLU activation function is moved from after the convolution to before. 29 | 30 | ### ResNet Block with Projection Shortcut 31 | 32 | #### v1.0 33 | 34 | 35 | 36 | #### v1.5 37 | 38 | 39 | 40 | In v1.5, the strided convolution is moved from the 1x1 convolution to the 3x3 bottleneck convolution. 41 | 42 | #### v2.0 43 | 44 | In v2.0, the BatchNormalization and ReLU activation function is moved from after the convolution to before. 45 | 46 | ### Classifier 47 | 48 | 49 | 50 | ## Composable 51 | 52 | *Example: Instantiate a stock ResNet model* 53 | 54 | ```python 55 | from resnet_v1_c import ResNetV1 56 | 57 | # ResNet50 v1.0 from research paper 58 | resnet = ResNetV1(50) 59 | 60 | # ResNet50 v1.0 custom input shape/classes 61 | resnet = ResNetV1(50, input_shape=(128, 128, 3), n_classes=50) 62 | 63 | # getter for the tf.keras model 64 | model = resnet.model 65 | ``` 66 | 67 | *Example: Compose and Train a ResNet model* 68 | 69 | ```python 70 | ''' Example for constructing/training a ResNet V1 model on CIFAR-10 71 | ''' 72 | # Example of constructing a mini-ResNet 73 | groups = [ { 'n_filters' : 64, 'n_blocks': 1 }, 74 | { 'n_filters': 128, 'n_blocks': 2 }, 75 | { 'n_filters': 256, 'n_blocks': 2 }] 76 | resnet = ResNetV1(groups, input_shape=(32, 32, 3), n_classes=10) 77 | resnet.model.summary() 78 | resnet.cifar10() 79 | ``` 80 | 81 | -------------------------------------------------------------------------------- /zoo/resnet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/classifier.jpg -------------------------------------------------------------------------------- /zoo/resnet/identity-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/identity-block.jpg -------------------------------------------------------------------------------- /zoo/resnet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/macro.jpg -------------------------------------------------------------------------------- /zoo/resnet/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/micro.jpg -------------------------------------------------------------------------------- /zoo/resnet/projection-block-v1.5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/projection-block-v1.5.jpg -------------------------------------------------------------------------------- /zoo/resnet/projection-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/projection-block.jpg -------------------------------------------------------------------------------- /zoo/resnet/resnet34.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # ResNet 34 (2015) 16 | # Paper: https://arxiv.org/pdf/1512.03385.pdf 17 | 18 | import tensorflow 19 | from tensorflow.keras import Model, Input 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, ReLU, BatchNormalization 21 | from tensorflow.keras.layers import Add, GlobalAveragePooling2D 22 | 23 | def stem(inputs): 24 | """ Construct the Stem Convolution Group 25 | inputs : input vector 26 | """ 27 | # First Convolutional layer, where pooled feature maps will be reduced by 75% 28 | x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', activation='relu', kernel_initializer="he_normal")(inputs) 29 | x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x) 30 | return x 31 | 32 | def learner(x): 33 | """ Construct the Learner 34 | x : input to the learner 35 | """ 36 | # First Residual Block Group of 64 filters 37 | x = residual_group(x, 64, 3) 38 | 39 | # Second Residual Block Group of 128 filters 40 | x = residual_group(x, 128, 3) 41 | 42 | # Third Residual Block Group of 256 filters 43 | x = residual_group(x, 256, 5) 44 | 45 | # Fourth Residual Block Group of 512 filters 46 | x = residual_group(x, 512, 2, False) 47 | return x 48 | 49 | 50 | def residual_group(x, n_filters, n_blocks, conv=True): 51 | """ Construct a Residual Group 52 | x : input to the group 53 | n_filters: number of filters 54 | n_blocks : number of blocks in the group 55 | conv : flag to include the convolution block connector 56 | """ 57 | for _ in range(n_blocks): 58 | x = residual_block(x, n_filters) 59 | 60 | # Double the size of filters and reduce feature maps by 75% (strides=2, 2) to fit the next Residual Group 61 | if conv: 62 | x = conv_block(x, n_filters * 2) 63 | return x 64 | 65 | def residual_block(x, n_filters): 66 | """ Construct a Residual Block of Convolutions 67 | x : input into the block 68 | n_filters: number of filters 69 | """ 70 | shortcut = x 71 | x = Conv2D(n_filters, (3, 3), strides=(1, 1), padding="same", 72 | activation="relu", kernel_initializer="he_normal")(x) 73 | x = Conv2D(n_filters, (3, 3), strides=(1, 1), padding="same", 74 | activation="relu", kernel_initializer="he_normal")(x) 75 | x = Add()([shortcut, x]) 76 | return x 77 | 78 | def conv_block(x, n_filters): 79 | """ Construct Block of Convolutions without Pooling 80 | x : input into the block 81 | n_filters: number of filters 82 | """ 83 | x = Conv2D(n_filters, (3, 3), strides=(2, 2), padding="same", 84 | activation="relu", kernel_initializer="he_normal")(x) 85 | x = Conv2D(n_filters, (3, 3), strides=(2, 2), padding="same", 86 | activation="relu", kernel_initializer="he_normal")(x) 87 | return x 88 | 89 | def classifier(x, n_classes): 90 | """ Construct the Classifier Group 91 | x : input vector 92 | n_classes : number of output classes 93 | """ 94 | # Pool at the end of all the convolutional residual blocks 95 | x = GlobalAveragePooling2D()(x) 96 | 97 | # Final Dense Outputting Layer for the outputs 98 | outputs = Dense(n_classes, activation='softmax', kernel_initializer='he_normal')(x) 99 | return outputs 100 | 101 | # The input tensor 102 | inputs = Input(shape=(224, 224, 3)) 103 | 104 | # The Stem Convolution Group 105 | x = stem(inputs) 106 | 107 | # The learner 108 | x = learner(x) 109 | 110 | # The Classifier for 1000 classes 111 | outputs = classifier(x, 1000) 112 | 113 | # Instantiate the Model 114 | model = Model(inputs, outputs) 115 | 116 | -------------------------------------------------------------------------------- /zoo/resnet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnet/stem.jpg -------------------------------------------------------------------------------- /zoo/resnext/README.md: -------------------------------------------------------------------------------- 1 | 2 | # ResNeXt 3 | 4 | [resnext.py](resnext.py) - academic (idiomatic)
5 | [resnext_cifar10.py](resnext_cifar10.py) - academic (idiomatic)
6 | [resnext_c.py](resnext_c.py) - production (composable) 7 | 8 | [Paper](https://arxiv.org/pdf/1611.05431.pdf) 9 | 10 | ## Macro-Architecture 11 | 12 | 13 | 14 | ## Micro-Architecture 15 | 16 | 17 | 18 | ### Stem Group 19 | 20 | 21 | 22 | ### ResNeXt Block with Identity Shortcut 23 | 24 | 25 | 26 | ### ResNeXt Block with Projection Shortcut 27 | 28 | 29 | 30 | ### Cardinality 31 | 32 | 33 | 34 | ### Classifier 35 | 36 | 37 | 38 | ## Composable 39 | 40 | *Example: Instantiate a stock ResNeXt model* 41 | 42 | ```python 43 | from resnext_c import ResNeXt 44 | # ResNeXt50 from research paper 45 | resnext = ResNeXt(50) 46 | 47 | # ResNeXt50 custom input shape/classes 48 | resnext = ResNeXt(50, input_shape=(128, 128, 3), n_classes=50) 49 | 50 | # getter for the tf.keras model 51 | model = resnext.model 52 | ``` 53 | 54 | *Example: Compose and Train a ResNext model* 55 | 56 | ```python 57 | ''' Example for constructing/training a ResNeXt model on CIFAR-10 58 | ''' 59 | # Example of constructing a mini-ResNeXt 60 | groups = [ { 'filters_in': 128, 'filters_out' : 256, 'n_blocks': 1 }, 61 | { 'filters_in': 256, 'filters_out' : 512, 'n_blocks': 2 }, 62 | { 'filters_in': 512, 'filters_out' : 1024, 'n_blocks': 2 } ] 63 | resnext = ResNeXt(groups, input_shape=(32, 32, 3), n_classes=10) 64 | resnext.model.summary() 65 | resnext.cifar10() 66 | ``` 67 | 68 | -------------------------------------------------------------------------------- /zoo/resnext/cardinality.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/cardinality.jpg -------------------------------------------------------------------------------- /zoo/resnext/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/classifier.jpg -------------------------------------------------------------------------------- /zoo/resnext/identity-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/identity-block.jpg -------------------------------------------------------------------------------- /zoo/resnext/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/macro.jpg -------------------------------------------------------------------------------- /zoo/resnext/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/micro.jpg -------------------------------------------------------------------------------- /zoo/resnext/projection-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/projection-block.jpg -------------------------------------------------------------------------------- /zoo/resnext/resnext_cifar10.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # ResNeXt for CIFAR-10 and CIFAR-100 16 | # Paper: https://arxiv.org/pdf/1611.05431.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Model, Input 20 | from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Concatenate, Lambda, Add 21 | from tensorflow.keras.layers import Dense, GlobalAveragePooling2D 22 | 23 | def stem(inputs): 24 | """ Construct the stem convolutional group 25 | inputs : the input vector 26 | """ 27 | # Stem Convolutional layer 28 | x = Conv2D(64, (3, 3), strides=(1, 1), padding='same', kernel_initializer='he_normal', use_bias=False)(inputs) 29 | x = BatchNormalization()(x) 30 | x = ReLU()(x) 31 | return x 32 | 33 | def learner(x, cardinality=32): 34 | """ Construct the Learner 35 | x : input to the learner 36 | cardinality: width of group convolution 37 | """ 38 | # First ResNeXt Group 39 | for _ in range(3): 40 | x = resnext_block(x, 64, 128, cardinality=cardinality) 41 | 42 | # Second ResNeXt Group 43 | for _ in range(3): 44 | x = resnext_block(x, 128, 256, cardinality=cardinality) 45 | 46 | # Third ResNeXt Group 47 | for _ in range(3): 48 | x = resnext_block(x, 256, 512, cardinality=cardinality) 49 | return x 50 | 51 | 52 | def resnext_block(shortcut, filters_in, filters_out, cardinality=32): 53 | """ Construct a ResNeXT block 54 | shortcut : previous layer and shortcut for identity link 55 | filters_in : number of filters (channels) at the input convolution 56 | filters_out: number of filters (channels) at the output convolution 57 | cardinality: width of group convolution 58 | """ 59 | # Dimensionality reduction 60 | x = Conv2D(filters_in, (1, 1), strides=(1, 1), 61 | padding='same', kernel_initializer='he_normal', use_bias=False)(shortcut) 62 | x = BatchNormalization()(x) 63 | x = ReLU()(x) 64 | 65 | # Cardinality (Wide) Layer (split-transform) 66 | filters_card = filters_in // cardinality 67 | groups = [] 68 | for i in range(cardinality): 69 | group = Lambda(lambda z: z[:, :, :, i * filters_card:i * 70 | filters_card + filters_card])(x) 71 | groups.append(Conv2D(filters_card, (3, 3), strides=(1, 1), 72 | padding='same', kernel_initializer='he_normal', use_bias=False)(group)) 73 | 74 | # Concatenate the outputs of the cardinality layer together (merge) 75 | x = Concatenate()(groups) 76 | x = BatchNormalization()(x) 77 | x = ReLU()(x) 78 | 79 | # Dimensionality restoration 80 | x = Conv2D(filters_out, (1, 1), strides=(1, 1), 81 | padding='same', kernel_initializer='he_normal', use_bias=False)(x) 82 | x = BatchNormalization()(x) 83 | 84 | # If first resnext block in a group, use projection shortcut 85 | if shortcut.shape[-1] != filters_out: 86 | # use convolutional layer to double the input size to the block so it 87 | # matches the output size (so we can add them) 88 | shortcut = Conv2D(filters_out, (1, 1), strides=(1, 1), 89 | padding='same', kernel_initializer='he_normal', use_bias=False)(shortcut) 90 | shortcut = BatchNormalization()(shortcut) 91 | 92 | # Identity Link: Add the shortcut (input) to the output of the block 93 | x = Add()([shortcut, x]) 94 | x = ReLU()(x) 95 | return x 96 | 97 | def classifier(x, nclasses): 98 | """ Construct the classifier 99 | """ 100 | # Final Dense Outputting Layer for the outputs 101 | x = GlobalAveragePooling2D()(x) 102 | outputs = Dense(nclasses, activation='softmax', kernel_initializer='he_normal')(x) 103 | return outputs 104 | 105 | # Meta-parameter: width of group convolutional 106 | cardinality=32 107 | 108 | # The input tensor 109 | inputs = Input(shape=(32, 32, 3)) 110 | 111 | # The Stem Group 112 | x = stem(inputs) 113 | 114 | # The learner 115 | x = learner(x, cardinality=cardinality) 116 | 117 | # The Classifier for the 10 outputs 118 | outputs = classifier(x, 10) 119 | 120 | # Instantiate the Model 121 | model = Model(inputs, outputs) 122 | -------------------------------------------------------------------------------- /zoo/resnext/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/resnext/stem.jpg -------------------------------------------------------------------------------- /zoo/senet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # SE-Net 3 | 4 | [se_resnet.py](se_resnet.py) - academic (idiomatic)
5 | [se_resnext.py](se_resnext.py) - academic (idiomatic)
6 | [se_resnet_c.py](se_resnet_c.py) - production (composable)
7 | [se_resnext_c.py](se_resnext_c.py) - production (composable) 8 | 9 | [Paper](https://arxiv.org/pdf/1709.01507.pdf) 10 | 11 | ## Macro-Architecture 12 | 13 | 14 | 15 | ## Micro-Architecture 16 | 17 | 18 | 19 | ### Residual Block and Identity Shortcut w/SE Link 20 | 21 | 22 | 23 | ### Residual Block and Projection Shortcut w/SE Link 24 | 25 | 26 | 27 | ### Squeeze-Excitation Block 28 | 29 | 30 | 31 | ## Composable 32 | 33 | *Example: Instantiate a stock SE-ResNet model* 34 | 35 | ```python 36 | from se_resnet_c import SEResNet 37 | 38 | # SE-ResNet50 from research paper 39 | senet = SEResNet(50) 40 | 41 | # ResNet50 custom input shape/classes 42 | senet = SEResNet(50, input_shape=(128, 128, 3), n_classes=50) 43 | 44 | # getter for the tf.keras model 45 | model = senet.model 46 | ``` 47 | 48 | *Example: Compose and Train a SE-ResNet* 49 | 50 | ```python 51 | ''' Example for constructing/training a SE-ResNet model on CIFAR-10 52 | ''' 53 | # Example of constructing a mini-ResNet 54 | groups = [ { 'n_filters' : 64, 'n_blocks': 1 }, 55 | { 'n_filters': 128, 'n_blocks': 2 }, 56 | { 'n_filters': 256, 'n_blocks': 2 } ] 57 | senet = SEResNet(groups, input_shape=(32, 32, 3), n_classes=10) 58 | senet.model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) 59 | senet.model.summary() 60 | senet.cifar10() 61 | ``` 62 | -------------------------------------------------------------------------------- /zoo/senet/identity-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/senet/identity-block.jpg -------------------------------------------------------------------------------- /zoo/senet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/senet/macro.jpg -------------------------------------------------------------------------------- /zoo/senet/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/senet/micro.jpg -------------------------------------------------------------------------------- /zoo/senet/projection-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/senet/projection-block.jpg -------------------------------------------------------------------------------- /zoo/senet/se-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/senet/se-block.jpg -------------------------------------------------------------------------------- /zoo/shufflenet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # ShuffleNet v1.0 3 | 4 | [shufflenet.py](shufflenet.py) - academic (idiomatic)
5 | [shufflenet_c.py](shufflenet_c.py) - production (composable) 6 | 7 | [Paper](https://arxiv.org/pdf/1707.01083.pdf) 8 | 9 | ## Macro-Architecture 10 | 11 | 12 | 13 | ## Micro-Architecture 14 | 15 | 16 | 17 | ### Stem Group 18 | 19 | 20 | 21 | ### Shuffle Block 22 | 23 | 24 | 25 | ### Strided Shuffle Block 26 | 27 | 28 | 29 | ### Classifier 30 | 31 | 32 | 33 | ## Composable 34 | 35 | *Example: Instantiate a stock ShuffleNet model* 36 | 37 | ```python 38 | from shufflenet_c import ShuffleNet 39 | 40 | # ShuffleNet v1 from research paper 41 | shufflenet = ShuffleNet() 42 | 43 | # ShuffleNet v1 custom input shape/classes 44 | shufflenet = ShuffleNet(input_shape=(128, 128, 3), n_classes=50) 45 | 46 | # getter for the tf.keras model 47 | model = shufflenet.model 48 | ``` 49 | 50 | *Example: Compose and Train a ShuffleNet model* 51 | 52 | ```python 53 | ''' Example for constructing/training a ShuffleNet model on CIFAR-10 54 | ''' 55 | # Example of constructing a mini-ShuffleNet 56 | groups = [ { 'n_blocks' : 2 }, { 'n_blocks' : 4 } ] 57 | shufflenet = ShuffleNet(groups, input_shape=(32, 32, 3), n_classes=10) 58 | shufflenet.model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) 59 | shufflenet.model.summary() 60 | shufflenet.cifar10() 61 | ``` 62 | -------------------------------------------------------------------------------- /zoo/shufflenet/block.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/block.png -------------------------------------------------------------------------------- /zoo/shufflenet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/classifier.jpg -------------------------------------------------------------------------------- /zoo/shufflenet/macro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/macro.png -------------------------------------------------------------------------------- /zoo/shufflenet/micro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/micro.png -------------------------------------------------------------------------------- /zoo/shufflenet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/stem.jpg -------------------------------------------------------------------------------- /zoo/shufflenet/strided-block.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/shufflenet/strided-block.png -------------------------------------------------------------------------------- /zoo/siamese_twin/README.md: -------------------------------------------------------------------------------- 1 | # Siamese Neural Network 2 | 3 | [siamese_twin.py](siamese_twin.py) - academic (idiomatic) 4 | [siamese_twin_c.py](siamese_twin_c.py) - production (composable) 5 | 6 | [Paper](https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pdf) 7 | 8 | ## Macro-Architecture 9 | 10 | ### Siamese Neural Network 11 | 12 | 13 | 14 | ### Twin Micro-Architecture 15 | 16 | 17 | 18 | ### Stem Group 19 | 20 | 21 | 22 | ### Convolutional Block 23 | 24 | 25 | 26 | ### Encoder 27 | 28 | 29 | 30 | ### Classifier Group 31 | 32 | 33 | 34 | ## Composable 35 | 36 | *Example: Instantiate a stock Siamese Twin model*/ 37 | 38 | ```python 39 | from siamese_twin_c import SiameseTwin 40 | 41 | # Siamese Twin from research paper 42 | siam = SiameseTwin() 43 | 44 | # getter for the tf.keras model 45 | model = siam.model 46 | ``` 47 | 48 | -------------------------------------------------------------------------------- /zoo/siamese_twin/block-conv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/block-conv.jpg -------------------------------------------------------------------------------- /zoo/siamese_twin/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/classifier.jpg -------------------------------------------------------------------------------- /zoo/siamese_twin/encoder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/encoder.jpg -------------------------------------------------------------------------------- /zoo/siamese_twin/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/macro.jpg -------------------------------------------------------------------------------- /zoo/siamese_twin/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/micro.jpg -------------------------------------------------------------------------------- /zoo/siamese_twin/siamese_twin.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Siamese Net for One-Shot Image Classification (Koch, et. al.) 16 | # Paper: https://www.cs.cmu.edu/~rsalakhu/papers/oneshot1.pd 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Input, Sequential, Model 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Lambda 21 | from tensorflow.keras.initializers import RandomNormal 22 | import tensorflow.keras.backend as K 23 | 24 | def twin(input_shape): 25 | ''' Construct the model for both twins of the Siamese (connected) Network 26 | input_shape : input shape for input vector 27 | ''' 28 | global dense_weights, biases 29 | 30 | model = Sequential() 31 | 32 | # The weights for the convolutional layers are initialized from a normal distribution 33 | # with a zero_mean and standard deviation of 10e-2 34 | conv_weights = RandomNormal(mean=0.0, stddev=10e-2) 35 | 36 | # The weights for the dense layers are initialized from a normal distribution 37 | # with a mean of 0 and standard deviation of 2 * 10e-1 38 | dense_weights = RandomNormal(mean=0.0, stddev=(2 * 10e-1)) 39 | 40 | # The biases for all layers are initialized from a normal distribution 41 | # with a mean of 0.5 and standard deviation of 10e-2 42 | biases = RandomNormal(mean=0.5, stddev=10e-2) 43 | 44 | def stem(input_shape): 45 | ''' Construct the Stem Group 46 | input_shape: input shape for input vector 47 | ''' 48 | 49 | # entry convolutional layer and reduce feature maps by 75% (max pooling) 50 | model.add(Conv2D(64, (10, 10), activation='relu', kernel_initializer=conv_weights, bias_initializer=biases, input_shape=input_shape)) 51 | model.add(MaxPooling2D((2, 2), strides=2)) 52 | 53 | def block(): 54 | ''' Construct a Convolutional Block ''' 55 | 56 | # 2nd convolutional layer doubling the number of filters, and reduce feature maps by 75% (max pooling) 57 | model.add(Conv2D(128, (7, 7), activation='relu', kernel_initializer=conv_weights, bias_initializer=biases)) 58 | model.add(MaxPooling2D((2, 2), strides=2)) 59 | 60 | # 3rd convolutional layer and reduce feature maps by 75% (max pooling) 61 | model.add(Conv2D(128, (4, 4), activation='relu', kernel_initializer=conv_weights, bias_initializer=biases)) 62 | model.add(MaxPooling2D((2, 2), strides=2)) 63 | 64 | # 4th convolutional layer doubling the number of filters with no feature map downsampling 65 | model.add(Conv2D(256, (4, 4), activation='relu', kernel_initializer=conv_weights, bias_initializer=biases)) 66 | # for a 105x105 input, the feature map size will be 6x6 67 | 68 | def encoder(): 69 | ''' Construct the Encoding block ''' 70 | # flatten the maps into a 1D vector 71 | model.add(Flatten()) 72 | 73 | # use dense layer to produce a 4096 encoding of the flattened feature maps 74 | model.add(Dense(4096, activation='sigmoid', kernel_initializer=dense_weights, bias_initializer=biases)) 75 | 76 | # Build the model 77 | stem(input_shape) 78 | block() 79 | encoder() 80 | return model 81 | 82 | # Input shape for the Omniglot dataset 83 | input_shape = (105, 105, 3) 84 | 85 | # Create the twin model using the Sequential API 86 | model = twin(input_shape) 87 | 88 | # Create input tensors for the left and right side (twins) of the network. 89 | left_input = Input(input_shape) 90 | right_input = Input(input_shape) 91 | 92 | # Create the encoders for the left and right side (twins) 93 | left = model( left_input ) 94 | right = model( right_input ) 95 | 96 | # Use Lambda method to create a custom layer for implementing a L1 distance layer. 97 | L1Distance = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1])) 98 | 99 | # Connect the left and right twins (via encoders) to the layer that calculates the 100 | # distance between the encodings. 101 | connected = L1Distance([left, right]) 102 | 103 | # Create the output layer for predicting the similarity from the distance layer 104 | outputs = Dense(1,activation='sigmoid', kernel_initializer=dense_weights, bias_initializer=biases)(connected) 105 | 106 | # Create the Siamese Network model 107 | # Connect the left and right inputs to the outputs 108 | model = Model(inputs=[left_input,right_input],outputs=outputs) 109 | 110 | 111 | -------------------------------------------------------------------------------- /zoo/siamese_twin/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/siamese_twin/stem.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/README.md: -------------------------------------------------------------------------------- 1 | 2 | # SqueezeNet 3 | 4 | [squeezenet(_bypass/_complex).py](squeezenet.py) - academic (idiomatic)
5 | [squeezenet(_bypass/_complex)_c.py](squeezenet_c.py) - production (composable) 6 | 7 | [Paper](https://arxiv.org/pdf/1602.07360.pdf) 8 | 9 | ## Macro-Architecture 10 | 11 | 12 | 13 | ## Micro-Architecture 14 | 15 | 16 | 17 | ### Stem Group 18 | 19 | 20 | 21 | ### Fire Block 22 | 23 | 24 | 25 | ### Classifier Group 26 | 27 | 28 | 29 | ## Fire Bypass Micro-Archirecture 30 | 31 | 32 | 33 | ### Fire Bypass Block 34 | 35 | 36 | 37 | ## Fire Complex Bypass Micro-Archirecture 38 | 39 | 40 | 41 | ### Fire Complex Bypass Block 42 | 43 | 44 | 45 | ## Composable 46 | 47 | *Example: Instantiate a stock SqueezeNet model* 48 | 49 | ```python 50 | from squeezenet_c import SqueezeNet 51 | # SqueezeNet from research paper 52 | squeezenet = SqueezeNet() 53 | 54 | # ResNeXt50 custom input shape/classes 55 | squeezenet = SqueezeNet(input_shape=(128, 128, 3), n_classes=50) 56 | 57 | # getter for the tf.keras model 58 | model = squeezenet.model 59 | ``` 60 | 61 | *Example: Compose and Train a SqueezeNet model* 62 | 63 | ```python 64 | ''' Example for constructing/training a SqueezeNet model on CIFAR-10 65 | ''' 66 | # Example of constructing a mini-SqueezeNet 67 | groups = [ [ { 'n_filters' : 16 }, { 'n_filters' : 16 }, { 'n_filters' : 32 } ], 68 | [ { 'n_filters' : 64 } ] ] 69 | squeezenet = SqueezeNet(groups, input_shape=(32, 32, 3), n_classes=10) 70 | squeezenet.model.summary() 71 | squeezenet.cifar10() 72 | ``` 73 | -------------------------------------------------------------------------------- /zoo/squeezenet/bypass-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/bypass-block.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/classifier.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/complex-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/complex-block.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/fire.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/fire.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/macro.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/micro-bypass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/micro-bypass.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/micro-complex.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/micro-complex.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/micro.jpg -------------------------------------------------------------------------------- /zoo/squeezenet/squeezenet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # SqueezeNet v1.0 (2016) 16 | # Paper: https://arxiv.org/pdf/1602.07360.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Input, Model 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Concatenate, Dropout 21 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation 22 | 23 | def stem(inputs): 24 | ''' Construct the Stem Group 25 | inputs: the input tensor 26 | ''' 27 | x = Conv2D(96, (7, 7), strides=2, padding='same', activation='relu', 28 | kernel_initializer='glorot_uniform')(inputs) 29 | x = MaxPooling2D(3, strides=2)(x) 30 | return x 31 | 32 | def learner(x): 33 | ''' Construct the Learner 34 | x : input to the learner 35 | ''' 36 | # First fire group, progressively increase number of filters 37 | x = group(x, [16, 16, 32]) 38 | 39 | # Second fire group 40 | x = group(x, [32, 48, 48, 64]) 41 | 42 | # Last fire block (module) 43 | x = fire_block(x, 64) 44 | 45 | # Dropout is delayed to end of fire groups 46 | x = Dropout(0.5)(x) 47 | return x 48 | 49 | def group(x, filters): 50 | ''' Construct a Fire Group 51 | x : input to the group 52 | filters: list of number of filters per fire block (module) 53 | ''' 54 | # Add the fire blocks (modules) for this group 55 | for n_filters in filters: 56 | x = fire_block(x, n_filters) 57 | 58 | # Delayed downsampling 59 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 60 | return x 61 | 62 | 63 | def fire_block(x, n_filters): 64 | ''' Construct a Fire Block 65 | x : input to the block 66 | n_filters: number of filters 67 | ''' 68 | # squeeze layer 69 | squeeze = Conv2D(n_filters, (1, 1), strides=1, activation='relu', 70 | padding='same', kernel_initializer='glorot_uniform')(x) 71 | 72 | # branch the squeeze layer into a 1x1 and 3x3 convolution and double the number 73 | # of filters 74 | expand1x1 = Conv2D(n_filters * 4, (1, 1), strides=1, activation='relu', 75 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 76 | expand3x3 = Conv2D(n_filters * 4, (3, 3), strides=1, activation='relu', 77 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 78 | 79 | # concatenate the feature maps from the 1x1 and 3x3 branches 80 | x = Concatenate()([expand1x1, expand3x3]) 81 | return x 82 | 83 | def classifier(x, n_classes): 84 | ''' Construct the Classifier 85 | x : input to the classifier 86 | n_classes: number of output classes 87 | ''' 88 | # set the number of filters equal to number of classes 89 | x = Conv2D(n_classes, (1, 1), strides=1, activation='relu', padding='same', 90 | kernel_initializer='glorot_uniform')(x) 91 | 92 | # reduce each filter (class) to a single value 93 | x = GlobalAveragePooling2D()(x) 94 | x = Activation('softmax')(x) 95 | return x 96 | 97 | # The input shape 98 | inputs = Input((224, 224, 3)) 99 | 100 | # The Stem Group 101 | x = stem(inputs) 102 | 103 | # The Learner 104 | x = learner(x) 105 | 106 | # The classifier 107 | outputs = classifier(x, 1000) 108 | 109 | # Instantiate the Model 110 | model = Model(inputs, outputs) 111 | -------------------------------------------------------------------------------- /zoo/squeezenet/squeezenet_bypass.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # SqueezeNet v1.0 with simple bypass (2016) 16 | # Paper: https://arxiv.org/pdf/1602.07360.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Input, Model 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Concatenate, Add, Dropout 21 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation 22 | 23 | def stem(inputs): 24 | ''' Construct the Stem Group 25 | inputs : input to the stem 26 | ''' 27 | x = Conv2D(96, (7, 7), strides=2, padding='same', activation='relu', 28 | kernel_initializer='glorot_uniform')(inputs) 29 | x = MaxPooling2D(3, strides=2)(x) 30 | return x 31 | 32 | def learner(x): 33 | ''' Construct the Learner 34 | x : input to the learner 35 | ''' 36 | # Fire blocks with simple bypass on blocks 2, 4, 6 and 8 37 | 38 | # First Fire group, progressively increase number of filters 39 | x = group(x, [(16, False), (16, True), (32, False)]) 40 | 41 | # Second Fire group 42 | x = group(x, [(32, True), (48, False), (48, True), (64, False)]) 43 | 44 | # Last fire block 45 | x = fire_block(x, 64, True) 46 | 47 | # Dropout is delayed to end of fire groups 48 | x = Dropout(0.5)(x) 49 | return x 50 | 51 | def group(x, filters): 52 | ''' Construct the Fire Group 53 | x : input to the group 54 | filters : list of number of filters per fire block in group 55 | ''' 56 | for n_filters, bypass in filters: 57 | x = fire_block(x, n_filters) 58 | 59 | # Delayed downsampling 60 | x = MaxPooling2D((3, 3), strides=2)(x) 61 | return x 62 | 63 | def fire_block(x, n_filters, bypass=False): 64 | ''' Construct a Fire Block 65 | x : input to the block 66 | n_filters: number of filters in the block 67 | bypass : whether block has an identity shortcut 68 | ''' 69 | # remember the input 70 | shortcut = x 71 | 72 | # squeeze layer 73 | squeeze = Conv2D(n_filters, (1, 1), strides=1, activation='relu', 74 | padding='same', kernel_initializer='glorot_uniform')(x) 75 | 76 | # branch the squeeze layer into a 1x1 and 3x3 convolution and double the number 77 | # of filters 78 | expand1x1 = Conv2D(n_filters * 4, (1, 1), strides=1, activation='relu', 79 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 80 | expand3x3 = Conv2D(n_filters * 4, (3, 3), strides=1, activation='relu', 81 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 82 | 83 | # concatenate the feature maps from the 1x1 and 3x3 branches 84 | x = Concatenate()([expand1x1, expand3x3]) 85 | 86 | # if identity link, add (matrix addition) input filters to output filters 87 | if bypass: 88 | x = Add()([x, shortcut]) 89 | 90 | return x 91 | 92 | def classifier(x, n_classes): 93 | ''' Construct the Classifier ''' 94 | # set the number of filters equal to number of classes 95 | x = Conv2D(n_classes, (1, 1), strides=1, activation='relu', padding='same', 96 | kernel_initializer='glorot_uniform')(x) 97 | # reduce each filter (class) to a single value 98 | x = GlobalAveragePooling2D()(x) 99 | x = Activation('softmax')(x) 100 | return x 101 | 102 | # The input shape 103 | inputs = Input((224, 224, 3)) 104 | 105 | # The Stem Group 106 | x = stem(inputs) 107 | 108 | # The Learner 109 | x = learner(x) 110 | 111 | # The Classifier 112 | outputs = classifier(x, 1000) 113 | 114 | model = Model(inputs, outputs) 115 | -------------------------------------------------------------------------------- /zoo/squeezenet/squeezenet_complex.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # SqueezeNet v1.0 with complex bypass (i.e., transition convolution on identify link) (2016) 16 | # Paper: https://arxiv.org/pdf/1602.07360.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Input, Model 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Concatenate, Add, Dropout 21 | from tensorflow.keras.layers import GlobalAveragePooling2D, Activation 22 | 23 | def stem(inputs): 24 | ''' Construct the Stem Group 25 | inputs : input tensor 26 | ''' 27 | x = Conv2D(96, (7, 7), strides=2, padding='same', activation='relu', 28 | kernel_initializer='glorot_uniform')(inputs) 29 | x = MaxPooling2D(3, strides=2)(x) 30 | return x 31 | 32 | def learner(x): 33 | ''' Construct the Learner 34 | x : input to the learner 35 | ''' 36 | # First Fire group, progressively increase number of filters 37 | x = group(x, [16, 16, 32]) 38 | 39 | # Second Fire group 40 | x = group(x, [32, 48, 48, 64]) 41 | 42 | # Last Fire block 43 | x = fire_block(x, 64) 44 | 45 | # Dropout is delayed to end of fire modules 46 | x = Dropout(0.5)(x) 47 | return x 48 | 49 | def group(x, filters): 50 | ''' Construct a Fire Group 51 | x : input to the group 52 | filters: list of number of filters per block in group 53 | ''' 54 | for n_filters in filters: 55 | x = fire_block(x, n_filters) 56 | 57 | # Delayed downsampling 58 | x = MaxPooling2D((3, 3), strides=2)(x) 59 | return x 60 | 61 | 62 | def fire_block(x, n_filters): 63 | ''' Construct a Fire Block with complex bypass 64 | x : input to the block 65 | n_filters: number of filters in block 66 | ''' 67 | # remember the input (identity) 68 | shortcut = x 69 | 70 | # if the number of input filters does not equal the number of output filters, then use 71 | # a transition convolution to match the number of filters in identify link to output 72 | if shortcut.shape[3] != 8 * n_filters: 73 | shortcut = Conv2D(n_filters * 8, (1, 1), strides=1, activation='relu', 74 | padding='same', kernel_initializer='glorot_uniform')(shortcut) 75 | 76 | # squeeze layer 77 | squeeze = Conv2D(n_filters, (1, 1), strides=1, activation='relu', 78 | padding='same', kernel_initializer='glorot_uniform')(x) 79 | 80 | # branch the squeeze layer into a 1x1 and 3x3 convolution and double the number 81 | # of filters 82 | expand1x1 = Conv2D(n_filters * 4, (1, 1), strides=1, activation='relu', 83 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 84 | expand3x3 = Conv2D(n_filters * 4, (3, 3), strides=1, activation='relu', 85 | padding='same', kernel_initializer='glorot_uniform')(squeeze) 86 | 87 | # concatenate the feature maps from the 1x1 and 3x3 branches 88 | x = Concatenate()([expand1x1, expand3x3]) 89 | 90 | # if identity link, add (matrix addition) input filters to output filters 91 | if shortcut is not None: 92 | x = Add()([x, shortcut]) 93 | return x 94 | 95 | def classifier(x, n_classes): 96 | ''' Construct the Classifier 97 | x : input to the classifier 98 | n_classes: number of output classes 99 | ''' 100 | # set the number of filters equal to number of classes 101 | x = Conv2D(n_classes, (1, 1), strides=1, activation='relu', padding='same', 102 | kernel_initializer='glorot_uniform')(x) 103 | # reduce each filter (class) to a single value 104 | x = GlobalAveragePooling2D()(x) 105 | x = Activation('softmax')(x) 106 | return x 107 | 108 | # The input shape 109 | inputs = Input((224, 224, 3)) 110 | 111 | # The Stem Group 112 | x = stem(inputs) 113 | 114 | # The Learner 115 | x = learner(x) 116 | 117 | # The Classifier 118 | outputs = classifier(x, 1000) 119 | 120 | model = Model(inputs, outputs) 121 | -------------------------------------------------------------------------------- /zoo/squeezenet/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/squeezenet/stem.jpg -------------------------------------------------------------------------------- /zoo/srcnn/README.md: -------------------------------------------------------------------------------- 1 | # SRCNN 2 | 3 | [srcnn.py](srcnn.py) - academic (idiomatic)
4 | [srcnn-post.py](srcnn-post.py) - academic (idiomatic)
5 | [srcnn_c.py](srcnn_c.py) - production (composable)
6 | [srcnn-post_c.py](srcnn-post_c.py) - production (composable)
7 | 8 | [Paper](https://arxiv.org/pdf/1501.00092.pdf) 9 | 10 | ## Macro-Architecture 11 | 12 | ### Pre-Upsampling SR 13 | 14 | 15 | 16 | ### Post-Upsampling SR 17 | 18 | 19 | -------------------------------------------------------------------------------- /zoo/srcnn/macro-post.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/srcnn/macro-post.jpg -------------------------------------------------------------------------------- /zoo/srcnn/macro-pre.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/srcnn/macro-pre.jpg -------------------------------------------------------------------------------- /zoo/srcnn/srcnn-post.py: -------------------------------------------------------------------------------- 1 | # Licensed under the Apache License, Version 2.0 (the "License"); 2 | # you may not use this file except in compliance with the License. 3 | # You may obtain a copy of the License at 4 | # 5 | # https://www.apache.org/licenses/LICENSE-2.0 6 | # 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | 13 | # Super Resolution CNN (SRCNN) 14 | # Paper: https://arxiv.org/pdf/1501.00092.pdf 15 | # This is a post upsampling version of SRCNN not presented in the paper 16 | 17 | from tensorflow.keras import Input, Model 18 | from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Conv2DTranspose, Activation 19 | from tensorflow.keras.optimizers import Adam 20 | 21 | def stem(inputs): 22 | x = Conv2D(16, (3, 3), padding='same')(inputs) 23 | x = BatchNormalization()(x) 24 | x = ReLU()(x) 25 | return x 26 | 27 | def learner(x): 28 | x = Conv2D(32, (3, 3), padding='same')(x) 29 | x = BatchNormalization()(x) 30 | x = ReLU()(x) 31 | x = Conv2D(64, (3, 3), padding='same')(x) 32 | x = BatchNormalization()(x) 33 | x = ReLU()(x) 34 | return x 35 | 36 | def decoder(x): 37 | x = Conv2DTranspose(3, (3, 3), strides=2, padding='same')(x) 38 | x = BatchNormalization()(x) 39 | x = Activation('sigmoid')(x) 40 | return x 41 | 42 | inputs = Input((16, 16, 3)) 43 | x = stem(inputs) 44 | x = learner(x) 45 | outputs = decoder(x) 46 | 47 | model = Model(inputs, outputs) 48 | model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.001), 49 | metrics=['accuracy']) 50 | 51 | from tensorflow.keras.datasets import cifar10 52 | import numpy as np 53 | import cv2 54 | 55 | def train(): 56 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 57 | 58 | x_train_lr = [] 59 | for image in x_train: 60 | x_train_lr.append(cv2.resize(image, (16, 16, 3))) 61 | x_train_lr = np.asarray(x_train_lr) 62 | 63 | x_train = (x_train / 255.0).astype(np.float32) 64 | x_train_lr = (x_train_lr / 255.0).astype(np.float32) 65 | 66 | model.fit(x_train_lr, x_train, epochs=25, batch_size=32, verbose=1, 67 | validation_split=0.1) 68 | 69 | -------------------------------------------------------------------------------- /zoo/srcnn/srcnn-post_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Post-Upsampling Super Resolution CNN (SRCNN) (2016) 16 | # Paper: https://arxiv.org/pdf/1501.00092.pdf 17 | 18 | from tensorflow.keras import Input, Model 19 | from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Conv2DTranspose, Activation 20 | from tensorflow.keras.optimizers import Adam 21 | from tensorflow.keras.regularizers import l2 22 | 23 | import sys 24 | sys.path.append('../') 25 | from models_c import Composable 26 | 27 | class SRCNNPost(Composable): 28 | ''' Construct a Post Upsampling Super Resolution CNN ''' 29 | # Meta-parameter: 30 | groups = [ { 'n_filters': 32, 'n_filters' : 64 } ] 31 | 32 | # Initial Hyperparameters 33 | hyperparameters = { 'initializer': 'he_normal', 34 | 'regularizer': None, 35 | 'relu_clip' : None, 36 | 'bn_epsilon' : None, 37 | 'use_bias' : False 38 | } 39 | 40 | def __init__(self, groups=None , 41 | input_shape=(32, 32, 3), include_top=True, 42 | **hyperparameters): 43 | """ Construct a Wids Residual (Convolutional Neural) Network 44 | groups : metaparameter for group configuration 45 | input_shape : input shape 46 | include_top : include the reconstruction component 47 | initializer : kernel initialization 48 | regularizer : kernel regularization 49 | relu_clip : max value for ReLU 50 | bn_epsilon : epsilon for batch norm 51 | use_bias : whether use bias in conjunction with batch norm 52 | """ 53 | # Configure base (super) class 54 | Composable.__init__(self, input_shape, include_top, self.hyperparameters, **hyperparameters) 55 | 56 | if groups is None: 57 | groups = self.groups 58 | 59 | # The input tensor 60 | inputs = Input(input_shape) 61 | 62 | # The stem convolutional group 63 | x = self.stem(inputs) 64 | 65 | # The learner 66 | outputs = self.learner(x, groups) 67 | 68 | # The reconstruction 69 | if include_top: 70 | outputs = self.decoder(outputs) 71 | 72 | # Instantiate the Model 73 | self._model = Model(inputs, outputs) 74 | 75 | def stem(self, inputs): 76 | """ Construct the Stem Convolutional Group 77 | inputs : the input tensor 78 | """ 79 | # n1, dimensionality expansion with large coarse filter 80 | x = self.Conv2D(inputs, 16, (3, 3), padding='same') 81 | x = self.BatchNormalization(x) 82 | x = self.ReLU(x) 83 | return x 84 | 85 | def learner(self, x, groups): 86 | """ Construct the Learner 87 | x : input to the learner 88 | groups : group configuration 89 | """ 90 | for group in groups: 91 | n_filters = group['n_filters' ] 92 | x = self.Conv2D(x, n_filters, (3, 3), padding='same') 93 | x = self.BatchNormalization(x) 94 | x = self.ReLU(x) 95 | return x 96 | 97 | def decoder(self, x): 98 | """ Construct the Decoder 99 | x : input to the decoder 100 | """ 101 | # reconstruction 102 | x = self.Conv2DTranspose(x, 3, (3, 3), strides=2, padding='same') 103 | x = self.BatchNormalization(x) 104 | x = Activation('sigmoid')(x) 105 | return x 106 | 107 | # Example 108 | # srcnn = SRCNNPost() 109 | -------------------------------------------------------------------------------- /zoo/srcnn/srcnn.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Super Resolution CNN (SRCNN) 16 | # Paper: https://arxiv.org/pdf/1501.00092.pdf 17 | 18 | from tensorflow.keras import Input, Model 19 | from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Conv2DTranspose, Activation 20 | from tensorflow.keras.optimizers import Adam 21 | 22 | def stem(inputs): 23 | # dimensionality expansion with large coarse filter 24 | x = Conv2D(64, (9, 9), padding='same')(inputs) 25 | x = BatchNormalization()(x) 26 | x = ReLU()(x) 27 | return x 28 | 29 | def encoder(x): 30 | # 1x1 bottleneck convolution 31 | x = Conv2D(32, (1, 1), padding='same')(x) 32 | x = BatchNormalization()(x) 33 | x = ReLU()(x) 34 | 35 | # Reconstruction 36 | x = Conv2D(3, (5, 5), padding='same')(x) 37 | x = BatchNormalization()(x) 38 | outputs = Activation('sigmoid')(x) 39 | return outputs 40 | 41 | inputs = Input((32, 32, 3)) 42 | x = stem(inputs) 43 | outputs = encoder(x) 44 | 45 | model = Model(inputs, outputs) 46 | model.compile(loss='mean_squared_error', optimizer=Adam(lr=0.001), metrics=['accuracy']) 47 | 48 | from tensorflow.keras.datasets import cifar10 49 | import numpy as np 50 | import cv2 51 | def train(): 52 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 53 | 54 | x_train_lr = [] 55 | for image in x_train: 56 | image = cv2.resize(image, (16, 16), interpolation=cv2.INTER_CUBIC) 57 | x_train_lr.append(cv2.resize(image, (32, 32), interpolation=cv2.INTER_CUBIC)) 58 | x_train_lr = np.asarray(x_train_lr) 59 | 60 | x_test_lr = [] 61 | for image in x_test: 62 | image = cv2.resize(image, (16, 16), interpolation=cv2.INTER_CUBIC) 63 | x_test_lr.append(cv2.resize(image, (32, 32), interpolation=cv2.INTER_CUBIC)) 64 | x_test_lr = np.asarray(x_test_lr) 65 | 66 | x_train = (x_train / 255.0).astype(np.float32) 67 | x_train_lr = (x_train_lr / 255.0).astype(np.float32) 68 | 69 | x_test = (x_test / 255.0).astype(np.float32) 70 | x_test_lr = (x_test_lr / 255.0).astype(np.float32) 71 | model.fit(x_train_lr, x_train, epochs=25, batch_size=32, verbose=1, validation_split=0.1) 72 | -------------------------------------------------------------------------------- /zoo/srcnn/srcnn_c.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Super Resolution CNN (SRCNN) (2016) 16 | # Paper: https://arxiv.org/pdf/1501.00092.pdf 17 | 18 | from tensorflow.keras import Input, Model 19 | from tensorflow.keras.layers import Conv2D, BatchNormalization, ReLU, Conv2DTranspose, Activation 20 | from tensorflow.keras.optimizers import Adam 21 | from tensorflow.keras.regularizers import l2 22 | 23 | import sys 24 | sys.path.append('../') 25 | from models_c import Composable 26 | 27 | class SRCNN(Composable): 28 | ''' Construct a Super Resolution CNN ''' 29 | # Meta-parameter: filter sizes for n1, n2 and n3 convolutions 30 | f1 = 9 31 | f2 = 1 32 | f3 = 5 33 | 34 | # Initial Hyperparameters 35 | hyperparameters = { 'initializer': 'he_normal', 36 | 'regularizer': None, 37 | 'relu_clip' : None, 38 | 'bn_epsilon' : None, 39 | 'use_bias' : False 40 | } 41 | 42 | def __init__(self, 43 | input_shape=(32, 32, 3), include_top=True, 44 | f1 = 9, f2=1, f3=5, 45 | **hyperparameters): 46 | """ Construct a Wids Residual (Convolutional Neural) Network 47 | f1, f2, f3 : number of filters for convolutional layers n1, n2 and n3 48 | input_shape : input shape 49 | include_top : include the reconstruction component 50 | initializer : kernel initialization 51 | regularizer : kernel regularization 52 | relu_clip : max value for ReLU 53 | bn_epsilon : epsilon for batch norm 54 | use_bias : whether use bias in conjunction with batch norm 55 | """ 56 | # Configure base (super) class 57 | Composable.__init__(self, input_shape, include_top, self.hyperparameters, **hyperparameters) 58 | 59 | # The input tensor 60 | inputs = Input(input_shape) 61 | 62 | # The stem convolutional group 63 | x = self.stem(inputs, f1) 64 | 65 | # The encoder 66 | outputs = self.encoder(x, f2) 67 | 68 | # The reconstruction 69 | if include_top: 70 | outputs = self.reconstruction(outputs, f3) 71 | 72 | # Instantiate the Model 73 | self._model = Model(inputs, outputs) 74 | 75 | def stem(self, inputs, f1): 76 | """ Construct the Stem Convolutional Group 77 | inputs : the input tensor 78 | f1 : filter size 79 | """ 80 | # n1, dimensionality expansion with large coarse filter 81 | x = self.Conv2D(inputs, 64, (f1, f1), padding='same') 82 | x = self.BatchNormalization(x) 83 | x = self.ReLU(x) 84 | return x 85 | 86 | def encoder(self, x, f2): 87 | """ Construct the Encoder 88 | x : the input to the encoder 89 | f2 : the filter size 90 | """ 91 | # n2, 1x1 bottleneck convolution 92 | x = self.Conv2D(x, 32, (f2, f2), padding='same') 93 | x = self.BatchNormalization(x) 94 | x = self.ReLU(x) 95 | return x 96 | 97 | def reconstruction(self, x, f3): 98 | """ Construct the Encoder 99 | x : the input to the reconstruction 100 | f3 : the filter size 101 | """ 102 | # n3, reconstruction convolution 103 | x = self.Conv2D(x, 3, (f3, f3), padding='same') 104 | x = self.BatchNormalization(x) 105 | outputs = Activation('sigmoid')(x) 106 | return outputs 107 | 108 | # Example 109 | # srcnn = SRCNN(f1=9, f2=1, f3=5) 110 | -------------------------------------------------------------------------------- /zoo/unet/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/classifier.jpg -------------------------------------------------------------------------------- /zoo/unet/contract-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/contract-block.jpg -------------------------------------------------------------------------------- /zoo/unet/contract-macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/contract-macro.jpg -------------------------------------------------------------------------------- /zoo/unet/expand-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/expand-block.jpg -------------------------------------------------------------------------------- /zoo/unet/expand-macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/expand-macro.jpg -------------------------------------------------------------------------------- /zoo/unet/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/unet/macro.jpg -------------------------------------------------------------------------------- /zoo/unet/readme.md: -------------------------------------------------------------------------------- 1 | 2 | # U-Net 3 | 4 | [unet_c.py](unet_c.py) - production (composable) 5 | 6 | [Paper](https://arxiv.org/pdf/1505.04597.pdf) 7 | 8 | ## Macro-Architecture 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | ## Micro-Architecture 17 | 18 | ### Contracting Group 19 | 20 | 21 | 22 | ### Expanding Group 23 | 24 | 25 | 26 | ### Classifier 27 | 28 | 29 | 30 | ## Composable 31 | 32 | *Example: Instantiate a stock U-Net model* 33 | 34 | ```python 35 | from unet_c import UNet 36 | 37 | # U-Net from research paper 38 | resnet = UNet() 39 | 40 | # getter for the tf.keras model 41 | model = unet.model 42 | ``` 43 | -------------------------------------------------------------------------------- /zoo/vgg/README.md: -------------------------------------------------------------------------------- 1 | 2 | # VGG 3 | 4 | vgg.py - academic (idiomatic) 5 | vgg_c.py - production (composable) 6 | 7 | [Paper](https://arxiv.org/pdf/1409.1556.pdf) 8 | 9 | ## Macro-Architecture 10 | 11 | 12 | 13 | ## Micro-Architecture 14 | 15 | ### Convolutional Group (Block) 16 | 17 | 18 | 19 | ### Stem Group 20 | 21 | 22 | 23 | ### Classifier Group 24 | 25 | 26 | 27 | ## Composable 28 | 29 | *Example Instantiate a stock VGG model* 30 | 31 | ```python 32 | from vgg_c import VGG 33 | 34 | # VGG16 from research paper 35 | vgg = VGG(16) 36 | 37 | # VGG16 custom input shape/classes 38 | vgg = VGG(16, input_shape=(128, 128, 3), n_classes=50) 39 | 40 | # getter for the tf.keras model 41 | model = vgg.model 42 | ``` 43 | 44 | *Example: Compose and train a mini-VGG model* 45 | 46 | ```python 47 | ''' Example for constructing/training a VGG model on CIFAR-10 48 | ''' 49 | # Example of constructing a mini-VGG 50 | groups = [ { 'n_layers': 1, 'n_filters': 64 }, 51 | { 'n_layers': 2, 'n_filters': 128 }, 52 | { 'n_layers': 2, 'n_filters': 256 } ] 53 | vgg = VGG(groups, input_shape=(32, 32, 3), n_classes=10) 54 | vgg.model.summary() 55 | 56 | # train on CIFAR-10 57 | vgg.cifar10() 58 | ``` 59 | 60 | -------------------------------------------------------------------------------- /zoo/vgg/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/vgg/classifier.jpg -------------------------------------------------------------------------------- /zoo/vgg/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/vgg/macro.jpg -------------------------------------------------------------------------------- /zoo/vgg/micro-conv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/vgg/micro-conv.jpg -------------------------------------------------------------------------------- /zoo/vgg/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/vgg/stem.jpg -------------------------------------------------------------------------------- /zoo/vgg/vgg.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # VGG (16 and 19) (2014) 16 | # Paper: https://arxiv.org/pdf/1409.1556.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Input, Model 20 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense 21 | 22 | def stem(inputs): 23 | """ Construct the Stem Convolutional Group 24 | inputs : the input vector 25 | """ 26 | x = Conv2D(64, (3, 3), strides=(1, 1), padding="same", activation="relu")(inputs) 27 | return x 28 | 29 | def learner(x, blocks): 30 | """ Construct the (Feature) Learner 31 | x : input to the learner 32 | blocks : list of groups: filter size and number of conv layers 33 | """ 34 | # The convolutional groups 35 | for n_layers, n_filters in blocks: 36 | x = group(x, n_layers, n_filters) 37 | return x 38 | 39 | def group(x, n_layers, n_filters): 40 | """ Construct a Convolutional Group 41 | x : input to the group 42 | n_layers : number of convolutional layers 43 | n_filters: number of filters 44 | """ 45 | # Block of convolutional layers 46 | for n in range(n_layers): 47 | x = Conv2D(n_filters, (3, 3), strides=(1, 1), padding="same", activation="relu")(x) 48 | 49 | # Max pooling at the end of the block 50 | x = MaxPooling2D(2, strides=(2, 2))(x) 51 | return x 52 | 53 | def classifier(x, n_classes): 54 | """ Construct the Classifier 55 | x : input to the classifier 56 | n_classes : number of output classes 57 | """ 58 | # Flatten the feature maps 59 | x = Flatten()(x) 60 | 61 | # Two fully connected dense layers 62 | x = Dense(4096, activation='relu')(x) 63 | x = Dense(4096, activation='relu')(x) 64 | 65 | # Output layer for classification 66 | x = Dense(n_classes, activation='softmax')(x) 67 | return x 68 | 69 | # Meta-parameter: list of groups: number of layers and filter size 70 | groups = { 16 : [ (1, 64), (2, 128), (3, 256), (3, 512), (3, 512) ], # VGG16 71 | 19 : [ (1, 64), (2, 128), (4, 256), (4, 512), (4, 512) ] } # VGG19 72 | 73 | # The input vector 74 | inputs = Input( (224, 224, 3) ) 75 | 76 | # The stem group 77 | x = stem(inputs) 78 | 79 | # The learner 80 | x = learner(x, groups[16]) 81 | 82 | # The classifier 83 | outputs = classifier(x, 1000) 84 | 85 | # Instantiate the Model 86 | model = Model(inputs, outputs) 87 | -------------------------------------------------------------------------------- /zoo/wrn/README.md: -------------------------------------------------------------------------------- 1 | 2 | # WRN (Wide Residual Network) 3 | 4 | [wrn_c.py](wrn_c.p) - production (composable) 5 | 6 | [Paper](https://arxiv.org/pdf/1605.07146.pdf) 7 | 8 | ## Macro-Architecture 9 | 10 | 11 | 12 | ## Micro-Architecture 13 | 14 | 15 | 16 | ### Stem Group 17 | 18 | 19 | 20 | ### Wide Residual Block with Identity Link 21 | 22 | 23 | 24 | ### Wide Residual Block with Projection Shortcut 25 | 26 | 27 | 28 | ### Classifier Group 29 | 30 | 31 | 32 | ## Composable 33 | 34 | *Example: Instantiate a stock WRN model* 35 | 36 | ```python 37 | from wrn_c import WRN 38 | 39 | wrn = WRN(depth=28, k=10) 40 | ``` 41 | 42 | *Example: Compose and Train a WRN model* 43 | 44 | ```python 45 | example(): 46 | ''' Example for constructing/training a WRN model on CIFAR-10 47 | ''' 48 | # Example of constructing a mini WRN 49 | wrn = WRN(depth=14, k=2, input_shape=(32, 32, 3), n_classes=10) 50 | wrn.model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc']) 51 | wrn.model.summary() 52 | wrn.cifar10() 53 | ``` 54 | -------------------------------------------------------------------------------- /zoo/wrn/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/classifier.jpg -------------------------------------------------------------------------------- /zoo/wrn/identity-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/identity-block.jpg -------------------------------------------------------------------------------- /zoo/wrn/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/macro.jpg -------------------------------------------------------------------------------- /zoo/wrn/micro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/micro.jpg -------------------------------------------------------------------------------- /zoo/wrn/projection-block.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/projection-block.jpg -------------------------------------------------------------------------------- /zoo/wrn/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/wrn/stem.jpg -------------------------------------------------------------------------------- /zoo/xception/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Xception 3 | 4 | [xception.py](xception.py) - academic (idiomatic)
5 | [xception_c.py](xception_c.py) - production (composable)
6 | 7 | [Paper](https://arxiv.org/pdf/1610.02357.pdf) 8 | 9 | ## Macro-Architecture 10 | 11 | 12 | 13 | ## Micro-Architecture - Entry Flow 14 | 15 | 16 | 17 | ### Entry Flow Stem Group 18 | 19 | 20 | 21 | ### Entry Flow Block 22 | 23 | 24 | 25 | ## Micro-Architecture - Middle Flow 26 | 27 | 28 | 29 | ### Middle Flow Block 30 | 31 | 32 | 33 | ## Micro-Architecture - Exit Flow 34 | 35 | 36 | 37 | ### Exit Flow Residual Block 38 | 39 | 40 | 41 | ### Exit Flow Convolutional Block 42 | 43 | 44 | 45 | ### Exit Flow Classifier 46 | 47 | 48 | 49 | ## Composable 50 | 51 | *Example: Instantiate a stock Xception model* 52 | 53 | ```python 54 | from xception_c import Xception 55 | 56 | # Xception from research paper 57 | xception = Xception() 58 | 59 | # Xception custom input shape/classes 60 | xception = Xception(input_shape=(128, 128, 3), n_classes=50) 61 | 62 | # getter for the tf.keras model 63 | model = xception.model 64 | ``` 65 | 66 | *Example: Compose and Train an Xception model* 67 | 68 | ```python 69 | ''' Example for constructing/training a Xception model on CIFAR-10 70 | ''' 71 | # Example of constructing a mini-Xception 72 | entry = [{ 'n_filters' : 128 }, { 'n_filters' : 728 }] 73 | middle = [{ 'n_filters' : 728 }, { 'n_filters' : 728 }, { 'n_filters' : 728 }] 74 | 75 | xception = Xception(entry=entry, middle=middle, input_shape=(32, 32, 3), n_classes=10) 76 | xception.model.summary() 77 | xception.cifar10() 78 | ``` 79 | -------------------------------------------------------------------------------- /zoo/xception/block-exit-conv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/block-exit-conv.jpg -------------------------------------------------------------------------------- /zoo/xception/block-exit-residual.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/block-exit-residual.jpg -------------------------------------------------------------------------------- /zoo/xception/block-middle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/block-middle.jpg -------------------------------------------------------------------------------- /zoo/xception/block-projection.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/block-projection.jpg -------------------------------------------------------------------------------- /zoo/xception/classifier.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/classifier.jpg -------------------------------------------------------------------------------- /zoo/xception/macro.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/macro.jpg -------------------------------------------------------------------------------- /zoo/xception/micro-entry.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/micro-entry.jpg -------------------------------------------------------------------------------- /zoo/xception/micro-exit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/micro-exit.jpg -------------------------------------------------------------------------------- /zoo/xception/micro-middle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/micro-middle.jpg -------------------------------------------------------------------------------- /zoo/xception/stem.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/keras-idiomatic-programmer/760e4a86892c5684907dc66aa96ebc3b0897f608/zoo/xception/stem.jpg -------------------------------------------------------------------------------- /zoo/zfnet/zfnet.py: -------------------------------------------------------------------------------- 1 | # Copyright 2020 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # ZFNet (2013) 16 | # Paper: https://arxiv.org/pdf/1311.2901v3.pdf 17 | 18 | import tensorflow as tf 19 | from tensorflow.keras import Model, Input 20 | from tensorflow.keras.layers import Conv2D, ReLU, MaxPooling2D, Dense, Flatten 21 | 22 | def stem(inputs): 23 | """ Construct the Stem Convolutional Group 24 | inputs : the input vector 25 | """ 26 | # First Convolutional layer which uses an extremely large (coarse) filter 27 | x = Conv2D(96, (7, 7), strides=(2, 2), padding='same')(inputs) 28 | x = ReLU()(x) 29 | 30 | # Second Convolutional layer 31 | x = Conv2D(256, (5, 5), strides=(2, 2), padding='same')(x) 32 | x = ReLU()(x) 33 | 34 | # Pooled feature maps will be reduced by 75% 35 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 36 | return x 37 | 38 | def learner(x): 39 | """ Construct the Learner 40 | x : input to the learner 41 | """ 42 | # Third Convolutional layer 43 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same')(x) 44 | x = ReLU()(x) 45 | 46 | # Pooled feature maps will be reduced by 75% 47 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 48 | 49 | # Fourth Convolutional layer 50 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same')(x) 51 | x = ReLU()(x) 52 | 53 | # Ffth Convolutional layer 54 | x = Conv2D(256, (3, 3), strides=(1, 1), padding='same')(x) 55 | x = ReLU()(x) 56 | 57 | # Pooled feature maps will be reduced by 75% 58 | x = MaxPooling2D((3, 3), strides=(2, 2))(x) 59 | 60 | return x 61 | 62 | def classifier(x, n_classes): 63 | """ Construct the Classifier Group 64 | x : input to the classifier 65 | n_classes : number of output classes 66 | """ 67 | # Flatten into 1D vector 68 | x = Flatten()(x) 69 | 70 | # Two dense layers of 4096 71 | x = Dense(4096, activation='relu')(x) 72 | x = Dense(4096, activation='relu')(x) 73 | 74 | # Final Dense Outputting Layer for the outputs 75 | outputs = Dense(n_classes, activation='softmax')(x) 76 | return outputs 77 | 78 | 79 | # The input tensor 80 | inputs = Input(shape=(224, 224, 3)) 81 | 82 | # The stem convolutional group 83 | x = stem(inputs) 84 | 85 | # The learner 86 | x = learner(x) 87 | 88 | # The classifier for 1000 classes 89 | outputs = classifier(x, 1000) 90 | 91 | # Instantiate the Model 92 | model = Model(inputs, outputs) 93 | model.summary() 94 | --------------------------------------------------------------------------------