This is called the Hypnagogic Imagery Technique.