0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CRF層を組み込んだ画像キャプション生成モデルのGRPO強化学習におけるn-gramの繰り返しおよびキャプション長の改善

0
Last updated at Posted at 2026-07-01

はじめに

現在、文章を生成する際に用いられる主力は、Transformer Decoder を自己回帰的に使うモデルである。しかし、自己回帰的な計算では、Transformer Decoderの計算回数は生成する文章のシーケンス長となり、計算資源と計算時間が必要になり、高価なシステムが必要になる。生成 AI が広まるにつれ安価なシステム構築要求も増えることが予想される。このためには、非自己回帰的な計算手法の開発が必要となる。非自己回帰的な計算手法は、一度だけ計算すれば良く、省資源で短い時間で計算可能である。

そんな中、CRF層を考慮した画像キャプショニングモデル

CLIP + MLP_Connector + BERT + CRF-層

の教師あり事前学習(SPT, supervised pre-training )を行い、SPT をもとに GRPO 強化学習を行った。SPT だけでは、n-gram の繰り返しが見られ、キャプションの長さが短かかった。強化学習により、n-gram の繰り返しを減らし、キャプション長を改善することができたので、報告する。このモデルでは、キャプションを生成するための繰り返し計算は行わず、推論計算は十分、高速に行える。

CRF 層で シーケンス i- 1 から シーケンス i へのトークンの遷移確率を扱うので、まともに計算すると計算資源と計算量(時間)が大きくなる。そこで、CRF 層で扱う語彙数をbert の出力の放出確率が 上位 256 の語彙に絞って計算を行った。CRF 層の top-k 近似である。また、top-k 近似を行っても遷移確率が 256 * 256 の行列となり計算資源を消費するため、low_rank = 32 を導入して、遷移行列を ( 256 * 32 ) * ( 32 * 256 ) と 2つの ( 256 * 32 ) 行列で近似した。low-rank 近似である。

プログラムを開発した時期が、1年くらい前でしたので、もとのプログラムは HF のtransformers 4系対応でしたが、計算が一段落して時間がとれたので、transformers 5系対応に改修して計算しています。

SPT の結果

SPT については、

が参考になるかもしれません。transformers 5系対応で計算し直しました。

テストデータについての各スコアの値

cider           0.919
rougeL          0.514
clip_score      0.280
bert_score      0.837
repeat_count    2.26
length_penalty -1.70 * e-3

repeat_count は n-gram repeat が多いと大きい値、repeat が少ないと小さい値である。

length_penalty について説明する。生成したキャプションの長さと教師キャプションの長さを固定の最大キャプション長 97 で割って 1 にスケーリングする。length_penalty は、両者の差を二乗したものに -1 を乗じたものの平均値である。0に近いほど生成したキャプションが教師キャプションの長さに近い。

生成されたキャプション

写真 1
Screenshot from 2026-07-01 09-55-21.png

hypo: [CLS] a dog that is playing its floor with its floor. [SEP]
refe: [CLS] a golden retriever eating something on a hardwood floor. [SEP]

写真 2
Screenshot from 2026-07-01 09-57-15.png

hypo: [CLS] a white plate topped with a plate on a plate. [SEP]
refe: [CLS] a plate with a couple of sandwiches on it [SEP]
hypo: [CLS] a dog that is playing its floor with its floor. [SEP]
refe: [CLS] a golden retriever eating something on a hardwood floor. [SEP]
hypo: [CLS] a pair of scissors and scissors and a table. [SEP]
refe: [CLS] a selection of scissors, including pinking shears, under a glass shelf. [SEP]
hypo: [CLS] a woman holding a pink using under an umbrella. [SEP]
refe: [CLS] a woman holds up an electronic cigarette underneath an umbrella. [SEP]
hypo: [CLS] a large clock tower front of a clock. [SEP]
refe: [CLS] a steeple with a bell tower and a clock with many windows. [SEP]
hypo: [CLS] two tour busses parked in a parking lot. [SEP]
refe: [CLS] two blue buses parked in front of a volvo store. [SEP]
hypo: [CLS] a soccer throwing a soccer on a ball. [SEP]
refe: [CLS] a soccer player runs up to kick the ball while the crowd watches. [SEP]
hypo: [CLS] two are benches that are benches park bench. [SEP]
refe: [CLS] a couple of creepy statues sitting on a bench in a park at night. [SEP]
hypo: [CLS] a woman in a tennis racquet. [SEP]
refe: [CLS] the female tennis player swings the racket overhead. [SEP]
hypo: [CLS] two giraffes are standing in a grassy field. [SEP]
refe: [CLS] a couple of giraffe playing around in a field of grass. [SEP]
hypo: [CLS] a bird perched being birds near a window. [SEP]
refe: [CLS] a bird on the outside of a door with clear windows [SEP]
hypo: [CLS] a woman in a baseball bat holding a ball. [SEP]
refe: [CLS] a woman dressed in uniform swinging a baseball bat. [SEP]
hypo: [CLS] a baseball game baseball players on a baseball game. [SEP]
refe: [CLS] a baseball game in progress, with many onlookers. [SEP]
hypo: [CLS] two truck are in the back of a car truck. [SEP]
refe: [CLS] a truck with a cage in the bed carries live animals on a highway. [SEP]
hypo: [CLS] a baseball player swinging a bat at a baseball game. [SEP]
refe: [CLS] a group of men in a field playing baseball. [SEP]
hypo: [CLS] a cutting board let laid differentni on top tools let squash fresh
refe: [CLS] various vegetables sit openly on the counter top. [SEP]
hypo: [CLS] a black and white cat laying on a couch. [SEP]
refe: [CLS] a long haired cat that is posing for a picture. [SEP]
hypo: [CLS] a man on a motorcycle on a motorcycle. [SEP]
refe: [CLS] a person in motorcycle apparel from the back, standing in front of a person in motorcycle apparel on a motorcycle from the side, on pave surface by water. [SEP]
hypo: [CLS] a baseball player swinging a bat at a baseball game. [SEP]
refe: [CLS] baseball player completing his batting swing at home plate [SEP]
hypo: [CLS] a man with a dog on a dog boat dog. [SEP]
refe: [CLS] a person that is floating in some water [SEP]
hypo: [CLS] a cutting board with carrot chopped cutting board multiple knife. [SEP]
refe: [CLS] carrots and cucumber on wooden cutting board near knives. [SEP]
hypo: [CLS] a donuts donuts somekin of a table. [SEP]
refe: [CLS] a cook book for making donuts with donuts and coffee pictures on it ' s cover. [SEP]
hypo: [CLS] a bathroom with a toilet in a toilet. [SEP]
refe: [CLS] a white toilet sitting underneath a window next to a tub. [SEP]
hypo: [CLS] a cat sitting on top of a park bench. [SEP]
refe: [CLS] a bird sitting on top of a bench and a cat sitting underneath it. [SEP]
hypo: [CLS] a woman in a kitchen with a kitchen. [SEP]
refe: [CLS] the frantic lady is checking her latest concoction. [SEP]

GRPO 強化学習の結果

検証データについての学習曲線

検証時の repeat_penalty、length_penalty * 1000, CIDEr、ROUGE-L、CLIP_score、および BERT_scoreの学習曲線を示す。

Screenshot from 2026-07-01 10-01-49.png

Screenshot from 2026-07-01 10-05-08.png

Screenshot from 2026-07-01 10-05-31.png

Screenshot from 2026-07-01 10-06-04.png

Screenshot from 2026-07-01 10-06-24.png

Screenshot from 2026-07-01 10-06-43.png

テストデータについての各スコア

cider           0.861
rougeL          0.526
clip_score      0.275
bert_score      0.833
repeat_count    0.240
length_penalty -1.34 e-3

生成されたキャプション

写真 1
Screenshot from 2026-07-01 09-55-21.png

hypo: [CLS] a dog that is sitting on the floor. [SEP]
refe: [CLS] a dog chewing on a stick on a hardwood floor. [SEP]

写真 2
Screenshot from 2026-07-01 09-57-15.png

hypo: [CLS] a close up topped with a sandwich. [SEP]
refe: [CLS] a plate with a couple of sandwiches on it [SEP]
hypo: [CLS] a dog that is sitting on the floor. [SEP]
refe: [CLS] a dog chewing on a stick on a hardwood floor. [SEP]
hypo: [CLS] a wooden crafts sci hung sitting on a table. [SEP]
refe: [CLS] pairs of scissors and a rubber mallet laid out on a table [SEP]
hypo: [CLS] a person standing in an open holding umbrella. [SEP]
refe: [CLS] a woman staying dry from the rain and holding an umbrella. [SEP]
hypo: [CLS] a large clock tower on top of a building. [SEP]
refe: [CLS] tall tower with a large clock in the center of the tower [SEP]
hypo: [CLS] a couple of parked in a parking lot. [SEP]
refe: [CLS] two blue buses parked in front of a volvo store. [SEP]
hypo: [CLS] a soccer throwing a soccer on the ball. [SEP]
refe: [CLS] a soccer player runs up to kick the ball while the crowd watches. [SEP]
hypo: [CLS] a couple of benches that are benches clock. [SEP]
refe: [CLS] a couple of creepy statues sitting on a bench in a park at night. [SEP]
hypo: [CLS] a woman in a tennis racquet. [SEP]
refe: [CLS] woman tennis player twisting both arms to snag the ball [SEP]
hypo: [CLS] two giraffes are standing in a grassy field. [SEP]
refe: [CLS] two very pretty giraffes together in a big grassy field. [SEP]
hypo: [CLS] a large sitting on top of a window. [SEP]
refe: [CLS] a bird on the outside of a door with clear windows [SEP]
hypo: [CLS] a woman that is ready ready at the ball. [SEP]
refe: [CLS] a female baseball player looks on with intensity while swinging the bat. [SEP]
hypo: [CLS] a group of people on wait home played. [SEP]
refe: [CLS] the pitcher just threw the pitch at the baseball game. [SEP]
hypo: [CLS] a couple of in the back of a car truck. [SEP]
refe: [CLS] a couple of animals are in the back of a truck [SEP]
hypo: [CLS] a baseball player swinging a bat while home. [SEP]
refe: [CLS] a baseball game is in action at the plate. [SEP]
hypo: [CLS] a view onionsdis least differentni on a table. [SEP]
refe: [CLS] there are some fresh vegetables on a cutting board [SEP]
hypo: [CLS] a black and white on a leatherless. [SEP]
refe: [CLS] a cat that has very long and white whiskers. [SEP]
hypo: [CLS] a man on a motorcycle on a motorcycle. [SEP]
refe: [CLS] a man sitting on a motorcycle as another man walks towards him. [SEP]
hypo: [CLS] a baseball player is ready at the ball. [SEP]
refe: [CLS] baseball player completing his batting swing at home plate [SEP]
hypo: [CLS] a man is sitting on top of water. [SEP]
refe: [CLS] a person that is floating in some water [SEP]
hypo: [CLS] a cutting board with carrot chopped cutting board. [SEP]
refe: [CLS] a close up of many different vegetables on a cutting board. [SEP]
hypo: [CLS] a close up doughnut and a table. [SEP]
refe: [CLS] a spanish advertisement for two sweet glazed donuts. [SEP]
hypo: [CLS] a bathroom with a toilet in the floor. [SEP]
refe: [CLS] a toilet that has cans of paint next to it. [SEP]
hypo: [CLS] a cat is sitting on a park bench. [SEP]
refe: [CLS] a bird perched on a park bench as a cat hides underneath. [SEP]
hypo: [CLS] a woman standing in a kitchen preparing oven. [SEP]
refe: [CLS] a person in a room in front of a stove. [SEP]

比較

強化学習により、repeat_count、 length_penalty が改善されている。各言語スコア、clip_score も、良好な値に保たれている。

強化学習のプログラムを GitHub に置いておきます。

ディレクトリは GRPO_suppl_compatible_transformers5 と COCO_SPT_compatible_transformers5 です。

訓練データは COCO train2017 です。

プログラムて注意すべき点。

  1. 独自に開発した GRPO 用の確率的 viterbi サンプリング関数を使っています。
  2. 画像キャプショニングモデルは、サンプリング、訓練、kl_div の参照と3回計算しています。それぞれ、top-k 近似を行っています。torch.topk 関数の実行は、サンプリング時のみです。訓練時と参照用には、サンプリング時の top_indicies を使って、beam_emission_scores を計算しています。
  3. log_probs から sampled_log_probs を求めるためのトークンパスも、全てサンプリング時のトークンパスを使っています。
  4. 参照用の log_probs の計算において、シーケンス i の log_probs の計算にシーケンス i-1 のトークンの情報が必要です。シーケンス i-1 のトークンもサンプリングで得られたトークンを用いています。関数 _compute_ref_probs。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?