Home > Net >  Object counting in tensorflow
Object counting in tensorflow

Time:09-17

i have a question. I want to create a deep learning image classification model in tensorflow for neglect detection like i have an image which will have right side empty or left side empty or no side empty. i will give input an image to model and model will classify image as left side emoty or left side empty or no side empty.

left side empty mean there is no object on left side right side empty mean there is no object on right side no side empty mean there is object on both side.

that object can be anything no matter it is circle or triangle or anything else, just needed it to be object. how i can do this in tensorflow? any guide? mean how to count objects on left and right side of image

Thanks

CodePudding user response:

If it is a classification model you do not need to worry about how to detect or count objects in the image. That's what deep learning does (probably Neural Network in your case). Read this to get an idea about image classification using tensorflow.

CodePudding user response:

What you are trying to do is image classification. In this case you have 3 classes lets call them rempty, lempty and noempty. Now create a directory structure of the form shown below

image_dir
---rempty
   ----image 0  # first image where right side is empty
   ----image 1  # second image where right side is empty
   ----
   --- image N  # Nth images where right side is empty
---lempty
   ----image 0  # first image where left side is empty
   ----image 1  # second image where left side is empty
   ----
   --- image K  # Kth images where left side is empty
---noempty
   ----image 0  # first image where no side is empty
   ----image 1  # second image where no side is empty
   ----
   --- image J  # Jth images where left side is empty

So now we will create a training set, a test set and a validation set. We will use data frames to create them

def preprocess (sdir, trsplit, vsplit, random_seed):
    filepaths=[]
    labels=[]    
    classlist=os.listdir(sdir)
    for klass in classlist:
        classpath=os.path.join(sdir,klass)
        flist=os.listdir(classpath)
        for f in flist:
            fpath=os.path.join(classpath,f)
            filepaths.append(fpath)
            labels.append(klass)
    Fseries=pd.Series(filepaths, name='filepaths')
    Lseries=pd.Series(labels, name='labels')
    df=pd.concat([Fseries, Lseries], axis=1)       
    # split df into train_df and test_df 
    dsplit=vsplit/(1-trsplit)
    strat=df['labels']    
    train_df, dummy_df=train_test_split(df, train_size=trsplit, shuffle=True, random_state=random_seed, stratify=strat)
    strat=dummy_df['labels']
    valid_df, test_df=train_test_split(dummy_df, train_size=dsplit, shuffle=True, random_state=random_seed, stratify=strat)
    print('train_df length: ', len(train_df), '  test_df length: ',len(test_df), '  valid_df length: ', len(valid_df))
    print(list(train_df['labels'].value_counts()))
    return train_df, test_df, valid_df

The above function creates the train, test and valid data frames. Now call the function and use them to create the train generator, the test generator and the validation generator

sdir=img_dir
train_df, test_df, valid_df= preprocess(sdir, .8,.1, 123)
img_size=(224,224) # set this to the desired image size
channels=3 # set to 3 for rgb images
length=len(test_df)
test_batch_size=sorted([int(length/n) for n in range(1,length 1) if length % n ==0 and length/n<=80],reverse=True)[0]  
test_steps=int(length/test_batch_size)
batch_size=30 # set this to the desired batch size
def scalar(img):    
    return img  # EfficientNet expects pixelsin range 0 to 255 so no scaling is required
gen=ImageDataGenerator(preprocessing_function=scalar)
train_gen=gen.flow_from_dataframe( train_df, x_col='filepaths', y_col='labels', target_size=img_size, class_mode='categorical',
                                    color_mode='rgb', shuffle=True, batch_size=batch_size)
test_gen=gen.flow_from_dataframe( test_df, x_col='filepaths', y_col='labels', target_size=img_size, class_mode='categorical',
                                    color_mode='rgb', shuffle=False, batch_size=test_batch_size)

valid_gen=gen.flow_from_dataframe( valid_df, x_col='filepaths', y_col='labels', target_size=img_size, class_mode='categorical',
                                    color_mode='rgb', shuffle=True, batch_size=batch_size)
classes=list(train_gen.class_indices.keys())
class_count=len(classes)

Now the train_gen and valid_gen can be used in model.fit. The test_gen can be used in model.evaluate or model.predict. A good model to use if shown below

base_model=tf.keras.applications.EfficientNetB2(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
x=base_model.output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
                bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)
x=Dropout(rate=.45, seed=123)(x)        
output=Dense(class_count, activation='softmax')(x)
model=Model(inputs=base_model.input, outputs=output)
model.compile(Adamax(lr=.001), loss='categorical_crossentropy', metrics=['accuracy']) 

Now train the model and evaluate the performance on the test set

epochs=10 # set this to desired epochs
history=model.fit(x=train_gen,  epochs=epochs, verbose=1, validation_data=valid_gen,
               validation_steps=None,  shuffle=False,  initial_epoch=0)
acc=model.evaluate( test_gen, verbose=1, steps=test_steps, return_dict=False)[1]*100
msg=f'accuracy on the test set is {acc:5.2f} %'
print(msg)

you need to import these modules

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Activation, Dropout, BatchNormalization
from tensorflow.keras.optimizers import Adam, Adamax
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras import regularizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import os
  • Related