J.M. Smucker Co. agreed to buy Ainsworth Pet Nutrition in a deal valued at $1.9 billion, betting that pet food can help reinvigorate sales in a sluggish consumer-product industry.

The transaction, which gives Smucker a brand of premium dog and cat food backed by celebrity chef Rachael Ray, will amount to about $1.7 billion when excluding a $200 million tax benefit. The company also confirmed that it’s exploring the sale of its U.S. baking unit, which generates roughly $370 million in annual sales.

Smucker is coping with a consumer shift away from longstanding supermarket staples, a trend that contributed to a sales decline last year. In this tough environment for human fare, dog and cat food has been a bright spot — especially as Americans increasingly treat their animals like family members.

Smucker first pushed into the pet business in 2015 with a $5.8 billion acquisition of Big Heart Pet Brands. The company, best known for its namesake jam, also produces Folgers coffee and Jif peanut butter.

Under Chief Executive Officer Mark Smucker, who became the fifth generation of his family to run the business when he took over in 2016, the company has tried to take the pet business more upscale. High-end pet food has surged 33 percent industrywide over the past five years and now accounts for more than 50 percent of the market. Consumers are shelling out more money for specialty diets, part of a trend known as “humanization.” Ainsworth generates about two-thirds of its sales from its Rachel Ray Nutrish brand.

“The humanization trend is here to stay,” the CEO said in an interview. “This brand fits exactly where we need it to — it’s very important to continue to gain scale where it’s relevant.”

In addition to the Ainsworth acquisition, Orrville, Ohio-based Smucker said it’s looking at a potential sale of the U.S. baking unit as it reshapes its portfolio to focus on coffee, pet products and snacks. The division includes brands like Pillsbury, Martha White, Hungry Jack and Jim Dandy. Bloomberg reported last month that the operation could fetch as much as $700 million in a sale, citing people familiar with the situation.

Smucker generates about 85 percent of its revenue in the U.S. and the acquisition of Ainsworth will make pet food its largest business unit, accounting for about $3 billion in sales.

Smucker is the latest food company to tap into the upscale pet market. General Mills Inc., mired in a three-year sales slump, agreed in February to buy Blue Buffalo Pet Products Inc. for about $8 billion.

Pet products also give packaged-food companies a way to access the growth of e-commerce, a key initiative in the industry as Walmart Inc. and Kroger Co. ramp up the delivery of groceries in the aftermath of Amazon.com Inc.’s acquisition of Whole Foods Market.

Online sales currently only account for about 3 percent of Smucker’s business. But the category is growing fast: In the first three quarters of the current fiscal year, pet sales through e-commerce have surged 70 percent, according to the company.

“As consumers shift online, we have to get our fair share,” Mark Smucker said.