For ship target detection in synthetic aperture radar (SAR) images in complex environments, an algorithm based on improved YOLOv5 is proposed. First, a new feature pyramid network is proposed by using residual network thinking, which adds weighted cross-scale connections and reduces the loss of small target feature information; Secondly, the CBAM attention module suppresses the learning of background features and improves the feature learning ability of ship targets in complex backgrounds; Finally, the Soft-NMS linear penalty function is introduced to reduce the loss of the bounding box of the ship in the complex environment and improve the average accuracy. The experimental results show that the method proposed in this paper can effectively detect the ship target in the complex background, the detection speed is fast, and the missed detection rate is low.