Fire accidents continually threaten lives and property, posing a great risk to public safety. Therefore, timely, accurate fire smoke and flame detection technology provides a technical foundation for ensuring personnel and property safety. This paper proposes a deep transfer learning model that integrates attention mechanisms and pruning techniques into the DenseNet network (P-DenseNet-A-TL) for detecting fire smoke and flame targets. To reduce the computational complexity of the fire detection model, we simplified the DenseNet network structure. To improve the recognition accuracy of the fire detection model, we incorporated attention mechanisms including a channel attention module (CAM) and spatial attention module (SAM) into the pruned dense network structure. To expedite the model training process, we also introduced a transfer learning model. Furthermore, to enhance the generalization ability and robustness of the model, we created a large-scale fire smoke and flame dataset. The experimental results show that our model (P-DenseNet-A-TL) achieved a test accuracy of 99.06%, F1 score of 99.09%, area under the curve (AUC) of 0.97, and a detection speed of 756 frames per second (FPS). The comparison experimental results and ablation experimental results indicate that our method achieves high detection accuracy and efficiency. Additionally, it possesses strong generalization capability and robustness.