forked from lukas-blecher/LaTeX-OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lukas-blecher#150 from TITC/data-parallelism
Data parallelism【multi-gpu train】+pure ViT work + small modify
- Loading branch information
Showing
14 changed files
with
358 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,3 +138,5 @@ pix2tex/model/checkpoints/** | |
!**/.gitkeep | ||
.vscode | ||
.DS_Store | ||
test/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
gpu_devices: null #[0,1,2,3,4,5,6,7] | ||
betas: | ||
- 0.9 | ||
- 0.999 | ||
batchsize: 64 | ||
bos_token: 1 | ||
channels: 1 | ||
data: dataset/data/train.pkl | ||
debug: false | ||
decoder_args: | ||
attn_on_attn: true | ||
cross_attend: true | ||
ff_glu: true | ||
rel_pos_bias: false | ||
use_scalenorm: false | ||
dim: 256 | ||
emb_dropout: 0 | ||
encoder_depth: 4 | ||
eos_token: 2 | ||
epochs: 10 | ||
gamma: 0.9995 | ||
heads: 8 | ||
id: null | ||
load_chkpt: null | ||
lr: 0.0005 | ||
lr_step: 30 | ||
max_height: 192 | ||
max_seq_len: 512 | ||
max_width: 672 | ||
min_height: 32 | ||
min_width: 32 | ||
micro_batchsize: 64 | ||
model_path: checkpoints_add | ||
name: pix2tex-vit | ||
num_layers: 4 | ||
num_tokens: 8000 | ||
optimizer: Adam | ||
output_path: outputs | ||
pad: false | ||
pad_token: 0 | ||
patch_size: 16 | ||
sample_freq: 1000 | ||
save_freq: 5 | ||
scheduler: StepLR | ||
seed: 42 | ||
encoder_structure: vit | ||
temperature: 0.2 | ||
test_samples: 5 | ||
testbatchsize: 20 | ||
tokenizer: dataset/tokenizer.json | ||
valbatches: 100 | ||
valdata: dataset/data/val.pkl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .utils import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import torch | ||
import torch.nn as nn | ||
|
||
from timm.models.vision_transformer import VisionTransformer | ||
from timm.models.vision_transformer_hybrid import HybridEmbed | ||
from timm.models.resnetv2 import ResNetV2 | ||
from timm.models.layers import StdConv2dSame | ||
from einops import repeat | ||
|
||
class CustomVisionTransformer(VisionTransformer): | ||
def __init__(self, img_size=224, patch_size=16, *args, **kwargs): | ||
super(CustomVisionTransformer, self).__init__(img_size=img_size, patch_size=patch_size, *args, **kwargs) | ||
self.height, self.width = img_size | ||
self.patch_size = patch_size | ||
|
||
def forward_features(self, x): | ||
B, c, h, w = x.shape | ||
x = self.patch_embed(x) | ||
|
||
cls_tokens = self.cls_token.expand(B, -1, -1) # stole cls_tokens impl from Phil Wang, thanks | ||
x = torch.cat((cls_tokens, x), dim=1) | ||
h, w = h//self.patch_size, w//self.patch_size | ||
pos_emb_ind = repeat(torch.arange(h)*(self.width//self.patch_size-w), 'h -> (h w)', w=w)+torch.arange(h*w) | ||
pos_emb_ind = torch.cat((torch.zeros(1), pos_emb_ind+1), dim=0).long() | ||
x += self.pos_embed[:, pos_emb_ind] | ||
#x = x + self.pos_embed | ||
x = self.pos_drop(x) | ||
|
||
for blk in self.blocks: | ||
x = blk(x) | ||
|
||
x = self.norm(x) | ||
return x | ||
|
||
|
||
def get_encoder(args): | ||
backbone = ResNetV2( | ||
layers=args.backbone_layers, num_classes=0, global_pool='', in_chans=args.channels, | ||
preact=False, stem_type='same', conv_layer=StdConv2dSame) | ||
min_patch_size = 2**(len(args.backbone_layers)+1) | ||
|
||
def embed_layer(**x): | ||
ps = x.pop('patch_size', min_patch_size) | ||
assert ps % min_patch_size == 0 and ps >= min_patch_size, 'patch_size needs to be multiple of %i with current backbone configuration' % min_patch_size | ||
return HybridEmbed(**x, patch_size=ps//min_patch_size, backbone=backbone) | ||
|
||
encoder = CustomVisionTransformer(img_size=(args.max_height, args.max_width), | ||
patch_size=args.patch_size, | ||
in_chans=args.channels, | ||
num_classes=0, | ||
embed_dim=args.dim, | ||
depth=args.encoder_depth, | ||
num_heads=args.heads, | ||
embed_layer=embed_layer | ||
) | ||
return encoder |
Oops, something went wrong.