bELOW IS A SIMPLE MODEL FOR IMAGE CLASSIFICATION OF HAND GESTURE RECOGNITION using Kaggle dataset
# -- coding: utf-8 --
"""kaggle_dataset_code.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1kfj2kPVrioXlWX_CDDOGEfxlwMUj5vs6
"""
!pip install kaggle
#You can download the kaggl.json file from your kaggle account. We are going to upload the kaggle.json file.
from google.colab import files
files.upload()
#making kaggle directory as kaggle website has guided.
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
#Giving specical permissions to the kaggle.json file.
!chmod 600 ~/.kaggle/kaggle.json
downloading the kaggle dataset from the website by copying the API token
!kaggle datasets download -d gti-upm/leapgestrecog
#Unzip the dataset
zip_data_path = "/content/leapgestrecog.zip"
from zipfile import ZipFile
file_name = "leapgestrecog.zip"
with ZipFile(file_name,'r') as zip:
zip.extractall()
print("done")
import cv2
image_data = []
CATEGORIES = ["01_palm", '02_l','03_fist','04_fist_moved','05_thumb','06_index','07_ok','08_palm_moved','09_c','10_down']
IMG_SIZE = 50
import os
unzipped_data_path = "/content/leapgestrecog/leapGestRecog/"
print(os.listdir(unzipped_data_path))
for dr in os.listdir(unzipped_data_path):
for category in CATEGORIES:
class_index = CATEGORIES.index(category)
path = os.path.join(unzipped_data_path, dr, category)
for image in os.listdir(path):
image_array = cv2.imread(os.path.join(path, image), cv2.IMREAD_GRAYSCALE)
image_data.append([cv2.resize(image_array, (IMG_SIZE, IMG_SIZE)), class_index])
#image data of a 19000th image
image_data[19000]
import random
random.shuffle(image_data)
input_data = []
label = []
for X, y in image_data:
input_data.append(X)
label.append(y)
import matplotlib.pyplot as plt # for plotting
plt.figure(1, figsize=(10,10))
for i in range(1,10):
plt.subplot(3,3,i)
plt.imshow(image_data[i][0], cmap='hot')
plt.xticks([])
plt.yticks([])
plt.title(CATEGORIES[label[i]][3:])
plt.show()
import numpy as np
input_data = np.array(input_data)
label = np.array(label)
input_data = input_data/255.0
import keras
label = keras.utils.to_categorical(label, num_classes=10,dtype='i1')
label[0]
input_data.shape = (-1, IMG_SIZE, IMG_SIZE, 1)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(input_data, label, test_size = 0.3, random_state=0)
from keras.layers import Conv2D, Activation, MaxPool2D, Dense, Flatten, Dropout
model = keras.models.Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3,3), input_shape = (IMG_SIZE, IMG_SIZE, 1)))
model.add(Activation('relu'))
model.add(Conv2D(filters = 32, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Conv2D(filters = 64, kernel_size = (3,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.3))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
model.summary()
model.fit(X_train, y_train, epochs = 7, batch_size=32, validation_data=(X_test, y_test))
score = model.evaluate(X_test, y_test, batch_size=128)
print(score)
model.save("kaggle_dataset_model.h5")
but i get the similar following error no matter which model i try
ValueError: Input 0 of layer sequential_2 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 50, 50, 3]
The code where I want the model to make predictions is below
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1PWDO7aYA6Lhl9FgdgMHh8fj-vlLF_mTw
"""
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# dimensions of our images
img_width = 50
img_height = 50
# load the model we saved
model = load_model('KaggleModelLeapGesture.h5')
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
from google.colab import files
from keras.preprocessing import image
uploaded = files.upload()
for fn in uploaded.keys():
# predicting images
path = fn
img = image.load_img(path, target_size=(50, 50))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
classes = model.predict(images, batch_size=10)
print(fn)
print(classes)
As Dr. Snoopy suggested, the model is trained on Grey scale images, but you are trying to predict on RGB image. Kindly use the grey scale version of the image.
Coming to your next question regarding the predictions, the last layer of your model is having model.add(Dense(10, activation='softmax')) - that means you have 10 class to be predicted and as you have used softmax function, it gives the probability of the image belonging to these 10 different classes. The sum of all the probability will be equal to 1.
Related
I made a neural network to recognize objects, I trained this model using 7 categories of images. When I train this model, I always get the accuracy of 0.217. Even I changed each count of neuron of each layers, still I get the accuracy of 0.217
categories of training image data
(I used open cv to convert images to arrays and used pickle to store datasets)
'create data set'
import numpy as np
import os
import cv2
import pickle
import random
datadir = r"C:\Users\pc\Desktop\Tenserflow\upgrade1\Images"
categories = []
for root, dirs, files in os.walk(datadir, topdown=False):
for name in dirs:
categories.append(name)
training_data = []
img_size = 100
def create_training_data():
for category in categories:
path = os.path.join(datadir, category)
class_num = categories.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img),cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (img_size, img_size))
training_data.append([new_array,class_num])
except Exception as e:
pass
create_training_data()
random.shuffle(training_data)
x =[]
y =[]
for features ,label in training_data:
x.append(features)
y.append(label)
x = np.array(x).reshape(-1, img_size, img_size, 1)
y = np.array(y)
file1 = open('x.pickle', 'wb')
file2 = open('y.pickle', 'wb')
pickle.dump(x, file1)
pickle.dump(y, file2)
file1.close()
file2.close()
'training code'
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Activation, Conv2D, MaxPooling2D
import pickle
import numpy as np
x =pickle.load(open("x.pickle", "rb"))
y =pickle.load(open("y.pickle", "rb"))
x = x/255.0
model = Sequential()
model.add(Conv2D(3,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(7,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(7,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(5))
model.add(Activation("relu"))
model.add(Dense(7))
model.add(Activation('softmax'))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
model.fit(x, y, epochs = 2, validation_split = 0.1)
I see two issues in your code:
1: you use categorical_crossentropy as your loss. This loss assumes that the targets are in format [1,0,0,0,0,0,0], [0,1,0,0,0,0,0], [0,0,1,0,0,0,0] etc but your dataset creation seems to have the targets as [0], [1], [2] etc.
Either you need to switch to sparse_categorical_crossentropy loss and single output feature or use to_categorical function for your targets. I would suggest using the to_categorical function so you don't need to change your network.
More info on those:
https://www.tensorflow.org/api_docs/python/tf/keras/losses/SparseCategoricalCrossentropy
https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical
2: Your network is far too simple to give you a good accuracy. You use far too few filters in your Conv2D. Try using 16, 32 and 32 filters for example. Also your Dense layer is far too small. Try using 128 for the first and 7 for the second dense layer.
I have a folder called "computerVision" inside that folder I have train_model.py, lenet.py, and another folder called datasets where i store images for dataset. When I execute the train_model.py file i get an error:
- python train_model.py --dataset ../dataset/train_folder \ -m/--model output/lenet.hdf5
- Using TensorFlow backend.
- usage: train_model.py [-h] -d DATASET -m MODEL
- train_model.py: error: the following arguments are required: -m/--model
train_model.py
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.preprocessing.image import img_to_array
from keras.utils import np_utils
from lenet import LeNet
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import imutils
import cv2
import os
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to input dataset of faces")
ap.add_argument("-m", "--model", required=True, help="path to output model")
args = vars(ap.parse_args())
# initialize the list of data and labels
data = []
labels = []
# loop over the input images
for imagePath in sorted(list(paths.list_images(args["dataset"]))):
# load teh image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = imutils.resize(image, width=28)
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the label list
label = imagePath.split(os.path.sep)[-3]
label = "smiling" if label == "1" else "not_smiling"
labels.append(label)
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
# convert the labels from integer to vector
le = LabelEncoder().fit(labels)
labels = np_utils.to_categorical(le.transform(labels), 2)
# account for skew in the labeled data
classTotals = labels.sum(axis=0)
classWeight = classTotals.max() / classTotals
# partition the data into training and testing
trainX, testX, trainY, testY = train_test_split(data, labels, test_size=0.20, stratify=labels, random_state=42)
# initialize the model
print("[INFO compiling model....")
model = LeNet.build(width=28, height=28, depth=1, classes=2)
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# train the network
print("[INFO] training network....")
H = model.fit(trainX, trainY, validation_data=(testX, testY), class_weight=classWeight, batch_size=64, epochs=15, verbose=1)
# evaluate the network
print("[INFO] evaluating network...")
predictions = model.predict(testX, batch_size=64)
print(classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=le.classes_))
# save the model to disk
print("[INFO] serializing network...")
model.save(args["model"])
lenet.py
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation, Flatten, Dense
from keras import backend as K
class LeNet:
#staticmethod
def build(width, height, depth, classes):
# initialize the model
model = Sequential()
inputShape = (height, width, depth)
# if we are using " channels first ", update the input shape
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
# first set of CONV => RELU => POOL layer
model.add(Conv2D(20, (5, 5), padding="same", input_shape=inputShape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# second set of CONV => RELU => Pool layer
model.add(Conv2D(50, (5, 5), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# first and only set FC => RELU layer
model.add(Flatten())
model.add(Dense(500))
model.add(Activation("relu"))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
return model
I'm trying to create model for facial emotion recognition.
My dataset is from "kaggle face recognition" (or sth like this).
Set is in *.csv file emotion+pixels (as a string eg. "3, pixel pixel pixel...") and I'm preprocessing it by this method:
def resizeImages(x):
x = x.astype('float32')
x = x / 255.0
return x
def loadData():
print("Data loading START")
rawData = pd.read_csv("../data/data.csv")
pixels = rawData['pixels'].tolist()
images = []
for each in pixels:
image = [int(pixel) for pixel in each.split()]
image = np.asarray(image).reshape(width, height)
images.append(image.astype('float32'))
images = np.asarray(images)
images = np.expand_dims(images, -1)
images = np.repeat(images, 3, axis=3)
emotions = pd.get_dummies(rawData['emotion'])
print(emotions)
images = resizeImages(images)
print("Data loading DONE")
return images, emotions
My model is made like this:
#Constants
width, height, depth = 48, 48, 3
numOfClasses = 7
epochs = 10
batch_size = 50
#Loading Data
from Model.DataProcess import loadData
pixels, emotions = loadData()
#Spliting Data
from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(pixels, emotions, test_size = 0.2)
#Loading VGG16 Model
from keras.applications.vgg16 import VGG16
vgg16model = VGG16(include_top=False, weights='imagenet',
input_shape=(width, height, depth), pooling='avg')
#Frezzing layers at VGG16 model - not trainable
for layer in vgg16model.layers:
layer.trainable = False
#Creating final classifier
from keras.models import Sequential
from keras.layers import Dropout, Dense
myModel = Sequential([
vgg16model,
Dense(256, input_shape=(512,), activation="relu"),
Dense(256, input_shape=(256,), activation="relu"),
Dropout(0.25),
Dense(128, input_shape=(256,)),
Dense(output_dim=numOfClasses, activation="softmax")
])
myModel.summary()
#Creating optimizer
from keras.optimizers import Adamax
adamax = Adamax()
myModel.compile(loss='categorical_crossentropy',
optimizer=adamax,
metrics=['accuracy'])
#Fiting model
history = myModel.fit(
xtrain, ytrain,
epochs=epochs,
validation_data=(xtest, ytest),
callbacks = callbacks
)
Am I doing something wrong, couse after a bunch op epchos my loss is still about 1.5 and accuracy is nearly 0.4
from keras.datasets import mnist
from keras.models import Sequential, load_model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape((X_train.shape[0], 28, 28, 1)).astype('float32')
X_test = X_test.reshape((X_test.shape[0], 28, 28, 1)).astype('float32')
X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
def larger_model():
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape=(28, 28, 1), activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(15, (3, 3), activation='relu'))
model.add(MaxPooling2D())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = larger_model()
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
scores = model.evaluate(X_test, y_test, verbose=0)
print("Large CNN Error: %.2f%%" % (100-scores[1]*100))
model.save('good_model.h5')
print("Model saved")
After running this code, we get a '.h5' model, then to predict this image
i added this code:
import cv2
model = load_model('good_model.h5')
file = cv2.imread('screenshot.png')
file = cv2.resize(file, (28, 28))
file = cv2.cvtColor(file, cv2.COLOR_BGR2GRAY)
file = file.reshape((-1, 28, 28,1))
result = model.predict(file)
print(result[0])
t = (np.argmax(result[0]))
print("I predict this number is a:", t)
But I always get the same answer which is 4. above I tried to load the image with cv and convert it to gray and then reshape to the size of the input. It takes the input correctly but the answer is always the same no matter what image I give it as input
You need to invert the image before prediction. Once you invert the image, it will predict correctly. The given example is predicting as "2" but I checked with other images such as "7" and it is correctly predicting.
file = cv2.bitwise_not(file)
Other than the above, I made one change. I imported modules from Tensorflow 2.x. Please check the full code here.
hi im new to machine learning and i just wanted to know how to make a confusion matrix from this code i just followed the instructions on youtube and i think im lost i just need to plot the confusion matrix my data sets is all about cancer and has 2 categories with and withought cancer i just followed the video of sentdex and changed his data sets
import tensorflow as tf
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy
import matplotlib.pyplot as plt
import os
import cv2
DATADIR = "C:/Users/Acer/imagerec/MRI"
CATEGORIES = ["yes", "no"]
for category in CATEGORIES:
path = os.path.join(DATADIR,category)
for img in os.listdir(path):
img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)
plt.imshow(img_array, cmap='gray')
plt.show()
break
break
print(img_array)
print(img_array.shape)
IMG_SIZE = 50
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
plt.imshow(new_array, cmap='gray')
plt.show()
training_data = []
def create_training_data():
for category in CATEGORIES:
path = os.path.join(DATADIR, category)
class_num = CATEGORIES.index(category)
for img in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
training_data.append([new_array, class_num])
except Exception as e:
pass
create_training_data()
print(len(training_data))
import random
random.shuffle(training_data)
for sample in training_data[:10]:
print(sample[1])
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = numpy.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
import pickle
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X, y, batch_size=5, epochs=1, validation_split=0.1)
model.save('64x2-CNN.model')
This will show you where the classifier predicted right/wrong on the training data (because there is not test set in your code).
from sklearn.metrics import confusion_matrix
pred = model.predict(X)
conf = confusion_matrix(y, pred)
Out[1]:
array([[5, 8], # rows are actual classes, columns are predicted classes
[9, 3]], dtype=int64)
To plot it (minimal example):
import seaborn as sns
sns.heatmap(conf, annot=True)