The problem might be with the dataloader,
Both the target (labels) and the data(input to model) are having different batch sizes.
Also, the “flattening of image” may be doing something wrong of which I’m not sure, try tweaking it, and do seed back .