BackgroundThe coronavirus infection has endangered human health because of the high speed of the outbreak. A rapid and accurate diagnosis of the infection is essential to avoid further spread. Due to the cost of diagnostic kits and the availability of radiology equipment in most parts of the world, the COVID-19 detection method using X-ray images is still used in underprivileged countries. However, they are challenging due to being prone to human error, time-consuming, and demanding. The success of deep learning (DL) in automatic COVID-19 diagnosis systems has necessitated a detection system using these techniques. The most critical challenge in using deep learning techniques in diagnosing COVID-19 is accuracy because it plays an essential role in controlling the spread of the disease.MethodsThis article presents a new framework for detecting COVID-19 using X-ray images. The model uses a modified version of DenseNet-121 for the network layer, an image data loader to separate images in batches, a loss function to reduce the prediction error, and a weighted random sampler to balance the training phase. Finally, an optimizer changes the attributes of the neural networks.ResultsExtensive experiments using different types of pneumonia expresses satisfactory diagnosis performance with an accuracy of 99.81%.ConclusionThis work aims to design a new deep neural network for highly accurate online recognition of medical images. The evaluation results show that the proposed framework can be considered an auxiliary device to help radiologists accurately confirm initial screening.