I am working in a project name "Handwritten Math Evaluation"
SO what basically happen in this is that there are 11 classes of (0 - 9) and (+,-) each containing 50 clean handwritten digits in them. Then I trained a CNN model for it with 80 % of data used in training and 20 % using in testing of model which lead in a accuracy of 98.83 %. Here is the code for the architecture of CNN model :-
import pandas as pd
import numpy as np
import pickle
np.random.seed(1212)
import keras
from keras.models import Model
from keras.layers import *
from keras import optimizers
from keras.layers import Input, Dense
from keras.models import Sequential
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 import backend as K
from keras.utils.np_utils import to_categorical
from keras.models import model_from_json
import matplotlib.pyplot as plt
model = Sequential()
model.add(Conv2D(30, (5, 5), input_shape =(28,28,1), activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Conv2D(15, (3, 3), activation ='relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation ='relu'))
model.add(Dense(50, activation ='relu'))
model.add(Dense(12, activation ='softmax'))
# Compile model
model.compile(loss ='categorical_crossentropy',
optimizer ='adam', metrics =['accuracy'])
model.fit(X_train, y_train, epochs=1000)
Now each image in dataset is preprocesed as follows:-
import cv2
im = cv2.imread(path)
im_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rect = rects[0]
im_crop =im_th[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]]
im_resize = cv2.resize(im_crop,(28,28))
im_resize = np.array(im_resize)
im_resize=im_resize.reshape(28,28)
I have made an evaluation function which solves simple expression like 7+8 :-
def evaluate(im):
s = ''
data = []
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
boundingBoxes = [cv2.boundingRect(c) for c in ctrs]
look_up = ['0','1','2','3','4','5','6','7','8','9','+','-']
i=0
for c in ctrs:
rect = boundingBoxes[i]
im_crop = im_th[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
im_resize = cv2.resize(im_crop,(28,28))
im_resize = np.array(im_resize)
im_resize = im_resize.reshape(28,28,1)
data.append(im_resize)
i+=1
data = np.array(data)
predictions = model.predict(data)
i=0
while i<len(boundingBoxes):
rect = boundingBoxes[i]
print(rect[2],rect[3])
print(predictions[i])
s += look_up[predictions[i].argmax()]
i+=1
return s
I need help extending this thought for Compund fractions but the problem is that they are identical to subtraction sign when resized to (28 , 28) so I need help in distuingish between them.
This is my first Question so please tell if any detail is left.
Related
I'm still new to deep learning and CNN and I don't know what this error is so anyone can help me?
This model is designed to take input of image array and output is a list of items and each items contains an int,int,float and I cannot make a model than contains no error.
The error
ValueError: Dimensions must be equal, but are 162 and 3 for '{{node mean_squared_error/SquaredDifference}} = SquaredDifference[T=DT_FLOAT](sequential/dense_1/Relu, mean_squared_error/Cast)' with input shapes: [?,162], [?,54,3].
The code
import numpy as np
import os
from skimage import io
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
def SeperateDataFrameItems(imagesArray, maxCount):
tempList = []
finalList = []
ct = 0
for i in range(len(imagesArray)):
if imagesArray[i][0] != '#':
tempList.append(np.array([imagesArray[i][1], imagesArray[i][2], imagesArray[i][3]]))
ct = ct + 1
else:
for i in range(ct, maxCount):
tempList.append(np.array([-1,-1,-1]))
finalList.append(tempList)
tempList = []
ct = 0
return np.stack(finalList, axis=0)
def HandleDesigndata(start, end):
path = 'Designs/designs/'
all_images = []
for image_path in os.listdir(path):
img = io.imread(path + image_path, as_gray=True)
img = img.reshape([768, 607, 1])
all_images.append(img)
return np.array(all_images)
image_list = HandleDesigndata(100, 200)
circles_data = pd.read_csv('Data.csv')
circles_data = SeperateDataFrameItems(circles_data.to_numpy(), 54)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(768, 607, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(162, activation='relu'))
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
model.fit(x=image_list, y=circles_data, batch_size=100, epochs=10, validation_split=0.1)```
You have to add a final dense layer before the compiler. And the n_number is the number of the output of your model:
model.add(Dense(n_number,activation='sigmoid'))
I'm working on a neural network and one of the pieces I want to use is an autoencoder. However for some reason the training losses remain stuck at around 600.000 which is roughly equivalent to a loss of about 0.5 per pixel which seems to heavily indicate that no learning is taking place and that the results are somehow completly random/always the same. Anybody notice what I'm doing wrong?
Full code:
import tensorflow
import keras
import pandas as pd
import numpy as np
import random
import os
import cv2
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import Model, Sequential
from keras.layers import Dense, Conv2D, Dropout, BatchNormalization, Input, Reshape, Flatten, Conv2DTranspose, MaxPooling2D, UpSampling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import Adam
TrainingDirectory="trainingdata"
modelPath = "models/autoencoder1"
def main():
model = make_model()
model.fit_generator(mygenerator(),steps_per_epoch=2000,epochs=5)
model.save(modelPath)
#ENCODER
def make_model():
input_layer = Input(shape=(None,None, 3))
# encoder
h = Conv2D(64, (3, 3), activation='relu', padding='same')(input_layer)
h = MaxPooling2D((2, 2), padding='same')(h)
# decoder
h = Conv2D(64, (3, 3), activation='relu', padding='same')(h)
h = UpSampling2D((2, 2))(h)
output_layer = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(h)
#assemble network
ae = Model(input_layer, output_layer)
ae.summary()
ae.compile(optimizer="adam", loss="mse")
return ae
def Preprocess(image):
resized_image = scale_down(image)
return resized_image
def scale_down(image):
max_size= max(image.shape)
scale=1;
if(max_size>750):
scale=750/max_size
width = int(image.shape[1] * scale)
height = int(image.shape[0] * scale)
if width%72!=0:
width=width+(72-width%72)
if height%72!=0:
height=height+(72-height%72)
dsize = (width, height)
image=cv2.resize(image, dsize)
return image
def mygenerator():
batch_features=None
batch_labels=None
while True:
target=random.choice(os.listdir(TrainingDirectory))
print(target)
batch_features=Preprocess(cv2.imread(TrainingDirectory+'/'+target+"/input.jpg", cv2.IMREAD_COLOR))
batch_labels=Preprocess(cv2.imread(TrainingDirectory+'/'+target+"/labelWalls.png", cv2.IMREAD_COLOR))
print(batch_features.shape)
yield np.array([batch_features]), np.array([batch_features])
main()
I want to make Class activation map, so I have write the code
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=30, batch_size=256, shuffle=True, validation_split=0.3)
works fine, so I have imported visualize_cam module
from vis.visualization import visualize_cam
import matplotlib.pyplot as plt
import numpy as np
for i in range(10):
ind = np.where(y_test == i)[0][0]
plt.subplot(141)
plt.imshow(x_test_resized[ind].reshape((28,28)))
for j,modifier in enumerate([None, 'guided', 'relu']):
heat_map = visualize_cam(model, 4, y_test[ind], x_test_resized[ind], backprop_modifier=modifier)
plt.subplot(1,4,j+2)
plt.imshow(heat_map)
plt.show()
but the visualize_cam didn`t work well
I tried many times to fix the module but it doesn`t go well
(it depends on scipy which version is below 1.3. but )
so I have to implement cam without that module
Is there any solution to replace visualize_cam into other option to implement CAM?
Here is a scipy library independent implementation.
from keras.datasets import mnist
from keras.layers import Conv2D, Dense, GlobalAveragePooling2D
from keras.models import Model, Input
from keras.utils import to_categorical
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train_resized = x_train.reshape((60000, 28, 28, 1))
x_test_resized = x_test.reshape((10000, 28, 28, 1))
y_train_hot_encoded = to_categorical(y_train)
y_test_hot_encoded = to_categorical(y_test)
inputs = Input(shape=(28,28, 1))
x = Conv2D(64, (3,3), activation='relu')(inputs)
x = Conv2D(64, (3,3), activation='relu', name='final_conv')(x)
x = GlobalAveragePooling2D()(x)
predictions = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train_resized, y_train_hot_encoded, epochs=1, batch_size=256, shuffle=True, validation_split=0.3)
import numpy as np
import cv2
import io
import requests
from PIL import Image
import matplotlib.pyplot as plt
# Using Keras implementation from tensorflow
from tensorflow.python.keras import applications
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
from tensorflow.python.keras import backend as K
# Get the layer of the last conv layer
fianlconv = model.get_layer('final_conv')
# Get the weights matrix of the last layer
weight_softmax = model.layers[-1].get_weights()[0]
# Function to generate Class Activation Mapping
HEIGHT = 28
WIDTH = 28
def returnCAM(feature_conv, weight_softmax, class_idx):
size_upsample = (WIDTH, HEIGHT)
# Keras default is channels last, hence nc is in last
bz, h, w, nc = feature_conv.shape
output_cam = []
for idx in class_idx:
cam = np.dot(weight_softmax[:, idx], np.transpose(feature_conv.reshape(h*w, nc)))
cam = cam.reshape(h, w)
cam = cam - np.min(cam)
cam_img = cam / np.max(cam)
cam_img = np.uint8(255 * cam_img)
output_cam.append(cv2.resize(cam_img, size_upsample))
return output_cam
x = x_test_resized[0,:,:,0]
plt.imshow(x)
plt.show()
classes = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 0:'0'}
probs_extractor = K.function([model.input], [model.output])
features_conv_extractor = K.function([model.input], [fianlconv.output])
probs = probs_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blob = features_conv_extractor([np.expand_dims(x, 0).reshape(1,28,28,1)])[0]
features_blobs = []
features_blobs.append(features_blob)
idx = np.argsort(probs)
probs = np.sort(probs)
for i in range(-1, -6, -1):
print('{:.3f} -> {}'.format(probs[0, i], classes[idx[0, i]]))
CAMs = returnCAM(features_blobs[0], weight_softmax, [idx[0, -1]])
heatmap = cv2.applyColorMap(cv2.resize(CAMs[0], (28, 28)), cv2.COLORMAP_JET)
result = heatmap[:,:,0] * 0.3 + x * 0.5
print(result.shape)
plt.imshow(result)
plt.show()
N.B: I'm plotting normalized images so the result isn't great, I also trained only for 1 epoch. For better results, you may try training more, change to appropriate color space.
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)
This is my first time training a model in cnn and predicting results but I am getting same value for images I have input. Here is my code
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.optimizers import Adam
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D,
GlobalAveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
classifier=Sequential()
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train/=255
X_test/=255
number_of_classes = 10
Y_train = np_utils.to_categorical(y_train, number_of_classes)
Y_test = np_utils.to_categorical(y_test, number_of_classes)
classifier.add(Convolution2D(32,3,3,input_shape=
(28,28,1),activation='relu'))
classifier.add(BatchNormalization(axis=-1))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Convolution2D(32,3,3,activation='relu'))
classifier.add(BatchNormalization(axis=-1))
classifier.add(MaxPooling2D(pool_size=(2,2)))
classifier.add(Flatten())
classifier.add(Dense(output_dim=256,activation='relu'))
classifier.add(BatchNormalization())
classifier.add(Dense(output_dim=10,activation='softmax'))
classifier.compile(optimizer='adam',loss='categorical_crossentropy',metrics=
['accuracy'])
gen = ImageDataGenerator(rotation_range=8, width_shift_range=0.08,
shear_range=0.3, height_shift_range=0.08, zoom_range=0.08)
test_gen = ImageDataGenerator()
train_generator = gen.flow(X_train, Y_train, batch_size=64)
test_generator = test_gen.flow(X_test, Y_test, batch_size=64)
classifier.fit_generator(train_generator, steps_per_epoch=60000, epochs=1,
validation_data=test_generator, validation_steps=10000)
import cv2
image = cv2.imread("pitrain.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
ret,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV)
#threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
im2,contours, hierarchy =
cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# get contours
# for each contour found, draw a rectangle around it on original image
for contour in contours:
# get rectangle bounding contour
[x,y,w,h] = cv2.boundingRect(contour)
# discard areas that are too large
if h>300 and w>300:
continue
# discard areas that are too small
if h<40 or w<40:
continue
# draw rectangle around contour on original image
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
image.shape
image=image[:,:,:1]
newimg = cv2.resize(image,(28,28))
img.shape
img = np.reshape(newimg,[1,28,28,1])
cv2.imshow("screen",img)
classifier.predict(img)
The output I am getting is array of zeros with 1 at third position.
This is where I copied the contour part from https://www.quora.com/How-do-I-extract-a-particular-object-from-images-using-OpenCV
Epoch is equal to 1 because I only wanted to test my model and still I got accuracy of above 99%