The code!

Imports

1–4 We import the libraries we will be using

Checking for a RAM backed temporary folder

6 The directory that the resized image will be put in to store it momentarily before it is deleted at the end of execution is stored here. It is set to /tmp by default as on most Linux distros /tmp is RAM backed so we get the faster read and write speeds of RAM and avoid unnecessary wear on the hard drive

7 This checks if the string tmpfs is in the output of the command df -T /tmp (this won’t be /tmp if tmp_dir has been changed) ie this checks whether or not tmp_dir is RAM backed

8–10 Let the user know the result of our check

Redefining bash

12 I break coding best practices and redefine bash instead of just calling it something different. This is done here as we only need bash to run commands for the rest of the program but needed to check the output of a command for our tmpfs check.

Loading the model

14 Does what it says on the tin

Image pre-processing

16 This is where the file path should be set, this is a piece of demo code that is mainly to prove the network works so this is a hard-coded value but feel free to change this to be user inputted

17–18 This removes any trailing slashes in file but I’d rather people just set file without a trailing slash in the first place

19 This compiles a command to resize file and save it to tmp_dir with the same filename as file

20 This redefines file to point towards the file in tmp_dir

21 This executes the command

Loading the image

23 This loads the image

24 Converts the image to a numpy array

25 Applies the data normalisation that was applied to images during training

Making the actual prediction

27 We make a prediction on img

29 If the largest value in the prediction is the first (zeroth to a computer)

30 Tells the user it is a cat and prints the exact value for our cat probability as a percentage

31–32 Same as cat but with dog so the second (1st to a computer) probability

33–39 Prints more or less all the variables as debug info, although this code should in theory never run

Cleaning up tmp_dir

41 We compile a command to delete the file

42 We execute the command