Accurate heading estimation is the foundation of numerous applications, including augmented reality, pedestrian dead reckoning, and human-computer interactions. While magnetometer is a key source of heading information, the poor accuracy of consumer-grade hardware coupled with the pervasive magnetic disturbances makes accurate heading estimation a challenging issue. Heading error is one of the main error sources of pedestrian dead reckoning. To reduce the heading error and enhance robustness, we proposed a novel heading estimation method based on Spatial Transformer Networks (STNs) and Long Short-Term Memory (LSTM), termed DeepHeading, which uses sensors embedded in a smartphone without any historical training data or dedicated infrastructure. We automatically annotate heading data based on map matching, and augment heading data based on device attitude. We leverage the STNs to align the device coordinate system and the navigation coordinate system, allow an unconstrained use of smartphones. Based on the characteristics of pedestrian heading continuity, we designed a hierarchical LSTM-basedSeq2Seq model to estimate the walking heading of the pedestrian. We conducted well-designed experiments to evaluate the performance of deepheading and compared it with the state-of-the-art heading estimation algorithms. The experimental results on real-world demonstrated that deepheading outperformed the compared heading estimation algorithms and achieved promising estimation accuracy with a median heading error of 4.52 • , mean heading error of 6.07 • and heading error of 9.18 • at the confidence of 80% when a pedestrian is walking in indoor environments with magnetic field disturbances. The proposed method is high-efficiency and easy to integrate with various mobile applications. INDEX TERMS Indoor positioning, heading estimation, pedestrian dead reckoning, deep learning.