I am trying to create a neural network that can classify an image into a rectangle or circle and find the bounding box for learning purposes.
For this I am using tensorflow.keras
on an artificially created training set of images.
The main problem is that the loss is plateauing towards nonsensical values and I have no clue whats going wrong here.
I tried changing adding/removing layers & neurons, changed the optimizer and loss also but no improvement.so kindly suggest.
Main code snippets are given below:
blank=255*np.ones(shape=(200,200,1),dtype='uint8')
shapes=['rect','circle']
inputShape=blank.shape
x_train,y_train=[],[]
for i in range(0,1000):
img=blank.copy()
shapeChoice=choice(shapes)
if shapeChoice=='rect':
x1,y1=randrange(50,100),randrange(50,100)
x3,y3=x1+randrange(10,50),y1+randrange(10,50)
xc,yc,h,w=x1/2+x3/2,y1/2+y3/2,x3-x1,y3-y1
img=cv2.rectangle(img,(x1,y1),(x3,y3),0,-1)
y_train.append([1,0,xc,yc,w,h])
if shapeChoice=='circle':
xc,yc,R=randrange(50,100),randrange(50,100),randrange(10,50)
img=cv2.circle(img,(xc,yc),R,0,-1)
y_train.append([0,1,xc,yc,R,R])
x_train.append(img)
x_train=np.array(x_train,dtype='uint8')
y_train=np.array(y_train,dtype='uint8')
x_train=x_train/255
outputUnits=sum(y_train[0].shape)
cnn = models.Sequential([
layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=inputShape),
layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'),
layers.Conv2D(filters=16, kernel_size=(3, 3), activation='relu'),
layers.Conv2D(filters=8, kernel_size=(3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(filters=4, kernel_size=(2, 2), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(16*outputUnits, activation='relu'),
layers.Dense(8*outputUnits, activation='relu'),
layers.Dense(4*outputUnits, activation='relu'),
layers.Dense(2*outputUnits, activation='relu'),
layers.Dense(outputUnits, activation='softmax')])
cnn.compile(optimizer=optimizers.Adam(learning_rate=0.1,beta_1=0.9,beta_2=0.99),
loss='categorical_crossentropy',
metrics=['accuracy'])
history=cnn.fit(x_train, y_train, epochs=5,batch_size=5)
The output of training epochs:
Epoch 1/5
200/200 [==============================] - 22s 15ms/step - loss: nan - accuracy: 0.0510
Epoch 2/5
200/200 [==============================] - 3s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 3/5
200/200 [==============================] - 3s 16ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 4/5
200/200 [==============================] - 3s 15ms/step - loss: nan - accuracy: 0.0000e+00
Epoch 5/5
200/200 [==============================] - 3s 15ms/step - loss: nan - accuracy: 0.0000e+00