Here in this paper, we introduce a solution to the Phys-ioNet Challenge 2021. The method is based on the ResNetdeep neural network architecture with a multi-head atten-tion mechanism for ECG classification into 26 indepen-dent groups. The model is optimized using a mixture ofloss functions, i.e., binary cross-entropy, custom challengescore loss function, and sparsity loss function. Probabilitythresholds for each classification class are estimated us-ing the evolutionary optimization method. The final modelconsists of three submodels forming a majority voting clas-sification ensemble. The proposed method can classifyECGs with a variable number of leads, e.g., 12-lead, 6-lead, 3-lead, and 2-lead. The algorithm was trained andvalidated on the public dataset proposed for the challenge.The trained algorithm was tested using a hidden validationset during the official phase of the challenge and obtainedvalidation scores (ISIBrno-AIMT): 0.64, 0.62, 0.63, 0.63,and 0.62 for lead configurations 12, 6, 3, 4, and 2, respec-tively. The total training time was approximately 27 hours,i.e., 9 hours per model