Pet dogs are our good friends. Realizing the dog’s emotions through the dog's facial expressions is beneficial to the harmonious coexistence between human beings and pet dogs. This paper describes a study on dog facial expression recognition using convolutional neural network (CNN), which is a representative algorithm model of deep learning. Parameter settings have a profound impact on the performance of a CNN model, improper parameter setting will make the model exposes several shortcomings, such as slow learning speed, easy to fall into local optimal solution, etc. In response to these shortcomings and improve the accuracy of recognition, a novel CNN model based on the improved whale optimization algorithm (IWOA) called IWOA–CNN is applied to complete this recognition task. Unlike human face recognition, a dedicated face detector in Dlib toolkit is utilized to recognize the facial region, and the captured facial images are augmented to build an expression dataset. The random dropout layer and L2 regularization are introduced into the network to reduce the number of transmission parameters of network and avoid over fitting. The IWOA optimizes the keep probability of the dropout layer, the parameter λ of L2 regularization and the dynamic learning rate of gradient descent optimizer. Carry out a comparative experiment of IWOA–CNN, Support Vector Machine, LeNet-5 and other classifiers for facial expression recognition, its results demonstrate that the IWOA–CNN has better recognition effect in facial expression recognition and also explain the efficiency of the swarm intelligence algorithm in dealing with model parameter optimization.