Yeah, your loss probably explodes, since it seems the class_weights 7 will be really high in case you are just dealing with a background image (target_sum should be zero, so you are dividing by epsilon).
Here 12 is a simple implementation from @IssamLaradji which uses a smoothing factor, which might counter this effect.