2.2.1. Generic Image Classifier

The Generic Image Classifier is a class that can be used for creating user defined classifier.


  1. init

    1. modelName Path to model

    2. classNames List of class names

    3. resultFile default = preds.csv

    4. workDir default = tmp

    5. printRes default=True

  2. train

  1. baseModel: default=’InceptionV3’

  2. lr1: default=0.0001

  3. initial_epochs: default==10,

  4. fine_tune_at: default==300,

  5. lr2: default=0.00001,

  6. fine_tune_epochs: default==50,

  7. color_mode default: =’rgb’,

  8. horizontalFlip: default=False,

  9. verticalFlip: default=False,

  10. dropout: default=0.6

  11. randomRotation: default=0.0,

  12. callbacks: default==[],

  13. plot: default==True

  1. predict

    1. image: single image or list of images

    2. color_mode: default=’rgb’

  2. loadData

  1. imgDir:

  2. valimgDir: default=’’

  3. randomseed: default=1993,

  4. color_mode default=’rgb’,

  5. image_size default=(256, 256),

  6. batch_size: default = 32,

  7. split: default=[0.8,0.2]): Decription

This class implements the abstraction of an image classifier, it can be first used to train the classifier and save the data needed by the classifier locally. Once trained, the classifier can be used to predict the class of each image given a set of images. The user provides categorized images to the classifier so that it can be initially trained. Example

The following is an example, in which a classifier is created and trained.

The image dataset for this example contains street view images categorized according to construction materials.

The dataset can be downloaded here.

When unzipped, the file gives the ‘building_materials’ which is a directory that contains the images for training:

│── class_1
│       └── *.png
│── class_2
|      └── *.png
│── ...
└── class_n
       └── *.png Construct the image classifier

# import the module
from brails.modules import ImageClassifier

# initialize the classifier, give it a name
materialClassifier = ImageClassifier(modelName='materialClassifierV0.1')

# load data
materialClassifier.loadData('building_materials') Train the model

# train the base model for 50 epochs and then fine tune for 200 epochs
materialClassifier.train(baseModel='InceptionV3', initial_epochs=50,fine_tune_epochs=200)

It is recommended to run the above example on a GPU machine.

The following ML model training options are available for selection as the baseModel key:

'EfficientNetB7' Classify Images Based on Model

Now you can use the trained model to predict the (building materials) class for a given image.

# If you are running the inference from another place, you need to initialize the classifier firstly:
from brails.GenericImageClassifier import ImageClassifier
materialClassifier = ImageClassifier(modelName='materialClassifierV0.1')

# define the paths of images in a list
imgs = ['building_materials/concrete/469 VAN BUREN AVE Oakland2.jpg',
        'building_materials/masonry/101 FAIRMOUNT AVE Oakland2.jpg',
        'building_materials/wood/41 MOSS AVE Oakland2.jpg']

# use the model to predict
predictions = materialClassifier.predict(imgs)

The predictions will be written in preds.csv under the current directory.


The generic image classifier is intended to illustrate the overall process of model training and prediction. The classifier takes an image as the input and will always produce a prediction. Since the classifier is trained to classify only a specific category of images, its prediction is meaningful only if the input image belongs to the category the model is trained for.