Using CNN to get value from dartboard where dart landed

Viewed 97

I want to use CNN in python to get values from dartboard (or the value of the field where dart landed) using pictures.

I took 208 photos of dartboard, in each dart is in specific location. I want to predict if the dart in next image is in specific field (208 pictures represent 4 classes/52 each) (single, double and triple from same field represent same number or in our case, same class.

sample dart in a field

Then i use similar picture to test model.

When I try to fit model I get something like this

208/208 [==============================] - 3s 15ms/sample - loss: 0.0010 - accuracy: 1.0000 - val_loss: 8.1726 - val_accuracy: 0.2500
Epoch 29/100
208/208 [==============================] - 3s 15ms/sample - loss: 9.8222e-04 - accuracy: 1.0000 - val_loss: 8.6713 - val_accuracy: 0.2500
Epoch 30/100
208/208 [==============================] - 3s 15ms/sample - loss: 8.5902e-04 - accuracy: 1.0000 - val_loss: 9.2214 - val_accuracy: 0.2500
Epoch 31/100
208/208 [==============================] - 3s 15ms/sample - loss: 7.9463e-04 - accuracy: 1.0000 - val_loss: 9.6584 - val_accuracy: 0.2500

As the accuracy hits 1 the val_accuracy stays the same, some previous model got me a little better result, but it was little better than this.

As I am new in the field I need some advice to get my model or whole program better.

Here is my current model_

model = Sequential()
model.add(Conv2D(32, kernel_size=3, activation='relu', input_shape=(640, 480, 3)))

model.add(MaxPooling2D(2, 2))

model.add(BatchNormalization())

model.add(Conv2D(64, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Conv2D(128, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Conv2D(256, kernel_size=3, activation='relu'))

model.add(MaxPooling2D(2, 2))

model.add(Flatten())

model.add(Dense(512, activation='relu', kernel_initializer='he_uniform'))

model.add(Dense(4, activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

history = model.fit(X, y, batch_size=16, epochs=100, validation_data=(Xtest,ytest))

AND MY SAMPLE PROGRAM

training_data = []
DATADIR = 'C:/PikadaNew'

dir = sorted(os.listdir(DATADIR), key=len)
def create_training_data():
    for category in dir:  # do dogs and cats
        path = os.path.join(DATADIR,category)
        class_num = dir.index(category) 
        for img in tqdm(os.listdir(path)): 
            try:
                img_array = cv2.imread(os.path.join(path,img))     
                training_data.append([img_array, class_num])      
            except Exception as e: 
                pass          

create_training_data()

DATATESTDIR = 'C:/PikadaNewTest'
dir1 = sorted(os.listdir(DATATESTDIR), key=len)
test_data = []

def create_test_data():
    for category in dir1:
        path = os.path.join(DATATESTDIR,category) 
        class_num = dir1.index(category) 
        for img in tqdm(os.listdir(path)):
            try:
                img_array = cv2.imread(os.path.join(path,img))  # convert to array
                test_data.append([img_array, class_num])                
            except Exception as e:
                pass

create_test_data()

#print(len(training_data))
#print(len(test_data))

X = []
y = []

Xtest = []
ytest = []

for features,label in training_data:
    X.append(features)
    y.append(label)

for features,label in test_data:
    Xtest.append(features)
    ytest.append(label)


X = np.array(X).reshape(-1, 640, 480, 3)
Xtest= np.array(Xtest).reshape(-1, 640, 480, 3)
y = np.array(y)
ytest = np.array(ytest)


y = to_categorical(y)
ytest = to_categorical(ytest)


X = X/255.0
Xtest = Xtest/255.0


X,y = shuffle(X,y)
Xtest,ytest = shuffle(Xtest,ytest)

Thanks and sorry for mistakes, i hope its understandable what i wanna to achieve Every advice is much appreciated Samo

1 Answers

You are facing an overfitting problem because your data are so small and the model in more complex than needed. you can try the following:

  • Add more data if you can.
  • Try to simplify the model by removing some layers.
  • Add dropout to the model and use regularizes.
  • Use smaller number of epochs.
Related