Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
leondgarse committed May 19, 2024
1 parent 1ba3b1c commit a1da3b7
Show file tree
Hide file tree
Showing 16 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions keras_cv_attention_models/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
try:
from importlib.metadata import version

tf_version = version('tensorflow').split(".")
tf_version = version("tensorflow").split(".")
try:
keras_version = version('keras').split(".")
keras_version = version("keras").split(".")
except:
keras_version = tf_version

try:
tf_keras_version = version('tf_keras').split(".")
tf_keras_version = version("tf_keras").split(".")
except:
tf_keras_version = keras_version

Expand Down
1 change: 1 addition & 0 deletions keras_cv_attention_models/clip/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Copied from https://github.com/mlfoundations/open_clip/blob/main/src/open_clip/tokenizer.py
Copied from https://github.com/openai/CLIP. Originally MIT License, Copyright (c) 2021 OpenAI.
"""

import os
import html
from functools import lru_cache
Expand Down
2 changes: 1 addition & 1 deletion keras_cv_attention_models/coco/eval_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __decode_single__(
self.__init_anchor__(input_shape)

if self.num_masks > 0: # Segmentation masks
pred, masks = pred[:, :-self.num_masks], pred[:, -self.num_masks:]
pred, masks = pred[:, : -self.num_masks], pred[:, -self.num_masks :]
else:
masks = None

Expand Down
5 changes: 2 additions & 3 deletions keras_cv_attention_models/coco/torch_losses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Just copied from ultralytics tal.py / Loss function / BboxLoss
"""

import torch
import math
from torch import nn
Expand Down Expand Up @@ -30,9 +31,7 @@ def __init__(self, device="cpu", stride=[8, 16, 32], nc=80, reg_max=16, input_sh
self.proj = torch.arange(reg_max, dtype=torch.float, device=self.device)

def get_feature_sizes(self):
feature_sizes = [
(math.ceil(self.input_shape[-2] / (2**ii)), math.ceil(self.input_shape[-1] / (2**ii))) for ii in range(3, 3 + self.num_pyramid_levels)
]
feature_sizes = [(math.ceil(self.input_shape[-2] / (2**ii)), math.ceil(self.input_shape[-1] / (2**ii))) for ii in range(3, 3 + self.num_pyramid_levels)]
feature_lens = [ii[0] * ii[1] for ii in feature_sizes]
return feature_sizes, feature_lens

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Creates an EfficientNet-EdgeTPU model
Ref impl: https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/edgetpu
"""

import math
from keras_cv_attention_models.efficientnet.efficientnet_v2 import EfficientNetV2
from keras_cv_attention_models.attention_layers import make_divisible
Expand Down
1 change: 1 addition & 0 deletions keras_cv_attention_models/efficientnet/efficientnet_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Creates a EfficientNetV1 Model as defined in: EfficientNetV1: Self-training with Noisy Student improves ImageNet classification.
arXiv preprint arXiv:1911.04252.
"""

import math
from keras_cv_attention_models.efficientnet.efficientnet_v2 import EfficientNetV2
from keras_cv_attention_models.models import register_model
Expand Down
1 change: 1 addition & 0 deletions keras_cv_attention_models/efficientnet/efficientnet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Creates a EfficientNetV2 Model as defined in: Mingxing Tan, Quoc V. Le. (2021). arXiv preprint arXiv:2104.00298.
EfficientNetV2: Smaller Models and Faster Training.
"""

import numpy as np
from keras_cv_attention_models import backend
from keras_cv_attention_models.backend import layers, functional, models, is_channels_last
Expand Down
2 changes: 1 addition & 1 deletion keras_cv_attention_models/gpt2/gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def call(self, inputs):
else:
causal_mask = self.causal_mask
qq_len, kk_len = (functional.shape(inputs)[2], functional.shape(inputs)[3]) if backend.is_tensorflow_backend else (inputs.shape[2], inputs.shape[3])
return (inputs + causal_mask[:, :, :qq_len, :kk_len])
return inputs + causal_mask[:, :, :qq_len, :kk_len]

def compute_output_shape(self, input_shape):
return input_shape[0] if self.is_kv_cache else input_shape
Expand Down
4 changes: 3 additions & 1 deletion keras_cv_attention_models/imagenet/augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copied from: https://github.com/tensorflow/models/blob/master/official/vision/image_classification/augment.py
Midified according to: https://github.com/rwightman/pytorch-image-models/blob/master/timm/data/auto_augment.py
"""

# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -724,7 +725,7 @@ def select_and_apply_random_policy(policies: Any, image: tf.Tensor):
policy_to_select = tf.random.uniform([], maxval=len(policies), dtype=tf.int32)
# Note that using tf.case instead of tf.conds would result in significantly
# larger graphs and would even break export for some larger policies.
for (i, policy) in enumerate(policies):
for i, policy in enumerate(policies):
image = tf.cond(tf.equal(i, policy_to_select), lambda selected_policy=policy: selected_policy(image), lambda: image)
return image

Expand Down Expand Up @@ -927,6 +928,7 @@ def __call__(self, image: tf.Tensor) -> tf.Tensor:
for policy_info in policy:
policy_info = list(policy_info) + [replace_value, self.cutout_const, self.translate_const]
tf_policy.append(_parse_policy_info(*policy_info))

# Now build the tf policy that will apply the augmentation procedue
# on image.
def make_final_policy(tf_policy_):
Expand Down
2 changes: 1 addition & 1 deletion keras_cv_attention_models/imagenet/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def set_model(self, model):
param.requires_grad_(False)
self.basic_save_name = model.name if self.basic_save_name is None else self.basic_save_name
self.save_file_path = os.path.join(self.save_path, self.basic_save_name) + "_ema.h5"
self.ema.history = namedtuple('history', ['history'])({}) # Record history and pass back to model if any
self.ema.history = namedtuple("history", ["history"])({}) # Record history and pass back to model if any
self.enabled = True

def on_train_batch_end(self, batch, logs=None):
Expand Down
4 changes: 2 additions & 2 deletions keras_cv_attention_models/imagenet/train_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def set_random_seed(seed):
tf.random.set_seed(seed)
tf.experimental.numpy.random.seed(seed)
# When running on the CuDNN backend, two further options must be set
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
os.environ['TF_DETERMINISTIC_OPS'] = '1'
os.environ["TF_CUDNN_DETERMINISTIC"] = "1"
os.environ["TF_DETERMINISTIC_OPS"] = "1"


def init_global_strategy(enable_float16=True, seed=0, TPU=False):
Expand Down
5 changes: 3 additions & 2 deletions keras_cv_attention_models/llama2/llama2.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def call(self, inputs, **kwargs):
cur_tensor, start_pos = inputs, 0
left, right = functional.unstack(cur_tensor, axis=-2)
seq_len = functional.shape(left)[-3] if backend.is_tensorflow_backend else left.shape[-3]
pos_cos, pos_sin = self.pos_cos[start_pos:start_pos + seq_len], self.pos_sin[start_pos:start_pos + seq_len]
pos_cos, pos_sin = self.pos_cos[start_pos : start_pos + seq_len], self.pos_sin[start_pos : start_pos + seq_len]
out = functional.stack([left * pos_cos - right * pos_sin, right * pos_cos + left * pos_sin], axis=-2)
return out

Expand Down Expand Up @@ -93,6 +93,7 @@ class KVCache(layers.Layer):
print(f"{np.allclose(aa.cache[:1, :4], out) = }")
# np.allclose(aa.cache[:1, :4], out) = True
"""

def __init__(self, max_batch_size=32, max_seq_len=2048, **kwargs):
super().__init__(**kwargs)
self.max_batch_size, self.max_seq_len = max_batch_size, max_seq_len
Expand All @@ -113,7 +114,7 @@ def call(self, inputs):
else:
batch_size, seq_len = functional.shape(cur_tensor)[0], functional.shape(cur_tensor)[1]
tensor_with_cache = functional.concat([self.cache[:batch_size, :start_pos], cur_tensor], axis=1)
cur_batch_cache = functional.concat([tensor_with_cache, self.cache[:batch_size, start_pos + seq_len:]], axis=1)
cur_batch_cache = functional.concat([tensor_with_cache, self.cache[:batch_size, start_pos + seq_len :]], axis=1)
self.cache.assign(functional.concat([cur_batch_cache, self.cache[batch_size:]], axis=0))
return tensor_with_cache

Expand Down
2 changes: 1 addition & 1 deletion keras_cv_attention_models/pytorch_backend/initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def _to_dtype_(tensor, dtype=None):
if dtype is None:
return tensor

default_dtype = str(torch.get_default_dtype()).split('.')[-1] # torch.float32 -> "float32"
default_dtype = str(torch.get_default_dtype()).split(".")[-1] # torch.float32 -> "float32"
if dtype == default_dtype:
return tensor
return tensor.to(dtype=getattr(torch, dtype, torch.get_default_dtype()))
Expand Down
2 changes: 1 addition & 1 deletion keras_cv_attention_models/pytorch_backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def train_step(self, xx, yy):
def fit(self, x=None, y=None, batch_size=32, epochs=1, callbacks=None, validation_data=None, initial_epoch=0, validation_batch_size=None, **kwargs):
callbacks = callbacks or []
[ii.set_model(self) for ii in callbacks if ii.model is None]
self.history = namedtuple('history', ['history', 'epoch'])({"loss": []}, []) # Mimic of `keras.callbacks.History`
self.history = namedtuple("history", ["history", "epoch"])({"loss": []}, []) # Mimic of `keras.callbacks.History`
validation_batch_size = validation_batch_size or batch_size
self.batch_size, self.callbacks, self.validation_batch_size = batch_size, callbacks, validation_batch_size
self.accumulate_passed_batches = 0
Expand Down
1 change: 1 addition & 0 deletions keras_cv_attention_models/volo/volo.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ def __init__(self, scale=2, beta=1.0, **kwargs):

def call(self, inputs, training=None, **kwargs):
height, width = functional.shape(inputs)[1], functional.shape(inputs)[2]

# tf.print("training:", training)
def _call_train():
return functional.stack(self.rand_bbox(height, width))
Expand Down
1 change: 1 addition & 0 deletions keras_cv_attention_models/yolov8/train.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np


class ModelEMA:
"""Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models
Keeps a moving average of everything in the model state_dict (parameters and buffers)
Expand Down

0 comments on commit a1da3b7

Please sign in to comment.