Image segmentation model quantization method and apparatus, computer device, and storage medium
View Patent ↗Embodiments of this application disclose an image segmentation model quantization in the field of computer technologies. A method includes: acquiring a first image segmentation model, the first image segmentation model including a plurality of first operators, a concatenation layer, and a second operator; determining an input range of each of the plurality of first operators and the second operator, and updating a quantization parameter of each operator based on the input range of each operator and a target output range, so that the quantization parameter of each operator converges to a quantization parameter corresponding to the target output range; quantizing a network parameter of the concatenation layer based on the quantization parameter corresponding to the target output range, to obtain a quantized image segmentation model; and calling the quantized image segmentation model to perform an image segmentation task.
1 . An image segmentation model quantization method, the method comprising:
acquiring, by a computer device, a first image segmentation model, the first image segmentation model comprising a plurality of first operators, a concatenation layer, and a second operator, wherein outputs of the first operators are inputs of the concatenation layer, each first operator of the plurality of first operators comprises a first quantization operator and a first dequantization operator, the second operator comprises a second quantization operator and a second dequantization operator, and an output of the concatenation layer is an input of the second operator;
determining, by the computer device, an input range of each of the plurality of first operators and the second operator, and continuing iteratively updating a quantization parameter of each operator based on the input range of each operator and a target output range, until the quantization parameter of each operator converges to a quantization parameter corresponding to the target output range, the quantization parameter corresponding to the target output range used to quantize any data into the target output range;
quantizing, by the computer device, a network parameter of the concatenation layer based on the quantization parameter corresponding to the target output range, to obtain a quantized image segmentation model;
deleting, by the computer device, the first dequantization operator from each first operator, and deleting the second quantization operator from the second operator; and
calling the quantized image segmentation model to perform an image segmentation task.
2 . The method according to claim 1 , wherein the calling the quantized image segmentation model to perform the image segmentation task comprises:
capturing, by using a camera, a first original image comprising a character, and calling the quantized image segmentation model to segment the first original image, to obtain a first target image comprising a character area; or
capturing, by using the camera, a second original image comprising a face, and calling the quantized image segmentation model to segment the second original image, to obtain a second target image comprising a face area; or
capturing, by using the camera, an environment image comprising an environment element, and calling the quantized image segmentation model to segment a third original image, to obtain a third target image comprising the environment element.
3 . The method according to claim 1 , wherein continuing iteratively updating, by the computer device, the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range comprises:
updating, by the computer device, a quantization parameter of each first operator based on an input range of each first operator, an input range of the second operator, and the target output range; and
updating, by the computer device, a quantization parameter of the second operator based on the input range of the second operator and the target output range, so that the quantization parameter of each first operator and the quantization parameter of the second operator converge to the quantization parameter corresponding to the target output range.
4 . The method according to claim 1 , wherein the determining, by the computer device, the input range of each of the plurality of first operators and the second operator, and continuing iteratively updating the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range comprises:
for each of a plurality of rounds, determining, by the computer device, an input range of each operator in each of the plurality of rounds; and
updating, by the computer device, the quantization parameter of each operator based on the input range of each operator in each of the plurality of rounds, an input range of each operator in a previous round of each of the plurality of rounds, and the target output range, so that the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range.
5 . The method according to claim 4 , wherein the determining, by the computer device, the input range of each operator in each of the plurality of rounds comprises:
calling, by the computer device, the first image segmentation model to process a plurality of pieces of test data corresponding to each of the plurality of rounds, to determine the input range of each operator in each of the plurality of rounds, a plurality of pieces of test data corresponding to each round varying.
6 . The method according to claim 4 , wherein the updating, by the computer device, the quantization parameter of each operator based on the input range of each operator in each of the plurality of rounds, the input range of each operator in the previous round of each of the plurality of rounds, and the target output range, so that the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range comprises:
updating, by the computer device, a quantization parameter of each first operator based on an input range of each first operator in each of the plurality of rounds, an input range of each first operator in the previous round, an input range of the second operator in each of the plurality of rounds, an input range of the second operator in the previous round, and the target output range; and
updating, by the computer device, a quantization parameter of the second operator based on the input range of the second operator in each of the plurality of rounds, the input range of the second operator in the previous round, and the target output range, until the quantization parameter of each first operator and the quantization parameter of the second operator converge to the quantization parameter corresponding to the target output range.
7 . The method according to claim 6 , wherein the updating, by the computer device, the quantization parameter of each first operator based on the input range of each first operator in each of the plurality of rounds, the input range of each first operator in the previous round, the input range of the second operator in each of the plurality of rounds, the input range of the second operator in the previous round, and the target output range comprises:
weighting, by the computer device, the input range of each first operator in each of the plurality of rounds and the input range of each first operator in the previous round, to obtain a first input range of each first operator;
weighting, by the computer device, the input range of the second operator in each of the plurality of rounds and the input range of the second operator in the previous round, to obtain a second input range of the second operator; and
updating, by the computer device, the quantization parameter of each first operator based on the first input range of each first operator, the second input range, and the target output range.
8 . The method according to claim 7 , wherein the updating, by the computer device, the quantization parameter of each first operator based on the first input range of each first operator, the second input range, and the target output range comprises:
determining, by the computer device, a first weight parameter corresponding to each first operator, and a second weight parameter corresponding to the second operator, the first weight parameter being negatively correlated with a number of updates of the quantization parameter, and the second weight parameter being positively correlated with the number of updates;
weighting, by the computer device, the first input range of each first operator and the second input range based on the first weight parameter and the second weight parameter, to obtain a target input range of each first operator; and
updating, by the computer device, the quantization parameter of each first operator based on the target input range of each first operator and the target output range, so that an updated quantization parameter is capable of quantizing data within the target input range into the target output range.
9 . The method according to claim 8 , wherein the weighting, by the computer device, the first input range of each first operator and the second input range based on the first weight parameter and the second weight parameter, to obtain the target input range of each first operator comprises:
for each first operator, weighting, by the computer device based on a first weight parameter of the first operator and the second weight parameter, a minimum value within a first input range of the first operator and a minimum value within the second input range, to obtain a target minimum value;
weighting, by the computer device based on the first weight parameter and the second weight parameter, a maximum value within the first input range and a maximum value within the second input range, to obtain a target maximum value; and
determining, by the computer device, that a range between the target minimum value and the target maximum value is a target input range of the first operator.
10 . The method according to claim 8 , wherein the first operator comprises a first quantization parameter, and the updating, by the computer device, the quantization parameter of each first operator based on the target input range of each first operator and the target output range, so that the updated quantization parameter is capable of quantizing data within the target input range into the target output range comprises:
for each first operator, determining, by the computer device, a first difference between a maximum value and a minimum value within a target input range of the first operator;
determining, by the computer device, a second difference between a maximum value and a minimum value within the target output range; and
determining, by the computer device, that a ratio of the first difference to the second difference is an updated first quantization parameter of the first operator, the first quantization parameter indicating a scaling ratio of data.
11 . The method according to claim 10 , wherein the first operator further comprises a second quantization parameter, and the updating, by the computer device, the quantization parameter of each first operator based on the target input range of each first operator and the target output range, so that the updated quantization parameter is capable of quantizing data within the target input range into the target output range comprises:
rounding, by the computer device, a ratio of the minimum value within the target input range to the updated first quantization parameter, to obtain a reference minimum value; and
determining, by the computer device, that a difference between the minimum value within the target output range and the reference minimum value is an updated second quantization parameter of the first operator, the second quantization parameter indicating a shift magnitude of data.
12 . The method according to claim 1 , wherein determining, by the computer device, the input range of each of the plurality of first operators and the second operator, and continuing iteratively updating the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range comprises:
disabling, by the computer device, each first operator and the second operator in the first image segmentation model, determining that a plurality of input ranges of the concatenation layer are respectively third input ranges of corresponding first operators, and determining that an output range of the concatenation layer is a third input range of the second operator;
updating, by the computer device, a quantization parameter of each first operator and a quantization parameter of the second operator based on a third input range of each first operator, the third input range of the second operator, and the target output range;
enabling, by the computer device, each first operator and the second operator in the first image segmentation model, and determining a fourth input range of each first operator and a fourth input range of the second operator; and
continuing, by the computer device, to update the quantization parameter of each first operator and the quantization parameter of the second operator based on the fourth input range of each first operator, the fourth input range of the second operator, and the target output range, so that the quantization parameter of each first operator and the quantization parameter of the second operator converge to the quantization parameter corresponding to the target output range.
13 . An image segmentation model quantization apparatus comprising:
a memory storing a plurality of instructions; and
a processor configured to execute the plurality of instructions, and upon execution of the plurality of instructions, the processor is configured to:
acquire a first image segmentation model, the first image segmentation model comprising a plurality of first operators, a concatenation layer, and a second operator, wherein outputs of the first operators are inputs of the concatenation layer, each first operator of the plurality of first operators comprises a first quantization operator and a first dequantization operator, the second operator comprises a second quantization operator and a second dequantization operator, and an output of the concatenation layer is an input of the second operator;
determine an input range of each of the plurality of first operators and the second operator, and continue iteratively updating a quantization parameter of each operator based on the input range of each operator and a target output range, until the quantization parameter of each operator converges to a quantization parameter corresponding to the target output range, the quantization parameter corresponding to the target output range being used for quantizing any data into the target output range;
quantize a network parameter of the concatenation layer based on the quantization parameter corresponding to the target output range, to obtain a quantized image segmentation model;
delete the first dequantization operator from each first operator, and delete the second quantization operator from the second operator; and
call the quantized image segmentation model to perform an image segmentation task.
14 . The apparatus according to claim 13 , wherein in order to call the quantized image segmentation model to perform the image segmentation task, the processor, upon execution of the plurality of instructions, is configured to:
call the quantized image segmentation model to segment a first original image comprising a character to obtain a first target image comprising a character area; or
call the quantized image segmentation model to segment a second original image comprising a face, to obtain a second target image comprising a face area; or
call the quantized image segmentation model to segment a third original image to obtain a third target image comprising an environment element.
15 . The apparatus according to claim 13 , wherein in order to continue iteratively updating the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range, the processor, upon execution of the plurality of instructions, is configured to:
update a quantization parameter of each first operator based on an input range of each first operator, an input range of the second operator, and the target output range; and
update a quantization parameter of the second operator based on the input range of the second operator and the target output range, so that the quantization parameter of each first operator and the quantization parameter of the second operator converge to the quantization parameter corresponding to the target output range.
16 . The apparatus according to claim 13 , wherein in order to determine the input range of each of the plurality of first operators and the second operator, and continue iteratively updating the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range, the processor, upon execution of the plurality of instructions, is configured to:
for each of a plurality of rounds, determine an input range of each operator in each of the plurality of rounds; and
update the quantization parameter of each operator based on the input range of each operator in each of the plurality of rounds, an input range of each operator in a previous round of each of the plurality of rounds, and the target output range, so that the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range.
17 . A non-transitory computer-readable storage medium storing a plurality of instructions executable by a processor, wherein upon execution by the processor, the plurality of instructions is configured to cause the processor to:
acquire a first image segmentation model, the first image segmentation model comprising a plurality of first operators, a concatenation layer, and a second operator, wherein outputs of the first operators are inputs of the concatenation layer, each first operator of the plurality of first operators comprises a first quantization operator and a first dequantization operator, the second operator comprises a second quantization operator and a second dequantization operator, and an output of the concatenation layer is an input of the second operator;
determine an input range of each of the plurality of first operators and the second operator, and continue iteratively updating a quantization parameter of each operator based on the input range of each operator and a target output range, until the quantization parameter of each operator converges to a quantization parameter corresponding to the target output range, the quantization parameter corresponding to the target output range being used for quantizing any data into the target output range;
quantize a network parameter of the concatenation layer based on the quantization parameter corresponding to the target output range, to obtain a quantized image segmentation model;
delete the first dequantization operator from each first operator, and delete the second quantization operator from the second operator; and
call the quantized image segmentation model to perform an image segmentation task.
18 . The non-transitory computer-readable storage media according to claim 17 , wherein in order to determine the input range of each of the plurality of first operators and the second operator, and continue iteratively updating the quantization parameter of each operator based on the input range of each operator and the target output range, until the quantization parameter of each operator converges to the quantization parameter corresponding to the target output range, the plurality of instructions, upon execution by the processor, is configured to cause the processor to:
disable each first operator and the second operator in the first image segmentation model, determine that a plurality of input ranges of the concatenation layer are respectively third input ranges of corresponding first operators, and determine that an output range of the concatenation layer is a third input range of the second operator;
update a quantization parameter of each first operator and a quantization parameter of the second operator based on a third input range of each first operator, the third input range of the second operator, and the target output range;
enable each first operator and the second operator in the first image segmentation model, and determine a fourth input range of each first operator and a fourth input range of the second operator; and
continue to update the quantization parameter of each first operator and the quantization parameter of the second operator based on the fourth input range of each first operator, the fourth input range of the second operator, and the target output range, so that the quantization parameter of each first operator and the quantization parameter of the second operator converge to the quantization parameter corresponding to the target output range.