image 1 contains type A molecule. image 2 contain only type B molecule . image 3 contain both A and B.By training model based on image 1 and 2 . I want to classify molecules in image 3 .But not good accuracy is coming
import numpy as np
import cv2
import matplotlib.pyplot as plt
import numpy
from skimage import exposure
from skimage.filters import try_all_threshold
import imutils
from image2 import *
from keras.utils.np_utils import to_categorical
def make_dimension_same(lst): # [image1,image2]
foo = list(map(lambda x : x.shape,lst))
shapes = [[val[0],val[1],val[2]] for val in foo]
shapes = np.array(shapes)
shapes_min = tuple(shapes.min(axis = 0)[:2])
shapes_min_list = [shapes_min for i in range(len(lst))]
all_picture_original_resized = list(map(cv2.resize,lst,shapes_min_list))
#return(list(map(lambda x : x.shape,all_picture_original_resized)))
return(all_picture_original_resized)
def preprocess_image(image): # a single image--> a ndnumpy array
image_copy = image.copy()
image_shape = image.shape[:2] #
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
r,g,b = cv2.split(image_rgb)
equalize1 = cv2.equalizeHist(r)
equalize2 = cv2.equalizeHist(g)
equalize3 = cv2.equalizeHist(b)
equalize = cv2.merge((r,g,b))
gray = cv2.cvtColor(equalize, cv2.COLOR_BGR2GRAY)
clahe = cv2.createCLAHE(clipLimit = 5)
final_img = clahe.apply(gray)
ret, binary = cv2.threshold(final_img, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
contours,hierarchy = cv2.findContours(binary, mode = cv2.RETR_LIST, method=cv2.CHAIN_APPROX_NONE)
contours_filtered = list(filter(lambda x: cv2.contourArea(x) < 29500 and cv2.contourArea(x) > 1000,contours))
list_of_contour_images = []
for c in contours_filtered:
x,y,w,h = cv2.boundingRect(c)
#cv2.rectangle(image1_copy, (x, y), (x + w, y + h), (0,0,255), 2)
ROI = image_copy[y:y+h, x:x+w]
list_of_contour_images.append(ROI)
#foo = list(map(lambda x: x.shape,list_of_contour_images))
#x = [val[0] for val in foo]
#y = [val[1] for val in foo]
#x_mean = int(np.mean(x))
#y_mean = int(np.mean(y))
#dim = (x_mean,y_mean)
return(list_of_contour_images)
# Reading Image
picture11 = cv2.imread(r'PictureA1.jpg')
picture12 = cv2.imread(r'PictureA2.jpg')
picture13 = cv2.imread(r'PictureA3.jpg')
picture31 = cv2.imread(r'PictureB1.jpg')
picture32 = cv2.imread(r'PictureB2.jpg')
picture33 = cv2.imread(r'PictureB3.jpg')
response = [0,0,0,1,1,1]
all_picture = [picture11,picture12,picture13,picture31,picture32,picture33]
all_picture_original = all_picture.copy()
data = list(map(preprocess_image,all_picture_original))
# PREPARING RESPONSE VECTOR
foo = list(map(len,data))
Response = list(map(np.repeat,response,foo))
Response = list(map(list,Response))
Response = reduce(lambda x,y:x+y,Response)
flatten_list_of_all_contours = []
for sublist in data:
for val in sublist:
flatten_list_of_all_contours.append(val)
foo = list(map(lambda x: x.shape,flatten_list_of_all_contours))
x = [val[0] for val in foo]
y = [val[1] for val in foo]
x_mean = int(np.mean(x))
y_mean = int(np.mean(y))
dim = (x_mean,y_mean)
Dat = list(map(lambda x : cv2.resize(x,dim),flatten_list_of_all_contours))
Dat = np.array(Dat).reshape(len(Dat),x_mean,y_mean,3)
X = Dat
y = np.array(Response)
y = to_categorical(y)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,shuffle = True)
from keras.models import Sequential
model = Sequential()
from keras.layers import Dense,Conv2D,Flatten, MaxPool2D
shape = (x_mean,y_mean,3)
model.add(Conv2D(32,kernel_size = 3,activation = 'relu',input_shape = shape,
padding = 'valid'))
#model.add(MaxPool2D(2))
model.add(Conv2D(10,kernel_size = 3,activation = 'relu'))
model.add(Flatten())
model.add(Dense(100,activation = 'relu'))
model.add(Dense(10,activation = 'relu'))
model.add(Dense(2,activation = 'softmax'))
model.compile(optimizer='adam',
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
model.fit(X_train,y_train,epochs = 5)
from keras.callbacks import EarlyStopping
#early_stopping_monitor = EarlyStopping(patience = 3)
#model.fit(X_train, y_train, validation_split = 0.1,epochs = 10,callbacks = [early_stopping_monitor])
model.fit(X_train, y_train,epochs = 5)
model.evaluate(X_test,y_test)
Any suggestion is highly apperciated