Plant pests and diseases play a signifcant role in reducing the quality of agricultural products. As one of the most important plant pathogens, pests like Mediterranean fruit fly cause signifcant damage to crops and thus annually farmers face a lot of loss in their products. Therefore, the use of modern and non-destructive methods such as machine vision systems and deep learning for early detection of pests in agricultural products is of particular importance. In this study, citrus fruit images were taken in three stages: 1) before pest infestation, 2) beginning of fruit infestation, and 3) eight days after the second stage, in natural light conditions (7000–11,000 lux). A total of 1519 images were prepared for all classes. To classify the images, 70% of the images were used for the network training stage, 10% and 20% of the images were used for the validation and testing stages. Four pre-trained CNN models, namely ResNet-50, GoogleNet, VGG-16 and AlexNet as well as the SGDm, RMSProp and Adam optimization algorithms were used to identify and classify healthy fruit and fruit infected with the Mediterranean fly. The results of evaluating the models in the pest outbreak stage showed that the VGG-16 model with the help of SGDm algorithm had the best effciency with the highest detection accuracy and F1 of 98.33% and 98.36%, respectively. The evaluation of the third stage showed that the AlexNet model with the help of SGDm algorithm had the best result with the highest detection accuracy and F1 of 99.33% and 99.34%, respectively. AlexNet model using SGDm optimization algorithm had the shortest network training time (323 s). The results of this study showed that convolutional neural network method and machine vision system can be effective in controlling and managing pests in orchards and other agricultural products.