In this python tutorial, we will go over how to build a drag and drop image classifier using Streamlit and Keras.
General Notes:
See my tutorial on Streamlit for details on how to get started.
For this tutorial, an Anaconda package environment was created and used with all packages needed.
Images in tutorial are from Wikipedia.
Many other models in addition to ResNet50 can be used (see Keras documentation for list).
The ImageNet project is a large visual database designed for use in visual object recognition software research. Source: Wikipedia
Some versions of packages and language may not be compatible. For examples shown in tutorial, python 3.7 and tensorflow 1.14.0 was used.
Keras can be accessed from TensorFlow or as standalone depending on the package and language versions being used however this may change.
Streamlit can be added to Anaconda package environment via terminal via the environment (pip, conda install, etc.) or add conda-forge channel and install with Anaconda Navigator GUI
If using Windows, make sure to change the path format for Windows. If path is copied from file properties, put in extra back slashes or make the path string a raw python f-string.
More Details on TensorFlow Keras Code
We use a series of steps to get the image data ready to make predictions including:
1. load image with target_size
2. img_to_array() converts a PIL Image instance to a Numpy array and returns a 3D Numpy array
3. np.expand_dims() converts image into 4 dimensional tensor (multi-dimensional arrays)
dimensions example = [batch, height, width, channels] (batch could be # of images)
4. preprocess_input()
caffe: will convert the images from RGB to BGR, then will zero-center each color channel with respect to the ImageNet dataset, without scaling (default).
tf: will scale pixels between -1 and 1, sample-wise.
torch: will scale pixels between 0 and 1 and then will normalize each channel with respect to the ImageNet dataset.
5. model.predict() returns Numpy array(s) of predictions
6. decode_predictions() decodes array(s) and returns class_name, class_description (prediction), score
Notes on How to Set Up Environment and Use with Terminal / Command Prompt, etc.
create Anaconda package environment with all packages needed (streamlit, tensorflow, pillow, etc.)
in terminal / command prompt type "conda activate python_tensorflow_keras_env" (change name to what you named the package environment) or just open terminal from the environment (click environment arrow, Open Terminal)
in terminal / command prompt change directory to location of python file (example - cd Desktop)
in terminal / command prompt type “streamlit run file_name.py” (whatever you named file - file_name with no spaces)