Seven weeks after their frenetic retreat from Ramadi, Iraqi security forces are preparing to mount a counteroffensive in the coming weeks to try to reclaim the pivotal western Iraqi city from the Islamic State, American and Iraqi officials say.

Since the Islamic State seized Ramadi in May, about 2,000 militant fighters have been building up defenses — including rigging empty buildings with explosives.

Struggling to regain the momentum in its campaign to “degrade” and ultimately defeat the Islamic State, the militant group also known as ISIS or ISIL, the Obama administration wants the Iraqis to retake the city before the militants dig in even more.

At the Pentagon on Monday, President Obama said the fall of Ramadi was a setback that had “galvanized” the Iraqi government and accelerated an American effort that had been “moving too slowly” to better train and equip Iraqi forces, including Sunni fighters.