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

Fix InceptionV3 and add Codecov #20

Merged
merged 5 commits into from
Jan 20, 2024
Merged
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
7 changes: 7 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ jobs:
- name: Test with pytest
run: |
pytest
coverage xml -o coverage.xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.xml
flags: kimm,kimm-${{ matrix.backend }}

format:
name: Check the code format
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<!-- markdownlint-disable MD033 -->
<!-- markdownlint-disable MD041 -->

# Keras Image Models

<div align="center">
<img width="50%" src="docs/banner/kimm.png" alt="KIMM">

[![PyPI](https://img.shields.io/pypi/v/kimm)](https://pypi.org/project/kimm/)
[![Contributions Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/james77777778/kimm/issues)
[![codecov](https://codecov.io/gh/james77777778/kimm/graph/badge.svg?token=eEha1SR80D)](https://codecov.io/gh/james77777778/kimm)
</div>

## Description

**K**eras **Im**age **M**odels (`kimm`) is a collection of image models, blocks and layers written in Keras 3. The goal is to offer SOTA models with pretrained weights in a user-friendly manner.
Expand Down
Binary file added docs/banner/kimm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions kimm/models/inception_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,14 @@ def __init__(
):
kwargs = self.fix_config(kwargs)
if weights == "imagenet":
has_aux_logits = False
file_name = "inceptionv3_inception_v3.gluon_in1k.keras"
if has_aux_logits:
file_name = (
"inceptionv3_inception_v3.gluon_in1k_aux_logits.keras"
)
else:
file_name = (
"inceptionv3_inception_v3.gluon_in1k_no_aux_logits.keras"
)
kwargs["weights_url"] = f"{self.default_origin}/{file_name}"
super().__init__(
has_aux_logits,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tests = [
"black",
"pytest",
"pytest-cov",
"coverage",
]
examples = ["opencv-python", "matplotlib"]

Expand Down
42 changes: 32 additions & 10 deletions tools/convert_inception_v3_from_timm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,36 @@

timm_model_names = [
"inception_v3.gluon_in1k",
"inception_v3.gluon_in1k",
]
keras_model_classes = [
inception_v3.InceptionV3,
inception_v3.InceptionV3,
]
has_aux_logits_list = [True, False]

for timm_model_name, keras_model_class in zip(
timm_model_names, keras_model_classes
for timm_model_name, keras_model_class, has_aux_logits in zip(
timm_model_names,
keras_model_classes,
has_aux_logits_list,
):
"""
Prepare timm model and keras model
"""
input_shape = [299, 299, 3]
torch_model = timm.create_model(
timm_model_name, pretrained=True, aux_logits=False
timm_model_name, pretrained=True, aux_logits=has_aux_logits
)
torch_model = torch_model.eval()
trainable_state_dict, non_trainable_state_dict = separate_torch_state_dict(
torch_model.state_dict()
)
keras_model = keras_model_class(
has_aux_logits=False,
has_aux_logits=has_aux_logits,
input_shape=input_shape,
include_preprocessing=False,
classifier_activation="linear",
weights=None,
)
trainable_weights, non_trainable_weights = separate_keras_weights(
keras_model
Expand Down Expand Up @@ -129,17 +135,33 @@
np.random.seed(2023)
keras_data = np.random.uniform(size=[1] + input_shape).astype("float32")
torch_data = torch.from_numpy(np.transpose(keras_data, [0, 3, 1, 2]))
torch_y = torch_model(torch_data)
keras_y = keras_model(keras_data, training=False)
torch_y = torch_y.detach().cpu().numpy()
keras_y = keras.ops.convert_to_numpy(keras_y)
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
if has_aux_logits:
torch_y = torch_model(torch_data)[0]
keras_y = keras_model(keras_data, training=False)[0]
torch_y = torch_y.detach().cpu().numpy()
keras_y = keras.ops.convert_to_numpy(keras_y)
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
else:
torch_y = torch_model(torch_data)
keras_y = keras_model(keras_data, training=False)
torch_y = torch_y.detach().cpu().numpy()
keras_y = keras.ops.convert_to_numpy(keras_y)
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
print(f"{keras_model_class.__name__}: output matched!")

"""
Save converted model
"""
os.makedirs("exported", exist_ok=True)
export_path = f"exported/{keras_model.name.lower()}_{timm_model_name}.keras"
if has_aux_logits:
export_path = (
f"exported/{keras_model.name.lower()}_{timm_model_name}_"
"aux_logits.keras"
)
else:
export_path = (
f"exported/{keras_model.name.lower()}_{timm_model_name}_"
"no_aux_logits.keras"
)
keras_model.save(export_path)
print(f"Export to {export_path}")