python – Detecting junctions from rough line drawings by ignoring the rough lines


My code is to detect junctions from rough line drawings by ignoring the rough lines. I have tried to do the coding but the result is not satisfied. My problem is that I couldn’t have a good result yet. Could anyone help me, please?
Part 1 – Building the CNN
importing the Keras libraries and packages
import keras
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, Dropout
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

def myfun(image):
img = np.array(image)
grayscal = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
ret, bin_image = cv2.threshold(grayscal, 127, 255, cv2.THRESH_BINARY)
return Image.fromarray(bin_image)
Initialing the CNN

classifier = Sequential()
Step 1 -Convolution Layer
classifier.add(Convolution2D(32, (3, 3), input_shape=(28, 28, 1), activation=’relu’))
step 2 – Pooling
classifier.add(MaxPooling2D(pool_size=(2, 2)))
Adding second convolution layer
classifier.add(Convolution2D(32, (3, 3), activation=’relu’))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
Adding 3rd Convolution Layer
classifier.add(Convolution2D(64, (3, 3), activation=’relu’))
classifier.add(MaxPooling2D(pool_size=(2, 2)))
Step 3 – Flattening
classifier.add(Flatten())
Step 4 – Full Connection
classifier.add(Dense(256, activation=’relu’))
classifier.add(Dropout(0.5))
classifier.add(Dense(6, activation=’softmax’))
Compiling The CNN
classifier.compile(
optimizer=optimizers.SGD(lr=0.01),
loss=’categorical_crossentropy’,
metrics=(‘accuracy’))
Part 2 Fittting the CNN to the image

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

training_set = train_datagen.flow_from_directory(
‘traininglast2′,
target_size=(28, 28),
batch_size=32,
color_mode=’grayscale’,
class_mode=’categorical’)
print(training_set)
test_set = test_datagen.flow_from_directory(
‘testinglast’,
target_size=(28, 28),
batch_size=32,
color_mode=’grayscale’,
class_mode=’categorical’)

model = classifier.fit_generator(
training_set,
steps_per_epoch=10000,
epochs=50,
validation_data=test_set,
validation_steps=6799 # #number of testing images/batch size
)

”’#Saving the model
import h5py”’

classifier.save(‘model5.h5’)
print(model.history.keys())

summarize history for accuracy

plt.plot(model.history(‘accuracy’))
plt.plot(model.history(‘val_accuracy’))
plt.title(‘model.accuracy’)
plt.ylabel(‘accuracy’)
plt.xlabel(‘epoch’)
plt.legend((‘train’, ‘test’), loc=’upper left’)
plt.show()

plt.plot(model.history(‘loss’))
plt.plot(model.history(‘val_loss’))
plt.title(‘model loss’)
plt.ylabel(‘loss’)
plt.xlabel(‘epoch’)
plt.legend((‘train’, ‘test’), loc=’upper left’)
plt.show()
this is one of line drawing that I have done until now