top of page

Hello

Home: Welcome

About Me

IMG_7861.JPG

Hi, welcome to my website! I’m not really sure what this is or should be; but hopefully it can be at least fun or interesting. I aim to make regular posts about things I find inspiring or technical things I hope I can remember in the future.

First, a little about me…


I am a professional physicist; I am a generalist with specialisms in all sorts. I am passionate about AI and I try to reflect on the use of AI in research and business. I also take a strong interest in the ways we run our projects and how we are influenced by the culture of work or research around us; I like to teach and to share knowledge and in part that explains this site.


I have spent the last decade in Durham, UK where I completed a BSc, Masters and PhD in theoretical physics. My interests are in the detection of dark matter, which sits somewhere between particle physics and cosmology. Although I have left academic research; I still maintain ties to the university. My tome of a thesis (500 pages!) was designed to be a useful textbook-like reference for students of dark matter, and it expands at length on my various published research topics.


The decision to leave academia wasn’t easy; but there is a world of skills and knowledge in industry which I was ready to tap. I started out as a software developer at a consultancy firm in Oxford; learning the ropes with C++ and Python and contributing as a developer to the Mantid codebase for muon beam analysis at Rutherford Appleton Labs. I moved back to Durham to join Kromek, a innovative R&D company who make detectors to work on X-ray security applications.


Now, I mostly spend my days running X-ray projects with a focus on AI. 

Home: About Me
Home: Blog2

Let’s Chat

Thanks for submitting!

DSCF2493.JPG
Home: Contact
  • Writer's pictureTom Jubb

Tensorflow Object Detection : Making Predictions

In this tutorial I will try to demonstrate how to run a trained Tensorflow object detection model for "inference", or in other words, to make predictions on new images. If you follow the steps in the previous post on this topic, you will have successfully run the training for an object detector.


The code in this post can be found on my GitHub.


In the previous example; we had set out a folder structure to store all the various parts of the model (data, config files, record files etc) in order to train it. I will maintain consistency with that post and so your trained model checkpoints should have appeared inside the `training/` folder. This is the structure we were using;

Once training is complete the training folder will be full of a load of new files. The files we are interested in are those marked as checkpoints, with the extension

where the number represents the number of iterations in the training. Generally, we want to go for the file with the highest number.


So what is an iteration (quick recap)?


A CNN works by passing "training examples" forward and backward through the network one at a time. In our case a training example is a single image with objects in. After a number of training examples have been passed, the gradients and loss are updated.


A lot of the CNN code in keras displays training in terms of "epochs"; one epoch being the training on the full dataset. Each epoch is split into a number of batches (where the user determines the batch size, usually 32 or 64); each time a single batch is passed through the training this is a single iteration.

So iterations are just the number of epochs multiplied by the (total number of images over the batch size). For example, if I have a batch size of 32 and 320 images then a single epoch is 10 iterations. Often the object detectors run into the hundreds of thousands of iterations.

Anyway; the trained model checkpoints should be available for the next step so make sure you have them. We are going to convert the checkpoint into a saved model format which can then be used for inference. Luckily, there is a script already in the API to do this which you can call from the terminal;

Now we need to convert the model checkpoint into an "inference graph". Open the Anaconda Prompt

These steps in words are:


  • Move to the root directory (object_detector_example in the folder structure above).

  • Shift to the environment we created to work with Tensorflow in the previous post.

  • Create a directory to store the inference graph.

  • Add the slim subfolder from the Tensorflow API to the path.

Now run the following command, replacing the folder names as appropriate

You should see plenty of output, and ultimately a folder called saved_model should appear in the inference folder (alongside some other files and gubbins). If you want to share your model; you only need to share this single folder, as we will see.

Now we have prepared the model for inference lets check out a Jupyter notebook that will give you a quick and easy way to apply the CNN to some images. This file comes from my GitHub; and is provided alongside a pre-trained model which you can download to test the notebook with (beware when downloading, the file is over 100Mb). This pre-trained model is designed simply to spot mobile phones in X-ray images.


The loading of the inference graph is done in one line;

Now this function can be used to make predictions using the model.

I have provided some simple code which wraps around this function so you can load jpg images and see the  result of the inference.

All of this can be run by downloading the relevant folder from my GitHub repo.

An example inference applied to one of the images supplied with the code

That's all! Hopefully with this information you can get your own detectors running; or if you've encountered a bug hopefully looking at all the steps above (and the previous posts in this series) will help you fix it.

 

44 views0 comments
  • linkedin
  • generic-social-link

©2020 by Thomas Jubb.

bottom of page