CoolFace
Modelpublic

alakxender/dhivehi-nougat-base

sourceHugging Facecc-by-4.0updated 2y agoView on Hugging Face
0likes19downloads
Model Card

DHIVEHI NOUGAT BASE (IMAGE-TO-TEXT)

This model is a fine-tuned version of facebook/nougat-base on the None dataset. It achieves the following results on the evaluation set:

  • —Loss: 0.0142

Model description

Finetuned dhivehi on text-image dataset, config all

Usage

python
from PIL import Image
import torch
from transformers import NougatProcessor, VisionEncoderDecoderModel
from pathlib import Path

# Load the model and processor
processor = NougatProcessor.from_pretrained("alakxender/dhivehi-nougat-base")
model = VisionEncoderDecoderModel.from_pretrained(
    "alakxender/dhivehi-nougat-base",  
    torch_dtype=torch.bfloat16,                 # Optional: Load the model with BF16 data type for faster inference and lower memory usage
    attn_implementation={                       # Optional: Specify the attention kernel implementations for different parts of the model
        "decoder": "flash_attention_2",         # Use FlashAttention-2 for the decoder for improved performance
        "encoder": "eager"                      # Use the default ("eager") attention implementation for the encoder
    }
)

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

context_length = 128

def predict(img_path):
    # Ensure image is in RGB format
    image = Image.open(img_path).convert("RGB")  
    pixel_values = processor(image, return_tensors="pt").pixel_values.to(torch.bfloat16)

    # generate prediction
    outputs = model.generate(
        pixel_values.to(device),
        min_length=1,
        max_new_tokens=context_length,
        repetition_penalty=1.5,
        bad_words_ids=[[processor.tokenizer.unk_token_id]],
        eos_token_id=processor.tokenizer.eos_token_id,
    )

    page_sequence = processor.batch_decode(outputs, skip_special_tokens=True)[0]
    return page_sequence

print(predict("DV01-04_31.jpg"))

Training procedure

Training hyperparameters

The following hyperparameters were used during training:

  • —learning_rate: 0.0001
  • —trainbatchsize: 3
  • —evalbatchsize: 3
  • —seed: 42
  • —gradientaccumulationsteps: 6
  • —totaltrainbatch_size: 18
  • —optimizer: Use adamwtorch with betas=(0.9,0.999) and epsilon=1e-08 and optimizerargs=No additional optimizer arguments
  • —lrschedulertype: linear
  • —num_epochs: 100

Training results

Training LossEpochStepValidation Loss
6.44040.00571001.0417
5.77610.01142000.9055
5.17230.01713000.8193
4.83150.02284000.7661
4.42170.02855000.7232
3.98610.03426000.6724
3.72680.04007000.5966
3.53930.04578000.5337
2.86660.05149000.4108
2.02690.057110000.2803
1.41210.062811000.1904
1.01610.068512000.1351
0.8670.074213000.1130
0.75060.079914000.0950
0.57640.085615000.0801
0.51230.091316000.0716
0.5580.097017000.0650
0.52420.102718000.0616
0.42290.108419000.0556
0.37210.114220000.0545
0.33880.119921000.0519
0.40420.125622000.0499
0.35930.131323000.0449
0.38370.137024000.0421
0.32910.142725000.0407
0.30920.148426000.0388
0.27620.154127000.0380
0.30730.159828000.0422
0.25770.165529000.0340
0.25960.171230000.0331
0.33970.176931000.0328
0.30190.182632000.0307
0.25220.188433000.0314
0.25460.194134000.0289
0.19720.199835000.0282
0.22310.205536000.0300
0.23420.211237000.0278
0.21520.216938000.0276
0.20590.222639000.0260
0.21650.228340000.0257
0.19190.234041000.0253
0.16080.239742000.0244
0.16730.245443000.0242
0.20040.251144000.0248
0.22770.256845000.0230
0.18310.262546000.0228
0.19050.268347000.0221
0.09960.274048000.0215
0.15960.279749000.0213
0.1680.285450000.0208
0.21190.291151000.0215
0.14360.296852000.0202
0.16560.302553000.0202
0.11830.308254000.0194
0.13970.313955000.0202
0.12480.319656000.0191
0.12020.325357000.0191
0.11750.331058000.0207
0.14270.336759000.0183
0.14870.342560000.0178
0.15970.348261000.0174
0.13630.353962000.0172
0.12660.359663000.0171
0.12880.365364000.0170
0.12020.371065000.0170
0.11740.376766000.0164
0.13340.382467000.0168
0.16270.388168000.0164
0.09820.393869000.0161
0.10380.399570000.0160
0.15230.405271000.0160
0.13370.410972000.0157
0.20630.416773000.0153
0.14760.422474000.0156
0.08380.428175000.0150
0.0820.433876000.0158
0.12690.439577000.0159
0.11680.445278000.0147
0.10240.450979000.0147
0.11380.456680000.0145
0.11880.462381000.0146
0.08810.468082000.0142
0.07520.473783000.0138
0.11650.479484000.0141
0.10170.485185000.0137
0.09710.490986000.0135
0.1350.496687000.0136
0.07320.502388000.0137
0.12170.508089000.0142

Framework versions

  • —Transformers 4.47.0
  • —Pytorch 2.6.0+cu124
  • —Datasets 3.2.0
  • —Tokenizers 0.21.0