In the worldwide working-age population, visual disability and blindness are common conditions caused by diabetic retinopathy (DR) and diabetic macular edema (DME). Nowadays, due to diabetes, many people are affected by eye-related issues. Among these, DR and DME are the two foremost eye diseases, the severity of which may lead to some eye-related problems and blindness. Early detection of DR and DME is essential to preventing vision loss. Therefore, an enhanced capsule generation adversarial network (ECGAN) optimized with the rat swarm optimization (RSO) approach is proposed in this article to coincide with DR and DME grading (DR-DME-ECGAN-RSO-ISBI 2018 IDRiD). The input images are obtained from the ISBI 2018 unbalanced DR grading data set. Then, the input fundus images are preprocessed using the Savitzky-Golay (SG) filter filtering technique, which reduces noise from the input image. The preprocessed image is fed to the discrete shearlet transform (DST) for feature extraction. The extracting features of DR-DME are given to the ECGAN-RSO algorithm to categorize the grading of DR and DME disorders. The proposed approach is implemented in Python and achieves better accuracy by 7.94%, 36.66%, and 4.88% compared to the existing models, such as the combined DR with DME grading for the cross-disease attention network (DR-DME-CANet-ISBI 2018 IDRiD), category attention block for unbalanced grading of DR (DR-DME-HDLCNN-MGMO-ISBI 2018 IDRiD), combined DR-DME classification with a deep learning-convolutional neural network-based modified gray-wolf optimizer with variable weights (DR-DME-ANN-ISBI 2018 IDRiD).