Hand gesture recognization

hello.i’m doing a project that classifies whether the given photo is “C” or not by using cnn algorithm but while running the model i’m getting accuracy for train set =1 and for test set = 0.27.even i have given big dataset nearly 600 images and i also keep on changing the optimizer algo but still my model is overfitting…can anyone pls tell how to overcome this

this is the code i written

import cv2 
import os 
import glob 
import matplotlib.pyplot as plt 
import numpy as np
%matplotlib inline 


img_dir = r"C:\Users\N KARTHIK\Downloads\csn291deep\train\C" 
data_path = os.path.join(img_dir,'*g') 
files = glob.glob(data_path) 
data = []
y = []
for f1 in files: 
    img = cv2.imread(f1) 
    data.append(img) 
    y.append(0)    
img_dir = r"C:\Users\N KARTHIK\Downloads\O-20221003T171350Z-001\O" 
data_path = os.path.join(img_dir,'*g') 
files = glob.glob(data_path) 
for f1 in files: 
    img = cv2.imread(f1) 
    data.append(img) 
    y.append(1)
img_dir = r"C:\Users\N KARTHIK\Downloads\csn291deep\test\C" 
data_path = os.path.join(img_dir,'*g') 
files = glob.glob(data_path) 
y_test = []
x_test = []
for f1 in files: 
    img = cv2.imread(f1) 
    x_test.append(img) 
    y_test.append(0)
img_dir = r"C:\Users\N KARTHIK\Downloads\csn291deep\test\O-20221005T180911Z-001\O" 
data_path = os.path.join(img_dir,'*g') 
files = glob.glob(data_path) 
for f1 in files: 
    img = cv2.imread(f1) 
    x_test.append(img) 
    y_test.append(1)    

classes = ["C","O"]
y = np.array(y)
x_test = np.array(x_test)
y_test = np.array(y_test)

data = data/255
import tensorflow as tf
from tensorflow.keras import layers, models,optimizers

#cnn

cnn = models.Sequential([
    layers.Conv2D(filters=96, kernel_size=(11, 11), activation='relu',strides = (3,3),input_shape=(310, 310, 3)),
    layers.MaxPooling2D((3, 3),strides = (2,2)),
    
    layers.Conv2D(filters=256, kernel_size=(5, 5),activation='relu',padding = 'same'),
    layers.MaxPooling2D((3, 3),strides = (2,2)),
    layers.Conv2D(filters=384, kernel_size=(3, 3), activation='relu',padding = 'same'),
    layers.Conv2D(filters=384, kernel_size=(3, 3), activation='relu',padding = 'same'),
    layers.Conv2D(filters=256, kernel_size=(3, 3), activation='relu',padding = 'same'),
    layers.MaxPooling2D((3, 3),strides = (2,2)),
    layers.Flatten(),
    layers.Dense(4096, activation='relu'),
    layers.Dense(4096, activation='relu'),
    layers.Dense(1, activation='sigmoid'),
])
model.compile(optimizer = 'adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])
model.fit(data, y, epochs=4)

The way you pasted your code into your message makes it unreadable, because all of the whitespace was removed.

If you want advice on your code, please post it again but use the "“preformatted text” attribute.

Note that there is a fundamental issue with the way you posed your question. You need to have both True and False labels. How do you label the false cases that are not the letter ‘C’? There are an infinite number of things that are not the letter ‘C’. This poses a problem in getting generalized results.

You should also keep in mind that 600 should not really considered a “big” dataset. 6,000,000 yes. 600,000 sure. 600? :thinking: