"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

December 31, 2019

Day #308 - Display on Rasberry PI

Finally got a 2.8 Inch LCD display for Rasberry PI. Next step is to experiment with the models with real-time situations. This LCD will help to visualize the output. Package installation commands and reference.

#https://www.waveshare.com/wiki/2.8inch_RPi_LCD_(A)
pi Display
============
git clone https://github.com/waveshare/LCD-show.git
cd LCD-show/
/LCD28-show
To Get back to monitor display
================================
cd LCD-show/
./LCD-hdmi
Virtual Keyboard
===================
#https://raspberrypi.stackexchange.com/questions/41150/virtual-keyboard-activation
sudo apt-get install matchbox-keyboard
sudo reboot
Menu >> Accessories >> Keyboard
view raw RPI_Display.txt hosted with ❤ by GitHub
Happy Learning!!!

December 30, 2019

Social Media Responsibilities

How do we measure social media impact? What are the pros and cons of Social Media
Pros
  • Information Sharing
  • Connect with a larger set of population
Cons
  • Sharing information without Authenticity
  • Motive / Authenticity of people sharing information
  • Smiles / Selfies vs Reality of Life
  • Manipulative / Biased / Personalized targeted ads
  • Exploit human tendencies of feedback / Sensitized to bias 
Social Media Accountability
  • Freedom of speech vs Hurting Sentiments
  • Consequences of blackmail/threat/bullying through social media contacts
  • Consequences of any form of violence instigated through messages
  • Validating the facts/claims shared?
  • Endorsing political advertisement claims without a moral stand?
  • Biased targeting of users?
  • Depression / Suicide due to excess usage of social media?
  • Freedom of speech vs Authenticity of speech vs Intentions of information shared?





When we can’t even agree on what is real


Keep Thinking!!!

December 26, 2019

Analyzing top 25 AI companies in 2019

Analyzing top 25 AI companies listed in Link




Happy Learning AI Landscape!!!

December 24, 2019

Data Analysis vs Forensic Science

Before AI / BI it's about #exploring the data to uncover the #DataInsights. #DataAnalysis is similar to #ForensicScience. Side by Side comparison of both perspectives.


#Data and #Insights sets the #direction for successful #AI / #BI usecases #datascience #bigdata #analytics

Happy Learning!!!

December 23, 2019

Difference between SQL and NOSQL Systems

Reposting from my two-year-old Quora answer

The Key differences between them lies in the understanding CAP theorem
  • Consistency
  • Availability
  • Partition Tolerance
In layman terms. SQL systems ex-RDBMS will adhere ACID properties (Atomicity, Consistency, Isolation, Durability).
  • The datatypes, schema are predefined, You cannot store non-matching datatypes
  • To avoid dirty data, systems enforce isolation levels that govern only committed data is read (Consistency)
  • Only latest records are available, records at that point in time are not available
  • Banking Systems, ordering systems where data needs to consistent will be mostly SQL based systems where consistency is important
No-SQL systems (Not Only SQL)
  • The schema is not tightly governed, its flexible you can store different datatypes in same columns
  • These may be geographically distributed where data may be synced and eventually be consistent end of day not realtime
  • They also support point in time data, data values at a point in time can also be looked up
  • Where there is no requirement for consistency we can achieve other 2 Availability and partition tolerance
  • Since some of the ACID properties are compromised you will have high availability of this systems
It is more to do with business need to decided SQL or SQL based storage.

Happy Learning!!!

December 17, 2019

Improving Women Safety

To reduce crime against women more than strengthening laws we need to get to the root cause of issues. We need to analyze the crime data and fix the source of the problem.

We need to analyze the crime patterns based on different aspects to find the underlying patterns.

Pattern vs Solutions
  • Correlation with alcohol - How to reduce/limit alcohol consumption 
  • Correlation with education - How to reduce dropouts and improve education
  • Correlation with income category - Sustainable jobs
  • Correlation with marital status - Family aspects
  • Correlation to caste - Driven by caste / Unemployment / Dropouts
  • Correlation to age group - Social media, porn impact
  • Correlation to social behavior - Drugs / partying / Addiction
  • Correlation to job type - Government vs Private jobs vs Daily vs Organized Crimes
Education is not limited to a few years. Education is not about degrees. The real purpose of education is to unlearn and relearn things from morality/humanity perspective.

It needs complete society change, not just laws. Let's prepare a safer tomorrow by making the required changes. 

Keep Questioning!!!

December 16, 2019

Day #307 - Porting Keras to Tensorflow Lite Version

Next Task is to run all the developed models in Pi using Tensorflow Lite. I am using google colab to convert the models into lite version.

#Step 1
from google.colab import drive
drive.mount('/content/drive')
#Upload data to drive in directory ColabData
#Step 2
import tensorflow as tf
print(tf.__version__)
#tensorflow.contrib is not there in 2.0
#!pip install tensorflow==2.0.0
#Downgrade tensorflow to 1.13.2
#!pip install tensorflow==1.13.2
#Step 3
from tensorflow.contrib import lite
converter = tf.lite.TFLiteConverter.from_keras_model_file(r'/content/drive/My Drive/ColabData/model_landmark_vgg16.h5')
tflite_model = converter.convert()
open(r'/content/drive/My Drive/ColabData/model.tflite','wb').write(tflite_model)
The ported models we will attempt to run in Rasberry PI as next steps

Happy Learning!!!

Day#306 - Express the SQL in pandas, TSQL in Pandas

I wanted to mimic joins, aggregation, sum whatever we do in Database with pandas. A simple storyline of Data Analysis between Employee, Department and Salary using pandas dataframes.


import pandas as pd
#Define Data Frames
Employee = {'name': ['Raj', 'Siva', 'Mike', 'Gopi','New_Joinee'],
'age': [22,38,26,35,22]}
dfEmployee = pd.DataFrame(Employee)
print(dfEmployee)
#Salary
Salary = {'name': ['Raj', 'Siva', 'Mike', 'Gopi','Raj', 'Siva'],
'salary': [2200,3800,2600,3500,7000,5000],
'Month': ['Jan','Feb','Jan','Jan','Feb','Mar']
}
dfSalary = pd.DataFrame(Salary)
print(dfSalary)
#Department
Department = {'name': ['Raj', 'Siva', 'Mike', 'Gopi','NOCODE'],
'dept': ['IT','AI','HR','DB','NOCODE']}
dfDepartment = pd.DataFrame(Department)
print(dfDepartment)
#Inner Join, Employee and Dept
print('Outer Join')
print(pd.merge(dfEmployee, dfDepartment, on='name', how='outer'))
#Left Join
print('Left Join')
print(pd.merge(dfEmployee, dfDepartment, on='name', how='left'))
#Right Join
print('Right Join')
print(pd.merge(dfEmployee, dfDepartment, on='name', how='right'))
#Inner Join
print('Inner Join')
print(pd.merge(dfEmployee, dfDepartment, on='name', how='inner'))
#Group by
#Total Salary Group by Employee
#Do a join
salarydata = pd.merge(dfEmployee, dfSalary, on='name', how='inner')
print(salarydata)
print('Total Paid by Employee')
#Perform Group By
print(salarydata.groupby(['name']).sum())
#Sum
#Total Salary Paid
salarydata = pd.merge(dfEmployee, dfSalary, on='name', how='inner')
Total = salarydata['salary'].sum()
print('Total Salary Paid')
print(Total)
#Min
#Min Salary paid by employee
MinimumSalary = salarydata['salary'].min()
print('MinimumSalary')
print(MinimumSalary)
#Max
#Max Salary paid by employee
MaxSalary = salarydata['salary'].max()
print('MaxSalary')
print(MaxSalary)
view raw Pandas_Joins.py hosted with ❤ by GitHub
Everything can be done in SQL. This is a different approach to it using pandas.

Happy Learning!!!

Day #305 - Loading from Weights file HDF, saved models H5 files

We will look at
  • Vanilla Model
  • Load preexisting weights HDF5 and Continue
  • Load preexisting model H5 and Continue

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, CSVLogger, EarlyStopping
import os
batch_size = 128
num_classes = 10
epochs = 5
log_file_path = r'E:\Landmark\mnist_training_log.log'
model_save_path = r"E:\Landmark\\mnist.h5"
weights_filepath="E:\\Landmark\\mnist-weights-improvement-{epoch:02d}.hdf5"
pre_weights_path = "E:\\Landmark\\mnist-weights-improvement-04.hdf5"
pre_model_h5_path = "E:\\Landmark\\mnist.h5"
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
from keras.models import load_model
def CreateModel():
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
return model
def LoadModelfromH5(model_h5_path):
if os.path.exists(model_h5_path):
print('Loading Definitions')
model = load_model(model_h5_path)
return model
def LoadModelWeights(pre_weights_path):
model = CreateModel()
model.load_weights(pre_weights_path)
return model
#Option 1
#model = CreateModel()
#Option 2
#model = LoadModelWeights(pre_weights_path)
#Option#3
model = LoadModelfromH5(pre_model_h5_path)
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
#Add Early Stop and Checkpoint
early_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
checkpoint = ModelCheckpoint(weights_filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
csv_logger = CSVLogger(log_file_path, append=False)
callbacks_list = [checkpoint,early_stop,csv_logger]
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test), callbacks=callbacks_list)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save(model_save_path)
import pandas as pd
import matplotlib.pyplot as plt
# Plot the Loss
file_name = log_file_path
df = pd.DataFrame.from_csv(file_name)
print(df.head())
training_loss = df['loss']
test_loss = df['val_loss']
print(training_loss)
print(test_loss)
epoch_count = range(1, len(training_loss) + 1)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show();
Results

Option #1 - Vanilla Model
Option #2 - Continue from Saved Weights
Option #3 - Continue from Saved Model H5 File


Happy Learning!!!

Day #304 - Analysis of Deep Fashion Dataset - LandmarkDetection

Three different poses

8 localization points only 4 is not null in all columns
Different visibility value options (0,1,2) - visibility: v=2 visible; v=1 occlusion; v=0 not labeled

One model for each category we need to do
The top-level has 3 generic categories:
  • 1: “top” (upper-body clothes such as jackets, sweaters, tees, etc.)
  • 2: “bottom” (lower-body clothes such as jeans, shorts, skirts, etc.)
  • 3: “long” (full-body clothes such as dresses, coats, robes, etc.)
The implementation is defined in paper - Link
Data - Link

Architecture Implementation

Data Analysis of the Dataset for Non-Zero Columns

  • image_name                   0
  • landmark_visibility_1        0
  • landmark_location_x_1        0
  • landmark_location_y_1        0
  • landmark_visibility_2        0
  • landmark_location_x_2        0
  • landmark_location_y_2        0
  • landmark_visibility_3        0
  • landmark_location_x_3        0
  • landmark_location_y_3        0
  • landmark_visibility_4        0
  • landmark_location_x_4        0
  • landmark_location_y_4        0
  • landmark_visibility_5    30972
  • landmark_location_x_5    30972
  • landmark_location_y_5    30972
  • landmark_visibility_6    30972
  • landmark_location_x_6    30972
  • landmark_location_y_6    30972
  • landmark_visibility_7    73003
  • landmark_location_x_7    73003
  • landmark_location_y_7    73003
  • landmark_visibility_8    73003
  • landmark_location_x_8    73003
  • landmark_location_y_8    73003

Non-zero columns

  • landmark_visibility_1        0
  • landmark_location_x_1        0
  • landmark_location_y_1        0
  • landmark_visibility_2        0
  • landmark_location_x_2        0
  • landmark_location_y_2        0
  • landmark_visibility_3        0
  • landmark_location_x_3        0
  • landmark_location_y_3        0
  • landmark_visibility_4        0
  • landmark_location_x_4        0
  • landmark_location_y_4        0

Happy Learning!!!

Day #303 - Model Training Guidelines - Part II

Here we will look at two more additions on top of the previous post 
  • Save model h5 file after every run/epoch 
  • Add Data batching to run in smaller iterations, Leverage Sequencer

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, CSVLogger, EarlyStopping
import os
import numpy as np
import math
batch_size = 128
num_classes = 10
epochs = 5
log_file_path = r'E:\Landmark\mnist_training_log.log'
model_checkpoint_path = r"E:\Landmark\\mnist.h5"
model_save_path = r"E:\Landmark\\mnist_model_{}.hd5.h5"
weights_filepath="E:\\Landmark\\mnist-weights-improvement-{epoch:02d}.hdf5"
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#Data Batching
class Generator(keras.utils.Sequence):
# Class is a dataset wrapper for better training performance
def __init__(self, x_set, y_set, batch_size, datacount):
self.x = x_set
self.y = y_set
self.batch_size = batch_size
self.indices = np.arange(self.x.shape[0])
self.idx = 0
self.datacount = datacount
def __len__(self):
print('length')
print(math.ceil(self.datacount/ self.batch_size))
return math.ceil(self.datacount/ self.batch_size)
def __getitem__(self, idx):
print('idx')
print(idx)
i1 = idx*self.batch_size
i2 = (idx+1)*self.batch_size
print('Start-' + str(i1) + '- End-' + str(i2))
if(i2 > self.datacount):
i2 = self.datacount
batch_x = self.x[i1:i2]
batch_y = self.y[i1:i2]
return batch_x, batch_y
def on_epoch_end(self):
np.random.shuffle(self.indices)
#Save Model after every Epoch
#https://stackoverflow.com/questions/54323960/save-keras-model-at-specific-epochs
class CustomSaver(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
#if epoch == 2: # or save after some epoch, each k-th epoch etc.
self.model.save(model_save_path.format(epoch))
batch_size = 500
print('x_train')
print(len(x_train))
print('x_test')
print(len(x_test))
training_generator = Generator(x_train, y_train, batch_size, len(x_train))
validation_generator = Generator(x_test, y_test, batch_size, len(x_test))
from keras.models import load_model
#Load and Continue Training
# load weights if it exists
if os.path.exists(model_checkpoint_path):
print('Loading Definitions')
model = load_model(model_checkpoint_path)
else:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
#Add Early Stop and Checkpoint
early_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
checkpoint = ModelCheckpoint(weights_filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
csv_logger = CSVLogger(log_file_path, append=False)
saver = CustomSaver()
callbacks_list = [checkpoint,early_stop,csv_logger,saver]
model.fit_generator(training_generator, validation_data = validation_generator, epochs = 10, callbacks=callbacks_list)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save(model_save_path)
import pandas as pd
import matplotlib.pyplot as plt
# Plot the Loss
file_name = log_file_path
df = pd.DataFrame.from_csv(file_name)
print(df.head())
training_loss = df['loss']
test_loss = df['val_loss']
print(training_loss)
print(test_loss)
epoch_count = range(1, len(training_loss) + 1)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show();

This is a template code. This can be customized for larger datasets


Happy Learning!!!

December 15, 2019

Project Learning Notes

Tracking, Counting has always been quite interesting topic for sometime. Explored this codebase link

#https://github.com/poojavinod100/People-Counting-Crowd-Density-Detection/blob/master/people_counter.py
#pip install imutils
#pip install CMake
#pip install dlib
python people_counter.py --prototxt mobilenet_ssd/MobileNetSSD_deploy.prototxt --model mobilenet_ssd/MobileNetSSD_deploy.caffemodel --output output/webcam_output.avi
view raw commands.txt hosted with ❤ by GitHub

I liked the approach of directionality based tracking. This is very needed for directionality based counting. Hoping to reuse / implement it in people counting scenarios.


My perspective is

  • Tracking by Sampling Frames (Reduce Load)
  • Use Euclidean and other attributes to track/match
  • Evaluate existing tracking built in OpenCV (Again these need frame by frame tracking)

Happy Learning!!!

December 13, 2019

Day #302 - Keras Best Practices during Training

In this post we take the raw version of code and add below features in code
  • Adding Checkpoint
  • Adding Logging
  • Plot Results
  • Restart Training from Checkpoint
  • Early Stopping
#Base code - https://keras.io/examples/mnist_cnn/
#Added Features
#========================
# Adding Checkpoint
# Adding Logging
# Plot Results
# Restart Training from Checkpoint
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.callbacks import ModelCheckpoint, CSVLogger, EarlyStopping
import os
batch_size = 128
num_classes = 10
epochs = 5
log_file_path = r'E:\Landmark\mnist_training_log.log'
model_checkpoint_path = r"E:\Landmark\\mnist.h5"
model_save_path = r"E:\Landmark\\mnist.h5"
weights_filepath="E:\\Landmark\\mnist-weights-improvement-{epoch:02d}.hdf5"
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
from keras.models import load_model
#Load and Continue Training
# load weights if it exists
if os.path.exists(model_checkpoint_path):
print('Loading Definitions')
model = load_model(model_checkpoint_path)
else:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
#Add Early Stop and Checkpoint
early_stop = EarlyStopping(monitor='val_loss', patience=5, verbose=1)
checkpoint = ModelCheckpoint(weights_filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')
csv_logger = CSVLogger(log_file_path, append=False)
callbacks_list = [checkpoint,early_stop,csv_logger]
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test), callbacks=callbacks_list)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save(model_save_path)
import pandas as pd
import matplotlib.pyplot as plt
# Plot the Loss
file_name = log_file_path
df = pd.DataFrame.from_csv(file_name)
print(df.head())
training_loss = df['loss']
test_loss = df['val_loss']
print(training_loss)
print(test_loss)
epoch_count = range(1, len(training_loss) + 1)
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show();

Run #1 Output (10 Epochs)


Run #2 Continue with Existing Weights (5 Epochs)


Happy Learning!!!

December 11, 2019

Day #301 - Data Batching in Keras

This post is about custom data batching using Keras. Here we override the methods of inbuilt sequence. The below example is with dummy data generation, data splitting and fetching the batch of records.

#Generate dummy data
import pandas as pd
import numpy as np
#Generate 250 Records and Split into 6 Columns
df = pd.DataFrame(np.random.randint(0,100,size=(250, 6)), columns=list(['X1','X2','X3','X4','Y1','Y2']))
print(df.head())
print(df.count())
#Split into X and Y
X = df[['X1','X2','X3','X4']]
Y = df[['Y1','Y2']]
print(X.head())
print(Y.head())
#Split data into train and test
from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
import keras
import math
class Generator(keras.utils.Sequence):
# Class is a dataset wrapper for better training performance
def __init__(self, x_set, y_set, batch_size, datacount):
self.x = x_set
self.y = y_set
self.batch_size = batch_size
self.indices = np.arange(self.x.shape[0])
self.idx = 0
self.datacount = datacount
def __len__(self):
print('length')
print(math.ceil(self.datacount/ self.batch_size))
return math.ceil(self.datacount/ self.batch_size)
def __getitem__(self, idx):
print('idx')
print(idx)
i1 = idx*self.batch_size
i2 = (idx+1)*self.batch_size
print('Start-' + str(i1) + '- End-' + str(i2))
if(i2 > self.datacount):
i2 = self.datacount
batch_x = self.x[i1:i2]
batch_y = self.y[i1:i2]
return batch_x, batch_y
def on_epoch_end(self):
np.random.shuffle(self.indices)
batch_size = 10
print('x_train')
print(x_train['X1'].count())
print('x_test')
print(x_test['X1'].count())
training_generator = Generator(x_train, y_train, batch_size, x_train['X1'].count())
validation_generator = Generator(x_test, y_test, batch_size, x_test['X1'].count())
print('training_generator')
#Compute batches for training data
num_batches_train = x_train['X1'].count()/batch_size
for batch_id in range(int(num_batches_train)):
print('batch_id')
print(batch_id)
print(training_generator.__getitem__(batch_id))
print('validation_generator')
#Compute batches for test data
num_batches_test = x_test['X1'].count()/batch_size
for batch_id in range(int(num_batches_test)):
print('batch_id')
print(batch_id)
print(validation_generator.__getitem__(batch_id))
#Model Architecture, Layers, Compile
#Model fitgenerator
#Model Save Checkpoint


Other strategies
  • Databases -> CSV 50K Data Chunks Records -> Training and Save Checkpoint
  • Checkpoint to Save for Each run and reuse for next 50K Chunk of Data
This is a classic data fetching solution. Database can store millions of records. We can fetch each batch, export it to a CSV and use each chunk, train and save checkpoint and continue further for next run.

Happy Learning!!!

December 01, 2019

Day #300 - Lessons Learnt from Multi-Label Classification

Today is 300th Post on Data Science. It has been a long journey. Still I feel there is a lot more to catchup. Keep Learning, Keep Going.

There are different tasks involved

1. Data Collection - Fatkun Batch Download Image chrome extension to download images
2. Script to reshape images and store in a standard format
3. Simple DB script to update and prepare data

import pyodbc
import os
files = os.listdir(r'E:\Multi_Label\Input_Data\T-Shirt_Jeans')
base_filepath = r'E:\Multi_Label\Input_Data\T-Shirt_Jeans'
cnxn = pyodbc.connect(r'DRIVER={SQL SERVER};SERVER=XXXXX\SQLEXPRESS;DATABASE=DataGeneration;Trusted_Connection=yes;')
cursor = cnxn.cursor()
for file in files:
filepath = base_filepath + '\\' + file
cursor.execute("insert into Dataset([FileName],[Cap],[Jeans],[Jacket],[TShirt],[Shirt],[Pants]) values (?,0,1,0,1,0,0)",filepath)
cnxn.commit()
cnxn.close()
4. This base implementation was useful for model implementation link
5. Data Test Results

#Test Code
import cv2
import os
import numpy as np
from keras.models import save_model, load_model
test_dataset = r'E:\Multi_Label\Test'
model = load_model(r'E:\Multi_Label\\model_multi_label.h5')
print(model.summary())
test_images = []
arr = os.listdir(test_dataset)
files = []
for file in arr:
path = test_dataset + '\\'+file
img = cv2.imread(path,1)
img = cv2.resize(img,(256,256))
test_images.append([np.array(img)])
files.append(path)
i = 0
for data in test_images:
test_img_data = np.array(data).reshape(-1,256,256,3)
result = model.predict(test_img_data)
#print(result)
item = ['1-Cap','2 Jeans','3 - Jacket','4-TShirt','5-Shirt','6-Pants']
values = []
values.append(result[0][0])
values.append(result[0][1])
values.append(result[0][2])
values.append(result[0][3])
values.append(result[0][4])
values.append(result[0][5])
print(files[i])
if(float(result[0][0])>0.5):
print('Cap')
if(float(result[0][1])>0.5):
print('Jeans')
if(float(result[0][2])>0.5):
print('Jacket')
if(float(result[0][3])>0.5):
print('Tshirt')
if(float(result[0][4])>0.5):
print('Shirt')
if(float(result[0][5])>0.5):
print('Pants')
#print(values)
i = i+1
#E:\Multi_Label\Test\Cap_Jean.jpeg
#Cap
#Jeans
#E:\Multi_Label\Test\Cap_Jean_2.jpeg
#Cap
#Jeans
#E:\Multi_Label\Test\Cap_Jean_3.jpeg
#Cap
#Jeans
#E:\Multi_Label\Test\Jacket_5.jpg
#Cap
#Jeans
#E:\Multi_Label\Test\Jacket_WM2.jpg
#Cap
#Jeans
#Jacket
#E:\Multi_Label\Test\Jacket_WM3.jpg
#Cap
#Jeans
#Jacket
#E:\Multi_Label\Test\Jacket_WM4.jpg
#Cap
#Jeans
#E:\Multi_Label\Test\Jacket_WML1.jpg
#Cap
#Jeans
#E:\Multi_Label\Test\T_Shirt_Target.jpg
#Cap
#Jeans
#E:\Multi_Label\Test\T_Shirt_Target1.jpg
#Cap
#Jeans
view raw Test_Results.py hosted with ❤ by GitHub
Happy Learning!!!


November 26, 2019

Amazon Go /Big Basket Smart Machine - Tech Analysis

Amazon Go
  • QR Code for user account linking
  • Pick items, Auto detected
  • Multiple validation points (Video Object Detection, Shelf Weight sensor-based confirmation, RFID reads etc)
  • Multiple RFID readers to capture item movement across ISLEs
Cons
  • Massive surveillance 
  • Real-time computation
IMO, Big Basket Smart Machine is also similar to the implementation
  • QR Code for user account linking
  • Pick and Buy
  • Mobile App integration 
  • Weight Sensors used to detect Shelf Item Quantity
  • Unique Items in each row. It is not mixed
Since its a standalone machine, it will not need any tracking with RFID / Video camera. Only Weight sensors sufficient and it is a single person operated at a time.

Happy Learning!!!

Day #299 - OpenVino Python Code for Faces - Pedestrain - Attributes

Hope this helps for other developers trying out executing intel openvino models in python

#https://github.com/wangxiao5791509/Pedestrian-Attribute-Recognition-Paper-List
import cv2
import numpy as np
import sys
import time
import os
FACE_DATA_DIR = '/home/upsquared/Documents/projects/Code/faces'
DATA_DIR = '/home/upsquared/Documents/projects/Code/samples'
RESULTS_DIR = '/home/upsquared/Documents/projects/Code/results'
ATTRIBUTES_RESULTS_DIR = '/home/upsquared/Documents/projects/Code/attribute_results'
def Detect_Faces():
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/face-detection-retail-0004/FP32/face-detection-retail-0004.bin'
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/face-detection-retail-0004/FP32/face-detection-retail-0004.xml'
ped_net = cv2.dnn.readNet(attr_bin, attr_xml)
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
print("Models loaded")
files = os.listdir(FACE_DATA_DIR)
#get all files in directory
#loop through for all files
i = 0
for file in files:
filepath = FACE_DATA_DIR + '//'+ file
print(filepath)
rawframe = cv2.imread(filepath)
frame = cv2.resize(rawframe, (300,300))
#https://docs.openvinotoolkit.org/latest/_models_intel_person_detection_retail_0013_description_person_detection_retail_0013.html
try:
blob = cv2.dnn.blobFromImage(frame,size=(300,300),ddepth=cv2.CV_8U)
ped_net.setInput(blob)
out = ped_net.forward()
predictions = []
for detection in out.reshape(-1,7):
image_id,label,conf,x_min,y_min,x_max,y_max = detection
print(conf)
#print(label)
if conf > 0.5:
predictions.append(detection)
#print(predictions)
print(len(predictions))
for detection in predictions:
confidence = float(detection[2])
xmin = int(detection[3]*frame.shape[1])
ymin = int(detection[4]*frame.shape[0])
xmax = int(detection[5]*frame.shape[1])
ymax = int(detection[6]*frame.shape[0])
print(xmin,ymin,xmax,ymax)
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0))
#Crop and Save
cv2.imshow("Result",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg'
#write the output in directory
cv2.imwrite(result_filepath,frame)
i = i+1
except:
print('Error')
print(frame)
pass
def Detect_Pedestrians_Adas():
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/pedestrian-detection-adas-0002/FP32/pedestrian-detection-adas-0002.bin'
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/pedestrian-detection-adas-0002/FP32/pedestrian-detection-adas-0002.xml'
ped_net = cv2.dnn.readNet(attr_bin, attr_xml)
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
print("Models loaded")
files = os.listdir(DATA_DIR)
#get all files in directory
#loop through for all files
i = 0
for file in files:
filepath = DATA_DIR + '//'+ file
print(filepath)
frame = cv2.imread(filepath)
#https://docs.openvinotoolkit.org/latest/_models_intel_pedestrian_detection_adas_0002_description_pedestrian_detection_adas_0002.html
blob = cv2.dnn.blobFromImage(frame,size=(672,384),ddepth=cv2.CV_8U)
ped_net.setInput(blob)
out = ped_net.forward()
predictions = []
for detection in out.reshape(-1,7):
image_id,label,conf,x_min,y_min,x_max,y_max = detection
if conf > 0.5:
predictions.append(detection)
#print(predictions)
print(len(predictions))
for detection in predictions:
confidence = float(detection[2])
xmin = int(detection[3]*frame.shape[1])
ymin = int(detection[4]*frame.shape[0])
xmax = int(detection[5]*frame.shape[1])
ymax = int(detection[6]*frame.shape[0])
print(xmin,ymin,xmax,ymax)
pedestrian = frame[ymin:ymax,xmin:xmax]
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg'
#write the output in directory
cv2.imwrite(result_filepath,pedestrian)
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0))
i = i+1
cv2.imshow("Result",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
def Detect_Pedestrians_Retail():
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-detection-retail-0013/FP32/person-detection-retail-0013.bin'
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-detection-retail-0013/FP32/person-detection-retail-0013.xml'
ped_net = cv2.dnn.readNet(attr_bin, attr_xml)
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
print("Models loaded")
files = os.listdir(DATA_DIR)
#get all files in directory
#loop through for all files
i = 0
for file in files:
filepath = DATA_DIR + '//'+ file
print(filepath)
frame = cv2.imread(filepath)
#https://docs.openvinotoolkit.org/latest/_models_intel_pedestrian_detection_adas_0002_description_pedestrian_detection_adas_0002.html
#blob = cv2.dnn.blobFromImage(frame,size=(384,672),ddepth=cv2.CV_8U)
#https://docs.openvinotoolkit.org/latest/_models_intel_person_detection_retail_0013_description_person_detection_retail_0013.html
blob = cv2.dnn.blobFromImage(frame,size=(544,320),ddepth=cv2.CV_8U)
ped_net.setInput(blob)
out = ped_net.forward()
predictions = []
for detection in out.reshape(-1,7):
image_id,label,conf,x_min,y_min,x_max,y_max = detection
#print(conf)
#print(label)
if conf > 0.5:
predictions.append(detection)
#print(predictions)
print(len(predictions))
for detection in predictions:
confidence = float(detection[2])
xmin = int(detection[3]*frame.shape[1])
ymin = int(detection[4]*frame.shape[0])
xmax = int(detection[5]*frame.shape[1])
ymax = int(detection[6]*frame.shape[0])
print(xmin,ymin,xmax,ymax)
pedestrian = frame[ymin:ymax,xmin:xmax]
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg'
#write the output in directory
cv2.imwrite(result_filepath,pedestrian)
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0))
i = i+1
#Crop and Save
cv2.imshow("Result",frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
def Detect_Attributes():
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-attributes-recognition-crossroad-0200/FP32/person-attributes-recognition-crossroad-0200.bin'
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-attributes-recognition-crossroad-0200/FP32/person-attributes-recognition-crossroad-0200.xml'
ped_net = cv2.dnn.readNet(attr_bin, attr_xml)
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
print("Models loaded")
files = os.listdir(RESULTS_DIR)
#get all files in directory
#loop through for all files
for file in files:
try:
filepath = RESULTS_DIR + '//'+ file
print(filepath)
frame = cv2.imread(filepath,1)
#H = 160
#W = 80
h,w = frame.shape[:2]
if (w>=80 and h>=160):
#https://docs.openvinotoolkit.org/latest/_models_intel_person_attributes_recognition_crossroad_0230_description_person_attributes_recognition_crossroad_0230.html
blob = cv2.dnn.blobFromImage(frame,size=(80,160),ddepth=cv2.CV_8U)
ped_net.setInput(blob)
print("part 1")
out = ped_net.forward("453")
#print(out)
predictions = []
for detection in out.reshape(-1,8):
is_male, has_bag, has_backpack, has_hat, has_longsleeves, has_longpants, has_longhair, has_coat_jacket = detection
predictions.append(detection)
print(predictions)
#print(len(predictions))
for detection in predictions:
if(detection[0] > 0.5):
print('MALE')
else:
print('Female')
if(detection[1] > 0.5):
print('Has BAG')
if(detection[2] > 0.5):
print('has_backpack')
if(detection[3] > 0.5):
print('has_hat')
if(detection[4] > 0.5):
print('has_longsleeves')
if(detection[5] > 0.5):
print('has_longpants')
if(detection[6] > 0.5):
print('has_longhair')
if(detection[7] > 0.5):
print('has_coat_jacket')
print("part 2")
out1 = ped_net.forward("456")
print(out1)
predictions = []
for detection in out1.reshape(-1,2):
point_with_top_colorx, point_with_top_colory = detection
predictions.append(detection)
for detection in predictions:
print(detection[0])
print(detection[1])
print("part 3")
out2 = ped_net.forward("459")
print(out2)
predictions = []
for detection in out2.reshape(-1,2):
point_with_bottom_colorx, point_with_bottom_colory = detection
predictions.append(detection)
for detection in predictions:
print(detection[0])
print(detection[1])
except:
pass
#Detect_Faces()
#Detect_Pedestrians_Adas()
#Detect_Pedestrians_Retail()
Detect_Attributes()
Happy Learning!!!

November 24, 2019

Day #298 - Data Analysis of PNB Defaulters

Data Source - Link

Data Analysis of PNB Defaulters

Chart #1 - Top 20 States By Company Registration State and Defaulted Amount



Chart #2 - Top 20 States By Defaulters Count





Chart #3 - Top 20 Branches with Maximum Defaulters





Chart #4 - Top 20 Branches with Maximum Defaulters Loan Value





Possible Feature Variables
  1. Branch Related Approval Score, Higher Defaulters lower the rating
  2. Similar Industry Match Score
  3. State Related Scores
  4. Connections / Joint ventures in Past with Collapsed Companies
  5. Rules for threshold limit based on Industry / State / Branch
  6. Multiple Models for ongoing monitoring / performance / social medial trends etc..
  7. Build a global model with defaulter list across banks and identify common patterns/modus operandi
Happy Learning!!!

November 18, 2019

Day #297 - Paper Analysis - WIDER Face and Pedestrian Challenge

WIDER Face and Pedestrian Challenge
Tasks - face detection, pedestrian detection, person search
Dataset - WIDER Pedestrian Track - 20,000 images. From surveillance cameras, driving vehicles

Face Detection
  • Approach 1 -  single stage detector with the network structure based on RetinaNet [7] and FAN - Face attention network
  • Approach 2 - two-stage face detector following Faster R-CNN [12] and FPN Feature pyramid networks [13] framework
  • Approach 3 - a two-stage face detection framework. RetinaNet [7] and RefineDet [15]. The team uses two-stage classification and regression to improve the accuracy of classification
PEDESTRIAN DETECTION TRACK
  • Approach 1 - basic detection framework of the champion is Cascade R-CNN. Five models are ensembled: ResNet50 [18], DenseNet-161 [19], 197 SENet-154 [20] and two ResNext-101 [21] models.
  • Approach 2 - The second team uses FPN [13] and Faster R-CNN [12] as the basis of their detection framework
  • Approach 3 - The team at the third place uses Cascade R-CNN [16] as the detection framework
PERSON SEARCH TRACK
  • Approach 1 - The winning team designs a cascaded model that utilizes both face and body features for person search. (1) The face detector used here is MTCNN [26] trained on WIDER FACE [4]. (2) The face recognition model backbones include ResNet [18], InceptionResNet-v2 [27], DenseNet [19], DPN and MobiletNet [28]. (3) The Re-ID backbones include ResNet=50, ResNet-101, DenseNet-161 and DenseNet-201
  • Approach 2 - The solution is decomposed into two stages - the first stage is to retrieve faces, and the second stage is to retrieve the bodies. Finally, the retrieval results of the two stages are combined as the ranking result. (1) Face Detection. The face detector used here are PCN [29] and MTCNN [26]. (2) Face Retrieval. A second-order networks [30], [31], [32] (ResNet34 as backbone) trained on VGGFace2 [33] with softmax loss and ring loss [34] is used here
  • Approach 3 -  In the first step, the face in the query is used to search persons, whose faces can be detected, by face recognition. Then these images are further used to search again in all candidate images by person reidentification feature to get the final result
Happy Learning!!!


November 13, 2019

Day #296 - TensorflowLite

TensorflowLite models run in gmail, google photos, google assistant, google photos etc..

Advantages
  • Low Latency
  • No Data connection required
  • On device sensors
Key Points
  • On Device ML on many platforms
  • Tensorflow model saved in graph format
  • Converted to Lite format
TFLite
  • Model compression
  • Quantization
  • Optimized SIMD Kernels
  • Converter to generate model. Interpreter to run models
Benefits
  • Cross-Platform deployment
  • Inference speed increases
  • Binary size reduction
  • Hardware acceleration roadmap
#Build and save keras model
model = build_your_model()
tf.keras.experimental.export_saved_model(model,saved_model_dir)

#convert keras to tensorflow lite model
converter = tf.lite.TFLiteConverter.from_saved_models(saved_model_dir)
#To experiment new feature
converter.experimental_new_converter = True
tflite_model = converter.convert()

Link1, Link2

Improve Performance of models
  • Reduce precision of weights (16 bit instead of 32 bit precision) - Quantization
  • Pruning - Remove connections during training
  • Op Kernels - ARM
  • Delegates - GPU (Run on Specialized hardware)




Tensorflow Lite on Micro-controllers is an impressive move. More than mobiles, cross platforms this is a very impressive step.

Happy Learning!!!

November 11, 2019

Getting new ideas perspectives

Some meaningful tips for new ideas, solutions, creative thinking




1. Learn a lot of facts
2. Have deep knowledge of the background material
3. Spend time thinking about the problem
4. A curiosity about fundamental characteristics – what makes things tick (big picture and not details)
5. Strong drive to want to find out the answers
6. Pick your work colleagues based on their ability to help simulate good ideas
7. Narrow the scope of the problem if you are not making progress
8. Moving to a new situation will allow you to change your behavior since there aren’t preconceived expectations of your behavior.
9. Similarity to a known problem (experience helps)
10. Structural analysis (break problem into pieces); solve a microproblem first and then build up – divide & conquer
11. Ask conceptual questions about everyday things
12. Simplify and deep dive, Spend a lot of time reading
13. Work in isolation before participating in a group
14. You got to be a learning machine to improve your thinking
15. Drive Decision based on facts, behavior, intuition, apply lessons learned in the past
https://spinlab.me/2017/11/19/isaac-asimov-asks-how-do-people-get-new-ideas/
https://spinlab.me/2017/07/29/r-w-hamming-on-creativity/
https://spinlab.me/2017/07/30/claude-shannons-1952-lecture-on-creative-thinking/
https://qr.ae/TWgnaR
Another Interesting Read - Idea Generation - https://blog.samaltman.com/idea-generation
Good Environment to discuss ideas
Optimistic people
Think without the constraints
Good feel for the future
"Stay away from people who are world-weary and belittle your ambitions"
"You want to be able to project yourself 20 years into the future, and then think backwards from there. Trust yourself—20 years is a long time; it’s ok if your ideas about it seem pretty radical."
"Finally, a good test for an idea is if you can articulate why most people think it’s a bad idea, but you understand what makes it good"
Technical Debt and Product Success - https://medium.com/@romanpichler/technical-debt-and-product-success-42ec1c5718a7
view raw NewIdeas.txt hosted with ❤ by GitHub
Happy Learning!!!

November 05, 2019

Day #295 - Age - Emotion - Gender Detection Model

It seems I am aging faster than ever.


Deep Learning Model for Age, Gender, Emotion and Real-time implementation. It seems I am agining faster. If it says I am 40, I have just a decade left to code and transition to something else.

Years progressed so fast seems already aged. I hope to code and do something till my day of death. Keep Going...

Happy Learning!!!

November 04, 2019

Day #294 - Setting up Home Surveillance System

Finally, after a few months, I was able to set up a Home Surveillance System. Person Detection and Real-time alert.

Installation


Demo

Happy Learning!!!

November 01, 2019

Day #293 - Date with RASA - Chatbot Learning day :)

Found an interesting workshop on end to end demo with RASA.

Training



Demo


#https://www.youtube.com/watch?v=xu6D_vLP5vY
#https://github.com/JustinaPetr/Weatherbot_Tutorial
Rasa based chatbot
Step 1 - Pre-Requisites
========================
1. Clone Project https://github.com/JustinaPetr/Weatherbot_Tutorial
2. Install Requirements from FULL Code Directory
cd E:\Code_Repo\Weatherbot_Tutorial\Full_Code
pip install -r requirements.txt
3. Download English Spacy model - To parse and get necessary information
python -m spacy download en
4. Install npm with node.js. https://www.npmjs.com/get-npm
https://nodejs.org/dist/v12.13.0/node-v12.13.0-x64.msi
5. In New Terminal
npm i -g rasa-nlu-trainer
Data Annotation - rasa nlu trainer
Deconstruct into Intent, Entities
Intent - What it is about
Entity - Location, Place, Object in Discussion
Example Messages, Alongside intents, Entities
Examples
Greeting
GoodBye
Asking
Step 2 - Data Annotation
============================
Step #1 - File data.json
{
"rasa_nlu_data":{
"common_examples":[
{
"text":"Hello"
"intent":"Greet",
"entities":[]
}
{
"text":"goodbye"
"intent":"goodbye",
"entities":[]
}
]
}
Step #2 - Launch the trainer in Anaconda console
1. Goto Location E:\Code_Repo\Weatherbot_Tutorial\Full_Code_Latest>
2. Run Command rasa-nlu-trainer
3. Custom adding intent and examples
4. All additional examples present in git code in updated Data.json file
Step #3 - Train model
=======================
1. Configuration File
- Provide parameters
- Pipeline - Feature extractors to fetch messages
- Model save path
- Data path for annotated data
{
"pipeline":"spacy_sklearn",
"path":"./models/nlu",
"data":"./data/data.json"
}
config_spacy.json file
2. nlu_model.py file for script for model training
#import libraries
from rasa_nlu.converters import load_data
#load configuration files
from rasa_nlu.config import RasaNLUConfig
#load trainer
from rasa_nlu.model import Trainer
def train_nlu(data,config,model_sir):
training_data = load_data(data)
trainer = Trainer(RasaNLUConfig(config))
trainer.train(training_data)
model_directory = trainer.persist(model_dir,fixed_model_name='weathernlu')
if __name__=='__main__':
train_nlu('./data/data.json','config_spacy.json','./models/nlu'
#Run this to train the model
#Models created in folder directory
2. Code to test with additional code in nlu_model.py
#import libraries
from rasa_nlu.converters import load_data
#load configuration files
from rasa_nlu.config import RasaNLUConfig
#load trainer
from rasa_nlu.model import Trainer
from rasa_nlu.model import Metadata, Interpreter
def train_nlu(data,config,model_sir):
training_data = load_data(data)
trainer = Trainer(RasaNLUConfig(config))
trainer.train(training_data)
model_directory = trainer.persist(model_dir,fixed_model_name='weathernlu')
def run_nlu():
interpreter = interpreter.load('./models/nlu/default/weathernlu',RasaNLUConfig('config_spacy.json'))
#load the model
print(interpreter.parse(u"I am planning my holiday to barcelona, I wounder what is the weather out there"))
if __name__=='__main__':
run_nlu()
3. Changes to run for custom packages (Code will run in these versions)
pip install rasa_core==0.10.3
pip install rasa-nlu==0.11.5
Rerun - nlu_model.py file
Step #4 - Building the conversation
======================================
1. Dialogue management will predict action. Domain file. It is yml file.
2. Key parts are
slots - placeholders for context of conversation,
intents - ,
entities - ,
templates - ,
actions -
3. All details used for predictions
slot and entities have same attributes - Observations
template - text responses for users (multiple answers)
weather_domain.yml
slots:
location:
type:text
intents:
- greet
- goodbye
- inform
entities:
- location
templates:
utter-greet:
- 'Hello, How can i help?'
utter-goodbye:
- 'ttyl'
utter_ask_location:
-'In what location?'
actions:
- utter_greet
- utter_goodbye
- utter_ask_location
- actions.ActionWeather
Step #5 - Custom Action creation file
========================================
actions.py
from __future__import absolute_import
from __future__import division
from __future__import unicode_literals
from rasa_core.actions.action import Action
from rasa_core.events import SlotSet
class ActionWeather(Action):
def name(self):
return 'action_weather'
def run(self,dispatcher,tracker,domain):
from apixu.client import ApixuClient
api_key = ""
#Authentication
client = ApixuClient(api_key)
loc = tracker.get_slot('location')
current = client.getCurrentWeather(q=loc)
#parse and extract required details
country = current['location']['country']
city = current['location']['name']
condition = current['current']['condition']['text']
temperature_c = current['current']['temp_c']
humidity = current['current']['humidity']
wind_mph = current['current']['wind_mph']
response = """It is currently {} in {} at the moment. {} {} and wind {}""".format(condition,city,temperature,humidity,wind_mph)
dispatcher.utter_message(response)
#custom action
return [SlotSet('location',loc)]
#file updated in actions weather_domain.yml
Step #6 - Story formation
==========================
New file Stories.md markdown file in data folder
Stories.md
==========
#story 01
* greet
- utter_greet
## story 02
* goodbye
- utter_goodbye
##story 03
* inform
- utter_ask_location
##story 04
* inform
- action_weather
Step #7
========
Start online session
using train_init.py and train_online.py
train_init.py
- train dialogue management model
- agent used to train model
- keras polcies used to train model
- data file path
- augmentation factor to add more stories
- Save model with persist function
pip install rasa-nlu==0.13.1
Modified train_init.py
=======================
Step #8
========
train_online.py
- import libraries
- parser to parse extract features
- load model
Retrain Model
python -m rasa_core.train -s data/stories.md -d weather_domain.yml -o models/dialogue --epochs 300
Step #9
=======
Run online Training
python -m rasa_nlu.train -c nlu_model_config.yml --fixed_model_name current --data ./data/nlu.md --path models/ --project nlu
Step #10
=========
dialogue_management_model.py
Final code to demo
Summary
========
- Install requirements from FULL Code only
- Run code nlu_model.py train block
- Run code train_init.py
- Run code train_online.py (Actual Conversations with chatbot)
- Action_Listen (Wait for input)
- Experimented the greet - ask - response - quit workflow
- Run demo, dialogue_management_model.py
Next Reads
https://towardsdatascience.com/create-chatbot-using-rasa-part-1-67f68e89ddad
https://medium.com/analytics-vidhya/learn-how-to-build-and-deploy-a-chatbot-in-minutes-using-rasa-5787fe9cce19
https://forum.rasa.com/t/what-is-the-recommended-setup-for-production-deployemnt/1882
view raw rasabot.txt hosted with ❤ by GitHub

Happy Learning!!!

October 30, 2019

Day #292 - Gstream on Windows10

Note - Do "complete" instead of "typical" for both cases for it to work

This link was useful to experiment and follow.

Step 1. Download installer1 from link

Step 2. Download installer2 from link

Install both of them in Windows, Goto Folder - F:\gstreamer\1.0\x86_64\bin

Step #3 - Command
gst-launch-1.0.exe -v ksvideosrc device-index=0 ! video/x-raw, format=YUY2, width=320, heigh=240, framerate=30/1, pixel-aspect-ratio=1/1 ! videoconvert ! autovideosink

Stream Output

Next Post Stream from rasberry pi to windows

Happy Learning!!!

October 29, 2019

Day #291 - Working with chatterbot - Windows 10

#pip install --ignore-installed PyYAML
#pip install chatterbot
#pip install chatterbot-corpus
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer
welcomebot = ChatBot("Charlie")
#The current training method takes a list of statements that represent a conversation
initialconversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]
welcomebottrainer = ListTrainer(welcomebot)
welcomebottrainer.train(initialconversation)
#The program selects the closest matching response by searching for the closest matching known statement that matches the input
response = welcomebot.get_response("Hi there")
print(response)
response = welcomebot.get_response("Hello")
print(response)
coursebot = ChatBot("Charlie")
coursebottrainer = ListTrainer(coursebot)
querycourses = [
"Share courses",
"We offer courses in AI, ML, Devops, Big Data",
"Great ",
"Share me AI Details",
"It has machine learning basics to advanced models",
"What about big data",
"hadoop, streaming tools and kafka",
"What about Devops",
"Docker, Kubernetics"
]
coursebottrainer.train(querycourses)
response = welcomebot.get_response("Share courses")
print(response)
response = welcomebot.get_response("Share me AI Details")
print(response)
paymentbot = ChatBot("Charlie")
paymentbottrainer = ListTrainer(paymentbot)
feescourses = [
"Devops Fees",
"Online 15K, Offline 10K",
"AI Feeds ",
"Online 25K, Offline 10K",
"Big Data Fees",
"Online 35K, Offline 10K",
]
paymentbottrainer.train(feescourses)
response = paymentbot.get_response("Devops Fees")
print(response)
response = paymentbot.get_response("Big Data Fees")
print(response)
view raw chattberbot.py hosted with ❤ by GitHub

Happy Learning!!!

Day #290 - Yolo from OpenCV DNN - Windows 10

OpenCV has examples to invoke Deep Network using readNet modules

Key Methods
  • cv.dnn.readNet - Load the network
  • cv.dnn.NMSBoxes - Non Max Suppression
  • cv.dnn.blobFromImage - Input for Deep Network
Steps

Package Install - pip install opencv-python
Step #1 - git clone https://github.com/opencv/opencv
Step #2 - Goto Path - opencv/opencv/tree/master/samples/dnn
Link https://docs.opencv.org/master/da/d9d/tutorial_dnn_yolo.html
Step #3 - Steps to Run
example_dnn_object_detection --config=[PATH-TO-DARKNET]/cfg/yolo.cfg --model=[PATH-TO-DARKNET]/yolo.weights --classes=object_detection_classes_pascal_voc.txt --width=416 --height=416 --scale=0.00392 --input=[PATH-TO-IMAGE-OR-VIDEO-FILE] --rgb
Step #4 - cd E:\Code_Repo\opencv\samples\dnn
Step #5 - For Image
python object_detection.py --config="E:\\Code_Repo\\darkflow\\cfg\\yolo.cfg" --model="E:\\Code_Repo\\darkflow\\yolov2.weights" --classes="E:\\Code_Repo\\darkflow\\cfg\\coco.names" --width=416 --height=416 --scale=0.00392 --input="E:\Car_Detection\Data1\\frame214000.jpg" --rgb
Step #6 - For Video
python object_detection.py --config="E:\\Code_Repo\\darkflow\\cfg\\yolo.cfg" --model="E:\\Code_Repo\\darkflow\\yolov2.weights" --classes="E:\\Code_Repo\\darkflow\\cfg\\coco.names" --width=416 --height=416 --scale=0.00392 --input="E:\Car_Detection\\ch10_20190304115220.mp4" --rgb
Another Good Ref - https://github.com/rdeepc/ExploreOpencvDnn
Results




Happy Learning!!!

October 25, 2019

Day #289 - Example code for Class Creation, Data Persistence, Email and Phone number Validation

Example code for Class Creation, Data Persistence, Email and Phone number validators

#pip install phonenumbers
#pip install validate_email
import phonenumbers
from phonenumbers import carrier
from phonenumbers.phonenumberutil import number_type
from validate_email import validate_email
import pickle
userdata = r'E:\Code_Repo\messaging\phonename.dictionary'
useremail = r'E:\Code_Repo\messaging\phoneemail.dictionary'
phonename = {}
phoneemail = {}
class Person:
def __init__(self,name,phonenumber,emailaddress):
#Constructor
self.name = name
self.phonenumber = phonenumber
self.emailaddress = emailaddress
def validate_phonenumber(self):
#Phone number validation
status = carrier._is_mobile(number_type(phonenumbers.parse(self.phonenumber)))
return status
def validate_email_addr(self):
#Email validation
is_valid = validate_email(self.emailaddress)
return is_valid
def Save_data():
guestuser1 =Person('Raj','+91-7406947660','siva@siva.com')
guestuser2 =Person('Raja','+91-7406947760','siva1@siva.com')
print(guestuser1.validate_email_addr())
print(guestuser1.validate_phonenumber())
#Save data
phonename[guestuser1.phonenumber] = guestuser1.name
phoneemail[guestuser1.emailaddress] = guestuser1.emailaddress
phonename[guestuser2.phonenumber] = guestuser2.name
phoneemail[guestuser2.emailaddress] = guestuser2.emailaddress
#Persist data
with open(userdata, 'wb') as config_dictionary_file:
pickle.dump(phonename, config_dictionary_file)
with open(useremail, 'wb') as config_dictionary_file:
pickle.dump(phoneemail, config_dictionary_file)
def Read_data():
#read data
with open(userdata, 'rb') as config_dictionary_file:
userdatavalues = pickle.load(config_dictionary_file)
for key,datavalues in userdatavalues.items():
print(key)
print(datavalues)
with open(useremail, 'rb') as config_dictionary_file:
useremailvalues = pickle.load(config_dictionary_file)
for key,datavalues in useremailvalues.items():
print(key)
print(datavalues)
Save_data()
Read_data()
Happy Learning!!!

October 24, 2019

Day #288 - Messaging using pyzmq

pip install pyzmq

Example - python usage

Output


Real world use case - imagezmq: Transporting OpenCV images

Happy Learning!!!

October 19, 2019

Day #287 - Dlib Custom Detector

Learning's
  • Aspect ratio needs to be maintained
  • Trained for only one type of Object (Vim Dishwasher)
  • Trained with just a few images 30 images of train and 5 images of test
  • Environment - Windows 10, After all the Setup and Steps, It would take ~45 mins to develop for label, training, and testing
Data Set
  • Custom Shelf and Vim dishwash Detection
Image Pre-requisites
  • I resized data to 512 x 512 format to be consistent
  • Place all images in train and test directory respectively before next steps
Label Training Data
  • Img Lab Installation - Refer previous post
  • Use Shift key to select rectangle
  • Use Alt D to delete region
  • Each tool has its own commands
Step 1 - Specify XML and image path
=====================================
E:\Code_Repo\dlib\tools\imglab\build\Release\imglab.exe -c E:\Code_Repo\dlib_obj_count\ShelfData\train\train.xml E:\Code_Repo\dlib_obj_count\ShelfData\train
Step 2
=======
cd E:\Code_Repo\dlib_obj_count
Step 3
=======
imglab.exe E:\Code_Repo\dlib_obj_count\ShelfData\train\train.xml



Label Testing Data

Step 1
========
E:\Code_Repo\dlib\tools\imglab\build\Release\imglab.exe -c E:\Code_Repo\dlib_obj_count\ShelfData\test\test.xml E:\Code_Repo\dlib_obj_count\ShelfData\test
Step 2
======
cd E:\Code_Repo\dlib_obj_count
Step 3
======
imglab.exe E:\Code_Repo\dlib_obj_count\ShelfData\test\test.xml

Training Code


#Base code - https://gist.github.com/atotto/c1ccbfa44ee70a476816f6389834945e
#Minor changes for my requirements
import os
import sys
import dlib
options = dlib.simple_object_detector_training_options()
options.add_left_right_image_flips = False
options.C = 5
options.num_threads = 2
options.be_verbose = True
training_xml_path = r'E:\Code_Repo\dlib_obj_count\ShelfData\train\train.xml'
testing_xml_path = r'E:\Code_Repo\dlib_obj_count\ShelfData\test\test.xml'
dlib.train_simple_object_detector(training_xml_path, r'E:\Code_Repo\dlib_obj_count\ShelfData\detector.svm', options)
print("")
print("Training accuracy")
print(dlib.test_simple_object_detector(training_xml_path, r'E:\Code_Repo\dlib_obj_count\ShelfData\detector.svm'))
print("Testing accuracy")
print(dlib.test_simple_object_detector(testing_xml_path, r'E:\Code_Repo\dlib_obj_count\ShelfData\detector.svm'))
view raw train_dlib.py hosted with ❤ by GitHub


Test Code

import dlib
import cv2
detector = dlib.simple_object_detector(r'E:\Code_Repo\dlib_obj_count\ShelfData\detector.svm')
filepath = r'E:\Code_Repo\dlib_obj_count\ShelfData\test\25.png'
img = cv2.imread(filepath,1)
dets = detector(img)
for d in dets:
cv2.rectangle(img, (d.left(), d.top()), (d.right(), d.bottom()), (0, 0, 255), 2)
# Display the resulting frame
cv2.imshow("frame",img)
cv2.waitKey(0)
# When everything done, release the capture
cv2.destroyAllWindows()
view raw test_dlib.py hosted with ❤ by GitHub


Result

Day #286 - Working with imglab Annotation Tool

Working with imglab annotation tool.


Step 1
======
git clone https://github.com/davisking/dlib
Step 2
=======
cd to tools/imglab
Step 3
=======
mkdir build
cd build
Step 4
=======
cmake ..
Step 5
======
cmake --build . --config Release
Step 6
======
cd E:\Code_Repo\dlib\tools\imglab\build
Step 7
======
E:\Code_Repo\dlib\tools\imglab\build\Release\imglab.exe
view raw imglab.txt hosted with ❤ by GitHub
Happy Learning!!!

October 17, 2019

Learning Moments

Last three-four days, I was breaking my head for a segmentation task. There were a ton of tutorials. Everywhere I pick a code and ended up not working. Finally, I managed to segment it. When we sit and learn alone defintely there will be moments of long failures. Do whatever you do with a bit of curiosity and interest. Learning is an ongoing habit. We are not used to proper learning with focus, attention, curiosity, passion, and experimentation.  

Happy Learning!!!

Day #285 - Experimenting with Unet Segmentation

U-Net

  • Symmetric U-Shape - Convolutions + Poolings
  • Up-Convolutions - Upsampled layers
  • Encoder / Decoder
  • Contraction / Expansion
  • Skip Connections to learn pixel information

There are a ton of tutorials out there but it takes time to find to what works for us :) in our environment. I was experimenting on u-net based segmentation past few days. I will share my learnings on what worked for me.

Step 1 - The initial image is

I am interested in segmenting the parts (products)

Step 2 - The first step is to resize the image into 256 x 256 dimension



Step 3 - The Next Step is to binarize the image

This is the source image. The target image is


Step 4 - Tool - I used paint 3D and white brush in it to segment the required parts for my need

Step 5 - Follow the steps and create the train and label (source and segmented image)

Step 6 - Train the model, Got the repo and customized it link

Step 7 - The predictions for the test image are




Next Demo


 Happy Learning!!!


October 15, 2019

Day #284 - OpenCV Error in Windows server 2012


  • Turn windows features on or off
  • Skip the roles screen and directly go to Feature screen
  • Select "Desktop Experience" under "User Interfaces and Infrastructure"

Think answer was useful link

Happy Learning!!!

October 11, 2019

Day #283 - Clustering to group similar Images


For large retail datasets, before object detection. Clustering becomes essential to focus on each cluster to take it forward. Today's post is clustering images into similar groups

  • Generate Feature Data based on VGG / Resnet
  • Cluster them using Kmeans
  • Result output to their respective cluster directory

#Base Code - https://medium.com/@franky07724_57962/using-keras-pre-trained-models-for-feature-extraction-in-image-clustering-a142c6cdf5b1
#Modified for our custom need
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.resnet50 import ResNet50
import numpy as np
import os
from sklearn.cluster import KMeans
from keras.applications.resnet50 import preprocess_input, decode_predictions
import shutil
datadir = r'E:\Code_Repo\Images'
output_dir = r'E:\Code_Repo\results'
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print('Error: Creating directory. ' + directory)
def VGG_Cluster(numberofclusters):
feature_list = []
model = VGG16(weights='imagenet', include_top=False)
files = os.listdir(datadir)
for file in files:
img_path = datadir+ str('\\') + file
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
feature = model.predict(img_data)
feature_np = np.array(feature)
feature_list.append(feature_np.flatten())
feature_list_np = np.array(feature_list)
kmeans = KMeans(n_clusters=numberofclusters, random_state=0).fit(feature_list_np)
labelresult = kmeans.labels_
print(kmeans.labels_)
print(kmeans.cluster_centers_)
print('VGG Results')
#Create Directory based on number of clusters
for i in range(numberofclusters):
directoryname = output_dir + str('\\') + str(i) + str('\\')
createFolder(directoryname)
for i in range(len(files)):
img_path = datadir+ str('\\') + files[i]
#Copy image according to the directory
print(files[i])
shutil.copy(img_path, output_dir + '\\' + str(labelresult[i]) + '\\')
print(labelresult[i])
def Resnet_Cluster(numberofclusters):
feature_list = []
model = ResNet50(weights='imagenet', include_top=False)
files = os.listdir(datadir)
for file in files:
img_path = datadir+ str('\\') + file
img = image.load_img(img_path, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
feature = model.predict(img_data)
feature_np = np.array(feature)
feature_list.append(feature_np.flatten())
feature_list_np = np.array(feature_list)
kmeans = KMeans(n_clusters=numberofclusters, random_state=0).fit(feature_list_np)
labelresult = kmeans.labels_
print(kmeans.labels_)
print(kmeans.cluster_centers_)
print('Resnet Results')
#Create Directory based on number of clusters
for i in range(numberofclusters):
directoryname = output_dir + str('\\') + str(i) + str('\\')
createFolder(directoryname)
for i in range(len(files)):
#Copy image according to the directory
img_path = datadir+ str('\\') + files[i]
print(files[i])
shutil.copy(img_path, output_dir + '\\' + str(labelresult[i]) + '\\')
print(labelresult[i])
#VGG_Cluster(3)
Resnet_Cluster(3)

Input - Mixed Set of Images
Output 
Cluster 1
Cluster 2

Cluster 3
More Reads - Example (in R)

Happy Learning!!!

October 10, 2019

Day #282 - Retail Product Detection / Retail Object Detection


Paper #1 - Automatic Detection of Out-Of-Shelf Products in the Retail Sector Supply Chain

Rule-based information system
  • OOS Contribution factors - Measurement of product availability, Measurement of shelf availability
  • Approach - Radio-Frequency Identification based
  • Rule - “IF (a product is fast-moving) AND (has low sales volatility) AND (POS sales = 0 for today) THEN the product is OOS
  • IF Fastmoving product sales count is zero then there is a problem
  • Detection approach = Historical data -> Patterns -> Rules = Apply on Current Data
Paper #2 - Retail Shelf Analytics Through Image Processing and Deep Learning
Analysis
  • Tasks - Automatic product checkout using segmentation, Object detection of products on store shelves
  • Approach - Shelf Image -> Detector for regions (Class, BBox, Mask) -> Crop Each Region -> Object -> Feature Extractor -> KNN Classifier

Paper #3 - A deep learning pipeline for product recognition on store shelves
Analysis
  • Shelf Image -> Region proposals -> Crop -> Reference Images -> Refinement -> Detection

Paper #4 - Planogram Compliance Checking Based on Detection of Recurring Patterns
Analysis
  • Shelf Image -> Region Partition -> Recurring Pattern Detection -> Compliance Checking

High Level Recommendations (Apply Combination of techniques)
  • Shelf Image -> Region Partition -> Region proposals -> Detect Recurring Pattern, Reference Images for refinement -> Prediction
Project Analysis 
The Shelf Detector System For Retail Stores Using Object Detection

pip install -r requirements.txt
python train_obj_detector.py testNutella1

Code Details - https://github.com/bobquest33/dlib_obj_count/blob/master/nutella.pdf
Tool Used - https://imglab.in/

Interesting Read
Retail Product Recognition on Supermarket Shelves

Paper #5 - Rethinking Object Detection in Retail Stores
Key Notes
  • Simultaneously object localization and counting, abbreviated as Locount
  • Algorithms to localize groups of objects of interest with the number of instances
  • Most of the state-of-the-art object detectors use non-maximal suppression (NMS) to post-process object proposals to produce final detections
New Approach
  • Cascaded localization and counting network (CLCNet)
  • Localize groups of objects of interest with the numbers of instances
Dataset
  • Grozi-120 dataset
  • Freiburg Groceries dataset
  • GameStop dataset
  • Retail-121 dataset
  • Sku110k dataset
  • TGFS dataset
Cascade R-CNN [1] proposes a multi-stage object detection architecture, which is formed by a sequence of detectors trained with increasing IoU thresholds

Locount Dataset
  • 140 common commodities, including 9 big subclasses
  • Cascaded localization and counting network (CLCNet
    • count-regression strategy for counting
    • count-classification strategy for counting
  • Locount to localize groups of objects with the instance numbers, which is more practical in retail scenarios



Happy Learning!!!

October 09, 2019

Day #281 - Yolo based Object Counting and Duplicate Removal

Today's learning is Yolo based object counting, duplicate removal using intersection over union metric.

import cv2
import sys
import os
#Windows 10 Setup
from darkflow.net.build import TFNet
sys.path.append(r"E:\Code_Repo\darkflow\darkflow")
options = {
'model': 'E:\\Code_Repo\\darkflow\\cfg\\yolo.cfg',
'load': 'E:\\Code_Repo\\darkflow\\yolov2.weights',
'threshold': 0.14,
'gpu': 1.0
}
tfnet = TFNet(options)
#https://github.com/tejaslodaya/car-detection-yolo/blob/master/app_utils.py
def iou(box1, box2):
"""Implement the intersection over union (IoU) between box1 and box2
Arguments:
box1 -- first box, list object with coordinates (x1, y1, x2, y2)
box2 -- second box, list object with coordinates (x1, y1, x2, y2)
"""
# Calculate the (y1, x1, y2, x2) coordinates of the intersection of box1 and box2. Calculate its Area.
xi1 = max(box1[0], box2[0])
yi1 = max(box1[1], box2[1])
xi2 = min(box1[2], box2[2])
yi2 = min(box1[3], box2[3])
inter_area = (yi2 - yi1) * (xi2 - xi1)
# Calculate the Union area by using Formula: Union(A,B) = A + B - Inter(A,B)
box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
union_area = box1_area + box2_area - inter_area
# compute the IoU
iou = inter_area / union_area
return iou
def Object_Count_Yolo(imagefilepath):
coordinates = []
boxes = []
boxlabels = []
finalboxes = []
finalboxlabels = []
img = cv2.imread(imagefilepath, cv2.IMREAD_COLOR)
result = tfnet.return_predict(img)
for data in result:
coordinates = [data['topleft']['x'], data['topleft']['y'],data['bottomright']['x'], data['bottomright']['y']]
boxes.append(coordinates)
boxlabels.append(data['label'])
for k in range(0,len(boxes)):
selectflag = 0
for m in range(k+1,len(boxes)):
iouvalue = iou(boxes[k],boxes[m])
if iouvalue > .7:
selectflag = 1
if(selectflag==0):
finalboxes.append(boxes[k])
finalboxlabels.append(boxlabels[k])
return len(finalboxes)
def diffObjectCount(SourceImagepath, DestinationImagePath):
#Compute and Find Difference in Count
#Return %% based on Count Difference
SourceObjectCount = Object_Count_Yolo(SourceImagepath)
DestinationObjectCount = Object_Count_Yolo(DestinationImagePath)
print(SourceObjectCount)
print(DestinationObjectCount)
Happy Learning!!!

October 08, 2019

Day #280 - Human detection and Tracking

Project #1 - Human Detection and Tracking 

Overview
  • Detecting a human and its face in a given video and storing Local Binary Pattern Histogram
  • Recognize them in any other videos
  • Local Binary Pattern Histogram - type of visual descriptor, clockwise direction check neighbour values
Execution Steps
Clone the project
Step 1 - python create_face_model.py -i data
Step 2 - python main.py -v video

Project #2 - Person-Detection-and-Tracking  (Pending Execution)

Overview
  • The person detection in Real-time is done with the help of Single Shot MultiBox Detector
  • Single Shot MultiBox Detector
  • Tracking - Kalman Filter is fed with the velocity, position and direction of the person which helps it to predict the future location 
Single Shot MultiBox Detector
  • The core of SSD is predicting category scores and box offsets for a fixed set of default bounding boxes using small convolutional filters applied to feature maps
  • The key difference between training SSD and training a typical detector that uses region proposals, is that ground truth information needs to be assigned to specific outputs in the fixed set of detector outputs
Execution Steps
Clone the project https://github.com/ambakick/Person-Detection-and-Tracking

Execute - camera.py in Spyder

Project #3 - Tracker Types Demo Project (Pending Execution)

Overview
  • Track Multiple faces
  • Download and Experiment
Run Below Demos
demo - track multiple faces.py
Multiple_Trackers.py
face_eye.py
distance_to_camera.py

Datasets - Link

Object Motion Detection and Tracking for Video Surveillance
Measuring size and distance with OpenCV
Calculate X, Y, Z Real World Coordinates from Image Coordinates using OpenCV

Happy Learning!!!

October 07, 2019

Learning and Growth

Every job / role / growth is more about understanding the problem / perspective / business and technical dimensions. AI has a lot of tools, languages, architecture, research insights. It is ongoing learning to evaluate all possible solutions, productize with certain boundaries, keep learning to expand other architectures. Be open to learning, titles/roles are a cascade effect of your consistent efforts.

Keep Going!!!


October 03, 2019

Day #279- Multi-Object Tracking

Project #1 - vehicle-speed-check

Clone Repository - Link

On Anaconda prompt,
cd vehicle-speed-check
pip install -r requirements.txt
python speed_check.py

Comments - Very good project to get started. The logic of speed computation with respect to frames per second, pixel movement can be reused in other use cases. Use of dlib correlation tracker. The tracking logic can be reused in other similar implementation

Project #2 - Simple Example code (ROI Based)

#pip uninstall opencv-python
#pip uninstall opencv-contrib-python
#pip intall opencv-contrib-python.
#Learnt from https://www.learnopencv.com/multitracker-multiple-object-tracking-using-opencv-c-python/
#Rewrote based on my requirements
import cv2
import sys
# Read video
video = cv2.VideoCapture(r"E:\Code_Repo\vehicle-speed-check\cars.mp4")
#Create two trackers
trackers = []
tracker1 = cv2.TrackerKCF_create()
trackers.append(tracker1)
tracker2 = cv2.TrackerKCF_create()
trackers.append(tracker2)
tracker3 = cv2.TrackerKCF_create()
trackers.append(tracker3)
if not video.isOpened():
print("Could not open video")
sys.exit()
# Read first frame.
ok, frame1 = video.read()
height , width , layers = frame1.shape
new_h=int(height/2)
new_w=int(width/2)
frame = cv2.resize(frame1, (new_w, new_h))
if not ok:
print('Cannot read video file')
sys.exit()
bboxdata = []
#Select two regions to track
for i in range(3):
bbox = cv2.selectROI(frame, False)
bboxdata.append(bbox)
print(bbox)
print(bboxdata)
# Initialize tracker with first frame and bounding box
#tracker1 update
ok1 = trackers[0].init(frame, bboxdata[0])
#tracker2 update
ok2 = trackers[1].init(frame, bboxdata[1])
#tracker3 update
ok3 = trackers[2].init(frame, bboxdata[2])
flag = 0
count = 0
while True:
# Read a new frame
ok1, frame1 = video.read()
height , width , layers = frame1.shape
new_h=int(height/2)
new_w=int(width/2)
frame = cv2.resize(frame1, (new_w, new_h))
if not ok1:
break
i = 0
for tracker in trackers:
result, bbox = tracker.update(frame)
i = i + 1
if result:
# Tracking success
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
else:
print('Tracking failure for Object ', i)
#remove from the list
trackers.remove(tracker)
#for every object that enters append to this
if flag==0:
bbox = cv2.selectROI(frame, False)
tracker = cv2.TrackerKCF_create()
tracker.init(frame, bbox)
trackers.append(tracker)
flag = 1
cv2.imshow("Tracking", frame)
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
if k == 27: break
cv2.destroyAllWindows()
print(trackers)


Project #3 - Another interesting project from Adrian blog 

Cloned the project and executed the demo. This code does not work in windows 10 though. Someone has fixed the code. The working code is in link 

python multi_object_tracking_fast.py --prototxt E:\Code_Repo\multiobject-tracking-dlib\mobilenet_ssd\MobileNetSSD_deploy.prototxt  --model E:\Code_Repo\multiobject-tracking-dlib\mobilenet_ssd\MobileNetSSD_deploy.caffemodel --video E:\Code_Repo\multiobject-tracking-dlib\race.mp4 --output E:\Code_Repo\multiobject-tracking-dlib\race_output_fast.avi

October 02, 2019

Day #278 - Object Tracking - TensorFlow Object Counting API

I came across this project. Fantastic work!! The Detection part needs to be finetuned for the Indian scenario, the tracking seems fine performing decently. You can spot a few false positives, Trucks on the other side of the lane are not detected, Indian Trucks are not well recognized. This can be handled by a custom detection model. Overall the tracking and counter approach can be reused in multiple scenarios.

Clone the project - object_counting_api

vehicle_counting.py - Executed this for some of my highway videos.

is_color_recognition_enabled = 1 # set it to 1 for enabling the color prediction for the detected objects
roi = 600 # roi line position
deviation = 2 # the constant that represents the object counting area
The objects passing through the line will be counted and incremented. Minor changes to roi

Output of the same



Happy Learning!!!

Analytics in Elections

If Analytics is used to target people, neutralize opinions, create digital impressions. This would lead to the creation of bias. This is not an ethical use of AI.

Analytics is needed to create affordable health care, forecast economy, provide good insights to develop people, economy jobs. AI should be used in the right sense. AI for politics will benefit in the short term but it is a curse in the long term.

When the truth is neutralized with biased facts the consequences of power with the wrong leaders will be a curse for the future generations

Keep Thinking!!!

September 29, 2019

The curse of Social Media and State of Education - Dark Future

With the growing amount of selfies, pics data shared across quora, facebook, Instagram, tiktok. We need to think of
  • Impact of the content on the psychology of kids
  • Emotional Maturity / Bias / Impact on Kids / Children
  • Loss of productivity / Distraction
  • There is considerably more bad content than the good content
  • How does these content fuel for better thinking / better focus
  • Who is responsible for moderating all the online content? How do I know as a parent the influence on my Kids
  • The state of education/mindset hasn't changed with this cheap data plans. Instead, we started losing our own individual thinking
  • This is a follow the crowd pattern approach. Developed countries do not have so much of exams but produce better research and innovation. With the state of education, we produce more failures and ship abroad few successful students
  • Changing students to merely solving problems within time constraint does not develop creativity or curiosity
  • If 90 kids fail in this education system what do they end up? Delivery boys?
  • Population can't be controlled by force. It needs education, awareness, and responsibility
Better metrics for a better future generation and better thinking towards
  • How do we make people self-sufficient
  • How has education improved scientific thinking
  • How has education improved morality
  • How has education promoted social responsibility
  • Are things affordable for the poor and needy
  • What have we done to create ideas at grassroots
Creating a better intelligent generation needs more thoughts than just about GDP

Keep Thinking!!!





September 26, 2019

The Curse of Cheap Data Plans

Many time I wonder cheap data plans are a curse, not a boom. I see more often these days
  • More time I personally spend on Youtube
  • Forwards of TikTok/ Halo Status videoes
  • Rechecking same repetitive news everywhere
I have lost a lot of sleeping hours. Google Youtube recommendation is the most unfair recommendation. Providing extremely similar recommendations. There is no mix of different sources. Sometimes tailored information is not what we need, we need the raw data.

Too much of personalization is a curse. You will lose yourself biased on your perspectives. Sometimes raw information makes more sense than tailored information.

Escape the Web!!!

Day #277 - Tracking Objects - Deep SORT

What is Deep Sort ?
Simple Online and Realtime Tracking with a Deep Association Metric

How it works ?
It performs Kalman filtering in image space and frame-by-frame data association using the Hungarian method with an association metric that measures bounding box overlap

Paper - Link


Step #1 - Clone it - https://github.com/nwojke/deep_sort
Step #2 - Download Data from - https://motchallenge.net/data/MOT16/
Step #3- Download Data from https://drive.google.com/drive/folders/1VVqtL0klSUvLnmBKS89il1EKC3IxUBVK
Step #4 - Goto Project Directory in Anaconda Prompt
Step #5 - python deep_sort_app.py --sequence_dir=C:\Data_ML\MOT16\test\MOT16-06 --detection_file=C:\Data_ML\MOT16\Data_Test/MOT16-06.npy --min_confidence=0.3 --nn_budget=100 --display=True
view raw DeepSort.txt hosted with ❤ by GitHub
Happy Learning!!!


September 25, 2019

Day # 276 - Segmentation, Age-Gender Estimations

This post is based on current learning's exploring segmentation and age/gender detection.

Experiment #1 - Used the existing model and took faces from edouardjanssens.com for both men / women from 1-100 in increments of 5. The actual result and predicted chart summary

Demo Code



#http://edouardjanssens.com/art/1-to-100-years/men/
#http://edouardjanssens.com/art/1-to-100-years/women/
import cv2
print(cv2.__version__)
import glob, os
from pyagender import PyAgender
agender = PyAgender()
def Approach1():
f = open(r'C:\Data_ML\Data_Scrub\Results.csv','w')
os.chdir(r"C:\Data_ML\Data_Scrub\Boys")
for file in glob.glob("*.*"):
faces = agender.detect_genders_ages(cv2.imread(file))
#print(faces)
print("_________________________________________________")
print(file)
for data in faces:
for key in data:
if(key=='gender' or key == 'age'):
if key=='gender':
if data[key] <0.5:
print(key)
print('Male')
val1 = 'Male'
else:
print(key)
print('Female')
val1 = 'Female'
if key=='age':
print(key)
print(data[key])
val2= data[key]
print("_________________________________________________")
data = file + "," + str(val1) + "," + str(val2) + "\n"
f.write(data)
f.close()
Experiment #2 - Getting Started with Segmentation, Experimented with Repo - https://github.com/divamgupta/image-segmentation-keras

Few tweaks in code

Demo Code
#https://github.com/divamgupta/image-segmentation-keras
import keras_segmentation
model = keras_segmentation.pretrained.pspnet_50_ADE_20K() # load the pretrained model trained on ADE20k dataset
# load 3 pretrained models
out = model.predict_segmentation(
inp=r"E:\Code_Repo\image-segmentation-keras\sample_images\Data1.jpeg",
out_fname=r"E:\Code_Repo\image-segmentation-keras\sample_images\out1.png"
)
model2 = keras_segmentation.pretrained.pspnet_101_cityscapes()
out = model2.predict_segmentation(
inp=r"E:\Code_Repo\image-segmentation-keras\sample_images\Data1.jpeg",
out_fname=r"E:\Code_Repo\image-segmentation-keras\sample_images\out2.png"
)
model3 = keras_segmentation.pretrained.pspnet_101_voc12()
out = model3.predict_segmentation(
inp=r"E:\Code_Repo\image-segmentation-keras\sample_images\Data1.jpeg",
out_fname=r"E:\Code_Repo\image-segmentation-keras\sample_images\out3.png"
)
view raw segdemo.py hosted with ❤ by GitHub
Happy Learning!!!