Giter Club home page Giter Club logo

practicaldl / practical-deep-learning-book Goto Github PK

View Code? Open in Web Editor NEW
722.0 28.0 305.0 336.22 MB

Official code repo for the O'Reilly Book - Practical Deep Learning for Cloud, Mobile & Edge

Home Page: http://practicaldeeplearning.ai

License: MIT License

HTML 0.05% Swift 0.02% Shell 0.01% Python 0.14% Jupyter Notebook 99.79%
deep-learning deep-learning-tutorial object-detection oreilly-books oreilly artificial-intelligence machine-learning cloud mobile edge

practical-deep-learning-book's Introduction

Practical Deep Learning for Cloud, Mobile, and Edge

This is the official code repository for the O'Reilly Publication,
Practical Deep Learning for Cloud, Mobile, and Edge
by Anirudh Koul, Siddha Ganju and Meher Kasam.


** Featured as a learning resource on the official Keras website **

[Online on Safari] | [Buy on Amazon] | [Online on Google Books] | [Book Website] | [Presentation on Slideshare]

Table of contents

Book Description

Whether you’re a software engineer aspiring to enter the world of deep learning, a veteran data scientist, or a hobbyist with a simple dream of making the next viral AI app, you might have wondered where do I begin? This step-by-step guide teaches you how to build practical deep learning applications for the cloud, mobile, browser, and edge devices using a hands-on approach.

Relying on years of industry experience transforming deep learning research into award-winning applications, Anirudh Koul, Siddha Ganju, and Meher Kasam guide you through the process of converting an idea into something that people in the real world can use.

  • Train, tune, and deploy computer vision models with Keras, TensorFlow, Core ML, and TensorFlow Lite
  • Develop AI for a range of devices including Raspberry Pi, Jetson Nano, and Google Coral
  • Explore fun projects, from Silicon Valley’s "Not Hotdog" app to 40+ industry case studies
  • Simulate an autonomous car in a video game environment and build a miniature version with reinforcement learning.
  • Use transfer learning to train models in minutes
  • Discover 50+ practical tips for maximizing model accuracy and speed, debugging, and scaling to millions of users

Chapter List

Chapter 1 - Exploring the Landscape of Artificial Intelligence | Read online | Figures

We take a tour of this evolving landscape, from 1950s till today, and analyze the ingredients that make for a perfect deep learning recipe, get familiar with common AI terminology and datasets, and take a peek into the world of responsible AI.

Chapter 2 - What’s in the Picture: Image Classification with Keras | Read online | Figures

We delve into the world of image classification in a mere five lines of Keras code. We then learn what neural networks are paying attention to while making predictions by overlaying heatmaps on videos. Bonus: we hear the motivating personal journey of François Chollet, the creator of Keras, illustrating the impact a single individual can have.

Chapter 3 - Cats versus Dogs: Transfer Learning in 30 Lines with Keras | Read online | Figures

We use transfer learning to reuse a previously trained network on a new custom classification task to get near state-of-the-art accuracy in a matter of minutes. We then slice and dice the results to understand how well is it classifying. Along the way, we build a common machine learning pipeline, which is repurposed throughout the book. Bonus: we hear from Jeremy Howard, co-founder of fast.ai, on how hundreds of thousands of students use transfer learning to jumpstart their AI journey.

Chapter 4 - Building a Reverse Image Search Engine: Understanding Embeddings | Read online | Figures

Like Google Reverse Image Search, we explore how one can use embeddings—a contextual representation of an image to find similar images in under ten lines. And then the fun starts when we explore different strategies and algorithms to speed this up at scale, from thousands to several million images, and making them searchable in microseconds.

Chapter 5 - From Novice to Master Predictor: Maximizing Convolutional Neural Network Accuracy | Read online | Figures

We explore strategies to maximize the accuracy that our classifier can achieve, with the help of a range of tools including TensorBoard, What-If Tool, tf-explain, TensorFlow Datasets, AutoKeras, AutoAugment. Along the way, we conduct experiments to develop an intuition of what parameters might or might not work for your AI task.

Chapter 6 - Maximizing Speed and Performance of TensorFlow: A Handy Checklist | Read online | Figures

We take the speed of training and inference into hyperdrive by going through a checklist of 30 tricks to reduce as many inefficiencies as possible and maximize the value of your current hardware.

Chapter 7 - Practical Tools, Tips, and Tricks | Read online | Figures

We diversify our practical skills in a variety of topics and tools, ranging from installation, data collection, experiment management, visualizations, keeping track of the state-of-the-art in research all the way to exploring further avenues for building the theoretical foundations of deep learning.

Chapter 8 - Cloud APIs for Computer Vision: Up and Running in 15 Minutes | Read online | Figures

Work smart, not hard. We utilize the power of cloud AI platforms from Google, Microsoft, Amazon, IBM and Clarifai in under 15 minutes. For tasks not solved with existing APIs, we then use custom classification services to train classifiers without coding. And then we pit them against each other in an open benchmark, you might be surprised who won.

Chapter 9 - Scalable Inference Serving on Cloud with TensorFlow Serving and KubeFlow | Read online | Figures

We take our custom trained model to the cloud/on-premises to scalably serve from tens to millions of requests. We explore Flask, Google Cloud ML Engine, TensorFlow Serving, and KubeFlow, showcasing the effort, scenario, and cost-benefit analysis.

Chapter 10 - AI in the Browser with TensorFlow.js and ml5.js | Read online | Figures

Every single individual who uses a computer or a smartphone uniformly has access to one software program—their browser. Reach all those users with browser-based deep learning libraries including TensorFlow.js and ml5.js. Guest author Zaid Alyafeai walks us through techniques and tasks such as body pose estimation, generative adversarial networks (GANs), image-to-image translation with Pix2Pix and more, running not on a server but in the browser itself. Bonus: Hear from TensorFlow.js and ml5.js teams on how the projects incubated.

Chapter 11 - Real-Time Object Classification on iOS with Core ML | Read online | Figures

We explore the landscape of deep learning on mobile, with a sharp focus on the Apple ecosystem with Core ML. We benchmark models on different iPhones, investigate strategies to reduce app size and energy impact, dynamic model deployment, training on device, and how professional apps are built.

Chapter 12 - Not Hotdog on iOS with Core ML and Create ML | Read online | Figures

Silicon Valley’s Not Hotdog app (from HBO) is considered the “Hello World” of mobile AI, so we pay tribute by building a real-time version in not one, not two, but three different ways.

Chapter 13 - Shazam for Food: Developing Android Apps with TensorFlow Lite and ML Kit | Read online | Figures

We bring AI to Android with the help of TensorFlow Lite. We then look at cross-platform development using ML Kit (which is built on top of TensorFlow Lite) and Fritz to explore the end-to-end development life cycle for building a self-improving AI app. Along the way we look at model versioning, A/B testing, measuring success, dynamic updates, model optimization, and other topics. Bonus: We get to hear about Pete Warden’s (technical lead for Mobile and Embedded TensorFlow) rich experience in bringing AI to edge devices.

Chapter 14 - Building the Purrfect Cat Locator App with TensorFlow Object Detection API | Read online | Figures

We explore four different methods for locating the position of objects within images. We take a look at the evolution of object detection over the years, and analyze the tradeoffs between speed and accuracy. This builds the base for case studies such as crowd counting, face detection, and autonomous cars.

Chapter 15 - Becoming a Maker: Exploring Embedded AI at the Edge | Read online | Figures

Guest author Sam Sterckval brings deep learning to low-power devices as he showcases a range of AI-capable edge devices with varying processing power and cost including Raspberry Pi, NVIDIA Jetson Nano, Google Coral, Intel Movidius, PYNQ-Z2 FPGA, opening the doors for robotics and maker projects. Bonus: Hear from the NVIDIA Jetson Nano team on how people are building creative robots quickly from their open-sourced recipe book.

Chapter 16 - Simulating a Self-Driving Car using End-to-End Deep Learning with Keras | Read online | Figures

Using the photorealistic simulation environment of Microsoft AirSim, guest authors Aditya Sharma and Mitchell Spryn guide us in training a virtual car by driving it first within the environment and then teaching an AI model to replicate its behavior. Along the way, this chapter covers a number of concepts that are applicable in the autonomous car industry.

Chapter 17 - Building an Autonomous Car in Under an Hour: Reinforcement Learning with AWS DeepRacer | Read online | Figures

Moving from the virtual to the physical world, guest author Sunil Mallya showcases how AWS DeepRacer, a miniature car, can be assembled, trained and raced in under an hour. And with the help of reinforcement learning, the car learns to drive on its own, penalizing mistakes and maximizing success. We learn how to apply this knowledge to races from the Olympics of AI Driving to RoboRace (using full-sized autonomous cars). Bonus: Hear from Anima Anandkumar (NVIDIA) and Chris Anderson (founder of DIY Robocars) on where the self-driving automotive industry is headed.

How to Use this Repository

First off, welcome! We are happy that you have decided to use the book and the code to learn more about Deep Learning! We wish you the best for your journey forward. Here are a few things to keep in mind while using the repository.

  • Code for each chapter is present in the code folder.
  • There is a respective README in each chapter that provides chapter-specific instructions on how to proceed with the code, and what data to download.

Please follow these instructions to load the GitHub repo on Google Colab. Keep in mind that you will need access to your own Google Drive as we will be using data from a local system.

Environment

We will use a virtualenv by the name of practicaldl throughout the book. The requirements.txt for this virtualenv are in the root directory. Help and instructions to install virtualenv are in the Installation section in the FAQ document.

Bug Reporting

Please file a issue according to CONTRIBUTING and we will investigate.

About the authors

@AnirudhKoul is a noted AI expert, UN/TEDx speaker and a former scientist at Microsoft AI & Research, where he founded Seeing AI, often considered the most used technology among the blind community after the iPhone. Anirudh serves as the Head of AI & Research at Aira, recognized by Time Magazine as one of the best inventions of 2018. With features shipped to a billion users, he brings over a decade of production-oriented Applied Research experience on PetaByte scale datasets. He has been developing technologies using AI techniques for Augmented Reality, Robotics, Speech, Productivity as well as Accessibility. His work in the AI for Good field, which IEEE has called ‘life-changing’, has received awards from CES, FCC, MIT, Cannes Lions, American Council of the Blind, showcased at events by UN, World Economic Forum, White House, House of Lords, Netflix, National Geographic, and lauded by world leaders including Justin Trudeau and Theresa May.

@SiddhaGanju, an AI researcher who Forbes featured in their 30 under 30 list, is a Self-Driving Architect at Nvidia. As an AI Advisor to NASA FDL, she helped build an automated meteor detection pipeline for the CAMS project at NASA, which ended up discovering a comet. Previously at Deep Vision, she developed deep learning models for resource constraint edge devices. Her work ranges from Visual Question Answering to Generative Adversarial Networks to gathering insights from CERN's petabyte-scale data and has been published at top-tier conferences including CVPR and NeurIPS. She has served as a featured jury member in several international tech competitions including CES. As an advocate for diversity and inclusion in technology, she speaks at schools and colleges to motivate and grow a new generation of technologies from all backgrounds.

@MeherKasam is a seasoned software developer with apps used by tens of millions of users every day. Currently an iOS developer at Square, and having previously worked at Microsoft and Amazon, he has shipped features for a range of apps from Square’s Point of Sale to the Bing iPhone app. Previously, he worked at Microsoft, where he was the mobile development lead for the Seeing AI app, which has received widespread recognition and awards from Mobile World Congress, CES, FCC, and the American Council of the Blind, to name a few. A hacker at heart with a flair for fast prototyping, he’s won several hackathons and converted them to features shipped in widely used products. He also serves as a judge of international competitions including Global Mobile Awards and Edison Awards.

Citation

Please cite us if you use our code.

@book{Koul2019PracticalDLBook,
  title={Practical Deep Learning for Cloud, Mobile and Edge: Real-World AI and Computer Vision Projects Using Python, Keras and TensorFlow},
  author={Koul, A. and Ganju, S. and Kasam, M.},
  isbn={9781492034865},
  url={https://www.oreilly.com/library/view/practical-deep-learning/9781492034858/},
  year={2019},
  publisher={O'Reilly Media, Incorporated}
}

practical-deep-learning-book's People

Contributors

koul avatar matthewtafazoli avatar meherkasam avatar meherkasam-square avatar muffinuser avatar practicaldl avatar sashagallaway avatar sidgan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

practical-deep-learning-book's Issues

chapter-4/1_feature_extraction.ipynb assumed running on colab

features = extract_features('cat.jpg', model)
is assuming it is run in colab. I suggest features = extract_features(IMG_PATH, model) so it also works locally off colab and uses IMG_PATH which was defined in the cell right before

IMG_PATH = '../../sample-images/cat.jpg'
if IS_COLAB_ENV:
  !curl https://raw.githubusercontent.com/PracticalDL/Practical-Deep-Learning-Book/master/sample-images/cat.jpg --output cat.jpg
  IMG_PATH = 'cat.jpg'

Same for the %timeit features = extract_features('cat.jpg', model) line, to change to %timeit features = extract_features(IMG_PATH, model)

'requirements.txt' pins number of libs that are either stale or not on PyPi

Before I open an issue, I have to admit that this is a great piece of work that cuts to the chase.
I got the book today. And, I already finished first chapter. It is the best intro I ever read on AI books. Koul Cool trick with Dr. May Carson 😉.

Issue:
Unless I missed something, the readers will have minor hiccup in installing the requirements.txt. I spent about half an hour, and I found some of the pinned packages are either not on PyPi or versions are stale. Please try steps below to reproduce:

  1. System running Ubuntu 16.04 LTS
  2. Python version 3.7.4
  3. Created venv > then activate it
  4. Then issued this command: pip install -r requirements.txt

Outcome:
Some packages will be either unable to install or can not be found in PyPi server (e.g. apturl).
There are few, but I mentioned one as example. It seems the requirement file may need some maintenance

Thanks.

chapter-4/1_feature_extraction.ipynb using default batch size 32 instead of defined batch_size 128

batch_size = 128
datagen = tensorflow.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=preprocess_input)

generator = datagen.flow_from_directory(root_dir,
                                        target_size=(224, 224),
                                        class_mode=None,
                                        shuffle=False)

uses default batch_size = 32.

num_epochs = int(math.ceil(num_images / batch_size))
calculates 68 epochs based on 128 batch size.
So generator only generates 68*32 = 2176 images for feature_list, mismatching the 8677 in standard_feature_list with features extracted from the non generator method.

Chapter 4: Fine tuned feature embeddings -> calculate from output or intermediate layer?

Congratulations on an amazing book. Excellent so far!

I noticed in the image search project in Chapter 4 that the feature embeddings for the out of the box ResNet50 model are calculated from an intermediate layer (GlobalMaxPooling2D) (None, 2048).

But after fine tuning based on our own dataset the features are calculated based on the final output layer and hence the feature dimensions become more like (1, 10) based on the number of classes in our dataset.

I'm not sure is this an error/oversight or if both methodologies are correct?
Would love to get your opinion on this. And if a fix is needed I can offer to help also.
Thanks again for the great book.
Niall

Bad inference after 70k step using faster_rcnn_resnet152_pets (model zoo)

Hello to every one, Am training a bird specie object detection. I have 7(with 525 image, 75 pictures per class) classes and using:

software and libraire

model:

  • faster_rcnn_resnet152_pets.config

Laptop:

  • OMEN 15 which have RTX graphic card (6Go) on Ubuntu 18.04.

The level:
loss:1.4247278, step: 75185
The loss still decreasing but slowly.

I started the training since yesterday and I stopped the training and continued at this morning at the saved checkpoint as you can know.
But at this stage, I saved and export the model.*-7108 to test test prediction, but i did not get prediction and no boxes was drawn, only if I decrease the treshold from 0.6 to 0.1, i get some false and true prediction.

My question is: does that means my model was not learned very well? or should I continue the training until I reach 200k step.

still

chapter-4/2-similarity-search-level-3.ipynb Accuracy calculation on 2048 dim is too slow

It is taking ~40 minutes on my CPU which is unbearable.

Why not use numpy?
class_ids was pickled but not used.

Here's the full code, takes < 1 second (i didn't wrap in function or multiply accuracy by 100)

class_ids = pickle.load(open('features/class_ids-caltech101.pickle', 'rb'))

neighbors = NearestNeighbors(n_neighbors=5,
                             algorithm='brute',
                             metric='euclidean').fit(feature_list_compressed)
    
distances, np_indices = neighbors.kneighbors(feature_list_compressed)

neighbors_class_ids = class_ids[np_indices]
class_equalities = np.equal(neighbors_class_ids[:, [0]], neighbors_class_ids[:, 1:5])
class_equalities.mean()

Invalid Argument Error

image
image
whille training or running this particular code snippet, showing this error.
Please help me out.

No Chapter 9 code

There is no chapter 9 code...what h5_to_tf.ipynb that is supposed to be in this repository under chapter-9 directory

How to build a flask app

i want to make a image search engine using deep learning and flask framework and try to load a pickle files but confused how to build a flask app to browse as a web based search engine.

Benchmarking on chapter4

Hi,
In the notebook 2-similarity-search-level-2.ipynb, would you recommend running on a subset of the features / classnames / filenames ?
The benchmarks takes a very long time, maybe it would be better to add some code to select a subpart of the files only.

chapter-4/2-similarity-search-level-1.ipynb - Plot a scatter plot from the generated t-SNE results

1.cant visualize this:

#color_map = plt.cm.get_cmap('coolwarm')
color_map = matplotlib.colormaps['coolwarm']
scatter_plot = plt.scatter(tsne_results[:, 0],
                           tsne_results[:, 1],
                           c=selected_class_ids,
                           cmap=color_map)
plt.colorbar(scatter_plot)
plt.show()
# To save the plot in a high definition format i.e. PDF, uncomment the following line:
#plt.savefig('results/' + str(ADD_NAME_HERE)+'.pdf', format='pdf', dpi=1000)

gives:

ValueError                                Traceback (most recent call last)
File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4439, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4438 try:  # Is 'c' acceptable as PathCollection facecolors?
-> 4439     colors = mcolors.to_rgba_array(c)
   4440 except (TypeError, ValueError) as err:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:487, in to_rgba_array(c, alpha)
    486 else:
--> 487     rgba = np.array([to_rgba(cc) for cc in c])
    489 if alpha is not None:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:487, in <listcomp>(.0)
    486 else:
--> 487     rgba = np.array([to_rgba(cc) for cc in c])
    489 if alpha is not None:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:299, in to_rgba(c, alpha)
    298 if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 299     rgba = _to_rgba_no_colorcycle(c, alpha)
    300     try:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:381, in _to_rgba_no_colorcycle(c, alpha)
    380 if not np.iterable(c):
--> 381     raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
    382 if len(c) not in [3, 4]:

ValueError: Invalid RGBA argument: 0.0

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[62], line 3
      1 #color_map = plt.cm.get_cmap('coolwarm')
      2 color_map = matplotlib.colormaps['coolwarm']
----> 3 scatter_plot = plt.scatter(tsne_results[:, 0],
      4                            tsne_results[:, 1],
      5                            c=selected_class_ids,
      6                            cmap=color_map)
      7 plt.colorbar(scatter_plot)
      8 plt.show()

File ~/.local/lib/python3.8/site-packages/matplotlib/pyplot.py:2862, in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, data, **kwargs)
   2857 @_copy_docstring_and_deprecators(Axes.scatter)
   2858 def scatter(
   2859         x, y, s=None, c=None, marker=None, cmap=None, norm=None,
   2860         vmin=None, vmax=None, alpha=None, linewidths=None, *,
   2861         edgecolors=None, plotnonfinite=False, data=None, **kwargs):
-> 2862     __ret = gca().scatter(
   2863         x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
   2864         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
   2865         edgecolors=edgecolors, plotnonfinite=plotnonfinite,
   2866         **({"data": data} if data is not None else {}), **kwargs)
   2867     sci(__ret)
   2868     return __ret

File ~/.local/lib/python3.8/site-packages/matplotlib/__init__.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1439 @functools.wraps(func)
   1440 def inner(ax, *args, data=None, **kwargs):
   1441     if data is None:
-> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1444     bound = new_sig.bind(ax, *args, **kwargs)
   1445     auto_label = (bound.arguments.get(label_namer)
   1446                   or bound.kwargs.get(label_namer))

File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4602, in Axes.scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
   4599 if edgecolors is None:
   4600     orig_edgecolor = kwargs.get('edgecolor', None)
   4601 c, colors, edgecolors = \
-> 4602     self._parse_scatter_color_args(
   4603         c, edgecolors, kwargs, x.size,
   4604         get_next_color_func=self._get_patches_for_fill.get_next_color)
   4606 if plotnonfinite and colors is None:
   4607     c = np.ma.masked_invalid(c)

File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4445, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4443 else:
   4444     if not valid_shape:
-> 4445         raise invalid_shape_exception(c.size, xsize) from err
   4446     # Both the mapping *and* the RGBA conversion failed: pretty
   4447     # severe failure => one may appreciate a verbose feedback.
   4448     raise ValueError(
   4449         f"'c' argument must be a color, a sequence of colors, "
   4450         f"or a sequence of numbers, not {c!r}") from err

ValueError: 'c' argument has 8677 elements, which is inconsistent with 'x' and 'y' with size 2176.

Chapter 3 predictions classifying 'dog' all the time.

I'm trying to figure out why the call to validation_generator.class_indicies is always showing a 1 next to dog. Regardless of whether I use a cat or dog. Prediction is also showing 2 float values rather than one. During training, my loss and accuracy are both looking good across the board (approx 97/98%), but when I proceed to test I get results similar to those shown below :

[[9.9989903e-01 1.0094591e-04]]
{'cat': 0, 'dog': 1}

I am using Tensorflow-gpu 2.2 under Ubuntu 18.04 Cuda 10.1. I originally was following with the code in the book, but then verified with your code on git and all seems to look ok to me. Does anything jump out at you as to what I am doing wrong?

I have attached a screenshot showing the test result.
DL-Cat-Dog-Screenshot

AnnoyIndex - Vector has wrong length

When trying to run the AnnoyIndex code of 2-similarity-search-level-2, I get the following error:

IndexError: Vector has wrong length (expected 2048, got 500)

Should the length of features passed be 2048 ?

Chapter2 - visualization.py fails on line 126

After using the instructions mentioned in https://github.com/PracticalDL/Practical-Deep-Learning-Book/tree/master/code/chapter-2 (updating the .json file) and running the script on my own environment. I get the following error:

2019-12-29 21:33:16.538110: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Traceback (most recent call last):
File "visualization.py", line 162, in
process_image(image_path, output_prefix + "_output.jpg")
File "visualization.py", line 126, in process_image heatmap = explainer.explain(data, model, "block5_conv3", class_index)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tf_explain/core/grad_cam.py", line 50, inexplain model, images, layer_name, class_index
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 457, in call result = self._call(*args, **kwds)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 503, in _call
self._initialize(args, kwds, add_initializers_to=initializer_map)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 408, in _initialize
*args, **kwds))
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 1848, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2150, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/function.py", line 2041, in _create_graph_function
capture_by_value=self._capture_by_value),
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 915, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/eager/def_function.py", line 358, in wrapped_fn
return weak_wrapped_fn().wrapped(*args, **kwds)
File "/home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages/tensorflow_core/python/framework/func_graph.py", line 905, in wrapper
raise e.ag_error_metadata.to_exception(e)
TypeError: in converted code:
relative to /home/ec2-user/anaconda3/envs/tensorflow_p36/lib/python3.6/site-packages:

tf_explain/core/grad_cam.py:102 get_gradients_and_filters  *
    grad_model = tf.keras.models.Model(
tensorflow_core/python/keras/engine/network.py:539 get_layer
    raise ValueError('No such layer: ' + name)

TypeError: must be str, not numpy.int64

requirements.txt

please add back the requirements.txt the various package used here have many conflicts if we just update to the latest version. I am having a lot of difficulty setting up to start working in chapter 5 for example

The gzip file cannot be unzipped

Firefox_Screenshot_2020-09-21T20-51-20 996Z

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

I tried all the ways but its not getting unzipped

ML Engine example: bad data preprocessing results in bad predictions

The image-to-json.py conversion script used in chapter 13 does not preprocess the data, instead leaving all inputs as values between 0-255. If used with the model from chapter 3, the predictions will be more or less random.

An easy solution is to convert the value to floating-point numbers. Here's the script I use:

import json
import numpy as np
import tensorflow as tf
from PIL import Image

model = tf.keras.models.load_model('cats_vs_dogs_01')
# The layer name of the input needs to be present in the JSON file.
input_layer_name = model.layers[0].name

img = Image.open('data/cats-vs-dogs/cat.9827.jpg')
# Because the values are floating-point, don't make the image to large
img.thumbnail((128, 128), Image.ANTIALIAS)
# Preprocess the data
data = np.asarray(img) / 255.
with open('cat_image.json', 'w') as fp:
    json.dump({input_layer_name: data.tolist()}, fp)

This script can now be used as such:

export AI_MODEL='your_model_name'
export AI_JSON_FILE='cat_image.json'
export AI_REGION='europe-west4'
gcloud ai-platform predict \
    --model $AI_MODEL \
    --json-instances $AI_JSON_FILE \
    --region $AI_REGION

chapter-5/1-develop-tool.ipynb

when train model (transfer learning)

# select the percentage of layers to be trained while using the transfer learning
# technique. The selected layers will be close to the output/final layers.
unfreeze_percentage = 0

learning_rate = 0.001

if training_format == "scratch":
    print("Training a model from scratch")
    model = scratch(train, val, learning_rate)
elif training_format == "transfer_learning":
    print("Fine Tuning the MobileNet model")
    model = transfer_learn(train, val, unfreeze_percentage, learning_rate)

i see messages like that:
Corrupt JPEG data: 65 extraneous bytes before marker 0xd9

i googled that and the problem is with corrupted images in dataset (cats and dogs). to fix that one needs to use code to clean dataset:

import os
num_skipped = 0
for folder_name in ("Cat", "Dog"):
    folder_path = os.path.join("PetImages", folder_name)
    for fname in os.listdir(folder_path):
        fpath = os.path.join(folder_path, fname)
        try:
            fobj = open(fpath, "rb")
            is_jfif = tf.compat.as_bytes("JFIF") in fobj.peek(10)
        finally:
            fobj.close()

        if not is_jfif:
            num_skipped += 1
            # Delete corrupted image
            os.remove(fpath)
print("Deleted %d images" % num_skipped)

before training.
see also - https://discuss.tensorflow.org/t/first-steps-in-keras-error/8049/11

but i have no idea how to clean it if i have dataset presented as tfrecords:

cats_vs_dogs-train.tfrecord-00000-of-00008
cats_vs_dogs-train.tfrecord-00001-of-00008
cats_vs_dogs-train.tfrecord-00002-of-00008
cats_vs_dogs-train.tfrecord-00003-of-00008
cats_vs_dogs-train.tfrecord-00004-of-00008
cats_vs_dogs-train.tfrecord-00005-of-00008
cats_vs_dogs-train.tfrecord-00006-of-00008
cats_vs_dogs-train.tfrecord-00007-of-00008

how you fix that and if is not will the model will be correct ?

chapter-2/1-predict-class MobileNetV3Small

chapter ends with MobileNetV2 example. i extend a bit using MobileNetV3, as it is a bit fresher.
but perfomance results shows strange things.

def predict2(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    model = tf.keras.applications.**MobileNetV2**()
    img_array = image.img_to_array(img)
    img_batch = np.expand_dims(img_array, axis=0)
    img_preprocessed = preprocess_input(img_batch)
    prediction = model.predict(img_preprocessed)
    print(decode_predictions(prediction, top=3)[0])

%timeit -r 3 predict2(IMG_PATH) 

MobileNetV2 gives 912 ms ± 5.02 ms per loop

while as MobileNetV3 gives 1.04 s ± 19.9 ms per loop
from tensorflow.keras.applications import MobileNetV3Small

def predict3(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    model = tf.keras.applications.MobileNetV3Small()
    img_array = image.img_to_array(img)
    img_batch = np.expand_dims(img_array, axis=0)
    img_preprocessed = preprocess_input(img_batch)    
    prediction = model.predict(img_preprocessed)
    #print(prediction)
    print(decode_predictions(prediction, top=3)[0]) 
%timeit -r 3 predict3(IMG_PATH) 

very strange. MobileNetV3Small should be faster.

Chapter 2 - Error: "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"

When using the Non-Colab-Code in a Jupiter Notebook I receive the following error:

"2020-01-11 20:34:01.993022: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-11 20:34:02.030317: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fee2133a110 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-01-11 20:34:02.030342: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
File "visualization.py", line 165, in
process_image(image_path, output_prefix + "_output.jpg")
File "visualization.py", line 128, in process_image
class_index, class_name, prob_value = get_predictions(img, model)
File "visualization.py", line 50, in get_predictions
return decode_predictions_modified(preds, top=1)
File "visualization.py", line 35, in decode_predictions_modified
CLASS_INDEX = json.load(open(fpath))
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/init.py", line 293, in load
return loads(fp.read(),
File "/Users/olgawillner/venv2/bin/../lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"

The Code for the Colab Workbook works completely fine. Does anyone have an idea why I receive the error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte"?

chapter-4/3-reduce-feature-length-with-pca.ipynb Wrong data used in annoy section

2 occurences of feature_list should be dataset instead, also 2048 should be 100.
These are defined earlier in

num_items = 100000
num_dimensions = 100
dataset = np.random.randn(num_items, num_dimensions)
dataset /= np.linalg.norm(dataset, axis=1).reshape(-1, 1)
annoy_training_time = []
annoy_test_time = []
annoy_trees = [
    1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300
]
for num_trees in annoy_trees:
    t = AnnoyIndex(2048)  # Length of item vector that will be indexed
    for i in range(num_images):
        feature = feature_list[i]
        t.add_item(i, feature)
    start_time = time.time()
    t.build(num_trees)  #50 trees
    end_time = time.time()
    annoy_training_time.append(end_time - start_time)
    start_time = time.time()
    indices = t.get_nns_by_vector(feature_list[random_image_index],
                                  5,
                                  include_distances=True)
    end_time = time.time()
    annoy_test_time.append(end_time - start_time)
    print("For number of trees = ", num_trees, ",\tTime to train = ",
          annoy_training_time[-1], ",\tTime to test = ", annoy_test_time[-1])

ch14 generate_tfRecord.py error

receiving this error message in ch14 generate_tfRecord.py

File "generate_tfRecord.py", line 20, in
flags = tf.app.flags
AttributeError: module 'tensorflow' has no attribute 'app'

Ch-6 Store as TF Records

For example on creating TFRecords - Consider adding the following functions to store the packed BytesList and Int64List as per the documentation to assist the reader, throws an error otherwise.

def _int64_feature(value):
  return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

def _bytes_feature(value):
  return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

Code Example of AutoKeras throws a TypeError

If I execute the same code as mentioned in the notebook https://github.com/PracticalDL/Practical-Deep-Learning-Book/blob/master/code/chapter-5/5-autokeras.ipynb, then for the line
from autokeras.image.image_supervised import ImageClassifier ,
it is throwing the following error
ModuleNotFoundError: No module named 'autokeras.image'

And if I import just
from autokeras import ImageClassifier ,
then while executing the file, it is throwing an error saying
TypeError: __init__() got an unexpected keyword argument 'path'
on the following part of the code
clf = ImageClassifier(path=".",verbose=True, augment=True, \ searcher_args={'trainer_args':{'max_iter_num':7}} )

Chapter 2 part 2 sample code


AttributeError Traceback (most recent call last)
in
6
7 from tensorflow.keras.applications.vgg16 import VGG16, preprocess_input
----> 8 from tf_explain.core.grad_cam import GradCAM
9
10 import PIL

c:\python\lib\site-packages\tf_explain_init_.py in
8 version = "0.2.1"
9
---> 10 from . import core
11 from . import callbacks
12 from . import utils

c:\python\lib\site-packages\tf_explain\core_init_.py in
5 """
6 from .activations import ExtractActivations
----> 7 from .grad_cam import GradCAM
8 from .gradients_inputs import GradientsInputs
9 from .vanilla_gradients import VanillaGradients

c:\python\lib\site-packages\tf_explain\core\grad_cam.py in
10
11
---> 12 class GradCAM:
13
14 """

c:\python\lib\site-packages\tf_explain\core\grad_cam.py in GradCAM()
90
91 @staticmethod
---> 92 @tf.function
93 def get_gradients_and_filters(model, images, layer_name, class_index):
94 """

AttributeError: module 'tensorflow' has no attribute 'function'

Chapter 2: OperatorNotAllowedInGraphError in colabs

Hi,

This happened when running the 2-colab-what-does-my-neural-network-think.ipynb notebook in colabs.

When tensorflow 2.0 is installed in step 2 (!pip install tensorflow==2.0.0), it seems like you have to restart the runtime and run the code again. That is not entirely clear in the notebook but you can see a notice at the end of the installation.

If you do not restart, the process_image give and errorOperatorNotAllowedInGraphError(See https://stackoverflow.com/questions/57888872/how-to-fix-operatornotallowedingrapherror-error-in-tensorflow-2-0).

Not sure but maybe the code or instructions need an update to make this work from scratch. Hope this helps.

Seems like there is an issue reported in tensorflow 2.0, see tensorflow/tensorflow#32546.

The issue mentions also a gist which reproduces the exact same error, see https://colab.research.google.com/gist/oanush/0983d6584c1c687c0456fff142db4ad4/32546.ipynb

Ch5-1-develop-tool.ipynb notebook throws error when running get_dataset() method

I have not made any changes to the code but when I run the below code

train, info_train, val, info_val, IMG_H, IMG_W, IMG_CHANNELS, NUM_CLASSES, NUM_EXAMPLES = get_dataset(dataset_name, 100)

I am getting an error saying
AssertionError: Unrecognized instruction format: NamedSplit('train')(tfds.percent[0:90])

Please help. Thank You

CLASS_INDEX_PATH should take URL instead of relative path

Issue

https://github.com/PracticalDL/Practical-Deep-Learning-Book/blob/master/code/chapter-2/visualization.py

Run the following command to run the program:
python visualization.py --process image --path ../../sample-images/dog.jpg

The program will fail if you are running this locally (not on Colab) for the first time:

data_utils.py", line 251, in get_file
    urlretrieve(origin, fpath, dl_progress)
  File "/usr/lib/python3.7/urllib/request.py", line 247, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.7/urllib/request.py", line 510, in open
    req = Request(fullurl, data)
  File "/usr/lib/python3.7/urllib/request.py", line 328, in __init__
    self.full_url = url
  File "/usr/lib/python3.7/urllib/request.py", line 354, in full_url
    self._parse()
  File "/usr/lib/python3.7/urllib/request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '../imagenet_class_index.json'


Code
https://github.com/PracticalDL/Practical-Deep-Learning-Book/blob/master/code/chapter-2/visualization.py

CLASS_INDEX_PATH = '../imagenet_class_index.json'
...
 if CLASS_INDEX is None:
        fpath = get_file('imagenet_class_index.json',
                         CLASS_INDEX_PATH, cache_subdir='models')
        CLASS_INDEX = json.load(open(fpath))

Issue
TensorFlow's get_file() API lists the second parameter as URL not as relative path: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/keras/utils/data_utils.py#L164

Under the hood, it uses following code:

try:
        urlretrieve(origin, fpath, dl_progress)
      except HTTPError as e:
        raise Exception(error_msg.format(origin, e.code, e.msg))

The following one-liner will fix the issue:
CLASS_INDEX_PATH = 'https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json'

Note

  1. If someone runs this example once using the one-liner fix, the current code as is would work because it uses cached data imagenet_class_index.json. Probably that is how the bug snuck in.

  2. Easy fix, but I could not make pull request due to permission. You can technically enable PR by protecting your branch. This way any merge will be blocked until code owners approve the PR. This will help you to keep the code up-to-date by leveraging external PR. Just food for thought.

chapter-4/2-similarity-search-level-1.ipynb Broken float division

def plot_images(filenames, distances):
    images = []
    for filename in filenames:
        images.append(mpimg.imread(filename))
    plt.figure(figsize=(20, 10))
    columns = 4
    for i, image in enumerate(images):
        ax = plt.subplot(len(images) / columns + 1, columns, i + 1)

ax = plt.subplot(len(images) / columns + 1, columns, i + 1) should be ax = plt.subplot(len(images) // columns + 1, columns, i + 1), subplot requires integers for specifying number of rows/cols

Predict chapter 2

chapter 2 shows the dog with code
predict(img)

the github code doesn't include this line, but if we implement the code the following error is received.

NameError: name 'predict' is not defined

OOM on P100 GPU for chapter-2 video viz

Hi, when trying to run the script to convert the video frames to heatmaps, I get the following OOM error on a P100 GPU:

tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[25088,4096] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc [Op:Add] name: fc1_24/kernel/Initializer/random_uniform/

Not sure why it happens, since it is just one image..
Seems memory previously allocated doesn't get deallocated.
Any recommendation ? Using tensorflow 2.0 inside Docker.

By the way, looks like a great book :)

tqdm notebook issues

Getting this message when running the code ch4-1
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:6: TqdmDeprecationWarning: This function will be removed in tqdm==5.0.0
Please use tqdm.notebook.tqdm instead of tqdm.tqdm_notebook

the model doesn't seem to run with the current tqdm, i changed to tqdm.notebook.tqdm and it errors out. if i find a way to get through this I will post.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.