Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow packaging and running from a wheel #780

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
2 changes: 1 addition & 1 deletion manga_translator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
colorama.init(autoreset=True)
load_dotenv()

from .manga_translator import *
from manga_translator.manga_translator import *
17 changes: 9 additions & 8 deletions manga_translator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import logging
from argparse import Namespace

from manga_translator import Config
from manga_translator.config import Config
from manga_translator.args import parser, reparse
from .manga_translator import (
from manga_translator.manga_translator import (
set_main_logger, load_dictionary, apply_dictionary,
)
from .args import parser
from .utils import (
from manga_translator.utils import (
BASE_PATH,
init_logging,
get_logger,
Expand All @@ -21,7 +20,8 @@

async def dispatch(args: Namespace):
args_dict = vars(args)

global logger
logger = get_logger(args.mode)
logger.info(f'Running in {args.mode} mode')

if args.mode == 'local':
Expand Down Expand Up @@ -77,9 +77,7 @@ async def dispatch(args: Namespace):
config = Config.schema()
print(json.dumps(config, indent=2))



if __name__ == '__main__':
def main():
args = None
init_logging()
try:
Expand All @@ -98,3 +96,6 @@ async def dispatch(args: Namespace):
except Exception as e:
logger.error(f'{e.__class__.__name__}: {e}',
exc_info=e if args and args.verbose else None)

if __name__ == '__main__':
main()
6 changes: 0 additions & 6 deletions manga_translator/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
import os
from urllib.parse import unquote

from .detection import DETECTORS
from .ocr import OCRS
from .inpainting import INPAINTERS
from .translators import VALID_LANGUAGES, TRANSLATORS, TranslatorChain
from .upscaling import UPSCALERS
from .colorization import COLORIZERS
from .save import OUTPUT_FORMATS

def url_decode(s):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import argparse
import time


import numpy as np
import cv2
import torch
import torch.nn as nn
from torch.autograd import Variable
from .models import FFDNet
from .utils import normalize, variable_to_cv2_image, remove_dataparallel_wrapper, is_rgb
from .utils import normalize, variable_to_cv2_image, remove_dataparallel_wrapper

class FFDNetDenoiser:
def __init__(self, _device, _sigma = 25, _weights_dir = 'denoising/models/', _in_ch = 3):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import torch
import torch.nn as nn
import math

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as M
import math
from torch import Tensor
from torch.nn import Parameter

from .extractor import SEResNeXt_Origin, BottleneckX_Origin
Expand Down
4 changes: 2 additions & 2 deletions manga_translator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def __init__(self, string: str):
else:
translator = Translator[trans]
if translator not in TRANSLATORS:
raise ValueError(f'Invalid choice: %s (choose from %s)' % (trans, ', '.join(map(repr, TRANSLATORS))))
raise ValueError('Invalid choice: %s (choose from %s)' % (trans, ', '.join(map(repr, TRANSLATORS))))
if lang not in VALID_LANGUAGES:
raise ValueError(f'Invalid choice: %s (choose from %s)' % (lang, ', '.join(map(repr, VALID_LANGUAGES))))
raise ValueError('Invalid choice: %s (choose from %s)' % (lang, ', '.join(map(repr, VALID_LANGUAGES))))
self.chain.append((translator, lang))
self.translators, self.langs = list(zip(*self.chain))

Expand Down
11 changes: 3 additions & 8 deletions manga_translator/detection/craft.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@

import os
import shutil
import numpy as np
import torch
import cv2
import einops
from typing import List, Tuple

from .default_utils.DBNet_resnet34 import TextDetection as TextDetectionDefault
from .default_utils import imgproc, dbnet_utils, craft_utils
from .default_utils import imgproc, craft_utils
from .common import OfflineDetector
from ..utils import TextBlock, Quadrilateral, det_rearrange_forward
from shapely.geometry import Polygon, MultiPoint
from ..utils import Quadrilateral
from shapely.geometry import MultiPoint
from shapely import affinity

from .craft_utils.vgg16_bn import vgg16_bn, init_weights
Expand Down
2 changes: 0 additions & 2 deletions manga_translator/detection/craft_utils/refiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from .vgg16_bn import init_weights


Expand Down
2 changes: 1 addition & 1 deletion manga_translator/detection/ctd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .ctd_utils.utils.yolov5_utils import non_max_suppression
from .ctd_utils.utils.db_utils import SegDetectorRepresenter
from .ctd_utils.utils.imgproc_utils import letterbox
from .ctd_utils.textmask import REFINEMASK_INPAINT, refine_mask
from .ctd_utils.textmask import refine_mask
from .common import OfflineDetector
from ..utils import Quadrilateral, det_rearrange_forward

Expand Down
1 change: 0 additions & 1 deletion manga_translator/detection/ctd_utils/utils/io_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import os.path as osp
import glob
from pathlib import Path
Expand Down
11 changes: 0 additions & 11 deletions manga_translator/detection/ctd_utils/yolov5/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,12 @@
Common modules
"""

import json
import math
import platform
import warnings
from collections import OrderedDict, namedtuple
from copy import copy
from pathlib import Path

import cv2
import numpy as np
import requests
import torch
import torch.nn as nn
from PIL import Image
from torch.cuda import amp

from ..utils.yolov5_utils import make_divisible, initialize_weights, check_anchor_order, check_version, fuse_conv_and_bn

def autopad(k, p=None): # kernel, padding
# Pad to 'same'
Expand Down
2 changes: 0 additions & 2 deletions manga_translator/detection/ctd_utils/yolov5/yolo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from operator import mod
from cv2 import imshow
# from utils.yolov5_utils import scale_img
from copy import deepcopy
from .common import *
Expand Down
6 changes: 1 addition & 5 deletions manga_translator/detection/dbnet_convnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init

from torchvision.models import resnet34

import einops
import math

from timm.layers import trunc_normal_, AvgPool2dSame, DropPath, Mlp, GlobalResponseNormMlp, \
LayerNorm2d, LayerNorm, create_conv2d, get_act_layer, make_divisible, to_ntuple
Expand Down Expand Up @@ -493,7 +489,7 @@ def forward(self, x) :
import os
from .default_utils import imgproc, dbnet_utils, craft_utils
from .common import OfflineDetector
from ..utils import TextBlock, Quadrilateral, det_rearrange_forward
from ..utils import Quadrilateral, det_rearrange_forward

MODEL = None
def det_batch_forward_default(batch: np.ndarray, device: str):
Expand Down
3 changes: 1 addition & 2 deletions manga_translator/detection/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import torch
import cv2
import einops
from typing import List, Tuple

from .default_utils.DBNet_resnet34 import TextDetection as TextDetectionDefault
from .default_utils import imgproc, dbnet_utils, craft_utils
from .common import OfflineDetector
from ..utils import TextBlock, Quadrilateral, det_rearrange_forward
from ..utils import Quadrilateral, det_rearrange_forward

MODEL = None
def det_batch_forward_default(batch: np.ndarray, device: str):
Expand Down
2 changes: 0 additions & 2 deletions manga_translator/detection/default_utils/CRAFT_resnet34.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init

from torchvision.models import resnet34

import einops
import math

class ImageMultiheadSelfAttention(nn.Module):
def __init__(self, planes):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import torch
import torch.nn as nn
import torch.nn.functional as F

from torchvision.models import resnet101

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 1 addition & 5 deletions manga_translator/inpainting/booru_tagger.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import os
import gc
import pandas as pd
import numpy as np
from onnxruntime import InferenceSession
from typing import Tuple, List, Dict
from io import BytesIO
from typing import Dict
from PIL import Image

import cv2
from pathlib import Path

from tqdm import tqdm

def make_square(img, target_size):
old_size = img.shape[:2]
Expand Down
18 changes: 2 additions & 16 deletions manga_translator/inpainting/guided_ldm_inpainting.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@


import einops
import torch
import torch as th
import torch.nn as nn
import numpy as np
from tqdm import tqdm
import cv2

from .ldm.modules.diffusionmodules.util import (
conv_nd,
linear,
zero_module,
timestep_embedding,
)

from .ldm.modules.diffusionmodules.util import noise_like
from einops import rearrange, repeat
from torchvision.utils import make_grid
from .ldm.modules.attention import SpatialTransformer
from .ldm.modules.diffusionmodules.openaimodel import UNetModel, TimestepEmbedSequential, ResBlock, Downsample, AttentionBlock
from .ldm.models.diffusion.ddpm import LatentDiffusion
from .ldm.models.diffusion.ddim import DDIMSampler
from .ldm.util import log_txt_as_img, exists, instantiate_from_config
from .ldm.util import instantiate_from_config

class GuidedDDIMSample(DDIMSampler) :
def __init__(self, *args, **kwargs):
Expand Down
3 changes: 1 addition & 2 deletions manga_translator/inpainting/inpainting_lama.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, List[torch.Tensor]]:
Predict scores and get intermediate activations. Useful for feature matching loss
:return tuple (scores, list of intermediate activations)
"""
raise NotImplemented()
raise NotImplementedError()


def get_conv_block_ctor(kind='default'):
Expand Down Expand Up @@ -687,7 +687,6 @@ def get_discriminator():
}
return FFCNLayerDiscriminator(3, norm_layer = nn.Identity, init_conv_kwargs = init_conv_kwargs, conv_kwargs = conv_kwargs)

from torchsummary import summary

def test_model():
dis = get_generator()
Expand Down
6 changes: 3 additions & 3 deletions manga_translator/inpainting/ldm/models/diffusion/ddpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(self,
self.init_from_ckpt(ckpt_path, ignore_keys=ignore_keys, only_model=load_only_unet)
if reset_ema:
assert self.use_ema
print(f"Resetting ema to pure model weights. This is useful when restoring from an ema-only checkpoint.")
print("Resetting ema to pure model weights. This is useful when restoring from an ema-only checkpoint.")
self.model_ema = LitEma(self.model)
if reset_num_ema_updates:
print(" +++++++++++ WARNING: RESETTING NUM_EMA UPDATES TO ZERO +++++++++++ ")
Expand Down Expand Up @@ -227,7 +227,7 @@ def init_from_ckpt(self, path, ignore_keys=list(), only_model=False):
desc="Fitting old weights to new weights",
total=n_params
):
if not name in sd:
if name not in sd:
continue
old_shape = sd[name].shape
new_shape = param.shape
Expand Down Expand Up @@ -574,7 +574,7 @@ def __init__(self,
if reset_ema:
assert self.use_ema
print(
f"Resetting ema to pure model weights. This is useful when restoring from an ema-only checkpoint.")
"Resetting ema to pure model weights. This is useful when restoring from an ema-only checkpoint.")
self.model_ema = LitEma(self.model)
if reset_num_ema_updates:
print(" +++++++++++ WARNING: RESETTING NUM_EMA UPDATES TO ZERO +++++++++++ ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import torch
import torch.nn.functional as F
import math
from tqdm import tqdm

Expand Down
1 change: 0 additions & 1 deletion manga_translator/inpainting/ldm/models/diffusion/plms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import torch
import numpy as np
from tqdm import tqdm
from functools import partial

from ldm.modules.diffusionmodules.util import make_ddim_sampling_parameters, make_ddim_timesteps, noise_like
from ldm.models.diffusion.sampling_util import norm_thresholding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import torch
import numpy as np


def append_dims(x, target_dims):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# thanks!


import os
import math
import torch
import torch.nn as nn
Expand Down
4 changes: 2 additions & 2 deletions manga_translator/inpainting/ldm/modules/ema.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def forward(self, model):
shadow_params[sname] = shadow_params[sname].type_as(m_param[key])
shadow_params[sname].sub_(one_minus_decay * (shadow_params[sname] - m_param[key]))
else:
assert not key in self.m_name2s_name
assert key not in self.m_name2s_name

def copy_to(self, model):
m_param = dict(model.named_parameters())
Expand All @@ -54,7 +54,7 @@ def copy_to(self, model):
if m_param[key].requires_grad:
m_param[key].data.copy_(shadow_params[self.m_name2s_name[key]].data)
else:
assert not key in self.m_name2s_name
assert key not in self.m_name2s_name

def store(self, parameters):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from transformers import T5Tokenizer, T5EncoderModel, CLIPTokenizer, CLIPTextModel

import open_clip
from ldm.util import default, count_params
from ldm.util import count_params


class AbstractEncoder(nn.Module):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
_make_pretrained_vitb_rn50_384,
_make_pretrained_vitl16_384,
_make_pretrained_vitb16_384,
forward_vit,
)

def _make_encoder(backbone, features, use_pretrained, groups=1, expand=False, exportable=True, hooks=None, use_vit_only=False, use_readout="ignore",):
Expand Down
Loading
Loading