In the operation and maintenance of solar photovoltaic panels, maintenance issues are one of the important factors that limit their performance and lifetime. In this paper, an optimization algorithm based on YOLOv7-GX for PV panel defect detection is proposed for the problem of multi-fault identification of PV panel images. First, a detection layer dedicated to detecting tiny targets is designed, and a prior frame suitable for the dataset of this paper is generated using k-means and genetic algorithms to improve the detection performance of very small targets and reduce the false detection rate. Second, a custom 1x1 convolutional block with GAM(Global Attention Mechanism) attention mechanism is proposed to enhance the perceptual and expressive capabilities of the model while avoiding increasing the complexity of the network structure. Then, the XIOU loss function is adopted to replace the original CIOU(Complete-IOU) loss function to enhance the robustness and convergence speed of the model. Finally, during the training process, different weights are assigned to different images and categories to solve the problem of unbalanced sample distribution, so as to achieve balanced training. Experiments show that the highest mAP of the improved method reaches 93.4% better than other existing models.