Yosemite keeps restarting or going into grey screen I have an iMac from 2011 and I have installed Yosemite as soon as it was released, without any issues before this one. 

Here is what happened: it has suddenly shut itself off, after turning it on, it goes into the loading screen, loads to about half of the loading bar and restarts itself, this happens in a loop.
I have tried internet recovery. After choosing the network, connecting, and going through the loading screen, the screen becomes grey. Same thing when I try recovery mode or when I boot from external usb device. I am able to enter the verbose mode and I was able to do system check, which did not find any issues.

Not sure what to do and whether this is a hardware or a software problem.
These symptoms imply an issue with your GPU. There is actually an ongoing class-action lawsuit against Apple regarding the AMD GPUs in the 2011 Macs (primarily MacBook Pros). I have seen the issue before in person; my sister's MacBook Pro just encountered it. The symptoms were just as you described: boot chime, Apple logo, spinning loader, grey screen, nothing else. Apple had recently started using lead-free solder for internal components in order to adhere to EU environmental regulations, but the solder is more prone to stress and cracking from rapid heat fluctuation, which can cause the GPU to slowly separate from the logic board. If this is in fact the case, you have 3 options:


Allow Apple to repair it: This will run you $310 if your machine is not under warranty (unlikely, since your machine is 3-4 years old). Apple will replace your entire logic board, which includes the GPU. The only problem with this is that they will be swapping in a logic board with the exact same lead-free solder, which one can consider to be a manufacturing defect. The new board will still likely fail in the future...which is why this issue is big enough to elicit a lawsuit.
Reball: Some 3rd-party computer repair establishments can remove the GPU from the logic board, replace the lead-free solder with lead-based solder, and fix the GPU back onto the logic board. This process is known as "reballing." It is often cheaper than the Apple repair, and more reliable (as long as the repair place is reputable, of course). This would void your warranty, but if you've read this far, yours probably expired already anyway.
Reflow: If you're willing to get your hands dirty, you can use a heat gun to melt the solder back down into place. This is called "reflowing," and is the same process used to fix the "yellow light of death" issue on PS3 consoles.


You can read more about the lawsuit here: http://time.com/3545723/apple-macbook-lawsuit/