Salaam alaikoum habibi,
First of all never lose hope! There's a cure for everything and that's what's so beautiful about Islam
Now this is a habit so that's why it comes back all the time even when you stop it for some weeks. Everything can become a habit. Even praying can become a habit and it doesn't mean it's always good. It all starts with the eyes. Our Imams (a.s.) say that the eyes are the spy of the heart. Now the thing is that we can train the eyes by forcing it to not look at non-mahram when we are at school, work, walking at the street etc. But the issue will be still the mind that is used to these wrong thoughts. So it still asks the eyes for new information and views. In order to stop this whole proces it needs a lot of dedication, but eventually you will get rid off it step by step.
1. Start by forcing your eyes not to look at women who are non-mahram. When you are out on the streets just keep looking down and not look too much here and there. It will automatically bring more peace to the heart and mind. It's actually something adviced by the Holy Prophet (s) that we shouldn't let our eyes roam freely. Because everything the eyes see will be transmitted to the heart. This will be very hard at the beginning because your heart and mind are still used to it. But in order to stop this you have to look at the root of the problem and that are your eyes. They can't just roam freely and they have to understand this. So disciplining the eyes is your main goal. The mind and heart will follow eventually, because the mind will receive less 'bad' views from the eyes and the heart will find peace. Allah says that remembering Him will put the hearts and ease and will give it comfort so imagine what will happen to the heart when we do the opposite of remembering Allah. It will be stressed, overloaded with trash and discomfort.
2. Reduce your speech (step by step). There is nothing wrong with talking but only when it is meaningful. Try this step by step and you will notice difference in the heart and mind.
3. Eat less (step by step). Don't overload your stomac with food because a full stomac all the time will stimulate your desires and bad behaviour. Eat what you need and khalas. Also this step by step.
4. Sleep less (step by step). Try to wake up little bit before Fajr and pray 1 rak'aat Witr. It's the last rak'aat of salatul layl. This salaat will be the key you need for Dunya and Akhira. Now maybe we have hard times by waking up before Fajr so let alone waking up a little earlier to pray nawafil. So if you are having hard times waking up before Fajr then first start with doing your best to pray Dohr, Asr, Maghreb and 'Isha on time. This will give you the Tawfeeq (success) to wake up for Fajr. It's the advice of Ayatollah Bahjat (r.a.).
5. Do you have any hobbies what you can relate to the Ahlulbayt (a.s.)? Like poetry or something. Try to do it more often.
6. Observe yourself more and more. This needs a lot of practice, but it is possible to observe yourself while you are performing an act. Eveluate your day before sleeping. Remember your sins, ask forgiveness for it and try to not commit them anymore. This is the meaning of Astaghafirullahi Rabbi Wa Atubu Ilayhi. Verily, I seek the forgiveness of Allah, who is my Lord and Sustainer, and I turn to Him in repentance. Turning to Him means we will not commit that sin anymore.
7. Remember Allah (s.w.t.) more and more even by just talking to Him in your mind when you are walking on the street or at school etc. Let your mind get used to these Divine thoughts.
Be dedicated bro and don't be too hard on yourself. You have to fight this step by step. It will take time.
Like mentioned in point 4. Read about salatul layl. Here is a link with explanation and below is the video in which it is explained step by step. Believe me salatul layl is the solution for your worldy problems and a rescuer for your afterlife.
Please try step by step and never lose hope. Allah is all merciful and forgiving.