Skip to content

Commit 2f771ac

Browse files
Fix InceptionV3 and add Codecov (#20)
* Fix `InceptionV3` * Update README * Add Codecov * Update Codecov * Add Codecov badge
1 parent b3b8219 commit 2f771ac

File tree

6 files changed

+59
-12
lines changed

6 files changed

+59
-12
lines changed

.github/workflows/actions.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ jobs:
4545
- name: Test with pytest
4646
run: |
4747
pytest
48+
coverage xml -o coverage.xml
49+
- name: Upload coverage reports to Codecov
50+
uses: codecov/codecov-action@v3
51+
with:
52+
token: ${{ secrets.CODECOV_TOKEN }}
53+
files: coverage.xml
54+
flags: kimm,kimm-${{ matrix.backend }}
4855

4956
format:
5057
name: Check the code format

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1+
<!-- markdownlint-disable MD033 -->
2+
<!-- markdownlint-disable MD041 -->
3+
14
# Keras Image Models
25

6+
<div align="center">
7+
<img width="50%" src="docs/banner/kimm.png" alt="KIMM">
8+
9+
[![PyPI](https://img.shields.io/pypi/v/kimm)](https://pypi.org/project/kimm/)
10+
[![Contributions Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/james77777778/kimm/issues)
11+
[![codecov](https://codecov.io/gh/james77777778/kimm/graph/badge.svg?token=eEha1SR80D)](https://codecov.io/gh/james77777778/kimm)
12+
</div>
13+
314
## Description
415

516
**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.

docs/banner/kimm.png

12.4 KB
Loading

kimm/models/inception_v3.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,14 @@ def __init__(
299299
):
300300
kwargs = self.fix_config(kwargs)
301301
if weights == "imagenet":
302-
has_aux_logits = False
303-
file_name = "inceptionv3_inception_v3.gluon_in1k.keras"
302+
if has_aux_logits:
303+
file_name = (
304+
"inceptionv3_inception_v3.gluon_in1k_aux_logits.keras"
305+
)
306+
else:
307+
file_name = (
308+
"inceptionv3_inception_v3.gluon_in1k_no_aux_logits.keras"
309+
)
304310
kwargs["weights_url"] = f"{self.default_origin}/{file_name}"
305311
super().__init__(
306312
has_aux_logits,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tests = [
3333
"black",
3434
"pytest",
3535
"pytest-cov",
36+
"coverage",
3637
]
3738
examples = ["opencv-python", "matplotlib"]
3839

tools/convert_inception_v3_from_timm.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,36 @@
1717

1818
timm_model_names = [
1919
"inception_v3.gluon_in1k",
20+
"inception_v3.gluon_in1k",
2021
]
2122
keras_model_classes = [
2223
inception_v3.InceptionV3,
24+
inception_v3.InceptionV3,
2325
]
26+
has_aux_logits_list = [True, False]
2427

25-
for timm_model_name, keras_model_class in zip(
26-
timm_model_names, keras_model_classes
28+
for timm_model_name, keras_model_class, has_aux_logits in zip(
29+
timm_model_names,
30+
keras_model_classes,
31+
has_aux_logits_list,
2732
):
2833
"""
2934
Prepare timm model and keras model
3035
"""
3136
input_shape = [299, 299, 3]
3237
torch_model = timm.create_model(
33-
timm_model_name, pretrained=True, aux_logits=False
38+
timm_model_name, pretrained=True, aux_logits=has_aux_logits
3439
)
3540
torch_model = torch_model.eval()
3641
trainable_state_dict, non_trainable_state_dict = separate_torch_state_dict(
3742
torch_model.state_dict()
3843
)
3944
keras_model = keras_model_class(
40-
has_aux_logits=False,
45+
has_aux_logits=has_aux_logits,
4146
input_shape=input_shape,
4247
include_preprocessing=False,
4348
classifier_activation="linear",
49+
weights=None,
4450
)
4551
trainable_weights, non_trainable_weights = separate_keras_weights(
4652
keras_model
@@ -129,17 +135,33 @@
129135
np.random.seed(2023)
130136
keras_data = np.random.uniform(size=[1] + input_shape).astype("float32")
131137
torch_data = torch.from_numpy(np.transpose(keras_data, [0, 3, 1, 2]))
132-
torch_y = torch_model(torch_data)
133-
keras_y = keras_model(keras_data, training=False)
134-
torch_y = torch_y.detach().cpu().numpy()
135-
keras_y = keras.ops.convert_to_numpy(keras_y)
136-
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
138+
if has_aux_logits:
139+
torch_y = torch_model(torch_data)[0]
140+
keras_y = keras_model(keras_data, training=False)[0]
141+
torch_y = torch_y.detach().cpu().numpy()
142+
keras_y = keras.ops.convert_to_numpy(keras_y)
143+
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
144+
else:
145+
torch_y = torch_model(torch_data)
146+
keras_y = keras_model(keras_data, training=False)
147+
torch_y = torch_y.detach().cpu().numpy()
148+
keras_y = keras.ops.convert_to_numpy(keras_y)
149+
np.testing.assert_allclose(torch_y, keras_y, atol=1e-5)
137150
print(f"{keras_model_class.__name__}: output matched!")
138151

139152
"""
140153
Save converted model
141154
"""
142155
os.makedirs("exported", exist_ok=True)
143-
export_path = f"exported/{keras_model.name.lower()}_{timm_model_name}.keras"
156+
if has_aux_logits:
157+
export_path = (
158+
f"exported/{keras_model.name.lower()}_{timm_model_name}_"
159+
"aux_logits.keras"
160+
)
161+
else:
162+
export_path = (
163+
f"exported/{keras_model.name.lower()}_{timm_model_name}_"
164+
"no_aux_logits.keras"
165+
)
144166
keras_model.save(export_path)
145167
print(f"Export to {export_path}")

0 commit comments

Comments
 (0)