Your labels shape in inner training loop seems to be worng. It should be (1,2) not (2). (2) means you have output for two samples. (1,2) means, you have output for 1 sample having 2 classes (essentially a onehot vector).
Generally the training loop should contain 2 nested loops. The outer one is for epochs and the inner one should be for batch.
I am struggling to understand your inner loop though.
It seems that your data set is in shape of [n_samples, img_height, img_width] as the images are gray images.
At first you need to reshape it to [n_samples, 1, img_height, img_width].
You may want to re-write you inner loop like
for batchIdx in range(len(trainX)):
    x = trainX[batchIdx] #as your batch size is 1
    # Flatten it as you are using linear layer. You could use conv layers though
    y  = trainY[batchIdx]; # 
    # Now rest of your code.

Hope this helps