I see a couple of things here:
1. Minding your own business and trying to upgrade everything might be a bad idea. You need to anticipate enemy attacks, and prepare your army in advance. Rome is a mo-fo. If I see Augustus anywhere near me, I know I'm going to have a fight on my hands, and when. You can almost set your watch by it. Attila and Shaka were already mentioned; I almost always have a beef with Napoleon. Bouddica can be a piece of work, too. Know who your neighbors are.
Are you building early Wonders? With rare exceptions, I tend not to build early Wonders.
Use Barbarians to get promotions without having to build a Barracks. Don't kill Barb camps unless an AI is going to do it anyway, you need to clear the space for a trade route, or a City State gives you a quest. Sometimes I'll hold off until a 2nd CS gives me a quest to wipe the same Barb Camp.
Watch AI trade routes to your cities. If they start to reassign their caravans, it's a sign they're preparing for an attack.
2. The AI is really terrible at fighting wars. I get frustrated with myself if I lose any units. "Pumping troops out and losing them all" is a catastrophe.
I just played a very interesting game as Egypt on king. I didn't have very big army, I was mostly doing some wonderspam and teching. I was being peaceful and friendly with Sweden and Portugal, my neighbors in the continent. Earlier on, I actually allied with Portugal, to kill Greece from the game. Sweden was same religion as me also.
Then suddenly my friendly civs, Sweden and Portugal, who I've signed RAs with, suddenly DoW me all at the same time. Huns and Austria from another continent also attack be at the same time. Most CS were also at war with me, except the two closest ones.
Now, at this moment when the entire world declared war on me, I was 1 turn away from infantry. I had like 2 CS allies only, it was pretty hilarious, to fight non-stop vs the entire world from plastics onwards. I won domination in the end, at about turn 240-250. Nobody agreed to a peace deal even though I was essentially massacring all their armies in the mid-portion of the war.
-sweden had rifles and cannons and lancers
-austria had small outpost cities in my continent, austria had hussars, muskets, crossbows
-Portugal suicided caravels into my coastal city and spammed crossbows vs my gatling guns and great war infantry.
-attila the hun had knights and crossbows as his best technology. I took his empire down with an elite team of 4 infantry units + 2 battleships on the coast.
I didn't have dynamite yet, and I purposely delayed dynamite, to profit more from my infantry + great wall vs hussars, crossbow, musket and rifleman.
It took quite a while to produce credible offensive army with rocket artillery, bombers and battleships but essentially it was just a trudge-fest. Nobody agreed to peace, it was just pure war until the end. LOL
A lot of units were killed, in that game.... Let's just say that. The only regret about that game was that I didn't have full honor. I was just thinking about that I would have become very rich civ with that number of unit kills.
I lost only some obsolete knights and sadly one of my foreign legions died to swedish rifleman spam. I wish you could rebuild dead foreign legions as freedom.