File size: 2,661 Bytes
5ffc566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-BZ2HFB9OtWP"
      },
      "outputs": [],
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "%cd /content/drive/MyDrive/donut"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "background_save": true
        },
        "id": "SJpD4AAj7qeZ"
      },
      "outputs": [],
      "source": [
        "!pip install transformers==4.25.1\n",
        "!pip install timm==0.5.4\n",
        "!pip install donut-python"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from donut import DonutModel\n",
        "from PIL import Image\n",
        "import torch"
      ],
      "metadata": {
        "id": "PxFaO3rfDHQJ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Ro21MdJPSTZs"
      },
      "outputs": [],
      "source": [
        "# Copy one default config yaml file and amend to fit your use case.\n",
        "!python train.py --config ./config/train_Booking.yaml"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "J1ITHX4jV2Go"
      },
      "outputs": [],
      "source": [
        "# After train, you can evaluate and use the model.\n",
        "\n",
        "model = DonutModel.from_pretrained(\"/content/drive/MyDrive/donut/result/train_Booking/20240327_032854\")\n",
        "if torch.cuda.is_available():\n",
        "    model.half()\n",
        "    device = torch.device(\"cuda\")\n",
        "    model.to(device)\n",
        "else:\n",
        "    model.encoder.to(torch.bfloat16)\n",
        "\n",
        "model.eval()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2UhjFTmrWIrX"
      },
      "outputs": [],
      "source": [
        "image = Image.open(\"/content/COSCO_000.jpg\").convert(\"RGB\")\n",
        "with torch.no_grad():\n",
        "  # My dataset name is Booking , tag i.e. <s_Booking>\n",
        "  output = model.inference(image=image, prompt=\"<s_Booking>\")\n",
        "output"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "V100",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}