With increasing consumer expectations for the quality and safety of agricultural products, intelligent quality detection and gradation have considerable significance in agricultural production. The surface defect is an important indicator of quality, but is classified mainly using inefficient manual identification for “Yuluxiang” pears. Because of the uncertainty and high difficulty of image acquisition in agriculture, the data imbalance between categories is a common problem. For the resolution of these problems, the class balance (CB) was used to re-weight the sigmoid cross-entropy loss (SGM-CE), softmax cross-entropy loss (SM-CE), focal loss (FL) functions in this study. CB-SGM-CE, CB-SM-CE, and CB-FL were used to construct a GoogLeNet network as a convolutional neural network (CNN) generalized feature extractor, and transfer learning was combined to build detection models, respectively. The results showed that CB-SGM-CE, CB-SM-CE, and CB-FL were better than SGM-CE, SM-CE, and FL, respectively. CB-FL achieved the best detection results (F1 score of 0.993–1.000) in 3 CB loss functions. Then, CB-FL was used to construct VGG 16, AlexNet, SqueezeNet, and MobileNet V2 networks based on transfer learning, respectively. Machine learning (ML) and CNN were used to build classification models in this study. Compared with ML models and the other 4 CNN models, the CB-FL-GoogLeNet model achieved the best detection results (accuracy of 99.78%). A system for surface defect detection was developed. The results showed that the testing accuracy of the CB-FL-GoogLeNet model was 95.28% based on this system. This study realizes the surface defect detection of the “Yuluxiang” pear with an unbalanced dataset, and provides a method for intelligent detection in agriculture.