├── 9781484258866.jpg ├── Contributing.md ├── LICENSE.txt ├── README.md ├── chapter10 ├── .DS_Store ├── LICENSE ├── README.md ├── conf │ └── cluster.conf ├── distributed_training.py ├── distributed_training_ps.py ├── horovod_tensorflow_mnist.py ├── mirrored_strategy.py └── training_launcher.py ├── chapter2 ├── Listing2_4_b.py ├── Listing_2_1.py ├── Listing_2_2.py ├── Listing_2_3.py ├── Listing_2_4_a.py ├── Listing_2_5.py ├── images │ ├── circle.jpg │ ├── marsrover.png │ └── rectangle.jpg └── rectangle.jpg ├── chapter3 ├── Listing_3_1.py ├── Listing_3_10.py ├── Listing_3_11.py ├── Listing_3_12.py ├── Listing_3_13.py ├── Listing_3_14.py ├── Listing_3_15.py ├── Listing_3_16.py ├── Listing_3_17.py ├── Listing_3_18.py ├── Listing_3_19.py ├── Listing_3_2.py ├── Listing_3_20.py ├── Listing_3_21.py ├── Listing_3_22.py ├── Listing_3_3.py ├── Listing_3_4.py ├── Listing_3_5.py ├── Listing_3_6.py ├── Listing_3_7.py ├── Listing_3_8.py ├── Listing_3_9.py ├── imageoverlay.py ├── images │ ├── boat.jpg │ ├── cat1.png │ ├── cat2.png │ ├── coffeestain.jpg │ ├── mri.jpg │ ├── nature.jpg │ ├── park.jpg │ ├── receipt.jpg │ ├── salesreceipt.jpg │ ├── salt-pepper.jpg │ ├── scanned_doc.png │ ├── screen1.png │ ├── screen2.png │ ├── soccer-in-green.jpg │ ├── sudoku.jpg │ ├── zebra.png │ └── zebrasmall.png └── subtraction.py ├── chapter4 ├── Listing_4_1.py ├── Listing_4_2.py ├── Listing_4_3.py ├── Listing_4_4.py ├── Listing_4_5.py ├── Listing_4_6.py ├── Listing_4_7.py └── images │ ├── nature.jpg │ └── obama.jpg ├── chapter5 ├── .DS_Store ├── Listing_5_1.py ├── Listing_5_2.py ├── Listing_5_5.py ├── Listing_5_6.py ├── Listing_5_7.py ├── Listing_5_8.py ├── __pycache__ │ ├── dirmanager.cpython-36.pyc │ └── label_data_creater.cpython-36.pyc ├── dirmanager.py ├── evaluation.py ├── label_data_creater.py ├── models │ ├── .DS_Store │ └── pneumiacnn │ │ ├── .DS_Store │ │ ├── saved_model.pb │ │ └── variables │ │ ├── variables.data-00000-of-00001 │ │ └── variables.index └── resources │ ├── .DS_Store │ └── retina_labels.txt ├── chapter6 ├── Listing_6_1 ├── Listing_6_15.py ├── Listing_6_16.sh ├── Listing_6_17.sh ├── Listing_6_18.sh ├── Listing_6_19.sh ├── Listing_6_2 ├── Listing_6_20.sh ├── Listing_6_21.sh ├── Listing_6_22.py ├── Listing_6_23.sh ├── Listing_6_24.py ├── Listing_6_3 ├── Listing_6_4 ├── Listing_6_5 ├── Listing_6_6 ├── Listing_6_7 └── Listing_6_8 ├── chapter7 ├── model │ ├── .DS_Store │ └── mscoco_label_map.pbtxt └── video_tracking │ ├── __pycache__ │ ├── object_tracker.cpython-36.pyc │ ├── tracker.cpython-36.pyc │ └── videoasync.cpython-36.pyc │ ├── object_tracker.py │ ├── templates │ └── index.html │ ├── tracker.py │ ├── video_server.py │ └── videoasync.py ├── chapter8 ├── .DS_Store ├── Listing_8_1.sh ├── Listing_8_10.sh ├── Listing_8_2.py ├── Listing_8_3.sh ├── Listing_8_4.sh ├── Listing_8_5.sh ├── Listing_8_6.sh ├── Listing_8_7.sh ├── Listing_8_8.sh ├── Listing_8_9.sh ├── README.txt ├── converter.py └── facenet.py ├── chapter9 ├── Listing_9_1.sh ├── Listing_9_3.sh ├── Listing_9_4.sh ├── Listing_9_6.sh ├── Listing_9_7.sh ├── Listing_9_8.sh ├── generic_xml_to_tf_record.py └── steel_label_map.pbtxt └── errata.md /9781484258866.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/9781484258866.jpg -------------------------------------------------------------------------------- /Contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Freeware License, some rights reserved 2 | 3 | Copyright (c) 2020 Shamshad Ansari 4 | 5 | Permission is hereby granted, free of charge, to anyone obtaining a copy 6 | of this software and associated documentation files (the "Software"), 7 | to work with the Software within the limits of freeware distribution and fair use. 8 | This includes the rights to use, copy, and modify the Software for personal use. 9 | Users are also allowed and encouraged to submit corrections and modifications 10 | to the Software for the benefit of other users. 11 | 12 | It is not allowed to reuse, modify, or redistribute the Software for 13 | commercial use in any way, or for a user’s educational materials such as books 14 | or blog articles without prior permission from the copyright holder. 15 | 16 | The above copyright notice and this permission notice need to be included 17 | in all copies or substantial portions of the software. 18 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | SOFTWARE. 26 | 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Building Computer Vision Applications Using Artificial Neural Networks*](https://www.apress.com/9781484258866) by Shamshad Ansari (Apress, 2020). 4 | 5 | [comment]: #cover 6 | ![Cover image](9781484258866.jpg) 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Clone Instruction 15 | Clone repo with all history: 16 | git clone https://github.com/Apress/building-computer-vision-apps-artificial-neural-networks.git 17 | 18 | This will download all prior commit history. The overall size will be of the order of 1.4GB. If this causes any error, clone only the latest commit using the command: 19 | 20 | git clone https://github.com/Apress/building-computer-vision-apps-artificial-neural-networks.git --depth 1 21 | 22 | 23 | ## Contributions 24 | 25 | See the file Contributing.md for more information on how you can contribute to this repository. 26 | 27 | # Book Value Proposition: 28 | - Contains real examples that you can implement and modify to build useful computer vision systems 29 | - Gives line-by-line explanations of computer vision working code examples 30 | - Explains training neural networks involving large numbers of images on cloud infrastructure, such as Amazon AWS, Google Cloud Platform, and Microsoft Azure 31 | 32 | # About the Book: 33 | Apply computer vision and machine learning concepts in developing business and industrial applications ​using a practical, step-by-step approach. 34 | 35 | The book comprises four main sections starting with setting up your programming environment and configuring your computer with all the prerequisites to run the code examples. Section 1 covers the basics of image and video processing with code examples of how to manipulate and extract useful information from the images. You will mainly use OpenCV with Python to work with examples in this section. 36 | 37 | Section 2 describes machine learning and neural network concepts as applied to computer vision. You will learn different algorithms of the neural network, such as convolutional neural network (CNN), region-based convolutional neural network (R-CNN), and YOLO. In this section, you will also learn how to train, tune, and manage neural networks for computer vision. Section 3 provides step-by-step examples of developing business and industrial applications, such as facial recognition in video surveillance and surface defect detection in manufacturing. 38 | 39 | The final section is about training neural networks involving a large number of images on cloud infrastructure, such as Amazon AWS, Google Cloud Platform, and Microsoft Azure. It walks you through the process of training distributed neural networks for computer vision on GPU-based cloud infrastructure. By the time you finish reading Building Computer Vision Applications Using Artificial Neural Networks and working through the code examples, you will have developed some real-world use cases of computer vision with deep learning. 40 | 41 | ## What You Will Learn 42 | 43 | · Employ image processing, manipulation, and feature extraction techniques 44 | 45 | · Work with various deep learning algorithms for computer vision 46 | · Train, manage, and tune hyperparameters of CNNs and object detection models, such as R-CNN, SSD, and YOLO 47 | 48 | · Build neural network models using Keras and TensorFlow 49 | 50 | · Discover best practices when implementing computer vision applications in business and industry 51 | 52 | · Train distributed models on GPU-based cloud infrastructure 53 | ## Who This Book Is For 54 | 55 | Data scientists, analysts, and machine learning and software engineering professionals with Python programming knowledge. 56 | 57 | ## About the Author: 58 | Shamshad (Sam) Ansari is a distinguished data scientist, inventor and author. He has several technology publishing in his name. He has co-authored 4 US Patents related to AI in healthcare. 59 | 60 | Sam is the founder, president and CEO of Accure, an AI automation company. He enjoys working with software engineers, data scientists, devops, and business analysts, and solving real-world scientific and business problems. 61 | 62 | He has worked with 4 tech startups in his 20+ years of career. Prior to starting Accure, he worked for Apixio, a healthcare AI startup, IBM, and Orbit Solutions. 63 | 64 | He has technical expertise in the area of computer vision, machine learning, AI, cognitive science, NLP, and big data. He architected, designed, and developed an AI automation platform, called Momentum, that allows to build AI solutions without writing code. 65 | 66 | He is passionate about teaching and mentoring. He has spent more than 10,000 hours in teaching and mentoring students from across the world. 67 | 68 | Sam holds a Bachelor's degree in engineering from BIT Sindri and Master's degree in computer science from Indian Institute of Information Technology and Management (IIITM) Kerala. 69 | -------------------------------------------------------------------------------- /chapter10/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter10/.DS_Store -------------------------------------------------------------------------------- /chapter10/LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /chapter10/README.md: -------------------------------------------------------------------------------- 1 | # dist-tf-modeling 2 | TensorFlow distributed model training 3 | -------------------------------------------------------------------------------- /chapter10/conf/cluster.conf: -------------------------------------------------------------------------------- 1 | [cluster] 2 | chief=localhost:8900 3 | worker=localhost:8901,localhost:8902,localhost:8903 4 | ps=localhost:8904 -------------------------------------------------------------------------------- /chapter10/distributed_training.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import tensorflow as tf 4 | # import keras 5 | 6 | strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() 7 | 8 | parser = argparse.ArgumentParser() 9 | parser.add_argument( 10 | "--input_path", 11 | type=str, 12 | default="", 13 | help="Directory path to the input file. Could you be clous storage" 14 | ) 15 | parser.add_argument( 16 | "--output_path", 17 | type=str, 18 | default="", 19 | help="Directory path to the input file. Could you be clous storage" 20 | ) 21 | 22 | FLAGS, unparsed = parser.parse_known_args() 23 | 24 | 25 | tf.print("TF version::", tf.version.VERSION) 26 | 27 | callback = tf.keras.callbacks.ModelCheckpoint(filepath=FLAGS.output_path) 28 | 29 | 30 | 31 | # Load MNIST data using built-in datasets download function 32 | mnist = tf.keras.datasets.mnist 33 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 34 | 35 | #Noramalize the pixel values by deviding each pixel by 255 36 | x_train, x_test = x_train / 255.0, x_test / 255.0 37 | 38 | BUFFER_SIZE = len(x_train) 39 | 40 | BATCH_SIZE_PER_REPLICA = 16 41 | GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync 42 | print("---------- global batch size------------", GLOBAL_BATCH_SIZE) 43 | EPOCHS = 10 44 | 45 | train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(BUFFER_SIZE).batch(GLOBAL_BATCH_SIZE) 46 | test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(GLOBAL_BATCH_SIZE) 47 | 48 | train_dist_dataset = strategy.experimental_distribute_dataset(train_dataset) 49 | test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset) 50 | train_dist_dataset = strategy.experimental_distribute_dataset(train_dataset) 51 | test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset) 52 | 53 | with strategy.scope(): 54 | 55 | # Build the ANN with 4-layers 56 | model = tf.keras.models.Sequential([ 57 | tf.keras.layers.Flatten(input_shape=(28, 28)), 58 | tf.keras.layers.Dense(128, activation='relu'), 59 | tf.keras.layers.Dense(60, activation='relu'), 60 | tf.keras.layers.Dense(10, activation='softmax')]) 61 | 62 | # Compile the model and set optimizer,loss function and metrics 63 | model.compile(optimizer='adam', 64 | loss='sparse_categorical_crossentropy', 65 | metrics=['accuracy']) 66 | 67 | # Finally, train or fot the model 68 | history = model.fit(train_dataset, epochs=5, steps_per_epoch=100, callbacks=[callback]) 69 | model.save("model1.h5") 70 | # print(history) 71 | # # Evaluate the result using the test set.\ 72 | # evalResult = model.evaluate(x_test, y_test, verbose=1) 73 | # print("Evaluation", evalResult) 74 | # predicted = model.predict(x_test) 75 | # print("Predicted", predicted) 76 | -------------------------------------------------------------------------------- /chapter10/distributed_training_ps.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import tensorflow as tf 3 | from tensorflow_core.python.lib.io import file_io 4 | 5 | #Disable eager execution 6 | tf.compat.v1.disable_eager_execution() 7 | 8 | #Instantiate the distribution strategy -- ParameterServerStrategy. This needs to be in the beginning of the code. 9 | strategy = tf.distribute.experimental.ParameterServerStrategy() 10 | 11 | #Parse the command line arguments 12 | parser = argparse.ArgumentParser() 13 | parser.add_argument( 14 | "--input_path", 15 | type=str, 16 | default="", 17 | help="Directory path to the input file. Could you be cloud storage" 18 | ) 19 | parser.add_argument( 20 | "--output_path", 21 | type=str, 22 | default="", 23 | help="Directory path to the input file. Could you be cloud storage" 24 | ) 25 | FLAGS, unparsed = parser.parse_known_args() 26 | 27 | # Load MNIST data using built-in datasets' download function 28 | mnist = tf.keras.datasets.mnist 29 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 30 | 31 | #Noramalize the pixel values by deviding each pixel by 255 32 | x_train, x_test = x_train / 255.0, x_test / 255.0 33 | 34 | BUFFER_SIZE = len(x_train) 35 | BATCH_SIZE_PER_REPLICA = 16 36 | GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * 2 37 | EPOCHS = 10 38 | STEPS_PER_EPOCH = int(BUFFER_SIZE/EPOCHS) 39 | 40 | train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).repeat().shuffle(BUFFER_SIZE).batch(GLOBAL_BATCH_SIZE,drop_remainder=True) 41 | test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(GLOBAL_BATCH_SIZE) 42 | 43 | 44 | with strategy.scope(): 45 | # Build the ANN with 4-layers 46 | model = tf.keras.models.Sequential([ 47 | tf.keras.layers.Flatten(input_shape=(28, 28)), 48 | tf.keras.layers.Dense(128, activation='relu'), 49 | tf.keras.layers.Dense(60, activation='relu'), 50 | tf.keras.layers.Dense(10, activation='softmax')]) 51 | 52 | # Compile the model and set optimizer,loss function and metrics 53 | model.compile(optimizer='adam', 54 | loss='sparse_categorical_crossentropy', 55 | metrics=['accuracy']) 56 | 57 | #Save checkpoints to the output location -- most probably on a cloud storage, such as GCS 58 | callback = tf.keras.callbacks.ModelCheckpoint(filepath=FLAGS.output_path) 59 | # Finally, train or fit the model 60 | history = model.fit(train_dataset, epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH, callbacks=[callback], use_multiprocessing=True) 61 | # history = model.fit(train_dataset, epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH, use_multiprocessing=True) 62 | # Save the model to the cloud storage 63 | model.save("model.h5") 64 | with file_io.FileIO('model.h5', mode='r') as input_f: 65 | with file_io.FileIO(FLAGS.output_path+ '/model.h5', mode='wb+') as output_f: 66 | output_f.write(input_f.read()) 67 | 68 | -------------------------------------------------------------------------------- /chapter10/horovod_tensorflow_mnist.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import horovod.tensorflow.keras as hvd 3 | 4 | # Horovod: initialize Horovod. 5 | hvd.init() 6 | 7 | # Horovod: pin GPU to be used to process local rank (one GPU per process) 8 | gpus = tf.config.experimental.list_physical_devices('GPU') 9 | for gpu in gpus: 10 | tf.config.experimental.set_memory_growth(gpu, True) 11 | if gpus: 12 | tf.config.experimental.set_visible_devices(gpus[hvd.local_rank()], 'GPU') 13 | 14 | # Load MNIST data using built-in datasets download function 15 | mnist = tf.keras.datasets.mnist 16 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 17 | 18 | #Noramalize the pixel values by deviding each pixel by 255 19 | x_train, x_test = x_train / 255.0, x_test / 255.0 20 | 21 | BUFFER_SIZE = len(x_train) 22 | BATCH_SIZE_PER_REPLICA = 16 23 | GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * 2 24 | EPOCHS = 100 25 | STEPS_PER_EPOCH = int(BUFFER_SIZE/EPOCHS) 26 | 27 | train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).repeat().shuffle(BUFFER_SIZE).batch(GLOBAL_BATCH_SIZE,drop_remainder=True) 28 | test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(GLOBAL_BATCH_SIZE) 29 | 30 | 31 | mnist_model = tf.keras.Sequential([ 32 | tf.keras.layers.Conv2D(32, [3, 3], activation='relu'), 33 | tf.keras.layers.Conv2D(64, [3, 3], activation='relu'), 34 | tf.keras.layers.MaxPooling2D(pool_size=(2, 2)), 35 | tf.keras.layers.Dropout(0.25), 36 | tf.keras.layers.Flatten(), 37 | tf.keras.layers.Dense(128, activation='relu'), 38 | tf.keras.layers.Dropout(0.5), 39 | tf.keras.layers.Dense(10, activation='softmax') 40 | ]) 41 | 42 | # Horovod: adjust learning rate based on number of GPUs. 43 | opt = tf.optimizers.Adam(0.001 * hvd.size()) 44 | 45 | # Horovod: add Horovod DistributedOptimizer. 46 | opt = hvd.DistributedOptimizer(opt) 47 | 48 | # Horovod: Specify `experimental_run_tf_function=False` to ensure TensorFlow 49 | # uses hvd.DistributedOptimizer() to compute gradients. 50 | mnist_model.compile(loss=tf.losses.SparseCategoricalCrossentropy(), 51 | optimizer=opt, 52 | metrics=['accuracy'], 53 | experimental_run_tf_function=False) 54 | 55 | callbacks = [ 56 | # Horovod: broadcast initial variable states from rank 0 to all other processes. 57 | # This is necessary to ensure consistent initialization of all workers when 58 | # training is started with random weights or restored from a checkpoint. 59 | hvd.callbacks.BroadcastGlobalVariablesCallback(0), 60 | 61 | # Horovod: average metrics among workers at the end of every epoch. 62 | # 63 | # Note: This callback must be in the list before the ReduceLROnPlateau, 64 | # TensorBoard or other metrics-based callbacks. 65 | hvd.callbacks.MetricAverageCallback(), 66 | 67 | # Horovod: using `lr = 1.0 * hvd.size()` from the very beginning leads to worse final 68 | # accuracy. Scale the learning rate `lr = 1.0` ---> `lr = 1.0 * hvd.size()` during 69 | # the first three epochs. See https://arxiv.org/abs/1706.02677 for details. 70 | hvd.callbacks.LearningRateWarmupCallback(warmup_epochs=3, verbose=1), 71 | ] 72 | 73 | # Horovod: save checkpoints only on worker 0 to prevent other workers from corrupting them. 74 | if hvd.rank() == 0: 75 | callbacks.append(tf.keras.callbacks.ModelCheckpoint('./checkpoint-{epoch}.h5')) 76 | 77 | # Horovod: write logs on worker 0. 78 | verbose = 1 if hvd.rank() == 0 else 0 79 | 80 | # Train the model. 81 | # Horovod: adjust number of steps based on number of GPUs. 82 | mnist_model.fit(train_dataset, steps_per_epoch=500 // hvd.size(), callbacks=callbacks, epochs=24, verbose=verbose) -------------------------------------------------------------------------------- /chapter10/mirrored_strategy.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | # Load MNIST data using built-in datasets download function 4 | mnist = tf.keras.datasets.mnist 5 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 6 | 7 | #Noramalize the pixel values by deviding each pixel by 255 8 | x_train, x_test = x_train / 255.0, x_test / 255.0 9 | 10 | BUFFER_SIZE = len(x_train) 11 | BATCH_SIZE_PER_REPLICA = 16 12 | GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * 2 13 | EPOCHS = 100 14 | STEPS_PER_EPOCH = int(BUFFER_SIZE/EPOCHS) 15 | 16 | train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)).repeat().shuffle(BUFFER_SIZE).batch(GLOBAL_BATCH_SIZE,drop_remainder=True) 17 | test_dataset = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(GLOBAL_BATCH_SIZE) 18 | 19 | strategy = tf.distribute.MirroredStrategy() 20 | with strategy.scope(): 21 | # Build the ANN with 4-layers 22 | model = tf.keras.models.Sequential([ 23 | tf.keras.layers.Flatten(input_shape=(28, 28)), 24 | tf.keras.layers.Dense(128, activation='relu'), 25 | tf.keras.layers.Dense(60, activation='relu'), 26 | tf.keras.layers.Dense(10, activation='softmax') 27 | ]) 28 | 29 | # Compile the model and set optimizer,loss function and metrics 30 | model.compile(optimizer='adam', 31 | loss='sparse_categorical_crossentropy', 32 | metrics=['accuracy']) 33 | 34 | # Finally, train or fot the model 35 | history = model.fit(test_dataset, epochs=EPOCHS) 36 | 37 | # Evaluate the result using the test set.\ 38 | evalResult = model.evaluate(x_test, y_test, verbose=1) 39 | print("Evaluation", evalResult) 40 | predicted = model.predict(x_test) 41 | print("Predicted", predicted) 42 | -------------------------------------------------------------------------------- /chapter10/training_launcher.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import sys 3 | import pip 4 | import subprocess 5 | import configparser 6 | import json 7 | import os 8 | FLAGS = None 9 | 10 | tf_config = json.loads(json.dumps({ 11 | "cluster": { 12 | "worker": [], 13 | "ps": [] 14 | }, 15 | "task": {"type": "worker", "index": 0} 16 | })) 17 | 18 | def get_config(conf_file_path): 19 | config = configparser.ConfigParser() 20 | config.read(conf_file_path) 21 | return config 22 | 23 | def get_cluster_components(config): 24 | chief = None 25 | if "chief" in config["cluster"].keys(): 26 | chief = config["cluster"]["chief"] 27 | chief = [x.strip() for x in chief.split(',')] 28 | workers = None 29 | if "worker" in config["cluster"].keys(): 30 | workers= config["cluster"]["worker"] 31 | workers = [x.strip() for x in workers.split(',')] 32 | 33 | ps = None 34 | if "ps" in config["cluster"].keys(): 35 | ps= config["cluster"]["ps"] 36 | ps = [x.strip() for x in ps.split(',')] 37 | 38 | return chief,workers,ps 39 | 40 | 41 | def init_tf_config(master, workers, ps): 42 | if master !=None: 43 | tf_config["cluster"]["chief"] = master 44 | if workers != None: 45 | tf_config["cluster"]["worker"] = workers 46 | if ps !=None: 47 | tf_config["cluster"]["ps"] = ps 48 | 49 | 50 | def get_tf_config(role, index): 51 | tf_config["task"]["type"] =role 52 | tf_config["task"]["index"] = index 53 | 54 | def get_launch_command(py_script, ssh_host): 55 | env_variable = str(json.dumps(tf_config)).replace("\"", "\\\"") 56 | command1 = "python "+py_script+" --input_path "+FLAGS.input_path+" --output_path "+FLAGS.output_path 57 | command2 = "\"export TF_CONFIG='" + env_variable + "';" + command1 + "\"" 58 | command = command2 + " & " 59 | if ssh_host != "localhost": 60 | command = "ssh "+ssh_host +" "+ command2+ " &" 61 | return command 62 | 63 | 64 | def train(): 65 | config = get_config(FLAGS.cluster_conf_path) 66 | master,workers,ps = get_cluster_components(config) 67 | init_tf_config(master,workers,ps) 68 | counter = 0 69 | if master is not None: 70 | for host in master: 71 | parts = host.split(":") 72 | h = parts[0] 73 | p = parts[1] 74 | get_tf_config("chief", counter) 75 | cmd = get_launch_command(FLAGS.python_script_path, h) 76 | print(cmd) 77 | os.system(cmd) 78 | counter = counter+1 79 | counter = 0 80 | if workers is not None: 81 | for host in workers: 82 | parts = host.split(":") 83 | h = parts[0] 84 | p = parts[1] 85 | get_tf_config("worker", counter) 86 | cmd = get_launch_command(FLAGS.python_script_path, h) 87 | print(cmd) 88 | os.system(cmd) 89 | counter = counter+1 90 | counter = 0 91 | if ps is not None: 92 | for host in ps: 93 | parts = host.split(":") 94 | h = parts[0] 95 | p = parts[1] 96 | get_tf_config("ps", counter) 97 | cmd = get_launch_command(FLAGS.python_script_path, h) 98 | print(cmd) 99 | os.system(cmd) 100 | counter = counter + 1 101 | 102 | 103 | if __name__ == "__main__": 104 | parser = argparse.ArgumentParser() 105 | parser.add_argument( 106 | "--python_script_path", 107 | type=str, 108 | default="", 109 | help="Path of python file that has the model training code" 110 | ) 111 | 112 | # Flags for defining the tf.train.Server 113 | parser.add_argument( 114 | "--input_path", 115 | type=str, 116 | default="", 117 | help="Directory path to the input file. Could you be clous storage" 118 | ) 119 | parser.add_argument( 120 | "--output_path", 121 | type=str, 122 | default="", 123 | help="Directory path to the input file. Could you be clous storage" 124 | ) 125 | parser.add_argument( 126 | "--cluster_conf_path", 127 | type=str, 128 | default="cluster.conf", 129 | help="Path to conf file containing cluster host list" 130 | ) 131 | FLAGS, unparsed = parser.parse_known_args() 132 | train() 133 | print("Model training started.") 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /chapter2/Listing2_4_b.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # create a new canvas 6 | canvas = np.zeros((200, 200, 3), dtype = "uint8") 7 | start = (10,10) 8 | end = (100,100) 9 | color = (0,0,255) 10 | thickness = 5 11 | cv2.rectangle(canvas, start, end, color, thickness) 12 | cv2.imwrite("rectangle.jpg", canvas) 13 | cv2.imshow("Rectangle", canvas) 14 | cv2.waitKey(0) 15 | -------------------------------------------------------------------------------- /chapter2/Listing_2_1.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | 4 | # image path 5 | image_path = "images/marsrover.png" 6 | # Read or load image from its path 7 | image = cv2.imread(image_path) 8 | # image is a NumPy array 9 | print("Dimension of the image: ", image.ndim) 10 | print("Image height: ", format(image.shape[0])) 11 | print("Image width: ", format(image.shape[1])) 12 | print("Image channels: ", format(image.shape[2])) 13 | print("Size of the image array: ", image.size) 14 | # Display the image and wait until a key is pressed 15 | cv2.imshow("My Image", image) 16 | cv2.waitKey(0) 17 | -------------------------------------------------------------------------------- /chapter2/Listing_2_2.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | 4 | # image path 5 | image_path = "images/marsrover.png" 6 | # Read or load image from its path 7 | image = cv2.imread(image_path) 8 | 9 | # Access pixel at (0,0) location 10 | (b, g, r) = image[0, 0] 11 | print("Blue, Green and Red values at (0,0): ", format((b, g, r))) 12 | 13 | # Manipulate pixels and show modified image 14 | image[0:100, 0:100] = (255, 255, 0) 15 | cv2.imshow("Modified Image", image) 16 | cv2.waitKey(0) 17 | -------------------------------------------------------------------------------- /chapter2/Listing_2_3.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | 4 | # image path 5 | image_path = "images/marsrover.png" 6 | # Read or load image from its path 7 | image = cv2.imread(image_path) 8 | 9 | # set start and end coordinates 10 | start = (0, 0) 11 | end = (image.shape[1], image.shape[0]) 12 | # set the color in BGR 13 | color = (255,0,0) 14 | # set thickness in pixel 15 | thickness = 4 16 | cv2.line(image, start, end, color, thickness) 17 | 18 | #display the modified image 19 | cv2.imshow("Modified Image", image) 20 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter2/Listing_2_4_a.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | 4 | # image path 5 | image_path = "images/marsrover.png" 6 | # Read or load image from its path 7 | image = cv2.imread(image_path) 8 | # set the start and end coordinates 9 | # of the top-left and bottom-right corners of the rectangle 10 | start = (100,70) 11 | end = (350,380) 12 | # Set the color and thickness of the outline 13 | color = (0,255,0) 14 | thickness = 5 15 | # Draw the rectangle 16 | cv2.rectangle(image, start, end, color, thickness) 17 | # Save the modified image with the rectangle drawn to it. 18 | cv2.imwrite("rectangle.jpg", image) 19 | # Display the modified image 20 | cv2.imshow("Rectangle", image) 21 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter2/Listing_2_5.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # create a new canvas 6 | canvas = np.zeros((200, 200, 3), dtype = "uint8") 7 | center = (100,100) 8 | radius = 50 9 | color = (0,0,255) 10 | thickness = 5 11 | cv2.circle(canvas, center, radius, color, thickness) 12 | cv2.imwrite("circle.jpg", canvas) 13 | cv2.imshow("My Circle", canvas) 14 | cv2.waitKey(0) 15 | -------------------------------------------------------------------------------- /chapter2/images/circle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter2/images/circle.jpg -------------------------------------------------------------------------------- /chapter2/images/marsrover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter2/images/marsrover.png -------------------------------------------------------------------------------- /chapter2/images/rectangle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter2/images/rectangle.jpg -------------------------------------------------------------------------------- /chapter2/rectangle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter2/rectangle.jpg -------------------------------------------------------------------------------- /chapter3/Listing_3_1.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # Load image 6 | imagePath = "images/zebra.png" 7 | image = cv2.imread(imagePath) 8 | 9 | # Get image shape which returns height, width, and channels as a tuple. Calculate the aspect ratio 10 | (h, w) = image.shape[:2] 11 | aspect = w / h 12 | 13 | # lets resize the image to decrease height by half of the original image. 14 | # Remember, pixel values must be integers. 15 | height = int(0.5 * h) 16 | width = int(height * aspect) 17 | 18 | # New image dimension as a tuple 19 | dimension = (height, width) 20 | resizedImage = cv2.resize(image, dimension, interpolation=cv2.INTER_AREA) 21 | cv2.imshow("Resized Image", resizedImage) 22 | 23 | # Resize using x and y factors 24 | resizedWithFactors = cv2.resize(image, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_LANCZOS4) 25 | cv2.imshow("Resized with factors", resizedWithFactors) 26 | cv2.waitKey(0) 27 | -------------------------------------------------------------------------------- /chapter3/Listing_3_10.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load the image 5 | natureImage = cv2.imread("images/nature.jpg") 6 | 7 | # Split the image into component colors 8 | (b,g,r) = cv2.split(natureImage) 9 | 10 | # show the blue image 11 | cv2.imshow("Blue Image", b) 12 | 13 | # Show the green image 14 | cv2.imshow("Green image", g) 15 | 16 | # Show the red image 17 | cv2.imshow("Red image", r) 18 | 19 | cv2.waitKey(0) 20 | -------------------------------------------------------------------------------- /chapter3/Listing_3_11.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load the image 5 | natureImage = cv2.imread("images/nature.jpg") 6 | 7 | # Split the image into component colors 8 | (b,g,r) = cv2.split(natureImage) 9 | 10 | # show the blue image 11 | cv2.imshow("Blue Image", b) 12 | 13 | # Show the green image 14 | cv2.imshow("Green image", g) 15 | 16 | # Show the red image 17 | cv2.imshow("Red image", r) 18 | 19 | merged = cv2.merge([b,g,r]) 20 | cv2.imshow("Merged Image", merged) 21 | cv2.waitKey(0) 22 | -------------------------------------------------------------------------------- /chapter3/Listing_3_12.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load the image 5 | park = cv2.imread("images/nature.jpg") 6 | cv2.imshow("Original Park Image", park) 7 | 8 | #Define the kernal 9 | kernal = (3,3) 10 | blurred3x3 = cv2.blur(park,kernal) 11 | cv2.imshow("3x3 Blurred Image", blurred3x3) 12 | 13 | blurred5x5 = cv2.blur(park,(5,5)) 14 | cv2.imshow("5x5 Blurred Image", blurred5x5) 15 | 16 | blurred7x7 = cv2.blur(park, (7,7)) 17 | cv2.imshow("7x7 Blurred Image", blurred7x7) 18 | cv2.waitKey(0) 19 | -------------------------------------------------------------------------------- /chapter3/Listing_3_13.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load the park image 5 | parkImage = cv2.imread("images/park.jpg") 6 | cv2.imshow("Original Image", parkImage) 7 | 8 | # Gaussian blurring with 3x3 kernel height and 0 for standard deviation to calculate from the kernel 9 | GaussianFiltered = cv2.GaussianBlur(parkImage, (5,5), 0) 10 | cv2.imshow("Gaussian Blurred Image", GaussianFiltered) 11 | 12 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_14.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # Load a noisy image 4 | saltpepperImage = cv2.imread("images/salt-pepper.jpg") 5 | cv2.imshow("Original noisy image", saltpepperImage) 6 | 7 | # Median filtering for noise reduction 8 | blurredImage3 = cv2.medianBlur(saltpepperImage, 3) 9 | cv2.imshow("Blurred image 3", blurredImage3) 10 | 11 | # Median filtering for noise reduction 12 | blurredImage5 = cv2.medianBlur(saltpepperImage, 5) 13 | cv2.imshow("Blurred image 5", blurredImage5) 14 | 15 | 16 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_15.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # Load a noisy image 4 | noisyImage = cv2.imread("images/nature.jpg") 5 | cv2.imshow("Original image", noisyImage) 6 | 7 | # Bilateral Filter with 8 | fileteredImag5 = cv2.bilateralFilter(noisyImage, 5, 150,50) 9 | cv2.imshow("Blurred image 5", fileteredImag5) 10 | 11 | # Bilateral blurring with kernal 7 12 | fileteredImag7 = cv2.bilateralFilter(noisyImage, 7, 160,60) 13 | cv2.imshow("Blurred image 7", fileteredImag7) 14 | 15 | cv2.waitKey(0) 16 | -------------------------------------------------------------------------------- /chapter3/Listing_3_16.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/scanned_doc.png") 6 | # convert the image to grayscale 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | cv2.imshow("Original Grayscale Receipt", image) 9 | 10 | # Binarize the image using thresholding 11 | (T, binarizedImage) = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY) 12 | cv2.imshow("Binarized Receipt", binarizedImage) 13 | 14 | # Binarization with inverse thresholding 15 | (Ti, inverseBinarizedImage) = cv2.threshold(image, 60, 255, cv2.THRESH_BINARY_INV) 16 | cv2.imshow("Inverse Binarized Receipt", inverseBinarizedImage) 17 | cv2.waitKey(0) 18 | -------------------------------------------------------------------------------- /chapter3/Listing_3_17.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/boat.jpg") 6 | # convert the image to grayscale 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | 9 | cv2.imshow("Original Grayscale Image", image) 10 | 11 | # Binarization using adaptive thresholding and simple mean 12 | binarized = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, 3) 13 | cv2.imshow("Binarized Image with Simple Mean", binarized) 14 | 15 | # Binarization using adaptive thresholding and Gaussian Mean 16 | binarized = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 3) 17 | cv2.imshow("Binarized Image with Gaussian Mean", binarized) 18 | 19 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_18.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/scanned_doc.png") 6 | # convert the image to grayscale 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | cv2.imshow("Original Grayscale Receipt", image) 9 | 10 | # Binarize the image using thresholding 11 | (T, binarizedImage) = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) 12 | print("Threshold value with Otsu binarization", T) 13 | cv2.imshow("Binarized Receipt", binarizedImage) 14 | 15 | # Binarization with inverse thresholding 16 | (Ti, inverseBinarizedImage) = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 17 | cv2.imshow("Inverse Binarized Receipt", inverseBinarizedImage) 18 | print("Threshold value with Otsu inverse binazarion", Ti) 19 | cv2.waitKey(0) 20 | -------------------------------------------------------------------------------- /chapter3/Listing_3_19.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | # Load an image 4 | image = cv2.imread("images/sudoku.jpg") 5 | cv2.imshow("Original Image", image) 6 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 7 | image = cv2.bilateralFilter(image, 5, 50, 50) 8 | cv2.imshow("Blurred image", image) 9 | 10 | # Sobel gradient detection 11 | sobelx = cv2.Sobel(image,cv2.CV_64F,1,0,ksize=3) 12 | sobelx = np.uint8(np.absolute(sobelx)) 13 | sobely = cv2.Sobel(image,cv2.CV_64F,0,1,ksize=3) 14 | sobely = np.uint8(np.absolute(sobely)) 15 | 16 | cv2.imshow("Sobel X", sobelx) 17 | cv2.imshow("Sobel Y", sobely) 18 | 19 | # Schar gradient detection by passing ksize = -1 to Sobel function 20 | scharx = cv2.Sobel(image,cv2.CV_64F,1,0,ksize=-1) 21 | scharx = np.uint8(np.absolute(scharx)) 22 | schary = cv2.Sobel(image,cv2.CV_64F,0,1,ksize=-1) 23 | schary = np.uint8(np.absolute(schary)) 24 | cv2.imshow("Schar X", scharx) 25 | cv2.imshow("Schar Y", schary) 26 | 27 | cv2.waitKey(0) 28 | -------------------------------------------------------------------------------- /chapter3/Listing_3_2.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | #Load image 6 | imagePath = "images/soccer-in-green.jpg" 7 | image = cv2.imread(imagePath) 8 | 9 | #Define translation matrix 10 | translationMatrix = np.float32([[1,0,50],[0,1,20]]) 11 | 12 | #Move the image 13 | movedImage = cv2.warpAffine(image, translationMatrix, (image.shape[1], image.shape[0])) 14 | 15 | cv2.imshow("Moved image", movedImage) 16 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_20.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/sudoku.jpg") 6 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 7 | 8 | image = cv2.bilateralFilter(image, 5, 50, 50) 9 | cv2.imshow("Blurred image", image) 10 | 11 | # Laplace function for edge detection 12 | laplace = cv2.Laplacian(image,cv2.CV_64F) 13 | laplace = np.uint8(np.absolute(laplace)) 14 | 15 | cv2.imshow("Laplacian Edges", laplace) 16 | 17 | cv2.waitKey(0) 18 | -------------------------------------------------------------------------------- /chapter3/Listing_3_21.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/sudoku.jpg") 6 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 7 | cv2.imshow("Blurred image", image) 8 | 9 | # Canny function for edge detection 10 | canny = cv2.Canny(image, 50, 170) 11 | cv2.imshow("Canny Edges", canny) 12 | 13 | cv2.waitKey(0) 14 | -------------------------------------------------------------------------------- /chapter3/Listing_3_22.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | image = cv2.imread("images/sudoku.jpg") 6 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 7 | cv2.imshow("Blurred image", image) 8 | 9 | # Binarize the image 10 | (T,binarized) = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) 11 | cv2.imshow("Binarized image", binarized) 12 | 13 | # Canny function for edge detection 14 | canny = cv2.Canny(binarized, 0, 255) 15 | cv2.imshow("Canny Edges", canny) 16 | 17 | (contours, hierarchy) = cv2.findContours(canny,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 18 | print("Number of contours determined are ", format(len(contours))) 19 | 20 | copiedImage = image.copy() 21 | cv2.drawContours(copiedImage, contours, -1, (0,255,0), 2) 22 | cv2.imshow("Contours", copiedImage) 23 | cv2.waitKey(0) 24 | -------------------------------------------------------------------------------- /chapter3/Listing_3_3.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # Load image 6 | imagePath = "images/zebrasmall.png" 7 | image = cv2.imread(imagePath) 8 | (h,w) = image.shape[:2] 9 | 10 | #Define translation matrix 11 | center = (h//2, w//2) 12 | angle = -45 13 | scale = 1.0 14 | 15 | rotationMatrix = cv2.getRotationMatrix2D(center, angle, scale) 16 | 17 | # Rotate the image 18 | rotatedImage = cv2.warpAffine(image, rotationMatrix, (image.shape[1], image.shape[0])) 19 | 20 | cv2.imshow("Rotated image", rotatedImage) 21 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_4.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # Load image 6 | imagePath = "images/zebrasmall.png" 7 | image = cv2.imread(imagePath) 8 | 9 | # Flip horizontally 10 | flippedHorizontally = cv2.flip(image, 1) 11 | cv2.imshow("Flipped Horizontally", flippedHorizontally) 12 | cv2.waitKey(-1) 13 | 14 | # Flip vertically 15 | flippedVertically = cv2.flip(image, 0) 16 | cv2.imshow("Flipped Vertically", flippedVertically) 17 | cv2.waitKey(-1) 18 | # Flip horizontally and then vertically 19 | flippedHV = cv2.flip(image, -1) 20 | cv2.imshow("Flipped H and V", flippedHV) 21 | cv2.waitKey(0) 22 | -------------------------------------------------------------------------------- /chapter3/Listing_3_5.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | # Load image 6 | imagePath = "images/zebrasmall.png" 7 | image = cv2.imread(imagePath) 8 | cv2.imshow("Original Image", image) 9 | cv2.waitKey(0) 10 | 11 | # Crop the image to get only the face of the zebra 12 | croppedImage = image[0:150, 0:250] 13 | cv2.imshow("Cropped Image", croppedImage) 14 | cv2.waitKey(0) 15 | -------------------------------------------------------------------------------- /chapter3/Listing_3_6.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | image1Path = "images/zebra.png" 6 | image2Path = "images/nature.jpg" 7 | 8 | image1 = cv2.imread(image1Path) 9 | image2 = cv2.imread(image2Path) 10 | 11 | # resize the two images to make them of the same dimension. This is a must to add two images 12 | resizedImage1 = cv2.resize(image1,(300,300),interpolation=cv2.INTER_AREA) 13 | resizedImage2 = cv2.resize(image2,(300,300),interpolation=cv2.INTER_AREA) 14 | 15 | # This is a simple addition of two images 16 | resultant = cv2.add(resizedImage1, resizedImage2) 17 | 18 | # Display these images to see the difference 19 | cv2.imshow("Resized 1", resizedImage1) 20 | cv2.waitKey(0) 21 | 22 | cv2.imshow("Resized 2", resizedImage2) 23 | cv2.waitKey(0) 24 | 25 | cv2.imshow("Resultant Image", resultant) 26 | cv2.waitKey(0) 27 | 28 | # This is weighted addition of the two images 29 | weightedImage = cv2.addWeighted(resizedImage1,0.7, resizedImage2, 0.3, 0) 30 | cv2.imshow("Weighted Image", weightedImage) 31 | cv2.waitKey(0) 32 | 33 | imageEnhanced = 255*resizedImage1 34 | cv2.imshow("Enhanced Image", imageEnhanced) 35 | cv2.waitKey(0) 36 | 37 | arrayImage = resizedImage1+resizedImage2 38 | cv2.imshow("Array Image", arrayImage) 39 | cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter3/Listing_3_7.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import cv2 3 | import numpy as np 4 | 5 | image1Path = "images/zebra.png" 6 | image2Path = "images/nature.jpg" 7 | image3Path = "images/receipt.jpg" 8 | image4Path = "images/cat1.png" 9 | image5Path = "images/cat2.png" 10 | 11 | image1 = cv2.imread(image1Path) 12 | image2 = cv2.imread(image2Path) 13 | image3 = cv2.imread(image3Path) 14 | image4 = cv2.imread(image4Path) 15 | image5 = cv2.imread(image5Path) 16 | 17 | # resize the two images to make them of the same dimension. This is a must to add two images 18 | resizedImage1 = cv2.resize(image1,(300,300),interpolation=cv2.INTER_AREA) 19 | resizedImage2 = cv2.resize(image2,(300,300),interpolation=cv2.INTER_AREA) 20 | resizedImage3 = cv2.resize(image3,(300,500),interpolation=cv2.INTER_AREA) 21 | resizedImage4 = cv2.resize(image4,(int(500*image4.shape[1]/image4.shape[0]), 500),interpolation=cv2.INTER_AREA) 22 | resizedImage5 = cv2.resize(image5,(int(500*image4.shape[1]/image4.shape[0]), 500),interpolation=cv2.INTER_AREA) 23 | 24 | cv2.imshow("Screen 1", resizedImage4) 25 | cv2.imshow("Screen 2", resizedImage5) 26 | 27 | cv2.imshow("Diff",cv2.subtract(resizedImage4, resizedImage5)) 28 | cv2.waitKey(-1) 29 | 30 | 31 | # subtract images 32 | subtractedImage = cv2.subtract(resizedImage1, resizedImage2) 33 | cv2.imshow("Subtracted Image", subtractedImage) 34 | cv2.waitKey(-1) 35 | 36 | subtractedImage2 = resizedImage1 - resizedImage2 37 | cv2.imshow("Numpy Sub Image", subtractedImage2) 38 | cv2.waitKey(-1) 39 | 40 | subtractedImage3 = resizedImage1 - 50 41 | cv2.imshow("Constant Sub Image", subtractedImage3) 42 | cv2.waitKey(-1) 43 | 44 | -------------------------------------------------------------------------------- /chapter3/Listing_3_8.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # create a circle 5 | circle = cv2.circle(np.zeros((200, 200, 3), dtype = "uint8"), (100,100), 90, (255,255,255), -1) 6 | cv2.imshow("A white circle", circle) 7 | cv2.waitKey(0) 8 | 9 | # create a square 10 | square = cv2.rectangle(np.zeros((200,200,3), dtype= "uint8"), (30,30), (170,170),(255,255,255), -1) 11 | cv2.imshow("A white square", square) 12 | cv2.waitKey(0) 13 | 14 | #bitwise AND 15 | bitwiseAnd = cv2.bitwise_and(square, circle) 16 | cv2.imshow("AND Operation", bitwiseAnd) 17 | cv2.waitKey(0) 18 | 19 | #bitwise OR 20 | bitwiseOr = cv2.bitwise_or(square, circle) 21 | cv2.imshow("OR Operation", bitwiseOr) 22 | cv2.waitKey(0) 23 | 24 | #bitwise XOR 25 | bitwiseXor = cv2.bitwise_xor(square, circle) 26 | cv2.imshow("XOR Operation", bitwiseXor) 27 | cv2.waitKey(0) 28 | 29 | #bitwise NOT 30 | bitwiseNot = cv2.bitwise_not(square) 31 | cv2.imshow("NOT Operation", bitwiseNot) 32 | cv2.waitKey(0) 33 | -------------------------------------------------------------------------------- /chapter3/Listing_3_9.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Load an image 5 | natureImage = cv2.imread("images/nature.jpg") 6 | cv2.imshow("Original Nature Image", natureImage) 7 | 8 | # Create a rectangular mask 9 | maskImage = cv2.rectangle(np.zeros(natureImage.shape[:2], dtype="uint8"), 10 | (50, 50), (int(natureImage.shape[1])-50, int(natureImage.shape[0] / 2)-50), (255, 255, 255), -1) 11 | cv2.imshow("Mask Image", maskImage) 12 | cv2.waitKey(0) 13 | 14 | # Using bitwise_and operation perform masking. Notice the mask=maskImage argument 15 | masked = cv2.bitwise_and(natureImage, natureImage, mask=maskImage) 16 | cv2.imshow("Masked image", masked) 17 | cv2.waitKey(0) 18 | -------------------------------------------------------------------------------- /chapter3/imageoverlay.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | # Read the two images 5 | 6 | image1 = cv2.imread("images/nature.jpg") 7 | image2 = cv2.imread("images/zebrasmall.png") 8 | 9 | cv2.imshow("Nature", image1) 10 | cv2.imshow("Zebra", image2) 11 | cv2.waitKey(0) 12 | 13 | 14 | -------------------------------------------------------------------------------- /chapter3/images/boat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/boat.jpg -------------------------------------------------------------------------------- /chapter3/images/cat1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/cat1.png -------------------------------------------------------------------------------- /chapter3/images/cat2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/cat2.png -------------------------------------------------------------------------------- /chapter3/images/coffeestain.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/coffeestain.jpg -------------------------------------------------------------------------------- /chapter3/images/mri.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/mri.jpg -------------------------------------------------------------------------------- /chapter3/images/nature.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/nature.jpg -------------------------------------------------------------------------------- /chapter3/images/park.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/park.jpg -------------------------------------------------------------------------------- /chapter3/images/receipt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/receipt.jpg -------------------------------------------------------------------------------- /chapter3/images/salesreceipt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/salesreceipt.jpg -------------------------------------------------------------------------------- /chapter3/images/salt-pepper.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/salt-pepper.jpg -------------------------------------------------------------------------------- /chapter3/images/scanned_doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/scanned_doc.png -------------------------------------------------------------------------------- /chapter3/images/screen1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/screen1.png -------------------------------------------------------------------------------- /chapter3/images/screen2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/screen2.png -------------------------------------------------------------------------------- /chapter3/images/soccer-in-green.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/soccer-in-green.jpg -------------------------------------------------------------------------------- /chapter3/images/sudoku.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/sudoku.jpg -------------------------------------------------------------------------------- /chapter3/images/zebra.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/zebra.png -------------------------------------------------------------------------------- /chapter3/images/zebrasmall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter3/images/zebrasmall.png -------------------------------------------------------------------------------- /chapter3/subtraction.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | image1Path = "images/cat1.png" 6 | image2Path = "images/cat2.png" 7 | 8 | image1 = cv2.imread(image1Path) 9 | image2 = cv2.imread(image2Path) 10 | 11 | # resize the two images to make them of the same dimensions. This is a must to subtract two images 12 | resizedImage1 = cv2.resize(image1,(int(500*image1.shape[1]/image1.shape[0]), 500),interpolation=cv2.INTER_AREA) 13 | resizedImage2 = cv2.resize(image2,(int(500*image2.shape[1]/image2.shape[0]), 500),interpolation=cv2.INTER_AREA) 14 | 15 | cv2.imshow("Cat 1", resizedImage1) 16 | cv2.imshow("Cat 2", resizedImage2) 17 | 18 | # Subtract image 1 from 2 19 | cv2.imshow("Diff Cat1 and Cat2",cv2.subtract(resizedImage2, resizedImage1)) 20 | cv2.waitKey(-1) 21 | 22 | 23 | # subtract images 2 from 1 24 | subtractedImage = cv2.subtract(resizedImage1, resizedImage2) 25 | cv2.imshow("Cat2 subtracted from Cat1", subtractedImage) 26 | cv2.waitKey(-1) 27 | 28 | # Numpy Subtraction Cat2 from Cat1 29 | subtractedImage2 = resizedImage2 - resizedImage1 30 | cv2.imshow("Numpy Subracts Images", subtractedImage2) 31 | cv2.waitKey(-1) 32 | 33 | # A constant subtraction 34 | subtractedImage3 = resizedImage1 - 50 35 | cv2.imshow("Constant Subracted from the image", subtractedImage3) 36 | cv2.waitKey(-1) 37 | 38 | -------------------------------------------------------------------------------- /chapter4/Listing_4_1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from matplotlib import pyplot as plot 4 | 5 | # Read an image and convert it to grayscale 6 | image = cv2.imread("images/nature.jpg") 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | cv2.imshow("Original Image", image) 9 | 10 | # calculate histogram 11 | hist = cv2.calcHist([image], [0], None, [256], [0,255]) 12 | 13 | # Plot histogram graph 14 | plot.figure() 15 | plot.title("Grayscale Histogram") 16 | plot.xlabel("Bins") 17 | plot.ylabel("Number of Pixels") 18 | plot.plot(hist) 19 | plot.show() 20 | cv2.waitKey(0) 21 | 22 | 23 | -------------------------------------------------------------------------------- /chapter4/Listing_4_2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from matplotlib import pyplot as plot 4 | 5 | # Read a color image 6 | image = cv2.imread("images/nature.jpg") 7 | 8 | cv2.imshow("Original Color Image", image) 9 | #Remember OpenCV stores color in BGR sequence instead of RBG. 10 | colors = ("blue", "green", "red") 11 | # calculate histogram 12 | for i, color in enumerate(colors): 13 | hist = cv2.calcHist([image], [i], None, [32], [0,256]) 14 | # Plot histogram graph 15 | plot.plot(hist, color=color) 16 | 17 | plot.title("RGB Color Histogram") 18 | plot.xlabel("Bins") 19 | plot.ylabel("Number of Pixels") 20 | plot.show() 21 | cv2.waitKey(0) 22 | 23 | 24 | -------------------------------------------------------------------------------- /chapter4/Listing_4_3.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from matplotlib import pyplot as plot 4 | 5 | # Read an image and convert it into grayscale 6 | image = cv2.imread("images/nature.jpg") 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | cv2.imshow("Original Image", image) 9 | 10 | # calculate histogram of the original image 11 | hist = cv2.calcHist([image], [0], None, [256], [0,255]) 12 | 13 | # Plot histogram graph 14 | #plot.figure() 15 | plot.title("Grayscale Histogram of Original Image") 16 | plot.xlabel("Bins") 17 | plot.ylabel("Number of Pixels") 18 | plot.plot(hist) 19 | plot.show() 20 | 21 | equalizedImage = cv2.equalizeHist(image) 22 | cv2.imshow("Equalized Image", equalizedImage) 23 | 24 | # calculate histogram of the original image 25 | histEqualized = cv2.calcHist([equalizedImage], [0], None, [256], [0,255]) 26 | 27 | # Plot histogram graph 28 | #plot.figure() 29 | plot.title("Grayscale Histogram of Equalized Image") 30 | plot.xlabel("Bins") 31 | plot.ylabel("Number of Pixels") 32 | plot.plot(histEqualized) 33 | plot.show() 34 | cv2.waitKey(0) 35 | 36 | 37 | -------------------------------------------------------------------------------- /chapter4/Listing_4_4.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import skimage.feature as sk 3 | import numpy as np 4 | 5 | #Read an image from the disk and convert it into grayscale 6 | image = cv2.imread("images/nature.jpg") 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | 9 | #Calculate GLCM of the grayscale image 10 | glcm = sk.greycomatrix(image,[2],[0, np.pi/2]) 11 | print(glcm) 12 | 13 | 14 | -------------------------------------------------------------------------------- /chapter4/Listing_4_5.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import skimage.feature as sk 3 | import numpy as np 4 | 5 | #Read an image from the disk and convert it into grayscale 6 | image = cv2.imread("images/nature.jpg") 7 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 8 | 9 | #Calculate GLCM of the grayscale image 10 | glcm = sk.greycomatrix(image,[2],[0, np.pi/2]) 11 | 12 | #Calculate Contrast 13 | contrast = sk.greycoprops(glcm) 14 | print("Contrast:",contrast) 15 | 16 | #Calculate ‘dissimilarity’ 17 | dissimilarity = sk.greycoprops(glcm, prop='dissimilarity') 18 | print("Dissimilarity: ", dissimilarity) 19 | 20 | #Calculate ‘homogeneity’ 21 | homogeneity = sk.greycoprops(glcm, prop='homogeneity') 22 | print("Homogeneity: ", homogeneity) 23 | 24 | #Calculate ‘ASM’ 25 | ASM = sk.greycoprops(glcm, prop='ASM') 26 | print("ASM: ", ASM) 27 | 28 | #Calculate ‘energy’ 29 | energy = sk.greycoprops(glcm, prop='energy') 30 | print("Energy: ", energy) 31 | 32 | #Calculate ‘correlation’ 33 | correlation = sk.greycoprops(glcm, prop='correlation') 34 | print("Correlation: ", correlation) 35 | 36 | -------------------------------------------------------------------------------- /chapter4/Listing_4_6.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from skimage import feature as sk 4 | 5 | #Load an image from the disk 6 | image = cv2.imread("images/obama.jpg") 7 | #Resize the image. 8 | image = cv2.resize(image,(int(image.shape[0]/5),int(image.shape[1]/5))) 9 | 10 | # HOG calculation 11 | (HOG, hogImage) = sk.hog(image, orientations=9, pixels_per_cell=(8, 8), 12 | cells_per_block=(2, 2), visualize=True, transform_sqrt=True, block_norm="L2-Hys", feature_vector=True) 13 | 14 | print("Image Dimension",image.shape) 15 | print("Feature Vector Dimension:", HOG.shape) 16 | 17 | #showing the original and HOG images 18 | cv2.imshow("Original image", image) 19 | cv2.imshow("HOG Image", hogImage) 20 | cv2.waitKey(0) 21 | -------------------------------------------------------------------------------- /chapter4/Listing_4_7.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from skimage import feature as sk 4 | from matplotlib import pyplot as plt 5 | 6 | #Load an image from the disk, resize and convert to grayscale 7 | image = cv2.imread("images/obama.jpg") 8 | image = cv2.resize(image, (int(image.shape[0]/5), int(image.shape[1]/5))) 9 | image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 10 | 11 | # calculate Histogram of original image and plot it 12 | originalHist = cv2.calcHist(image, [0], None, [256], [0,256]) 13 | 14 | plt.figure() 15 | plt.title("Histogram of Original Image") 16 | plt.plot(originalHist, color='r') 17 | 18 | # Calculate LBP image and histogram over the LBP, then plot the histogram 19 | radius = 3 20 | points = 3*8 21 | # LBP calculation 22 | lbp = sk.local_binary_pattern(image, points, radius, method='default') 23 | lbpHist, _ = np.histogram(lbp, density=True, bins=256, range=(0, 256)) 24 | 25 | plt.figure() 26 | plt.title("Histogram of LBP Image") 27 | plt.plot(lbpHist, color='g') 28 | plt.show() 29 | 30 | #showing the original and LBP images 31 | cv2.imshow("Original image", image) 32 | cv2.imshow("LBP Image", lbp) 33 | cv2.waitKey(0) 34 | -------------------------------------------------------------------------------- /chapter4/images/nature.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter4/images/nature.jpg -------------------------------------------------------------------------------- /chapter4/images/obama.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter4/images/obama.jpg -------------------------------------------------------------------------------- /chapter5/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/.DS_Store -------------------------------------------------------------------------------- /chapter5/Listing_5_1.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | # create a tensor variable with zero filled with default datatype float32 4 | a_tensor = tf.Variable(tf.zeros([2,2,2])) 5 | 6 | # Create a 0-D array or scalar variable with data type tf.int32 7 | a_scalar = tf.Variable(200, tf.int32) 8 | 9 | # Create a 1-D array or vector with data type tf.int32 10 | an_initialized_vector = tf.Variable([1, 3, 5, 7, 9, 11], tf.int32) 11 | 12 | # Create a 2-D array or matrix with default data type which is tf.float32 13 | an_initialized_matrix = tf.Variable([ [2, 4], [5, 25] ]) 14 | 15 | # Get the tensor's rank and shape 16 | rank = tf.rank(a_tensor) 17 | shape = tf.shape(a_tensor) 18 | 19 | # Create a constant initiaized with a fixed value. 20 | a_constant_tensor = tf.constant(123.100) 21 | print(a_constant_tensor) 22 | tf.print(a_constant_tensor) 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /chapter5/Listing_5_2.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf, numpy as np 2 | import matplotlib.pyplot as plt 3 | # Load MNIST data using built-in datasets download function 4 | mnist = tf.keras.datasets.mnist 5 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 6 | 7 | #Noramalize the pixel values by deviding each pixel by 255 8 | x_train, x_test = x_train / 255.0, x_test / 255.0 9 | 10 | # Build the 4-layer neural network (MLP) 11 | model = tf.keras.models.Sequential([ 12 | tf.keras.layers.Flatten(input_shape=(28, 28)), 13 | tf.keras.layers.Dense(128, activation='relu'), 14 | tf.keras.layers.Dense(60, activation='relu'), 15 | tf.keras.layers.Dense(10, activation='softmax') 16 | ]) 17 | 18 | # Compile the model and set optimizer,loss function and metrics 19 | model.compile(optimizer='adam', 20 | loss='sparse_categorical_crossentropy', 21 | metrics=['accuracy']) 22 | 23 | # Finally, train or fit the model 24 | trained_model = model.fit(x_train, y_train, validation_split=0.3, epochs=2) 25 | 26 | # Visualize loss and accuracy history 27 | plt.plot(trained_model.history['loss'], 'r--') 28 | plt.plot(trained_model.history['accuracy'], 'b-') 29 | plt.legend(['Training Loss', 'Training Accuracy']) 30 | plt.xlabel('Epoch') 31 | plt.ylabel('Percent') 32 | plt.show(); 33 | 34 | # Evaluate the result using the test set.\ 35 | evalResult = model.evaluate(x_test, y_test, verbose=1) 36 | print("Evaluation", evalResult) 37 | predicted = model.predict(x_test) 38 | print("Predicted", predicted) 39 | 40 | confusion = tf.math.confusion_matrix(y_test, np.argmax(predicted, axis=1), num_classes=10) 41 | tf.print(confusion) -------------------------------------------------------------------------------- /chapter5/Listing_5_5.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorboard.plugins.hparams import api as hp 3 | 4 | # Load MNIST data using built-in datasets download function 5 | mnist = tf.keras.datasets.mnist 6 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 7 | 8 | x_train, x_test = x_train / 255.0, x_test / 255.0 9 | 10 | HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([16, 32])) 11 | HP_DROPOUT = hp.HParam('dropout', hp.RealInterval(0.1, 0.2)) 12 | HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd'])) 13 | 14 | METRIC_ACCURACY = 'accuracy' 15 | 16 | with tf.summary.create_file_writer('logs/hparam_tuning').as_default(): 17 | hp.hparams_config( 18 | hparams=[HP_NUM_UNITS, HP_DROPOUT, HP_OPTIMIZER], 19 | metrics=[hp.Metric(METRIC_ACCURACY, display_name='Accuracy')], 20 | ) 21 | 22 | 23 | def train_test_model(hparams): 24 | model = tf.keras.models.Sequential([ 25 | tf.keras.layers.Flatten(), 26 | tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.relu), 27 | tf.keras.layers.Dropout(hparams[HP_DROPOUT]), 28 | tf.keras.layers.Dense(10, activation=tf.nn.softmax), 29 | ]) 30 | model.compile( 31 | optimizer=hparams[HP_OPTIMIZER], 32 | loss='sparse_categorical_crossentropy', 33 | metrics=['accuracy'], 34 | ) 35 | 36 | model.fit(x_train, y_train, epochs=5) 37 | _, accuracy = model.evaluate(x_test, y_test) 38 | return accuracy 39 | def run(run_dir, hparams): 40 | with tf.summary.create_file_writer(run_dir).as_default(): 41 | hp.hparams(hparams) # record the values used in this trial 42 | accuracy = train_test_model(hparams) 43 | tf.summary.scalar(METRIC_ACCURACY, accuracy, step=1) 44 | 45 | session_num = 0 46 | 47 | for num_units in HP_NUM_UNITS.domain.values: 48 | for dropout_rate in (HP_DROPOUT.domain.min_value, HP_DROPOUT.domain.max_value): 49 | for optimizer in HP_OPTIMIZER.domain.values: 50 | hparams = { 51 | HP_NUM_UNITS: num_units, 52 | HP_DROPOUT: dropout_rate, 53 | HP_OPTIMIZER: optimizer, 54 | } 55 | run_name = "run-%d" % session_num 56 | print('--- Starting trial: %s' % run_name) 57 | print({h.name: hparams[h] for h in hparams}) 58 | run('logs/hparam_tuning/' + run_name, hparams) 59 | session_num += 1 -------------------------------------------------------------------------------- /chapter5/Listing_5_6.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import matplotlib.pyplot as plt 3 | import os 4 | 5 | # The file path where checkpoint will be saved. 6 | checkpoint_path = "cv_checkpoint_dir/mnist_model.ckpt" 7 | checkpoint_dir = os.path.dirname(checkpoint_path) 8 | 9 | # Create a callback that saves the model's weights. 10 | cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path, 11 | save_weights_only=True, 12 | verbose=1) 13 | 14 | # Load MNIST data using built-in datasets download function. 15 | mnist = tf.keras.datasets.mnist 16 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 17 | 18 | # Noramalize the pixel values by deviding each pixel by 255. 19 | x_train, x_test = x_train / 255.0, x_test / 255.0 20 | 21 | # Build the ANN with 4-layers. 22 | model = tf.keras.models.Sequential([ 23 | tf.keras.layers.Flatten(input_shape=(28, 28)), 24 | tf.keras.layers.Dense(128, activation='relu'), 25 | tf.keras.layers.Dense(60, activation='relu'), 26 | tf.keras.layers.Dense(10, activation='softmax') 27 | ]) 28 | 29 | # Compile the model and set optimizer,loss function and metrics 30 | model.compile(optimizer='adam', 31 | loss='sparse_categorical_crossentropy', 32 | metrics=['accuracy']) 33 | 34 | # Finally, train or fit the model, pass callbacks to save the model weights. 35 | trained_model = model.fit(x_train, y_train, validation_split=0.3, epochs=10, callbacks=[cp_callback]) 36 | 37 | # Visualize loss and accuracy history 38 | plt.plot(trained_model.history['loss'], 'r--') 39 | plt.plot(trained_model.history['accuracy'], 'b-') 40 | plt.legend(['Training Loss', 'Training Accuracy']) 41 | plt.xlabel('Epoch') 42 | plt.ylabel('Percent') 43 | plt.show(); 44 | 45 | # Evaluate the result using the test set. 46 | evalResult = model.evaluate(x_test, y_test, verbose=1) 47 | print("Evaluation Result: ", evalResult) 48 | 49 | -------------------------------------------------------------------------------- /chapter5/Listing_5_7.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pathlib 3 | import cv2 4 | import tensorflow as tf 5 | import matplotlib.pyplot as plt 6 | 7 | 8 | # Section1: Loading images from directories for training and test 9 | trainig_img_dir ="images/chest_xray/train" 10 | test_img_dir ="images/chest_xray/test" 11 | 12 | # ImageDataGenerator class provides mechaism to load both small and large dataset. 13 | # Instruct ImageDataGenerator to scale to normalize pixel values to range (0, 1) 14 | datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) 15 | #Create training image iterator that will be loaded in small batch size. Resize all images to a standard sizes. 16 | train_it = datagen.flow_from_directory(trainig_img_dir, batch_size=8, target_size=(1024,1024)) 17 | #Create training image iterator that will be loaded in small batch size. Resize all images to a standard sizes. 18 | test_it = datagen.flow_from_directory(test_img_dir, batch_size=8, target_size=(1024, 1024)) 19 | 20 | # Lines 22 through 24 are optional to explore your images. 21 | # Notice, next() function call returns both pixel and labels values as numpy arrays. 22 | train_images, train_labels = train_it.next() 23 | test_images, test_labels = test_it.next() 24 | print('Batch shape=%s, min=%.3f, max=%.3f' % (train_images.shape, train_images.min(), train_images.max())) 25 | 26 | # Section 2: Build CNN network and train with training dataset. 27 | # You could pass argument parameters to build_cnn() function to set some of the values 28 | # such as number of filters, strides, activation function, number of layers etc. 29 | def build_cnn(): 30 | model = tf.keras.models.Sequential() 31 | model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu', strides=(2,2), input_shape=(1024, 1024, 3))) 32 | model.add(tf.keras.layers.MaxPooling2D((2, 2))) 33 | model.add(tf.keras.layers.Conv2D(32, (3, 3), strides=(2,2),activation='relu')) 34 | model.add(tf.keras.layers.MaxPooling2D((2, 2))) 35 | model.add(tf.keras.layers.Conv2D(16, (3, 3), strides=(2,2),activation='relu')) 36 | model.add(tf.keras.layers.Flatten()) 37 | model.add(tf.keras.layers.Dense(16, activation='relu')) 38 | model.add(tf.keras.layers.Dense(2, activation='softmax')) 39 | return model 40 | 41 | # Build CNN model 42 | model = build_cnn() 43 | #Compile the model with optimizer and loss function 44 | model.compile(optimizer='adam', 45 | loss='categorical_crossentropy', 46 | metrics=['accuracy']) 47 | 48 | # Fit the model. fit_generator() function iteratively loads large number of images in batches 49 | history = model.fit_generator(train_it, epochs=10, steps_per_epoch=16, 50 | validation_data=test_it, validation_steps=8) 51 | 52 | # Section 3: Save the CNN model to disk for later use. 53 | model_path = "models/pneumiacnn" 54 | model.save(filepath=model_path) 55 | 56 | # Section 4: Display evaluation metrics 57 | print(history.history.keys()) 58 | plt.plot(history.history['accuracy'], label='accuracy') 59 | plt.plot(history.history['val_accuracy'], label = 'val_accuracy') 60 | plt.plot(history.history['loss'], label='loss') 61 | plt.plot(history.history['val_loss'], label = 'val_loss') 62 | 63 | plt.xlabel('Epoch') 64 | plt.ylabel('Metrics') 65 | plt.ylim([0.5, 1]) 66 | plt.legend(loc='lower right') 67 | plt.show() 68 | test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) 69 | print(test_acc) 70 | 71 | -------------------------------------------------------------------------------- /chapter5/Listing_5_8.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pathlib 3 | import cv2 4 | import tensorflow as tf 5 | import matplotlib.pyplot as plt 6 | 7 | model_path = "models/pneumiacnn" 8 | 9 | val_img_dir ="images/chest_xray/val" 10 | # ImageDataGenerator class provides mechaism to load both small and large dataset. 11 | # Instruct ImageDataGenerator to scale to normalize pixel values to range (0, 1) 12 | datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255.) 13 | #Create training image iterator that will be loaded in small batch size. Resize all images to a standard sizes. 14 | val_it = datagen.flow_from_directory(val_img_dir, batch_size=8, target_size=(1024,1024)) 15 | 16 | 17 | # Load and create the exact same model, including its weights and the optimizer 18 | model = tf.keras.models.load_model(model_path) 19 | 20 | # Predict the class of the input image from the loaded model 21 | predicted = model.predict_generator(val_it, steps=24) 22 | print("Predicted", predicted) 23 | -------------------------------------------------------------------------------- /chapter5/__pycache__/dirmanager.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/__pycache__/dirmanager.cpython-36.pyc -------------------------------------------------------------------------------- /chapter5/__pycache__/label_data_creater.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/__pycache__/label_data_creater.cpython-36.pyc -------------------------------------------------------------------------------- /chapter5/dirmanager.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import shutil 4 | import cv2 5 | 6 | 7 | class FileManager: 8 | def __init__(self): 9 | pass 10 | 11 | # Read each line from the label_path and move images from image_path to dest_path 12 | def img_to_class_dir(self, img_path=None, label_path=None, dest_path=None): 13 | labels = open(label_path, "r") 14 | for line in labels: 15 | fields = line.split("\t") 16 | print("imageid:",fields[0]) 17 | print("disease code: " + fields[1].split(" ")[0]) 18 | image_filename = img_path+""+fields[0]+".ppm" 19 | dest_filename = dest_path+"/"+fields[1].split(" ")[0]+"/"+fields[0]+".jpg" 20 | if not os.path.exists(dest_path+"/"+fields[1].split(" ")[0]): 21 | os.makedirs(dest_path+"/"+fields[1].split(" ")[0]) 22 | if os.path.exists(image_filename): 23 | shutil.move(image_filename, dest_filename) 24 | print("Moved file") 25 | def move_file(filepath=None, dest_path=None): 26 | print("file move") 27 | 28 | 29 | 30 | #FileManager.img_to_class_dir(img_path="resources/all-images/", label_path="resources/retina_labels.txt", dest_path="images/retina") 31 | for path, subdirs, files in os.walk("images/retina/"): 32 | for name in files: 33 | print(os.path.join(path, name)) 34 | images = cv2.imread(os.path.join(path, name)) 35 | cv2.imwrite("images/retina/pbgs/"+name+".png", images) 36 | cv2.imshow("im", images) 37 | #cv2.waitKey(0) -------------------------------------------------------------------------------- /chapter5/evaluation.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | import sys 5 | tf.executing_eagerly() 6 | 7 | # Load MNIST data using built-in datasets download function 8 | mnist = tf.keras.datasets.mnist 9 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 10 | 11 | #Noramalize the pixel values by deviding each pixel by 255 12 | x_train, x_test = x_train / 255.0, x_test / 255.0 13 | 14 | # Build the ANN with 4-layers 15 | model = tf.keras.models.Sequential([ 16 | tf.keras.layers.Flatten(input_shape=(28, 28)), 17 | tf.keras.layers.Dense(128, activation='relu'), 18 | tf.keras.layers.Dense(60, activation='relu'), 19 | tf.keras.layers.Dense(10, activation='softmax') 20 | ]) 21 | 22 | # Compile the model and set optimizer,loss function and metrics 23 | model.compile(optimizer='adam', 24 | loss='sparse_categorical_crossentropy', 25 | metrics=['accuracy']) 26 | 27 | # Finally, train or fot the model 28 | history = model.fit(x_train, y_train, validation_split=0.3, epochs=10) 29 | 30 | # Visualize loss and accuracy history 31 | plt.plot(history.history['loss'], 'r--') 32 | plt.plot(history.history['accuracy'], 'b-') 33 | plt.legend(['Training Loss', 'Training Accuracy']) 34 | plt.xlabel('Epoch') 35 | plt.ylabel('Percent') 36 | #plt.show(); 37 | 38 | # Evaluate the result using the test set.\ 39 | evalResult = model.evaluate(x_test, y_test, verbose=1) 40 | print("Evaluation", evalResult) 41 | predicted = model.predict(x_test) 42 | print("Predicted", predicted) 43 | print("prediction class", np.argmax(predicted, axis=1)) 44 | 45 | confusion = tf.math.confusion_matrix(y_test, np.argmax(predicted, axis=1), num_classes=10) 46 | 47 | tf.print(confusion) 48 | -------------------------------------------------------------------------------- /chapter5/label_data_creater.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, division, print_function, unicode_literals 2 | import tensorflow as tf 3 | import numpy as np 4 | import IPython.display as display 5 | from PIL import Image, PpmImagePlugin 6 | import matplotlib.pyplot as plt 7 | import os 8 | import pathlib 9 | import glob 10 | import cv2 11 | 12 | print(tf.__version__) 13 | 14 | 15 | def get_ds(data_dir_str, batch_size=32, img_height=224, img_width=224): 16 | data_dir = pathlib.Path(data_dir_str) 17 | image_count = len(list(data_dir.glob('*/*'))) 18 | STEPS_PER_EPOCH = np.ceil(image_count / batch_size) 19 | # compute the class name from the dir 20 | class_names = np.array([item.name for item in data_dir.glob('*')]) 21 | 22 | list_ds = tf.data.Dataset.list_files(str(data_dir / '*/*')) 23 | 24 | def get_label(file_path): 25 | # convert the path to a list of path components 26 | parts = tf.strings.split(file_path, os.path.sep) 27 | 28 | # The second to last is the class-directory 29 | 30 | return parts[-2] == class_names 31 | 32 | def decode_img(img): 33 | # convert the compressed string to a 3D uint8 tensor 34 | img = tf.image.decode_jpeg(img, channels=3) 35 | # Use `convert_image_dtype` to convert to floats in the [0,1] range. 36 | img = tf.image.convert_image_dtype(img, tf.float32) 37 | # resize the image to the desired size. 38 | return tf.image.resize(img, [img_width, img_height]) 39 | 40 | def process_path(file_path): 41 | label = get_label(file_path) 42 | 43 | # load the raw data from the file as a string 44 | img = tf.io.read_file(file_path) 45 | img = decode_img(img) 46 | return img, label 47 | 48 | # Set `num_parallel_calls` so multiple images are loaded/processed in parallel. 49 | AUTOTUNE = tf.data.experimental.AUTOTUNE 50 | labeled_ds = list_ds.map(process_path, num_parallel_calls=AUTOTUNE) 51 | 52 | 53 | def prepare_for_training(ds, cache=True, shuffle_buffer_size=1000): 54 | # This is a small dataset, only load it once, and keep it in memory. 55 | # use `.cache(filename)` to cache preprocessing work for datasets that don't 56 | # fit in memory. 57 | if cache: 58 | if isinstance(cache, str): 59 | ds = ds.cache(cache) 60 | else: 61 | ds = ds.cache() 62 | 63 | ds = ds.shuffle(buffer_size=shuffle_buffer_size) 64 | 65 | # Repeat forever 66 | ds = ds.repeat() 67 | 68 | ds = ds.batch(batch_size) 69 | 70 | # `prefetch` lets the dataset fetch batches in the background while the model 71 | # is training. 72 | ds = ds.prefetch(buffer_size=AUTOTUNE) 73 | 74 | return ds 75 | 76 | 77 | train_ds = prepare_for_training(labeled_ds, cache=True) 78 | 79 | image_batch, label_batch = next(iter(train_ds)) 80 | 81 | return image_batch.numpy(), label_batch.numpy() 82 | -------------------------------------------------------------------------------- /chapter5/models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/models/.DS_Store -------------------------------------------------------------------------------- /chapter5/models/pneumiacnn/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/models/pneumiacnn/.DS_Store -------------------------------------------------------------------------------- /chapter5/models/pneumiacnn/saved_model.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/models/pneumiacnn/saved_model.pb -------------------------------------------------------------------------------- /chapter5/models/pneumiacnn/variables/variables.data-00000-of-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/models/pneumiacnn/variables/variables.data-00000-of-00001 -------------------------------------------------------------------------------- /chapter5/models/pneumiacnn/variables/variables.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/models/pneumiacnn/variables/variables.index -------------------------------------------------------------------------------- /chapter5/resources/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter5/resources/.DS_Store -------------------------------------------------------------------------------- /chapter5/resources/retina_labels.txt: -------------------------------------------------------------------------------- 1 | im0001 7 Background Diabetic Retinopathy 2 | im0002 13 Choroidal Neovascularization AND Arteriosclerotic Retinopathy 3 | im0003 14 Drusen, large AND Geographic Atrophy RPE" 4 | im0004 14 Cilio-Retinal Artery Occlusion OR Central Retinal Artery Occlusion 5 | im0005 3 Central Retinal Artery Occlusion AND Central Retinal Vein Occlusion 6 | im0006 14 Drusen 7 | im0007 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND ASR &HTR 8 | im0008 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND ASR 9 | im0009 7 Background Diabetic Retinopathy 10 | im0010 14 Histoplasmosis 11 | im0011 13 Choroidal Neovascularization AND Histoplasmosis 12 | im0012 14 Drusen 13 | im0013 7 Background Diabetic Retinopathy 14 | im0014 14 Nevus 15 | im0015 14 Drusen 16 | im0016 7 Background Diabetic Retinopathy 17 | im0017 13 Choroidal Neovascularization 18 | im0018 5 Central Retinal Vein Occlusion, compensated 19 | im0019 5 Central Retinal Vein Occlusion 20 | im0020 3 Central Retinal Artery Occlusion AND Central Retinal Vein Occlusion 21 | im0021 8 Central Retinal Vein Occlusion 22 | im0022 9 Arteriosclerotic Retinopathy 23 | im0023 14 Nevus 24 | im0024 14 Epiretinal Membrane?? 25 | im0025 5 Central Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 26 | im0026 5 Central Retinal Vein Occlusion 27 | im0027 5 Central Retinal Vein Occlusion 28 | im0028 6 Hemi-Central Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 29 | im0029 14 Unknown Diagnosis 30 | im0030 0 Normal? 31 | im0031 7 Background Diabetic Retinopathy 32 | im0032 0 Normal 33 | im0033 5 Central Retinal Vein Occlusion 34 | im0034 13 Choroidal Neovascularization AND Age Related Macular Degeneration 35 | im0035 14 Normal 36 | im0036 13 Choroidal Neovascularization AND Age Related Macular Degeneration 37 | im0037 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND HTR 38 | im0038 7 Background Diabetic Retinopathy AND Hypertensive Retinopathy 39 | im0039 14 Drusen, fine 40 | im0040 7 Background Diabetic Retinopathy OR Human Immune Deficiency Virus 41 | im0041 14 Drusen, fine 42 | im0042 14 Retinitis, Disease AND Toxoplasmosis 43 | im0043 14 Retinitis, Disease 44 | im0044 14 Retinitis, Disease 45 | im0045 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND ASR 46 | im0046 14 Drusen 47 | im0047 14 Slide missed 48 | im0048 10 Hypertensive Retinopathy 49 | im0049 11 Coats', Not Isolated Telangiectasia 50 | im0050 7 Background Diabetic Retinopathy 51 | im0051 7 Background Diabetic Retinopathy 52 | im0052 11 Coats', Not Isolated Telangiectasia 53 | im0053 13 Choroidal Neovascularization AND Age Related Macular Degeneration 54 | im0054 13 Choroidal Neovascularization AND Histoplasmosis 55 | im0055 14 Choroidal Melanoma 56 | im0056 14 Drusen 57 | im0057 10 Hypertensive Retinopathy??? OR Background Diabetic Retinopathy 58 | im0058 7 Background Diabetic Retinopathy 59 | im0059 7 Background Diabetic Retinopathy??? OR Human Immune Deficiency Virus 60 | im0060 7 Background Diabetic Retinopathy??? 61 | im0061 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND HTR 62 | im0062 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND ASR 63 | im0063 14 Drusen 64 | im0064 12 Macroaneurism AND Hypertensive Retinopathy AND Arteriosclerotic Retinopathy 65 | im0065 10 Hypertensive Retinopathy??? OR Background Diabetic Retinopathy 66 | im0066 14 Drusen 67 | im0067 14 Retinitis, Disease 68 | im0068 14 Drusen 69 | im0069 7 Background Diabetic Retinopathy 70 | im0070 7 Background Diabetic Retinopathy??? 71 | im0071 14 Myelinated Nerve Fibers 72 | im0072 14 Unknown Diagnosis, poor quality 73 | im0073 10 Hypertensive Retinopathy AND Arteriosclerotic Retinopathy 74 | im0074 5 Central Retinal Vein Occlusion 75 | im0075 10 Hypertensive Retinopathy? 76 | im0076 0 Normal 77 | im0077 0 Normal AND Hypertensive Retinopathy [V: How could it be?] 78 | im0078 14 Optic Nerve Atrophy 79 | im0079 14 Unknown Diagnosis, unannotated 80 | im0080 0 Normal?, possibly fine drusen 81 | im0081 0 Normal 82 | im0082 0 Normal 83 | im0083 13 Choroidal Neovascularization AND Age Related Macular Degeneration 84 | im0084 13 Choroidal Neovascularization AND Age Related Macular Degeneration 85 | im0085 8 Proliferative Diabetic Retinopathy 86 | im0086 14 Stellate Maculopathy 87 | im0087 8 Proliferative Diabetic Retinopathy 88 | im0088 14 Retinitis, Disease 89 | im0089 7 Background Diabetic Retinopathy AND Hypertensive Retinopathy 90 | im0090 10 Hypertensive Retinopathy 91 | im0091 13 Choroidal Neovascularization AND Age Related Macular Degeneration AND RPED 92 | im0092 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 93 | im0093 11 Coats', Not Isolated Telangiectasia 94 | im0094 7 10 Background Diabetic Retinopathy AND Hypertensive Retinopathy 95 | im0095 4 Branch Retinal Vein Occlusion 96 | im0096 7 Background Diabetic Retinopathy 97 | im0097 14 Drusen, large AND Drusen, fine 98 | im0098 14 10 Drusen, large AND Hypertensive Retinopathy 99 | im0099 14 Toxoplasmosis 100 | im0100 13 14 Choroidal Neovascularization AND Histoplasmosis 101 | im0101 13 Choroidal Neovascularization 102 | im0102 14 Patterned PPEopathy 103 | im0103 13 Choroidal Neovascularization 104 | im0104 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 105 | im0105 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 106 | im0106 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 107 | im0107 14 Histoplasmosis 108 | im0108 0 Slide missed 109 | im0109 0 Slide missed 110 | im0110 13 14 Choroidal Neovascularization AND Histoplasmosis 111 | im0111 7 14 Background Diabetic Retinopathy AND Histoplasmosis 112 | im0112 14 Histoplasmosis 113 | im0113 7 Background Diabetic Retinopathy 114 | im0114 7 Background Diabetic Retinopathy 115 | im0115 7 Background Diabetic Retinopathy 116 | im0116 7 Background Diabetic Retinopathy 117 | im0117 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 118 | im0118 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 119 | im0119 0 Normal 120 | im0120 0 Normal 121 | im0121 7 Background Diabetic Retinopathy 122 | im0122 7 Background Diabetic Retinopathy 123 | im0123 7 Background Diabetic Retinopathy 124 | im0124 7 Background Diabetic Retinopathy 125 | im0125 7 Background Diabetic Retinopathy 126 | im0126 7 Background Diabetic Retinopathy 127 | im0127 7 Background Diabetic Retinopathy 128 | im0128 14 Chorioretinal Scar 129 | im0129 14 Retinitis, Disease?? 130 | im0130 14 Retinitis, Disease 131 | im0131 14 Retinitis, Disease 132 | im0132 14 Frosted Branch Vascuopathy 133 | im0133 7 Background Diabetic Retinopathy 134 | im0134 7 Background Diabetic Retinopathy 135 | im0135 7 Background Diabetic Retinopathy 136 | im0136 7 Background Diabetic Retinopathy 137 | im0137 7 Background Diabetic Retinopathy 138 | im0138 7 Background Diabetic Retinopathy 139 | im0139 7 Background Diabetic Retinopathy 140 | im0140 7 Background Diabetic Retinopathy 141 | im0141 7 Background Diabetic Retinopathy 142 | im0142 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 143 | im0143 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 144 | im0144 14 Age Related Macular Degeneration 145 | im0145 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 146 | im0146 14 Nevus 147 | im0147 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 148 | im0148 13 Choroidal Neovascularization 149 | im0149 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 150 | im0150 13 Choroidal Neovascularization 151 | im0151 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 152 | im0152 14 Asteroid Hyalosis 153 | im0153 14 Asteroud Hyalosis 154 | im0154 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 155 | im0155 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 156 | im0156 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 157 | im0157 7 Background Diabetic Retinopathy 158 | im0158 13 7 Choroidal Neovascularization AND Age Related Macular Degeneration 159 | im0159 14 Drusen, fine 160 | im0160 5 9 Central Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 161 | im0161 5 Central Retinal Vein Occlusion 162 | im0162 0 Normal 163 | im0163 0 Normal 164 | im0164 0 Normal 165 | im0165 5 Central Retinal Vein Occlusion 166 | im0166 5 Central Retinal Vein Occlusion 167 | im0167 0 Normal 168 | im0168 5 3 Central Retinal Vein Occlusion AND Central Retinal Artery Occlusion 169 | im0169 5 3 Central Retinal Vein Occlusion AND Central Retinal Artery Occlusion 170 | im0170 0 Normal 171 | im0171 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 172 | im0172 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 173 | im0173 14 Myelinated Nerve Fibers 174 | im0174 14 Myelinated Nerve Fibers 175 | im0175 14 Drusen, large AND Drusen, fine 176 | im0176 14 Unknown diagnosis 177 | im0177 11 Coats', Not Isolated Telangiectasia" 178 | im0178 11 Coats', Not Isolated Telangiectasia" 179 | im0179 8 Proliferative Diabetic Retinopathy" 180 | im0180 14 Histoplasmosis??? 181 | im0181 14 Histoplasmosis??? 182 | im0182 14 RD 183 | im0183 14 Histoplasmosis 184 | im0184 0 14 Normal AND Choroidal Nevus 185 | im0185 14 Unknown Diagnosis 186 | im0186 7 Background Diabetic Retinopathy AND Choroidal Nevus 187 | im0187 7 Background Diabetic Retinopathy 188 | im0188 7 Background Diabetic Retinopathy 189 | im0189 7 Background Diabetic Retinopathy 190 | im0190 0 Normal 191 | im0191 7 Background Diabetic Retinopathy 192 | im0192 8 14 Proliferative Diabetic Retinopathy AND Drusen, large 193 | im0193 7 14 Background Diabetic Retinopathy AND Drusen, large 194 | im0194 7 Background Diabetic Retinopathy 195 | im0195 7 Background Diabetic Retinopathy 196 | im0196 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 197 | im0197 7 Background Diabetic Retinopathy 198 | im0198 0 Normal 199 | im0199 0 Normal 200 | im0200 4 Branch Retinal Vein Occlusion 201 | im0201 4 Branch Retinal Vein Occlusion 202 | im0202 4 Branch Retinal Vein Occlusion 203 | im0203 8 Proliferative Diabetic Retinopathy 204 | im0204 8 Proliferative Diabetic Retinopathy 205 | im0205 7 Background Diabetic Retinopathy 206 | im0206 5 Central Retinal Vein Occlusion, Rxed 207 | im0207 5 Central Retinal Vein Occlusion 208 | im0208 9 Arteriosclerotic Retinopathy 209 | im0209 4 9 Branch Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 210 | im0210 5 Central Retinal Vein Occlusion 211 | im0211 5 Central Retinal Vein Occlusion 212 | im0212 14 10 Epiretinal Membrane AND Hypertensive Retinopathy 213 | im0213 0 14 Normal AND High Myopia 214 | im0214 6 Hemi-Central Retinal Vein Occlusion 215 | im0215 6 Hemi-Central Retinal Vein Occlusion 216 | im0216 0 Normal 217 | im0217 5 Central Retinal Vein Occlusion 218 | im0218 5 Central Retinal Vein Occlusion 219 | im0219 0 Normal 220 | im0220 10 Hypertensive Retinopathy 221 | im0221 14 Epiretinal Membrane 222 | im0222 7 Background Diabetic Retinopathy 223 | im0223 7 Background Diabetic Retinopathy 224 | im0224 7 Background Diabetic Retinopathy 225 | im0225 7 Background Diabetic Retinopathy 226 | im0226 6 Hemi-Central Retinal Vein Occlusion 227 | im0227 7 Background Diabetic Retinopathy 228 | im0228 7 Background Diabetic Retinopathy 229 | im0229 7 14 Background Diabetic Retinopathy AND Choroidal Nevus 230 | im0230 7 9 Background Diabetic Retinopathy AND Arteriosclerotic Retinopathy 231 | im0231 0 Normal 232 | im0232 8 Proliferative Diabetic Retinopathy 233 | im0233 7 Background Diabetic Retinopathy 234 | im0234 0 Normal 235 | im0235 0 Normal 236 | im0236 0 Normal 237 | im0237 0 Normal 238 | im0238 0 Normal 239 | im0239 0 Normal 240 | im0240 0 Normal 241 | im0241 0 Normal 242 | im0242 0 Normal 243 | im0243 0 Normal 244 | im0244 0 Normal 245 | im0245 0 Normal 246 | im0246 13 Choroidal Neovascularization 247 | im0247 7 Background Diabetic Retinopathy 248 | im0248 13 Choroidal Neovascularization 249 | im0249 0 Normal 250 | im0250 14 Drusen, large 251 | im0251 7 Background Diabetic Retinopathy 252 | im0252 0 Normal 253 | im0253 0 Normal 254 | im0254 0 Normal 255 | im0255 0 Normal 256 | im0256 7 14 Background Diabetic Retinopathy AND Choroidal Nevus 257 | im0257 5 Central Retinal Vein Occlusion 258 | im0258 5 Central Retinal Vein Occlusion 259 | im0259 13 Choroidal Neovascularization 260 | im0260 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 261 | im0261 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 262 | im0262 14 9 Drusen, large AND Arteriosclerotic Retinopathy 263 | im0263 14 9 Drusen, large AND Arteriosclerotic Retinopathy 264 | im0264 14 10 Drusen, large AND Hypertensuve Retinopathy AND Age Related Macular Degeneration 265 | im0265 14 10 Drusen, large AND Hypertensuve Retinopathy AND Age Related Macular Degeneration 266 | im0266 14 10 Drusen, large AND Hypertensuve Retinopathy AND Age Related Macular Degeneration 267 | im0267 13 10 14 Choroidal Neovascularization AND Hypertensuve Retinopathy AND Age Related Macular 268 | im0268 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 269 | im0269 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 270 | im0270 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 271 | im0271 10 Hypertensuve Retinopathy?? 272 | im0272 10 Hypertensive Retinopathy?? 273 | im0273 14 Optic Atrophy 274 | im0274 10 Hypertensive Retinopathy?? 275 | im0275 10 Hypertensive Retinopathy?? 276 | im0276 10 Hypertensive Retinopathy?? 277 | im0277 7 Background Diabetic Retinopathy 278 | im0278 0 Normal 279 | im0279 12 MacroAneurism 280 | im0280 9 Arteriosclerotic Retinopathy 281 | im0281 10 Hypertensuve Retinopathy?? 282 | im0282 14 Drusen 283 | im0283 14 Vasculitis 284 | im0284 9 Arteriosclerotic Retinopathy 285 | im0285 14 Retinitis Pigmentosa 286 | im0286 14 Retinitis, Disease 287 | im0287 14 Retinitis, Disease 288 | im0288 14 Retinitis, Disease 289 | im0289 14 Vasculitis 290 | im0290 14 Retinitis, Disease 291 | im0291 14 Vasculitis 292 | im0292 14 Drusen 293 | im0293 9 10 Arteriosclerotic Retinopathy AND Hypertensuve Retinopathy 294 | im0294 14 Unknown Diagnosis 295 | im0295 14 Retinitis, Disease 296 | im0296 14 Retinitis, Disease 297 | im0297 2 Branch Retinal Artery Occlusion 298 | im0298 14 Retinitis, Disease 299 | im0299 2 Branch Retinal Artery Occlusion 300 | im0300 14 Retinitis, Disease 301 | im0301 14 Unknown Diagnosis 302 | im0302 14 Unknown Diagnosis 303 | im0303 14 Unknown Diagnosis 304 | im0304 14 Choroidal Nevus AND Drusen 305 | im0305 14 Retinitis, Disease 306 | im0306 11 Coats', Not Isolated Telangestasia 307 | im0307 14 Choroidal Nevus AND Drusen 308 | im0308 11 Coats', Not Isolated Telangestasia 309 | im0309 11 Coats', Not Isolated Telangestasia 310 | im0310 13 14 Choroidal Neovascularization AND Histoplasmosis 311 | im0311 11 Coats', Not Isolated Telangestasia 312 | im0312 11 Coats', Not Isolated Telangestasia 313 | im0313 11 Coats', Not Isolated Telangestasia 314 | im0314 11 Coats', Not Isolated Telangestasia 315 | im0315 11 Coats', Not Isolated Telangestasia 316 | im0316 11 Coats', Not Isolated Telangestasia 317 | im0317 1 Emboli, Non-Hollenhorst 318 | im0318 3 1 Central Retinal Artery Occlusion AND Emboli 319 | im0319 1 Emboli, Non-Hollenhorst 320 | im0320 3 Central Retinal Artery Occlusion 321 | im0321 3 1 Central Retinal Artery Occlusion AND Hollenhorst Emboli 322 | im0322 3 1 Central Retinal Artery Occlusion AND Hollenhorst Emboli 323 | im0323 1 Hollenhorst Plaque 324 | im0324 1 Hollenhorst Plaque 325 | im0325 1 Hollenhorst Plaque 326 | im0326 2 1 Branch Retinal Artery Occlusion AND Emboli 327 | im0327 2 1 Branch Retinal Artery Occlusion AND Emboli 328 | im0328 2 1 Branch Retinal Artery Occlusion AND Emboli 329 | im0329 6 Hemi-Central Retinal Vein Occlusion 330 | im0330 4 Branch Retinal Vein Occlusion (old) 331 | im0331 5 Central Retinal Vein Occlusion 332 | im0332 6 10 Hemi-Central Retinal Vein Occlusion AND Hypertensive Retinopathy 333 | im0333 6 Hemi-Central Retinal Vein Occlusion 334 | im0334 6 9 Hemi-Central Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 335 | im0335 6 Hemi-Central Retinal Vein Occlusion 336 | im0336 6 Hemi-Central Retinal Vein Occlusion 337 | im0337 5 Central Retinal Vein Occlusion (old) 338 | im0338 4 Branch Retinal Vein Occlusion 339 | im0339 14 10 Unsure AND Hypertensive Retinopathy 340 | im0340 8 Proliferative Diabetic Retinopathy 341 | im0341 8 Proliferative Diabetic Retinopathy 342 | im0342 8 Proliferative Diabetic Retinopathy 343 | im0343 8 Proliferative Diabetic Retinopathy 344 | im0344 14 Unknown Diagnosis 345 | im0345 8 Proliferative Diabetic Retinopathy 346 | im0346 8 Proliferative Diabetic Retinopathy 347 | im0347 8 Proliferative Diabetic Retinopathy 348 | im0348 8 Proliferative Diabetic Retinopathy 349 | im0349 8 Proliferative Diabetic Retinopathy 350 | im0350 8 Proliferative Diabetic Retinopathy 351 | im0351 8 Proliferative Diabetic Retinopathy 352 | im0352 8 Proliferative Diabetic Retinopathy 353 | im0353 8 Proliferative Diabetic Retinopathy 354 | im0354 8 Proliferative Diabetic Retinopathy 355 | im0355 14 10 9 Unknown Diagnosis AND Hypertensive Retinopathy AND Arteriosclerotic Retinopathy 356 | im0356 8 Proliferative Diabetic Retinopathy 357 | im0357 12 Macroaneurism, Disease 358 | im0358 12 Macroaneurism, Disease 359 | im0359 12 Macroaneurism, Disease 360 | im0360 14 Unknown Diagnosis AND Nevus 361 | im0361 12 10 Macroaneurism, Disease AND Hypertensive Retinopathy 362 | im0362 12 Macroaneurism, Disease 363 | im0363 12 Macroaneurism, Disease 364 | im0364 4 9 Branch Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 365 | im0365 4 Branch Retinal Vein Occlusion 366 | im0366 4 9 Branch Retinal Vein Occlusion AND Arteriosclerotic Retinopathy 367 | im0367 4 Branch Retinal Vein Occlusion 368 | im0368 6 Hemi-Central Retinal Vein Occlusion 369 | im0369 6 Hemi-Central Retinal Vein Occlusion 370 | im0370 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 371 | im0371 13 Choroidal Neovascularization 372 | im0372 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 373 | im0373 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 374 | im0374 14 Patterned RPEopathy 375 | im0375 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 376 | im0376 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 377 | im0377 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 378 | im0378 13 10 14 Choroidal Neovascularization AND Hypertensive Retinopathy AND Age Related Macular 379 | im0379 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 380 | im0380 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 381 | im0381 13 9 14 Choroidal Neovascularization AND Arteriosclerotic Retinopathy AND Drusen, large AND Nevus 382 | im0382 13 14 Choroidal Neovascularization AND Drusen, large 383 | im0383 13 14 Choroidal Neovascularization? AND Age Related Macular Degeneration 384 | im0384 13 14 Choroidal Neovascularization AND Age Related Macular Degeneration 385 | im0385 13 14 Choroidal Neovascularization OR Age Related Macular Degeneration 386 | im0386 14 Choroidal Hemangioma (Recommendation: Don't use) 387 | im0387 14 Choroidal Hemangioma (Recommendation: Don't use) 388 | im0388 14 Not obvious, Choroidal Hemangioma (Recommendation:"Don't use) 389 | im0389 14 Choroidal Hemangioma, unknown type (Recommendation:"Don't use) 390 | im0390 14 Choroidal Hemangioma (Recommendation: Don't use) 391 | im0391 14 Choroidal Hemangioma (Recommendation: Don't use) 392 | im0392 14 Choroidal Hemangioma (Recommendation: Don't use) 393 | im0393 14 tumor unknown (Recommendation: Don't use) 394 | im0394 14 Choroidal Hemangioma (Recommendation: Don't use) 395 | im0395 14 Choroidal Hemangioma 396 | im0396 10 9 Hypertensive Retinopathy AND Arteriosclerotic Retinopathy 397 | im0397 10 Hypertensive Retinopathy 398 | im0398 10 9 Hypertensive Retinopathy AND Arteriosclerotic Retinopathy 399 | im0399 2 1 9 Branch Retinal Artery Occlusion AND Hollenhorst Plaque Emboli AND ASR 400 | im0400 2 1 9 Branch Retinal Artery Occlusion AND Hollenhorst Plaque Emboli AND ASR 401 | im0401 14 Drusen, large AND Nevus 402 | im0402 14 Drusen, large 403 | -------------------------------------------------------------------------------- /chapter6/Listing_6_1: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | sudo apt-get install protobuf-compiler python-pil python-lxml python-tk 4 | pip install --user Cython 5 | pip install --user contextlib2 6 | pip install --user pillow 7 | pip install --user lxml 8 | pip install --user matplotlib 9 | -------------------------------------------------------------------------------- /chapter6/Listing_6_15.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pathlib 3 | import random 4 | import numpy as np 5 | import tensorflow as tf 6 | import cv2 7 | # Import the object detection module. 8 | from object_detection.utils import ops as utils_ops 9 | from object_detection.utils import label_map_util 10 | 11 | # to make gfile compatible with v2 12 | tf.gfile = tf.io.gfile 13 | 14 | model_path = "ssd_model/final_model" 15 | labels_path = "models/research/object_detection/data/pet_label_map.pbtxt" 16 | image_dir = "images" 17 | image_file_pattern = "*.jpg" 18 | output_path="output_dir" 19 | 20 | PATH_TO_IMAGES_DIR = pathlib.Path(image_dir) 21 | IMAGE_PATHS = sorted(list(PATH_TO_IMAGES_DIR.glob(image_file_pattern))) 22 | 23 | # List of the strings that is used to add correct label for each box. 24 | category_index = label_map_util.create_category_index_from_labelmap(labels_path, use_display_name=True) 25 | class_num =len(category_index) 26 | 27 | def get_color_table(class_num, seed=0): 28 | random.seed(seed) 29 | color_table = {} 30 | for i in range(class_num): 31 | color_table[i] = [random.randint(0, 255) for _ in range(3)] 32 | return color_table 33 | 34 | colortable = get_color_table(class_num) 35 | 36 | # # Model preparation and loading the model from the disk 37 | def load_model(model_path): 38 | 39 | model_dir = pathlib.Path(model_path) / "saved_model" 40 | model = tf.saved_model.load(str(model_dir)) 41 | model = model.signatures['serving_default'] 42 | return model 43 | 44 | # Predict objects and bounding boxes and format the result 45 | def run_inference_for_single_image(model, image): 46 | 47 | # The input needs to be a tensor, convert it using `tf.convert_to_tensor`. 48 | input_tensor = tf.convert_to_tensor(image) 49 | # The model expects a batch of images, so add an axis with `tf.newaxis`. 50 | input_tensor = input_tensor[tf.newaxis, ...] 51 | 52 | # Run prediction from the model 53 | output_dict = model(input_tensor) 54 | 55 | # Input to model is a tensor, so the output is also a tensor 56 | # Convert to numpy arrays, and take index [0] to remove the batch dimension. 57 | # We're only interested in the first num_detections. 58 | num_detections = int(output_dict.pop('num_detections')) 59 | output_dict = {key: value[0, :num_detections].numpy() 60 | for key, value in output_dict.items()} 61 | output_dict['num_detections'] = num_detections 62 | 63 | # detection_classes should be ints. 64 | output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64) 65 | 66 | # Handle models with masks: 67 | if 'detection_masks' in output_dict: 68 | # Reframe the the bbox mask to the image size. 69 | detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( 70 | output_dict['detection_masks'], output_dict['detection_boxes'], 71 | image.shape[0], image.shape[1]) 72 | detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5, 73 | tf.uint8) 74 | output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy() 75 | 76 | return output_dict 77 | 78 | 79 | def infer_object(model, image_path): 80 | # Read the image using openCV and create an image numpy 81 | # The final output image with boxes and labels on it. 82 | imagename = os.path.basename(image_path) 83 | 84 | image_np = cv2.imread(os.path.abspath(image_path)) 85 | # Actual detection. 86 | output_dict = run_inference_for_single_image(model, image_np) 87 | 88 | # Visualization of the results of a detection. 89 | for i in range(output_dict['detection_classes'].size): 90 | 91 | box = output_dict['detection_boxes'][i] 92 | classes = output_dict['detection_classes'][i] 93 | scores = output_dict['detection_scores'][i] 94 | 95 | if scores > 0.5: 96 | h = image_np.shape[0] 97 | w = image_np.shape[1] 98 | classname = category_index[classes]['name'] 99 | classid =category_index[classes]['id'] 100 | #Draw bounding boxes 101 | cv2.rectangle(image_np, (int(box[1] * w), int(box[0] * h)), (int(box[3] * w), int(box[2] * h)), colortable[classid], 2) 102 | 103 | #Write the class name on top of the bounding box 104 | font = cv2.FONT_HERSHEY_COMPLEX_SMALL 105 | size = cv2.getTextSize(str(classname) + ":" + str(scores), font, 0.75, 1)[0][0] 106 | 107 | cv2.rectangle(image_np,(int(box[1] * w), int(box[0] * h-20)), ((int(box[1] * w)+size+5), int(box[0] * h)), colortable[classid],-1) 108 | cv2.putText(image_np, str(classname) + ":" + str(scores), 109 | (int(box[1] * w), int(box[0] * h)-5), font, 0.75, (0,0,0), 1, 1) 110 | else: 111 | break 112 | # Save the result image with bounding boxes and class labels in file system 113 | cv2.imwrite(output_path+"/"+imagename, image_np) 114 | # cv2.imshow(imagename, image_np) 115 | 116 | # Obtain the model object 117 | detection_model = load_model(model_path) 118 | 119 | # For each image, call the prediction 120 | for image_path in IMAGE_PATHS: 121 | infer_object(detection_model, image_path) -------------------------------------------------------------------------------- /chapter6/Listing_6_16.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | git clone https://github.com/ansarisam/darknet.git 3 | # Official repository 4 | #git clone https://github.com/pjreddie/darknet.git 5 | -------------------------------------------------------------------------------- /chapter6/Listing_6_17.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd darknet/ 3 | make -------------------------------------------------------------------------------- /chapter6/Listing_6_18.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd darknet 3 | ./darknet -------------------------------------------------------------------------------- /chapter6/Listing_6_19.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | mkdir pretrained 3 | cd pretrained 4 | wget https://pjreddie.com/media/files/darknet53.conv.74 -------------------------------------------------------------------------------- /chapter6/Listing_6_2: -------------------------------------------------------------------------------- 1 | %%shell 2 | mkdir computer_vision 3 | cd computer_vision 4 | git clone https://github.com/tensorflow/models.git 5 | 6 | cd models/research 7 | pwd 8 | protoc object_detection/protos/*.proto --python_out=. 9 | 10 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research 11 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research/slim 12 | 13 | 14 | python setup.py build 15 | python setup.py install -------------------------------------------------------------------------------- /chapter6/Listing_6_20.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | mkdir petdata 3 | cd petdata 4 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz 5 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz 6 | tar -xvf images.tar.gz 7 | tar -xvf annotations.tar.gz -------------------------------------------------------------------------------- /chapter6/Listing_6_21.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd /content/petdata/images 3 | rm *.mat -------------------------------------------------------------------------------- /chapter6/Listing_6_22.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | import pandas as pd 4 | import xml.etree.ElementTree as ET 5 | 6 | 7 | def xml_to_csv(path, img_path, label_path): 8 | if not os.path.exists(label_path): 9 | os.makedirs(label_path) 10 | 11 | class_list = [] 12 | for xml_file in glob.glob(path + '/*.xml'): 13 | xml_list = [] 14 | tree = ET.parse(xml_file) 15 | root = tree.getroot() 16 | for member in root.findall('object'): 17 | imagename = str(root.find('filename').text) 18 | print("image", imagename) 19 | index = int(imagename.rfind("_")) 20 | print("index: ", index) 21 | classname = imagename[0:index] 22 | 23 | class_index = 0 24 | if (class_list.count(classname) > 0): 25 | class_index = class_list.index(classname) 26 | 27 | else: 28 | class_list.append(classname) 29 | class_index = class_list.index(classname) 30 | 31 | print("width: ", root.find("size").find("width").text) 32 | print("height: ", root.find("size").find("height").text) 33 | print("minx: ", member[4][0].text) 34 | print("ymin:", member[4][1].text) 35 | print("maxx: ", member[4][2].text) 36 | print("maxy: ", member[4][3].text) 37 | w = float(root.find("size").find("width").text) 38 | h = float(root.find("size").find("height").text) 39 | dw = 1.0 / w 40 | dh = 1.0 / h 41 | x = (float(member[4][0].text) + float(member[4][2].text)) / 2.0 - 1 42 | y = (float(member[4][1].text) + float(member[4][3].text)) / 2.0 - 1 43 | w = float(member[4][2].text) - float(member[4][0].text) 44 | h = float(member[4][3].text) - float(member[4][1].text) 45 | x = x * dw 46 | w = w * dw 47 | y = y * dh 48 | h = h * dh 49 | 50 | value = (class_index, 51 | x, 52 | y, 53 | y, 54 | h 55 | ) 56 | print("The line value is: ", value) 57 | print("csv file name: ", os.path.join(label_path, imagename.rsplit('.', 1)[0] + '.txt')) 58 | xml_list.append(value) 59 | df = pd.DataFrame(xml_list) 60 | df.to_csv(os.path.join(label_path, imagename.rsplit('.', 1)[0] + '.txt'), index=None, header=False, sep=' ') 61 | 62 | class_df = pd.DataFrame(class_list) 63 | return class_df 64 | 65 | 66 | def create_training_and_test(image_dir, label_dir): 67 | file_list = [] 68 | for img in glob.glob(image_dir + "/*"): 69 | print(os.path.abspath(img)) 70 | 71 | imagefile = os.path.basename(img) 72 | 73 | textfile = imagefile.rsplit('.', 1)[0] + '.txt' 74 | 75 | if not os.path.isfile(label_dir + "/" + textfile): 76 | print("delete image file ", img) 77 | os.remove(img) 78 | continue 79 | file_list.append(os.path.abspath(img)) 80 | 81 | file_df = pd.DataFrame(file_list) 82 | train = file_df.sample(frac=0.7, random_state=10) 83 | test = file_df.drop(train.index) 84 | train.to_csv("petdata/train.txt", index=None, header=False) 85 | test.to_csv("petdata/test.txt", index=None, header=False) 86 | 87 | 88 | def main(): 89 | img_dir = "petdata/images" 90 | label_dir = "petdata/labels" 91 | 92 | xml_path = os.path.join(os.getcwd(), 'petdata/annotations/xmls') 93 | img_path = os.path.join(os.getcwd(), img_dir) 94 | label_path = os.path.join(os.getcwd(), label_dir) 95 | 96 | class_df = xml_to_csv(xml_path, img_path, label_path) 97 | class_df.to_csv('petdata/class.data', index=None, header=False) 98 | create_training_and_test(img_dir, label_path) 99 | print('Successfully converted xml to csv.') 100 | 101 | 102 | main() -------------------------------------------------------------------------------- /chapter6/Listing_6_23.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd darknet/ 3 | ./darknet detector train cfg/pet_input.cfg cfg/yolov3-pet.cfg /content/pretrained/darknet53.conv.74 -------------------------------------------------------------------------------- /chapter6/Listing_6_24.py: -------------------------------------------------------------------------------- 1 | import os 2 | import subprocess 3 | import pandas as pd 4 | image_path="/Users/sansari/PycharmProjects/cviz_tf2_3/chapter6/yolov3/darknet/test_images/dog.jpg" 5 | yolov3_weights_path="/Users/sansari/PycharmProjects/cviz_tf2_3/chapter6/yolov3/darknet/backup/yolov3.weights" 6 | cfg_path="/Users/sansari/PycharmProjects/cviz_tf2_3/chapter6/yolov3/darknet/cfg/yolov3.cfg" 7 | output_path="output_path" 8 | image_name = os.path.basename(image_path) 9 | process = subprocess.Popen(['yolov3/darknet/darknet', 'detect', cfg_path, yolov3_weights_path, image_path], 10 | stdout=subprocess.PIPE, 11 | stderr=subprocess.PIPE) 12 | stdout, stderr = process.communicate() 13 | 14 | std_string = stdout.decode("utf-8") 15 | std_string = std_string.split(image_path)[1] 16 | count = 0 17 | outputList = [] 18 | rowDict = {} 19 | for line in std_string.splitlines(): 20 | 21 | if count > 0: 22 | if count%2 > 0: 23 | obj_score = line.split(":") 24 | obj = obj_score[0] 25 | score = obj_score[1] 26 | rowDict["object"] = obj 27 | rowDict["score"] = score 28 | else: 29 | bbox = line.split(",") 30 | rowDict["bbox"] = bbox 31 | outputList.append(rowDict) 32 | rowDict = {} 33 | count = count +1 34 | rowDict["image"] = image_path 35 | rowDict["predictions"] = outputList 36 | 37 | df = pd.DataFrame(rowDict) 38 | df.to_json(output_path+"/"+image_name.replace(".jpg", ".json").replace(".png", ".json"),orient='records') -------------------------------------------------------------------------------- /chapter6/Listing_6_3: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd computer_vision 3 | mkdir petdata 4 | cd petdata 5 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz 6 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz 7 | tar -xvf annotations.tar.gz 8 | tar -xvf images.tar.gz -------------------------------------------------------------------------------- /chapter6/Listing_6_4: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd computer_vision 3 | cd models/research 4 | 5 | python object_detection/dataset_tools/create_pet_tf_record.py \ 6 | --label_map_path=object_detection/data/pet_label_map.pbtxt \ 7 | --data_dir=/content/computer_vision/petdata \ 8 | --output_dir=/content/computer_vision/petdata/ -------------------------------------------------------------------------------- /chapter6/Listing_6_5: -------------------------------------------------------------------------------- 1 | %%shell 2 | cd computer_vision 3 | mkdir pre-trained-model 4 | cd pre-trained-model 5 | wget http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2018_01_28.tar.gz 6 | tar -xvf ssd_inception_v2_coco_2018_01_28.tar.gz -------------------------------------------------------------------------------- /chapter6/Listing_6_6: -------------------------------------------------------------------------------- 1 | %%shell 2 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research 3 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research/slim 4 | cd computer_vision/models/research/ 5 | PIPELINE_CONFIG_PATH=/content/computer_vision/pre-trained-model/ssd_inception_v2_coco_2018_01_28/pipeline.config 6 | MODEL_DIR=/content/computer_vision/pet_detection_model/ 7 | NUM_TRAIN_STEPS=1000 8 | SAMPLE_1_OF_N_EVAL_EXAMPLES=1 9 | python object_detection/model_main.py \ 10 | --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ 11 | --model_dir=${MODEL_DIR} \ 12 | --num_train_steps=${NUM_TRAIN_STEPS} \ 13 | --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ 14 | --alsologtostderr -------------------------------------------------------------------------------- /chapter6/Listing_6_7: -------------------------------------------------------------------------------- 1 | %%shell 2 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research 3 | export PYTHONPATH=$PYTHONPATH:/content/computer_vision/models/research/slim 4 | cd computer_vision/models/research 5 | 6 | python object_detection/export_inference_graph.py \ 7 | --input_type image_tensor \ 8 | --pipeline_config_path /content/computer_vision/pre-trained-model/ssd_inception_v2_coco_2018_01_28/pipeline.config \ 9 | --trained_checkpoint_prefix /content/computer_vision/pet_detection_model/model.ckpt-100 \ 10 | --output_directory /content/computer_vision/pet_detection_model/final_model -------------------------------------------------------------------------------- /chapter6/Listing_6_8: -------------------------------------------------------------------------------- 1 | %load_ext tensorboard 2 | %tensorboard --logdir /content/computer_vision/pet_detection_model -------------------------------------------------------------------------------- /chapter7/model/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter7/model/.DS_Store -------------------------------------------------------------------------------- /chapter7/model/mscoco_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | name: "/m/01g317" 3 | id: 1 4 | display_name: "person" 5 | } 6 | item { 7 | name: "/m/0199g" 8 | id: 2 9 | display_name: "bicycle" 10 | } 11 | item { 12 | name: "/m/0k4j" 13 | id: 3 14 | display_name: "car" 15 | } 16 | item { 17 | name: "/m/04_sv" 18 | id: 4 19 | display_name: "motorcycle" 20 | } 21 | item { 22 | name: "/m/05czz6l" 23 | id: 5 24 | display_name: "airplane" 25 | } 26 | item { 27 | name: "/m/01bjv" 28 | id: 6 29 | display_name: "bus" 30 | } 31 | item { 32 | name: "/m/07jdr" 33 | id: 7 34 | display_name: "train" 35 | } 36 | item { 37 | name: "/m/07r04" 38 | id: 8 39 | display_name: "truck" 40 | } 41 | item { 42 | name: "/m/019jd" 43 | id: 9 44 | display_name: "boat" 45 | } 46 | item { 47 | name: "/m/015qff" 48 | id: 10 49 | display_name: "traffic light" 50 | } 51 | item { 52 | name: "/m/01pns0" 53 | id: 11 54 | display_name: "fire hydrant" 55 | } 56 | item { 57 | name: "/m/02pv19" 58 | id: 13 59 | display_name: "stop sign" 60 | } 61 | item { 62 | name: "/m/015qbp" 63 | id: 14 64 | display_name: "parking meter" 65 | } 66 | item { 67 | name: "/m/0cvnqh" 68 | id: 15 69 | display_name: "bench" 70 | } 71 | item { 72 | name: "/m/015p6" 73 | id: 16 74 | display_name: "bird" 75 | } 76 | item { 77 | name: "/m/01yrx" 78 | id: 17 79 | display_name: "cat" 80 | } 81 | item { 82 | name: "/m/0bt9lr" 83 | id: 18 84 | display_name: "dog" 85 | } 86 | item { 87 | name: "/m/03k3r" 88 | id: 19 89 | display_name: "horse" 90 | } 91 | item { 92 | name: "/m/07bgp" 93 | id: 20 94 | display_name: "sheep" 95 | } 96 | item { 97 | name: "/m/01xq0k1" 98 | id: 21 99 | display_name: "cow" 100 | } 101 | item { 102 | name: "/m/0bwd_0j" 103 | id: 22 104 | display_name: "elephant" 105 | } 106 | item { 107 | name: "/m/01dws" 108 | id: 23 109 | display_name: "bear" 110 | } 111 | item { 112 | name: "/m/0898b" 113 | id: 24 114 | display_name: "zebra" 115 | } 116 | item { 117 | name: "/m/03bk1" 118 | id: 25 119 | display_name: "giraffe" 120 | } 121 | item { 122 | name: "/m/01940j" 123 | id: 27 124 | display_name: "backpack" 125 | } 126 | item { 127 | name: "/m/0hnnb" 128 | id: 28 129 | display_name: "umbrella" 130 | } 131 | item { 132 | name: "/m/080hkjn" 133 | id: 31 134 | display_name: "handbag" 135 | } 136 | item { 137 | name: "/m/01rkbr" 138 | id: 32 139 | display_name: "tie" 140 | } 141 | item { 142 | name: "/m/01s55n" 143 | id: 33 144 | display_name: "suitcase" 145 | } 146 | item { 147 | name: "/m/02wmf" 148 | id: 34 149 | display_name: "frisbee" 150 | } 151 | item { 152 | name: "/m/071p9" 153 | id: 35 154 | display_name: "skis" 155 | } 156 | item { 157 | name: "/m/06__v" 158 | id: 36 159 | display_name: "snowboard" 160 | } 161 | item { 162 | name: "/m/018xm" 163 | id: 37 164 | display_name: "sports ball" 165 | } 166 | item { 167 | name: "/m/02zt3" 168 | id: 38 169 | display_name: "kite" 170 | } 171 | item { 172 | name: "/m/03g8mr" 173 | id: 39 174 | display_name: "baseball bat" 175 | } 176 | item { 177 | name: "/m/03grzl" 178 | id: 40 179 | display_name: "baseball glove" 180 | } 181 | item { 182 | name: "/m/06_fw" 183 | id: 41 184 | display_name: "skateboard" 185 | } 186 | item { 187 | name: "/m/019w40" 188 | id: 42 189 | display_name: "surfboard" 190 | } 191 | item { 192 | name: "/m/0dv9c" 193 | id: 43 194 | display_name: "tennis racket" 195 | } 196 | item { 197 | name: "/m/04dr76w" 198 | id: 44 199 | display_name: "bottle" 200 | } 201 | item { 202 | name: "/m/09tvcd" 203 | id: 46 204 | display_name: "wine glass" 205 | } 206 | item { 207 | name: "/m/08gqpm" 208 | id: 47 209 | display_name: "cup" 210 | } 211 | item { 212 | name: "/m/0dt3t" 213 | id: 48 214 | display_name: "fork" 215 | } 216 | item { 217 | name: "/m/04ctx" 218 | id: 49 219 | display_name: "knife" 220 | } 221 | item { 222 | name: "/m/0cmx8" 223 | id: 50 224 | display_name: "spoon" 225 | } 226 | item { 227 | name: "/m/04kkgm" 228 | id: 51 229 | display_name: "bowl" 230 | } 231 | item { 232 | name: "/m/09qck" 233 | id: 52 234 | display_name: "banana" 235 | } 236 | item { 237 | name: "/m/014j1m" 238 | id: 53 239 | display_name: "apple" 240 | } 241 | item { 242 | name: "/m/0l515" 243 | id: 54 244 | display_name: "sandwich" 245 | } 246 | item { 247 | name: "/m/0cyhj_" 248 | id: 55 249 | display_name: "orange" 250 | } 251 | item { 252 | name: "/m/0hkxq" 253 | id: 56 254 | display_name: "broccoli" 255 | } 256 | item { 257 | name: "/m/0fj52s" 258 | id: 57 259 | display_name: "carrot" 260 | } 261 | item { 262 | name: "/m/01b9xk" 263 | id: 58 264 | display_name: "hot dog" 265 | } 266 | item { 267 | name: "/m/0663v" 268 | id: 59 269 | display_name: "pizza" 270 | } 271 | item { 272 | name: "/m/0jy4k" 273 | id: 60 274 | display_name: "donut" 275 | } 276 | item { 277 | name: "/m/0fszt" 278 | id: 61 279 | display_name: "cake" 280 | } 281 | item { 282 | name: "/m/01mzpv" 283 | id: 62 284 | display_name: "chair" 285 | } 286 | item { 287 | name: "/m/02crq1" 288 | id: 63 289 | display_name: "couch" 290 | } 291 | item { 292 | name: "/m/03fp41" 293 | id: 64 294 | display_name: "potted plant" 295 | } 296 | item { 297 | name: "/m/03ssj5" 298 | id: 65 299 | display_name: "bed" 300 | } 301 | item { 302 | name: "/m/04bcr3" 303 | id: 67 304 | display_name: "dining table" 305 | } 306 | item { 307 | name: "/m/09g1w" 308 | id: 70 309 | display_name: "toilet" 310 | } 311 | item { 312 | name: "/m/07c52" 313 | id: 72 314 | display_name: "tv" 315 | } 316 | item { 317 | name: "/m/01c648" 318 | id: 73 319 | display_name: "laptop" 320 | } 321 | item { 322 | name: "/m/020lf" 323 | id: 74 324 | display_name: "mouse" 325 | } 326 | item { 327 | name: "/m/0qjjc" 328 | id: 75 329 | display_name: "remote" 330 | } 331 | item { 332 | name: "/m/01m2v" 333 | id: 76 334 | display_name: "keyboard" 335 | } 336 | item { 337 | name: "/m/050k8" 338 | id: 77 339 | display_name: "cell phone" 340 | } 341 | item { 342 | name: "/m/0fx9l" 343 | id: 78 344 | display_name: "microwave" 345 | } 346 | item { 347 | name: "/m/029bxz" 348 | id: 79 349 | display_name: "oven" 350 | } 351 | item { 352 | name: "/m/01k6s3" 353 | id: 80 354 | display_name: "toaster" 355 | } 356 | item { 357 | name: "/m/0130jx" 358 | id: 81 359 | display_name: "sink" 360 | } 361 | item { 362 | name: "/m/040b_t" 363 | id: 82 364 | display_name: "refrigerator" 365 | } 366 | item { 367 | name: "/m/0bt_c3" 368 | id: 84 369 | display_name: "book" 370 | } 371 | item { 372 | name: "/m/01x3z" 373 | id: 85 374 | display_name: "clock" 375 | } 376 | item { 377 | name: "/m/02s195" 378 | id: 86 379 | display_name: "vase" 380 | } 381 | item { 382 | name: "/m/01lsmm" 383 | id: 87 384 | display_name: "scissors" 385 | } 386 | item { 387 | name: "/m/0kmg4" 388 | id: 88 389 | display_name: "teddy bear" 390 | } 391 | item { 392 | name: "/m/03wvsk" 393 | id: 89 394 | display_name: "hair drier" 395 | } 396 | item { 397 | name: "/m/012xff" 398 | id: 90 399 | display_name: "toothbrush" 400 | } 401 | -------------------------------------------------------------------------------- /chapter7/video_tracking/__pycache__/object_tracker.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter7/video_tracking/__pycache__/object_tracker.cpython-36.pyc -------------------------------------------------------------------------------- /chapter7/video_tracking/__pycache__/tracker.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter7/video_tracking/__pycache__/tracker.cpython-36.pyc -------------------------------------------------------------------------------- /chapter7/video_tracking/__pycache__/videoasync.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter7/video_tracking/__pycache__/videoasync.cpython-36.pyc -------------------------------------------------------------------------------- /chapter7/video_tracking/object_tracker.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pathlib 3 | import random 4 | import numpy as np 5 | import tensorflow as tf 6 | import cv2 7 | import threading 8 | 9 | # Import the object detection module. 10 | from object_detection.utils import ops as utils_ops 11 | from object_detection.utils import label_map_util 12 | 13 | from videoasync import VideoCaptureAsync 14 | import tracker as hasher 15 | 16 | lock = threading.Lock() 17 | 18 | # to make gfile compatible with v2 19 | tf.gfile = tf.io.gfile 20 | 21 | model_path = "./../model/ssd_inception_v2_coco_2018_01_28" 22 | labels_path = "./../model/mscoco_label_map.pbtxt" 23 | 24 | # List of the strings that is used to add correct label for each box. 25 | category_index = label_map_util.create_category_index_from_labelmap(labels_path, use_display_name=True) 26 | class_num =len(category_index)+100 27 | object_ids = {} 28 | hasher_object = hasher.ObjectHasher() 29 | 30 | #Function to create color table for each object class 31 | def get_color_table(class_num, seed=50): 32 | random.seed(seed) 33 | color_table = {} 34 | for i in range(class_num): 35 | color_table[i] = [random.randint(0, 255) for _ in range(3)] 36 | return color_table 37 | 38 | colortable = get_color_table(class_num) 39 | 40 | # Initialize and start the asynchronous video capture thread 41 | cap = VideoCaptureAsync().start() 42 | 43 | # # Model preparation 44 | def load_model(model_path): 45 | model_dir = pathlib.Path(model_path) / "saved_model" 46 | model = tf.saved_model.load(str(model_dir)) 47 | model = model.signatures['serving_default'] 48 | return model 49 | 50 | model = load_model(model_path) 51 | 52 | # Predict objects and bounding boxes and format the result 53 | def run_inference_for_single_image(model, image): 54 | # The input needs to be a tensor, convert it using `tf.convert_to_tensor`. 55 | input_tensor = tf.convert_to_tensor(image) 56 | # The model expects a batch of images, so add an axis with `tf.newaxis`. 57 | input_tensor = input_tensor[tf.newaxis, ...] 58 | 59 | # Run prediction from the model 60 | output_dict = model(input_tensor) 61 | 62 | # Input to model is a tensor, so the output is also a tensor 63 | # Convert to numpy arrays, and take index [0] to remove the batch dimension. 64 | # We're only interested in the first num_detections. 65 | num_detections = int(output_dict.pop('num_detections')) 66 | output_dict = {key: value[0, :num_detections].numpy() 67 | for key, value in output_dict.items()} 68 | output_dict['num_detections'] = num_detections 69 | 70 | # detection_classes should be ints. 71 | output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64) 72 | 73 | return output_dict 74 | 75 | # Function to draw bounding boxes and tracking information on the image frame 76 | def track_object(model, image_np): 77 | global object_ids, lock 78 | # Actual detection. 79 | output_dict = run_inference_for_single_image(model, image_np) 80 | 81 | # Visualization of the results of a detection. 82 | for i in range(output_dict['detection_classes'].size): 83 | 84 | box = output_dict['detection_boxes'][i] 85 | classes = output_dict['detection_classes'][i] 86 | scores = output_dict['detection_scores'][i] 87 | 88 | if scores > 0.5: 89 | h = image_np.shape[0] 90 | w = image_np.shape[1] 91 | 92 | classname = category_index[classes]['name'] 93 | classid =category_index[classes]['id'] 94 | #Draw bounding boxes 95 | cv2.rectangle(image_np, (int(box[1] * w), int(box[0] * h)), (int(box[3] * w), int(box[2] * h)), colortable[classid], 2) 96 | 97 | #Write the class name on top of the bounding box 98 | font = cv2.FONT_HERSHEY_COMPLEX_SMALL 99 | hash, object_ids = hasher_object.getObjectId(image_np, int(box[1] * w), int(box[0] * h), int(box[3] * w), 100 | int(box[2] * h), object_ids) 101 | 102 | size = cv2.getTextSize(str(classname) + ":" + str(scores)+"[Id: "+str(object_ids.get(hash))+"]", font, 0.75, 1)[0][0] 103 | 104 | cv2.rectangle(image_np,(int(box[1] * w), int(box[0] * h-20)), ((int(box[1] * w)+size+5), int(box[0] * h)), colortable[classid],-1) 105 | cv2.putText(image_np, str(classname) + ":" + str(scores)+"[Id: "+str(object_ids.get(hash))+"]", 106 | (int(box[1] * w), int(box[0] * h)-5), font, 0.75, (0,0,0), 1, 1) 107 | 108 | cv2.putText(image_np, "Number of objects detected: "+str(len(object_ids)), 109 | (10,20), font, 0.75, (0, 0, 0), 1, 1) 110 | else: 111 | break 112 | return image_np 113 | 114 | # Function to implement infinite while loop to read video frames and generate the output for web browser 115 | def streamVideo(): 116 | global lock 117 | while (True): 118 | retrieved, frame = cap.read() 119 | if retrieved: 120 | with lock: 121 | frame = track_object(model, frame) 122 | 123 | (flag, encodedImage) = cv2.imencode(".jpg", frame) 124 | if not flag: 125 | continue 126 | 127 | yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + 128 | bytearray(encodedImage) + b'\r\n') 129 | 130 | if cv2.waitKey(1) & 0xFF == ord('q'): 131 | cap.stop() 132 | cv2.destroyAllWindows() 133 | break 134 | 135 | # When everything done, release the capture 136 | cap.stop() 137 | cv2.destroyAllWindows() 138 | -------------------------------------------------------------------------------- /chapter7/video_tracking/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Computer Vision 4 | 5 | 6 |

Video Surveillance

7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /chapter7/video_tracking/tracker.py: -------------------------------------------------------------------------------- 1 | # tracker.py 2 | import numpy as np 3 | import cv2 4 | 5 | class ObjectHasher: 6 | def __init__(self, threshold=20, size=8, max_track_frame=10, radius_tracker=5): 7 | self.threshold = 20 8 | self.size = 8 9 | self.max_track_frame = 10 10 | self.radius_tracker = 5 11 | 12 | def getCenter(self, xmin, ymin, xmax, ymax): 13 | x_center = int((xmin + xmax)/2) 14 | y_center = int((ymin+ymax)/2) 15 | return (x_center, y_center) 16 | 17 | 18 | def getObjectId(self, image_np, xmin, ymin, xmax, ymax, hamming_dict={}): 19 | croppedImage = self.getCropped(image_np,int(xmin*0.8), int(ymin*0.8), int(xmax*0.8), int(ymax*0.8)) 20 | croppedImage = cv2.cvtColor(croppedImage, cv2.COLOR_BGR2GRAY) 21 | 22 | resizedImage = self.resize(croppedImage, self.size) 23 | 24 | hash = self.getHash(resizedImage) 25 | center = self.getCenter(xmin*0.8, ymin*0.8, xmax*0.8, ymax*0.8) 26 | 27 | # hamming_dict = self.createHammingDict(hash, center, hamming_dict) 28 | hamming_dict = self.getObjectCounter(hash, hamming_dict) 29 | return hash, hamming_dict 30 | 31 | 32 | def getCropped(self, image_np, xmin, ymin, xmax, ymax): 33 | return image_np[ymin:ymax, xmin:xmax] 34 | 35 | def resize(self, cropped_image, size=8): 36 | resized = cv2.resize(cropped_image, (size+1, size)) 37 | return resized 38 | 39 | def getHash(self, resized_image): 40 | diff = resized_image[:, 1:] > resized_image[:, :-1] 41 | # convert the difference image to a hash 42 | dhash = sum([2 ** i for (i, v) in enumerate(diff.flatten()) if v]) 43 | return int(np.array(dhash, dtype="float64")) 44 | 45 | def hamming(self, hashA, hashB): 46 | # compute and return the Hamming distance between the integers 47 | return bin(int(hashA) ^ int(hashB)).count("1") 48 | 49 | def createHammingDict(self, dhash, center, hamming_dict): 50 | centers = [] 51 | matched = False 52 | matched_hash = dhash 53 | # matched_classid = classid 54 | 55 | if hamming_dict.__len__() > 0: 56 | if hamming_dict.get(dhash): 57 | matched = True 58 | 59 | else: 60 | for key in hamming_dict.keys(): 61 | 62 | hd = self.hamming(dhash, key) 63 | 64 | if(hd < self.threshold): 65 | centers = hamming_dict.get(key) 66 | if len(centers) > self.max_track_frame: 67 | centers.pop(0) 68 | centers.append(center) 69 | del hamming_dict[key] 70 | hamming_dict[dhash] = centers 71 | matched = True 72 | break 73 | 74 | if not matched: 75 | centers.append(center) 76 | hamming_dict[dhash] = centers 77 | 78 | return hamming_dict 79 | 80 | def getObjectCounter(self, dhash, hamming_dict): 81 | matched = False 82 | matched_hash = dhash 83 | lowest_hamming_dist = self.threshold 84 | object_counter = 0 85 | 86 | if len(hamming_dict) > 0: 87 | if dhash in hamming_dict: 88 | lowest_hamming_dist = 0 89 | matched_hash = dhash 90 | object_counter = hamming_dict.get(dhash) 91 | matched = True 92 | 93 | else: 94 | for key in hamming_dict.keys(): 95 | hd = self.hamming(dhash, key) 96 | if(hd < self.threshold): 97 | if hd < lowest_hamming_dist: 98 | lowest_hamming_dist = hd 99 | matched = True 100 | matched_hash = key 101 | object_counter = hamming_dict.get(key) 102 | if not matched: 103 | object_counter = len(hamming_dict) 104 | if matched_hash in hamming_dict: 105 | del hamming_dict[matched_hash] 106 | 107 | hamming_dict[dhash] = object_counter 108 | return hamming_dict 109 | 110 | 111 | def drawTrackingPoints(self, image_np, centers, color=(0,0,255)): 112 | image_np = cv2.line(image_np, centers[0], centers[len(centers) - 1], color) 113 | return image_np 114 | 115 | 116 | -------------------------------------------------------------------------------- /chapter7/video_tracking/video_server.py: -------------------------------------------------------------------------------- 1 | # video_server.py 2 | from flask import Flask, render_template, Response 3 | import object_tracker as ot 4 | app = Flask(__name__) 5 | 6 | @app.route("/") 7 | def index(): 8 | # return the rendered template 9 | return render_template("index.html") 10 | 11 | @app.route("/video_feed") 12 | def video_feed(): 13 | # return the response generated along with the specific media 14 | # type (mime type) 15 | return Response(ot.streamVideo(),mimetype = "multipart/x-mixed-replace; boundary=frame") 16 | 17 | if __name__ == '__main__': 18 | app.run(host="localhost", port="5019", debug=True, 19 | threaded=True, use_reloader=False) 20 | 21 | -------------------------------------------------------------------------------- /chapter7/video_tracking/videoasync.py: -------------------------------------------------------------------------------- 1 | # file: videoasync.py 2 | import threading 3 | import cv2 4 | 5 | class VideoCaptureAsync: 6 | def __init__(self, src=0): 7 | self.src = src 8 | self.cap = cv2.VideoCapture(self.src) 9 | self.grabbed, self.frame = self.cap.read() 10 | self.started = False 11 | self.read_lock = threading.Lock() 12 | 13 | def set(self, key, value): 14 | self.cap.set(key, value) 15 | 16 | def start(self): 17 | if self.started: 18 | print('[Warning] Asynchronous video capturing is already started.') 19 | return None 20 | self.started = True 21 | self.thread = threading.Thread(target=self.update, args=()) 22 | self.thread.start() 23 | return self 24 | 25 | def update(self): 26 | while self.started: 27 | grabbed, frame = self.cap.read() 28 | with self.read_lock: 29 | self.grabbed = grabbed 30 | self.frame = frame 31 | 32 | def read(self): 33 | with self.read_lock: 34 | frame = self.frame.copy() 35 | grabbed = self.grabbed 36 | return grabbed, frame 37 | 38 | def stop(self): 39 | self.started = False 40 | # self.cap.release() 41 | # cv2.destroyAllWindows() 42 | self.thread.join() 43 | 44 | def __exit__(self, exec_type, exc_value, traceback): 45 | self.cap.release() -------------------------------------------------------------------------------- /chapter8/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/building-computer-vision-apps-artificial-neural-networks/302c5df16dd8e75f9210c7b512fa9c64ff6ec410/chapter8/.DS_Store -------------------------------------------------------------------------------- /chapter8/Listing_8_1.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | git clone https://github.com/ansarisam/facenet.git -------------------------------------------------------------------------------- /chapter8/Listing_8_10.sh: -------------------------------------------------------------------------------- 1 | python real_time_face_recognition.py \ 2 | --source youtube \ 3 | --url https://www.youtube.com/watch?v=ZYkxVbYxy-c \ 4 | --facenet_model_checkpoint ~/20180402-114759/20180402-114759.pb \ 5 | --classfier_model ~/presidents_aligned/face_classifier.pkl 6 | -------------------------------------------------------------------------------- /chapter8/Listing_8_2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import getpass 3 | import requests 4 | 5 | VGG_FACE_URL = "http://zeus.robots.ox.ac.uk/vgg_face2/login/" 6 | IMAGE_URL = "http://zeus.robots.ox.ac.uk/vgg_face2/get_file?fname=vggface2_train.tar.gz" 7 | TEST_IMAGE_URL="http://zeus.robots.ox.ac.uk/vgg_face2/get_file?fname=vggface2_test.tar.gz" 8 | 9 | print('Please enter your VGG Face 2 credentials:') 10 | user_string = input(' User: ') 11 | password_string = getpass.getpass(prompt=' Password: ') 12 | 13 | credential = { 14 | 'username': user_string, 15 | 'password': password_string 16 | } 17 | 18 | session = requests.session() 19 | r = session.get(VGG_FACE_URL) 20 | 21 | if 'csrftoken' in session.cookies: 22 | csrftoken = session.cookies['csrftoken'] 23 | elif 'csrf' in session.cookies: 24 | csrftoken = session.cookies['csrf'] 25 | else: 26 | raise ValueError("Unable to locate CSRF token.") 27 | 28 | credential['csrfmiddlewaretoken'] = csrftoken 29 | 30 | r = session.post(VGG_FACE_URL, data=credential) 31 | 32 | imagefiles = IMAGE_URL.split('=')[-1] 33 | 34 | with open(imagefiles, "wb") as files: 35 | print(f"Downloading the file: `{imagefiles}`") 36 | r = session.get(IMAGE_URL, data=credential, stream=True) 37 | bytes_written = 0 38 | for data in r.iter_content(chunk_size=400096): 39 | files.write(data) 40 | bytes_written += len(data) 41 | MegaBytes = bytes_written / (1024 * 1024) 42 | sys.stdout.write(f"\r{MegaBytes:0.2f} MiB downloaded...") 43 | sys.stdout.flush() 44 | 45 | print("\n Images are successfully downloaded. Exiting the process.") -------------------------------------------------------------------------------- /chapter8/Listing_8_3.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | tar xvzf vggface2_train.tar.gz 3 | tar xvzf vggface2_test.tar.gz -------------------------------------------------------------------------------- /chapter8/Listing_8_4.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | export PYTHONPATH=$PYTHONPATH:/content/facenet 4 | export PYTHONPATH=$PYTHONPATH:/content/facenet/src 5 | for N in {1..10}; do \ 6 | python facenet/src/align/align_dataset_mtcnn.py \ 7 | /content/train \ 8 | /content/train_aligned \ 9 | --image_size 182 \ 10 | --margin 44 \ 11 | --random_order \ 12 | --gpu_memory_fraction 0.10 \ 13 | & done -------------------------------------------------------------------------------- /chapter8/Listing_8_5.sh: -------------------------------------------------------------------------------- 1 | %tensorflow_version 1.x 2 | !export PYTHONPATH=$PYTHONPATH:/content/facenet/src 3 | !python facenet/src/train_tripletloss.py \ 4 | --logs_base_dir logs/facenet/ \ 5 | --models_base_dir /content/drive/'My Drive'/chapter8/facenet_model/ \ 6 | --data_dir /content/drive/'My Drive'/chapter8/train_aligned/ \ 7 | --image_size 160 \ 8 | --model_def models.inception_resnet_v1 \ 9 | --optimizer ADAGRAD \ 10 | --learning_rate 0.01 \ 11 | --weight_decay 1e-4 \ 12 | --max_nrof_epochs 10 \ 13 | --epoch_size 200 -------------------------------------------------------------------------------- /chapter8/Listing_8_6.sh: -------------------------------------------------------------------------------- 1 | %tensorflow_version 2.x 2 | %load_ext tensorboard 3 | %tensorboard --logdir /content/logs/facenet 4 | -------------------------------------------------------------------------------- /chapter8/Listing_8_7.sh: -------------------------------------------------------------------------------- 1 | python facenet/src/align/align_dataset_mtcnn.py \ 2 | ~/presidents/ \ 3 | ~/presidents_aligned \ 4 | --image_size 182 \ 5 | --margin 44 6 | -------------------------------------------------------------------------------- /chapter8/Listing_8_8.sh: -------------------------------------------------------------------------------- 1 | python facenet/src/classifier.py TRAIN \ 2 | ~/presidents_aligned \ 3 | ~/20180402-114759/20180402-114759.pb \ 4 | ~/presidents_aligned/face_classifier.pkl \ 5 | --batch_size 1000 \ 6 | --min_nrof_images_per_class 40 \ 7 | --nrof_train_images_per_class 35 \ 8 | --use_split_dataset 9 | -------------------------------------------------------------------------------- /chapter8/Listing_8_9.sh: -------------------------------------------------------------------------------- 1 | pip install pafy 2 | pip install youtube_dl 3 | -------------------------------------------------------------------------------- /chapter8/README.txt: -------------------------------------------------------------------------------- 1 | README.md 2 | 3 | The protobuf.pbtxt is required to execute the code in this chapter 8.This file is larger than 200MB and exceeds the maximum file size (100MB) allowed by github. 4 | Therefore, we are storing this file in a google drive folder. Please do the following: 5 | 1. Download the protobuf.pbtxt.zip file from Google drive location https://drive.google.com/file/d/1uHi5G615e_C1ts1JkekutsJ4T65nDWBt/view?usp=sharing 6 | 2. Uncompress the file 7 | 3. Copy the protobuf.pbtxt file in the directory chapter8 8 | -------------------------------------------------------------------------------- /chapter8/converter.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | import cv2 3 | import tensorflow as tf 4 | from tensorflow_core._api.v2.io import gfile 5 | from tensorflow_core.core.framework.graph_pb2 import GraphDef 6 | from tensorflow_core.python.framework.graph_io import write_graph 7 | 8 | print(tf.version.VERSION) 9 | model_path="/Users/sansari/Downloads/20180408-102900/" 10 | # # Model preparation 11 | def load_model(model_path): 12 | model_dir = pathlib.Path(model_path) 13 | model = tf.keras.models.load_model(str(model_dir)) 14 | print(model.signatures) 15 | # model = model.signatures['serving_default'] 16 | return model 17 | 18 | model = load_model(model_path) 19 | 20 | 21 | def convert_pb_to_pbtxt(filename): 22 | with gfile.GFile(filename,'rb') as f: 23 | graph_def = GraphDef() 24 | 25 | graph_def.ParseFromString(f.read()) 26 | 27 | tf.import_graph_def(graph_def, name='') 28 | 29 | write_graph(graph_def, './', 'protobuf.pbtxt', as_text=True) 30 | return 31 | 32 | # convert_pb_to_pbtxt("/Users/sansari/Downloads/20180408-102900/20180408-102900.pb") 33 | image = cv2.imread("/Users/sansari/Downloads/test_images/catotherpets.jpg") 34 | # The input needs to be a tensor, convert it using `tf.convert_to_tensor`. 35 | input_tensor = tf.convert_to_tensor(image) 36 | # The model expects a batch of images, so add an axis with `tf.newaxis`. 37 | input_tensor = input_tensor[tf.newaxis, ...] 38 | 39 | # Run prediction from the model 40 | output_dict = model(input_tensor) 41 | print(output_dict) -------------------------------------------------------------------------------- /chapter8/facenet.py: -------------------------------------------------------------------------------- 1 | 2 | # example of loading the keras facenet model 3 | import tensorflow.compat.v1 as tf 4 | from mtcnn import MTCNN 5 | 6 | tf.disable_v2_behavior() 7 | import cv2 8 | 9 | 10 | 11 | # load the model 12 | model = tf.keras.models.load_model('/Users/sansari/Downloads/keras-facenet/model/facenet_keras.h5') 13 | # summarize input and output shape 14 | print(model.inputs) 15 | print(model.outputs) 16 | 17 | image = cv2.imread("/User/sansari/Downloads/test_images/Egyptian_Mau_219.jpg") 18 | detector = MTCNN() 19 | # detect faces in the image 20 | results = detector.detect_faces(image) 21 | 22 | # extract the bounding box from the first face 23 | x1, y1, width, height = results[0]['box'] 24 | # bug fix 25 | x1, y1 = abs(x1), abs(y1) 26 | x2, y2 = x1 + width, y1 + height 27 | print(x1, x2, y1, y2) 28 | -------------------------------------------------------------------------------- /chapter9/Listing_9_1.sh: -------------------------------------------------------------------------------- 1 | # Code block 1: Mount Google Drive 2 | from google.colab import drive 3 | drive.mount('/content/drive') 4 | 5 | # Code block 2: uncompress NEU data 6 | %%shell 7 | ls /content/drive/'My Drive'/NEU-DET.zip 8 | unzip /content/drive/'My Drive'/NEU-DET.zip 9 | 10 | # Code block 3: Clone github repository of Tensorflow model project 11 | !git clone https://github.com/ansarisam/models.git 12 | 13 | # Code block 4: Install Google protobuf compiler and other dependencies 14 | !sudo apt-get install protobuf-compiler python-pil python-lxml python-tk 15 | 16 | # Code block 4: Install dependencies 17 | %%shell 18 | cd models/research 19 | pwd 20 | protoc object_detection/protos/*.proto --python_out=. 21 | pip install --user Cython 22 | pip install --user contextlib2 23 | pip install --user pillow 24 | pip install --user lxml 25 | pip install --user jupyter 26 | pip install --user matplotlib 27 | 28 | # Code block 5: Build models project 29 | %%shell 30 | export PYTHONPATH=$PYTHONPATH:/content/models/research:/content/models/research/slim 31 | cd /content/models/research 32 | python setup.py build 33 | python setup.py install 34 | -------------------------------------------------------------------------------- /chapter9/Listing_9_3.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | 4 | python /content/generic_xml_to_tf_record.py \ 5 | --label_map_path=/content/steel_label_map.pbtxt \ 6 | --data_dir=/content/NEU-DET \ 7 | --output_path=/content/NEU-DET/out \ 8 | --annotations_dir=ANNOTATIONS \ 9 | --image_dir=IMAGES -------------------------------------------------------------------------------- /chapter9/Listing_9_4.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | mkdir pre-trained-model 4 | cd pre-trained-model 5 | wget http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2018_01_28.tar.gz 6 | tar -xvf ssd_inception_v2_coco_2018_01_28.tar.gz -------------------------------------------------------------------------------- /chapter9/Listing_9_6.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | export PYTHONPATH=$PYTHONPATH:/content/models/research:/content/models/research/slim 4 | cd models/research/ 5 | PIPELINE_CONFIG_PATH=/content/pre-trained-model/ssd_inception_v2_coco_2018_01_28/steel_defect_pipeline.config 6 | MODEL_DIR=/content/neu-det-models/ 7 | NUM_TRAIN_STEPS=10000 8 | SAMPLE_1_OF_N_EVAL_EXAMPLES=1 9 | python object_detection/model_main.py \ 10 | --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ 11 | --model_dir=${MODEL_DIR} \ 12 | --num_train_steps=${NUM_TRAIN_STEPS} \ 13 | --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ 14 | --alsologtostderr -------------------------------------------------------------------------------- /chapter9/Listing_9_7.sh: -------------------------------------------------------------------------------- 1 | %%shell 2 | %tensorflow_version 1.x 3 | export PYTHONPATH=$PYTHONPATH:/content/models/research 4 | export PYTHONPATH=$PYTHONPATH:/content/models/research/slim 5 | cd /content/models/research 6 | 7 | python object_detection/export_inference_graph.py \ 8 | --input_type image_tensor \ 9 | --pipeline_config_path /content/pre-trained-model/ssd_inception_v2_coco_2018_01_28/steel_defect_pipeline.config \ 10 | --trained_checkpoint_prefix /content/neu-det-models/model.ckpt-10000 \ 11 | --output_directory /content/NEU-DET/final_model -------------------------------------------------------------------------------- /chapter9/Listing_9_8.sh: -------------------------------------------------------------------------------- 1 | %tensorflow_version 2.x 2 | %load_ext tensorboard 3 | %tensorboard --logdir /drive/'My Drive'/NEU-DET-models/ -------------------------------------------------------------------------------- /chapter9/generic_xml_to_tf_record.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | import hashlib 6 | import io 7 | import logging 8 | import os 9 | 10 | from lxml import etree 11 | import PIL.Image 12 | import tensorflow as tf 13 | 14 | from object_detection.utils import dataset_util 15 | from object_detection.utils import label_map_util 16 | import random 17 | 18 | flags = tf.app.flags 19 | flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.') 20 | 21 | flags.DEFINE_string('annotations_dir', 'annotations', 22 | '(Relative) path to annotations directory.') 23 | flags.DEFINE_string('image_dir', 'images', 24 | '(Relative) path to images directory.') 25 | 26 | flags.DEFINE_string('output_path', '', 'Path to output TFRecord') 27 | flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt', 28 | 'Path to label map proto') 29 | flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore ' 30 | 'difficult instances') 31 | FLAGS = flags.FLAGS 32 | 33 | # This function generates a list of images for training and validation. 34 | def create_trainval_list(data_dir): 35 | trainval_filename = os.path.abspath(os.path.join(data_dir,"trainval.txt")) 36 | trainval = open(os.path.abspath(trainval_filename), "w") 37 | files = os.listdir(os.path.join(data_dir, FLAGS.image_dir)) 38 | for f in files: 39 | absfile =os.path.abspath(os.path.join(data_dir, FLAGS.image_dir, f)) 40 | trainval.write(absfile+"\n") 41 | print(absfile) 42 | trainval.close() 43 | 44 | 45 | def dict_to_tf_example(data, 46 | dataset_directory, 47 | label_map_dict, 48 | ignore_difficult_instances=False, 49 | image_subdirectory=FLAGS.image_dir): 50 | """Convert XML derived dict to tf.Example proto. 51 | 52 | Notice that this function normalizes the bounding box coordinates provided 53 | by the raw data. 54 | 55 | Args: 56 | data: dict holding PASCAL XML fields for a single image 57 | dataset_directory: Path to root directory holding PASCAL dataset 58 | label_map_dict: A map from string label names to integers ids. 59 | ignore_difficult_instances: Whether to skip difficult instances in the 60 | dataset (default: False). 61 | image_subdirectory: String specifying subdirectory within the 62 | PASCAL dataset directory holding the actual image data. 63 | 64 | Returns: 65 | example: The converted tf.Example. 66 | 67 | Raises: 68 | ValueError: if the image pointed to by data['filename'] is not a valid JPEG 69 | """ 70 | filename = data['filename'] 71 | 72 | if filename.find(".jpg") < 0: 73 | filename = filename+".jpg" 74 | img_path = os.path.join("",image_subdirectory, filename) 75 | full_path = os.path.join(dataset_directory, img_path) 76 | 77 | with tf.gfile.GFile(full_path, 'rb') as fid: 78 | encoded_jpg = fid.read() 79 | encoded_jpg_io = io.BytesIO(encoded_jpg) 80 | image = PIL.Image.open(encoded_jpg_io) 81 | if image.format != 'JPEG': 82 | raise ValueError('Image format not JPEG') 83 | key = hashlib.sha256(encoded_jpg).hexdigest() 84 | 85 | width = int(data['size']['width']) 86 | height = int(data['size']['height']) 87 | 88 | xmin = [] 89 | ymin = [] 90 | xmax = [] 91 | ymax = [] 92 | classes = [] 93 | classes_text = [] 94 | truncated = [] 95 | poses = [] 96 | difficult_obj = [] 97 | if 'object' in data: 98 | for obj in data['object']: 99 | difficult = bool(int(obj['difficult'])) 100 | if ignore_difficult_instances and difficult: 101 | continue 102 | 103 | difficult_obj.append(int(difficult)) 104 | 105 | xmin.append(float(obj['bndbox']['xmin']) / width) 106 | ymin.append(float(obj['bndbox']['ymin']) / height) 107 | xmax.append(float(obj['bndbox']['xmax']) / width) 108 | ymax.append(float(obj['bndbox']['ymax']) / height) 109 | classes_text.append(obj['name'].encode('utf8')) 110 | classes.append(label_map_dict[obj['name']]) 111 | truncated.append(int(obj['truncated'])) 112 | poses.append(obj['pose'].encode('utf8')) 113 | 114 | example = tf.train.Example(features=tf.train.Features(feature={ 115 | 'image/height': dataset_util.int64_feature(height), 116 | 'image/width': dataset_util.int64_feature(width), 117 | 'image/filename': dataset_util.bytes_feature( 118 | data['filename'].encode('utf8')), 119 | 'image/source_id': dataset_util.bytes_feature( 120 | data['filename'].encode('utf8')), 121 | 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), 122 | 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 123 | 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 124 | 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), 125 | 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), 126 | 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), 127 | 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), 128 | 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 129 | 'image/object/class/label': dataset_util.int64_list_feature(classes), 130 | 'image/object/difficult': dataset_util.int64_list_feature(difficult_obj), 131 | 'image/object/truncated': dataset_util.int64_list_feature(truncated), 132 | 'image/object/view': dataset_util.bytes_list_feature(poses), 133 | })) 134 | return example 135 | 136 | def create_tf(examples_list, annotations_dir, label_map_dict, dataset_type): 137 | writer = None 138 | if not os.path.exists(FLAGS.output_path+"/"+dataset_type): 139 | os.mkdir(FLAGS.output_path+"/"+dataset_type) 140 | 141 | j = 0 142 | for idx, example in enumerate(examples_list): 143 | 144 | if idx % 100 == 0: 145 | logging.info('On image %d of %d', idx, len(examples_list)) 146 | print((FLAGS.output_path + "/tf_training_" + str(j) + ".record")) 147 | writer = tf.python_io.TFRecordWriter(FLAGS.output_path + "/"+dataset_type+"/tf_training_" + str(j) + ".record") 148 | j = j + 1 149 | 150 | path = os.path.join(annotations_dir, os.path.basename(example).replace(".jpg", '.xml')) 151 | 152 | with tf.gfile.GFile(path, 'r') as fid: 153 | xml_str = fid.read() 154 | xml = etree.fromstring(xml_str) 155 | data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation'] 156 | 157 | tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict, 158 | FLAGS.ignore_difficult_instances) 159 | writer.write(tf_example.SerializeToString()) 160 | 161 | def main(_): 162 | 163 | data_dir = FLAGS.data_dir 164 | create_trainval_list(data_dir) 165 | 166 | label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path) 167 | 168 | examples_path = os.path.join(data_dir,'trainval.txt') 169 | annotations_dir = os.path.join(data_dir, FLAGS.annotations_dir) 170 | examples_list = dataset_util.read_examples_list(examples_path) 171 | 172 | random.seed(42) 173 | random.shuffle(examples_list) 174 | num_examples = len(examples_list) 175 | num_train = int(0.7 * num_examples) 176 | train_examples = examples_list[:num_train] 177 | val_examples = examples_list[num_train:] 178 | 179 | create_tf(train_examples, annotations_dir, label_map_dict, "train") 180 | create_tf(val_examples, annotations_dir, label_map_dict, "val") 181 | 182 | if __name__ == '__main__': 183 | tf.app.run() 184 | -------------------------------------------------------------------------------- /chapter9/steel_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | id: 1 3 | name: 'rolled-in_scale' 4 | } 5 | 6 | item { 7 | id: 2 8 | name: 'patches' 9 | } 10 | 11 | item { 12 | id: 3 13 | name: 'crazing' 14 | } 15 | 16 | item { 17 | id: 4 18 | name: 'pitted_surface' 19 | } 20 | 21 | item { 22 | id: 5 23 | name: 'inclusion' 24 | } 25 | 26 | item { 27 | id: 6 28 | name: 'scratches' 29 | } 30 | -------------------------------------------------------------------------------- /errata.md: -------------------------------------------------------------------------------- 1 | # Errata for *Book Title* 2 | 3 | On **page xx** [Summary of error]: 4 | 5 | Details of error here. Highlight key pieces in **bold**. 6 | 7 | *** 8 | 9 | On **page xx** [Summary of error]: 10 | 11 | Details of error here. Highlight key pieces in **bold**. 12 | 13 | *** --------------------------------------------------------------------------------